2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include "CAudioIODef.h"
24 using namespace tizen_media_audio;
28 * class CAudioSessionHandler
30 int CAudioSessionHandler::__sCaptureRef = 0;
32 int CAudioSessionHandler::__pcmCaptureCountInc() {
35 actual = __sCaptureRef;
36 } while (!__sync_bool_compare_and_swap(&__sCaptureRef, actual, actual + 1));
37 AUDIO_IO_LOGD("CaptureRefCount+1 > [%d]", __sCaptureRef);
41 int CAudioSessionHandler::__pcmCaptureCountDec() {
44 actual = __sCaptureRef;
45 } while (!__sync_bool_compare_and_swap(&__sCaptureRef, actual, actual - 1));
46 AUDIO_IO_LOGD("CaptureRefCount-1 > [%d]", __sCaptureRef);
47 if (__sCaptureRef < 0) {
48 AUDIO_IO_LOGE("A CaptureRef[%d] is not valid! Something is wrong!", __sCaptureRef);
54 int CAudioSessionHandler::__pcmCaptureCountGet() {
55 AUDIO_IO_LOGD("CaptureRefCount > [%d]", __sCaptureRef);
59 int CAudioSessionHandler::__sFocusRef = 0;
61 int CAudioSessionHandler::__focusIdCountInc() {
65 } while (!__sync_bool_compare_and_swap(&__sFocusRef, actual, actual + 1));
66 AUDIO_IO_LOGD("FocusRefCount+1 > [%d]", __sFocusRef);
70 int CAudioSessionHandler::__focusIdCountDec() {
74 } while (!__sync_bool_compare_and_swap(&__sFocusRef, actual, actual - 1));
75 AUDIO_IO_LOGD("FocusRefCount-1 > [%d]", __sFocusRef);
79 int CAudioSessionHandler::__focusIdCountGet() {
80 /* AUDIO_IO_LOGD("FocusRefCount > [%d]", __sFocusRef); */
84 void CAudioSessionHandler::__lockFocusCBMutex() {
85 if (pthread_mutex_lock(&__mFocusCBMutex) != 0) {
86 THROW_ERROR_MSG(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pthread_mutex_lock()");
88 #ifdef _AUDIO_IO_DEBUG_TIMING_
89 AUDIO_IO_LOGD(COLOR_RED "LOCK - FocusCB Mutex" COLOR_END);
93 void CAudioSessionHandler::__unlockFocusCBMutex() {
94 if (pthread_mutex_unlock(&__mFocusCBMutex) != 0) {
95 THROW_ERROR_MSG(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pthread_mutex_unlock()");
97 #ifdef _AUDIO_IO_DEBUG_TIMING_
98 AUDIO_IO_LOGD(COLOR_GREEN "UNLOCK - FocusCB Mutex" COLOR_END);
102 CAudioSessionHandler::CAudioSessionHandler(EAudioSessionType sessionType, CAudioInfo& audioInfo, IAudioSessionEventListener* listener) :
105 __mAudioSession(sessionType),
106 __mMultimediaSession(MM_SESSION_TYPE_MEDIA),
107 __mpEventListener(listener),
111 __mAcquiredFocus(FOCUS_NONE),
112 __mReasonForChange(NULL),
113 __mAdditionalInfo(NULL),
114 __mFocusCBMutex(PTHREAD_MUTEX_INITIALIZER) {
115 __mAudioInfo = audioInfo;
118 CAudioSessionHandler::~CAudioSessionHandler() {
121 CAudioError CAudioSessionHandler::__convertStreamType(EAudioSessionType type1, MMSessionType type2, int *index) {
125 assert(index != NULL);
127 if (type1 == EAudioSessionType::AUDIO_SESSION_TYPE_CAPTURE) {
128 for (i = 0 ; i < sizeof(__STREAM_TYPE_TABLE_IN) / sizeof(__STREAM_TYPE_TABLE_IN[0]) ; i++) {
129 if (__STREAM_TYPE_TABLE_IN[i].type == type2) {
135 for (i = 0 ; i < sizeof(__STREAM_TYPE_TABLE_OUT) / sizeof(__STREAM_TYPE_TABLE_OUT[0]) ; i++) {
136 if (__STREAM_TYPE_TABLE_OUT[i].type == type2) {
144 RET_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "Does not support session type.");
147 RET_ERROR(CAudioError::EError::ERROR_NONE);
150 CAudioError CAudioSessionHandler::__getAsmInformation(MMSessionType *type, int *options) {
151 assert(type != NULL);
152 assert(options != NULL);
154 MMSessionType currentSession = MM_SESSION_TYPE_MEDIA;
155 int sessionOptions = 0;
157 /* Read session information */
159 if ((ret = _mm_session_util_read_information(-1, (int*)¤tSession, &sessionOptions)) < 0) {
160 if (ret == (int) MM_ERROR_INVALID_HANDLE) {
161 RET_ERROR_MSG(CAudioError::EError::ERROR_INVALID_HANDLE, "Failed _mm_session_util_read_information(). Invalid handle");
163 RET_ERROR_MSG(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed _mm_session_util_read_information(). Not exist");
167 *type = currentSession;
168 *options = sessionOptions;
170 RET_ERROR(CAudioError::EError::ERROR_NONE);
173 bool CAudioSessionHandler::__isFocusRequired(MMSessionType type, int options) {
174 if ((options & MM_SESSION_OPTION_PAUSE_OTHERS)
175 || ((type != MM_SESSION_TYPE_MEDIA) && (type != MM_SESSION_TYPE_MEDIA_RECORD)))
181 bool CAudioSessionHandler::__isFocusDisableReacquisitionRequired(MMSessionType type, int options) {
182 if ((type == MM_SESSION_TYPE_MEDIA) &&
183 !(options & MM_SESSION_OPTION_RESUME_BY_SYSTEM_OR_MEDIA_PAUSED))
189 int CAudioSessionHandler::getId() {
193 int CAudioSessionHandler::getOptions() {
197 CAudioSessionHandler::EAudioSessionType CAudioSessionHandler::getAudioSession() {
198 return __mAudioSession;
201 MMSessionType CAudioSessionHandler::getMultimediaSession() {
202 return __mMultimediaSession;
205 void CAudioSessionHandler::getInternalVoipStreamInfo(sound_stream_info_h *stream_info) {
208 if ((ret = sound_manager_get_internal_voip_stream_information(stream_info))) {
209 if (ret == SOUND_MANAGER_ERROR_NO_DATA) {
210 AUDIO_IO_LOGW("there's no internal voip stream info.");
212 AUDIO_IO_LOGE("failed to sound_manager_get_internal_voip_stream_information(), ret(0x%x)", ret);
218 unsigned int CAudioSessionHandler::getSubscribeId() {
219 return __mSubscribeId;
222 CAudioInfo CAudioSessionHandler::getAudioInfo() {
226 void CAudioSessionHandler::__sound_pcm_signal_cb(mm_sound_signal_name_t signal, int value, void *user_data) {
229 AUDIO_IO_LOGD("[signal:%d], [value:%d], [user_data:%p]", signal, value, user_data);
231 CAudioSessionHandler* pHandler = static_cast<CAudioSessionHandler*>(user_data);
232 if (pHandler->__mpEventListener != NULL)
233 pHandler->__mpEventListener->onSignal(pHandler, signal, value);
236 void CAudioSessionHandler::initialize() {
238 if (__mIsInit == true) {
242 MMSessionType currentSession = MM_SESSION_TYPE_MEDIA;
243 int sessionOptions = 0; // Mix with others by default
245 CAudioError err = __getAsmInformation(¤tSession, &sessionOptions);
246 if (err == CAudioError::EError::ERROR_NONE) {
247 if (currentSession == MM_SESSION_TYPE_REPLACED_BY_STREAM) {
249 AUDIO_IO_LOGD("Stream info. was created outside, skip audio focus concept internally!");
251 // Session was configured before, use focus callback
253 AUDIO_IO_LOGD("Use audio focus concept internally!");
256 if (err == CAudioError::EError::ERROR_INVALID_HANDLE) {
257 // No session, No stream_info, No focus watch callback before
258 // Use focus watch callback with signal subscribe
260 int errorCode = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &__mSubscribeId, __sound_pcm_signal_cb, static_cast<void*>(this));
261 if (errorCode != MM_ERROR_NONE || __mSubscribeId == 0) {
262 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed mm_sound_subscribe_signal() err:0x%x, __mSubscribeId:%u",
263 errorCode, __mSubscribeId);
266 AUDIO_IO_LOGD("Subscribed mm_sound signal [id:%d]", __mSubscribeId);
268 sessionOptions = 0; // Mix with others by default
270 AUDIO_IO_LOGD("Use audio focus(watch) concept internally!");
273 AUDIO_IO_LOGD("Skip audio focus concept!");
276 if (__mAudioSession == EAudioSessionType::AUDIO_SESSION_TYPE_CAPTURE) {
277 AUDIO_IO_LOGD("Set default \"Media_Record\" type");
278 currentSession = MM_SESSION_TYPE_MEDIA_RECORD;
280 AUDIO_IO_LOGD("Set default \"Media\" type");
281 currentSession = MM_SESSION_TYPE_MEDIA;
285 // Updates session information
286 __mMultimediaSession = currentSession;
287 __mOptions = sessionOptions;
289 if (this->__mAudioSession == EAudioSessionType::AUDIO_SESSION_TYPE_CAPTURE) {
290 __pcmCaptureCountInc();
296 void CAudioSessionHandler::finalize() {
298 if (__mIsInit == false) {
302 if (__mAudioSession == EAudioSessionType::AUDIO_SESSION_TYPE_CAPTURE) {
303 __pcmCaptureCountDec();
308 if (__mSubscribeId > 0) {
309 AUDIO_IO_LOGD("Unsubscribed mm_sound signal [id:%d]", __mSubscribeId);
310 mm_sound_unsubscribe_signal(__mSubscribeId);
314 __mpEventListener = NULL;
319 bool CAudioSessionHandler::isSkipSession() {
320 if (__mMultimediaSession == MM_SESSION_TYPE_REPLACED_BY_STREAM ||
321 __mMultimediaSession == MM_SESSION_TYPE_VOIP ||
322 __mMultimediaSession == MM_SESSION_TYPE_CALL ||
323 __mMultimediaSession == MM_SESSION_TYPE_VIDEOCALL) {
324 AUDIO_IO_LOGD("__mMultimediaSession is [%d], skip session", __mMultimediaSession);
331 void CAudioSessionHandler::__sound_pcm_focus_cb(int id, mm_sound_focus_type_e focus_type, mm_sound_focus_state_e state,
332 const char *reason_for_change, int option, const char *additional_info, void *user_data) {
335 AUDIO_IO_LOGD("[id:%d], [focus_type:%d], [state:%d], [reason_for_change:%s], [additional_info:%s], [user_data:%p]",
336 id, focus_type, state, reason_for_change, additional_info, user_data);
338 /* FIXME: disable it temporarily */
339 #ifndef DISABLE_SESSION_BACK_COMP
341 CAudioSessionHandler* pHandler = static_cast<CAudioSessionHandler*>(user_data);
343 pHandler->__lockFocusCBMutex();
345 if (state == FOCUS_IS_RELEASED)
346 pHandler->__mAcquiredFocus &= ~focus_type;
347 else if (state == FOCUS_IS_ACQUIRED)
348 pHandler->__mAcquiredFocus |= focus_type;
349 pHandler->__mReasonForChange = (char *)reason_for_change;
350 pHandler->__mAdditionalInfo = (char *)additional_info;
352 if (pHandler->__mpEventListener != NULL)
353 pHandler->__mpEventListener->onInterrupt(pHandler, id, focus_type, state, reason_for_change, additional_info);
355 pHandler->__unlockFocusCBMutex();
360 void CAudioSessionHandler::__sound_pcm_focus_watch_cb(int id, mm_sound_focus_type_e focus_type, mm_sound_focus_state_e state,
361 const char *reason_for_change, const char *additional_info, void *user_data) {
362 AUDIO_IO_LOGD("[id:%d], [focus_type:%d], [state:%d], [reason_for_change:%s], [additional_info:%s], [user_data:%p]",
363 id, focus_type, state, reason_for_change, additional_info, user_data);
365 CAudioSessionHandler::__sound_pcm_focus_cb(-1, focus_type, state, reason_for_change, 0, additional_info, user_data);
370 void CAudioSessionHandler::registerSound() {
371 if (__mIsInit == false) {
372 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioSessionHandler");
375 if (__mUseFocus == true) {
377 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_POLICY_BLOCKED, "Already registered [id:%d]", __mId);
382 if (__isFocusRequired(__mMultimediaSession, __mOptions)) {
385 CAudioError err = __convertStreamType(__mAudioSession, __mMultimediaSession, &index);
386 if (err != CAudioError::EError::ERROR_NONE) {
390 errorCode = mm_sound_focus_get_id(&__mId);
391 if (errorCode != MM_ERROR_NONE) {
392 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed mm_sound_focus_get_id() err:0x%x", errorCode);
395 // Register focus callback
396 errorCode = mm_sound_register_focus_for_session(__mId,
398 __mAudioSession == EAudioSessionType::AUDIO_SESSION_TYPE_CAPTURE ? __STREAM_TYPE_TABLE_IN[index].name : __STREAM_TYPE_TABLE_OUT[index].name,
399 __sound_pcm_focus_cb,
400 static_cast<void*>(this));
401 if (errorCode != MM_ERROR_NONE) {
402 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed mm_sound_register_focus_for_session() err:0x%x", errorCode);
405 if (__isFocusDisableReacquisitionRequired(__mMultimediaSession, __mOptions)) {
406 errorCode = mm_sound_set_focus_reacquisition_for_session(__mId, false);
407 if (errorCode != MM_ERROR_NONE) {
408 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed mm_sound_set_focus_reacquisition() err:0x%x", errorCode);
414 AUDIO_IO_LOGD("Focus callback registered successfully [id:%d]", __mId);
415 } else if (!(__mOptions & MM_SESSION_OPTION_UNINTERRUPTIBLE)) {
416 // Register focus watch callback
417 errorCode = mm_sound_set_focus_watch_callback_for_session(getpid(), FOCUS_FOR_BOTH, __sound_pcm_focus_watch_cb, static_cast<void*>(this), &__mId);
419 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed mm_sound_set_focus_watch_callback_for_session() err:0x%x", errorCode);
424 AUDIO_IO_LOGD("Focus watch callback registered successfully [id:%d]", __mId);
429 void CAudioSessionHandler::unregisterSound() {
430 if (__mIsInit == false) {
431 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioSessionHandler");
434 if (__mUseFocus == true && __mId >= 0) {
437 if (__isFocusRequired(__mMultimediaSession, __mOptions)) {
438 // Unregister focus callback
439 errorCode = mm_sound_unregister_focus(__mId);
440 if (errorCode != MM_ERROR_NONE) {
441 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed mm_sound_unregister_focus() err:0x%x", errorCode);
446 AUDIO_IO_LOGD("Focus callback unregistered successfully [id:%d]", __mId);
448 } else if (!(__mOptions & MM_SESSION_OPTION_UNINTERRUPTIBLE)) {
449 // Unregister focus watch callback.
450 errorCode = mm_sound_unset_focus_watch_callback(__mId);
452 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed mm_sound_unset_focus_watch_callback() err:0x%x", errorCode);
457 AUDIO_IO_LOGD("Focus watch callback unregistered successfully [id:%d]", __mId);
460 __mAcquiredFocus = FOCUS_NONE;
464 void CAudioSessionHandler::updatePlaying() {
465 if (__mIsInit == false) {
466 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioSessionHandler");
469 if (__mUseFocus && __isFocusRequired(__mMultimediaSession, __mOptions)) {
471 int ret = MM_ERROR_NONE;
473 bool is_focus_cb_thread;
475 if ((ret = mm_sound_focus_is_cb_thread(&is_focus_cb_thread)))
476 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed mm_sound_focus_is_cb_thread() err:0x%x", ret);
478 if (!is_focus_cb_thread)
479 __lockFocusCBMutex();
481 if (__mAcquiredFocus == FOCUS_FOR_BOTH) {
482 AUDIO_IO_LOGW("Focus was already acquired, skip it...");
483 if (!is_focus_cb_thread)
484 __unlockFocusCBMutex();
488 focus_type |= (FOCUS_FOR_BOTH & ~__mAcquiredFocus);
489 if (__mMultimediaSession == MM_SESSION_TYPE_MEDIA)
490 ret = mm_sound_acquire_focus_with_option(__mId, (mm_sound_focus_type_e)focus_type, 1, "audio-io acquire focus"); /* option: 1 for no-resume */
492 ret = mm_sound_acquire_focus(__mId, (mm_sound_focus_type_e)focus_type, "audio-io acquire focus");
493 if (ret != MM_ERROR_NONE) {
494 if (!is_focus_cb_thread)
495 __unlockFocusCBMutex();
496 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_POLICY_BLOCKED, "Failed mm_sound_acquire_focus() err:0x%x", ret);
498 __mAcquiredFocus = FOCUS_FOR_BOTH;
499 AUDIO_IO_LOGD("Focus acquired successfully [id:%d]", __mId);
501 if (!is_focus_cb_thread)
502 __unlockFocusCBMutex();
507 void CAudioSessionHandler::updateStop() {
508 if (__mIsInit == false) {
509 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioSessionHandler");
512 if (__mUseFocus && __isFocusRequired(__mMultimediaSession, __mOptions)) {
514 int ret = MM_ERROR_NONE;
515 bool is_focus_cb_thread;
517 if ((ret = mm_sound_focus_is_cb_thread(&is_focus_cb_thread)))
518 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed mm_sound_focus_is_cb_thread() err:0x%x", ret);
520 if (!is_focus_cb_thread)
521 __lockFocusCBMutex();
523 if (__mAcquiredFocus == FOCUS_NONE) {
524 AUDIO_IO_LOGW("Focus was already released, skip it...");
525 if (!is_focus_cb_thread)
526 __unlockFocusCBMutex();
530 if (__mMultimediaSession == MM_SESSION_TYPE_MEDIA)
531 ret = mm_sound_release_focus_with_option(__mId, (mm_sound_focus_type_e)__mAcquiredFocus, 1, "audio-io release focus"); /* option: 1 for no-resume */
533 ret = mm_sound_release_focus(__mId, (mm_sound_focus_type_e)__mAcquiredFocus, "audio-io release focus");
534 if (ret != MM_ERROR_NONE) {
535 if (!is_focus_cb_thread)
536 __unlockFocusCBMutex();
537 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed mm_sound_release_focus() err:0x%x", ret);
539 __mAcquiredFocus = FOCUS_NONE;
540 AUDIO_IO_LOGD("Focus released successfully [id:%d]", __mId);
542 if (!is_focus_cb_thread)
543 __unlockFocusCBMutex();
548 void CAudioSessionHandler::disableSessionHandler() {
549 CAudioSessionHandler::updateStop();
550 CAudioSessionHandler::unregisterSound();
552 CAudioSessionHandler::__mUseFocus = false;
556 * class IAudioSessionEventListener
558 IAudioSessionEventListener::EInterruptCode IAudioSessionEventListener::convertInterruptedCode(int code, const char *reason_for_change) {
559 EInterruptCode e = EInterruptCode::INTERRUPT_BY_MEDIA;
562 case FOCUS_IS_ACQUIRED:
563 e = EInterruptCode::INTERRUPT_COMPLETED;
566 case FOCUS_IS_RELEASED:
567 if (!strcmp(reason_for_change, "media")) e = EInterruptCode::INTERRUPT_BY_MEDIA;
568 if (!strcmp(reason_for_change, "radio")) e = EInterruptCode::INTERRUPT_BY_MEDIA;
569 if (!strcmp(reason_for_change, "loopback")) e = EInterruptCode::INTERRUPT_BY_MEDIA;
570 if (!strcmp(reason_for_change, "system")) e = EInterruptCode::INTERRUPT_BY_MEDIA;
571 if (!strcmp(reason_for_change, "alarm")) e = EInterruptCode::INTERRUPT_BY_ALARM;
572 if (!strcmp(reason_for_change, "notification")) e = EInterruptCode::INTERRUPT_BY_NOTIFICATION;
573 if (!strcmp(reason_for_change, "emergency")) e = EInterruptCode::INTERRUPT_BY_EMERGENCY;
574 if (!strcmp(reason_for_change, "voice-information")) e = EInterruptCode::INTERRUPT_BY_MEDIA;
575 if (!strcmp(reason_for_change, "voice-recognition")) e = EInterruptCode::INTERRUPT_BY_MEDIA;
576 if (!strcmp(reason_for_change, "voice-recognition-service")) e = EInterruptCode::INTERRUPT_BY_MEDIA;
577 if (!strcmp(reason_for_change, "ringtone-voip")) e = EInterruptCode::INTERRUPT_BY_CALL;
578 if (!strcmp(reason_for_change, "ringtone-call")) e = EInterruptCode::INTERRUPT_BY_CALL;
579 if (!strcmp(reason_for_change, "voip")) e = EInterruptCode::INTERRUPT_BY_CALL;
580 if (!strcmp(reason_for_change, "call-voice")) e = EInterruptCode::INTERRUPT_BY_CALL;
581 if (!strcmp(reason_for_change, "call-video")) e = EInterruptCode::INTERRUPT_BY_CALL;