4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Seungbae Shin <seungbae.shin@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
33 #include <semaphore.h>
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"
45 #include <mm_session.h>
46 #include <mm_session_private.h>
49 #if defined(__GSOURCE_CALLBACK__)
53 #define CLIENT_HANDLE_MAX 256
55 #define FOCUS_HANDLE_MAX 512
56 #define FOCUS_HANDLE_INIT_VAL -1
57 #define CONFIG_ENABLE_RETCB
59 #define VOLUME_TYPE_LEN 64
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;
68 struct callback_data {
75 #define GET_CB_DATA(_cb_data, _func, _userdata, _extradata) \
77 _cb_data = (struct callback_data*) g_malloc0(sizeof(struct callback_data)); \
78 _cb_data->user_cb = _func; \
79 _cb_data->user_data = _userdata; \
80 _cb_data->extra_data = _extradata; \
88 GSourceFuncs* g_src_funcs;
94 mm_sound_focus_changed_cb focus_callback;
95 mm_sound_focus_changed_watch_cb watch_callback;
98 bool is_for_session; /* will be removed when the session concept is completely left out*/
106 char stream_type[MAX_STREAM_TYPE_LEN];
107 char ext_info[MM_SOUND_NAME_NUM];
112 mm_sound_focus_session_interrupt_cb user_cb;
114 } focus_session_interrupt_info_t;
116 typedef gboolean (*focus_gLoopPollHandler_t)(gpointer d);
118 GThread *g_focus_thread;
119 GMainLoop *g_focus_loop;
120 focus_sound_info_t g_focus_sound_handle[FOCUS_HANDLE_MAX];
121 focus_session_interrupt_info_t g_focus_session_interrupt_info = {NULL, NULL};
122 static pthread_mutex_t g_index_mutex = PTHREAD_MUTEX_INITIALIZER;
123 guint g_focus_signal_handle = 0;
126 gboolean g_need_emergent_exit = FALSE;
129 /* handle to watch end of playing */
131 /* subscription id to unsubscribe when handle ended */
133 } play_sound_end_callback_data_t;
135 void _system_signal_handler(int signo)
137 int ret = MM_ERROR_NONE;
138 sigset_t old_mask, all_mask;
140 debug_warning("Got signal : signo(%d)", signo);
144 sigfillset(&all_mask);
145 sigprocmask(SIG_BLOCK, &all_mask, &old_mask);
147 if (g_need_emergent_exit) {
148 ret = mm_sound_proxy_emergent_exit(getpid());
149 if (ret == MM_ERROR_NONE)
150 debug_msg("[Client] Success to emergnet_exit\n");
152 debug_error("[Client] Error occurred : 0x%x \n",ret);
155 sigprocmask(SIG_SETMASK, &old_mask, NULL);
160 sigaction(SIGINT, &system_int_old_action, NULL);
164 sigaction(SIGABRT, &system_abrt_old_action, NULL);
168 sigaction(SIGSEGV, &system_segv_old_action, NULL);
172 sigaction(SIGTERM, &system_term_old_action, NULL);
176 sigaction(SIGSYS, &system_sys_old_action, NULL);
180 sigaction(SIGXCPU, &system_xcpu_old_action, NULL);
187 debug_warning("signal handling end");
190 int mm_sound_client_initialize(void)
192 int ret = MM_ERROR_NONE;
195 mm_sound_proxy_initialize();
198 struct sigaction system_action;
199 system_action.sa_handler = _system_signal_handler;
200 system_action.sa_flags = SA_NOCLDSTOP;
202 sigemptyset(&system_action.sa_mask);
204 sigaction(SIGINT, &system_action, &system_int_old_action);
205 sigaction(SIGABRT, &system_action, &system_abrt_old_action);
206 sigaction(SIGSEGV, &system_action, &system_segv_old_action);
207 sigaction(SIGTERM, &system_action, &system_term_old_action);
208 sigaction(SIGSYS, &system_action, &system_sys_old_action);
209 sigaction(SIGXCPU, &system_action, &system_xcpu_old_action);
215 int mm_sound_client_finalize(void)
217 int ret = MM_ERROR_NONE;
221 if (g_need_emergent_exit) {
222 ret = mm_sound_proxy_emergent_exit(getpid());
223 if (ret == MM_ERROR_NONE)
224 debug_msg("[Client] Success to emergnet_exit\n");
226 debug_error("[Client] Error occurred : 0x%x \n",ret);
229 sigaction(SIGINT, &system_int_old_action, NULL);
230 sigaction(SIGABRT, &system_abrt_old_action, NULL);
231 sigaction(SIGSEGV, &system_segv_old_action, NULL);
232 sigaction(SIGTERM, &system_term_old_action, NULL);
233 sigaction(SIGSYS, &system_sys_old_action, NULL);
234 sigaction(SIGXCPU, &system_xcpu_old_action, NULL);
236 ret = mm_sound_proxy_finalize();
241 if (g_focus_thread) {
242 g_main_loop_quit(g_focus_loop);
243 g_thread_join(g_focus_thread);
244 debug_log("after thread join");
245 g_main_loop_unref(g_focus_loop);
246 g_focus_thread = NULL;
255 void mm_sound_convert_volume_type_to_stream_type(int volume_type, char *stream_type)
257 switch (volume_type) {
258 case VOLUME_TYPE_SYSTEM:
259 MMSOUND_STRNCPY(stream_type, "system", MM_SOUND_STREAM_TYPE_LEN);
261 case VOLUME_TYPE_NOTIFICATION:
262 MMSOUND_STRNCPY(stream_type, "notification", MM_SOUND_STREAM_TYPE_LEN);
264 case VOLUME_TYPE_ALARM:
265 MMSOUND_STRNCPY(stream_type, "alarm", MM_SOUND_STREAM_TYPE_LEN);
267 case VOLUME_TYPE_RINGTONE:
268 MMSOUND_STRNCPY(stream_type, "ringtone-voip", MM_SOUND_STREAM_TYPE_LEN);
270 case VOLUME_TYPE_MEDIA:
271 MMSOUND_STRNCPY(stream_type, "media", MM_SOUND_STREAM_TYPE_LEN);
273 case VOLUME_TYPE_CALL:
274 MMSOUND_STRNCPY(stream_type, "call-voice", MM_SOUND_STREAM_TYPE_LEN);
276 case VOLUME_TYPE_VOIP:
277 MMSOUND_STRNCPY(stream_type, "voip", MM_SOUND_STREAM_TYPE_LEN);
279 case VOLUME_TYPE_VOICE:
280 MMSOUND_STRNCPY(stream_type, "voice-recognition", MM_SOUND_STREAM_TYPE_LEN);
283 MMSOUND_STRNCPY(stream_type, "media", MM_SOUND_STREAM_TYPE_LEN);
287 debug_msg("volume type (%d) converted to stream type (%s)", volume_type, stream_type);
291 /*****************************************************************************************
292 DBUS SUPPORTED FUNCTIONS
293 ******************************************************************************************/
295 void _mm_sound_client_focus_signal_callback(mm_sound_signal_name_t signal, int value, void *user_data)
297 int ret = MM_ERROR_NONE;
300 debug_msg("focus signal received, value = %d", value);
303 ret = mm_sound_proxy_clear_focus(getpid());
305 debug_error("clear focus failed ret = 0x%x", ret);
306 mm_sound_unsubscribe_signal(g_focus_signal_handle);
307 g_focus_signal_handle = 0;
312 int mm_sound_client_play_tone(int number, int volume_config, double volume, int time, int *handle, bool enable_session)
314 int ret = MM_ERROR_NONE;
315 // int instance = -1; /* instance is unique to communicate with server : client message queue filter type */
316 int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(volume_config);
317 char stream_type[MM_SOUND_STREAM_TYPE_LEN] = {0, };
321 /* read session information */
322 int session_type = MM_SESSION_TYPE_MEDIA;
323 int session_options = 0;
324 int is_focus_registered = 0;
326 ret = mm_sound_get_signal_value(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &is_focus_registered);
328 debug_error("mm_sound_get_signal_value failed [0x%x]", ret);
329 return MM_ERROR_POLICY_INTERNAL;
332 if (is_focus_registered)
333 enable_session = false;
335 if (enable_session) {
336 if (MM_ERROR_NONE != _mm_session_util_read_information(-1, &session_type, &session_options)) {
337 debug_warning("[Client] Read Session Information failed. use default \"media\" type\n");
338 session_type = MM_SESSION_TYPE_MEDIA;
340 if(MM_ERROR_NONE != mm_session_init(session_type)) {
341 debug_critical("[Client] MMSessionInit() failed\n");
342 return MM_ERROR_POLICY_INTERNAL;
347 // instance = getpid();
348 //debug_log("[Client] pid for client ::: [%d]\n", instance);
351 debug_msg("[Client] Input number : %d\n", number);
352 /* Send req memory */
354 mm_sound_convert_volume_type_to_stream_type(volume_type, stream_type);
355 ret = mm_sound_proxy_play_tone(number, time, volume, volume_config,
356 session_type, session_options, getpid(), enable_session, handle, stream_type, -1);
358 if (enable_session && !g_focus_signal_handle) {
359 ret = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &g_focus_signal_handle, _mm_sound_client_focus_signal_callback, NULL);
361 debug_error("mm_sound_subscribe_signal failed [0x%x]", ret);
362 return MM_ERROR_POLICY_INTERNAL;
371 int mm_sound_client_play_tone_with_stream_info(int tone, char *stream_type, int stream_id, double volume, int duration, int *handle)
373 int ret = MM_ERROR_NONE;
377 ret = mm_sound_proxy_play_tone_with_stream_info(getpid(), tone, stream_type, stream_id, volume, duration, handle);
383 static void _mm_sound_stop_callback_wrapper_func(int ended_handle, void *userdata)
385 struct callback_data *cb_data = (struct callback_data*) userdata;
386 play_sound_end_callback_data_t *end_cb_data;
388 debug_log("[Wrapper CB][Play Stop] ended_handle : %d", ended_handle);
390 if (cb_data == NULL) {
391 debug_warning("stop callback data null");
395 end_cb_data = (play_sound_end_callback_data_t*) cb_data->extra_data;
397 if (ended_handle == end_cb_data->watching_handle) {
398 debug_log("Interested playing handle end : %d", ended_handle);
399 ((mm_sound_stop_callback_func)(cb_data->user_cb))(cb_data->user_data, ended_handle);
400 if (mm_sound_proxy_remove_play_sound_end_callback(end_cb_data->subs_id) != MM_ERROR_NONE)
401 debug_error("mm_sound_client_dbus_remove_play_file_end_callback failed");
403 debug_log("Not interested playing handle : %d", ended_handle);
407 static void play_end_callback_data_free_func(void *data)
409 struct callback_data *cb_data = (struct callback_data*) data;
412 if (cb_data->extra_data)
413 g_free(cb_data->extra_data);
418 int mm_sound_client_play_sound(MMSoundPlayParam *param, int tone, int *handle)
420 int ret = MM_ERROR_NONE;
421 int session_type = MM_SESSION_TYPE_MEDIA;
422 int session_options = 0;
423 int is_focus_registered = 0;
424 // int instance = -1; /* instance is unique to communicate with server : client message queue filter type */
425 int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(param->volume_config);
426 char stream_type[MM_SOUND_STREAM_TYPE_LEN] = {0, };
427 struct callback_data *cb_data = NULL;
428 play_sound_end_callback_data_t *end_cb_data;
432 /* read session information */
434 ret = mm_sound_get_signal_value(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &is_focus_registered);
436 debug_error("mm_sound_get_signal_value failed [0x%x]", ret);
437 return MM_ERROR_POLICY_INTERNAL;
440 if (is_focus_registered)
441 param->skip_session = true;
443 if (param->skip_session == false) {
444 if(MM_ERROR_NONE != _mm_session_util_read_information(-1, &session_type, &session_options)) {
445 debug_warning("[Client] Read MMSession Type failed. use default \"media\" type\n");
446 session_type = MM_SESSION_TYPE_MEDIA;
448 if(MM_ERROR_NONE != mm_session_init(session_type)) {
449 debug_critical("[Client] MMSessionInit() failed\n");
450 return MM_ERROR_POLICY_INTERNAL;
456 if ((param->mem_ptr && param->mem_size)) {
457 // Play memory, deprecated
458 return MM_ERROR_INVALID_ARGUMENT;
461 mm_sound_convert_volume_type_to_stream_type(volume_type, stream_type);
462 ret = mm_sound_proxy_play_sound(param->filename, tone, param->loop, param->volume, param->volume_config,
463 param->priority, session_type, session_options, getpid(), param->handle_route,
464 !param->skip_session, handle, stream_type, -1);
465 if (ret != MM_ERROR_NONE) {
466 debug_error("Play Sound Failed");
469 if (param->callback) {
470 end_cb_data = (play_sound_end_callback_data_t *) g_malloc0(sizeof(play_sound_end_callback_data_t));
471 end_cb_data->watching_handle = *handle;
472 GET_CB_DATA(cb_data, param->callback, param->data, end_cb_data);
474 ret = mm_sound_proxy_add_play_sound_end_callback(_mm_sound_stop_callback_wrapper_func, cb_data, play_end_callback_data_free_func, &end_cb_data->subs_id);
475 if (ret != MM_ERROR_NONE) {
476 debug_error("Add callback for play sound(%d) Failed", *handle);
480 if (!param->skip_session && !g_focus_signal_handle) {
481 ret = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &g_focus_signal_handle, _mm_sound_client_focus_signal_callback, NULL);
483 debug_error("mm_sound_subscribe_signal failed [0x%x]", ret);
484 return MM_ERROR_POLICY_INTERNAL;
495 int mm_sound_client_play_sound_with_stream_info(MMSoundPlayParam *param, int *handle, char* stream_type, int stream_id)
497 int ret = MM_ERROR_NONE;
498 struct callback_data *cb_data = NULL;
499 play_sound_end_callback_data_t *end_cb_data;
501 ret = mm_sound_proxy_play_sound_with_stream_info(param->filename, param->loop, param->volume,
502 param->priority, getpid(), param->handle_route, handle, stream_type, stream_id);
503 if (ret != MM_ERROR_NONE) {
504 debug_error("Play Sound Failed");
507 if (param->callback) {
508 end_cb_data = (play_sound_end_callback_data_t *) g_malloc0(sizeof(play_sound_end_callback_data_t));
509 end_cb_data->watching_handle = *handle;
510 GET_CB_DATA(cb_data, param->callback, param->data, end_cb_data);
512 ret = mm_sound_proxy_add_play_sound_end_callback(_mm_sound_stop_callback_wrapper_func, cb_data, play_end_callback_data_free_func, &end_cb_data->subs_id);
513 if (ret != MM_ERROR_NONE) {
514 debug_error("Add callback for play sound(%d) Failed", *handle);
525 int mm_sound_client_stop_sound(int handle)
527 int ret = MM_ERROR_NONE;
530 if (handle < 0 || handle > CLIENT_HANDLE_MAX) {
531 ret = MM_ERROR_INVALID_ARGUMENT;
535 ret = mm_sound_proxy_stop_sound(handle);
541 static int _mm_sound_client_device_list_dump (GList *device_list)
543 int ret = MM_ERROR_NONE;
545 mm_sound_device_t *device_node = NULL;
548 debug_error("Device list NULL, cannot dump list");
549 return MM_ERROR_SOUND_INTERNAL;
552 debug_log("======================== device list : start ==========================\n");
553 for (list = device_list; list != NULL; list = list->next) {
554 device_node = (mm_sound_device_t *)list->data;
556 debug_log(" list idx[%d]: type[%17s], id[%02d], io_direction[%d], state[%d], name[%s]\n",
557 count++, device_node->type, device_node->id, device_node->io_direction, device_node->state, device_node->name);
560 debug_log("======================== device list : end ============================\n");
565 int mm_sound_client_get_current_connected_device_list(int device_flags, mm_sound_device_list_t *device_list)
567 int ret = MM_ERROR_NONE;
571 debug_error("Device list NULL");
572 ret = MM_ERROR_COMMON_INVALID_ARGUMENT;
576 if ((ret = mm_sound_proxy_get_current_connected_device_list(device_flags, &device_list->list)) != MM_ERROR_NONE) {
577 debug_error("[Client] failed to get current connected device list with dbus, ret[0x%x]", ret);
580 if (!device_list->list) {
581 debug_error("Got device list null");
582 ret = MM_ERROR_SOUND_NO_DATA;
585 _mm_sound_client_device_list_dump(device_list->list);
592 static bool device_is_match_direction(int direction, int mask)
594 if (mask == DEVICE_IO_DIRECTION_FLAGS || mask == 0)
597 if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_IN_FLAG) && (direction & MM_SOUND_DEVICE_IO_DIRECTION_IN))
599 if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_OUT_FLAG) && (direction & MM_SOUND_DEVICE_IO_DIRECTION_OUT))
601 if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_BOTH_FLAG) && (direction & MM_SOUND_DEVICE_IO_DIRECTION_BOTH))
607 static bool device_is_match_state(int state, int mask)
609 if (mask == DEVICE_STATE_FLAGS || mask == 0)
612 if ((mask & MM_SOUND_DEVICE_STATE_DEACTIVATED_FLAG) && (state == MM_SOUND_DEVICE_STATE_DEACTIVATED))
614 if ((mask & MM_SOUND_DEVICE_STATE_ACTIVATED_FLAG) && (state == MM_SOUND_DEVICE_STATE_ACTIVATED))
620 static bool device_is_match_type(const char *type, int mask)
623 const char *builtin_prefix = "builtin";
625 if (mask == DEVICE_TYPE_FLAGS || mask == 0)
628 is_builtin = !strncmp(type, builtin_prefix, strlen(builtin_prefix));
630 if ((mask & MM_SOUND_DEVICE_TYPE_INTERNAL_FLAG) && (is_builtin))
632 if ((mask & MM_SOUND_DEVICE_TYPE_EXTERNAL_FLAG) && (!is_builtin))
638 static bool device_is_match_with_mask(const char *type, int direction, int state, int mask)
640 if (mask == DEVICE_ALL_FLAG)
643 return (device_is_match_direction(direction, mask & DEVICE_IO_DIRECTION_FLAGS) &&
644 device_is_match_state(state, mask & DEVICE_STATE_FLAGS) &&
645 device_is_match_type(type, mask & DEVICE_TYPE_FLAGS));
648 static int _fill_sound_device(mm_sound_device_t *device_h, int device_id, const char *device_type,
649 int direction, int state, const char *name, int *stream_id, int stream_num)
653 if (stream_num > 0 && stream_id == NULL) {
654 debug_error("stream_num is %d, but stream_id is NULL", stream_num);
658 if (stream_num > MAX_STREAM_ON_DEVICE) {
659 debug_error("too many streams on this device");
663 device_h->id = device_id;
664 device_h->io_direction = direction;
665 device_h->state = state;
666 MMSOUND_STRNCPY(device_h->name, name, MAX_DEVICE_NAME_NUM);
667 MMSOUND_STRNCPY(device_h->type, device_type, MAX_DEVICE_TYPE_STR_LEN);
669 if (stream_num > 0) {
670 device_h->stream_num = stream_num;
671 debug_log("%d streams on this device", stream_num);
672 for (i = 0; i < stream_num; i++) {
673 debug_log(" stream_id : %d", stream_id[i]);
674 device_h->stream_id[i] = stream_id[i];
677 device_h->stream_num = 0;
678 debug_log("There is no stream on this device");
684 static void _mm_sound_device_connected_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
685 int state, const char *name, int *stream_id, int stream_num, gboolean is_connected, void *userdata)
687 mm_sound_device_t device_h;
688 struct callback_data *cb_data = (struct callback_data*) userdata;
691 debug_log("[Device %s] id(%d) type(%s) direction(%d) state(%d) name(%s)",
692 is_connected ? "Connected" : "Disconnected", device_id, device_type, io_direction,
693 state, name, is_connected);
695 if (cb_data == NULL) {
696 debug_warning("device connected changed callback data null");
700 device_flags = (int) cb_data->extra_data;
701 if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
704 if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name, stream_id, stream_num) < 0) {
705 debug_error("Failed to fill sound device");
709 ((mm_sound_device_connected_cb)(cb_data->user_cb))(&device_h, is_connected, cb_data->user_data);
712 int mm_sound_client_add_device_connected_callback(int device_flags, mm_sound_device_connected_cb func, void* userdata, unsigned int *subs_id)
714 int ret = MM_ERROR_NONE;
715 struct callback_data *cb_data = NULL;
719 GET_CB_DATA(cb_data, func, userdata, (void*) device_flags);
721 ret = mm_sound_proxy_add_device_connected_callback(device_flags, _mm_sound_device_connected_callback_wrapper_func, cb_data, g_free, subs_id);
722 if (ret == MM_ERROR_NONE)
723 g_need_emergent_exit = TRUE;
729 int mm_sound_client_remove_device_connected_callback(unsigned int subs_id)
731 int ret = MM_ERROR_NONE;
734 ret = mm_sound_proxy_remove_device_connected_callback(subs_id);
740 static void _mm_sound_device_info_changed_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
741 int state, const char *name, int *stream_id, int stream_num, int changed_device_info_type, void *userdata)
743 mm_sound_device_t device_h;
744 struct callback_data *cb_data = (struct callback_data*) userdata;
747 debug_log("[Device Info Changed] id(%d) type(%s) direction(%d) state(%d) name(%s) changed_info_type(%d)",
748 device_id, device_type, io_direction, state, name, changed_device_info_type);
750 if (cb_data == NULL) {
751 debug_warning("device info changed callback data null");
755 device_flags = (int) cb_data->extra_data;
756 if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
759 if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name, stream_id, stream_num) < 0) {
760 debug_error("Failed to fill sound device");
764 ((mm_sound_device_info_changed_cb)(cb_data->user_cb))(&device_h, changed_device_info_type, cb_data->user_data);
767 int mm_sound_client_add_device_info_changed_callback(int device_flags, mm_sound_device_info_changed_cb func, void *userdata, unsigned int *subs_id)
769 int ret = MM_ERROR_NONE;
770 struct callback_data *cb_data = (struct callback_data*) userdata;
774 GET_CB_DATA(cb_data, func, userdata, (void *) device_flags);
776 ret = mm_sound_proxy_add_device_info_changed_callback(device_flags, _mm_sound_device_info_changed_callback_wrapper_func, cb_data, g_free, subs_id);
782 int mm_sound_client_remove_device_info_changed_callback(unsigned int subs_id)
784 int ret = MM_ERROR_NONE;
787 ret = mm_sound_proxy_remove_device_info_changed_callback(subs_id);
794 static void _mm_sound_device_state_changed_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
795 int state, const char *name, int *stream_id, int stream_num, void *userdata)
797 mm_sound_device_t device_h;
798 struct callback_data *cb_data = (struct callback_data*) userdata;
801 debug_log("[Device State Changed] id(%d) type(%s) direction(%d) state(%d) name(%s)",
802 device_id, device_type, io_direction, state, name);
804 if (cb_data == NULL) {
805 debug_warning("device state changed callback data null");
809 device_flags = (int) cb_data->extra_data;
811 if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
814 if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name, stream_id, stream_num) < 0) {
815 debug_error("Failed to fill sound device");
819 ((mm_sound_device_state_changed_cb)(cb_data->user_cb))(&device_h, state, cb_data->user_data);
822 int mm_sound_client_add_device_state_changed_callback(int device_flags, mm_sound_device_state_changed_cb func, void *userdata, unsigned int *id)
824 int ret = MM_ERROR_NONE;
825 struct callback_data *cb_data = (struct callback_data*) userdata;
829 GET_CB_DATA(cb_data, func, userdata, (void *) device_flags);
831 ret = mm_sound_proxy_add_device_state_changed_callback(device_flags, _mm_sound_device_state_changed_callback_wrapper_func, cb_data, g_free, id);
837 int mm_sound_client_remove_device_state_changed_callback(unsigned int id)
839 int ret = MM_ERROR_NONE;
842 ret = mm_sound_proxy_remove_device_state_changed_callback(id);
848 int mm_sound_client_is_stream_on_device(int stream_id, int device_id, bool *is_on)
850 int ret = MM_ERROR_NONE;
854 debug_error("Invalid Parameter");
855 ret = MM_ERROR_COMMON_INVALID_ARGUMENT;
859 if ((ret = mm_sound_proxy_is_stream_on_device(stream_id, device_id, is_on)) != MM_ERROR_NONE) {
860 debug_error("[Client] failed to query is stream on device, ret[0x%x]", ret);
869 int __convert_volume_type_to_str(int volume_type, char **volume_type_str)
871 int ret = MM_ERROR_NONE;
873 if (!volume_type_str) {
874 return MM_ERROR_COMMON_INVALID_ARGUMENT;
877 switch (volume_type) {
878 case VOLUME_TYPE_SYSTEM:
879 *volume_type_str = "system";
881 case VOLUME_TYPE_NOTIFICATION:
882 *volume_type_str = "notification";
884 case VOLUME_TYPE_ALARM:
885 *volume_type_str = "alarm";
887 case VOLUME_TYPE_RINGTONE:
888 *volume_type_str = "ringtone";
890 case VOLUME_TYPE_MEDIA:
891 *volume_type_str = "media";
893 case VOLUME_TYPE_CALL:
894 *volume_type_str = "call";
896 case VOLUME_TYPE_VOIP:
897 *volume_type_str = "voip";
899 case VOLUME_TYPE_VOICE:
900 *volume_type_str = "voice";
903 if (!strncmp(*volume_type_str,"", VOLUME_TYPE_LEN)) {
904 debug_error("could not find the volume_type[%d] in this switch case statement", volume_type);
905 ret = MM_ERROR_SOUND_INTERNAL;
907 debug_log("volume_type[%s]", *volume_type_str);
912 static int __convert_volume_type_to_int(const char *volume_type_str, volume_type_t *volume_type)
914 int ret = MM_ERROR_NONE;
916 if (!volume_type || !volume_type_str) {
917 return MM_ERROR_COMMON_INVALID_ARGUMENT;
920 if (!strncmp(volume_type_str, "system", VOLUME_TYPE_LEN)) {
921 *volume_type = VOLUME_TYPE_SYSTEM;
922 } else if (!strncmp(volume_type_str, "notification", VOLUME_TYPE_LEN)) {
923 *volume_type = VOLUME_TYPE_NOTIFICATION;
924 } else if (!strncmp(volume_type_str, "alarm", VOLUME_TYPE_LEN)) {
925 *volume_type = VOLUME_TYPE_ALARM;
926 } else if (!strncmp(volume_type_str, "ringtone", VOLUME_TYPE_LEN)) {
927 *volume_type = VOLUME_TYPE_RINGTONE;
928 } else if (!strncmp(volume_type_str, "media", VOLUME_TYPE_LEN)) {
929 *volume_type = VOLUME_TYPE_MEDIA;
930 } else if (!strncmp(volume_type_str, "call", VOLUME_TYPE_LEN)) {
931 *volume_type = VOLUME_TYPE_CALL;
932 } else if (!strncmp(volume_type_str, "voip", VOLUME_TYPE_LEN)) {
933 *volume_type = VOLUME_TYPE_VOIP;
934 } else if (!strncmp(volume_type_str, "voice", VOLUME_TYPE_LEN)) {
935 *volume_type = VOLUME_TYPE_VOICE;
937 debug_log("Invalid volume type : [%s]", volume_type_str);
938 ret = MM_ERROR_SOUND_INTERNAL;
944 int mm_sound_client_set_volume_by_type(const int volume_type, const unsigned int volume_level)
946 int ret = MM_ERROR_NONE;
947 char *type_str = NULL;
950 if ((ret = __convert_volume_type_to_str(volume_type, &type_str)) != MM_ERROR_NONE) {
951 debug_error("volume type convert failed");
955 ret = mm_sound_proxy_set_volume_by_type(type_str, volume_level);
962 static void _mm_sound_volume_changed_callback_wrapper_func(const char *direction, const char *volume_type_str, int volume_level, void *userdata)
964 volume_type_t volume_type = 0;
965 struct callback_data *cb_data = (struct callback_data *) userdata;
967 debug_log("[Wrapper CB][Volume Changed] direction : %s, volume_type : %s, volume_level : %d", direction, volume_type_str, volume_level);
969 if (cb_data == NULL) {
970 debug_warning("volume changed callback data null");
974 if (__convert_volume_type_to_int(volume_type_str, &volume_type) != MM_ERROR_NONE) {
975 debug_error("volume type convert failed");
978 debug_log("Call volume changed user cb, direction : %s, vol_type : %s(%d), level : %u", direction, volume_type_str, volume_type, volume_level);
979 ((mm_sound_volume_changed_cb)(cb_data->user_cb))(volume_type, volume_level, cb_data->user_data);
982 int mm_sound_client_add_volume_changed_callback(mm_sound_volume_changed_cb func, void* userdata, unsigned int *subs_id)
984 int ret = MM_ERROR_NONE;
985 struct callback_data *cb_data = NULL;
989 GET_CB_DATA(cb_data, func, userdata, NULL);
991 ret = mm_sound_proxy_add_volume_changed_callback(_mm_sound_volume_changed_callback_wrapper_func, cb_data, g_free, subs_id);
998 int mm_sound_client_remove_volume_changed_callback(unsigned int subs_id)
1000 int ret = MM_ERROR_NONE;
1003 ret = mm_sound_proxy_remove_volume_changed_callback(subs_id);
1010 int mm_sound_client_set_session_interrupt_callback(mm_sound_focus_session_interrupt_cb callback, void* user_data)
1012 int ret = MM_ERROR_NONE;
1017 return MM_ERROR_INVALID_ARGUMENT;
1019 g_focus_session_interrupt_info.user_cb = callback;
1020 g_focus_session_interrupt_info.user_data = user_data;
1026 int mm_sound_client_unset_session_interrupt_callback(void)
1028 int ret = MM_ERROR_NONE;
1032 if (!g_focus_session_interrupt_info.user_cb) {
1033 debug_error("no callback to unset");
1034 return MM_ERROR_SOUND_INTERNAL;
1037 g_focus_session_interrupt_info.user_cb = NULL;
1038 g_focus_session_interrupt_info.user_data = NULL;
1044 static gpointer _focus_thread_func(gpointer data)
1046 debug_log(">>> thread func..ID of this thread(%u)\n", (unsigned int)pthread_self());
1047 g_main_loop_run(g_focus_loop);
1048 debug_log("<<< quit thread func..\n");
1052 static gboolean _focus_fd_check(GSource * source)
1058 debug_error("GSource is null");
1061 fd_list = source->poll_fds;
1063 debug_error("fd_list is null");
1067 temp = (GPollFD*)fd_list->data;
1069 debug_error("fd_list->data is null");
1072 if (temp->revents & (POLLIN | POLLPRI)) {
1075 fd_list = fd_list->next;
1078 return FALSE; /* there is no change in any fd state */
1081 static gboolean _focus_fd_prepare(GSource *source, gint *timeout)
1086 static gboolean _focus_fd_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
1088 callback(user_data);
1092 static int _focus_find_index_by_handle(int handle)
1095 for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1096 if (g_focus_sound_handle[i].focus_callback && handle == g_focus_sound_handle[i].handle) {
1097 /* debug_msg("found index(%d) for handle(%d)", i, handle);*/
1098 return (handle == FOCUS_HANDLE_INIT_VAL)? -1 : i;
1104 static int _focus_watch_find_index_by_handle(int handle)
1107 for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1108 if (g_focus_sound_handle[i].watch_callback && handle == g_focus_sound_handle[i].handle) {
1109 /* debug_msg("found index(%d) for watch handle(%d)", i, handle);*/
1110 return (handle == FOCUS_HANDLE_INIT_VAL)? -1 : i;
1116 static gboolean _focus_callback_handler(gpointer d)
1118 GPollFD *data = (GPollFD*)d;
1121 int focus_index = 0;
1122 focus_cb_data_lib cb_data;
1123 debug_log(">>> focus_callback_handler()..ID of this thread(%u)\n", (unsigned int)pthread_self());
1125 memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1128 debug_error("GPollFd is null");
1131 if (data->revents & (POLLIN | POLLPRI)) {
1132 int changed_state = -1;
1134 count = read(data->fd, &cb_data, sizeof(cb_data));
1136 char str_error[256];
1137 strerror_r(errno, str_error, sizeof(str_error));
1138 debug_error("GpollFD read fail, errno=%d(%s)",errno, str_error);
1141 changed_state = cb_data.state;
1142 focus_index = _focus_find_index_by_handle(cb_data.handle);
1143 if (focus_index == -1) {
1144 debug_error("Could not find index");
1148 g_mutex_lock(&g_focus_sound_handle[focus_index].focus_lock);
1150 tid = g_focus_sound_handle[focus_index].focus_tid;
1152 if (changed_state != -1) {
1153 debug_msg("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)", tid, cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type);
1154 if (g_focus_sound_handle[focus_index].focus_callback == NULL) {
1155 debug_error("callback is null..");
1156 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1159 debug_msg("[CALLBACK(%p) START]",g_focus_sound_handle[focus_index].focus_callback);
1160 (g_focus_sound_handle[focus_index].focus_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type, cb_data.option, cb_data.ext_info, g_focus_sound_handle[focus_index].user_data);
1161 debug_msg("[CALLBACK END]");
1162 if (g_focus_session_interrupt_info.user_cb) {
1163 debug_msg("sending session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
1164 (g_focus_session_interrupt_info.user_cb)(cb_data.state, cb_data.stream_type, false, g_focus_session_interrupt_info.user_data);
1167 #ifdef CONFIG_ENABLE_RETCB
1171 unsigned int buf = 0;
1172 char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", g_focus_sound_handle[focus_index].focus_tid, cb_data.handle);
1173 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1175 char str_error[256];
1176 strerror_r(errno, str_error, sizeof(str_error));
1177 debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n", tid, tmpfd, filename2, errno, str_error);
1179 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1182 /* buf contains data as below,
1183 * |<--12bits--><--4bits (reacquisition)--><--16bits (handle)-->| */
1184 buf = (unsigned int)((0x0000ffff & cb_data.handle) | (g_focus_sound_handle[focus_index].auto_reacquire << 16));
1185 rett = write(tmpfd, &buf, sizeof(buf));
1188 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
1193 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1198 static gboolean _focus_watch_callback_handler(gpointer d)
1200 GPollFD *data = (GPollFD*)d;
1203 int focus_index = 0;
1204 focus_cb_data_lib cb_data;
1208 memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1211 debug_error("GPollFd is null");
1214 if (data->revents & (POLLIN | POLLPRI)) {
1215 count = read(data->fd, &cb_data, sizeof(cb_data));
1217 char str_error[256];
1218 strerror_r(errno, str_error, sizeof(str_error));
1219 debug_error("GpollFD read fail, errno=%d(%s)",errno, str_error);
1223 focus_index = _focus_watch_find_index_by_handle(cb_data.handle);
1224 if (focus_index == -1) {
1225 debug_error("Could not find index");
1229 debug_msg("lock focus_lock = %p", &g_focus_sound_handle[focus_index].focus_lock);
1230 g_mutex_lock(&g_focus_sound_handle[focus_index].focus_lock);
1232 tid = g_focus_sound_handle[focus_index].focus_tid;
1234 debug_msg("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)", tid, cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type);
1236 if (g_focus_sound_handle[focus_index].watch_callback == NULL) {
1237 debug_msg("callback is null..");
1239 debug_msg("[CALLBACK(%p) START]",g_focus_sound_handle[focus_index].watch_callback);
1240 (g_focus_sound_handle[focus_index].watch_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type, cb_data.ext_info, g_focus_sound_handle[focus_index].user_data);
1241 debug_msg("[CALLBACK END]");
1242 if (g_focus_session_interrupt_info.user_cb) {
1243 debug_msg("sending session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
1244 (g_focus_session_interrupt_info.user_cb)(cb_data.state, cb_data.stream_type, true, g_focus_session_interrupt_info.user_data);
1248 #ifdef CONFIG_ENABLE_RETCB
1253 char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr", g_focus_sound_handle[focus_index].focus_tid, cb_data.handle);
1254 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1256 char str_error[256];
1257 strerror_r(errno, str_error, sizeof(str_error));
1258 debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n", tid, tmpfd, filename2, errno, str_error);
1260 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1263 buf = cb_data.handle;
1264 rett = write(tmpfd, &buf, sizeof(buf));
1267 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
1272 debug_msg("unlock focus_lock = %p", &g_focus_sound_handle[focus_index].focus_lock);
1273 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1281 static void _focus_open_callback(int index, bool is_for_watching)
1288 if (is_for_watching) {
1289 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1291 filename = g_strdup_printf("/tmp/FOCUS.%d.%d", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1293 pre_mask = umask(0);
1294 if (mknod(filename, S_IFIFO|0666, 0)) {
1295 debug_error("mknod() failure, errno(%d)", errno);
1298 g_focus_sound_handle[index].focus_fd = open(filename, O_RDWR|O_NONBLOCK);
1299 if (g_focus_sound_handle[index].focus_fd == -1) {
1300 debug_error("Open fail : index(%d), file open error(%d)", index, errno);
1302 debug_log("Open success : index(%d), filename(%s), fd(%d)", index, filename, g_focus_sound_handle[index].focus_fd);
1307 #ifdef CONFIG_ENABLE_RETCB
1310 if (is_for_watching) {
1311 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1313 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1315 pre_mask = umask(0);
1316 if (mknod(filename2, S_IFIFO | 0666, 0)) {
1317 debug_error("mknod() failure, errno(%d)", errno);
1327 void _focus_close_callback(int index, bool is_for_watching)
1329 char *filename = NULL;
1333 if (g_focus_sound_handle[index].focus_fd < 0) {
1334 debug_error("Close fail : index(%d)", index);
1336 close(g_focus_sound_handle[index].focus_fd);
1337 debug_log("Close Success : index(%d)", index);
1340 if (is_for_watching) {
1341 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1343 filename = g_strdup_printf("/tmp/FOCUS.%d.%d", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1345 if (remove(filename)) {
1346 debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)", filename, errno);
1351 #ifdef CONFIG_ENABLE_RETCB
1354 if (is_for_watching) {
1355 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1357 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1359 if (remove(filename2)) {
1360 debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)", filename2, errno);
1370 static bool _focus_add_sound_callback(int index, int fd, gushort events, focus_gLoopPollHandler_t p_gloop_poll_handler )
1372 GSource* g_src = NULL;
1373 GSourceFuncs *g_src_funcs = NULL; /* handler function */
1375 GPollFD *g_poll_fd = NULL; /* file descriptor */
1379 g_mutex_init(&g_focus_sound_handle[index].focus_lock);
1381 /* 1. make GSource Object */
1382 g_src_funcs = (GSourceFuncs *)g_malloc(sizeof(GSourceFuncs));
1384 debug_error("failed to g_malloc for g_src_funcs");
1388 g_src_funcs->prepare = _focus_fd_prepare;
1389 g_src_funcs->check = _focus_fd_check;
1390 g_src_funcs->dispatch = _focus_fd_dispatch;
1391 g_src_funcs->finalize = NULL;
1392 g_src = g_source_new(g_src_funcs, sizeof(GSource));
1394 debug_error("failed to g_source_new for g_src");
1398 /* 2. add file description which used in g_loop() */
1399 g_poll_fd = (GPollFD *)g_malloc(sizeof(GPollFD));
1401 debug_error("failed to g_malloc for g_poll_fd");
1405 g_poll_fd->events = events;
1407 /* 3. combine g_source object and file descriptor */
1408 g_source_add_poll(g_src, g_poll_fd);
1409 g_src_id = g_source_attach(g_src, g_main_loop_get_context(g_focus_loop));
1411 debug_error("failed to attach the source to context");
1415 /* 4. set callback */
1416 g_source_set_callback(g_src, p_gloop_poll_handler,(gpointer)g_poll_fd, NULL);
1418 debug_log("g_malloc : g_src_funcs(%p), g_poll_fd(%p)", g_src_funcs, g_poll_fd);
1420 /* 5. store to global handle */
1421 g_focus_sound_handle[index].focus_src = g_src;
1422 g_focus_sound_handle[index].g_src_funcs = g_src_funcs;
1423 g_focus_sound_handle[index].g_poll_fd = g_poll_fd;
1430 g_free(g_src_funcs);
1434 g_source_unref(g_src);
1439 static bool _focus_remove_sound_callback(int index, gushort events)
1445 g_mutex_clear(&g_focus_sound_handle[index].focus_lock);
1447 GSourceFuncs *g_src_funcs = g_focus_sound_handle[index].g_src_funcs;
1448 GPollFD *g_poll_fd = g_focus_sound_handle[index].g_poll_fd; /* store file descriptor */
1450 debug_error("g_poll_fd is null..");
1454 g_poll_fd->fd = g_focus_sound_handle[index].focus_fd;
1455 g_poll_fd->events = events;
1457 if (!g_focus_sound_handle[index].focus_src) {
1458 debug_error("g_focus_sound_handle[%d].focus_src is null..", index);
1462 g_source_remove_poll(g_focus_sound_handle[index].focus_src, g_poll_fd);
1463 debug_log("g_source_remove_poll : fd(%d), event(%x)", g_poll_fd->fd, g_poll_fd->events);
1466 if (g_focus_sound_handle[index].focus_src)
1467 g_source_destroy(g_focus_sound_handle[index].focus_src);
1469 debug_log("g_free : g_src_funcs(%x), g_poll_fd(%x)", g_src_funcs, g_poll_fd);
1471 g_free(g_src_funcs);
1479 g_focus_sound_handle[index].g_src_funcs = NULL;
1480 g_focus_sound_handle[index].g_poll_fd = NULL;
1481 g_focus_sound_handle[index].focus_src = NULL;
1482 g_focus_sound_handle[index].focus_callback = NULL;
1483 g_focus_sound_handle[index].watch_callback = NULL;
1490 static void _focus_add_callback(int index, bool is_for_watching)
1493 if (!is_for_watching) {
1494 if (!_focus_add_sound_callback(index, g_focus_sound_handle[index].focus_fd, (gushort)POLLIN | POLLPRI, _focus_callback_handler)) {
1495 debug_error("failed to _focus_add_sound_callback()");
1498 } else { // need to check if it's necessary
1499 if (!_focus_add_sound_callback(index, g_focus_sound_handle[index].focus_fd, (gushort)POLLIN | POLLPRI, _focus_watch_callback_handler)) {
1500 debug_error("failed to _focus_add_sound_callback()");
1507 static void _focus_remove_callback(int index)
1510 if (!_focus_remove_sound_callback(index, (gushort)POLLIN | POLLPRI)) {
1511 debug_error("failed to __focus_remove_sound_callback()");
1517 static void _focus_init_callback(int index, bool is_for_watching)
1520 _focus_open_callback(index, is_for_watching);
1521 _focus_add_callback(index, is_for_watching);
1525 static void _focus_destroy_callback(int index, bool is_for_watching)
1528 _focus_remove_callback(index);
1529 _focus_close_callback(index, is_for_watching);
1533 int mm_sound_client_get_unique_id(int *id)
1535 int ret = MM_ERROR_NONE;
1540 ret = MM_ERROR_INVALID_ARGUMENT;
1542 ret = mm_sound_proxy_get_unique_id(id);
1549 int mm_sound_client_is_focus_cb_thread(GThread *mine, bool *result)
1551 int ret = MM_ERROR_NONE;
1553 if (!mine || !result)
1554 ret = MM_ERROR_INVALID_ARGUMENT;
1556 if (mine == g_focus_thread)
1565 int mm_sound_client_register_focus(int id, int pid, const char *stream_type, mm_sound_focus_changed_cb callback, bool is_for_session, void* user_data)
1567 int ret = MM_ERROR_NONE;
1572 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1576 for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
1577 if (g_focus_sound_handle[index].is_used == false) {
1578 g_focus_sound_handle[index].is_used = true;
1583 g_focus_sound_handle[index].focus_tid = instance;
1584 g_focus_sound_handle[index].handle = id;
1585 g_focus_sound_handle[index].focus_callback = callback;
1586 g_focus_sound_handle[index].user_data = user_data;
1587 g_focus_sound_handle[index].is_for_session = is_for_session;
1588 g_focus_sound_handle[index].auto_reacquire = true;
1590 ret = mm_sound_proxy_register_focus(id, pid, stream_type, callback, is_for_session, user_data);
1592 if (ret == MM_ERROR_NONE) {
1593 debug_msg("[Client] Success to register focus\n");
1594 g_need_emergent_exit = TRUE;
1595 if (!g_focus_thread) {
1596 GMainContext* focus_context = g_main_context_new ();
1597 g_focus_loop = g_main_loop_new (focus_context, FALSE);
1598 g_main_context_unref(focus_context);
1599 g_focus_thread = g_thread_new("focus-callback-thread", _focus_thread_func, NULL);
1600 if (g_focus_thread == NULL) {
1601 debug_error ("could not create thread..");
1602 g_main_loop_unref(g_focus_loop);
1603 g_focus_sound_handle[index].is_used = false;
1604 ret = MM_ERROR_SOUND_INTERNAL;
1609 debug_error("[Client] Error occurred : 0x%x \n",ret);
1610 g_focus_sound_handle[index].is_used = false;
1614 _focus_init_callback(index, false);
1617 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1623 int mm_sound_client_unregister_focus(int id)
1625 int ret = MM_ERROR_NONE;
1630 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1632 index = _focus_find_index_by_handle(id);
1634 debug_error("Could not find index");
1635 ret = MM_ERROR_INVALID_ARGUMENT;
1638 instance = g_focus_sound_handle[index].focus_tid;
1640 if (!g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1641 debug_warning("maybe focus_callback is being called, try one more time..");
1642 usleep(2500000); // 2.5 sec
1643 if (g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1644 debug_msg("finally got focus_lock");
1648 ret = mm_sound_proxy_unregister_focus(instance, id, g_focus_sound_handle[index].is_for_session);
1650 if (ret == MM_ERROR_NONE)
1651 debug_msg("[Client] Success to unregister focus\n");
1653 debug_error("[Client] Error occurred : 0x%x \n",ret);
1655 g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
1657 _focus_destroy_callback(index, false);
1658 g_focus_sound_handle[index].focus_fd = 0;
1659 g_focus_sound_handle[index].focus_tid = 0;
1660 g_focus_sound_handle[index].handle = 0;
1661 g_focus_sound_handle[index].is_used = false;
1663 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1668 int mm_sound_client_set_focus_reacquisition(int id, bool reacquisition)
1670 int ret = MM_ERROR_NONE;
1676 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1678 index = _focus_find_index_by_handle(id);
1680 debug_error("Could not find index");
1681 ret = MM_ERROR_INVALID_ARGUMENT;
1684 instance = g_focus_sound_handle[index].focus_tid;
1686 ret = mm_sound_client_is_focus_cb_thread(g_thread_self(), &result);
1688 debug_error("[Client] mm_sound_client_is_focus_cb_thread failed");
1690 } else if (!result) {
1691 ret = mm_sound_proxy_set_foucs_reacquisition(instance, id, reacquisition);
1692 if (ret == MM_ERROR_NONE) {
1693 debug_msg("[Client] Success to set focus reacquisition\n");
1695 debug_error("[Client] Error occurred : 0x%x \n",ret);
1699 debug_warning("[Client] Inside the focus cb thread, bypassing dbus method call");
1702 g_focus_sound_handle[index].auto_reacquire = reacquisition;
1705 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1710 int mm_sound_client_get_focus_reacquisition(int id, bool *reacquisition)
1712 int ret = MM_ERROR_NONE;
1717 if (!reacquisition) {
1718 debug_error("Invalid parameter");
1719 return MM_ERROR_INVALID_ARGUMENT;
1722 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1724 index = _focus_find_index_by_handle(id);
1726 debug_error("Could not find index");
1727 ret = MM_ERROR_INVALID_ARGUMENT;
1731 *reacquisition = g_focus_sound_handle[index].auto_reacquire;
1734 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1739 int mm_sound_client_get_acquired_focus_stream_type(int focus_type, char **stream_type, int *option, char **ext_info)
1741 int ret = MM_ERROR_NONE;
1742 char *ext_str = NULL;
1746 ret = mm_sound_proxy_get_acquired_focus_stream_type(focus_type, stream_type, option, &ext_str);
1747 if (ret == MM_ERROR_NONE) {
1748 debug_msg("[Client] Success to get stream type of acquired focus, stream_type(%s), ext_info(%s)\n", *stream_type, ext_str);
1749 *ext_info = strdup(ext_str);
1752 debug_error("[Client] Error occurred : 0x%x \n",ret);
1759 int mm_sound_client_acquire_focus(int id, mm_sound_focus_type_e type, int option, const char *ext_info)
1761 int ret = MM_ERROR_NONE;
1766 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1768 index = _focus_find_index_by_handle(id);
1770 debug_error("Could not find index");
1771 ret = MM_ERROR_INVALID_ARGUMENT;
1774 instance = g_focus_sound_handle[index].focus_tid;
1776 ret = mm_sound_proxy_acquire_focus(instance, id, type, option, ext_info, g_focus_sound_handle[index].is_for_session);
1777 if (ret == MM_ERROR_NONE)
1778 debug_msg("[Client] Success to acquire focus\n");
1780 debug_error("[Client] Error occurred : 0x%x \n",ret);
1783 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1788 int mm_sound_client_release_focus(int id, mm_sound_focus_type_e type, int option, const char *ext_info)
1790 int ret = MM_ERROR_NONE;
1795 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1797 index = _focus_find_index_by_handle(id);
1799 debug_error("Could not find index");
1800 ret = MM_ERROR_INVALID_ARGUMENT;
1803 instance = g_focus_sound_handle[index].focus_tid;
1805 ret = mm_sound_proxy_release_focus(instance, id, type, option, ext_info, g_focus_sound_handle[index].is_for_session);
1806 if (ret == MM_ERROR_NONE)
1807 debug_msg("[Client] Success to release focus\n");
1809 debug_error("[Client] Error occurred : 0x%x \n",ret);
1812 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1817 int mm_sound_client_set_focus_watch_callback(int pid, mm_sound_focus_type_e focus_type, mm_sound_focus_changed_watch_cb callback, bool is_for_session, void* user_data, int *id)
1819 int ret = MM_ERROR_NONE;
1826 return MM_ERROR_INVALID_ARGUMENT;
1828 //pthread_mutex_lock(&g_thread_mutex2);
1832 ret = mm_sound_proxy_get_unique_id(id);
1836 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1838 for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
1839 if (g_focus_sound_handle[index].is_used == false) {
1840 g_focus_sound_handle[index].is_used = true;
1845 g_focus_sound_handle[index].focus_tid = instance;
1846 g_focus_sound_handle[index].handle = *id;
1847 g_focus_sound_handle[index].watch_callback = callback;
1848 g_focus_sound_handle[index].user_data = user_data;
1849 g_focus_sound_handle[index].is_for_session = is_for_session;
1851 ret = mm_sound_proxy_set_focus_watch_callback(pid, g_focus_sound_handle[index].handle, focus_type, callback, is_for_session, user_data);
1853 if (ret == MM_ERROR_NONE) {
1854 debug_msg("[Client] Success to watch focus");
1855 g_need_emergent_exit = TRUE;
1856 if (!g_focus_thread) {
1857 GMainContext* focus_context = g_main_context_new ();
1858 g_focus_loop = g_main_loop_new (focus_context, FALSE);
1859 g_main_context_unref(focus_context);
1860 g_focus_thread = g_thread_new("focus-callback-thread", _focus_thread_func, NULL);
1861 if (g_focus_thread == NULL) {
1862 debug_error ("could not create thread..");
1863 g_main_loop_unref(g_focus_loop);
1864 ret = MM_ERROR_SOUND_INTERNAL;
1869 debug_error("[Client] Error occurred : 0x%x",ret);
1873 _focus_init_callback(index, true);
1878 g_focus_sound_handle[index].is_used = false;
1881 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1886 int mm_sound_client_unset_focus_watch_callback(int id)
1888 int ret = MM_ERROR_NONE;
1891 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1893 index = _focus_watch_find_index_by_handle(id);
1895 debug_error("Could not find index");
1896 ret = MM_ERROR_INVALID_ARGUMENT;
1900 g_mutex_lock(&g_focus_sound_handle[index].focus_lock);
1902 ret = mm_sound_proxy_unset_focus_watch_callback(g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle, g_focus_sound_handle[index].is_for_session);
1904 if (ret == MM_ERROR_NONE)
1905 debug_msg("[Client] Success to unwatch focus\n");
1907 debug_error("[Client] Error occurred : 0x%x \n",ret);
1910 g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
1912 _focus_destroy_callback(index, true);
1913 g_focus_sound_handle[index].focus_fd = 0;
1914 g_focus_sound_handle[index].focus_tid = 0;
1915 g_focus_sound_handle[index].handle = 0;
1916 g_focus_sound_handle[index].is_used = false;
1918 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1925 int mm_sound_client_add_test_callback(mm_sound_test_cb func, void* user_data, unsigned int *subs_id)
1927 int ret = MM_ERROR_NONE;
1931 ret = mm_sound_proxy_add_test_callback(func, user_data, g_free, subs_id);
1937 int mm_sound_client_remove_test_callback(unsigned int subs_id)
1939 int ret = MM_ERROR_NONE;
1942 ret = mm_sound_proxy_remove_test_callback(subs_id);
1949 int mm_sound_client_test(int a, int b, int* getv)
1951 int ret = MM_ERROR_NONE;
1955 ret = mm_sound_proxy_test(a, b, getv);
1956 debug_log("%d * %d -> result : %d", a, b, *getv);