Add protection code for focus-callback creation
[platform/core/multimedia/libmm-sound.git] / mm_sound_client.c
index 48da082..87a6bf8 100644 (file)
@@ -137,7 +137,7 @@ void _system_signal_handler(int signo)
        int ret = MM_ERROR_NONE;
        sigset_t old_mask, all_mask;
 
-       debug_error("Got signal : signo(%d)", signo);
+       debug_warning("Got signal : signo(%d)", signo);
 
        /* signal block */
 
@@ -184,7 +184,7 @@ void _system_signal_handler(int signo)
                break;
        }
 
-       debug_error("signal handling end");
+       debug_warning("signal handling end");
 }
 
 int mm_sound_client_initialize(void)
@@ -284,7 +284,7 @@ void mm_sound_convert_volume_type_to_stream_type(int volume_type, char *stream_t
                break;
        }
 
-       debug_error("volume type (%d) converted to stream type (%s)", volume_type, stream_type);
+       debug_msg("volume type (%d) converted to stream type (%s)", volume_type, stream_type);
 
 }
 
@@ -297,7 +297,7 @@ void _mm_sound_client_focus_signal_callback(mm_sound_signal_name_t signal, int v
        int ret = MM_ERROR_NONE;
 
        debug_fenter();
-       debug_error("focus signal received, value = %d", value);
+       debug_msg("focus signal received, value = %d", value);
 
        if (value == 1) {
                ret = mm_sound_proxy_clear_focus(getpid());
@@ -589,70 +589,108 @@ failed:
        return ret;
 }
 
-static bool is_device_match_flags(const char *device_type, int io_direction, int state, int device_flags)
+static bool device_is_match_direction(int direction, int mask)
 {
-       int io_direction_flag = device_flags & DEVICE_IO_DIRECTION_FLAGS;
-       int state_flag = device_flags & DEVICE_STATE_FLAGS;
-       int type_flag = device_flags & DEVICE_TYPE_FLAGS;
-       char builtin_prefix[] = "builtin";
+       if (mask == DEVICE_IO_DIRECTION_FLAGS || mask == 0)
+               return true;
 
-       if (device_flags == DEVICE_ALL_FLAG)
-               return TRUE;
+       if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_IN_FLAG) && (direction & MM_SOUND_DEVICE_IO_DIRECTION_IN))
+               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))
+               return true;
 
