Add mutex lock to protect focus state of stream information handle 11/134811/1
authorSangchul Lee <sc11.lee@samsung.com>
Tue, 20 Jun 2017 02:20:45 +0000 (11:20 +0900)
committerSangchul Lee <sc11.lee@samsung.com>
Tue, 20 Jun 2017 05:51:48 +0000 (14:51 +0900)
[Version] 0.4.12
[Issue Type] Enhancement

Change-Id: Iad37b8f3c94300615bc9aaac2bb30261b79f2084
Signed-off-by: Sangchul Lee <sc11.lee@samsung.com>
include/sound_manager_private.h
packaging/capi-media-sound-manager.spec
src/sound_manager.c

index ac41ced..4d0a51b 100644 (file)
@@ -177,6 +177,7 @@ typedef struct _sound_stream_info_s {
        sound_stream_focus_state_changed_cb user_cb;
        void *user_data;
        manual_route_info_s manual_route_info;
+       pthread_mutex_t focus_state_mutex;
 } sound_stream_info_s;
 
 typedef enum {
index 22d8da6..192aeee 100644 (file)
@@ -1,6 +1,6 @@
 Name:       capi-media-sound-manager
 Summary:    Sound Manager library
-Version:    0.4.11
+Version:    0.4.12
 Release:    0
 Group:      Multimedia/API
 License:    Apache-2.0
index 0f50db2..5007106 100644 (file)
@@ -266,7 +266,9 @@ int sound_manager_destroy_stream_information(sound_stream_info_h stream_info)
        SM_INSTANCE_CHECK(stream_h);
 
        SM_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_stream_info_count_mutex, MM_ERROR_SOUND_INTERNAL);
+       SM_ENTER_CRITICAL_SECTION_WITH_RETURN(&stream_h->focus_state_mutex, MM_ERROR_SOUND_INTERNAL);
        ret = _destroy_pa_connection_and_unregister_focus(stream_h);
