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