Fix bug regarding ignore session
[platform/core/api/audio-io.git] / src / cpp / CAudioSessionHandler.cpp
index 07d3690..5a6dbaf 100644 (file)
@@ -88,7 +88,7 @@ CAudioSessionHandler::CAudioSessionHandler(EAudioSessionType sessionType, CAudio
     __mMultimediaSession(MM_SESSION_TYPE_MEDIA),
     __mpEventListener(listener),
     __mIsInit(false),
-    __mSubscribeId(-1),
+    __mSubscribeId(0),
     __mUseFocus(false),
     __mFocusType(FOCUS_NONE),
     __mState(FOCUS_IS_RELEASED),
@@ -176,7 +176,7 @@ MMSessionType CAudioSessionHandler::getMultimediaSession() {
     return __mMultimediaSession;
 }
 
-int CAudioSessionHandler::getSubscribeId() {
+unsigned int CAudioSessionHandler::getSubscribeId() {
     return __mSubscribeId;
 }
 
@@ -190,12 +190,11 @@ void CAudioSessionHandler::__sound_pcm_signal_cb(mm_sound_signal_name_t signal,
     AUDIO_IO_LOGD("[signal:%d], [value:%d], [user_data:0x%x]", signal, value, user_data);
 
     CAudioSessionHandler* pHandler = static_cast<CAudioSessionHandler*>(user_data);
-    if (pHandler->__mpEventListener != NULL) {
+    if (pHandler->__mpEventListener != NULL)
         pHandler->__mpEventListener->onSignal(pHandler, signal, value);
-    }
 }
 
-void CAudioSessionHandler::initialize() throw (CAudioError) {
+void CAudioSessionHandler::initialize() throw(CAudioError) {
     AUDIO_IO_LOGD("");
     if (__mIsInit == true) {
         return;
@@ -206,38 +205,30 @@ void CAudioSessionHandler::initialize() throw (CAudioError) {
 
     CAudioError err = __getAsmInformation(&currentSession, &sessionOptions);
     if (err == CAudioError::EError::ERROR_NONE) {
-        // Session was configured before, use focus callback
-        __mUseFocus = true;
-        AUDIO_IO_LOGD("Use audio focus concept internally!");
+        if (currentSession == MM_SESSION_TYPE_REPLACED_BY_STREAM) {
+            __mUseFocus = false;
+            AUDIO_IO_LOGD("Stream info. was created outside, skip audio focus concept internally!");
+        } else {
+            // Session was configured before, use focus callback
+            __mUseFocus = true;
+            AUDIO_IO_LOGD("Use audio focus concept internally!");
+        }
     } else {
         if (err == CAudioError::EError::ERROR_INVALID_HANDLE) {
-            int value = 0;
-            unsigned int subscribe_id;
+            // No session, No stream_info, No focus watch callback before
+            // Use focus watch callback with signal subscribe
 
-            int errorCode = mm_sound_get_signal_value(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &value);
-            if (errorCode != MM_ERROR_NONE) {
-                THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_POLICY_BLOCKED, "Failed mm_sound_get_signal_value() err:0x%x", errorCode);
+            int errorCode = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &__mSubscribeId, __sound_pcm_signal_cb, static_cast<void*>(this));
+            if (errorCode != MM_ERROR_NONE || __mSubscribeId == 0) {
+                THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed mm_sound_subscribe_signal() err:0x%x, __mSubscribeId:%u",
+                                       errorCode, __mSubscribeId);
             }
 
-            if (value == 1) {
-                // stream_info was created or focus watch callback was configured before
-                __mUseFocus = false;
-                AUDIO_IO_LOGD("Skip audio focus concept!");
-            } else if (value == 0) {
-                // No session, No stream_info, No focus watch callback before
-                // Use focus watch callback with signal subscribe
-                errorCode = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &subscribe_id, __sound_pcm_signal_cb, static_cast<void*>(this));
-                if (errorCode != MM_ERROR_NONE) {
-                    THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_POLICY_BLOCKED, "Failed mm_sound_get_signal_value() err:0x%x", errorCode);
-                }
-
-                __mSubscribeId = (int)subscribe_id;
-                AUDIO_IO_LOGD("Subscribed mm_sound signal");
+            AUDIO_IO_LOGD("Subscribed mm_sound signal");
 
-                sessionOptions = 0;  // Mix with others by default
-                __mUseFocus = true;
-                AUDIO_IO_LOGD("Use audio focus(watch) concept internally!");
-            }
+            sessionOptions = 0;  // Mix with others by default
+            __mUseFocus = true;
+            AUDIO_IO_LOGD("Use audio focus(watch) concept internally!");
         } else {
             __mUseFocus = false;
             AUDIO_IO_LOGD("Skip audio focus concept!");
@@ -273,14 +264,17 @@ void CAudioSessionHandler::finalize() {
         __pcmCaptureCountDec();
     }
 
-    if (__mSubscribeId >= 0) {
+    if (__mSubscribeId > 0) {
         mm_sound_unsubscribe_signal(__mSubscribeId);
+        __mSubscribeId = 0;
     }
 
+    __mpEventListener = NULL;
+
     __mIsInit = false;
 }
 
-bool CAudioSessionHandler::isSkipSessionEvent() throw (CAudioError) {
+bool CAudioSessionHandler::isSkipSessionEvent() throw(CAudioError) {
     bool ret = false;
 
     // To be regarded...
@@ -289,7 +283,6 @@ bool CAudioSessionHandler::isSkipSessionEvent() throw (CAudioError) {
     if (mEvent != ASM_EVENT_CALL              && mEvent != ASM_EVENT_VOIP              &&
         mEvent != ASM_EVENT_VIDEOCALL         && mEvent != ASM_EVENT_VOICE_RECOGNITION &&
         mEvent != ASM_EVENT_MMCAMCORDER_AUDIO && mEvent != ASM_EVENT_MMCAMCORDER_VIDEO) {
-
         // Check AudioType
         switch (__mAudioInfo.getAudioType()) {
         case CAudioInfo::AUDIO_IN_TYPE_MEDIA:
@@ -322,33 +315,35 @@ bool CAudioSessionHandler::isSkipSessionEvent() throw (CAudioError) {
     return ret;
 }
 
-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) {
+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, int option, const char *additional_info, void *user_data) {
     assert(user_data);
 
     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);
 
+/* FIXME: disable it temporarily */
+#ifndef DISABLE_SESSION_BACK_COMP
+
     CAudioSessionHandler* pHandler = static_cast<CAudioSessionHandler*>(user_data);
     pHandler->__mFocusType       = focus_type;
     pHandler->__mState           = state;
     pHandler->__mReasonForChange = (char *)reason_for_change;
     pHandler->__mAdditionalInfo  = (char *)additional_info;
 
-    if (pHandler->__mpEventListener != NULL) {
+    if (pHandler->__mpEventListener != NULL)
         pHandler->__mpEventListener->onInterrupt(pHandler, id, focus_type, state, reason_for_change, additional_info);
-    }
-
+#endif
     return;
 }
 
 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) {
     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);
 
-    CAudioSessionHandler::__sound_pcm_focus_cb(-1, focus_type, state, reason_for_change, additional_info, user_data);
+    CAudioSessionHandler::__sound_pcm_focus_cb(-1, focus_type, state, reason_for_change, 0, additional_info, user_data);
 
     return;
 }
 
-void CAudioSessionHandler::registerSound() throw (CAudioError) {
+void CAudioSessionHandler::registerSound() throw(CAudioError) {
     if (__mIsInit == false) {
         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioSessionHandler");
     }
@@ -369,7 +364,7 @@ void CAudioSessionHandler::registerSound() throw (CAudioError) {
 
             errorCode = mm_sound_focus_get_id(&__mId);
             if (errorCode != MM_ERROR_NONE) {
-                THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_POLICY_BLOCKED, "Failed mm_sound_focus_get_id() err:0x%x", errorCode);
+                THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed mm_sound_focus_get_id() err:0x%x", errorCode);
             }
 
             // Register focus callback
@@ -379,7 +374,7 @@ void CAudioSessionHandler::registerSound() throw (CAudioError) {
                                                 __sound_pcm_focus_cb,
                                                 static_cast<void*>(this));
             if (errorCode != MM_ERROR_NONE) {
-                THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_POLICY_BLOCKED, "Failed mm_sound_register_focus_for_session() err:0x%x", errorCode);
+                THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed mm_sound_register_focus_for_session() err:0x%x", errorCode);
             }
 
             __focusIdCountInc();
@@ -389,7 +384,7 @@ void CAudioSessionHandler::registerSound() throw (CAudioError) {
             // Register focus watch callback
             errorCode = mm_sound_set_focus_watch_callback_for_session(getpid(), FOCUS_FOR_BOTH, __sound_pcm_focus_watch_cb, static_cast<void*>(this), &__mId);
             if (errorCode < 0) {
-                THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_POLICY_BLOCKED, "Failed mm_sound_set_focus_watch_callback_for_session() err:0x%x", errorCode);
+                THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed mm_sound_set_focus_watch_callback_for_session() err:0x%x", errorCode);
             }
 
             __focusIdCountInc();
@@ -399,23 +394,19 @@ void CAudioSessionHandler::registerSound() throw (CAudioError) {
     }
 }
 
-void CAudioSessionHandler::unregisterSound() throw (CAudioError) {
+void CAudioSessionHandler::unregisterSound() throw(CAudioError) {
     if (__mIsInit == false) {
         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioSessionHandler");
     }
 
-    if (__mUseFocus == true) {
-        if (__mId < 0) {
-            THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_POLICY_BLOCKED, "Did not register [id:%d]", __mId);
-        }
-
+    if (__mUseFocus == true && __mId >= 0) {
         int errorCode = 0;
 
         if (__isFocusRequired(__mMultimediaSession, __mOptions)) {
             // Unregister focus callback
             errorCode = mm_sound_unregister_focus(__mId);
             if (errorCode != MM_ERROR_NONE) {
-                THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_POLICY_BLOCKED, "Failed mm_sound_unregister_focus() err:0x%x", errorCode);
+                THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed mm_sound_unregister_focus() err:0x%x", errorCode);
             }
 
             __focusIdCountDec();
@@ -426,7 +417,7 @@ void CAudioSessionHandler::unregisterSound() throw (CAudioError) {
             // Unregister focus watch callback.
             errorCode = mm_sound_unset_focus_watch_callback(__mId);
             if (errorCode < 0) {
-                THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_POLICY_BLOCKED, "Failed mm_sound_unset_focus_watch_callback() err:0x%x", errorCode);
+                THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed mm_sound_unset_focus_watch_callback() err:0x%x", errorCode);
             }
 
             __focusIdCountDec();
@@ -437,7 +428,7 @@ void CAudioSessionHandler::unregisterSound() throw (CAudioError) {
     }
 }
 
-void CAudioSessionHandler::updatePlaying() throw (CAudioError) {
+void CAudioSessionHandler::updatePlaying() throw(CAudioError) {
     if (__mIsInit == false) {
         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioSessionHandler");
     }
@@ -453,7 +444,7 @@ void CAudioSessionHandler::updatePlaying() throw (CAudioError) {
     }
 }
 
-void CAudioSessionHandler::updateStop() throw (CAudioError) {
+void CAudioSessionHandler::updateStop() throw(CAudioError) {
     if (__mIsInit == false) {
         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioSessionHandler");
     }
@@ -462,14 +453,14 @@ void CAudioSessionHandler::updateStop() throw (CAudioError) {
         if (__mId >= 0) {
             int ret = mm_sound_release_focus(__mId, FOCUS_FOR_BOTH, "audio-io release focus");
             if (ret != MM_ERROR_NONE) {
-                THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_POLICY_BLOCKED, "Failed mm_sound_release_focus() err:0x%x", ret);
+                THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed mm_sound_release_focus() err:0x%x", ret);
             }
             AUDIO_IO_LOGD("Focus released successfully [id:%d]", __mId);
         }
     }
 }
 
-void CAudioSessionHandler::disableSessionHandler() throw (CAudioError) {
+void CAudioSessionHandler::disableSessionHandler() throw(CAudioError) {
     CAudioSessionHandler::updateStop();
     CAudioSessionHandler::unregisterSound();
 
@@ -482,8 +473,7 @@ void CAudioSessionHandler::disableSessionHandler() throw (CAudioError) {
 IAudioSessionEventListener::EInterruptCode IAudioSessionEventListener::convertInterruptedCode(int code, const char *reason_for_change) {
     EInterruptCode e = EInterruptCode::INTERRUPT_COMPLETED;
 
-    switch (code)
-    {
+    switch (code) {
     case FOCUS_IS_ACQUIRED:
         e = EInterruptCode::INTERRUPT_COMPLETED;
         break;