Add protection code for focus-callback creation
[platform/core/multimedia/libmm-sound.git] / mm_sound_client.c
index 16b8e2d..87a6bf8 100644 (file)
@@ -598,7 +598,7 @@ static bool device_is_match_direction(int direction, int mask)
                return true;
        if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_OUT_FLAG) && (direction & MM_SOUND_DEVICE_IO_DIRECTION_OUT))
                return true;
-       if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_BOTH_FLAG) && (direction & MM_SOUND_DEVICE_IO_DIRECTION_BOTH))
+       if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_BOTH_FLAG) && (direction == MM_SOUND_DEVICE_IO_DIRECTION_BOTH))
                return true;
 
        return false;
@@ -1043,9 +1043,12 @@ int mm_sound_client_unset_session_interrupt_callback(void)
 
 static gpointer _focus_thread_func(gpointer data)
 {
-       debug_log(">>> thread func..ID of this thread(%u)\n", (unsigned int)pthread_self());
-       g_main_loop_run(g_focus_loop);
-       debug_log("<<< quit thread func..\n");
+       unsigned int thread_id = (unsigned int)pthread_self();
+       debug_warning(">>> thread func..ID of this thread(%u), mainloop(%p)", thread_id, g_focus_loop);
+       if (g_focus_loop)
+               g_main_loop_run(g_focus_loop);
+
+       debug_warning("<<< quit thread func..(%u), mainloop(%p)", thread_id, g_focus_loop);
        return NULL;
 }
 
@@ -1603,24 +1606,36 @@ int mm_sound_client_register_focus(int id, int pid, const char *stream_type, mm_
                        GMainContext* focus_context = g_main_context_new ();
                        g_focus_loop = g_main_loop_new (focus_context, FALSE);
                        g_main_context_unref(focus_context);
-                       g_focus_thread = g_thread_new("focus-callback-thread", _focus_thread_func, NULL);
+                       if (g_focus_loop == NULL) {
+                               debug_error("could not create mainloop..");
+                               ret = MM_ERROR_SOUND_INTERNAL;
+                               goto cleanup;
+                       }
+
+                       g_focus_thread = g_thread_new("focus-cb-thread", _focus_thread_func, NULL);
                        if (g_focus_thread == NULL) {
-                               debug_error ("could not create thread..");
+                               debug_error("could not create thread..");
                                g_main_loop_unref(g_focus_loop);
-                               g_focus_sound_handle[index].is_used = false;
                                ret = MM_ERROR_SOUND_INTERNAL;
                                goto cleanup;
                        }
+               } else {
+                       debug_warning("focus thread(%p) with mainloop(%p) exists, skip thread creation",
+                                               g_focus_thread, g_focus_loop);
                }
        } else {
                debug_error("[Client] Error occurred : 0x%x \n",ret);
-               g_focus_sound_handle[index].is_used = false;
                goto cleanup;
        }
 
        _focus_init_callback(index, false);
 
 cleanup:
+
+       if (ret) {
+               g_focus_sound_handle[index].is_used = false;
+       }
+
        MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
        debug_fleave();
 
@@ -1697,13 +1712,13 @@ int mm_sound_client_set_focus_reacquisition(int id, bool reacquisition)
        } else if (!result) {
                ret = mm_sound_proxy_set_foucs_reacquisition(instance, id, reacquisition);
                if (ret == MM_ERROR_NONE) {
-                       debug_msg("[Client] Success to set focus reacquisition\n");
+                       debug_msg("[Client] Success to set focus reacquisition to [%d]\n", reacquisition);
                } else {
                        debug_error("[Client] Error occurred : 0x%x \n",ret);
                        goto cleanup;
                }
        } else {
-               debug_warning("[Client] Inside the focus cb thread, bypassing dbus method call");
+               debug_warning("[Client] Inside the focus cb thread, set focus reacquisition to [%d]\n", reacquisition);
        }
 
        g_focus_sound_handle[index].auto_reacquire = reacquisition;
@@ -1864,13 +1879,22 @@ int mm_sound_client_set_focus_watch_callback(int pid, mm_sound_focus_type_e focu
                        GMainContext* focus_context = g_main_context_new ();
                        g_focus_loop = g_main_loop_new (focus_context, FALSE);
                        g_main_context_unref(focus_context);
-                       g_focus_thread = g_thread_new("focus-callback-thread", _focus_thread_func, NULL);
+                       if (g_focus_loop == NULL) {
+                               debug_error("could not create mainloop..");
+                               ret = MM_ERROR_SOUND_INTERNAL;
+                               goto cleanup;
+                       }
+
+                       g_focus_thread = g_thread_new("focus-cb-thread", _focus_thread_func, NULL);
                        if (g_focus_thread == NULL) {
                                debug_error ("could not create thread..");
                                g_main_loop_unref(g_focus_loop);
                                ret = MM_ERROR_SOUND_INTERNAL;
                                goto cleanup;
                        }
+               } else {
+                       debug_warning("focus thread(%p) with mainloop(%p) exists, skip thread creation",
+                                               g_focus_thread, g_focus_loop);
                }
        } else {
                debug_error("[Client] Error occurred : 0x%x",ret);