+       SM_LEAVE_CRITICAL_SECTION(&stream_h->focus_state_mutex);
        if (ret == MM_ERROR_NONE) {
                SM_SAFE_FREE(stream_h);
                SM_UNREF_FOR_STREAM_INFO(g_stream_info_count);
@@ -392,15 +394,19 @@ int sound_manager_acquire_focus(sound_stream_info_h stream_info, sound_stream_fo
                return _convert_sound_manager_error_code(__func__, MM_ERROR_POLICY_INTERNAL);
        }
 
+       SM_ENTER_CRITICAL_SECTION_WITH_RETURN(&stream_h->focus_state_mutex, MM_ERROR_SOUND_INTERNAL);
+
        if (stream_h->acquired_focus & focus_mask) {
                LOGE("invalid state: focus_mask[0x%x], acquired_focus[0x%x]", focus_mask, stream_h->acquired_focus);
-               return _convert_sound_manager_error_code(__func__, MM_ERROR_SOUND_INVALID_STATE);
+               ret = MM_ERROR_SOUND_INVALID_STATE;
+               goto LEAVE;
        }
 
        if (is_focus_cb_thread && ((stream_h->prev_acquired_focus | ~stream_h->acquired_focus) & SOUND_STREAM_FOCUS_FOR_BOTH) != focus_mask) {
                LOGE("just lost focus in this callback, it is not allowed to acquire the focus[0x%x] again. acquired_focus[0x%x], prev[0x%x]",
                        focus_mask, stream_h->acquired_focus, stream_h->prev_acquired_focus);
-               return _convert_sound_manager_error_code(__func__, MM_ERROR_POLICY_INTERNAL);
+               ret = MM_ERROR_POLICY_INTERNAL;
+               goto LEAVE;
        }
 
        ret = mm_sound_acquire_focus_with_option(stream_h->index, (mm_sound_focus_type_e)focus_mask, sound_behavior, extra_info);
@@ -412,6 +418,9 @@ int sound_manager_acquire_focus(sound_stream_info_h stream_info, sound_stream_fo
 
        LOGI("acquired_focus[0x%x], prev[0x%x]", stream_h->acquired_focus, stream_h->prev_acquired_focus);
 
+LEAVE:
+       SM_LEAVE_CRITICAL_SECTION(&stream_h->focus_state_mutex);
+
        return _convert_sound_manager_error_code(__func__, ret);
 }
 
@@ -428,15 +437,19 @@ int sound_manager_release_focus(sound_stream_info_h stream_info, sound_stream_fo
        if ((ret = mm_sound_focus_is_cb_thread(&is_focus_cb_thread)))
                return _convert_sound_manager_error_code(__func__, ret);
 
+       SM_ENTER_CRITICAL_SECTION_WITH_RETURN(&stream_h->focus_state_mutex, MM_ERROR_SOUND_INTERNAL);
+
        if (!(stream_h->acquired_focus & focus_mask)) {
                LOGE("invalid state: focus_mask[0x%x], acquired_focus[0x%x]", focus_mask, stream_h->acquired_focus);
-               return _convert_sound_manager_error_code(__func__, MM_ERROR_SOUND_INVALID_STATE);
+               ret = MM_ERROR_SOUND_INVALID_STATE;
+               goto LEAVE;
        }
 
        if (is_focus_cb_thread && ((stream_h->prev_acquired_focus & stream_h->acquired_focus) != focus_mask)) {
                LOGE("just acquired focus in this callback, it is not allowed to release the focus[0x%x] again. acquired_focus[0x%x], prev[0x%x]",
                        focus_mask, stream_h->acquired_focus, stream_h->prev_acquired_focus);
-               return _convert_sound_manager_error_code(__func__, MM_ERROR_SOUND_INVALID_STATE);
+               ret = MM_ERROR_SOUND_INVALID_STATE;
+               goto LEAVE;
        }
 
        ret = mm_sound_release_focus_with_option(stream_h->index, (mm_sound_focus_type_e)focus_mask, sound_behavior, extra_info);
@@ -448,6 +461,9 @@ int sound_manager_release_focus(sound_stream_info_h stream_info, sound_stream_fo
 
        LOGI("acquired_focus[0x%x], prev[0x%x]", stream_h->acquired_focus, stream_h->prev_acquired_focus);
 
+LEAVE:
+       SM_LEAVE_CRITICAL_SECTION(&stream_h->focus_state_mutex);
+
        return _convert_sound_manager_error_code(__func__, ret);
 }
 
@@ -475,16 +491,20 @@ int sound_manager_acquire_focus_all(sound_stream_info_h stream_info, int sound_b
                return _convert_sound_manager_error_code(__func__, MM_ERROR_POLICY_INTERNAL);
        }
 
+       SM_ENTER_CRITICAL_SECTION_WITH_RETURN(&stream_h->focus_state_mutex, MM_ERROR_SOUND_INTERNAL);
+
        if (is_focus_cb_thread && (stream_h->prev_acquired_focus > stream_h->acquired_focus)) {
                LOGE("just lost focus in this callback, it is not allowed to acquire all again. acquired_focus[0x%x], prev[0x%x]",
                        stream_h->acquired_focus, stream_h->prev_acquired_focus);
-               return _convert_sound_manager_error_code(__func__, MM_ERROR_POLICY_INTERNAL);
+               ret = MM_ERROR_POLICY_INTERNAL;
+               goto LEAVE;
        }
 
        focus_mask = SOUND_STREAM_FOCUS_FOR_BOTH & ~(stream_h->acquired_focus);
        if (!focus_mask) {
                LOGI("PLAYBACK/RECORDING focuses have already been ACQUIRED");
-               return SOUND_MANAGER_ERROR_NONE;
+               ret = MM_ERROR_NONE;
+               goto LEAVE;
        }
 
        ret = mm_sound_acquire_focus_with_option(stream_h->index, (mm_sound_focus_type_e)focus_mask, sound_behavior, extra_info);
@@ -494,6 +514,9 @@ int sound_manager_acquire_focus_all(sound_stream_info_h stream_info, int sound_b
                _update_focus_status(stream_h->index, (unsigned int)stream_h->acquired_focus);
        }
 
+LEAVE:
+       SM_LEAVE_CRITICAL_SECTION(&stream_h->focus_state_mutex);
+
        return _convert_sound_manager_error_code(__func__, ret);
 }
 
@@ -510,15 +533,19 @@ int sound_manager_release_focus_all(sound_stream_info_h stream_info, int sound_b
        if ((ret = mm_sound_focus_is_cb_thread(&is_focus_cb_thread)))
                return _convert_sound_manager_error_code(__func__, ret);
 
+       SM_ENTER_CRITICAL_SECTION_WITH_RETURN(&stream_h->focus_state_mutex, MM_ERROR_SOUND_INTERNAL);
+
        if (is_focus_cb_thread && (stream_h->prev_acquired_focus < stream_h->acquired_focus)) {
                LOGE("just acquired focus in this callback, it is not allowed to release all again. acquired_focus[0x%x], prev[0x%x]",
                        stream_h->acquired_focus, stream_h->prev_acquired_focus);
-               return _convert_sound_manager_error_code(__func__, MM_ERROR_POLICY_INTERNAL);
+               ret = MM_ERROR_POLICY_INTERNAL;
+               goto LEAVE;
        }
 
        if (!stream_h->acquired_focus) {
                LOGI("PLAYBACK/RECORDING focuses have already been RELEASED");
-               return SOUND_MANAGER_ERROR_NONE;
+               ret = MM_ERROR_NONE;
+               goto LEAVE;
        }
 
        ret = mm_sound_release_focus_with_option(stream_h->index, (mm_sound_focus_type_e)stream_h->acquired_focus, sound_behavior, extra_info);
@@ -528,6 +555,9 @@ int sound_manager_release_focus_all(sound_stream_info_h stream_info, int sound_b
                _update_focus_status(stream_h->index, (unsigned int)stream_h->acquired_focus);
        }
 
+LEAVE:
+       SM_LEAVE_CRITICAL_SECTION(&stream_h->focus_state_mutex);
+
        return _convert_sound_manager_error_code(__func__, ret);
 }
 
@@ -539,10 +569,10 @@ int sound_manager_get_focus_state(sound_stream_info_h stream_info, sound_stream_
        LOGI(">> enter");
 
        SM_INSTANCE_CHECK(stream_h);
-       if (!state_for_playback && !state_for_recording) {
-               ret = MM_ERROR_INVALID_ARGUMENT;
-               goto LEAVE;
-       }
+       if (!state_for_playback && !state_for_recording)
+               return SOUND_MANAGER_ERROR_INVALID_PARAMETER;
+
+       SM_ENTER_CRITICAL_SECTION_WITH_RETURN(&stream_h->focus_state_mutex, MM_ERROR_SOUND_INTERNAL);
 
        if (state_for_playback)
                *state_for_playback = ((stream_h->acquired_focus & SOUND_STREAM_FOCUS_FOR_PLAYBACK) ? (SOUND_STREAM_FOCUS_STATE_ACQUIRED) : (SOUND_STREAM_FOCUS_STATE_RELEASED));
@@ -551,7 +581,8 @@ int sound_manager_get_focus_state(sound_stream_info_h stream_info, sound_stream_
 
        LOGI("acquired_focus(0x%x)", stream_h->acquired_focus);
 
-LEAVE:
+       SM_LEAVE_CRITICAL_SECTION(&stream_h->focus_state_mutex);
+
        return _convert_sound_manager_error_code(__func__, ret);
 }