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 memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1155 if (!focus_handle) {
1156 debug_error("focus_handle is null");
1157 return G_SOURCE_CONTINUE;
1159 if (!(poll_fd = &focus_handle->fsrc->poll_fd)) {
1160 debug_error("poll_fd is null");
1161 return G_SOURCE_CONTINUE;
1163 debug_log("focus_handle(%p), poll_fd(%p)", focus_handle, poll_fd);
1165 if (poll_fd->revents & (POLLIN | POLLPRI)) {
1166 int changed_state = -1;
1168 count = read(poll_fd->fd, &cb_data, sizeof(cb_data));
1170 char str_error[256];
1171 strerror_r(errno, str_error, sizeof(str_error));
1172 debug_error("GpollFD read fail, errno=%d(%s)",errno, str_error);
1173 return G_SOURCE_CONTINUE;
1175 changed_state = cb_data.state;
1177 g_mutex_lock(&focus_handle->focus_lock);
1179 tid = focus_handle->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 (focus_handle->focus_callback == NULL) {
1185 debug_error("focus callback is null..");
1186 g_mutex_unlock(&focus_handle->focus_lock);
1187 return G_SOURCE_CONTINUE;
1189 debug_msg("[CALLBACK(%p) START]", focus_handle->focus_callback);
1190 (focus_handle->focus_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type,
1191 cb_data.option, cb_data.ext_info, focus_handle->user_data);
1192 debug_msg("[CALLBACK END]");
1193 if (g_focus_session_interrupt_info.user_cb) {
1194 debug_msg("sending session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
1195 g_focus_session_interrupt_info.user_cb(cb_data.state, cb_data.stream_type, false,
1196 g_focus_session_interrupt_info.user_data);
1199 #ifdef CONFIG_ENABLE_RETCB
1203 unsigned int buf = 0;
1204 char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", focus_handle->focus_tid, cb_data.handle);
1205 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1207 char str_error[256];
1208 strerror_r(errno, str_error, sizeof(str_error));
1209 debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n",
1210 tid, tmpfd, filename2, errno, str_error);
1212 g_mutex_unlock(&focus_handle->focus_lock);
1213 return G_SOURCE_CONTINUE;
1215 /* buf contains data as below,
1216 * |<--12bits--><--4bits (reacquisition)--><--16bits (handle)-->| */
1217 buf = (unsigned int)((0x0000ffff & cb_data.handle) | (focus_handle->auto_reacquire << 16));
1218 rett = write(tmpfd, &buf, sizeof(buf));
1221 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
1226 g_mutex_unlock(&focus_handle->focus_lock);
1230 return G_SOURCE_CONTINUE;
1233 static gboolean _focus_watch_callback_handler(gpointer user_data)
1235 focus_sound_info_t *focus_handle = (focus_sound_info_t *)user_data;
1239 focus_cb_data_lib cb_data;
1241 debug_log(">>> _focus_watch_callback_handler(), this thread id(%u)\n", (unsigned int)pthread_self());
1243 memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1244 if (!focus_handle) {
1245 debug_error("focus_handle is null");
1248 if (!(poll_fd = &focus_handle->fsrc->poll_fd)) {
1249 debug_error("g_poll_fd is null");
1252 debug_log("focus_handle(%p), poll_fd(%p)", focus_handle, poll_fd);
1254 if (poll_fd->revents & (POLLIN | POLLPRI)) {
1255 count = read(poll_fd->fd, &cb_data, sizeof(cb_data));
1257 char str_error[256];
1258 strerror_r(errno, str_error, sizeof(str_error));
1259 debug_error("GpollFD read fail, errno=%d(%s)",errno, str_error);
1260 return G_SOURCE_CONTINUE;
1263 if (!focus_handle->is_used) {
1264 debug_warning("unsetting watch calllback has been already requested");
1265 goto SKIP_CB_AND_RET;
1268 g_mutex_lock(&focus_handle->focus_lock);
1270 tid = focus_handle->focus_tid;
1272 debug_msg("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)",
1273 tid, cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type);
1275 if (focus_handle->watch_callback == NULL) {
1276 debug_warning("watch callback is null..");
1277 goto SKIP_CB_AND_RET;
1279 if (focus_handle->unset_watch_callback_requested == true) {
1280 debug_warning("unset_watch_callback_requested..");
1281 goto SKIP_CB_AND_RET;
1284 debug_msg("[CALLBACK(%p) START]", focus_handle->watch_callback);
1285 (focus_handle->watch_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type,
1286 cb_data.ext_info, focus_handle->user_data);
1287 debug_msg("[CALLBACK END]");
1288 if (g_focus_session_interrupt_info.user_cb) {
1289 debug_msg("sending session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
1290 (g_focus_session_interrupt_info.user_cb)(cb_data.state, cb_data.stream_type, true,
1291 g_focus_session_interrupt_info.user_data);
1295 #ifdef CONFIG_ENABLE_RETCB
1300 char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr", focus_handle->focus_tid, cb_data.handle);
1301 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1303 char str_error[256];
1304 strerror_r(errno, str_error, sizeof(str_error));
1305 debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n",
1306 tid, tmpfd, filename2, errno, str_error);
1308 g_mutex_unlock(&focus_handle->focus_lock);
1309 return G_SOURCE_CONTINUE;
1311 buf = cb_data.handle;
1312 rett = write(tmpfd, &buf, sizeof(buf));
1315 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
1320 if (focus_handle->is_used) {
1321 debug_msg("unlock focus_lock = %p", &focus_handle->focus_lock);
1322 g_mutex_unlock(&focus_handle->focus_lock);
1327 return G_SOURCE_CONTINUE;
1330 static void _focus_open_callback(int index, bool is_for_watching)
1337 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1338 debug_error("Invalid focus handle index [%d]", index);
1342 if (is_for_watching) {
1343 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch",
1344 g_focus_sound_handle[index].focus_tid,
1345 g_focus_sound_handle[index].handle);
1347 filename = g_strdup_printf("/tmp/FOCUS.%d.%d",
1348 g_focus_sound_handle[index].focus_tid,
1349 g_focus_sound_handle[index].handle);
1351 pre_mask = umask(0);
1352 if (mknod(filename, S_IFIFO|0666, 0)) {
1353 debug_error("mknod() failure, errno(%d)", errno);
1356 g_focus_sound_handle[index].focus_fd = open(filename, O_RDWR|O_NONBLOCK);
1357 if (g_focus_sound_handle[index].focus_fd == -1) {
1358 debug_error("Open fail : index(%d), file open error(%d)", index, errno);
1360 debug_log("Open success : index(%d), filename(%s), fd(%d)",
1361 index, filename, g_focus_sound_handle[index].focus_fd);
1366 #ifdef CONFIG_ENABLE_RETCB
1369 if (is_for_watching) {
1370 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr",
1371 g_focus_sound_handle[index].focus_tid,
1372 g_focus_sound_handle[index].handle);
1374 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr",
1375 g_focus_sound_handle[index].focus_tid,
1376 g_focus_sound_handle[index].handle);
1378 pre_mask = umask(0);
1379 if (mknod(filename2, S_IFIFO | 0666, 0)) {
1380 debug_error("mknod() failure, errno(%d)", errno);
1390 void _focus_close_callback(int index, bool is_for_watching)
1392 char *filename = NULL;
1396 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1397 debug_error("Invalid focus handle index [%d]", index);
1401 if (g_focus_sound_handle[index].focus_fd < 0) {
1402 debug_error("Close fail : index(%d)", index);
1404 close(g_focus_sound_handle[index].focus_fd);
1405 debug_log("Close Success : index(%d)", index);
1408 if (is_for_watching) {
1409 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch",
1410 g_focus_sound_handle[index].focus_tid,
1411 g_focus_sound_handle[index].handle);
1413 filename = g_strdup_printf("/tmp/FOCUS.%d.%d",
1414 g_focus_sound_handle[index].focus_tid,
1415 g_focus_sound_handle[index].handle);
1417 if (remove(filename)) {
1418 debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)",
1424 #ifdef CONFIG_ENABLE_RETCB
1427 if (is_for_watching) {
1428 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr",
1429 g_focus_sound_handle[index].focus_tid,
1430 g_focus_sound_handle[index].handle);
1432 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr",
1433 g_focus_sound_handle[index].focus_tid,
1434 g_focus_sound_handle[index].handle);
1436 if (remove(filename2)) {
1437 debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)",
1448 static GSourceFuncs event_funcs = {
1449 .prepare = _focus_fd_prepare,
1450 .check = _focus_fd_check,
1451 .dispatch = _focus_fd_dispatch,
1452 .finalize = _focus_fd_finalize,
1455 static bool _focus_add_sound_callback(int index, focus_callback_handler_t focus_cb_handler)
1457 FocusSource *fsrc = NULL;
1458 GSource *src = NULL;
1463 g_mutex_init(&g_focus_sound_handle[index].focus_lock);
1465 src = g_source_new(&event_funcs, sizeof(FocusSource));
1467 debug_error("failed to g_source_new for focus source");
1471 fsrc = (FocusSource*) src;
1473 fsrc->poll_fd.fd = g_focus_sound_handle[index].focus_fd;
1474 fsrc->poll_fd.events = (gushort)(POLLIN | POLLPRI);
1475 g_source_add_poll(src, &fsrc->poll_fd);
1477 g_source_set_callback(src, focus_cb_handler, (gpointer)&g_focus_sound_handle[index], NULL);
1479 debug_warning("fsrc(%p), src_funcs(%p), pollfd(%p), fd(%d)",
1480 fsrc, &event_funcs, &fsrc->poll_fd, fsrc->poll_fd.fd);
1482 fsrc_id = g_source_attach(src, g_main_loop_get_context(g_focus_sound_handle[index].focus_loop));
1484 debug_error("failed to attach the source to context");
1487 g_source_unref(src);
1489 g_focus_sound_handle[index].fsrc = fsrc;
1496 g_source_unref(src);
1501 static bool _focus_remove_sound_callback(int index)
1503 focus_sound_info_t *h = NULL;
1507 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1508 debug_error("Invalid focus handle index [%d]", index);
1512 h = &g_focus_sound_handle[index];
1514 g_source_destroy((GSource *)h->fsrc);
1518 h->focus_callback = NULL;
1519 h->watch_callback = NULL;
1521 g_mutex_clear(&h->focus_lock);
1528 static void _focus_add_callback(int index, bool is_for_watching)
1532 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1533 debug_error("Invalid focus handle index [%d]", index);
1537 if (!is_for_watching) {
1538 if (!_focus_add_sound_callback(index, _focus_callback_handler))
1539 debug_error("failed to _focus_add_sound_callback(%p)", _focus_callback_handler);
1540 } else { // need to check if it's necessary
1541 if (!_focus_add_sound_callback(index, _focus_watch_callback_handler))
1542 debug_error("failed to _focus_add_sound_callback(%p)", _focus_watch_callback_handler);
1547 static void _focus_remove_callback(int index)
1550 if (!_focus_remove_sound_callback(index))
1551 debug_error("failed to __focus_remove_sound_callback()");
1555 static void _focus_init_callback(int index, bool is_for_watching)
1558 _focus_open_callback(index, is_for_watching);
1559 _focus_add_callback(index, is_for_watching);
1563 static void _focus_deinit_callback(int index, bool is_for_watching)
1566 _focus_remove_callback(index);
1567 _focus_close_callback(index, is_for_watching);
1571 #define INTERVAL_MS 20
1572 static int _focus_loop_is_running_timed_wait(GMainLoop *focus_loop, int timeout_ms)
1574 int reduced_time_ms = timeout_ms;
1575 if (!focus_loop || timeout_ms < 0) {
1576 debug_error("invalid argument, focus_loop(%p), timeout_ms(%d)", focus_loop, timeout_ms);
1577 return MM_ERROR_INVALID_ARGUMENT;
1581 if (g_main_loop_is_running(focus_loop))
1582 return MM_ERROR_NONE;
1584 usleep(INTERVAL_MS * 1000);
1585 if (reduced_time_ms < timeout_ms)
1586 debug_warning("reduced_time_ms(%d)", reduced_time_ms);
1587 } while ((reduced_time_ms -= INTERVAL_MS) >= 0);
1589 debug_error("focus_loop is not running for timeout_ms(%d), focus_loop(%p) ", timeout_ms, focus_loop);
1591 return MM_ERROR_SOUND_INTERNAL;
1594 #define LOOP_RUNNING_WAIT_TIME_MS 200
1595 static int _focus_init_context(int index)
1597 int ret = MM_ERROR_NONE;
1598 GMainContext *focus_context;
1602 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1603 debug_error("index(%d) is not valid", index);
1604 return MM_ERROR_INVALID_ARGUMENT;
1607 focus_context = g_main_context_new();
1608 g_focus_sound_handle[index].focus_loop = g_main_loop_new(focus_context, FALSE);
1609 g_main_context_unref(focus_context);
1610 if (g_focus_sound_handle[index].focus_loop == NULL) {
1611 debug_error("could not create mainloop..");
1615 g_focus_sound_handle[index].focus_cb_thread = g_thread_new("focus-cb-thread",
1617 g_focus_sound_handle[index].focus_loop);
1618 if (g_focus_sound_handle[index].focus_cb_thread == NULL) {
1619 debug_error("could not create thread..");
1623 debug_warning("focus cb thread[%p] with mainloop[%p] is created for index(%d)",
1624 g_focus_sound_handle[index].focus_cb_thread, g_focus_sound_handle[index].focus_loop, index);
1626 if ((ret = _focus_loop_is_running_timed_wait(g_focus_sound_handle[index].focus_loop, LOOP_RUNNING_WAIT_TIME_MS))) {
1627 debug_error("failed to _focus_loop_is_running_timed_wait(), ret[0x%x]", ret);
1633 return MM_ERROR_NONE;
1636 if (g_focus_sound_handle[index].focus_loop) {
1637 g_main_loop_unref(g_focus_sound_handle[index].focus_loop);
1638 g_focus_sound_handle[index].focus_loop = NULL;
1640 return MM_ERROR_SOUND_INTERNAL;
1643 static void _focus_deinit_context(int index)
1647 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1648 debug_error("index(%d) is not valid", index);
1652 if (!g_focus_sound_handle[index].focus_loop || !g_focus_sound_handle[index].focus_cb_thread) {
1653 debug_error("focus_loop[%p] or focus_cb_thread[%p] is null",
1654 g_focus_sound_handle[index].focus_loop, g_focus_sound_handle[index].focus_cb_thread);
1658 g_main_loop_quit(g_focus_sound_handle[index].focus_loop);
1659 g_thread_join(g_focus_sound_handle[index].focus_cb_thread);
1660 debug_warning("after thread join, thread[%p], mainloop[%p] for index(%d)",
1661 g_focus_sound_handle[index].focus_cb_thread, g_focus_sound_handle[index].focus_loop, index);
1662 g_main_loop_unref(g_focus_sound_handle[index].focus_loop);
1663 g_focus_sound_handle[index].focus_loop = NULL;
1664 g_focus_sound_handle[index].focus_cb_thread = NULL;
1669 int mm_sound_client_get_unique_id(int *id)
1671 int ret = MM_ERROR_NONE;
1676 ret = MM_ERROR_INVALID_ARGUMENT;
1678 ret = mm_sound_proxy_get_unique_id(id);
1685 int mm_sound_client_is_focus_cb_thread(GThread *mine, bool *result)
1687 int ret = MM_ERROR_NONE;
1690 if (!mine || !result)
1691 ret = MM_ERROR_INVALID_ARGUMENT;
1694 for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1695 if (!g_focus_sound_handle[i].is_used)
1697 if (g_focus_sound_handle[i].focus_cb_thread == mine) {
1707 int mm_sound_client_register_focus(int id, int pid, const char *stream_type,
1708 mm_sound_focus_changed_cb callback,
1709 bool is_for_session, void* user_data)
1711 int ret = MM_ERROR_NONE;
1716 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1720 for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
1721 if (g_focus_sound_handle[index].is_used == false) {
1722 g_focus_sound_handle[index].is_used = true;
1727 g_focus_sound_handle[index].focus_tid = instance;
1728 g_focus_sound_handle[index].handle = id;
1729 g_focus_sound_handle[index].focus_callback = callback;
1730 g_focus_sound_handle[index].user_data = user_data;
1731 g_focus_sound_handle[index].is_for_session = is_for_session;
1732 g_focus_sound_handle[index].auto_reacquire = true;
1734 ret = mm_sound_proxy_register_focus(id, pid, stream_type, callback, is_for_session, user_data);
1735 if (ret == MM_ERROR_NONE) {
1736 debug_msg("[Client] Success to register focus\n");
1737 g_need_emergent_exit = TRUE;
1738 if (_focus_init_context(index)) {
1739 ret = MM_ERROR_SOUND_INTERNAL;
1743 debug_error("[Client] Error occurred : 0x%x \n",ret);
1747 _focus_init_callback(index, false);
1751 g_focus_sound_handle[index].is_used = false;
1754 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1760 int mm_sound_client_unregister_focus(int id)
1762 int ret = MM_ERROR_NONE;
1767 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1769 index = _focus_find_index_by_handle(id);
1771 debug_error("Could not find index");
1772 ret = MM_ERROR_INVALID_ARGUMENT;
1775 instance = g_focus_sound_handle[index].focus_tid;
1777 if (!g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1778 debug_warning("maybe focus_callback is being called, try one more time..");
1779 usleep(2500000); // 2.5 sec
1780 if (g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1781 debug_msg("finally got focus_lock");
1785 ret = mm_sound_proxy_unregister_focus(instance, id, g_focus_sound_handle[index].is_for_session);
1786 if (ret == MM_ERROR_NONE)
1787 debug_msg("[Client] Success to unregister focus\n");
1789 debug_error("[Client] Error occurred : 0x%x \n",ret);
1791 g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
1793 _focus_deinit_callback(index, false);
1794 g_focus_sound_handle[index].focus_fd = 0;
1795 g_focus_sound_handle[index].focus_tid = 0;
1796 g_focus_sound_handle[index].handle = 0;
1797 g_focus_sound_handle[index].is_used = false;
1798 _focus_deinit_context(index);
1802 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1807 int mm_sound_client_set_focus_reacquisition(int id, bool reacquisition, bool is_for_session)
1809 int ret = MM_ERROR_NONE;
1815 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1817 index = _focus_find_index_by_handle(id);
1819 debug_error("Could not find index");
1820 ret = MM_ERROR_INVALID_ARGUMENT;
1823 instance = g_focus_sound_handle[index].focus_tid;
1825 ret = mm_sound_client_is_focus_cb_thread(g_thread_self(), &result);
1827 debug_error("[Client] mm_sound_client_is_focus_cb_thread failed");
1829 } else if (!result) {
1830 ret = mm_sound_proxy_set_focus_reacquisition(instance, id, reacquisition, is_for_session);
1831 if (ret == MM_ERROR_NONE) {
1832 debug_msg("[Client] Success to set focus reacquisition to [%d]\n", reacquisition);
1834 debug_error("[Client] Error occurred : 0x%x \n",ret);
1838 debug_warning("[Client] Inside the focus cb thread, set focus reacquisition to [%d]\n", reacquisition);
1841 g_focus_sound_handle[index].auto_reacquire = reacquisition;
1842 debug_msg("[Client] set focus reacquisition(%d) for id(%d)\n", reacquisition, id);
1845 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1850 int mm_sound_client_get_focus_reacquisition(int id, bool *reacquisition)
1852 int ret = MM_ERROR_NONE;
1857 if (!reacquisition) {
1858 debug_error("Invalid parameter");
1859 return MM_ERROR_INVALID_ARGUMENT;
1862 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1864 index = _focus_find_index_by_handle(id);
1866 debug_error("Could not find index");
1867 ret = MM_ERROR_INVALID_ARGUMENT;
1871 *reacquisition = g_focus_sound_handle[index].auto_reacquire;
1872 debug_msg("[Client] get focus reacquisition(%d) for id(%d)\n", *reacquisition, id);
1875 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1880 int mm_sound_client_get_acquired_focus_stream_type(int focus_type, char **stream_type, int *option, char **ext_info)
1882 int ret = MM_ERROR_NONE;
1883 char *ext_str = NULL;
1887 ret = mm_sound_proxy_get_acquired_focus_stream_type(focus_type, stream_type, option, &ext_str);
1888 if (ret == MM_ERROR_NONE) {
1889 debug_msg("[Client] Success to get stream type of acquired focus, stream_type(%s), ext_info(%s)\n",
1890 *stream_type, ext_str);
1891 *ext_info = strdup(ext_str);
1894 debug_error("[Client] Error occurred : 0x%x \n",ret);
1901 int mm_sound_client_acquire_focus(int id, mm_sound_focus_type_e type, int option, const char *ext_info)
1903 int ret = MM_ERROR_NONE;
1908 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1910 index = _focus_find_index_by_handle(id);
1912 debug_error("Could not find index");
1913 ret = MM_ERROR_INVALID_ARGUMENT;
1916 instance = g_focus_sound_handle[index].focus_tid;
1918 ret = mm_sound_proxy_acquire_focus(instance, id, type, option, ext_info, g_focus_sound_handle[index].is_for_session);
1919 if (ret == MM_ERROR_NONE)
1920 debug_msg("[Client] Success to acquire focus\n");
1922 debug_error("[Client] Error occurred : 0x%x \n",ret);
1925 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1930 int mm_sound_client_release_focus(int id, mm_sound_focus_type_e type, int option, const char *ext_info)
1932 int ret = MM_ERROR_NONE;
1937 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1939 index = _focus_find_index_by_handle(id);
1941 debug_error("Could not find index");
1942 ret = MM_ERROR_INVALID_ARGUMENT;
1945 instance = g_focus_sound_handle[index].focus_tid;
1947 ret = mm_sound_proxy_release_focus(instance, id, type, option, ext_info, g_focus_sound_handle[index].is_for_session);
1948 if (ret == MM_ERROR_NONE)
1949 debug_msg("[Client] Success to release focus\n");
1951 debug_error("[Client] Error occurred : 0x%x \n",ret);
1954 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1959 int mm_sound_client_update_stream_focus_status(int id, unsigned int status)
1961 int ret = MM_ERROR_NONE;
1964 if ((ret = mm_sound_proxy_update_stream_focus_status(id, status)) != MM_ERROR_NONE)
1965 debug_error("[Client] failed to update stream focus status, ret[0x%x]", ret);
1971 int mm_sound_client_set_focus_watch_callback(int pid, mm_sound_focus_type_e focus_type,
1972 mm_sound_focus_changed_watch_cb callback,
1973 bool is_for_session, void* user_data, int *id)
1975 int ret = MM_ERROR_NONE;
1982 return MM_ERROR_INVALID_ARGUMENT;
1984 //pthread_mutex_lock(&g_thread_mutex2);
1988 ret = mm_sound_proxy_get_unique_id(id);
1992 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1994 for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
1995 if (g_focus_sound_handle[index].is_used == false) {
1996 g_focus_sound_handle[index].is_used = true;
2001 g_focus_sound_handle[index].focus_tid = instance;
2002 g_focus_sound_handle[index].handle = *id;
2003 g_focus_sound_handle[index].watch_callback = callback;
2004 g_focus_sound_handle[index].user_data = user_data;
2005 g_focus_sound_handle[index].is_for_session = is_for_session;
2006 g_focus_sound_handle[index].unset_watch_callback_requested = false;
2008 ret = mm_sound_proxy_set_focus_watch_callback(pid, g_focus_sound_handle[index].handle, focus_type,
2009 callback, is_for_session, user_data);
2011 if (ret == MM_ERROR_NONE) {
2012 debug_msg("[Client] Success to watch focus");
2013 g_need_emergent_exit = TRUE;
2014 if (_focus_init_context(index)) {
2015 ret = MM_ERROR_SOUND_INTERNAL;
2019 debug_error("[Client] Error occurred : 0x%x",ret);
2023 _focus_init_callback(index, true);
2027 g_focus_sound_handle[index].is_used = false;
2030 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2035 int mm_sound_client_request_unset_focus_watch_callback(int id)
2037 int ret = MM_ERROR_NONE;
2041 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2043 index = _focus_watch_find_index_by_handle(id);
2045 debug_error("Could not find index");
2046 ret = MM_ERROR_INVALID_ARGUMENT;
2049 g_focus_sound_handle[index].unset_watch_callback_requested = true;
2052 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2057 int mm_sound_client_unset_focus_watch_callback(int id)
2059 int ret = MM_ERROR_NONE;
2063 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2065 index = _focus_watch_find_index_by_handle(id);
2067 debug_error("Could not find index");
2068 ret = MM_ERROR_INVALID_ARGUMENT;
2072 g_mutex_lock(&g_focus_sound_handle[index].focus_lock);
2074 g_focus_sound_handle[index].is_used = false;
2076 ret = mm_sound_proxy_unset_focus_watch_callback(g_focus_sound_handle[index].focus_tid,
2077 g_focus_sound_handle[index].handle,
2078 g_focus_sound_handle[index].is_for_session);
2080 if (ret == MM_ERROR_NONE)
2081 debug_msg("[Client] Success to unwatch focus\n");
2083 debug_error("[Client] Error occurred : 0x%x \n",ret);
2086 g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
2088 _focus_deinit_callback(index, true);
2089 g_focus_sound_handle[index].focus_fd = 0;
2090 g_focus_sound_handle[index].focus_tid = 0;
2091 g_focus_sound_handle[index].handle = 0;
2092 _focus_deinit_context(index);
2095 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2100 static gboolean _idle_event_callback(void *data)
2102 focus_idle_event_t *idle_event_data = (focus_idle_event_t*)data;
2103 int ret = MM_ERROR_NONE;
2106 debug_error("data is null");
2110 debug_msg("idle_event_data(%p): type(%d), data(%d)",
2111 idle_event_data, idle_event_data->type, idle_event_data->data);
2113 switch (idle_event_data->type) {
2114 case IDLE_EVENT_TYPE_UNSET_FOCUS_WATCH_CB:
2115 if ((ret = mm_sound_client_unset_focus_watch_callback(idle_event_data->data)))
2116 debug_error("Could not unset focus watch callback, id(%d), ret = %x\n", idle_event_data->data, ret);
2118 case IDLE_EVENT_TYPE_UNREGISTER_FOCUS:
2119 if ((ret = mm_sound_client_unregister_focus(idle_event_data->data)))
2120 debug_error("Could not unregister focus, id(%d), ret = %x\n", idle_event_data->data, ret);
2123 debug_warning("invalid type(%d)", idle_event_data->type);
2127 g_free(idle_event_data);
2129 g_idle_event_src = 0;
2131 MMSOUND_LEAVE_CRITICAL_SECTION(&g_event_mutex);
2136 int mm_sound_client_execute_focus_func_in_main_context(focus_idle_event_type_e type, int data)
2138 focus_idle_event_t *idle_event_data = NULL;
2140 if (IDLE_EVENT_TYPE_UNSET_FOCUS_WATCH_CB > type || IDLE_EVENT_TYPE_MAX < type)
2141 return MM_ERROR_INVALID_ARGUMENT;
2143 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_event_mutex, MM_ERROR_SOUND_INTERNAL);
2145 idle_event_data = g_new0(focus_idle_event_t, 1);
2146 idle_event_data->type = type;
2147 idle_event_data->data = data;
2149 g_idle_event_src = g_idle_add_full(G_PRIORITY_HIGH,
2150 (GSourceFunc)_idle_event_callback,
2151 (gpointer)idle_event_data,
2154 return MM_ERROR_NONE;
2159 int mm_sound_client_add_test_callback(mm_sound_test_cb func, void* user_data, unsigned int *subs_id)
2161 int ret = MM_ERROR_NONE;
2165 ret = mm_sound_proxy_add_test_callback(func, user_data, g_free, subs_id);
2171 int mm_sound_client_remove_test_callback(unsigned int subs_id)
2173 int ret = MM_ERROR_NONE;
2176 ret = mm_sound_proxy_remove_test_callback(subs_id);
2183 int mm_sound_client_test(int a, int b, int* getv)
2185 int ret = MM_ERROR_NONE;
2189 ret = mm_sound_proxy_test(a, b, getv);
2190 debug_log("%d * %d -> result : %d", a, b, *getv);