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