cda4d918f6457aaaea3a26cba93ec08c9a57ff61
[platform/core/multimedia/libmm-sound.git] / mm_sound_client.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 <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <poll.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <signal.h>
31
32 #include <pthread.h>
33 #include <semaphore.h>
34
35 #include <mm_error.h>
36 #include <mm_debug.h>
37
38 #include "include/mm_sound.h"
39 #include "include/mm_sound_client.h"
40 #include "include/mm_sound_proxy.h"
41 #include "include/mm_sound_common.h"
42 #include "include/mm_sound_device.h"
43 #include "include/mm_sound_stream.h"
44
45 #include <mm_session.h>
46 #include <mm_session_private.h>
47
48 #include <glib.h>
49 #if defined(__GSOURCE_CALLBACK__)
50 #include <sys/poll.h>
51 #endif
52
53 #define CLIENT_HANDLE_MAX 256
54
55 #define FOCUS_HANDLE_MAX 512
56 #define FOCUS_HANDLE_INIT_VAL -1
57 #define CONFIG_ENABLE_RETCB
58
59 #define VOLUME_TYPE_LEN 64
60
61 struct sigaction system_int_old_action;
62 struct sigaction system_abrt_old_action;
63 struct sigaction system_segv_old_action;
64 struct sigaction system_term_old_action;
65 struct sigaction system_sys_old_action;
66 struct sigaction system_xcpu_old_action;
67
68 struct callback_data {
69         void *user_cb;
70         void *user_data;
71         void *extra_data;
72         guint subs_id;
73 };
74
75 typedef struct _FocusSource {
76         GSource source;
77         GPollFD poll_fd;
78 } FocusSource;
79
80 #define GET_CB_DATA(_cb_data, _func, _userdata, _extradata) \
81         do { \
82                 _cb_data = (struct callback_data*) g_malloc0(sizeof(struct callback_data)); \
83                 _cb_data->user_cb = _func; \
84                 _cb_data->user_data = _userdata; \
85                 _cb_data->extra_data = _extradata; \
86         } while (0)
87
88 #ifdef USE_FOCUS
89 typedef struct {
90         int focus_tid;
91         int handle;
92         int focus_fd;
93         FocusSource *fsrc;
94         bool is_used;
95         bool auto_reacquire;
96         GMutex focus_lock;
97         GThread *focus_cb_thread;
98         GMainLoop *focus_loop;
99         mm_sound_focus_changed_cb focus_callback;
100         mm_sound_focus_changed_watch_cb watch_callback;
101         void* user_data;
102         bool unset_watch_callback_requested;
103
104         /* will be removed when the session concept is completely left out*/
105         bool is_for_session;
106         bool is_for_monitor;
107 } focus_sound_info_t;
108
109 typedef struct {
110         int pid;
111         int handle;
112         int type;
113         int state;
114         char stream_type[MAX_STREAM_TYPE_LEN];
115         char ext_info[MM_SOUND_NAME_NUM];
116         int option;
117 } focus_cb_data_lib;
118
119 typedef struct {
120         int watch_cb_id;
121         mm_sound_focus_session_interrupt_cb user_cb;
122         void* user_data;
123 } focus_session_interrupt_info_t;
124
125 typedef gboolean(*focus_callback_handler_t)(gpointer user_data);
126
127 focus_sound_info_t g_focus_sound_handle[FOCUS_HANDLE_MAX];
128 focus_session_interrupt_info_t g_focus_session_interrupt_info = {-1, NULL, NULL};
129 static pthread_mutex_t g_index_mutex = PTHREAD_MUTEX_INITIALIZER;
130 static pthread_mutex_t g_event_mutex = PTHREAD_MUTEX_INITIALIZER;
131 guint g_focus_signal_handle;
132 guint g_idle_event_src;
133 #endif
134
135 gboolean g_need_emergent_exit = FALSE;
136
137 typedef struct {
138         /* handle to watch end of playing */
139         int watching_handle;
140         /* subscription id to unsubscribe when handle ended */
141         unsigned subs_id;
142 } play_sound_end_callback_data_t;
143
144 typedef struct _focus_idle_event {
145         focus_idle_event_type_e type;
146         int data;
147 } focus_idle_event_t;
148
149 void mm_sound_client_cleanup(void)
150 {
151         int ret = MM_ERROR_NONE;
152
153         if (g_need_emergent_exit) {
154                 ret = mm_sound_proxy_emergent_exit(getpid());
155                 if (ret == MM_ERROR_NONE)
156                         debug_msg("Success to emergnet_exit");
157                 else
158                         debug_error("Error occurred : 0x%x", ret);
159         }
160 }
161
162 void _system_signal_handler(int signo, siginfo_t *siginfo, void *context)
163 {
164         sigset_t old_mask, all_mask;
165
166         debug_warning("Got signal : signo(%d)", signo);
167
168         /* signal block */
169
170         sigfillset(&all_mask);
171         sigprocmask(SIG_BLOCK, &all_mask, &old_mask);
172
173         mm_sound_client_cleanup();
174
175         sigprocmask(SIG_SETMASK, &old_mask, NULL);
176         /* signal unblock */
177
178         switch (signo) {
179         case SIGINT:
180                 if (system_int_old_action.sa_sigaction)
181                         system_int_old_action.sa_sigaction(signo, siginfo, context);
182                 else
183                         sigaction(signo, &system_int_old_action, NULL);
184                 break;
185         case SIGABRT:
186                 if (system_abrt_old_action.sa_sigaction)
187                         system_abrt_old_action.sa_sigaction(signo, siginfo, context);
188                 else
189                         sigaction(signo, &system_abrt_old_action, NULL);
190                 break;
191         case SIGSEGV:
192                 if (system_segv_old_action.sa_sigaction)
193                         system_segv_old_action.sa_sigaction(signo, siginfo, context);
194                 else
195                         sigaction(signo, &system_segv_old_action, NULL);
196                 break;
197         case SIGTERM:
198                 if (system_term_old_action.sa_sigaction)
199                         system_term_old_action.sa_sigaction(signo, siginfo, context);
200                 else
201                         sigaction(signo, &system_term_old_action, NULL);
202                 break;
203         case SIGSYS:
204                 if (system_sys_old_action.sa_sigaction)
205                         system_sys_old_action.sa_sigaction(signo, siginfo, context);
206                 else
207                         sigaction(signo, &system_sys_old_action, NULL);
208                 break;
209         case SIGXCPU:
210                 if (system_xcpu_old_action.sa_sigaction)
211                         system_xcpu_old_action.sa_sigaction(signo, siginfo, context);
212                 else
213                         sigaction(signo, &system_xcpu_old_action, NULL);
214                 break;
215         default:
216                 break;
217         }
218
219         debug_warning("signal handling end");
220 }
221
222 #ifdef TIZEN_TV
223 static bool _is_dotnet_app(void)
224 {
225         char *is_dotnet = NULL;
226
227         is_dotnet = getenv("DOTNET_APP");
228         if (is_dotnet && atoi(is_dotnet) == 1)
229                 return true;
230         else
231                 return false;
232 }
233 #endif
234
235 int mm_sound_client_initialize(void)
236 {
237         int ret = MM_ERROR_NONE;
238         struct sigaction system_action;
239
240         debug_fenter();
241
242         mm_sound_proxy_initialize();
243         g_idle_event_src = 0;
244
245 #ifdef TIZEN_TV
246         if (_is_dotnet_app()) {
247                 debug_warning("no signal handler for dotnet!!");
248                 return ret;
249         }
250 #endif
251         system_action.sa_sigaction = _system_signal_handler;
252         system_action.sa_flags = SA_NOCLDSTOP | SA_SIGINFO;
253
254         sigemptyset(&system_action.sa_mask);
255
256         sigaction(SIGINT, &system_action, &system_int_old_action);
257         sigaction(SIGABRT, &system_action, &system_abrt_old_action);
258         sigaction(SIGSEGV, &system_action, &system_segv_old_action);
259         sigaction(SIGTERM, &system_action, &system_term_old_action);
260         sigaction(SIGSYS, &system_action, &system_sys_old_action);
261         sigaction(SIGXCPU, &system_action, &system_xcpu_old_action);
262
263         debug_fleave();
264         return ret;
265 }
266
267 int mm_sound_client_finalize(void)
268 {
269         int ret = MM_ERROR_NONE;
270
271         debug_fenter();
272
273         mm_sound_client_cleanup();
274
275         ret = mm_sound_proxy_finalize();
276
277 #ifdef USE_FOCUS
278         if (g_idle_event_src > 0) {
279                 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_event_mutex, MM_ERROR_SOUND_INTERNAL);
280                 g_source_remove(g_idle_event_src);
281                 MMSOUND_LEAVE_CRITICAL_SECTION(&g_event_mutex);
282         }
283 #endif
284
285 #ifdef TIZEN_TV
286         if (_is_dotnet_app()) {
287                 debug_warning("no signal handler for dotnet!!");
288                 return ret;
289         }
290 #endif
291
292         sigaction(SIGINT, &system_int_old_action, NULL);
293         sigaction(SIGABRT, &system_abrt_old_action, NULL);
294         sigaction(SIGSEGV, &system_segv_old_action, NULL);
295         sigaction(SIGTERM, &system_term_old_action, NULL);
296         sigaction(SIGSYS, &system_sys_old_action, NULL);
297         sigaction(SIGXCPU, &system_xcpu_old_action, NULL);
298
299         debug_fleave();
300         return ret;
301 }
302
303 void mm_sound_convert_volume_type_to_stream_type(int volume_type, char *stream_type)
304 {
305         switch (volume_type) {
306         case VOLUME_TYPE_SYSTEM:
307                 MMSOUND_STRNCPY(stream_type, "system", MAX_STREAM_TYPE_LEN);
308                 break;
309         case VOLUME_TYPE_NOTIFICATION:
310                 MMSOUND_STRNCPY(stream_type, "notification", MAX_STREAM_TYPE_LEN);
311                 break;
312         case VOLUME_TYPE_ALARM:
313                 MMSOUND_STRNCPY(stream_type, "alarm", MAX_STREAM_TYPE_LEN);
314                 break;
315         case VOLUME_TYPE_RINGTONE:
316                 MMSOUND_STRNCPY(stream_type, "ringtone-voip", MAX_STREAM_TYPE_LEN);
317                 break;
318         case VOLUME_TYPE_MEDIA:
319                 MMSOUND_STRNCPY(stream_type, "media", MAX_STREAM_TYPE_LEN);
320                 break;
321         case VOLUME_TYPE_CALL:
322                 MMSOUND_STRNCPY(stream_type, "call-voice", MAX_STREAM_TYPE_LEN);
323                 break;
324         case VOLUME_TYPE_VOIP:
325                 MMSOUND_STRNCPY(stream_type, "voip", MAX_STREAM_TYPE_LEN);
326                 break;
327         case VOLUME_TYPE_VOICE:
328                 MMSOUND_STRNCPY(stream_type, "voice-information", MAX_STREAM_TYPE_LEN);
329                 break;
330         default:
331                 MMSOUND_STRNCPY(stream_type, "media", MAX_STREAM_TYPE_LEN);
332                 break;
333         }
334
335         debug_msg("volume type (%d) converted to stream type (%s)", volume_type, stream_type);
336
337 }
338
339 /*****************************************************************************************
340                             DBUS SUPPORTED FUNCTIONS
341 ******************************************************************************************/
342 #ifdef USE_FOCUS
343 void _mm_sound_client_focus_signal_callback(mm_sound_signal_name_t signal, int value, void *user_data)
344 {
345         int ret = MM_ERROR_NONE;
346
347         debug_fenter();
348         debug_msg("focus signal received, value = %d", value);
349
350         if (value == 1) {
351                 ret = mm_sound_proxy_clear_focus(getpid());
352                 if (ret)
353                         debug_error("clear focus failed ret = 0x%x", ret);
354                 mm_sound_unsubscribe_signal(g_focus_signal_handle);
355                 g_focus_signal_handle = 0;
356         }
357 }
358 #endif
359
360 int mm_sound_client_play_tone(int number, int volume_config, double volume,
361                                                         int time, int *handle, bool enable_session)
362 {
363         int ret = MM_ERROR_NONE;
364 //       int instance = -1; /* instance is unique to communicate with server : client message queue filter type */
365         int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(volume_config);
366         char stream_type[MAX_STREAM_TYPE_LEN] = {0, };
367
368          debug_fenter();
369
370         /* read session information */
371         int session_type = MM_SESSION_TYPE_MEDIA;
372         int session_options = 0;
373         int is_focus_registered = 0;
374
375         ret = mm_sound_get_signal_value(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &is_focus_registered);
376         if (ret) {
377                 debug_error("mm_sound_get_signal_value failed [0x%x]", ret);
378                 return MM_ERROR_POLICY_INTERNAL;
379         }
380
381         if (is_focus_registered)
382                 enable_session = false;
383
384         if (enable_session) {
385                 if (MM_ERROR_NONE != _mm_session_util_read_information(-1, &session_type, &session_options)) {
386                         debug_warning("Read Session Information failed. use default \"media\" type");
387                         session_type = MM_SESSION_TYPE_MEDIA;
388
389                         if (MM_ERROR_NONE != mm_session_init(session_type)) {
390                                 debug_critical("MMSessionInit() failed");
391                                 return MM_ERROR_POLICY_INTERNAL;
392                         }
393                 }
394         }
395
396          // instance = getpid();
397          //debug_log("pid for client ::: [%d]", instance);
398
399          /* Send msg */
400          debug_msg("Input number : %d", number);
401          /* Send req memory */
402
403         mm_sound_convert_volume_type_to_stream_type(volume_type, stream_type);
404         ret = mm_sound_proxy_play_tone(number, time, volume, volume_config,
405                                         session_type, session_options, getpid(), enable_session, handle, stream_type, -1);
406 #ifdef USE_FOCUS
407         if (enable_session && !g_focus_signal_handle) {
408                 ret = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &g_focus_signal_handle,
409                                                                                 _mm_sound_client_focus_signal_callback, NULL);
410                 if (ret) {
411                         debug_error("mm_sound_subscribe_signal failed [0x%x]", ret);
412                         return MM_ERROR_POLICY_INTERNAL;
413                 }
414         }
415 #endif
416
417         debug_fleave();
418         return ret;
419 }
420
421 int mm_sound_client_play_tone_with_stream_info(int tone, char *stream_type, int stream_id,
422                                                                                         double volume, int duration, int *handle)
423 {
424         int ret = MM_ERROR_NONE;
425
426         debug_fenter();
427
428         ret = mm_sound_proxy_play_tone_with_stream_info(getpid(), tone, stream_type, stream_id, volume, duration, handle);
429
430         debug_fleave();
431         return ret;
432 }
433
434 static void _mm_sound_stop_callback_wrapper_func(int ended_handle, void *userdata)
435 {
436         struct callback_data *cb_data = (struct callback_data*) userdata;
437         play_sound_end_callback_data_t *end_cb_data;
438
439         debug_log("ended_handle : %d", ended_handle);
440
441         if (cb_data == NULL) {
442                 debug_warning("stop callback data null");
443                 return;
444         }
445
446         end_cb_data = (play_sound_end_callback_data_t*) cb_data->extra_data;
447
448         if (ended_handle == end_cb_data->watching_handle) {
449                 debug_log("Interested playing handle end : %d", ended_handle);
450                 ((mm_sound_stop_callback_func)(cb_data->user_cb))(cb_data->user_data, ended_handle);
451                 if (mm_sound_proxy_remove_play_sound_end_callback(end_cb_data->subs_id) != MM_ERROR_NONE)
452                         debug_error("mm_sound_client_dbus_remove_play_file_end_callback failed");
453         } else {
454                 debug_log("Not interested playing handle : %d", ended_handle);
455         }
456 }
457
458 static void play_end_callback_data_free_func(void *data)
459 {
460         struct callback_data *cb_data = (struct callback_data*) data;
461
462         if (cb_data) {
463                 g_free(cb_data->extra_data);
464                 g_free(cb_data);
465         }
466 }
467
468 int mm_sound_client_play_sound(MMSoundPlayParam *param, int tone, int *handle)
469 {
470         int ret = MM_ERROR_NONE;
471         int session_type = MM_SESSION_TYPE_MEDIA;
472         int session_options = 0;
473         int is_focus_registered = 0;
474 //      int instance = -1;      /* instance is unique to communicate with server : client message queue filter type */
475         int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(param->volume_config);
476         char stream_type[MAX_STREAM_TYPE_LEN] = {0, };
477         struct callback_data *cb_data = NULL;
478         play_sound_end_callback_data_t *end_cb_data;
479
480         debug_fenter();
481
482         /* read session information */
483
484         ret = mm_sound_get_signal_value(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &is_focus_registered);
485         if (ret) {
486                 debug_error("mm_sound_get_signal_value failed [0x%x]", ret);
487                 return MM_ERROR_POLICY_INTERNAL;
488         }
489
490         if (is_focus_registered)
491                 param->skip_session = true;
492
493         if (param->skip_session == false) {
494                 if (MM_ERROR_NONE != _mm_session_util_read_information(-1, &session_type, &session_options)) {
495                         debug_warning("Read MMSession Type failed. use default \"media\" type");
496                         session_type = MM_SESSION_TYPE_MEDIA;
497
498                         if (MM_ERROR_NONE != mm_session_init(session_type)) {
499                                 debug_critical("MMSessionInit() failed");
500                                 return MM_ERROR_POLICY_INTERNAL;
501                         }
502                 }
503         }
504
505         /* Send msg */
506         if ((param->mem_ptr && param->mem_size)) {
507                 // Play memory, deprecated
508                 return MM_ERROR_INVALID_ARGUMENT;
509         }
510
511         mm_sound_convert_volume_type_to_stream_type(volume_type, stream_type);
512         ret = mm_sound_proxy_play_sound(param->filename, tone, param->loop, param->volume, param->volume_config,
513                                          session_type, session_options, getpid(), param->skip_session, handle, stream_type, -1);
514         if (ret != MM_ERROR_NONE) {
515                 debug_error("Play Sound Failed");
516                 goto failed;
517         }
518         if (param->callback) {
519                 end_cb_data = (play_sound_end_callback_data_t *) g_malloc0(sizeof(play_sound_end_callback_data_t));
520                 end_cb_data->watching_handle = *handle;
521                 GET_CB_DATA(cb_data, param->callback, param->data, end_cb_data);
522
523                 ret = mm_sound_proxy_add_play_sound_end_callback(_mm_sound_stop_callback_wrapper_func, cb_data,
524                                                                                                                 play_end_callback_data_free_func, &end_cb_data->subs_id);
525                 if (ret != MM_ERROR_NONE)
526                         debug_error("Add callback for play sound(%d) Failed", *handle);
527         }
528 #ifdef USE_FOCUS
529         if (!param->skip_session && !g_focus_signal_handle) {
530                 ret = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &g_focus_signal_handle,
531                                                                                 _mm_sound_client_focus_signal_callback, NULL);
532                 if (ret) {
533                         debug_error("mm_sound_subscribe_signal failed [0x%x]", ret);
534                         return MM_ERROR_POLICY_INTERNAL;
535                 }
536         }
537 #endif
538
539 failed:
540
541         debug_fleave();
542         return ret;
543 }
544
545 int mm_sound_client_play_sound_with_stream_info(MMSoundPlayParam *param, int *handle, char* stream_type, int stream_id)
546 {
547         int ret = MM_ERROR_NONE;
548         struct callback_data *cb_data = NULL;
549         play_sound_end_callback_data_t *end_cb_data;
550
551         ret = mm_sound_proxy_play_sound_with_stream_info(param->filename, param->loop, param->volume,
552                                                                                                         getpid(), handle, stream_type, stream_id);
553         if (ret != MM_ERROR_NONE) {
554                 debug_error("Play Sound Failed");
555                 goto failed;
556         }
557         if (param->callback) {
558                 end_cb_data = (play_sound_end_callback_data_t *) g_malloc0(sizeof(play_sound_end_callback_data_t));
559                 end_cb_data->watching_handle = *handle;
560                 GET_CB_DATA(cb_data, param->callback, param->data, end_cb_data);
561
562                 ret = mm_sound_proxy_add_play_sound_end_callback(_mm_sound_stop_callback_wrapper_func, cb_data,
563                                                                                                                 play_end_callback_data_free_func, &end_cb_data->subs_id);
564                 if (ret != MM_ERROR_NONE)
565                         debug_error("Add callback for play sound(%d) Failed", *handle);
566         }
567
568 failed:
569
570         debug_fleave();
571         return ret;
572
573 }
574
575 int mm_sound_client_stop_sound(int handle)
576 {
577         int ret = MM_ERROR_NONE;
578         debug_fenter();
579
580         if (handle < 0 || handle > CLIENT_HANDLE_MAX) {
581                 ret = MM_ERROR_INVALID_ARGUMENT;
582                 return ret;
583         }
584
585         ret = mm_sound_proxy_stop_sound(handle);
586
587         debug_fleave();
588         return ret;
589 }
590
591 static int _mm_sound_client_device_list_dump(GList *device_list)
592 {
593         int ret = MM_ERROR_NONE;
594         GList *list = NULL;
595         mm_sound_device_t *device_node = NULL;
596         int count = 0;
597         if (!device_list) {
598                 debug_error("Device list NULL, cannot dump list");
599                 return MM_ERROR_SOUND_INTERNAL;
600         }
601
602         debug_log("======================== device list : start ==========================");
603         for (list = device_list; list != NULL; list = list->next) {
604                 device_node = (mm_sound_device_t *)list->data;
605                 if (device_node) {
606                         debug_log(" list idx[%d]: type[%17s], id[%02d], io_direction[%d], state[%d], name[%s]",
607                                                 count++, device_node->type, device_node->id, device_node->io_direction,
608                                                 device_node->state, device_node->name);
609                 }
610         }
611         debug_log("======================== device list : end ============================");
612
613         return ret;
614 }
615
616 int mm_sound_client_get_current_connected_device_list(int device_flags, mm_sound_device_list_t *device_list)
617 {
618         int ret = MM_ERROR_NONE;
619         debug_fenter();
620
621         if (!device_list) {
622                 debug_error("Device list NULL");
623                 ret = MM_ERROR_COMMON_INVALID_ARGUMENT;
624                 goto failed;
625         }
626
627         if ((ret = mm_sound_proxy_get_current_connected_device_list(device_flags, &device_list->list)) != MM_ERROR_NONE) {
628                 debug_error("failed to get current connected device list with dbus, ret[0x%x]", ret);
629                 goto failed;
630         }
631         if (!device_list->list) {
632                 debug_error("Got device list null");
633                 ret = MM_ERROR_SOUND_NO_DATA;
634                 goto failed;
635         }
636         _mm_sound_client_device_list_dump(device_list->list);
637
638 failed:
639         debug_fleave();
640         return ret;
641 }
642
643 int mm_sound_client_get_device_by_id(int device_id, mm_sound_device_t **device)
644 {
645         int ret = MM_ERROR_NONE;
646
647         debug_fenter();
648
649         if ((ret = mm_sound_proxy_get_device_by_id(device_id, device)) != MM_ERROR_NONE)
650                 debug_error("failed to get device by id");
651
652         debug_fleave();
653
654         return ret;
655 }
656
657 static bool device_is_match_direction(int direction, int mask)
658 {
659         if (mask == DEVICE_IO_DIRECTION_FLAGS || mask == 0)
660                 return true;
661
662         if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_IN_FLAG) && (direction & MM_SOUND_DEVICE_IO_DIRECTION_IN))
663                 return true;
664         if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_OUT_FLAG) && (direction & MM_SOUND_DEVICE_IO_DIRECTION_OUT))
665                 return true;
666         if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_BOTH_FLAG) && (direction == MM_SOUND_DEVICE_IO_DIRECTION_BOTH))
667                 return true;
668
669         return false;
670 }
671
672 static bool device_is_match_state(int state, int mask)
673 {
674         if (mask == DEVICE_STATE_FLAGS || mask == 0)
675                 return true;
676
677         if ((mask & MM_SOUND_DEVICE_STATE_DEACTIVATED_FLAG) && (state == MM_SOUND_DEVICE_STATE_DEACTIVATED))
678                 return true;
679         if ((mask & MM_SOUND_DEVICE_STATE_ACTIVATED_FLAG) && (state == MM_SOUND_DEVICE_STATE_ACTIVATED))
680                 return true;
681
682         return false;
683 }
684
685 static bool device_is_match_type(const char *type, int mask)
686 {
687         bool is_builtin;
688         const char *builtin_prefix = "builtin";
689
690         if (mask == DEVICE_TYPE_FLAGS || mask == 0)
691                 return true;
692
693         is_builtin = !strncmp(type, builtin_prefix, strlen(builtin_prefix));
694
695         if ((mask & MM_SOUND_DEVICE_TYPE_INTERNAL_FLAG) && (is_builtin))
696                 return true;
697         if ((mask & MM_SOUND_DEVICE_TYPE_EXTERNAL_FLAG) && (!is_builtin))
698                 return true;
699
700         return false;
701 }
702
703 static bool device_is_match_with_mask(const char *type, int direction, int state, int mask)
704 {
705         if (mask == DEVICE_ALL_FLAG)
706                 return true;
707
708         return (device_is_match_direction(direction, mask & DEVICE_IO_DIRECTION_FLAGS) &&
709                         device_is_match_state(state, mask & DEVICE_STATE_FLAGS) &&
710                         device_is_match_type(type, mask & DEVICE_TYPE_FLAGS));
711 }
712
713 static int _fill_sound_device(mm_sound_device_t *device_h, int device_id, const char *device_type,
714                 int direction, int state, const char *name, int vendor_id, int product_id,
715                 int *stream_id, int stream_num, bool is_running)
716 {
717         int i;
718
719         if (stream_num > 0 && stream_id == NULL) {
720                 debug_error("stream_num is %d, but stream_id is NULL", stream_num);
721                 return -1;
722         }
723
724         if (stream_num > MAX_STREAM_ON_DEVICE) {
725                 debug_error("too many streams on this device");
726                 return -1;
727         }
728
729         device_h->id = device_id;
730         device_h->io_direction = direction;
731         device_h->state = state;
732         MMSOUND_STRNCPY(device_h->name, name, MAX_DEVICE_NAME_NUM);
733         MMSOUND_STRNCPY(device_h->type, device_type, MAX_DEVICE_TYPE_STR_LEN);
734         device_h->vendor_id = vendor_id;
735         device_h->product_id = product_id;
736         device_h->is_running = is_running;
737
738         if (stream_num > 0) {
739                 device_h->stream_num = stream_num;
740                 debug_log("%d streams on this device", stream_num);
741                 for (i = 0; i < stream_num; i++) {
742                         debug_log("  stream_id : %d", stream_id[i]);
743                         device_h->stream_id[i] = stream_id[i];
744                 }
745         } else {
746                 device_h->stream_num = 0;
747                 debug_log("There is no stream on this device");
748         }
749
750         return 0;
751 }
752
753 static void _mm_sound_device_connected_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
754                                                                                                                         int state, const char *name, int vendor_id, int product_id, bool is_running,
755                                                                                                                         int *stream_id, int stream_num, bool is_connected, void *userdata)
756 {
757         mm_sound_device_t device_h;
758         struct callback_data *cb_data = (struct callback_data*) userdata;
759         int device_flags;
760
761         debug_log("[Device %s] id(%d) type(%s) direction(%d) state(%d) name(%s) is_running(%d) vendor-id(%04x) product-id(%04x)",
762                                 is_connected ? "Connected" : "Disconnected", device_id, device_type, io_direction, state, name,
763                                 is_running, vendor_id, product_id);
764
765         if (cb_data == NULL) {
766                 debug_warning("device connected changed callback data null");
767                 return;
768         }
769
770         device_flags = (int) cb_data->extra_data;
771         if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
772                 return;
773
774         if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name,
775                                                         vendor_id, product_id, stream_id, stream_num, is_running) < 0) {
776                 debug_error("Failed to fill sound device");
777                 return;
778         }
779
780         ((mm_sound_device_connected_cb)(cb_data->user_cb))(&device_h, is_connected, cb_data->user_data);
781 }
782
783 int mm_sound_client_add_device_connected_callback(int device_flags, mm_sound_device_connected_cb func,
784                                                                                                 void* userdata, unsigned int *subs_id)
785 {
786         int ret = MM_ERROR_NONE;
787         struct callback_data *cb_data = NULL;
788
789         debug_fenter();
790
791         GET_CB_DATA(cb_data, func, userdata, (void*) device_flags);
792
793         ret = mm_sound_proxy_add_device_connected_callback(_mm_sound_device_connected_callback_wrapper_func,
794                                                                                                         cb_data, g_free, subs_id);
795         if (ret == MM_ERROR_NONE)
796                 g_need_emergent_exit = TRUE;
797
798         debug_fleave();
799         return ret;
800 }
801
802 int mm_sound_client_remove_device_connected_callback(unsigned int subs_id)
803 {
804         int ret = MM_ERROR_NONE;
805         debug_fenter();
806
807         ret = mm_sound_proxy_remove_device_connected_callback(subs_id);
808
809         debug_fleave();
810         return ret;
811 }
812
813 static void _mm_sound_device_info_changed_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
814                                                                                                                                 int state, const char *name, int vendor_id, int product_id, bool is_running,
815                                                                                                                                 int *stream_id, int stream_num, int changed_device_info_type, void *userdata)
816 {
817         mm_sound_device_t device_h;
818         struct callback_data *cb_data = (struct callback_data*) userdata;
819         int device_flags;
820
821         debug_log("[Device Info Changed] id(%d) type(%s) direction(%d) state(%d) name(%s) is_running(%d) "
822                         "vendor-id(%04x) product-id(%04x) changed_info_type(%d)",
823                         device_id, device_type, io_direction, state, name, is_running, vendor_id, product_id, changed_device_info_type);
824
825         if (cb_data == NULL) {
826                 debug_warning("device info changed callback data null");
827                 return;
828         }
829
830         device_flags = (int) cb_data->extra_data;
831         if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
832                 return;
833
834         if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name,
835                                 vendor_id, product_id, stream_id, stream_num, is_running) < 0) {
836                 debug_error("Failed to fill sound device");
837                 return;
838         }
839
840         ((mm_sound_device_info_changed_cb)(cb_data->user_cb))(&device_h, changed_device_info_type, cb_data->user_data);
841 }
842
843 int mm_sound_client_add_device_info_changed_callback(int device_flags, mm_sound_device_info_changed_cb func,
844                                                                                                         void *userdata, unsigned int *subs_id)
845 {
846         int ret = MM_ERROR_NONE;
847         struct callback_data *cb_data = (struct callback_data*) userdata;
848
849         debug_fenter();
850
851         GET_CB_DATA(cb_data, func, userdata, (void *) device_flags);
852
853         ret = mm_sound_proxy_add_device_info_changed_callback(_mm_sound_device_info_changed_callback_wrapper_func,
854                                                                                                                 cb_data, g_free, subs_id);
855
856         debug_fleave();
857         return ret;
858 }
859
860 int mm_sound_client_remove_device_info_changed_callback(unsigned int subs_id)
861 {
862         int ret = MM_ERROR_NONE;
863         debug_fenter();
864
865         ret =  mm_sound_proxy_remove_device_info_changed_callback(subs_id);
866
867         debug_fleave();
868         return ret;
869
870 }
871
872 static void _mm_sound_device_state_changed_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
873                                                                                                                                 int state, const char *name, int vendor_id, int product_id,
874                                                                                                                                 bool is_running, int *stream_id, int stream_num, void *userdata)
875 {
876         mm_sound_device_t device_h;
877         struct callback_data *cb_data = (struct callback_data*) userdata;
878         int device_flags;
879
880         debug_log("[Device State Changed] id(%d) type(%s) direction(%d) state(%d) name(%s) is_running(%d) vendor-id(%04x) product-id(%04x)",
881                                 device_id, device_type, io_direction, state, name, is_running, vendor_id, product_id);
882
883         if (cb_data == NULL) {
884                 debug_warning("device state changed callback data null");
885                 return;
886         }
887
888         device_flags = (int) cb_data->extra_data;
889
890         if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
891                 return;
892
893         if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name,
894                                 vendor_id, product_id, stream_id, stream_num, is_running) < 0) {
895                 debug_error("Failed to fill sound device");
896                 return;
897         }
898
899         ((mm_sound_device_state_changed_cb)(cb_data->user_cb))(&device_h, state, cb_data->user_data);
900 }
901
902 int mm_sound_client_add_device_state_changed_callback(int device_flags, mm_sound_device_state_changed_cb func,
903                                                                                                         void *userdata, unsigned int *id)
904 {
905         int ret = MM_ERROR_NONE;
906         struct callback_data *cb_data = (struct callback_data*) userdata;
907
908         debug_fenter();
909
910         GET_CB_DATA(cb_data, func, userdata, (void *) device_flags);
911
912         ret = mm_sound_proxy_add_device_state_changed_callback(_mm_sound_device_state_changed_callback_wrapper_func,
913                                                                                                                 cb_data, g_free, id);
914
915         debug_fleave();
916         return ret;
917 }
918
919 int mm_sound_client_remove_device_state_changed_callback(unsigned int id)
920 {
921         int ret = MM_ERROR_NONE;
922         debug_fenter();
923
924         ret =  mm_sound_proxy_remove_device_state_changed_callback(id);
925
926         debug_fleave();
927         return ret;
928 }
929
930 static void _mm_sound_device_running_changed_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
931                                                                                                                                 int state, const char *name, int vendor_id, int product_id,
932                                                                                                                                 bool is_running, int *stream_id, int stream_num, void *userdata)
933 {
934         mm_sound_device_t device_h;
935         struct callback_data *cb_data = (struct callback_data*) userdata;
936         int device_flags;
937
938         debug_log("[Device Running Changed] id(%d) type(%s) direction(%d) state(%d) name(%s) is_running(%d) vendor-id(%04x), product-id(%04x)",
939                         device_id, device_type, io_direction, state, name, is_running, vendor_id, product_id);
940
941         if (cb_data == NULL) {
942                 debug_warning("device running changed callback data null");
943                 return;
944         }
945
946         device_flags = (int) cb_data->extra_data;
947
948         if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
949                 return;
950
951         if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name,
952                                                         vendor_id, product_id, stream_id, stream_num, is_running) < 0) {
953                 debug_error("Failed to fill sound device");
954                 return;
955         }
956
957         ((mm_sound_device_running_changed_cb)(cb_data->user_cb))(&device_h, is_running, cb_data->user_data);
958 }
959
960 int mm_sound_client_add_device_running_changed_callback(int device_flags, mm_sound_device_running_changed_cb func,
961                                                                                                                 void *userdata, unsigned int *id)
962 {
963         int ret = MM_ERROR_NONE;
964         struct callback_data *cb_data = (struct callback_data*) userdata;
965
966         debug_fenter();
967
968         GET_CB_DATA(cb_data, func, userdata, (void *) device_flags);
969
970         ret = mm_sound_proxy_add_device_running_changed_callback(_mm_sound_device_running_changed_callback_wrapper_func,
971                                                                                                                         cb_data, g_free, id);
972
973         debug_fleave();
974         return ret;
975 }
976
977 int mm_sound_client_remove_device_running_changed_callback(unsigned int id)
978 {
979         int ret = MM_ERROR_NONE;
980         debug_fenter();
981
982         ret =  mm_sound_proxy_remove_device_running_changed_callback(id);
983
984         debug_fleave();
985         return ret;
986 }
987
988 int mm_sound_client_is_stream_on_device(int stream_id, int device_id, bool *is_on)
989 {
990         int ret = MM_ERROR_NONE;
991         debug_fenter();
992
993         if (!is_on) {
994                 debug_error("Invalid Parameter");
995                 ret = MM_ERROR_COMMON_INVALID_ARGUMENT;
996                 goto failed;
997         }
998
999         if ((ret = mm_sound_proxy_is_stream_on_device(stream_id, device_id, is_on)) != MM_ERROR_NONE) {
1000                 debug_error("failed to query is stream on device, ret[0x%x]", ret);
1001                 goto failed;
1002         }
1003
1004 failed:
1005         debug_fleave();
1006         return ret;
1007 }
1008
1009 int __convert_volume_type_to_str(int volume_type, char **volume_type_str)
1010 {
1011         int ret = MM_ERROR_NONE;
1012
1013         if (!volume_type_str)
1014                 return MM_ERROR_COMMON_INVALID_ARGUMENT;
1015
1016         switch (volume_type) {
1017         case VOLUME_TYPE_SYSTEM:
1018                 *volume_type_str = "system";
1019                 break;
1020         case VOLUME_TYPE_NOTIFICATION:
1021                 *volume_type_str = "notification";
1022                 break;
1023         case VOLUME_TYPE_ALARM:
1024                 *volume_type_str = "alarm";
1025                 break;
1026         case VOLUME_TYPE_RINGTONE:
1027                 *volume_type_str = "ringtone";
1028                 break;
1029         case VOLUME_TYPE_MEDIA:
1030                 *volume_type_str = "media";
1031                 break;
1032         case VOLUME_TYPE_CALL:
1033                 *volume_type_str = "call";
1034                 break;
1035         case VOLUME_TYPE_VOIP:
1036                 *volume_type_str = "voip";
1037                 break;
1038         case VOLUME_TYPE_VOICE:
1039                 *volume_type_str = "voice";
1040                 break;
1041         default:
1042                 debug_error("unexpected volume type [%d]", volume_type);
1043                 return MM_ERROR_SOUND_INTERNAL;
1044         }
1045         if (!strncmp(*volume_type_str, "", VOLUME_TYPE_LEN)) {
1046                 debug_error("could not find the volume_type[%d] in this switch case statement", volume_type);
1047                 ret = MM_ERROR_SOUND_INTERNAL;
1048         } else {
1049                 debug_log("volume_type[%s]", *volume_type_str);
1050         }
1051         return ret;
1052 }
1053
1054 static int __convert_volume_type_to_int(const char *volume_type_str, volume_type_t *volume_type)
1055 {
1056         int ret = MM_ERROR_NONE;
1057
1058         if (!volume_type || !volume_type_str)
1059                 return MM_ERROR_COMMON_INVALID_ARGUMENT;
1060
1061         if (!strncmp(volume_type_str, "system", VOLUME_TYPE_LEN)) {
1062                 *volume_type = VOLUME_TYPE_SYSTEM;
1063         } else if (!strncmp(volume_type_str, "notification", VOLUME_TYPE_LEN)) {
1064                 *volume_type = VOLUME_TYPE_NOTIFICATION;
1065         } else if (!strncmp(volume_type_str, "alarm", VOLUME_TYPE_LEN)) {
1066                 *volume_type = VOLUME_TYPE_ALARM;
1067         } else if (!strncmp(volume_type_str, "ringtone", VOLUME_TYPE_LEN)) {
1068                 *volume_type = VOLUME_TYPE_RINGTONE;
1069         } else if (!strncmp(volume_type_str, "media", VOLUME_TYPE_LEN)) {
1070                 *volume_type = VOLUME_TYPE_MEDIA;
1071         } else if (!strncmp(volume_type_str, "call", VOLUME_TYPE_LEN)) {
1072                 *volume_type = VOLUME_TYPE_CALL;
1073         } else if (!strncmp(volume_type_str, "voip", VOLUME_TYPE_LEN)) {
1074                 *volume_type = VOLUME_TYPE_VOIP;
1075         } else if (!strncmp(volume_type_str, "voice", VOLUME_TYPE_LEN)) {
1076                 *volume_type = VOLUME_TYPE_VOICE;
1077         } else {
1078                 debug_log("Invalid volume type : [%s]", volume_type_str);
1079                 ret = MM_ERROR_SOUND_INTERNAL;
1080         }
1081
1082         return ret;
1083 }
1084
1085 int mm_sound_client_set_volume_by_type(const int volume_type, const unsigned int volume_level)
1086 {
1087         int ret = MM_ERROR_NONE;
1088         char *type_str = NULL;
1089         debug_fenter();
1090
1091         if ((ret = __convert_volume_type_to_str(volume_type, &type_str)) != MM_ERROR_NONE) {
1092                 debug_error("volume type convert failed");
1093                 goto failed;
1094         }
1095
1096         ret = mm_sound_proxy_set_volume_by_type(type_str, volume_level);
1097
1098 failed:
1099         debug_fleave();
1100         return ret;
1101 }
1102
1103 static void _mm_sound_volume_changed_callback_wrapper_func(const char *direction, const char *volume_type_str,
1104                                                                                                                 int volume_level, void *userdata)
1105 {
1106         volume_type_t volume_type = 0;
1107         struct callback_data *cb_data = (struct callback_data *) userdata;
1108
1109         debug_log("direction : %s, volume_type : %s, volume_level : %d",
1110                         direction, volume_type_str, volume_level);
1111
1112         if (cb_data == NULL) {
1113                 debug_warning("volume changed callback data null");
1114                 return;
1115         }
1116
1117         if (__convert_volume_type_to_int(volume_type_str, &volume_type) != MM_ERROR_NONE) {
1118                 debug_error("volume type convert failed");
1119                 return;
1120         }
1121         debug_log("Call volume changed user cb, direction : %s, vol_type : %s(%d), level : %u",
1122                         direction, volume_type_str, volume_type, volume_level);
1123         ((mm_sound_volume_changed_cb)(cb_data->user_cb))(volume_type, volume_level, cb_data->user_data);
1124 }
1125
1126 int mm_sound_client_add_volume_changed_callback(mm_sound_volume_changed_cb func, void* userdata, unsigned int *subs_id)
1127 {
1128         int ret = MM_ERROR_NONE;
1129         struct callback_data *cb_data = NULL;
1130
1131         debug_fenter();
1132
1133         GET_CB_DATA(cb_data, func, userdata, NULL);
1134
1135         ret = mm_sound_proxy_add_volume_changed_callback(_mm_sound_volume_changed_callback_wrapper_func, cb_data, g_free, subs_id);
1136
1137         debug_fleave();
1138
1139         return ret;
1140 }
1141
1142 int mm_sound_client_remove_volume_changed_callback(unsigned int subs_id)
1143 {
1144         int ret = MM_ERROR_NONE;
1145         debug_fenter();
1146
1147         ret = mm_sound_proxy_remove_volume_changed_callback(subs_id);
1148
1149         debug_fleave();
1150         return ret;
1151 }
1152
1153 int mm_sound_client_set_filter_by_type(const char *stream_type, const char *filter_name, const char *filter_parameters, const char *filter_group)
1154 {
1155         int ret = MM_ERROR_NONE;
1156         debug_fenter();
1157
1158         ret = mm_sound_proxy_set_filter_by_type(stream_type, filter_name, filter_parameters, filter_group);
1159
1160         debug_fleave();
1161         return ret;
1162 }
1163
1164 int mm_sound_client_unset_filter_by_type(const char *stream_type)
1165 {
1166         int ret = MM_ERROR_NONE;
1167         debug_fenter();
1168
1169         ret = mm_sound_proxy_unset_filter_by_type(stream_type);
1170
1171         debug_fleave();
1172         return ret;
1173 }
1174
1175 int mm_sound_client_control_filter_by_type(const char *stream_type, const char *filter_name, const char *filter_controls)
1176 {
1177         int ret = MM_ERROR_NONE;
1178         debug_fenter();
1179
1180         ret = mm_sound_proxy_control_filter_by_type(stream_type, filter_name, filter_controls);
1181
1182         debug_fleave();
1183         return ret;
1184 }
1185
1186 #ifdef USE_FOCUS
1187 static gboolean _interrupted_completed(gpointer *data)
1188 {
1189         if (!data) {
1190                 debug_error("data is null");
1191                 return false;
1192         }
1193         if (!g_focus_session_interrupt_info.user_cb) {
1194                 debug_error("user_cb is null");
1195                 free(data);
1196                 return false;
1197         }
1198
1199         debug_msg("invoke user_cb(%p)", g_focus_session_interrupt_info.user_cb);
1200         (g_focus_session_interrupt_info.user_cb)(FOCUS_IS_RELEASED, (const char *)data, g_focus_session_interrupt_info.user_data);
1201         debug_msg("invoked");
1202
1203         free(data);
1204         return false;
1205 }
1206
1207 static void _session_interrupted_cb(int id, mm_sound_focus_type_e focus_type, mm_sound_focus_state_e state,
1208                                                                         const char *reason_for_change, const char *ext_info, void *user_data)
1209 {
1210         debug_msg("id(%d), focus_type(%d), state(%d), reason(%s)", id, focus_type, state, reason_for_change);
1211
1212         if (id != g_focus_session_interrupt_info.watch_cb_id) {
1213                 debug_error("id is not valid(param id:%d, g_focus_session_interrupt_watch_cb_id:%d)",
1214                                         id, g_focus_session_interrupt_info.watch_cb_id);
1215                 return;
1216         }
1217         if (!g_focus_session_interrupt_info.user_cb) {
1218                 debug_error("user callback is null");
1219                 return;
1220         }
1221
1222         debug_msg("  >>> invoking session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
1223         if (state == FOCUS_IS_RELEASED)
1224                 (g_focus_session_interrupt_info.user_cb)(FOCUS_IS_ACQUIRED, reason_for_change, g_focus_session_interrupt_info.user_data);
1225         else {
1226                 debug_msg("INTERRUPTED COMPLETED case, append it to idle");
1227                 g_idle_add((GSourceFunc)_interrupted_completed, strdup(reason_for_change));
1228         }
1229         debug_msg("  <<< session interrupt callback finished");
1230 }
1231
1232 int mm_sound_client_set_session_interrupt_callback(mm_sound_focus_session_interrupt_cb callback, void* user_data)
1233 {
1234         int ret = MM_ERROR_NONE;
1235
1236         debug_fenter();
1237
1238         if (!callback)
1239                 return MM_ERROR_INVALID_ARGUMENT;
1240
1241         /* add internal focus watch callback */
1242         if (g_focus_session_interrupt_info.watch_cb_id == -1) {
1243                 if ((ret = mm_sound_client_set_focus_watch_callback(getpid(), FOCUS_FOR_BOTH, true, true, _session_interrupted_cb, NULL,
1244                                                                                                                         &g_focus_session_interrupt_info.watch_cb_id))) {
1245                         debug_error("failed to mm_sound_client_set_focus_watch_callback(), ret(0x%x)", ret);
1246                         return ret;
1247                 }
1248         }
1249         g_focus_session_interrupt_info.user_cb = callback;
1250         g_focus_session_interrupt_info.user_data = user_data;
1251
1252         debug_fleave();
1253         return ret;
1254 }
1255
1256 int mm_sound_client_unset_session_interrupt_callback(void)
1257 {
1258         int ret = MM_ERROR_NONE;
1259
1260         debug_fenter();
1261
1262         if (!g_focus_session_interrupt_info.user_cb || g_focus_session_interrupt_info.watch_cb_id == -1) {
1263                 debug_error("no callback to unset");
1264                 return MM_ERROR_SOUND_INTERNAL;
1265         }
1266
1267         /* remove internal focus watch callback */
1268         if ((ret = mm_sound_client_unset_focus_watch_callback(g_focus_session_interrupt_info.watch_cb_id))) {
1269                 debug_error("failed to mm_sound_client_unset_focus_watch_callback(), id(%d), ret(0x%x)",
1270                                         g_focus_session_interrupt_info.watch_cb_id, ret);
1271                 return ret;
1272         }
1273         g_focus_session_interrupt_info.watch_cb_id = -1;
1274         g_focus_session_interrupt_info.user_cb = NULL;
1275         g_focus_session_interrupt_info.user_data = NULL;
1276
1277         debug_fleave();
1278         return ret;
1279 }
1280
1281 static gpointer _focus_thread_func(gpointer data)
1282 {
1283         unsigned int thread_id = (unsigned int)pthread_self();
1284         GMainLoop *focus_loop = (GMainLoop*)data;
1285
1286         debug_warning(">>> thread id(%u), mainloop[%p]", thread_id, focus_loop);
1287         if (focus_loop)
1288                 g_main_loop_run(focus_loop);
1289         debug_warning("<<< quit : thread id(%u), mainloop[%p]", thread_id, focus_loop);
1290
1291         return NULL;
1292 }
1293
1294 static gboolean _focus_fd_prepare(GSource *source, gint *timeout)
1295 {
1296 #ifdef __DEBUG__
1297         debug_warning("[ PREPARE : %p, (%p, %d)", source, timeout, timeout ? *timeout : -1);
1298 #endif
1299         return FALSE;
1300 }
1301
1302 static gboolean _focus_fd_check(GSource * source)
1303 {
1304         FocusSource* fsource = (FocusSource *)source;
1305
1306         if (!fsource) {
1307                 debug_error("GSource is null");
1308                 return FALSE;
1309         }
1310 #ifdef __DEBUG__
1311         debug_warning("CHECK : %p, 0x%x ]", source, fsource->pollfd.revents);
1312 #endif
1313         if (fsource->poll_fd.revents & (POLLIN | POLLPRI))
1314                 return TRUE;
1315         else
1316                 return FALSE;
1317 }
1318
1319 static gboolean _focus_fd_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
1320 {
1321         debug_warning("*** DISPATCH : %p, (%p, %p)", source, callback, user_data);
1322         return callback(user_data);
1323 }
1324
1325 static void _focus_fd_finalize(GSource *source)
1326 {
1327         debug_warning("### FINALIZE : %p", source);
1328 }
1329
1330 static int _focus_find_index_by_handle(int handle)
1331 {
1332         int i = 0;
1333         for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1334                 if (g_focus_sound_handle[i].focus_callback && handle == g_focus_sound_handle[i].handle) {
1335                         /* debug_msg("found index(%d) for handle(%d)", i, handle);*/
1336                         return (handle == FOCUS_HANDLE_INIT_VAL) ? -1 : i;
1337                 }
1338         }
1339         return -1;
1340 }
1341
1342 static int _focus_watch_find_index_by_handle(int handle)
1343 {
1344         int i = 0;
1345         for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1346                 if (g_focus_sound_handle[i].watch_callback && handle == g_focus_sound_handle[i].handle) {
1347                         /* debug_msg("found index(%d) for watch handle(%d)", i, handle);*/
1348                         return (handle == FOCUS_HANDLE_INIT_VAL) ? -1 : i;
1349                 }
1350         }
1351         return -1;
1352 }
1353
1354 static gboolean _focus_callback_handler(gpointer user_data)
1355 {
1356         focus_sound_info_t *focus_handle = (focus_sound_info_t *)user_data;
1357         GPollFD *poll_fd;
1358         int count;
1359         int tid = 0;
1360         focus_cb_data_lib cb_data;
1361
1362         debug_log(">>> thread id(%u)", (unsigned int)pthread_self());
1363
1364         if (!focus_handle) {
1365                 debug_error("focus_handle is null");
1366                 return G_SOURCE_CONTINUE;
1367         }
1368         poll_fd = &focus_handle->fsrc->poll_fd;
1369         debug_log("focus_handle(%p), poll_fd(%p)", focus_handle, poll_fd);
1370
1371         memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1372
1373         if (poll_fd->revents & (POLLIN | POLLPRI)) {
1374                 int changed_state = -1;
1375
1376                 count = read(poll_fd->fd, &cb_data, sizeof(cb_data));
1377                 if (count < 0) {
1378                         char str_error[256];
1379                         strerror_r(errno, str_error, sizeof(str_error));
1380                         debug_error("GpollFD read fail, errno=%d(%s)", errno, str_error);
1381                         return G_SOURCE_CONTINUE;
1382                 }
1383                 changed_state = cb_data.state;
1384
1385                 g_mutex_lock(&focus_handle->focus_lock);
1386
1387                 tid = focus_handle->focus_tid;
1388
1389                 if (changed_state != -1) {
1390                         debug_msg("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)",
1391                                         tid, cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type);
1392                         if (focus_handle->focus_callback == NULL) {
1393                                         debug_error("focus callback is null..");
1394                                         g_mutex_unlock(&focus_handle->focus_lock);
1395                                         return G_SOURCE_CONTINUE;
1396                         }
1397                         debug_msg("[CALLBACK(%p) START]", focus_handle->focus_callback);
1398                         (focus_handle->focus_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type,
1399                                                                                 cb_data.option, cb_data.ext_info, focus_handle->user_data);
1400                         debug_msg("[CALLBACK END]");
1401                 }
1402 #ifdef CONFIG_ENABLE_RETCB
1403                 {
1404                         int rett = 0;
1405                         int tmpfd = -1;
1406                         unsigned int buf = 0;
1407                         char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", focus_handle->focus_tid, cb_data.handle);
1408                         tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1409                         if (tmpfd < 0) {
1410                                 char str_error[256];
1411                                 strerror_r(errno, str_error, sizeof(str_error));
1412                                 debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)",
1413                                                         tid, tmpfd, filename2, errno, str_error);
1414                                 g_free(filename2);
1415                                 g_mutex_unlock(&focus_handle->focus_lock);
1416                                 return G_SOURCE_CONTINUE;
1417                         }
1418                         /* buf contains data as below,
1419                          * |<--12bits--><--4bits (reacquisition)--><--16bits (handle)-->| */
1420                         buf = (unsigned int)((0x0000ffff & cb_data.handle) | (focus_handle->auto_reacquire << 16));
1421                         rett = write(tmpfd, &buf, sizeof(buf));
1422                         close(tmpfd);
1423                         g_free(filename2);
1424                         debug_msg("[RETCB] tid(%d) finishing CB (write=%d)", tid, rett);
1425                 }
1426 #endif
1427         }
1428
1429         g_mutex_unlock(&focus_handle->focus_lock);
1430
1431         debug_fleave();
1432
1433         return G_SOURCE_CONTINUE;
1434 }
1435
1436 static gboolean _focus_watch_callback_handler(gpointer user_data)
1437 {
1438         focus_sound_info_t *focus_handle = (focus_sound_info_t *)user_data;
1439         GPollFD *poll_fd;
1440         int count;
1441         int tid = 0;
1442         focus_cb_data_lib cb_data;
1443
1444         debug_log(">>> thread id(%u)", (unsigned int)pthread_self());
1445
1446         if (!focus_handle) {
1447                 debug_error("focus_handle is null");
1448                 return G_SOURCE_CONTINUE;
1449         }
1450         poll_fd = &focus_handle->fsrc->poll_fd;
1451         debug_log("focus_handle(%p), poll_fd(%p)", focus_handle, poll_fd);
1452
1453         memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1454
1455         if (poll_fd->revents & (POLLIN | POLLPRI)) {
1456                 count = read(poll_fd->fd, &cb_data, sizeof(cb_data));
1457                 if (count < 0) {
1458                         char str_error[256];
1459                         strerror_r(errno, str_error, sizeof(str_error));
1460                         debug_error("GpollFD read fail, errno=%d(%s)", errno, str_error);
1461                         return G_SOURCE_CONTINUE;
1462                 }
1463
1464                 if (!focus_handle->is_used) {
1465                         debug_warning("unsetting watch calllback has been already requested");
1466                         goto SKIP_CB_AND_RET;
1467                 }
1468
1469                 g_mutex_lock(&focus_handle->focus_lock);
1470
1471                 tid = focus_handle->focus_tid;
1472
1473                 debug_msg("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)",
1474                                 tid, cb_data.handle,  cb_data.type, cb_data.state, cb_data.stream_type);
1475
1476                 if (focus_handle->watch_callback == NULL) {
1477                         debug_warning("watch callback is null..");
1478                         goto SKIP_CB_AND_RET;
1479                 }
1480                 if (focus_handle->unset_watch_callback_requested == true) {
1481                         debug_warning("unset_watch_callback_requested..");
1482                         goto SKIP_CB_AND_RET;
1483                 }
1484
1485                 debug_msg("[CALLBACK(%p) START]", focus_handle->watch_callback);
1486                 (focus_handle->watch_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type,
1487                                                                         cb_data.ext_info, focus_handle->user_data);
1488                 debug_msg("[CALLBACK END]");
1489
1490 SKIP_CB_AND_RET:
1491 #ifdef CONFIG_ENABLE_RETCB
1492                 {
1493                         int rett = 0;
1494                         int tmpfd = -1;
1495                         int buf = -1;
1496                         char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr", focus_handle->focus_tid, cb_data.handle);
1497                         tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1498                         if (tmpfd < 0) {
1499                                 char str_error[256];
1500                                 strerror_r(errno, str_error, sizeof(str_error));
1501                                 debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)",
1502                                                         tid, tmpfd, filename2, errno, str_error);
1503                                 g_free(filename2);
1504                                 g_mutex_unlock(&focus_handle->focus_lock);
1505                                 return G_SOURCE_CONTINUE;
1506                         }
1507                         buf = cb_data.handle;
1508                         rett = write(tmpfd, &buf, sizeof(buf));
1509                         close(tmpfd);
1510                         g_free(filename2);
1511                         debug_msg("[RETCB] tid(%d) finishing CB (write=%d)", tid, rett);
1512                 }
1513 #endif
1514         }
1515
1516         if (focus_handle->is_used) {
1517                 debug_msg("unlock focus_lock = %p", &focus_handle->focus_lock);
1518                 g_mutex_unlock(&focus_handle->focus_lock);
1519         }
1520
1521         debug_fleave();
1522
1523         return G_SOURCE_CONTINUE;
1524 }
1525
1526 static void _focus_open_callback(int index, bool is_for_watching)
1527 {
1528         mode_t pre_mask;
1529         char *filename;
1530
1531         debug_fenter();
1532
1533         if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1534                 debug_error("Invalid focus handle index [%d]", index);
1535                 return;
1536         }
1537
1538         if (is_for_watching) {
1539                 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch",
1540                                                                 g_focus_sound_handle[index].focus_tid,
1541                                                                 g_focus_sound_handle[index].handle);
1542         } else {
1543                 filename = g_strdup_printf("/tmp/FOCUS.%d.%d",
1544                                                                 g_focus_sound_handle[index].focus_tid,
1545                                                                 g_focus_sound_handle[index].handle);
1546         }
1547         pre_mask = umask(0);
1548         if (mknod(filename, S_IFIFO|0666, 0))
1549                 debug_error("mknod() failure, errno(%d)", errno);
1550         umask(pre_mask);
1551         g_focus_sound_handle[index].focus_fd = open(filename, O_RDWR|O_NONBLOCK);
1552         if (g_focus_sound_handle[index].focus_fd == -1) {
1553                 debug_error("Open fail : index(%d), file open error(%d)", index, errno);
1554         } else {
1555                 debug_log("Open success : index(%d), filename(%s), fd(%d)",
1556                                 index, filename, g_focus_sound_handle[index].focus_fd);
1557         }
1558         g_free(filename);
1559         filename = NULL;
1560
1561 #ifdef CONFIG_ENABLE_RETCB
1562         char *filename2;
1563
1564         if (is_for_watching) {
1565                 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr",
1566                                                                         g_focus_sound_handle[index].focus_tid,
1567                                                                         g_focus_sound_handle[index].handle);
1568         } else {
1569                 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr",
1570                                                                         g_focus_sound_handle[index].focus_tid,
1571                                                                         g_focus_sound_handle[index].handle);
1572         }
1573         pre_mask = umask(0);
1574         if (mknod(filename2, S_IFIFO | 0666, 0))
1575                 debug_error("mknod() failure, errno(%d)", errno);
1576         umask(pre_mask);
1577         g_free(filename2);
1578         filename2 = NULL;
1579 #endif
1580         debug_fleave();
1581
1582 }
1583
1584 void _focus_close_callback(int index, bool is_for_watching)
1585 {
1586         char *filename = NULL;
1587
1588         debug_fenter();
1589
1590         if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1591                 debug_error("Invalid focus handle index [%d]", index);
1592                 return;
1593         }
1594
1595         if (g_focus_sound_handle[index].focus_fd < 0) {
1596                 debug_error("Close fail : index(%d)", index);
1597         } else {
1598                 close(g_focus_sound_handle[index].focus_fd);
1599                 debug_log("Close Success : index(%d)", index);
1600         }
1601
1602         if (is_for_watching) {
1603                 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch",
1604                                                                 g_focus_sound_handle[index].focus_tid,
1605                                                                 g_focus_sound_handle[index].handle);
1606         } else {
1607                 filename = g_strdup_printf("/tmp/FOCUS.%d.%d",
1608                                                                 g_focus_sound_handle[index].focus_tid,
1609                                                                 g_focus_sound_handle[index].handle);
1610         }
1611         if (remove(filename)) {
1612                 debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)",
1613                                         filename, errno);
1614         }
1615         g_free(filename);
1616         filename = NULL;
1617
1618 #ifdef CONFIG_ENABLE_RETCB
1619         char *filename2;
1620
1621         if (is_for_watching) {
1622                 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr",
1623                                                                         g_focus_sound_handle[index].focus_tid,
1624                                                                         g_focus_sound_handle[index].handle);
1625         } else {
1626                 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr",
1627                                                                         g_focus_sound_handle[index].focus_tid,
1628                                                                         g_focus_sound_handle[index].handle);
1629         }
1630         if (remove(filename2)) {
1631                 debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)",
1632                                         filename2, errno);
1633         }
1634         g_free(filename2);
1635         filename2 = NULL;
1636
1637         debug_fleave();
1638 #endif
1639
1640 }
1641
1642 static GSourceFuncs event_funcs = {
1643         .prepare = _focus_fd_prepare,
1644         .check = _focus_fd_check,
1645         .dispatch = _focus_fd_dispatch,
1646         .finalize = _focus_fd_finalize,
1647 };
1648
1649 static bool _focus_add_sound_callback(int index, focus_callback_handler_t focus_cb_handler)
1650 {
1651         FocusSource *fsrc = NULL;
1652         GSource *src = NULL;
1653         guint fsrc_id = 0;
1654
1655         debug_fenter();
1656
1657         g_mutex_init(&g_focus_sound_handle[index].focus_lock);
1658
1659         src = g_source_new(&event_funcs, sizeof(FocusSource));
1660         if (!src) {
1661                 debug_error("failed to g_source_new for focus source");
1662                 goto ERROR;
1663         }
1664
1665         fsrc = (FocusSource*) src;
1666
1667         fsrc->poll_fd.fd = g_focus_sound_handle[index].focus_fd;
1668         fsrc->poll_fd.events = (gushort)(POLLIN | POLLPRI);
1669         g_source_add_poll(src, &fsrc->poll_fd);
1670
1671         g_source_set_callback(src, focus_cb_handler, (gpointer)&g_focus_sound_handle[index], NULL);
1672
1673         debug_warning("fsrc(%p), src_funcs(%p), pollfd(%p), fd(%d)",
1674                                 fsrc, &event_funcs, &fsrc->poll_fd, fsrc->poll_fd.fd);
1675
1676         fsrc_id = g_source_attach(src, g_main_loop_get_context(g_focus_sound_handle[index].focus_loop));
1677         if (!fsrc_id) {
1678                 debug_error("failed to attach the source to context");
1679                 goto ERROR;
1680         }
1681         g_source_unref(src);
1682
1683         g_focus_sound_handle[index].fsrc = fsrc;
1684
1685         debug_fleave();
1686         return true;
1687
1688 ERROR:
1689         if (src)
1690                 g_source_unref(src);
1691
1692         return false;
1693 }
1694
1695 static bool _focus_remove_sound_callback(int index)
1696 {
1697         focus_sound_info_t *h = NULL;
1698
1699         debug_fenter();
1700
1701         if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1702                 debug_error("Invalid focus handle index [%d]", index);
1703                 return false;
1704         }
1705
1706         h = &g_focus_sound_handle[index];
1707         if (h->fsrc) {
1708                 g_source_destroy((GSource *)h->fsrc);
1709                 h->fsrc = NULL;
1710         }
1711
1712         h->focus_callback = NULL;
1713         h->watch_callback = NULL;
1714
1715         g_mutex_clear(&h->focus_lock);
1716
1717         debug_fleave();
1718
1719         return true;
1720 }
1721
1722 static void _focus_add_callback(int index, bool is_for_watching)
1723 {
1724         debug_fenter();
1725
1726         if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1727                 debug_error("Invalid focus handle index [%d]", index);
1728                 return;
1729         }
1730
1731         if (!is_for_watching) {
1732                 if (!_focus_add_sound_callback(index, _focus_callback_handler))
1733                         debug_error("failed to _focus_add_sound_callback(%p)", _focus_callback_handler);
1734         } else { // need to check if it's necessary
1735                 if (!_focus_add_sound_callback(index, _focus_watch_callback_handler))
1736                         debug_error("failed to _focus_add_sound_callback(%p)", _focus_watch_callback_handler);
1737         }
1738         debug_fleave();
1739 }
1740
1741 static void _focus_remove_callback(int index)
1742 {
1743         debug_fenter();
1744         if (!_focus_remove_sound_callback(index))
1745                 debug_error("failed to __focus_remove_sound_callback()");
1746         debug_fleave();
1747 }
1748
1749 static void _focus_init_callback(int index, bool is_for_watching)
1750 {
1751         debug_fenter();
1752         _focus_open_callback(index, is_for_watching);
1753         _focus_add_callback(index, is_for_watching);
1754         debug_fleave();
1755 }
1756
1757 static void _focus_deinit_callback(int index, bool is_for_watching)
1758 {
1759         debug_fenter();
1760         _focus_remove_callback(index);
1761         _focus_close_callback(index, is_for_watching);
1762         debug_fleave();
1763 }
1764
1765 #define INTERVAL_MS 20
1766 static int _focus_loop_is_running_timed_wait(GMainLoop *focus_loop, int timeout_ms)
1767 {
1768         int reduced_time_ms = timeout_ms;
1769         if (!focus_loop || timeout_ms < 0) {
1770                 debug_error("invalid argument, focus_loop(%p), timeout_ms(%d)", focus_loop, timeout_ms);
1771                 return MM_ERROR_INVALID_ARGUMENT;
1772         }
1773
1774         do {
1775                 if (g_main_loop_is_running(focus_loop))
1776                         return MM_ERROR_NONE;
1777
1778                 usleep(INTERVAL_MS * 1000);
1779                 if (reduced_time_ms < timeout_ms)
1780                         debug_warning("reduced_time_ms(%d)", reduced_time_ms);
1781         } while ((reduced_time_ms -= INTERVAL_MS) >= 0);
1782
1783         debug_error("focus_loop is not running for timeout_ms(%d), focus_loop(%p) ", timeout_ms, focus_loop);
1784
1785         return MM_ERROR_SOUND_INTERNAL;
1786 }
1787
1788 #define LOOP_RUNNING_WAIT_TIME_MS 200
1789 static int _focus_init_context(int index)
1790 {
1791         int ret = MM_ERROR_NONE;
1792         GMainContext *focus_context;
1793
1794         debug_fenter();
1795
1796         if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1797                 debug_error("index(%d) is not valid", index);
1798                 return MM_ERROR_INVALID_ARGUMENT;
1799         }
1800
1801         focus_context = g_main_context_new();
1802         g_focus_sound_handle[index].focus_loop = g_main_loop_new(focus_context, FALSE);
1803         g_main_context_unref(focus_context);
1804         if (g_focus_sound_handle[index].focus_loop == NULL) {
1805                 debug_error("could not create mainloop..");
1806                 goto ERROR;
1807         }
1808
1809         g_focus_sound_handle[index].focus_cb_thread = g_thread_new("focus-cb-thread",
1810                                                                         _focus_thread_func,
1811                                                                         g_focus_sound_handle[index].focus_loop);
1812         if (g_focus_sound_handle[index].focus_cb_thread == NULL) {
1813                 debug_error("could not create thread..");
1814                 goto ERROR;
1815         }
1816
1817         debug_warning("focus cb thread[%p] with mainloop[%p] is created for index(%d)",
1818                                 g_focus_sound_handle[index].focus_cb_thread, g_focus_sound_handle[index].focus_loop, index);
1819
1820         if ((ret = _focus_loop_is_running_timed_wait(g_focus_sound_handle[index].focus_loop, LOOP_RUNNING_WAIT_TIME_MS))) {
1821                 debug_error("failed to _focus_loop_is_running_timed_wait(), ret[0x%x]", ret);
1822                 goto ERROR;
1823         }
1824
1825         debug_fleave();
1826
1827         return MM_ERROR_NONE;
1828
1829 ERROR:
1830         if (g_focus_sound_handle[index].focus_loop) {
1831                 g_main_loop_unref(g_focus_sound_handle[index].focus_loop);
1832                 g_focus_sound_handle[index].focus_loop = NULL;
1833         }
1834         return MM_ERROR_SOUND_INTERNAL;
1835 }
1836
1837 static void _focus_deinit_context(int index)
1838 {
1839         debug_fenter();
1840
1841         if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1842                 debug_error("index(%d) is not valid", index);
1843                 return;
1844         }
1845
1846         if (!g_focus_sound_handle[index].focus_loop || !g_focus_sound_handle[index].focus_cb_thread) {
1847                 debug_error("focus_loop[%p] or focus_cb_thread[%p] is null",
1848                                 g_focus_sound_handle[index].focus_loop, g_focus_sound_handle[index].focus_cb_thread);
1849                 return;
1850         }
1851
1852         g_main_loop_quit(g_focus_sound_handle[index].focus_loop);
1853         g_thread_join(g_focus_sound_handle[index].focus_cb_thread);
1854         debug_warning("after thread join, thread[%p], mainloop[%p] for index(%d)",
1855                         g_focus_sound_handle[index].focus_cb_thread, g_focus_sound_handle[index].focus_loop, index);
1856         g_main_loop_unref(g_focus_sound_handle[index].focus_loop);
1857         g_focus_sound_handle[index].focus_loop = NULL;
1858         g_focus_sound_handle[index].focus_cb_thread = NULL;
1859
1860         debug_fleave();
1861 }
1862
1863 int mm_sound_client_get_unique_id(int *id)
1864 {
1865         int ret = MM_ERROR_NONE;
1866
1867         debug_fenter();
1868
1869         if (!id)
1870                 ret = MM_ERROR_INVALID_ARGUMENT;
1871         else
1872                 ret = mm_sound_proxy_get_unique_id(id);
1873
1874         debug_fleave();
1875
1876         return ret;
1877 }
1878
1879 int mm_sound_client_is_focus_cb_thread(GThread *mine, bool *result)
1880 {
1881         int ret = MM_ERROR_NONE;
1882         int i = 0;
1883
1884         if (!mine || !result)
1885                 ret = MM_ERROR_INVALID_ARGUMENT;
1886         else {
1887                 *result = false;
1888                 for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1889                         if (!g_focus_sound_handle[i].is_used)
1890                                 continue;
1891                         if (g_focus_sound_handle[i].focus_cb_thread == mine) {
1892                                 *result = true;
1893                                 break;
1894                         }
1895                 }
1896         }
1897
1898         return ret;
1899 }
1900
1901 int mm_sound_client_register_focus(int id, int pid, const char *stream_type, bool is_for_session,
1902                                                                 mm_sound_focus_changed_cb callback, void* user_data)
1903 {
1904         int ret = MM_ERROR_NONE;
1905         int instance;
1906         int index = 0;
1907
1908         debug_fenter();
1909         MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1910
1911         instance = pid;
1912
1913         for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
1914                 if (g_focus_sound_handle[index].is_used == false) {
1915                         g_focus_sound_handle[index].is_used = true;
1916                         break;
1917                 }
1918         }
1919
1920         g_focus_sound_handle[index].focus_tid = instance;
1921         g_focus_sound_handle[index].handle = id;
1922         g_focus_sound_handle[index].focus_callback = callback;
1923         g_focus_sound_handle[index].user_data = user_data;
1924         g_focus_sound_handle[index].is_for_session = is_for_session;
1925         g_focus_sound_handle[index].auto_reacquire = true;
1926
1927         ret = mm_sound_proxy_register_focus(id, pid, stream_type, callback, is_for_session, user_data);
1928         if (ret == MM_ERROR_NONE) {
1929                 debug_msg("Success to register focus");
1930                 g_need_emergent_exit = TRUE;
1931                 if (_focus_init_context(index)) {
1932                         ret = MM_ERROR_SOUND_INTERNAL;
1933                         goto cleanup;
1934                 }
1935         } else {
1936                 debug_error("Error occurred : 0x%x", ret);
1937                 goto cleanup;
1938         }
1939
1940         _focus_init_callback(index, false);
1941
1942 cleanup:
1943         if (ret)
1944                 g_focus_sound_handle[index].is_used = false;
1945
1946         MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1947         debug_fleave();
1948
1949         return ret;
1950 }
1951
1952 int mm_sound_client_unregister_focus(int id)
1953 {
1954         int ret = MM_ERROR_NONE;
1955         int instance;
1956         int index = -1;
1957
1958         debug_fenter();
1959         MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1960
1961         index = _focus_find_index_by_handle(id);
1962         if (index == -1) {
1963                 debug_error("Could not find index");
1964                 ret = MM_ERROR_INVALID_ARGUMENT;
1965                 goto cleanup;
1966         }
1967         instance = g_focus_sound_handle[index].focus_tid;
1968
1969         if (!g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1970                 debug_warning("maybe focus_callback is being called, try one more time..");
1971                 usleep(2500000); // 2.5 sec
1972                 if (g_mutex_trylock(&g_focus_sound_handle[index].focus_lock))
1973                         debug_msg("finally got focus_lock");
1974         }
1975
1976         ret = mm_sound_proxy_unregister_focus(instance, id, g_focus_sound_handle[index].is_for_session);
1977         if (ret == MM_ERROR_NONE)
1978                 debug_msg("Success to unregister focus");
1979         else
1980                 debug_error("Error occurred : 0x%x", ret);
1981
1982         g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
1983
1984         _focus_deinit_callback(index, false);
1985         g_focus_sound_handle[index].focus_fd = 0;
1986         g_focus_sound_handle[index].focus_tid = 0;
1987         g_focus_sound_handle[index].handle = 0;
1988         g_focus_sound_handle[index].is_used = false;
1989         _focus_deinit_context(index);
1990
1991
1992 cleanup:
1993         MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1994         debug_fleave();
1995         return ret;
1996 }
1997
1998 int mm_sound_client_set_focus_reacquisition(int id, bool reacquisition, bool is_for_session)
1999 {
2000         int ret = MM_ERROR_NONE;
2001         int instance;
2002         int index = -1;
2003         bool result;
2004
2005         debug_fenter();
2006
2007         /* Since the muse server which uses this library for multiple handles executes requests from clients serially,
2008          * we can skip locking/unlocking the mutex for this case. */
2009         if (!is_for_session)
2010                 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2011
2012         index = _focus_find_index_by_handle(id);
2013         if (index == -1) {
2014                 debug_error("Could not find index");
2015                 ret = MM_ERROR_INVALID_ARGUMENT;
2016                 goto cleanup;
2017         }
2018         instance = g_focus_sound_handle[index].focus_tid;
2019
2020         ret = mm_sound_client_is_focus_cb_thread(g_thread_self(), &result);
2021         if (ret) {
2022                 debug_error("mm_sound_client_is_focus_cb_thread failed");
2023                 goto cleanup;
2024         } else if (!result) {
2025                 ret = mm_sound_proxy_set_focus_reacquisition(instance, id, reacquisition, is_for_session);
2026                 if (ret == MM_ERROR_NONE) {
2027                         debug_msg("Success to set focus reacquisition to [%d]", reacquisition);
2028                 } else {
2029                         debug_error("Error occurred : 0x%x", ret);
2030                         goto cleanup;
2031                 }
2032         } else {
2033                 debug_warning("Inside the focus cb thread, set focus reacquisition to [%d]", reacquisition);
2034         }
2035
2036         g_focus_sound_handle[index].auto_reacquire = reacquisition;
2037         debug_msg("set focus reacquisition(%d) for id(%d)", reacquisition, id);
2038
2039 cleanup:
2040         if (!is_for_session)
2041                 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2042         debug_fleave();
2043         return ret;
2044 }
2045
2046 int mm_sound_client_get_focus_reacquisition(int id, bool *reacquisition)
2047 {
2048         int ret = MM_ERROR_NONE;
2049         int index = -1;
2050
2051         debug_fenter();
2052
2053         if (!reacquisition) {
2054                 debug_error("Invalid parameter");
2055                 return MM_ERROR_INVALID_ARGUMENT;
2056         }
2057
2058         MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2059
2060         index = _focus_find_index_by_handle(id);
2061         if (index == -1) {
2062                 debug_error("Could not find index");
2063                 ret = MM_ERROR_INVALID_ARGUMENT;
2064                 goto cleanup;
2065         }
2066
2067         *reacquisition = g_focus_sound_handle[index].auto_reacquire;
2068         debug_msg("get focus reacquisition(%d) for id(%d)", *reacquisition, id);
2069
2070 cleanup:
2071         MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2072         debug_fleave();
2073         return ret;
2074 }
2075
2076 int mm_sound_client_get_acquired_focus_stream_type(int focus_type, char **stream_type, int *option, char **ext_info)
2077 {
2078         int ret = MM_ERROR_NONE;
2079         char *stream_type_str = NULL;
2080         char *ext_info_str = NULL;
2081
2082         debug_fenter();
2083
2084         ret = mm_sound_proxy_get_acquired_focus_stream_type(focus_type, &stream_type_str, option, &ext_info_str);
2085         if (ret == MM_ERROR_NONE) {
2086                 debug_msg("Success to get stream type of acquired focus, stream_type(%s), ext_info(%s)",
2087                                 stream_type_str, ext_info_str);
2088                 *stream_type = strdup(stream_type_str);
2089                 *ext_info = strdup(ext_info_str);
2090                 g_free(stream_type_str);
2091                 g_free(ext_info_str);
2092         } else {
2093                 debug_error("Error occurred : 0x%x", ret);
2094         }
2095
2096         debug_fleave();
2097         return ret;
2098 }
2099
2100 int mm_sound_client_acquire_focus(int id, mm_sound_focus_type_e type, int option, const char *ext_info)
2101 {
2102         int ret = MM_ERROR_NONE;
2103         int instance;
2104         int index = -1;
2105
2106         debug_fenter();
2107         MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2108
2109         index = _focus_find_index_by_handle(id);
2110         if (index == -1) {
2111                 debug_error("Could not find index");
2112                 ret = MM_ERROR_INVALID_ARGUMENT;
2113                 goto cleanup;
2114         }
2115         instance = g_focus_sound_handle[index].focus_tid;
2116
2117         ret = mm_sound_proxy_acquire_focus(instance, id, type, option, ext_info, g_focus_sound_handle[index].is_for_session);
2118         if (ret == MM_ERROR_NONE)
2119                 debug_msg("Success to acquire focus");
2120         else
2121                 debug_error("Error occurred : 0x%x", ret);
2122
2123 cleanup:
2124         MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2125         debug_fleave();
2126         return ret;
2127 }
2128
2129 int mm_sound_client_release_focus(int id, mm_sound_focus_type_e type, int option, const char *ext_info)
2130 {
2131         int ret = MM_ERROR_NONE;
2132         int instance;
2133         int index = -1;
2134
2135         debug_fenter();
2136         MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2137
2138         index = _focus_find_index_by_handle(id);
2139         if (index == -1) {
2140                 debug_error("Could not find index");
2141                 ret = MM_ERROR_INVALID_ARGUMENT;
2142                 goto cleanup;
2143         }
2144         instance = g_focus_sound_handle[index].focus_tid;
2145
2146         ret = mm_sound_proxy_release_focus(instance, id, type, option, ext_info, g_focus_sound_handle[index].is_for_session);
2147         if (ret == MM_ERROR_NONE)
2148                 debug_msg("Success to release focus");
2149         else
2150                 debug_error("Error occurred : 0x%x", ret);
2151
2152 cleanup:
2153         MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2154         debug_fleave();
2155         return ret;
2156 }
2157
2158 int mm_sound_client_update_stream_focus_status(int id, unsigned int status)
2159 {
2160         int ret = MM_ERROR_NONE;
2161         debug_fenter();
2162
2163         if ((ret = mm_sound_proxy_update_stream_focus_status(id, status)) != MM_ERROR_NONE)
2164                 debug_error("failed to update stream focus status, ret[0x%x]", ret);
2165
2166         debug_fleave();
2167         return ret;
2168 }
2169
2170 int mm_sound_client_deliver_focus(int pid, int src_id, int dst_id, mm_sound_focus_type_e focus_type)
2171 {
2172         int ret = MM_ERROR_NONE;
2173         debug_fenter();
2174
2175         if ((ret = mm_sound_proxy_deliver_focus(pid, src_id, dst_id, focus_type)) != MM_ERROR_NONE)
2176                 debug_error("failed to deliver focus, ret[0x%x]", ret);
2177
2178         debug_fleave();
2179         return ret;
2180 }
2181
2182 int mm_sound_client_set_focus_watch_callback(int pid, mm_sound_focus_type_e focus_type, bool is_for_session, bool is_for_monitor,
2183                                                                                         mm_sound_focus_changed_watch_cb callback, void* user_data, int *id)
2184 {
2185         int ret = MM_ERROR_NONE;
2186         int instance;
2187         int index = 0;
2188
2189         debug_fenter();
2190
2191         if (!id)
2192                 return MM_ERROR_INVALID_ARGUMENT;
2193
2194         //pthread_mutex_lock(&g_thread_mutex2);
2195
2196         instance = pid;
2197
2198         ret = mm_sound_proxy_get_unique_id(id);
2199         if (ret)
2200                 return ret;
2201
2202         MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2203
2204         for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
2205                 if (g_focus_sound_handle[index].is_used == false) {
2206                         g_focus_sound_handle[index].is_used = true;
2207                         break;
2208                 }
2209         }
2210
2211         g_focus_sound_handle[index].focus_tid = instance;
2212         g_focus_sound_handle[index].handle = *id;
2213         g_focus_sound_handle[index].watch_callback = callback;
2214         g_focus_sound_handle[index].user_data = user_data;
2215         g_focus_sound_handle[index].is_for_session = is_for_session;
2216         g_focus_sound_handle[index].is_for_monitor = is_for_monitor;
2217         g_focus_sound_handle[index].unset_watch_callback_requested = false;
2218
2219         ret = mm_sound_proxy_set_focus_watch_callback(pid, g_focus_sound_handle[index].handle, focus_type,
2220                                                                                                 is_for_session, is_for_monitor, callback, user_data);
2221
2222         if (ret == MM_ERROR_NONE) {
2223                 debug_msg("Success to watch focus");
2224                 g_need_emergent_exit = TRUE;
2225                 if (_focus_init_context(index)) {
2226                         ret = MM_ERROR_SOUND_INTERNAL;
2227                         goto cleanup;
2228                 }
2229         } else {
2230                 debug_error("Error occurred : 0x%x", ret);
2231                 goto cleanup;
2232         }
2233
2234         _focus_init_callback(index, true);
2235
2236 cleanup:
2237         if (ret)
2238                 g_focus_sound_handle[index].is_used = false;
2239
2240         MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2241         debug_fleave();
2242         return ret;
2243 }
2244
2245 int mm_sound_client_request_unset_focus_watch_callback(int id)
2246 {
2247         int ret = MM_ERROR_NONE;
2248         int index = -1;
2249
2250         debug_fenter();
2251         MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2252
2253         index = _focus_watch_find_index_by_handle(id);
2254         if (index == -1) {
2255                 debug_error("Could not find index");
2256                 ret = MM_ERROR_INVALID_ARGUMENT;
2257                 goto cleanup;
2258         }
2259         g_focus_sound_handle[index].unset_watch_callback_requested = true;
2260
2261 cleanup:
2262         MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2263         debug_fleave();
2264         return ret;
2265 }
2266
2267 int mm_sound_client_unset_focus_watch_callback(int id)
2268 {
2269         int ret = MM_ERROR_NONE;
2270         int index = -1;
2271
2272         debug_fenter();
2273         MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2274
2275         index = _focus_watch_find_index_by_handle(id);
2276         if (index == -1) {
2277                 debug_error("Could not find index");
2278                 ret = MM_ERROR_INVALID_ARGUMENT;
2279                 goto cleanup;
2280         }
2281
2282         g_mutex_lock(&g_focus_sound_handle[index].focus_lock);
2283
2284         g_focus_sound_handle[index].is_used = false;
2285
2286         ret = mm_sound_proxy_unset_focus_watch_callback(g_focus_sound_handle[index].focus_tid,
2287                                                                                                         g_focus_sound_handle[index].handle,
2288                                                                                                         g_focus_sound_handle[index].is_for_session);
2289
2290         if (ret == MM_ERROR_NONE)
2291                 debug_msg("Success to unwatch focus");
2292         else
2293                 debug_error("Error occurred : 0x%x", ret);
2294
2295
2296         g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
2297
2298         _focus_deinit_callback(index, true);
2299         g_focus_sound_handle[index].focus_fd = 0;
2300         g_focus_sound_handle[index].focus_tid = 0;
2301         g_focus_sound_handle[index].handle = 0;
2302         _focus_deinit_context(index);
2303
2304 cleanup:
2305         MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2306         debug_fleave();
2307         return ret;
2308 }
2309
2310 static gboolean _idle_event_callback(void *data)
2311 {
2312         focus_idle_event_t *idle_event_data = (focus_idle_event_t*)data;
2313         int ret = MM_ERROR_NONE;
2314
2315         if (data == NULL) {
2316                 debug_error("data is null");
2317                 return FALSE;
2318         }
2319
2320         debug_msg("idle_event_data(%p): type(%d), data(%d)",
2321                 idle_event_data, idle_event_data->type, idle_event_data->data);
2322
2323         switch (idle_event_data->type) {
2324         case IDLE_EVENT_TYPE_UNSET_FOCUS_WATCH_CB:
2325                 if ((ret = mm_sound_client_unset_focus_watch_callback(idle_event_data->data)))
2326                         debug_error("Could not unset focus watch callback, id(%d), ret = %x", idle_event_data->data, ret);
2327                 break;
2328         case IDLE_EVENT_TYPE_UNREGISTER_FOCUS:
2329                 if ((ret = mm_sound_client_unregister_focus(idle_event_data->data)))
2330                         debug_error("Could not unregister focus, id(%d), ret = %x", idle_event_data->data, ret);
2331                 break;
2332         default:
2333                 debug_warning("invalid type(%d)", idle_event_data->type);
2334                 break;
2335         }
2336
2337         g_free(idle_event_data);
2338
2339         g_idle_event_src = 0;
2340
2341         MMSOUND_LEAVE_CRITICAL_SECTION(&g_event_mutex);
2342
2343         return FALSE;
2344 }
2345
2346 int mm_sound_client_execute_focus_func_in_main_context(focus_idle_event_type_e type, int data)
2347 {
2348         focus_idle_event_t *idle_event_data = NULL;
2349
2350         if (IDLE_EVENT_TYPE_MAX < type)
2351                 return MM_ERROR_INVALID_ARGUMENT;
2352
2353         MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_event_mutex, MM_ERROR_SOUND_INTERNAL);
2354
2355         idle_event_data = g_new0(focus_idle_event_t, 1);
2356         idle_event_data->type = type;
2357         idle_event_data->data = data;
2358
2359         g_idle_event_src = g_idle_add_full(G_PRIORITY_HIGH,
2360                                 (GSourceFunc)_idle_event_callback,
2361                                 (gpointer)idle_event_data,
2362                                 NULL);
2363
2364         return MM_ERROR_NONE;
2365 }
2366 #endif
2367
2368
2369 int mm_sound_client_add_test_callback(mm_sound_test_cb func, void* user_data, unsigned int *subs_id)
2370 {
2371         int ret = MM_ERROR_NONE;
2372
2373         debug_fenter();
2374
2375         ret = mm_sound_proxy_add_test_callback(func, user_data, g_free, subs_id);
2376
2377         debug_fleave();
2378         return ret;
2379 }
2380
2381 int mm_sound_client_remove_test_callback(unsigned int subs_id)
2382 {
2383         int ret = MM_ERROR_NONE;
2384         debug_fenter();
2385
2386         ret = mm_sound_proxy_remove_test_callback(subs_id);
2387
2388         debug_fleave();
2389         return ret;
2390 }
2391
2392
2393 int mm_sound_client_test(int a, int b, int* getv)
2394 {
2395         int ret = MM_ERROR_NONE;
2396
2397         debug_fenter();
2398
2399         ret = mm_sound_proxy_test(a, b, getv);
2400         debug_log("%d * %d -> result : %d", a, b, *getv);
2401
2402         debug_fleave();
2403
2404         return ret;
2405 }