Remove useless parameters(route_info, priority) regarding audio path routing since...
[platform/core/multimedia/libmm-sound.git] / mm_sound.c
1 /*
2  * libmm-sound
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Seungbae Shin <seungbae.shin@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdlib.h>
23 #include <memory.h>
24 #include <unistd.h>
25 #include <pthread.h>
26
27 #include <errno.h>
28
29 #include <vconf.h>
30 #include <mm_types.h>
31 #include <mm_error.h>
32 #include <mm_session_private.h>
33 #include <mm_debug.h>
34 #include "include/mm_sound_private.h"
35 #include "include/mm_sound_utils.h"
36 #include "include/mm_sound_client.h"
37 #include "include/mm_sound_pa_client.h"
38 #include "include/mm_sound_common.h"
39
40
41 #define VOLUME_MAX_MULTIMEDIA   16
42 #define VOLUME_MAX_BASIC                8
43 #define VOLUME_MAX_SINGLE               1
44
45
46 #define MASTER_VOLUME_MAX 100
47 #define MASTER_VOLUME_MIN 0
48
49 #include <gio/gio.h>
50
51 static GList *g_subscribe_cb_list = NULL;
52 static pthread_mutex_t g_subscribe_cb_list_mutex = PTHREAD_MUTEX_INITIALIZER;
53
54 #define MM_SOUND_DBUS_BUS_NAME_PREPIX  "org.tizen.MMSound"
55 #define MM_SOUND_DBUS_OBJECT_PATH  "/org/tizen/MMSound"
56 #define MM_SOUND_DBUS_INTERFACE    "org.tizen.mmsound"
57
58 GDBusConnection *g_dbus_conn_mmsound;
59
60 int g_dbus_signal_values[MM_SOUND_SIGNAL_MAX] = {0,};
61
62 const char* dbus_signal_name_str[] = {
63         "ReleaseInternalFocus",
64 };
65
66 typedef struct _subscribe_cb {
67         mm_sound_signal_name_t signal_type;
68         mm_sound_signal_callback callback;
69         void *user_data;
70         unsigned int id;
71         int client_pid;
72 } subscribe_cb_t;
73
74 static const char* _get_volume_str (volume_type_t type)
75 {
76         static const char *volume_type_str[VOLUME_TYPE_MAX] =
77                 { "SYSTEM", "NOTIFICATION", "ALARM", "RINGTONE", "MEDIA", "CALL", "VOIP", "VOICE", "FIXED"};
78
79         return (type >= VOLUME_TYPE_SYSTEM && type < VOLUME_TYPE_MAX)? volume_type_str[type] : "Unknown";
80 }
81
82 static int _validate_volume(volume_type_t type, int value)
83 {
84         if (value < 0)
85                 return -1;
86
87         switch (type)
88         {
89         case VOLUME_TYPE_CALL:
90         case VOLUME_TYPE_VOIP:
91                 if (value >= VOLUME_MAX_BASIC) {
92                         return -1;
93                 }
94                 break;
95         case VOLUME_TYPE_SYSTEM:
96         case VOLUME_TYPE_MEDIA:
97         case VOLUME_TYPE_ALARM:
98         case VOLUME_TYPE_NOTIFICATION:
99         case VOLUME_TYPE_RINGTONE:
100         case VOLUME_TYPE_VOICE:
101                 if (value >= VOLUME_MAX_MULTIMEDIA) {
102                         return -1;
103                 }
104                 break;
105         default:
106                 return -1;
107                 break;
108         }
109         return 0;
110 }
111
112 EXPORT_API
113 int mm_sound_volume_remove_callback(volume_type_t type)
114 {
115         /* FIXME : Will be removed */
116         return MM_ERROR_NOT_SUPPORT_API;
117 }
118
119 EXPORT_API
120 int mm_sound_add_volume_changed_callback(mm_sound_volume_changed_cb func, void* user_data, unsigned int *subs_id)
121 {
122         int ret = MM_ERROR_NONE;
123
124         if (func == NULL || subs_id == NULL) {
125                 debug_error("argument is not valid\n");
126                 return MM_ERROR_INVALID_ARGUMENT;
127         }
128
129         ret = mm_sound_client_add_volume_changed_callback(func, user_data, subs_id);
130         if (ret < 0) {
131                 debug_error("Can not add volume changed callback, ret = %x\n", ret);
132         }
133
134         return ret;
135 }
136
137 EXPORT_API
138 int mm_sound_remove_volume_changed_callback(unsigned int subs_id)
139 {
140         int ret = MM_ERROR_NONE;
141
142         ret = mm_sound_client_remove_volume_changed_callback(subs_id);
143         if (ret < 0) {
144                 debug_error("Can not remove volume changed callback, ret = %x\n", ret);
145         }
146
147         return ret;
148 }
149
150 EXPORT_API
151 int mm_sound_volume_set_value(volume_type_t volume_type, const unsigned int volume_level)
152 {
153         int ret = MM_ERROR_NONE;
154
155         debug_msg("type = (%d)%s, value = %d", volume_type, _get_volume_str(volume_type), volume_level);
156
157         /* Check input param */
158         if (0 > _validate_volume(volume_type, (int)volume_level)) {
159                 debug_error("invalid volume type %d, value %u\n", volume_type, volume_level);
160                 return MM_ERROR_INVALID_ARGUMENT;
161         }
162
163         ret = mm_sound_util_volume_set_value_by_type(volume_type, volume_level);
164         if (ret == MM_ERROR_NONE) {
165                 /* update shared memory value */
166                 if(MM_ERROR_NONE != mm_sound_client_set_volume_by_type(volume_type, volume_level)) {
167                         debug_error("Can not set volume to shared memory 0x%x\n", ret);
168                 }
169         }
170
171         return ret;
172 }
173
174 EXPORT_API
175 int mm_sound_volume_get_value(volume_type_t type, unsigned int *value)
176 {
177         int ret = MM_ERROR_NONE;
178
179         /* Check input param */
180         if (value == NULL) {
181                 debug_error("invalid argument\n");
182                 return MM_ERROR_INVALID_ARGUMENT;
183         }
184         if (type < 0 || type >= VOLUME_TYPE_MAX) {
185                 debug_error("invalid volume type value %d\n", type);
186                 return MM_ERROR_INVALID_ARGUMENT;
187         }
188
189         ret = mm_sound_util_volume_get_value_by_type(type, value);
190
191         debug_msg("returned %s = %d", _get_volume_str(type), *value);
192         return ret;
193 }
194
195 EXPORT_API
196 int mm_sound_volume_primary_type_set(volume_type_t type)
197 {
198         int ret = MM_ERROR_NONE;
199
200         /* Check input param */
201         if(type < VOLUME_TYPE_UNKNOWN || type >= VOLUME_TYPE_MAX) {
202                 debug_error("invalid argument\n");
203                 return MM_ERROR_INVALID_ARGUMENT;
204         }
205
206         if (vconf_set_int(VCONFKEY_SOUND_PRIMARY_VOLUME_TYPE, type)) {
207                 debug_error("could not set vconf for RIMARY_VOLUME_TYPE\n");
208                 ret = MM_ERROR_SOUND_INTERNAL;
209         } else {
210                 debug_msg("set primary volume type forcibly %d(%s)", type, _get_volume_str(type));
211         }
212
213         return ret;
214 }
215
216 EXPORT_API
217 int mm_sound_volume_primary_type_get(volume_type_t *type)
218 {
219         int ret = MM_ERROR_NONE;
220         int voltype = VOLUME_TYPE_RINGTONE;
221
222         /* Check input param */
223         if(type == NULL) {
224                 debug_error("invalid argument\n");
225                 return MM_ERROR_INVALID_ARGUMENT;
226         }
227
228         /* check force set */
229         if (vconf_get_int(VCONFKEY_SOUND_PRIMARY_VOLUME_TYPE, &voltype)) {
230                 debug_error("could not get vconf for PRIMARY_VOLUME_TYPE\n");
231                 ret = MM_ERROR_SOUND_INTERNAL;
232         } else {
233                 debug_msg("get primary volume type %d(%s)", voltype, _get_volume_str(voltype));
234                 *type = voltype;
235         }
236
237         return ret;
238 }
239
240 ///////////////////////////////////
241 ////     MMSOUND PLAY APIs
242 ///////////////////////////////////
243 static inline void _mm_sound_fill_play_param(MMSoundPlayParam *param, const char *filename, int volume_config, mm_sound_stop_callback_func callback, void *data)
244 {
245         param->filename = filename;
246         param->volume = 0; //volume value dose not effect anymore
247         param->callback = callback;
248         param->data = data;
249         param->loop = 1;
250         param->volume_config = volume_config;
251 }
252
253 EXPORT_API
254 int mm_sound_play_loud_solo_sound(const char *filename, int volume_config, mm_sound_stop_callback_func callback, void *data, int *handle)
255 {
256         MMSoundPlayParam param = { 0, };
257
258     /* FIXME : this function will be deleted */
259         _mm_sound_fill_play_param(&param, filename, volume_config, callback, data);
260         return mm_sound_play_sound_ex(&param, handle);
261 }
262
263 EXPORT_API
264 int mm_sound_play_sound(const char *filename, int volume_config, mm_sound_stop_callback_func callback, void *data, int *handle)
265 {
266         MMSoundPlayParam param = { 0, };
267
268         _mm_sound_fill_play_param(&param, filename, volume_config, callback, data);
269         return mm_sound_play_sound_ex(&param, handle);
270 }
271
272 EXPORT_API
273 int mm_sound_play_sound_ex(MMSoundPlayParam *param, int *handle)
274 {
275         int err;
276         int lhandle = -1;
277         int volume_type = 0;
278         /* Check input param */
279         if (param == NULL) {
280                 debug_error("param is null\n");
281                 return MM_ERROR_INVALID_ARGUMENT;
282         }
283
284         volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(param->volume_config);
285
286         if (param->filename == NULL) {
287                 debug_error("filename is NULL\n");
288                 return MM_ERROR_SOUND_FILE_NOT_FOUND;
289         }
290         if (volume_type < 0 || volume_type >= VOLUME_TYPE_MAX) {
291                 debug_error("Volume type is invalid %d\n", volume_type);
292                 return MM_ERROR_INVALID_ARGUMENT;
293         }
294
295         /* Play sound */
296         err = mm_sound_client_play_sound(param, 0, &lhandle);
297         if (err < 0) {
298                 debug_error("Failed to play sound\n");
299                 return err;
300         }
301
302         /* Set handle to return */
303         if (handle) {
304                 *handle = lhandle;
305         } else {
306                 debug_critical("The sound hadle cannot be get [%d]\n", lhandle);
307         }
308
309         debug_warning ("success : handle=[%p]\n", handle);
310
311         return MM_ERROR_NONE;
312 }
313
314 EXPORT_API
315 int mm_sound_play_sound_with_stream_info(const char *filename, char *stream_type, int stream_id, unsigned int loop, mm_sound_stop_callback_func callback, void *data, int *handle)
316 {
317         MMSoundPlayParam param = { 0, };
318         int err;
319
320         param.filename = filename;
321         param.volume = 0; //volume value dose not effect anymore
322         param.callback = callback;
323         param.data = data;
324
325         if (loop == 0)
326                 param.loop = -1;
327         else
328                 param.loop = loop;
329
330         err = mm_sound_client_play_sound_with_stream_info(&param, handle, stream_type, stream_id);
331         if (err < 0) {
332                 debug_error("Failed to play sound\n");
333                 return err;
334         }
335
336         debug_warning ("success : handle=[%p]\n", handle);
337
338         return MM_ERROR_NONE;
339
340 }
341
342
343 EXPORT_API
344 int mm_sound_stop_sound(int handle)
345 {
346         int err;
347
348         debug_warning ("enter : handle=[%d]\n", handle);
349         /* Stop sound */
350         err = mm_sound_client_stop_sound(handle);
351         if (err < 0) {
352                 debug_error("Fail to stop sound\n");
353                 return err;
354         }
355         debug_warning ("success : handle=[%d]\n", handle);
356
357         return MM_ERROR_NONE;
358 }
359
360 ///////////////////////////////////
361 ////     MMSOUND TONE APIs
362 ///////////////////////////////////
363 EXPORT_API
364 int mm_sound_play_tone_ex (MMSoundTone_t num, int volume_config, const double volume, const int duration, int *handle, bool enable_session)
365 {
366         int lhandle = -1;
367         int err = MM_ERROR_NONE;
368         int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(volume_config);
369
370         debug_fenter();
371
372         /* Check input param */
373         if (duration < -1) {
374                 debug_error("number is invalid %d\n", duration);
375                 return MM_ERROR_INVALID_ARGUMENT;
376         }
377         if (num < MM_SOUND_TONE_DTMF_0 || num >= MM_SOUND_TONE_NUM) {
378                 debug_error("TONE Value is invalid %d\n", num);
379                 return MM_ERROR_INVALID_ARGUMENT;
380         }
381         if (volume_type < 0 || volume_type >= VOLUME_TYPE_MAX) {
382                 debug_error("Volume type is invalid %d\n", volume_type);
383                 return MM_ERROR_INVALID_ARGUMENT;
384         }
385         if (volume < 0.0 || volume > 1.0) {
386                 debug_error("Volume Value is invalid %d\n", volume);
387                 return MM_ERROR_INVALID_ARGUMENT;
388         }
389
390         /* Play tone */
391         debug_msg("Call MMSoundClientPlayTone\n");
392         err = mm_sound_client_play_tone(num, volume_config, volume, duration, &lhandle, enable_session);
393         if (err < 0) {
394                 debug_error("Failed to play sound\n");
395                 return err;
396         }
397
398         /* Set handle to return */
399         if (handle)
400                 *handle = lhandle;
401         else
402                 debug_critical("The sound handle cannot be get [%d]\n", lhandle);
403
404         debug_fleave();
405         return MM_ERROR_NONE;
406 }
407
408 EXPORT_API
409 int mm_sound_play_tone_with_stream_info(MMSoundTone_t tone, char *stream_type, int stream_id, const double volume, const int duration, int *handle)
410 {
411
412         int err = MM_ERROR_NONE;
413
414         err = mm_sound_client_play_tone_with_stream_info(tone, stream_type, stream_id, volume, duration, handle);
415         if (err <0) {
416                 debug_error("Failed to play sound\n");
417                 return err;
418         }
419
420         return err;
421
422 }
423
424
425 EXPORT_API
426 int mm_sound_play_tone (MMSoundTone_t num, int volume_config, const double volume, const int duration, int *handle)
427 {
428         return mm_sound_play_tone_ex (num, volume_config, volume, duration, handle, true);
429 }
430
431 ///////////////////////////////////
432 ////     MMSOUND ROUTING APIs
433 ///////////////////////////////////
434
435 EXPORT_API
436 int mm_sound_test(int a, int b, int* getv)
437 {
438         int ret = MM_ERROR_NONE;
439
440         debug_log("mm_sound_test enter");
441         if (!getv) {
442                 debug_error("argu null");
443                 return MM_ERROR_INVALID_ARGUMENT;
444         }
445         ret = mm_sound_client_test(a, b, getv);
446         if (ret < 0) {
447                 debug_error("Can not mm sound test, ret = %x\n", ret);
448         }
449         debug_log("mm_sound_test leave");
450
451         return ret;
452 }
453
454 EXPORT_API
455 int mm_sound_add_test_callback(mm_sound_test_cb func, void *user_data, unsigned int *subs_id)
456 {
457         int ret = MM_ERROR_NONE;
458
459         debug_log("mm_sound_add_test_callback enter");
460         if (!func || !subs_id) {
461                 debug_error("argument is not valid\n");
462                 return MM_ERROR_INVALID_ARGUMENT;
463         }
464
465         ret = mm_sound_client_add_test_callback(func, user_data, subs_id);
466         if (ret < 0) {
467                 debug_error("Can not add test callback, ret = %x\n", ret);
468         }
469         debug_log("mm_sound_add_test_callback leave");
470
471         return ret;
472 }
473
474 EXPORT_API
475 int mm_sound_remove_test_callback(unsigned int subs_id)
476 {
477         int ret = MM_ERROR_NONE;
478
479         debug_log("mm_sound_remove_test_callback enter");
480         ret = mm_sound_client_remove_test_callback(subs_id);
481         if (ret < 0) {
482                 debug_error("Can not remove test callback, ret = %x\n", ret);
483         }
484         debug_log("mm_sound_remove_test_callback leave");
485
486         return ret;
487 }
488
489 static int _convert_signal_name_str_to_enum (const char *name_str, mm_sound_signal_name_t *name_enum) {
490         int ret = MM_ERROR_NONE;
491
492         if (!name_str || !name_enum)
493                 return MM_ERROR_INVALID_ARGUMENT;
494
495         if (!strncmp(name_str, "ReleaseInternalFocus", strlen("ReleaseInternalFocus"))) {
496                 *name_enum = MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS;
497         } else {
498                 ret = MM_ERROR_INVALID_ARGUMENT;
499                 LOGE("not supported signal name(%s), err(0x%08x)", name_str, ret);
500         }
501         return ret;
502 }
503
504 static void _dbus_signal_callback (const char *signal_name, int value, void *user_data)
505 {
506         int ret = MM_ERROR_NONE;
507         mm_sound_signal_name_t signal;
508         subscribe_cb_t *subscribe_cb = (subscribe_cb_t*)user_data;
509
510         debug_fenter();
511
512         if (!subscribe_cb)
513                 return;
514
515         ret = _convert_signal_name_str_to_enum(signal_name, &signal);
516         if (ret)
517                 return;
518
519         debug_msg("signal: name[%s], value[%d], user_data[%p], type[%d]\n",
520                         signal_name, value, user_data, subscribe_cb->signal_type);
521
522         if (subscribe_cb->signal_type == MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS) {
523                 /* Trigger the signal callback when it comes from the same process.
524                 * In this case, the second integer argument is consist of
525                 * |<-- pid (16bits) -->|<-- value (16bits) -->|,
526                 * FYI, #define PID_MAX_DEFAULT (CONFIG_BASE_SMALL ? 0x1000 : 0x8000).
527                 * In case of daemon usage, it uses the client_pid of subscribe_cb. */
528                 debug_msg ("client_pid[%d], getpid[%d], value>>16[%d], callback[%p]\n",
529                                 subscribe_cb->client_pid, getpid(), (value >> 16), subscribe_cb);
530                 if ((subscribe_cb->client_pid ? subscribe_cb->client_pid : getpid()) == (value >> 16))
531                         subscribe_cb->callback(signal, (value & 0x0000FFFF), subscribe_cb->user_data);
532         } else {
533                 debug_warning("not supported type[%d]\n", subscribe_cb->signal_type);
534         }
535
536         debug_fleave();
537
538         return;
539 }
540
541 static void signal_callback(GDBusConnection *conn,
542                                                            const gchar *sender_name,
543                                                            const gchar *object_path,
544                                                            const gchar *interface_name,
545                                                            const gchar *signal_name,
546                                                            GVariant *parameters,
547                                                            gpointer user_data)
548 {
549         int value=0;
550         const GVariantType* value_type;
551
552         debug_msg ("sender : %s, object : %s, interface : %s, signal : %s",
553                         sender_name, object_path, interface_name, signal_name);
554         if (g_variant_is_of_type(parameters, G_VARIANT_TYPE("(i)"))) {
555                 g_variant_get(parameters, "(i)",&value);
556                 debug_msg(" - value : %d\n", value);
557                 _dbus_signal_callback(signal_name, value, user_data);
558         } else  {
559                 value_type = g_variant_get_type(parameters);
560                 debug_warning("signal type is %s", value_type);
561         }
562 }
563
564 EXPORT_API
565 int mm_sound_subscribe_signal(mm_sound_signal_name_t signal, unsigned int *subscribe_id, mm_sound_signal_callback callback, void *user_data)
566 {
567         int ret = MM_ERROR_NONE;
568         GError *err = NULL;
569
570         subscribe_cb_t *subscribe_cb = NULL;
571
572         debug_fenter();
573
574         MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_subscribe_cb_list_mutex, MM_ERROR_SOUND_INTERNAL);
575
576         if (signal < 0 || signal >= MM_SOUND_SIGNAL_MAX || !subscribe_id) {
577                 debug_error ("invalid argument, signal(%d), subscribe_id(0x%p)", signal, subscribe_id);
578                 ret = MM_ERROR_INVALID_ARGUMENT;
579                 goto error;
580         }
581
582         subscribe_cb = malloc(sizeof(subscribe_cb_t));
583         if (!subscribe_cb) {
584                 ret = MM_ERROR_SOUND_INTERNAL;
585                 goto error;
586         }
587         memset(subscribe_cb, 0, sizeof(subscribe_cb_t));
588
589         g_dbus_conn_mmsound = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
590         if (!g_dbus_conn_mmsound && err) {
591                 debug_error ("g_bus_get_sync() error (%s) ", err->message);
592                 g_error_free (err);
593                 ret = MM_ERROR_SOUND_INTERNAL;
594                 goto error;
595         }
596
597         subscribe_cb->signal_type = signal;
598         subscribe_cb->callback = callback;
599         subscribe_cb->user_data = user_data;
600
601         *subscribe_id = g_dbus_connection_signal_subscribe(g_dbus_conn_mmsound,
602                         NULL, MM_SOUND_DBUS_INTERFACE, dbus_signal_name_str[signal], MM_SOUND_DBUS_OBJECT_PATH, NULL, 0,
603                         signal_callback, subscribe_cb, NULL);
604         if (*subscribe_id == 0) {
605                 debug_error ("g_dbus_connection_signal_subscribe() error (%d)", *subscribe_id);
606                 ret = MM_ERROR_SOUND_INTERNAL;
607                 goto sig_error;
608         }
609
610         subscribe_cb->id = *subscribe_id;
611
612         g_subscribe_cb_list = g_list_append(g_subscribe_cb_list, subscribe_cb);
613         if (g_subscribe_cb_list) {
614                 debug_log("new subscribe_cb(0x%x)[user_callback(0x%x), subscribe_id(%u)] is added\n", subscribe_cb, subscribe_cb->callback, subscribe_cb->id);
615         } else {
616                 debug_error("g_list_append failed\n");
617                 ret = MM_ERROR_SOUND_INTERNAL;
618                 goto error;
619         }
620
621         MMSOUND_LEAVE_CRITICAL_SECTION(&g_subscribe_cb_list_mutex);
622
623         debug_fleave();
624
625         return ret;
626
627 sig_error:
628         g_dbus_connection_signal_unsubscribe(g_dbus_conn_mmsound, *subscribe_id);
629         g_object_unref(g_dbus_conn_mmsound);
630
631 error:
632         if (subscribe_cb)
633                 free (subscribe_cb);
634
635         MMSOUND_LEAVE_CRITICAL_SECTION(&g_subscribe_cb_list_mutex);
636
637         return ret;
638 }
639
640 EXPORT_API
641 int mm_sound_subscribe_signal_for_daemon(mm_sound_signal_name_t signal, int client_pid, unsigned int *subscribe_id, mm_sound_signal_callback callback, void *user_data)
642 {
643         int ret = MM_ERROR_NONE;
644         GError *err = NULL;
645
646         subscribe_cb_t *subscribe_cb = NULL;
647
648         debug_fenter();
649
650         MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_subscribe_cb_list_mutex, MM_ERROR_SOUND_INTERNAL);
651
652         if (signal < 0 || signal >= MM_SOUND_SIGNAL_MAX || !client_pid || !subscribe_id) {
653                 debug_error ("invalid argument, signal(%d), client_pid(%d), subscribe_id(0x%p)", signal, client_pid, subscribe_id);
654                 ret = MM_ERROR_INVALID_ARGUMENT;
655                 goto error;
656         }
657
658         subscribe_cb = malloc(sizeof(subscribe_cb_t));
659         if (!subscribe_cb) {
660                 ret = MM_ERROR_SOUND_INTERNAL;
661                 goto error;
662         }
663         memset(subscribe_cb, 0, sizeof(subscribe_cb_t));
664
665         g_dbus_conn_mmsound = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
666         if (!g_dbus_conn_mmsound && err) {
667                 debug_error ("g_bus_get_sync() error (%s) ", err->message);
668                 g_error_free (err);
669                 ret = MM_ERROR_SOUND_INTERNAL;
670                 goto error;
671         }
672
673         subscribe_cb->signal_type = signal;
674         subscribe_cb->callback = callback;
675         subscribe_cb->user_data = user_data;
676         subscribe_cb->client_pid = client_pid;
677
678         *subscribe_id = g_dbus_connection_signal_subscribe(g_dbus_conn_mmsound,
679                         NULL, MM_SOUND_DBUS_INTERFACE, dbus_signal_name_str[signal], MM_SOUND_DBUS_OBJECT_PATH, NULL, 0,
680                         signal_callback, subscribe_cb, NULL);
681         if (*subscribe_id == 0) {
682                 debug_error ("g_dbus_connection_signal_subscribe() error (%d)", *subscribe_id);
683                 ret = MM_ERROR_SOUND_INTERNAL;
684                 goto sig_error;
685         }
686
687         subscribe_cb->id = *subscribe_id;
688
689         g_subscribe_cb_list = g_list_append(g_subscribe_cb_list, subscribe_cb);
690         if (g_subscribe_cb_list) {
691                 debug_log("new subscribe_cb(0x%x)[user_callback(0x%x), subscribe_id(%u)] is added\n", subscribe_cb, subscribe_cb->callback, subscribe_cb->id);
692         } else {
693                 debug_error("g_list_append failed\n");
694                 ret = MM_ERROR_SOUND_INTERNAL;
695                 goto error;
696         }
697
698         MMSOUND_LEAVE_CRITICAL_SECTION(&g_subscribe_cb_list_mutex);
699
700         debug_fleave();
701
702         return ret;
703
704 sig_error:
705         g_dbus_connection_signal_unsubscribe(g_dbus_conn_mmsound, *subscribe_id);
706         g_object_unref(g_dbus_conn_mmsound);
707
708 error:
709         if (subscribe_cb)
710                 free (subscribe_cb);
711
712         MMSOUND_LEAVE_CRITICAL_SECTION(&g_subscribe_cb_list_mutex);
713
714         return ret;
715 }
716
717 EXPORT_API
718 void mm_sound_unsubscribe_signal(unsigned int subscribe_id)
719 {
720         GList *list = NULL;
721         subscribe_cb_t *subscribe_cb = NULL;
722
723         debug_fenter();
724
725         MMSOUND_ENTER_CRITICAL_SECTION(&g_subscribe_cb_list_mutex);
726
727         if (g_dbus_conn_mmsound && subscribe_id) {
728                 g_dbus_connection_signal_unsubscribe(g_dbus_conn_mmsound, subscribe_id);
729                 g_object_unref(g_dbus_conn_mmsound);
730                 for (list = g_subscribe_cb_list; list != NULL; list = list->next) {
731                         subscribe_cb = (subscribe_cb_t *)list->data;
732                         if (subscribe_cb && (subscribe_cb->id == subscribe_id)) {
733                                 g_subscribe_cb_list = g_list_remove(g_subscribe_cb_list, subscribe_cb);
734                                 debug_log("subscribe_cb(0x%x) is removed\n", subscribe_cb);
735                                 free (subscribe_cb);
736                         }
737                 }
738         }
739
740         MMSOUND_LEAVE_CRITICAL_SECTION(&g_subscribe_cb_list_mutex);
741
742         debug_fleave();
743 }
744
745 EXPORT_API
746 int mm_sound_send_signal(mm_sound_signal_name_t signal, int value)
747 {
748         int ret = MM_ERROR_NONE;
749         GError *err = NULL;
750         GDBusConnection *conn = NULL;
751         gboolean dbus_ret = TRUE;
752
753         debug_fenter();
754
755         MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_subscribe_cb_list_mutex, MM_ERROR_SOUND_INTERNAL);
756
757         if (signal < 0 || signal >= MM_SOUND_SIGNAL_MAX) {
758                 debug_error ("invalid argument, signal(%d)", signal);
759                 ret = MM_ERROR_INVALID_ARGUMENT;
760                 goto error;
761         }
762
763         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
764         if (!conn && err) {
765                 debug_error ("g_bus_get_sync() error (%s)", err->message);
766                 ret = MM_ERROR_SOUND_INTERNAL;
767                 goto error;
768         }
769
770         g_dbus_signal_values[signal] = value;
771         if (signal == MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS) {
772                 /* Trigger the signal callback when it comes from the same process.
773                 * |<-- pid (16bits) -->|<-- value (16bits) -->|,
774                 * FYI, #define PID_MAX_DEFAULT (CONFIG_BASE_SMALL ? 0x1000 : 0x8000). */
775                 value |= ((int)getpid() << 16);
776                 if ((_mm_session_util_write_information((int)getpid(), MM_SESSION_TYPE_REPLACED_BY_STREAM, 0)))
777                         debug_error ("failed to _mm_session_util_write_information for MM_SESSION_TYPE_REPLACED_BY_STREAM");
778         }
779         dbus_ret = g_dbus_connection_emit_signal (conn,
780                                 NULL, MM_SOUND_DBUS_OBJECT_PATH, MM_SOUND_DBUS_INTERFACE, dbus_signal_name_str[signal],
781                                 g_variant_new ("(i)", value),
782                                 &err);
783         if (!dbus_ret && err) {
784                 debug_error ("g_dbus_connection_emit_signal() error (%s)", err->message);
785                 ret = MM_ERROR_SOUND_INTERNAL;
786                 goto error;
787         }
788
789         dbus_ret = g_dbus_connection_flush_sync(conn, NULL, &err);
790         if (!dbus_ret && err) {
791                 debug_error ("g_dbus_connection_flush_sync() error (%s)", err->message);
792                 ret = MM_ERROR_SOUND_INTERNAL;
793                 goto error;
794         }
795
796         g_object_unref(conn);
797         debug_msg ("sending signal[%s], value[%d] success", dbus_signal_name_str[signal], value);
798
799         MMSOUND_LEAVE_CRITICAL_SECTION(&g_subscribe_cb_list_mutex);
800
801         debug_fleave();
802
803         return ret;
804
805 error:
806         if (err)
807                 g_error_free (err);
808         if (conn)
809                 g_object_unref(conn);
810
811         MMSOUND_LEAVE_CRITICAL_SECTION(&g_subscribe_cb_list_mutex);
812
813         return ret;
814 }
815
816 EXPORT_API
817 int mm_sound_get_signal_value(mm_sound_signal_name_t signal, int *value)
818 {
819         int ret = MM_ERROR_NONE;
820
821         debug_fenter();
822
823         MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_subscribe_cb_list_mutex, MM_ERROR_SOUND_INTERNAL);
824
825         *value = g_dbus_signal_values[signal];
826
827         MMSOUND_LEAVE_CRITICAL_SECTION(&g_subscribe_cb_list_mutex);
828
829         debug_fleave();
830
831         return ret;
832 }
833
834 __attribute__ ((constructor))
835 static void _mm_sound_initialize(void)
836 {
837         mm_sound_client_initialize();
838         /* Will be Fixed */
839 }
840
841 __attribute__ ((destructor))
842 static void _mm_sound_finalize(void)
843 {
844         mm_sound_client_finalize();
845 }
846
847