Add support for device running changed callback
[platform/core/multimedia/libmm-sound.git] / mm_sound_proxy.c
index 9eb678b..699949b 100644 (file)
@@ -71,16 +71,15 @@ static int _notify_signal_handled(audio_event_t event, uint32_t event_id, uint32
                return MM_ERROR_SOUND_INTERNAL;
        }
 
-       if ((ret = mm_sound_dbus_emit_signal(AUDIO_PROVIDER_AUDIO_CLIENT, AUDIO_EVENT_CLIENT_HANDLED, params))) {
+       if ((ret = mm_sound_dbus_emit_signal(AUDIO_PROVIDER_AUDIO_CLIENT, AUDIO_EVENT_CLIENT_HANDLED, params)))
                debug_error("dbus send signal for client handled failed");
-       }
 
        debug_fleave();
        return ret;
 }
 
 static int parse_device_variant(GVariant *v, int *device_id, const char **device_type, int *direction, int *state,
-                                               const char **device_name, int *vendor_id, int *product_id, int *stream_id, int *stream_num)
+                                               const char **device_name, int *vendor_id, int *product_id, bool *is_running, int *stream_id, int *stream_num)
 {
        const char *v_type;
        GVariant *array_v;
@@ -94,7 +93,7 @@ static int parse_device_variant(GVariant *v, int *device_id, const char **device
        }
 
        v_type = g_variant_get_type_string(v);
-       if (g_variant_type_equal(v_type, "(isiisiiai)") == FALSE) {
+       if (g_variant_type_equal(v_type, "(isiisiibai)") == FALSE) {
                debug_error("device variant type not matching '%s'", v_type);
                return MM_ERROR_NONE;
        }
@@ -107,6 +106,7 @@ static int parse_device_variant(GVariant *v, int *device_id, const char **device
        g_variant_iter_next(&iter, "&s", device_name);
        g_variant_iter_next(&iter, "i", vendor_id);
        g_variant_iter_next(&iter, "i", product_id);
+       g_variant_iter_next(&iter, "b", is_running);
 
        array_v = g_variant_iter_next_value(&iter);
        *stream_num = g_variant_iter_init(&array_iter, array_v);
@@ -137,6 +137,7 @@ static void dbus_callback(audio_event_t event, GVariant *params, void *userdata)
        int stream_id[MAX_STREAM_ON_DEVICE];
        int stream_num;
        int vendor_id, product_id;
+       bool is_running = FALSE;
 
        v_type = g_variant_get_type_string(params);
 
@@ -147,9 +148,9 @@ static void dbus_callback(audio_event_t event, GVariant *params, void *userdata)
                g_variant_get(params, "(&s&su)", &direction, &volume_type_str, &volume_level);
                ((mm_sound_volume_changed_wrapper_cb)(cb_data->user_cb))(direction, volume_type_str, volume_level, cb_data->user_data);
        } else if (event == AUDIO_EVENT_DEVICE_CONNECTED) {
-               gboolean is_connected = FALSE;
+               bool is_connected = false;
 
-               if (g_variant_type_equal(v_type, "(u(isiisiiai)b)") == FALSE) {
+               if (g_variant_type_equal(v_type, "(u(isiisiibai)b)") == FALSE) {
                        debug_error("Device connection changed signature not matching : %s", v_type);
                        return ;
                }
@@ -157,19 +158,19 @@ static void dbus_callback(audio_event_t event, GVariant *params, void *userdata)
                g_variant_iter_next(&iter, "u", &event_id);
                device_v = g_variant_iter_next_value(&iter);
                if (parse_device_variant(device_v, &device_id, &device_type, &direction, &state,
-                                                       &name, &vendor_id, &product_id, stream_id, &stream_num) < 0) {
+                                                       &name, &vendor_id, &product_id, &is_running, stream_id, &stream_num) < 0) {
                        debug_error("Failed to parse device variant");
                        return ;
                }
                g_variant_iter_next(&iter, "b", &is_connected);
 
                ((mm_sound_device_connected_wrapper_cb)(cb_data->user_cb))(device_id, device_type, direction,
-                       state, name, vendor_id, product_id, stream_id, stream_num, is_connected, cb_data->user_data);
+                       state, name, vendor_id, product_id, is_running, stream_id, stream_num, is_connected, cb_data->user_data);
                _notify_signal_handled(event, event_id, cb_data->subs_id, g_variant_new("(ib)", device_id, is_connected));
        } else if (event == AUDIO_EVENT_DEVICE_INFO_CHANGED) {
                int changed_device_info_type = 0;
 
-               if (g_variant_type_equal(v_type, "(u(isiisiiai)i)") == FALSE) {
+               if (g_variant_type_equal(v_type, "(u(isiisiibai)i)") == FALSE) {
                        debug_error("Device information changed signature not matching : %s", v_type);
                        return ;
                }
@@ -178,17 +179,17 @@ static void dbus_callback(audio_event_t event, GVariant *params, void *userdata)
                g_variant_iter_next(&iter, "u", &event_id);
                device_v = g_variant_iter_next_value(&iter);
                if (parse_device_variant(device_v, &device_id, &device_type, &direction, &state,
-                                                       &name, &vendor_id, &product_id, stream_id, &stream_num) < 0) {
+                                                       &name, &vendor_id, &product_id, &is_running, stream_id, &stream_num) < 0) {
                        debug_error("Failed to parse device variant");
                        return ;
                }
                g_variant_iter_next(&iter, "i", &changed_device_info_type);
 
                ((mm_sound_device_info_changed_wrapper_cb)(cb_data->user_cb))(device_id, device_type, direction,
-                       state, name, vendor_id, product_id, stream_id, stream_num, changed_device_info_type, cb_data->user_data);
+                       state, name, vendor_id, product_id, is_running, stream_id, stream_num, changed_device_info_type, cb_data->user_data);
        } else if (event == AUDIO_EVENT_DEVICE_STATE_CHANGED) {
 
-               if (g_variant_type_equal(v_type, "(u(isiisiiai))") == FALSE) {
+               if (g_variant_type_equal(v_type, "(u(isiisiibai))") == FALSE) {
                        debug_error("Device state changed signature not matching : %s", v_type);
                        return ;
                }
@@ -197,13 +198,31 @@ static void dbus_callback(audio_event_t event, GVariant *params, void *userdata)
                g_variant_iter_next(&iter, "u", &event_id);
                device_v = g_variant_iter_next_value(&iter);
                if (parse_device_variant(device_v, &device_id, &device_type, &direction, &state,
-                                                       &name, &vendor_id, &product_id, stream_id, &stream_num) < 0) {
+                                                       &name, &vendor_id, &product_id, &is_running, stream_id, &stream_num) < 0) {
                        debug_error("Failed to parse device variant");
                        return ;
                }
 
                ((mm_sound_device_state_changed_wrapper_cb)(cb_data->user_cb))(device_id, device_type, direction,
-                       state, name, vendor_id, product_id, stream_id, stream_num, cb_data->user_data);
+                       state, name, vendor_id, product_id, is_running, stream_id, stream_num, cb_data->user_data);
+       } else if (event == AUDIO_EVENT_DEVICE_RUNNING_CHANGED) {
+
+               if (g_variant_type_equal(v_type, "(u(isiisiibai))") == FALSE) {
+                       debug_error("Device state changed signature not matching : %s", v_type);
+                       return ;
+               }
+
+               g_variant_iter_init(&iter, params);
+               g_variant_iter_next(&iter, "u", &event_id);
+               device_v = g_variant_iter_next_value(&iter);
+               if (parse_device_variant(device_v, &device_id, &device_type, &direction, &state,
+                                                       &name, &vendor_id, &product_id, &is_running, stream_id, &stream_num) < 0) {
+                       debug_error("Failed to parse device variant");
+                       return ;
+               }
+
+               ((mm_sound_device_running_changed_wrapper_cb)(cb_data->user_cb))(device_id, device_type, direction,
+                       state, name, vendor_id, product_id, is_running, stream_id, stream_num, cb_data->user_data);
        } else if (event == AUDIO_EVENT_FOCUS_CHANGED) {
        } else if (event == AUDIO_EVENT_FOCUS_WATCH) {
        } else if (event == AUDIO_EVENT_TEST) {
@@ -368,15 +387,15 @@ int mm_sound_proxy_get_current_connected_device_list(int device_flags, GList** d
        g_variant_iter_init(&iter, child);
        while (1) {
                device_item = g_malloc0(sizeof(mm_sound_device_t));
-               if (device_item && g_variant_iter_loop(&iter, "(i&sii&sii)",
+               if (device_item && g_variant_iter_loop(&iter, "(i&sii&siib)",
                                        &device_item->id, &device_type_tmp, &device_item->io_direction, &device_item->state,
-                                       &device_name_tmp, &device_item->vendor_id, &device_item->product_id)) {
+                                       &device_name_tmp, &device_item->vendor_id, &device_item->product_id, &device_item->is_running)) {
                        MMSOUND_STRNCPY(device_item->name, device_name_tmp, MAX_DEVICE_NAME_NUM);
                        MMSOUND_STRNCPY(device_item->type, device_type_tmp, MAX_DEVICE_TYPE_STR_LEN);
                        *device_list = g_list_append(*device_list, device_item);
-                       debug_log("Added device id(%d) type(%17s) direction(%d) state(%d) name(%s) vendor-id(%04x), product-id(%04x)",
-                                       device_item->id, device_item->type,device_item->io_direction, device_item->state,
-                                       device_item->name, device_item->vendor_id, device_item->product_id);
+                       debug_log("Added device id(%d) type(%17s) direction(%d) state(%d) name(%s) vendor-id(%04x) product-id(%04x) is_running(%d)",
+                                       device_item->id, device_item->type, device_item->io_direction, device_item->state,
+                                       device_item->name, device_item->vendor_id, device_item->product_id, device_item->is_running);
                        device_item->stream_num = -1;
                } else {
                        if (device_item)
@@ -554,6 +573,36 @@ int mm_sound_proxy_remove_device_state_changed_callback(unsigned subs_id)
        return ret;
 }
 
+int mm_sound_proxy_add_device_running_changed_callback(int device_flags, mm_sound_device_running_changed_wrapper_cb func, void* userdata, mm_sound_proxy_userdata_free freefunc, unsigned *subs_id)
+{
+       int ret = MM_ERROR_NONE;
+       struct callback_data *cb_data;
+
+       debug_fenter();
+
+       CB_DATA_NEW(cb_data, func, userdata, freefunc);
+
+       if ((ret = mm_sound_dbus_signal_subscribe_to(AUDIO_PROVIDER_DEVICE_MANAGER, AUDIO_EVENT_DEVICE_RUNNING_CHANGED, dbus_callback, cb_data, simple_callback_data_free_func, &cb_data->subs_id)) != MM_ERROR_NONE)
+               debug_error("Add device running changed callback failed");
+       else
+               *subs_id = cb_data->subs_id;
+
+       debug_fleave();
+       return ret;
+}
+
+int mm_sound_proxy_remove_device_running_changed_callback(unsigned subs_id)
+{
+       int ret = MM_ERROR_NONE;
+       debug_fenter();
+
+       if ((ret = mm_sound_dbus_signal_unsubscribe(subs_id)) != MM_ERROR_NONE)
+               debug_error("remove device running changed callback failed");
+
+       debug_fleave();
+       return ret;
+}
+
 int mm_sound_proxy_set_volume_by_type(const char *volume_type, const unsigned volume_level)
 {
        int ret = MM_ERROR_NONE;
@@ -576,7 +625,7 @@ int mm_sound_proxy_set_volume_by_type(const char *volume_type, const unsigned vo
        if (result) {
                g_variant_get(result, "(&s)",  &reply);
                debug_log("reply : %s", reply);
-               if (!strcmp(reply, "STREAM_MANAGER_RETURN_ERROR"))
+               if (strcmp(reply, "STREAM_MANAGER_RETURN_OK"))
                        ret = MM_ERROR_SOUND_INTERNAL;
        } else {
                debug_error("reply null");
@@ -644,7 +693,7 @@ int mm_sound_proxy_set_filter_by_type(const char *stream_type, const char *filte
        if (result) {
                g_variant_get(result, "(&s)", &reply);
                debug_log("reply : %s", reply);
-               if (!strcmp(reply, "STREAM_MANAGER_RETURN_ERROR"))
+               if (strcmp(reply, "STREAM_MANAGER_RETURN_OK"))
                        ret = MM_ERROR_SOUND_INTERNAL;
        } else {
                debug_error("reply null");
@@ -680,7 +729,7 @@ int mm_sound_proxy_unset_filter_by_type(const char *stream_type)
        if (result) {
                g_variant_get(result, "(&s)", &reply);
                debug_log("reply : %s", reply);
-               if (!strcmp(reply, "STREAM_MANAGER_RETURN_ERROR"))
+               if (strcmp(reply, "STREAM_MANAGER_RETURN_OK"))
                        ret = MM_ERROR_SOUND_INTERNAL;
        } else {
                debug_error("reply null");
@@ -716,7 +765,7 @@ int mm_sound_proxy_control_filter_by_type(const char *stream_type, const char *f
        if (result) {
                g_variant_get(result, "(&s)", &reply);
                debug_log("reply : %s", reply);
-               if (!strcmp(reply, "STREAM_MANAGER_RETURN_ERROR"))
+               if (strcmp(reply, "STREAM_MANAGER_RETURN_OK"))
                        ret = MM_ERROR_SOUND_INTERNAL;
        } else {
                debug_error("reply null");
@@ -747,7 +796,7 @@ int mm_sound_proxy_play_tone(int tone, int repeat, int volume, int volume_config
        debug_fenter();
 
        params = g_variant_new("(iiiiiiibsi)", tone, repeat, volume, volume_config, session_type,
-                              session_options, client_pid , _enable_session, stream_type, stream_index);
+                                               session_options, client_pid, _enable_session, stream_type, stream_index);
        if (params) {
                if ((ret = mm_sound_dbus_method_call_to(AUDIO_PROVIDER_SOUND_SERVER, AUDIO_METHOD_PLAY_DTMF, params, &result)) != MM_ERROR_NONE) {
                        debug_error("dbus play tone failed");
@@ -829,7 +878,7 @@ int mm_sound_proxy_play_sound(const char* filename, int tone, int repeat, int vo
        debug_fenter();
 
        params = g_variant_new("(siiiiiiibsi)", filename, tone, repeat, volume,
-                     volume_config, session_type, session_options, client_pid, _enable_session, stream_type, stream_index);
+                               volume_config, session_type, session_options, client_pid, _enable_session, stream_type, stream_index);
        if (params) {
                if ((ret = mm_sound_dbus_method_call_to(AUDIO_PROVIDER_SOUND_SERVER, AUDIO_METHOD_PLAY_FILE_START, params, &result)) != MM_ERROR_NONE) {
                        debug_error("dbus play file failed");
@@ -1191,23 +1240,49 @@ int mm_sound_proxy_update_stream_focus_status(int focus_id, unsigned int status)
        if (params) {
                if ((ret = mm_sound_dbus_method_call_to(AUDIO_PROVIDER_STREAM_MANAGER, AUDIO_METHOD_UPDATE_STREAM_FOCUS_STATUS, params, &result)) != MM_ERROR_NONE) {
                        debug_error("dbus set volume by type failed");
-                       goto cleanup;
+                       if (result) {
+                               g_variant_get(result, "(&s)",  &reply);
+                               debug_log("reply : %s", reply);
+                               if (strcmp(reply, "STREAM_MANAGER_RETURN_OK"))
+                                       ret = MM_ERROR_SOUND_INTERNAL;
+                       }
                }
        } else {
                debug_error("Construct Param for method call failed");
                return MM_ERROR_SOUND_INTERNAL;
        }
 
-       if (result) {
-               g_variant_get(result, "(&s)",  &reply);
-               debug_log("reply : %s", reply);
-               if (!strcmp(reply, "STREAM_MANAGER_RETURN_ERROR"))
-                       ret = MM_ERROR_SOUND_INTERNAL;
+       if (result)
+               g_variant_unref(result);
+
+       debug_fleave();
+       return ret;
+}
+
+int mm_sound_proxy_deliver_focus(int pid, int src_id, int dst_id, mm_sound_focus_type_e focus_type)
+{
+       int ret = MM_ERROR_NONE;
+       char *reply = NULL;
+       GVariant *params = NULL, *result = NULL;
+
+       debug_fenter();
+
+       params = g_variant_new("(iiii)", pid, src_id, dst_id, focus_type);
+       if (params) {
+               if ((ret = mm_sound_dbus_method_call_to(AUDIO_PROVIDER_FOCUS_SERVER, AUDIO_METHOD_DELIVER_FOCUS, params, &result)) != MM_ERROR_NONE) {
+                       debug_error("dbus deliver focus failed");
+                       if (result) {
+                               g_variant_get(result, "(&s)", &reply);
+                               debug_log("reply : %s", reply);
+                               if (strcmp(reply, "STREAM_MANAGER_RETURN_OK"))
+                                       ret = MM_ERROR_SOUND_INTERNAL;
+                       }
+               }
        } else {
-               debug_error("reply null");
+               debug_error("Construct Param for method call failed");
+               return MM_ERROR_SOUND_INTERNAL;
        }
 
-cleanup:
        if (result)
                g_variant_unref(result);