-       switch (io_direction_flag) {
-       case DEVICE_IO_DIRECTION_IN_FLAG:
-               if (io_direction != DEVICE_IO_DIRECTION_IN)
-                       return FALSE;
-               break;
-       case DEVICE_IO_DIRECTION_OUT_FLAG:
-               if (io_direction != DEVICE_IO_DIRECTION_OUT)
-                       return FALSE;
-               break;
-       case DEVICE_IO_DIRECTION_BOTH_FLAG:
-               if (io_direction != DEVICE_IO_DIRECTION_BOTH)
-                       return FALSE;
-               break;
-       default:
-               break;
+       return false;
+}
+
+static bool device_is_match_state(int state, int mask)
+{
+       if (mask == DEVICE_STATE_FLAGS || mask == 0)
+               return true;
+
+       if ((mask & MM_SOUND_DEVICE_STATE_DEACTIVATED_FLAG) && (state == MM_SOUND_DEVICE_STATE_DEACTIVATED))
+               return true;
+       if ((mask & MM_SOUND_DEVICE_STATE_ACTIVATED_FLAG) && (state == MM_SOUND_DEVICE_STATE_ACTIVATED))
+               return true;
+
+       return false;
+}
+
+static bool device_is_match_type(const char *type, int mask)
+{
+       bool is_builtin;
+       const char *builtin_prefix = "builtin";
+
+       if (mask == DEVICE_TYPE_FLAGS || mask == 0)
+               return true;
+
+       is_builtin = !strncmp(type, builtin_prefix, strlen(builtin_prefix));
+
+       if ((mask & MM_SOUND_DEVICE_TYPE_INTERNAL_FLAG) && (is_builtin))
+               return true;
+       if ((mask & MM_SOUND_DEVICE_TYPE_EXTERNAL_FLAG) && (!is_builtin))
+               return true;
+
+       return false;
+}
+
+static bool device_is_match_with_mask(const char *type, int direction, int state, int mask)
+{
+       if (mask == DEVICE_ALL_FLAG)
+               return true;
+
+       return (device_is_match_direction(direction, mask & DEVICE_IO_DIRECTION_FLAGS) &&
+                       device_is_match_state(state, mask & DEVICE_STATE_FLAGS) &&
+                       device_is_match_type(type, mask & DEVICE_TYPE_FLAGS));
+}
+
+static int _fill_sound_device(mm_sound_device_t *device_h, int device_id, const char *device_type,
+               int direction, int state, const char *name, int *stream_id, int stream_num)
+{
+       int i;
+
+       if (stream_num > 0 && stream_id == NULL) {
+               debug_error("stream_num is %d, but stream_id is NULL", stream_num);
+               return -1;
        }
 
-       switch (state_flag) {
-       case DEVICE_STATE_DEACTIVATED_FLAG:
-               if (state != DEVICE_STATE_DEACTIVATED)
-                       return FALSE;
-               break;
-       case DEVICE_STATE_ACTIVATED_FLAG:
-               if (state != DEVICE_STATE_ACTIVATED)
-                       return FALSE;
-               break;
-       default:
-               break;
+       if (stream_num > MAX_STREAM_ON_DEVICE) {
+               debug_error("too many streams on this device");
+               return -1;
        }
 
-       switch (type_flag) {
-       case DEVICE_TYPE_INTERNAL_FLAG:
-               if (strncmp(device_type, builtin_prefix, strlen(builtin_prefix)) != 0)
-                       return FALSE;
-               break;
-       case DEVICE_TYPE_EXTERNAL_FLAG:
-               if (strncmp(device_type, builtin_prefix, strlen(builtin_prefix)) == 0)
-                       return FALSE;
-               break;
-       default:
-               break;
+       device_h->id = device_id;
+       device_h->io_direction = direction;
+       device_h->state = state;
+       MMSOUND_STRNCPY(device_h->name, name, MAX_DEVICE_NAME_NUM);
+       MMSOUND_STRNCPY(device_h->type, device_type, MAX_DEVICE_TYPE_STR_LEN);
+
+       if (stream_num > 0) {
+               device_h->stream_num = stream_num;
+               debug_log("%d streams on this device", stream_num);
+               for (i = 0; i < stream_num; i++) {
+                       debug_log("  stream_id : %d", stream_id[i]);
+                       device_h->stream_id[i] = stream_id[i];
+               }
+       } else {
+               device_h->stream_num = 0;
+               debug_log("There is no stream on this device");
        }
 
-       return TRUE;
+       return 0;
 }
 
-static void _mm_sound_device_connected_callback_wrapper_func(int device_id, const char *device_type, int io_direction, int state, const char *name, gboolean is_connected, void *userdata)
+static void _mm_sound_device_connected_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
+               int state, const char *name, int *stream_id, int stream_num, gboolean is_connected, void *userdata)
 {
        mm_sound_device_t device_h;
        struct callback_data *cb_data = (struct callback_data*) userdata;
        int device_flags;
 
-       debug_log("[Wrapper CB][Device Connnected] device_id : %d, device_type : %s, direction : %d, state : %d, name : %s, is_connected : %d",
-                         device_id, device_type, io_direction, state, name, is_connected);
+       debug_log("[Device %s] id(%d) type(%s) direction(%d) state(%d) name(%s)",
+                         is_connected ? "Connected" : "Disconnected", device_id, device_type, io_direction,
+                         state, name, is_connected);
 
        if (cb_data == NULL) {
                debug_warning("device connected changed callback data null");
@@ -660,15 +698,13 @@ static void _mm_sound_device_connected_callback_wrapper_func(int device_id, cons
        }
 
        device_flags = (int) cb_data->extra_data;
-
-       if (!is_device_match_flags(device_type, io_direction, state, device_flags))
+       if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
                return;
 
-       device_h.id = device_id;
-       device_h.io_direction = io_direction;
-       device_h.state = state;
-       MMSOUND_STRNCPY(device_h.name, name, MAX_DEVICE_NAME_NUM);
-       MMSOUND_STRNCPY(device_h.type, device_type, MAX_DEVICE_TYPE_STR_LEN);
+       if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name, stream_id, stream_num) < 0) {
+               debug_error("Failed to fill sound device");
+               return;
+       }
 
        ((mm_sound_device_connected_cb)(cb_data->user_cb))(&device_h, is_connected, cb_data->user_data);
 }
