Manage current acquired focus state and check it before acquiring/releasing focus
[platform/core/api/audio-io.git] / src / cpp / CAudioSessionHandler.cpp
index 0fe1271..7091980 100644 (file)
@@ -90,8 +90,7 @@ CAudioSessionHandler::CAudioSessionHandler(EAudioSessionType sessionType, CAudio
     __mIsInit(false),
     __mSubscribeId(0),
     __mUseFocus(false),
-    __mFocusType(FOCUS_NONE),
-    __mState(FOCUS_IS_RELEASED),
+    __mAcquiredFocus(FOCUS_NONE),
     __mReasonForChange(NULL),
     __mAdditionalInfo(NULL) {
     __mAudioInfo = audioInfo;
@@ -317,8 +316,11 @@ void CAudioSessionHandler::__sound_pcm_focus_cb(int id, mm_sound_focus_type_e fo
 #ifndef DISABLE_SESSION_BACK_COMP
 
     CAudioSessionHandler* pHandler = static_cast<CAudioSessionHandler*>(user_data);
-    pHandler->__mFocusType       = focus_type;
-    pHandler->__mState           = state;
+
+    if (state == FOCUS_IS_RELEASED)
+        pHandler->__mAcquiredFocus &= ~focus_type;
+    else if (state == FOCUS_IS_ACQUIRED)
+        pHandler->__mAcquiredFocus |= focus_type;
     pHandler->__mReasonForChange = (char *)reason_for_change;
     pHandler->__mAdditionalInfo  = (char *)additional_info;
 
@@ -425,6 +427,7 @@ void CAudioSessionHandler::unregisterSound() throw(CAudioError) {
             AUDIO_IO_LOGD("Focus watch callback unregistered successfully [id:%d]", __mId);
             __mId = -1;
         }
+        __mAcquiredFocus = FOCUS_NONE;
     }
 }
 
@@ -436,13 +439,20 @@ void CAudioSessionHandler::updatePlaying() throw(CAudioError) {
     if (__mUseFocus && __isFocusRequired(__mMultimediaSession, __mOptions)) {
         if (__mId >= 0) {
             int ret = MM_ERROR_NONE;
+            int focus_type = 0;
+            if (__mAcquiredFocus == FOCUS_FOR_BOTH) {
+                AUDIO_IO_LOGW("Focus was already acquired, skip it...");
+                return;
+            }
+            focus_type |= (FOCUS_FOR_BOTH & ~__mAcquiredFocus);
             if (__mMultimediaSession == MM_SESSION_TYPE_MEDIA)
-                ret = mm_sound_acquire_focus_with_option(__mId, FOCUS_FOR_BOTH, 1, "audio-io acquire focus"); /* option: 1 for no-resume */
+                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 */
             else
-                ret = mm_sound_acquire_focus(__mId, FOCUS_FOR_BOTH, "audio-io acquire focus");
+                ret = mm_sound_acquire_focus(__mId, (mm_sound_focus_type_e)focus_type, "audio-io acquire focus");
             if (ret != MM_ERROR_NONE) {
                 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_POLICY_BLOCKED, "Failed mm_sound_acquire_focus() err:0x%x", ret);
             }
+            __mAcquiredFocus = FOCUS_FOR_BOTH;
             AUDIO_IO_LOGD("Focus acquired successfully [id:%d]", __mId);
         }
     }
@@ -456,13 +466,18 @@ void CAudioSessionHandler::updateStop() throw(CAudioError) {
     if (__mUseFocus && __isFocusRequired(__mMultimediaSession, __mOptions)) {
         if (__mId >= 0) {
             int ret = MM_ERROR_NONE;
+            if (__mAcquiredFocus == FOCUS_NONE) {
+                AUDIO_IO_LOGW("Focus was already released, skip it...");
+                return;
+            }
             if (__mMultimediaSession == MM_SESSION_TYPE_MEDIA)
-                ret = mm_sound_release_focus_with_option(__mId, FOCUS_FOR_BOTH, 1, "audio-io release focus"); /* option: 1 for no-resume */
+                ret = mm_sound_release_focus_with_option(__mId, (mm_sound_focus_type_e)__mAcquiredFocus, 1, "audio-io release focus"); /* option: 1 for no-resume */
             else
-                ret = mm_sound_release_focus(__mId, FOCUS_FOR_BOTH, "audio-io release focus");
+                ret = mm_sound_release_focus(__mId, (mm_sound_focus_type_e)__mAcquiredFocus, "audio-io release focus");
             if (ret != MM_ERROR_NONE) {
                 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed mm_sound_release_focus() err:0x%x", ret);
             }
+            __mAcquiredFocus = FOCUS_NONE;
             AUDIO_IO_LOGD("Focus released successfully [id:%d]", __mId);
         }
     }