Manage focus cb thread and focus watch cb thread separately 18/202018/1 accepted/tizen/unified/20190325.070904 submit/tizen/20190322.072428
authorSangchul Lee <sc11.lee@samsung.com>
Fri, 22 Mar 2019 01:25:30 +0000 (10:25 +0900)
committerSangchul Lee <sc11.lee@samsung.com>
Fri, 22 Mar 2019 01:33:12 +0000 (10:33 +0900)
API is revised as below.
 - int mm_sound_focus_is_cb_thread(bool *result)
 + int mm_sound_focus_is_cb_thread(bool *result, bool *is_for_watching)

[Version] 0.12.41
[Issue Type] Enhancement

Change-Id: Ib2cfa6b7b2425d5a37db224bb242c7c2a1651d14
Signed-off-by: Sangchul Lee <sc11.lee@samsung.com>
include/mm_sound_client.h
include/mm_sound_focus.h
include/mm_sound_focus_private.h
mm_sound_client.c
mm_sound_focus.c
mm_sound_focus_private.c
mm_sound_proxy.c
packaging/libmm-sound.spec

index ce62bb9..d939e0b 100644 (file)
@@ -59,7 +59,7 @@ int mm_sound_client_remove_device_state_changed_callback(unsigned int id);
 int mm_sound_client_add_device_running_changed_callback(int device_flags, mm_sound_device_running_changed_cb func, void *userdata, unsigned int *id);
 int mm_sound_client_remove_device_running_changed_callback(unsigned int id);
 int mm_sound_client_is_stream_on_device(int stream_id, int device_id, bool *is_on);
-int mm_sound_client_is_focus_cb_thread(GThread *mine, bool *result);
+int mm_sound_client_is_focus_cb_thread(GThread *mine, bool *result, bool *is_for_watching);
 int mm_sound_client_register_focus(int pid, const char *stream_type, mm_sound_focus_changed_cb callback, void* user_data, int *id);
 int mm_sound_client_unregister_focus(int id);
 int mm_sound_client_set_focus_reacquisition(int id, bool reacquisition);
index d725233..9ec7f0c 100644 (file)
@@ -48,7 +48,7 @@ typedef enum {
        FOCUS_FOR_BOTH,
 } mm_sound_focus_type_e;
 
-int mm_sound_focus_is_cb_thread(bool *result);
+int mm_sound_focus_is_cb_thread(bool *result, bool *is_for_watching);
 
 typedef void (*mm_sound_focus_changed_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 *ext_info, void *user_data);
 int mm_sound_register_focus(const char *stream_type, mm_sound_focus_changed_cb callback, void *user_data, int *id);