@@ -701,13 +737,14 @@ int mm_sound_client_remove_device_connected_callback(unsigned int subs_id)
        return ret;
 }
 
-static void _mm_sound_device_info_changed_callback_wrapper_func(int device_id, const char *device_type, int io_direction, int state, const char *name, int changed_device_info_type, void *userdata)
+static void _mm_sound_device_info_changed_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
+               int state, const char *name, int *stream_id, int stream_num, int changed_device_info_type, void *userdata)
 {
        mm_sound_device_t device_h;
        struct callback_data *cb_data = (struct callback_data*) userdata;
        int device_flags;
 
-       debug_log("[Wrapper CB][Device Info Changed] device_id : %d, device_type : %s, direction : %d, state : %d, name : %s, changed_info_type : %d",
+       debug_log("[Device Info Changed] id(%d) type(%s) direction(%d) state(%d) name(%s) changed_info_type(%d)",
                          device_id, device_type, io_direction, state, name, changed_device_info_type);
 
        if (cb_data == NULL) {
@@ -716,15 +753,13 @@ static void _mm_sound_device_info_changed_callback_wrapper_func(int device_id, c
        }
 
        device_flags = (int) cb_data->extra_data;
-
-       if (!is_device_match_flags(device_type, io_direction, state, device_flags))
+       if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
                return;
 
-       device_h.id = device_id;
-       device_h.io_direction = io_direction;
-       device_h.state = state;
-       MMSOUND_STRNCPY(device_h.name, name, MAX_DEVICE_NAME_NUM);
-       MMSOUND_STRNCPY(device_h.type, device_type, MAX_DEVICE_TYPE_STR_LEN);
+       if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name, stream_id, stream_num) < 0) {
+               debug_error("Failed to fill sound device");
+               return;
+       }
 
        ((mm_sound_device_info_changed_cb)(cb_data->user_cb))(&device_h, changed_device_info_type, cb_data->user_data);
 }
@@ -756,6 +791,81 @@ int mm_sound_client_remove_device_info_changed_callback(unsigned int subs_id)
 
 }
 
+static void _mm_sound_device_state_changed_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
+               int state, const char *name, int *stream_id, int stream_num, void *userdata)
+{
+       mm_sound_device_t device_h;
+       struct callback_data *cb_data = (struct callback_data*) userdata;
+       int device_flags;
+
+       debug_log("[Device State Changed] id(%d) type(%s) direction(%d) state(%d) name(%s)",
+                         device_id, device_type, io_direction, state, name);
+
+       if (cb_data == NULL) {
+               debug_warning("device state changed callback data null");
+               return;
+       }
+
+       device_flags = (int) cb_data->extra_data;
+
+       if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
+               return;
+
+       if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name, stream_id, stream_num) < 0) {
+               debug_error("Failed to fill sound device");
+               return;
+       }
+
+       ((mm_sound_device_state_changed_cb)(cb_data->user_cb))(&device_h, state, cb_data->user_data);
+}
+
+int mm_sound_client_add_device_state_changed_callback(int device_flags, mm_sound_device_state_changed_cb func, void *userdata, unsigned int *id)
+{
+       int ret = MM_ERROR_NONE;
+       struct callback_data *cb_data = (struct callback_data*) userdata;
+
+       debug_fenter();
+
+       GET_CB_DATA(cb_data, func, userdata, (void *) device_flags);
+
+       ret = mm_sound_proxy_add_device_state_changed_callback(device_flags, _mm_sound_device_state_changed_callback_wrapper_func, cb_data, g_free, id);
+
+       debug_fleave();
+       return ret;
+}
+
+int mm_sound_client_remove_device_state_changed_callback(unsigned int id)
+{
+       int ret = MM_ERROR_NONE;
+       debug_fenter();
+
+       ret =  mm_sound_proxy_remove_device_state_changed_callback(id);
+
+       debug_fleave();
+       return ret;
+}
+
+int mm_sound_client_is_stream_on_device(int stream_id, int device_id, bool *is_on)
+{
+       int ret = MM_ERROR_NONE;
+       debug_fenter();
+
+       if (!is_on) {
+               debug_error("Invalid Parameter");
+               ret = MM_ERROR_COMMON_INVALID_ARGUMENT;
+               goto failed;
+       }
+
+       if ((ret = mm_sound_proxy_is_stream_on_device(stream_id, device_id, is_on)) != MM_ERROR_NONE) {
+               debug_error("[Client] failed to query is stream on device, ret[0x%x]", ret);
+               goto failed;
+       }
+
+failed:
+       debug_fleave();
+       return ret;
+}
+
 int __convert_volume_type_to_str(int volume_type, char **volume_type_str)
 {
        int ret = MM_ERROR_NONE;
@@ -933,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;
 }
 
