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