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_SOURCE_MANAGER_H 17 #define OHOS_DAUDIO_SOURCE_MANAGER_H 18 19 #include <map> 20 #include <mutex> 21 #include <thread> 22 23 #include "event_handler.h" 24 25 #include "daudio_hdi_handler.h" 26 #include "daudio_source_dev.h" 27 #include "daudio_source_mgr_callback.h" 28 #include "idaudio_sink.h" 29 #include "single_instance.h" 30 31 namespace OHOS { 32 namespace DistributedHardware { 33 class DAudioSourceManager { 34 DECLARE_SINGLE_INSTANCE_BASE(DAudioSourceManager); 35 36 public: 37 int32_t Init(const sptr<IDAudioIpcCallback> &callback); 38 int32_t UnInit(); 39 int32_t EnableDAudio(const std::string &devId, const std::string &dhId, const std::string &version, 40 const std::string &attrs, const std::string &reqId); 41 int32_t DisableDAudio(const std::string &devId, const std::string &dhId, const std::string &reqId); 42 int32_t HandleDAudioNotify(const std::string &devId, const std::string &dhId, const int32_t eventType, 43 const std::string &eventContent); 44 int32_t DAudioNotify(const std::string &devId, const std::string &dhId, const int32_t eventType, 45 const std::string &eventContent); 46 int32_t OnEnableDAudio(const std::string &devId, const std::string &dhId, const int32_t result); 47 int32_t OnDisableDAudio(const std::string &devId, const std::string &dhId, const int32_t result); 48 int32_t LoadAVSenderEngineProvider(); 49 int32_t UnloadAVSenderEngineProvider(); 50 int32_t LoadAVReceiverEngineProvider(); 51 int32_t UnloadAVReceiverEngineProvider(); 52 IAVEngineProvider *getSenderProvider(); 53 IAVEngineProvider *getReceiverProvider(); 54 55 private: 56 DAudioSourceManager(); 57 ~DAudioSourceManager(); 58 int32_t CreateAudioDevice(const std::string &devId); 59 void DeleteAudioDevice(const std::string &devId, const std::string &dhId); 60 std::string GetRequestId(const std::string &devId, const std::string &dhId); 61 void ClearAudioDev(const std::string &devId); 62 void ListenAudioDev(); 63 void RestoreThreadStatus(); 64 int32_t DoEnableDAudio(const std::string &args); 65 int32_t DoDisableDAudio(const std::string &args); 66 67 typedef struct { 68 std::string devId; 69 std::shared_ptr<DAudioSourceDev> dev; 70 std::map<std::string, std::string> ports; 71 } AudioDevice; 72 73 private: 74 static constexpr const char* DEVCLEAR_THREAD = "sourceClearTh"; 75 static constexpr const char* LISTEN_THREAD = "sourceListenTh"; 76 static constexpr int32_t WATCHDOG_INTERVAL_TIME = 20000; 77 static constexpr int32_t WATCHDOG_DELAY_TIME = 5000; 78 static constexpr size_t SLEEP_TIME = 1000000; 79 static constexpr size_t WAIT_HANDLER_IDLE_TIME_US = 10000; 80 81 std::string localDevId_; 82 std::mutex devMapMtx_; 83 std::map<std::string, AudioDevice> audioDevMap_; 84 std::mutex remoteSvrMutex_; 85 std::map<std::string, sptr<IDAudioSink>> sinkServiceMap_; 86 sptr<IDAudioIpcCallback> ipcCallback_ = nullptr; 87 std::shared_ptr<DAudioSourceMgrCallback> daudioMgrCallback_ = nullptr; 88 std::thread devClearThread_; 89 std::thread listenThread_; 90 IAVEngineProvider *sendProviderPtr_ = nullptr; 91 IAVEngineProvider *rcvProviderPtr_ = nullptr; 92 void *pSHandler_ = nullptr; 93 void *pRHandler_ = nullptr; 94 std::atomic<bool> isHicollieRunning_ = false; 95 96 class SourceManagerHandler : public AppExecFwk::EventHandler { 97 public: 98 SourceManagerHandler(const std::shared_ptr<AppExecFwk::EventRunner> &runner); 99 ~SourceManagerHandler() override; 100 void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; 101 102 private: 103 void EnableDAudioCallback(const AppExecFwk::InnerEvent::Pointer &event); 104 void DisableDAudioCallback(const AppExecFwk::InnerEvent::Pointer &event); 105 int32_t GetEventParam(const AppExecFwk::InnerEvent::Pointer &event, std::string &eventParam); 106 107 private: 108 using SourceManagerFunc = void (SourceManagerHandler::*)(const AppExecFwk::InnerEvent::Pointer &event); 109 std::map<uint32_t, SourceManagerFunc> mapEventFuncs_; 110 }; 111 std::shared_ptr<SourceManagerHandler> handler_; 112 }; 113 } // DistributedHardware 114 } // OHOS 115 #endif // OHOS_DAUDIO_SINK_MANAGER_H 116