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 OnHardwareStateChanged(const std::string &devId, const std::string &dhId, const int32_t state); 49 int32_t OnDataSyncTrigger(const std::string &devId); 50 int32_t LoadAVSenderEngineProvider(); 51 int32_t UnloadAVSenderEngineProvider(); 52 int32_t LoadAVReceiverEngineProvider(); 53 int32_t UnloadAVReceiverEngineProvider(); 54 IAVEngineProvider *getSenderProvider(); 55 IAVEngineProvider *getReceiverProvider(); 56 57 private: 58 DAudioSourceManager(); 59 ~DAudioSourceManager(); 60 int32_t CreateAudioDevice(const std::string &devId); 61 void DeleteAudioDevice(const std::string &devId, const std::string &dhId); 62 std::string GetRequestId(const std::string &devId, const std::string &dhId); 63 void ClearAudioDev(const std::string &devId); 64 void ListenAudioDev(); 65 void RestoreThreadStatus(); 66 int32_t DoEnableDAudio(const std::string &args); 67 int32_t DoDisableDAudio(const std::string &args); 68 69 typedef struct { 70 std::string devId; 71 std::shared_ptr<DAudioSourceDev> dev; 72 std::map<std::string, std::string> ports; 73 } AudioDevice; 74 75 private: 76 static constexpr const char* DEVCLEAR_THREAD = "sourceClearTh"; 77 static constexpr const char* LISTEN_THREAD = "sourceListenTh"; 78 static constexpr int32_t WATCHDOG_INTERVAL_TIME = 20000; 79 static constexpr int32_t WATCHDOG_DELAY_TIME = 5000; 80 static constexpr size_t SLEEP_TIME = 1000000; 81 static constexpr size_t WAIT_HANDLER_IDLE_TIME_US = 10000; 82 83 std::string localDevId_; 84 std::mutex devMapMtx_; 85 std::map<std::string, AudioDevice> audioDevMap_; 86 std::mutex remoteSvrMutex_; 87 std::mutex ipcCallbackMutex_; 88 std::map<std::string, sptr<IDAudioSink>> sinkServiceMap_; 89 sptr<IDAudioIpcCallback> ipcCallback_ = nullptr; 90 std::shared_ptr<DAudioSourceMgrCallback> daudioMgrCallback_ = nullptr; 91 std::thread devClearThread_; 92 std::thread listenThread_; 93 IAVEngineProvider *sendProviderPtr_ = nullptr; 94 IAVEngineProvider *rcvProviderPtr_ = nullptr; 95 void *pSHandler_ = nullptr; 96 void *pRHandler_ = nullptr; 97 std::atomic<bool> isHicollieRunning_ = true; 98 99 class SourceManagerHandler : public AppExecFwk::EventHandler { 100 public: 101 SourceManagerHandler(const std::shared_ptr<AppExecFwk::EventRunner> &runner); 102 ~SourceManagerHandler() override; 103 void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; 104 105 private: 106 void EnableDAudioCallback(const AppExecFwk::InnerEvent::Pointer &event); 107 void DisableDAudioCallback(const AppExecFwk::InnerEvent::Pointer &event); 108 int32_t GetEventParam(const AppExecFwk::InnerEvent::Pointer &event, std::string &eventParam); 109 110 private: 111 using SourceManagerFunc = void (SourceManagerHandler::*)(const AppExecFwk::InnerEvent::Pointer &event); 112 std::map<uint32_t, SourceManagerFunc> mapEventFuncs_; 113 }; 114 std::shared_ptr<SourceManagerHandler> handler_; 115 }; 116 } // DistributedHardware 117 } // OHOS 118 #endif // OHOS_DAUDIO_SINK_MANAGER_H 119