index b4e1893..834ec3a 100644 (file)
@@ -44,6 +44,7 @@ typedef struct {
        bool auto_reacquire;
        GMutex focus_lock;
        GThread *focus_cb_thread;
+       GThread *focus_watch_cb_thread;
        GMainLoop *focus_loop;
        mm_sound_focus_changed_cb focus_callback;
        mm_sound_focus_changed_watch_cb watch_callback;
@@ -68,8 +69,8 @@ int focus_find_empty_index(int *handle);
 int focus_find_index_by_handle(int handle);
 int focus_watch_find_index_by_handle(int handle);
 
-int focus_init_context(int index);
-void focus_deinit_context(int index);
+int focus_init_context(int index, bool is_for_watching);
+void focus_deinit_context(int index, bool is_for_watching);
 void focus_init_callback(int index, bool is_for_watching);
 void focus_deinit_callback(int index, bool is_for_watching);
 
index 95e5a25..e66193a 100644 (file)
@@ -998,7 +998,7 @@ int mm_sound_client_control_filter_by_type(const char *stream_type, const char *
        return ret;
 }
 
-int mm_sound_client_is_focus_cb_thread(GThread *mine, bool *result)
+int mm_sound_client_is_focus_cb_thread(GThread *mine, bool *result, bool *is_for_watching)
 {
        int ret = MM_ERROR_NONE;
        int i = 0;
@@ -1012,6 +1012,14 @@ int mm_sound_client_is_focus_cb_thread(GThread *mine, bool *result)
                                continue;
                        if (g_focus_sound_handle[i].focus_cb_thread == mine) {
                                *result = true;
+                               if (is_for_watching)
+                                       *is_for_watching = false;
+                               break;
+                       }
+                       if (g_focus_sound_handle[i].focus_watch_cb_thread == mine) {
+                               *result = true;
+                               if (is_for_watching)
+                                       *is_for_watching = true;
                                break;
                        }
                }
@@ -1042,7 +1050,7 @@ int mm_sound_client_register_focus(int pid, const char *stream_type,
        ret = mm_sound_proxy_register_focus(index, stream_type, id);
        if (ret == MM_ERROR_NONE) {
                debug_msg("Success to register focus, client_fd[%d], id[%d]", g_focus_sound_handle[index].client_fd, *id);
-               if (focus_init_context(index)) {
+               if (focus_init_context(index, false)) {
                        ret = MM_ERROR_SOUND_INTERNAL;
                        goto cleanup;
                }
@@ -1099,7 +1107,7 @@ int mm_sound_client_unregister_focus(int id)
        g_focus_sound_handle[index].client_fd = 0;
        g_focus_sound_handle[index].handle = 0;
        g_focus_sound_handle[index].is_used = false;
-       focus_deinit_context(index);
+       focus_deinit_context(index, false);
 
 cleanup:
        MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
@@ -1124,7 +1132,7 @@ int mm_sound_client_set_focus_reacquisition(int id, bool reacquisition)
                goto cleanup;
        }
 
-       ret = mm_sound_client_is_focus_cb_thread(g_thread_self(), &result);
+       ret = mm_sound_client_is_focus_cb_thread(g_thread_self(), &result, NULL);
        if (ret) {
                debug_error("mm_sound_client_is_focus_cb_thread failed");
                goto cleanup;
@@ -1329,7 +1337,7 @@ int mm_sound_client_set_focus_watch_callback(int pid, mm_sound_focus_type_e focu
        if (ret == MM_ERROR_NONE) {
                *id = g_focus_sound_handle[index].handle;
                debug_msg("Success to add watch focus cb, id(%d)", *id);
-               if (focus_init_context(index)) {
+               if (focus_init_context(index, true)) {
                        ret = MM_ERROR_SOUND_INTERNAL;
                        goto cleanup;
                }
@@ -1404,7 +1412,7 @@ int mm_sound_client_unset_focus_watch_callback(int id)
        g_focus_sound_handle[index].focus_pid = 0;
        g_focus_sound_handle[index].client_fd = 0;
        g_focus_sound_handle[index].handle = 0;
-       focus_deinit_context(index);
+       focus_deinit_context(index, true);
 
 cleanup:
        MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
index 21e8a82..1b0fd5f 100644 (file)
@@ -30,7 +30,7 @@
 { \
        int ret = MM_ERROR_NONE; \
        bool result = false; \
-       ret = mm_sound_client_is_focus_cb_thread(x_thread, &result); \
+       ret = mm_sound_client_is_focus_cb_thread(x_thread, &result, NULL); \
        if (ret) \
                return ret; \
        if (result) { \
 } \
 
 EXPORT_API
-int mm_sound_focus_is_cb_thread(bool *result)
+int mm_sound_focus_is_cb_thread(bool *result, bool *is_for_watching)
 {
        int ret = MM_ERROR_NONE;
 
        debug_fenter();
 
-       ret = mm_sound_client_is_focus_cb_thread(g_thread_self(), result);
+       if (result == NULL) {
+               debug_error("argument is not valid");
+               return MM_ERROR_INVALID_ARGUMENT;
+       }
+
+       ret = mm_sound_client_is_focus_cb_thread(g_thread_self(), result, is_for_watching);
        if (!ret) {
                if (*result)
                        debug_msg("it might be called in the thread of focus callback");
+               if (is_for_watching && *is_for_watching)
+                       debug_msg("it's in the focus watch callback");
        }
 
        debug_fleave();
@@ -93,7 +100,7 @@ int mm_sound_unregister_focus(int id)
                return MM_ERROR_INVALID_ARGUMENT;
        }
 
-       mm_sound_client_is_focus_cb_thread(g_thread_self(), &result);
+       mm_sound_client_is_focus_cb_thread(g_thread_self(), &result, NULL);
        if (!result) {
                if ((ret = mm_sound_client_unregister_focus(id)))
                        debug_error("Could not unregister focus, ret = %x", ret);
@@ -347,7 +354,7 @@ int mm_sound_unset_focus_watch_callback(int id)
                return ret;
        }
 
-       mm_sound_client_is_focus_cb_thread(g_thread_self(), &result);
+       mm_sound_client_is_focus_cb_thread(g_thread_self(), &result, NULL);
        if (!result) {
                if ((ret = mm_sound_client_unset_focus_watch_callback(id)))
                        debug_error("Could not unset focus watch callback, id(%d), ret[0x%x]", id, ret);
index da33303..933b025 100644 (file)
@@ -571,10 +571,12 @@ int focus_watch_find_index_by_handle(int handle)
 }
 
 #define LOOP_RUNNING_WAIT_TIME_MS 2500
-int focus_init_context(int index)
+int focus_init_context(int index, bool is_for_watching)
 {
        int ret = MM_ERROR_NONE;
        GMainContext *focus_context;
+       GThread **focus_cb_thread = NULL;
+       char *focus_cb_thread_name;
 
        debug_fenter();
 
@@ -591,16 +593,24 @@ int focus_init_context(int index)
                goto ERROR;
        }
 
-       g_focus_sound_handle[index].focus_cb_thread = g_thread_new("focus-cb-thread",
+       if (is_for_watching) {
+               focus_cb_thread = &g_focus_sound_handle[index].focus_watch_cb_thread;
+               focus_cb_thread_name = "focus-watch-cb-thread";
+       } else {
+               focus_cb_thread = &g_focus_sound_handle[index].focus_cb_thread;
+               focus_cb_thread_name = "focus-cb-thread";
+       }
+
+       *focus_cb_thread = g_thread_new(focus_cb_thread_name,
                                                                        _focus_thread_func,
                                                                        g_focus_sound_handle[index].focus_loop);
-       if (g_focus_sound_handle[index].focus_cb_thread == NULL) {
+       if (*focus_cb_thread == NULL) {
                debug_error("could not create thread..");
                goto ERROR;
        }
 
-       debug_warning("focus cb thread[%p] with mainloop[%p] is created for index(%d)",
-                               g_focus_sound_handle[index].focus_cb_thread, g_focus_sound_handle[index].focus_loop, index);
+       debug_warning("focus cb thread[%p] with mainloop[%p] is created for index(%d), is_for_watching(%d)",
+                       *focus_cb_thread, g_focus_sound_handle[index].focus_loop, index, is_for_watching);
 
        if ((ret = _focus_loop_is_running_timed_wait(g_focus_sound_handle[index].focus_loop, LOOP_RUNNING_WAIT_TIME_MS))) {
                debug_error("failed to _focus_loop_is_running_timed_wait(), ret[0x%x]", ret);
@@ -613,12 +623,12 @@ int focus_init_context(int index)
 
 ERROR:
        if (g_focus_sound_handle[index].focus_loop) {
-               if (g_focus_sound_handle[index].focus_cb_thread) {
+               if (*focus_cb_thread) {
                        g_main_loop_quit(g_focus_sound_handle[index].focus_loop);
-                       g_thread_join(g_focus_sound_handle[index].focus_cb_thread);
-                       debug_warning("after thread join, thread[%p], mainloop[%p] for index(%d)",
-                                       g_focus_sound_handle[index].focus_cb_thread, g_focus_sound_handle[index].focus_loop, index);
-                       g_focus_sound_handle[index].focus_cb_thread = NULL;
+                       g_thread_join(*focus_cb_thread);
+                       debug_warning("after thread join, thread[%p], mainloop[%p] for index(%d), is_for_watching(%d)",
+                                       *focus_cb_thread, g_focus_sound_handle[index].focus_loop, index, is_for_watching);
+                       *focus_cb_thread = NULL;
                }
                g_main_loop_unref(g_focus_sound_handle[index].focus_loop);
                g_focus_sound_handle[index].focus_loop = NULL;
@@ -626,8 +636,10 @@ ERROR:
        return MM_ERROR_SOUND_INTERNAL;
 }
 
-void focus_deinit_context(int index)
+void focus_deinit_context(int index, bool is_for_watching)
 {
+       GThread **focus_cb_thread = NULL;
+
        debug_fenter();
 
        if (index < 0 || index >= FOCUS_HANDLE_MAX) {
@@ -635,19 +647,28 @@ void focus_deinit_context(int index)
                return;
        }
 
-       if (!g_focus_sound_handle[index].focus_loop || !g_focus_sound_handle[index].focus_cb_thread) {
-               debug_error("focus_loop[%p] or focus_cb_thread[%p] is null",
-                               g_focus_sound_handle[index].focus_loop, g_focus_sound_handle[index].focus_cb_thread);
+       if (!g_focus_sound_handle[index].focus_loop ||
+           (!is_for_watching && !g_focus_sound_handle[index].focus_cb_thread) ||
+               (is_for_watching && !g_focus_sound_handle[index].focus_watch_cb_thread)) {
+               debug_error("focus_loop[%p] or focus_cb_thread[%p] or focus_watch_cb_thread[%p] is null",
+                               g_focus_sound_handle[index].focus_loop, g_focus_sound_handle[index].focus_cb_thread,
+                               g_focus_sound_handle[index].focus_watch_cb_thread);
                return;
        }
 
        g_main_loop_quit(g_focus_sound_handle[index].focus_loop);
-       g_thread_join(g_focus_sound_handle[index].focus_cb_thread);
-       debug_warning("after thread join, thread[%p], mainloop[%p] for index(%d)",
-                       g_focus_sound_handle[index].focus_cb_thread, g_focus_sound_handle[index].focus_loop, index);
+
+       if (is_for_watching)
+               focus_cb_thread = &g_focus_sound_handle[index].focus_watch_cb_thread;
+       else
+               focus_cb_thread = &g_focus_sound_handle[index].focus_cb_thread;
+       g_thread_join(*focus_cb_thread);
+
+       debug_warning("after thread join, thread[%p], mainloop[%p] for index(%d), is_for_watching(%d)",
+                       *focus_cb_thread, g_focus_sound_handle[index].focus_loop, index, is_for_watching);
        g_main_loop_unref(g_focus_sound_handle[index].focus_loop);
        g_focus_sound_handle[index].focus_loop = NULL;
-       g_focus_sound_handle[index].focus_cb_thread = NULL;
+       *focus_cb_thread = NULL;
 
        debug_fleave();
 }
index 31cfafe..04f493f 100644 (file)
@@ -1051,7 +1051,7 @@ int mm_sound_proxy_acquire_focus(int index, mm_sound_focus_type_e type, int opti
 
        debug_fenter();
 
-       mm_sound_client_is_focus_cb_thread(g_thread_self(), &is_in_focus_cb_thread);
+       mm_sound_client_is_focus_cb_thread(g_thread_self(), &is_in_focus_cb_thread, NULL);
        if (!is_in_focus_cb_thread) {
                if ((ret = mm_sound_focus_socket_acquire(pid, client_fd, id,
                                                                                                type, option, ext_info ? ext_info : "", true)))
@@ -1085,7 +1085,7 @@ int mm_sound_proxy_release_focus(int index, mm_sound_focus_type_e type, int opti
 
        debug_fenter();
 
-       mm_sound_client_is_focus_cb_thread(g_thread_self(), &is_in_focus_cb_thread);
+       mm_sound_client_is_focus_cb_thread(g_thread_self(), &is_in_focus_cb_thread, NULL);
        if (!is_in_focus_cb_thread) {
                if ((ret = mm_sound_focus_socket_release(pid, client_fd, id, type, option, ext_info ? ext_info : "", true)))
                        debug_error("failed to mm_sound_focus_socket_release(), ret[0x%x]", ret);
index 2c50937..0c20ed3 100644 (file)
@@ -1,6 +1,6 @@
 Name:       libmm-sound
 Summary:    MMSound Package contains client lib and sound_server binary
-Version:    0.12.40
+Version:    0.12.41
 Release:    0
 Group:      System/Libraries
 License:    Apache-2.0