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