1 /*
2 * Copyright (c) 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 "OHAudioRoutingManager.h"
17
18 #include <set>
19
20 #include "audio_errors.h"
21 #include "audio_routing_manager.h"
22 #include "parameters.h"
23
24 namespace {
25 const size_t MAX_VALID_SIZE = 128; // MAX AudioDevice size.
26 const std::set<OH_AudioDevice_Usage> VALID_OH_AUDIO_DEVICE_UASGES = {
27 AUDIO_DEVICE_USAGE_MEDIA_OUTPUT,
28 AUDIO_DEVICE_USAGE_MEDIA_INPUT,
29 AUDIO_DEVICE_USAGE_MEDIA_ALL,
30 AUDIO_DEVICE_USAGE_CALL_OUTPUT,
31 AUDIO_DEVICE_USAGE_CALL_INPUT,
32 AUDIO_DEVICE_USAGE_CALL_ALL
33 };
34 const std::set<OH_AudioStream_Usage> VALID_OH_STREAM_USAGES = {
35 AUDIOSTREAM_USAGE_UNKNOWN,
36 AUDIOSTREAM_USAGE_MUSIC,
37 AUDIOSTREAM_USAGE_VOICE_COMMUNICATION,
38 AUDIOSTREAM_USAGE_VOICE_ASSISTANT,
39 AUDIOSTREAM_USAGE_ALARM,
40 AUDIOSTREAM_USAGE_VOICE_MESSAGE,
41 AUDIOSTREAM_USAGE_RINGTONE,
42 AUDIOSTREAM_USAGE_NOTIFICATION,
43 AUDIOSTREAM_USAGE_ACCESSIBILITY,
44 AUDIOSTREAM_USAGE_MOVIE,
45 AUDIOSTREAM_USAGE_GAME,
46 AUDIOSTREAM_USAGE_AUDIOBOOK,
47 AUDIOSTREAM_USAGE_NAVIGATION,
48 AUDIOSTREAM_USAGE_VIDEO_COMMUNICATION
49 };
50 const std::set<OH_AudioStream_SourceType> VALID_OH_SOURCE_TYPES = {
51 AUDIOSTREAM_SOURCE_TYPE_MIC,
52 AUDIOSTREAM_SOURCE_TYPE_VOICE_RECOGNITION,
53 AUDIOSTREAM_SOURCE_TYPE_PLAYBACK_CAPTURE,
54 AUDIOSTREAM_SOURCE_TYPE_VOICE_COMMUNICATION,
55 AUDIOSTREAM_SOURCE_TYPE_VOICE_MESSAGE
56 };
57 }
58
59 using OHOS::AudioStandard::OHAudioRoutingManager;
60 using OHOS::AudioStandard::OHAudioDeviceDescriptor;
61 using OHOS::AudioStandard::AudioRoutingManager;
62 using OHOS::AudioStandard::DeviceFlag;
63 using OHOS::AudioStandard::AudioDeviceUsage;
64 using OHOS::AudioStandard::StreamUsage;
65 using OHOS::AudioStandard::SourceType;
66
convertManager(OH_AudioRoutingManager* manager)67 static OHOS::AudioStandard::OHAudioRoutingManager *convertManager(OH_AudioRoutingManager* manager)
68 {
69 return (OHAudioRoutingManager*) manager;
70 }
71
OH_AudioManager_GetAudioRoutingManager(OH_AudioRoutingManager **audioRoutingManager)72 OH_AudioCommon_Result OH_AudioManager_GetAudioRoutingManager(OH_AudioRoutingManager **audioRoutingManager)
73 {
74 OHAudioRoutingManager* ohAudioRoutingManager = OHAudioRoutingManager::GetInstance();
75 *audioRoutingManager = (OH_AudioRoutingManager*)ohAudioRoutingManager;
76 return AUDIOCOMMON_RESULT_SUCCESS;
77 }
78
OH_AudioRoutingManager_GetDevices(OH_AudioRoutingManager *audioRoutingManager, OH_AudioDevice_Flag deviceFlag, OH_AudioDeviceDescriptorArray **audioDeviceDescriptorArray)79 OH_AudioCommon_Result OH_AudioRoutingManager_GetDevices(OH_AudioRoutingManager *audioRoutingManager,
80 OH_AudioDevice_Flag deviceFlag, OH_AudioDeviceDescriptorArray **audioDeviceDescriptorArray)
81 {
82 OHAudioRoutingManager* ohAudioRoutingManager = convertManager(audioRoutingManager);
83 CHECK_AND_RETURN_RET_LOG(ohAudioRoutingManager != nullptr,
84 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "ohAudioRoutingManager is nullptr");
85 CHECK_AND_RETURN_RET_LOG((
86 deviceFlag == AUDIO_DEVICE_FLAG_NONE ||
87 deviceFlag == AUDIO_DEVICE_FLAG_OUTPUT ||
88 deviceFlag == AUDIO_DEVICE_FLAG_INPUT ||
89 deviceFlag == AUDIO_DEVICE_FLAG_ALL),
90 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "deviceFlag is invalid");
91 CHECK_AND_RETURN_RET_LOG(audioDeviceDescriptorArray != nullptr,
92 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "audioDeviceDescriptorArray is nullptr");
93 DeviceFlag flag = static_cast<DeviceFlag>(deviceFlag);
94 *audioDeviceDescriptorArray = ohAudioRoutingManager->GetDevices(flag);
95 CHECK_AND_RETURN_RET_LOG(*audioDeviceDescriptorArray != nullptr,
96 AUDIOCOMMON_RESULT_ERROR_NO_MEMORY, "*audioDeviceDescriptorArray is nullptr");
97 return AUDIOCOMMON_RESULT_SUCCESS;
98 }
99
OH_AudioRoutingManager_GetAvailableDevices(OH_AudioRoutingManager *audioRoutingManager, OH_AudioDevice_Usage deviceUsage, OH_AudioDeviceDescriptorArray **audioDeviceDescriptorArray)100 OH_AudioCommon_Result OH_AudioRoutingManager_GetAvailableDevices(OH_AudioRoutingManager *audioRoutingManager,
101 OH_AudioDevice_Usage deviceUsage, OH_AudioDeviceDescriptorArray **audioDeviceDescriptorArray)
102 {
103 if (audioRoutingManager == nullptr || !VALID_OH_AUDIO_DEVICE_UASGES.count(deviceUsage) ||
104 audioDeviceDescriptorArray == nullptr) {
105 AUDIO_ERR_LOG("Invalid params!");
106 return AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM;
107 }
108 OHAudioRoutingManager* ohAudioRoutingManager = convertManager(audioRoutingManager);
109 CHECK_AND_RETURN_RET_LOG(ohAudioRoutingManager != nullptr,
110 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "audioRoutingManager is nullptr");
111
112 AudioDeviceUsage usage = static_cast<AudioDeviceUsage>(deviceUsage);
113 *audioDeviceDescriptorArray = ohAudioRoutingManager->GetAvailableDevices(usage);
114 CHECK_AND_RETURN_RET_LOG(*audioDeviceDescriptorArray != nullptr,
115 AUDIOCOMMON_RESULT_ERROR_NO_MEMORY, "*audioDeviceDescriptorArray is nullptr");
116
117 return AUDIOCOMMON_RESULT_SUCCESS;
118 }
119
OH_AudioRoutingManager_GetPreferredOutputDevice(OH_AudioRoutingManager *audioRoutingManager, OH_AudioStream_Usage streamUsage, OH_AudioDeviceDescriptorArray **audioDeviceDescriptorArray)120 OH_AudioCommon_Result OH_AudioRoutingManager_GetPreferredOutputDevice(OH_AudioRoutingManager *audioRoutingManager,
121 OH_AudioStream_Usage streamUsage, OH_AudioDeviceDescriptorArray **audioDeviceDescriptorArray)
122 {
123 if (audioRoutingManager == nullptr || !VALID_OH_STREAM_USAGES.count(streamUsage) ||
124 audioDeviceDescriptorArray == nullptr) {
125 AUDIO_ERR_LOG("Invalid params!");
126 return AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM;
127 }
128 OHAudioRoutingManager* ohAudioRoutingManager = convertManager(audioRoutingManager);
129 CHECK_AND_RETURN_RET_LOG(ohAudioRoutingManager != nullptr,
130 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "audioRoutingManager is nullptr");
131
132 StreamUsage usage = static_cast<StreamUsage>(streamUsage);
133 *audioDeviceDescriptorArray = ohAudioRoutingManager->GetPreferredOutputDevice(usage);
134 CHECK_AND_RETURN_RET_LOG(*audioDeviceDescriptorArray != nullptr,
135 AUDIOCOMMON_RESULT_ERROR_NO_MEMORY, "*audioDeviceDescriptorArray is nullptr");
136
137 return AUDIOCOMMON_RESULT_SUCCESS;
138 }
139
OH_AudioRoutingManager_GetPreferredInputDevice(OH_AudioRoutingManager *audioRoutingManager, OH_AudioStream_SourceType sourceType, OH_AudioDeviceDescriptorArray **audioDeviceDescriptorArray)140 OH_AudioCommon_Result OH_AudioRoutingManager_GetPreferredInputDevice(OH_AudioRoutingManager *audioRoutingManager,
141 OH_AudioStream_SourceType sourceType, OH_AudioDeviceDescriptorArray **audioDeviceDescriptorArray)
142 {
143 if (audioRoutingManager == nullptr || !VALID_OH_SOURCE_TYPES.count(sourceType) ||
144 audioDeviceDescriptorArray == nullptr) {
145 AUDIO_ERR_LOG("Invalid params!");
146 return AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM;
147 }
148 OHAudioRoutingManager* ohAudioRoutingManager = convertManager(audioRoutingManager);
149 CHECK_AND_RETURN_RET_LOG(ohAudioRoutingManager != nullptr,
150 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "audioRoutingManager is nullptr");
151
152 SourceType type = static_cast<SourceType>(sourceType);
153 *audioDeviceDescriptorArray = ohAudioRoutingManager->GetPreferredInputDevice(type);
154 CHECK_AND_RETURN_RET_LOG(*audioDeviceDescriptorArray != nullptr,
155 AUDIOCOMMON_RESULT_ERROR_NO_MEMORY, "*audioDeviceDescriptorArray is nullptr");
156
157 return AUDIOCOMMON_RESULT_SUCCESS;
158 }
159
OH_AudioRoutingManager_RegisterDeviceChangeCallback( OH_AudioRoutingManager *audioRoutingManager, OH_AudioDevice_Flag deviceFlag, OH_AudioRoutingManager_OnDeviceChangedCallback callback)160 OH_AudioCommon_Result OH_AudioRoutingManager_RegisterDeviceChangeCallback(
161 OH_AudioRoutingManager *audioRoutingManager, OH_AudioDevice_Flag deviceFlag,
162 OH_AudioRoutingManager_OnDeviceChangedCallback callback)
163 {
164 OHAudioRoutingManager* ohAudioRoutingManager = convertManager(audioRoutingManager);
165 CHECK_AND_RETURN_RET_LOG(ohAudioRoutingManager != nullptr,
166 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "audioRoutingManager is nullptr");
167 CHECK_AND_RETURN_RET_LOG((
168 deviceFlag == AUDIO_DEVICE_FLAG_NONE ||
169 deviceFlag == AUDIO_DEVICE_FLAG_OUTPUT ||
170 deviceFlag == AUDIO_DEVICE_FLAG_INPUT ||
171 deviceFlag == AUDIO_DEVICE_FLAG_ALL),
172 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "deviceFlag is invalid");
173 CHECK_AND_RETURN_RET_LOG(callback != nullptr, AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "callback is nullptr");
174 DeviceFlag flag = static_cast<DeviceFlag>(deviceFlag);
175 ohAudioRoutingManager->SetDeviceChangeCallback(flag, callback);
176 return AUDIOCOMMON_RESULT_SUCCESS;
177 }
178
OH_AudioRoutingManager_UnregisterDeviceChangeCallback( OH_AudioRoutingManager *audioRoutingManager, OH_AudioRoutingManager_OnDeviceChangedCallback callback)179 OH_AudioCommon_Result OH_AudioRoutingManager_UnregisterDeviceChangeCallback(
180 OH_AudioRoutingManager *audioRoutingManager,
181 OH_AudioRoutingManager_OnDeviceChangedCallback callback)
182 {
183 OHAudioRoutingManager* ohAudioRoutingManager = convertManager(audioRoutingManager);
184 CHECK_AND_RETURN_RET_LOG(ohAudioRoutingManager != nullptr,
185 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "audioRoutingManager is nullptr");
186 CHECK_AND_RETURN_RET_LOG(callback != nullptr, AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "callback is nullptr");
187 DeviceFlag flag = static_cast<DeviceFlag>(AUDIO_DEVICE_FLAG_ALL);
188 ohAudioRoutingManager->UnsetDeviceChangeCallback(flag, callback);
189 return AUDIOCOMMON_RESULT_SUCCESS;
190 }
191
OH_AudioRoutingManager_ReleaseDevices( OH_AudioRoutingManager *audioRoutingManager, OH_AudioDeviceDescriptorArray *audioDeviceDescriptorArray)192 OH_AudioCommon_Result OH_AudioRoutingManager_ReleaseDevices(
193 OH_AudioRoutingManager *audioRoutingManager,
194 OH_AudioDeviceDescriptorArray *audioDeviceDescriptorArray)
195 {
196 OHAudioRoutingManager* ohAudioRoutingManager = convertManager(audioRoutingManager);
197 CHECK_AND_RETURN_RET_LOG(ohAudioRoutingManager != nullptr,
198 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "audioRoutingManager is nullptr");
199 CHECK_AND_RETURN_RET_LOG(audioDeviceDescriptorArray != nullptr,
200 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "audioDeviceDescriptorArray is nullptr");
201 if (audioDeviceDescriptorArray == nullptr) {
202 return AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM;
203 }
204 for (uint32_t index = 0; index < audioDeviceDescriptorArray->size; index++) {
205 OHAudioDeviceDescriptor* ohAudioDeviceDescriptor =
206 (OHAudioDeviceDescriptor*)audioDeviceDescriptorArray->descriptors[index];
207 delete ohAudioDeviceDescriptor;
208 audioDeviceDescriptorArray->descriptors[index] = nullptr;
209 }
210 free(audioDeviceDescriptorArray->descriptors);
211 audioDeviceDescriptorArray->descriptors = nullptr;
212 free(audioDeviceDescriptorArray);
213 audioDeviceDescriptorArray = nullptr;
214 return AUDIOCOMMON_RESULT_SUCCESS;
215 }
216
OH_AudioRoutingManager_IsMicBlockDetectionSupported( OH_AudioRoutingManager *audioRoutingManager, bool *supported)217 OH_AudioCommon_Result OH_AudioRoutingManager_IsMicBlockDetectionSupported(
218 OH_AudioRoutingManager *audioRoutingManager, bool *supported)
219 {
220 *supported = OHOS::system::GetBoolParameter("const.multimedia.audio.mic_block_detection", false);
221 if (*supported == true) {
222 AUDIO_INFO_LOG("mic block detection supported");
223 return AUDIOCOMMON_RESULT_SUCCESS;
224 } else {
225 AUDIO_ERR_LOG("mic block detection is not supported");
226 return AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM;
227 }
228 }
229
OH_AudioRoutingManager_SetMicBlockStatusCallback( OH_AudioRoutingManager *audioRoutingManager, OH_AudioRoutingManager_OnDeviceBlockStatusCallback callback, void *userData)230 OH_AudioCommon_Result OH_AudioRoutingManager_SetMicBlockStatusCallback(
231 OH_AudioRoutingManager *audioRoutingManager,
232 OH_AudioRoutingManager_OnDeviceBlockStatusCallback callback, void *userData)
233 {
234 OHAudioRoutingManager *ohAudioRoutingManager = convertManager(audioRoutingManager);
235 CHECK_AND_RETURN_RET_LOG(ohAudioRoutingManager != nullptr,
236 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "audioRoutingManager is nullptr");
237 ohAudioRoutingManager->SetMicrophoneBlockedCallback(callback, userData);
238 return AUDIOCOMMON_RESULT_SUCCESS;
239 }
240
241 namespace OHOS {
242 namespace AudioStandard {
243
DestroyAudioDeviceDescriptor(OH_AudioDeviceDescriptorArray *array)244 void DestroyAudioDeviceDescriptor(OH_AudioDeviceDescriptorArray *array)
245 {
246 if (array) {
247 for (uint32_t index = 0; index < array->size; index++) {
248 OHAudioDeviceDescriptor* ohAudioDeviceDescriptor = (OHAudioDeviceDescriptor*)array->descriptors[index];
249 delete ohAudioDeviceDescriptor;
250 array->descriptors[index] = nullptr;
251 }
252 free(array->descriptors);
253 free(array);
254 }
255 }
256
OHAudioRoutingManager()257 OHAudioRoutingManager::OHAudioRoutingManager()
258 {
259 AUDIO_INFO_LOG("OHAudioRoutingManager created!");
260 }
261
~OHAudioRoutingManager()262 OHAudioRoutingManager::~OHAudioRoutingManager()
263 {
264 AUDIO_INFO_LOG("OHAudioRoutingManager destroyed!");
265 }
266
ConvertDesc(std::vector<sptr<AudioDeviceDescriptor>> &desc)267 OH_AudioDeviceDescriptorArray *OHAudioRoutingManager::ConvertDesc(std::vector<sptr<AudioDeviceDescriptor>> &desc)
268 {
269 size_t size = desc.size();
270 if (size == 0 || size >= MAX_VALID_SIZE) {
271 AUDIO_ERR_LOG("failed to convert device info, size is %{public}zu", size);
272 return nullptr;
273 }
274
275 OH_AudioDeviceDescriptorArray *audioDeviceDescriptorArray =
276 (OH_AudioDeviceDescriptorArray *)malloc(sizeof(OH_AudioDeviceDescriptorArray));
277
278 if (audioDeviceDescriptorArray == nullptr) {
279 AUDIO_ERR_LOG("failed to malloc.");
280 return nullptr;
281 }
282 audioDeviceDescriptorArray->size = 0;
283 audioDeviceDescriptorArray->descriptors =
284 (OH_AudioDeviceDescriptor **)malloc(sizeof(OH_AudioDeviceDescriptor *) * size);
285 if (audioDeviceDescriptorArray->descriptors == nullptr) {
286 free(audioDeviceDescriptorArray);
287 audioDeviceDescriptorArray = nullptr;
288 AUDIO_ERR_LOG("failed to malloc descriptors.");
289 return nullptr;
290 }
291
292 uint32_t index = 0;
293 for (auto deviceDescriptor : desc) {
294 audioDeviceDescriptorArray->descriptors[index] =
295 (OH_AudioDeviceDescriptor *)(new OHAudioDeviceDescriptor(deviceDescriptor));
296 if (audioDeviceDescriptorArray->descriptors[index] == nullptr) {
297 DestroyAudioDeviceDescriptor(audioDeviceDescriptorArray);
298 return nullptr;
299 }
300 index++;
301 audioDeviceDescriptorArray->size = index;
302 }
303 return audioDeviceDescriptorArray;
304 }
305
GetDevices(DeviceFlag deviceFlag)306 OH_AudioDeviceDescriptorArray* OHAudioRoutingManager::GetDevices(DeviceFlag deviceFlag)
307 {
308 CHECK_AND_RETURN_RET_LOG(audioSystemManager_ != nullptr,
309 nullptr, "failed, audioSystemManager is null");
310 std::vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors = audioSystemManager_->GetDevices(deviceFlag);
311 uint32_t size = audioDeviceDescriptors.size();
312 if (size <= 0) {
313 AUDIO_ERR_LOG("audioDeviceDescriptors is null");
314 return nullptr;
315 }
316 return ConvertDesc(audioDeviceDescriptors);
317 }
318
GetAvailableDevices(AudioDeviceUsage deviceUsage)319 OH_AudioDeviceDescriptorArray *OHAudioRoutingManager::GetAvailableDevices(AudioDeviceUsage deviceUsage)
320 {
321 std::vector<std::unique_ptr<AudioDeviceDescriptor>> tempDesc =
322 AudioRoutingManager::GetInstance()->GetAvailableDevices(deviceUsage);
323 if (tempDesc.size() == 0) {
324 AUDIO_ERR_LOG("get no device");
325 return nullptr;
326 }
327 std::vector<sptr<AudioDeviceDescriptor>> altaDesc = {};
328 for (const auto &availableDesc : tempDesc) {
329 sptr<AudioDeviceDescriptor> dec = new(std::nothrow) AudioDeviceDescriptor(*availableDesc);
330 altaDesc.push_back(dec);
331 }
332 return ConvertDesc(altaDesc);
333 }
334
GetPreferredOutputDevice(StreamUsage streamUsage)335 OH_AudioDeviceDescriptorArray *OHAudioRoutingManager::GetPreferredOutputDevice(StreamUsage streamUsage)
336 {
337 AudioRendererInfo rendererInfo = {};
338 rendererInfo.streamUsage = streamUsage;
339 std::vector<sptr<AudioDeviceDescriptor>> desc = {};
340
341 int32_t ret = AudioRoutingManager::GetInstance()->GetPreferredOutputDeviceForRendererInfo(rendererInfo, desc);
342 if (ret != SUCCESS) {
343 AUDIO_ERR_LOG("call failed!");
344 return nullptr;
345 }
346 return ConvertDesc(desc);
347 }
348
GetPreferredInputDevice(SourceType sourceType)349 OH_AudioDeviceDescriptorArray *OHAudioRoutingManager::GetPreferredInputDevice(SourceType sourceType)
350 {
351 AudioCapturerInfo capturerInfo = {};
352 capturerInfo.sourceType = sourceType;
353 std::vector<sptr<AudioDeviceDescriptor>> desc = {};
354
355 int32_t ret = AudioRoutingManager::GetInstance()->GetPreferredInputDeviceForCapturerInfo(capturerInfo, desc);
356 if (ret != SUCCESS) {
357 AUDIO_ERR_LOG("call failed!");
358 return nullptr;
359 }
360 return ConvertDesc(desc);
361 }
362
SetDeviceChangeCallback(const DeviceFlag deviceFlag, OH_AudioRoutingManager_OnDeviceChangedCallback callback)363 OH_AudioCommon_Result OHAudioRoutingManager::SetDeviceChangeCallback(const DeviceFlag deviceFlag,
364 OH_AudioRoutingManager_OnDeviceChangedCallback callback)
365 {
366 CHECK_AND_RETURN_RET_LOG(audioSystemManager_ != nullptr,
367 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "failed, audioSystemManager is null");
368 std::shared_ptr<OHAudioDeviceChangedCallback> ohAudioOnDeviceChangedCallback =
369 std::make_shared<OHAudioDeviceChangedCallback>(callback);
370 if (ohAudioOnDeviceChangedCallback) {
371 audioSystemManager_->SetDeviceChangeCallback(deviceFlag, ohAudioOnDeviceChangedCallback);
372 ohAudioOnDeviceChangedCallbackArray_.push_back(ohAudioOnDeviceChangedCallback);
373 return AUDIOCOMMON_RESULT_SUCCESS;
374 }
375 return AUDIOCOMMON_RESULT_ERROR_NO_MEMORY;
376 }
377
UnsetDeviceChangeCallback(DeviceFlag deviceFlag, OH_AudioRoutingManager_OnDeviceChangedCallback ohOnDeviceChangedcallback)378 OH_AudioCommon_Result OHAudioRoutingManager::UnsetDeviceChangeCallback(DeviceFlag deviceFlag,
379 OH_AudioRoutingManager_OnDeviceChangedCallback ohOnDeviceChangedcallback)
380 {
381 CHECK_AND_RETURN_RET_LOG(audioSystemManager_ != nullptr,
382 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "failed, audioSystemManager is null");
383 auto iter = std::find_if(ohAudioOnDeviceChangedCallbackArray_.begin(), ohAudioOnDeviceChangedCallbackArray_.end(),
384 [&](const std::shared_ptr<OHAudioDeviceChangedCallback> &item) {
385 return item->GetCallback() == ohOnDeviceChangedcallback;
386 });
387 if (iter == ohAudioOnDeviceChangedCallbackArray_.end()) {
388 return AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM;
389 }
390 audioSystemManager_->UnsetDeviceChangeCallback(deviceFlag);
391 ohAudioOnDeviceChangedCallbackArray_.erase(iter);
392 return AUDIOCOMMON_RESULT_SUCCESS;
393 }
394
OnDeviceChange(const DeviceChangeAction &deviceChangeAction)395 void OHAudioDeviceChangedCallback::OnDeviceChange(const DeviceChangeAction &deviceChangeAction)
396 {
397 CHECK_AND_RETURN_LOG(callback_ != nullptr, "failed, pointer to the fuction is nullptr");
398 OH_AudioDevice_ChangeType type = static_cast<OH_AudioDevice_ChangeType>(deviceChangeAction.type);
399 uint32_t size = deviceChangeAction.deviceDescriptors.size();
400 if (size <= 0) {
401 AUDIO_ERR_LOG("audioDeviceDescriptors is null");
402 return;
403 }
404
405 OH_AudioDeviceDescriptorArray *audioDeviceDescriptorArray =
406 (OH_AudioDeviceDescriptorArray *)malloc(sizeof(OH_AudioDeviceDescriptorArray));
407 if (audioDeviceDescriptorArray) {
408 audioDeviceDescriptorArray->descriptors =
409 (OH_AudioDeviceDescriptor**)malloc(sizeof(OH_AudioDeviceDescriptor*) * size);
410 if (audioDeviceDescriptorArray->descriptors == nullptr) {
411 free(audioDeviceDescriptorArray);
412 audioDeviceDescriptorArray = nullptr;
413 AUDIO_ERR_LOG("failed to malloc descriptors.");
414 return;
415 }
416 audioDeviceDescriptorArray->size = size;
417 uint32_t index = 0;
418 for (auto deviceDescriptor : deviceChangeAction.deviceDescriptors) {
419 audioDeviceDescriptorArray->descriptors[index] =
420 (OH_AudioDeviceDescriptor *)(new OHAudioDeviceDescriptor(deviceDescriptor));
421 if (audioDeviceDescriptorArray->descriptors[index] == nullptr) {
422 DestroyAudioDeviceDescriptor(audioDeviceDescriptorArray);
423 return;
424 }
425 index++;
426 }
427 }
428 callback_(type, audioDeviceDescriptorArray);
429 }
430
OnMicrophoneBlocked(const MicrophoneBlockedInfo µphoneBlockedInfo)431 void OHMicrophoneBlockCallback::OnMicrophoneBlocked(const MicrophoneBlockedInfo µphoneBlockedInfo)
432 {
433 AUDIO_INFO_LOG("Enter blocked info: %{public}d", microphoneBlockedInfo.blockStatus);
434 CHECK_AND_RETURN_LOG(blockedCallback_ != nullptr, "failed, pointer to the fuction is nullptr");
435 uint32_t size = microphoneBlockedInfo.devices.size();
436 if (size <= 0) {
437 AUDIO_ERR_LOG("audioDeviceDescriptors is null");
438 return;
439 }
440 OH_AudioDevice_BlockStatus status = static_cast<OH_AudioDevice_BlockStatus>(microphoneBlockedInfo.blockStatus);
441 OH_AudioDeviceDescriptorArray *audioDeviceDescriptorArray =
442 (OH_AudioDeviceDescriptorArray *)malloc(sizeof(OH_AudioDeviceDescriptorArray));
443 if (audioDeviceDescriptorArray) {
444 audioDeviceDescriptorArray->descriptors =
445 (OH_AudioDeviceDescriptor**)malloc(sizeof(OH_AudioDeviceDescriptor*) * size);
446 if (audioDeviceDescriptorArray->descriptors == nullptr) {
447 free(audioDeviceDescriptorArray);
448 audioDeviceDescriptorArray = nullptr;
449 AUDIO_ERR_LOG("failed to malloc descriptors.");
450 return;
451 }
452 audioDeviceDescriptorArray->size = size;
453 uint32_t index = 0;
454 for (auto deviceDescriptor : microphoneBlockedInfo.devices) {
455 audioDeviceDescriptorArray->descriptors[index] =
456 (OH_AudioDeviceDescriptor *)(new OHAudioDeviceDescriptor(deviceDescriptor));
457 if (audioDeviceDescriptorArray->descriptors[index] == nullptr) {
458 DestroyAudioDeviceDescriptor(audioDeviceDescriptorArray);
459 return;
460 }
461 index++;
462 }
463 }
464 blockedCallback_(audioDeviceDescriptorArray, status, nullptr);
465 }
466
SetMicrophoneBlockedCallback( OH_AudioRoutingManager_OnDeviceBlockStatusCallback callback, void *userData)467 OH_AudioCommon_Result OHAudioRoutingManager::SetMicrophoneBlockedCallback(
468 OH_AudioRoutingManager_OnDeviceBlockStatusCallback callback, void *userData)
469 {
470 CHECK_AND_RETURN_RET_LOG(audioSystemManager_ != nullptr,
471 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "failed, audioSystemManager is null");
472 if (callback == nullptr) {
473 UnsetMicrophoneBlockedCallback(callback);
474 return AUDIOCOMMON_RESULT_SUCCESS;
475 }
476 std::shared_ptr<OHMicrophoneBlockCallback> microphoneBlock =
477 std::make_shared<OHMicrophoneBlockCallback>(callback, userData);
478 audioSystemManager_->SetMicrophoneBlockedCallback(microphoneBlock);
479 ohMicroPhoneBlockCallbackArray_.push_back(microphoneBlock);
480 return AUDIOCOMMON_RESULT_SUCCESS;
481 }
482
UnsetMicrophoneBlockedCallback( OH_AudioRoutingManager_OnDeviceBlockStatusCallback callback)483 OH_AudioCommon_Result OHAudioRoutingManager::UnsetMicrophoneBlockedCallback(
484 OH_AudioRoutingManager_OnDeviceBlockStatusCallback callback)
485 {
486 CHECK_AND_RETURN_RET_LOG(audioSystemManager_ != nullptr,
487 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "failed, audioSystemManager is null");
488
489 audioSystemManager_->UnsetMicrophoneBlockedCallback();
490
491 auto iter = std::find_if(ohMicroPhoneBlockCallbackArray_.begin(), ohMicroPhoneBlockCallbackArray_.end(),
492 [&](const std::shared_ptr<OHMicrophoneBlockCallback> &item) {
493 return item->GetCallback() == callback;
494 });
495 if (iter == ohMicroPhoneBlockCallbackArray_.end()) {
496 return AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM;
497 }
498
499 ohMicroPhoneBlockCallbackArray_.erase(iter);
500 return AUDIOCOMMON_RESULT_SUCCESS;
501 }
502 } // namespace AudioStandard
503 } // namespace OHOS