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