Add protection code for focus-callback creation
[platform/core/multimedia/libmm-sound.git] / mm_sound_client.c
index c1b4926..87a6bf8 100644 (file)
@@ -80,7 +80,7 @@ struct callback_data {
                _cb_data->extra_data = _extradata; \
        } while (0)
 
-
+#ifdef USE_FOCUS
 typedef struct {
        int focus_tid;
        int handle;
@@ -104,7 +104,8 @@ typedef struct {
        int type;
        int state;
        char stream_type[MAX_STREAM_TYPE_LEN];
-       char name[MM_SOUND_NAME_NUM];
+       char ext_info[MM_SOUND_NAME_NUM];
+       int option;
 } focus_cb_data_lib;
 
 typedef struct {
@@ -112,13 +113,6 @@ typedef struct {
        void* user_data;
 } focus_session_interrupt_info_t;
 
-typedef struct {
-       /* handle to watch end of playing */
-       int watching_handle;
-       /* subscription id to unsubscribe when handle ended */
-       unsigned subs_id;
-} play_sound_end_callback_data_t;
-
 typedef gboolean (*focus_gLoopPollHandler_t)(gpointer d);
 
 GThread *g_focus_thread;
@@ -126,16 +120,24 @@ GMainLoop *g_focus_loop;
 focus_sound_info_t g_focus_sound_handle[FOCUS_HANDLE_MAX];
 focus_session_interrupt_info_t g_focus_session_interrupt_info = {NULL, NULL};
 static pthread_mutex_t g_index_mutex = PTHREAD_MUTEX_INITIALIZER;
-
 guint g_focus_signal_handle = 0;
+#endif
+
 gboolean g_need_emergent_exit = FALSE;
 
+typedef struct {
+       /* handle to watch end of playing */
+       int watching_handle;
+       /* subscription id to unsubscribe when handle ended */
+       unsigned subs_id;
+} play_sound_end_callback_data_t;
+
 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 */
 
@@ -182,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)
@@ -282,20 +284,20 @@ 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);
 
 }
 
 /*****************************************************************************************
                            DBUS SUPPORTED FUNCTIONS
 ******************************************************************************************/
-
+#ifdef USE_FOCUS
 void _mm_sound_client_focus_signal_callback(mm_sound_signal_name_t signal, int value, void *user_data)
 {
        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());
@@ -305,6 +307,7 @@ void _mm_sound_client_focus_signal_callback(mm_sound_signal_name_t signal, int v
                g_focus_signal_handle = 0;
        }
 }
+#endif
 
 int mm_sound_client_play_tone(int number, int volume_config, double volume, int time, int *handle, bool enable_session)
 {
@@ -351,7 +354,7 @@ int mm_sound_client_play_tone(int number, int volume_config, double volume, int
        mm_sound_convert_volume_type_to_stream_type(volume_type, stream_type);
        ret = mm_sound_proxy_play_tone(number, time, volume, volume_config,
                                        session_type, session_options, getpid(), enable_session, handle, stream_type, -1);
-
+#ifdef USE_FOCUS
        if (enable_session && !g_focus_signal_handle) {
                ret = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &g_focus_signal_handle, _mm_sound_client_focus_signal_callback, NULL);
                if (ret) {
@@ -359,6 +362,7 @@ int mm_sound_client_play_tone(int number, int volume_config, double volume, int
                        return MM_ERROR_POLICY_INTERNAL;
                }
        }
+#endif
 
        debug_fleave();
        return ret;
@@ -472,6 +476,7 @@ int mm_sound_client_play_sound(MMSoundPlayParam *param, int tone, int *handle)
                        debug_error("Add callback for play sound(%d) Failed", *handle);
                }
        }
+#ifdef USE_FOCUS
        if (!param->skip_session && !g_focus_signal_handle) {
                ret = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &g_focus_signal_handle, _mm_sound_client_focus_signal_callback, NULL);
                if (ret) {
@@ -479,6 +484,7 @@ int mm_sound_client_play_sound(MMSoundPlayParam *param, int tone, int *handle)
                        return MM_ERROR_POLICY_INTERNAL;
                }
        }
+#endif
 
 failed:
 
@@ -583,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");
@@ -654,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);
 }
@@ -695,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) {
@@ -710,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);
 }
@@ -750,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;
@@ -927,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;
 }
 
@@ -976,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;
@@ -1025,40 +1153,43 @@ 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);
-                       if (g_focus_sound_handle[focus_index].focus_callback== NULL) {
+                       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);
-                       (g_focus_sound_handle[focus_index].focus_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type, cb_data.name, g_focus_sound_handle[focus_index].user_data);
-                       debug_error("[CALLBACK END]");
+                       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_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);
                        }
                }
 #ifdef CONFIG_ENABLE_RETCB
