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