1 /*
2  * Copyright (c) 2022-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_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_0/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_0::DAudioEvent;
36 using OHOS::HDI::DistributedAudio::Audioext::V2_0::IDAudioCallback;
37 
38 typedef struct {
39     std::string adapterName;
40     uint32_t dhId;
41     uint32_t eventType;
42     uint32_t deviceType;
43     uint32_t volGroupId;
44     uint32_t iptGroupId;
45 } DAudioDevEvent;
46 
47 class AudioManagerInterfaceImpl : public IAudioManager {
48 public:
GetAudioManager()49     static AudioManagerInterfaceImpl *GetAudioManager()
50     {
51         if (audioManager_ == nullptr) {
52             std::unique_lock<std::mutex> mgr_mutex(audioManagerMtx_);
53             if (audioManager_ == nullptr) {
54                 audioManager_ = new AudioManagerInterfaceImpl();
55             }
56         }
57         return audioManager_;
58     }
59 
60     ~AudioManagerInterfaceImpl() override;
61     int32_t GetAllAdapters(std::vector<AudioAdapterDescriptor> &descs) override;
62     int32_t LoadAdapter(const AudioAdapterDescriptor &desc, sptr<IAudioAdapter> &adapter) override;
63     int32_t UnloadAdapter(const std::string &adapterName) override;
64     int32_t ReleaseAudioManagerObject() override;
65 
66     int32_t AddAudioDevice(const std::string &adpName, const uint32_t dhId, const std::string &caps,
67         const sptr<IDAudioCallback> &callback);
68     int32_t RemoveAudioDevice(const std::string &adpName, const uint32_t dhId);
69     int32_t Notify(const std::string &adpName, const uint32_t devId,
70         const uint32_t streamId, const DAudioEvent &event);
71     void SetDeviceObject(struct HdfDeviceObject *deviceObject);
72 
73 private:
74     AudioManagerInterfaceImpl();
75     int32_t NotifyFwk(const DAudioDevEvent &event);
76     int32_t CreateAdapter(const std::string &adpName, const uint32_t devId, const sptr<IDAudioCallback> &callback);
77     sptr<IRemoteObject> GetRemote(const std::string &adpName);
78     sptr<AudioAdapterInterfaceImpl> GetAdapterFromMap(const std::string &adpName);
79 
80 private:
81     class Deletor {
82     public:
~Deletor()83         ~Deletor()
84         {
85             if (AudioManagerInterfaceImpl::audioManager_ != nullptr) {
86                 delete AudioManagerInterfaceImpl::audioManager_;
87             }
88         };
89     };
90 
91     class AudioManagerRecipient : public IRemoteObject::DeathRecipient {
92     public:
93         void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
94     };
95     static Deletor deletor;
96     sptr<AudioManagerRecipient> audioManagerRecipient_;
97 
98 private:
99     static AudioManagerInterfaceImpl *audioManager_;
100     static std::mutex audioManagerMtx_;
101     struct HdfDeviceObject *deviceObject_ = nullptr;
102     std::mutex adapterMapMtx_;
103     std::map<std::string, sptr<AudioAdapterInterfaceImpl>> mapAudioAdapter_;
104     std::map<std::string, sptr<IDAudioCallback>> mapAudioCallback_;
105     std::map<std::string, bool> mapAddFlags_;
106 };
107 } // V1_0
108 } // Audio
109 } // Distributedaudio
110 } // HDI
111 } // OHOS
112 #endif // OHOS_AUDIO_MANAGER_INTERFACE_IMPL_H
113