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_client.h"
45 #include "include/mm_sound_proxy.h"
46 #include "include/mm_sound_common.h"
47 #include "include/mm_sound_device.h"
48 #include "include/mm_sound_stream.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;
79 struct callback_data {
86 #define GET_CB_DATA(_cb_data, _func, _userdata, _extradata) \
88 _cb_data = (struct callback_data*) g_malloc0(sizeof(struct callback_data)); \
89 _cb_data->user_cb = _func; \
90 _cb_data->user_data = _userdata; \
91 _cb_data->extra_data = _extradata; \
99 GSourceFuncs* g_src_funcs;
105 mm_sound_focus_changed_cb focus_callback;
106 mm_sound_focus_changed_watch_cb watch_callback;
109 bool is_for_session; /* will be removed when the session concept is completely left out*/
110 } focus_sound_info_t;
117 char stream_type[MAX_STREAM_TYPE_LEN];
118 char name[MM_SOUND_NAME_NUM];
122 mm_sound_focus_session_interrupt_cb user_cb;
124 } focus_session_interrupt_info_t;
127 /* handle to watch end of playing */
129 /* subscription id to unsubscribe when handle ended */
131 } play_sound_end_callback_data_t;
133 typedef gboolean (*focus_gLoopPollHandler_t)(gpointer d);
135 GThread *g_focus_thread;
136 GMainLoop *g_focus_loop;
137 focus_sound_info_t g_focus_sound_handle[FOCUS_HANDLE_MAX];
138 focus_session_interrupt_info_t g_focus_session_interrupt_info = {NULL, NULL};
140 /* global variables for device list */
141 static pthread_mutex_t g_id_mutex = PTHREAD_MUTEX_INITIALIZER;
143 guint g_focus_signal_handle = 0;
145 void _focus_signal_handler(int signo)
147 int ret = MM_ERROR_NONE;
150 sigset_t old_mask, all_mask;
152 debug_error("Got signal : signo(%d)", signo);
156 sigfillset(&all_mask);
157 sigprocmask(SIG_BLOCK, &all_mask, &old_mask);
161 /* need implementation */
162 //send exit pid to focus server and focus server will clear focus or watch if necessary.
164 for (index = 0; index < FOCUS_HANDLE_MAX; index++) {
165 if (g_focus_sound_handle[index].is_used == true && g_focus_sound_handle[index].focus_tid == exit_pid) {
166 ret = mm_sound_proxy_emergent_exit_focus(exit_pid);
171 if (ret == MM_ERROR_NONE)
172 debug_msg("[Client] Success to emergnet_exit_focus\n");
174 debug_error("[Client] Error occurred : 0x%x \n",ret);
176 sigprocmask(SIG_SETMASK, &old_mask, NULL);
181 sigaction(SIGINT, &FOCUS_int_old_action, NULL);
185 sigaction(SIGABRT, &FOCUS_abrt_old_action, NULL);
189 sigaction(SIGSEGV, &FOCUS_segv_old_action, NULL);
193 sigaction(SIGTERM, &FOCUS_term_old_action, NULL);
197 sigaction(SIGSYS, &FOCUS_sys_old_action, NULL);
201 sigaction(SIGXCPU, &FOCUS_xcpu_old_action, NULL);
208 debug_error("signal handling end");
211 int mm_sound_client_initialize(void)
213 int ret = MM_ERROR_NONE;
216 mm_sound_proxy_initialize();
220 struct sigaction FOCUS_action;
221 FOCUS_action.sa_handler = _focus_signal_handler;
222 FOCUS_action.sa_flags = SA_NOCLDSTOP;
224 sigemptyset(&FOCUS_action.sa_mask);
226 sigaction(SIGINT, &FOCUS_action, &FOCUS_int_old_action);
227 sigaction(SIGABRT, &FOCUS_action, &FOCUS_abrt_old_action);
228 sigaction(SIGSEGV, &FOCUS_action, &FOCUS_segv_old_action);
229 sigaction(SIGTERM, &FOCUS_action, &FOCUS_term_old_action);
230 sigaction(SIGSYS, &FOCUS_action, &FOCUS_sys_old_action);
231 sigaction(SIGXCPU, &FOCUS_action, &FOCUS_xcpu_old_action);
239 int mm_sound_client_finalize(void)
241 int ret = MM_ERROR_NONE;
245 ret = mm_sound_proxy_finalize();
254 for (index = 0; index < FOCUS_HANDLE_MAX; index++) {
255 if (g_focus_sound_handle[index].is_used == true && g_focus_sound_handle[index].focus_tid == exit_pid) {
256 mm_sound_proxy_emergent_exit_focus(exit_pid);
260 if (g_focus_thread) {
261 g_main_loop_quit(g_focus_loop);
262 g_thread_join(g_focus_thread);
263 debug_log("after thread join");
264 g_main_loop_unref(g_focus_loop);
265 g_focus_thread = NULL;
268 /* is it necessary? */
269 sigaction(SIGINT, &FOCUS_int_old_action, NULL);
270 sigaction(SIGABRT, &FOCUS_abrt_old_action, NULL);
271 sigaction(SIGSEGV, &FOCUS_segv_old_action, NULL);
272 sigaction(SIGTERM, &FOCUS_term_old_action, NULL);
273 sigaction(SIGSYS, &FOCUS_sys_old_action, NULL);
274 sigaction(SIGXCPU, &FOCUS_xcpu_old_action, NULL);
282 void mm_sound_convert_volume_type_to_stream_type(int volume_type, char *stream_type)
284 switch (volume_type) {
285 case VOLUME_TYPE_SYSTEM:
286 MMSOUND_STRNCPY(stream_type, "system", MM_SOUND_STREAM_TYPE_LEN);
288 case VOLUME_TYPE_NOTIFICATION:
289 MMSOUND_STRNCPY(stream_type, "notification", MM_SOUND_STREAM_TYPE_LEN);
291 case VOLUME_TYPE_ALARM:
292 MMSOUND_STRNCPY(stream_type, "alarm", MM_SOUND_STREAM_TYPE_LEN);
294 case VOLUME_TYPE_RINGTONE:
295 MMSOUND_STRNCPY(stream_type, "ringtone-voip", MM_SOUND_STREAM_TYPE_LEN);
297 case VOLUME_TYPE_MEDIA:
298 MMSOUND_STRNCPY(stream_type, "media", MM_SOUND_STREAM_TYPE_LEN);
300 case VOLUME_TYPE_CALL:
301 MMSOUND_STRNCPY(stream_type, "call-voice", MM_SOUND_STREAM_TYPE_LEN);
303 case VOLUME_TYPE_VOIP:
304 MMSOUND_STRNCPY(stream_type, "voip", MM_SOUND_STREAM_TYPE_LEN);
306 case VOLUME_TYPE_VOICE:
307 MMSOUND_STRNCPY(stream_type, "voice-recognition", MM_SOUND_STREAM_TYPE_LEN);
310 MMSOUND_STRNCPY(stream_type, "media", MM_SOUND_STREAM_TYPE_LEN);
314 debug_error("volume type (%d) converted to stream type (%s)", volume_type, stream_type);
318 /*****************************************************************************************
319 DBUS SUPPORTED FUNCTIONS
320 ******************************************************************************************/
322 void _mm_sound_client_focus_signal_callback(mm_sound_signal_name_t signal, int value, void *user_data)
324 int ret = MM_ERROR_NONE;
327 debug_error("focus signal received, value = %d", value);
330 ret = mm_sound_proxy_clear_focus(getpid());
332 debug_error("clear focus failed ret = 0x%x", ret);
333 mm_sound_unsubscribe_signal(g_focus_signal_handle);
334 g_focus_signal_handle = 0;
338 int mm_sound_client_play_tone(int number, int volume_config, double volume, int time, int *handle, bool enable_session)
340 int ret = MM_ERROR_NONE;
341 // int instance = -1; /* instance is unique to communicate with server : client message queue filter type */
342 int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(volume_config);
343 char stream_type[MM_SOUND_STREAM_TYPE_LEN] = {0, };
347 /* read session information */
348 int session_type = MM_SESSION_TYPE_MEDIA;
349 int session_options = 0;
350 int is_focus_registered = 0;
352 ret = mm_sound_get_signal_value(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &is_focus_registered);
354 debug_error("mm_sound_get_signal_value failed [0x%x]", ret);
355 return MM_ERROR_POLICY_INTERNAL;
358 if (is_focus_registered)
359 enable_session = false;
361 if (enable_session) {
362 if (MM_ERROR_NONE != _mm_session_util_read_information(-1, &session_type, &session_options)) {
363 debug_warning("[Client] Read Session Information failed. use default \"media\" type\n");
364 session_type = MM_SESSION_TYPE_MEDIA;
366 if(MM_ERROR_NONE != mm_session_init(session_type)) {
367 debug_critical("[Client] MMSessionInit() failed\n");
368 return MM_ERROR_POLICY_INTERNAL;
373 // instance = getpid();
374 //debug_log("[Client] pid for client ::: [%d]\n", instance);
377 debug_msg("[Client] Input number : %d\n", number);
378 /* Send req memory */
380 mm_sound_convert_volume_type_to_stream_type(volume_type, stream_type);
381 ret = mm_sound_proxy_play_tone(number, time, volume, volume_config,
382 session_type, session_options, getpid(), enable_session, handle, stream_type, -1);
384 if (enable_session && !g_focus_signal_handle) {
385 ret = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &g_focus_signal_handle, _mm_sound_client_focus_signal_callback, NULL);
387 debug_error("mm_sound_subscribe_signal failed [0x%x]", ret);
388 return MM_ERROR_POLICY_INTERNAL;
396 int mm_sound_client_play_tone_with_stream_info(int tone, char *stream_type, int stream_id, double volume, int duration, int *handle)
398 int ret = MM_ERROR_NONE;
402 ret = mm_sound_proxy_play_tone_with_stream_info(getpid(), tone, stream_type, stream_id, volume, duration, handle);
408 static void _mm_sound_stop_callback_wrapper_func(int ended_handle, void *userdata)
410 struct callback_data *cb_data = (struct callback_data*) userdata;
411 play_sound_end_callback_data_t *end_cb_data = (play_sound_end_callback_data_t*) cb_data->extra_data;
413 debug_log("[Wrapper CB][Play Stop] ended_handle : %d", ended_handle);
415 if (cb_data == NULL) {
416 debug_warning("stop callback data null");
419 if (ended_handle == end_cb_data->watching_handle) {
420 debug_log("Interested playing handle end : %d", ended_handle);
421 ((mm_sound_stop_callback_func)(cb_data->user_cb))(cb_data->user_data, ended_handle);
422 if (mm_sound_proxy_remove_play_sound_end_callback(end_cb_data->subs_id) != MM_ERROR_NONE)
423 debug_error("mm_sound_client_dbus_remove_play_file_end_callback failed");
425 debug_log("Not interested playing handle : %d", ended_handle);
429 static void play_end_callback_data_free_func(void *data)
431 struct callback_data *cb_data = (struct callback_data*) data;
432 play_sound_end_callback_data_t *end_cb_data = (play_sound_end_callback_data_t*) cb_data->extra_data;
437 int mm_sound_client_play_sound(MMSoundPlayParam *param, int tone, int *handle)
439 int ret = MM_ERROR_NONE;
440 int session_type = MM_SESSION_TYPE_MEDIA;
441 int session_options = 0;
442 int is_focus_registered = 0;
443 // int instance = -1; /* instance is unique to communicate with server : client message queue filter type */
444 int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(param->volume_config);
445 char stream_type[MM_SOUND_STREAM_TYPE_LEN] = {0, };
446 struct callback_data *cb_data = NULL;
447 play_sound_end_callback_data_t *end_cb_data;
451 /* read session information */
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 param->skip_session = true;
462 if (param->skip_session == false) {
463 if(MM_ERROR_NONE != _mm_session_util_read_information(-1, &session_type, &session_options)) {
464 debug_warning("[Client] Read MMSession Type 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;
475 if ((param->mem_ptr && param->mem_size)) {
476 // Play memory, deprecated
477 return MM_ERROR_INVALID_ARGUMENT;
480 mm_sound_convert_volume_type_to_stream_type(volume_type, stream_type);
481 ret = mm_sound_proxy_play_sound(param->filename, tone, param->loop, param->volume, param->volume_config,
482 param->priority, session_type, session_options, getpid(), param->handle_route,
483 !param->skip_session, handle, stream_type, -1);
484 if (ret != MM_ERROR_NONE) {
485 debug_error("Play Sound Failed");
488 if (param->callback) {
489 end_cb_data = (play_sound_end_callback_data_t *) g_malloc0(sizeof(play_sound_end_callback_data_t));
490 end_cb_data->watching_handle = *handle;
491 GET_CB_DATA(cb_data, param->callback, param->data, end_cb_data);
493 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);
494 if (ret != MM_ERROR_NONE) {
495 debug_error("Add callback for play sound(%d) Failed", *handle);
498 if (!param->skip_session && !g_focus_signal_handle) {
499 ret = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &g_focus_signal_handle, _mm_sound_client_focus_signal_callback, NULL);
501 debug_error("mm_sound_subscribe_signal failed [0x%x]", ret);
502 return MM_ERROR_POLICY_INTERNAL;
512 int mm_sound_client_play_sound_with_stream_info(MMSoundPlayParam *param, int *handle, char* stream_type, int stream_id)
514 int ret = MM_ERROR_NONE;
515 struct callback_data *cb_data = NULL;
516 play_sound_end_callback_data_t *end_cb_data;
518 ret = mm_sound_proxy_play_sound_with_stream_info(param->filename, param->loop, param->volume,
519 param->priority, getpid(), param->handle_route, handle, stream_type, stream_id);
520 if (ret != MM_ERROR_NONE) {
521 debug_error("Play Sound Failed");
524 if (param->callback) {
525 end_cb_data = (play_sound_end_callback_data_t *) g_malloc0(sizeof(play_sound_end_callback_data_t));
526 end_cb_data->watching_handle = *handle;
527 GET_CB_DATA(cb_data, param->callback, param->data, end_cb_data);
529 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);
530 if (ret != MM_ERROR_NONE) {
531 debug_error("Add callback for play sound(%d) Failed", *handle);
542 int mm_sound_client_stop_sound(int handle)
544 int ret = MM_ERROR_NONE;
547 if (handle < 0 || handle > CLIENT_HANDLE_MAX) {
548 ret = MM_ERROR_INVALID_ARGUMENT;
552 ret = mm_sound_proxy_stop_sound(handle);
558 static int _mm_sound_client_device_list_dump (GList *device_list)
560 int ret = MM_ERROR_NONE;
562 mm_sound_device_t *device_node = NULL;
565 debug_error("Device list NULL, cannot dump list");
566 return MM_ERROR_SOUND_INTERNAL;
569 debug_log("======================== device list : start ==========================\n");
570 for (list = device_list; list != NULL; list = list->next) {
571 device_node = (mm_sound_device_t *)list->data;
573 debug_log(" list idx[%d]: type[%17s], id[%02d], io_direction[%d], state[%d], name[%s]\n",
574 count++, device_node->type, device_node->id, device_node->io_direction, device_node->state, device_node->name);
577 debug_log("======================== device list : end ============================\n");
582 int mm_sound_client_get_current_connected_device_list(int device_flags, mm_sound_device_list_t *device_list)
584 int ret = MM_ERROR_NONE;
588 debug_error("Device list NULL");
589 ret = MM_ERROR_COMMON_INVALID_ARGUMENT;
593 if ((ret = mm_sound_proxy_get_current_connected_device_list(device_flags, &device_list->list)) != MM_ERROR_NONE) {
594 debug_error("[Client] failed to get current connected device list with dbus, ret[0x%x]", ret);
597 if (!device_list->list) {
598 debug_error("Got device list null");
599 ret = MM_ERROR_SOUND_NO_DATA;
602 _mm_sound_client_device_list_dump(device_list->list);
609 static bool is_device_match_flags(const char *device_type, int io_direction, int state, int device_flags)
611 int io_direction_flag = device_flags & DEVICE_IO_DIRECTION_FLAGS;
612 int state_flag = device_flags & DEVICE_STATE_FLAGS;
613 int type_flag = device_flags & DEVICE_TYPE_FLAGS;
614 char builtin_prefix[] = "builtin";
616 if (device_flags == DEVICE_ALL_FLAG)
619 switch (io_direction_flag) {
620 case DEVICE_IO_DIRECTION_IN_FLAG:
621 if (io_direction != DEVICE_IO_DIRECTION_IN)
623 case DEVICE_IO_DIRECTION_OUT_FLAG:
624 if (io_direction != DEVICE_IO_DIRECTION_OUT)
626 case DEVICE_IO_DIRECTION_BOTH_FLAG:
627 if (io_direction != DEVICE_IO_DIRECTION_BOTH)
633 switch (state_flag) {
634 case DEVICE_STATE_DEACTIVATED_FLAG:
635 if (state != DEVICE_STATE_DEACTIVATED)
637 case DEVICE_STATE_ACTIVATED_FLAG:
638 if (state != DEVICE_STATE_ACTIVATED)
645 case DEVICE_TYPE_INTERNAL_FLAG:
646 if (strncmp(device_type, builtin_prefix, strlen(builtin_prefix)) != 0)
648 case DEVICE_TYPE_EXTERNAL_FLAG:
649 if (strncmp(device_type, builtin_prefix, strlen(builtin_prefix)) == 0)
658 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)
660 mm_sound_device_t device_h;
661 struct callback_data *cb_data = (struct callback_data*) userdata;
662 int device_flags = (int) cb_data->extra_data;
664 debug_log("[Wrapper CB][Device Connnected] device_id : %d, device_type : %s, direction : %d, state : %d, name : %s, is_connected : %d",
665 device_id, device_type, io_direction, state, name, is_connected);
667 if (cb_data == NULL) {
668 debug_warning("device connected changed callback data null");
671 if (!is_device_match_flags(device_type, io_direction, state, device_flags))
674 device_h.id = device_id;
675 device_h.io_direction = io_direction;
676 device_h.state = state;
677 MMSOUND_STRNCPY(device_h.name, name, MAX_DEVICE_NAME_NUM);
678 MMSOUND_STRNCPY(device_h.type, device_type, MAX_DEVICE_TYPE_STR_LEN);
680 ((mm_sound_device_connected_cb)(cb_data->user_cb))(&device_h, is_connected, cb_data->user_data);
683 int mm_sound_client_add_device_connected_callback(int device_flags, mm_sound_device_connected_cb func, void* userdata, unsigned int *subs_id)
685 int ret = MM_ERROR_NONE;
686 struct callback_data *cb_data = NULL;
690 GET_CB_DATA(cb_data, func, userdata, (void*) device_flags);
692 ret = mm_sound_proxy_add_device_connected_callback(device_flags, _mm_sound_device_connected_callback_wrapper_func, cb_data, g_free, subs_id);
698 int mm_sound_client_remove_device_connected_callback(unsigned int subs_id)
700 int ret = MM_ERROR_NONE;
703 ret = mm_sound_proxy_remove_device_connected_callback(subs_id);
709 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)
711 mm_sound_device_t device_h;
712 struct callback_data *cb_data = (struct callback_data*) userdata;
713 int device_flags = (int) cb_data->extra_data;
715 debug_log("[Wrapper CB][Device Info Changed] device_id : %d, device_type : %s, direction : %d, state : %d, name : %s, changed_info_type : %d",
716 device_id, device_type, io_direction, state, name, changed_device_info_type);
718 if (cb_data == NULL) {
719 debug_warning("device info changed callback data null");
722 if (!is_device_match_flags(device_type, io_direction, state, device_flags))
725 device_h.id = device_id;
726 device_h.io_direction = io_direction;
727 device_h.state = state;
728 MMSOUND_STRNCPY(device_h.name, name, MAX_DEVICE_NAME_NUM);
729 MMSOUND_STRNCPY(device_h.type, device_type, MAX_DEVICE_TYPE_STR_LEN);
731 ((mm_sound_device_info_changed_cb)(cb_data->user_cb))(&device_h, changed_device_info_type, cb_data->user_data);
734 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)
736 int ret = MM_ERROR_NONE;
737 struct callback_data *cb_data = (struct callback_data*) userdata;
741 GET_CB_DATA(cb_data, func, userdata, (void *) device_flags);
743 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);
749 int mm_sound_client_remove_device_info_changed_callback(unsigned int subs_id)
751 int ret = MM_ERROR_NONE;
754 ret = mm_sound_proxy_remove_device_info_changed_callback(subs_id);
761 int __convert_volume_type_to_str(int volume_type, char **volume_type_str)
763 int ret = MM_ERROR_NONE;
765 if (!volume_type_str) {
766 return MM_ERROR_COMMON_INVALID_ARGUMENT;
769 switch (volume_type) {
770 case VOLUME_TYPE_SYSTEM:
771 *volume_type_str = "system";
773 case VOLUME_TYPE_NOTIFICATION:
774 *volume_type_str = "notification";
776 case VOLUME_TYPE_ALARM:
777 *volume_type_str = "alarm";
779 case VOLUME_TYPE_RINGTONE:
780 *volume_type_str = "ringtone";
782 case VOLUME_TYPE_MEDIA:
783 *volume_type_str = "media";
785 case VOLUME_TYPE_CALL:
786 *volume_type_str = "call";
788 case VOLUME_TYPE_VOIP:
789 *volume_type_str = "voip";
791 case VOLUME_TYPE_VOICE:
792 *volume_type_str = "voice";
795 if (!strncmp(*volume_type_str,"", VOLUME_TYPE_LEN)) {
796 debug_error("could not find the volume_type[%d] in this switch case statement", volume_type);
797 ret = MM_ERROR_SOUND_INTERNAL;
799 debug_log("volume_type[%s]", *volume_type_str);
804 static int __convert_volume_type_to_int(const char *volume_type_str, volume_type_t *volume_type)
806 int ret = MM_ERROR_NONE;
808 if (!volume_type || !volume_type_str) {
809 return MM_ERROR_COMMON_INVALID_ARGUMENT;
812 if (!strncmp(volume_type_str, "system", VOLUME_TYPE_LEN)) {
813 *volume_type = VOLUME_TYPE_SYSTEM;
814 } else if (!strncmp(volume_type_str, "notification", VOLUME_TYPE_LEN)) {
815 *volume_type = VOLUME_TYPE_NOTIFICATION;
816 } else if (!strncmp(volume_type_str, "alarm", VOLUME_TYPE_LEN)) {
817 *volume_type = VOLUME_TYPE_ALARM;
818 } else if (!strncmp(volume_type_str, "ringtone", VOLUME_TYPE_LEN)) {
819 *volume_type = VOLUME_TYPE_RINGTONE;
820 } else if (!strncmp(volume_type_str, "media", VOLUME_TYPE_LEN)) {
821 *volume_type = VOLUME_TYPE_MEDIA;
822 } else if (!strncmp(volume_type_str, "call", VOLUME_TYPE_LEN)) {
823 *volume_type = VOLUME_TYPE_CALL;
824 } else if (!strncmp(volume_type_str, "voip", VOLUME_TYPE_LEN)) {
825 *volume_type = VOLUME_TYPE_VOIP;
826 } else if (!strncmp(volume_type_str, "voice", VOLUME_TYPE_LEN)) {
827 *volume_type = VOLUME_TYPE_VOICE;
829 debug_log("Invalid volume type : [%s]", volume_type_str);
830 ret = MM_ERROR_SOUND_INTERNAL;
836 int mm_sound_client_set_volume_by_type(const int volume_type, const unsigned int volume_level)
838 int ret = MM_ERROR_NONE;
839 char *type_str = NULL;
842 if ((ret = __convert_volume_type_to_str(volume_type, &type_str)) != MM_ERROR_NONE) {
843 debug_error("volume type convert failed");
847 ret = mm_sound_proxy_set_volume_by_type(type_str, volume_level);
854 static void _mm_sound_volume_changed_callback_wrapper_func(const char *direction, const char *volume_type_str, int volume_level, void *userdata)
856 volume_type_t volume_type = 0;
857 struct callback_data *cb_data = (struct callback_data *) userdata;
859 debug_log("[Wrapper CB][Volume Changed] direction : %s, volume_type : %s, volume_level : %d", direction, volume_type_str, volume_level);
861 if (cb_data == NULL) {
862 debug_warning("volume changed callback data null");
866 if (__convert_volume_type_to_int(volume_type_str, &volume_type) != MM_ERROR_NONE) {
867 debug_error("volume type convert failed");
870 debug_log("Call volume changed user cb, direction : %s, vol_type : %s(%d), level : %u", direction, volume_type_str, volume_type, volume_level);
871 ((mm_sound_volume_changed_cb)(cb_data->user_cb))(volume_type, volume_level, cb_data->user_data);
874 int mm_sound_client_add_volume_changed_callback(mm_sound_volume_changed_cb func, void* userdata, unsigned int *subs_id)
876 int ret = MM_ERROR_NONE;
877 struct callback_data *cb_data = NULL;
881 GET_CB_DATA(cb_data, func, userdata, NULL);
883 ret = mm_sound_proxy_add_volume_changed_callback(_mm_sound_volume_changed_callback_wrapper_func, cb_data, g_free, subs_id);
890 int mm_sound_client_remove_volume_changed_callback(unsigned int subs_id)
892 int ret = MM_ERROR_NONE;
895 ret = mm_sound_proxy_remove_volume_changed_callback(subs_id);
902 int mm_sound_client_set_session_interrupt_callback(mm_sound_focus_session_interrupt_cb callback, void* user_data)
904 int ret = MM_ERROR_NONE;
909 return MM_ERROR_INVALID_ARGUMENT;
911 g_focus_session_interrupt_info.user_cb = callback;
912 g_focus_session_interrupt_info.user_data = user_data;
918 int mm_sound_client_unset_session_interrupt_callback(void)
920 int ret = MM_ERROR_NONE;
924 if (!g_focus_session_interrupt_info.user_cb) {
925 debug_error("no callback to unset");
926 return MM_ERROR_SOUND_INTERNAL;
929 g_focus_session_interrupt_info.user_cb = NULL;
930 g_focus_session_interrupt_info.user_data = NULL;
936 static gpointer _focus_thread_func(gpointer data)
938 debug_log(">>> thread func..ID of this thread(%u)\n", (unsigned int)pthread_self());
939 g_main_loop_run(g_focus_loop);
940 debug_log("<<< quit thread func..\n");
944 static gboolean _focus_fd_check(GSource * source)
950 debug_error("GSource is null");
953 fd_list = source->poll_fds;
955 debug_error("fd_list is null");
959 temp = (GPollFD*)fd_list->data;
961 debug_error("fd_list->data is null");
964 if (temp->revents & (POLLIN | POLLPRI)) {
967 fd_list = fd_list->next;
970 return FALSE; /* there is no change in any fd state */
973 static gboolean _focus_fd_prepare(GSource *source, gint *timeout)
978 static gboolean _focus_fd_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
985 static int _focus_find_index_by_handle(int handle)
988 for(i = 0; i< FOCUS_HANDLE_MAX; i++) {
989 if (handle == g_focus_sound_handle[i].handle) {
990 //debug_msg("found index(%d) for handle(%d)", i, handle);
991 if (handle == FOCUS_HANDLE_INIT_VAL) {
1000 static gboolean _focus_callback_handler(gpointer d)
1002 GPollFD *data = (GPollFD*)d;
1005 int focus_index = 0;
1006 focus_cb_data_lib cb_data;
1007 debug_log(">>> focus_callback_handler()..ID of this thread(%u)\n", (unsigned int)pthread_self());
1009 memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1012 debug_error("GPollFd is null");
1015 if (data->revents & (POLLIN | POLLPRI)) {
1016 int changed_state = -1;
1018 count = read(data->fd, &cb_data, sizeof(cb_data));
1020 char str_error[256];
1021 strerror_r(errno, str_error, sizeof(str_error));
1022 debug_error("GpollFD read fail, errno=%d(%s)",errno, str_error);
1025 changed_state = cb_data.state;
1026 focus_index = _focus_find_index_by_handle(cb_data.handle);
1027 if (focus_index == -1) {
1028 debug_error("Could not find index");
1032 g_mutex_lock(&g_focus_sound_handle[focus_index].focus_lock);
1034 tid = g_focus_sound_handle[focus_index].focus_tid;
1036 if (changed_state != -1) {
1037 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);
1038 if (g_focus_sound_handle[focus_index].focus_callback== NULL) {
1039 debug_error("callback is null..");
1040 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1043 debug_error("[CALLBACK(%p) START]",g_focus_sound_handle[focus_index].focus_callback);
1044 (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);
1045 debug_error("[CALLBACK END]");
1046 if (g_focus_session_interrupt_info.user_cb) {
1047 debug_error("sending session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
1048 (g_focus_session_interrupt_info.user_cb)(cb_data.state, cb_data.stream_type, false, g_focus_session_interrupt_info.user_data);
1051 #ifdef CONFIG_ENABLE_RETCB
1055 unsigned int buf = 0;
1056 char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", g_focus_sound_handle[focus_index].focus_tid, cb_data.handle);
1057 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1059 char str_error[256];
1060 strerror_r(errno, str_error, sizeof(str_error));
1061 debug_error("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n", tid, tmpfd, filename2, errno, str_error);
1063 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1066 buf = (unsigned int)((0x0000ffff & cb_data.handle) |(g_focus_sound_handle[focus_index].auto_reacquire << 16));
1067 rett = write(tmpfd, &buf, sizeof(buf));
1070 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
1074 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1079 static gboolean _focus_watch_callback_handler( gpointer d)
1081 GPollFD *data = (GPollFD*)d;
1084 int focus_index = 0;
1085 focus_cb_data_lib cb_data;
1089 memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1092 debug_error("GPollFd is null");
1095 if (data->revents & (POLLIN | POLLPRI)) {
1096 count = read(data->fd, &cb_data, sizeof(cb_data));
1098 char str_error[256];
1099 strerror_r(errno, str_error, sizeof(str_error));
1100 debug_error("GpollFD read fail, errno=%d(%s)",errno, str_error);
1104 focus_index = _focus_find_index_by_handle(cb_data.handle);
1105 if (focus_index == -1) {
1106 debug_error("Could not find index");
1110 debug_error("lock focus_lock = %p", &g_focus_sound_handle[focus_index].focus_lock);
1111 g_mutex_lock(&g_focus_sound_handle[focus_index].focus_lock);
1113 tid = g_focus_sound_handle[focus_index].focus_tid;
1115 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);
1117 if (g_focus_sound_handle[focus_index].watch_callback == NULL) {
1118 debug_msg("callback is null..");
1120 debug_msg("[CALLBACK(%p) START]",g_focus_sound_handle[focus_index].watch_callback);
1121 (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);
1122 debug_msg("[CALLBACK END]");
1123 if (g_focus_session_interrupt_info.user_cb) {
1124 debug_error("sending session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
1125 (g_focus_session_interrupt_info.user_cb)(cb_data.state, cb_data.stream_type, true, g_focus_session_interrupt_info.user_data);
1129 #ifdef CONFIG_ENABLE_RETCB
1134 char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.wchr", g_focus_sound_handle[focus_index].focus_tid);
1135 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1137 char str_error[256];
1138 strerror_r(errno, str_error, sizeof(str_error));
1139 debug_error("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n", tid, tmpfd, filename2, errno, str_error);
1141 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1144 buf = cb_data.handle;
1145 rett = write(tmpfd, &buf, sizeof(buf));
1148 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
1154 debug_error("unlock focus_lock = %p", &g_focus_sound_handle[focus_index].focus_lock);
1155 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1163 static void _focus_open_callback(int index, bool is_for_watching)
1170 if (is_for_watching) {
1171 filename = g_strdup_printf("/tmp/FOCUS.%d.wch", g_focus_sound_handle[index].focus_tid);
1173 filename = g_strdup_printf("/tmp/FOCUS.%d.%d", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1175 pre_mask = umask(0);
1176 if (mknod(filename, S_IFIFO|0666, 0)) {
1177 debug_error("mknod() failure, errno(%d)", errno);
1180 g_focus_sound_handle[index].focus_fd = open( filename, O_RDWR|O_NONBLOCK);
1181 if (g_focus_sound_handle[index].focus_fd == -1) {
1182 debug_error("Open fail : index(%d), file open error(%d)", index, errno);
1184 debug_log("Open sucess : index(%d), filename(%s), fd(%d)", index, filename, g_focus_sound_handle[index].focus_fd);
1189 #ifdef CONFIG_ENABLE_RETCB
1192 if (is_for_watching) {
1193 filename2 = g_strdup_printf("/tmp/FOCUS.%d.wchr", g_focus_sound_handle[index].focus_tid);
1195 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1197 pre_mask = umask(0);
1198 if (mknod(filename2, S_IFIFO | 0666, 0)) {
1199 debug_error("mknod() failure, errno(%d)", errno);
1209 void _focus_close_callback(int index, bool is_for_watching)
1213 if (g_focus_sound_handle[index].focus_fd < 0) {
1214 debug_error("Close fail : fd error.");
1217 if (is_for_watching) {
1218 filename = g_strdup_printf("/tmp/FOCUS.%d.wch", g_focus_sound_handle[index].focus_tid);
1220 filename = g_strdup_printf("/tmp/FOCUS.%d.%d", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1222 close(g_focus_sound_handle[index].focus_fd);
1223 if (remove(filename)) {
1224 debug_error("remove() failure, filename(%s), errno(%d)", filename, errno);
1226 debug_log("Close Sucess : index(%d), filename(%s)", index, filename);
1231 #ifdef CONFIG_ENABLE_RETCB
1235 if (is_for_watching) {
1236 filename2 = g_strdup_printf("/tmp/FOCUS.%d.wchr", g_focus_sound_handle[index].focus_tid);
1238 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1241 /* Defensive code - wait until callback timeout although callback is removed */
1242 int buf = MM_ERROR_NONE; //no need to specify cb result to server, just notice if the client got the callback properly or not
1245 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1247 char str_error[256];
1248 strerror_r(errno, str_error, sizeof(str_error));
1249 debug_warning("could not open file(%s) (may server close it first), tid(%d) fd(%d) %s errno=%d(%s)",
1250 filename2, g_focus_sound_handle[index].focus_tid, tmpfd, filename2, errno, str_error);
1252 debug_msg("write MM_ERROR_NONE(tid:%d) for waiting server", g_focus_sound_handle[index].focus_tid);
1253 written = write(tmpfd, &buf, sizeof(buf));
1257 if (remove(filename2)) {
1258 debug_error("remove() failure, filename(%s), errno(%d)", filename2, errno);
1266 static bool _focus_add_sound_callback(int index, int fd, gushort events, focus_gLoopPollHandler_t p_gloop_poll_handler )
1268 GSource* g_src = NULL;
1269 GSourceFuncs *g_src_funcs = NULL; /* handler function */
1270 guint gsource_handle;
1271 GPollFD *g_poll_fd = NULL; /* file descriptor */
1275 g_mutex_init(&g_focus_sound_handle[index].focus_lock);
1277 /* 1. make GSource Object */
1278 g_src_funcs = (GSourceFuncs *)g_malloc(sizeof(GSourceFuncs));
1280 debug_error("g_malloc failed on g_src_funcs");
1284 g_src_funcs->prepare = _focus_fd_prepare;
1285 g_src_funcs->check = _focus_fd_check;
1286 g_src_funcs->dispatch = _focus_fd_dispatch;
1287 g_src_funcs->finalize = NULL;
1288 g_src = g_source_new(g_src_funcs, sizeof(GSource));
1290 debug_error("g_malloc failed on m_readfd");
1293 g_focus_sound_handle[index].focus_src = g_src;
1294 g_focus_sound_handle[index].g_src_funcs = g_src_funcs;
1296 /* 2. add file description which used in g_loop() */
1297 g_poll_fd = (GPollFD *)g_malloc(sizeof(GPollFD));
1299 debug_error("g_malloc failed on g_poll_fd");
1303 g_poll_fd->events = events;
1304 g_focus_sound_handle[index].g_poll_fd = g_poll_fd;
1306 /* 3. combine g_source object and file descriptor */
1307 g_source_add_poll(g_src, g_poll_fd);
1308 gsource_handle = g_source_attach(g_src, g_main_loop_get_context(g_focus_loop));
1309 if (!gsource_handle) {
1310 debug_error(" Failed to attach the source to context");
1313 //g_source_unref(g_src);
1315 /* 4. set callback */
1316 g_source_set_callback(g_src, p_gloop_poll_handler,(gpointer)g_poll_fd, NULL);
1318 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)",
1319 g_src_funcs, g_poll_fd, gsource_handle, errno);
1327 static bool _focus_remove_sound_callback(int index, gushort events)
1333 g_mutex_clear(&g_focus_sound_handle[index].focus_lock);
1335 GSourceFuncs *g_src_funcs = g_focus_sound_handle[index].g_src_funcs;
1336 GPollFD *g_poll_fd = g_focus_sound_handle[index].g_poll_fd; /* store file descriptor */
1338 debug_error("g_poll_fd is null..");
1342 g_poll_fd->fd = g_focus_sound_handle[index].focus_fd;
1343 g_poll_fd->events = events;
1345 if (!g_focus_sound_handle[index].focus_src) {
1346 debug_error("FOCUS_sound_handle[%d].focus_src is null..", index);
1349 debug_log(" g_source_remove_poll : fd(%d), event(%x), errno(%d)", g_poll_fd->fd, g_poll_fd->events, errno);
1350 g_source_remove_poll(g_focus_sound_handle[index].focus_src, g_poll_fd);
1354 if (g_focus_sound_handle[index].focus_src) {
1355 g_source_destroy(g_focus_sound_handle[index].focus_src);
1356 if (!g_source_is_destroyed (g_focus_sound_handle[index].focus_src)) {
1357 debug_warning(" failed to g_source_destroy(), focus_src(0x%p)", g_focus_sound_handle[index].focus_src);
1360 debug_log(" g_free : g_src_funcs(%#X), g_poll_fd(%#X)", g_src_funcs, g_poll_fd);
1363 g_free(g_src_funcs);
1371 g_focus_sound_handle[index].g_src_funcs = NULL;
1372 g_focus_sound_handle[index].g_poll_fd = NULL;
1373 g_focus_sound_handle[index].focus_src = NULL;
1374 g_focus_sound_handle[index].focus_callback = NULL;
1375 g_focus_sound_handle[index].watch_callback = NULL;
1382 static void _focus_add_callback(int index, bool is_for_watching)
1385 if (!is_for_watching) {
1386 if (!_focus_add_sound_callback(index, g_focus_sound_handle[index].focus_fd, (gushort)POLLIN | POLLPRI, _focus_callback_handler)) {
1387 debug_error("failed to _focus_add_sound_callback()");
1390 } else { // need to check if it's necessary
1391 if (!_focus_add_sound_callback(index, g_focus_sound_handle[index].focus_fd, (gushort)POLLIN | POLLPRI, _focus_watch_callback_handler)) {
1392 debug_error("failed to _focus_add_sound_callback()");
1399 static void _focus_remove_callback(int index)
1402 if (!_focus_remove_sound_callback(index, (gushort)POLLIN | POLLPRI)) {
1403 debug_error("failed to __focus_remove_sound_callback()");
1409 static void _focus_init_callback(int index, bool is_for_watching)
1412 _focus_open_callback(index, is_for_watching);
1413 _focus_add_callback(index, is_for_watching);
1417 static void _focus_destroy_callback(int index, bool is_for_watching)
1420 _focus_remove_callback(index);
1421 _focus_close_callback(index, is_for_watching);
1425 int mm_sound_client_get_unique_id(int *id)
1427 int ret = MM_ERROR_NONE;
1429 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_id_mutex, MM_ERROR_SOUND_INTERNAL);
1433 ret = MM_ERROR_INVALID_ARGUMENT;
1435 ret = mm_sound_proxy_get_unique_id(id);
1438 MMSOUND_LEAVE_CRITICAL_SECTION(&g_id_mutex);
1443 int mm_sound_client_is_focus_cb_thread(GThread *mine, bool *result)
1445 int ret = MM_ERROR_NONE;
1447 if (!mine || !result)
1448 ret = MM_ERROR_INVALID_ARGUMENT;
1450 if (mine == g_focus_thread)
1459 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)
1461 int ret = MM_ERROR_NONE;
1468 for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
1469 if (g_focus_sound_handle[index].is_used == false) {
1470 g_focus_sound_handle[index].is_used = true;
1475 g_focus_sound_handle[index].focus_tid = instance;
1476 g_focus_sound_handle[index].handle = id;
1477 g_focus_sound_handle[index].focus_callback = callback;
1478 g_focus_sound_handle[index].user_data = user_data;
1479 g_focus_sound_handle[index].is_for_session = is_for_session;
1480 g_focus_sound_handle[index].auto_reacquire = true;
1482 ret = mm_sound_proxy_register_focus(id, pid, stream_type, callback, is_for_session, user_data);
1484 if (ret == MM_ERROR_NONE) {
1485 debug_msg("[Client] Success to register focus\n");
1486 if (!g_focus_thread) {
1487 GMainContext* focus_context = g_main_context_new ();
1488 g_focus_loop = g_main_loop_new (focus_context, FALSE);
1489 g_main_context_unref(focus_context);
1490 g_focus_thread = g_thread_new("focus-callback-thread", _focus_thread_func, NULL);
1491 if (g_focus_thread == NULL) {
1492 debug_error ("could not create thread..");
1493 g_main_loop_unref(g_focus_loop);
1494 g_focus_sound_handle[index].is_used = false;
1495 ret = MM_ERROR_SOUND_INTERNAL;
1500 debug_error("[Client] Error occurred : 0x%x \n",ret);
1501 g_focus_sound_handle[index].is_used = false;
1505 _focus_init_callback(index, false);
1513 int mm_sound_client_unregister_focus(int id)
1515 int ret = MM_ERROR_NONE;
1520 index = _focus_find_index_by_handle(id);
1522 debug_error("Could not find index");
1523 return MM_ERROR_INVALID_ARGUMENT;
1525 instance = g_focus_sound_handle[index].focus_tid;
1527 if (!g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1528 debug_warning("maybe focus_callback is being called, try one more time..");
1529 usleep(2500000); // 2.5 sec
1530 if (g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1531 debug_msg("finally got focus_lock");
1535 ret = mm_sound_proxy_unregister_focus(instance, id, g_focus_sound_handle[index].is_for_session);
1537 if (ret == MM_ERROR_NONE)
1538 debug_msg("[Client] Success to unregister focus\n");
1540 debug_error("[Client] Error occurred : 0x%x \n",ret);
1542 g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
1544 _focus_destroy_callback(index, false);
1545 g_focus_sound_handle[index].focus_fd = 0;
1546 g_focus_sound_handle[index].focus_tid = 0;
1547 g_focus_sound_handle[index].handle = 0;
1548 g_focus_sound_handle[index].is_used = false;
1554 int mm_sound_client_set_focus_reacquisition(int id, bool reacquisition)
1556 int ret = MM_ERROR_NONE;
1563 index = _focus_find_index_by_handle(id);
1565 debug_error("Could not find index");
1566 return MM_ERROR_INVALID_ARGUMENT;
1568 instance = g_focus_sound_handle[index].focus_tid;
1570 ret = mm_sound_client_is_focus_cb_thread(g_thread_self(), &result);
1572 debug_error("[Client] mm_sound_client_is_focus_cb_thread failed");
1574 } else if (!result) {
1575 ret = mm_sound_proxy_set_foucs_reacquisition(instance, id, reacquisition);
1576 if (ret == MM_ERROR_NONE) {
1577 debug_msg("[Client] Success to set focus reacquisition\n");
1579 debug_error("[Client] Error occurred : 0x%x \n",ret);
1583 debug_warning("[Client] Inside the focus cb thread, bypassing dbus method call");
1586 g_focus_sound_handle[index].auto_reacquire = reacquisition;
1594 int mm_sound_client_get_focus_reacquisition(int id, bool *reacquisition)
1596 int ret = MM_ERROR_NONE;
1601 if (!reacquisition) {
1602 debug_error("Invalid parameter");
1603 return MM_ERROR_INVALID_ARGUMENT;
1606 index = _focus_find_index_by_handle(id);
1608 debug_error("Could not find index");
1609 return MM_ERROR_INVALID_ARGUMENT;
1612 *reacquisition = g_focus_sound_handle[index].auto_reacquire;
1618 int mm_sound_client_get_acquired_focus_stream_type(int focus_type, char **stream_type, char **additional_info)
1620 int ret = MM_ERROR_NONE;
1624 ret = mm_sound_proxy_get_acquired_focus_stream_type(focus_type, stream_type, additional_info);
1625 if (ret == MM_ERROR_NONE)
1626 debug_msg("[Client] Success to get stream type of acquired focus, stream_type(%s), additional_info(%s)\n", *stream_type, *additional_info);
1628 debug_error("[Client] Error occurred : 0x%x \n",ret);
1634 int mm_sound_client_acquire_focus(int id, mm_sound_focus_type_e type, const char *option)
1636 int ret = MM_ERROR_NONE;
1642 index = _focus_find_index_by_handle(id);
1644 debug_error("Could not find index");
1645 return MM_ERROR_INVALID_ARGUMENT;
1647 instance = g_focus_sound_handle[index].focus_tid;
1649 ret = mm_sound_proxy_acquire_focus(instance, id, type, option, g_focus_sound_handle[index].is_for_session);
1651 if (ret == MM_ERROR_NONE)
1652 debug_msg("[Client] Success to acquire focus\n");
1654 debug_error("[Client] Error occurred : 0x%x \n",ret);
1660 int mm_sound_client_release_focus(int id, mm_sound_focus_type_e type, const char *option)
1662 int ret = MM_ERROR_NONE;
1667 index = _focus_find_index_by_handle(id);
1669 debug_error("Could not find index");
1670 return MM_ERROR_INVALID_ARGUMENT;
1672 instance = g_focus_sound_handle[index].focus_tid;
1674 ret = mm_sound_proxy_release_focus(instance, id, type, option, g_focus_sound_handle[index].is_for_session);
1676 if (ret == MM_ERROR_NONE)
1677 debug_msg("[Client] Success to release focus\n");
1679 debug_error("[Client] Error occurred : 0x%x \n",ret);
1686 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)
1688 int ret = MM_ERROR_NONE;
1694 return MM_ERROR_INVALID_ARGUMENT;
1696 //pthread_mutex_lock(&g_thread_mutex2);
1700 ret = mm_sound_proxy_get_unique_id(id);
1704 for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
1705 if (g_focus_sound_handle[index].is_used == false) {
1706 g_focus_sound_handle[index].is_used = true;
1711 g_focus_sound_handle[index].focus_tid = instance;
1712 g_focus_sound_handle[index].handle = *id;
1713 g_focus_sound_handle[index].watch_callback = callback;
1714 g_focus_sound_handle[index].user_data = user_data;
1715 g_focus_sound_handle[index].is_for_session = is_for_session;
1717 ret = mm_sound_proxy_set_focus_watch_callback(pid, g_focus_sound_handle[index].handle, focus_type, callback, is_for_session, user_data);
1719 if (ret == MM_ERROR_NONE) {
1720 debug_msg("[Client] Success to watch focus");
1721 if (!g_focus_thread) {
1722 GMainContext* focus_context = g_main_context_new ();
1723 g_focus_loop = g_main_loop_new (focus_context, FALSE);
1724 g_main_context_unref(focus_context);
1725 g_focus_thread = g_thread_new("focus-callback-thread", _focus_thread_func, NULL);
1726 if (g_focus_thread == NULL) {
1727 debug_error ("could not create thread..");
1728 g_main_loop_unref(g_focus_loop);
1729 ret = MM_ERROR_SOUND_INTERNAL;
1734 debug_error("[Client] Error occurred : 0x%x",ret);
1738 _focus_init_callback(index, true);
1743 g_focus_sound_handle[index].is_used = false;
1750 int mm_sound_client_unset_focus_watch_callback(int id)
1752 int ret = MM_ERROR_NONE;
1756 index = _focus_find_index_by_handle(id);
1758 debug_error("Could not find index");
1759 return MM_ERROR_INVALID_ARGUMENT;
1762 g_mutex_lock(&g_focus_sound_handle[index].focus_lock);
1764 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);
1766 if (ret == MM_ERROR_NONE)
1767 debug_msg("[Client] Success to unwatch focus\n");
1769 debug_error("[Client] Error occurred : 0x%x \n",ret);
1772 g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
1774 _focus_destroy_callback(index, true);
1775 g_focus_sound_handle[index].focus_fd = 0;
1776 g_focus_sound_handle[index].focus_tid = 0;
1777 g_focus_sound_handle[index].handle = 0;
1778 g_focus_sound_handle[index].is_used = false;
1786 int mm_sound_client_add_test_callback(mm_sound_test_cb func, void* user_data, unsigned int *subs_id)
1788 int ret = MM_ERROR_NONE;
1792 ret = mm_sound_proxy_add_test_callback(func, user_data, g_free, subs_id);
1798 int mm_sound_client_remove_test_callback(unsigned int subs_id)
1800 int ret = MM_ERROR_NONE;
1803 ret = mm_sound_proxy_remove_test_callback(subs_id);
1810 int mm_sound_client_test(int a, int b, int* getv)
1812 int ret = MM_ERROR_NONE;
1816 ret = mm_sound_proxy_test(a, b, getv);
1817 debug_log("%d * %d -> result : %d", a, b, *getv);