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