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