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