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 void SetCallerTokenId(uint64_t tokenId); 76 int32_t CheckOsType(const std::string &networkId, bool &isInvalid); 77 78 private: 79 DAudioSinkManager(); 80 ~DAudioSinkManager(); 81 int32_t LoadAVSenderEngineProvider(); 82 int32_t UnloadAVSenderEngineProvider(); 83 int32_t LoadAVReceiverEngineProvider(); 84 int32_t UnloadAVReceiverEngineProvider(); 85 bool CheckDeviceSecurityLevel(const std::string &srcDeviceId, const std::string &dstDeviceId); 86 int32_t GetDeviceSecurityLevel(const std::string &udid); 87 std::string GetUdidByNetworkId(const std::string &networkId); 88 int32_t VerifySecurityLevel(const std::string &devId); 89 int32_t InitAudioDevice(std::shared_ptr<DAudioSinkDev> dev, const std::string &devId, bool isSpkOrMic); 90 int32_t ParseValueFromCjson(std::string args, std::string key); 91 92 private: 93 static constexpr const char* DEVCLEAR_THREAD = "sinkClearTh"; 94 std::mutex devMapMutex_; 95 std::unordered_map<std::string, std::shared_ptr<DAudioSinkDev>> audioDevMap_; 96 std::mutex remoteSvrMutex_; 97 std::mutex ipcCallbackMutex_; 98 std::map<std::string, sptr<IDAudioSource>> sourceServiceMap_; 99 std::thread devClearThread_; 100 std::string localNetworkId_; 101 ChannelState channelState_ = ChannelState::UNKNOWN; 102 103 std::shared_ptr<EngineProviderListener> providerListener_ = nullptr; 104 std::shared_ptr<CtrlChannelListener> ctrlListenerCallback_ = nullptr; 105 std::shared_ptr<DaudioCtrlChannelListener> ctrlListener_ = nullptr; 106 IAVEngineProvider *sendProviderPtr_ = nullptr; 107 IAVEngineProvider *rcvProviderPtr_ = nullptr; 108 void *pSHandler_ = nullptr; 109 void *pRHandler_ = nullptr; 110 bool isSensitive_ = false; 111 bool isSameAccount_ = false; 112 bool isCheckSecLevel_ = false; 113 sptr<IDAudioSinkIpcCallback> ipcSinkCallback_ = nullptr; 114 std::shared_ptr<DmInitCallback> initCallback_ = nullptr; 115 uint64_t callerTokenId_ = 0; 116 }; 117 } // DistributedHardware 118 } // OHOS 119 #endif // OHOS_DAUDIO_SINK_MANAGER_H 120