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