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 name[MM_SOUND_NAME_NUM];
111 mm_sound_focus_session_interrupt_cb user_cb;
113 } focus_session_interrupt_info_t;
116 /* handle to watch end of playing */
118 /* subscription id to unsubscribe when handle ended */
120 } play_sound_end_callback_data_t;
122 typedef gboolean (*focus_gLoopPollHandler_t)(gpointer d);
124 GThread *g_focus_thread;
125 GMainLoop *g_focus_loop;
126 focus_sound_info_t g_focus_sound_handle[FOCUS_HANDLE_MAX];
127 focus_session_interrupt_info_t g_focus_session_interrupt_info = {NULL, NULL};
129 /* global variables for device list */
130 static pthread_mutex_t g_id_mutex = PTHREAD_MUTEX_INITIALIZER;
132 guint g_focus_signal_handle = 0;
133 gboolean g_need_emergent_exit = FALSE;
135 void _system_signal_handler(int signo)
137 int ret = MM_ERROR_NONE;
138 sigset_t old_mask, all_mask;
140 debug_error("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_error("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_error("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_error("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;
311 int mm_sound_client_play_tone(int number, int volume_config, double volume, int time, int *handle, bool enable_session)
313 int ret = MM_ERROR_NONE;
314 // int instance = -1; /* instance is unique to communicate with server : client message queue filter type */
315 int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(volume_config);
316 char stream_type[MM_SOUND_STREAM_TYPE_LEN] = {0, };
320 /* read session information */
321 int session_type = MM_SESSION_TYPE_MEDIA;
322 int session_options = 0;
323 int is_focus_registered = 0;
325 ret = mm_sound_get_signal_value(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &is_focus_registered);
327 debug_error("mm_sound_get_signal_value failed [0x%x]", ret);
328 return MM_ERROR_POLICY_INTERNAL;
331 if (is_focus_registered)
332 enable_session = false;
334 if (enable_session) {
335 if (MM_ERROR_NONE != _mm_session_util_read_information(-1, &session_type, &session_options)) {
336 debug_warning("[Client] Read Session Information failed. use default \"media\" type\n");
337 session_type = MM_SESSION_TYPE_MEDIA;
339 if(MM_ERROR_NONE != mm_session_init(session_type)) {
340 debug_critical("[Client] MMSessionInit() failed\n");
341 return MM_ERROR_POLICY_INTERNAL;
346 // instance = getpid();
347 //debug_log("[Client] pid for client ::: [%d]\n", instance);
350 debug_msg("[Client] Input number : %d\n", number);
351 /* Send req memory */
353 mm_sound_convert_volume_type_to_stream_type(volume_type, stream_type);
354 ret = mm_sound_proxy_play_tone(number, time, volume, volume_config,
355 session_type, session_options, getpid(), enable_session, handle, stream_type, -1);
357 if (enable_session && !g_focus_signal_handle) {
358 ret = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &g_focus_signal_handle, _mm_sound_client_focus_signal_callback, NULL);
360 debug_error("mm_sound_subscribe_signal failed [0x%x]", ret);
361 return MM_ERROR_POLICY_INTERNAL;
369 int mm_sound_client_play_tone_with_stream_info(int tone, char *stream_type, int stream_id, double volume, int duration, int *handle)
371 int ret = MM_ERROR_NONE;
375 ret = mm_sound_proxy_play_tone_with_stream_info(getpid(), tone, stream_type, stream_id, volume, duration, handle);
381 static void _mm_sound_stop_callback_wrapper_func(int ended_handle, void *userdata)
383 struct callback_data *cb_data = (struct callback_data*) userdata;
384 play_sound_end_callback_data_t *end_cb_data = (play_sound_end_callback_data_t*) cb_data->extra_data;
386 debug_log("[Wrapper CB][Play Stop] ended_handle : %d", ended_handle);
388 if (cb_data == NULL) {
389 debug_warning("stop callback data null");
392 if (ended_handle == end_cb_data->watching_handle) {
393 debug_log("Interested playing handle end : %d", ended_handle);
394 ((mm_sound_stop_callback_func)(cb_data->user_cb))(cb_data->user_data, ended_handle);
395 if (mm_sound_proxy_remove_play_sound_end_callback(end_cb_data->subs_id) != MM_ERROR_NONE)
396 debug_error("mm_sound_client_dbus_remove_play_file_end_callback failed");
398 debug_log("Not interested playing handle : %d", ended_handle);
402 static void play_end_callback_data_free_func(void *data)
404 struct callback_data *cb_data = (struct callback_data*) data;
405 play_sound_end_callback_data_t *end_cb_data = (play_sound_end_callback_data_t*) cb_data->extra_data;
410 int mm_sound_client_play_sound(MMSoundPlayParam *param, int tone, int *handle)
412 int ret = MM_ERROR_NONE;
413 int session_type = MM_SESSION_TYPE_MEDIA;
414 int session_options = 0;
415 int is_focus_registered = 0;
416 // int instance = -1; /* instance is unique to communicate with server : client message queue filter type */
417 int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(param->volume_config);
418 char stream_type[MM_SOUND_STREAM_TYPE_LEN] = {0, };
419 struct callback_data *cb_data = NULL;
420 play_sound_end_callback_data_t *end_cb_data;
424 /* read session information */
426 ret = mm_sound_get_signal_value(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &is_focus_registered);
428 debug_error("mm_sound_get_signal_value failed [0x%x]", ret);
429 return MM_ERROR_POLICY_INTERNAL;
432 if (is_focus_registered)
433 param->skip_session = true;
435 if (param->skip_session == false) {
436 if(MM_ERROR_NONE != _mm_session_util_read_information(-1, &session_type, &session_options)) {
437 debug_warning("[Client] Read MMSession Type failed. use default \"media\" type\n");
438 session_type = MM_SESSION_TYPE_MEDIA;
440 if(MM_ERROR_NONE != mm_session_init(session_type)) {
441 debug_critical("[Client] MMSessionInit() failed\n");
442 return MM_ERROR_POLICY_INTERNAL;
448 if ((param->mem_ptr && param->mem_size)) {
449 // Play memory, deprecated
450 return MM_ERROR_INVALID_ARGUMENT;
453 mm_sound_convert_volume_type_to_stream_type(volume_type, stream_type);
454 ret = mm_sound_proxy_play_sound(param->filename, tone, param->loop, param->volume, param->volume_config,
455 param->priority, session_type, session_options, getpid(), param->handle_route,
456 !param->skip_session, handle, stream_type, -1);
457 if (ret != MM_ERROR_NONE) {
458 debug_error("Play Sound Failed");
461 if (param->callback) {
462 end_cb_data = (play_sound_end_callback_data_t *) g_malloc0(sizeof(play_sound_end_callback_data_t));
463 end_cb_data->watching_handle = *handle;
464 GET_CB_DATA(cb_data, param->callback, param->data, end_cb_data);
466 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);
467 if (ret != MM_ERROR_NONE) {
468 debug_error("Add callback for play sound(%d) Failed", *handle);
471 if (!param->skip_session && !g_focus_signal_handle) {
472 ret = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &g_focus_signal_handle, _mm_sound_client_focus_signal_callback, NULL);
474 debug_error("mm_sound_subscribe_signal failed [0x%x]", ret);
475 return MM_ERROR_POLICY_INTERNAL;
485 int mm_sound_client_play_sound_with_stream_info(MMSoundPlayParam *param, int *handle, char* stream_type, int stream_id)
487 int ret = MM_ERROR_NONE;
488 struct callback_data *cb_data = NULL;
489 play_sound_end_callback_data_t *end_cb_data;
491 ret = mm_sound_proxy_play_sound_with_stream_info(param->filename, param->loop, param->volume,
492 param->priority, getpid(), param->handle_route, handle, stream_type, stream_id);
493 if (ret != MM_ERROR_NONE) {
494 debug_error("Play Sound Failed");
497 if (param->callback) {
498 end_cb_data = (play_sound_end_callback_data_t *) g_malloc0(sizeof(play_sound_end_callback_data_t));
499 end_cb_data->watching_handle = *handle;
500 GET_CB_DATA(cb_data, param->callback, param->data, end_cb_data);
502 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);
503 if (ret != MM_ERROR_NONE) {
504 debug_error("Add callback for play sound(%d) Failed", *handle);
515 int mm_sound_client_stop_sound(int handle)
517 int ret = MM_ERROR_NONE;
520 if (handle < 0 || handle > CLIENT_HANDLE_MAX) {
521 ret = MM_ERROR_INVALID_ARGUMENT;
525 ret = mm_sound_proxy_stop_sound(handle);
531 static int _mm_sound_client_device_list_dump (GList *device_list)
533 int ret = MM_ERROR_NONE;
535 mm_sound_device_t *device_node = NULL;
538 debug_error("Device list NULL, cannot dump list");
539 return MM_ERROR_SOUND_INTERNAL;
542 debug_log("======================== device list : start ==========================\n");
543 for (list = device_list; list != NULL; list = list->next) {
544 device_node = (mm_sound_device_t *)list->data;
546 debug_log(" list idx[%d]: type[%17s], id[%02d], io_direction[%d], state[%d], name[%s]\n",
547 count++, device_node->type, device_node->id, device_node->io_direction, device_node->state, device_node->name);
550 debug_log("======================== device list : end ============================\n");
555 int mm_sound_client_get_current_connected_device_list(int device_flags, mm_sound_device_list_t *device_list)
557 int ret = MM_ERROR_NONE;
561 debug_error("Device list NULL");
562 ret = MM_ERROR_COMMON_INVALID_ARGUMENT;
566 if ((ret = mm_sound_proxy_get_current_connected_device_list(device_flags, &device_list->list)) != MM_ERROR_NONE) {
567 debug_error("[Client] failed to get current connected device list with dbus, ret[0x%x]", ret);
570 if (!device_list->list) {
571 debug_error("Got device list null");
572 ret = MM_ERROR_SOUND_NO_DATA;
575 _mm_sound_client_device_list_dump(device_list->list);
582 static bool is_device_match_flags(const char *device_type, int io_direction, int state, int device_flags)
584 int io_direction_flag = device_flags & DEVICE_IO_DIRECTION_FLAGS;
585 int state_flag = device_flags & DEVICE_STATE_FLAGS;
586 int type_flag = device_flags & DEVICE_TYPE_FLAGS;
587 char builtin_prefix[] = "builtin";
589 if (device_flags == DEVICE_ALL_FLAG)
592 switch (io_direction_flag) {
593 case DEVICE_IO_DIRECTION_IN_FLAG:
594 if (io_direction != DEVICE_IO_DIRECTION_IN)
597 case DEVICE_IO_DIRECTION_OUT_FLAG:
598 if (io_direction != DEVICE_IO_DIRECTION_OUT)
601 case DEVICE_IO_DIRECTION_BOTH_FLAG:
602 if (io_direction != DEVICE_IO_DIRECTION_BOTH)
609 switch (state_flag) {
610 case DEVICE_STATE_DEACTIVATED_FLAG:
611 if (state != DEVICE_STATE_DEACTIVATED)
614 case DEVICE_STATE_ACTIVATED_FLAG:
615 if (state != DEVICE_STATE_ACTIVATED)
623 case DEVICE_TYPE_INTERNAL_FLAG:
624 if (strncmp(device_type, builtin_prefix, strlen(builtin_prefix)) != 0)
627 case DEVICE_TYPE_EXTERNAL_FLAG:
628 if (strncmp(device_type, builtin_prefix, strlen(builtin_prefix)) == 0)
638 static void _mm_sound_device_connected_callback_wrapper_func(int device_id, const char *device_type, int io_direction, int state, const char *name, gboolean is_connected, void *userdata)
640 mm_sound_device_t device_h;
641 struct callback_data *cb_data = (struct callback_data*) userdata;
642 int device_flags = (int) cb_data->extra_data;
644 debug_log("[Wrapper CB][Device Connnected] device_id : %d, device_type : %s, direction : %d, state : %d, name : %s, is_connected : %d",
645 device_id, device_type, io_direction, state, name, is_connected);
647 if (cb_data == NULL) {
648 debug_warning("device connected changed callback data null");
651 if (!is_device_match_flags(device_type, io_direction, state, device_flags))
654 device_h.id = device_id;
655 device_h.io_direction = io_direction;
656 device_h.state = state;
657 MMSOUND_STRNCPY(device_h.name, name, MAX_DEVICE_NAME_NUM);
658 MMSOUND_STRNCPY(device_h.type, device_type, MAX_DEVICE_TYPE_STR_LEN);
660 ((mm_sound_device_connected_cb)(cb_data->user_cb))(&device_h, is_connected, cb_data->user_data);
663 int mm_sound_client_add_device_connected_callback(int device_flags, mm_sound_device_connected_cb func, void* userdata, unsigned int *subs_id)
665 int ret = MM_ERROR_NONE;
666 struct callback_data *cb_data = NULL;
670 GET_CB_DATA(cb_data, func, userdata, (void*) device_flags);
672 ret = mm_sound_proxy_add_device_connected_callback(device_flags, _mm_sound_device_connected_callback_wrapper_func, cb_data, g_free, subs_id);
673 if (ret != MM_ERROR_NONE)
674 g_need_emergent_exit = TRUE;
680 int mm_sound_client_remove_device_connected_callback(unsigned int subs_id)
682 int ret = MM_ERROR_NONE;
685 ret = mm_sound_proxy_remove_device_connected_callback(subs_id);
691 static void _mm_sound_device_info_changed_callback_wrapper_func(int device_id, const char *device_type, int io_direction, int state, const char *name, int changed_device_info_type, void *userdata)
693 mm_sound_device_t device_h;
694 struct callback_data *cb_data = (struct callback_data*) userdata;
695 int device_flags = (int) cb_data->extra_data;
697 debug_log("[Wrapper CB][Device Info Changed] device_id : %d, device_type : %s, direction : %d, state : %d, name : %s, changed_info_type : %d",
698 device_id, device_type, io_direction, state, name, changed_device_info_type);
700 if (cb_data == NULL) {
701 debug_warning("device info changed callback data null");
704 if (!is_device_match_flags(device_type, io_direction, state, device_flags))
707 device_h.id = device_id;
708 device_h.io_direction = io_direction;
709 device_h.state = state;
710 MMSOUND_STRNCPY(device_h.name, name, MAX_DEVICE_NAME_NUM);
711 MMSOUND_STRNCPY(device_h.type, device_type, MAX_DEVICE_TYPE_STR_LEN);
713 ((mm_sound_device_info_changed_cb)(cb_data->user_cb))(&device_h, changed_device_info_type, cb_data->user_data);
716 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)
718 int ret = MM_ERROR_NONE;
719 struct callback_data *cb_data = (struct callback_data*) userdata;
723 GET_CB_DATA(cb_data, func, userdata, (void *) device_flags);
725 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);
731 int mm_sound_client_remove_device_info_changed_callback(unsigned int subs_id)
733 int ret = MM_ERROR_NONE;
736 ret = mm_sound_proxy_remove_device_info_changed_callback(subs_id);
743 int __convert_volume_type_to_str(int volume_type, char **volume_type_str)
745 int ret = MM_ERROR_NONE;
747 if (!volume_type_str) {
748 return MM_ERROR_COMMON_INVALID_ARGUMENT;
751 switch (volume_type) {
752 case VOLUME_TYPE_SYSTEM:
753 *volume_type_str = "system";
755 case VOLUME_TYPE_NOTIFICATION:
756 *volume_type_str = "notification";
758 case VOLUME_TYPE_ALARM:
759 *volume_type_str = "alarm";
761 case VOLUME_TYPE_RINGTONE:
762 *volume_type_str = "ringtone";
764 case VOLUME_TYPE_MEDIA:
765 *volume_type_str = "media";
767 case VOLUME_TYPE_CALL:
768 *volume_type_str = "call";
770 case VOLUME_TYPE_VOIP:
771 *volume_type_str = "voip";
773 case VOLUME_TYPE_VOICE:
774 *volume_type_str = "voice";
777 if (!strncmp(*volume_type_str,"", VOLUME_TYPE_LEN)) {
778 debug_error("could not find the volume_type[%d] in this switch case statement", volume_type);
779 ret = MM_ERROR_SOUND_INTERNAL;
781 debug_log("volume_type[%s]", *volume_type_str);
786 static int __convert_volume_type_to_int(const char *volume_type_str, volume_type_t *volume_type)
788 int ret = MM_ERROR_NONE;
790 if (!volume_type || !volume_type_str) {
791 return MM_ERROR_COMMON_INVALID_ARGUMENT;
794 if (!strncmp(volume_type_str, "system", VOLUME_TYPE_LEN)) {
795 *volume_type = VOLUME_TYPE_SYSTEM;
796 } else if (!strncmp(volume_type_str, "notification", VOLUME_TYPE_LEN)) {
797 *volume_type = VOLUME_TYPE_NOTIFICATION;
798 } else if (!strncmp(volume_type_str, "alarm", VOLUME_TYPE_LEN)) {
799 *volume_type = VOLUME_TYPE_ALARM;
800 } else if (!strncmp(volume_type_str, "ringtone", VOLUME_TYPE_LEN)) {
801 *volume_type = VOLUME_TYPE_RINGTONE;
802 } else if (!strncmp(volume_type_str, "media", VOLUME_TYPE_LEN)) {
803 *volume_type = VOLUME_TYPE_MEDIA;
804 } else if (!strncmp(volume_type_str, "call", VOLUME_TYPE_LEN)) {
805 *volume_type = VOLUME_TYPE_CALL;
806 } else if (!strncmp(volume_type_str, "voip", VOLUME_TYPE_LEN)) {
807 *volume_type = VOLUME_TYPE_VOIP;
808 } else if (!strncmp(volume_type_str, "voice", VOLUME_TYPE_LEN)) {
809 *volume_type = VOLUME_TYPE_VOICE;
811 debug_log("Invalid volume type : [%s]", volume_type_str);
812 ret = MM_ERROR_SOUND_INTERNAL;
818 int mm_sound_client_set_volume_by_type(const int volume_type, const unsigned int volume_level)
820 int ret = MM_ERROR_NONE;
821 char *type_str = NULL;
824 if ((ret = __convert_volume_type_to_str(volume_type, &type_str)) != MM_ERROR_NONE) {
825 debug_error("volume type convert failed");
829 ret = mm_sound_proxy_set_volume_by_type(type_str, volume_level);
836 static void _mm_sound_volume_changed_callback_wrapper_func(const char *direction, const char *volume_type_str, int volume_level, void *userdata)
838 volume_type_t volume_type = 0;
839 struct callback_data *cb_data = (struct callback_data *) userdata;
841 debug_log("[Wrapper CB][Volume Changed] direction : %s, volume_type : %s, volume_level : %d", direction, volume_type_str, volume_level);
843 if (cb_data == NULL) {
844 debug_warning("volume changed callback data null");
848 if (__convert_volume_type_to_int(volume_type_str, &volume_type) != MM_ERROR_NONE) {
849 debug_error("volume type convert failed");
852 debug_log("Call volume changed user cb, direction : %s, vol_type : %s(%d), level : %u", direction, volume_type_str, volume_type, volume_level);
853 ((mm_sound_volume_changed_cb)(cb_data->user_cb))(volume_type, volume_level, cb_data->user_data);
856 int mm_sound_client_add_volume_changed_callback(mm_sound_volume_changed_cb func, void* userdata, unsigned int *subs_id)
858 int ret = MM_ERROR_NONE;
859 struct callback_data *cb_data = NULL;
863 GET_CB_DATA(cb_data, func, userdata, NULL);
865 ret = mm_sound_proxy_add_volume_changed_callback(_mm_sound_volume_changed_callback_wrapper_func, cb_data, g_free, subs_id);
872 int mm_sound_client_remove_volume_changed_callback(unsigned int subs_id)
874 int ret = MM_ERROR_NONE;
877 ret = mm_sound_proxy_remove_volume_changed_callback(subs_id);
884 int mm_sound_client_set_session_interrupt_callback(mm_sound_focus_session_interrupt_cb callback, void* user_data)
886 int ret = MM_ERROR_NONE;
891 return MM_ERROR_INVALID_ARGUMENT;
893 g_focus_session_interrupt_info.user_cb = callback;
894 g_focus_session_interrupt_info.user_data = user_data;
900 int mm_sound_client_unset_session_interrupt_callback(void)
902 int ret = MM_ERROR_NONE;
906 if (!g_focus_session_interrupt_info.user_cb) {
907 debug_error("no callback to unset");
908 return MM_ERROR_SOUND_INTERNAL;
911 g_focus_session_interrupt_info.user_cb = NULL;
912 g_focus_session_interrupt_info.user_data = NULL;
918 static gpointer _focus_thread_func(gpointer data)
920 debug_log(">>> thread func..ID of this thread(%u)\n", (unsigned int)pthread_self());
921 g_main_loop_run(g_focus_loop);
922 debug_log("<<< quit thread func..\n");
926 static gboolean _focus_fd_check(GSource * source)
932 debug_error("GSource is null");
935 fd_list = source->poll_fds;
937 debug_error("fd_list is null");
941 temp = (GPollFD*)fd_list->data;
943 debug_error("fd_list->data is null");
946 if (temp->revents & (POLLIN | POLLPRI)) {
949 fd_list = fd_list->next;
952 return FALSE; /* there is no change in any fd state */
955 static gboolean _focus_fd_prepare(GSource *source, gint *timeout)
960 static gboolean _focus_fd_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
967 static int _focus_find_index_by_handle(int handle)
970 for(i = 0; i< FOCUS_HANDLE_MAX; i++) {
971 if (handle == g_focus_sound_handle[i].handle) {
972 //debug_msg("found index(%d) for handle(%d)", i, handle);
973 if (handle == FOCUS_HANDLE_INIT_VAL) {
982 static gboolean _focus_callback_handler(gpointer d)
984 GPollFD *data = (GPollFD*)d;
988 focus_cb_data_lib cb_data;
989 debug_log(">>> focus_callback_handler()..ID of this thread(%u)\n", (unsigned int)pthread_self());
991 memset(&cb_data, 0, sizeof(focus_cb_data_lib));
994 debug_error("GPollFd is null");
997 if (data->revents & (POLLIN | POLLPRI)) {
998 int changed_state = -1;
1000 count = read(data->fd, &cb_data, sizeof(cb_data));
1002 char str_error[256];
1003 strerror_r(errno, str_error, sizeof(str_error));
1004 debug_error("GpollFD read fail, errno=%d(%s)",errno, str_error);
1007 changed_state = cb_data.state;
1008 focus_index = _focus_find_index_by_handle(cb_data.handle);
1009 if (focus_index == -1) {
1010 debug_error("Could not find index");
1014 g_mutex_lock(&g_focus_sound_handle[focus_index].focus_lock);
1016 tid = g_focus_sound_handle[focus_index].focus_tid;
1018 if (changed_state != -1) {
1019 debug_error("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);
1020 if (g_focus_sound_handle[focus_index].focus_callback== NULL) {
1021 debug_error("callback is null..");
1022 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1025 debug_error("[CALLBACK(%p) START]",g_focus_sound_handle[focus_index].focus_callback);
1026 (g_focus_sound_handle[focus_index].focus_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type, cb_data.name, g_focus_sound_handle[focus_index].user_data);
1027 debug_error("[CALLBACK END]");
1028 if (g_focus_session_interrupt_info.user_cb) {
1029 debug_error("sending session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
1030 (g_focus_session_interrupt_info.user_cb)(cb_data.state, cb_data.stream_type, false, g_focus_session_interrupt_info.user_data);
1033 #ifdef CONFIG_ENABLE_RETCB
1037 unsigned int buf = 0;
1038 char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", g_focus_sound_handle[focus_index].focus_tid, cb_data.handle);
1039 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1041 char str_error[256];
1042 strerror_r(errno, str_error, sizeof(str_error));
1043 debug_error("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n", tid, tmpfd, filename2, errno, str_error);
1045 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1048 buf = (unsigned int)((0x0000ffff & cb_data.handle) |(g_focus_sound_handle[focus_index].auto_reacquire << 16));
1049 rett = write(tmpfd, &buf, sizeof(buf));
1052 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
1056 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1061 static gboolean _focus_watch_callback_handler( gpointer d)
1063 GPollFD *data = (GPollFD*)d;
1066 int focus_index = 0;
1067 focus_cb_data_lib cb_data;
1071 memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1074 debug_error("GPollFd is null");
1077 if (data->revents & (POLLIN | POLLPRI)) {
1078 count = read(data->fd, &cb_data, sizeof(cb_data));
1080 char str_error[256];
1081 strerror_r(errno, str_error, sizeof(str_error));
1082 debug_error("GpollFD read fail, errno=%d(%s)",errno, str_error);
1086 focus_index = _focus_find_index_by_handle(cb_data.handle);
1087 if (focus_index == -1) {
1088 debug_error("Could not find index");
1092 debug_error("lock focus_lock = %p", &g_focus_sound_handle[focus_index].focus_lock);
1093 g_mutex_lock(&g_focus_sound_handle[focus_index].focus_lock);
1095 tid = g_focus_sound_handle[focus_index].focus_tid;
1097 debug_error("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);
1099 if (g_focus_sound_handle[focus_index].watch_callback == NULL) {
1100 debug_msg("callback is null..");
1102 debug_msg("[CALLBACK(%p) START]",g_focus_sound_handle[focus_index].watch_callback);
1103 (g_focus_sound_handle[focus_index].watch_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type, cb_data.name, g_focus_sound_handle[focus_index].user_data);
1104 debug_msg("[CALLBACK END]");
1105 if (g_focus_session_interrupt_info.user_cb) {
1106 debug_error("sending session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
1107 (g_focus_session_interrupt_info.user_cb)(cb_data.state, cb_data.stream_type, true, g_focus_session_interrupt_info.user_data);
1111 #ifdef CONFIG_ENABLE_RETCB
1116 char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.wchr", g_focus_sound_handle[focus_index].focus_tid);
1117 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1119 char str_error[256];
1120 strerror_r(errno, str_error, sizeof(str_error));
1121 debug_error("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n", tid, tmpfd, filename2, errno, str_error);
1123 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1126 buf = cb_data.handle;
1127 rett = write(tmpfd, &buf, sizeof(buf));
1130 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
1136 debug_error("unlock focus_lock = %p", &g_focus_sound_handle[focus_index].focus_lock);
1137 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1145 static void _focus_open_callback(int index, bool is_for_watching)
1152 if (is_for_watching) {
1153 filename = g_strdup_printf("/tmp/FOCUS.%d.wch", g_focus_sound_handle[index].focus_tid);
1155 filename = g_strdup_printf("/tmp/FOCUS.%d.%d", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1157 pre_mask = umask(0);
1158 if (mknod(filename, S_IFIFO|0666, 0)) {
1159 debug_error("mknod() failure, errno(%d)", errno);
1162 g_focus_sound_handle[index].focus_fd = open( filename, O_RDWR|O_NONBLOCK);
1163 if (g_focus_sound_handle[index].focus_fd == -1) {
1164 debug_error("Open fail : index(%d), file open error(%d)", index, errno);
1166 debug_log("Open sucess : index(%d), filename(%s), fd(%d)", index, filename, g_focus_sound_handle[index].focus_fd);
1171 #ifdef CONFIG_ENABLE_RETCB
1174 if (is_for_watching) {
1175 filename2 = g_strdup_printf("/tmp/FOCUS.%d.wchr", g_focus_sound_handle[index].focus_tid);
1177 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1179 pre_mask = umask(0);
1180 if (mknod(filename2, S_IFIFO | 0666, 0)) {
1181 debug_error("mknod() failure, errno(%d)", errno);
1191 void _focus_close_callback(int index, bool is_for_watching)
1195 if (g_focus_sound_handle[index].focus_fd < 0) {
1196 debug_error("Close fail : fd error.");
1199 if (is_for_watching) {
1200 filename = g_strdup_printf("/tmp/FOCUS.%d.wch", g_focus_sound_handle[index].focus_tid);
1202 filename = g_strdup_printf("/tmp/FOCUS.%d.%d", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1204 close(g_focus_sound_handle[index].focus_fd);
1205 if (remove(filename)) {
1206 debug_error("remove() failure, filename(%s), errno(%d)", filename, errno);
1208 debug_log("Close Sucess : index(%d), filename(%s)", index, filename);
1213 #ifdef CONFIG_ENABLE_RETCB
1216 if (is_for_watching) {
1217 filename2 = g_strdup_printf("/tmp/FOCUS.%d.wchr", g_focus_sound_handle[index].focus_tid);
1219 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1222 /* Defensive code - wait until callback timeout although callback is removed */
1223 int buf = MM_ERROR_NONE; //no need to specify cb result to server, just notice if the client got the callback properly or not
1225 char str_error[256];
1227 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1229 strerror_r(errno, str_error, sizeof(str_error));
1230 debug_warning("could not open file(%s) (may server close it first), tid(%d) fd(%d) %s errno=%d(%s)",
1231 filename2, g_focus_sound_handle[index].focus_tid, tmpfd, filename2, errno, str_error);
1234 debug_msg("write MM_ERROR_NONE(tid:%d) for waiting server", g_focus_sound_handle[index].focus_tid);
1235 written = write(tmpfd, &buf, sizeof(buf));
1236 if (written == -1) {
1237 strerror_r(errno, str_error, sizeof(str_error));
1238 debug_warning("write failed, %s", str_error);
1243 if (remove(filename2)) {
1244 debug_error("remove() failure, filename(%s), errno(%d)", filename2, errno);
1252 static bool _focus_add_sound_callback(int index, int fd, gushort events, focus_gLoopPollHandler_t p_gloop_poll_handler )
1254 GSource* g_src = NULL;
1255 GSourceFuncs *g_src_funcs = NULL; /* handler function */
1256 guint gsource_handle;
1257 GPollFD *g_poll_fd = NULL; /* file descriptor */
1261 g_mutex_init(&g_focus_sound_handle[index].focus_lock);
1263 /* 1. make GSource Object */
1264 g_src_funcs = (GSourceFuncs *)g_malloc(sizeof(GSourceFuncs));
1266 debug_error("g_malloc failed on g_src_funcs");
1270 g_src_funcs->prepare = _focus_fd_prepare;
1271 g_src_funcs->check = _focus_fd_check;
1272 g_src_funcs->dispatch = _focus_fd_dispatch;
1273 g_src_funcs->finalize = NULL;
1274 g_src = g_source_new(g_src_funcs, sizeof(GSource));
1276 debug_error("g_malloc failed on m_readfd");
1279 g_focus_sound_handle[index].focus_src = g_src;
1280 g_focus_sound_handle[index].g_src_funcs = g_src_funcs;
1282 /* 2. add file description which used in g_loop() */
1283 g_poll_fd = (GPollFD *)g_malloc(sizeof(GPollFD));
1285 debug_error("g_malloc failed on g_poll_fd");
1289 g_poll_fd->events = events;
1290 g_focus_sound_handle[index].g_poll_fd = g_poll_fd;
1292 /* 3. combine g_source object and file descriptor */
1293 g_source_add_poll(g_src, g_poll_fd);
1294 gsource_handle = g_source_attach(g_src, g_main_loop_get_context(g_focus_loop));
1295 if (!gsource_handle) {
1296 debug_error(" Failed to attach the source to context");
1299 //g_source_unref(g_src);
1301 /* 4. set callback */
1302 g_source_set_callback(g_src, p_gloop_poll_handler,(gpointer)g_poll_fd, NULL);
1304 debug_log(" g_malloc:g_src_funcs(%#X),g_poll_fd(%#X) g_source_add_poll:g_src_id(%d) g_source_set_callback:errno(%d)",
1305 g_src_funcs, g_poll_fd, gsource_handle, errno);
1313 static bool _focus_remove_sound_callback(int index, gushort events)
1319 g_mutex_clear(&g_focus_sound_handle[index].focus_lock);
1321 GSourceFuncs *g_src_funcs = g_focus_sound_handle[index].g_src_funcs;
1322 GPollFD *g_poll_fd = g_focus_sound_handle[index].g_poll_fd; /* store file descriptor */
1324 debug_error("g_poll_fd is null..");
1328 g_poll_fd->fd = g_focus_sound_handle[index].focus_fd;
1329 g_poll_fd->events = events;
1331 if (!g_focus_sound_handle[index].focus_src) {
1332 debug_error("FOCUS_sound_handle[%d].focus_src is null..", index);
1335 debug_log(" g_source_remove_poll : fd(%d), event(%x), errno(%d)", g_poll_fd->fd, g_poll_fd->events, errno);
1336 g_source_remove_poll(g_focus_sound_handle[index].focus_src, g_poll_fd);
1340 if (g_focus_sound_handle[index].focus_src) {
1341 g_source_destroy(g_focus_sound_handle[index].focus_src);
1342 if (!g_source_is_destroyed (g_focus_sound_handle[index].focus_src)) {
1343 debug_warning(" failed to g_source_destroy(), focus_src(0x%p)", g_focus_sound_handle[index].focus_src);
1346 debug_log(" g_free : g_src_funcs(%#X), g_poll_fd(%#X)", g_src_funcs, g_poll_fd);
1349 g_free(g_src_funcs);
1357 g_focus_sound_handle[index].g_src_funcs = NULL;
1358 g_focus_sound_handle[index].g_poll_fd = NULL;
1359 g_focus_sound_handle[index].focus_src = NULL;
1360 g_focus_sound_handle[index].focus_callback = NULL;
1361 g_focus_sound_handle[index].watch_callback = NULL;
1368 static void _focus_add_callback(int index, bool is_for_watching)
1371 if (!is_for_watching) {
1372 if (!_focus_add_sound_callback(index, g_focus_sound_handle[index].focus_fd, (gushort)POLLIN | POLLPRI, _focus_callback_handler)) {
1373 debug_error("failed to _focus_add_sound_callback()");
1376 } else { // need to check if it's necessary
1377 if (!_focus_add_sound_callback(index, g_focus_sound_handle[index].focus_fd, (gushort)POLLIN | POLLPRI, _focus_watch_callback_handler)) {
1378 debug_error("failed to _focus_add_sound_callback()");
1385 static void _focus_remove_callback(int index)
1388 if (!_focus_remove_sound_callback(index, (gushort)POLLIN | POLLPRI)) {
1389 debug_error("failed to __focus_remove_sound_callback()");
1395 static void _focus_init_callback(int index, bool is_for_watching)
1398 _focus_open_callback(index, is_for_watching);
1399 _focus_add_callback(index, is_for_watching);
1403 static void _focus_destroy_callback(int index, bool is_for_watching)
1406 _focus_remove_callback(index);
1407 _focus_close_callback(index, is_for_watching);
1411 int mm_sound_client_get_unique_id(int *id)
1413 int ret = MM_ERROR_NONE;
1415 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_id_mutex, MM_ERROR_SOUND_INTERNAL);
1419 ret = MM_ERROR_INVALID_ARGUMENT;
1421 ret = mm_sound_proxy_get_unique_id(id);
1424 MMSOUND_LEAVE_CRITICAL_SECTION(&g_id_mutex);
1429 int mm_sound_client_is_focus_cb_thread(GThread *mine, bool *result)
1431 int ret = MM_ERROR_NONE;
1433 if (!mine || !result)
1434 ret = MM_ERROR_INVALID_ARGUMENT;
1436 if (mine == g_focus_thread)
1445 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)
1447 int ret = MM_ERROR_NONE;
1454 for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
1455 if (g_focus_sound_handle[index].is_used == false) {
1456 g_focus_sound_handle[index].is_used = true;
1461 g_focus_sound_handle[index].focus_tid = instance;
1462 g_focus_sound_handle[index].handle = id;
1463 g_focus_sound_handle[index].focus_callback = callback;
1464 g_focus_sound_handle[index].user_data = user_data;
1465 g_focus_sound_handle[index].is_for_session = is_for_session;
1466 g_focus_sound_handle[index].auto_reacquire = true;
1468 ret = mm_sound_proxy_register_focus(id, pid, stream_type, callback, is_for_session, user_data);
1470 if (ret == MM_ERROR_NONE) {
1471 debug_msg("[Client] Success to register focus\n");
1472 g_need_emergent_exit = TRUE;
1473 if (!g_focus_thread) {
1474 GMainContext* focus_context = g_main_context_new ();
1475 g_focus_loop = g_main_loop_new (focus_context, FALSE);
1476 g_main_context_unref(focus_context);
1477 g_focus_thread = g_thread_new("focus-callback-thread", _focus_thread_func, NULL);
1478 if (g_focus_thread == NULL) {
1479 debug_error ("could not create thread..");
1480 g_main_loop_unref(g_focus_loop);
1481 g_focus_sound_handle[index].is_used = false;
1482 ret = MM_ERROR_SOUND_INTERNAL;
1487 debug_error("[Client] Error occurred : 0x%x \n",ret);
1488 g_focus_sound_handle[index].is_used = false;
1492 _focus_init_callback(index, false);
1500 int mm_sound_client_unregister_focus(int id)
1502 int ret = MM_ERROR_NONE;
1507 index = _focus_find_index_by_handle(id);
1509 debug_error("Could not find index");
1510 return MM_ERROR_INVALID_ARGUMENT;
1512 instance = g_focus_sound_handle[index].focus_tid;
1514 if (!g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1515 debug_warning("maybe focus_callback is being called, try one more time..");
1516 usleep(2500000); // 2.5 sec
1517 if (g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1518 debug_msg("finally got focus_lock");
1522 ret = mm_sound_proxy_unregister_focus(instance, id, g_focus_sound_handle[index].is_for_session);
1524 if (ret == MM_ERROR_NONE)
1525 debug_msg("[Client] Success to unregister focus\n");
1527 debug_error("[Client] Error occurred : 0x%x \n",ret);
1529 g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
1531 _focus_destroy_callback(index, false);
1532 g_focus_sound_handle[index].focus_fd = 0;
1533 g_focus_sound_handle[index].focus_tid = 0;
1534 g_focus_sound_handle[index].handle = 0;
1535 g_focus_sound_handle[index].is_used = false;
1541 int mm_sound_client_set_focus_reacquisition(int id, bool reacquisition)
1543 int ret = MM_ERROR_NONE;
1550 index = _focus_find_index_by_handle(id);
1552 debug_error("Could not find index");
1553 return MM_ERROR_INVALID_ARGUMENT;
1555 instance = g_focus_sound_handle[index].focus_tid;
1557 ret = mm_sound_client_is_focus_cb_thread(g_thread_self(), &result);
1559 debug_error("[Client] mm_sound_client_is_focus_cb_thread failed");
1561 } else if (!result) {
1562 ret = mm_sound_proxy_set_foucs_reacquisition(instance, id, reacquisition);
1563 if (ret == MM_ERROR_NONE) {
1564 debug_msg("[Client] Success to set focus reacquisition\n");
1566 debug_error("[Client] Error occurred : 0x%x \n",ret);
1570 debug_warning("[Client] Inside the focus cb thread, bypassing dbus method call");
1573 g_focus_sound_handle[index].auto_reacquire = reacquisition;
1581 int mm_sound_client_get_focus_reacquisition(int id, bool *reacquisition)
1583 int ret = MM_ERROR_NONE;
1588 if (!reacquisition) {
1589 debug_error("Invalid parameter");
1590 return MM_ERROR_INVALID_ARGUMENT;
1593 index = _focus_find_index_by_handle(id);
1595 debug_error("Could not find index");
1596 return MM_ERROR_INVALID_ARGUMENT;
1599 *reacquisition = g_focus_sound_handle[index].auto_reacquire;
1605 int mm_sound_client_get_acquired_focus_stream_type(int focus_type, char **stream_type, char **additional_info)
1607 int ret = MM_ERROR_NONE;
1611 ret = mm_sound_proxy_get_acquired_focus_stream_type(focus_type, stream_type, additional_info);
1612 if (ret == MM_ERROR_NONE)
1613 debug_msg("[Client] Success to get stream type of acquired focus, stream_type(%s), additional_info(%s)\n", *stream_type, *additional_info);
1615 debug_error("[Client] Error occurred : 0x%x \n",ret);
1621 int mm_sound_client_acquire_focus(int id, mm_sound_focus_type_e type, const char *option)
1623 int ret = MM_ERROR_NONE;
1629 index = _focus_find_index_by_handle(id);
1631 debug_error("Could not find index");
1632 return MM_ERROR_INVALID_ARGUMENT;
1634 instance = g_focus_sound_handle[index].focus_tid;
1636 ret = mm_sound_proxy_acquire_focus(instance, id, type, option, g_focus_sound_handle[index].is_for_session);
1638 if (ret == MM_ERROR_NONE)
1639 debug_msg("[Client] Success to acquire focus\n");
1641 debug_error("[Client] Error occurred : 0x%x \n",ret);
1647 int mm_sound_client_release_focus(int id, mm_sound_focus_type_e type, const char *option)
1649 int ret = MM_ERROR_NONE;
1654 index = _focus_find_index_by_handle(id);
1656 debug_error("Could not find index");
1657 return MM_ERROR_INVALID_ARGUMENT;
1659 instance = g_focus_sound_handle[index].focus_tid;
1661 ret = mm_sound_proxy_release_focus(instance, id, type, option, g_focus_sound_handle[index].is_for_session);
1663 if (ret == MM_ERROR_NONE)
1664 debug_msg("[Client] Success to release focus\n");
1666 debug_error("[Client] Error occurred : 0x%x \n",ret);
1673 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)
1675 int ret = MM_ERROR_NONE;
1681 return MM_ERROR_INVALID_ARGUMENT;
1683 //pthread_mutex_lock(&g_thread_mutex2);
1687 ret = mm_sound_proxy_get_unique_id(id);
1691 for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
1692 if (g_focus_sound_handle[index].is_used == false) {
1693 g_focus_sound_handle[index].is_used = true;
1698 g_focus_sound_handle[index].focus_tid = instance;
1699 g_focus_sound_handle[index].handle = *id;
1700 g_focus_sound_handle[index].watch_callback = callback;
1701 g_focus_sound_handle[index].user_data = user_data;
1702 g_focus_sound_handle[index].is_for_session = is_for_session;
1704 ret = mm_sound_proxy_set_focus_watch_callback(pid, g_focus_sound_handle[index].handle, focus_type, callback, is_for_session, user_data);
1706 if (ret == MM_ERROR_NONE) {
1707 debug_msg("[Client] Success to watch focus");
1708 g_need_emergent_exit = TRUE;
1709 if (!g_focus_thread) {
1710 GMainContext* focus_context = g_main_context_new ();
1711 g_focus_loop = g_main_loop_new (focus_context, FALSE);
1712 g_main_context_unref(focus_context);
1713 g_focus_thread = g_thread_new("focus-callback-thread", _focus_thread_func, NULL);
1714 if (g_focus_thread == NULL) {
1715 debug_error ("could not create thread..");
1716 g_main_loop_unref(g_focus_loop);
1717 ret = MM_ERROR_SOUND_INTERNAL;
1722 debug_error("[Client] Error occurred : 0x%x",ret);
1726 _focus_init_callback(index, true);
1731 g_focus_sound_handle[index].is_used = false;
1738 int mm_sound_client_unset_focus_watch_callback(int id)
1740 int ret = MM_ERROR_NONE;
1744 index = _focus_find_index_by_handle(id);
1746 debug_error("Could not find index");
1747 return MM_ERROR_INVALID_ARGUMENT;
1750 g_mutex_lock(&g_focus_sound_handle[index].focus_lock);
1752 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);
1754 if (ret == MM_ERROR_NONE)
1755 debug_msg("[Client] Success to unwatch focus\n");
1757 debug_error("[Client] Error occurred : 0x%x \n",ret);
1760 g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
1762 _focus_destroy_callback(index, true);
1763 g_focus_sound_handle[index].focus_fd = 0;
1764 g_focus_sound_handle[index].focus_tid = 0;
1765 g_focus_sound_handle[index].handle = 0;
1766 g_focus_sound_handle[index].is_used = false;
1774 int mm_sound_client_add_test_callback(mm_sound_test_cb func, void* user_data, unsigned int *subs_id)
1776 int ret = MM_ERROR_NONE;
1780 ret = mm_sound_proxy_add_test_callback(func, user_data, g_free, subs_id);
1786 int mm_sound_client_remove_test_callback(unsigned int subs_id)
1788 int ret = MM_ERROR_NONE;
1791 ret = mm_sound_proxy_remove_test_callback(subs_id);
1798 int mm_sound_client_test(int a, int b, int* getv)
1800 int ret = MM_ERROR_NONE;
1804 ret = mm_sound_proxy_test(a, b, getv);
1805 debug_log("%d * %d -> result : %d", a, b, *getv);