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