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