-
-                               int rett = 0;
-                               int tmpfd = -1;
-                               unsigned int buf = 0;
-                               char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", g_focus_sound_handle[focus_index].focus_tid, cb_data.handle);
-                               tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
-                               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);
-                                       g_free(filename2);
-                                       g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
-                                       return FALSE;
-                               }
-                               buf = (unsigned int)((0x0000ffff & cb_data.handle) |(g_focus_sound_handle[focus_index].auto_reacquire << 16));
-                               rett = write(tmpfd, &buf, sizeof(buf));
-                               close(tmpfd);
+               {
+                       int rett = 0;
+                       int tmpfd = -1;
+                       unsigned int buf = 0;
+                       char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", g_focus_sound_handle[focus_index].focus_tid, cb_data.handle);
+                       tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
+                       if (tmpfd < 0) {
+                               char str_error[256];
+                               strerror_r(errno, str_error, sizeof(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);
-                               debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
+                               g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
+                               return FALSE;
+                       }
+                       /* buf contains data as below,
+                        * |<--12bits--><--4bits (reacquisition)--><--16bits (handle)-->| */
+                       buf = (unsigned int)((0x0000ffff & cb_data.handle) | (g_focus_sound_handle[focus_index].auto_reacquire << 16));
+                       rett = write(tmpfd, &buf, sizeof(buf));
+                       close(tmpfd);
+                       g_free(filename2);
+                       debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
+               }
 #endif
        }
 
@@ -1092,32 +1223,39 @@ 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..");
                } else {
                        debug_msg("[CALLBACK(%p) START]",g_focus_sound_handle[focus_index].watch_callback);
-                       (g_focus_sound_handle[focus_index].watch_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type, cb_data.name, g_focus_sound_handle[focus_index].user_data);
+                       (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;
                        int tmpfd = -1;
                        int buf = -1;
@@ -1126,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;
@@ -1136,17 +1274,17 @@ static gboolean _focus_watch_callback_handler(gpointer d)
                        close(tmpfd);
                        g_free(filename2);
                        debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
-
+               }
 #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;
 }
 
@@ -1171,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;
@@ -1206,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) {
@@ -1468,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();
 
@@ -1562,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;
@@ -1608,23 +1758,27 @@ cleanup:
        return ret;
 }
 
-int mm_sound_client_get_acquired_focus_stream_type(int focus_type, char **stream_type, char **additional_info)
+int mm_sound_client_get_acquired_focus_stream_type(int focus_type, char **stream_type, int *option, char **ext_info)
 {
        int ret = MM_ERROR_NONE;
+       char *ext_str = NULL;
 
        debug_fenter();
 
-       ret = mm_sound_proxy_get_acquired_focus_stream_type(focus_type, stream_type, additional_info);
-       if (ret == MM_ERROR_NONE)
-               debug_msg("[Client] Success to get stream type of acquired focus, stream_type(%s), additional_info(%s)\n", *stream_type, *additional_info);
-       else
+       ret = mm_sound_proxy_get_acquired_focus_stream_type(focus_type, stream_type, option, &ext_str);
+       if (ret == MM_ERROR_NONE) {
+               debug_msg("[Client] Success to get stream type of acquired focus, stream_type(%s), ext_info(%s)\n", *stream_type, ext_str);
+               *ext_info = strdup(ext_str);
+               g_free(ext_str);
+       } else {
                debug_error("[Client] Error occurred : 0x%x \n",ret);
+       }
 
        debug_fleave();
        return ret;
 }
 
-int mm_sound_client_acquire_focus(int id, mm_sound_focus_type_e type, const char *option)
+int mm_sound_client_acquire_focus(int id, mm_sound_focus_type_e type, int option, const char *ext_info)
 {
        int ret = MM_ERROR_NONE;
        int instance;
@@ -1641,8 +1795,7 @@ int mm_sound_client_acquire_focus(int id, mm_sound_focus_type_e type, const char
        }
        instance = g_focus_sound_handle[index].focus_tid;
 
-       ret = mm_sound_proxy_acquire_focus(instance, id, type, option, g_focus_sound_handle[index].is_for_session);
-
+       ret = mm_sound_proxy_acquire_focus(instance, id, type, option, ext_info, g_focus_sound_handle[index].is_for_session);
        if (ret == MM_ERROR_NONE)
                debug_msg("[Client] Success to acquire focus\n");
        else
@@ -1654,7 +1807,7 @@ cleanup:
        return ret;
 }
 
-int mm_sound_client_release_focus(int id, mm_sound_focus_type_e type, const char *option)
+int mm_sound_client_release_focus(int id, mm_sound_focus_type_e type, int option, const char *ext_info)
 {
        int ret = MM_ERROR_NONE;
        int instance;
@@ -1671,8 +1824,7 @@ int mm_sound_client_release_focus(int id, mm_sound_focus_type_e type, const char
        }
        instance = g_focus_sound_handle[index].focus_tid;
 
-       ret = mm_sound_proxy_release_focus(instance, id, type, option, g_focus_sound_handle[index].is_for_session);
-
+       ret = mm_sound_proxy_release_focus(instance, id, type, option, ext_info, g_focus_sound_handle[index].is_for_session);
        if (ret == MM_ERROR_NONE)
                debug_msg("[Client] Success to release focus\n");
        else
@@ -1727,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);
@@ -1760,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;
@@ -1769,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)
@@ -1783,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();