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 24 #include "event_handler.h" 25 #include "nlohmann/json.hpp" 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 37 using json = nlohmann::json; 38 39 namespace OHOS { 40 namespace DistributedHardware { 41 enum class ChannelState { 42 SPK_CONTROL_OPENED, 43 MIC_CONTROL_OPENED, 44 UNKNOWN, 45 }; 46 47 class DAudioSinkDev : public IAudioEventCallback, public std::enable_shared_from_this<DAudioSinkDev> { 48 public: 49 explicit DAudioSinkDev(const std::string &networkId, const sptr<IDAudioSinkIpcCallback> &sinkCallback); 50 ~DAudioSinkDev() override; 51 52 int32_t AwakeAudioDev(); 53 void SleepAudioDev(); 54 void NotifyEvent(const AudioEvent &audioEvent) override; 55 int32_t InitAVTransEngines(const ChannelState channelState, IAVEngineProvider *providerPtr); 56 int32_t PauseDistributedHardware(const std::string &networkId); 57 int32_t ResumeDistributedHardware(const std::string &networkId); 58 int32_t StopDistributedHardware(const std::string &networkId); 59 void JudgeDeviceStatus(); 60 61 private: 62 int32_t TaskOpenDSpeaker(const std::string &args); 63 int32_t TaskCloseDSpeaker(const std::string &args); 64 int32_t TaskStartRender(const std::string &args); 65 int32_t TaskOpenDMic(const std::string &args); 66 int32_t TaskCloseDMic(const std::string &args); 67 int32_t TaskSetParameter(const std::string &args); 68 int32_t TaskVolumeChange(const std::string &args); 69 int32_t TaskFocusChange(const std::string &args); 70 int32_t TaskRenderStateChange(const std::string &args); 71 int32_t TaskSetVolume(const std::string &args); 72 int32_t TaskSetMute(const std::string &args); 73 int32_t TaskPlayStatusChange(const std::string &args); 74 int32_t TaskDisableDevice(const std::string &args); 75 76 void NotifySourceDev(const AudioEventType type, const std::string dhId, const int32_t result); 77 int32_t from_json(const json &j, AudioParam &audioParam); 78 int32_t HandleEngineMessage(uint32_t type, std::string content, std::string devId); 79 int32_t SendAudioEventToRemote(const AudioEvent &event); 80 void PullUpPage(); 81 82 int32_t ParseDhidFromEvent(std::string args); 83 int32_t ConvertString2Int(std::string val); 84 85 private: 86 std::mutex rpcWaitMutex_; 87 std::condition_variable rpcWaitCond_; 88 std::string devId_; 89 std::string spkDhId_; 90 std::string micDhId_; 91 std::mutex spkClientMutex_; 92 std::map<int32_t, std::shared_ptr<ISpkClient>> spkClientMap_; 93 std::mutex micClientMutex_; 94 std::map<int32_t, std::shared_ptr<DMicClient>> micClientMap_; 95 std::shared_ptr<DAudioSinkDevCtrlMgr> audioCtrlMgr_ = nullptr; 96 static constexpr size_t WAIT_HANDLER_IDLE_TIME_US = 10000; 97 const std::string SUBTYPE = "mic"; 98 sptr<IDAudioSinkIpcCallback> ipcSinkCallback_ = nullptr; 99 std::atomic<bool> isPageStatus_ = false; 100 101 std::atomic<bool> isSpkInUse_ = false; 102 std::atomic<bool> isMicInUse_ = false; 103 104 class SinkEventHandler : public AppExecFwk::EventHandler { 105 public: 106 SinkEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> &runner, 107 const std::shared_ptr<DAudioSinkDev> &dev); 108 ~SinkEventHandler() override; 109 void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; 110 111 private: 112 void NotifyCtrlOpened(const AppExecFwk::InnerEvent::Pointer &event); 113 void NotifyCtrlClosed(const AppExecFwk::InnerEvent::Pointer &event); 114 void NotifyOpenSpeaker(const AppExecFwk::InnerEvent::Pointer &event); 115 void NotifyCloseSpeaker(const AppExecFwk::InnerEvent::Pointer &event); 116 void NotifySpeakerOpened(const AppExecFwk::InnerEvent::Pointer &event); 117 void NotifySpeakerClosed(const AppExecFwk::InnerEvent::Pointer &event); 118 void NotifyOpenMic(const AppExecFwk::InnerEvent::Pointer &event); 119 void NotifyCloseMic(const AppExecFwk::InnerEvent::Pointer &event); 120 void NotifyMicOpened(const AppExecFwk::InnerEvent::Pointer &event); 121 void NotifyMicClosed(const AppExecFwk::InnerEvent::Pointer &event); 122 void NotifySetVolume(const AppExecFwk::InnerEvent::Pointer &event); 123 void NotifyVolumeChange(const AppExecFwk::InnerEvent::Pointer &event); 124 void NotifySetParam(const AppExecFwk::InnerEvent::Pointer &event); 125 void NotifySetMute(const AppExecFwk::InnerEvent::Pointer &event); 126 void NotifyFocusChange(const AppExecFwk::InnerEvent::Pointer &event); 127 void NotifyRenderStateChange(const AppExecFwk::InnerEvent::Pointer &event); 128 void NotifyPlayStatusChange(const AppExecFwk::InnerEvent::Pointer &event); 129 int32_t GetEventParam(const AppExecFwk::InnerEvent::Pointer &event, std::string &eventParam); 130 131 private: 132 using SinkEventFunc = void (SinkEventHandler::*)(const AppExecFwk::InnerEvent::Pointer &event); 133 std::map<uint32_t, SinkEventFunc> mapEventFuncs_; 134 std::weak_ptr<DAudioSinkDev> sinkDev_; 135 }; 136 std::shared_ptr<SinkEventHandler> handler_; 137 }; 138 } // DistributedHardware 139 } // OHOS 140 #endif // OHOS_DAUDIO_SINK_DEV_H 141