@@ -982,13 +1095,22 @@ static gboolean _focus_fd_dispatch(GSource *source,      GSourceFunc callback, gpoint
 static int _focus_find_index_by_handle(int handle)
 {
        int i = 0;
-       for(i = 0; i< FOCUS_HANDLE_MAX; i++) {
-               if (handle == g_focus_sound_handle[i].handle) {
-                       //debug_msg("found index(%d) for handle(%d)", i, handle);
-                       if (handle == FOCUS_HANDLE_INIT_VAL) {
-                               return -1;
-                       }
-                       return i;
+       for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
+               if (g_focus_sound_handle[i].focus_callback && handle == g_focus_sound_handle[i].handle) {
+                       /* debug_msg("found index(%d) for handle(%d)", i, handle);*/
+                       return (handle == FOCUS_HANDLE_INIT_VAL)? -1 : i;
+               }
+       }
+       return -1;
+}
+
+static int _focus_watch_find_index_by_handle(int handle)
+{
+       int i = 0;
+       for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
+               if (g_focus_sound_handle[i].watch_callback && handle == g_focus_sound_handle[i].handle) {
+                       /* debug_msg("found index(%d) for watch handle(%d)", i, handle);*/
+                       return (handle == FOCUS_HANDLE_INIT_VAL)? -1 : i;
                }
        }
        return -1;
@@ -1031,17 +1153,17 @@ static gboolean _focus_callback_handler(gpointer d)
                tid = g_focus_sound_handle[focus_index].focus_tid;
 
                if (changed_state != -1) {
-                       debug_error("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)", tid, cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type);
+                       debug_msg("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)", tid, cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type);
                        if (g_focus_sound_handle[focus_index].focus_callback == NULL) {
                                        debug_error("callback is null..");
                                        g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
                                        return FALSE;
                        }
-                       debug_error("[CALLBACK(%p) START]",g_focus_sound_handle[focus_index].focus_callback);
+                       debug_msg("[CALLBACK(%p) START]",g_focus_sound_handle[focus_index].focus_callback);
                        (g_focus_sound_handle[focus_index].focus_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type, cb_data.option, cb_data.ext_info, g_focus_sound_handle[focus_index].user_data);
-                       debug_error("[CALLBACK END]");
+                       debug_msg("[CALLBACK END]");
                        if (g_focus_session_interrupt_info.user_cb) {
-                               debug_error("sending session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
+                               debug_msg("sending session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
                                (g_focus_session_interrupt_info.user_cb)(cb_data.state, cb_data.stream_type, false, g_focus_session_interrupt_info.user_data);
                        }
                }
@@ -1055,7 +1177,7 @@ static gboolean _focus_callback_handler(gpointer d)
                        if (tmpfd < 0) {
                                char str_error[256];
                                strerror_r(errno, str_error, sizeof(str_error));
-                               debug_error("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n", tid, tmpfd, filename2, errno, str_error);
+                               debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n", tid, tmpfd, filename2, errno, str_error);
                                g_free(filename2);
                                g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
                                return FALSE;
@@ -1101,18 +1223,23 @@ static gboolean _focus_watch_callback_handler(gpointer d)
                        return FALSE;
                }
 
-               focus_index = _focus_find_index_by_handle(cb_data.handle);
+               focus_index = _focus_watch_find_index_by_handle(cb_data.handle);
                if (focus_index == -1) {
                        debug_error("Could not find index");
                        return FALSE;
                }
 
-               debug_error("lock focus_lock = %p", &g_focus_sound_handle[focus_index].focus_lock);
+               if (!g_focus_sound_handle[focus_index].is_used) {
+                       debug_warning("unsetting watch calllback has been already requested");
+                       goto SKIP_CB_AND_RET;
+               }
+
+               debug_msg("lock focus_lock = %p", &g_focus_sound_handle[focus_index].focus_lock);
                g_mutex_lock(&g_focus_sound_handle[focus_index].focus_lock);
 
                tid = g_focus_sound_handle[focus_index].focus_tid;
 
-               debug_error("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)", tid, cb_data.handle,  cb_data.type, cb_data.state, cb_data.stream_type);
+               debug_msg("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)", tid, cb_data.handle,  cb_data.type, cb_data.state, cb_data.stream_type);
 
                if (g_focus_sound_handle[focus_index].watch_callback == NULL) {
                        debug_msg("callback is null..");
@@ -1121,11 +1248,12 @@ static gboolean _focus_watch_callback_handler(gpointer d)
                        (g_focus_sound_handle[focus_index].watch_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type, cb_data.ext_info, g_focus_sound_handle[focus_index].user_data);
                        debug_msg("[CALLBACK END]");
                        if (g_focus_session_interrupt_info.user_cb) {
-                               debug_error("sending session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
+                               debug_msg("sending session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
                                (g_focus_session_interrupt_info.user_cb)(cb_data.state, cb_data.stream_type, true, g_focus_session_interrupt_info.user_data);
                        }
                }
 
+SKIP_CB_AND_RET:
 #ifdef CONFIG_ENABLE_RETCB
                {
                        int rett = 0;
@@ -1136,7 +1264,7 @@ static gboolean _focus_watch_callback_handler(gpointer d)
                        if (tmpfd < 0) {
                                char str_error[256];
                                strerror_r(errno, str_error, sizeof(str_error));
-                               debug_error("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n", tid, tmpfd, filename2, errno, str_error);
+                               debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n", tid, tmpfd, filename2, errno, str_error);
                                g_free(filename2);
                                g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
                                return FALSE;
@@ -1150,12 +1278,13 @@ static gboolean _focus_watch_callback_handler(gpointer d)
 #endif
        }
 
-       debug_error("unlock focus_lock = %p", &g_focus_sound_handle[focus_index].focus_lock);
-       g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
+       if (g_focus_sound_handle[focus_index].is_used) {
+               debug_msg("unlock focus_lock = %p", &g_focus_sound_handle[focus_index].focus_lock);
+               g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
+       }
 
        debug_fleave();
 
-
        return TRUE;
 }
 
@@ -1180,7 +1309,7 @@ static void _focus_open_callback(int index, bool is_for_watching)
        if (g_focus_sound_handle[index].focus_fd == -1) {
                debug_error("Open fail : index(%d), file open error(%d)", index, errno);
        } else {
-               debug_log("Open sucess : index(%d), filename(%s), fd(%d)", index, filename, g_focus_sound_handle[index].focus_fd);
+               debug_log("Open success : index(%d), filename(%s), fd(%d)", index, filename, g_focus_sound_handle[index].focus_fd);
        }
        g_free(filename);
        filename = NULL;
@@ -1215,7 +1344,7 @@ void _focus_close_callback(int index, bool is_for_watching)
                debug_error("Close fail : index(%d)", index);
        } else {
                close(g_focus_sound_handle[index].focus_fd);
-               debug_log("Close Sucess : index(%d)", index);
+               debug_log("Close Success : index(%d)", index);
        }
 
        if (is_for_watching) {
@@ -1477,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();
 
@@ -1571,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;
@@ -1738,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);
@@ -1771,7 +1921,7 @@ int mm_sound_client_unset_focus_watch_callback(int id)
        debug_fenter();
        MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
 
-       index = _focus_find_index_by_handle(id);
+       index = _focus_watch_find_index_by_handle(id);
        if (index == -1) {
                debug_error("Could not find index");
                ret = MM_ERROR_INVALID_ARGUMENT;
@@ -1780,6 +1930,8 @@ int mm_sound_client_unset_focus_watch_callback(int id)
 
        g_mutex_lock(&g_focus_sound_handle[index].focus_lock);
 
+       g_focus_sound_handle[index].is_used = false;
+
        ret = mm_sound_proxy_unset_focus_watch_callback(g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle, g_focus_sound_handle[index].is_for_session);
 
        if (ret == MM_ERROR_NONE)
@@ -1794,7 +1946,6 @@ int mm_sound_client_unset_focus_watch_callback(int id)
        g_focus_sound_handle[index].focus_fd = 0;
        g_focus_sound_handle[index].focus_tid = 0;
        g_focus_sound_handle[index].handle = 0;
-       g_focus_sound_handle[index].is_used = false;
 cleanup:
        MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
        debug_fleave();