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 "cJSON.h"
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 namespace OHOS {
39 namespace DistributedHardware {
40 enum DaudioBusinessState : int32_t {
41     UNKNOWN,
42     IDLE,
43     RUNNING,
44     PAUSING
45 };
46 class DAudioSourceDev : public IAudioEventCallback, public std::enable_shared_from_this<DAudioSourceDev> {
47 public:
48     DAudioSourceDev(const std::string &devId, const std::shared_ptr<DAudioSourceMgrCallback> &callback);
49     ~DAudioSourceDev() override = default;
50 
51     int32_t AwakeAudioDev();
52     void SleepAudioDev();
53 
54     int32_t EnableDAudio(const std::string &dhId, const std::string &attrs);
55     int32_t DisableDAudio(const std::string &dhId);
56     void SetThreadStatusFlag(bool flag);
57     bool GetThreadStatusFlag();
58     void NotifyEvent(const AudioEvent &event) override;
59 
60 private:
61     int32_t EnableDSpeaker(const int32_t dhId, const std::string &attrs);
62     int32_t EnableDMic(const int32_t dhId, const std::string &attrs);
63     int32_t DisableDSpeaker(const int32_t dhId);
64     int32_t DisableDMic(const int32_t dhId);
65     int32_t DisableDAudioInner(const std::string &dhId);
66 
67     int32_t TaskEnableDAudio(const std::string &args);
68     int32_t TaskDisableDAudio(const std::string &args);
69     int32_t TaskOpenDSpeaker(const std::string &args);
70     int32_t OpenDSpeakerInner(std::shared_ptr<DAudioIoDev> &speaker, const int32_t dhId);
71     int32_t TaskCloseDSpeaker(const std::string &args);
72     int32_t TaskOpenDMic(const std::string &args);
73     int32_t TaskCloseDMic(const std::string &args);
74     int32_t TaskDMicClosed(const std::string &args);
75     int32_t TaskSetVolume(const std::string &args);
76     int32_t TaskChangeVolume(const std::string &args);
77     int32_t TaskChangeFocus(const std::string &args);
78     int32_t TaskChangeRenderState(const std::string &args);
79     int32_t TaskPlayStatusChange(const std::string &args);
80     int32_t TaskSpkMmapStart(const std::string &args);
81     int32_t TaskSpkMmapStop(const std::string &args);
82     int32_t TaskMicMmapStart(const std::string &args);
83     int32_t TaskMicMmapStop(const std::string &args);
84     void NotifyFwkRunning(const std::string &devId, const std::string &dhId);
85     void NotifyFwkIdle(const std::string &devId, const std::string &dhId);
86 
87     void OnDisableTaskResult(int32_t resultCode, const std::string &result, const std::string &funcName);
88     void OnEnableTaskResult(int32_t resultCode, const std::string &result, const std::string &funcName);
89     void OnTaskResult(int32_t resultCode, const std::string &result, const std::string &funcName);
90 
91     int32_t HandleOpenDSpeaker(const AudioEvent &event);
92     int32_t HandleCloseDSpeaker(const AudioEvent &event);
93     int32_t HandleDSpeakerOpened(const AudioEvent &event);
94     int32_t HandleDSpeakerClosed(const AudioEvent &event);
95     int32_t HandleOpenDMic(const AudioEvent &event);
96     int32_t HandleCloseDMic(const AudioEvent &event);
97     int32_t HandleDMicOpened(const AudioEvent &event);
98     int32_t HandleDMicClosed(const AudioEvent &event);
99     int32_t HandleCtrlTransClosed(const AudioEvent &event);
100     int32_t HandleNotifyRPC(const AudioEvent &event);
101     int32_t WaitForRPC(const AudioEventType type);
102     int32_t HandleVolumeSet(const AudioEvent &event);
103     int32_t HandleVolumeChange(const AudioEvent &event);
104     int32_t HandleFocusChange(const AudioEvent &event);
105     int32_t HandleRenderStateChange(const AudioEvent &event);
106     int32_t HandlePlayStatusChange(const AudioEvent &event);
107     int32_t HandleSpkMmapStart(const AudioEvent &event);
108     int32_t HandleSpkMmapStop(const AudioEvent &event);
109     int32_t HandleMicMmapStart(const AudioEvent &event);
110     int32_t HandleMicMmapStop(const AudioEvent &event);
111 
112     int32_t NotifySinkDev(const AudioEventType type, const cJSON *Param, const std::string dhId);
113     int32_t NotifyHDF(const AudioEventType type, const std::string result, const int32_t dhId);
114     AudioEventType getEventTypeFromArgs(const std::string &args);
115     void to_json(cJSON *j, const AudioParam &param);
116     int32_t SendAudioEventToRemote(const AudioEvent &event);
117     int32_t CloseSpkNew(const std::string &args);
118     int32_t CloseMicNew(const std::string &args);
119     std::shared_ptr<DAudioIoDev> FindIoDevImpl(std::string args);
120     int32_t ParseDhidFromEvent(std::string args);
121     int32_t ConvertString2Int(std::string val);
122     int32_t CreateMicEngine(std::shared_ptr<DAudioIoDev> mic);
123     void SetRegDataType(const std::string &capability);
124     void NotifyEventInner(const AudioEvent &event);
125 
126 private:
127     static constexpr uint8_t RPC_WAIT_SECONDS = 10;
128     static constexpr uint8_t TASK_QUEUE_CAPACITY = 20;
129     static constexpr uint8_t EVENT_NOTIFY_OPEN_SPK = 0x01;
130     static constexpr uint8_t EVENT_NOTIFY_CLOSE_SPK = 0x02;
131     static constexpr uint8_t EVENT_NOTIFY_OPEN_MIC = 0x04;
132     static constexpr uint8_t EVENT_NOTIFY_CLOSE_MIC = 0x08;
133     static constexpr uint8_t EVENT_NOTIFY_OPEN_CTRL = 0x10;
134     static constexpr uint8_t EVENT_NOTIFY_CLOSE_CTRL = 0x20;
135     static constexpr size_t WAIT_HANDLER_IDLE_TIME_US = 10000;
136 
137     std::string devId_;
138     std::shared_ptr<DAudioSourceMgrCallback> mgrCallback_;
139     std::mutex ioDevMtx_;
140     std::map<int32_t, std::shared_ptr<DAudioIoDev>> deviceMap_;
141     std::shared_ptr<DAudioIoDev> speaker_;
142     std::shared_ptr<DAudioIoDev> mic_;
143     std::shared_ptr<DAudioSourceDevCtrlMgr> audioCtrlMgr_;
144 
145     std::mutex rpcWaitMutex_;
146     std::condition_variable rpcWaitCond_;
147     std::atomic<bool> isRpcOpen_ = false;
148     std::atomic<bool> isFull_ = false;
149     int32_t rpcResult_ = ERR_DH_AUDIO_FAILED;
150     uint8_t rpcNotify_ = 0;
151     std::atomic<bool> threadStatusFlag_ = false;
152 
153     class SourceEventHandler : public AppExecFwk::EventHandler {
154     public:
155         SourceEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> &runner,
156             const std::shared_ptr<DAudioSourceDev> &dev);
157         ~SourceEventHandler() override;
158         void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override;
159 
160     private:
161         void EnableDAudioCallback(const AppExecFwk::InnerEvent::Pointer &event);
162         void DisableDAudioCallback(const AppExecFwk::InnerEvent::Pointer &event);
163         void OpenDSpeakerCallback(const AppExecFwk::InnerEvent::Pointer &event);
164         void CloseDSpeakerCallback(const AppExecFwk::InnerEvent::Pointer &event);
165         void OpenDMicCallback(const AppExecFwk::InnerEvent::Pointer &event);
166         void CloseDMicCallback(const AppExecFwk::InnerEvent::Pointer &event);
167         void DMicClosedCallback(const AppExecFwk::InnerEvent::Pointer &event);
168         void SetVolumeCallback(const AppExecFwk::InnerEvent::Pointer &event);
169         void ChangeVolumeCallback(const AppExecFwk::InnerEvent::Pointer &event);
170         void ChangeFocusCallback(const AppExecFwk::InnerEvent::Pointer &event);
171         void ChangeRenderStateCallback(const AppExecFwk::InnerEvent::Pointer &event);
172         void PlayStatusChangeCallback(const AppExecFwk::InnerEvent::Pointer &event);
173         void SpkMmapStartCallback(const AppExecFwk::InnerEvent::Pointer &event);
174         void SpkMmapStopCallback(const AppExecFwk::InnerEvent::Pointer &event);
175         void MicMmapStartCallback(const AppExecFwk::InnerEvent::Pointer &event);
176         void MicMmapStopCallback(const AppExecFwk::InnerEvent::Pointer &event);
177         int32_t GetEventParam(const AppExecFwk::InnerEvent::Pointer &event, std::string &eventParam);
178         void ProcessEventInner(const AppExecFwk::InnerEvent::Pointer &event);
179 
180     private:
181         using SourceEventFunc = void (SourceEventHandler::*)(const AppExecFwk::InnerEvent::Pointer &event);
182         std::map<uint32_t, SourceEventFunc> mapEventFuncs_;
183         std::weak_ptr<DAudioSourceDev> sourceDev_;
184     };
185 
186     using DAudioSourceDevFunc = int32_t (DAudioSourceDev::*)(const AudioEvent &audioEvent);
187     std::map<AudioEventType, DAudioSourceDevFunc> memberFuncMap_;
188     std::map<AudioEventType, uint8_t> eventNotifyMap_;
189     std::shared_ptr<SourceEventHandler> handler_;
190 };
191 } // DistributedHardware
192 } // OHOS
193 #endif // OHOS_DAUDIO_SOURCE_DEV_H