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_SINK_DEV_H 17 #define OHOS_DAUDIO_SINK_DEV_H 18 19 #include <atomic> 20 #include <condition_variable> 21 #include <mutex> 22 #include <initializer_list> 23 #include "cJSON.h" 24 25 #include "event_handler.h" 26 27 #include "daudio_sink_dev_ctrl_mgr.h" 28 #include "dmic_client.h" 29 #include "dspeaker_client.h" 30 #include "iaudio_event_callback.h" 31 #include "imic_client.h" 32 #include "ispk_client.h" 33 #include "i_av_engine_provider.h" 34 #include "i_av_receiver_engine_callback.h" 35 #include "idaudio_sink_ipc_callback.h" 36 #include "device_manager_callback.h" 37 38 namespace OHOS { 39 namespace DistributedHardware { 40 enum class ChannelState { 41 SPK_CONTROL_OPENED, 42 MIC_CONTROL_OPENED, 43 UNKNOWN, 44 }; 45 46 class DAudioSinkDev : public IAudioEventCallback, public std::enable_shared_from_this<DAudioSinkDev> { 47 public: 48 explicit DAudioSinkDev(const std::string &networkId, const sptr<IDAudioSinkIpcCallback> &sinkCallback); 49 ~DAudioSinkDev() override; 50 51 int32_t AwakeAudioDev(); 52 void SleepAudioDev(); 53 void NotifyEvent(const AudioEvent &audioEvent) override; 54 int32_t InitAVTransEngines(const ChannelState channelState, IAVEngineProvider *providerPtr); 55 int32_t PauseDistributedHardware(const std::string &networkId); 56 int32_t ResumeDistributedHardware(const std::string &networkId); 57 int32_t StopDistributedHardware(const std::string &networkId); 58 void JudgeDeviceStatus(); 59 void SetDevLevelStatus(bool checkStatus); 60 void SetUserId(int32_t value); 61 void SetTokenId(int32_t value); 62 void SetAccountId(string value); 63 bool CheckAclRight(); 64 void SetSinkTokenId(uint64_t value); 65 66 private: 67 int32_t TaskOpenDSpeaker(const std::string &args); 68 int32_t TaskCloseDSpeaker(const std::string &args); 69 int32_t TaskStartRender(const std::string &args); 70 int32_t TaskOpenDMic(const std::string &args); 71 int32_t TaskCloseDMic(const std::string &args); 72 int32_t TaskSetParameter(const std::string &args); 73 int32_t TaskVolumeChange(const std::string &args); 74 int32_t TaskFocusChange(const std::string &args); 75 int32_t TaskRenderStateChange(const std::string &args); 76 int32_t TaskSetVolume(const std::string &args); 77 int32_t TaskSetMute(const std::string &args); 78 int32_t TaskPlayStatusChange(const std::string &args); 79 int32_t TaskDisableDevice(const std::string &args); 80 81 void NotifySourceDev(const AudioEventType type, const std::string dhId, const int32_t result); 82 int32_t from_json(const cJSON *j, AudioParam &audioParam); 83 int32_t HandleEngineMessage(uint32_t type, std::string content, std::string devId); 84 int32_t SendAudioEventToRemote(const AudioEvent &event); 85 void PullUpPage(); 86 87 int32_t GetParamValue(const cJSON *j, const char* key, int32_t &value); 88 int32_t GetCJsonObjectItems(const cJSON *j, AudioParam &audioParam); 89 int32_t ParseDhidFromEvent(std::string args); 90 int32_t ParseResultFromEvent(std::string args); 91 int32_t ConvertString2Int(std::string val); 92 93 private: 94 std::mutex rpcWaitMutex_; 95 std::condition_variable rpcWaitCond_; 96 std::string devId_; 97 std::string spkDhId_; 98 std::string micDhId_; 99 std::mutex spkClientMutex_; 100 std::map<int32_t, std::shared_ptr<ISpkClient>> spkClientMap_; 101 std::mutex micClientMutex_; 102 std::map<int32_t, std::shared_ptr<DMicClient>> micClientMap_; 103 std::shared_ptr<DAudioSinkDevCtrlMgr> audioCtrlMgr_ = nullptr; 104 static constexpr size_t WAIT_HANDLER_IDLE_TIME_US = 10000; 105 const std::string SUBTYPE = "mic"; 106 sptr<IDAudioSinkIpcCallback> ipcSinkCallback_ = nullptr; 107 std::atomic<bool> isPageStatus_ = false; 108 109 std::atomic<bool> isSpkInUse_ = false; 110 std::atomic<bool> isMicInUse_ = false; 111 bool isDevLevelStatus_ = false; 112 int32_t userId_ = -1; 113 uint64_t tokenId_ = 0; 114 uint64_t sinkTokenId_ = 0; 115 std::string accountId_ = ""; 116 117 class SinkEventHandler : public AppExecFwk::EventHandler { 118 public: 119 SinkEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> &runner, 120 const std::shared_ptr<DAudioSinkDev> &dev); 121 ~SinkEventHandler() override; 122 void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; 123 124 private: 125 void NotifyCtrlOpened(const AppExecFwk::InnerEvent::Pointer &event); 126 void NotifyCtrlClosed(const AppExecFwk::InnerEvent::Pointer &event); 127 void NotifyOpenSpeaker(const AppExecFwk::InnerEvent::Pointer &event); 128 void NotifyCloseSpeaker(const AppExecFwk::InnerEvent::Pointer &event); 129 void NotifySpeakerOpened(const AppExecFwk::InnerEvent::Pointer &event); 130 void NotifySpeakerClosed(const AppExecFwk::InnerEvent::Pointer &event); 131 void NotifyOpenMic(const AppExecFwk::InnerEvent::Pointer &event); 132 void NotifyCloseMic(const AppExecFwk::InnerEvent::Pointer &event); 133 void NotifyMicOpened(const AppExecFwk::InnerEvent::Pointer &event); 134 void NotifyMicClosed(const AppExecFwk::InnerEvent::Pointer &event); 135 void NotifySetVolume(const AppExecFwk::InnerEvent::Pointer &event); 136 void NotifyVolumeChange(const AppExecFwk::InnerEvent::Pointer &event); 137 void NotifySetParam(const AppExecFwk::InnerEvent::Pointer &event); 138 void NotifySetMute(const AppExecFwk::InnerEvent::Pointer &event); 139 void NotifyFocusChange(const AppExecFwk::InnerEvent::Pointer &event); 140 void NotifyRenderStateChange(const AppExecFwk::InnerEvent::Pointer &event); 141 void NotifyPlayStatusChange(const AppExecFwk::InnerEvent::Pointer &event); 142 int32_t GetEventParam(const AppExecFwk::InnerEvent::Pointer &event, std::string &eventParam); 143 void ProcessEventInner(const AppExecFwk::InnerEvent::Pointer &event); 144 int32_t ParseValueFromEvent(std::string args, std::string key); 145 std::string ParseStringFromEvent(std::string args, std::string key); 146 147 private: 148 using SinkEventFunc = void (SinkEventHandler::*)(const AppExecFwk::InnerEvent::Pointer &event); 149 std::map<uint32_t, SinkEventFunc> mapEventFuncs_; 150 std::weak_ptr<DAudioSinkDev> sinkDev_; 151 }; 152 std::shared_ptr<SinkEventHandler> handler_; 153 }; 154 } // DistributedHardware 155 } // OHOS 156 #endif // OHOS_DAUDIO_SINK_DEV_H 157