Use empty string instead of NULL in convert_volume_gain_type_to_string()
[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, int priority, int handle_route)
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         param->priority = priority;
252         param->handle_route = handle_route;
253 }
254
255 EXPORT_API
256 int mm_sound_play_loud_solo_sound(const char *filename, int volume_config, mm_sound_stop_callback_func callback, void *data, int *handle)
257 {
258         MMSoundPlayParam param = { 0, };
259
260     /* FIXME : this function will be deleted */
261         _mm_sound_fill_play_param(&param, filename, volume_config, callback, data, HANDLE_PRIORITY_NORMAL, MM_SOUND_HANDLE_ROUTE_USING_CURRENT);
262         return mm_sound_play_sound_ex(&param, handle);
263 }
264
265 EXPORT_API
266 int mm_sound_play_sound(const char *filename, int volume_config, mm_sound_stop_callback_func callback, void *data, int *handle)
267 {
268         MMSoundPlayParam param = { 0, };
269
270         _mm_sound_fill_play_param(&param, filename, volume_config, callback, data, HANDLE_PRIORITY_NORMAL, MM_SOUND_HANDLE_ROUTE_USING_CURRENT);
271         return mm_sound_play_sound_ex(&param, handle);
272 }
273
274 EXPORT_API
275 int mm_sound_play_sound_ex(MMSoundPlayParam *param, int *handle)
276 {
277         int err;
278         int lhandle = -1;
279         int volume_type = 0;
280         /* Check input param */
281         if (param == NULL) {
282                 debug_error("param is null\n");
283                 return MM_ERROR_INVALID_ARGUMENT;
284         }
285
286         volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(param->volume_config);
287
288         if (param->filename == NULL) {
289                 debug_error("filename is NULL\n");
290                 return MM_ERROR_SOUND_FILE_NOT_FOUND;
291         }
292         if (volume_type < 0 || volume_type >= VOLUME_TYPE_MAX) {
293                 debug_error("Volume type is invalid %d\n", volume_type);
294                 return MM_ERROR_INVALID_ARGUMENT;
295         }
296
297         debug_warning ("play sound : priority=[%d], handle_route=[%d]\n", param->priority, param->handle_route);
298
299         /* Play sound */
300         err = mm_sound_client_play_sound(param, 0, &lhandle);
301         if (err < 0) {
302                 debug_error("Failed to play sound\n");
303                 return err;
304         }
305
306         /* Set handle to return */
307         if (handle) {
308                 *handle = lhandle;
309         } else {
310                 debug_critical("The sound hadle cannot be get [%d]\n", lhandle);
311         }
312
313         debug_warning ("success : handle=[%p]\n", handle);
314
315         return MM_ERROR_NONE;
316 }
317
318 EXPORT_API
319 int mm_sound_play_sound_with_stream_info(const char *filename, char *stream_type, int stream_id, mm_sound_stop_callback_func callback, void *data, int *handle)
320 {
321         MMSoundPlayParam param = { 0, };
322         int err;
323
324         param.filename = filename;
325         param.volume = 0; //volume value dose not effect anymore
326         param.callback = callback;
327         param.data = data;
328         param.loop = 1;
329         param.priority = HANDLE_PRIORITY_NORMAL;
330         param.handle_route = MM_SOUND_HANDLE_ROUTE_USING_CURRENT;
331
332         err = mm_sound_client_play_sound_with_stream_info(&param, handle, stream_type, stream_id);
333         if (err < 0) {
334                 debug_error("Failed to play sound\n");
335                 return err;
336         }
337
338         debug_warning ("success : handle=[%p]\n", handle);
339
340         return MM_ERROR_NONE;
341
342 }
343
344
345 EXPORT_API
346 int mm_sound_stop_sound(int handle)
347 {
348         int err;
349
350         debug_warning ("enter : handle=[%d]\n", handle);
351         /* Stop sound */
352         err = mm_sound_client_stop_sound(handle);
353         if (err < 0) {
354                 debug_error("Fail to stop sound\n");
355                 return err;
356         }
357         debug_warning ("success : handle=[%d]\n", handle);
358
359         return MM_ERROR_NONE;
360 }
361
362 ///////////////////////////////////
363 ////     MMSOUND TONE APIs
364 ///////////////////////////////////
365 EXPORT_API
366 int mm_sound_play_tone_ex (MMSoundTone_t num, int volume_config, const double volume, const int duration, int *handle, bool enable_session)
367 {
368         int lhandle = -1;
369         int err = MM_ERROR_NONE;
370         int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(volume_config);
371
372         debug_fenter();
373
374         /* Check input param */
375         if (duration < -1) {
376                 debug_error("number is invalid %d\n", duration);
377                 return MM_ERROR_INVALID_ARGUMENT;
378         }
379         if (num < MM_SOUND_TONE_DTMF_0 || num >= MM_SOUND_TONE_NUM) {
380                 debug_error("TONE Value is invalid %d\n", num);
381                 return MM_ERROR_INVALID_ARGUMENT;
382         }
383         if (volume_type < 0 || volume_type >= VOLUME_TYPE_MAX) {
384                 debug_error("Volume type is invalid %d\n", volume_type);
385                 return MM_ERROR_INVALID_ARGUMENT;
386         }
387         if (volume < 0.0 || volume > 1.0) {
388                 debug_error("Volume Value is invalid %d\n", volume);
389                 return MM_ERROR_INVALID_ARGUMENT;
390         }
391
392         /* Play tone */
393         debug_msg("Call MMSoundClientPlayTone\n");
394         err = mm_sound_client_play_tone(num, volume_config, volume, duration, &lhandle, enable_session);
395         if (err < 0) {
396                 debug_error("Failed to play sound\n");
397                 return err;
398         }
399
400         /* Set handle to return */
401         if (handle)
402                 *handle = lhandle;
403         else
404                 debug_critical("The sound handle cannot be get [%d]\n", lhandle);
405
406         debug_fleave();
407         return MM_ERROR_NONE;
408 }
409
410 EXPORT_API
411 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)
412 {
413
414         int err = MM_ERROR_NONE;
415
416         err = mm_sound_client_play_tone_with_stream_info(tone, stream_type, stream_id, volume, duration, handle);
417         if (err <0) {
418                 debug_error("Failed to play sound\n");
419                 return err;
420         }
421
422         return err;
423
424 }
425
426
427 EXPORT_API
428 int mm_sound_play_tone (MMSoundTone_t num, int volume_config, const double volume, const int duration, int *handle)
429 {
430         return mm_sound_play_tone_ex (num, volume_config, volume, duration, handle, true);
431 }
432
433 ///////////////////////////////////
434 ////     MMSOUND ROUTING APIs
435 ///////////////////////////////////
436
437 EXPORT_API
438 int mm_sound_test(int a, int b, int* getv)
439 {
440         int ret = MM_ERROR_NONE;
441
442         debug_log("mm_sound_test enter");
443         if (!getv) {
444                 debug_error("argu null");
445                 return MM_ERROR_INVALID_ARGUMENT;
446         }
447         ret = mm_sound_client_test(a, b, getv);
448         if (ret < 0) {
449                 debug_error("Can not mm sound test, ret = %x\n", ret);
450         }
451         debug_log("mm_sound_test leave");
452
453         return ret;
454 }
455
456 EXPORT_API
457 int mm_sound_add_test_callback(mm_sound_test_cb func, void *user_data, unsigned int *subs_id)
458 {
459         int ret = MM_ERROR_NONE;
460
461         debug_log("mm_sound_add_test_callback enter");
462         if (!func || !subs_id) {
463                 debug_error("argument is not valid\n");
464                 return MM_ERROR_INVALID_ARGUMENT;
465         }
466
467         ret = mm_sound_client_add_test_callback(func, user_data, subs_id);
468         if (ret < 0) {
469                 debug_error("Can not add test callback, ret = %x\n", ret);
470         }
471         debug_log("mm_sound_add_test_callback leave");
472
473         return ret;
474 }
475
476 EXPORT_API
477 int mm_sound_remove_test_callback(unsigned int subs_id)
478 {
479         int ret = MM_ERROR_NONE;
480
481         debug_log("mm_sound_remove_test_callback enter");
482         ret = mm_sound_client_remove_test_callback(subs_id);
483         if (ret < 0) {
484                 debug_error("Can not remove test callback, ret = %x\n", ret);
485         }
486         debug_log("mm_sound_remove_test_callback leave");
487
488         return ret;
489 }
490
491 static int _convert_signal_name_str_to_enum (const char *name_str, mm_sound_signal_name_t *name_enum) {
492         int ret = MM_ERROR_NONE;
493
494         if (!name_str || !name_enum)
495                 return MM_ERROR_INVALID_ARGUMENT;
496
497         if (!strncmp(name_str, "ReleaseInternalFocus", strlen("ReleaseInternalFocus"))) {
498                 *name_enum = MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS;
499         } else {
500                 ret = MM_ERROR_INVALID_ARGUMENT;
501                 LOGE("not supported signal name(%s), err(0x%08x)", name_str, ret);
502         }
503         return ret;
504 }
505
506 static void _dbus_signal_callback (const char *signal_name, int value, void *user_data)
507 {
508         int ret = MM_ERROR_NONE;
509         mm_sound_signal_name_t signal;
510         subscribe_cb_t *subscribe_cb = (subscribe_cb_t*)user_data;
511
512         debug_fenter();
513
514         if (!subscribe_cb)
515                 return;
516
517         ret = _convert_signal_name_str_to_enum(signal_name, &signal);
518         if (ret)
519                 return;
520
521         debug_msg ("signal_name[%s], value[%d], user_data[0x%x]\n", signal_name, value, user_data);
522
523         if (subscribe_cb->signal_type == MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS) {
524                 /* Trigger the signal callback when it comes from the same process.
525                 * In this case, the second integer argument is consist of
526                 * |<-- pid (16bits) -->|<-- value (16bits) -->|,
527                 * FYI, #define PID_MAX_DEFAULT (CONFIG_BASE_SMALL ? 0x1000 : 0x8000).
528                 * In case of daemon usage, it uses the client_pid of subscribe_cb. */
529                 if ((subscribe_cb->client_pid ? subscribe_cb->client_pid : getpid()) == (value >> 16)) {
530                         subscribe_cb->callback(signal, (value & 0x0000FFFF), subscribe_cb->user_data);
531                 }
532         } else {
533                 subscribe_cb->callback(signal, value, subscribe_cb->user_data);
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