1 /*
2 * Copyright (c) 2023 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 #ifndef LOG_TAG
16 #define LOG_TAG "AudioDeviceManagerImpl"
17 #endif
18
19 #include "audio_device_manager_impl.h"
20
21 #include <dlfcn.h>
22
23 #include "audio_device_adapter_impl.h"
24 #include "audio_errors.h"
25 #include "audio_hdi_log.h"
26
27 namespace OHOS {
28 namespace AudioStandard {
GetInstance()29 AudioDeviceManagerFactory &AudioDeviceManagerFactory::GetInstance()
30 {
31 static AudioDeviceManagerFactory devMgr;
32 return devMgr;
33 }
34
DestoryDeviceManager(const AudioDeviceManagerType audioMgrType)35 int32_t AudioDeviceManagerFactory::DestoryDeviceManager(const AudioDeviceManagerType audioMgrType)
36 {
37 std::lock_guard<std::mutex> lock(devMgrFactoryMtx_);
38 if (allHdiDevMgr_.find(audioMgrType) == allHdiDevMgr_.end()) {
39 AUDIO_INFO_LOG("Audio manager is already destoried, audioMgrType %{public}d.", audioMgrType);
40 return SUCCESS;
41 }
42
43 int32_t ret = allHdiDevMgr_[audioMgrType]->Release();
44 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Audio manager is busy, audioMgrType %{public}d.", audioMgrType);
45
46 allHdiDevMgr_.erase(audioMgrType);
47 return SUCCESS;
48 }
49
CreatDeviceManager(const AudioDeviceManagerType audioMgrType)50 std::shared_ptr<IAudioDeviceManager> AudioDeviceManagerFactory::CreatDeviceManager(
51 const AudioDeviceManagerType audioMgrType)
52 {
53 std::lock_guard<std::mutex> lock(devMgrFactoryMtx_);
54 if (allHdiDevMgr_.find(audioMgrType) != allHdiDevMgr_.end()) {
55 return allHdiDevMgr_[audioMgrType];
56 }
57 std::shared_ptr<IAudioDeviceManager> audioDevMgr = nullptr;
58 switch (audioMgrType) {
59 case LOCAL_DEV_MGR:
60 audioDevMgr = InitLocalAudioMgr();
61 break;
62 case REMOTE_DEV_MGR:
63 audioDevMgr = InitRemoteAudioMgr();
64 break;
65 case BLUETOOTH_DEV_MGR:
66 audioDevMgr = InitBluetoothAudioMgr();
67 break;
68 default:
69 AUDIO_ERR_LOG("Get audio manager of audioMgrType %{public}d is not supported.", audioMgrType);
70 return nullptr;
71 }
72
73 CHECK_AND_RETURN_RET_LOG(audioDevMgr != nullptr, nullptr,
74 "Get audio manager of audioMgrType %{public}d fail.", audioMgrType);
75 allHdiDevMgr_[audioMgrType] = audioDevMgr;
76 return allHdiDevMgr_[audioMgrType];
77 }
78
InitLocalAudioMgr()79 std::shared_ptr<IAudioDeviceManager> AudioDeviceManagerFactory::InitLocalAudioMgr()
80 {
81 AUDIO_ERR_LOG("Get local audio manager is not supported.");
82 return nullptr;
83 }
84
InitRemoteAudioMgr()85 std::shared_ptr<IAudioDeviceManager> AudioDeviceManagerFactory::InitRemoteAudioMgr()
86 {
87 AUDIO_INFO_LOG("AudioDeviceManagerFactory: Init remote audio manager proxy.");
88 #ifdef FEATURE_DISTRIBUTE_AUDIO
89 sptr<IAudioManager> audioMgr = IAudioManager::Get("daudio_primary_service", false);
90 #else
91 sptr<IAudioManager> audioMgr = nullptr;
92 #endif // FEATURE_DISTRIBUTE_AUDIO
93
94 CHECK_AND_RETURN_RET_LOG((audioMgr != nullptr), nullptr, "Init remote audio manager proxy fail.");
95 AUDIO_DEBUG_LOG("Init remote hdi manager proxy success.");
96 return std::make_shared<AudioDeviceManagerImpl>(REMOTE_DEV_MGR, audioMgr);
97 }
98
InitBluetoothAudioMgr()99 std::shared_ptr<IAudioDeviceManager> AudioDeviceManagerFactory::InitBluetoothAudioMgr()
100 {
101 AUDIO_ERR_LOG("Get local audio manager is not supported.");
102 return nullptr;
103 }
104
GetAllAdapters()105 int32_t AudioDeviceManagerImpl::GetAllAdapters()
106 {
107 CHECK_AND_RETURN_RET_LOG((audioMgr_ != nullptr), ERR_INVALID_HANDLE,
108 "Audio manager is null, audioMgrType %{public}d.", audioMgrType_);
109 int32_t ret = audioMgr_->GetAllAdapters(descriptors_);
110 CHECK_AND_RETURN_RET_LOG(ret == 0, ERR_NOT_STARTED, "Get adapters failed");
111 CHECK_AND_RETURN_RET_LOG(descriptors_.data() != nullptr, ERR_OPERATION_FAILED,
112 "Get target adapters descriptor fail.");
113 return SUCCESS;
114 }
115
GetTargetAdapterDesc(const std::string & adapterName,bool isMmap)116 struct AudioAdapterDescriptor *AudioDeviceManagerImpl::GetTargetAdapterDesc(const std::string &adapterName,
117 bool isMmap)
118 {
119 (void) isMmap;
120 int32_t ret = GetAllAdapters();
121 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, nullptr,
122 "Get all adapters fail, audioMgrType %{public}d, ret %{public}d.", audioMgrType_, ret);
123
124 int32_t targetIdx = INVALID_INDEX;
125 for (uint32_t index = 0; index < descriptors_.size(); index++) {
126 struct AudioAdapterDescriptor desc = descriptors_[index];
127 if (desc.adapterName.c_str() == nullptr) {
128 continue;
129 }
130 if (adapterName.compare(desc.adapterName)) {
131 AUDIO_INFO_LOG("[%{public}d] is not target adapter", index);
132 continue;
133 }
134 targetIdx = static_cast<int32_t>(index);
135 }
136 CHECK_AND_RETURN_RET_LOG((targetIdx >= 0), nullptr, "can not find target adapter.");
137 return &descriptors_[targetIdx];
138 }
139
LoadAdapters(const std::string & adapterName,bool isMmap)140 std::shared_ptr<IAudioDeviceAdapter> AudioDeviceManagerImpl::LoadAdapters(const std::string &adapterName, bool isMmap)
141 {
142 struct AudioAdapterDescriptor *desc = GetTargetAdapterDesc(adapterName, isMmap);
143 CHECK_AND_RETURN_RET_LOG(desc != nullptr, nullptr, "can not find target adapter.");
144
145 std::lock_guard<std::mutex> lock(mtx_);
146 if (enableAdapters_.find(std::string(desc->adapterName)) != enableAdapters_.end()) {
147 AUDIO_INFO_LOG("LoadAdapters: Audio adapter already inited.");
148 return enableAdapters_[std::string(desc->adapterName)].devAdp;
149 }
150
151 CHECK_AND_RETURN_RET_LOG((audioMgr_ != nullptr), nullptr,
152 "LoadAdapters: Audio manager is null, audioMgrType %{public}d.", audioMgrType_);
153 sptr<IAudioAdapter> audioAdapter = nullptr;
154 AudioAdapterDescriptor descriptor = {
155 .adapterName = desc->adapterName,
156 };
157 int32_t ret = audioMgr_->LoadAdapter(descriptor, audioAdapter);
158 CHECK_AND_RETURN_RET_LOG(ret == 0 && audioAdapter != nullptr,
159 nullptr, "Load audio adapter fail, audioMgrType %{public}d, ret %{public}d.", audioMgrType_, ret);
160 auto audioDevAdp = std::make_shared<AudioDeviceAdapterImpl>(std::string(desc->adapterName), audioAdapter);
161 audioDevAdp->SetParamCallback(audioDevAdp);
162 ret = audioDevAdp->Init();
163 CHECK_AND_RETURN_RET_LOG((ret == SUCCESS), nullptr, "LoadAdapters: Init all ports fail, ret %{public}d.", ret);
164
165 DeviceAdapterInfo adpInfo = {audioDevAdp, audioAdapter};
166 enableAdapters_[std::string(desc->adapterName)] = adpInfo;
167 AUDIO_DEBUG_LOG("Load adapter end.");
168 return audioDevAdp;
169 }
170
UnloadAdapter(const std::string & adapterName)171 int32_t AudioDeviceManagerImpl::UnloadAdapter(const std::string &adapterName)
172 {
173 AUDIO_INFO_LOG("Unload adapter, audioMgrType %{public}d.", audioMgrType_);
174 std::lock_guard<std::mutex> lock(mtx_);
175 if (enableAdapters_.find(adapterName) == enableAdapters_.end()) {
176 AUDIO_INFO_LOG("Adapter is already unloaded.");
177 return SUCCESS;
178 }
179
180 auto adpInfo = enableAdapters_[adapterName];
181 CHECK_AND_RETURN_RET_LOG((audioMgr_ != nullptr && adpInfo.audioAdapter != nullptr), ERR_INVALID_HANDLE,
182 "UnloadAdapter: Audio manager or audio adapter is null, audioMgrType %{public}d.", audioMgrType_);
183 if (adpInfo.devAdp != nullptr) {
184 int32_t ret = adpInfo.devAdp->Release();
185 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Adapter release fail, ret %{public}d.", ret);
186 AUDIO_DEBUG_LOG("Device adapter release OK, audioMgrType %{public}d.", audioMgrType_);
187 }
188
189 audioMgr_->UnloadAdapter(adapterName);
190 enableAdapters_.erase(adapterName);
191 return SUCCESS;
192 }
193
Release()194 int32_t AudioDeviceManagerImpl::Release()
195 {
196 AUDIO_INFO_LOG("Release enter.");
197 std::lock_guard<std::mutex> lock(mtx_);
198 CHECK_AND_RETURN_RET_LOG(enableAdapters_.empty(), ERR_ILLEGAL_STATE,
199 "Audio manager has some adapters busy, audioMgrType %{public}d.", audioMgrType_);
200
201 audioMgr_ = nullptr;
202 return SUCCESS;
203 }
204 } // namespace AudioStandard
205 } // namespace OHOS