1 /* 2 * Copyright (c) 2022-2025 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_AUDIO_MANAGER_INTERFACE_IMPL_H 17 #define OHOS_AUDIO_MANAGER_INTERFACE_IMPL_H 18 19 #include <map> 20 #include <mutex> 21 #include <string> 22 23 #include "hdf_device_desc.h" 24 #include "iremote_object.h" 25 #include <v1_0/iaudio_manager.h> 26 #include <v2_1/id_audio_manager.h> 27 28 #include "audio_adapter_interface_impl.h" 29 30 namespace OHOS { 31 namespace HDI { 32 namespace DistributedAudio { 33 namespace Audio { 34 namespace V1_0 { 35 using OHOS::HDI::DistributedAudio::Audioext::V2_1::DAudioEvent; 36 using OHOS::HDI::DistributedAudio::Audioext::V2_1::IDAudioCallback; 37 using OHOS::HDI::DistributedAudio::Audioext::V2_1::IDAudioHdfCallback; 38 39 typedef struct { 40 std::string adapterName; 41 uint32_t dhId; 42 uint32_t eventType; 43 uint32_t deviceType; 44 uint32_t volGroupId; 45 uint32_t iptGroupId; 46 std::string caps; 47 } DAudioDevEvent; 48 49 class AudioManagerInterfaceImpl : public IAudioManager { 50 public: GetAudioManager()51 static AudioManagerInterfaceImpl *GetAudioManager() 52 { 53 if (audioManager_ == nullptr) { 54 std::unique_lock<std::mutex> mgr_mutex(audioManagerMtx_); 55 if (audioManager_ == nullptr) { 56 audioManager_ = new AudioManagerInterfaceImpl(); 57 } 58 } 59 return audioManager_; 60 } 61 62 ~AudioManagerInterfaceImpl() override; 63 int32_t GetAllAdapters(std::vector<AudioAdapterDescriptor> &descs) override; 64 int32_t LoadAdapter(const AudioAdapterDescriptor &desc, sptr<IAudioAdapter> &adapter) override; 65 int32_t UnloadAdapter(const std::string &adapterName) override; 66 int32_t ReleaseAudioManagerObject() override; 67 68 int32_t AddAudioDevice(const std::string &adpName, const uint32_t dhId, const std::string &caps, 69 const sptr<IDAudioCallback> &callback); 70 int32_t RemoveAudioDevice(const std::string &adpName, const uint32_t dhId); 71 int32_t Notify(const std::string &adpName, const uint32_t devId, 72 const uint32_t streamId, const DAudioEvent &event); 73 void SetDeviceObject(struct HdfDeviceObject *deviceObject); 74 int32_t RegisterAudioHdfListener(const std::string &serviceName, const sptr<IDAudioHdfCallback> &callbackObj); 75 int32_t UnRegisterAudioHdfListener(const std::string &serviceName); 76 void ForceNotifyFwk(); 77 78 private: 79 AudioManagerInterfaceImpl(); 80 int32_t NotifyFwk(const DAudioDevEvent &event); 81 int32_t CreateAdapter(const std::string &adpName, const uint32_t devId, const sptr<IDAudioCallback> &callback); 82 sptr<IRemoteObject> GetRemote(const std::string &adpName); 83 sptr<AudioAdapterInterfaceImpl> GetAdapterFromMap(const std::string &adpName); 84 int32_t AddAudioDeviceInner(const uint32_t dhId, const DAudioDevEvent &event); 85 int32_t AddClearRegisterRecipient(sptr<IRemoteObject> &remote, 86 const std::string &deviceId, uint32_t dhId); 87 int32_t RemoveClearRegisterRecipient(sptr<IRemoteObject> &remote, 88 const std::string &deviceId, uint32_t dhId); 89 bool IsAllClearRegisterRecipientErased(); 90 91 private: 92 class Deletor { 93 public: ~Deletor()94 ~Deletor() 95 { 96 if (AudioManagerInterfaceImpl::audioManager_ != nullptr) { 97 delete AudioManagerInterfaceImpl::audioManager_; 98 } 99 }; 100 }; 101 102 class ClearRegisterRecipient : public IRemoteObject::DeathRecipient { 103 public: ClearRegisterRecipient(const std::string & deviceId,uint32_t dhId)104 explicit ClearRegisterRecipient(const std::string &deviceId, uint32_t dhId) 105 : deviceId_(deviceId), dhId_(dhId) 106 { 107 } IsNeedErase()108 bool IsNeedErase() 109 { 110 return needErase_; 111 } IsMatch(const std::string & deviceId,uint32_t dhId)112 bool IsMatch(const std::string &deviceId, uint32_t dhId) 113 { 114 return (deviceId == deviceId_) && (dhId == dhId_); 115 } 116 protected: 117 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 118 private: 119 std::string deviceId_; 120 uint32_t dhId_; 121 bool needErase_ = false; 122 }; 123 std::mutex clearRegisterRecipientsMtx_; 124 std::vector<sptr<ClearRegisterRecipient>> clearRegisterRecipients_; 125 126 class AudioManagerRecipient : public IRemoteObject::DeathRecipient { 127 public: 128 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 129 }; 130 static Deletor deletor; 131 sptr<AudioManagerRecipient> audioManagerRecipient_; 132 133 private: 134 static AudioManagerInterfaceImpl *audioManager_; 135 static std::mutex audioManagerMtx_; 136 struct HdfDeviceObject *deviceObject_ = nullptr; 137 static constexpr int32_t LOW_LATENCY_RENDER_ID = 1 << 1 | 1 << 0; 138 std::mutex adapterMapMtx_; 139 std::map<std::string, sptr<AudioAdapterInterfaceImpl>> mapAudioAdapter_; 140 std::map<std::string, sptr<IDAudioCallback>> mapAudioCallback_; 141 std::map<std::string, bool> mapAddFlags_; 142 std::mutex hdfCallbackMapMtx_; 143 std::map<std::string, sptr<IDAudioHdfCallback>> mapAudioHdfCallback_; 144 std::mutex notifyFwkMtx_; 145 std::map<std::string, DAudioDevEvent> notifyFwkMap_; 146 }; 147 } // V1_0 148 } // Audio 149 } // Distributedaudio 150 } // HDI 151 } // OHOS 152 #endif // OHOS_AUDIO_MANAGER_INTERFACE_IMPL_H 153