Fix stack-buffer-overflow bug issued by ASAN
[platform/core/multimedia/libmm-sound.git] / mm_sound_proxy.c
1 #include <stdint.h>
2 #include <glib.h>
3
4 #ifdef TIZEN_TV
5 #include <vconf.h>
6 #endif
7
8 #include <mm_error.h>
9 #include <mm_debug.h>
10
11 #include "include/mm_sound_proxy.h"
12 #include "include/mm_sound_common.h"
13 #include "include/mm_sound_dbus.h"
14 #include "include/mm_sound_intf.h"
15 #include "include/mm_sound_focus_socket.h"
16 #include "include/mm_sound_focus_private.h"
17
18 extern focus_sound_info_t g_focus_sound_handle[FOCUS_HANDLE_MAX];
19
20 struct callback_data {
21         void *user_cb;
22         void *user_data;
23         mm_sound_proxy_userdata_free free_func;
24         uint32_t subs_id;
25 };
26
27 #define CB_DATA_NEW(_cb_data, _func, _userdata, _freefunc) \
28         do { \
29                 _cb_data = (struct callback_data*) g_malloc0(sizeof(struct callback_data)); \
30                 _cb_data->user_cb = _func; \
31                 _cb_data->user_data = _userdata; \
32                 _cb_data->free_func = _freefunc; \
33                 _cb_data->subs_id = 0; \
34         } while (0)
35
36 /* subscribe is true when add callback,
37  * false when remove callback */
38 static int _notify_subscription(audio_event_t event, uint32_t subs_id, gboolean subscribe)
39 {
40         int ret = MM_ERROR_NONE;
41         GVariant *params = NULL;
42         const char *event_name = NULL;
43
44         debug_fenter();
45
46         if ((ret = mm_sound_dbus_get_event_name(event, &event_name) != MM_ERROR_NONE)) {
47                 debug_error("Failed to get event name");
48                 return MM_ERROR_SOUND_INTERNAL;
49         }
50
51         if (!(params = g_variant_new("(sub)", event_name, subs_id, subscribe))) {
52                 debug_error("Construct Param failed");
53                 return MM_ERROR_SOUND_INTERNAL;
54         }
55
56         if ((ret = mm_sound_dbus_emit_signal(AUDIO_PROVIDER_AUDIO_CLIENT, AUDIO_EVENT_CLIENT_SUBSCRIBED, params)))
57                 debug_error("dbus send signal for client subscribed failed");
58
59         debug_fleave();
60         return ret;
61 }
62
63 static int _notify_signal_handled(audio_event_t event, uint32_t event_id, uint32_t subs_id, GVariant *signal_params)
64 {
65         int ret = MM_ERROR_NONE;
66         GVariant *params = NULL;
67         const char *event_name = NULL;
68
69         debug_fenter();
70
71         if ((ret = mm_sound_dbus_get_event_name(event, &event_name) != MM_ERROR_NONE)) {
72                 debug_error("Failed to get event name");
73                 return MM_ERROR_SOUND_INTERNAL;
74         }
75
76         if (!(params = g_variant_new("(usuv)", event_id, event_name, subs_id, signal_params))) {
77                 debug_error("Construct Param failed");
78                 return MM_ERROR_SOUND_INTERNAL;
79         }
80
81         if ((ret = mm_sound_dbus_emit_signal(AUDIO_PROVIDER_AUDIO_CLIENT, AUDIO_EVENT_CLIENT_HANDLED, params)))
82                 debug_error("dbus send signal for client handled failed");
83
84         debug_fleave();
85         return ret;
86 }
87
88 static int parse_device_variant(GVariant *v, int *device_id, const char **device_type, int *direction, int *state,
89                                                 const char **device_name, int *vendor_id, int *product_id, gboolean *is_running, int *stream_id, int *stream_num)
90 {
91         const char *v_type;
92         GVariant *array_v;
93         GVariantIter iter, array_iter;
94         int i = 0;
95         int ret = MM_ERROR_NONE;
96
97         if (v == NULL) {
98                 debug_error("Variant NULL");
99                 return MM_ERROR_NONE;
100         }
101
102         v_type = g_variant_get_type_string(v);
103         if (g_variant_type_equal(v_type, "(isiisiibai)") == FALSE) {
104                 debug_error("device variant type not matching '%s'", v_type);
105                 return MM_ERROR_NONE;
106         }
107
108         g_variant_iter_init(&iter, v);
109         g_variant_iter_next(&iter, "i", device_id);
110         g_variant_iter_next(&iter, "&s", device_type);
111         g_variant_iter_next(&iter, "i", direction);
112         g_variant_iter_next(&iter, "i", state);
113         g_variant_iter_next(&iter, "&s", device_name);
114         g_variant_iter_next(&iter, "i", vendor_id);
115         g_variant_iter_next(&iter, "i", product_id);
116         g_variant_iter_next(&iter, "b", is_running);
117
118         array_v = g_variant_iter_next_value(&iter);
119         *stream_num = g_variant_iter_init(&array_iter, array_v);
120         if (*stream_num > MAX_STREAM_ON_DEVICE) {
121                 debug_error("too many streams on device %d", *stream_num);
122                 ret = MM_ERROR_SOUND_INTERNAL;
123                 goto finish;
124         }
125
126         while (g_variant_iter_loop(&array_iter, "i", &stream_id[i++])) ;
127 finish:
128         g_variant_unref(array_v);
129
130         return ret;
131 }
132
133 /* This callback unmarshall general-formed paramters to subject specific parameters,
134  * and call proper callback */
135 static void dbus_callback(audio_event_t event, GVariant *params, void *userdata)
136 {
137         struct callback_data *cb_data  = (struct callback_data*) userdata;
138         uint32_t event_id;
139         const char *name = NULL, *device_type = NULL;
140         int device_id, direction, state;
141         const gchar *v_type;
142         GVariantIter iter;
143         GVariant *device_v;
144         int stream_id[MAX_STREAM_ON_DEVICE];
145         int stream_num;
146         int vendor_id, product_id;
147         gboolean is_running = FALSE;
148
149         v_type = g_variant_get_type_string(params);
150
151         if (event == AUDIO_EVENT_VOLUME_CHANGED) {
152                 char *volume_type_str = NULL, *direction = NULL;
153                 unsigned volume_level;
154
155                 g_variant_get(params, "(&s&su)", &direction, &volume_type_str, &volume_level);
156                 ((mm_sound_volume_changed_wrapper_cb)(cb_data->user_cb))(direction, volume_type_str, volume_level, cb_data->user_data);
157         } else if (event == AUDIO_EVENT_DEVICE_CONNECTED) {
158                 gboolean is_connected = FALSE;
159
160                 if (g_variant_type_equal(v_type, "(u(isiisiibai)b)") == FALSE) {
161                         debug_error("Device connection changed signature not matching : %s", v_type);
162                         return ;
163                 }
164                 g_variant_iter_init(&iter, params);
165                 g_variant_iter_next(&iter, "u", &event_id);
166                 device_v = g_variant_iter_next_value(&iter);
167                 if (parse_device_variant(device_v, &device_id, &device_type, &direction, &state,
168                                                         &name, &vendor_id, &product_id, &is_running, stream_id, &stream_num) < 0) {
169                         debug_error("Failed to parse device variant");
170                         return ;
171                 }
172                 g_variant_iter_next(&iter, "b", &is_connected);
173
174                 ((mm_sound_device_connected_wrapper_cb)(cb_data->user_cb))(device_id, device_type, direction,
175                         state, name, vendor_id, product_id, (bool)is_running, stream_id, stream_num, (bool)is_connected, cb_data->user_data);
176                 _notify_signal_handled(event, event_id, cb_data->subs_id, g_variant_new("(ib)", device_id, (bool)is_connected));
177         } else if (event == AUDIO_EVENT_DEVICE_INFO_CHANGED) {
178                 int changed_device_info_type = 0;
179
180                 if (g_variant_type_equal(v_type, "(u(isiisiibai)i)") == FALSE) {
181                         debug_error("Device information changed signature not matching : %s", v_type);
182                         return ;
183                 }
184
185                 g_variant_iter_init(&iter, params);
186                 g_variant_iter_next(&iter, "u", &event_id);
187                 device_v = g_variant_iter_next_value(&iter);
188                 if (parse_device_variant(device_v, &device_id, &device_type, &direction, &state,
189                                                         &name, &vendor_id, &product_id, &is_running, stream_id, &stream_num) < 0) {
190                         debug_error("Failed to parse device variant");
191                         return ;
192                 }
193                 g_variant_iter_next(&iter, "i", &changed_device_info_type);
194
195                 ((mm_sound_device_info_changed_wrapper_cb)(cb_data->user_cb))(device_id, device_type, direction,
196                         state, name, vendor_id, product_id, (bool)is_running, stream_id, stream_num, changed_device_info_type, cb_data->user_data);
197         } else if (event == AUDIO_EVENT_DEVICE_STATE_CHANGED) {
198
199                 if (g_variant_type_equal(v_type, "(u(isiisiibai))") == FALSE) {
200                         debug_error("Device state changed signature not matching : %s", v_type);
201                         return ;
202                 }
203
204                 g_variant_iter_init(&iter, params);
205                 g_variant_iter_next(&iter, "u", &event_id);
206                 device_v = g_variant_iter_next_value(&iter);
207                 if (parse_device_variant(device_v, &device_id, &device_type, &direction, &state,
208                                                         &name, &vendor_id, &product_id, &is_running, stream_id, &stream_num) < 0) {
209                         debug_error("Failed to parse device variant");
210                         return ;
211                 }
212
213                 ((mm_sound_device_state_changed_wrapper_cb)(cb_data->user_cb))(device_id, device_type, direction,
214                         state, name, vendor_id, product_id, (bool)is_running, stream_id, stream_num, cb_data->user_data);
215         } else if (event == AUDIO_EVENT_DEVICE_RUNNING_CHANGED) {
216
217                 if (g_variant_type_equal(v_type, "(u(isiisiibai))") == FALSE) {
218                         debug_error("Device state changed signature not matching : %s", v_type);
219                         return ;
220                 }
221
222                 g_variant_iter_init(&iter, params);
223                 g_variant_iter_next(&iter, "u", &event_id);
224                 device_v = g_variant_iter_next_value(&iter);
225                 if (parse_device_variant(device_v, &device_id, &device_type, &direction, &state,
226                                                         &name, &vendor_id, &product_id, &is_running, stream_id, &stream_num) < 0) {
227                         debug_error("Failed to parse device variant");
228                         return ;
229                 }
230
231                 ((mm_sound_device_running_changed_wrapper_cb)(cb_data->user_cb))(device_id, device_type, direction,
232                         state, name, vendor_id, product_id, (bool)is_running, stream_id, stream_num, cb_data->user_data);
233         } else if (event == AUDIO_EVENT_FOCUS_CHANGED) {
234         } else if (event == AUDIO_EVENT_FOCUS_WATCH) {
235         } else if (event == AUDIO_EVENT_TEST) {
236                 int test_var = 0;
237                 g_variant_get(params, "(i)", &test_var);
238                 ((mm_sound_test_cb)(cb_data->user_cb))(test_var, cb_data->user_data);
239         } else if (event == AUDIO_EVENT_PLAY_FILE_END) {
240                 int ended_handle = 0;
241                 g_variant_get(params, "(i)", &ended_handle);
242                 ((mm_sound_stop_callback_wrapper_func)(cb_data->user_cb))(ended_handle, cb_data->user_data);
243         }
244 }
245
246 static void simple_callback_data_free_func(void *data)
247 {
248         struct callback_data *cb_data = (struct callback_data*) data;
249
250         if (cb_data) {
251                 if (cb_data->free_func)
252                         cb_data->free_func(cb_data->user_data);
253                 g_free(cb_data);
254         }
255 }
256
257 int mm_sound_proxy_add_test_callback(mm_sound_test_cb func, void *userdata, mm_sound_proxy_userdata_free freefunc, unsigned *subs_id)
258 {
259         int ret = MM_ERROR_NONE;
260         struct callback_data *cb_data;
261
262         debug_fenter();
263
264         CB_DATA_NEW(cb_data, func, userdata, freefunc);
265
266         if ((ret = mm_sound_dbus_signal_subscribe_to(AUDIO_PROVIDER_SOUND_SERVER, AUDIO_EVENT_TEST, dbus_callback, cb_data, simple_callback_data_free_func, &cb_data->subs_id)) != MM_ERROR_NONE)
267                 debug_error("add test callback failed");
268         else
269                 *subs_id = cb_data->subs_id;
270
271         debug_fleave();
272         return ret;
273 }
274
275 int mm_sound_proxy_remove_test_callback(unsigned subs_id)
276 {
277         int ret = MM_ERROR_NONE;
278         debug_fenter();
279
280         if ((ret = mm_sound_dbus_signal_unsubscribe(subs_id)) != MM_ERROR_NONE)
281                 debug_error("remove test callback failed");
282
283         debug_fleave();
284         return ret;
285 }
286
287 int mm_sound_proxy_test(int a, int b, int *get)
288 {
289         int ret = MM_ERROR_NONE;
290         int reply = 0;
291         GVariant *params = NULL, *result = NULL;
292
293         debug_fenter();
294
295         params = g_variant_new("(ii)", a, b);
296         if (params) {
297                 if ((ret = mm_sound_dbus_method_call_to(AUDIO_PROVIDER_SOUND_SERVER, AUDIO_METHOD_TEST, params, &result)) != MM_ERROR_NONE) {
298                         debug_error("dbus test call failed");
299                         goto cleanup;
300                 }
301         } else {
302                 debug_error("Construct Param for method call failed");
303                 return MM_ERROR_SOUND_INTERNAL;
304         }
305
306         if (result) {
307                 g_variant_get(result, "(i)",  &reply);
308                 debug_log("reply : %d", reply);
309                 *get = reply;
310         } else {
311                 debug_error("reply null");
312         }
313
314 cleanup:
315         if (result)
316                 g_variant_unref(result);
317
318         debug_fleave();
319         return ret;
320 }
321
322 int mm_sound_proxy_is_stream_on_device(int stream_id, int device_id, bool *is_on)
323 {
324         int ret = MM_ERROR_NONE;
325         GVariant *params, *result;
326         gboolean _is_on;
327
328         debug_fenter();
329
330         if (!is_on) {
331                 debug_error("Invalid Parameter, is_on null");
332                 return MM_ERROR_INVALID_ARGUMENT;
333         }
334
335         if ((params = g_variant_new("(ii)", stream_id, device_id)) == NULL) {
336                 debug_error("Construct Param for query is stream on device failed");
337                 return MM_ERROR_SOUND_INTERNAL;
338         }
339
340         if ((ret = mm_sound_dbus_method_call_to(AUDIO_PROVIDER_DEVICE_MANAGER, AUDIO_METHOD_IS_STREAM_ON_DEVICE, params, &result)) != MM_ERROR_NONE) {
341                 debug_error("is stream on device failed");
342                 goto cleanup;
343         }
344
345         if (result) {
346                 g_variant_get(result, "(b)",  &_is_on);
347                 debug_log("is_on : %d", _is_on);
348                 *is_on = (bool)_is_on;
349         } else {
350                 debug_error("reply null");
351                 ret = MM_ERROR_SOUND_INTERNAL;
352         }
353
354 cleanup:
355         if (params)
356                 g_variant_unref(params);
357         if (result)
358                 g_variant_unref(result);
359
360         debug_fleave();
361         return ret;
362 }
363
364 #ifdef TIZEN_TV
365 #define _VCONF_KEY_SOUND_SPEAKER_SELECTION "file/private/sound/feature/SpeakerSelection"
366 #define _AUDIO_TV_OUTPUT_BT_HEADSET 5
367
368 static mm_sound_device_t* _get_tv_bt_device(void)
369 {
370         int speaker_value = 0;
371         mm_sound_device_t* device_item = NULL;
372
373         if (vconf_get_int(_VCONF_KEY_SOUND_SPEAKER_SELECTION, &speaker_value) == VCONF_ERROR) {
374                 debug_error("vconf_get_int(%s) failed..", _VCONF_KEY_SOUND_SPEAKER_SELECTION);
375                 return NULL;
376         }
377
378         debug_warning("speaker selection value = %d", speaker_value);
379         if (speaker_value != _AUDIO_TV_OUTPUT_BT_HEADSET)
380                 return NULL;
381
382         device_item = g_malloc0(sizeof(mm_sound_device_t));
383         if (!device_item)
384                 return NULL;
385
386         MMSOUND_STRNCPY(device_item->name, "BluetoothMedia", MAX_DEVICE_NAME_NUM);
387         MMSOUND_STRNCPY(device_item->type, "bt-a2dp", MAX_DEVICE_TYPE_STR_LEN);
388         device_item->id = 99;
389         device_item->io_direction = DEVICE_IO_DIRECTION_OUT;
390         device_item->state = DEVICE_STATE_ACTIVATED;
391         device_item->vendor_id = -1;
392         device_item->product_id = -1;
393         device_item->stream_num = -1;
394
395         return device_item;
396 }
397 #endif /* TIZEN_TV */
398
399 int mm_sound_proxy_get_current_connected_device_list(int device_flags, GList** device_list)
400 {
401         int ret = MM_ERROR_NONE;
402         GVariant *result = NULL, *child = NULL;
403         GVariant *params = NULL;
404         GVariantIter iter;
405         mm_sound_device_t* device_item;
406         const gchar *device_name_tmp = NULL, *device_type_tmp = NULL;
407
408         debug_fenter();
409
410         if (!device_list) {
411                 debug_error("Invalid Parameter, device_list null");
412                 ret = MM_ERROR_INVALID_ARGUMENT;
413                 goto cleanup;
414         }
415
416         params = g_variant_new("(i)", device_flags);
417
418         if (params) {
419                 if ((ret = mm_sound_dbus_method_call_to(AUDIO_PROVIDER_DEVICE_MANAGER, AUDIO_METHOD_GET_CONNECTED_DEVICE_LIST, params, &result)) != MM_ERROR_NONE) {
420                         debug_error("Get current connected device list failed");
421                         goto cleanup;
422                 }
423         } else {
424                 debug_error("Construct Param for get current connected device failed");
425                 return MM_ERROR_SOUND_INTERNAL;
426         }
427
428         child = g_variant_get_child_value(result, 0);
429         g_variant_iter_init(&iter, child);
430         while (1) {
431                 device_item = g_malloc0(sizeof(mm_sound_device_t));
432                 if (device_item && g_variant_iter_loop(&iter, "(i&sii&siib)",
433                                         &device_item->id, &device_type_tmp, &device_item->io_direction, &device_item->state,
434                                         &device_name_tmp, &device_item->vendor_id, &device_item->product_id, &device_item->is_running)) {
435                         MMSOUND_STRNCPY(device_item->name, device_name_tmp, MAX_DEVICE_NAME_NUM);
436                         MMSOUND_STRNCPY(device_item->type, device_type_tmp, MAX_DEVICE_TYPE_STR_LEN);
437                         *device_list = g_list_append(*device_list, device_item);
438                         debug_log("Added device id(%d) type(%17s) direction(%d) state(%d) name(%s) vendor-id(%04x) product-id(%04x) is_running(%d)",
439                                         device_item->id, device_item->type, device_item->io_direction, device_item->state,
440                                         device_item->name, device_item->vendor_id, device_item->product_id, device_item->is_running);
441                         device_item->stream_num = -1;
442                 } else {
443                         if (device_item)
444                                 g_free(device_item);
445                         break;
446                 }
447         }
448
449
450 #ifdef TIZEN_TV
451         device_item = _get_tv_bt_device();
452         if (device_item) {
453                 *device_list = g_list_append(*device_list, device_item);
454                 debug_msg("Added TV bt device id(%d) type(%17s) direction(%d) state(%d) name(%s) vendor-id(%04x) product-id(%04x)",
455                                         device_item->id, device_item->type, device_item->io_direction, device_item->state,
456                                         device_item->name, device_item->vendor_id, device_item->product_id);
457         }
458 #endif /* TIZEN_TV */
459
460 cleanup:
461         if (result)
462                 g_variant_unref(result);
463
464         debug_fleave();
465         return ret;
466 }
467
468 int mm_sound_proxy_get_device_by_id(int device_id, mm_sound_device_t **device)
469 {
470         int ret = MM_ERROR_NONE;
471         GVariant *params = NULL, *result = NULL;
472         mm_sound_device_t* device_item = NULL;
473         const gchar *device_name_tmp = NULL, *device_type_tmp = NULL;
474
475         debug_fenter();
476
477         if (!device || device_id < 1) {
478                 debug_error("Invalid Parameter, device null or improper device id");
479                 return MM_ERROR_INVALID_ARGUMENT;
480         }
481
482         if ((params = g_variant_new("(i)", device_id)) == NULL) {
483                 debug_error("Construct Param for get device by id failed");
484                 return MM_ERROR_SOUND_INTERNAL;
485         }
486
487         if ((ret = mm_sound_dbus_method_call_to(AUDIO_PROVIDER_DEVICE_MANAGER, AUDIO_METHOD_GET_DEVICE_BY_ID, params, &result)) != MM_ERROR_NONE) {
488                 debug_error("get device by id failed");
489                 ret = MM_ERROR_SOUND_NO_DATA;
490                 goto cleanup;
491         }
492
493         if (result) {
494                 if ((device_item = g_malloc0(sizeof(mm_sound_device_t))) == NULL) {
495                         debug_error("Alloc device handle failed");
496                         ret = MM_ERROR_SOUND_INTERNAL;
497                         goto cleanup;
498                 }
499
500                 g_variant_get(result, "(i&sii&sii)",
501                                 &device_item->id, &device_type_tmp, &device_item->io_direction,
502                                 &device_item->state, &device_name_tmp,
503                                 &device_item->vendor_id, &device_item->product_id);
504                 MMSOUND_STRNCPY(device_item->name, device_name_tmp, MAX_DEVICE_NAME_NUM);
505                 MMSOUND_STRNCPY(device_item->type, device_type_tmp, MAX_DEVICE_TYPE_STR_LEN);
506                 debug_log("Get device id(%d) type(%17s) direction(%d) state(%d) name(%s) vendor-id(%04x) product-id(%04x)",
507                                 device_item->id, device_item->type, device_item->io_direction,
508                                 device_item->state, device_item->name,
509                                 device_item->vendor_id, device_item->product_id);
510                 device_item->stream_num = -1;
511                 *device = device_item;
512         } else {
513                 debug_error("reply null");
514                 ret = MM_ERROR_SOUND_INTERNAL;
515         }
516
517 cleanup:
518         if (result)
519                 g_variant_unref(result);
520
521         debug_fleave();
522         return ret;
523 }
524
525 int mm_sound_proxy_add_device_connected_callback(mm_sound_device_connected_wrapper_cb func, void *userdata, mm_sound_proxy_userdata_free freefunc, unsigned *subs_id)
526 {
527         int ret = MM_ERROR_NONE;
528         struct callback_data *cb_data;
529
530         debug_fenter();
531
532         CB_DATA_NEW(cb_data, func, userdata, freefunc);
533
534         if ((ret = mm_sound_dbus_signal_subscribe_to(AUDIO_PROVIDER_DEVICE_MANAGER, AUDIO_EVENT_DEVICE_CONNECTED, dbus_callback, cb_data, simple_callback_data_free_func, &cb_data->subs_id)) != MM_ERROR_NONE) {
535                 debug_error("add device connected callback failed");
536                 goto finish;
537         }
538
539         if ((ret = _notify_subscription(AUDIO_EVENT_DEVICE_CONNECTED, cb_data->subs_id, TRUE)) != MM_ERROR_NONE) {
540                 debug_error("failed to notify subscription of device connected event");
541                 goto finish;
542         }
543
544         *subs_id = cb_data->subs_id;
545
546 finish:
547         debug_fleave();
548         return ret;
549 }
550
551 int mm_sound_proxy_remove_device_connected_callback(unsigned subs_id)
552 {
553         int ret = MM_ERROR_NONE;
554         debug_fenter();
555
556         if ((ret = mm_sound_dbus_signal_unsubscribe(subs_id)) != MM_ERROR_NONE) {
557                 debug_error("remove device connected callback failed");
558                 goto finish;
559         }
560
561         if ((ret = _notify_subscription(AUDIO_EVENT_DEVICE_CONNECTED, subs_id, FALSE)) != MM_ERROR_NONE)
562                 debug_error("failed to notify unsubscription of device connected event");
563
564 finish:
565         debug_fleave();
566         return ret;
567 }
568
569 int mm_sound_proxy_add_device_info_changed_callback(mm_sound_device_info_changed_wrapper_cb func, void* userdata, mm_sound_proxy_userdata_free freefunc, unsigned *subs_id)
570 {
571         int ret = MM_ERROR_NONE;
572         struct callback_data *cb_data;
573
574         debug_fenter();
575
576         CB_DATA_NEW(cb_data, func, userdata, freefunc);
577
578         if ((ret = mm_sound_dbus_signal_subscribe_to(AUDIO_PROVIDER_DEVICE_MANAGER, AUDIO_EVENT_DEVICE_INFO_CHANGED, dbus_callback, cb_data, simple_callback_data_free_func, &cb_data->subs_id)) != MM_ERROR_NONE)
579                 debug_error("Add device info changed callback failed");
580         else
581                 *subs_id = cb_data->subs_id;
582
583         debug_fleave();
584         return ret;
585 }
586
587 int mm_sound_proxy_remove_device_info_changed_callback(unsigned subs_id)
588 {
589         int ret = MM_ERROR_NONE;
590         debug_fenter();
591
592         if ((ret = mm_sound_dbus_signal_unsubscribe(subs_id)) != MM_ERROR_NONE)
593                 debug_error("remove device info changed callback failed");
594
595         debug_fleave();
596         return ret;
597 }
598
599 int mm_sound_proxy_add_device_state_changed_callback(mm_sound_device_state_changed_wrapper_cb func, void* userdata, mm_sound_proxy_userdata_free freefunc, unsigned *subs_id)
600 {
601         int ret = MM_ERROR_NONE;
602         struct callback_data *cb_data;
603
604         debug_fenter();
605
606         CB_DATA_NEW(cb_data, func, userdata, freefunc);
607
608         if ((ret = mm_sound_dbus_signal_subscribe_to(AUDIO_PROVIDER_DEVICE_MANAGER, AUDIO_EVENT_DEVICE_STATE_CHANGED, dbus_callback, cb_data, simple_callback_data_free_func, &cb_data->subs_id)) != MM_ERROR_NONE)
609                 debug_error("Add device state changed callback failed");
610         else
611                 *subs_id = cb_data->subs_id;
612
613         debug_fleave();
614         return ret;
615 }
616
617 int mm_sound_proxy_remove_device_state_changed_callback(unsigned subs_id)
618 {
619         int ret = MM_ERROR_NONE;
620         debug_fenter();
621
622         if ((ret = mm_sound_dbus_signal_unsubscribe(subs_id)) != MM_ERROR_NONE)
623                 debug_error("remove device state changed callback failed");
624
625         debug_fleave();
626         return ret;
627 }
628
629 int mm_sound_proxy_add_device_running_changed_callback(mm_sound_device_running_changed_wrapper_cb func, void* userdata, mm_sound_proxy_userdata_free freefunc, unsigned *subs_id)
630 {
631         int ret = MM_ERROR_NONE;
632         struct callback_data *cb_data;
633
634         debug_fenter();
635
636         CB_DATA_NEW(cb_data, func, userdata, freefunc);
637
638         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)
639                 debug_error("Add device running changed callback failed");
640         else
641                 *subs_id = cb_data->subs_id;
642
643         debug_fleave();
644         return ret;
645 }
646
647 int mm_sound_proxy_remove_device_running_changed_callback(unsigned subs_id)
648 {
649         int ret = MM_ERROR_NONE;
650         debug_fenter();
651
652         if ((ret = mm_sound_dbus_signal_unsubscribe(subs_id)) != MM_ERROR_NONE)
653                 debug_error("remove device running changed callback failed");
654
655         debug_fleave();
656         return ret;
657 }
658
659 int mm_sound_proxy_set_volume_by_type(const char *volume_type, const unsigned volume_level)
660 {
661         int ret = MM_ERROR_NONE;
662         char *reply = NULL, *direction = "out";
663         GVariant *params = NULL, *result = NULL;
664
665         debug_fenter();
666
667         params = g_variant_new("(ssu)", direction, volume_type, volume_level);
668         if (params) {
669                 if ((ret = mm_sound_dbus_method_call_to(AUDIO_PROVIDER_STREAM_MANAGER, AUDIO_METHOD_SET_VOLUME_LEVEL, params, &result)) != MM_ERROR_NONE) {
670                         debug_error("dbus set volume by type failed");
671                         goto cleanup;
672                 }
673         } else {
674                 debug_error("Construct Param for method call failed");
675                 return MM_ERROR_SOUND_INTERNAL;
676         }
677
678         if (result) {
679                 g_variant_get(result, "(&s)",  &reply);
680                 debug_log("reply : %s", reply);
681                 if (strcmp(reply, "STREAM_MANAGER_RETURN_OK"))
682                         ret = MM_ERROR_SOUND_INTERNAL;
683         } else {
684                 debug_error("reply null");
685         }
686
687 cleanup:
688         if (result)
689                 g_variant_unref(result);
690
691         debug_fleave();
692         return ret;
693 }
694
695 int mm_sound_proxy_add_volume_changed_callback(mm_sound_volume_changed_wrapper_cb func, void* userdata, mm_sound_proxy_userdata_free freefunc, unsigned *subs_id)
696 {
697         int ret = MM_ERROR_NONE;
698         struct callback_data *cb_data;
699
700         debug_fenter();
701
702         CB_DATA_NEW(cb_data, func, userdata, freefunc);
703
704         if ((ret = mm_sound_dbus_signal_subscribe_to(AUDIO_PROVIDER_STREAM_MANAGER, AUDIO_EVENT_VOLUME_CHANGED, dbus_callback, cb_data, simple_callback_data_free_func, &cb_data->subs_id)) != MM_ERROR_NONE)
705                 debug_error("Add Volume changed callback failed");
706         else
707                 *subs_id = cb_data->subs_id;
708
709
710         debug_fleave();
711
712         return ret;
713 }
714
715 int mm_sound_proxy_remove_volume_changed_callback(unsigned subs_id)
716 {
717         int ret = MM_ERROR_NONE;
718         debug_fenter();
719
720         if ((ret = mm_sound_dbus_signal_unsubscribe(subs_id)) != MM_ERROR_NONE)
721                 debug_error("Remove Volume changed callback failed");
722
723         debug_fleave();
724         return ret;
725 }
726
727 int mm_sound_proxy_set_filter_by_type(const char *stream_type, const char *filter_name, const char *filter_parameters, const char *filter_group)
728 {
729         int ret = MM_ERROR_NONE;
730         char *reply = NULL;
731         GVariant *params = NULL, *result = NULL;
732
733         debug_fenter();
734
735         params = g_variant_new("(ssss)", filter_name, filter_parameters, filter_group, stream_type);
736         if (params) {
737                 if ((ret = mm_sound_dbus_method_call_to(AUDIO_PROVIDER_STREAM_MANAGER, AUDIO_METHOD_SET_FILTER, params, &result)) != MM_ERROR_NONE) {
738                         debug_error("dbus set filter by type failed");
739                         goto cleanup;
740                 }
741         } else {
742                 debug_error("construct param for method call failed");
743                 return MM_ERROR_SOUND_INTERNAL;
744         }
745
746         if (result) {
747                 g_variant_get(result, "(&s)", &reply);
748                 debug_log("reply : %s", reply);
749                 if (strcmp(reply, "STREAM_MANAGER_RETURN_OK"))
750                         ret = MM_ERROR_SOUND_INTERNAL;
751         } else {
752                 debug_error("reply null");
753         }
754
755 cleanup:
756         if (result)
757                 g_variant_unref(result);
758
759         debug_fleave();
760         return ret;
761 }
762
763 int mm_sound_proxy_unset_filter_by_type(const char *stream_type)
764 {
765         int ret = MM_ERROR_NONE;
766         char *reply = NULL;
767         GVariant *params = NULL, *result = NULL;
768
769         debug_fenter();
770
771         params = g_variant_new("(s)", stream_type);
772         if (params) {
773                 if ((ret = mm_sound_dbus_method_call_to(AUDIO_PROVIDER_STREAM_MANAGER, AUDIO_METHOD_UNSET_FILTER, params, &result)) != MM_ERROR_NONE) {
774                         debug_error("dbus unset filter by type failed");
775                         goto cleanup;
776                 }
777         } else {
778                 debug_error("construct param for method call failed");
779                 return MM_ERROR_SOUND_INTERNAL;
780         }
781
782         if (result) {
783                 g_variant_get(result, "(&s)", &reply);
784                 debug_log("reply : %s", reply);
785                 if (strcmp(reply, "STREAM_MANAGER_RETURN_OK"))
786                         ret = MM_ERROR_SOUND_INTERNAL;
787         } else {
788                 debug_error("reply null");
789         }
790
791 cleanup:
792         if (result)
793                 g_variant_unref(result);
794
795         debug_fleave();
796         return ret;
797 }
798
799 int mm_sound_proxy_control_filter_by_type(const char *stream_type, const char *filter_name, const char *filter_controls)
800 {
801         int ret = MM_ERROR_NONE;
802         char *reply = NULL;
803         GVariant *params = NULL, *result = NULL;
804
805         debug_fenter();
806
807         params = g_variant_new("(sss)", filter_name, filter_controls, stream_type);
808         if (params) {
809                 if ((ret = mm_sound_dbus_method_call_to(AUDIO_PROVIDER_STREAM_MANAGER, AUDIO_METHOD_CONTROL_FILTER, params, &result)) != MM_ERROR_NONE) {
810                         debug_error("dbus control filter by type failed");
811                         goto cleanup;
812                 }
813         } else {
814                 debug_error("construct param for method call failed");
815                 return MM_ERROR_SOUND_INTERNAL;
816         }
817
818         if (result) {
819                 g_variant_get(result, "(&s)", &reply);
820                 debug_log("reply : %s", reply);
821                 if (strcmp(reply, "STREAM_MANAGER_RETURN_OK"))
822                         ret = MM_ERROR_SOUND_INTERNAL;
823         } else {
824                 debug_error("reply null");
825         }
826
827 cleanup:
828         if (result)
829                 g_variant_unref(result);
830
831         debug_fleave();
832         return ret;
833 }
834
835 int mm_sound_proxy_play_tone_with_stream_info(int client_pid, int tone, char *stream_type, int stream_index, int volume, int repeat, int *codechandle)
836 {
837         int ret = MM_ERROR_NONE;
838         int handle = 0;
839         GVariant *params = NULL, *result = NULL;
840
841         debug_fenter();
842
843         if (!codechandle) {
844                 debug_error("Param for play is null");
845                 return MM_ERROR_INVALID_ARGUMENT;
846         }
847
848         params = g_variant_new("(iiiisi)", tone, repeat, volume, client_pid, stream_type, stream_index);
849         if (params) {
850                 if ((ret = mm_sound_dbus_method_call_to(AUDIO_PROVIDER_SOUND_SERVER, AUDIO_METHOD_PLAY_DTMF_WITH_STREAM_INFO, params, &result)) != MM_ERROR_NONE) {
851                         debug_error("dbus play tone failed");
852                         goto cleanup;
853                 }
854         } else {
855                 debug_error("Construct Param for method call failed");
856         }
857
858         if (result) {
859                 g_variant_get(result, "(i)",  &handle);
860                 debug_log("handle : %d", handle);
861                 *codechandle = handle;
862         } else {
863                 debug_error("reply null");
864         }
865
866 cleanup:
867         if (result)
868                 g_variant_unref(result);
869
870         debug_fleave();
871         return ret;
872 }
873
874 int mm_sound_proxy_play_sound_with_stream_info(const char* filename, int repeat, int volume,
875                                 int client_pid, int *codechandle, char *stream_type, int stream_index)
876 {
877         int ret = MM_ERROR_NONE;
878         int handle = 0;
879         GVariant *params = NULL, *result = NULL;
880
881         if (!filename || !codechandle) {
882                 debug_error("Param for play is null");
883                 return MM_ERROR_INVALID_ARGUMENT;
884         }
885
886         debug_fenter();
887
888         params = g_variant_new("(siiisi)", filename, repeat, volume, client_pid, stream_type, stream_index);
889         if (params) {
890                 if ((ret = mm_sound_dbus_method_call_to(AUDIO_PROVIDER_SOUND_SERVER, AUDIO_METHOD_PLAY_FILE_START_WITH_STREAM_INFO, params, &result)) != MM_ERROR_NONE) {
891                         debug_error("dbus play file failed");
892                         goto cleanup;
893                 }
894         } else {
895                 debug_error("Construct Param for method call failed");
896         }
897
898         if (result) {
899                 g_variant_get(result, "(i)",  &handle);
900                 debug_log("handle : %d", handle);
901                 *codechandle = handle;
902         } else {
903                 debug_error("reply null");
904         }
905
906 cleanup:
907         if (result)
908                 g_variant_unref(result);
909
910         debug_fleave();
911         return ret;
912
913
914 }
915
916 int mm_sound_proxy_stop_sound(int handle)
917 {
918         int ret = MM_ERROR_NONE;
919         GVariant *result = NULL;
920
921         debug_fenter();
922
923         if ((ret = mm_sound_dbus_method_call_to(AUDIO_PROVIDER_SOUND_SERVER, AUDIO_METHOD_PLAY_FILE_STOP, g_variant_new("(i)", handle), &result)) != MM_ERROR_NONE) {
924                 debug_error("dbus stop file playing failed");
925                 goto cleanup;
926         }
927
928 cleanup:
929         if (result)
930                 g_variant_unref(result);
931
932         debug_fleave();
933         return ret;
934 }
935
936 int mm_sound_proxy_add_play_sound_end_callback(mm_sound_stop_callback_wrapper_func func, void* userdata, mm_sound_proxy_userdata_free freefunc, unsigned *subs_id)
937 {
938         int ret = MM_ERROR_NONE;
939         struct callback_data *cb_data;
940
941         debug_fenter();
942
943         CB_DATA_NEW(cb_data, func, userdata, freefunc);
944
945         if ((ret = mm_sound_dbus_signal_subscribe_to(AUDIO_PROVIDER_SOUND_SERVER, AUDIO_EVENT_PLAY_FILE_END, dbus_callback, cb_data, simple_callback_data_free_func, &cb_data->subs_id)) != MM_ERROR_NONE)
946                 debug_error("add play sound end callback failed");
947         else
948                 *subs_id = cb_data->subs_id;
949
950         debug_fleave();
951
952         return ret;
953 }
954
955 int mm_sound_proxy_remove_play_sound_end_callback(unsigned subs_id)
956 {
957         int ret = MM_ERROR_NONE;
958         debug_fenter();
959
960         if ((ret = mm_sound_dbus_signal_unsubscribe(subs_id)) != MM_ERROR_NONE)
961                 debug_error("Remove Play File End callback failed");
962
963         debug_fleave();
964         return ret;
965 }
966
967 int mm_sound_proxy_register_focus(int index, const char *stream_type, int *id)
968 {
969         int ret = MM_ERROR_NONE;
970         int pid = g_focus_sound_handle[index].focus_pid;
971         int client_fd = 0;
972
973 #ifndef TIZEN_TV
974         debug_fenter();
975 #endif
976         ret = mm_sound_focus_socket_register(pid, stream_type, &client_fd, id);
977         if (ret) {
978                 debug_error("failed to mm_sound_focus_socket_register(), ret[0x%x]", ret);
979         } else {
980                 g_focus_sound_handle[index].client_fd = client_fd;
981                 g_focus_sound_handle[index].handle = *id;
982         }
983
984         debug_fleave();
985
986         return ret;
987 }
988
989 int mm_sound_proxy_unregister_focus(int index)
990 {
991         int ret = MM_ERROR_NONE;
992         int pid = g_focus_sound_handle[index].focus_pid;
993         int client_fd = g_focus_sound_handle[index].client_fd;
994         int id = g_focus_sound_handle[index].handle;
995
996         debug_fenter();
997
998         ret = mm_sound_focus_socket_unregister(pid, client_fd, id);
999         if (ret)
1000                 debug_error("failed to mm_sound_focus_socket_unregister(), client_fd[%d], id[%d], ret[0x%x]", client_fd, id, ret);
1001
1002         debug_fleave();
1003
1004         return ret;
1005 }
1006
1007 int mm_sound_proxy_set_focus_reacquisition(int index, bool reacquisition)
1008 {
1009         int ret = MM_ERROR_NONE;
1010         int pid = g_focus_sound_handle[index].focus_pid;
1011         int client_fd = g_focus_sound_handle[index].client_fd;
1012         int id = g_focus_sound_handle[index].handle;
1013
1014         debug_fenter();
1015
1016         ret = mm_sound_focus_socket_set_reacquisition(pid, client_fd, id, reacquisition);
1017         if (ret)
1018                 debug_error("failed to mm_sound_focus_socket_set_reacquisition(), ret[0x%x]", ret);
1019
1020         debug_fleave();
1021         return ret;
1022 }
1023
1024 int mm_sound_proxy_get_acquired_focus_stream_type(int focus_type, char **stream_type, int *option, char **ext_info)
1025 {
1026         int ret = MM_ERROR_NONE;
1027
1028         debug_fenter();
1029
1030         ret = mm_sound_focus_socket_get_acquired_focus_stream_type(focus_type, stream_type, option, ext_info);
1031         if (ret)
1032                 debug_error("failed to mm_sound_focus_socket_get_acquired_focus_stream_type(), ret[0x%x]", ret);
1033
1034         debug_fleave();
1035         return ret;
1036 }
1037
1038 int mm_sound_proxy_acquire_focus(int index, mm_sound_focus_type_e type, int option, const char *ext_info)
1039 {
1040         int ret = MM_ERROR_NONE;
1041         bool is_in_focus_cb_thread = false;
1042         int pid = g_focus_sound_handle[index].focus_pid;
1043         int client_fd = g_focus_sound_handle[index].client_fd;
1044         int id = g_focus_sound_handle[index].handle;
1045
1046         debug_fenter();
1047
1048         mm_sound_client_is_focus_cb_thread(g_thread_self(), &is_in_focus_cb_thread);
1049         if (!is_in_focus_cb_thread) {
1050                 if ((ret = mm_sound_focus_socket_acquire(pid, client_fd, id,
1051                                                                                                 type, option, ext_info ? ext_info : "", true)))
1052                         debug_error("failed to mm_sound_focus_socket_acquire(), ret[0x%x]", ret);
1053         } else {
1054                 GVariant *params = NULL, *result = NULL;
1055
1056                 params = g_variant_new("(iiiis)", pid, id, type, option, ext_info ? ext_info : "");
1057                 if (params) {
1058                         if ((ret = mm_sound_dbus_method_call_to(AUDIO_PROVIDER_FOCUS_SERVER, AUDIO_METHOD_ACQUIRE_FOCUS, params, &result)) != MM_ERROR_NONE)
1059                                 debug_error("dbus acquire focus failed");
1060                 } else {
1061                         debug_error("Construct Param for method call failed");
1062                 }
1063
1064                 if (ret != MM_ERROR_NONE)
1065                         g_variant_get(result, "(i)",  &ret);
1066                 if (result)
1067                         g_variant_unref(result);
1068         }
1069
1070         debug_fleave();
1071         return ret;
1072 }
1073
1074 int mm_sound_proxy_release_focus(int index, mm_sound_focus_type_e type, int option, const char *ext_info)
1075 {
1076         int ret = MM_ERROR_NONE;
1077         bool is_in_focus_cb_thread = false;
1078         int pid = g_focus_sound_handle[index].focus_pid;
1079         int client_fd = g_focus_sound_handle[index].client_fd;
1080         int id = g_focus_sound_handle[index].handle;
1081
1082         debug_fenter();
1083
1084         mm_sound_client_is_focus_cb_thread(g_thread_self(), &is_in_focus_cb_thread);
1085         if (!is_in_focus_cb_thread) {
1086                 if ((ret = mm_sound_focus_socket_release(pid, client_fd, id, type, option, ext_info ? ext_info : "", true)))
1087                         debug_error("failed to mm_sound_focus_socket_release(), ret[0x%x]", ret);
1088         } else {
1089                 GVariant *params = NULL, *result = NULL;
1090
1091                 params = g_variant_new("(iiiis)", pid, id, type, option, ext_info ? ext_info : "");
1092                 if (params) {
1093                         if ((ret = mm_sound_dbus_method_call_to(AUDIO_PROVIDER_FOCUS_SERVER, AUDIO_METHOD_RELEASE_FOCUS, params, &result)) != MM_ERROR_NONE)
1094                                 debug_error("dbus release focus failed");
1095                 } else {
1096                         debug_error("Construct Param for method call failed");
1097                 }
1098
1099                 if (ret != MM_ERROR_NONE)
1100                         g_variant_get(result, "(i)",  &ret);
1101                 if (result)
1102                         g_variant_unref(result);
1103         }
1104
1105         debug_fleave();
1106         return ret;
1107 }
1108
1109 int mm_sound_proxy_update_stream_focus_status(int focus_id, unsigned int status)
1110 {
1111         int ret = MM_ERROR_NONE;
1112         char *reply = NULL;
1113         GVariant *params = NULL, *result = NULL;
1114
1115         debug_fenter();
1116
1117         params = g_variant_new("(iu)", focus_id, status);
1118         if (params) {
1119                 if ((ret = mm_sound_dbus_method_call_to(AUDIO_PROVIDER_STREAM_MANAGER, AUDIO_METHOD_UPDATE_STREAM_FOCUS_STATUS, params, &result)) != MM_ERROR_NONE) {
1120                         debug_error("dbus set volume by type failed");
1121                         if (result) {
1122                                 g_variant_get(result, "(&s)",  &reply);
1123                                 debug_log("reply : %s", reply);
1124                                 if (strcmp(reply, "STREAM_MANAGER_RETURN_OK"))
1125                                         ret = MM_ERROR_SOUND_INTERNAL;
1126                         }
1127                 }
1128         } else {
1129                 debug_error("Construct Param for method call failed");
1130                 return MM_ERROR_SOUND_INTERNAL;
1131         }
1132
1133         if (result)
1134                 g_variant_unref(result);
1135
1136         debug_fleave();
1137         return ret;
1138 }
1139
1140 int mm_sound_proxy_deliver_focus(int src_index, int dst_index, mm_sound_focus_type_e focus_type)
1141 {
1142         int ret = MM_ERROR_NONE;
1143         int pid = g_focus_sound_handle[src_index].focus_pid;
1144         int src_client_fd = g_focus_sound_handle[src_index].client_fd;
1145         int src_server_fd = g_focus_sound_handle[src_index].handle;
1146         int dst_client_fd = g_focus_sound_handle[dst_index].client_fd;
1147         int dst_server_fd = g_focus_sound_handle[dst_index].handle;
1148
1149         debug_fenter();
1150
1151         ret = mm_sound_focus_socket_deliver(pid, src_client_fd, src_server_fd, dst_client_fd, dst_server_fd, focus_type);
1152         if (ret)
1153                 debug_error("failed to mm_sound_focus_socket_deliver(), ret[0x%x]", ret);
1154
1155         debug_fleave();
1156         return ret;
1157 }
1158
1159 int mm_sound_proxy_add_focus_watch_callback(int index, mm_sound_focus_type_e type)
1160 {
1161         int ret = MM_ERROR_NONE;
1162         int pid = g_focus_sound_handle[index].focus_pid;
1163         int id = 0;
1164         int client_fd = 0;
1165
1166         debug_fenter();
1167
1168         ret = mm_sound_focus_socket_add_watch_cb(pid, type, &client_fd, &id);
1169         if (ret) {
1170                 debug_error("failed to mm_sound_focus_socket_add_watch_cb(), ret[0x%x]", ret);
1171         } else {
1172                 g_focus_sound_handle[index].handle = id;
1173                 g_focus_sound_handle[index].client_fd = client_fd;
1174         }
1175
1176         debug_fleave();
1177
1178         return ret;
1179 }
1180
1181 int mm_sound_proxy_remove_focus_watch_callback(int index)
1182 {
1183         int ret = MM_ERROR_NONE;
1184         int pid = g_focus_sound_handle[index].focus_pid;
1185         int client_fd = g_focus_sound_handle[index].client_fd;
1186         int id = g_focus_sound_handle[index].handle;
1187
1188         debug_fenter();
1189
1190         ret = mm_sound_focus_socket_remove_watch_cb(pid, client_fd, id);
1191         if (ret)
1192                 debug_error("failed to mm_sound_focus_socket_remove_watch_cb(), ret[0x%x]", ret);
1193
1194         debug_fleave();
1195
1196         return ret;
1197 }
1198
1199 int mm_sound_proxy_initialize(void)
1200 {
1201         int ret = MM_ERROR_NONE;
1202
1203         debug_fenter();
1204         debug_fleave();
1205
1206         return ret;
1207 }
1208
1209 int mm_sound_proxy_finalize(void)
1210 {
1211         int ret = MM_ERROR_NONE;
1212
1213         debug_fenter();
1214         debug_fleave();
1215
1216         return ret;
1217 }