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 #ifdef AUDIO_SUPPORT_SHARED_BUFFER 112 void HandleAudioStatus(const AudioEvent &event); 113 int32_t HandleAudioStart(const AudioEvent &event); 114 int32_t HandleAudioStop(const AudioEvent &event); 115 #endif 116 117 int32_t NotifySinkDev(const AudioEventType type, const cJSON *Param, const std::string dhId); 118 int32_t NotifyHDF(const AudioEventType type, const std::string result, const int32_t dhId); 119 AudioEventType getEventTypeFromArgs(const std::string &args); 120 void to_json(cJSON *j, const AudioParam ¶m); 121 int32_t SendAudioEventToRemote(const AudioEvent &event); 122 int32_t CloseSpkNew(const std::string &args); 123 int32_t CloseMicNew(const std::string &args); 124 std::shared_ptr<DAudioIoDev> FindIoDevImpl(std::string args); 125 int32_t ParseDhidFromEvent(std::string args); 126 int32_t ConvertString2Int(std::string val); 127 int32_t CreateSpkEngine(std::shared_ptr<DAudioIoDev> speaker); 128 int32_t CreateMicEngine(std::shared_ptr<DAudioIoDev> mic); 129 void SetRegDataType(const std::string &capability); 130 void NotifyEventInner(const AudioEvent &event); 131 132 private: 133 static constexpr uint8_t RPC_WAIT_SECONDS = 10; 134 static constexpr uint8_t TASK_QUEUE_CAPACITY = 20; 135 static constexpr uint8_t EVENT_NOTIFY_OPEN_SPK = 0x01; 136 static constexpr uint8_t EVENT_NOTIFY_CLOSE_SPK = 0x02; 137 static constexpr uint8_t EVENT_NOTIFY_OPEN_MIC = 0x04; 138 static constexpr uint8_t EVENT_NOTIFY_CLOSE_MIC = 0x08; 139 static constexpr uint8_t EVENT_NOTIFY_OPEN_CTRL = 0x10; 140 static constexpr uint8_t EVENT_NOTIFY_CLOSE_CTRL = 0x20; 141 static constexpr size_t WAIT_HANDLER_IDLE_TIME_US = 10000; 142 143 std::string devId_; 144 std::shared_ptr<DAudioSourceMgrCallback> mgrCallback_; 145 std::mutex ioDevMtx_; 146 std::map<int32_t, std::shared_ptr<DAudioIoDev>> deviceMap_; 147 std::shared_ptr<DAudioIoDev> speaker_; 148 std::shared_ptr<DAudioIoDev> mic_; 149 std::shared_ptr<DAudioSourceDevCtrlMgr> audioCtrlMgr_; 150 151 std::mutex rpcWaitMutex_; 152 std::condition_variable rpcWaitCond_; 153 std::atomic<bool> isRpcOpen_ = false; 154 std::atomic<bool> isFull_ = false; 155 int32_t rpcResult_ = ERR_DH_AUDIO_FAILED; 156 uint8_t rpcNotify_ = 0; 157 std::atomic<bool> threadStatusFlag_ = false; 158 159 class SourceEventHandler : public AppExecFwk::EventHandler { 160 public: 161 SourceEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> &runner, 162 const std::shared_ptr<DAudioSourceDev> &dev); 163 ~SourceEventHandler() override; 164 void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; 165 166 private: 167 void EnableDAudioCallback(const AppExecFwk::InnerEvent::Pointer &event); 168 void DisableDAudioCallback(const AppExecFwk::InnerEvent::Pointer &event); 169 void OpenDSpeakerCallback(const AppExecFwk::InnerEvent::Pointer &event); 170 void CloseDSpeakerCallback(const AppExecFwk::InnerEvent::Pointer &event); 171 void OpenDMicCallback(const AppExecFwk::InnerEvent::Pointer &event); 172 void CloseDMicCallback(const AppExecFwk::InnerEvent::Pointer &event); 173 void DMicClosedCallback(const AppExecFwk::InnerEvent::Pointer &event); 174 void SetVolumeCallback(const AppExecFwk::InnerEvent::Pointer &event); 175 void ChangeVolumeCallback(const AppExecFwk::InnerEvent::Pointer &event); 176 void ChangeFocusCallback(const AppExecFwk::InnerEvent::Pointer &event); 177 void ChangeRenderStateCallback(const AppExecFwk::InnerEvent::Pointer &event); 178 void PlayStatusChangeCallback(const AppExecFwk::InnerEvent::Pointer &event); 179 void SpkMmapStartCallback(const AppExecFwk::InnerEvent::Pointer &event); 180 void SpkMmapStopCallback(const AppExecFwk::InnerEvent::Pointer &event); 181 void MicMmapStartCallback(const AppExecFwk::InnerEvent::Pointer &event); 182 void MicMmapStopCallback(const AppExecFwk::InnerEvent::Pointer &event); 183 int32_t GetEventParam(const AppExecFwk::InnerEvent::Pointer &event, std::string &eventParam); 184 void ProcessEventInner(const AppExecFwk::InnerEvent::Pointer &event); 185 186 private: 187 using SourceEventFunc = void (SourceEventHandler::*)(const AppExecFwk::InnerEvent::Pointer &event); 188 std::map<uint32_t, SourceEventFunc> mapEventFuncs_; 189 std::weak_ptr<DAudioSourceDev> sourceDev_; 190 }; 191 192 using DAudioSourceDevFunc = int32_t (DAudioSourceDev::*)(const AudioEvent &audioEvent); 193 std::map<AudioEventType, DAudioSourceDevFunc> memberFuncMap_; 194 std::map<AudioEventType, uint8_t> eventNotifyMap_; 195 std::shared_ptr<SourceEventHandler> handler_; 196 }; 197 } // DistributedHardware 198 } // OHOS 199 #endif // OHOS_DAUDIO_SOURCE_DEV_H