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 pthread_mutex_t g_id_mutex = PTHREAD_MUTEX_INITIALIZER;
127 guint g_focus_signal_handle = 0;
129 void _focus_signal_handler(int signo)
131 int ret = MM_ERROR_NONE;
134 sigset_t old_mask, all_mask;
136 debug_error("Got signal : signo(%d)", signo);
140 sigfillset(&all_mask);
141 sigprocmask(SIG_BLOCK, &all_mask, &old_mask);
145 /* need implementation */
146 //send exit pid to focus server and focus server will clear focus or watch if necessary.
148 for (index = 0; index < FOCUS_HANDLE_MAX; index++) {
149 if (g_focus_sound_handle[index].is_used == true && g_focus_sound_handle[index].focus_tid == exit_pid) {
150 ret = mm_sound_client_dbus_emergent_exit_focus(exit_pid);
155 if (ret == MM_ERROR_NONE)
156 debug_msg("[Client] Success to emergnet_exit_focus\n");
158 debug_error("[Client] Error occurred : 0x%x \n",ret);
160 sigprocmask(SIG_SETMASK, &old_mask, NULL);
165 sigaction(SIGINT, &FOCUS_int_old_action, NULL);
169 sigaction(SIGABRT, &FOCUS_abrt_old_action, NULL);
173 sigaction(SIGSEGV, &FOCUS_segv_old_action, NULL);
177 sigaction(SIGTERM, &FOCUS_term_old_action, NULL);
181 sigaction(SIGSYS, &FOCUS_sys_old_action, NULL);
185 sigaction(SIGXCPU, &FOCUS_xcpu_old_action, NULL);
192 debug_error("signal handling end");
195 int mm_sound_client_initialize(void)
197 int ret = MM_ERROR_NONE;
200 mm_sound_client_dbus_initialize();
204 struct sigaction FOCUS_action;
205 FOCUS_action.sa_handler = _focus_signal_handler;
206 FOCUS_action.sa_flags = SA_NOCLDSTOP;
208 sigemptyset(&FOCUS_action.sa_mask);
210 sigaction(SIGINT, &FOCUS_action, &FOCUS_int_old_action);
211 sigaction(SIGABRT, &FOCUS_action, &FOCUS_abrt_old_action);
212 sigaction(SIGSEGV, &FOCUS_action, &FOCUS_segv_old_action);
213 sigaction(SIGTERM, &FOCUS_action, &FOCUS_term_old_action);
214 sigaction(SIGSYS, &FOCUS_action, &FOCUS_sys_old_action);
215 sigaction(SIGXCPU, &FOCUS_action, &FOCUS_xcpu_old_action);
224 int mm_sound_client_finalize(void)
226 int ret = MM_ERROR_NONE;
230 ret = mm_sound_client_dbus_finalize();
239 for (index = 0; index < FOCUS_HANDLE_MAX; index++) {
240 if (g_focus_sound_handle[index].is_used == true && g_focus_sound_handle[index].focus_tid == exit_pid) {
241 mm_sound_client_dbus_emergent_exit_focus(exit_pid);
245 if (g_focus_thread) {
246 g_main_loop_quit(g_focus_loop);
247 g_thread_join(g_focus_thread);
248 debug_log("after thread join");
249 g_main_loop_unref(g_focus_loop);
250 g_focus_thread = NULL;
253 /* is it necessary? */
254 sigaction(SIGINT, &FOCUS_int_old_action, NULL);
255 sigaction(SIGABRT, &FOCUS_abrt_old_action, NULL);
256 sigaction(SIGSEGV, &FOCUS_segv_old_action, NULL);
257 sigaction(SIGTERM, &FOCUS_term_old_action, NULL);
258 sigaction(SIGSYS, &FOCUS_sys_old_action, NULL);
259 sigaction(SIGXCPU, &FOCUS_xcpu_old_action, NULL);
267 void mm_sound_convert_volume_type_to_stream_type(int volume_type, char *stream_type)
269 switch (volume_type) {
270 case VOLUME_TYPE_SYSTEM:
271 MMSOUND_STRNCPY(stream_type, "system", MM_SOUND_STREAM_TYPE_LEN);
273 case VOLUME_TYPE_NOTIFICATION:
274 MMSOUND_STRNCPY(stream_type, "notification", MM_SOUND_STREAM_TYPE_LEN);
276 case VOLUME_TYPE_ALARM:
277 MMSOUND_STRNCPY(stream_type, "alarm", MM_SOUND_STREAM_TYPE_LEN);
279 case VOLUME_TYPE_RINGTONE:
280 MMSOUND_STRNCPY(stream_type, "ringtone-voip", MM_SOUND_STREAM_TYPE_LEN);
282 case VOLUME_TYPE_MEDIA:
283 MMSOUND_STRNCPY(stream_type, "media", MM_SOUND_STREAM_TYPE_LEN);
285 case VOLUME_TYPE_CALL:
286 MMSOUND_STRNCPY(stream_type, "call-voice", MM_SOUND_STREAM_TYPE_LEN);
288 case VOLUME_TYPE_VOIP:
289 MMSOUND_STRNCPY(stream_type, "voip", MM_SOUND_STREAM_TYPE_LEN);
291 case VOLUME_TYPE_VOICE:
292 MMSOUND_STRNCPY(stream_type, "voice-recognition", MM_SOUND_STREAM_TYPE_LEN);
295 MMSOUND_STRNCPY(stream_type, "media", MM_SOUND_STREAM_TYPE_LEN);
299 debug_error("volume type (%d) converted to stream type (%s)", volume_type, stream_type);
303 /*****************************************************************************************
304 DBUS SUPPORTED FUNCTIONS
305 ******************************************************************************************/
307 void _mm_sound_client_focus_signal_callback(mm_sound_signal_name_t signal, int value, void *user_data)
309 int ret = MM_ERROR_NONE;
312 debug_error("focus signal received, value = %d", value);
315 ret = mm_sound_client_dbus_clear_focus(getpid());
317 debug_error("clear focus failed ret = 0x%x", ret);
318 mm_sound_unsubscribe_signal(g_focus_signal_handle);
319 g_focus_signal_handle = 0;
323 int mm_sound_client_play_tone(int number, int volume_config, double volume, int time, int *handle, bool enable_session)
325 int ret = MM_ERROR_NONE;
326 // int instance = -1; /* instance is unique to communicate with server : client message queue filter type */
327 int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(volume_config);
328 char stream_type[MM_SOUND_STREAM_TYPE_LEN] = {0, };
332 /* read session information */
333 int session_type = MM_SESSION_TYPE_MEDIA;
334 int session_options = 0;
335 int is_focus_registered = 0;
337 ret = mm_sound_get_signal_value(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &is_focus_registered);
339 debug_error("mm_sound_get_signal_value failed [0x%x]", ret);
340 return MM_ERROR_POLICY_INTERNAL;
343 if (is_focus_registered)
344 enable_session = false;
346 if (enable_session) {
347 if (MM_ERROR_NONE != _mm_session_util_read_information(-1, &session_type, &session_options)) {
348 debug_warning("[Client] Read Session Information failed. use default \"media\" type\n");
349 session_type = MM_SESSION_TYPE_MEDIA;
351 if(MM_ERROR_NONE != mm_session_init(session_type)) {
352 debug_critical("[Client] MMSessionInit() failed\n");
353 return MM_ERROR_POLICY_INTERNAL;
358 // instance = getpid();
359 //debug_log("[Client] pid for client ::: [%d]\n", instance);
362 debug_msg("[Client] Input number : %d\n", number);
363 /* Send req memory */
365 mm_sound_convert_volume_type_to_stream_type(volume_type, stream_type);
366 ret = mm_sound_client_dbus_play_tone(number, time, volume, volume_config,
367 session_type, session_options, getpid(), enable_session, handle, stream_type, -1);
369 if (enable_session && !g_focus_signal_handle) {
370 ret = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &g_focus_signal_handle, _mm_sound_client_focus_signal_callback, NULL);
372 debug_error("mm_sound_subscribe_signal failed [0x%x]", ret);
373 return MM_ERROR_POLICY_INTERNAL;
381 int mm_sound_client_play_tone_with_stream_info(int tone, char *stream_type, int stream_id, double volume, int duration, int *handle)
383 int ret = MM_ERROR_NONE;
387 ret = mm_sound_client_dbus_play_tone_with_stream_info(getpid(), tone, stream_type, stream_id, volume, duration, handle);
393 void _mm_sound_stop_callback_wrapper_func(int ended_handle, void *userdata)
395 client_cb_data_t *cb_data = (client_cb_data_t*) userdata;
397 debug_log("[Wrapper CB][Play Stop] ended_handle : %d", ended_handle);
399 if (cb_data == NULL) {
400 debug_warning("stop callback data null");
403 if (ended_handle == cb_data->mask) {
404 debug_log("Interested playing handle end : %d", ended_handle);
405 ((mm_sound_stop_callback_func)(cb_data->user_cb))(cb_data->user_data, ended_handle);
406 if (mm_sound_client_dbus_remove_play_sound_end_callback(cb_data->subs_id) != MM_ERROR_NONE)
407 debug_error("mm_sound_client_dbus_remove_play_file_end_callback failed");
409 debug_log("Not interested playing handle : %d", ended_handle);
413 int mm_sound_client_play_sound(MMSoundPlayParam *param, int tone, int *handle)
415 int ret = MM_ERROR_NONE;
416 int session_type = MM_SESSION_TYPE_MEDIA;
417 int session_options = 0;
418 int is_focus_registered = 0;
419 // int instance = -1; /* instance is unique to communicate with server : client message queue filter type */
420 int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(param->volume_config);
421 char stream_type[MM_SOUND_STREAM_TYPE_LEN] = {0, };
422 client_cb_data_t *cb_data = NULL;
426 /* read session information */
428 ret = mm_sound_get_signal_value(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &is_focus_registered);
430 debug_error("mm_sound_get_signal_value failed [0x%x]", ret);
431 return MM_ERROR_POLICY_INTERNAL;
434 if (is_focus_registered)
435 param->skip_session = true;
437 if (param->skip_session == false) {
438 if(MM_ERROR_NONE != _mm_session_util_read_information(-1, &session_type, &session_options)) {
439 debug_warning("[Client] Read MMSession Type failed. use default \"media\" type\n");
440 session_type = MM_SESSION_TYPE_MEDIA;
442 if(MM_ERROR_NONE != mm_session_init(session_type)) {
443 debug_critical("[Client] MMSessionInit() failed\n");
444 return MM_ERROR_POLICY_INTERNAL;
450 if ((param->mem_ptr && param->mem_size)) {
451 // Play memory, deprecated
452 return MM_ERROR_INVALID_ARGUMENT;
455 mm_sound_convert_volume_type_to_stream_type(volume_type, stream_type);
456 ret = mm_sound_client_dbus_play_sound(param->filename, tone, param->loop, param->volume, param->volume_config,
457 param->priority, session_type, session_options, getpid(), param->handle_route,
458 !param->skip_session, handle, stream_type, -1);
459 if (ret != MM_ERROR_NONE) {
460 debug_error("Play Sound Failed");
463 if (param->callback) {
464 cb_data = g_malloc0(sizeof(client_cb_data_t));
465 cb_data->user_cb = param->callback;
466 cb_data->user_data = param->data;
467 cb_data->mask = *handle;
469 ret = mm_sound_client_dbus_add_play_sound_end_callback(_mm_sound_stop_callback_wrapper_func, cb_data, &cb_data->subs_id);
470 if (ret != MM_ERROR_NONE) {
471 debug_error("Add callback for play sound(%d) Failed", *handle);
474 if (!param->skip_session && !g_focus_signal_handle) {
475 ret = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &g_focus_signal_handle, _mm_sound_client_focus_signal_callback, NULL);
477 debug_error("mm_sound_subscribe_signal failed [0x%x]", ret);
478 return MM_ERROR_POLICY_INTERNAL;
488 int mm_sound_client_play_sound_with_stream_info(MMSoundPlayParam *param, int *handle, char* stream_type, int stream_id)
490 int ret = MM_ERROR_NONE;
491 client_cb_data_t *cb_data = NULL;
493 ret = mm_sound_client_dbus_play_sound_with_stream_info(param->filename, param->loop, param->volume,
494 param->priority, getpid(), param->handle_route, handle, stream_type, stream_id);
495 if (ret != MM_ERROR_NONE) {
496 debug_error("Play Sound Failed");
499 if (param->callback) {
500 cb_data = g_malloc0(sizeof(client_cb_data_t));
501 cb_data->user_cb = param->callback;
502 cb_data->user_data = param->data;
503 cb_data->mask = *handle;
505 ret = mm_sound_client_dbus_add_play_sound_end_callback(_mm_sound_stop_callback_wrapper_func, cb_data, &cb_data->subs_id);
506 if (ret != MM_ERROR_NONE) {
507 debug_error("Add callback for play sound(%d) Failed", *handle);
518 int mm_sound_client_stop_sound(int handle)
520 int ret = MM_ERROR_NONE;
523 if (handle < 0 || handle > CLIENT_HANDLE_MAX) {
524 ret = MM_ERROR_INVALID_ARGUMENT;
528 ret = mm_sound_client_dbus_stop_sound(handle);
534 static int _mm_sound_client_device_list_dump (GList *device_list)
536 int ret = MM_ERROR_NONE;
538 mm_sound_device_t *device_node = NULL;
541 debug_error("Device list NULL, cannot dump list");
542 return MM_ERROR_SOUND_INTERNAL;
545 debug_log("======================== device list : start ==========================\n");
546 for (list = device_list; list != NULL; list = list->next) {
547 device_node = (mm_sound_device_t *)list->data;
549 debug_log(" list idx[%d]: type[%17s], id[%02d], io_direction[%d], state[%d], name[%s]\n",
550 count++, device_node->type, device_node->id, device_node->io_direction, device_node->state, device_node->name);
553 debug_log("======================== device list : end ============================\n");
558 int mm_sound_client_get_current_connected_device_list(int device_flags, mm_sound_device_list_t *device_list)
560 int ret = MM_ERROR_NONE;
564 debug_error("Device list NULL");
565 ret = MM_ERROR_COMMON_INVALID_ARGUMENT;
569 if ((ret = mm_sound_client_dbus_get_current_connected_device_list(device_flags, &device_list->list)) != MM_ERROR_NONE) {
570 debug_error("[Client] failed to get current connected device list with dbus, ret[0x%x]", ret);
573 if (!device_list->list) {
574 debug_error("Got device list null");
575 ret = MM_ERROR_SOUND_NO_DATA;
578 _mm_sound_client_device_list_dump(device_list->list);
585 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)
587 mm_sound_device_t device_h;
588 client_cb_data_t *cb_data = (client_cb_data_t*) userdata;
590 debug_log("[Wrapper CB][Device Connnected] device_id : %d, device_type : %s, direction : %d, state : %d, name : %s, is_connected : %d",
591 device_id, device_type, io_direction, state, name, is_connected);
593 if (cb_data == NULL) {
594 debug_warning("device connected changed callback data null");
598 device_h.id = device_id;
599 device_h.io_direction = io_direction;
600 device_h.state = state;
601 MMSOUND_STRNCPY(device_h.name, name, MAX_DEVICE_NAME_NUM);
602 MMSOUND_STRNCPY(device_h.type, device_type, MAX_DEVICE_TYPE_STR_LEN);
604 ((mm_sound_device_connected_cb)(cb_data->user_cb))(&device_h, is_connected, cb_data->user_data);
607 int mm_sound_client_add_device_connected_callback(int device_flags, mm_sound_device_connected_cb func, void* user_data, unsigned int *subs_id)
609 int ret = MM_ERROR_NONE;
610 client_cb_data_t *cb_data = NULL;
614 cb_data = g_malloc0(sizeof(client_cb_data_t));
615 cb_data->user_cb = func;
616 cb_data->user_data = user_data;
618 ret = mm_sound_client_dbus_add_device_connected_callback(device_flags, _mm_sound_device_connected_callback_wrapper_func, cb_data, subs_id);
624 int mm_sound_client_remove_device_connected_callback(unsigned int subs_id)
626 int ret = MM_ERROR_NONE;
629 ret = mm_sound_client_dbus_remove_device_connected_callback(subs_id);
635 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)
637 mm_sound_device_t device_h;
638 client_cb_data_t *cb_data = (client_cb_data_t*) userdata;
640 debug_log("[Wrapper CB][Device Info Changed] device_id : %d, device_type : %s, direction : %d, state : %d, name : %s, changed_info_type : %d",
641 device_id, device_type, io_direction, state, name, changed_device_info_type);
643 if (cb_data == NULL) {
644 debug_warning("device info changed callback data null");
648 device_h.id = device_id;
649 device_h.io_direction = io_direction;
650 device_h.state = state;
651 MMSOUND_STRNCPY(device_h.name, name, MAX_DEVICE_NAME_NUM);
652 MMSOUND_STRNCPY(device_h.type, device_type, MAX_DEVICE_TYPE_STR_LEN);
654 ((mm_sound_device_info_changed_cb)(cb_data->user_cb))(&device_h, changed_device_info_type, cb_data->user_data);
657 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)
659 int ret = MM_ERROR_NONE;
660 client_cb_data_t *cb_data = (client_cb_data_t*) userdata;
664 cb_data = g_malloc0(sizeof(client_cb_data_t));
665 cb_data->user_cb = func;
666 cb_data->user_data = userdata;
668 ret = mm_sound_client_dbus_add_device_info_changed_callback(device_flags, _mm_sound_device_info_changed_callback_wrapper_func, cb_data, subs_id);
674 int mm_sound_client_remove_device_info_changed_callback(unsigned int subs_id)
676 int ret = MM_ERROR_NONE;
679 ret = mm_sound_client_dbus_remove_device_info_changed_callback(subs_id);
686 int __convert_volume_type_to_str(int volume_type, char **volume_type_str)
688 int ret = MM_ERROR_NONE;
690 if (!volume_type_str) {
691 return MM_ERROR_COMMON_INVALID_ARGUMENT;
694 switch (volume_type) {
695 case VOLUME_TYPE_SYSTEM:
696 *volume_type_str = "system";
698 case VOLUME_TYPE_NOTIFICATION:
699 *volume_type_str = "notification";
701 case VOLUME_TYPE_ALARM:
702 *volume_type_str = "alarm";
704 case VOLUME_TYPE_RINGTONE:
705 *volume_type_str = "ringtone";
707 case VOLUME_TYPE_MEDIA:
708 *volume_type_str = "media";
710 case VOLUME_TYPE_CALL:
711 *volume_type_str = "call";
713 case VOLUME_TYPE_VOIP:
714 *volume_type_str = "voip";
716 case VOLUME_TYPE_VOICE:
717 *volume_type_str = "voice";
720 if (!strncmp(*volume_type_str,"", VOLUME_TYPE_LEN)) {
721 debug_error("could not find the volume_type[%d] in this switch case statement", volume_type);
722 ret = MM_ERROR_SOUND_INTERNAL;
724 debug_log("volume_type[%s]", *volume_type_str);
729 static int __convert_volume_type_to_int(const char *volume_type_str, volume_type_t *volume_type)
731 int ret = MM_ERROR_NONE;
733 if (!volume_type || !volume_type_str) {
734 return MM_ERROR_COMMON_INVALID_ARGUMENT;
737 if (!strncmp(volume_type_str, "system", VOLUME_TYPE_LEN)) {
738 *volume_type = VOLUME_TYPE_SYSTEM;
739 } else if (!strncmp(volume_type_str, "notification", VOLUME_TYPE_LEN)) {
740 *volume_type = VOLUME_TYPE_NOTIFICATION;
741 } else if (!strncmp(volume_type_str, "alarm", VOLUME_TYPE_LEN)) {
742 *volume_type = VOLUME_TYPE_ALARM;
743 } else if (!strncmp(volume_type_str, "ringtone", VOLUME_TYPE_LEN)) {
744 *volume_type = VOLUME_TYPE_RINGTONE;
745 } else if (!strncmp(volume_type_str, "media", VOLUME_TYPE_LEN)) {
746 *volume_type = VOLUME_TYPE_MEDIA;
747 } else if (!strncmp(volume_type_str, "call", VOLUME_TYPE_LEN)) {
748 *volume_type = VOLUME_TYPE_CALL;
749 } else if (!strncmp(volume_type_str, "voip", VOLUME_TYPE_LEN)) {
750 *volume_type = VOLUME_TYPE_VOIP;
751 } else if (!strncmp(volume_type_str, "voice", VOLUME_TYPE_LEN)) {
752 *volume_type = VOLUME_TYPE_VOICE;
754 debug_log("Invalid volume type : [%s]", volume_type_str);
755 ret = MM_ERROR_SOUND_INTERNAL;
761 int mm_sound_client_set_volume_by_type(const int volume_type, const unsigned int volume_level)
763 int ret = MM_ERROR_NONE;
764 char *type_str = NULL;
767 if ((ret = __convert_volume_type_to_str(volume_type, &type_str)) != MM_ERROR_NONE) {
768 debug_error("volume type convert failed");
772 ret = mm_sound_client_dbus_set_volume_by_type(type_str, volume_level);
779 static void _mm_sound_volume_changed_callback_wrapper_func(const char *direction, const char *volume_type_str, int volume_level, void *userdata)
781 volume_type_t volume_type = 0;
782 client_cb_data_t *cb_data = (client_cb_data_t*) userdata;
784 debug_log("[Wrapper CB][Volume Changed] direction : %s, volume_type : %s, volume_level : %d", direction, volume_type_str, volume_level);
786 if (cb_data == NULL) {
787 debug_warning("volume changed callback data null");
791 if (__convert_volume_type_to_int(volume_type_str, &volume_type) != MM_ERROR_NONE) {
792 debug_error("volume type convert failed");
795 debug_log("Call volume changed user cb, direction : %s, vol_type : %s(%d), level : %u", direction, volume_type_str, volume_type, volume_level);
796 ((mm_sound_volume_changed_cb)(cb_data->user_cb))(volume_type, volume_level, cb_data->user_data);
799 int mm_sound_client_add_volume_changed_callback(mm_sound_volume_changed_cb func, void* user_data, unsigned int *subs_id)
801 int ret = MM_ERROR_NONE;
802 client_cb_data_t *cb_data = NULL;
806 cb_data = g_malloc0(sizeof(client_cb_data_t));
807 cb_data->user_cb = func;
808 cb_data->user_data = user_data;
810 ret = mm_sound_client_dbus_add_volume_changed_callback(_mm_sound_volume_changed_callback_wrapper_func, cb_data, subs_id);
817 int mm_sound_client_remove_volume_changed_callback(unsigned int subs_id)
819 int ret = MM_ERROR_NONE;
822 ret = mm_sound_client_dbus_remove_volume_changed_callback(subs_id);
829 int mm_sound_client_set_session_interrupt_callback(mm_sound_focus_session_interrupt_cb callback, void* user_data)
831 int ret = MM_ERROR_NONE;
836 return MM_ERROR_INVALID_ARGUMENT;
838 g_focus_session_interrupt_info.user_cb = callback;
839 g_focus_session_interrupt_info.user_data = user_data;
845 int mm_sound_client_unset_session_interrupt_callback(void)
847 int ret = MM_ERROR_NONE;
851 if (!g_focus_session_interrupt_info.user_cb) {
852 debug_error("no callback to unset");
853 return MM_ERROR_SOUND_INTERNAL;
856 g_focus_session_interrupt_info.user_cb = NULL;
857 g_focus_session_interrupt_info.user_data = NULL;
863 static gpointer _focus_thread_func(gpointer data)
865 debug_log(">>> thread func..ID of this thread(%u)\n", (unsigned int)pthread_self());
866 g_main_loop_run(g_focus_loop);
867 debug_log("<<< quit thread func..\n");
871 static gboolean _focus_fd_check(GSource * source)
877 debug_error("GSource is null");
880 fd_list = source->poll_fds;
882 debug_error("fd_list is null");
886 temp = (GPollFD*)fd_list->data;
888 debug_error("fd_list->data is null");
891 if (temp->revents & (POLLIN | POLLPRI)) {
894 fd_list = fd_list->next;
897 return FALSE; /* there is no change in any fd state */
900 static gboolean _focus_fd_prepare(GSource *source, gint *timeout)
905 static gboolean _focus_fd_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
912 static int _focus_find_index_by_handle(int handle)
915 for(i = 0; i< FOCUS_HANDLE_MAX; i++) {
916 if (handle == g_focus_sound_handle[i].handle) {
917 //debug_msg("found index(%d) for handle(%d)", i, handle);
918 if (handle == FOCUS_HANDLE_INIT_VAL) {
927 static gboolean _focus_callback_handler(gpointer d)
929 GPollFD *data = (GPollFD*)d;
933 focus_cb_data_lib cb_data;
934 debug_log(">>> focus_callback_handler()..ID of this thread(%u)\n", (unsigned int)pthread_self());
936 memset(&cb_data, 0, sizeof(focus_cb_data_lib));
939 debug_error("GPollFd is null");
942 if (data->revents & (POLLIN | POLLPRI)) {
943 int changed_state = -1;
945 count = read(data->fd, &cb_data, sizeof(cb_data));
948 strerror_r(errno, str_error, sizeof(str_error));
949 debug_error("GpollFD read fail, errno=%d(%s)",errno, str_error);
952 changed_state = cb_data.state;
953 focus_index = _focus_find_index_by_handle(cb_data.handle);
954 if (focus_index == -1) {
955 debug_error("Could not find index");
959 g_mutex_lock(&g_focus_sound_handle[focus_index].focus_lock);
961 tid = g_focus_sound_handle[focus_index].focus_tid;
963 if (changed_state != -1) {
964 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);
965 if (g_focus_sound_handle[focus_index].focus_callback== NULL) {
966 debug_error("callback is null..");
967 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
970 debug_error("[CALLBACK(%p) START]",g_focus_sound_handle[focus_index].focus_callback);
971 (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);
972 debug_error("[CALLBACK END]");
973 if (g_focus_session_interrupt_info.user_cb) {
974 debug_error("sending session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
975 (g_focus_session_interrupt_info.user_cb)(cb_data.state, cb_data.stream_type, false, g_focus_session_interrupt_info.user_data);
978 #ifdef CONFIG_ENABLE_RETCB
982 unsigned int buf = 0;
983 char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", g_focus_sound_handle[focus_index].focus_tid, cb_data.handle);
984 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
987 strerror_r(errno, str_error, sizeof(str_error));
988 debug_error("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n", tid, tmpfd, filename2, errno, str_error);
990 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
993 buf = (unsigned int)((0x0000ffff & cb_data.handle) |(g_focus_sound_handle[focus_index].auto_reacquire << 16));
994 rett = write(tmpfd, &buf, sizeof(buf));
997 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
1001 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1006 static gboolean _focus_watch_callback_handler( gpointer d)
1008 GPollFD *data = (GPollFD*)d;
1011 int focus_index = 0;
1012 focus_cb_data_lib cb_data;
1016 memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1019 debug_error("GPollFd is null");
1022 if (data->revents & (POLLIN | POLLPRI)) {
1023 count = read(data->fd, &cb_data, sizeof(cb_data));
1025 char str_error[256];
1026 strerror_r(errno, str_error, sizeof(str_error));
1027 debug_error("GpollFD read fail, errno=%d(%s)",errno, str_error);
1031 focus_index = _focus_find_index_by_handle(cb_data.handle);
1032 if (focus_index == -1) {
1033 debug_error("Could not find index");
1037 debug_error("lock focus_lock = %p", &g_focus_sound_handle[focus_index].focus_lock);
1038 g_mutex_lock(&g_focus_sound_handle[focus_index].focus_lock);
1040 tid = g_focus_sound_handle[focus_index].focus_tid;
1042 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);
1044 if (g_focus_sound_handle[focus_index].watch_callback == NULL) {
1045 debug_msg("callback is null..");
1047 debug_msg("[CALLBACK(%p) START]",g_focus_sound_handle[focus_index].watch_callback);
1048 (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);
1049 debug_msg("[CALLBACK END]");
1050 if (g_focus_session_interrupt_info.user_cb) {
1051 debug_error("sending session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
1052 (g_focus_session_interrupt_info.user_cb)(cb_data.state, cb_data.stream_type, true, g_focus_session_interrupt_info.user_data);
1056 #ifdef CONFIG_ENABLE_RETCB
1061 char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.wchr", g_focus_sound_handle[focus_index].focus_tid);
1062 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1064 char str_error[256];
1065 strerror_r(errno, str_error, sizeof(str_error));
1066 debug_error("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n", tid, tmpfd, filename2, errno, str_error);
1068 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1071 buf = cb_data.handle;
1072 rett = write(tmpfd, &buf, sizeof(buf));
1075 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
1081 debug_error("unlock focus_lock = %p", &g_focus_sound_handle[focus_index].focus_lock);
1082 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1090 static void _focus_open_callback(int index, bool is_for_watching)
1097 if (is_for_watching) {
1098 filename = g_strdup_printf("/tmp/FOCUS.%d.wch", g_focus_sound_handle[index].focus_tid);
1100 filename = g_strdup_printf("/tmp/FOCUS.%d.%d", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1102 pre_mask = umask(0);
1103 if (mknod(filename, S_IFIFO|0666, 0)) {
1104 debug_error("mknod() failure, errno(%d)", errno);
1107 g_focus_sound_handle[index].focus_fd = open( filename, O_RDWR|O_NONBLOCK);
1108 if (g_focus_sound_handle[index].focus_fd == -1) {
1109 debug_error("Open fail : index(%d), file open error(%d)", index, errno);
1111 debug_log("Open sucess : index(%d), filename(%s), fd(%d)", index, filename, g_focus_sound_handle[index].focus_fd);
1116 #ifdef CONFIG_ENABLE_RETCB
1119 if (is_for_watching) {
1120 filename2 = g_strdup_printf("/tmp/FOCUS.%d.wchr", g_focus_sound_handle[index].focus_tid);
1122 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1124 pre_mask = umask(0);
1125 if (mknod(filename2, S_IFIFO | 0666, 0)) {
1126 debug_error("mknod() failure, errno(%d)", errno);
1136 void _focus_close_callback(int index, bool is_for_watching)
1140 if (g_focus_sound_handle[index].focus_fd < 0) {
1141 debug_error("Close fail : fd error.");
1144 if (is_for_watching) {
1145 filename = g_strdup_printf("/tmp/FOCUS.%d.wch", g_focus_sound_handle[index].focus_tid);
1147 filename = g_strdup_printf("/tmp/FOCUS.%d.%d", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1149 close(g_focus_sound_handle[index].focus_fd);
1150 if (remove(filename)) {
1151 debug_error("remove() failure, filename(%s), errno(%d)", filename, errno);
1153 debug_log("Close Sucess : index(%d), filename(%s)", index, filename);
1158 #ifdef CONFIG_ENABLE_RETCB
1162 if (is_for_watching) {
1163 filename2 = g_strdup_printf("/tmp/FOCUS.%d.wchr", g_focus_sound_handle[index].focus_tid);
1165 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1168 /* Defensive code - wait until callback timeout although callback is removed */
1169 int buf = MM_ERROR_NONE; //no need to specify cb result to server, just notice if the client got the callback properly or not
1172 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1174 char str_error[256];
1175 strerror_r(errno, str_error, sizeof(str_error));
1176 debug_warning("could not open file(%s) (may server close it first), tid(%d) fd(%d) %s errno=%d(%s)",
1177 filename2, g_focus_sound_handle[index].focus_tid, tmpfd, filename2, errno, str_error);
1179 debug_msg("write MM_ERROR_NONE(tid:%d) for waiting server", g_focus_sound_handle[index].focus_tid);
1180 written = write(tmpfd, &buf, sizeof(buf));
1184 if (remove(filename2)) {
1185 debug_error("remove() failure, filename(%s), errno(%d)", filename2, errno);
1193 static bool _focus_add_sound_callback(int index, int fd, gushort events, focus_gLoopPollHandler_t p_gloop_poll_handler )
1195 GSource* g_src = NULL;
1196 GSourceFuncs *g_src_funcs = NULL; /* handler function */
1197 guint gsource_handle;
1198 GPollFD *g_poll_fd = NULL; /* file descriptor */
1202 g_mutex_init(&g_focus_sound_handle[index].focus_lock);
1204 /* 1. make GSource Object */
1205 g_src_funcs = (GSourceFuncs *)g_malloc(sizeof(GSourceFuncs));
1207 debug_error("g_malloc failed on g_src_funcs");
1211 g_src_funcs->prepare = _focus_fd_prepare;
1212 g_src_funcs->check = _focus_fd_check;
1213 g_src_funcs->dispatch = _focus_fd_dispatch;
1214 g_src_funcs->finalize = NULL;
1215 g_src = g_source_new(g_src_funcs, sizeof(GSource));
1217 debug_error("g_malloc failed on m_readfd");
1220 g_focus_sound_handle[index].focus_src = g_src;
1221 g_focus_sound_handle[index].g_src_funcs = g_src_funcs;
1223 /* 2. add file description which used in g_loop() */
1224 g_poll_fd = (GPollFD *)g_malloc(sizeof(GPollFD));
1226 debug_error("g_malloc failed on g_poll_fd");
1230 g_poll_fd->events = events;
1231 g_focus_sound_handle[index].g_poll_fd = g_poll_fd;
1233 /* 3. combine g_source object and file descriptor */
1234 g_source_add_poll(g_src, g_poll_fd);
1235 gsource_handle = g_source_attach(g_src, g_main_loop_get_context(g_focus_loop));
1236 if (!gsource_handle) {
1237 debug_error(" Failed to attach the source to context");
1240 //g_source_unref(g_src);
1242 /* 4. set callback */
1243 g_source_set_callback(g_src, p_gloop_poll_handler,(gpointer)g_poll_fd, NULL);
1245 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)",
1246 g_src_funcs, g_poll_fd, gsource_handle, errno);
1254 static bool _focus_remove_sound_callback(int index, gushort events)
1260 g_mutex_clear(&g_focus_sound_handle[index].focus_lock);
1262 GSourceFuncs *g_src_funcs = g_focus_sound_handle[index].g_src_funcs;
1263 GPollFD *g_poll_fd = g_focus_sound_handle[index].g_poll_fd; /* store file descriptor */
1265 debug_error("g_poll_fd is null..");
1269 g_poll_fd->fd = g_focus_sound_handle[index].focus_fd;
1270 g_poll_fd->events = events;
1272 if (!g_focus_sound_handle[index].focus_src) {
1273 debug_error("FOCUS_sound_handle[%d].focus_src is null..", index);
1276 debug_log(" g_source_remove_poll : fd(%d), event(%x), errno(%d)", g_poll_fd->fd, g_poll_fd->events, errno);
1277 g_source_remove_poll(g_focus_sound_handle[index].focus_src, g_poll_fd);
1281 if (g_focus_sound_handle[index].focus_src) {
1282 g_source_destroy(g_focus_sound_handle[index].focus_src);
1283 if (!g_source_is_destroyed (g_focus_sound_handle[index].focus_src)) {
1284 debug_warning(" failed to g_source_destroy(), focus_src(0x%p)", g_focus_sound_handle[index].focus_src);
1287 debug_log(" g_free : g_src_funcs(%#X), g_poll_fd(%#X)", g_src_funcs, g_poll_fd);
1290 g_free(g_src_funcs);
1298 g_focus_sound_handle[index].g_src_funcs = NULL;
1299 g_focus_sound_handle[index].g_poll_fd = NULL;
1300 g_focus_sound_handle[index].focus_src = NULL;
1301 g_focus_sound_handle[index].focus_callback = NULL;
1302 g_focus_sound_handle[index].watch_callback = NULL;
1309 static void _focus_add_callback(int index, bool is_for_watching)
1312 if (!is_for_watching) {
1313 if (!_focus_add_sound_callback(index, g_focus_sound_handle[index].focus_fd, (gushort)POLLIN | POLLPRI, _focus_callback_handler)) {
1314 debug_error("failed to _focus_add_sound_callback()");
1317 } else { // need to check if it's necessary
1318 if (!_focus_add_sound_callback(index, g_focus_sound_handle[index].focus_fd, (gushort)POLLIN | POLLPRI, _focus_watch_callback_handler)) {
1319 debug_error("failed to _focus_add_sound_callback()");
1326 static void _focus_remove_callback(int index)
1329 if (!_focus_remove_sound_callback(index, (gushort)POLLIN | POLLPRI)) {
1330 debug_error("failed to __focus_remove_sound_callback()");
1336 static void _focus_init_callback(int index, bool is_for_watching)
1339 _focus_open_callback(index, is_for_watching);
1340 _focus_add_callback(index, is_for_watching);
1344 static void _focus_destroy_callback(int index, bool is_for_watching)
1347 _focus_remove_callback(index);
1348 _focus_close_callback(index, is_for_watching);
1352 int mm_sound_client_get_unique_id(int *id)
1354 int ret = MM_ERROR_NONE;
1356 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_id_mutex, MM_ERROR_SOUND_INTERNAL);
1360 ret = MM_ERROR_INVALID_ARGUMENT;
1362 ret = mm_sound_client_dbus_get_unique_id(id);
1365 MMSOUND_LEAVE_CRITICAL_SECTION(&g_id_mutex);
1370 int mm_sound_client_is_focus_cb_thread(GThread *mine, bool *result)
1372 int ret = MM_ERROR_NONE;
1374 if (!mine || !result)
1375 ret = MM_ERROR_INVALID_ARGUMENT;
1377 if (mine == g_focus_thread)
1386 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)
1388 int ret = MM_ERROR_NONE;
1395 for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
1396 if (g_focus_sound_handle[index].is_used == false) {
1397 g_focus_sound_handle[index].is_used = true;
1402 g_focus_sound_handle[index].focus_tid = instance;
1403 g_focus_sound_handle[index].handle = id;
1404 g_focus_sound_handle[index].focus_callback = callback;
1405 g_focus_sound_handle[index].user_data = user_data;
1406 g_focus_sound_handle[index].is_for_session = is_for_session;
1407 g_focus_sound_handle[index].auto_reacquire = true;
1409 ret = mm_sound_client_dbus_register_focus(id, pid, stream_type, callback, is_for_session, user_data);
1411 if (ret == MM_ERROR_NONE) {
1412 debug_msg("[Client] Success to register focus\n");
1413 if (!g_focus_thread) {
1414 GMainContext* focus_context = g_main_context_new ();
1415 g_focus_loop = g_main_loop_new (focus_context, FALSE);
1416 g_main_context_unref(focus_context);
1417 g_focus_thread = g_thread_new("focus-callback-thread", _focus_thread_func, NULL);
1418 if (g_focus_thread == NULL) {
1419 debug_error ("could not create thread..");
1420 g_main_loop_unref(g_focus_loop);
1421 g_focus_sound_handle[index].is_used = false;
1422 ret = MM_ERROR_SOUND_INTERNAL;
1427 debug_error("[Client] Error occurred : 0x%x \n",ret);
1428 g_focus_sound_handle[index].is_used = false;
1432 _focus_init_callback(index, false);
1440 int mm_sound_client_unregister_focus(int id)
1442 int ret = MM_ERROR_NONE;
1447 index = _focus_find_index_by_handle(id);
1449 debug_error("Could not find index");
1450 return MM_ERROR_INVALID_ARGUMENT;
1452 instance = g_focus_sound_handle[index].focus_tid;
1454 if (!g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1455 debug_warning("maybe focus_callback is being called, try one more time..");
1456 usleep(2500000); // 2.5 sec
1457 if (g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1458 debug_msg("finally got focus_lock");
1462 ret = mm_sound_client_dbus_unregister_focus(instance, id, g_focus_sound_handle[index].is_for_session);
1464 if (ret == MM_ERROR_NONE)
1465 debug_msg("[Client] Success to unregister focus\n");
1467 debug_error("[Client] Error occurred : 0x%x \n",ret);
1469 g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
1471 _focus_destroy_callback(index, false);
1472 g_focus_sound_handle[index].focus_fd = 0;
1473 g_focus_sound_handle[index].focus_tid = 0;
1474 g_focus_sound_handle[index].handle = 0;
1475 g_focus_sound_handle[index].is_used = false;
1481 int mm_sound_client_set_focus_reacquisition(int id, bool reacquisition)
1483 int ret = MM_ERROR_NONE;
1490 index = _focus_find_index_by_handle(id);
1492 debug_error("Could not find index");
1493 return MM_ERROR_INVALID_ARGUMENT;
1495 instance = g_focus_sound_handle[index].focus_tid;
1497 ret = mm_sound_client_is_focus_cb_thread(g_thread_self(), &result);
1499 debug_error("[Client] mm_sound_client_is_focus_cb_thread failed");
1501 } else if (!result) {
1502 ret = mm_sound_client_dbus_set_foucs_reacquisition(instance, id, reacquisition);
1503 if (ret == MM_ERROR_NONE) {
1504 debug_msg("[Client] Success to set focus reacquisition\n");
1506 debug_error("[Client] Error occurred : 0x%x \n",ret);
1510 debug_warning("[Client] Inside the focus cb thread, bypassing dbus method call");
1513 g_focus_sound_handle[index].auto_reacquire = reacquisition;
1521 int mm_sound_client_get_focus_reacquisition(int id, bool *reacquisition)
1523 int ret = MM_ERROR_NONE;
1528 if (!reacquisition) {
1529 debug_error("Invalid parameter");
1530 return MM_ERROR_INVALID_ARGUMENT;
1533 index = _focus_find_index_by_handle(id);
1535 debug_error("Could not find index");
1536 return MM_ERROR_INVALID_ARGUMENT;
1539 *reacquisition = g_focus_sound_handle[index].auto_reacquire;
1545 int mm_sound_client_get_acquired_focus_stream_type(int focus_type, char **stream_type, char **additional_info)
1547 int ret = MM_ERROR_NONE;
1551 ret = mm_sound_client_dbus_get_acquired_focus_stream_type(focus_type, stream_type, additional_info);
1552 if (ret == MM_ERROR_NONE)
1553 debug_msg("[Client] Success to get stream type of acquired focus, stream_type(%s), additional_info(%s)\n", *stream_type, *additional_info);
1555 debug_error("[Client] Error occurred : 0x%x \n",ret);
1561 int mm_sound_client_acquire_focus(int id, mm_sound_focus_type_e type, const char *option)
1563 int ret = MM_ERROR_NONE;
1569 index = _focus_find_index_by_handle(id);
1571 debug_error("Could not find index");
1572 return MM_ERROR_INVALID_ARGUMENT;
1574 instance = g_focus_sound_handle[index].focus_tid;
1576 ret = mm_sound_client_dbus_acquire_focus(instance, id, type, option, g_focus_sound_handle[index].is_for_session);
1578 if (ret == MM_ERROR_NONE)
1579 debug_msg("[Client] Success to acquire focus\n");
1581 debug_error("[Client] Error occurred : 0x%x \n",ret);
1587 int mm_sound_client_release_focus(int id, mm_sound_focus_type_e type, const char *option)
1589 int ret = MM_ERROR_NONE;
1594 index = _focus_find_index_by_handle(id);
1596 debug_error("Could not find index");
1597 return MM_ERROR_INVALID_ARGUMENT;
1599 instance = g_focus_sound_handle[index].focus_tid;
1601 ret = mm_sound_client_dbus_release_focus(instance, id, type, option, g_focus_sound_handle[index].is_for_session);
1603 if (ret == MM_ERROR_NONE)
1604 debug_msg("[Client] Success to release focus\n");
1606 debug_error("[Client] Error occurred : 0x%x \n",ret);
1613 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)
1615 int ret = MM_ERROR_NONE;
1621 return MM_ERROR_INVALID_ARGUMENT;
1623 //pthread_mutex_lock(&g_thread_mutex2);
1627 ret = mm_sound_client_dbus_get_unique_id(id);
1631 for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
1632 if (g_focus_sound_handle[index].is_used == false) {
1633 g_focus_sound_handle[index].is_used = true;
1638 g_focus_sound_handle[index].focus_tid = instance;
1639 g_focus_sound_handle[index].handle = *id;
1640 g_focus_sound_handle[index].watch_callback = callback;
1641 g_focus_sound_handle[index].user_data = user_data;
1642 g_focus_sound_handle[index].is_for_session = is_for_session;
1644 ret = mm_sound_client_dbus_set_focus_watch_callback(pid, g_focus_sound_handle[index].handle, focus_type, callback, is_for_session, user_data);
1646 if (ret == MM_ERROR_NONE) {
1647 debug_msg("[Client] Success to watch focus");
1648 if (!g_focus_thread) {
1649 GMainContext* focus_context = g_main_context_new ();
1650 g_focus_loop = g_main_loop_new (focus_context, FALSE);
1651 g_main_context_unref(focus_context);
1652 g_focus_thread = g_thread_new("focus-callback-thread", _focus_thread_func, NULL);
1653 if (g_focus_thread == NULL) {
1654 debug_error ("could not create thread..");
1655 g_main_loop_unref(g_focus_loop);
1656 ret = MM_ERROR_SOUND_INTERNAL;
1661 debug_error("[Client] Error occurred : 0x%x",ret);
1665 _focus_init_callback(index, true);
1670 g_focus_sound_handle[index].is_used = false;
1677 int mm_sound_client_unset_focus_watch_callback(int id)
1679 int ret = MM_ERROR_NONE;
1683 index = _focus_find_index_by_handle(id);
1685 debug_error("Could not find index");
1686 return MM_ERROR_INVALID_ARGUMENT;
1689 g_mutex_lock(&g_focus_sound_handle[index].focus_lock);
1691 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);
1693 if (ret == MM_ERROR_NONE)
1694 debug_msg("[Client] Success to unwatch focus\n");
1696 debug_error("[Client] Error occurred : 0x%x \n",ret);
1699 g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
1701 _focus_destroy_callback(index, true);
1702 g_focus_sound_handle[index].focus_fd = 0;
1703 g_focus_sound_handle[index].focus_tid = 0;
1704 g_focus_sound_handle[index].handle = 0;
1705 g_focus_sound_handle[index].is_used = false;
1713 int mm_sound_client_add_test_callback(mm_sound_test_cb func, void* user_data, unsigned int *subs_id)
1715 int ret = MM_ERROR_NONE;
1719 ret = mm_sound_client_dbus_add_test_callback(func, user_data, subs_id);
1725 int mm_sound_client_remove_test_callback(unsigned int subs_id)
1727 int ret = MM_ERROR_NONE;
1730 ret = mm_sound_client_dbus_remove_test_callback(subs_id);
1737 int mm_sound_client_test(int a, int b, int* getv)
1739 int ret = MM_ERROR_NONE;
1743 ret = mm_sound_client_dbus_test(a, b, getv);
1744 debug_log("%d * %d -> result : %d", a, b, *getv);