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; \
97 GThread *focus_cb_thread;
98 GMainLoop *focus_loop;
99 mm_sound_focus_changed_cb focus_callback;
100 mm_sound_focus_changed_watch_cb watch_callback;
102 bool unset_watch_callback_requested;
104 bool is_for_session; /* will be removed when the session concept is completely left out*/
105 } focus_sound_info_t;
112 char stream_type[MAX_STREAM_TYPE_LEN];
113 char ext_info[MM_SOUND_NAME_NUM];
118 mm_sound_focus_session_interrupt_cb user_cb;
120 } focus_session_interrupt_info_t;
122 typedef gboolean (*focus_callback_handler_t)(gpointer user_data);
124 focus_sound_info_t g_focus_sound_handle[FOCUS_HANDLE_MAX];
125 focus_session_interrupt_info_t g_focus_session_interrupt_info = {NULL, NULL};
126 static pthread_mutex_t g_index_mutex = PTHREAD_MUTEX_INITIALIZER;
127 static pthread_mutex_t g_event_mutex = PTHREAD_MUTEX_INITIALIZER;
128 guint g_focus_signal_handle;
129 guint g_idle_event_src;
132 gboolean g_need_emergent_exit = FALSE;
135 /* handle to watch end of playing */
137 /* subscription id to unsubscribe when handle ended */
139 } play_sound_end_callback_data_t;
141 typedef struct _focus_idle_event {
142 focus_idle_event_type_e type;
144 } focus_idle_event_t;
146 void _system_signal_handler(int signo)
148 int ret = MM_ERROR_NONE;
149 sigset_t old_mask, all_mask;
151 debug_warning("Got signal : signo(%d)", signo);
155 sigfillset(&all_mask);
156 sigprocmask(SIG_BLOCK, &all_mask, &old_mask);
158 if (g_need_emergent_exit) {
159 ret = mm_sound_proxy_emergent_exit(getpid());
160 if (ret == MM_ERROR_NONE)
161 debug_msg("[Client] Success to emergnet_exit\n");
163 debug_error("[Client] Error occurred : 0x%x \n",ret);
166 sigprocmask(SIG_SETMASK, &old_mask, NULL);
171 sigaction(SIGINT, &system_int_old_action, NULL);
175 sigaction(SIGABRT, &system_abrt_old_action, NULL);
179 sigaction(SIGSEGV, &system_segv_old_action, NULL);
183 sigaction(SIGTERM, &system_term_old_action, NULL);
187 sigaction(SIGSYS, &system_sys_old_action, NULL);
191 sigaction(SIGXCPU, &system_xcpu_old_action, NULL);
198 debug_warning("signal handling end");
201 int mm_sound_client_initialize(void)
203 int ret = MM_ERROR_NONE;
206 mm_sound_proxy_initialize();
207 g_idle_event_src = 0;
210 struct sigaction system_action;
211 system_action.sa_handler = _system_signal_handler;
212 system_action.sa_flags = SA_NOCLDSTOP;
214 sigemptyset(&system_action.sa_mask);
216 sigaction(SIGINT, &system_action, &system_int_old_action);
217 sigaction(SIGABRT, &system_action, &system_abrt_old_action);
218 sigaction(SIGSEGV, &system_action, &system_segv_old_action);
219 sigaction(SIGTERM, &system_action, &system_term_old_action);
220 sigaction(SIGSYS, &system_action, &system_sys_old_action);
221 sigaction(SIGXCPU, &system_action, &system_xcpu_old_action);
227 int mm_sound_client_finalize(void)
229 int ret = MM_ERROR_NONE;
233 if (g_need_emergent_exit) {
234 ret = mm_sound_proxy_emergent_exit(getpid());
235 if (ret == MM_ERROR_NONE)
236 debug_msg("[Client] Success to emergnet_exit\n");
238 debug_error("[Client] Error occurred : 0x%x \n",ret);
241 sigaction(SIGINT, &system_int_old_action, NULL);
242 sigaction(SIGABRT, &system_abrt_old_action, NULL);
243 sigaction(SIGSEGV, &system_segv_old_action, NULL);
244 sigaction(SIGTERM, &system_term_old_action, NULL);
245 sigaction(SIGSYS, &system_sys_old_action, NULL);
246 sigaction(SIGXCPU, &system_xcpu_old_action, NULL);
248 ret = mm_sound_proxy_finalize();
251 if (g_idle_event_src > 0) {
252 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_event_mutex, MM_ERROR_SOUND_INTERNAL);
253 g_source_remove(g_idle_event_src);
254 MMSOUND_LEAVE_CRITICAL_SECTION(&g_event_mutex);
262 void mm_sound_convert_volume_type_to_stream_type(int volume_type, char *stream_type)
264 switch (volume_type) {
265 case VOLUME_TYPE_SYSTEM:
266 MMSOUND_STRNCPY(stream_type, "system", MM_SOUND_STREAM_TYPE_LEN);
268 case VOLUME_TYPE_NOTIFICATION:
269 MMSOUND_STRNCPY(stream_type, "notification", MM_SOUND_STREAM_TYPE_LEN);
271 case VOLUME_TYPE_ALARM:
272 MMSOUND_STRNCPY(stream_type, "alarm", MM_SOUND_STREAM_TYPE_LEN);
274 case VOLUME_TYPE_RINGTONE:
275 MMSOUND_STRNCPY(stream_type, "ringtone-voip", MM_SOUND_STREAM_TYPE_LEN);
277 case VOLUME_TYPE_MEDIA:
278 MMSOUND_STRNCPY(stream_type, "media", MM_SOUND_STREAM_TYPE_LEN);
280 case VOLUME_TYPE_CALL:
281 MMSOUND_STRNCPY(stream_type, "call-voice", MM_SOUND_STREAM_TYPE_LEN);
283 case VOLUME_TYPE_VOIP:
284 MMSOUND_STRNCPY(stream_type, "voip", MM_SOUND_STREAM_TYPE_LEN);
286 case VOLUME_TYPE_VOICE:
287 MMSOUND_STRNCPY(stream_type, "voice-information", MM_SOUND_STREAM_TYPE_LEN);
290 MMSOUND_STRNCPY(stream_type, "media", MM_SOUND_STREAM_TYPE_LEN);
294 debug_msg("volume type (%d) converted to stream type (%s)", volume_type, stream_type);
298 /*****************************************************************************************
299 DBUS SUPPORTED FUNCTIONS
300 ******************************************************************************************/
302 void _mm_sound_client_focus_signal_callback(mm_sound_signal_name_t signal, int value, void *user_data)
304 int ret = MM_ERROR_NONE;
307 debug_msg("focus signal received, value = %d", value);
310 ret = mm_sound_proxy_clear_focus(getpid());
312 debug_error("clear focus failed ret = 0x%x", ret);
313 mm_sound_unsubscribe_signal(g_focus_signal_handle);
314 g_focus_signal_handle = 0;
319 int mm_sound_client_play_tone(int number, int volume_config, double volume,
320 int time, int *handle, bool enable_session)
322 int ret = MM_ERROR_NONE;
323 // int instance = -1; /* instance is unique to communicate with server : client message queue filter type */
324 int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(volume_config);
325 char stream_type[MM_SOUND_STREAM_TYPE_LEN] = {0, };
329 /* read session information */
330 int session_type = MM_SESSION_TYPE_MEDIA;
331 int session_options = 0;
332 int is_focus_registered = 0;
334 ret = mm_sound_get_signal_value(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &is_focus_registered);
336 debug_error("mm_sound_get_signal_value failed [0x%x]", ret);
337 return MM_ERROR_POLICY_INTERNAL;
340 if (is_focus_registered)
341 enable_session = false;
343 if (enable_session) {
344 if (MM_ERROR_NONE != _mm_session_util_read_information(-1, &session_type, &session_options)) {
345 debug_warning("[Client] Read Session Information failed. use default \"media\" type\n");
346 session_type = MM_SESSION_TYPE_MEDIA;
348 if(MM_ERROR_NONE != mm_session_init(session_type)) {
349 debug_critical("[Client] MMSessionInit() failed\n");
350 return MM_ERROR_POLICY_INTERNAL;
355 // instance = getpid();
356 //debug_log("[Client] pid for client ::: [%d]\n", instance);
359 debug_msg("[Client] Input number : %d\n", number);
360 /* Send req memory */
362 mm_sound_convert_volume_type_to_stream_type(volume_type, stream_type);
363 ret = mm_sound_proxy_play_tone(number, time, volume, volume_config,
364 session_type, session_options, getpid(), enable_session, handle, stream_type, -1);
366 if (enable_session && !g_focus_signal_handle) {
367 ret = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &g_focus_signal_handle,
368 _mm_sound_client_focus_signal_callback, NULL);
370 debug_error("mm_sound_subscribe_signal failed [0x%x]", ret);
371 return MM_ERROR_POLICY_INTERNAL;
380 int mm_sound_client_play_tone_with_stream_info(int tone, char *stream_type, int stream_id,
381 double volume, int duration, int *handle)
383 int ret = MM_ERROR_NONE;
387 ret = mm_sound_proxy_play_tone_with_stream_info(getpid(), tone, stream_type, stream_id, volume, duration, handle);
393 static void _mm_sound_stop_callback_wrapper_func(int ended_handle, void *userdata)
395 struct callback_data *cb_data = (struct callback_data*) userdata;
396 play_sound_end_callback_data_t *end_cb_data;
398 debug_log("[Wrapper CB][Play Stop] ended_handle : %d", ended_handle);
400 if (cb_data == NULL) {
401 debug_warning("stop callback data null");
405 end_cb_data = (play_sound_end_callback_data_t*) cb_data->extra_data;
407 if (ended_handle == end_cb_data->watching_handle) {
408 debug_log("Interested playing handle end : %d", ended_handle);
409 ((mm_sound_stop_callback_func)(cb_data->user_cb))(cb_data->user_data, ended_handle);
410 if (mm_sound_proxy_remove_play_sound_end_callback(end_cb_data->subs_id) != MM_ERROR_NONE)
411 debug_error("mm_sound_client_dbus_remove_play_file_end_callback failed");
413 debug_log("Not interested playing handle : %d", ended_handle);
417 static void play_end_callback_data_free_func(void *data)
419 struct callback_data *cb_data = (struct callback_data*) data;
422 g_free(cb_data->extra_data);
427 int mm_sound_client_play_sound(MMSoundPlayParam *param, int tone, int *handle)
429 int ret = MM_ERROR_NONE;
430 int session_type = MM_SESSION_TYPE_MEDIA;
431 int session_options = 0;
432 int is_focus_registered = 0;
433 // int instance = -1; /* instance is unique to communicate with server : client message queue filter type */
434 int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(param->volume_config);
435 char stream_type[MM_SOUND_STREAM_TYPE_LEN] = {0, };
436 struct callback_data *cb_data = NULL;
437 play_sound_end_callback_data_t *end_cb_data;
441 /* read session information */
443 ret = mm_sound_get_signal_value(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &is_focus_registered);
445 debug_error("mm_sound_get_signal_value failed [0x%x]", ret);
446 return MM_ERROR_POLICY_INTERNAL;
449 if (is_focus_registered)
450 param->skip_session = true;
452 if (param->skip_session == false) {
453 if(MM_ERROR_NONE != _mm_session_util_read_information(-1, &session_type, &session_options)) {
454 debug_warning("[Client] Read MMSession Type failed. use default \"media\" type\n");
455 session_type = MM_SESSION_TYPE_MEDIA;
457 if(MM_ERROR_NONE != mm_session_init(session_type)) {
458 debug_critical("[Client] MMSessionInit() failed\n");
459 return MM_ERROR_POLICY_INTERNAL;
465 if ((param->mem_ptr && param->mem_size)) {
466 // Play memory, deprecated
467 return MM_ERROR_INVALID_ARGUMENT;
470 mm_sound_convert_volume_type_to_stream_type(volume_type, stream_type);
471 ret = mm_sound_proxy_play_sound(param->filename, tone, param->loop, param->volume, param->volume_config,
472 session_type, session_options, getpid(), param->skip_session, handle, stream_type, -1);
473 if (ret != MM_ERROR_NONE) {
474 debug_error("Play Sound Failed");
477 if (param->callback) {
478 end_cb_data = (play_sound_end_callback_data_t *) g_malloc0(sizeof(play_sound_end_callback_data_t));
479 end_cb_data->watching_handle = *handle;
480 GET_CB_DATA(cb_data, param->callback, param->data, end_cb_data);
482 ret = mm_sound_proxy_add_play_sound_end_callback(_mm_sound_stop_callback_wrapper_func, cb_data,
483 play_end_callback_data_free_func, &end_cb_data->subs_id);
484 if (ret != MM_ERROR_NONE) {
485 debug_error("Add callback for play sound(%d) Failed", *handle);
489 if (!param->skip_session && !g_focus_signal_handle) {
490 ret = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &g_focus_signal_handle,
491 _mm_sound_client_focus_signal_callback, NULL);
493 debug_error("mm_sound_subscribe_signal failed [0x%x]", ret);
494 return MM_ERROR_POLICY_INTERNAL;
505 int mm_sound_client_play_sound_with_stream_info(MMSoundPlayParam *param, int *handle, char* stream_type, int stream_id)
507 int ret = MM_ERROR_NONE;
508 struct callback_data *cb_data = NULL;
509 play_sound_end_callback_data_t *end_cb_data;
511 ret = mm_sound_proxy_play_sound_with_stream_info(param->filename, param->loop, param->volume,
512 getpid(), handle, stream_type, stream_id);
513 if (ret != MM_ERROR_NONE) {
514 debug_error("Play Sound Failed");
517 if (param->callback) {
518 end_cb_data = (play_sound_end_callback_data_t *) g_malloc0(sizeof(play_sound_end_callback_data_t));
519 end_cb_data->watching_handle = *handle;
520 GET_CB_DATA(cb_data, param->callback, param->data, end_cb_data);
522 ret = mm_sound_proxy_add_play_sound_end_callback(_mm_sound_stop_callback_wrapper_func, cb_data,
523 play_end_callback_data_free_func, &end_cb_data->subs_id);
524 if (ret != MM_ERROR_NONE) {
525 debug_error("Add callback for play sound(%d) Failed", *handle);
536 int mm_sound_client_stop_sound(int handle)
538 int ret = MM_ERROR_NONE;
541 if (handle < 0 || handle > CLIENT_HANDLE_MAX) {
542 ret = MM_ERROR_INVALID_ARGUMENT;
546 ret = mm_sound_proxy_stop_sound(handle);
552 static int _mm_sound_client_device_list_dump (GList *device_list)
554 int ret = MM_ERROR_NONE;
556 mm_sound_device_t *device_node = NULL;
559 debug_error("Device list NULL, cannot dump list");
560 return MM_ERROR_SOUND_INTERNAL;
563 debug_log("======================== device list : start ==========================\n");
564 for (list = device_list; list != NULL; list = list->next) {
565 device_node = (mm_sound_device_t *)list->data;
567 debug_log(" list idx[%d]: type[%17s], id[%02d], io_direction[%d], state[%d], name[%s]\n",
568 count++, device_node->type, device_node->id, device_node->io_direction,
569 device_node->state, device_node->name);
572 debug_log("======================== device list : end ============================\n");
577 int mm_sound_client_get_current_connected_device_list(int device_flags, mm_sound_device_list_t *device_list)
579 int ret = MM_ERROR_NONE;
583 debug_error("Device list NULL");
584 ret = MM_ERROR_COMMON_INVALID_ARGUMENT;
588 if ((ret = mm_sound_proxy_get_current_connected_device_list(device_flags, &device_list->list)) != MM_ERROR_NONE) {
589 debug_error("[Client] failed to get current connected device list with dbus, ret[0x%x]", ret);
592 if (!device_list->list) {
593 debug_error("Got device list null");
594 ret = MM_ERROR_SOUND_NO_DATA;
597 _mm_sound_client_device_list_dump(device_list->list);
604 static bool device_is_match_direction(int direction, int mask)
606 if (mask == DEVICE_IO_DIRECTION_FLAGS || mask == 0)
609 if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_IN_FLAG) && (direction & MM_SOUND_DEVICE_IO_DIRECTION_IN))
611 if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_OUT_FLAG) && (direction & MM_SOUND_DEVICE_IO_DIRECTION_OUT))
613 if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_BOTH_FLAG) && (direction == MM_SOUND_DEVICE_IO_DIRECTION_BOTH))
619 static bool device_is_match_state(int state, int mask)
621 if (mask == DEVICE_STATE_FLAGS || mask == 0)
624 if ((mask & MM_SOUND_DEVICE_STATE_DEACTIVATED_FLAG) && (state == MM_SOUND_DEVICE_STATE_DEACTIVATED))
626 if ((mask & MM_SOUND_DEVICE_STATE_ACTIVATED_FLAG) && (state == MM_SOUND_DEVICE_STATE_ACTIVATED))
632 static bool device_is_match_type(const char *type, int mask)
635 const char *builtin_prefix = "builtin";
637 if (mask == DEVICE_TYPE_FLAGS || mask == 0)
640 is_builtin = !strncmp(type, builtin_prefix, strlen(builtin_prefix));
642 if ((mask & MM_SOUND_DEVICE_TYPE_INTERNAL_FLAG) && (is_builtin))
644 if ((mask & MM_SOUND_DEVICE_TYPE_EXTERNAL_FLAG) && (!is_builtin))
650 static bool device_is_match_with_mask(const char *type, int direction, int state, int mask)
652 if (mask == DEVICE_ALL_FLAG)
655 return (device_is_match_direction(direction, mask & DEVICE_IO_DIRECTION_FLAGS) &&
656 device_is_match_state(state, mask & DEVICE_STATE_FLAGS) &&
657 device_is_match_type(type, mask & DEVICE_TYPE_FLAGS));
660 static int _fill_sound_device(mm_sound_device_t *device_h, int device_id, const char *device_type,
661 int direction, int state, const char *name, int *stream_id, int stream_num)
665 if (stream_num > 0 && stream_id == NULL) {
666 debug_error("stream_num is %d, but stream_id is NULL", stream_num);
670 if (stream_num > MAX_STREAM_ON_DEVICE) {
671 debug_error("too many streams on this device");
675 device_h->id = device_id;
676 device_h->io_direction = direction;
677 device_h->state = state;
678 MMSOUND_STRNCPY(device_h->name, name, MAX_DEVICE_NAME_NUM);
679 MMSOUND_STRNCPY(device_h->type, device_type, MAX_DEVICE_TYPE_STR_LEN);
681 if (stream_num > 0) {
682 device_h->stream_num = stream_num;
683 debug_log("%d streams on this device", stream_num);
684 for (i = 0; i < stream_num; i++) {
685 debug_log(" stream_id : %d", stream_id[i]);
686 device_h->stream_id[i] = stream_id[i];
689 device_h->stream_num = 0;
690 debug_log("There is no stream on this device");
696 static void _mm_sound_device_connected_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
697 int state, const char *name, int *stream_id, int stream_num,
698 gboolean is_connected, void *userdata)
700 mm_sound_device_t device_h;
701 struct callback_data *cb_data = (struct callback_data*) userdata;
704 debug_log("[Device %s] id(%d) type(%s) direction(%d) state(%d) name(%s)",
705 is_connected ? "Connected" : "Disconnected", device_id, device_type, io_direction,
706 state, name, is_connected);
708 if (cb_data == NULL) {
709 debug_warning("device connected changed callback data null");
713 device_flags = (int) cb_data->extra_data;
714 if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
717 if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name, stream_id, stream_num) < 0) {
718 debug_error("Failed to fill sound device");
722 ((mm_sound_device_connected_cb)(cb_data->user_cb))(&device_h, is_connected, cb_data->user_data);
725 int mm_sound_client_add_device_connected_callback(int device_flags, mm_sound_device_connected_cb func,
726 void* userdata, unsigned int *subs_id)
728 int ret = MM_ERROR_NONE;
729 struct callback_data *cb_data = NULL;
733 GET_CB_DATA(cb_data, func, userdata, (void*) device_flags);
735 ret = mm_sound_proxy_add_device_connected_callback(device_flags,
736 _mm_sound_device_connected_callback_wrapper_func,
737 cb_data, g_free, subs_id);
738 if (ret == MM_ERROR_NONE)
739 g_need_emergent_exit = TRUE;
745 int mm_sound_client_remove_device_connected_callback(unsigned int subs_id)
747 int ret = MM_ERROR_NONE;
750 ret = mm_sound_proxy_remove_device_connected_callback(subs_id);
756 static void _mm_sound_device_info_changed_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
757 int state, const char *name, int *stream_id, int stream_num,
758 int changed_device_info_type, void *userdata)
760 mm_sound_device_t device_h;
761 struct callback_data *cb_data = (struct callback_data*) userdata;
764 debug_log("[Device Info Changed] id(%d) type(%s) direction(%d) state(%d) name(%s) changed_info_type(%d)",
765 device_id, device_type, io_direction, state, name, changed_device_info_type);
767 if (cb_data == NULL) {
768 debug_warning("device info changed callback data null");
772 device_flags = (int) cb_data->extra_data;
773 if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
776 if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name, stream_id, stream_num) < 0) {
777 debug_error("Failed to fill sound device");
781 ((mm_sound_device_info_changed_cb)(cb_data->user_cb))(&device_h, changed_device_info_type, cb_data->user_data);
784 int mm_sound_client_add_device_info_changed_callback(int device_flags, mm_sound_device_info_changed_cb func,
785 void *userdata, unsigned int *subs_id)
787 int ret = MM_ERROR_NONE;
788 struct callback_data *cb_data = (struct callback_data*) userdata;
792 GET_CB_DATA(cb_data, func, userdata, (void *) device_flags);
794 ret = mm_sound_proxy_add_device_info_changed_callback(device_flags,
795 _mm_sound_device_info_changed_callback_wrapper_func,
796 cb_data, g_free, subs_id);
802 int mm_sound_client_remove_device_info_changed_callback(unsigned int subs_id)
804 int ret = MM_ERROR_NONE;
807 ret = mm_sound_proxy_remove_device_info_changed_callback(subs_id);
814 static void _mm_sound_device_state_changed_callback_wrapper_func(int device_id, const char *device_type,
815 int io_direction, int state, const char *name,
816 int *stream_id, int stream_num, void *userdata)
818 mm_sound_device_t device_h;
819 struct callback_data *cb_data = (struct callback_data*) userdata;
822 debug_log("[Device State Changed] id(%d) type(%s) direction(%d) state(%d) name(%s)",
823 device_id, device_type, io_direction, state, name);
825 if (cb_data == NULL) {
826 debug_warning("device state changed callback data null");
830 device_flags = (int) cb_data->extra_data;
832 if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
835 if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name, stream_id, stream_num) < 0) {
836 debug_error("Failed to fill sound device");
840 ((mm_sound_device_state_changed_cb)(cb_data->user_cb))(&device_h, state, cb_data->user_data);
843 int mm_sound_client_add_device_state_changed_callback(int device_flags, mm_sound_device_state_changed_cb func,
844 void *userdata, unsigned int *id)
846 int ret = MM_ERROR_NONE;
847 struct callback_data *cb_data = (struct callback_data*) userdata;
851 GET_CB_DATA(cb_data, func, userdata, (void *) device_flags);
853 ret = mm_sound_proxy_add_device_state_changed_callback(device_flags,
854 _mm_sound_device_state_changed_callback_wrapper_func,
855 cb_data, g_free, id);
861 int mm_sound_client_remove_device_state_changed_callback(unsigned int id)
863 int ret = MM_ERROR_NONE;
866 ret = mm_sound_proxy_remove_device_state_changed_callback(id);
872 int mm_sound_client_is_stream_on_device(int stream_id, int device_id, bool *is_on)
874 int ret = MM_ERROR_NONE;
878 debug_error("Invalid Parameter");
879 ret = MM_ERROR_COMMON_INVALID_ARGUMENT;
883 if ((ret = mm_sound_proxy_is_stream_on_device(stream_id, device_id, is_on)) != MM_ERROR_NONE) {
884 debug_error("[Client] failed to query is stream on device, ret[0x%x]", ret);
893 int __convert_volume_type_to_str(int volume_type, char **volume_type_str)
895 int ret = MM_ERROR_NONE;
897 if (!volume_type_str) {
898 return MM_ERROR_COMMON_INVALID_ARGUMENT;
901 switch (volume_type) {
902 case VOLUME_TYPE_SYSTEM:
903 *volume_type_str = "system";
905 case VOLUME_TYPE_NOTIFICATION:
906 *volume_type_str = "notification";
908 case VOLUME_TYPE_ALARM:
909 *volume_type_str = "alarm";
911 case VOLUME_TYPE_RINGTONE:
912 *volume_type_str = "ringtone";
914 case VOLUME_TYPE_MEDIA:
915 *volume_type_str = "media";
917 case VOLUME_TYPE_CALL:
918 *volume_type_str = "call";
920 case VOLUME_TYPE_VOIP:
921 *volume_type_str = "voip";
923 case VOLUME_TYPE_VOICE:
924 *volume_type_str = "voice";
927 if (!strncmp(*volume_type_str,"", VOLUME_TYPE_LEN)) {
928 debug_error("could not find the volume_type[%d] in this switch case statement", volume_type);
929 ret = MM_ERROR_SOUND_INTERNAL;
931 debug_log("volume_type[%s]", *volume_type_str);
936 static int __convert_volume_type_to_int(const char *volume_type_str, volume_type_t *volume_type)
938 int ret = MM_ERROR_NONE;
940 if (!volume_type || !volume_type_str) {
941 return MM_ERROR_COMMON_INVALID_ARGUMENT;
944 if (!strncmp(volume_type_str, "system", VOLUME_TYPE_LEN)) {
945 *volume_type = VOLUME_TYPE_SYSTEM;
946 } else if (!strncmp(volume_type_str, "notification", VOLUME_TYPE_LEN)) {
947 *volume_type = VOLUME_TYPE_NOTIFICATION;
948 } else if (!strncmp(volume_type_str, "alarm", VOLUME_TYPE_LEN)) {
949 *volume_type = VOLUME_TYPE_ALARM;
950 } else if (!strncmp(volume_type_str, "ringtone", VOLUME_TYPE_LEN)) {
951 *volume_type = VOLUME_TYPE_RINGTONE;
952 } else if (!strncmp(volume_type_str, "media", VOLUME_TYPE_LEN)) {
953 *volume_type = VOLUME_TYPE_MEDIA;
954 } else if (!strncmp(volume_type_str, "call", VOLUME_TYPE_LEN)) {
955 *volume_type = VOLUME_TYPE_CALL;
956 } else if (!strncmp(volume_type_str, "voip", VOLUME_TYPE_LEN)) {
957 *volume_type = VOLUME_TYPE_VOIP;
958 } else if (!strncmp(volume_type_str, "voice", VOLUME_TYPE_LEN)) {
959 *volume_type = VOLUME_TYPE_VOICE;
961 debug_log("Invalid volume type : [%s]", volume_type_str);
962 ret = MM_ERROR_SOUND_INTERNAL;
968 int mm_sound_client_set_volume_by_type(const int volume_type, const unsigned int volume_level)
970 int ret = MM_ERROR_NONE;
971 char *type_str = NULL;
974 if ((ret = __convert_volume_type_to_str(volume_type, &type_str)) != MM_ERROR_NONE) {
975 debug_error("volume type convert failed");
979 ret = mm_sound_proxy_set_volume_by_type(type_str, volume_level);
986 static void _mm_sound_volume_changed_callback_wrapper_func(const char *direction, const char *volume_type_str,
987 int volume_level, void *userdata)
989 volume_type_t volume_type = 0;
990 struct callback_data *cb_data = (struct callback_data *) userdata;
992 debug_log("[Wrapper CB][Volume Changed] direction : %s, volume_type : %s, volume_level : %d",
993 direction, volume_type_str, volume_level);
995 if (cb_data == NULL) {
996 debug_warning("volume changed callback data null");
1000 if (__convert_volume_type_to_int(volume_type_str, &volume_type) != MM_ERROR_NONE) {
1001 debug_error("volume type convert failed");
1004 debug_log("Call volume changed user cb, direction : %s, vol_type : %s(%d), level : %u",
1005 direction, volume_type_str, volume_type, volume_level);
1006 ((mm_sound_volume_changed_cb)(cb_data->user_cb))(volume_type, volume_level, cb_data->user_data);
1009 int mm_sound_client_add_volume_changed_callback(mm_sound_volume_changed_cb func, void* userdata, unsigned int *subs_id)
1011 int ret = MM_ERROR_NONE;
1012 struct callback_data *cb_data = NULL;
1016 GET_CB_DATA(cb_data, func, userdata, NULL);
1018 ret = mm_sound_proxy_add_volume_changed_callback(_mm_sound_volume_changed_callback_wrapper_func, cb_data, g_free, subs_id);
1025 int mm_sound_client_remove_volume_changed_callback(unsigned int subs_id)
1027 int ret = MM_ERROR_NONE;
1030 ret = mm_sound_proxy_remove_volume_changed_callback(subs_id);
1037 int mm_sound_client_set_session_interrupt_callback(mm_sound_focus_session_interrupt_cb callback, void* user_data)
1039 int ret = MM_ERROR_NONE;
1044 return MM_ERROR_INVALID_ARGUMENT;
1046 g_focus_session_interrupt_info.user_cb = callback;
1047 g_focus_session_interrupt_info.user_data = user_data;
1053 int mm_sound_client_unset_session_interrupt_callback(void)
1055 int ret = MM_ERROR_NONE;
1059 if (!g_focus_session_interrupt_info.user_cb) {
1060 debug_error("no callback to unset");
1061 return MM_ERROR_SOUND_INTERNAL;
1064 g_focus_session_interrupt_info.user_cb = NULL;
1065 g_focus_session_interrupt_info.user_data = NULL;
1071 static gpointer _focus_thread_func(gpointer data)
1073 unsigned int thread_id = (unsigned int)pthread_self();
1074 GMainLoop *focus_loop = (GMainLoop*)data;
1076 debug_warning(">>> thread func : thread id(%u), mainloop[%p]", thread_id, focus_loop);
1078 g_main_loop_run(focus_loop);
1079 debug_warning("<<< quit thread func : thread id(%u), mainloop[%p]", thread_id, focus_loop);
1084 static gboolean _focus_fd_prepare(GSource *source, gint *timeout)
1087 debug_warning("[ PREPARE : %p, (%p, %d)", source, timeout, timeout? *timeout : -1);
1092 static gboolean _focus_fd_check(GSource * source)
1094 FocusSource* fsource = (FocusSource *)source;
1097 debug_error("GSource is null");
1101 debug_warning("CHECK : %p, 0x%x ]", source, fsource->pollfd.revents);
1103 if (fsource->poll_fd.revents & (POLLIN | POLLPRI))
1109 static gboolean _focus_fd_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
1111 debug_warning("*** DISPATCH : %p, (%p, %p)", source, callback, user_data);
1112 return callback(user_data);
1115 static void _focus_fd_finalize(GSource *source)
1117 debug_warning("### FINALIZE : %p", source);
1120 static int _focus_find_index_by_handle(int handle)
1123 for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1124 if (g_focus_sound_handle[i].focus_callback && handle == g_focus_sound_handle[i].handle) {
1125 /* debug_msg("found index(%d) for handle(%d)", i, handle);*/
1126 return (handle == FOCUS_HANDLE_INIT_VAL)? -1 : i;
1132 static int _focus_watch_find_index_by_handle(int handle)
1135 for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1136 if (g_focus_sound_handle[i].watch_callback && handle == g_focus_sound_handle[i].handle) {
1137 /* debug_msg("found index(%d) for watch handle(%d)", i, handle);*/
1138 return (handle == FOCUS_HANDLE_INIT_VAL)? -1 : i;
1144 static gboolean _focus_callback_handler(gpointer user_data)
1146 focus_sound_info_t *focus_handle = (focus_sound_info_t *)user_data;
1150 focus_cb_data_lib cb_data;
1152 debug_log(">>> _focus_callback_handler(), this thread id(%u)\n", (unsigned int)pthread_self());
1154 if (!focus_handle) {
1155 debug_error("focus_handle is null");
1156 return G_SOURCE_CONTINUE;
1158 poll_fd = &focus_handle->fsrc->poll_fd;
1159 debug_log("focus_handle(%p), poll_fd(%p)", focus_handle, poll_fd);
1161 memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1163 if (poll_fd->revents & (POLLIN | POLLPRI)) {
1164 int changed_state = -1;
1166 count = read(poll_fd->fd, &cb_data, sizeof(cb_data));
1168 char str_error[256];
1169 strerror_r(errno, str_error, sizeof(str_error));
1170 debug_error("GpollFD read fail, errno=%d(%s)",errno, str_error);
1171 return G_SOURCE_CONTINUE;
1173 changed_state = cb_data.state;
1175 g_mutex_lock(&focus_handle->focus_lock);
1177 tid = focus_handle->focus_tid;
1179 if (changed_state != -1) {
1180 debug_msg("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)",
1181 tid, cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type);
1182 if (focus_handle->focus_callback == NULL) {
1183 debug_error("focus callback is null..");
1184 g_mutex_unlock(&focus_handle->focus_lock);
1185 return G_SOURCE_CONTINUE;
1187 debug_msg("[CALLBACK(%p) START]", focus_handle->focus_callback);
1188 (focus_handle->focus_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type,
1189 cb_data.option, cb_data.ext_info, focus_handle->user_data);
1190 debug_msg("[CALLBACK END]");
1191 if (g_focus_session_interrupt_info.user_cb) {
1192 debug_msg("sending session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
1193 g_focus_session_interrupt_info.user_cb(cb_data.state, cb_data.stream_type, false,
1194 g_focus_session_interrupt_info.user_data);
1197 #ifdef CONFIG_ENABLE_RETCB
1201 unsigned int buf = 0;
1202 char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", focus_handle->focus_tid, cb_data.handle);
1203 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1205 char str_error[256];
1206 strerror_r(errno, str_error, sizeof(str_error));
1207 debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n",
1208 tid, tmpfd, filename2, errno, str_error);
1210 g_mutex_unlock(&focus_handle->focus_lock);
1211 return G_SOURCE_CONTINUE;
1213 /* buf contains data as below,
1214 * |<--12bits--><--4bits (reacquisition)--><--16bits (handle)-->| */
1215 buf = (unsigned int)((0x0000ffff & cb_data.handle) | (focus_handle->auto_reacquire << 16));
1216 rett = write(tmpfd, &buf, sizeof(buf));
1219 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
1224 g_mutex_unlock(&focus_handle->focus_lock);
1228 return G_SOURCE_CONTINUE;
1231 static gboolean _focus_watch_callback_handler(gpointer user_data)
1233 focus_sound_info_t *focus_handle = (focus_sound_info_t *)user_data;
1237 focus_cb_data_lib cb_data;
1239 debug_log(">>> _focus_watch_callback_handler(), this thread id(%u)\n", (unsigned int)pthread_self());
1241 if (!focus_handle) {
1242 debug_error("focus_handle is null");
1243 return G_SOURCE_CONTINUE;
1245 poll_fd = &focus_handle->fsrc->poll_fd;
1246 debug_log("focus_handle(%p), poll_fd(%p)", focus_handle, poll_fd);
1248 memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1250 if (poll_fd->revents & (POLLIN | POLLPRI)) {
1251 count = read(poll_fd->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 if (!focus_handle->is_used) {
1260 debug_warning("unsetting watch calllback has been already requested");
1261 goto SKIP_CB_AND_RET;
1264 g_mutex_lock(&focus_handle->focus_lock);
1266 tid = focus_handle->focus_tid;
1268 debug_msg("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)",
1269 tid, cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type);
1271 if (focus_handle->watch_callback == NULL) {
1272 debug_warning("watch callback is null..");
1273 goto SKIP_CB_AND_RET;
1275 if (focus_handle->unset_watch_callback_requested == true) {
1276 debug_warning("unset_watch_callback_requested..");
1277 goto SKIP_CB_AND_RET;
1280 debug_msg("[CALLBACK(%p) START]", focus_handle->watch_callback);
1281 (focus_handle->watch_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type,
1282 cb_data.ext_info, focus_handle->user_data);
1283 debug_msg("[CALLBACK END]");
1284 if (g_focus_session_interrupt_info.user_cb) {
1285 debug_msg("sending session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
1286 (g_focus_session_interrupt_info.user_cb)(cb_data.state, cb_data.stream_type, true,
1287 g_focus_session_interrupt_info.user_data);
1291 #ifdef CONFIG_ENABLE_RETCB
1296 char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr", focus_handle->focus_tid, cb_data.handle);
1297 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1299 char str_error[256];
1300 strerror_r(errno, str_error, sizeof(str_error));
1301 debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n",
1302 tid, tmpfd, filename2, errno, str_error);
1304 g_mutex_unlock(&focus_handle->focus_lock);
1305 return G_SOURCE_CONTINUE;
1307 buf = cb_data.handle;
1308 rett = write(tmpfd, &buf, sizeof(buf));
1311 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
1316 if (focus_handle->is_used) {
1317 debug_msg("unlock focus_lock = %p", &focus_handle->focus_lock);
1318 g_mutex_unlock(&focus_handle->focus_lock);
1323 return G_SOURCE_CONTINUE;
1326 static void _focus_open_callback(int index, bool is_for_watching)
1333 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1334 debug_error("Invalid focus handle index [%d]", index);
1338 if (is_for_watching) {
1339 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch",
1340 g_focus_sound_handle[index].focus_tid,
1341 g_focus_sound_handle[index].handle);
1343 filename = g_strdup_printf("/tmp/FOCUS.%d.%d",
1344 g_focus_sound_handle[index].focus_tid,
1345 g_focus_sound_handle[index].handle);
1347 pre_mask = umask(0);
1348 if (mknod(filename, S_IFIFO|0666, 0)) {
1349 debug_error("mknod() failure, errno(%d)", errno);
1352 g_focus_sound_handle[index].focus_fd = open(filename, O_RDWR|O_NONBLOCK);
1353 if (g_focus_sound_handle[index].focus_fd == -1) {
1354 debug_error("Open fail : index(%d), file open error(%d)", index, errno);
1356 debug_log("Open success : index(%d), filename(%s), fd(%d)",
1357 index, filename, g_focus_sound_handle[index].focus_fd);
1362 #ifdef CONFIG_ENABLE_RETCB
1365 if (is_for_watching) {
1366 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr",
1367 g_focus_sound_handle[index].focus_tid,
1368 g_focus_sound_handle[index].handle);
1370 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr",
1371 g_focus_sound_handle[index].focus_tid,
1372 g_focus_sound_handle[index].handle);
1374 pre_mask = umask(0);
1375 if (mknod(filename2, S_IFIFO | 0666, 0)) {
1376 debug_error("mknod() failure, errno(%d)", errno);
1386 void _focus_close_callback(int index, bool is_for_watching)
1388 char *filename = NULL;
1392 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1393 debug_error("Invalid focus handle index [%d]", index);
1397 if (g_focus_sound_handle[index].focus_fd < 0) {
1398 debug_error("Close fail : index(%d)", index);
1400 close(g_focus_sound_handle[index].focus_fd);
1401 debug_log("Close Success : index(%d)", index);
1404 if (is_for_watching) {
1405 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch",
1406 g_focus_sound_handle[index].focus_tid,
1407 g_focus_sound_handle[index].handle);
1409 filename = g_strdup_printf("/tmp/FOCUS.%d.%d",
1410 g_focus_sound_handle[index].focus_tid,
1411 g_focus_sound_handle[index].handle);
1413 if (remove(filename)) {
1414 debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)",
1420 #ifdef CONFIG_ENABLE_RETCB
1423 if (is_for_watching) {
1424 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr",
1425 g_focus_sound_handle[index].focus_tid,
1426 g_focus_sound_handle[index].handle);
1428 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr",
1429 g_focus_sound_handle[index].focus_tid,
1430 g_focus_sound_handle[index].handle);
1432 if (remove(filename2)) {
1433 debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)",
1444 static GSourceFuncs event_funcs = {
1445 .prepare = _focus_fd_prepare,
1446 .check = _focus_fd_check,
1447 .dispatch = _focus_fd_dispatch,
1448 .finalize = _focus_fd_finalize,
1451 static bool _focus_add_sound_callback(int index, focus_callback_handler_t focus_cb_handler)
1453 FocusSource *fsrc = NULL;
1454 GSource *src = NULL;
1459 g_mutex_init(&g_focus_sound_handle[index].focus_lock);
1461 src = g_source_new(&event_funcs, sizeof(FocusSource));
1463 debug_error("failed to g_source_new for focus source");
1467 fsrc = (FocusSource*) src;
1469 fsrc->poll_fd.fd = g_focus_sound_handle[index].focus_fd;
1470 fsrc->poll_fd.events = (gushort)(POLLIN | POLLPRI);
1471 g_source_add_poll(src, &fsrc->poll_fd);
1473 g_source_set_callback(src, focus_cb_handler, (gpointer)&g_focus_sound_handle[index], NULL);
1475 debug_warning("fsrc(%p), src_funcs(%p), pollfd(%p), fd(%d)",
1476 fsrc, &event_funcs, &fsrc->poll_fd, fsrc->poll_fd.fd);
1478 fsrc_id = g_source_attach(src, g_main_loop_get_context(g_focus_sound_handle[index].focus_loop));
1480 debug_error("failed to attach the source to context");
1483 g_source_unref(src);
1485 g_focus_sound_handle[index].fsrc = fsrc;
1492 g_source_unref(src);
1497 static bool _focus_remove_sound_callback(int index)
1499 focus_sound_info_t *h = NULL;
1503 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1504 debug_error("Invalid focus handle index [%d]", index);
1508 h = &g_focus_sound_handle[index];
1510 g_source_destroy((GSource *)h->fsrc);
1514 h->focus_callback = NULL;
1515 h->watch_callback = NULL;
1517 g_mutex_clear(&h->focus_lock);
1524 static void _focus_add_callback(int index, bool is_for_watching)
1528 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1529 debug_error("Invalid focus handle index [%d]", index);
1533 if (!is_for_watching) {
1534 if (!_focus_add_sound_callback(index, _focus_callback_handler))
1535 debug_error("failed to _focus_add_sound_callback(%p)", _focus_callback_handler);
1536 } else { // need to check if it's necessary
1537 if (!_focus_add_sound_callback(index, _focus_watch_callback_handler))
1538 debug_error("failed to _focus_add_sound_callback(%p)", _focus_watch_callback_handler);
1543 static void _focus_remove_callback(int index)
1546 if (!_focus_remove_sound_callback(index))
1547 debug_error("failed to __focus_remove_sound_callback()");
1551 static void _focus_init_callback(int index, bool is_for_watching)
1554 _focus_open_callback(index, is_for_watching);
1555 _focus_add_callback(index, is_for_watching);
1559 static void _focus_deinit_callback(int index, bool is_for_watching)
1562 _focus_remove_callback(index);
1563 _focus_close_callback(index, is_for_watching);
1567 #define INTERVAL_MS 20
1568 static int _focus_loop_is_running_timed_wait(GMainLoop *focus_loop, int timeout_ms)
1570 int reduced_time_ms = timeout_ms;
1571 if (!focus_loop || timeout_ms < 0) {
1572 debug_error("invalid argument, focus_loop(%p), timeout_ms(%d)", focus_loop, timeout_ms);
1573 return MM_ERROR_INVALID_ARGUMENT;
1577 if (g_main_loop_is_running(focus_loop))
1578 return MM_ERROR_NONE;
1580 usleep(INTERVAL_MS * 1000);
1581 if (reduced_time_ms < timeout_ms)
1582 debug_warning("reduced_time_ms(%d)", reduced_time_ms);
1583 } while ((reduced_time_ms -= INTERVAL_MS) >= 0);
1585 debug_error("focus_loop is not running for timeout_ms(%d), focus_loop(%p) ", timeout_ms, focus_loop);
1587 return MM_ERROR_SOUND_INTERNAL;
1590 #define LOOP_RUNNING_WAIT_TIME_MS 200
1591 static int _focus_init_context(int index)
1593 int ret = MM_ERROR_NONE;
1594 GMainContext *focus_context;
1598 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1599 debug_error("index(%d) is not valid", index);
1600 return MM_ERROR_INVALID_ARGUMENT;
1603 focus_context = g_main_context_new();
1604 g_focus_sound_handle[index].focus_loop = g_main_loop_new(focus_context, FALSE);
1605 g_main_context_unref(focus_context);
1606 if (g_focus_sound_handle[index].focus_loop == NULL) {
1607 debug_error("could not create mainloop..");
1611 g_focus_sound_handle[index].focus_cb_thread = g_thread_new("focus-cb-thread",
1613 g_focus_sound_handle[index].focus_loop);
1614 if (g_focus_sound_handle[index].focus_cb_thread == NULL) {
1615 debug_error("could not create thread..");
1619 debug_warning("focus cb thread[%p] with mainloop[%p] is created for index(%d)",
1620 g_focus_sound_handle[index].focus_cb_thread, g_focus_sound_handle[index].focus_loop, index);
1622 if ((ret = _focus_loop_is_running_timed_wait(g_focus_sound_handle[index].focus_loop, LOOP_RUNNING_WAIT_TIME_MS))) {
1623 debug_error("failed to _focus_loop_is_running_timed_wait(), ret[0x%x]", ret);
1629 return MM_ERROR_NONE;
1632 if (g_focus_sound_handle[index].focus_loop) {
1633 g_main_loop_unref(g_focus_sound_handle[index].focus_loop);
1634 g_focus_sound_handle[index].focus_loop = NULL;
1636 return MM_ERROR_SOUND_INTERNAL;
1639 static void _focus_deinit_context(int index)
1643 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1644 debug_error("index(%d) is not valid", index);
1648 if (!g_focus_sound_handle[index].focus_loop || !g_focus_sound_handle[index].focus_cb_thread) {
1649 debug_error("focus_loop[%p] or focus_cb_thread[%p] is null",
1650 g_focus_sound_handle[index].focus_loop, g_focus_sound_handle[index].focus_cb_thread);
1654 g_main_loop_quit(g_focus_sound_handle[index].focus_loop);
1655 g_thread_join(g_focus_sound_handle[index].focus_cb_thread);
1656 debug_warning("after thread join, thread[%p], mainloop[%p] for index(%d)",
1657 g_focus_sound_handle[index].focus_cb_thread, g_focus_sound_handle[index].focus_loop, index);
1658 g_main_loop_unref(g_focus_sound_handle[index].focus_loop);
1659 g_focus_sound_handle[index].focus_loop = NULL;
1660 g_focus_sound_handle[index].focus_cb_thread = NULL;
1665 int mm_sound_client_get_unique_id(int *id)
1667 int ret = MM_ERROR_NONE;
1672 ret = MM_ERROR_INVALID_ARGUMENT;
1674 ret = mm_sound_proxy_get_unique_id(id);
1681 int mm_sound_client_is_focus_cb_thread(GThread *mine, bool *result)
1683 int ret = MM_ERROR_NONE;
1686 if (!mine || !result)
1687 ret = MM_ERROR_INVALID_ARGUMENT;
1690 for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1691 if (!g_focus_sound_handle[i].is_used)
1693 if (g_focus_sound_handle[i].focus_cb_thread == mine) {
1703 int mm_sound_client_register_focus(int id, int pid, const char *stream_type,
1704 mm_sound_focus_changed_cb callback,
1705 bool is_for_session, void* user_data)
1707 int ret = MM_ERROR_NONE;
1712 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1716 for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
1717 if (g_focus_sound_handle[index].is_used == false) {
1718 g_focus_sound_handle[index].is_used = true;
1723 g_focus_sound_handle[index].focus_tid = instance;
1724 g_focus_sound_handle[index].handle = id;
1725 g_focus_sound_handle[index].focus_callback = callback;
1726 g_focus_sound_handle[index].user_data = user_data;
1727 g_focus_sound_handle[index].is_for_session = is_for_session;
1728 g_focus_sound_handle[index].auto_reacquire = true;
1730 ret = mm_sound_proxy_register_focus(id, pid, stream_type, callback, is_for_session, user_data);
1731 if (ret == MM_ERROR_NONE) {
1732 debug_msg("[Client] Success to register focus\n");
1733 g_need_emergent_exit = TRUE;
1734 if (_focus_init_context(index)) {
1735 ret = MM_ERROR_SOUND_INTERNAL;
1739 debug_error("[Client] Error occurred : 0x%x \n",ret);
1743 _focus_init_callback(index, false);
1747 g_focus_sound_handle[index].is_used = false;
1750 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1756 int mm_sound_client_unregister_focus(int id)
1758 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;
1771 instance = g_focus_sound_handle[index].focus_tid;
1773 if (!g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1774 debug_warning("maybe focus_callback is being called, try one more time..");
1775 usleep(2500000); // 2.5 sec
1776 if (g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1777 debug_msg("finally got focus_lock");
1781 ret = mm_sound_proxy_unregister_focus(instance, id, g_focus_sound_handle[index].is_for_session);
1782 if (ret == MM_ERROR_NONE)
1783 debug_msg("[Client] Success to unregister focus\n");
1785 debug_error("[Client] Error occurred : 0x%x \n",ret);
1787 g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
1789 _focus_deinit_callback(index, false);
1790 g_focus_sound_handle[index].focus_fd = 0;
1791 g_focus_sound_handle[index].focus_tid = 0;
1792 g_focus_sound_handle[index].handle = 0;
1793 g_focus_sound_handle[index].is_used = false;
1794 _focus_deinit_context(index);
1798 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1803 int mm_sound_client_set_focus_reacquisition(int id, bool reacquisition, bool is_for_session)
1805 int ret = MM_ERROR_NONE;
1811 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1813 index = _focus_find_index_by_handle(id);
1815 debug_error("Could not find index");
1816 ret = MM_ERROR_INVALID_ARGUMENT;
1819 instance = g_focus_sound_handle[index].focus_tid;
1821 ret = mm_sound_client_is_focus_cb_thread(g_thread_self(), &result);
1823 debug_error("[Client] mm_sound_client_is_focus_cb_thread failed");
1825 } else if (!result) {
1826 ret = mm_sound_proxy_set_focus_reacquisition(instance, id, reacquisition, is_for_session);
1827 if (ret == MM_ERROR_NONE) {
1828 debug_msg("[Client] Success to set focus reacquisition to [%d]\n", reacquisition);
1830 debug_error("[Client] Error occurred : 0x%x \n",ret);
1834 debug_warning("[Client] Inside the focus cb thread, set focus reacquisition to [%d]\n", reacquisition);
1837 g_focus_sound_handle[index].auto_reacquire = reacquisition;
1838 debug_msg("[Client] set focus reacquisition(%d) for id(%d)\n", reacquisition, id);
1841 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1846 int mm_sound_client_get_focus_reacquisition(int id, bool *reacquisition)
1848 int ret = MM_ERROR_NONE;
1853 if (!reacquisition) {
1854 debug_error("Invalid parameter");
1855 return MM_ERROR_INVALID_ARGUMENT;
1858 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1860 index = _focus_find_index_by_handle(id);
1862 debug_error("Could not find index");
1863 ret = MM_ERROR_INVALID_ARGUMENT;
1867 *reacquisition = g_focus_sound_handle[index].auto_reacquire;
1868 debug_msg("[Client] get focus reacquisition(%d) for id(%d)\n", *reacquisition, id);
1871 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1876 int mm_sound_client_get_acquired_focus_stream_type(int focus_type, char **stream_type, int *option, char **ext_info)
1878 int ret = MM_ERROR_NONE;
1879 char *ext_str = NULL;
1883 ret = mm_sound_proxy_get_acquired_focus_stream_type(focus_type, stream_type, option, &ext_str);
1884 if (ret == MM_ERROR_NONE) {
1885 debug_msg("[Client] Success to get stream type of acquired focus, stream_type(%s), ext_info(%s)\n",
1886 *stream_type, ext_str);
1887 *ext_info = strdup(ext_str);
1890 debug_error("[Client] Error occurred : 0x%x \n",ret);
1897 int mm_sound_client_acquire_focus(int id, mm_sound_focus_type_e type, int option, const char *ext_info)
1899 int ret = MM_ERROR_NONE;
1904 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1906 index = _focus_find_index_by_handle(id);
1908 debug_error("Could not find index");
1909 ret = MM_ERROR_INVALID_ARGUMENT;
1912 instance = g_focus_sound_handle[index].focus_tid;
1914 ret = mm_sound_proxy_acquire_focus(instance, id, type, option, ext_info, g_focus_sound_handle[index].is_for_session);
1915 if (ret == MM_ERROR_NONE)
1916 debug_msg("[Client] Success to acquire focus\n");
1918 debug_error("[Client] Error occurred : 0x%x \n",ret);
1921 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1926 int mm_sound_client_release_focus(int id, mm_sound_focus_type_e type, int option, const char *ext_info)
1928 int ret = MM_ERROR_NONE;
1933 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1935 index = _focus_find_index_by_handle(id);
1937 debug_error("Could not find index");
1938 ret = MM_ERROR_INVALID_ARGUMENT;
1941 instance = g_focus_sound_handle[index].focus_tid;
1943 ret = mm_sound_proxy_release_focus(instance, id, type, option, ext_info, g_focus_sound_handle[index].is_for_session);
1944 if (ret == MM_ERROR_NONE)
1945 debug_msg("[Client] Success to release focus\n");
1947 debug_error("[Client] Error occurred : 0x%x \n",ret);
1950 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1955 int mm_sound_client_update_stream_focus_status(int id, unsigned int status)
1957 int ret = MM_ERROR_NONE;
1960 if ((ret = mm_sound_proxy_update_stream_focus_status(id, status)) != MM_ERROR_NONE)
1961 debug_error("[Client] failed to update stream focus status, ret[0x%x]", ret);
1967 int mm_sound_client_set_focus_watch_callback(int pid, mm_sound_focus_type_e focus_type,
1968 mm_sound_focus_changed_watch_cb callback,
1969 bool is_for_session, void* user_data, int *id)
1971 int ret = MM_ERROR_NONE;
1978 return MM_ERROR_INVALID_ARGUMENT;
1980 //pthread_mutex_lock(&g_thread_mutex2);
1984 ret = mm_sound_proxy_get_unique_id(id);
1988 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1990 for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
1991 if (g_focus_sound_handle[index].is_used == false) {
1992 g_focus_sound_handle[index].is_used = true;
1997 g_focus_sound_handle[index].focus_tid = instance;
1998 g_focus_sound_handle[index].handle = *id;
1999 g_focus_sound_handle[index].watch_callback = callback;
2000 g_focus_sound_handle[index].user_data = user_data;
2001 g_focus_sound_handle[index].is_for_session = is_for_session;
2002 g_focus_sound_handle[index].unset_watch_callback_requested = false;
2004 ret = mm_sound_proxy_set_focus_watch_callback(pid, g_focus_sound_handle[index].handle, focus_type,
2005 callback, is_for_session, user_data);
2007 if (ret == MM_ERROR_NONE) {
2008 debug_msg("[Client] Success to watch focus");
2009 g_need_emergent_exit = TRUE;
2010 if (_focus_init_context(index)) {
2011 ret = MM_ERROR_SOUND_INTERNAL;
2015 debug_error("[Client] Error occurred : 0x%x",ret);
2019 _focus_init_callback(index, true);
2023 g_focus_sound_handle[index].is_used = false;
2026 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2031 int mm_sound_client_request_unset_focus_watch_callback(int id)
2033 int ret = MM_ERROR_NONE;
2037 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2039 index = _focus_watch_find_index_by_handle(id);
2041 debug_error("Could not find index");
2042 ret = MM_ERROR_INVALID_ARGUMENT;
2045 g_focus_sound_handle[index].unset_watch_callback_requested = true;
2048 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2053 int mm_sound_client_unset_focus_watch_callback(int id)
2055 int ret = MM_ERROR_NONE;
2059 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2061 index = _focus_watch_find_index_by_handle(id);
2063 debug_error("Could not find index");
2064 ret = MM_ERROR_INVALID_ARGUMENT;
2068 g_mutex_lock(&g_focus_sound_handle[index].focus_lock);
2070 g_focus_sound_handle[index].is_used = false;
2072 ret = mm_sound_proxy_unset_focus_watch_callback(g_focus_sound_handle[index].focus_tid,
2073 g_focus_sound_handle[index].handle,
2074 g_focus_sound_handle[index].is_for_session);
2076 if (ret == MM_ERROR_NONE)
2077 debug_msg("[Client] Success to unwatch focus\n");
2079 debug_error("[Client] Error occurred : 0x%x \n",ret);
2082 g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
2084 _focus_deinit_callback(index, true);
2085 g_focus_sound_handle[index].focus_fd = 0;
2086 g_focus_sound_handle[index].focus_tid = 0;
2087 g_focus_sound_handle[index].handle = 0;
2088 _focus_deinit_context(index);
2091 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2096 static gboolean _idle_event_callback(void *data)
2098 focus_idle_event_t *idle_event_data = (focus_idle_event_t*)data;
2099 int ret = MM_ERROR_NONE;
2102 debug_error("data is null");
2106 debug_msg("idle_event_data(%p): type(%d), data(%d)",
2107 idle_event_data, idle_event_data->type, idle_event_data->data);
2109 switch (idle_event_data->type) {
2110 case IDLE_EVENT_TYPE_UNSET_FOCUS_WATCH_CB:
2111 if ((ret = mm_sound_client_unset_focus_watch_callback(idle_event_data->data)))
2112 debug_error("Could not unset focus watch callback, id(%d), ret = %x\n", idle_event_data->data, ret);
2114 case IDLE_EVENT_TYPE_UNREGISTER_FOCUS:
2115 if ((ret = mm_sound_client_unregister_focus(idle_event_data->data)))
2116 debug_error("Could not unregister focus, id(%d), ret = %x\n", idle_event_data->data, ret);
2119 debug_warning("invalid type(%d)", idle_event_data->type);
2123 g_free(idle_event_data);
2125 g_idle_event_src = 0;
2127 MMSOUND_LEAVE_CRITICAL_SECTION(&g_event_mutex);
2132 int mm_sound_client_execute_focus_func_in_main_context(focus_idle_event_type_e type, int data)
2134 focus_idle_event_t *idle_event_data = NULL;
2136 if (IDLE_EVENT_TYPE_UNSET_FOCUS_WATCH_CB > type || IDLE_EVENT_TYPE_MAX < type)
2137 return MM_ERROR_INVALID_ARGUMENT;
2139 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_event_mutex, MM_ERROR_SOUND_INTERNAL);
2141 idle_event_data = g_new0(focus_idle_event_t, 1);
2142 idle_event_data->type = type;
2143 idle_event_data->data = data;
2145 g_idle_event_src = g_idle_add_full(G_PRIORITY_HIGH,
2146 (GSourceFunc)_idle_event_callback,
2147 (gpointer)idle_event_data,
2150 return MM_ERROR_NONE;
2155 int mm_sound_client_add_test_callback(mm_sound_test_cb func, void* user_data, unsigned int *subs_id)
2157 int ret = MM_ERROR_NONE;
2161 ret = mm_sound_proxy_add_test_callback(func, user_data, g_free, subs_id);
2167 int mm_sound_client_remove_test_callback(unsigned int subs_id)
2169 int ret = MM_ERROR_NONE;
2172 ret = mm_sound_proxy_remove_test_callback(subs_id);
2179 int mm_sound_client_test(int a, int b, int* getv)
2181 int ret = MM_ERROR_NONE;
2185 ret = mm_sound_proxy_test(a, b, getv);
2186 debug_log("%d * %d -> result : %d", a, b, *getv);