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_DAUDIO_MANAGER_INTERDFACE_IMPL_H
17 #define OHOS_DAUDIO_MANAGER_INTERDFACE_IMPL_H
18 
19 #include <mutex>
20 
21 #include <v2_0/id_audio_manager.h>
22 
23 #include "audio_manager_interface_impl.h"
24 
25 namespace OHOS {
26 namespace HDI {
27 namespace DistributedAudio {
28 namespace Audioext {
29 namespace V2_0 {
30 using OHOS::HDI::DistributedAudio::Audioext::V2_0::DAudioEvent;
31 
32 class DAudioManagerInterfaceImpl : public IDAudioManager {
33 public:
GetDAudioManager()34     static DAudioManagerInterfaceImpl *GetDAudioManager()
35     {
36         if (dAudioMgr_ == nullptr) {
37             std::unique_lock<std::mutex> mgr_mutex(mgrMtx_);
38             if (dAudioMgr_ == nullptr) {
39                 dAudioMgr_ = new DAudioManagerInterfaceImpl();
40             }
41         }
42         return dAudioMgr_;
43     }
44 
45     ~DAudioManagerInterfaceImpl() override;
46 
47     int32_t RegisterAudioDevice(const std::string &adpName, int32_t devId, const std::string &capability,
48         const sptr<IDAudioCallback> &callbackObj) override;
49 
50     int32_t UnRegisterAudioDevice(const std::string &adpName, int32_t devId) override;
51 
52     int32_t NotifyEvent(const std::string &adpName, int32_t devId, int32_t streamId, const DAudioEvent &event) override;
53 
54 private:
55     DAudioManagerInterfaceImpl();
56 
57     DAudioManagerInterfaceImpl(const DAudioManagerInterfaceImpl &);
58 
59     DAudioManagerInterfaceImpl &operator = (const DAudioManagerInterfaceImpl &);
60 
61 private:
62     class Deletor {
63     public:
~Deletor()64         ~Deletor()
65         {
66             if (DAudioManagerInterfaceImpl::dAudioMgr_ != nullptr) {
67                 delete DAudioManagerInterfaceImpl::dAudioMgr_;
68             }
69         };
70     };
71     static Deletor deletor;
72 
73 private:
74     OHOS::HDI::DistributedAudio::Audio::V1_0::AudioManagerInterfaceImpl *audioMgr_;
75     static DAudioManagerInterfaceImpl *dAudioMgr_;
76     static std::mutex mgrMtx_;
77 };
78 } // V2_0
79 } // AudioExt
80 } // Distributedaudio
81 } // HDI
82 } // OHOS
83 #endif // OHOS_DAUDIO_MANAGER_INTERDFACE_IMPL_H
84