1 /* 2 * Copyright (c) 2024 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_MANAGER_H 17 #define OHOS_DAUDIO_SINK_MANAGER_H 18 19 #include <map> 20 #include <mutex> 21 22 #include "single_instance.h" 23 #include "device_manager.h" 24 #include "device_manager_callback.h" 25 #ifdef DEVICE_SECURITY_LEVEL_ENABLE 26 #include "device_security_defines.h" 27 #include "device_security_info.h" 28 #endif 29 30 #include "daudio_ctrl_channel_listener.h" 31 #include "daudio_sink_dev.h" 32 #include "idaudio_source.h" 33 #include "idaudio_sink_ipc_callback.h" 34 #include "i_av_engine_provider_callback.h" 35 36 namespace OHOS { 37 namespace DistributedHardware { 38 class EngineProviderListener : public IAVEngineProviderCallback { 39 public: EngineProviderListener()40 EngineProviderListener() {}; ~EngineProviderListener()41 ~EngineProviderListener() override {}; 42 43 int32_t OnProviderEvent(const AVTransEvent &event) override; 44 }; 45 46 class CtrlChannelListener : public CtrlChannelListenerCallback { 47 public: CtrlChannelListener()48 CtrlChannelListener() {}; ~CtrlChannelListener()49 ~CtrlChannelListener() override {}; 50 51 void OnCtrlChannelEvent(const AVTransEvent &event) override; 52 }; 53 54 class DeviceInitCallback : public DmInitCallback { 55 void OnRemoteDied() override; 56 }; 57 58 class DAudioSinkManager { 59 DECLARE_SINGLE_INSTANCE_BASE(DAudioSinkManager); 60 public: 61 int32_t Init(const sptr<IDAudioSinkIpcCallback> &sinkCallback); 62 int32_t UnInit(); 63 int32_t HandleDAudioNotify(const std::string &devId, const std::string &dhId, const int32_t eventType, 64 const std::string &eventContent); 65 int32_t DAudioNotify(const std::string &devId, const std::string &dhId, const int32_t eventType, 66 const std::string &eventContent); 67 void OnSinkDevReleased(const std::string &devId); 68 void NotifyEvent(const std::string &devId, const int32_t eventType, const std::string &eventContent); 69 void ClearAudioDev(const std::string &devId); 70 int32_t CreateAudioDevice(const std::string &devId); 71 void SetChannelState(const std::string &content); 72 int32_t PauseDistributedHardware(const std::string &networkId); 73 int32_t ResumeDistributedHardware(const std::string &networkId); 74 int32_t StopDistributedHardware(const std::string &networkId); 75 76 private: 77 DAudioSinkManager(); 78 ~DAudioSinkManager(); 79 int32_t LoadAVSenderEngineProvider(); 80 int32_t UnloadAVSenderEngineProvider(); 81 int32_t LoadAVReceiverEngineProvider(); 82 int32_t UnloadAVReceiverEngineProvider(); 83 bool CheckDeviceSecurityLevel(const std::string &srcDeviceId, const std::string &dstDeviceId); 84 int32_t GetDeviceSecurityLevel(const std::string &udid); 85 std::string GetUdidByNetworkId(const std::string &networkId); 86 int32_t VerifySecurityLevel(const std::string &devId); 87 int32_t InitAudioDevice(std::shared_ptr<DAudioSinkDev> dev, const std::string &devId, bool isSpkOrMic); 88 89 private: 90 static constexpr const char* DEVCLEAR_THREAD = "sinkClearTh"; 91 std::mutex devMapMutex_; 92 std::unordered_map<std::string, std::shared_ptr<DAudioSinkDev>> audioDevMap_; 93 std::mutex remoteSvrMutex_; 94 std::mutex ipcCallbackMutex_; 95 std::map<std::string, sptr<IDAudioSource>> sourceServiceMap_; 96 std::thread devClearThread_; 97 std::string localNetworkId_; 98 ChannelState channelState_ = ChannelState::UNKNOWN; 99 100 std::shared_ptr<EngineProviderListener> providerListener_; 101 std::shared_ptr<CtrlChannelListener> ctrlListenerCallback_ = nullptr; 102 std::shared_ptr<DaudioCtrlChannelListener> ctrlListener_ = nullptr; 103 IAVEngineProvider *sendProviderPtr_ = nullptr; 104 IAVEngineProvider *rcvProviderPtr_ = nullptr; 105 void *pSHandler_ = nullptr; 106 void *pRHandler_ = nullptr; 107 bool isSensitive_ = false; 108 bool isSameAccount_ = false; 109 bool isCheckSecLevel_ = false; 110 sptr<IDAudioSinkIpcCallback> ipcSinkCallback_ = nullptr; 111 std::shared_ptr<DmInitCallback> initCallback_; 112 }; 113 } // DistributedHardware 114 } // OHOS 115 #endif // OHOS_DAUDIO_SINK_MANAGER_H 116