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 CAudioSessionHandler::CAudioSessionHandler(EAudioSessionType sessionType, CAudioInfo& audioInfo, IAudioSessionEventListener* listener) :
87 __mAudioSession(sessionType),
88 __mMultimediaSession(MM_SESSION_TYPE_MEDIA),
89 __mpEventListener(listener),
93 __mFocusType(FOCUS_NONE),
94 __mState(FOCUS_IS_RELEASED),
95 __mReasonForChange(NULL),
96 __mAdditionalInfo(NULL) {
97 __mAudioInfo = audioInfo;
100 CAudioSessionHandler::~CAudioSessionHandler() {
103 CAudioError CAudioSessionHandler::__convertStreamType(EAudioSessionType type1, MMSessionType type2, int *index) {
107 assert(index != NULL);
109 if (type1 == EAudioSessionType::AUDIO_SESSION_TYPE_CAPTURE) {
110 for (i = 0 ; i < sizeof(__STREAM_TYPE_TABLE_IN) / sizeof(__STREAM_TYPE_TABLE_IN[0]) ; i++) {
111 if (__STREAM_TYPE_TABLE_IN[i].type == type2) {
117 for (i = 0 ; i < sizeof(__STREAM_TYPE_TABLE_OUT) / sizeof(__STREAM_TYPE_TABLE_OUT[0]) ; i++) {
118 if (__STREAM_TYPE_TABLE_OUT[i].type == type2) {
126 RET_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "Does not support session type.");
129 RET_ERROR(CAudioError::EError::ERROR_NONE);
132 CAudioError CAudioSessionHandler::__getAsmInformation(MMSessionType *type, int *options) {
133 assert(type != NULL);
134 assert(options != NULL);
136 MMSessionType currentSession = MM_SESSION_TYPE_MEDIA;
137 int sessionOptions = 0;
139 /* Read session information */
141 if ((ret = _mm_session_util_read_information(-1, (int*)¤tSession, &sessionOptions)) < 0) {
142 if (ret == (int) MM_ERROR_INVALID_HANDLE) {
143 RET_ERROR_MSG(CAudioError::EError::ERROR_INVALID_HANDLE, "Failed _mm_session_util_read_information(). Invalid handle");
145 RET_ERROR_MSG(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed _mm_session_util_read_information(). Not exist");
149 *type = currentSession;
150 *options = sessionOptions;
152 RET_ERROR(CAudioError::EError::ERROR_NONE);
155 bool CAudioSessionHandler::__isFocusRequired(MMSessionType type, int options) {
156 if ((options & MM_SESSION_OPTION_PAUSE_OTHERS)
157 || ((type != MM_SESSION_TYPE_MEDIA) && (type != MM_SESSION_TYPE_MEDIA_RECORD)))
163 int CAudioSessionHandler::getId() {
167 int CAudioSessionHandler::getOptions() {
171 CAudioSessionHandler::EAudioSessionType CAudioSessionHandler::getAudioSession() {
172 return __mAudioSession;
175 MMSessionType CAudioSessionHandler::getMultimediaSession() {
176 return __mMultimediaSession;
179 int CAudioSessionHandler::getSubscribeId() {
180 return __mSubscribeId;
183 CAudioInfo CAudioSessionHandler::getAudioInfo() {
187 void CAudioSessionHandler::__sound_pcm_signal_cb(mm_sound_signal_name_t signal, int value, void *user_data) {
190 AUDIO_IO_LOGD("[signal:%d], [value:%d], [user_data:0x%x]", signal, value, user_data);
192 CAudioSessionHandler* pHandler = static_cast<CAudioSessionHandler*>(user_data);
193 if (pHandler->__mpEventListener != NULL) {
194 pHandler->__mpEventListener->onSignal(pHandler, signal, value);
198 void CAudioSessionHandler::initialize() throw (CAudioError) {
200 if (__mIsInit == true) {
204 MMSessionType currentSession = MM_SESSION_TYPE_MEDIA;
205 int sessionOptions = 0; // Mix with others by default
207 CAudioError err = __getAsmInformation(¤tSession, &sessionOptions);
208 if (err == CAudioError::EError::ERROR_NONE) {
209 if (currentSession == MM_SESSION_TYPE_REPLACED_BY_STREAM) {
211 AUDIO_IO_LOGD("Stream info. was created outside, skip audio focus concept internally!");
213 // Session was configured before, use focus callback
215 AUDIO_IO_LOGD("Use audio focus concept internally!");
218 if (err == CAudioError::EError::ERROR_INVALID_HANDLE) {
219 // No session, No stream_info, No focus watch callback before
220 // Use focus watch callback with signal subscribe
221 unsigned int subscribe_id;
222 int errorCode = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &subscribe_id, __sound_pcm_signal_cb, static_cast<void*>(this));
223 if (errorCode != MM_ERROR_NONE) {
224 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_POLICY_BLOCKED, "Failed mm_sound_subscribe_signal() err:0x%x", errorCode);
227 __mSubscribeId = (int)subscribe_id;
228 AUDIO_IO_LOGD("Subscribed mm_sound signal");
230 sessionOptions = 0; // Mix with others by default
232 AUDIO_IO_LOGD("Use audio focus(watch) concept internally!");
235 AUDIO_IO_LOGD("Skip audio focus concept!");
238 if (__mAudioSession == EAudioSessionType::AUDIO_SESSION_TYPE_CAPTURE) {
239 AUDIO_IO_LOGD("Set default \"Media_Record\" type");
240 currentSession = MM_SESSION_TYPE_MEDIA_RECORD;
242 AUDIO_IO_LOGD("Set default \"Media\" type");
243 currentSession = MM_SESSION_TYPE_MEDIA;
247 // Updates session information
248 __mMultimediaSession = currentSession;
249 __mOptions = sessionOptions;
251 if (this->__mAudioSession == EAudioSessionType::AUDIO_SESSION_TYPE_CAPTURE) {
252 __pcmCaptureCountInc();
258 void CAudioSessionHandler::finalize() {
260 if (__mIsInit == false) {
264 if (__mAudioSession == EAudioSessionType::AUDIO_SESSION_TYPE_CAPTURE) {
265 __pcmCaptureCountDec();
268 if (__mSubscribeId >= 0) {
269 mm_sound_unsubscribe_signal(__mSubscribeId);
275 bool CAudioSessionHandler::isSkipSessionEvent() throw (CAudioError) {
280 /* Only Support below Event */
281 if (mEvent != ASM_EVENT_CALL && mEvent != ASM_EVENT_VOIP &&
282 mEvent != ASM_EVENT_VIDEOCALL && mEvent != ASM_EVENT_VOICE_RECOGNITION &&
283 mEvent != ASM_EVENT_MMCAMCORDER_AUDIO && mEvent != ASM_EVENT_MMCAMCORDER_VIDEO) {
286 switch (__mAudioInfo.getAudioType()) {
287 case CAudioInfo::AUDIO_IN_TYPE_MEDIA:
288 case CAudioInfo::AUDIO_IN_TYPE_VOICECONTROL:
292 case CAudioInfo::AUDIO_IN_TYPE_MIRRORING:
293 case CAudioInfo::AUDIO_IN_TYPE_LOOPBACK:
302 int captureCount = CAudioSessionHandler::__pcmCaptureCountGet();
303 if (captureCount == 1) {/* If this is last one */
304 /* Recover session information to MEDIA */
305 int sessionResult = _mm_session_util_write_information(-1, MM_SESSION_TYPE_MEDIA, __mOptions);
306 if (sessionResult != 0) {
307 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INTERNAL_OPERATION, "Failed _mm_session_util_write_information() ret:%d", sessionResult);
317 void CAudioSessionHandler::__sound_pcm_focus_cb(int id, mm_sound_focus_type_e focus_type, mm_sound_focus_state_e state, const char *reason_for_change, const char *additional_info, void *user_data) {
320 AUDIO_IO_LOGD("[id:%d], [focus_type:%d], [state:%d], [reason_for_change:%s], [additional_info:%s], [user_data:0x%x]", id, focus_type, state, reason_for_change, additional_info, user_data);
322 CAudioSessionHandler* pHandler = static_cast<CAudioSessionHandler*>(user_data);
323 pHandler->__mFocusType = focus_type;
324 pHandler->__mState = state;
325 pHandler->__mReasonForChange = (char *)reason_for_change;
326 pHandler->__mAdditionalInfo = (char *)additional_info;
328 if (pHandler->__mpEventListener != NULL) {
329 pHandler->__mpEventListener->onInterrupt(pHandler, id, focus_type, state, reason_for_change, additional_info);
335 void CAudioSessionHandler::__sound_pcm_focus_watch_cb(int id, mm_sound_focus_type_e focus_type, mm_sound_focus_state_e state, const char *reason_for_change, const char *additional_info, void *user_data) {
336 AUDIO_IO_LOGD("[id:%d], [focus_type:%d], [state:%d], [reason_for_change:%s], [additional_info:%s], [user_data:0x%x]", id, focus_type, state, reason_for_change, additional_info, user_data);
338 CAudioSessionHandler::__sound_pcm_focus_cb(-1, focus_type, state, reason_for_change, additional_info, user_data);
343 void CAudioSessionHandler::registerSound() throw (CAudioError) {
344 if (__mIsInit == false) {
345 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioSessionHandler");
348 if (__mUseFocus == true) {
350 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_POLICY_BLOCKED, "Already registered [id:%d]", __mId);
355 if (__isFocusRequired(__mMultimediaSession, __mOptions)) {
357 CAudioError err = __convertStreamType(__mAudioSession, __mMultimediaSession, &index);
358 if (err != CAudioError::EError::ERROR_NONE) {
362 errorCode = mm_sound_focus_get_id(&__mId);
363 if (errorCode != MM_ERROR_NONE) {
364 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_POLICY_BLOCKED, "Failed mm_sound_focus_get_id() err:0x%x", errorCode);
367 // Register focus callback
368 errorCode = mm_sound_register_focus_for_session(__mId,
370 __mAudioSession == EAudioSessionType::AUDIO_SESSION_TYPE_CAPTURE ? __STREAM_TYPE_TABLE_IN[index].name : __STREAM_TYPE_TABLE_OUT[index].name,
371 __sound_pcm_focus_cb,
372 static_cast<void*>(this));
373 if (errorCode != MM_ERROR_NONE) {
374 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_POLICY_BLOCKED, "Failed mm_sound_register_focus_for_session() err:0x%x", errorCode);
379 AUDIO_IO_LOGD("Focus callback registered successfully [id:%d]", __mId);
380 } else if (!(__mOptions & MM_SESSION_OPTION_UNINTERRUPTIBLE)) {
381 // Register focus watch callback
382 errorCode = mm_sound_set_focus_watch_callback_for_session(getpid(), FOCUS_FOR_BOTH, __sound_pcm_focus_watch_cb, static_cast<void*>(this), &__mId);
384 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_POLICY_BLOCKED, "Failed mm_sound_set_focus_watch_callback_for_session() err:0x%x", errorCode);
389 AUDIO_IO_LOGD("Focus watch callback registered successfully [id:%d]", __mId);
394 void CAudioSessionHandler::unregisterSound() throw (CAudioError) {
395 if (__mIsInit == false) {
396 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioSessionHandler");
399 if (__mUseFocus == true) {
401 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_POLICY_BLOCKED, "Did not register [id:%d]", __mId);
406 if (__isFocusRequired(__mMultimediaSession, __mOptions)) {
407 // Unregister focus callback
408 errorCode = mm_sound_unregister_focus(__mId);
409 if (errorCode != MM_ERROR_NONE) {
410 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_POLICY_BLOCKED, "Failed mm_sound_unregister_focus() err:0x%x", errorCode);
415 AUDIO_IO_LOGD("Focus callback unregistered successfully [id:%d]", __mId);
417 } else if (!(__mOptions & MM_SESSION_OPTION_UNINTERRUPTIBLE)) {
418 // Unregister focus watch callback.
419 errorCode = mm_sound_unset_focus_watch_callback(__mId);
421 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_POLICY_BLOCKED, "Failed mm_sound_unset_focus_watch_callback() err:0x%x", errorCode);
426 AUDIO_IO_LOGD("Focus watch callback unregistered successfully [id:%d]", __mId);
432 void CAudioSessionHandler::updatePlaying() throw (CAudioError) {
433 if (__mIsInit == false) {
434 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioSessionHandler");
437 if (__mUseFocus && __isFocusRequired(__mMultimediaSession, __mOptions)) {
439 int ret = mm_sound_acquire_focus(__mId, FOCUS_FOR_BOTH, "audio-io acquire focus");
440 if (ret != MM_ERROR_NONE) {
441 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_POLICY_BLOCKED, "Failed mm_sound_acquire_focus() err:0x%x", ret);
443 AUDIO_IO_LOGD("Focus acquired successfully [id:%d]", __mId);
448 void CAudioSessionHandler::updateStop() throw (CAudioError) {
449 if (__mIsInit == false) {
450 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioSessionHandler");
453 if (__mUseFocus && __isFocusRequired(__mMultimediaSession, __mOptions)) {
455 int ret = mm_sound_release_focus(__mId, FOCUS_FOR_BOTH, "audio-io release focus");
456 if (ret != MM_ERROR_NONE) {
457 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_POLICY_BLOCKED, "Failed mm_sound_release_focus() err:0x%x", ret);
459 AUDIO_IO_LOGD("Focus released successfully [id:%d]", __mId);
464 void CAudioSessionHandler::disableSessionHandler() throw (CAudioError) {
465 CAudioSessionHandler::updateStop();
466 CAudioSessionHandler::unregisterSound();
468 CAudioSessionHandler::__mUseFocus = false;
472 * class IAudioSessionEventListener
474 IAudioSessionEventListener::EInterruptCode IAudioSessionEventListener::convertInterruptedCode(int code, const char *reason_for_change) {
475 EInterruptCode e = EInterruptCode::INTERRUPT_COMPLETED;
479 case FOCUS_IS_ACQUIRED:
480 e = EInterruptCode::INTERRUPT_COMPLETED;
483 case FOCUS_IS_RELEASED:
484 if (!strcmp(reason_for_change, "media")) e = EInterruptCode::INTERRUPT_BY_MEDIA;
485 if (!strcmp(reason_for_change, "radio")) e = EInterruptCode::INTERRUPT_BY_MEDIA;
486 if (!strcmp(reason_for_change, "loopback")) e = EInterruptCode::INTERRUPT_BY_MEDIA;
487 if (!strcmp(reason_for_change, "system")) e = EInterruptCode::INTERRUPT_BY_MEDIA;
488 if (!strcmp(reason_for_change, "alarm")) e = EInterruptCode::INTERRUPT_BY_ALARM;
489 if (!strcmp(reason_for_change, "notification")) e = EInterruptCode::INTERRUPT_BY_NOTIFICATION;
490 if (!strcmp(reason_for_change, "emergency")) e = EInterruptCode::INTERRUPT_BY_EMERGENCY;
491 if (!strcmp(reason_for_change, "voice-information")) e = EInterruptCode::INTERRUPT_BY_MEDIA; //for what?
492 if (!strcmp(reason_for_change, "voice-recognition")) e = EInterruptCode::INTERRUPT_BY_MEDIA; //for what?
493 if (!strcmp(reason_for_change, "ringtone-voip")) e = EInterruptCode::INTERRUPT_BY_MEDIA; //for what?
494 if (!strcmp(reason_for_change, "ringtone-call")) e = EInterruptCode::INTERRUPT_BY_MEDIA; //for what?
495 if (!strcmp(reason_for_change, "voip")) e = EInterruptCode::INTERRUPT_BY_MEDIA; //for what?
496 if (!strcmp(reason_for_change, "call-voice")) e = EInterruptCode::INTERRUPT_BY_MEDIA; //for what?
497 if (!strcmp(reason_for_change, "call-video")) e = EInterruptCode::INTERRUPT_BY_MEDIA; //for what?