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 typedef struct _FocusSource {
80 #define GET_CB_DATA(_cb_data, _func, _userdata, _extradata) \
82 _cb_data = (struct callback_data*) g_malloc0(sizeof(struct callback_data)); \
83 _cb_data->user_cb = _func; \
84 _cb_data->user_data = _userdata; \
85 _cb_data->extra_data = _extradata; \
93 GSourceFuncs *g_src_funcs;
99 GThread *focus_cb_thread;
100 GMainLoop *focus_loop;
101 mm_sound_focus_changed_cb focus_callback;
102 mm_sound_focus_changed_watch_cb watch_callback;
105 bool is_for_session; /* will be removed when the session concept is completely left out*/
106 } focus_sound_info_t;
113 char stream_type[MAX_STREAM_TYPE_LEN];
114 char ext_info[MM_SOUND_NAME_NUM];
119 mm_sound_focus_session_interrupt_cb user_cb;
121 } focus_session_interrupt_info_t;
123 typedef gboolean (*focus_gLoopPollHandler_t)(gpointer d);
125 focus_sound_info_t g_focus_sound_handle[FOCUS_HANDLE_MAX];
126 focus_session_interrupt_info_t g_focus_session_interrupt_info = {NULL, NULL};
127 static pthread_mutex_t g_index_mutex = PTHREAD_MUTEX_INITIALIZER;
128 static pthread_mutex_t g_event_mutex = PTHREAD_MUTEX_INITIALIZER;
129 guint g_focus_signal_handle;
130 guint g_idle_event_src;
133 gboolean g_need_emergent_exit = FALSE;
136 /* handle to watch end of playing */
138 /* subscription id to unsubscribe when handle ended */
140 } play_sound_end_callback_data_t;
142 typedef struct _focus_idle_event {
143 focus_idle_event_type_e type;
145 } focus_idle_event_t;
147 void _system_signal_handler(int signo)
149 int ret = MM_ERROR_NONE;
150 sigset_t old_mask, all_mask;
152 debug_warning("Got signal : signo(%d)", signo);
156 sigfillset(&all_mask);
157 sigprocmask(SIG_BLOCK, &all_mask, &old_mask);
159 if (g_need_emergent_exit) {
160 ret = mm_sound_proxy_emergent_exit(getpid());
161 if (ret == MM_ERROR_NONE)
162 debug_msg("[Client] Success to emergnet_exit\n");
164 debug_error("[Client] Error occurred : 0x%x \n",ret);
167 sigprocmask(SIG_SETMASK, &old_mask, NULL);
172 sigaction(SIGINT, &system_int_old_action, NULL);
176 sigaction(SIGABRT, &system_abrt_old_action, NULL);
180 sigaction(SIGSEGV, &system_segv_old_action, NULL);
184 sigaction(SIGTERM, &system_term_old_action, NULL);
188 sigaction(SIGSYS, &system_sys_old_action, NULL);
192 sigaction(SIGXCPU, &system_xcpu_old_action, NULL);
199 debug_warning("signal handling end");
202 int mm_sound_client_initialize(void)
204 int ret = MM_ERROR_NONE;
207 mm_sound_proxy_initialize();
208 g_idle_event_src = 0;
211 struct sigaction system_action;
212 system_action.sa_handler = _system_signal_handler;
213 system_action.sa_flags = SA_NOCLDSTOP;
215 sigemptyset(&system_action.sa_mask);
217 sigaction(SIGINT, &system_action, &system_int_old_action);
218 sigaction(SIGABRT, &system_action, &system_abrt_old_action);
219 sigaction(SIGSEGV, &system_action, &system_segv_old_action);
220 sigaction(SIGTERM, &system_action, &system_term_old_action);
221 sigaction(SIGSYS, &system_action, &system_sys_old_action);
222 sigaction(SIGXCPU, &system_action, &system_xcpu_old_action);
228 int mm_sound_client_finalize(void)
230 int ret = MM_ERROR_NONE;
234 if (g_need_emergent_exit) {
235 ret = mm_sound_proxy_emergent_exit(getpid());
236 if (ret == MM_ERROR_NONE)
237 debug_msg("[Client] Success to emergnet_exit\n");
239 debug_error("[Client] Error occurred : 0x%x \n",ret);
242 sigaction(SIGINT, &system_int_old_action, NULL);
243 sigaction(SIGABRT, &system_abrt_old_action, NULL);
244 sigaction(SIGSEGV, &system_segv_old_action, NULL);
245 sigaction(SIGTERM, &system_term_old_action, NULL);
246 sigaction(SIGSYS, &system_sys_old_action, NULL);
247 sigaction(SIGXCPU, &system_xcpu_old_action, NULL);
249 ret = mm_sound_proxy_finalize();
252 if (g_idle_event_src > 0) {
253 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_event_mutex, MM_ERROR_SOUND_INTERNAL);
254 g_source_remove(g_idle_event_src);
255 MMSOUND_LEAVE_CRITICAL_SECTION(&g_event_mutex);
263 void mm_sound_convert_volume_type_to_stream_type(int volume_type, char *stream_type)
265 switch (volume_type) {
266 case VOLUME_TYPE_SYSTEM:
267 MMSOUND_STRNCPY(stream_type, "system", MM_SOUND_STREAM_TYPE_LEN);
269 case VOLUME_TYPE_NOTIFICATION:
270 MMSOUND_STRNCPY(stream_type, "notification", MM_SOUND_STREAM_TYPE_LEN);
272 case VOLUME_TYPE_ALARM:
273 MMSOUND_STRNCPY(stream_type, "alarm", MM_SOUND_STREAM_TYPE_LEN);
275 case VOLUME_TYPE_RINGTONE:
276 MMSOUND_STRNCPY(stream_type, "ringtone-voip", MM_SOUND_STREAM_TYPE_LEN);
278 case VOLUME_TYPE_MEDIA:
279 MMSOUND_STRNCPY(stream_type, "media", MM_SOUND_STREAM_TYPE_LEN);
281 case VOLUME_TYPE_CALL:
282 MMSOUND_STRNCPY(stream_type, "call-voice", MM_SOUND_STREAM_TYPE_LEN);
284 case VOLUME_TYPE_VOIP:
285 MMSOUND_STRNCPY(stream_type, "voip", MM_SOUND_STREAM_TYPE_LEN);
287 case VOLUME_TYPE_VOICE:
288 MMSOUND_STRNCPY(stream_type, "voice-information", MM_SOUND_STREAM_TYPE_LEN);
291 MMSOUND_STRNCPY(stream_type, "media", MM_SOUND_STREAM_TYPE_LEN);
295 debug_msg("volume type (%d) converted to stream type (%s)", volume_type, stream_type);
299 /*****************************************************************************************
300 DBUS SUPPORTED FUNCTIONS
301 ******************************************************************************************/
303 void _mm_sound_client_focus_signal_callback(mm_sound_signal_name_t signal, int value, void *user_data)
305 int ret = MM_ERROR_NONE;
308 debug_msg("focus signal received, value = %d", value);
311 ret = mm_sound_proxy_clear_focus(getpid());
313 debug_error("clear focus failed ret = 0x%x", ret);
314 mm_sound_unsubscribe_signal(g_focus_signal_handle);
315 g_focus_signal_handle = 0;
320 int mm_sound_client_play_tone(int number, int volume_config, double volume,
321 int time, int *handle, bool enable_session)
323 int ret = MM_ERROR_NONE;
324 // int instance = -1; /* instance is unique to communicate with server : client message queue filter type */
325 int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(volume_config);
326 char stream_type[MM_SOUND_STREAM_TYPE_LEN] = {0, };
330 /* read session information */
331 int session_type = MM_SESSION_TYPE_MEDIA;
332 int session_options = 0;
333 int is_focus_registered = 0;
335 ret = mm_sound_get_signal_value(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &is_focus_registered);
337 debug_error("mm_sound_get_signal_value failed [0x%x]", ret);
338 return MM_ERROR_POLICY_INTERNAL;
341 if (is_focus_registered)
342 enable_session = false;
344 if (enable_session) {
345 if (MM_ERROR_NONE != _mm_session_util_read_information(-1, &session_type, &session_options)) {
346 debug_warning("[Client] Read Session Information failed. use default \"media\" type\n");
347 session_type = MM_SESSION_TYPE_MEDIA;
349 if(MM_ERROR_NONE != mm_session_init(session_type)) {
350 debug_critical("[Client] MMSessionInit() failed\n");
351 return MM_ERROR_POLICY_INTERNAL;
356 // instance = getpid();
357 //debug_log("[Client] pid for client ::: [%d]\n", instance);
360 debug_msg("[Client] Input number : %d\n", number);
361 /* Send req memory */
363 mm_sound_convert_volume_type_to_stream_type(volume_type, stream_type);
364 ret = mm_sound_proxy_play_tone(number, time, volume, volume_config,
365 session_type, session_options, getpid(), enable_session, handle, stream_type, -1);
367 if (enable_session && !g_focus_signal_handle) {
368 ret = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &g_focus_signal_handle,
369 _mm_sound_client_focus_signal_callback, NULL);
371 debug_error("mm_sound_subscribe_signal failed [0x%x]", ret);
372 return MM_ERROR_POLICY_INTERNAL;
381 int mm_sound_client_play_tone_with_stream_info(int tone, char *stream_type, int stream_id,
382 double volume, int duration, int *handle)
384 int ret = MM_ERROR_NONE;
388 ret = mm_sound_proxy_play_tone_with_stream_info(getpid(), tone, stream_type, stream_id, volume, duration, handle);
394 static void _mm_sound_stop_callback_wrapper_func(int ended_handle, void *userdata)
396 struct callback_data *cb_data = (struct callback_data*) userdata;
397 play_sound_end_callback_data_t *end_cb_data;
399 debug_log("[Wrapper CB][Play Stop] ended_handle : %d", ended_handle);
401 if (cb_data == NULL) {
402 debug_warning("stop callback data null");
406 end_cb_data = (play_sound_end_callback_data_t*) cb_data->extra_data;
408 if (ended_handle == end_cb_data->watching_handle) {
409 debug_log("Interested playing handle end : %d", ended_handle);
410 ((mm_sound_stop_callback_func)(cb_data->user_cb))(cb_data->user_data, ended_handle);
411 if (mm_sound_proxy_remove_play_sound_end_callback(end_cb_data->subs_id) != MM_ERROR_NONE)
412 debug_error("mm_sound_client_dbus_remove_play_file_end_callback failed");
414 debug_log("Not interested playing handle : %d", ended_handle);
418 static void play_end_callback_data_free_func(void *data)
420 struct callback_data *cb_data = (struct callback_data*) data;
423 g_free(cb_data->extra_data);
428 int mm_sound_client_play_sound(MMSoundPlayParam *param, int tone, int *handle)
430 int ret = MM_ERROR_NONE;
431 int session_type = MM_SESSION_TYPE_MEDIA;
432 int session_options = 0;
433 int is_focus_registered = 0;
434 // int instance = -1; /* instance is unique to communicate with server : client message queue filter type */
435 int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(param->volume_config);
436 char stream_type[MM_SOUND_STREAM_TYPE_LEN] = {0, };
437 struct callback_data *cb_data = NULL;
438 play_sound_end_callback_data_t *end_cb_data;
442 /* read session information */
444 ret = mm_sound_get_signal_value(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &is_focus_registered);
446 debug_error("mm_sound_get_signal_value failed [0x%x]", ret);
447 return MM_ERROR_POLICY_INTERNAL;
450 if (is_focus_registered)
451 param->skip_session = true;
453 if (param->skip_session == false) {
454 if(MM_ERROR_NONE != _mm_session_util_read_information(-1, &session_type, &session_options)) {
455 debug_warning("[Client] Read MMSession Type failed. use default \"media\" type\n");
456 session_type = MM_SESSION_TYPE_MEDIA;
458 if(MM_ERROR_NONE != mm_session_init(session_type)) {
459 debug_critical("[Client] MMSessionInit() failed\n");
460 return MM_ERROR_POLICY_INTERNAL;
466 if ((param->mem_ptr && param->mem_size)) {
467 // Play memory, deprecated
468 return MM_ERROR_INVALID_ARGUMENT;
471 mm_sound_convert_volume_type_to_stream_type(volume_type, stream_type);
472 ret = mm_sound_proxy_play_sound(param->filename, tone, param->loop, param->volume, param->volume_config,
473 session_type, session_options, getpid(), param->skip_session, handle, stream_type, -1);
474 if (ret != MM_ERROR_NONE) {
475 debug_error("Play Sound Failed");
478 if (param->callback) {
479 end_cb_data = (play_sound_end_callback_data_t *) g_malloc0(sizeof(play_sound_end_callback_data_t));
480 end_cb_data->watching_handle = *handle;
481 GET_CB_DATA(cb_data, param->callback, param->data, end_cb_data);
483 ret = mm_sound_proxy_add_play_sound_end_callback(_mm_sound_stop_callback_wrapper_func, cb_data,
484 play_end_callback_data_free_func, &end_cb_data->subs_id);
485 if (ret != MM_ERROR_NONE) {
486 debug_error("Add callback for play sound(%d) Failed", *handle);
490 if (!param->skip_session && !g_focus_signal_handle) {
491 ret = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &g_focus_signal_handle,
492 _mm_sound_client_focus_signal_callback, NULL);
494 debug_error("mm_sound_subscribe_signal failed [0x%x]", ret);
495 return MM_ERROR_POLICY_INTERNAL;
506 int mm_sound_client_play_sound_with_stream_info(MMSoundPlayParam *param, int *handle, char* stream_type, int stream_id)
508 int ret = MM_ERROR_NONE;
509 struct callback_data *cb_data = NULL;
510 play_sound_end_callback_data_t *end_cb_data;
512 ret = mm_sound_proxy_play_sound_with_stream_info(param->filename, param->loop, param->volume,
513 getpid(), handle, stream_type, stream_id);
514 if (ret != MM_ERROR_NONE) {
515 debug_error("Play Sound Failed");
518 if (param->callback) {
519 end_cb_data = (play_sound_end_callback_data_t *) g_malloc0(sizeof(play_sound_end_callback_data_t));
520 end_cb_data->watching_handle = *handle;
521 GET_CB_DATA(cb_data, param->callback, param->data, end_cb_data);
523 ret = mm_sound_proxy_add_play_sound_end_callback(_mm_sound_stop_callback_wrapper_func, cb_data,
524 play_end_callback_data_free_func, &end_cb_data->subs_id);
525 if (ret != MM_ERROR_NONE) {
526 debug_error("Add callback for play sound(%d) Failed", *handle);
537 int mm_sound_client_stop_sound(int handle)
539 int ret = MM_ERROR_NONE;
542 if (handle < 0 || handle > CLIENT_HANDLE_MAX) {
543 ret = MM_ERROR_INVALID_ARGUMENT;
547 ret = mm_sound_proxy_stop_sound(handle);
553 static int _mm_sound_client_device_list_dump (GList *device_list)
555 int ret = MM_ERROR_NONE;
557 mm_sound_device_t *device_node = NULL;
560 debug_error("Device list NULL, cannot dump list");
561 return MM_ERROR_SOUND_INTERNAL;
564 debug_log("======================== device list : start ==========================\n");
565 for (list = device_list; list != NULL; list = list->next) {
566 device_node = (mm_sound_device_t *)list->data;
568 debug_log(" list idx[%d]: type[%17s], id[%02d], io_direction[%d], state[%d], name[%s]\n",
569 count++, device_node->type, device_node->id, device_node->io_direction,
570 device_node->state, device_node->name);
573 debug_log("======================== device list : end ============================\n");
578 int mm_sound_client_get_current_connected_device_list(int device_flags, mm_sound_device_list_t *device_list)
580 int ret = MM_ERROR_NONE;
584 debug_error("Device list NULL");
585 ret = MM_ERROR_COMMON_INVALID_ARGUMENT;
589 if ((ret = mm_sound_proxy_get_current_connected_device_list(device_flags, &device_list->list)) != MM_ERROR_NONE) {
590 debug_error("[Client] failed to get current connected device list with dbus, ret[0x%x]", ret);
593 if (!device_list->list) {
594 debug_error("Got device list null");
595 ret = MM_ERROR_SOUND_NO_DATA;
598 _mm_sound_client_device_list_dump(device_list->list);
605 static bool device_is_match_direction(int direction, int mask)
607 if (mask == DEVICE_IO_DIRECTION_FLAGS || mask == 0)
610 if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_IN_FLAG) && (direction & MM_SOUND_DEVICE_IO_DIRECTION_IN))
612 if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_OUT_FLAG) && (direction & MM_SOUND_DEVICE_IO_DIRECTION_OUT))
614 if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_BOTH_FLAG) && (direction == MM_SOUND_DEVICE_IO_DIRECTION_BOTH))
620 static bool device_is_match_state(int state, int mask)
622 if (mask == DEVICE_STATE_FLAGS || mask == 0)
625 if ((mask & MM_SOUND_DEVICE_STATE_DEACTIVATED_FLAG) && (state == MM_SOUND_DEVICE_STATE_DEACTIVATED))
627 if ((mask & MM_SOUND_DEVICE_STATE_ACTIVATED_FLAG) && (state == MM_SOUND_DEVICE_STATE_ACTIVATED))
633 static bool device_is_match_type(const char *type, int mask)
636 const char *builtin_prefix = "builtin";
638 if (mask == DEVICE_TYPE_FLAGS || mask == 0)
641 is_builtin = !strncmp(type, builtin_prefix, strlen(builtin_prefix));
643 if ((mask & MM_SOUND_DEVICE_TYPE_INTERNAL_FLAG) && (is_builtin))
645 if ((mask & MM_SOUND_DEVICE_TYPE_EXTERNAL_FLAG) && (!is_builtin))
651 static bool device_is_match_with_mask(const char *type, int direction, int state, int mask)
653 if (mask == DEVICE_ALL_FLAG)
656 return (device_is_match_direction(direction, mask & DEVICE_IO_DIRECTION_FLAGS) &&
657 device_is_match_state(state, mask & DEVICE_STATE_FLAGS) &&
658 device_is_match_type(type, mask & DEVICE_TYPE_FLAGS));
661 static int _fill_sound_device(mm_sound_device_t *device_h, int device_id, const char *device_type,
662 int direction, int state, const char *name, int *stream_id, int stream_num)
666 if (stream_num > 0 && stream_id == NULL) {
667 debug_error("stream_num is %d, but stream_id is NULL", stream_num);
671 if (stream_num > MAX_STREAM_ON_DEVICE) {
672 debug_error("too many streams on this device");
676 device_h->id = device_id;
677 device_h->io_direction = direction;
678 device_h->state = state;
679 MMSOUND_STRNCPY(device_h->name, name, MAX_DEVICE_NAME_NUM);
680 MMSOUND_STRNCPY(device_h->type, device_type, MAX_DEVICE_TYPE_STR_LEN);
682 if (stream_num > 0) {
683 device_h->stream_num = stream_num;
684 debug_log("%d streams on this device", stream_num);
685 for (i = 0; i < stream_num; i++) {
686 debug_log(" stream_id : %d", stream_id[i]);
687 device_h->stream_id[i] = stream_id[i];
690 device_h->stream_num = 0;
691 debug_log("There is no stream on this device");
697 static void _mm_sound_device_connected_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
698 int state, const char *name, int *stream_id, int stream_num,
699 gboolean is_connected, void *userdata)
701 mm_sound_device_t device_h;
702 struct callback_data *cb_data = (struct callback_data*) userdata;
705 debug_log("[Device %s] id(%d) type(%s) direction(%d) state(%d) name(%s)",
706 is_connected ? "Connected" : "Disconnected", device_id, device_type, io_direction,
707 state, name, is_connected);
709 if (cb_data == NULL) {
710 debug_warning("device connected changed callback data null");
714 device_flags = (int) cb_data->extra_data;
715 if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
718 if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name, stream_id, stream_num) < 0) {
719 debug_error("Failed to fill sound device");
723 ((mm_sound_device_connected_cb)(cb_data->user_cb))(&device_h, is_connected, cb_data->user_data);
726 int mm_sound_client_add_device_connected_callback(int device_flags, mm_sound_device_connected_cb func,
727 void* userdata, unsigned int *subs_id)
729 int ret = MM_ERROR_NONE;
730 struct callback_data *cb_data = NULL;
734 GET_CB_DATA(cb_data, func, userdata, (void*) device_flags);
736 ret = mm_sound_proxy_add_device_connected_callback(device_flags,
737 _mm_sound_device_connected_callback_wrapper_func,
738 cb_data, g_free, subs_id);
739 if (ret == MM_ERROR_NONE)
740 g_need_emergent_exit = TRUE;
746 int mm_sound_client_remove_device_connected_callback(unsigned int subs_id)
748 int ret = MM_ERROR_NONE;
751 ret = mm_sound_proxy_remove_device_connected_callback(subs_id);
757 static void _mm_sound_device_info_changed_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
758 int state, const char *name, int *stream_id, int stream_num,
759 int changed_device_info_type, void *userdata)
761 mm_sound_device_t device_h;
762 struct callback_data *cb_data = (struct callback_data*) userdata;
765 debug_log("[Device Info Changed] id(%d) type(%s) direction(%d) state(%d) name(%s) changed_info_type(%d)",
766 device_id, device_type, io_direction, state, name, changed_device_info_type);
768 if (cb_data == NULL) {
769 debug_warning("device info changed callback data null");
773 device_flags = (int) cb_data->extra_data;
774 if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
777 if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name, stream_id, stream_num) < 0) {
778 debug_error("Failed to fill sound device");
782 ((mm_sound_device_info_changed_cb)(cb_data->user_cb))(&device_h, changed_device_info_type, cb_data->user_data);
785 int mm_sound_client_add_device_info_changed_callback(int device_flags, mm_sound_device_info_changed_cb func,
786 void *userdata, unsigned int *subs_id)
788 int ret = MM_ERROR_NONE;
789 struct callback_data *cb_data = (struct callback_data*) userdata;
793 GET_CB_DATA(cb_data, func, userdata, (void *) device_flags);
795 ret = mm_sound_proxy_add_device_info_changed_callback(device_flags,
796 _mm_sound_device_info_changed_callback_wrapper_func,
797 cb_data, g_free, subs_id);
803 int mm_sound_client_remove_device_info_changed_callback(unsigned int subs_id)
805 int ret = MM_ERROR_NONE;
808 ret = mm_sound_proxy_remove_device_info_changed_callback(subs_id);
815 static void _mm_sound_device_state_changed_callback_wrapper_func(int device_id, const char *device_type,
816 int io_direction, int state, const char *name,
817 int *stream_id, int stream_num, void *userdata)
819 mm_sound_device_t device_h;
820 struct callback_data *cb_data = (struct callback_data*) userdata;
823 debug_log("[Device State Changed] id(%d) type(%s) direction(%d) state(%d) name(%s)",
824 device_id, device_type, io_direction, state, name);
826 if (cb_data == NULL) {
827 debug_warning("device state changed callback data null");
831 device_flags = (int) cb_data->extra_data;
833 if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
836 if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name, stream_id, stream_num) < 0) {
837 debug_error("Failed to fill sound device");
841 ((mm_sound_device_state_changed_cb)(cb_data->user_cb))(&device_h, state, cb_data->user_data);
844 int mm_sound_client_add_device_state_changed_callback(int device_flags, mm_sound_device_state_changed_cb func,
845 void *userdata, unsigned int *id)
847 int ret = MM_ERROR_NONE;
848 struct callback_data *cb_data = (struct callback_data*) userdata;
852 GET_CB_DATA(cb_data, func, userdata, (void *) device_flags);
854 ret = mm_sound_proxy_add_device_state_changed_callback(device_flags,
855 _mm_sound_device_state_changed_callback_wrapper_func,
856 cb_data, g_free, id);
862 int mm_sound_client_remove_device_state_changed_callback(unsigned int id)
864 int ret = MM_ERROR_NONE;
867 ret = mm_sound_proxy_remove_device_state_changed_callback(id);
873 int mm_sound_client_is_stream_on_device(int stream_id, int device_id, bool *is_on)
875 int ret = MM_ERROR_NONE;
879 debug_error("Invalid Parameter");
880 ret = MM_ERROR_COMMON_INVALID_ARGUMENT;
884 if ((ret = mm_sound_proxy_is_stream_on_device(stream_id, device_id, is_on)) != MM_ERROR_NONE) {
885 debug_error("[Client] failed to query is stream on device, ret[0x%x]", ret);
894 int __convert_volume_type_to_str(int volume_type, char **volume_type_str)
896 int ret = MM_ERROR_NONE;
898 if (!volume_type_str) {
899 return MM_ERROR_COMMON_INVALID_ARGUMENT;
902 switch (volume_type) {
903 case VOLUME_TYPE_SYSTEM:
904 *volume_type_str = "system";
906 case VOLUME_TYPE_NOTIFICATION:
907 *volume_type_str = "notification";
909 case VOLUME_TYPE_ALARM:
910 *volume_type_str = "alarm";
912 case VOLUME_TYPE_RINGTONE:
913 *volume_type_str = "ringtone";
915 case VOLUME_TYPE_MEDIA:
916 *volume_type_str = "media";
918 case VOLUME_TYPE_CALL:
919 *volume_type_str = "call";
921 case VOLUME_TYPE_VOIP:
922 *volume_type_str = "voip";
924 case VOLUME_TYPE_VOICE:
925 *volume_type_str = "voice";
928 if (!strncmp(*volume_type_str,"", VOLUME_TYPE_LEN)) {
929 debug_error("could not find the volume_type[%d] in this switch case statement", volume_type);
930 ret = MM_ERROR_SOUND_INTERNAL;
932 debug_log("volume_type[%s]", *volume_type_str);
937 static int __convert_volume_type_to_int(const char *volume_type_str, volume_type_t *volume_type)
939 int ret = MM_ERROR_NONE;
941 if (!volume_type || !volume_type_str) {
942 return MM_ERROR_COMMON_INVALID_ARGUMENT;
945 if (!strncmp(volume_type_str, "system", VOLUME_TYPE_LEN)) {
946 *volume_type = VOLUME_TYPE_SYSTEM;
947 } else if (!strncmp(volume_type_str, "notification", VOLUME_TYPE_LEN)) {
948 *volume_type = VOLUME_TYPE_NOTIFICATION;
949 } else if (!strncmp(volume_type_str, "alarm", VOLUME_TYPE_LEN)) {
950 *volume_type = VOLUME_TYPE_ALARM;
951 } else if (!strncmp(volume_type_str, "ringtone", VOLUME_TYPE_LEN)) {
952 *volume_type = VOLUME_TYPE_RINGTONE;
953 } else if (!strncmp(volume_type_str, "media", VOLUME_TYPE_LEN)) {
954 *volume_type = VOLUME_TYPE_MEDIA;
955 } else if (!strncmp(volume_type_str, "call", VOLUME_TYPE_LEN)) {
956 *volume_type = VOLUME_TYPE_CALL;
957 } else if (!strncmp(volume_type_str, "voip", VOLUME_TYPE_LEN)) {
958 *volume_type = VOLUME_TYPE_VOIP;
959 } else if (!strncmp(volume_type_str, "voice", VOLUME_TYPE_LEN)) {
960 *volume_type = VOLUME_TYPE_VOICE;
962 debug_log("Invalid volume type : [%s]", volume_type_str);
963 ret = MM_ERROR_SOUND_INTERNAL;
969 int mm_sound_client_set_volume_by_type(const int volume_type, const unsigned int volume_level)
971 int ret = MM_ERROR_NONE;
972 char *type_str = NULL;
975 if ((ret = __convert_volume_type_to_str(volume_type, &type_str)) != MM_ERROR_NONE) {
976 debug_error("volume type convert failed");
980 ret = mm_sound_proxy_set_volume_by_type(type_str, volume_level);
987 static void _mm_sound_volume_changed_callback_wrapper_func(const char *direction, const char *volume_type_str,
988 int volume_level, void *userdata)
990 volume_type_t volume_type = 0;
991 struct callback_data *cb_data = (struct callback_data *) userdata;
993 debug_log("[Wrapper CB][Volume Changed] direction : %s, volume_type : %s, volume_level : %d",
994 direction, volume_type_str, volume_level);
996 if (cb_data == NULL) {
997 debug_warning("volume changed callback data null");
1001 if (__convert_volume_type_to_int(volume_type_str, &volume_type) != MM_ERROR_NONE) {
1002 debug_error("volume type convert failed");
1005 debug_log("Call volume changed user cb, direction : %s, vol_type : %s(%d), level : %u",
1006 direction, volume_type_str, volume_type, volume_level);
1007 ((mm_sound_volume_changed_cb)(cb_data->user_cb))(volume_type, volume_level, cb_data->user_data);
1010 int mm_sound_client_add_volume_changed_callback(mm_sound_volume_changed_cb func, void* userdata, unsigned int *subs_id)
1012 int ret = MM_ERROR_NONE;
1013 struct callback_data *cb_data = NULL;
1017 GET_CB_DATA(cb_data, func, userdata, NULL);
1019 ret = mm_sound_proxy_add_volume_changed_callback(_mm_sound_volume_changed_callback_wrapper_func, cb_data, g_free, subs_id);
1026 int mm_sound_client_remove_volume_changed_callback(unsigned int subs_id)
1028 int ret = MM_ERROR_NONE;
1031 ret = mm_sound_proxy_remove_volume_changed_callback(subs_id);
1038 int mm_sound_client_set_session_interrupt_callback(mm_sound_focus_session_interrupt_cb callback, void* user_data)
1040 int ret = MM_ERROR_NONE;
1045 return MM_ERROR_INVALID_ARGUMENT;
1047 g_focus_session_interrupt_info.user_cb = callback;
1048 g_focus_session_interrupt_info.user_data = user_data;
1054 int mm_sound_client_unset_session_interrupt_callback(void)
1056 int ret = MM_ERROR_NONE;
1060 if (!g_focus_session_interrupt_info.user_cb) {
1061 debug_error("no callback to unset");
1062 return MM_ERROR_SOUND_INTERNAL;
1065 g_focus_session_interrupt_info.user_cb = NULL;
1066 g_focus_session_interrupt_info.user_data = NULL;
1072 static gpointer _focus_thread_func(gpointer data)
1074 unsigned int thread_id = (unsigned int)pthread_self();
1075 GMainLoop *focus_loop = (GMainLoop*)data;
1077 debug_warning(">>> thread func : thread id(%u), mainloop[%p]", thread_id, focus_loop);
1079 g_main_loop_run(focus_loop);
1080 debug_warning("<<< quit thread func : thread id(%u), mainloop[%p]", thread_id, focus_loop);
1085 static gboolean _focus_fd_prepare(GSource *source, gint *timeout)
1088 debug_warning("[ PREPARE : %p, (%p, %d)", source, timeout, timeout? *timeout : -1);
1093 static gboolean _focus_fd_check(GSource * source)
1095 FocusSource* fsource = (FocusSource *)source;
1098 debug_error("GSource is null");
1102 debug_warning("CHECK : %p, 0x%x ]", source, fsource->pollfd.revents);
1104 if (fsource->pollfd.revents & (POLLIN | POLLPRI))
1110 static gboolean _focus_fd_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
1112 debug_warning("*** DISPATCH : %p, (%p, %p)", source, callback, user_data);
1113 return callback(user_data);
1116 static void _focus_fd_finalize(GSource *source)
1118 debug_warning("### FINALIZE : %p", source);
1121 static int _focus_find_index_by_handle(int handle)
1124 for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1125 if (g_focus_sound_handle[i].focus_callback && handle == g_focus_sound_handle[i].handle) {
1126 /* debug_msg("found index(%d) for handle(%d)", i, handle);*/
1127 return (handle == FOCUS_HANDLE_INIT_VAL)? -1 : i;
1133 static int _focus_watch_find_index_by_handle(int handle)
1136 for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1137 if (g_focus_sound_handle[i].watch_callback && handle == g_focus_sound_handle[i].handle) {
1138 /* debug_msg("found index(%d) for watch handle(%d)", i, handle);*/
1139 return (handle == FOCUS_HANDLE_INIT_VAL)? -1 : i;
1145 static gboolean _focus_callback_handler(gpointer d)
1147 GPollFD *data = (GPollFD*)d;
1150 int focus_index = 0;
1151 focus_cb_data_lib cb_data;
1152 debug_log(">>> focus_callback_handler()..this thread id(%u)\n", (unsigned int)pthread_self());
1154 memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1157 debug_error("GPollFd is null");
1158 return G_SOURCE_CONTINUE;
1160 if (data->revents & (POLLIN | POLLPRI)) {
1161 int changed_state = -1;
1163 count = read(data->fd, &cb_data, sizeof(cb_data));
1165 char str_error[256];
1166 strerror_r(errno, str_error, sizeof(str_error));
1167 debug_error("GpollFD read fail, errno=%d(%s)",errno, str_error);
1168 return G_SOURCE_CONTINUE;
1170 changed_state = cb_data.state;
1171 focus_index = _focus_find_index_by_handle(cb_data.handle);
1172 if (focus_index == -1) {
1173 debug_error("Could not find index");
1174 return G_SOURCE_CONTINUE;
1177 g_mutex_lock(&g_focus_sound_handle[focus_index].focus_lock);
1179 tid = g_focus_sound_handle[focus_index].focus_tid;
1181 if (changed_state != -1) {
1182 debug_msg("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)",
1183 tid, cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type);
1184 if (g_focus_sound_handle[focus_index].focus_callback == NULL) {
1185 debug_error("callback is null..");
1186 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1187 return G_SOURCE_CONTINUE;
1189 debug_msg("[CALLBACK(%p) START]",g_focus_sound_handle[focus_index].focus_callback);
1190 g_focus_sound_handle[focus_index].focus_callback(cb_data.handle, cb_data.type, cb_data.state,
1191 cb_data.stream_type, cb_data.option, cb_data.ext_info,
1192 g_focus_sound_handle[focus_index].user_data);
1193 debug_msg("[CALLBACK END]");
1194 if (g_focus_session_interrupt_info.user_cb) {
1195 debug_msg("sending session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
1196 g_focus_session_interrupt_info.user_cb(cb_data.state, cb_data.stream_type, false,
1197 g_focus_session_interrupt_info.user_data);
1200 #ifdef CONFIG_ENABLE_RETCB
1204 unsigned int buf = 0;
1205 char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr",
1206 g_focus_sound_handle[focus_index].focus_tid,
1208 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1210 char str_error[256];
1211 strerror_r(errno, str_error, sizeof(str_error));
1212 debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n",
1213 tid, tmpfd, filename2, errno, str_error);
1215 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1216 return G_SOURCE_CONTINUE;
1218 /* buf contains data as below,
1219 * |<--12bits--><--4bits (reacquisition)--><--16bits (handle)-->| */
1220 buf = (unsigned int)((0x0000ffff & cb_data.handle) | (g_focus_sound_handle[focus_index].auto_reacquire << 16));
1221 rett = write(tmpfd, &buf, sizeof(buf));
1224 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
1229 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1231 return G_SOURCE_CONTINUE;
1234 static gboolean _focus_watch_callback_handler(gpointer d)
1236 GPollFD *data = (GPollFD*)d;
1239 int focus_index = 0;
1240 focus_cb_data_lib cb_data;
1244 memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1247 debug_error("GPollFd is null");
1248 return G_SOURCE_CONTINUE;
1250 if (data->revents & (POLLIN | POLLPRI)) {
1251 count = read(data->fd, &cb_data, sizeof(cb_data));
1253 char str_error[256];
1254 strerror_r(errno, str_error, sizeof(str_error));
1255 debug_error("GpollFD read fail, errno=%d(%s)",errno, str_error);
1256 return G_SOURCE_CONTINUE;
1259 focus_index = _focus_watch_find_index_by_handle(cb_data.handle);
1260 if (focus_index == -1) {
1261 debug_error("Could not find index");
1262 return G_SOURCE_CONTINUE;
1265 if (!g_focus_sound_handle[focus_index].is_used) {
1266 debug_warning("unsetting watch callback has been already requested");
1267 goto SKIP_CB_AND_RET;
1270 debug_msg("lock focus_lock = %p", &g_focus_sound_handle[focus_index].focus_lock);
1271 g_mutex_lock(&g_focus_sound_handle[focus_index].focus_lock);
1273 tid = g_focus_sound_handle[focus_index].focus_tid;
1275 debug_msg("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)",
1276 tid, cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type);
1278 if (g_focus_sound_handle[focus_index].watch_callback == NULL) {
1279 debug_msg("callback is null..");
1281 debug_msg("[CALLBACK(%p) START]",g_focus_sound_handle[focus_index].watch_callback);
1282 (g_focus_sound_handle[focus_index].watch_callback)(cb_data.handle, cb_data.type, cb_data.state,
1283 cb_data.stream_type, cb_data.ext_info,
1284 g_focus_sound_handle[focus_index].user_data);
1285 debug_msg("[CALLBACK END]");
1286 if (g_focus_session_interrupt_info.user_cb) {
1287 debug_msg("sending session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
1288 (g_focus_session_interrupt_info.user_cb)(cb_data.state, cb_data.stream_type, true,
1289 g_focus_session_interrupt_info.user_data);
1294 #ifdef CONFIG_ENABLE_RETCB
1299 char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr",
1300 g_focus_sound_handle[focus_index].focus_tid,
1302 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1304 char str_error[256];
1305 strerror_r(errno, str_error, sizeof(str_error));
1306 debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n",
1307 tid, tmpfd, filename2, errno, str_error);
1309 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1310 return G_SOURCE_CONTINUE;
1312 buf = cb_data.handle;
1313 rett = write(tmpfd, &buf, sizeof(buf));
1316 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
1321 if (g_focus_sound_handle[focus_index].is_used) {
1322 debug_msg("unlock focus_lock = %p", &g_focus_sound_handle[focus_index].focus_lock);
1323 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1328 return G_SOURCE_CONTINUE;
1331 static void _focus_open_callback(int index, bool is_for_watching)
1338 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1339 debug_error("Invalid focus handle index [%d]", index);
1343 if (is_for_watching) {
1344 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch",
1345 g_focus_sound_handle[index].focus_tid,
1346 g_focus_sound_handle[index].handle);
1348 filename = g_strdup_printf("/tmp/FOCUS.%d.%d",
1349 g_focus_sound_handle[index].focus_tid,
1350 g_focus_sound_handle[index].handle);
1352 pre_mask = umask(0);
1353 if (mknod(filename, S_IFIFO|0666, 0)) {
1354 debug_error("mknod() failure, errno(%d)", errno);
1357 g_focus_sound_handle[index].focus_fd = open(filename, O_RDWR|O_NONBLOCK);
1358 if (g_focus_sound_handle[index].focus_fd == -1) {
1359 debug_error("Open fail : index(%d), file open error(%d)", index, errno);
1361 debug_log("Open success : index(%d), filename(%s), fd(%d)",
1362 index, filename, g_focus_sound_handle[index].focus_fd);
1367 #ifdef CONFIG_ENABLE_RETCB
1370 if (is_for_watching) {
1371 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr",
1372 g_focus_sound_handle[index].focus_tid,
1373 g_focus_sound_handle[index].handle);
1375 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr",
1376 g_focus_sound_handle[index].focus_tid,
1377 g_focus_sound_handle[index].handle);
1379 pre_mask = umask(0);
1380 if (mknod(filename2, S_IFIFO | 0666, 0)) {
1381 debug_error("mknod() failure, errno(%d)", errno);
1391 void _focus_close_callback(int index, bool is_for_watching)
1393 char *filename = NULL;
1397 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1398 debug_error("Invalid focus handle index [%d]", index);
1402 if (g_focus_sound_handle[index].focus_fd < 0) {
1403 debug_error("Close fail : index(%d)", index);
1405 close(g_focus_sound_handle[index].focus_fd);
1406 debug_log("Close Success : index(%d)", index);
1409 if (is_for_watching) {
1410 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch",
1411 g_focus_sound_handle[index].focus_tid,
1412 g_focus_sound_handle[index].handle);
1414 filename = g_strdup_printf("/tmp/FOCUS.%d.%d",
1415 g_focus_sound_handle[index].focus_tid,
1416 g_focus_sound_handle[index].handle);
1418 if (remove(filename)) {
1419 debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)",
1425 #ifdef CONFIG_ENABLE_RETCB
1428 if (is_for_watching) {
1429 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr",
1430 g_focus_sound_handle[index].focus_tid,
1431 g_focus_sound_handle[index].handle);
1433 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr",
1434 g_focus_sound_handle[index].focus_tid,
1435 g_focus_sound_handle[index].handle);
1437 if (remove(filename2)) {
1438 debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)",
1449 static GSourceFuncs event_funcs = {
1450 .prepare = _focus_fd_prepare,
1451 .check = _focus_fd_check,
1452 .dispatch = _focus_fd_dispatch,
1453 .finalize = _focus_fd_finalize,
1456 static bool _focus_add_sound_callback(int index, focus_gLoopPollHandler_t p_gloop_poll_handler)
1458 FocusSource *fsrc = NULL;
1459 GSource *src = NULL;
1464 g_mutex_init(&g_focus_sound_handle[index].focus_lock);
1466 src = g_source_new(&event_funcs, sizeof(FocusSource));
1468 debug_error("failed to g_source_new for focus source");
1472 fsrc = (FocusSource*) src;
1474 fsrc->pollfd.fd = g_focus_sound_handle[index].focus_fd;
1475 fsrc->pollfd.events = (gushort)(POLLIN | POLLPRI);
1476 g_source_add_poll(src, &fsrc->pollfd);
1478 g_source_set_callback(src, p_gloop_poll_handler, (gpointer)&fsrc->pollfd, NULL);
1480 debug_warning("fsrc(%p), src_funcs(%p), pollfd(%p), fd(%d)",
1481 fsrc, &event_funcs, &fsrc->pollfd, fsrc->pollfd.fd);
1483 fsrc_id = g_source_attach(src, g_main_loop_get_context(g_focus_sound_handle[index].focus_loop));
1485 debug_error("failed to attach the source to context");
1488 g_source_unref(src);
1490 g_focus_sound_handle[index].g_src = src;
1497 g_source_unref(src);
1502 static bool _focus_remove_sound_callback(int index)
1504 focus_sound_info_t *h = NULL;
1508 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1509 debug_error("Invalid focus handle index [%d]", index);
1513 h = &g_focus_sound_handle[index];
1515 g_source_destroy(h->g_src);
1519 h->focus_callback = NULL;
1520 h->watch_callback = NULL;
1522 g_mutex_clear(&h->focus_lock);
1529 static void _focus_add_callback(int index, bool is_for_watching)
1533 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1534 debug_error("Invalid focus handle index [%d]", index);
1538 if (!is_for_watching) {
1539 if (!_focus_add_sound_callback(index, _focus_callback_handler))
1540 debug_error("failed to _focus_add_sound_callback(%p)", _focus_callback_handler);
1541 } else { // need to check if it's necessary
1542 if (!_focus_add_sound_callback(index, _focus_watch_callback_handler))
1543 debug_error("failed to _focus_add_sound_callback(%p)", _focus_watch_callback_handler);
1548 static void _focus_remove_callback(int index)
1551 if (!_focus_remove_sound_callback(index))
1552 debug_error("failed to __focus_remove_sound_callback()");
1556 static void _focus_init_callback(int index, bool is_for_watching)
1559 _focus_open_callback(index, is_for_watching);
1560 _focus_add_callback(index, is_for_watching);
1564 static void _focus_deinit_callback(int index, bool is_for_watching)
1567 _focus_remove_callback(index);
1568 _focus_close_callback(index, is_for_watching);
1572 static int _focus_init_context(int index)
1574 GMainContext *focus_context;
1578 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1579 debug_error("index(%d) is not valid", index);
1580 return MM_ERROR_INVALID_ARGUMENT;
1583 focus_context = g_main_context_new();
1584 g_focus_sound_handle[index].focus_loop = g_main_loop_new(focus_context, FALSE);
1585 g_main_context_unref(focus_context);
1586 if (g_focus_sound_handle[index].focus_loop == NULL) {
1587 debug_error("could not create mainloop..");
1588 return MM_ERROR_SOUND_INTERNAL;
1591 g_focus_sound_handle[index].focus_cb_thread = g_thread_new("focus-cb-thread",
1593 g_focus_sound_handle[index].focus_loop);
1594 if (g_focus_sound_handle[index].focus_cb_thread == NULL) {
1595 debug_error("could not create thread..");
1596 g_main_loop_unref(g_focus_sound_handle[index].focus_loop);
1597 return MM_ERROR_SOUND_INTERNAL;
1600 debug_warning("focus cb thread[%p] with mainloop[%p] is created for index(%d)",
1601 g_focus_sound_handle[index].focus_cb_thread, g_focus_sound_handle[index].focus_loop, index);
1605 return MM_ERROR_NONE;
1608 static void _focus_deinit_context(int index)
1612 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1613 debug_error("index(%d) is not valid", index);
1617 if (!g_focus_sound_handle[index].focus_loop || !g_focus_sound_handle[index].focus_cb_thread) {
1618 debug_error("focus_loop[%p] or focus_cb_thread[%p] is null",
1619 g_focus_sound_handle[index].focus_loop, g_focus_sound_handle[index].focus_cb_thread);
1623 g_main_loop_quit(g_focus_sound_handle[index].focus_loop);
1624 g_thread_join(g_focus_sound_handle[index].focus_cb_thread);
1625 debug_warning("after thread join, thread[%p], mainloop[%p] for index(%d)",
1626 g_focus_sound_handle[index].focus_cb_thread, g_focus_sound_handle[index].focus_loop, index);
1627 g_main_loop_unref(g_focus_sound_handle[index].focus_loop);
1628 g_focus_sound_handle[index].focus_loop = NULL;
1629 g_focus_sound_handle[index].focus_cb_thread = NULL;
1634 int mm_sound_client_get_unique_id(int *id)
1636 int ret = MM_ERROR_NONE;
1641 ret = MM_ERROR_INVALID_ARGUMENT;
1643 ret = mm_sound_proxy_get_unique_id(id);
1650 int mm_sound_client_is_focus_cb_thread(GThread *mine, bool *result)
1652 int ret = MM_ERROR_NONE;
1655 if (!mine || !result)
1656 ret = MM_ERROR_INVALID_ARGUMENT;
1659 for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1660 if (!g_focus_sound_handle[i].is_used)
1662 if (g_focus_sound_handle[i].focus_cb_thread == mine) {
1672 int mm_sound_client_register_focus(int id, int pid, const char *stream_type,
1673 mm_sound_focus_changed_cb callback,
1674 bool is_for_session, void* user_data)
1676 int ret = MM_ERROR_NONE;
1681 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1685 for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
1686 if (g_focus_sound_handle[index].is_used == false) {
1687 g_focus_sound_handle[index].is_used = true;
1692 g_focus_sound_handle[index].focus_tid = instance;
1693 g_focus_sound_handle[index].handle = id;
1694 g_focus_sound_handle[index].focus_callback = callback;
1695 g_focus_sound_handle[index].user_data = user_data;
1696 g_focus_sound_handle[index].is_for_session = is_for_session;
1697 g_focus_sound_handle[index].auto_reacquire = true;
1699 ret = mm_sound_proxy_register_focus(id, pid, stream_type, callback, is_for_session, user_data);
1700 if (ret == MM_ERROR_NONE) {
1701 debug_msg("[Client] Success to register focus\n");
1702 g_need_emergent_exit = TRUE;
1703 if (_focus_init_context(index)) {
1704 ret = MM_ERROR_SOUND_INTERNAL;
1708 debug_error("[Client] Error occurred : 0x%x \n",ret);
1712 _focus_init_callback(index, false);
1716 g_focus_sound_handle[index].is_used = false;
1719 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1725 int mm_sound_client_unregister_focus(int id)
1727 int ret = MM_ERROR_NONE;
1732 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1734 index = _focus_find_index_by_handle(id);
1736 debug_error("Could not find index");
1737 ret = MM_ERROR_INVALID_ARGUMENT;
1740 instance = g_focus_sound_handle[index].focus_tid;
1742 if (!g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1743 debug_warning("maybe focus_callback is being called, try one more time..");
1744 usleep(2500000); // 2.5 sec
1745 if (g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1746 debug_msg("finally got focus_lock");
1750 ret = mm_sound_proxy_unregister_focus(instance, id, g_focus_sound_handle[index].is_for_session);
1751 if (ret == MM_ERROR_NONE)
1752 debug_msg("[Client] Success to unregister focus\n");
1754 debug_error("[Client] Error occurred : 0x%x \n",ret);
1756 g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
1758 _focus_deinit_callback(index, false);
1759 g_focus_sound_handle[index].focus_fd = 0;
1760 g_focus_sound_handle[index].focus_tid = 0;
1761 g_focus_sound_handle[index].handle = 0;
1762 g_focus_sound_handle[index].is_used = false;
1763 _focus_deinit_context(index);
1767 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1772 int mm_sound_client_set_focus_reacquisition(int id, bool reacquisition)
1774 int ret = MM_ERROR_NONE;
1780 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1782 index = _focus_find_index_by_handle(id);
1784 debug_error("Could not find index");
1785 ret = MM_ERROR_INVALID_ARGUMENT;
1788 instance = g_focus_sound_handle[index].focus_tid;
1790 ret = mm_sound_client_is_focus_cb_thread(g_thread_self(), &result);
1792 debug_error("[Client] mm_sound_client_is_focus_cb_thread failed");
1794 } else if (!result) {
1795 ret = mm_sound_proxy_set_foucs_reacquisition(instance, id, reacquisition);
1796 if (ret == MM_ERROR_NONE) {
1797 debug_msg("[Client] Success to set focus reacquisition to [%d]\n", reacquisition);
1799 debug_error("[Client] Error occurred : 0x%x \n",ret);
1803 debug_warning("[Client] Inside the focus cb thread, set focus reacquisition to [%d]\n", reacquisition);
1806 g_focus_sound_handle[index].auto_reacquire = reacquisition;
1809 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1814 int mm_sound_client_get_focus_reacquisition(int id, bool *reacquisition)
1816 int ret = MM_ERROR_NONE;
1821 if (!reacquisition) {
1822 debug_error("Invalid parameter");
1823 return MM_ERROR_INVALID_ARGUMENT;
1826 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1828 index = _focus_find_index_by_handle(id);
1830 debug_error("Could not find index");
1831 ret = MM_ERROR_INVALID_ARGUMENT;
1835 *reacquisition = g_focus_sound_handle[index].auto_reacquire;
1838 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1843 int mm_sound_client_get_acquired_focus_stream_type(int focus_type, char **stream_type, int *option, char **ext_info)
1845 int ret = MM_ERROR_NONE;
1846 char *ext_str = NULL;
1850 ret = mm_sound_proxy_get_acquired_focus_stream_type(focus_type, stream_type, option, &ext_str);
1851 if (ret == MM_ERROR_NONE) {
1852 debug_msg("[Client] Success to get stream type of acquired focus, stream_type(%s), ext_info(%s)\n",
1853 *stream_type, ext_str);
1854 *ext_info = strdup(ext_str);
1857 debug_error("[Client] Error occurred : 0x%x \n",ret);
1864 int mm_sound_client_acquire_focus(int id, mm_sound_focus_type_e type, int option, const char *ext_info)
1866 int ret = MM_ERROR_NONE;
1871 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1873 index = _focus_find_index_by_handle(id);
1875 debug_error("Could not find index");
1876 ret = MM_ERROR_INVALID_ARGUMENT;
1879 instance = g_focus_sound_handle[index].focus_tid;
1881 ret = mm_sound_proxy_acquire_focus(instance, id, type, option, ext_info, g_focus_sound_handle[index].is_for_session);
1882 if (ret == MM_ERROR_NONE)
1883 debug_msg("[Client] Success to acquire focus\n");
1885 debug_error("[Client] Error occurred : 0x%x \n",ret);
1888 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1893 int mm_sound_client_release_focus(int id, mm_sound_focus_type_e type, int option, const char *ext_info)
1895 int ret = MM_ERROR_NONE;
1900 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1902 index = _focus_find_index_by_handle(id);
1904 debug_error("Could not find index");
1905 ret = MM_ERROR_INVALID_ARGUMENT;
1908 instance = g_focus_sound_handle[index].focus_tid;
1910 ret = mm_sound_proxy_release_focus(instance, id, type, option, ext_info, g_focus_sound_handle[index].is_for_session);
1911 if (ret == MM_ERROR_NONE)
1912 debug_msg("[Client] Success to release focus\n");
1914 debug_error("[Client] Error occurred : 0x%x \n",ret);
1917 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1922 int mm_sound_client_set_focus_watch_callback(int pid, mm_sound_focus_type_e focus_type,
1923 mm_sound_focus_changed_watch_cb callback,
1924 bool is_for_session, void* user_data, int *id)
1926 int ret = MM_ERROR_NONE;
1933 return MM_ERROR_INVALID_ARGUMENT;
1935 //pthread_mutex_lock(&g_thread_mutex2);
1939 ret = mm_sound_proxy_get_unique_id(id);
1943 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1945 for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
1946 if (g_focus_sound_handle[index].is_used == false) {
1947 g_focus_sound_handle[index].is_used = true;
1952 g_focus_sound_handle[index].focus_tid = instance;
1953 g_focus_sound_handle[index].handle = *id;
1954 g_focus_sound_handle[index].watch_callback = callback;
1955 g_focus_sound_handle[index].user_data = user_data;
1956 g_focus_sound_handle[index].is_for_session = is_for_session;
1958 ret = mm_sound_proxy_set_focus_watch_callback(pid, g_focus_sound_handle[index].handle, focus_type,
1959 callback, is_for_session, user_data);
1961 if (ret == MM_ERROR_NONE) {
1962 debug_msg("[Client] Success to watch focus");
1963 g_need_emergent_exit = TRUE;
1964 if (_focus_init_context(index)) {
1965 ret = MM_ERROR_SOUND_INTERNAL;
1969 debug_error("[Client] Error occurred : 0x%x",ret);
1973 _focus_init_callback(index, true);
1977 g_focus_sound_handle[index].is_used = false;
1980 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1985 int mm_sound_client_unset_focus_watch_callback(int id)
1987 int ret = MM_ERROR_NONE;
1990 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1992 index = _focus_watch_find_index_by_handle(id);
1994 debug_error("Could not find index");
1995 ret = MM_ERROR_INVALID_ARGUMENT;
1999 g_mutex_lock(&g_focus_sound_handle[index].focus_lock);
2001 g_focus_sound_handle[index].is_used = false;
2003 ret = mm_sound_proxy_unset_focus_watch_callback(g_focus_sound_handle[index].focus_tid,
2004 g_focus_sound_handle[index].handle,
2005 g_focus_sound_handle[index].is_for_session);
2007 if (ret == MM_ERROR_NONE)
2008 debug_msg("[Client] Success to unwatch focus\n");
2010 debug_error("[Client] Error occurred : 0x%x \n",ret);
2013 g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
2015 _focus_deinit_callback(index, true);
2016 g_focus_sound_handle[index].focus_fd = 0;
2017 g_focus_sound_handle[index].focus_tid = 0;
2018 g_focus_sound_handle[index].handle = 0;
2019 _focus_deinit_context(index);
2022 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2027 static gboolean _idle_event_callback(void *data)
2029 focus_idle_event_t *idle_event_data = (focus_idle_event_t*)data;
2030 int ret = MM_ERROR_NONE;
2033 debug_error("data is null");
2037 debug_msg("idle_event_data(%p): type(%d), data(%d)",
2038 idle_event_data, idle_event_data->type, idle_event_data->data);
2040 switch (idle_event_data->type) {
2041 case IDLE_EVENT_TYPE_UNSET_FOCUS_WATCH_CB:
2042 if ((ret = mm_sound_client_unset_focus_watch_callback(idle_event_data->data)))
2043 debug_error("Could not unset focus watch callback, id(%d), ret = %x\n", idle_event_data->data, ret);
2045 case IDLE_EVENT_TYPE_UNREGISTER_FOCUS:
2046 if ((ret = mm_sound_client_unregister_focus(idle_event_data->data)))
2047 debug_error("Could not unregister focus, id(%d), ret = %x\n", idle_event_data->data, ret);
2050 debug_warning("invalid type(%d)", idle_event_data->type);
2054 g_free(idle_event_data);
2056 g_idle_event_src = 0;
2058 MMSOUND_LEAVE_CRITICAL_SECTION(&g_event_mutex);
2063 int mm_sound_client_execute_focus_func_in_main_context(focus_idle_event_type_e type, int data)
2065 focus_idle_event_t *idle_event_data = NULL;
2067 if (IDLE_EVENT_TYPE_UNSET_FOCUS_WATCH_CB > type || IDLE_EVENT_TYPE_MAX < type)
2068 return MM_ERROR_INVALID_ARGUMENT;
2070 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_event_mutex, MM_ERROR_SOUND_INTERNAL);
2072 idle_event_data = g_new0(focus_idle_event_t, 1);
2073 idle_event_data->type = type;
2074 idle_event_data->data = data;
2076 g_idle_event_src = g_idle_add_full(G_PRIORITY_HIGH,
2077 (GSourceFunc)_idle_event_callback,
2078 (gpointer)idle_event_data,
2081 return MM_ERROR_NONE;
2086 int mm_sound_client_add_test_callback(mm_sound_test_cb func, void* user_data, unsigned int *subs_id)
2088 int ret = MM_ERROR_NONE;
2092 ret = mm_sound_proxy_add_test_callback(func, user_data, g_free, subs_id);
2098 int mm_sound_client_remove_test_callback(unsigned int subs_id)
2100 int ret = MM_ERROR_NONE;
2103 ret = mm_sound_proxy_remove_test_callback(subs_id);
2110 int mm_sound_client_test(int a, int b, int* getv)
2112 int ret = MM_ERROR_NONE;
2116 ret = mm_sound_proxy_test(a, b, getv);
2117 debug_log("%d * %d -> result : %d", a, b, *getv);