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 #include "daudio_manager_interface_impl.h"
17 
18 #include <hdf_base.h>
19 
20 #include "daudio_errcode.h"
21 #include "daudio_log.h"
22 #include "daudio_utils.h"
23 
24 #undef DH_LOG_TAG
25 #define DH_LOG_TAG "DAudioManagerInterfaceImpl"
26 
27 using namespace OHOS::DistributedHardware;
28 using namespace OHOS::HDI::DistributedAudio::Audio::V1_0;
29 
30 namespace OHOS {
31 namespace HDI {
32 namespace DistributedAudio {
33 namespace Audioext {
34 namespace V2_0 {
35 DAudioManagerInterfaceImpl *DAudioManagerInterfaceImpl::dAudioMgr_ = nullptr;
36 std::mutex DAudioManagerInterfaceImpl::mgrMtx_;
DAudioManagerImplGetInstance(void)37 extern "C" IDAudioManager *DAudioManagerImplGetInstance(void)
38 {
39     return DAudioManagerInterfaceImpl::GetDAudioManager();
40 }
41 
DAudioManagerInterfaceImpl()42 DAudioManagerInterfaceImpl::DAudioManagerInterfaceImpl()
43 {
44     DHLOGD("Distributed audio ext manager constructed.");
45     audioMgr_ = AudioManagerInterfaceImpl::GetAudioManager();
46 }
47 
~DAudioManagerInterfaceImpl()48 DAudioManagerInterfaceImpl::~DAudioManagerInterfaceImpl()
49 {
50     DHLOGD("Distributed audio ext manager destructed.");
51 }
52 
RegisterAudioDevice(const std::string & adpName,int32_t devId,const std::string & capability,const sptr<IDAudioCallback> & callbackObj)53 int32_t DAudioManagerInterfaceImpl::RegisterAudioDevice(const std::string &adpName, int32_t devId,
54     const std::string &capability, const sptr<IDAudioCallback> &callbackObj)
55 {
56     DHLOGI("Register audio device, name: %{public}s, device: %{public}s.", GetAnonyString(adpName).c_str(),
57         GetChangeDevIdMap(devId).c_str());
58     if (audioMgr_ == nullptr) {
59         DHLOGE("Audio manager is null.");
60         return HDF_FAILURE;
61     }
62 
63     int32_t ret = audioMgr_->AddAudioDevice(adpName, devId, capability, callbackObj);
64     if (ret != DH_SUCCESS) {
65         DHLOGE("Register audio device failed, ret = %{public}d", ret);
66         return HDF_FAILURE;
67     }
68 
69     DHLOGI("Register audio device success.");
70     return HDF_SUCCESS;
71 }
72 
UnRegisterAudioDevice(const std::string & adpName,int32_t devId)73 int32_t DAudioManagerInterfaceImpl::UnRegisterAudioDevice(const std::string &adpName, int32_t devId)
74 {
75     DHLOGI("UnRegister audio device, name: %{public}s, device: %{public}s.", GetAnonyString(adpName).c_str(),
76         GetChangeDevIdMap(devId).c_str());
77     if (audioMgr_ == nullptr) {
78         DHLOGE("Audio manager is null.");
79         return HDF_FAILURE;
80     }
81 
82     int32_t ret = audioMgr_->RemoveAudioDevice(adpName, devId);
83     if (ret != DH_SUCCESS) {
84         DHLOGE("UnRegister audio devcie failed. ret = %{public}d", ret);
85         return HDF_FAILURE;
86     }
87 
88     DHLOGI("UnRegister audio device success.");
89     return HDF_SUCCESS;
90 }
91 
NotifyEvent(const std::string & adpName,int32_t devId,int32_t streamId,const DAudioEvent & event)92 int32_t DAudioManagerInterfaceImpl::NotifyEvent(const std::string &adpName, int32_t devId,
93     int32_t streamId, const DAudioEvent &event)
94 {
95     if (audioMgr_ == nullptr) {
96         DHLOGE("Audio manager is null.");
97         return HDF_FAILURE;
98     }
99     DHLOGI("Notify event. event type = %{public}d", event.type);
100     int32_t ret = audioMgr_->Notify(adpName, devId, streamId, event);
101     if (ret != DH_SUCCESS) {
102         DHLOGE("Notify audio event failed. ret = %{public}d", ret);
103         return HDF_FAILURE;
104     }
105 
106     return HDF_SUCCESS;
107 }
108 } // v2_0
109 } // AudioExt
110 } // Daudio
111 } // HDI
112 } // OHOS
113