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.
25 #include <sys/types.h>
37 #include <semaphore.h>
43 #include "include/mm_sound.h"
44 #include "include/mm_sound_msg.h"
45 #include "include/mm_sound_client.h"
46 #include "include/mm_sound_client_dbus.h"
47 #include "include/mm_sound_common.h"
48 #include "include/mm_sound_device.h"
50 #include "include/mm_sound_focus.h"
53 #include <mm_session.h>
54 #include <mm_session_private.h>
56 #define __DIRECT_CALLBACK__
57 //#define __GIDLE_CALLBACK__
60 #if defined(__GSOURCE_CALLBACK__)
64 #define CLIENT_HANDLE_MAX 256
66 #define FOCUS_HANDLE_MAX 512
67 #define FOCUS_HANDLE_INIT_VAL -1
68 #define CONFIG_ENABLE_RETCB
70 #define VOLUME_TYPE_LEN 64
72 struct sigaction FOCUS_int_old_action;
73 struct sigaction FOCUS_abrt_old_action;
74 struct sigaction FOCUS_segv_old_action;
75 struct sigaction FOCUS_term_old_action;
76 struct sigaction FOCUS_sys_old_action;
77 struct sigaction FOCUS_xcpu_old_action;
90 GSourceFuncs* g_src_funcs;
96 mm_sound_focus_changed_cb focus_callback;
97 mm_sound_focus_changed_watch_cb watch_callback;
100 bool is_for_session; /* will be removed when the session concept is completely left out*/
101 } focus_sound_info_t;
108 char stream_type [MAX_STREAM_TYPE_LEN];
109 char name [MM_SOUND_NAME_NUM];
113 mm_sound_focus_session_interrupt_cb user_cb;
115 } focus_session_interrupt_info_t;
117 typedef gboolean (*focus_gLoopPollHandler_t)(gpointer d);
119 GThread *g_focus_thread;
120 GMainLoop *g_focus_loop;
121 focus_sound_info_t g_focus_sound_handle[FOCUS_HANDLE_MAX];
122 focus_session_interrupt_info_t g_focus_session_interrupt_info = {NULL, NULL};
124 /* global variables for device list */
125 //static GList *g_device_list = NULL;
126 static mm_sound_device_list_t g_device_list_t;
127 static pthread_mutex_t g_device_list_mutex = PTHREAD_MUTEX_INITIALIZER;
128 static pthread_mutex_t g_id_mutex = PTHREAD_MUTEX_INITIALIZER;
130 guint g_focus_signal_handle = 0;
132 void _focus_signal_handler(int signo)
134 int ret = MM_ERROR_NONE;
137 sigset_t old_mask, all_mask;
139 debug_error("Got signal : signo(%d)", signo);
143 sigfillset(&all_mask);
144 sigprocmask(SIG_BLOCK, &all_mask, &old_mask);
148 /* need implementation */
149 //send exit pid to focus server and focus server will clear focus or watch if necessary.
151 for (index = 0; index < FOCUS_HANDLE_MAX; index++) {
152 if (g_focus_sound_handle[index].is_used == true && g_focus_sound_handle[index].focus_tid == exit_pid) {
153 ret = mm_sound_client_dbus_emergent_exit_focus(exit_pid);
158 if (ret == MM_ERROR_NONE)
159 debug_msg("[Client] Success to emergnet_exit_focus\n");
161 debug_error("[Client] Error occurred : %d \n",ret);
163 sigprocmask(SIG_SETMASK, &old_mask, NULL);
168 sigaction(SIGINT, &FOCUS_int_old_action, NULL);
172 sigaction(SIGABRT, &FOCUS_abrt_old_action, NULL);
176 sigaction(SIGSEGV, &FOCUS_segv_old_action, NULL);
180 sigaction(SIGTERM, &FOCUS_term_old_action, NULL);
184 sigaction(SIGSYS, &FOCUS_sys_old_action, NULL);
188 sigaction(SIGXCPU, &FOCUS_xcpu_old_action, NULL);
195 debug_error("signal handling end");
198 int mm_sound_client_initialize(void)
200 int ret = MM_ERROR_NONE;
203 mm_sound_client_dbus_initialize();
207 struct sigaction FOCUS_action;
208 FOCUS_action.sa_handler = _focus_signal_handler;
209 FOCUS_action.sa_flags = SA_NOCLDSTOP;
211 sigemptyset(&FOCUS_action.sa_mask);
213 sigaction(SIGINT, &FOCUS_action, &FOCUS_int_old_action);
214 sigaction(SIGABRT, &FOCUS_action, &FOCUS_abrt_old_action);
215 sigaction(SIGSEGV, &FOCUS_action, &FOCUS_segv_old_action);
216 sigaction(SIGTERM, &FOCUS_action, &FOCUS_term_old_action);
217 sigaction(SIGSYS, &FOCUS_action, &FOCUS_sys_old_action);
218 sigaction(SIGXCPU, &FOCUS_action, &FOCUS_xcpu_old_action);
227 int mm_sound_client_finalize(void)
229 int ret = MM_ERROR_NONE;
233 ret = mm_sound_client_dbus_finalize();
242 for (index = 0; index < FOCUS_HANDLE_MAX; index++) {
243 if (g_focus_sound_handle[index].is_used == true && g_focus_sound_handle[index].focus_tid == exit_pid) {
244 mm_sound_client_dbus_emergent_exit_focus(exit_pid);
248 if (g_focus_thread) {
249 g_main_loop_quit(g_focus_loop);
250 g_thread_join(g_focus_thread);
251 debug_log("after thread join");
252 g_main_loop_unref(g_focus_loop);
253 g_focus_thread = NULL;
256 /* is it necessary? */
257 sigaction(SIGINT, &FOCUS_int_old_action, NULL);
258 sigaction(SIGABRT, &FOCUS_abrt_old_action, NULL);
259 sigaction(SIGSEGV, &FOCUS_segv_old_action, NULL);
260 sigaction(SIGTERM, &FOCUS_term_old_action, NULL);
261 sigaction(SIGSYS, &FOCUS_sys_old_action, NULL);
262 sigaction(SIGXCPU, &FOCUS_xcpu_old_action, NULL);
270 int mm_sound_client_is_route_available(mm_sound_route route, bool *is_available)
272 int ret = MM_ERROR_NONE;
275 ret = mm_sound_client_dbus_is_route_available(route, is_available);
282 int mm_sound_client_foreach_available_route_cb(mm_sound_available_route_cb available_route_cb, void *user_data)
284 int ret = MM_ERROR_NONE;
287 ret = mm_sound_client_dbus_foreach_available_route_cb(available_route_cb, user_data);
293 int mm_sound_client_set_active_route(mm_sound_route route, bool need_broadcast)
295 int ret = MM_ERROR_NONE;
298 ret = mm_sound_client_dbus_set_active_route(route, need_broadcast);
305 int mm_sound_client_set_active_route_auto(void)
307 int ret = MM_ERROR_NONE;
310 ret = mm_sound_client_dbus_set_active_route_auto();
317 int mm_sound_client_get_active_device(mm_sound_device_in *device_in, mm_sound_device_out *device_out)
320 int ret = MM_ERROR_NONE;
323 ret = mm_sound_client_dbus_get_active_device(device_in, device_out);
329 int mm_sound_client_add_active_device_changed_callback(const char *name, mm_sound_active_device_changed_cb func, void* user_data)
331 int ret = MM_ERROR_NONE;
334 ret = mm_sound_client_dbus_add_active_device_changed_callback(name, func, user_data);
340 int mm_sound_client_remove_active_device_changed_callback(const char *name)
342 int ret = MM_ERROR_NONE;
345 ret = mm_sound_client_dbus_remove_active_device_changed_callback(name);
350 int mm_sound_client_add_available_route_changed_callback(mm_sound_available_route_changed_cb func, void* user_data)
352 int ret = MM_ERROR_NONE;
355 ret = mm_sound_client_dbus_add_available_route_changed_callback(func, user_data);
361 int mm_sound_client_remove_available_route_changed_callback(void)
363 int ret = MM_ERROR_NONE;
366 ret = mm_sound_client_dbus_remove_available_route_changed_callback();
372 int mm_sound_client_set_sound_path_for_active_device(mm_sound_device_out device_out, mm_sound_device_in device_in)
374 int ret = MM_ERROR_NONE;
377 ret = mm_sound_client_dbus_set_sound_path_for_active_device(device_out, device_in);
383 void mm_sound_convert_volume_type_to_stream_type(int volume_type, char *stream_type)
385 switch (volume_type) {
386 case VOLUME_TYPE_SYSTEM:
387 strncpy(stream_type, "system", MM_SOUND_STREAM_TYPE_LEN);
389 case VOLUME_TYPE_NOTIFICATION:
390 strncpy(stream_type, "notification", MM_SOUND_STREAM_TYPE_LEN);
392 case VOLUME_TYPE_ALARM:
393 strncpy(stream_type, "alarm", MM_SOUND_STREAM_TYPE_LEN);
395 case VOLUME_TYPE_RINGTONE:
396 strncpy(stream_type, "ringtone-voip", MM_SOUND_STREAM_TYPE_LEN);
398 case VOLUME_TYPE_MEDIA:
399 strncpy(stream_type, "media", MM_SOUND_STREAM_TYPE_LEN);
401 case VOLUME_TYPE_CALL:
402 strncpy(stream_type, "system", MM_SOUND_STREAM_TYPE_LEN);
404 case VOLUME_TYPE_VOIP:
405 strncpy(stream_type, "voip", MM_SOUND_STREAM_TYPE_LEN);
407 case VOLUME_TYPE_VOICE:
408 strncpy(stream_type, "voice-recognition", MM_SOUND_STREAM_TYPE_LEN);
411 strncpy(stream_type, "media", MM_SOUND_STREAM_TYPE_LEN);
415 debug_error("volume type (%d) converted to stream type (%s)", volume_type, stream_type);
419 /*****************************************************************************************
420 DBUS SUPPORTED FUNCTIONS
421 ******************************************************************************************/
423 void _mm_sound_client_focus_signal_callback(mm_sound_signal_name_t signal, int value, void *user_data)
425 int ret = MM_ERROR_NONE;
428 debug_error("focus signal received, value = %d", value);
431 ret = mm_sound_client_dbus_clear_focus(getpid());
433 debug_error("clear focus failed ret = 0x%x", ret);
434 mm_sound_unsubscribe_signal(g_focus_signal_handle);
435 g_focus_signal_handle = 0;
439 int mm_sound_client_play_tone(int number, int volume_config, double volume, int time, int *handle, bool enable_session)
441 int ret = MM_ERROR_NONE;
442 // int instance = -1; /* instance is unique to communicate with server : client message queue filter type */
443 int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(volume_config);
444 char stream_type[MM_SOUND_STREAM_TYPE_LEN] = {0, };
448 /* read session information */
449 int session_type = MM_SESSION_TYPE_MEDIA;
450 int session_options = 0;
451 int is_focus_registered = 0;
453 ret = mm_sound_get_signal_value(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &is_focus_registered);
455 debug_error("mm_sound_get_signal_value failed [0x%x]", ret);
456 return MM_ERROR_POLICY_INTERNAL;
459 if (is_focus_registered)
460 enable_session = false;
462 if (enable_session) {
463 if (MM_ERROR_NONE != _mm_session_util_read_information(-1, &session_type, &session_options)) {
464 debug_warning("[Client] Read Session Information failed. use default \"media\" type\n");
465 session_type = MM_SESSION_TYPE_MEDIA;
467 if(MM_ERROR_NONE != mm_session_init(session_type)) {
468 debug_critical("[Client] MMSessionInit() failed\n");
469 return MM_ERROR_POLICY_INTERNAL;
474 // instance = getpid();
475 //debug_log("[Client] pid for client ::: [%d]\n", instance);
478 debug_msg("[Client] Input number : %d\n", number);
479 /* Send req memory */
481 mm_sound_convert_volume_type_to_stream_type(volume_type, stream_type);
482 ret = mm_sound_client_dbus_play_tone(number, time, volume, volume_config,
483 session_type, session_options, getpid(), enable_session, handle, stream_type, -1);
485 if (enable_session && !g_focus_signal_handle) {
486 ret = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &g_focus_signal_handle, _mm_sound_client_focus_signal_callback, NULL);
488 debug_error("mm_sound_subscribe_signal failed [0x%x]", ret);
489 return MM_ERROR_POLICY_INTERNAL;
497 int mm_sound_client_play_tone_with_stream_info(int tone, char *stream_type, int stream_id, double volume, int duration, int *handle)
499 int ret = MM_ERROR_NONE;
503 ret = mm_sound_client_dbus_play_tone_with_stream_info(getpid(), tone, stream_type, stream_id, volume, duration, handle);
509 void _mm_sound_stop_callback_wrapper_func(int ended_handle, void *userdata)
511 client_cb_data_t *cb_data = (client_cb_data_t*) userdata;
513 debug_log("[Wrapper CB][Play Stop] ended_handle : %d", ended_handle);
515 if (cb_data == NULL) {
516 debug_warning("stop callback data null");
519 if (ended_handle == cb_data->mask) {
520 debug_log("Interested playing handle end : %d", ended_handle);
521 ((mm_sound_stop_callback_func)(cb_data->user_cb))(cb_data->user_data, ended_handle);
522 if (mm_sound_client_dbus_remove_play_sound_end_callback(cb_data->subs_id) != MM_ERROR_NONE)
523 debug_error("mm_sound_client_dbus_remove_play_file_end_callback failed");
525 debug_log("Not interested playing handle : %d", ended_handle);
529 int mm_sound_client_play_sound(MMSoundPlayParam *param, int tone, int *handle)
531 int ret = MM_ERROR_NONE;
532 int session_type = MM_SESSION_TYPE_MEDIA;
533 int session_options = 0;
534 int is_focus_registered = 0;
535 // int instance = -1; /* instance is unique to communicate with server : client message queue filter type */
536 int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(param->volume_config);
537 char stream_type[MM_SOUND_STREAM_TYPE_LEN] = {0, };
538 client_cb_data_t *cb_data = NULL;
542 /* read session information */
544 ret = mm_sound_get_signal_value(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &is_focus_registered);
546 debug_error("mm_sound_get_signal_value failed [0x%x]", ret);
547 return MM_ERROR_POLICY_INTERNAL;
550 if (is_focus_registered)
551 param->skip_session = true;
553 if (param->skip_session == false) {
554 if(MM_ERROR_NONE != _mm_session_util_read_information(-1, &session_type, &session_options)) {
555 debug_warning("[Client] Read MMSession Type failed. use default \"media\" type\n");
556 session_type = MM_SESSION_TYPE_MEDIA;
558 if(MM_ERROR_NONE != mm_session_init(session_type)) {
559 debug_critical("[Client] MMSessionInit() failed\n");
560 return MM_ERROR_POLICY_INTERNAL;
566 if ((param->mem_ptr && param->mem_size)) {
567 // Play memory, deprecated
568 return MM_ERROR_INVALID_ARGUMENT;
571 mm_sound_convert_volume_type_to_stream_type(volume_type, stream_type);
572 ret = mm_sound_client_dbus_play_sound(param->filename, tone, param->loop, param->volume, param->volume_config,
573 param->priority, session_type, session_options, getpid(), param->handle_route,
574 !param->skip_session, handle, stream_type, -1);
575 if (ret != MM_ERROR_NONE) {
576 debug_error("Play Sound Failed");
579 if (param->callback) {
580 cb_data = g_malloc0(sizeof(client_cb_data_t));
581 cb_data->user_cb = param->callback;
582 cb_data->user_data = param->data;
583 cb_data->mask = *handle;
585 ret = mm_sound_client_dbus_add_play_sound_end_callback(_mm_sound_stop_callback_wrapper_func, cb_data, &cb_data->subs_id);
586 if (ret != MM_ERROR_NONE) {
587 debug_error("Add callback for play sound(%d) Failed", *handle);
590 if (!param->skip_session && !g_focus_signal_handle) {
591 ret = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &g_focus_signal_handle, _mm_sound_client_focus_signal_callback, NULL);
593 debug_error("mm_sound_subscribe_signal failed [0x%x]", ret);
594 return MM_ERROR_POLICY_INTERNAL;
604 int mm_sound_client_play_sound_with_stream_info(MMSoundPlayParam *param, int *handle, char* stream_type, int stream_id)
606 int ret = MM_ERROR_NONE;
607 client_cb_data_t *cb_data = NULL;
609 ret = mm_sound_client_dbus_play_sound_with_stream_info(param->filename, param->loop, param->volume,
610 param->priority, getpid(), param->handle_route, handle, stream_type, stream_id);
611 if (ret != MM_ERROR_NONE) {
612 debug_error("Play Sound Failed");
615 if (param->callback) {
616 cb_data = g_malloc0(sizeof(client_cb_data_t));
617 cb_data->user_cb = param->callback;
618 cb_data->user_data = param->data;
619 cb_data->mask = *handle;
621 ret = mm_sound_client_dbus_add_play_sound_end_callback(_mm_sound_stop_callback_wrapper_func, cb_data, &cb_data->subs_id);
622 if (ret != MM_ERROR_NONE) {
623 debug_error("Add callback for play sound(%d) Failed", *handle);
634 int mm_sound_client_stop_sound(int handle)
636 int ret = MM_ERROR_NONE;
639 if (handle < 0 || handle > CLIENT_HANDLE_MAX) {
640 ret = MM_ERROR_INVALID_ARGUMENT;
644 ret = mm_sound_client_dbus_stop_sound(handle);
650 static int _mm_sound_client_device_list_clear ()
652 int ret = MM_ERROR_NONE;
654 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_device_list_mutex, MM_ERROR_SOUND_INTERNAL);
656 if (g_device_list_t.list) {
657 g_list_free_full(g_device_list_t.list, g_free);
658 g_device_list_t.list = NULL;
661 MMSOUND_LEAVE_CRITICAL_SECTION(&g_device_list_mutex);
666 static int _mm_sound_client_device_list_dump (GList *device_list)
668 int ret = MM_ERROR_NONE;
670 mm_sound_device_t *device_node = NULL;
673 debug_error("Device list NULL, cannot dump list");
674 return MM_ERROR_SOUND_INTERNAL;
677 debug_log("======================== device list : start ==========================\n");
678 for (list = device_list; list != NULL; list = list->next) {
679 device_node = (mm_sound_device_t *)list->data;
681 debug_log(" list idx[%d]: type[%17s], id[%02d], io_direction[%d], state[%d], name[%s]\n",
682 count++, device_node->type, device_node->id, device_node->io_direction, device_node->state, device_node->name);
685 debug_log("======================== device list : end ============================\n");
690 int mm_sound_client_get_current_connected_device_list(int device_flags, mm_sound_device_list_t **device_list)
692 int ret = MM_ERROR_NONE;
695 ret = _mm_sound_client_device_list_clear();
697 debug_error("[Client] failed to __mm_sound_client_device_list_clear(), ret[0x%x]\n", ret);
701 if ((ret = mm_sound_client_dbus_get_current_connected_device_list(device_flags, &g_device_list_t.list)) != MM_ERROR_NONE) {
702 debug_error("[Client] failed to get current connected device list with dbus, ret[0x%x]", ret);
705 if (!g_device_list_t.list) {
706 debug_error("Got device list null");
707 ret = MM_ERROR_SOUND_NO_DATA;
710 // g_device_list_t.list = g_device_list;
711 _mm_sound_client_device_list_dump(g_device_list_t.list);
712 *device_list = &g_device_list_t;
719 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)
721 mm_sound_device_t device_h;
722 client_cb_data_t *cb_data = (client_cb_data_t*) userdata;
724 debug_log("[Wrapper CB][Device Connnected] device_id : %d, device_type : %s, direction : %d, state : %d, name : %s, is_connected : %d",
725 device_id, device_type, io_direction, state, name, is_connected);
727 if (cb_data == NULL) {
728 debug_warning("device connected changed callback data null");
732 device_h.id = device_id;
733 device_h.io_direction = io_direction;
734 device_h.state = state;
735 MMSOUND_STRNCPY(device_h.name, name, MAX_DEVICE_NAME_NUM);
736 MMSOUND_STRNCPY(device_h.type, device_type, MAX_DEVICE_TYPE_STR_LEN);
738 ((mm_sound_device_connected_cb)(cb_data->user_cb))(&device_h, is_connected, cb_data->user_data);
741 int mm_sound_client_add_device_connected_callback(int device_flags, mm_sound_device_connected_cb func, void* user_data, unsigned int *subs_id)
743 int ret = MM_ERROR_NONE;
744 client_cb_data_t *cb_data = NULL;
748 cb_data = g_malloc0(sizeof(client_cb_data_t));
749 cb_data->user_cb = func;
750 cb_data->user_data = user_data;
752 ret = mm_sound_client_dbus_add_device_connected_callback(device_flags, _mm_sound_device_connected_callback_wrapper_func, cb_data, subs_id);
758 int mm_sound_client_remove_device_connected_callback(unsigned int subs_id)
760 int ret = MM_ERROR_NONE;
763 ret = mm_sound_client_dbus_remove_device_connected_callback(subs_id);
769 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)
771 mm_sound_device_t device_h;
772 client_cb_data_t *cb_data = (client_cb_data_t*) userdata;
774 debug_log("[Wrapper CB][Device Info Changed] device_id : %d, device_type : %s, direction : %d, state : %d, name : %s, changed_info_type : %d",
775 device_id, device_type, io_direction, state, name, changed_device_info_type);
777 if (cb_data == NULL) {
778 debug_warning("device info changed callback data null");
782 device_h.id = device_id;
783 device_h.io_direction = io_direction;
784 device_h.state = state;
785 MMSOUND_STRNCPY(device_h.name, name, MAX_DEVICE_NAME_NUM);
786 MMSOUND_STRNCPY(device_h.type, device_type, MAX_DEVICE_TYPE_STR_LEN);
788 ((mm_sound_device_info_changed_cb)(cb_data->user_cb))(&device_h, changed_device_info_type, cb_data->user_data);
791 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)
793 int ret = MM_ERROR_NONE;
794 client_cb_data_t *cb_data = (client_cb_data_t*) userdata;
798 cb_data = g_malloc0(sizeof(client_cb_data_t));
799 cb_data->user_cb = func;
800 cb_data->user_data = userdata;
802 ret = mm_sound_client_dbus_add_device_info_changed_callback(device_flags, _mm_sound_device_info_changed_callback_wrapper_func, cb_data, subs_id);
808 int mm_sound_client_remove_device_info_changed_callback(unsigned int subs_id)
810 int ret = MM_ERROR_NONE;
813 ret = mm_sound_client_dbus_remove_device_info_changed_callback(subs_id);
819 int mm_sound_client_is_bt_a2dp_on (bool *connected, char** bt_name)
821 int ret = MM_ERROR_NONE;
825 ret = mm_sound_client_dbus_is_bt_a2dp_on(connected, bt_name);
831 int __convert_volume_type_to_str(int volume_type, char **volume_type_str)
833 int ret = MM_ERROR_NONE;
835 if (!volume_type_str) {
836 return MM_ERROR_COMMON_INVALID_ARGUMENT;
839 switch (volume_type) {
840 case VOLUME_TYPE_SYSTEM:
841 *volume_type_str = "system";
843 case VOLUME_TYPE_NOTIFICATION:
844 *volume_type_str = "notification";
846 case VOLUME_TYPE_ALARM:
847 *volume_type_str = "alarm";
849 case VOLUME_TYPE_RINGTONE:
850 *volume_type_str = "ringtone";
852 case VOLUME_TYPE_MEDIA:
853 *volume_type_str = "media";
855 case VOLUME_TYPE_CALL:
856 *volume_type_str = "call";
858 case VOLUME_TYPE_VOIP:
859 *volume_type_str = "voip";
861 case VOLUME_TYPE_VOICE:
862 *volume_type_str = "voice";
865 if (!strncmp(*volume_type_str,"", VOLUME_TYPE_LEN)) {
866 debug_error("could not find the volume_type[%d] in this switch case statement", volume_type);
867 ret = MM_ERROR_SOUND_INTERNAL;
869 debug_log("volume_type[%s]", *volume_type_str);
874 static int __convert_volume_type_to_int(const char *volume_type_str, volume_type_t *volume_type)
876 int ret = MM_ERROR_NONE;
878 if (!volume_type || !volume_type_str) {
879 return MM_ERROR_COMMON_INVALID_ARGUMENT;
882 if (!strncmp(volume_type_str, "system", VOLUME_TYPE_LEN)) {
883 *volume_type = VOLUME_TYPE_SYSTEM;
884 } else if (!strncmp(volume_type_str, "notification", VOLUME_TYPE_LEN)) {
885 *volume_type = VOLUME_TYPE_NOTIFICATION;
886 } else if (!strncmp(volume_type_str, "alarm", VOLUME_TYPE_LEN)) {
887 *volume_type = VOLUME_TYPE_ALARM;
888 } else if (!strncmp(volume_type_str, "ringtone", VOLUME_TYPE_LEN)) {
889 *volume_type = VOLUME_TYPE_RINGTONE;
890 } else if (!strncmp(volume_type_str, "media", VOLUME_TYPE_LEN)) {
891 *volume_type = VOLUME_TYPE_MEDIA;
892 } else if (!strncmp(volume_type_str, "call", VOLUME_TYPE_LEN)) {
893 *volume_type = VOLUME_TYPE_CALL;
894 } else if (!strncmp(volume_type_str, "voip", VOLUME_TYPE_LEN)) {
895 *volume_type = VOLUME_TYPE_VOIP;
896 } else if (!strncmp(volume_type_str, "voice", VOLUME_TYPE_LEN)) {
897 *volume_type = VOLUME_TYPE_VOICE;
899 debug_log("Invalid volume type : [%s]", volume_type_str);
900 ret = MM_ERROR_SOUND_INTERNAL;
906 int mm_sound_client_set_volume_by_type(const int volume_type, const unsigned int volume_level)
908 int ret = MM_ERROR_NONE;
909 char *type_str = NULL;
912 if ((ret = __convert_volume_type_to_str(volume_type, &type_str)) != MM_ERROR_NONE) {
913 debug_error("volume type convert failed");
917 ret = mm_sound_client_dbus_set_volume_by_type(type_str, volume_level);
924 static void _mm_sound_volume_changed_callback_wrapper_func(const char *direction, const char *volume_type_str, int volume_level, void *userdata)
926 volume_type_t volume_type = 0;
927 client_cb_data_t *cb_data = (client_cb_data_t*) userdata;
929 debug_log("[Wrapper CB][Volume Changed] direction : %s, volume_type : %s, volume_level : %d", direction, volume_type_str, volume_level);
931 if (cb_data == NULL) {
932 debug_warning("volume changed callback data null");
936 if (__convert_volume_type_to_int(volume_type_str, &volume_type) != MM_ERROR_NONE) {
937 debug_error("volume type convert failed");
940 debug_log("Call volume changed user cb, direction : %s, vol_type : %s(%d), level : %u", direction, volume_type_str, volume_type, volume_level);
941 ((mm_sound_volume_changed_cb)(cb_data->user_cb))(volume_type, volume_level, cb_data->user_data);
944 int mm_sound_client_add_volume_changed_callback(mm_sound_volume_changed_cb func, void* user_data, unsigned int *subs_id)
946 int ret = MM_ERROR_NONE;
947 client_cb_data_t *cb_data = NULL;
951 cb_data = g_malloc0(sizeof(client_cb_data_t));
952 cb_data->user_cb = func;
953 cb_data->user_data = user_data;
955 ret = mm_sound_client_dbus_add_volume_changed_callback(_mm_sound_volume_changed_callback_wrapper_func, cb_data, subs_id);
962 int mm_sound_client_remove_volume_changed_callback(unsigned int subs_id)
964 int ret = MM_ERROR_NONE;
967 ret = mm_sound_client_dbus_remove_volume_changed_callback(subs_id);
974 int mm_sound_client_get_audio_path(mm_sound_device_in *device_in, mm_sound_device_out *device_out)
976 int ret = MM_ERROR_NONE;
980 ret = mm_sound_client_dbus_get_audio_path(device_in, device_out);
989 int mm_sound_client_set_session_interrupt_callback(mm_sound_focus_session_interrupt_cb callback, void* user_data)
991 int ret = MM_ERROR_NONE;
996 return MM_ERROR_INVALID_ARGUMENT;
998 g_focus_session_interrupt_info.user_cb = callback;
999 g_focus_session_interrupt_info.user_data = user_data;
1005 int mm_sound_client_unset_session_interrupt_callback(void)
1007 int ret = MM_ERROR_NONE;
1011 if (!g_focus_session_interrupt_info.user_cb) {
1012 debug_error("no callback to unset");
1013 return MM_ERROR_SOUND_INTERNAL;
1016 g_focus_session_interrupt_info.user_cb = NULL;
1017 g_focus_session_interrupt_info.user_data = NULL;
1023 static gpointer _focus_thread_func(gpointer data)
1025 debug_log(">>> thread func..ID of this thread(%u)\n", (unsigned int)pthread_self());
1026 g_main_loop_run(g_focus_loop);
1027 debug_log("<<< quit thread func..\n");
1031 static gboolean _focus_fd_check(GSource * source)
1037 debug_error("GSource is null");
1040 fd_list = source->poll_fds;
1042 debug_error("fd_list is null");
1046 temp = (GPollFD*)fd_list->data;
1048 debug_error("fd_list->data is null");
1051 if (temp->revents & (POLLIN | POLLPRI)) {
1054 fd_list = fd_list->next;
1057 return FALSE; /* there is no change in any fd state */
1060 static gboolean _focus_fd_prepare(GSource *source, gint *timeout)
1065 static gboolean _focus_fd_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
1067 callback(user_data);
1072 static int _focus_find_index_by_handle(int handle)
1075 for(i = 0; i< FOCUS_HANDLE_MAX; i++) {
1076 if (handle == g_focus_sound_handle[i].handle) {
1077 //debug_msg("found index(%d) for handle(%d)", i, handle);
1078 if (handle == FOCUS_HANDLE_INIT_VAL) {
1087 static gboolean _focus_callback_handler(gpointer d)
1089 GPollFD *data = (GPollFD*)d;
1092 int focus_index = 0;
1093 focus_cb_data_lib cb_data;
1094 debug_log(">>> focus_callback_handler()..ID of this thread(%u)\n", (unsigned int)pthread_self());
1096 memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1099 debug_error("GPollFd is null");
1102 if (data->revents & (POLLIN | POLLPRI)) {
1103 int changed_state = -1;
1105 count = read(data->fd, &cb_data, sizeof(cb_data));
1107 char str_error[256];
1108 strerror_r(errno, str_error, sizeof(str_error));
1109 debug_error("GpollFD read fail, errno=%d(%s)",errno, str_error);
1112 changed_state = cb_data.state;
1113 focus_index = _focus_find_index_by_handle(cb_data.handle);
1114 if (focus_index == -1) {
1115 debug_error("Could not find index");
1119 g_mutex_lock(&g_focus_sound_handle[focus_index].focus_lock);
1121 tid = g_focus_sound_handle[focus_index].focus_tid;
1123 if (changed_state != -1) {
1124 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);
1125 if (g_focus_sound_handle[focus_index].focus_callback== NULL) {
1126 debug_error("callback is null..");
1127 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1130 debug_error("[CALLBACK(%p) START]",g_focus_sound_handle[focus_index].focus_callback);
1131 (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);
1132 debug_error("[CALLBACK END]");
1133 if (g_focus_session_interrupt_info.user_cb) {
1134 debug_error("sending session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
1135 (g_focus_session_interrupt_info.user_cb)(cb_data.state, cb_data.stream_type, false, g_focus_session_interrupt_info.user_data);
1138 #ifdef CONFIG_ENABLE_RETCB
1142 unsigned int buf = 0;
1143 char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", g_focus_sound_handle[focus_index].focus_tid, cb_data.handle);
1144 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1146 char str_error[256];
1147 strerror_r(errno, str_error, sizeof(str_error));
1148 debug_error("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n", tid, tmpfd, filename2, errno, str_error);
1150 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1153 buf = (unsigned int)((0x0000ffff & cb_data.handle) |(g_focus_sound_handle[focus_index].auto_reacquire << 16));
1154 rett = write(tmpfd, &buf, sizeof(buf));
1157 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
1161 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1166 static gboolean _focus_watch_callback_handler( gpointer d)
1168 GPollFD *data = (GPollFD*)d;
1171 int focus_index = 0;
1172 focus_cb_data_lib cb_data;
1176 memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1179 debug_error("GPollFd is null");
1182 if (data->revents & (POLLIN | POLLPRI)) {
1183 count = read(data->fd, &cb_data, sizeof(cb_data));
1185 char str_error[256];
1186 strerror_r(errno, str_error, sizeof(str_error));
1187 debug_error("GpollFD read fail, errno=%d(%s)",errno, str_error);
1191 focus_index = _focus_find_index_by_handle(cb_data.handle);
1192 if (focus_index == -1) {
1193 debug_error("Could not find index");
1197 debug_error("lock focus_lock = %p", &g_focus_sound_handle[focus_index].focus_lock);
1198 g_mutex_lock(&g_focus_sound_handle[focus_index].focus_lock);
1200 tid = g_focus_sound_handle[focus_index].focus_tid;
1202 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);
1204 if (g_focus_sound_handle[focus_index].watch_callback == NULL) {
1205 debug_msg("callback is null..");
1207 debug_msg("[CALLBACK(%p) START]",g_focus_sound_handle[focus_index].watch_callback);
1208 (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);
1209 debug_msg("[CALLBACK END]");
1210 if (g_focus_session_interrupt_info.user_cb) {
1211 debug_error("sending session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
1212 (g_focus_session_interrupt_info.user_cb)(cb_data.state, cb_data.stream_type, true, g_focus_session_interrupt_info.user_data);
1216 #ifdef CONFIG_ENABLE_RETCB
1221 char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.wchr", g_focus_sound_handle[focus_index].focus_tid);
1222 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1224 char str_error[256];
1225 strerror_r(errno, str_error, sizeof(str_error));
1226 debug_error("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n", tid, tmpfd, filename2, errno, str_error);
1228 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1231 buf = cb_data.handle;
1232 rett = write(tmpfd, &buf, sizeof(buf));
1235 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
1241 debug_error("unlock focus_lock = %p", &g_focus_sound_handle[focus_index].focus_lock);
1242 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1250 static void _focus_open_callback(int index, bool is_for_watching)
1257 if (is_for_watching) {
1258 filename = g_strdup_printf("/tmp/FOCUS.%d.wch", g_focus_sound_handle[index].focus_tid);
1260 filename = g_strdup_printf("/tmp/FOCUS.%d.%d", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1262 pre_mask = umask(0);
1263 if (mknod(filename, S_IFIFO|0666, 0)) {
1264 debug_error("mknod() failure, errno(%d)", errno);
1267 g_focus_sound_handle[index].focus_fd = open( filename, O_RDWR|O_NONBLOCK);
1268 if (g_focus_sound_handle[index].focus_fd == -1) {
1269 debug_error("Open fail : index(%d), file open error(%d)", index, errno);
1271 debug_log("Open sucess : index(%d), filename(%s), fd(%d)", index, filename, g_focus_sound_handle[index].focus_fd);
1276 #ifdef CONFIG_ENABLE_RETCB
1279 if (is_for_watching) {
1280 filename2 = g_strdup_printf("/tmp/FOCUS.%d.wchr", g_focus_sound_handle[index].focus_tid);
1282 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1284 pre_mask = umask(0);
1285 if (mknod(filename2, S_IFIFO | 0666, 0)) {
1286 debug_error("mknod() failure, errno(%d)", errno);
1296 void _focus_close_callback(int index, bool is_for_watching)
1300 if (g_focus_sound_handle[index].focus_fd < 0) {
1301 debug_error("Close fail : fd error.");
1304 if (is_for_watching) {
1305 filename = g_strdup_printf("/tmp/FOCUS.%d.wch", g_focus_sound_handle[index].focus_tid);
1307 filename = g_strdup_printf("/tmp/FOCUS.%d.%d", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1309 close(g_focus_sound_handle[index].focus_fd);
1310 if (remove(filename)) {
1311 debug_error("remove() failure, filename(%s), errno(%d)", filename, errno);
1313 debug_log("Close Sucess : index(%d), filename(%s)", index, filename);
1318 #ifdef CONFIG_ENABLE_RETCB
1322 if (is_for_watching) {
1323 filename2 = g_strdup_printf("/tmp/FOCUS.%d.wchr", g_focus_sound_handle[index].focus_tid);
1325 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1328 /* Defensive code - wait until callback timeout although callback is removed */
1329 int buf = MM_ERROR_NONE; //no need to specify cb result to server, just notice if the client got the callback properly or not
1332 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1334 char str_error[256];
1335 strerror_r(errno, str_error, sizeof(str_error));
1336 debug_warning("could not open file(%s) (may server close it first), tid(%d) fd(%d) %s errno=%d(%s)",
1337 filename2, g_focus_sound_handle[index].focus_tid, tmpfd, filename2, errno, str_error);
1339 debug_msg("write MM_ERROR_NONE(tid:%d) for waiting server", g_focus_sound_handle[index].focus_tid);
1340 written = write(tmpfd, &buf, sizeof(buf));
1344 if (remove(filename2)) {
1345 debug_error("remove() failure, filename(%s), errno(%d)", filename2, errno);
1353 static bool _focus_add_sound_callback(int index, int fd, gushort events, focus_gLoopPollHandler_t p_gloop_poll_handler )
1355 GSource* g_src = NULL;
1356 GSourceFuncs *g_src_funcs = NULL; /* handler function */
1357 guint gsource_handle;
1358 GPollFD *g_poll_fd = NULL; /* file descriptor */
1362 g_mutex_init(&g_focus_sound_handle[index].focus_lock);
1364 /* 1. make GSource Object */
1365 g_src_funcs = (GSourceFuncs *)g_malloc(sizeof(GSourceFuncs));
1367 debug_error("g_malloc failed on g_src_funcs");
1371 g_src_funcs->prepare = _focus_fd_prepare;
1372 g_src_funcs->check = _focus_fd_check;
1373 g_src_funcs->dispatch = _focus_fd_dispatch;
1374 g_src_funcs->finalize = NULL;
1375 g_src = g_source_new(g_src_funcs, sizeof(GSource));
1377 debug_error("g_malloc failed on m_readfd");
1380 g_focus_sound_handle[index].focus_src = g_src;
1381 g_focus_sound_handle[index].g_src_funcs = g_src_funcs;
1383 /* 2. add file description which used in g_loop() */
1384 g_poll_fd = (GPollFD *)g_malloc(sizeof(GPollFD));
1386 debug_error("g_malloc failed on g_poll_fd");
1390 g_poll_fd->events = events;
1391 g_focus_sound_handle[index].g_poll_fd = g_poll_fd;
1393 /* 3. combine g_source object and file descriptor */
1394 g_source_add_poll(g_src, g_poll_fd);
1395 gsource_handle = g_source_attach(g_src, g_main_loop_get_context(g_focus_loop));
1396 if (!gsource_handle) {
1397 debug_error(" Failed to attach the source to context");
1400 //g_source_unref(g_src);
1402 /* 4. set callback */
1403 g_source_set_callback(g_src, p_gloop_poll_handler,(gpointer)g_poll_fd, NULL);
1405 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)",
1406 g_src_funcs, g_poll_fd, gsource_handle, errno);
1414 static bool _focus_remove_sound_callback(int index, gushort events)
1420 g_mutex_clear(&g_focus_sound_handle[index].focus_lock);
1422 GSourceFuncs *g_src_funcs = g_focus_sound_handle[index].g_src_funcs;
1423 GPollFD *g_poll_fd = g_focus_sound_handle[index].g_poll_fd; /* store file descriptor */
1425 debug_error("g_poll_fd is null..");
1429 g_poll_fd->fd = g_focus_sound_handle[index].focus_fd;
1430 g_poll_fd->events = events;
1432 if (!g_focus_sound_handle[index].focus_src) {
1433 debug_error("FOCUS_sound_handle[%d].focus_src is null..", index);
1436 debug_log(" g_source_remove_poll : fd(%d), event(%x), errno(%d)", g_poll_fd->fd, g_poll_fd->events, errno);
1437 g_source_remove_poll(g_focus_sound_handle[index].focus_src, g_poll_fd);
1441 if (g_focus_sound_handle[index].focus_src) {
1442 g_source_destroy(g_focus_sound_handle[index].focus_src);
1443 if (!g_source_is_destroyed (g_focus_sound_handle[index].focus_src)) {
1444 debug_warning(" failed to g_source_destroy(), focus_src(0x%p)", g_focus_sound_handle[index].focus_src);
1447 debug_log(" g_free : g_src_funcs(%#X), g_poll_fd(%#X)", g_src_funcs, g_poll_fd);
1450 g_free(g_src_funcs);
1458 g_focus_sound_handle[index].g_src_funcs = NULL;
1459 g_focus_sound_handle[index].g_poll_fd = NULL;
1460 g_focus_sound_handle[index].focus_src = NULL;
1461 g_focus_sound_handle[index].focus_callback = NULL;
1462 g_focus_sound_handle[index].watch_callback = NULL;
1469 static void _focus_add_callback(int index, bool is_for_watching)
1472 if (!is_for_watching) {
1473 if (!_focus_add_sound_callback(index, g_focus_sound_handle[index].focus_fd, (gushort)POLLIN | POLLPRI, _focus_callback_handler)) {
1474 debug_error("failed to _focus_add_sound_callback()");
1477 } else { // need to check if it's necessary
1478 if (!_focus_add_sound_callback(index, g_focus_sound_handle[index].focus_fd, (gushort)POLLIN | POLLPRI, _focus_watch_callback_handler)) {
1479 debug_error("failed to _focus_add_sound_callback()");
1486 static void _focus_remove_callback(int index)
1489 if (!_focus_remove_sound_callback(index, (gushort)POLLIN | POLLPRI)) {
1490 debug_error("failed to __focus_remove_sound_callback()");
1496 static void _focus_init_callback(int index, bool is_for_watching)
1499 _focus_open_callback(index, is_for_watching);
1500 _focus_add_callback(index, is_for_watching);
1504 static void _focus_destroy_callback(int index, bool is_for_watching)
1507 _focus_remove_callback(index);
1508 _focus_close_callback(index, is_for_watching);
1512 int mm_sound_client_get_unique_id(int *id)
1514 int ret = MM_ERROR_NONE;
1516 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_id_mutex, MM_ERROR_SOUND_INTERNAL);
1520 ret = MM_ERROR_INVALID_ARGUMENT;
1522 ret = mm_sound_client_dbus_get_unique_id(id);
1525 MMSOUND_LEAVE_CRITICAL_SECTION(&g_id_mutex);
1530 int mm_sound_client_is_focus_cb_thread(GThread *mine, bool *result)
1532 int ret = MM_ERROR_NONE;
1534 if (!mine || !result)
1535 ret = MM_ERROR_INVALID_ARGUMENT;
1537 if (mine == g_focus_thread)
1546 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)
1548 int ret = MM_ERROR_NONE;
1555 for (index = 0; index < FOCUS_HANDLE_MAX; index++) {
1556 if (g_focus_sound_handle[index].is_used == false) {
1557 g_focus_sound_handle[index].is_used = true;
1562 g_focus_sound_handle[index].focus_tid = instance;
1563 g_focus_sound_handle[index].handle = id;
1564 g_focus_sound_handle[index].focus_callback = callback;
1565 g_focus_sound_handle[index].user_data = user_data;
1566 g_focus_sound_handle[index].is_for_session = is_for_session;
1567 g_focus_sound_handle[index].auto_reacquire = true;
1569 ret = mm_sound_client_dbus_register_focus(id, pid, stream_type, callback, is_for_session, user_data);
1571 if (ret == MM_ERROR_NONE) {
1572 debug_msg("[Client] Success to register focus\n");
1573 if (!g_focus_thread) {
1574 GMainContext* focus_context = g_main_context_new ();
1575 g_focus_loop = g_main_loop_new (focus_context, FALSE);
1576 g_main_context_unref(focus_context);
1577 g_focus_thread = g_thread_new("focus-callback-thread", _focus_thread_func, NULL);
1578 if (g_focus_thread == NULL) {
1579 debug_error ("could not create thread..");
1580 g_main_loop_unref(g_focus_loop);
1581 g_focus_sound_handle[index].is_used = false;
1582 ret = MM_ERROR_SOUND_INTERNAL;
1587 debug_error("[Client] Error occurred : %d \n",ret);
1588 g_focus_sound_handle[index].is_used = false;
1592 _focus_init_callback(index, false);
1600 int mm_sound_client_unregister_focus(int id)
1602 int ret = MM_ERROR_NONE;
1607 index = _focus_find_index_by_handle(id);
1609 debug_error("Could not find index");
1610 return MM_ERROR_INVALID_ARGUMENT;
1612 instance = g_focus_sound_handle[index].focus_tid;
1614 if (!g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1615 debug_warning("maybe focus_callback is being called, try one more time..");
1616 usleep(2500000); // 2.5 sec
1617 if (g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1618 debug_msg("finally got focus_lock");
1622 ret = mm_sound_client_dbus_unregister_focus(instance, id, g_focus_sound_handle[index].is_for_session);
1624 if (ret == MM_ERROR_NONE)
1625 debug_msg("[Client] Success to unregister focus\n");
1627 debug_error("[Client] Error occurred : %d \n",ret);
1629 g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
1631 _focus_destroy_callback(index, false);
1632 g_focus_sound_handle[index].focus_fd = 0;
1633 g_focus_sound_handle[index].focus_tid = 0;
1634 g_focus_sound_handle[index].handle = 0;
1635 g_focus_sound_handle[index].is_used = false;
1641 int mm_sound_client_set_focus_reacquisition(int id, bool reacquisition)
1643 int ret = MM_ERROR_NONE;
1648 index = _focus_find_index_by_handle(id);
1650 debug_error("Could not find index");
1651 return MM_ERROR_INVALID_ARGUMENT;
1654 g_focus_sound_handle[index].auto_reacquire = reacquisition;
1661 int mm_sound_client_get_focus_reacquisition(int id, bool *reacquisition)
1663 int ret = MM_ERROR_NONE;
1668 if (!reacquisition) {
1669 debug_error("Invalid parameter");
1670 return MM_ERROR_INVALID_ARGUMENT;
1673 index = _focus_find_index_by_handle(id);
1675 debug_error("Could not find index");
1676 return MM_ERROR_INVALID_ARGUMENT;
1679 *reacquisition = g_focus_sound_handle[index].auto_reacquire;
1686 int mm_sound_client_acquire_focus(int id, mm_sound_focus_type_e type, const char *option)
1688 int ret = MM_ERROR_NONE;
1694 index = _focus_find_index_by_handle(id);
1696 debug_error("Could not find index");
1697 return MM_ERROR_INVALID_ARGUMENT;
1699 instance = g_focus_sound_handle[index].focus_tid;
1701 ret = mm_sound_client_dbus_acquire_focus(instance, id, type, option, g_focus_sound_handle[index].is_for_session);
1703 if (ret == MM_ERROR_NONE)
1704 debug_msg("[Client] Success to acquire focus\n");
1706 debug_error("[Client] Error occurred : %d \n",ret);
1712 int mm_sound_client_release_focus(int id, mm_sound_focus_type_e type, const char *option)
1714 int ret = MM_ERROR_NONE;
1719 index = _focus_find_index_by_handle(id);
1721 debug_error("Could not find index");
1722 return MM_ERROR_INVALID_ARGUMENT;
1724 instance = g_focus_sound_handle[index].focus_tid;
1726 ret = mm_sound_client_dbus_release_focus(instance, id, type, option, g_focus_sound_handle[index].is_for_session);
1728 if (ret == MM_ERROR_NONE)
1729 debug_msg("[Client] Success to release focus\n");
1731 debug_error("[Client] Error occurred : %d \n",ret);
1738 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)
1740 int ret = MM_ERROR_NONE;
1746 return MM_ERROR_INVALID_ARGUMENT;
1748 //pthread_mutex_lock(&g_thread_mutex2);
1752 ret = mm_sound_client_dbus_get_unique_id(id);
1756 for (index = 0; index < FOCUS_HANDLE_MAX; index++) {
1757 if (g_focus_sound_handle[index].is_used == false) {
1758 g_focus_sound_handle[index].is_used = true;
1763 g_focus_sound_handle[index].focus_tid = instance;
1764 g_focus_sound_handle[index].handle = *id;
1765 g_focus_sound_handle[index].watch_callback = callback;
1766 g_focus_sound_handle[index].user_data = user_data;
1767 g_focus_sound_handle[index].is_for_session = is_for_session;
1769 ret = mm_sound_client_dbus_set_focus_watch_callback(pid, g_focus_sound_handle[index].handle, focus_type, callback, is_for_session, user_data);
1771 if (ret == MM_ERROR_NONE) {
1772 debug_msg("[Client] Success to watch focus");
1773 if (!g_focus_thread) {
1774 GMainContext* focus_context = g_main_context_new ();
1775 g_focus_loop = g_main_loop_new (focus_context, FALSE);
1776 g_main_context_unref(focus_context);
1777 g_focus_thread = g_thread_new("focus-callback-thread", _focus_thread_func, NULL);
1778 if (g_focus_thread == NULL) {
1779 debug_error ("could not create thread..");
1780 g_main_loop_unref(g_focus_loop);
1781 ret = MM_ERROR_SOUND_INTERNAL;
1786 debug_error("[Client] Error occurred : %d",ret);
1790 _focus_init_callback(index, true);
1795 g_focus_sound_handle[index].is_used = false;
1802 int mm_sound_client_unset_focus_watch_callback(int id)
1804 int ret = MM_ERROR_NONE;
1808 index = _focus_find_index_by_handle(id);
1810 debug_error("Could not find index");
1811 return MM_ERROR_INVALID_ARGUMENT;
1814 g_mutex_lock(&g_focus_sound_handle[index].focus_lock);
1816 ret = mm_sound_client_dbus_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);
1818 if (ret == MM_ERROR_NONE)
1819 debug_msg("[Client] Success to unwatch focus\n");
1821 debug_error("[Client] Error occurred : %d \n",ret);
1824 g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
1826 _focus_destroy_callback(index, true);
1827 g_focus_sound_handle[index].focus_fd = 0;
1828 g_focus_sound_handle[index].focus_tid = 0;
1829 g_focus_sound_handle[index].handle = 0;
1830 g_focus_sound_handle[index].is_used = false;
1838 int mm_sound_client_add_test_callback(mm_sound_test_cb func, void* user_data, unsigned int *subs_id)
1840 int ret = MM_ERROR_NONE;
1844 ret = mm_sound_client_dbus_add_test_callback(func, user_data, subs_id);
1850 int mm_sound_client_remove_test_callback(unsigned int subs_id)
1852 int ret = MM_ERROR_NONE;
1855 ret = mm_sound_client_dbus_remove_test_callback(subs_id);
1862 int mm_sound_client_test(int a, int b, int* getv)
1864 int ret = MM_ERROR_NONE;
1868 ret = mm_sound_client_dbus_test(a, b, getv);
1869 debug_log("%d * %d -> result : %d", a, b, *getv);