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