Add session mutex lock
[platform/core/api/audio-io.git] / src / cpp / CAudioSessionHandler.cpp
index 771910e..ceaf3ce 100644 (file)
@@ -81,19 +81,33 @@ int CAudioSessionHandler::__focusIdCountGet() {
     return __sFocusRef;
 }
 
+void CAudioSessionHandler::__lockFocusIdMutex() {
+    if (pthread_mutex_lock(&__mFocusIdMutex) != 0)
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pthread_mutex_lock() - FocusId Mutex");
+#ifdef _AUDIO_IO_DEBUG_TIMING_
+    AUDIO_IO_LOGD(COLOR_RED "LOCK - FocusId Mutex" COLOR_END);
+#endif
+}
+
+void CAudioSessionHandler::__unlockFocusIdMutex() {
+    if (pthread_mutex_unlock(&__mFocusIdMutex) != 0)
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pthread_mutex_unlock() - FocusId Mutex");
+#ifdef _AUDIO_IO_DEBUG_TIMING_
+    AUDIO_IO_LOGD(COLOR_GREEN "UNLOCK - FocusId Mutex" COLOR_END);
+#endif
+}
+
 void CAudioSessionHandler::__lockFocusCBMutex() {
-    if (pthread_mutex_lock(&__mFocusCBMutex) != 0) {
-        THROW_ERROR_MSG(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pthread_mutex_lock()");
-    }
+    if (pthread_mutex_lock(&__mFocusCBMutex) != 0)
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pthread_mutex_lock() - FocusCB Mutex");
 #ifdef _AUDIO_IO_DEBUG_TIMING_
     AUDIO_IO_LOGD(COLOR_RED "LOCK - FocusCB Mutex" COLOR_END);
 #endif
 }
 
 void CAudioSessionHandler::__unlockFocusCBMutex() {
-    if (pthread_mutex_unlock(&__mFocusCBMutex) != 0) {
-        THROW_ERROR_MSG(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pthread_mutex_unlock()");
-    }
+    if (pthread_mutex_unlock(&__mFocusCBMutex) != 0)
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pthread_mutex_unlock() - FocusCB Mutex");
 #ifdef _AUDIO_IO_DEBUG_TIMING_
     AUDIO_IO_LOGD(COLOR_GREEN "UNLOCK - FocusCB Mutex" COLOR_END);
 #endif
@@ -111,6 +125,7 @@ CAudioSessionHandler::CAudioSessionHandler(EAudioSessionType sessionType, CAudio
     __mAcquiredFocus(FOCUS_NONE),
     __mReasonForChange(NULL),
     __mAdditionalInfo(NULL),
+    __mFocusIdMutex(PTHREAD_MUTEX_INITIALIZER),
     __mFocusCBMutex(PTHREAD_MUTEX_INITIALIZER) {
     __mAudioInfo = audioInfo;
 }
@@ -157,11 +172,10 @@ CAudioError CAudioSessionHandler::__getAsmInformation(MMSessionType *type, int *
     /* Read session information */
     int ret = 0;
     if ((ret = _mm_session_util_read_information(-1, (int*)&currentSession, &sessionOptions)) < 0) {
-        if (ret == (int) MM_ERROR_INVALID_HANDLE) {
+        if (ret == (int) MM_ERROR_INVALID_HANDLE)
             RET_ERROR_MSG(CAudioError::EError::ERROR_INVALID_HANDLE, "Failed _mm_session_util_read_information(). Invalid handle");
-        } else {
+        else
             RET_ERROR_MSG(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed _mm_session_util_read_information(). Not exist");
-        }
     }
 
     *type    = currentSession;
@@ -219,15 +233,15 @@ MMSessionType CAudioSessionHandler::getMultimediaSession() {
 }
 
 void CAudioSessionHandler::getInternalVoipStreamInfo(sound_stream_info_h *stream_info) {
+#if 0
     int ret;
-
     if ((ret = sound_manager_get_internal_voip_stream_information(stream_info))) {
-        if (ret == SOUND_MANAGER_ERROR_NO_DATA) {
+        if (ret == SOUND_MANAGER_ERROR_NO_DATA)
             AUDIO_IO_LOGW("there's no internal voip stream info.");
-        } else {
+        else
             AUDIO_IO_LOGE("failed to sound_manager_get_internal_voip_stream_information(), ret(0x%x)", ret);
-        }
     }
+#endif
     return;
 }
 
@@ -251,9 +265,8 @@ void CAudioSessionHandler::__sound_pcm_signal_cb(mm_sound_signal_name_t signal,
 
 void CAudioSessionHandler::initialize() {
     AUDIO_IO_LOGD("");
-    if (__mIsInit == true) {
+    if (__mIsInit == true)
         return;
-    }
 
     MMSessionType currentSession = MM_SESSION_TYPE_MEDIA;
     int           sessionOptions = 0;  // Mix with others by default
@@ -274,10 +287,9 @@ void CAudioSessionHandler::initialize() {
             // Use focus watch callback with signal subscribe
 
             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) {
+            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);
-            }
 
             AUDIO_IO_LOGD("Subscribed mm_sound signal [id:%d]", __mSubscribeId);
 
@@ -302,22 +314,19 @@ void CAudioSessionHandler::initialize() {
     __mMultimediaSession = currentSession;
     __mOptions           = sessionOptions;
 
-    if (this->__mAudioSession == EAudioSessionType::AUDIO_SESSION_TYPE_CAPTURE) {
+    if (this->__mAudioSession == EAudioSessionType::AUDIO_SESSION_TYPE_CAPTURE)
         __pcmCaptureCountInc();
-    }
 
     __mIsInit = true;
 }
 
 void CAudioSessionHandler::finalize() {
     AUDIO_IO_LOGD("");
-    if (__mIsInit == false) {
+    if (__mIsInit == false)
         return;
-    }
 
-    if (__mAudioSession == EAudioSessionType::AUDIO_SESSION_TYPE_CAPTURE) {
+    if (__mAudioSession == EAudioSessionType::AUDIO_SESSION_TYPE_CAPTURE)
         __pcmCaptureCountDec();
-    }
 
     unregisterSound();
 
@@ -384,12 +393,13 @@ void CAudioSessionHandler::__sound_pcm_focus_watch_cb(int id, mm_sound_focus_typ
 }
 
 void CAudioSessionHandler::registerSound() {
-    if (__mIsInit == false) {
+    if (__mIsInit == false)
         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioSessionHandler");
-    }
 
     if (__mUseFocus == true) {
+        __lockFocusIdMutex();
         if (__mId >= 0) {
+            __unlockFocusIdMutex();
             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_POLICY_BLOCKED, "Already registered [id:%d]", __mId);
         }
 
@@ -400,11 +410,13 @@ void CAudioSessionHandler::registerSound() {
 
             CAudioError err = __convertStreamType(__mAudioSession, __mMultimediaSession, &index);
             if (err != CAudioError::EError::ERROR_NONE) {
+                __unlockFocusIdMutex();
                 throw err;
             }
 
             errorCode = mm_sound_focus_get_id(&__mId);
             if (errorCode != MM_ERROR_NONE) {
+                __unlockFocusIdMutex();
                 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed mm_sound_focus_get_id() err:0x%x", errorCode);
             }
 
@@ -415,12 +427,14 @@ void CAudioSessionHandler::registerSound() {
                                                 __sound_pcm_focus_cb,
                                                 static_cast<void*>(this));
             if (errorCode != MM_ERROR_NONE) {
+                __unlockFocusIdMutex();
                 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed mm_sound_register_focus_for_session() err:0x%x", errorCode);
             }
 
             if (__isFocusDisableReacquisitionRequired(__mMultimediaSession, __mOptions)) {
                 errorCode = mm_sound_set_focus_reacquisition_for_session(__mId, false);
                 if (errorCode != MM_ERROR_NONE) {
+                    __unlockFocusIdMutex();
                     THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed mm_sound_set_focus_reacquisition() err:0x%x", errorCode);
                 }
             }
@@ -432,6 +446,7 @@ void CAudioSessionHandler::registerSound() {
             // 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) {
+                __unlockFocusIdMutex();
                 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed mm_sound_set_focus_watch_callback_for_session() err:0x%x", errorCode);
             }
 
@@ -439,14 +454,15 @@ void CAudioSessionHandler::registerSound() {
 
             AUDIO_IO_LOGD("Focus watch callback registered successfully [id:%d]", __mId);
         }
+        __unlockFocusIdMutex();
     }
 }
 
 void CAudioSessionHandler::unregisterSound() {
-    if (__mIsInit == false) {
+    if (__mIsInit == false)
         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioSessionHandler");
-    }
 
+    __lockFocusIdMutex();
     if (__mUseFocus == true && __mId >= 0) {
         int errorCode = 0;
 
@@ -454,6 +470,7 @@ void CAudioSessionHandler::unregisterSound() {
             // Unregister focus callback
             errorCode = mm_sound_unregister_focus(__mId);
             if (errorCode != MM_ERROR_NONE) {
+                __unlockFocusIdMutex();
                 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed mm_sound_unregister_focus() err:0x%x", errorCode);
             }
 
@@ -465,6 +482,7 @@ void CAudioSessionHandler::unregisterSound() {
             // Unregister focus watch callback.
             errorCode = mm_sound_unset_focus_watch_callback(__mId);
             if (errorCode < 0) {
+                __unlockFocusIdMutex();
                 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed mm_sound_unset_focus_watch_callback() err:0x%x", errorCode);
             }
 
@@ -475,14 +493,18 @@ void CAudioSessionHandler::unregisterSound() {
         }
         __mAcquiredFocus = FOCUS_NONE;
     }
+    __unlockFocusIdMutex();
 }
 
 void CAudioSessionHandler::updatePlaying() {
-    if (__mIsInit == false) {
+    if (__mIsInit == false)
         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioSessionHandler");
-    }
-    if (!__mUseFocus || __mId < 0)
+
+    __lockFocusIdMutex();
+    if (!__mUseFocus || __mId < 0) {
+        __unlockFocusIdMutex();
         return;
+    }
 
     if (__mUseFocus && __isFocusRequired(__mMultimediaSession, __mOptions)) {
         if (__mId >= 0) {
@@ -490,8 +512,10 @@ void CAudioSessionHandler::updatePlaying() {
             int focus_type = 0;
             bool is_focus_cb_thread;
 
-            if ((ret = mm_sound_focus_is_cb_thread(&is_focus_cb_thread)))
+            if ((ret = mm_sound_focus_is_cb_thread(&is_focus_cb_thread))) {
+                __unlockFocusIdMutex();
                 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed mm_sound_focus_is_cb_thread() err:0x%x", ret);
+            }
 
             if (!is_focus_cb_thread)
                 __lockFocusCBMutex();
@@ -500,6 +524,7 @@ void CAudioSessionHandler::updatePlaying() {
                 AUDIO_IO_LOGW("Focus was already acquired, skip it...");
                 if (!is_focus_cb_thread)
                     __unlockFocusCBMutex();
+                __unlockFocusIdMutex();
                 return;
             }
 
@@ -511,6 +536,7 @@ void CAudioSessionHandler::updatePlaying() {
             if (ret != MM_ERROR_NONE) {
                 if (!is_focus_cb_thread)
                     __unlockFocusCBMutex();
+                __unlockFocusIdMutex();
                 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_POLICY_BLOCKED, "Failed mm_sound_acquire_focus() err:0x%x", ret);
             }
             __mAcquiredFocus = FOCUS_FOR_BOTH;
@@ -525,33 +551,39 @@ void CAudioSessionHandler::updatePlaying() {
         char *ext_info = NULL;
         int option = 0;
 
-        if ((ret = mm_sound_get_stream_type_of_acquired_focus(FOCUS_FOR_BOTH, &stream_type, &option, &ext_info)))
+        if ((ret = mm_sound_get_stream_type_of_acquired_focus(FOCUS_FOR_BOTH, &stream_type, &option, &ext_info))) {
+            __unlockFocusIdMutex();
             return;
+        }
 
         AUDIO_IO_LOGD("Focus is acquired by stream_type[%s], option[%d], ext_info[%s]", stream_type, option, ext_info);
 
         if (__checkNeedBlock((const char*)stream_type)) {
             free(stream_type);
             free(ext_info);
+            __unlockFocusIdMutex();
             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_POLICY_BLOCKED, "Blocked by an acquired focus");
         }
         free(stream_type);
         free(ext_info);
     }
+    __unlockFocusIdMutex();
 }
 
 void CAudioSessionHandler::updateStop() {
-    if (__mIsInit == false) {
+    if (__mIsInit == false)
         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioSessionHandler");
-    }
 
     if (__mUseFocus && __isFocusRequired(__mMultimediaSession, __mOptions)) {
+        __lockFocusIdMutex();
         if (__mId >= 0) {
             int ret = MM_ERROR_NONE;
             bool is_focus_cb_thread;
 
-            if ((ret = mm_sound_focus_is_cb_thread(&is_focus_cb_thread)))
+            if ((ret = mm_sound_focus_is_cb_thread(&is_focus_cb_thread))) {
+                __unlockFocusIdMutex();
                 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed mm_sound_focus_is_cb_thread() err:0x%x", ret);
+            }
 
             if (!is_focus_cb_thread)
                 __lockFocusCBMutex();
@@ -560,6 +592,7 @@ void CAudioSessionHandler::updateStop() {
                 AUDIO_IO_LOGW("Focus was already released, skip it...");
                 if (!is_focus_cb_thread)
                     __unlockFocusCBMutex();
+                __unlockFocusIdMutex();
                 return;
             }
 
@@ -570,6 +603,7 @@ void CAudioSessionHandler::updateStop() {
             if (ret != MM_ERROR_NONE) {
                 if (!is_focus_cb_thread)
                     __unlockFocusCBMutex();
+                __unlockFocusIdMutex();
                 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed mm_sound_release_focus() err:0x%x", ret);
             }
             __mAcquiredFocus = FOCUS_NONE;
@@ -578,6 +612,7 @@ void CAudioSessionHandler::updateStop() {
             if (!is_focus_cb_thread)
                 __unlockFocusCBMutex();
         }
+        __unlockFocusIdMutex();
     }
 }