1 /* 2 * Copyright (c) 2022 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 <v1_0/iaudio_manager.h> 25 #include <v1_0/id_audio_manager.h> 26 27 #include "audio_adapter_interface_impl.h" 28 29 namespace OHOS { 30 namespace HDI { 31 namespace DistributedAudio { 32 namespace Audio { 33 namespace V1_0 { 34 using OHOS::HDI::DistributedAudio::Audioext::V1_0::DAudioEvent; 35 using OHOS::HDI::DistributedAudio::Audioext::V1_0::IDAudioCallback; 36 37 typedef struct { 38 std::string adapterName; 39 uint32_t dhId; 40 uint32_t eventType; 41 uint32_t deviceType; 42 uint32_t volGroupId; 43 uint32_t iptGroupId; 44 } DAudioDevEvent; 45 46 class AudioManagerInterfaceImpl : public IAudioManager { 47 public: GetAudioManager()48 static AudioManagerInterfaceImpl *GetAudioManager() 49 { 50 if (audioManager_ == nullptr) { 51 std::unique_lock<std::mutex> mgr_mutex(audioManagerMtx_); 52 if (audioManager_ == nullptr) { 53 audioManager_ = new AudioManagerInterfaceImpl(); 54 } 55 } 56 return audioManager_; 57 } 58 59 ~AudioManagerInterfaceImpl() override; 60 int32_t GetAllAdapters(std::vector<AudioAdapterDescriptor> &descs) override; 61 int32_t LoadAdapter(const AudioAdapterDescriptor &desc, sptr<IAudioAdapter> &adapter) override; 62 int32_t UnloadAdapter(const std::string &adapterName) override; 63 int32_t ReleaseAudioManagerObject() override; 64 65 int32_t AddAudioDevice(const std::string &adpName, const uint32_t dhId, const std::string &caps, 66 const sptr<IDAudioCallback> &callback); 67 int32_t RemoveAudioDevice(const std::string &adpName, const uint32_t dhId); 68 int32_t Notify(const std::string &adpName, const uint32_t devId, const DAudioEvent &event); 69 void SetDeviceObject(struct HdfDeviceObject *deviceObject); 70 71 private: 72 AudioManagerInterfaceImpl(); 73 int32_t NotifyFwk(const DAudioDevEvent &event); 74 int32_t CreateAdapter(const std::string &adpName, const uint32_t devId, const sptr<IDAudioCallback> &callback); 75 76 private: 77 class Deletor { 78 public: ~Deletor()79 ~Deletor() 80 { 81 if (AudioManagerInterfaceImpl::audioManager_ != nullptr) { 82 delete AudioManagerInterfaceImpl::audioManager_; 83 } 84 }; 85 }; 86 static Deletor deletor; 87 88 private: 89 static AudioManagerInterfaceImpl *audioManager_; 90 static std::mutex audioManagerMtx_; 91 struct HdfDeviceObject *deviceObject_ = nullptr; 92 std::mutex adapterMapMtx_; 93 std::map<std::string, sptr<AudioAdapterInterfaceImpl>> mapAudioAdapter_; 94 }; 95 } // V1_0 96 } // Audio 97 } // Distributedaudio 98 } // HDI 99 } // OHOS 100 #endif // OHOS_AUDIO_MANAGER_INTERFACE_IMPL_H 101