• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_DAUDIO_SOURCE_DEV_H
17 #define OHOS_DAUDIO_SOURCE_DEV_H
18 
19 #include <map>
20 #include <mutex>
21 #include <initializer_list>
22 #include "nlohmann/json.hpp"
23 
24 #include "event_handler.h"
25 
26 #include "audio_event.h"
27 #include "daudio_io_dev.h"
28 #include "daudio_source_dev_ctrl_mgr.h"
29 #include "daudio_source_mgr_callback.h"
30 #include "dmic_dev.h"
31 #include "dspeaker_dev.h"
32 #include "iaudio_event_callback.h"
33 #include "iaudio_data_transport.h"
34 #include "iaudio_datatrans_callback.h"
35 #include "idaudio_ipc_callback.h"
36 #include "idaudio_hdi_callback.h"
37 
38 using json = nlohmann::json;
39 
40 namespace OHOS {
41 namespace DistributedHardware {
42 class DAudioSourceDev : public IAudioEventCallback, public std::enable_shared_from_this<DAudioSourceDev> {
43 public:
44     DAudioSourceDev(const std::string &devId, const std::shared_ptr<DAudioSourceMgrCallback> &callback);
45     ~DAudioSourceDev() override = default;
46 
47     int32_t AwakeAudioDev();
48     void SleepAudioDev();
49 
50     int32_t EnableDAudio(const std::string &dhId, const std::string &attrs);
51     int32_t DisableDAudio(const std::string &dhId);
52     int32_t RestoreThreadStatus();
53     void SetThreadStatusFlag();
54     bool GetThreadStatusFlag();
55     void NotifyEvent(const AudioEvent &event) override;
56 
57 private:
58     int32_t EnableDSpeaker(const int32_t dhId, const std::string &attrs);
59     int32_t EnableDMic(const int32_t dhId, const std::string &attrs);
60     int32_t DisableDSpeaker(const int32_t dhId);
61     int32_t DisableDMic(const int32_t dhId);
62 
63     int32_t TaskEnableDAudio(const std::string &args);
64     int32_t TaskDisableDAudio(const std::string &args);
65     int32_t TaskOpenDSpeaker(const std::string &args);
66     int32_t OpenDSpeakerInner(std::shared_ptr<DAudioIoDev> &speaker, const int32_t dhId);
67     int32_t TaskCloseDSpeaker(const std::string &args);
68     int32_t TaskOpenDMic(const std::string &args);
69     int32_t TaskCloseDMic(const std::string &args);
70     int32_t TaskDMicClosed(const std::string &args);
71     int32_t TaskSetVolume(const std::string &args);
72     int32_t TaskChangeVolume(const std::string &args);
73     int32_t TaskChangeFocus(const std::string &args);
74     int32_t TaskChangeRenderState(const std::string &args);
75     int32_t TaskPlayStatusChange(const std::string &args);
76     int32_t TaskSpkMmapStart(const std::string &args);
77     int32_t TaskSpkMmapStop(const std::string &args);
78     int32_t TaskMicMmapStart(const std::string &args);
79     int32_t TaskMicMmapStop(const std::string &args);
80 
81     void OnDisableTaskResult(int32_t resultCode, const std::string &result, const std::string &funcName);
82     void OnEnableTaskResult(int32_t resultCode, const std::string &result, const std::string &funcName);
83     void OnTaskResult(int32_t resultCode, const std::string &result, const std::string &funcName);
84 
85     int32_t HandleOpenDSpeaker(const AudioEvent &event);
86     int32_t HandleCloseDSpeaker(const AudioEvent &event);
87     int32_t HandleDSpeakerOpened(const AudioEvent &event);
88     int32_t HandleDSpeakerClosed(const AudioEvent &event);
89     int32_t HandleOpenDMic(const AudioEvent &event);
90     int32_t HandleCloseDMic(const AudioEvent &event);
91     int32_t HandleDMicOpened(const AudioEvent &event);
92     int32_t HandleDMicClosed(const AudioEvent &event);
93     int32_t HandleCtrlTransClosed(const AudioEvent &event);
94     int32_t HandleNotifyRPC(const AudioEvent &event);
95     int32_t WaitForRPC(const AudioEventType type);
96     int32_t HandleVolumeSet(const AudioEvent &event);
97     int32_t HandleVolumeChange(const AudioEvent &event);
98     int32_t HandleFocusChange(const AudioEvent &event);
99     int32_t HandleRenderStateChange(const AudioEvent &event);
100     int32_t HandlePlayStatusChange(const AudioEvent &event);
101     int32_t HandleSpkMmapStart(const AudioEvent &event);
102     int32_t HandleSpkMmapStop(const AudioEvent &event);
103     int32_t HandleMicMmapStart(const AudioEvent &event);
104     int32_t HandleMicMmapStop(const AudioEvent &event);
105 
106     int32_t NotifySinkDev(const AudioEventType type, const json Param, const std::string dhId);
107     int32_t NotifyHDF(const AudioEventType type, const std::string result, const int32_t dhId);
108     AudioEventType getEventTypeFromArgs(const std::string &args);
109     void to_json(json &j, const AudioParam &param);
110     int32_t SendAudioEventToRemote(const AudioEvent &event);
111     int32_t CloseSpkNew(const std::string &args);
112     int32_t CloseMicNew(const std::string &args);
113     std::shared_ptr<DAudioIoDev> FindIoDevImpl(std::string args);
114     int32_t ParseDhidFromEvent(std::string args);
115     int32_t ConvertString2Int(std::string val);
116 
117 private:
118     static constexpr uint8_t RPC_WAIT_SECONDS = 10;
119     static constexpr uint8_t TASK_QUEUE_CAPACITY = 20;
120     static constexpr uint8_t EVENT_NOTIFY_OPEN_SPK = 0x01;
121     static constexpr uint8_t EVENT_NOTIFY_CLOSE_SPK = 0x02;
122     static constexpr uint8_t EVENT_NOTIFY_OPEN_MIC = 0x04;
123     static constexpr uint8_t EVENT_NOTIFY_CLOSE_MIC = 0x08;
124     static constexpr uint8_t EVENT_NOTIFY_OPEN_CTRL = 0x10;
125     static constexpr uint8_t EVENT_NOTIFY_CLOSE_CTRL = 0x20;
126     static constexpr size_t WAIT_HANDLER_IDLE_TIME_US = 10000;
127 
128     std::string devId_;
129     std::shared_ptr<DAudioSourceMgrCallback> mgrCallback_;
130     std::mutex ioDevMtx_;
131     std::map<int32_t, std::shared_ptr<DAudioIoDev>> deviceMap_;
132     std::shared_ptr<DAudioIoDev> speaker_;
133     std::shared_ptr<DAudioIoDev> mic_;
134     std::shared_ptr<DAudioSourceDevCtrlMgr> audioCtrlMgr_;
135 
136     std::mutex rpcWaitMutex_;
137     std::condition_variable rpcWaitCond_;
138     std::atomic<bool> isRpcOpen_ = false;
139     int32_t rpcResult_ = ERR_DH_AUDIO_FAILED;
140     uint8_t rpcNotify_ = 0;
141     std::atomic<bool> threadStatusFlag_ = false;
142 
143     class SourceEventHandler : public AppExecFwk::EventHandler {
144     public:
145         SourceEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> &runner,
146             const std::shared_ptr<DAudioSourceDev> &dev);
147         ~SourceEventHandler() override;
148         void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override;
149 
150     private:
151         void EnableDAudioCallback(const AppExecFwk::InnerEvent::Pointer &event);
152         void DisableDAudioCallback(const AppExecFwk::InnerEvent::Pointer &event);
153         void OpenDSpeakerCallback(const AppExecFwk::InnerEvent::Pointer &event);
154         void CloseDSpeakerCallback(const AppExecFwk::InnerEvent::Pointer &event);
155         void OpenDMicCallback(const AppExecFwk::InnerEvent::Pointer &event);
156         void CloseDMicCallback(const AppExecFwk::InnerEvent::Pointer &event);
157         void DMicClosedCallback(const AppExecFwk::InnerEvent::Pointer &event);
158         void SetVolumeCallback(const AppExecFwk::InnerEvent::Pointer &event);
159         void ChangeVolumeCallback(const AppExecFwk::InnerEvent::Pointer &event);
160         void ChangeFocusCallback(const AppExecFwk::InnerEvent::Pointer &event);
161         void ChangeRenderStateCallback(const AppExecFwk::InnerEvent::Pointer &event);
162         void PlayStatusChangeCallback(const AppExecFwk::InnerEvent::Pointer &event);
163         void SpkMmapStartCallback(const AppExecFwk::InnerEvent::Pointer &event);
164         void SpkMmapStopCallback(const AppExecFwk::InnerEvent::Pointer &event);
165         void MicMmapStartCallback(const AppExecFwk::InnerEvent::Pointer &event);
166         void MicMmapStopCallback(const AppExecFwk::InnerEvent::Pointer &event);
167         void SetThreadStatusFlagTrue(const AppExecFwk::InnerEvent::Pointer &event);
168         int32_t GetEventParam(const AppExecFwk::InnerEvent::Pointer &event, std::string &eventParam);
169 
170     private:
171         using SourceEventFunc = void (SourceEventHandler::*)(const AppExecFwk::InnerEvent::Pointer &event);
172         std::map<uint32_t, SourceEventFunc> mapEventFuncs_;
173         std::weak_ptr<DAudioSourceDev> sourceDev_;
174     };
175 
176     using DAudioSourceDevFunc = int32_t (DAudioSourceDev::*)(const AudioEvent &audioEvent);
177     std::map<AudioEventType, DAudioSourceDevFunc> memberFuncMap_;
178     std::map<AudioEventType, uint8_t> eventNotifyMap_;
179     std::shared_ptr<SourceEventHandler> handler_;
180 };
181 } // DistributedHardware
182 } // OHOS
183 #endif // OHOS_DAUDIO_SOURCE_DEV_H