Add a mutex lock for focus callback 51/143651/1
authorSangchul Lee <sc11.lee@samsung.com>
Fri, 11 Aug 2017 00:55:08 +0000 (09:55 +0900)
committerSangchul Lee <sc11.lee@samsung.com>
Fri, 11 Aug 2017 00:56:47 +0000 (09:56 +0900)
[Version] 0.3.114
[Profile] Common
[Issue Type] Enhancement

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

index dbe5cd9bea02eaac7b2e9857442335e98179dd06..85f377f8569e6e6ddafa60dc1c878444d147818f 100644 (file)
@@ -75,10 +75,14 @@ _CHECK_CONDITION(handle->state == expected_state, MM_ERROR_SOUND_INVALID_STATE,
 #define SM_ENTER_CRITICAL_SECTION_WITH_RETURN(x_mutex, x_return) \
 switch (pthread_mutex_lock(x_mutex)) { \
 case EINVAL: \
-       LOGW("try mutex init..\n"); \
-       if (0 > pthread_mutex_init(x_mutex, NULL)) { \
+       LOGW("try mutex init.."); \
+       if (pthread_mutex_init(x_mutex, NULL)) { \
                return x_return; \
        } else { \
+               if (pthread_mutex_lock(x_mutex)) { \
+                       LOGE("mutex lock failed"); \
+                       return x_return; \
+               } \
                break; \
        } \
        return x_return; \
@@ -89,9 +93,30 @@ default: \
        return x_return; \
 }
 
+#define SM_ENTER_CRITICAL_SECTION(x_mutex) \
+switch (pthread_mutex_lock(x_mutex)) { \
+case EINVAL: \
+       LOGW("try mutex init.."); \
+       if (pthread_mutex_init(x_mutex, NULL)) { \
+               return; \
+       } else { \
+               if (pthread_mutex_lock(x_mutex)) { \
+                       LOGE("mutex lock failed"); \
+                       return; \
+               } \
+               break; \
+       } \
+       return; \
+case 0: \
+       break; \
+default: \
+       LOGE("mutex lock failed"); \
+       return; \
+}
+
 #define SM_LEAVE_CRITICAL_SECTION(x_mutex) \
 if (pthread_mutex_unlock(x_mutex)) { \
-       LOGE("mutex unlock failed\n"); \
+       LOGE("mutex unlock failed"); \
 }
 
 #define SM_REF_FOR_STREAM_INFO(x_count, x_return) \
@@ -172,6 +197,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_cb_mutex;
 } sound_stream_info_s;
 
 typedef enum {
index 75b3f66b6b8c6e7d164948f888a59df55de39ece..2e7a9dd2dfbc854e872d38a02c01d107ae16b60d 100755 (executable)
@@ -1,6 +1,6 @@
 Name:       capi-media-sound-manager
 Summary:    Sound Manager library
-Version:    0.3.113
+Version:    0.3.114
 Release:    0
 Group:      Multimedia/API
 License:    Apache-2.0
index 3608c3b0f881cff910ac6680b7f186bf2e957fb0..16ffecb7026211826aefb33c6919aeedd78bce85 100644 (file)
@@ -369,12 +369,16 @@ int sound_manager_get_focus_reacquisition(sound_stream_info_h stream_info, bool
 int sound_manager_acquire_focus(sound_stream_info_h stream_info, sound_stream_focus_mask_e focus_mask, int sound_behavior, const char *extra_info)
 {
        int ret = MM_ERROR_NONE;
+       bool is_focus_cb_thread = false;
        sound_stream_info_s *stream_h = (sound_stream_info_s*)stream_info;
 
        LOGI(">> enter");
 
        SM_INSTANCE_CHECK(stream_h);
 
+       if ((ret = mm_sound_focus_is_cb_thread(&is_focus_cb_thread)))
+               return _convert_sound_manager_error_code(__func__, ret);
+
        if (stream_h->is_focus_unavailable) {
                LOGE("acquiring focus is not allowed for this strema type(%s)", stream_h->stream_type);
                return _convert_sound_manager_error_code(__func__, MM_ERROR_POLICY_INTERNAL);
@@ -385,30 +389,46 @@ 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);
        }
 
+       if (!is_focus_cb_thread)
+               SM_ENTER_CRITICAL_SECTION_WITH_RETURN(&stream_h->focus_cb_mutex, MM_ERROR_SOUND_INTERNAL);
+
        ret = mm_sound_acquire_focus_with_option(stream_h->index, (mm_sound_focus_type_e)focus_mask, sound_behavior, extra_info);
        if (ret == MM_ERROR_NONE) {
                stream_h->acquired_focus |= focus_mask;
                _update_focus_status(stream_h->index, (unsigned int)stream_h->acquired_focus);
        }
 
+       if (!is_focus_cb_thread)
+               SM_LEAVE_CRITICAL_SECTION(&stream_h->focus_cb_mutex);
+
        return _convert_sound_manager_error_code(__func__, ret);
 }
 
 int sound_manager_release_focus(sound_stream_info_h stream_info, sound_stream_focus_mask_e focus_mask, int sound_behavior, const char *extra_info)
 {
        int ret = MM_ERROR_NONE;
+       bool is_focus_cb_thread = false;
        sound_stream_info_s *stream_h = (sound_stream_info_s*)stream_info;
 
        LOGI(">> enter");
 
        SM_INSTANCE_CHECK(stream_h);
 
+       if ((ret = mm_sound_focus_is_cb_thread(&is_focus_cb_thread)))
+               return _convert_sound_manager_error_code(__func__, ret);
+
+       if (!is_focus_cb_thread)
+               SM_ENTER_CRITICAL_SECTION_WITH_RETURN(&stream_h->focus_cb_mutex, MM_ERROR_SOUND_INTERNAL);
+
        ret = mm_sound_release_focus_with_option(stream_h->index, (mm_sound_focus_type_e)focus_mask, sound_behavior, extra_info);
        if (ret == MM_ERROR_NONE) {
                stream_h->acquired_focus &= ~focus_mask;
                _update_focus_status(stream_h->index, (unsigned int)stream_h->acquired_focus);
        }
 
+       if (!is_focus_cb_thread)
+               SM_LEAVE_CRITICAL_SECTION(&stream_h->focus_cb_mutex);
+
        return _convert_sound_manager_error_code(__func__, ret);
 }
 
index c7c9b9f17d70c8ca9115e89d07da8f3ade4de1e6..a53bc4c999d7bd808b2ac5f4d7228195639d7315 100644 (file)
@@ -572,6 +572,8 @@ void _focus_state_change_callback(int index, mm_sound_focus_type_e focus_type, m
                goto LEAVE;
        }
 
+       SM_ENTER_CRITICAL_SECTION(&stream_info->focus_cb_mutex);
+
        if (state == FOCUS_IS_RELEASED)
                stream_info->acquired_focus &= ~focus_type;
        else if (state == FOCUS_IS_ACQUIRED)
@@ -588,6 +590,8 @@ void _focus_state_change_callback(int index, mm_sound_focus_type_e focus_type, m
        if (state == FOCUS_IS_RELEASED)
                _update_focus_status(stream_info->index, (unsigned int)stream_info->acquired_focus);
 
+       SM_LEAVE_CRITICAL_SECTION(&stream_info->focus_cb_mutex);
+
 LEAVE:
        LOGI("<< leave");