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 #ifndef LOG_TAG
17 #define LOG_TAG "OHAudioSessionManager"
18 #endif
19
20 #include "OHAudioSessionManager.h"
21 #include "audio_errors.h"
22 #include <ostream>
23 #include <iostream>
24
25 using OHOS::AudioStandard::OHAudioSessionManager;
26 using OHOS::AudioStandard::AudioSessionManager;
27 using OHOS::AudioStandard::OHAudioDeviceDescriptor;
28 using namespace std;
29
convertManager(OH_AudioSessionManager * manager)30 static OHOS::AudioStandard::OHAudioSessionManager *convertManager(OH_AudioSessionManager* manager)
31 {
32 return (OHAudioSessionManager*) manager;
33 }
34
35
OH_AudioManager_GetAudioSessionManager(OH_AudioSessionManager ** audioSessionManager)36 OH_AudioCommon_Result OH_AudioManager_GetAudioSessionManager(OH_AudioSessionManager **audioSessionManager)
37 {
38 OHAudioSessionManager* ohAudioSessionManager = OHAudioSessionManager::GetInstance();
39 if (audioSessionManager == nullptr) {
40 AUDIO_ERR_LOG("audioSessionManager is nullptr");
41 return AUDIOCOMMON_RESULT_SUCCESS;
42 }
43 *audioSessionManager = reinterpret_cast<OH_AudioSessionManager*>(ohAudioSessionManager);
44 return AUDIOCOMMON_RESULT_SUCCESS;
45 }
46
OH_AudioSessionManager_RegisterSessionDeactivatedCallback(OH_AudioSessionManager * audioSessionManager,OH_AudioSession_DeactivatedCallback callback)47 OH_AudioCommon_Result OH_AudioSessionManager_RegisterSessionDeactivatedCallback(
48 OH_AudioSessionManager *audioSessionManager, OH_AudioSession_DeactivatedCallback callback)
49 {
50 OHAudioSessionManager* ohAudioSessionManager = convertManager(audioSessionManager);
51 CHECK_AND_RETURN_RET_LOG(ohAudioSessionManager != nullptr,
52 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "ohAudioSessionManager is nullptr");
53 CHECK_AND_RETURN_RET_LOG(callback != nullptr, AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "callback is nullptr");
54 return ohAudioSessionManager->SetAudioSessionCallback(callback);
55 }
56
OH_AudioSessionManager_UnregisterSessionDeactivatedCallback(OH_AudioSessionManager * audioSessionManager,OH_AudioSession_DeactivatedCallback callback)57 OH_AudioCommon_Result OH_AudioSessionManager_UnregisterSessionDeactivatedCallback(
58 OH_AudioSessionManager *audioSessionManager, OH_AudioSession_DeactivatedCallback callback)
59 {
60 OHAudioSessionManager* ohAudioSessionManager = convertManager(audioSessionManager);
61 CHECK_AND_RETURN_RET_LOG(ohAudioSessionManager != nullptr,
62 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "ohAudioSessionManager is nullptr");
63 CHECK_AND_RETURN_RET_LOG(callback != nullptr, AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "callback is nullptr");
64 return ohAudioSessionManager->UnsetAudioSessionCallback(callback);
65 }
66
OH_AudioSessionManager_ActivateAudioSession(OH_AudioSessionManager * audioSessionManager,const OH_AudioSession_Strategy * strategy)67 OH_AudioCommon_Result OH_AudioSessionManager_ActivateAudioSession(
68 OH_AudioSessionManager *audioSessionManager, const OH_AudioSession_Strategy *strategy)
69 {
70 OHAudioSessionManager* ohAudioSessionManager = convertManager(audioSessionManager);
71 CHECK_AND_RETURN_RET_LOG(ohAudioSessionManager != nullptr,
72 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "ohAudioSessionManager is nullptr");
73 CHECK_AND_RETURN_RET_LOG(strategy != nullptr,
74 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "strategy is nullptr");
75 OHOS::AudioStandard::AudioSessionStrategy audioStrategy;
76 audioStrategy.concurrencyMode =
77 static_cast<OHOS::AudioStandard::AudioConcurrencyMode>(strategy->concurrencyMode);
78 return ohAudioSessionManager->ActivateAudioSession(audioStrategy);
79 }
80
OH_AudioSessionManager_DeactivateAudioSession(OH_AudioSessionManager * audioSessionManager)81 OH_AudioCommon_Result OH_AudioSessionManager_DeactivateAudioSession(
82 OH_AudioSessionManager *audioSessionManager)
83 {
84 OHAudioSessionManager* ohAudioSessionManager = convertManager(audioSessionManager);
85 CHECK_AND_RETURN_RET_LOG(ohAudioSessionManager != nullptr,
86 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "ohAudioSessionManager is nullptr");
87 return ohAudioSessionManager->DeactivateAudioSession();
88 }
89
OH_AudioSessionManager_IsAudioSessionActivated(OH_AudioSessionManager * audioSessionManager)90 bool OH_AudioSessionManager_IsAudioSessionActivated(
91 OH_AudioSessionManager *audioSessionManager)
92 {
93 OHAudioSessionManager* ohAudioSessionManager = convertManager(audioSessionManager);
94 CHECK_AND_RETURN_RET_LOG(ohAudioSessionManager != nullptr, false, "ohAudioSessionManager is nullptr");
95 return ohAudioSessionManager->IsAudioSessionActivated();
96 }
97
OH_AudioSessionManager_SetScene(OH_AudioSessionManager * audioSessionManager,OH_AudioSession_Scene scene)98 OH_AudioCommon_Result OH_AudioSessionManager_SetScene(
99 OH_AudioSessionManager *audioSessionManager, OH_AudioSession_Scene scene)
100 {
101 OHAudioSessionManager *ohAudioSessionManager = convertManager(audioSessionManager);
102 CHECK_AND_RETURN_RET_LOG(ohAudioSessionManager != nullptr,
103 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "ohAudioSessionManager is nullptr");
104 CHECK_AND_RETURN_RET_LOG(((scene == AUDIO_SESSION_SCENE_MEDIA) ||
105 (scene == AUDIO_SESSION_SCENE_GAME) ||
106 (scene == AUDIO_SESSION_SCENE_VOICE_COMMUNICATION)),
107 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "scene is invalid");
108
109 OHOS::AudioStandard::AudioSessionScene sessionScene =
110 static_cast<OHOS::AudioStandard::AudioSessionScene>(scene);
111 return ohAudioSessionManager->SetAudioSessionScene(sessionScene);
112 }
113
OH_AudioSessionManager_RegisterStateChangeCallback(OH_AudioSessionManager * audioSessionManager,OH_AudioSession_StateChangedCallback callback)114 OH_AudioCommon_Result OH_AudioSessionManager_RegisterStateChangeCallback(
115 OH_AudioSessionManager *audioSessionManager, OH_AudioSession_StateChangedCallback callback)
116 {
117 OHAudioSessionManager *ohAudioSessionManager = convertManager(audioSessionManager);
118 CHECK_AND_RETURN_RET_LOG(ohAudioSessionManager != nullptr,
119 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "ohAudioSessionManager is nullptr");
120 CHECK_AND_RETURN_RET_LOG(callback != nullptr, AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "callback is nullptr");
121 return ohAudioSessionManager->SetAudioSessionStateChangeCallback(callback);
122 }
123
OH_AudioSessionManager_UnregisterStateChangeCallback(OH_AudioSessionManager * audioSessionManager,OH_AudioSession_StateChangedCallback callback)124 OH_AudioCommon_Result OH_AudioSessionManager_UnregisterStateChangeCallback(
125 OH_AudioSessionManager *audioSessionManager, OH_AudioSession_StateChangedCallback callback)
126 {
127 OHAudioSessionManager *ohAudioSessionManager = convertManager(audioSessionManager);
128 CHECK_AND_RETURN_RET_LOG(ohAudioSessionManager != nullptr,
129 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "ohAudioSessionManager is nullptr");
130 CHECK_AND_RETURN_RET_LOG(callback != nullptr, AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "callback is nullptr");
131 return ohAudioSessionManager->UnsetAudioSessionStateChangeCallback(callback);
132 }
133
OH_AudioSessionManager_SetDefaultOutputDevice(OH_AudioSessionManager * audioSessionManager,OH_AudioDevice_Type deviceType)134 OH_AudioCommon_Result OH_AudioSessionManager_SetDefaultOutputDevice(
135 OH_AudioSessionManager *audioSessionManager, OH_AudioDevice_Type deviceType)
136 {
137 OHAudioSessionManager *ohAudioSessionManager = convertManager(audioSessionManager);
138 CHECK_AND_RETURN_RET_LOG(ohAudioSessionManager != nullptr,
139 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "ohAudioSessionManager is nullptr");
140 CHECK_AND_RETURN_RET_LOG(((deviceType == AUDIO_DEVICE_TYPE_EARPIECE) ||
141 (deviceType == AUDIO_DEVICE_TYPE_SPEAKER) ||
142 (deviceType == AUDIO_DEVICE_TYPE_DEFAULT)),
143 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "deviceType is invalid");
144
145 OHOS::AudioStandard::DeviceType type = static_cast<OHOS::AudioStandard::DeviceType>(deviceType);
146 return ohAudioSessionManager->SetDefaultOutputDevice(type);
147 }
148
OH_AudioSessionManager_GetDefaultOutputDevice(OH_AudioSessionManager * audioSessionManager,OH_AudioDevice_Type * deviceType)149 OH_AudioCommon_Result OH_AudioSessionManager_GetDefaultOutputDevice(
150 OH_AudioSessionManager *audioSessionManager, OH_AudioDevice_Type *deviceType)
151 {
152 OHAudioSessionManager *ohAudioSessionManager = convertManager(audioSessionManager);
153 CHECK_AND_RETURN_RET_LOG(ohAudioSessionManager != nullptr,
154 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "ohAudioSessionManager is nullptr");
155 CHECK_AND_RETURN_RET_LOG(deviceType != nullptr,
156 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "deviceType is nullptr");
157
158 OHOS::AudioStandard::DeviceType type = OHOS::AudioStandard::DEVICE_TYPE_INVALID;
159 OH_AudioCommon_Result ret = ohAudioSessionManager->GetDefaultOutputDevice(type);
160 *deviceType = static_cast<OH_AudioDevice_Type>(type);
161 return ret;
162 }
163
OH_AudioSessionManager_ReleaseDevices(OH_AudioSessionManager * audioSessionManager,OH_AudioDeviceDescriptorArray * audioDeviceDescriptorArray)164 OH_AudioCommon_Result OH_AudioSessionManager_ReleaseDevices(
165 OH_AudioSessionManager *audioSessionManager,
166 OH_AudioDeviceDescriptorArray *audioDeviceDescriptorArray)
167 {
168 OHAudioSessionManager *ohAudioSessionManager = convertManager(audioSessionManager);
169 CHECK_AND_RETURN_RET_LOG(ohAudioSessionManager != nullptr,
170 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "ohAudioSessionManager is nullptr");
171 CHECK_AND_RETURN_RET_LOG(audioDeviceDescriptorArray != nullptr,
172 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "audioDeviceDescriptorArray is nullptr");
173 if ((audioDeviceDescriptorArray == nullptr) || (audioDeviceDescriptorArray->descriptors == nullptr)) {
174 return AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM;
175 }
176 for (uint32_t index = 0; index < audioDeviceDescriptorArray->size; index++) {
177 OHAudioDeviceDescriptor *ohAudioDeviceDescriptor =
178 (OHAudioDeviceDescriptor*)audioDeviceDescriptorArray->descriptors[index];
179 if (ohAudioDeviceDescriptor != nullptr) {
180 delete ohAudioDeviceDescriptor;
181 }
182 audioDeviceDescriptorArray->descriptors[index] = nullptr;
183 }
184 free(audioDeviceDescriptorArray->descriptors);
185 audioDeviceDescriptorArray->descriptors = nullptr;
186 free(audioDeviceDescriptorArray);
187 audioDeviceDescriptorArray = nullptr;
188 return AUDIOCOMMON_RESULT_SUCCESS;
189 }
190
OH_AudioSessionManager_RegisterCurrentOutputDeviceChangeCallback(OH_AudioSessionManager * audioSessionManager,OH_AudioSession_CurrentOutputDeviceChangedCallback callback)191 OH_AudioCommon_Result OH_AudioSessionManager_RegisterCurrentOutputDeviceChangeCallback(
192 OH_AudioSessionManager *audioSessionManager,
193 OH_AudioSession_CurrentOutputDeviceChangedCallback callback)
194 {
195 CHECK_AND_RETURN_RET_LOG(callback != nullptr, AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "callback is nullptr");
196
197 OHAudioSessionManager* ohAudioSessionManager = convertManager(audioSessionManager);
198 CHECK_AND_RETURN_RET_LOG(ohAudioSessionManager != nullptr,
199 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "ohAudioSessionManager is nullptr");
200 return ohAudioSessionManager->SetAudioSessionCurrentDeviceChangeCallback(callback);
201 }
202
OH_AudioSessionManager_UnregisterCurrentOutputDeviceChangeCallback(OH_AudioSessionManager * audioSessionManager,OH_AudioSession_CurrentOutputDeviceChangedCallback callback)203 OH_AudioCommon_Result OH_AudioSessionManager_UnregisterCurrentOutputDeviceChangeCallback(
204 OH_AudioSessionManager *audioSessionManager,
205 OH_AudioSession_CurrentOutputDeviceChangedCallback callback)
206 {
207 OHAudioSessionManager* ohAudioSessionManager = convertManager(audioSessionManager);
208 CHECK_AND_RETURN_RET_LOG(ohAudioSessionManager != nullptr,
209 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "ohAudioSessionManager is nullptr");
210 CHECK_AND_RETURN_RET_LOG(callback != nullptr, AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "callback is nullptr");
211 return ohAudioSessionManager->UnsetAudioSessionCurrentDeviceChangeCallback(callback);
212 }
213
214 namespace OHOS {
215 namespace AudioStandard {
216
217 namespace {
218 const size_t MAX_VALID_SIZE = 128; // MAX AudioDevice size.
219 }
220
DestroyAudioDeviceDescriptor(OH_AudioDeviceDescriptorArray * array)221 static void DestroyAudioDeviceDescriptor(OH_AudioDeviceDescriptorArray *array)
222 {
223 if (array) {
224 for (uint32_t index = 0; index < array->size; index++) {
225 OHAudioDeviceDescriptor* ohAudioDeviceDescriptor = (OHAudioDeviceDescriptor*)array->descriptors[index];
226 delete ohAudioDeviceDescriptor;
227 array->descriptors[index] = nullptr;
228 }
229 free(array->descriptors);
230 array->descriptors = nullptr;
231 free(array);
232 array = nullptr;
233 }
234 }
235
OHAudioSessionManager()236 OHAudioSessionManager::OHAudioSessionManager()
237 {
238 AUDIO_INFO_LOG("OHAudioSessionManager created!");
239 }
240
~OHAudioSessionManager()241 OHAudioSessionManager::~OHAudioSessionManager()
242 {
243 AUDIO_INFO_LOG("OHAudioSessionManager destroyed!");
244 }
245
SetAudioSessionCallback(OH_AudioSession_DeactivatedCallback callback)246 OH_AudioCommon_Result OHAudioSessionManager::SetAudioSessionCallback(OH_AudioSession_DeactivatedCallback callback)
247 {
248 CHECK_AND_RETURN_RET_LOG(audioSessionManager_ != nullptr,
249 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "failed, audioSessionManager_ is null");
250 std::shared_ptr<OHAudioSessionCallback> ohAudioSessionCallback =
251 std::make_shared<OHAudioSessionCallback>(callback);
252 audioSessionManager_->SetAudioSessionCallback(ohAudioSessionCallback);
253 return AUDIOCOMMON_RESULT_SUCCESS;
254 }
255
UnsetAudioSessionCallback(OH_AudioSession_DeactivatedCallback callback)256 OH_AudioCommon_Result OHAudioSessionManager::UnsetAudioSessionCallback(OH_AudioSession_DeactivatedCallback callback)
257 {
258 CHECK_AND_RETURN_RET_LOG(audioSessionManager_ != nullptr,
259 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "failed, audioSessionManager_ is null");
260 std::shared_ptr<OHAudioSessionCallback> ohAudioSessionCallback =
261 std::make_shared<OHAudioSessionCallback>(callback);
262 audioSessionManager_->UnsetAudioSessionCallback(ohAudioSessionCallback);
263 return AUDIOCOMMON_RESULT_SUCCESS;
264 }
265
ActivateAudioSession(const AudioSessionStrategy & strategy)266 OH_AudioCommon_Result OHAudioSessionManager::ActivateAudioSession(const AudioSessionStrategy &strategy)
267 {
268 CHECK_AND_RETURN_RET_LOG(audioSessionManager_ != nullptr,
269 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "failed, audioSessionManager_ is null");
270 int32_t ret = audioSessionManager_->ActivateAudioSession(strategy);
271 if (ret == 0) {
272 return AUDIOCOMMON_RESULT_SUCCESS;
273 } else {
274 return AUDIOCOMMON_RESULT_ERROR_ILLEGAL_STATE;
275 }
276 }
277
DeactivateAudioSession()278 OH_AudioCommon_Result OHAudioSessionManager::DeactivateAudioSession()
279 {
280 CHECK_AND_RETURN_RET_LOG(audioSessionManager_ != nullptr,
281 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "failed, audioSessionManager_ is null");
282 int32_t ret = audioSessionManager_->DeactivateAudioSession();
283 if (ret == 0) {
284 return AUDIOCOMMON_RESULT_SUCCESS;
285 } else {
286 return AUDIOCOMMON_RESULT_ERROR_ILLEGAL_STATE;
287 }
288 }
289
IsAudioSessionActivated()290 bool OHAudioSessionManager::IsAudioSessionActivated()
291 {
292 CHECK_AND_RETURN_RET_LOG(audioSessionManager_ != nullptr, false, "failed, audioSessionManager_ is null");
293 return audioSessionManager_->IsAudioSessionActivated();
294 }
295
SetAudioSessionScene(AudioSessionScene sene)296 OH_AudioCommon_Result OHAudioSessionManager::SetAudioSessionScene(AudioSessionScene sene)
297 {
298 CHECK_AND_RETURN_RET_LOG(audioSessionManager_ != nullptr,
299 AUDIOCOMMON_RESULT_ERROR_SYSTEM, "failed, audioSessionManager_ is null");
300
301 int32_t ret = audioSessionManager_->SetAudioSessionScene(sene);
302 if (ret == OHOS::AudioStandard::ERR_NOT_SUPPORTED) {
303 AUDIO_ERR_LOG("session satet error, set scene failed.");
304 return AUDIOCOMMON_RESULT_ERROR_ILLEGAL_STATE;
305 } else if (ret != AUDIOCOMMON_RESULT_SUCCESS) {
306 AUDIO_ERR_LOG("failed to SetAudioSessionScene.");
307 return AUDIOCOMMON_RESULT_ERROR_SYSTEM;
308 }
309
310 return AUDIOCOMMON_RESULT_SUCCESS;
311 }
312
SetAudioSessionStateChangeCallback(OH_AudioSession_StateChangedCallback callback)313 OH_AudioCommon_Result OHAudioSessionManager::SetAudioSessionStateChangeCallback(
314 OH_AudioSession_StateChangedCallback callback)
315 {
316 CHECK_AND_RETURN_RET_LOG(audioSessionManager_ != nullptr,
317 AUDIOCOMMON_RESULT_ERROR_SYSTEM, "failed, audioSessionManager_ is null");
318
319 if (callback == nullptr) {
320 AUDIO_ERR_LOG("invalid callback");
321 return AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM;
322 }
323
324 std::lock_guard<std::mutex> lock(sessionStateCbMutex_);
325 if (sessionStateCallbacks_.count(callback) != 0) {
326 AUDIO_INFO_LOG("callback already registed");
327 return AUDIOCOMMON_RESULT_SUCCESS;
328 }
329
330 std::shared_ptr<OHAudioSessionStateCallback> ohAudioSessionStateCallback =
331 std::make_shared<OHAudioSessionStateCallback>(callback);
332 CHECK_AND_RETURN_RET_LOG(ohAudioSessionStateCallback != nullptr, AUDIOCOMMON_RESULT_ERROR_SYSTEM,
333 "Failed to create AudioSessionState callback!");
334
335 int32_t ret = audioSessionManager_->SetAudioSessionStateChangeCallback(ohAudioSessionStateCallback);
336 if (ret != AUDIOCOMMON_RESULT_SUCCESS) {
337 AUDIO_ERR_LOG("failed to SetAudioSessionStateChangeCallback.");
338 return AUDIOCOMMON_RESULT_ERROR_SYSTEM;
339 }
340 sessionStateCallbacks_.emplace(callback, ohAudioSessionStateCallback);
341
342 return AUDIOCOMMON_RESULT_SUCCESS;
343 }
344
UnsetAudioSessionStateChangeCallback(OH_AudioSession_StateChangedCallback callback)345 OH_AudioCommon_Result OHAudioSessionManager::UnsetAudioSessionStateChangeCallback(
346 OH_AudioSession_StateChangedCallback callback)
347 {
348 CHECK_AND_RETURN_RET_LOG(audioSessionManager_ != nullptr,
349 AUDIOCOMMON_RESULT_ERROR_SYSTEM, "failed, audioSessionManager_ is null");
350
351 std::lock_guard<std::mutex> lock(sessionStateCbMutex_);
352 if ((callback == nullptr) || (sessionStateCallbacks_.count(callback) == 0)) {
353 AUDIO_ERR_LOG("invalid callback or callback not registered");
354 return AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM;
355 }
356
357 int32_t ret = audioSessionManager_->UnsetAudioSessionStateChangeCallback(sessionStateCallbacks_[callback]);
358 if (ret != AUDIOCOMMON_RESULT_SUCCESS) {
359 AUDIO_ERR_LOG("failed to UnsetAudioSessionStateChangeCallback.");
360 return AUDIOCOMMON_RESULT_ERROR_SYSTEM;
361 }
362 sessionStateCallbacks_.erase(callback);
363 return AUDIOCOMMON_RESULT_SUCCESS;
364 }
365
SetDefaultOutputDevice(DeviceType deviceType)366 OH_AudioCommon_Result OHAudioSessionManager::SetDefaultOutputDevice(DeviceType deviceType)
367 {
368 CHECK_AND_RETURN_RET_LOG(audioSessionManager_ != nullptr,
369 AUDIOCOMMON_RESULT_ERROR_SYSTEM, "failed, audioSessionManager_ is null");
370 int32_t ret = audioSessionManager_->SetDefaultOutputDevice(deviceType);
371 if (ret != AUDIOCOMMON_RESULT_SUCCESS) {
372 AUDIO_ERR_LOG("system error when calling this function");
373 return AUDIOCOMMON_RESULT_ERROR_SYSTEM;
374 }
375 return AUDIOCOMMON_RESULT_SUCCESS;
376 }
377
GetDefaultOutputDevice(DeviceType & deviceType)378 OH_AudioCommon_Result OHAudioSessionManager::GetDefaultOutputDevice(DeviceType &deviceType)
379 {
380 CHECK_AND_RETURN_RET_LOG(audioSessionManager_ != nullptr,
381 AUDIOCOMMON_RESULT_ERROR_SYSTEM, "failed, audioSessionManager_ is null");
382
383 int32_t ret = audioSessionManager_->GetDefaultOutputDevice(deviceType);
384 if (ret != AUDIOCOMMON_RESULT_SUCCESS) {
385 AUDIO_ERR_LOG("failed to GetDefaultOutputDevice.");
386 return AUDIOCOMMON_RESULT_ERROR_ILLEGAL_STATE;
387 }
388 return AUDIOCOMMON_RESULT_SUCCESS;
389 }
390
SetAudioSessionCurrentDeviceChangeCallback(OH_AudioSession_CurrentOutputDeviceChangedCallback callback)391 OH_AudioCommon_Result OHAudioSessionManager::SetAudioSessionCurrentDeviceChangeCallback(
392 OH_AudioSession_CurrentOutputDeviceChangedCallback callback)
393 {
394 CHECK_AND_RETURN_RET_LOG(audioSessionManager_ != nullptr,
395 AUDIOCOMMON_RESULT_ERROR_SYSTEM, "failed, audioSessionManager_ is null");
396
397 if (callback == nullptr) {
398 AUDIO_ERR_LOG("invalid callback");
399 return AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM;
400 }
401
402 std::lock_guard<std::mutex> lock(sessionDeviceCbMutex_);
403 if (sessionDeviceCallbacks_.count(callback) != 0) {
404 AUDIO_INFO_LOG("callback already registed");
405 return AUDIOCOMMON_RESULT_SUCCESS;
406 }
407
408 std::shared_ptr<OHAudioSessionDeviceCallback> ohAudioSessionDeviceCallback =
409 std::make_shared<OHAudioSessionDeviceCallback>(callback);
410 CHECK_AND_RETURN_RET_LOG(ohAudioSessionDeviceCallback != nullptr, AUDIOCOMMON_RESULT_ERROR_SYSTEM,
411 "Failed to create AudioSessionState callback!");
412
413 int32_t ret = audioSessionManager_->SetAudioSessionCurrentDeviceChangeCallback(ohAudioSessionDeviceCallback);
414 if (ret != AUDIOCOMMON_RESULT_SUCCESS) {
415 AUDIO_ERR_LOG("failed to SetAudioSessionCurrentDeviceChangeCallback.");
416 return AUDIOCOMMON_RESULT_ERROR_SYSTEM;
417 }
418 sessionDeviceCallbacks_.emplace(callback, ohAudioSessionDeviceCallback);
419
420 return AUDIOCOMMON_RESULT_SUCCESS;
421 }
422
UnsetAudioSessionCurrentDeviceChangeCallback(OH_AudioSession_CurrentOutputDeviceChangedCallback callback)423 OH_AudioCommon_Result OHAudioSessionManager::UnsetAudioSessionCurrentDeviceChangeCallback(
424 OH_AudioSession_CurrentOutputDeviceChangedCallback callback)
425 {
426 CHECK_AND_RETURN_RET_LOG(audioSessionManager_ != nullptr,
427 AUDIOCOMMON_RESULT_ERROR_SYSTEM, "failed, audioSessionManager_ is null");
428
429 std::lock_guard<std::mutex> lock(sessionDeviceCbMutex_);
430 if ((callback == nullptr) || (sessionDeviceCallbacks_.count(callback) == 0)) {
431 AUDIO_ERR_LOG("invalid callback or callback not registered");
432 return AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM;
433 }
434
435 int32_t ret = audioSessionManager_->UnsetAudioSessionCurrentDeviceChangeCallback(
436 sessionDeviceCallbacks_[callback]);
437 if (ret != AUDIOCOMMON_RESULT_SUCCESS) {
438 AUDIO_ERR_LOG("failed to UnsetAudioSessionCurrentDeviceChangeCallback.");
439 return AUDIOCOMMON_RESULT_ERROR_SYSTEM;
440 }
441 sessionDeviceCallbacks_.erase(callback);
442 return AUDIOCOMMON_RESULT_SUCCESS;
443 }
444
OnAudioSessionDeactive(const AudioSessionDeactiveEvent & deactiveEvent)445 void OHAudioSessionCallback::OnAudioSessionDeactive(const AudioSessionDeactiveEvent &deactiveEvent)
446 {
447 OH_AudioSession_DeactivatedEvent event;
448 event.reason = static_cast<OH_AudioSession_DeactivatedReason>(deactiveEvent.deactiveReason);
449 callback_(event);
450 }
451
OnAudioSessionStateChanged(const AudioSessionStateChangedEvent & stateChangedEvent)452 void OHAudioSessionStateCallback::OnAudioSessionStateChanged(const AudioSessionStateChangedEvent &stateChangedEvent)
453 {
454 CHECK_AND_RETURN_LOG(callback_ != nullptr, "failed, pointer to the function is nullptr");
455 OH_AudioSession_StateChangedEvent event;
456 event.stateChangeHint = static_cast<OH_AudioSession_StateChangeHint>(stateChangedEvent.stateChangeHint);
457 callback_(event);
458 }
459
OnAudioSessionCurrentDeviceChanged(const CurrentOutputDeviceChangedEvent & deviceChangedEvent)460 void OHAudioSessionDeviceCallback::OnAudioSessionCurrentDeviceChanged(
461 const CurrentOutputDeviceChangedEvent &deviceChangedEvent)
462 {
463 CHECK_AND_RETURN_LOG(callback_ != nullptr, "failed, pointer to the function is nullptr");
464
465 uint32_t size = deviceChangedEvent.devices.size();
466 if ((size == 0) || (size > MAX_VALID_SIZE)) {
467 AUDIO_ERR_LOG("audioDeviceDescriptors is null");
468 return;
469 }
470
471 OH_AudioDeviceDescriptorArray *audioDeviceDescriptorArray =
472 (OH_AudioDeviceDescriptorArray *)malloc(sizeof(OH_AudioDeviceDescriptorArray));
473 if (audioDeviceDescriptorArray) {
474 audioDeviceDescriptorArray->descriptors =
475 (OH_AudioDeviceDescriptor**)malloc(sizeof(OH_AudioDeviceDescriptor*) * size);
476 if (audioDeviceDescriptorArray->descriptors == nullptr) {
477 free(audioDeviceDescriptorArray);
478 audioDeviceDescriptorArray = nullptr;
479 AUDIO_ERR_LOG("failed to malloc descriptors.");
480 return;
481 }
482 audioDeviceDescriptorArray->size = size;
483 uint32_t index = 0;
484 for (auto deviceDescriptor : deviceChangedEvent.devices) {
485 audioDeviceDescriptorArray->descriptors[index] =
486 (OH_AudioDeviceDescriptor *)(new OHAudioDeviceDescriptor(deviceDescriptor));
487 if (audioDeviceDescriptorArray->descriptors[index] == nullptr) {
488 DestroyAudioDeviceDescriptor(audioDeviceDescriptorArray);
489 return;
490 }
491 index++;
492 }
493 }
494
495 callback_(audioDeviceDescriptorArray,
496 static_cast<OH_AudioStream_DeviceChangeReason>(deviceChangedEvent.changeReason),
497 static_cast<OH_AudioSession_OutputDeviceChangeRecommendedAction>(deviceChangedEvent.recommendedAction));
498 }
499
500 } // namespace AudioStandard
501 } // namespace OHOS