4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Seungbae Shin <seungbae.shin@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
33 #include <semaphore.h>
38 #include "include/mm_sound.h"
39 #include "include/mm_sound_client.h"
40 #include "include/mm_sound_proxy.h"
41 #include "include/mm_sound_common.h"
42 #include "include/mm_sound_device.h"
43 #include "include/mm_sound_stream.h"
45 #include <mm_session.h>
46 #include <mm_session_private.h>
49 #if defined(__GSOURCE_CALLBACK__)
53 #define CLIENT_HANDLE_MAX 256
55 #define FOCUS_HANDLE_MAX 512
56 #define FOCUS_HANDLE_INIT_VAL -1
57 #define CONFIG_ENABLE_RETCB
59 #define VOLUME_TYPE_LEN 64
61 struct sigaction system_int_old_action;
62 struct sigaction system_abrt_old_action;
63 struct sigaction system_segv_old_action;
64 struct sigaction system_term_old_action;
65 struct sigaction system_sys_old_action;
66 struct sigaction system_xcpu_old_action;
68 struct callback_data {
75 #define GET_CB_DATA(_cb_data, _func, _userdata, _extradata) \
77 _cb_data = (struct callback_data*) g_malloc0(sizeof(struct callback_data)); \
78 _cb_data->user_cb = _func; \
79 _cb_data->user_data = _userdata; \
80 _cb_data->extra_data = _extradata; \
88 GSourceFuncs* g_src_funcs;
94 mm_sound_focus_changed_cb focus_callback;
95 mm_sound_focus_changed_watch_cb watch_callback;
98 bool is_for_session; /* will be removed when the session concept is completely left out*/
106 char stream_type[MAX_STREAM_TYPE_LEN];
107 char name[MM_SOUND_NAME_NUM];
111 mm_sound_focus_session_interrupt_cb user_cb;
113 } focus_session_interrupt_info_t;
116 /* handle to watch end of playing */
118 /* subscription id to unsubscribe when handle ended */
120 } play_sound_end_callback_data_t;
122 typedef gboolean (*focus_gLoopPollHandler_t)(gpointer d);
124 GThread *g_focus_thread;
125 GMainLoop *g_focus_loop;
126 focus_sound_info_t g_focus_sound_handle[FOCUS_HANDLE_MAX];
127 focus_session_interrupt_info_t g_focus_session_interrupt_info = {NULL, NULL};
128 static pthread_mutex_t g_index_mutex = PTHREAD_MUTEX_INITIALIZER;
130 guint g_focus_signal_handle = 0;
131 gboolean g_need_emergent_exit = FALSE;
133 void _system_signal_handler(int signo)
135 int ret = MM_ERROR_NONE;
136 sigset_t old_mask, all_mask;
138 debug_error("Got signal : signo(%d)", signo);
142 sigfillset(&all_mask);
143 sigprocmask(SIG_BLOCK, &all_mask, &old_mask);
145 if (g_need_emergent_exit) {
146 ret = mm_sound_proxy_emergent_exit(getpid());
147 if (ret == MM_ERROR_NONE)
148 debug_msg("[Client] Success to emergnet_exit\n");
150 debug_error("[Client] Error occurred : 0x%x \n",ret);
153 sigprocmask(SIG_SETMASK, &old_mask, NULL);
158 sigaction(SIGINT, &system_int_old_action, NULL);
162 sigaction(SIGABRT, &system_abrt_old_action, NULL);
166 sigaction(SIGSEGV, &system_segv_old_action, NULL);
170 sigaction(SIGTERM, &system_term_old_action, NULL);
174 sigaction(SIGSYS, &system_sys_old_action, NULL);
178 sigaction(SIGXCPU, &system_xcpu_old_action, NULL);
185 debug_error("signal handling end");
188 int mm_sound_client_initialize(void)
190 int ret = MM_ERROR_NONE;
193 mm_sound_proxy_initialize();
196 struct sigaction system_action;
197 system_action.sa_handler = _system_signal_handler;
198 system_action.sa_flags = SA_NOCLDSTOP;
200 sigemptyset(&system_action.sa_mask);
202 sigaction(SIGINT, &system_action, &system_int_old_action);
203 sigaction(SIGABRT, &system_action, &system_abrt_old_action);
204 sigaction(SIGSEGV, &system_action, &system_segv_old_action);
205 sigaction(SIGTERM, &system_action, &system_term_old_action);
206 sigaction(SIGSYS, &system_action, &system_sys_old_action);
207 sigaction(SIGXCPU, &system_action, &system_xcpu_old_action);
213 int mm_sound_client_finalize(void)
215 int ret = MM_ERROR_NONE;
219 if (g_need_emergent_exit) {
220 ret = mm_sound_proxy_emergent_exit(getpid());
221 if (ret == MM_ERROR_NONE)
222 debug_msg("[Client] Success to emergnet_exit\n");
224 debug_error("[Client] Error occurred : 0x%x \n",ret);
227 sigaction(SIGINT, &system_int_old_action, NULL);
228 sigaction(SIGABRT, &system_abrt_old_action, NULL);
229 sigaction(SIGSEGV, &system_segv_old_action, NULL);
230 sigaction(SIGTERM, &system_term_old_action, NULL);
231 sigaction(SIGSYS, &system_sys_old_action, NULL);
232 sigaction(SIGXCPU, &system_xcpu_old_action, NULL);
234 ret = mm_sound_proxy_finalize();
239 if (g_focus_thread) {
240 g_main_loop_quit(g_focus_loop);
241 g_thread_join(g_focus_thread);
242 debug_log("after thread join");
243 g_main_loop_unref(g_focus_loop);
244 g_focus_thread = NULL;
253 void mm_sound_convert_volume_type_to_stream_type(int volume_type, char *stream_type)
255 switch (volume_type) {
256 case VOLUME_TYPE_SYSTEM:
257 MMSOUND_STRNCPY(stream_type, "system", MM_SOUND_STREAM_TYPE_LEN);
259 case VOLUME_TYPE_NOTIFICATION:
260 MMSOUND_STRNCPY(stream_type, "notification", MM_SOUND_STREAM_TYPE_LEN);
262 case VOLUME_TYPE_ALARM:
263 MMSOUND_STRNCPY(stream_type, "alarm", MM_SOUND_STREAM_TYPE_LEN);
265 case VOLUME_TYPE_RINGTONE:
266 MMSOUND_STRNCPY(stream_type, "ringtone-voip", MM_SOUND_STREAM_TYPE_LEN);
268 case VOLUME_TYPE_MEDIA:
269 MMSOUND_STRNCPY(stream_type, "media", MM_SOUND_STREAM_TYPE_LEN);
271 case VOLUME_TYPE_CALL:
272 MMSOUND_STRNCPY(stream_type, "call-voice", MM_SOUND_STREAM_TYPE_LEN);
274 case VOLUME_TYPE_VOIP:
275 MMSOUND_STRNCPY(stream_type, "voip", MM_SOUND_STREAM_TYPE_LEN);
277 case VOLUME_TYPE_VOICE:
278 MMSOUND_STRNCPY(stream_type, "voice-recognition", MM_SOUND_STREAM_TYPE_LEN);
281 MMSOUND_STRNCPY(stream_type, "media", MM_SOUND_STREAM_TYPE_LEN);
285 debug_error("volume type (%d) converted to stream type (%s)", volume_type, stream_type);
289 /*****************************************************************************************
290 DBUS SUPPORTED FUNCTIONS
291 ******************************************************************************************/
293 void _mm_sound_client_focus_signal_callback(mm_sound_signal_name_t signal, int value, void *user_data)
295 int ret = MM_ERROR_NONE;
298 debug_error("focus signal received, value = %d", value);
301 ret = mm_sound_proxy_clear_focus(getpid());
303 debug_error("clear focus failed ret = 0x%x", ret);
304 mm_sound_unsubscribe_signal(g_focus_signal_handle);
305 g_focus_signal_handle = 0;
309 int mm_sound_client_play_tone(int number, int volume_config, double volume, int time, int *handle, bool enable_session)
311 int ret = MM_ERROR_NONE;
312 // int instance = -1; /* instance is unique to communicate with server : client message queue filter type */
313 int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(volume_config);
314 char stream_type[MM_SOUND_STREAM_TYPE_LEN] = {0, };
318 /* read session information */
319 int session_type = MM_SESSION_TYPE_MEDIA;
320 int session_options = 0;
321 int is_focus_registered = 0;
323 ret = mm_sound_get_signal_value(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &is_focus_registered);
325 debug_error("mm_sound_get_signal_value failed [0x%x]", ret);
326 return MM_ERROR_POLICY_INTERNAL;
329 if (is_focus_registered)
330 enable_session = false;
332 if (enable_session) {
333 if (MM_ERROR_NONE != _mm_session_util_read_information(-1, &session_type, &session_options)) {
334 debug_warning("[Client] Read Session Information failed. use default \"media\" type\n");
335 session_type = MM_SESSION_TYPE_MEDIA;
337 if(MM_ERROR_NONE != mm_session_init(session_type)) {
338 debug_critical("[Client] MMSessionInit() failed\n");
339 return MM_ERROR_POLICY_INTERNAL;
344 // instance = getpid();
345 //debug_log("[Client] pid for client ::: [%d]\n", instance);
348 debug_msg("[Client] Input number : %d\n", number);
349 /* Send req memory */
351 mm_sound_convert_volume_type_to_stream_type(volume_type, stream_type);
352 ret = mm_sound_proxy_play_tone(number, time, volume, volume_config,
353 session_type, session_options, getpid(), enable_session, handle, stream_type, -1);
355 if (enable_session && !g_focus_signal_handle) {
356 ret = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &g_focus_signal_handle, _mm_sound_client_focus_signal_callback, NULL);
358 debug_error("mm_sound_subscribe_signal failed [0x%x]", ret);
359 return MM_ERROR_POLICY_INTERNAL;
367 int mm_sound_client_play_tone_with_stream_info(int tone, char *stream_type, int stream_id, double volume, int duration, int *handle)
369 int ret = MM_ERROR_NONE;
373 ret = mm_sound_proxy_play_tone_with_stream_info(getpid(), tone, stream_type, stream_id, volume, duration, handle);
379 static void _mm_sound_stop_callback_wrapper_func(int ended_handle, void *userdata)
381 struct callback_data *cb_data = (struct callback_data*) userdata;
382 play_sound_end_callback_data_t *end_cb_data;
384 debug_log("[Wrapper CB][Play Stop] ended_handle : %d", ended_handle);
386 if (cb_data == NULL) {
387 debug_warning("stop callback data null");
391 end_cb_data = (play_sound_end_callback_data_t*) cb_data->extra_data;
393 if (ended_handle == end_cb_data->watching_handle) {
394 debug_log("Interested playing handle end : %d", ended_handle);
395 ((mm_sound_stop_callback_func)(cb_data->user_cb))(cb_data->user_data, ended_handle);
396 if (mm_sound_proxy_remove_play_sound_end_callback(end_cb_data->subs_id) != MM_ERROR_NONE)
397 debug_error("mm_sound_client_dbus_remove_play_file_end_callback failed");
399 debug_log("Not interested playing handle : %d", ended_handle);
403 static void play_end_callback_data_free_func(void *data)
405 struct callback_data *cb_data = (struct callback_data*) data;
408 if (cb_data->extra_data)
409 g_free(cb_data->extra_data);
414 int mm_sound_client_play_sound(MMSoundPlayParam *param, int tone, int *handle)
416 int ret = MM_ERROR_NONE;
417 int session_type = MM_SESSION_TYPE_MEDIA;
418 int session_options = 0;
419 int is_focus_registered = 0;
420 // int instance = -1; /* instance is unique to communicate with server : client message queue filter type */
421 int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(param->volume_config);
422 char stream_type[MM_SOUND_STREAM_TYPE_LEN] = {0, };
423 struct callback_data *cb_data = NULL;
424 play_sound_end_callback_data_t *end_cb_data;
428 /* read session information */
430 ret = mm_sound_get_signal_value(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &is_focus_registered);
432 debug_error("mm_sound_get_signal_value failed [0x%x]", ret);
433 return MM_ERROR_POLICY_INTERNAL;
436 if (is_focus_registered)
437 param->skip_session = true;
439 if (param->skip_session == false) {
440 if(MM_ERROR_NONE != _mm_session_util_read_information(-1, &session_type, &session_options)) {
441 debug_warning("[Client] Read MMSession Type failed. use default \"media\" type\n");
442 session_type = MM_SESSION_TYPE_MEDIA;
444 if(MM_ERROR_NONE != mm_session_init(session_type)) {
445 debug_critical("[Client] MMSessionInit() failed\n");
446 return MM_ERROR_POLICY_INTERNAL;
452 if ((param->mem_ptr && param->mem_size)) {
453 // Play memory, deprecated
454 return MM_ERROR_INVALID_ARGUMENT;
457 mm_sound_convert_volume_type_to_stream_type(volume_type, stream_type);
458 ret = mm_sound_proxy_play_sound(param->filename, tone, param->loop, param->volume, param->volume_config,
459 param->priority, session_type, session_options, getpid(), param->handle_route,
460 !param->skip_session, handle, stream_type, -1);
461 if (ret != MM_ERROR_NONE) {
462 debug_error("Play Sound Failed");
465 if (param->callback) {
466 end_cb_data = (play_sound_end_callback_data_t *) g_malloc0(sizeof(play_sound_end_callback_data_t));
467 end_cb_data->watching_handle = *handle;
468 GET_CB_DATA(cb_data, param->callback, param->data, end_cb_data);
470 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);
471 if (ret != MM_ERROR_NONE) {
472 debug_error("Add callback for play sound(%d) Failed", *handle);
475 if (!param->skip_session && !g_focus_signal_handle) {
476 ret = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &g_focus_signal_handle, _mm_sound_client_focus_signal_callback, NULL);
478 debug_error("mm_sound_subscribe_signal failed [0x%x]", ret);
479 return MM_ERROR_POLICY_INTERNAL;
489 int mm_sound_client_play_sound_with_stream_info(MMSoundPlayParam *param, int *handle, char* stream_type, int stream_id)
491 int ret = MM_ERROR_NONE;
492 struct callback_data *cb_data = NULL;
493 play_sound_end_callback_data_t *end_cb_data;
495 ret = mm_sound_proxy_play_sound_with_stream_info(param->filename, param->loop, param->volume,
496 param->priority, getpid(), param->handle_route, handle, stream_type, stream_id);
497 if (ret != MM_ERROR_NONE) {
498 debug_error("Play Sound Failed");
501 if (param->callback) {
502 end_cb_data = (play_sound_end_callback_data_t *) g_malloc0(sizeof(play_sound_end_callback_data_t));
503 end_cb_data->watching_handle = *handle;
504 GET_CB_DATA(cb_data, param->callback, param->data, end_cb_data);
506 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);
507 if (ret != MM_ERROR_NONE) {
508 debug_error("Add callback for play sound(%d) Failed", *handle);
519 int mm_sound_client_stop_sound(int handle)
521 int ret = MM_ERROR_NONE;
524 if (handle < 0 || handle > CLIENT_HANDLE_MAX) {
525 ret = MM_ERROR_INVALID_ARGUMENT;
529 ret = mm_sound_proxy_stop_sound(handle);
535 static int _mm_sound_client_device_list_dump (GList *device_list)
537 int ret = MM_ERROR_NONE;
539 mm_sound_device_t *device_node = NULL;
542 debug_error("Device list NULL, cannot dump list");
543 return MM_ERROR_SOUND_INTERNAL;
546 debug_log("======================== device list : start ==========================\n");
547 for (list = device_list; list != NULL; list = list->next) {
548 device_node = (mm_sound_device_t *)list->data;
550 debug_log(" list idx[%d]: type[%17s], id[%02d], io_direction[%d], state[%d], name[%s]\n",
551 count++, device_node->type, device_node->id, device_node->io_direction, device_node->state, device_node->name);
554 debug_log("======================== device list : end ============================\n");
559 int mm_sound_client_get_current_connected_device_list(int device_flags, mm_sound_device_list_t *device_list)
561 int ret = MM_ERROR_NONE;
565 debug_error("Device list NULL");
566 ret = MM_ERROR_COMMON_INVALID_ARGUMENT;
570 if ((ret = mm_sound_proxy_get_current_connected_device_list(device_flags, &device_list->list)) != MM_ERROR_NONE) {
571 debug_error("[Client] failed to get current connected device list with dbus, ret[0x%x]", ret);
574 if (!device_list->list) {
575 debug_error("Got device list null");
576 ret = MM_ERROR_SOUND_NO_DATA;
579 _mm_sound_client_device_list_dump(device_list->list);
586 static bool is_device_match_flags(const char *device_type, int io_direction, int state, int device_flags)
588 int io_direction_flag = device_flags & DEVICE_IO_DIRECTION_FLAGS;
589 int state_flag = device_flags & DEVICE_STATE_FLAGS;
590 int type_flag = device_flags & DEVICE_TYPE_FLAGS;
591 char builtin_prefix[] = "builtin";
593 if (device_flags == DEVICE_ALL_FLAG)
596 switch (io_direction_flag) {
597 case DEVICE_IO_DIRECTION_IN_FLAG:
598 if (io_direction != DEVICE_IO_DIRECTION_IN)
601 case DEVICE_IO_DIRECTION_OUT_FLAG:
602 if (io_direction != DEVICE_IO_DIRECTION_OUT)
605 case DEVICE_IO_DIRECTION_BOTH_FLAG:
606 if (io_direction != DEVICE_IO_DIRECTION_BOTH)
613 switch (state_flag) {
614 case DEVICE_STATE_DEACTIVATED_FLAG:
615 if (state != DEVICE_STATE_DEACTIVATED)
618 case DEVICE_STATE_ACTIVATED_FLAG:
619 if (state != DEVICE_STATE_ACTIVATED)
627 case DEVICE_TYPE_INTERNAL_FLAG:
628 if (strncmp(device_type, builtin_prefix, strlen(builtin_prefix)) != 0)
631 case DEVICE_TYPE_EXTERNAL_FLAG:
632 if (strncmp(device_type, builtin_prefix, strlen(builtin_prefix)) == 0)
642 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)
644 mm_sound_device_t device_h;
645 struct callback_data *cb_data = (struct callback_data*) userdata;
648 debug_log("[Wrapper CB][Device Connnected] device_id : %d, device_type : %s, direction : %d, state : %d, name : %s, is_connected : %d",
649 device_id, device_type, io_direction, state, name, is_connected);
651 if (cb_data == NULL) {
652 debug_warning("device connected changed callback data null");
656 device_flags = (int) cb_data->extra_data;
658 if (!is_device_match_flags(device_type, io_direction, state, device_flags))
661 device_h.id = device_id;
662 device_h.io_direction = io_direction;
663 device_h.state = state;
664 MMSOUND_STRNCPY(device_h.name, name, MAX_DEVICE_NAME_NUM);
665 MMSOUND_STRNCPY(device_h.type, device_type, MAX_DEVICE_TYPE_STR_LEN);
667 ((mm_sound_device_connected_cb)(cb_data->user_cb))(&device_h, is_connected, cb_data->user_data);
670 int mm_sound_client_add_device_connected_callback(int device_flags, mm_sound_device_connected_cb func, void* userdata, unsigned int *subs_id)
672 int ret = MM_ERROR_NONE;
673 struct callback_data *cb_data = NULL;
677 GET_CB_DATA(cb_data, func, userdata, (void*) device_flags);
679 ret = mm_sound_proxy_add_device_connected_callback(device_flags, _mm_sound_device_connected_callback_wrapper_func, cb_data, g_free, subs_id);
680 if (ret == MM_ERROR_NONE)
681 g_need_emergent_exit = TRUE;
687 int mm_sound_client_remove_device_connected_callback(unsigned int subs_id)
689 int ret = MM_ERROR_NONE;
692 ret = mm_sound_proxy_remove_device_connected_callback(subs_id);
698 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)
700 mm_sound_device_t device_h;
701 struct callback_data *cb_data = (struct callback_data*) userdata;
704 debug_log("[Wrapper CB][Device Info Changed] device_id : %d, device_type : %s, direction : %d, state : %d, name : %s, changed_info_type : %d",
705 device_id, device_type, io_direction, state, name, changed_device_info_type);
707 if (cb_data == NULL) {
708 debug_warning("device info changed callback data null");
712 device_flags = (int) cb_data->extra_data;
714 if (!is_device_match_flags(device_type, io_direction, state, device_flags))
717 device_h.id = device_id;
718 device_h.io_direction = io_direction;
719 device_h.state = state;
720 MMSOUND_STRNCPY(device_h.name, name, MAX_DEVICE_NAME_NUM);
721 MMSOUND_STRNCPY(device_h.type, device_type, MAX_DEVICE_TYPE_STR_LEN);
723 ((mm_sound_device_info_changed_cb)(cb_data->user_cb))(&device_h, changed_device_info_type, cb_data->user_data);
726 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)
728 int ret = MM_ERROR_NONE;
729 struct callback_data *cb_data = (struct callback_data*) userdata;
733 GET_CB_DATA(cb_data, func, userdata, (void *) device_flags);
735 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);
741 int mm_sound_client_remove_device_info_changed_callback(unsigned int subs_id)
743 int ret = MM_ERROR_NONE;
746 ret = mm_sound_proxy_remove_device_info_changed_callback(subs_id);
753 int __convert_volume_type_to_str(int volume_type, char **volume_type_str)
755 int ret = MM_ERROR_NONE;
757 if (!volume_type_str) {
758 return MM_ERROR_COMMON_INVALID_ARGUMENT;
761 switch (volume_type) {
762 case VOLUME_TYPE_SYSTEM:
763 *volume_type_str = "system";
765 case VOLUME_TYPE_NOTIFICATION:
766 *volume_type_str = "notification";
768 case VOLUME_TYPE_ALARM:
769 *volume_type_str = "alarm";
771 case VOLUME_TYPE_RINGTONE:
772 *volume_type_str = "ringtone";
774 case VOLUME_TYPE_MEDIA:
775 *volume_type_str = "media";
777 case VOLUME_TYPE_CALL:
778 *volume_type_str = "call";
780 case VOLUME_TYPE_VOIP:
781 *volume_type_str = "voip";
783 case VOLUME_TYPE_VOICE:
784 *volume_type_str = "voice";
787 if (!strncmp(*volume_type_str,"", VOLUME_TYPE_LEN)) {
788 debug_error("could not find the volume_type[%d] in this switch case statement", volume_type);
789 ret = MM_ERROR_SOUND_INTERNAL;
791 debug_log("volume_type[%s]", *volume_type_str);
796 static int __convert_volume_type_to_int(const char *volume_type_str, volume_type_t *volume_type)
798 int ret = MM_ERROR_NONE;
800 if (!volume_type || !volume_type_str) {
801 return MM_ERROR_COMMON_INVALID_ARGUMENT;
804 if (!strncmp(volume_type_str, "system", VOLUME_TYPE_LEN)) {
805 *volume_type = VOLUME_TYPE_SYSTEM;
806 } else if (!strncmp(volume_type_str, "notification", VOLUME_TYPE_LEN)) {
807 *volume_type = VOLUME_TYPE_NOTIFICATION;
808 } else if (!strncmp(volume_type_str, "alarm", VOLUME_TYPE_LEN)) {
809 *volume_type = VOLUME_TYPE_ALARM;
810 } else if (!strncmp(volume_type_str, "ringtone", VOLUME_TYPE_LEN)) {
811 *volume_type = VOLUME_TYPE_RINGTONE;
812 } else if (!strncmp(volume_type_str, "media", VOLUME_TYPE_LEN)) {
813 *volume_type = VOLUME_TYPE_MEDIA;
814 } else if (!strncmp(volume_type_str, "call", VOLUME_TYPE_LEN)) {
815 *volume_type = VOLUME_TYPE_CALL;
816 } else if (!strncmp(volume_type_str, "voip", VOLUME_TYPE_LEN)) {
817 *volume_type = VOLUME_TYPE_VOIP;
818 } else if (!strncmp(volume_type_str, "voice", VOLUME_TYPE_LEN)) {
819 *volume_type = VOLUME_TYPE_VOICE;
821 debug_log("Invalid volume type : [%s]", volume_type_str);
822 ret = MM_ERROR_SOUND_INTERNAL;
828 int mm_sound_client_set_volume_by_type(const int volume_type, const unsigned int volume_level)
830 int ret = MM_ERROR_NONE;
831 char *type_str = NULL;
834 if ((ret = __convert_volume_type_to_str(volume_type, &type_str)) != MM_ERROR_NONE) {
835 debug_error("volume type convert failed");
839 ret = mm_sound_proxy_set_volume_by_type(type_str, volume_level);
846 static void _mm_sound_volume_changed_callback_wrapper_func(const char *direction, const char *volume_type_str, int volume_level, void *userdata)
848 volume_type_t volume_type = 0;
849 struct callback_data *cb_data = (struct callback_data *) userdata;
851 debug_log("[Wrapper CB][Volume Changed] direction : %s, volume_type : %s, volume_level : %d", direction, volume_type_str, volume_level);
853 if (cb_data == NULL) {
854 debug_warning("volume changed callback data null");
858 if (__convert_volume_type_to_int(volume_type_str, &volume_type) != MM_ERROR_NONE) {
859 debug_error("volume type convert failed");
862 debug_log("Call volume changed user cb, direction : %s, vol_type : %s(%d), level : %u", direction, volume_type_str, volume_type, volume_level);
863 ((mm_sound_volume_changed_cb)(cb_data->user_cb))(volume_type, volume_level, cb_data->user_data);
866 int mm_sound_client_add_volume_changed_callback(mm_sound_volume_changed_cb func, void* userdata, unsigned int *subs_id)
868 int ret = MM_ERROR_NONE;
869 struct callback_data *cb_data = NULL;
873 GET_CB_DATA(cb_data, func, userdata, NULL);
875 ret = mm_sound_proxy_add_volume_changed_callback(_mm_sound_volume_changed_callback_wrapper_func, cb_data, g_free, subs_id);
882 int mm_sound_client_remove_volume_changed_callback(unsigned int subs_id)
884 int ret = MM_ERROR_NONE;
887 ret = mm_sound_proxy_remove_volume_changed_callback(subs_id);
894 int mm_sound_client_set_session_interrupt_callback(mm_sound_focus_session_interrupt_cb callback, void* user_data)
896 int ret = MM_ERROR_NONE;
901 return MM_ERROR_INVALID_ARGUMENT;
903 g_focus_session_interrupt_info.user_cb = callback;
904 g_focus_session_interrupt_info.user_data = user_data;
910 int mm_sound_client_unset_session_interrupt_callback(void)
912 int ret = MM_ERROR_NONE;
916 if (!g_focus_session_interrupt_info.user_cb) {
917 debug_error("no callback to unset");
918 return MM_ERROR_SOUND_INTERNAL;
921 g_focus_session_interrupt_info.user_cb = NULL;
922 g_focus_session_interrupt_info.user_data = NULL;
928 static gpointer _focus_thread_func(gpointer data)
930 debug_log(">>> thread func..ID of this thread(%u)\n", (unsigned int)pthread_self());
931 g_main_loop_run(g_focus_loop);
932 debug_log("<<< quit thread func..\n");
936 static gboolean _focus_fd_check(GSource * source)
942 debug_error("GSource is null");
945 fd_list = source->poll_fds;
947 debug_error("fd_list is null");
951 temp = (GPollFD*)fd_list->data;
953 debug_error("fd_list->data is null");
956 if (temp->revents & (POLLIN | POLLPRI)) {
959 fd_list = fd_list->next;
962 return FALSE; /* there is no change in any fd state */
965 static gboolean _focus_fd_prepare(GSource *source, gint *timeout)
970 static gboolean _focus_fd_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
976 static int _focus_find_index_by_handle(int handle)
979 for(i = 0; i< FOCUS_HANDLE_MAX; i++) {
980 if (handle == g_focus_sound_handle[i].handle) {
981 //debug_msg("found index(%d) for handle(%d)", i, handle);
982 if (handle == FOCUS_HANDLE_INIT_VAL) {
991 static gboolean _focus_callback_handler(gpointer d)
993 GPollFD *data = (GPollFD*)d;
997 focus_cb_data_lib cb_data;
998 debug_log(">>> focus_callback_handler()..ID of this thread(%u)\n", (unsigned int)pthread_self());
1000 memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1003 debug_error("GPollFd is null");
1006 if (data->revents & (POLLIN | POLLPRI)) {
1007 int changed_state = -1;
1009 count = read(data->fd, &cb_data, sizeof(cb_data));
1011 char str_error[256];
1012 strerror_r(errno, str_error, sizeof(str_error));
1013 debug_error("GpollFD read fail, errno=%d(%s)",errno, str_error);
1016 changed_state = cb_data.state;
1017 focus_index = _focus_find_index_by_handle(cb_data.handle);
1018 if (focus_index == -1) {
1019 debug_error("Could not find index");
1023 g_mutex_lock(&g_focus_sound_handle[focus_index].focus_lock);
1025 tid = g_focus_sound_handle[focus_index].focus_tid;
1027 if (changed_state != -1) {
1028 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);
1029 if (g_focus_sound_handle[focus_index].focus_callback== NULL) {
1030 debug_error("callback is null..");
1031 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1034 debug_error("[CALLBACK(%p) START]",g_focus_sound_handle[focus_index].focus_callback);
1035 (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);
1036 debug_error("[CALLBACK END]");
1037 if (g_focus_session_interrupt_info.user_cb) {
1038 debug_error("sending session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
1039 (g_focus_session_interrupt_info.user_cb)(cb_data.state, cb_data.stream_type, false, g_focus_session_interrupt_info.user_data);
1042 #ifdef CONFIG_ENABLE_RETCB
1046 unsigned int buf = 0;
1047 char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", g_focus_sound_handle[focus_index].focus_tid, cb_data.handle);
1048 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1050 char str_error[256];
1051 strerror_r(errno, str_error, sizeof(str_error));
1052 debug_error("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n", tid, tmpfd, filename2, errno, str_error);
1054 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1057 buf = (unsigned int)((0x0000ffff & cb_data.handle) |(g_focus_sound_handle[focus_index].auto_reacquire << 16));
1058 rett = write(tmpfd, &buf, sizeof(buf));
1061 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
1065 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1070 static gboolean _focus_watch_callback_handler(gpointer d)
1072 GPollFD *data = (GPollFD*)d;
1075 int focus_index = 0;
1076 focus_cb_data_lib cb_data;
1080 memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1083 debug_error("GPollFd is null");
1086 if (data->revents & (POLLIN | POLLPRI)) {
1087 count = read(data->fd, &cb_data, sizeof(cb_data));
1089 char str_error[256];
1090 strerror_r(errno, str_error, sizeof(str_error));
1091 debug_error("GpollFD read fail, errno=%d(%s)",errno, str_error);
1095 focus_index = _focus_find_index_by_handle(cb_data.handle);
1096 if (focus_index == -1) {
1097 debug_error("Could not find index");
1101 debug_error("lock focus_lock = %p", &g_focus_sound_handle[focus_index].focus_lock);
1102 g_mutex_lock(&g_focus_sound_handle[focus_index].focus_lock);
1104 tid = g_focus_sound_handle[focus_index].focus_tid;
1106 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);
1108 if (g_focus_sound_handle[focus_index].watch_callback == NULL) {
1109 debug_msg("callback is null..");
1111 debug_msg("[CALLBACK(%p) START]",g_focus_sound_handle[focus_index].watch_callback);
1112 (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);
1113 debug_msg("[CALLBACK END]");
1114 if (g_focus_session_interrupt_info.user_cb) {
1115 debug_error("sending session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
1116 (g_focus_session_interrupt_info.user_cb)(cb_data.state, cb_data.stream_type, true, g_focus_session_interrupt_info.user_data);
1120 #ifdef CONFIG_ENABLE_RETCB
1124 char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr", g_focus_sound_handle[focus_index].focus_tid, cb_data.handle);
1125 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1127 char str_error[256];
1128 strerror_r(errno, str_error, sizeof(str_error));
1129 debug_error("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n", tid, tmpfd, filename2, errno, str_error);
1131 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1134 buf = cb_data.handle;
1135 rett = write(tmpfd, &buf, sizeof(buf));
1138 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
1144 debug_error("unlock focus_lock = %p", &g_focus_sound_handle[focus_index].focus_lock);
1145 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1153 static void _focus_open_callback(int index, bool is_for_watching)
1160 if (is_for_watching) {
1161 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1163 filename = g_strdup_printf("/tmp/FOCUS.%d.%d", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1165 pre_mask = umask(0);
1166 if (mknod(filename, S_IFIFO|0666, 0)) {
1167 debug_error("mknod() failure, errno(%d)", errno);
1170 g_focus_sound_handle[index].focus_fd = open(filename, O_RDWR|O_NONBLOCK);
1171 if (g_focus_sound_handle[index].focus_fd == -1) {
1172 debug_error("Open fail : index(%d), file open error(%d)", index, errno);
1174 debug_log("Open sucess : index(%d), filename(%s), fd(%d)", index, filename, g_focus_sound_handle[index].focus_fd);
1179 #ifdef CONFIG_ENABLE_RETCB
1182 if (is_for_watching) {
1183 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1185 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1187 pre_mask = umask(0);
1188 if (mknod(filename2, S_IFIFO | 0666, 0)) {
1189 debug_error("mknod() failure, errno(%d)", errno);
1199 void _focus_close_callback(int index, bool is_for_watching)
1201 char *filename = NULL;
1205 if (g_focus_sound_handle[index].focus_fd < 0) {
1206 debug_error("Close fail : index(%d)", index);
1208 close(g_focus_sound_handle[index].focus_fd);
1209 debug_log("Close Sucess : index(%d)", index);
1212 if (is_for_watching) {
1213 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1215 filename = g_strdup_printf("/tmp/FOCUS.%d.%d", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1217 if (remove(filename)) {
1218 debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)", filename, errno);
1223 #ifdef CONFIG_ENABLE_RETCB
1226 if (is_for_watching) {
1227 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1229 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1231 if (remove(filename2)) {
1232 debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)", filename2, errno);
1242 static bool _focus_add_sound_callback(int index, int fd, gushort events, focus_gLoopPollHandler_t p_gloop_poll_handler )
1244 GSource* g_src = NULL;
1245 GSourceFuncs *g_src_funcs = NULL; /* handler function */
1247 GPollFD *g_poll_fd = NULL; /* file descriptor */
1251 g_mutex_init(&g_focus_sound_handle[index].focus_lock);
1253 /* 1. make GSource Object */
1254 g_src_funcs = (GSourceFuncs *)g_malloc(sizeof(GSourceFuncs));
1256 debug_error("failed to g_malloc for g_src_funcs");
1260 g_src_funcs->prepare = _focus_fd_prepare;
1261 g_src_funcs->check = _focus_fd_check;
1262 g_src_funcs->dispatch = _focus_fd_dispatch;
1263 g_src_funcs->finalize = NULL;
1264 g_src = g_source_new(g_src_funcs, sizeof(GSource));
1266 debug_error("failed to g_source_new for g_src");
1270 /* 2. add file description which used in g_loop() */
1271 g_poll_fd = (GPollFD *)g_malloc(sizeof(GPollFD));
1273 debug_error("failed to g_malloc for g_poll_fd");
1277 g_poll_fd->events = events;
1279 /* 3. combine g_source object and file descriptor */
1280 g_source_add_poll(g_src, g_poll_fd);
1281 g_src_id = g_source_attach(g_src, g_main_loop_get_context(g_focus_loop));
1283 debug_error("failed to attach the source to context");
1287 /* 4. set callback */
1288 g_source_set_callback(g_src, p_gloop_poll_handler,(gpointer)g_poll_fd, NULL);
1290 debug_log("g_malloc : g_src_funcs(%p), g_poll_fd(%p)", g_src_funcs, g_poll_fd);
1292 /* 5. store to global handle */
1293 g_focus_sound_handle[index].focus_src = g_src;
1294 g_focus_sound_handle[index].g_src_funcs = g_src_funcs;
1295 g_focus_sound_handle[index].g_poll_fd = g_poll_fd;
1302 g_free(g_src_funcs);
1306 g_source_unref(g_src);
1311 static bool _focus_remove_sound_callback(int index, gushort events)
1317 g_mutex_clear(&g_focus_sound_handle[index].focus_lock);
1319 GSourceFuncs *g_src_funcs = g_focus_sound_handle[index].g_src_funcs;
1320 GPollFD *g_poll_fd = g_focus_sound_handle[index].g_poll_fd; /* store file descriptor */
1322 debug_error("g_poll_fd is null..");
1326 g_poll_fd->fd = g_focus_sound_handle[index].focus_fd;
1327 g_poll_fd->events = events;
1329 if (!g_focus_sound_handle[index].focus_src) {
1330 debug_error("g_focus_sound_handle[%d].focus_src is null..", index);
1334 g_source_remove_poll(g_focus_sound_handle[index].focus_src, g_poll_fd);
1335 debug_log("g_source_remove_poll : fd(%d), event(%x)", g_poll_fd->fd, g_poll_fd->events);
1338 if (g_focus_sound_handle[index].focus_src)
1339 g_source_destroy(g_focus_sound_handle[index].focus_src);
1341 debug_log("g_free : g_src_funcs(%x), g_poll_fd(%x)", g_src_funcs, g_poll_fd);
1343 g_free(g_src_funcs);
1350 if (g_focus_sound_handle[index].focus_src)
1351 g_source_unref(g_focus_sound_handle[index].focus_src);
1353 g_focus_sound_handle[index].g_src_funcs = NULL;
1354 g_focus_sound_handle[index].g_poll_fd = NULL;
1355 g_focus_sound_handle[index].focus_src = NULL;
1356 g_focus_sound_handle[index].focus_callback = NULL;
1357 g_focus_sound_handle[index].watch_callback = NULL;
1364 static void _focus_add_callback(int index, bool is_for_watching)
1367 if (!is_for_watching) {
1368 if (!_focus_add_sound_callback(index, g_focus_sound_handle[index].focus_fd, (gushort)POLLIN | POLLPRI, _focus_callback_handler)) {
1369 debug_error("failed to _focus_add_sound_callback()");
1372 } else { // need to check if it's necessary
1373 if (!_focus_add_sound_callback(index, g_focus_sound_handle[index].focus_fd, (gushort)POLLIN | POLLPRI, _focus_watch_callback_handler)) {
1374 debug_error("failed to _focus_add_sound_callback()");
1381 static void _focus_remove_callback(int index)
1384 if (!_focus_remove_sound_callback(index, (gushort)POLLIN | POLLPRI)) {
1385 debug_error("failed to __focus_remove_sound_callback()");
1391 static void _focus_init_callback(int index, bool is_for_watching)
1394 _focus_open_callback(index, is_for_watching);
1395 _focus_add_callback(index, is_for_watching);
1399 static void _focus_destroy_callback(int index, bool is_for_watching)
1402 _focus_remove_callback(index);
1403 _focus_close_callback(index, is_for_watching);
1407 int mm_sound_client_get_unique_id(int *id)
1409 int ret = MM_ERROR_NONE;
1414 ret = MM_ERROR_INVALID_ARGUMENT;
1416 ret = mm_sound_proxy_get_unique_id(id);
1423 int mm_sound_client_is_focus_cb_thread(GThread *mine, bool *result)
1425 int ret = MM_ERROR_NONE;
1427 if (!mine || !result)
1428 ret = MM_ERROR_INVALID_ARGUMENT;
1430 if (mine == g_focus_thread)
1439 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)
1441 int ret = MM_ERROR_NONE;
1446 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1450 for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
1451 if (g_focus_sound_handle[index].is_used == false) {
1452 g_focus_sound_handle[index].is_used = true;
1457 g_focus_sound_handle[index].focus_tid = instance;
1458 g_focus_sound_handle[index].handle = id;
1459 g_focus_sound_handle[index].focus_callback = callback;
1460 g_focus_sound_handle[index].user_data = user_data;
1461 g_focus_sound_handle[index].is_for_session = is_for_session;
1462 g_focus_sound_handle[index].auto_reacquire = true;
1464 ret = mm_sound_proxy_register_focus(id, pid, stream_type, callback, is_for_session, user_data);
1466 if (ret == MM_ERROR_NONE) {
1467 debug_msg("[Client] Success to register focus\n");
1468 g_need_emergent_exit = TRUE;
1469 if (!g_focus_thread) {
1470 GMainContext* focus_context = g_main_context_new ();
1471 g_focus_loop = g_main_loop_new (focus_context, FALSE);
1472 g_main_context_unref(focus_context);
1473 g_focus_thread = g_thread_new("focus-callback-thread", _focus_thread_func, NULL);
1474 if (g_focus_thread == NULL) {
1475 debug_error ("could not create thread..");
1476 g_main_loop_unref(g_focus_loop);
1477 g_focus_sound_handle[index].is_used = false;
1478 ret = MM_ERROR_SOUND_INTERNAL;
1483 debug_error("[Client] Error occurred : 0x%x \n",ret);
1484 g_focus_sound_handle[index].is_used = false;
1488 _focus_init_callback(index, false);
1491 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1497 int mm_sound_client_unregister_focus(int id)
1499 int ret = MM_ERROR_NONE;
1504 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1506 index = _focus_find_index_by_handle(id);
1508 debug_error("Could not find index");
1509 ret = MM_ERROR_INVALID_ARGUMENT;
1512 instance = g_focus_sound_handle[index].focus_tid;
1514 if (!g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1515 debug_warning("maybe focus_callback is being called, try one more time..");
1516 usleep(2500000); // 2.5 sec
1517 if (g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1518 debug_msg("finally got focus_lock");
1522 ret = mm_sound_proxy_unregister_focus(instance, id, g_focus_sound_handle[index].is_for_session);
1524 if (ret == MM_ERROR_NONE)
1525 debug_msg("[Client] Success to unregister focus\n");
1527 debug_error("[Client] Error occurred : 0x%x \n",ret);
1529 g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
1531 _focus_destroy_callback(index, false);
1532 g_focus_sound_handle[index].focus_fd = 0;
1533 g_focus_sound_handle[index].focus_tid = 0;
1534 g_focus_sound_handle[index].handle = 0;
1535 g_focus_sound_handle[index].is_used = false;
1537 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1542 int mm_sound_client_set_focus_reacquisition(int id, bool reacquisition)
1544 int ret = MM_ERROR_NONE;
1550 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1552 index = _focus_find_index_by_handle(id);
1554 debug_error("Could not find index");
1555 ret = MM_ERROR_INVALID_ARGUMENT;
1558 instance = g_focus_sound_handle[index].focus_tid;
1560 ret = mm_sound_client_is_focus_cb_thread(g_thread_self(), &result);
1562 debug_error("[Client] mm_sound_client_is_focus_cb_thread failed");
1564 } else if (!result) {
1565 ret = mm_sound_proxy_set_foucs_reacquisition(instance, id, reacquisition);
1566 if (ret == MM_ERROR_NONE) {
1567 debug_msg("[Client] Success to set focus reacquisition\n");
1569 debug_error("[Client] Error occurred : 0x%x \n",ret);
1573 debug_warning("[Client] Inside the focus cb thread, bypassing dbus method call");
1576 g_focus_sound_handle[index].auto_reacquire = reacquisition;
1579 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1584 int mm_sound_client_get_focus_reacquisition(int id, bool *reacquisition)
1586 int ret = MM_ERROR_NONE;
1591 if (!reacquisition) {
1592 debug_error("Invalid parameter");
1593 return MM_ERROR_INVALID_ARGUMENT;
1596 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1598 index = _focus_find_index_by_handle(id);
1600 debug_error("Could not find index");
1601 ret = MM_ERROR_INVALID_ARGUMENT;
1605 *reacquisition = g_focus_sound_handle[index].auto_reacquire;
1608 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1613 int mm_sound_client_get_acquired_focus_stream_type(int focus_type, char **stream_type, char **additional_info)
1615 int ret = MM_ERROR_NONE;
1619 ret = mm_sound_proxy_get_acquired_focus_stream_type(focus_type, stream_type, additional_info);
1620 if (ret == MM_ERROR_NONE)
1621 debug_msg("[Client] Success to get stream type of acquired focus, stream_type(%s), additional_info(%s)\n", *stream_type, *additional_info);
1623 debug_error("[Client] Error occurred : 0x%x \n",ret);
1629 int mm_sound_client_acquire_focus(int id, mm_sound_focus_type_e type, const char *option)
1631 int ret = MM_ERROR_NONE;
1636 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1638 index = _focus_find_index_by_handle(id);
1640 debug_error("Could not find index");
1641 ret = MM_ERROR_INVALID_ARGUMENT;
1644 instance = g_focus_sound_handle[index].focus_tid;
1646 ret = mm_sound_proxy_acquire_focus(instance, id, type, option, g_focus_sound_handle[index].is_for_session);
1648 if (ret == MM_ERROR_NONE)
1649 debug_msg("[Client] Success to acquire focus\n");
1651 debug_error("[Client] Error occurred : 0x%x \n",ret);
1654 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1659 int mm_sound_client_release_focus(int id, mm_sound_focus_type_e type, const char *option)
1661 int ret = MM_ERROR_NONE;
1666 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1668 index = _focus_find_index_by_handle(id);
1670 debug_error("Could not find index");
1671 ret = MM_ERROR_INVALID_ARGUMENT;
1674 instance = g_focus_sound_handle[index].focus_tid;
1676 ret = mm_sound_proxy_release_focus(instance, id, type, option, g_focus_sound_handle[index].is_for_session);
1678 if (ret == MM_ERROR_NONE)
1679 debug_msg("[Client] Success to release focus\n");
1681 debug_error("[Client] Error occurred : 0x%x \n",ret);
1684 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1689 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)
1691 int ret = MM_ERROR_NONE;
1698 return MM_ERROR_INVALID_ARGUMENT;
1700 //pthread_mutex_lock(&g_thread_mutex2);
1704 ret = mm_sound_proxy_get_unique_id(id);
1708 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1710 for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
1711 if (g_focus_sound_handle[index].is_used == false) {
1712 g_focus_sound_handle[index].is_used = true;
1717 g_focus_sound_handle[index].focus_tid = instance;
1718 g_focus_sound_handle[index].handle = *id;
1719 g_focus_sound_handle[index].watch_callback = callback;
1720 g_focus_sound_handle[index].user_data = user_data;
1721 g_focus_sound_handle[index].is_for_session = is_for_session;
1723 ret = mm_sound_proxy_set_focus_watch_callback(pid, g_focus_sound_handle[index].handle, focus_type, callback, is_for_session, user_data);
1725 if (ret == MM_ERROR_NONE) {
1726 debug_msg("[Client] Success to watch focus");
1727 g_need_emergent_exit = TRUE;
1728 if (!g_focus_thread) {
1729 GMainContext* focus_context = g_main_context_new ();
1730 g_focus_loop = g_main_loop_new (focus_context, FALSE);
1731 g_main_context_unref(focus_context);
1732 g_focus_thread = g_thread_new("focus-callback-thread", _focus_thread_func, NULL);
1733 if (g_focus_thread == NULL) {
1734 debug_error ("could not create thread..");
1735 g_main_loop_unref(g_focus_loop);
1736 ret = MM_ERROR_SOUND_INTERNAL;
1741 debug_error("[Client] Error occurred : 0x%x",ret);
1745 _focus_init_callback(index, true);
1750 g_focus_sound_handle[index].is_used = false;
1753 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1758 int mm_sound_client_unset_focus_watch_callback(int id)
1760 int ret = MM_ERROR_NONE;
1763 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1765 index = _focus_find_index_by_handle(id);
1767 debug_error("Could not find index");
1768 ret = MM_ERROR_INVALID_ARGUMENT;
1772 g_mutex_lock(&g_focus_sound_handle[index].focus_lock);
1774 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);
1776 if (ret == MM_ERROR_NONE)
1777 debug_msg("[Client] Success to unwatch focus\n");
1779 debug_error("[Client] Error occurred : 0x%x \n",ret);
1782 g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
1784 _focus_destroy_callback(index, true);
1785 g_focus_sound_handle[index].focus_fd = 0;
1786 g_focus_sound_handle[index].focus_tid = 0;
1787 g_focus_sound_handle[index].handle = 0;
1788 g_focus_sound_handle[index].is_used = false;
1790 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1797 int mm_sound_client_add_test_callback(mm_sound_test_cb func, void* user_data, unsigned int *subs_id)
1799 int ret = MM_ERROR_NONE;
1803 ret = mm_sound_proxy_add_test_callback(func, user_data, g_free, subs_id);
1809 int mm_sound_client_remove_test_callback(unsigned int subs_id)
1811 int ret = MM_ERROR_NONE;
1814 ret = mm_sound_proxy_remove_test_callback(subs_id);
1821 int mm_sound_client_test(int a, int b, int* getv)
1823 int ret = MM_ERROR_NONE;
1827 ret = mm_sound_proxy_test(a, b, getv);
1828 debug_log("%d * %d -> result : %d", a, b, *getv);