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 /* will be removed when the session concept is completely left out*/
107 } focus_sound_info_t;
114 char stream_type[MAX_STREAM_TYPE_LEN];
115 char ext_info[MM_SOUND_NAME_NUM];
121 mm_sound_focus_session_interrupt_cb user_cb;
123 } focus_session_interrupt_info_t;
125 typedef gboolean (*focus_callback_handler_t)(gpointer user_data);
127 focus_sound_info_t g_focus_sound_handle[FOCUS_HANDLE_MAX];
128 focus_session_interrupt_info_t g_focus_session_interrupt_info = {-1, NULL, NULL};
129 static pthread_mutex_t g_index_mutex = PTHREAD_MUTEX_INITIALIZER;
130 static pthread_mutex_t g_event_mutex = PTHREAD_MUTEX_INITIALIZER;
131 guint g_focus_signal_handle;
132 guint g_idle_event_src;
135 gboolean g_need_emergent_exit = FALSE;
138 /* handle to watch end of playing */
140 /* subscription id to unsubscribe when handle ended */
142 } play_sound_end_callback_data_t;
144 typedef struct _focus_idle_event {
145 focus_idle_event_type_e type;
147 } focus_idle_event_t;
149 void _system_signal_handler(int signo)
151 int ret = MM_ERROR_NONE;
152 sigset_t old_mask, all_mask;
154 debug_warning("Got signal : signo(%d)", signo);
158 sigfillset(&all_mask);
159 sigprocmask(SIG_BLOCK, &all_mask, &old_mask);
161 if (g_need_emergent_exit) {
162 ret = mm_sound_proxy_emergent_exit(getpid());
163 if (ret == MM_ERROR_NONE)
164 debug_msg("[Client] Success to emergnet_exit\n");
166 debug_error("[Client] Error occurred : 0x%x \n",ret);
169 sigprocmask(SIG_SETMASK, &old_mask, NULL);
174 sigaction(SIGINT, &system_int_old_action, NULL);
178 sigaction(SIGABRT, &system_abrt_old_action, NULL);
182 sigaction(SIGSEGV, &system_segv_old_action, NULL);
186 sigaction(SIGTERM, &system_term_old_action, NULL);
190 sigaction(SIGSYS, &system_sys_old_action, NULL);
194 sigaction(SIGXCPU, &system_xcpu_old_action, NULL);
201 debug_warning("signal handling end");
204 int mm_sound_client_initialize(void)
206 int ret = MM_ERROR_NONE;
209 mm_sound_proxy_initialize();
210 g_idle_event_src = 0;
213 struct sigaction system_action;
214 system_action.sa_handler = _system_signal_handler;
215 system_action.sa_flags = SA_NOCLDSTOP;
217 sigemptyset(&system_action.sa_mask);
219 sigaction(SIGINT, &system_action, &system_int_old_action);
220 sigaction(SIGABRT, &system_action, &system_abrt_old_action);
221 sigaction(SIGSEGV, &system_action, &system_segv_old_action);
222 sigaction(SIGTERM, &system_action, &system_term_old_action);
223 sigaction(SIGSYS, &system_action, &system_sys_old_action);
224 sigaction(SIGXCPU, &system_action, &system_xcpu_old_action);
230 int mm_sound_client_finalize(void)
232 int ret = MM_ERROR_NONE;
236 if (g_need_emergent_exit) {
237 ret = mm_sound_proxy_emergent_exit(getpid());
238 if (ret == MM_ERROR_NONE)
239 debug_msg("[Client] Success to emergnet_exit\n");
241 debug_error("[Client] Error occurred : 0x%x \n",ret);
244 sigaction(SIGINT, &system_int_old_action, NULL);
245 sigaction(SIGABRT, &system_abrt_old_action, NULL);
246 sigaction(SIGSEGV, &system_segv_old_action, NULL);
247 sigaction(SIGTERM, &system_term_old_action, NULL);
248 sigaction(SIGSYS, &system_sys_old_action, NULL);
249 sigaction(SIGXCPU, &system_xcpu_old_action, NULL);
251 ret = mm_sound_proxy_finalize();
254 if (g_idle_event_src > 0) {
255 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_event_mutex, MM_ERROR_SOUND_INTERNAL);
256 g_source_remove(g_idle_event_src);
257 MMSOUND_LEAVE_CRITICAL_SECTION(&g_event_mutex);
265 void mm_sound_convert_volume_type_to_stream_type(int volume_type, char *stream_type)
267 switch (volume_type) {
268 case VOLUME_TYPE_SYSTEM:
269 MMSOUND_STRNCPY(stream_type, "system", MAX_STREAM_TYPE_LEN);
271 case VOLUME_TYPE_NOTIFICATION:
272 MMSOUND_STRNCPY(stream_type, "notification", MAX_STREAM_TYPE_LEN);
274 case VOLUME_TYPE_ALARM:
275 MMSOUND_STRNCPY(stream_type, "alarm", MAX_STREAM_TYPE_LEN);
277 case VOLUME_TYPE_RINGTONE:
278 MMSOUND_STRNCPY(stream_type, "ringtone-voip", MAX_STREAM_TYPE_LEN);
280 case VOLUME_TYPE_MEDIA:
281 MMSOUND_STRNCPY(stream_type, "media", MAX_STREAM_TYPE_LEN);
283 case VOLUME_TYPE_CALL:
284 MMSOUND_STRNCPY(stream_type, "call-voice", MAX_STREAM_TYPE_LEN);
286 case VOLUME_TYPE_VOIP:
287 MMSOUND_STRNCPY(stream_type, "voip", MAX_STREAM_TYPE_LEN);
289 case VOLUME_TYPE_VOICE:
290 MMSOUND_STRNCPY(stream_type, "voice-information", MAX_STREAM_TYPE_LEN);
293 MMSOUND_STRNCPY(stream_type, "media", MAX_STREAM_TYPE_LEN);
297 debug_msg("volume type (%d) converted to stream type (%s)", volume_type, stream_type);
301 /*****************************************************************************************
302 DBUS SUPPORTED FUNCTIONS
303 ******************************************************************************************/
305 void _mm_sound_client_focus_signal_callback(mm_sound_signal_name_t signal, int value, void *user_data)
307 int ret = MM_ERROR_NONE;
310 debug_msg("focus signal received, value = %d", value);
313 ret = mm_sound_proxy_clear_focus(getpid());
315 debug_error("clear focus failed ret = 0x%x", ret);
316 mm_sound_unsubscribe_signal(g_focus_signal_handle);
317 g_focus_signal_handle = 0;
322 int mm_sound_client_play_tone(int number, int volume_config, double volume,
323 int time, int *handle, bool enable_session)
325 int ret = MM_ERROR_NONE;
326 // int instance = -1; /* instance is unique to communicate with server : client message queue filter type */
327 int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(volume_config);
328 char stream_type[MAX_STREAM_TYPE_LEN] = {0, };
332 /* read session information */
333 int session_type = MM_SESSION_TYPE_MEDIA;
334 int session_options = 0;
335 int is_focus_registered = 0;
337 ret = mm_sound_get_signal_value(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &is_focus_registered);
339 debug_error("mm_sound_get_signal_value failed [0x%x]", ret);
340 return MM_ERROR_POLICY_INTERNAL;
343 if (is_focus_registered)
344 enable_session = false;
346 if (enable_session) {
347 if (MM_ERROR_NONE != _mm_session_util_read_information(-1, &session_type, &session_options)) {
348 debug_warning("[Client] Read Session Information failed. use default \"media\" type\n");
349 session_type = MM_SESSION_TYPE_MEDIA;
351 if(MM_ERROR_NONE != mm_session_init(session_type)) {
352 debug_critical("[Client] MMSessionInit() failed\n");
353 return MM_ERROR_POLICY_INTERNAL;
358 // instance = getpid();
359 //debug_log("[Client] pid for client ::: [%d]\n", instance);
362 debug_msg("[Client] Input number : %d\n", number);
363 /* Send req memory */
365 mm_sound_convert_volume_type_to_stream_type(volume_type, stream_type);
366 ret = mm_sound_proxy_play_tone(number, time, volume, volume_config,
367 session_type, session_options, getpid(), enable_session, handle, stream_type, -1);
369 if (enable_session && !g_focus_signal_handle) {
370 ret = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &g_focus_signal_handle,
371 _mm_sound_client_focus_signal_callback, NULL);
373 debug_error("mm_sound_subscribe_signal failed [0x%x]", ret);
374 return MM_ERROR_POLICY_INTERNAL;
383 int mm_sound_client_play_tone_with_stream_info(int tone, char *stream_type, int stream_id,
384 double volume, int duration, int *handle)
386 int ret = MM_ERROR_NONE;
390 ret = mm_sound_proxy_play_tone_with_stream_info(getpid(), tone, stream_type, stream_id, volume, duration, handle);
396 static void _mm_sound_stop_callback_wrapper_func(int ended_handle, void *userdata)
398 struct callback_data *cb_data = (struct callback_data*) userdata;
399 play_sound_end_callback_data_t *end_cb_data;
401 debug_log("[Wrapper CB][Play Stop] ended_handle : %d", ended_handle);
403 if (cb_data == NULL) {
404 debug_warning("stop callback data null");
408 end_cb_data = (play_sound_end_callback_data_t*) cb_data->extra_data;
410 if (ended_handle == end_cb_data->watching_handle) {
411 debug_log("Interested playing handle end : %d", ended_handle);
412 ((mm_sound_stop_callback_func)(cb_data->user_cb))(cb_data->user_data, ended_handle);
413 if (mm_sound_proxy_remove_play_sound_end_callback(end_cb_data->subs_id) != MM_ERROR_NONE)
414 debug_error("mm_sound_client_dbus_remove_play_file_end_callback failed");
416 debug_log("Not interested playing handle : %d", ended_handle);
420 static void play_end_callback_data_free_func(void *data)
422 struct callback_data *cb_data = (struct callback_data*) data;
425 g_free(cb_data->extra_data);
430 int mm_sound_client_play_sound(MMSoundPlayParam *param, int tone, int *handle)
432 int ret = MM_ERROR_NONE;
433 int session_type = MM_SESSION_TYPE_MEDIA;
434 int session_options = 0;
435 int is_focus_registered = 0;
436 // int instance = -1; /* instance is unique to communicate with server : client message queue filter type */
437 int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(param->volume_config);
438 char stream_type[MAX_STREAM_TYPE_LEN] = {0, };
439 struct callback_data *cb_data = NULL;
440 play_sound_end_callback_data_t *end_cb_data;
444 /* read session information */
446 ret = mm_sound_get_signal_value(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &is_focus_registered);
448 debug_error("mm_sound_get_signal_value failed [0x%x]", ret);
449 return MM_ERROR_POLICY_INTERNAL;
452 if (is_focus_registered)
453 param->skip_session = true;
455 if (param->skip_session == false) {
456 if(MM_ERROR_NONE != _mm_session_util_read_information(-1, &session_type, &session_options)) {
457 debug_warning("[Client] Read MMSession Type failed. use default \"media\" type\n");
458 session_type = MM_SESSION_TYPE_MEDIA;
460 if(MM_ERROR_NONE != mm_session_init(session_type)) {
461 debug_critical("[Client] MMSessionInit() failed\n");
462 return MM_ERROR_POLICY_INTERNAL;
468 if ((param->mem_ptr && param->mem_size)) {
469 // Play memory, deprecated
470 return MM_ERROR_INVALID_ARGUMENT;
473 mm_sound_convert_volume_type_to_stream_type(volume_type, stream_type);
474 ret = mm_sound_proxy_play_sound(param->filename, tone, param->loop, param->volume, param->volume_config,
475 session_type, session_options, getpid(), param->skip_session, handle, stream_type, -1);
476 if (ret != MM_ERROR_NONE) {
477 debug_error("Play Sound Failed");
480 if (param->callback) {
481 end_cb_data = (play_sound_end_callback_data_t *) g_malloc0(sizeof(play_sound_end_callback_data_t));
482 end_cb_data->watching_handle = *handle;
483 GET_CB_DATA(cb_data, param->callback, param->data, end_cb_data);
485 ret = mm_sound_proxy_add_play_sound_end_callback(_mm_sound_stop_callback_wrapper_func, cb_data,
486 play_end_callback_data_free_func, &end_cb_data->subs_id);
487 if (ret != MM_ERROR_NONE) {
488 debug_error("Add callback for play sound(%d) Failed", *handle);
492 if (!param->skip_session && !g_focus_signal_handle) {
493 ret = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &g_focus_signal_handle,
494 _mm_sound_client_focus_signal_callback, NULL);
496 debug_error("mm_sound_subscribe_signal failed [0x%x]", ret);
497 return MM_ERROR_POLICY_INTERNAL;
508 int mm_sound_client_play_sound_with_stream_info(MMSoundPlayParam *param, int *handle, char* stream_type, int stream_id)
510 int ret = MM_ERROR_NONE;
511 struct callback_data *cb_data = NULL;
512 play_sound_end_callback_data_t *end_cb_data;
514 ret = mm_sound_proxy_play_sound_with_stream_info(param->filename, param->loop, param->volume,
515 getpid(), handle, stream_type, stream_id);
516 if (ret != MM_ERROR_NONE) {
517 debug_error("Play Sound Failed");
520 if (param->callback) {
521 end_cb_data = (play_sound_end_callback_data_t *) g_malloc0(sizeof(play_sound_end_callback_data_t));
522 end_cb_data->watching_handle = *handle;
523 GET_CB_DATA(cb_data, param->callback, param->data, end_cb_data);
525 ret = mm_sound_proxy_add_play_sound_end_callback(_mm_sound_stop_callback_wrapper_func, cb_data,
526 play_end_callback_data_free_func, &end_cb_data->subs_id);
527 if (ret != MM_ERROR_NONE) {
528 debug_error("Add callback for play sound(%d) Failed", *handle);
539 int mm_sound_client_stop_sound(int handle)
541 int ret = MM_ERROR_NONE;
544 if (handle < 0 || handle > CLIENT_HANDLE_MAX) {
545 ret = MM_ERROR_INVALID_ARGUMENT;
549 ret = mm_sound_proxy_stop_sound(handle);
555 static int _mm_sound_client_device_list_dump (GList *device_list)
557 int ret = MM_ERROR_NONE;
559 mm_sound_device_t *device_node = NULL;
562 debug_error("Device list NULL, cannot dump list");
563 return MM_ERROR_SOUND_INTERNAL;
566 debug_log("======================== device list : start ==========================\n");
567 for (list = device_list; list != NULL; list = list->next) {
568 device_node = (mm_sound_device_t *)list->data;
570 debug_log(" list idx[%d]: type[%17s], id[%02d], io_direction[%d], state[%d], name[%s]\n",
571 count++, device_node->type, device_node->id, device_node->io_direction,
572 device_node->state, device_node->name);
575 debug_log("======================== device list : end ============================\n");
580 int mm_sound_client_get_current_connected_device_list(int device_flags, mm_sound_device_list_t *device_list)
582 int ret = MM_ERROR_NONE;
586 debug_error("Device list NULL");
587 ret = MM_ERROR_COMMON_INVALID_ARGUMENT;
591 if ((ret = mm_sound_proxy_get_current_connected_device_list(device_flags, &device_list->list)) != MM_ERROR_NONE) {
592 debug_error("[Client] failed to get current connected device list with dbus, ret[0x%x]", ret);
595 if (!device_list->list) {
596 debug_error("Got device list null");
597 ret = MM_ERROR_SOUND_NO_DATA;
600 _mm_sound_client_device_list_dump(device_list->list);
607 static bool device_is_match_direction(int direction, int mask)
609 if (mask == DEVICE_IO_DIRECTION_FLAGS || mask == 0)
612 if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_IN_FLAG) && (direction & MM_SOUND_DEVICE_IO_DIRECTION_IN))
614 if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_OUT_FLAG) && (direction & MM_SOUND_DEVICE_IO_DIRECTION_OUT))
616 if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_BOTH_FLAG) && (direction == MM_SOUND_DEVICE_IO_DIRECTION_BOTH))
622 static bool device_is_match_state(int state, int mask)
624 if (mask == DEVICE_STATE_FLAGS || mask == 0)
627 if ((mask & MM_SOUND_DEVICE_STATE_DEACTIVATED_FLAG) && (state == MM_SOUND_DEVICE_STATE_DEACTIVATED))
629 if ((mask & MM_SOUND_DEVICE_STATE_ACTIVATED_FLAG) && (state == MM_SOUND_DEVICE_STATE_ACTIVATED))
635 static bool device_is_match_type(const char *type, int mask)
638 const char *builtin_prefix = "builtin";
640 if (mask == DEVICE_TYPE_FLAGS || mask == 0)
643 is_builtin = !strncmp(type, builtin_prefix, strlen(builtin_prefix));
645 if ((mask & MM_SOUND_DEVICE_TYPE_INTERNAL_FLAG) && (is_builtin))
647 if ((mask & MM_SOUND_DEVICE_TYPE_EXTERNAL_FLAG) && (!is_builtin))
653 static bool device_is_match_with_mask(const char *type, int direction, int state, int mask)
655 if (mask == DEVICE_ALL_FLAG)
658 return (device_is_match_direction(direction, mask & DEVICE_IO_DIRECTION_FLAGS) &&
659 device_is_match_state(state, mask & DEVICE_STATE_FLAGS) &&
660 device_is_match_type(type, mask & DEVICE_TYPE_FLAGS));
663 static int _fill_sound_device(mm_sound_device_t *device_h, int device_id, const char *device_type,
664 int direction, int state, const char *name, int *stream_id, int stream_num)
668 if (stream_num > 0 && stream_id == NULL) {
669 debug_error("stream_num is %d, but stream_id is NULL", stream_num);
673 if (stream_num > MAX_STREAM_ON_DEVICE) {
674 debug_error("too many streams on this device");
678 device_h->id = device_id;
679 device_h->io_direction = direction;
680 device_h->state = state;
681 MMSOUND_STRNCPY(device_h->name, name, MAX_DEVICE_NAME_NUM);
682 MMSOUND_STRNCPY(device_h->type, device_type, MAX_DEVICE_TYPE_STR_LEN);
684 if (stream_num > 0) {
685 device_h->stream_num = stream_num;
686 debug_log("%d streams on this device", stream_num);
687 for (i = 0; i < stream_num; i++) {
688 debug_log(" stream_id : %d", stream_id[i]);
689 device_h->stream_id[i] = stream_id[i];
692 device_h->stream_num = 0;
693 debug_log("There is no stream on this device");
699 static void _mm_sound_device_connected_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
700 int state, const char *name, int *stream_id, int stream_num,
701 gboolean is_connected, void *userdata)
703 mm_sound_device_t device_h;
704 struct callback_data *cb_data = (struct callback_data*) userdata;
707 debug_log("[Device %s] id(%d) type(%s) direction(%d) state(%d) name(%s)",
708 is_connected ? "Connected" : "Disconnected", device_id, device_type, io_direction,
709 state, name, is_connected);
711 if (cb_data == NULL) {
712 debug_warning("device connected changed callback data null");
716 device_flags = (int) cb_data->extra_data;
717 if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
720 if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name, stream_id, stream_num) < 0) {
721 debug_error("Failed to fill sound device");
725 ((mm_sound_device_connected_cb)(cb_data->user_cb))(&device_h, is_connected, cb_data->user_data);
728 int mm_sound_client_add_device_connected_callback(int device_flags, mm_sound_device_connected_cb func,
729 void* userdata, unsigned int *subs_id)
731 int ret = MM_ERROR_NONE;
732 struct callback_data *cb_data = NULL;
736 GET_CB_DATA(cb_data, func, userdata, (void*) device_flags);
738 ret = mm_sound_proxy_add_device_connected_callback(device_flags,
739 _mm_sound_device_connected_callback_wrapper_func,
740 cb_data, g_free, subs_id);
741 if (ret == MM_ERROR_NONE)
742 g_need_emergent_exit = TRUE;
748 int mm_sound_client_remove_device_connected_callback(unsigned int subs_id)
750 int ret = MM_ERROR_NONE;
753 ret = mm_sound_proxy_remove_device_connected_callback(subs_id);
759 static void _mm_sound_device_info_changed_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
760 int state, const char *name, int *stream_id, int stream_num,
761 int changed_device_info_type, void *userdata)
763 mm_sound_device_t device_h;
764 struct callback_data *cb_data = (struct callback_data*) userdata;
767 debug_log("[Device Info Changed] id(%d) type(%s) direction(%d) state(%d) name(%s) changed_info_type(%d)",
768 device_id, device_type, io_direction, state, name, changed_device_info_type);
770 if (cb_data == NULL) {
771 debug_warning("device info changed callback data null");
775 device_flags = (int) cb_data->extra_data;
776 if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
779 if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name, stream_id, stream_num) < 0) {
780 debug_error("Failed to fill sound device");
784 ((mm_sound_device_info_changed_cb)(cb_data->user_cb))(&device_h, changed_device_info_type, cb_data->user_data);
787 int mm_sound_client_add_device_info_changed_callback(int device_flags, mm_sound_device_info_changed_cb func,
788 void *userdata, unsigned int *subs_id)
790 int ret = MM_ERROR_NONE;
791 struct callback_data *cb_data = (struct callback_data*) userdata;
795 GET_CB_DATA(cb_data, func, userdata, (void *) device_flags);
797 ret = mm_sound_proxy_add_device_info_changed_callback(device_flags,
798 _mm_sound_device_info_changed_callback_wrapper_func,
799 cb_data, g_free, subs_id);
805 int mm_sound_client_remove_device_info_changed_callback(unsigned int subs_id)
807 int ret = MM_ERROR_NONE;
810 ret = mm_sound_proxy_remove_device_info_changed_callback(subs_id);
817 static void _mm_sound_device_state_changed_callback_wrapper_func(int device_id, const char *device_type,
818 int io_direction, int state, const char *name,
819 int *stream_id, int stream_num, void *userdata)
821 mm_sound_device_t device_h;
822 struct callback_data *cb_data = (struct callback_data*) userdata;
825 debug_log("[Device State Changed] id(%d) type(%s) direction(%d) state(%d) name(%s)",
826 device_id, device_type, io_direction, state, name);
828 if (cb_data == NULL) {
829 debug_warning("device state changed callback data null");
833 device_flags = (int) cb_data->extra_data;
835 if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
838 if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name, stream_id, stream_num) < 0) {
839 debug_error("Failed to fill sound device");
843 ((mm_sound_device_state_changed_cb)(cb_data->user_cb))(&device_h, state, cb_data->user_data);
846 int mm_sound_client_add_device_state_changed_callback(int device_flags, mm_sound_device_state_changed_cb func,
847 void *userdata, unsigned int *id)
849 int ret = MM_ERROR_NONE;
850 struct callback_data *cb_data = (struct callback_data*) userdata;
854 GET_CB_DATA(cb_data, func, userdata, (void *) device_flags);
856 ret = mm_sound_proxy_add_device_state_changed_callback(device_flags,
857 _mm_sound_device_state_changed_callback_wrapper_func,
858 cb_data, g_free, id);
864 int mm_sound_client_remove_device_state_changed_callback(unsigned int id)
866 int ret = MM_ERROR_NONE;
869 ret = mm_sound_proxy_remove_device_state_changed_callback(id);
875 int mm_sound_client_is_stream_on_device(int stream_id, int device_id, bool *is_on)
877 int ret = MM_ERROR_NONE;
881 debug_error("Invalid Parameter");
882 ret = MM_ERROR_COMMON_INVALID_ARGUMENT;
886 if ((ret = mm_sound_proxy_is_stream_on_device(stream_id, device_id, is_on)) != MM_ERROR_NONE) {
887 debug_error("[Client] failed to query is stream on device, ret[0x%x]", ret);
896 int __convert_volume_type_to_str(int volume_type, char **volume_type_str)
898 int ret = MM_ERROR_NONE;
900 if (!volume_type_str) {
901 return MM_ERROR_COMMON_INVALID_ARGUMENT;
904 switch (volume_type) {
905 case VOLUME_TYPE_SYSTEM:
906 *volume_type_str = "system";
908 case VOLUME_TYPE_NOTIFICATION:
909 *volume_type_str = "notification";
911 case VOLUME_TYPE_ALARM:
912 *volume_type_str = "alarm";
914 case VOLUME_TYPE_RINGTONE:
915 *volume_type_str = "ringtone";
917 case VOLUME_TYPE_MEDIA:
918 *volume_type_str = "media";
920 case VOLUME_TYPE_CALL:
921 *volume_type_str = "call";
923 case VOLUME_TYPE_VOIP:
924 *volume_type_str = "voip";
926 case VOLUME_TYPE_VOICE:
927 *volume_type_str = "voice";
930 if (!strncmp(*volume_type_str,"", VOLUME_TYPE_LEN)) {
931 debug_error("could not find the volume_type[%d] in this switch case statement", volume_type);
932 ret = MM_ERROR_SOUND_INTERNAL;
934 debug_log("volume_type[%s]", *volume_type_str);
939 static int __convert_volume_type_to_int(const char *volume_type_str, volume_type_t *volume_type)
941 int ret = MM_ERROR_NONE;
943 if (!volume_type || !volume_type_str) {
944 return MM_ERROR_COMMON_INVALID_ARGUMENT;
947 if (!strncmp(volume_type_str, "system", VOLUME_TYPE_LEN)) {
948 *volume_type = VOLUME_TYPE_SYSTEM;
949 } else if (!strncmp(volume_type_str, "notification", VOLUME_TYPE_LEN)) {
950 *volume_type = VOLUME_TYPE_NOTIFICATION;
951 } else if (!strncmp(volume_type_str, "alarm", VOLUME_TYPE_LEN)) {
952 *volume_type = VOLUME_TYPE_ALARM;
953 } else if (!strncmp(volume_type_str, "ringtone", VOLUME_TYPE_LEN)) {
954 *volume_type = VOLUME_TYPE_RINGTONE;
955 } else if (!strncmp(volume_type_str, "media", VOLUME_TYPE_LEN)) {
956 *volume_type = VOLUME_TYPE_MEDIA;
957 } else if (!strncmp(volume_type_str, "call", VOLUME_TYPE_LEN)) {
958 *volume_type = VOLUME_TYPE_CALL;
959 } else if (!strncmp(volume_type_str, "voip", VOLUME_TYPE_LEN)) {
960 *volume_type = VOLUME_TYPE_VOIP;
961 } else if (!strncmp(volume_type_str, "voice", VOLUME_TYPE_LEN)) {
962 *volume_type = VOLUME_TYPE_VOICE;
964 debug_log("Invalid volume type : [%s]", volume_type_str);
965 ret = MM_ERROR_SOUND_INTERNAL;
971 int mm_sound_client_set_volume_by_type(const int volume_type, const unsigned int volume_level)
973 int ret = MM_ERROR_NONE;
974 char *type_str = NULL;
977 if ((ret = __convert_volume_type_to_str(volume_type, &type_str)) != MM_ERROR_NONE) {
978 debug_error("volume type convert failed");
982 ret = mm_sound_proxy_set_volume_by_type(type_str, volume_level);
989 static void _mm_sound_volume_changed_callback_wrapper_func(const char *direction, const char *volume_type_str,
990 int volume_level, void *userdata)
992 volume_type_t volume_type = 0;
993 struct callback_data *cb_data = (struct callback_data *) userdata;
995 debug_log("[Wrapper CB][Volume Changed] direction : %s, volume_type : %s, volume_level : %d",
996 direction, volume_type_str, volume_level);
998 if (cb_data == NULL) {
999 debug_warning("volume changed callback data null");
1003 if (__convert_volume_type_to_int(volume_type_str, &volume_type) != MM_ERROR_NONE) {
1004 debug_error("volume type convert failed");
1007 debug_log("Call volume changed user cb, direction : %s, vol_type : %s(%d), level : %u",
1008 direction, volume_type_str, volume_type, volume_level);
1009 ((mm_sound_volume_changed_cb)(cb_data->user_cb))(volume_type, volume_level, cb_data->user_data);
1012 int mm_sound_client_add_volume_changed_callback(mm_sound_volume_changed_cb func, void* userdata, unsigned int *subs_id)
1014 int ret = MM_ERROR_NONE;
1015 struct callback_data *cb_data = NULL;
1019 GET_CB_DATA(cb_data, func, userdata, NULL);
1021 ret = mm_sound_proxy_add_volume_changed_callback(_mm_sound_volume_changed_callback_wrapper_func, cb_data, g_free, subs_id);
1028 int mm_sound_client_remove_volume_changed_callback(unsigned int subs_id)
1030 int ret = MM_ERROR_NONE;
1033 ret = mm_sound_proxy_remove_volume_changed_callback(subs_id);
1039 int mm_sound_client_set_filter_by_type(const char *stream_type, const char *filter_name, const char *filter_parameters, const char *filter_group)
1041 int ret = MM_ERROR_NONE;
1044 ret = mm_sound_proxy_set_filter_by_type(stream_type, filter_name, filter_parameters, filter_group);
1050 int mm_sound_client_unset_filter_by_type(const char *stream_type)
1052 int ret = MM_ERROR_NONE;
1055 ret = mm_sound_proxy_unset_filter_by_type(stream_type);
1061 int mm_sound_client_control_filter_by_type(const char *stream_type, const char *filter_name, const char *filter_controls)
1063 int ret = MM_ERROR_NONE;
1066 ret = mm_sound_proxy_control_filter_by_type(stream_type, filter_name, filter_controls);
1073 static gboolean _interrupted_completed(gpointer *data)
1076 debug_error("data is null");
1079 if (!g_focus_session_interrupt_info.user_cb) {
1080 debug_error("user_cb is null");
1085 debug_msg("invoke user_cb(%p)", g_focus_session_interrupt_info.user_cb);
1086 (g_focus_session_interrupt_info.user_cb)(FOCUS_IS_RELEASED, (const char *)data, g_focus_session_interrupt_info.user_data);
1087 debug_msg("invoked");
1093 static void _session_interrupted_cb(int id, mm_sound_focus_type_e focus_type, mm_sound_focus_state_e state,
1094 const char *reason_for_change, const char *ext_info, void *user_data)
1096 debug_msg("SESSION INTERRUPTED CB is called : id(%d), focus_type(%d), state(%d), reason(%s)", id, focus_type, state, reason_for_change);
1098 if (id != g_focus_session_interrupt_info.watch_cb_id) {
1099 debug_error("id is not valid(param id:%d, g_focus_session_interrupt_watch_cb_id:%d)",
1100 id, g_focus_session_interrupt_info.watch_cb_id);
1103 if (!g_focus_session_interrupt_info.user_cb) {
1104 debug_error("user callback is null");
1108 debug_msg(" >>> invoking session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
1109 if (state == FOCUS_IS_RELEASED)
1110 (g_focus_session_interrupt_info.user_cb)(FOCUS_IS_ACQUIRED, reason_for_change, g_focus_session_interrupt_info.user_data);
1112 debug_msg("INTERRUPTED COMPLETED case, append it to idle");
1113 g_idle_add((GSourceFunc)_interrupted_completed, strdup(reason_for_change));
1115 debug_msg(" <<< session interrupt callback finished");
1118 int mm_sound_client_set_session_interrupt_callback(mm_sound_focus_session_interrupt_cb callback, void* user_data)
1120 int ret = MM_ERROR_NONE;
1125 return MM_ERROR_INVALID_ARGUMENT;
1127 /* add internal focus watch callback */
1128 if (g_focus_session_interrupt_info.watch_cb_id == -1) {
1129 if ((ret = mm_sound_client_set_focus_watch_callback(getpid(), FOCUS_FOR_BOTH, true, true, _session_interrupted_cb, NULL,
1130 &g_focus_session_interrupt_info.watch_cb_id))) {
1131 debug_error("failed to mm_sound_client_set_focus_watch_callback(), ret(0x%x)", ret);
1135 g_focus_session_interrupt_info.user_cb = callback;
1136 g_focus_session_interrupt_info.user_data = user_data;
1142 int mm_sound_client_unset_session_interrupt_callback(void)
1144 int ret = MM_ERROR_NONE;
1148 if (!g_focus_session_interrupt_info.user_cb || g_focus_session_interrupt_info.watch_cb_id == -1) {
1149 debug_error("no callback to unset");
1150 return MM_ERROR_SOUND_INTERNAL;
1153 /* remove internal focus watch callback */
1154 if ((ret = mm_sound_client_unset_focus_watch_callback(g_focus_session_interrupt_info.watch_cb_id))) {
1155 debug_error("failed to mm_sound_client_unset_focus_watch_callback(), id(%d), ret(0x%x)",
1156 g_focus_session_interrupt_info.watch_cb_id, ret);
1159 g_focus_session_interrupt_info.watch_cb_id = -1;
1160 g_focus_session_interrupt_info.user_cb = NULL;
1161 g_focus_session_interrupt_info.user_data = NULL;
1167 static gpointer _focus_thread_func(gpointer data)
1169 unsigned int thread_id = (unsigned int)pthread_self();
1170 GMainLoop *focus_loop = (GMainLoop*)data;
1172 debug_warning(">>> thread func : thread id(%u), mainloop[%p]", thread_id, focus_loop);
1174 g_main_loop_run(focus_loop);
1175 debug_warning("<<< quit thread func : thread id(%u), mainloop[%p]", thread_id, focus_loop);
1180 static gboolean _focus_fd_prepare(GSource *source, gint *timeout)
1183 debug_warning("[ PREPARE : %p, (%p, %d)", source, timeout, timeout? *timeout : -1);
1188 static gboolean _focus_fd_check(GSource * source)
1190 FocusSource* fsource = (FocusSource *)source;
1193 debug_error("GSource is null");
1197 debug_warning("CHECK : %p, 0x%x ]", source, fsource->pollfd.revents);
1199 if (fsource->poll_fd.revents & (POLLIN | POLLPRI))
1205 static gboolean _focus_fd_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
1207 debug_warning("*** DISPATCH : %p, (%p, %p)", source, callback, user_data);
1208 return callback(user_data);
1211 static void _focus_fd_finalize(GSource *source)
1213 debug_warning("### FINALIZE : %p", source);
1216 static int _focus_find_index_by_handle(int handle)
1219 for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1220 if (g_focus_sound_handle[i].focus_callback && handle == g_focus_sound_handle[i].handle) {
1221 /* debug_msg("found index(%d) for handle(%d)", i, handle);*/
1222 return (handle == FOCUS_HANDLE_INIT_VAL)? -1 : i;
1228 static int _focus_watch_find_index_by_handle(int handle)
1231 for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1232 if (g_focus_sound_handle[i].watch_callback && handle == g_focus_sound_handle[i].handle) {
1233 /* debug_msg("found index(%d) for watch handle(%d)", i, handle);*/
1234 return (handle == FOCUS_HANDLE_INIT_VAL)? -1 : i;
1240 static gboolean _focus_callback_handler(gpointer user_data)
1242 focus_sound_info_t *focus_handle = (focus_sound_info_t *)user_data;
1246 focus_cb_data_lib cb_data;
1248 debug_log(">>> _focus_callback_handler(), this thread id(%u)\n", (unsigned int)pthread_self());
1250 if (!focus_handle) {
1251 debug_error("focus_handle is null");
1252 return G_SOURCE_CONTINUE;
1254 poll_fd = &focus_handle->fsrc->poll_fd;
1255 debug_log("focus_handle(%p), poll_fd(%p)", focus_handle, poll_fd);
1257 memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1259 if (poll_fd->revents & (POLLIN | POLLPRI)) {
1260 int changed_state = -1;
1262 count = read(poll_fd->fd, &cb_data, sizeof(cb_data));
1264 char str_error[256];
1265 strerror_r(errno, str_error, sizeof(str_error));
1266 debug_error("GpollFD read fail, errno=%d(%s)",errno, str_error);
1267 return G_SOURCE_CONTINUE;
1269 changed_state = cb_data.state;
1271 g_mutex_lock(&focus_handle->focus_lock);
1273 tid = focus_handle->focus_tid;
1275 if (changed_state != -1) {
1276 debug_msg("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)",
1277 tid, cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type);
1278 if (focus_handle->focus_callback == NULL) {
1279 debug_error("focus callback is null..");
1280 g_mutex_unlock(&focus_handle->focus_lock);
1281 return G_SOURCE_CONTINUE;
1283 debug_msg("[CALLBACK(%p) START]", focus_handle->focus_callback);
1284 (focus_handle->focus_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type,
1285 cb_data.option, cb_data.ext_info, focus_handle->user_data);
1286 debug_msg("[CALLBACK END]");
1288 #ifdef CONFIG_ENABLE_RETCB
1292 unsigned int buf = 0;
1293 char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", focus_handle->focus_tid, cb_data.handle);
1294 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1296 char str_error[256];
1297 strerror_r(errno, str_error, sizeof(str_error));
1298 debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n",
1299 tid, tmpfd, filename2, errno, str_error);
1301 g_mutex_unlock(&focus_handle->focus_lock);
1302 return G_SOURCE_CONTINUE;
1304 /* buf contains data as below,
1305 * |<--12bits--><--4bits (reacquisition)--><--16bits (handle)-->| */
1306 buf = (unsigned int)((0x0000ffff & cb_data.handle) | (focus_handle->auto_reacquire << 16));
1307 rett = write(tmpfd, &buf, sizeof(buf));
1310 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
1315 g_mutex_unlock(&focus_handle->focus_lock);
1319 return G_SOURCE_CONTINUE;
1322 static gboolean _focus_watch_callback_handler(gpointer user_data)
1324 focus_sound_info_t *focus_handle = (focus_sound_info_t *)user_data;
1328 focus_cb_data_lib cb_data;
1330 debug_log(">>> _focus_watch_callback_handler(), this thread id(%u)\n", (unsigned int)pthread_self());
1332 if (!focus_handle) {
1333 debug_error("focus_handle is null");
1334 return G_SOURCE_CONTINUE;
1336 poll_fd = &focus_handle->fsrc->poll_fd;
1337 debug_log("focus_handle(%p), poll_fd(%p)", focus_handle, poll_fd);
1339 memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1341 if (poll_fd->revents & (POLLIN | POLLPRI)) {
1342 count = read(poll_fd->fd, &cb_data, sizeof(cb_data));
1344 char str_error[256];
1345 strerror_r(errno, str_error, sizeof(str_error));
1346 debug_error("GpollFD read fail, errno=%d(%s)",errno, str_error);
1347 return G_SOURCE_CONTINUE;
1350 if (!focus_handle->is_used) {
1351 debug_warning("unsetting watch calllback has been already requested");
1352 goto SKIP_CB_AND_RET;
1355 g_mutex_lock(&focus_handle->focus_lock);
1357 tid = focus_handle->focus_tid;
1359 debug_msg("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)",
1360 tid, cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type);
1362 if (focus_handle->watch_callback == NULL) {
1363 debug_warning("watch callback is null..");
1364 goto SKIP_CB_AND_RET;
1366 if (focus_handle->unset_watch_callback_requested == true) {
1367 debug_warning("unset_watch_callback_requested..");
1368 goto SKIP_CB_AND_RET;
1371 debug_msg("[CALLBACK(%p) START]", focus_handle->watch_callback);
1372 (focus_handle->watch_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type,
1373 cb_data.ext_info, focus_handle->user_data);
1374 debug_msg("[CALLBACK END]");
1377 #ifdef CONFIG_ENABLE_RETCB
1382 char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr", focus_handle->focus_tid, cb_data.handle);
1383 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1385 char str_error[256];
1386 strerror_r(errno, str_error, sizeof(str_error));
1387 debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n",
1388 tid, tmpfd, filename2, errno, str_error);
1390 g_mutex_unlock(&focus_handle->focus_lock);
1391 return G_SOURCE_CONTINUE;
1393 buf = cb_data.handle;
1394 rett = write(tmpfd, &buf, sizeof(buf));
1397 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
1402 if (focus_handle->is_used) {
1403 debug_msg("unlock focus_lock = %p", &focus_handle->focus_lock);
1404 g_mutex_unlock(&focus_handle->focus_lock);
1409 return G_SOURCE_CONTINUE;
1412 static void _focus_open_callback(int index, bool is_for_watching)
1419 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1420 debug_error("Invalid focus handle index [%d]", index);
1424 if (is_for_watching) {
1425 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch",
1426 g_focus_sound_handle[index].focus_tid,
1427 g_focus_sound_handle[index].handle);
1429 filename = g_strdup_printf("/tmp/FOCUS.%d.%d",
1430 g_focus_sound_handle[index].focus_tid,
1431 g_focus_sound_handle[index].handle);
1433 pre_mask = umask(0);
1434 if (mknod(filename, S_IFIFO|0666, 0)) {
1435 debug_error("mknod() failure, errno(%d)", errno);
1438 g_focus_sound_handle[index].focus_fd = open(filename, O_RDWR|O_NONBLOCK);
1439 if (g_focus_sound_handle[index].focus_fd == -1) {
1440 debug_error("Open fail : index(%d), file open error(%d)", index, errno);
1442 debug_log("Open success : index(%d), filename(%s), fd(%d)",
1443 index, filename, g_focus_sound_handle[index].focus_fd);
1448 #ifdef CONFIG_ENABLE_RETCB
1451 if (is_for_watching) {
1452 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr",
1453 g_focus_sound_handle[index].focus_tid,
1454 g_focus_sound_handle[index].handle);
1456 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr",
1457 g_focus_sound_handle[index].focus_tid,
1458 g_focus_sound_handle[index].handle);
1460 pre_mask = umask(0);
1461 if (mknod(filename2, S_IFIFO | 0666, 0)) {
1462 debug_error("mknod() failure, errno(%d)", errno);
1472 void _focus_close_callback(int index, bool is_for_watching)
1474 char *filename = NULL;
1478 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1479 debug_error("Invalid focus handle index [%d]", index);
1483 if (g_focus_sound_handle[index].focus_fd < 0) {
1484 debug_error("Close fail : index(%d)", index);
1486 close(g_focus_sound_handle[index].focus_fd);
1487 debug_log("Close Success : index(%d)", index);
1490 if (is_for_watching) {
1491 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch",
1492 g_focus_sound_handle[index].focus_tid,
1493 g_focus_sound_handle[index].handle);
1495 filename = g_strdup_printf("/tmp/FOCUS.%d.%d",
1496 g_focus_sound_handle[index].focus_tid,
1497 g_focus_sound_handle[index].handle);
1499 if (remove(filename)) {
1500 debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)",
1506 #ifdef CONFIG_ENABLE_RETCB
1509 if (is_for_watching) {
1510 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr",
1511 g_focus_sound_handle[index].focus_tid,
1512 g_focus_sound_handle[index].handle);
1514 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr",
1515 g_focus_sound_handle[index].focus_tid,
1516 g_focus_sound_handle[index].handle);
1518 if (remove(filename2)) {
1519 debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)",
1530 static GSourceFuncs event_funcs = {
1531 .prepare = _focus_fd_prepare,
1532 .check = _focus_fd_check,
1533 .dispatch = _focus_fd_dispatch,
1534 .finalize = _focus_fd_finalize,
1537 static bool _focus_add_sound_callback(int index, focus_callback_handler_t focus_cb_handler)
1539 FocusSource *fsrc = NULL;
1540 GSource *src = NULL;
1545 g_mutex_init(&g_focus_sound_handle[index].focus_lock);
1547 src = g_source_new(&event_funcs, sizeof(FocusSource));
1549 debug_error("failed to g_source_new for focus source");
1553 fsrc = (FocusSource*) src;
1555 fsrc->poll_fd.fd = g_focus_sound_handle[index].focus_fd;
1556 fsrc->poll_fd.events = (gushort)(POLLIN | POLLPRI);
1557 g_source_add_poll(src, &fsrc->poll_fd);
1559 g_source_set_callback(src, focus_cb_handler, (gpointer)&g_focus_sound_handle[index], NULL);
1561 debug_warning("fsrc(%p), src_funcs(%p), pollfd(%p), fd(%d)",
1562 fsrc, &event_funcs, &fsrc->poll_fd, fsrc->poll_fd.fd);
1564 fsrc_id = g_source_attach(src, g_main_loop_get_context(g_focus_sound_handle[index].focus_loop));
1566 debug_error("failed to attach the source to context");
1569 g_source_unref(src);
1571 g_focus_sound_handle[index].fsrc = fsrc;
1578 g_source_unref(src);
1583 static bool _focus_remove_sound_callback(int index)
1585 focus_sound_info_t *h = NULL;
1589 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1590 debug_error("Invalid focus handle index [%d]", index);
1594 h = &g_focus_sound_handle[index];
1596 g_source_destroy((GSource *)h->fsrc);
1600 h->focus_callback = NULL;
1601 h->watch_callback = NULL;
1603 g_mutex_clear(&h->focus_lock);
1610 static void _focus_add_callback(int index, bool is_for_watching)
1614 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1615 debug_error("Invalid focus handle index [%d]", index);
1619 if (!is_for_watching) {
1620 if (!_focus_add_sound_callback(index, _focus_callback_handler))
1621 debug_error("failed to _focus_add_sound_callback(%p)", _focus_callback_handler);
1622 } else { // need to check if it's necessary
1623 if (!_focus_add_sound_callback(index, _focus_watch_callback_handler))
1624 debug_error("failed to _focus_add_sound_callback(%p)", _focus_watch_callback_handler);
1629 static void _focus_remove_callback(int index)
1632 if (!_focus_remove_sound_callback(index))
1633 debug_error("failed to __focus_remove_sound_callback()");
1637 static void _focus_init_callback(int index, bool is_for_watching)
1640 _focus_open_callback(index, is_for_watching);
1641 _focus_add_callback(index, is_for_watching);
1645 static void _focus_deinit_callback(int index, bool is_for_watching)
1648 _focus_remove_callback(index);
1649 _focus_close_callback(index, is_for_watching);
1653 #define INTERVAL_MS 20
1654 static int _focus_loop_is_running_timed_wait(GMainLoop *focus_loop, int timeout_ms)
1656 int reduced_time_ms = timeout_ms;
1657 if (!focus_loop || timeout_ms < 0) {
1658 debug_error("invalid argument, focus_loop(%p), timeout_ms(%d)", focus_loop, timeout_ms);
1659 return MM_ERROR_INVALID_ARGUMENT;
1663 if (g_main_loop_is_running(focus_loop))
1664 return MM_ERROR_NONE;
1666 usleep(INTERVAL_MS * 1000);
1667 if (reduced_time_ms < timeout_ms)
1668 debug_warning("reduced_time_ms(%d)", reduced_time_ms);
1669 } while ((reduced_time_ms -= INTERVAL_MS) >= 0);
1671 debug_error("focus_loop is not running for timeout_ms(%d), focus_loop(%p) ", timeout_ms, focus_loop);
1673 return MM_ERROR_SOUND_INTERNAL;
1676 #define LOOP_RUNNING_WAIT_TIME_MS 200
1677 static int _focus_init_context(int index)
1679 int ret = MM_ERROR_NONE;
1680 GMainContext *focus_context;
1684 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1685 debug_error("index(%d) is not valid", index);
1686 return MM_ERROR_INVALID_ARGUMENT;
1689 focus_context = g_main_context_new();
1690 g_focus_sound_handle[index].focus_loop = g_main_loop_new(focus_context, FALSE);
1691 g_main_context_unref(focus_context);
1692 if (g_focus_sound_handle[index].focus_loop == NULL) {
1693 debug_error("could not create mainloop..");
1697 g_focus_sound_handle[index].focus_cb_thread = g_thread_new("focus-cb-thread",
1699 g_focus_sound_handle[index].focus_loop);
1700 if (g_focus_sound_handle[index].focus_cb_thread == NULL) {
1701 debug_error("could not create thread..");
1705 debug_warning("focus cb thread[%p] with mainloop[%p] is created for index(%d)",
1706 g_focus_sound_handle[index].focus_cb_thread, g_focus_sound_handle[index].focus_loop, index);
1708 if ((ret = _focus_loop_is_running_timed_wait(g_focus_sound_handle[index].focus_loop, LOOP_RUNNING_WAIT_TIME_MS))) {
1709 debug_error("failed to _focus_loop_is_running_timed_wait(), ret[0x%x]", ret);
1715 return MM_ERROR_NONE;
1718 if (g_focus_sound_handle[index].focus_loop) {
1719 g_main_loop_unref(g_focus_sound_handle[index].focus_loop);
1720 g_focus_sound_handle[index].focus_loop = NULL;
1722 return MM_ERROR_SOUND_INTERNAL;
1725 static void _focus_deinit_context(int index)
1729 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1730 debug_error("index(%d) is not valid", index);
1734 if (!g_focus_sound_handle[index].focus_loop || !g_focus_sound_handle[index].focus_cb_thread) {
1735 debug_error("focus_loop[%p] or focus_cb_thread[%p] is null",
1736 g_focus_sound_handle[index].focus_loop, g_focus_sound_handle[index].focus_cb_thread);
1740 g_main_loop_quit(g_focus_sound_handle[index].focus_loop);
1741 g_thread_join(g_focus_sound_handle[index].focus_cb_thread);
1742 debug_warning("after thread join, thread[%p], mainloop[%p] for index(%d)",
1743 g_focus_sound_handle[index].focus_cb_thread, g_focus_sound_handle[index].focus_loop, index);
1744 g_main_loop_unref(g_focus_sound_handle[index].focus_loop);
1745 g_focus_sound_handle[index].focus_loop = NULL;
1746 g_focus_sound_handle[index].focus_cb_thread = NULL;
1751 int mm_sound_client_get_unique_id(int *id)
1753 int ret = MM_ERROR_NONE;
1758 ret = MM_ERROR_INVALID_ARGUMENT;
1760 ret = mm_sound_proxy_get_unique_id(id);
1767 int mm_sound_client_is_focus_cb_thread(GThread *mine, bool *result)
1769 int ret = MM_ERROR_NONE;
1772 if (!mine || !result)
1773 ret = MM_ERROR_INVALID_ARGUMENT;
1776 for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1777 if (!g_focus_sound_handle[i].is_used)
1779 if (g_focus_sound_handle[i].focus_cb_thread == mine) {
1789 int mm_sound_client_register_focus(int id, int pid, const char *stream_type, bool is_for_session,
1790 mm_sound_focus_changed_cb callback, void* user_data)
1792 int ret = MM_ERROR_NONE;
1797 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1801 for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
1802 if (g_focus_sound_handle[index].is_used == false) {
1803 g_focus_sound_handle[index].is_used = true;
1808 g_focus_sound_handle[index].focus_tid = instance;
1809 g_focus_sound_handle[index].handle = id;
1810 g_focus_sound_handle[index].focus_callback = callback;
1811 g_focus_sound_handle[index].user_data = user_data;
1812 g_focus_sound_handle[index].is_for_session = is_for_session;
1813 g_focus_sound_handle[index].auto_reacquire = true;
1815 ret = mm_sound_proxy_register_focus(id, pid, stream_type, callback, is_for_session, user_data);
1816 if (ret == MM_ERROR_NONE) {
1817 debug_msg("[Client] Success to register focus\n");
1818 g_need_emergent_exit = TRUE;
1819 if (_focus_init_context(index)) {
1820 ret = MM_ERROR_SOUND_INTERNAL;
1824 debug_error("[Client] Error occurred : 0x%x \n",ret);
1828 _focus_init_callback(index, false);
1832 g_focus_sound_handle[index].is_used = false;
1835 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1841 int mm_sound_client_unregister_focus(int id)
1843 int ret = MM_ERROR_NONE;
1848 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1850 index = _focus_find_index_by_handle(id);
1852 debug_error("Could not find index");
1853 ret = MM_ERROR_INVALID_ARGUMENT;
1856 instance = g_focus_sound_handle[index].focus_tid;
1858 if (!g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1859 debug_warning("maybe focus_callback is being called, try one more time..");
1860 usleep(2500000); // 2.5 sec
1861 if (g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1862 debug_msg("finally got focus_lock");
1866 ret = mm_sound_proxy_unregister_focus(instance, id, g_focus_sound_handle[index].is_for_session);
1867 if (ret == MM_ERROR_NONE)
1868 debug_msg("[Client] Success to unregister focus\n");
1870 debug_error("[Client] Error occurred : 0x%x \n",ret);
1872 g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
1874 _focus_deinit_callback(index, false);
1875 g_focus_sound_handle[index].focus_fd = 0;
1876 g_focus_sound_handle[index].focus_tid = 0;
1877 g_focus_sound_handle[index].handle = 0;
1878 g_focus_sound_handle[index].is_used = false;
1879 _focus_deinit_context(index);
1883 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1888 int mm_sound_client_set_focus_reacquisition(int id, bool reacquisition, bool is_for_session)
1890 int ret = MM_ERROR_NONE;
1896 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1898 index = _focus_find_index_by_handle(id);
1900 debug_error("Could not find index");
1901 ret = MM_ERROR_INVALID_ARGUMENT;
1904 instance = g_focus_sound_handle[index].focus_tid;
1906 ret = mm_sound_client_is_focus_cb_thread(g_thread_self(), &result);
1908 debug_error("[Client] mm_sound_client_is_focus_cb_thread failed");
1910 } else if (!result) {
1911 ret = mm_sound_proxy_set_focus_reacquisition(instance, id, reacquisition, is_for_session);
1912 if (ret == MM_ERROR_NONE) {
1913 debug_msg("[Client] Success to set focus reacquisition to [%d]\n", reacquisition);
1915 debug_error("[Client] Error occurred : 0x%x \n",ret);
1919 debug_warning("[Client] Inside the focus cb thread, set focus reacquisition to [%d]\n", reacquisition);
1922 g_focus_sound_handle[index].auto_reacquire = reacquisition;
1923 debug_msg("[Client] set focus reacquisition(%d) for id(%d)\n", reacquisition, id);
1926 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1931 int mm_sound_client_get_focus_reacquisition(int id, bool *reacquisition)
1933 int ret = MM_ERROR_NONE;
1938 if (!reacquisition) {
1939 debug_error("Invalid parameter");
1940 return MM_ERROR_INVALID_ARGUMENT;
1943 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1945 index = _focus_find_index_by_handle(id);
1947 debug_error("Could not find index");
1948 ret = MM_ERROR_INVALID_ARGUMENT;
1952 *reacquisition = g_focus_sound_handle[index].auto_reacquire;
1953 debug_msg("[Client] get focus reacquisition(%d) for id(%d)\n", *reacquisition, id);
1956 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1961 int mm_sound_client_get_acquired_focus_stream_type(int focus_type, char **stream_type, int *option, char **ext_info)
1963 int ret = MM_ERROR_NONE;
1964 char *stream_type_str = NULL;
1965 char *ext_info_str = NULL;
1969 ret = mm_sound_proxy_get_acquired_focus_stream_type(focus_type, &stream_type_str, option, &ext_info_str);
1970 if (ret == MM_ERROR_NONE) {
1971 debug_msg("[Client] Success to get stream type of acquired focus, stream_type(%s), ext_info(%s)\n",
1972 stream_type_str, ext_info_str);
1973 *stream_type = strdup(stream_type_str);
1974 *ext_info = strdup(ext_info_str);
1975 g_free(stream_type_str);
1976 g_free(ext_info_str);
1978 debug_error("[Client] Error occurred : 0x%x \n",ret);
1985 int mm_sound_client_acquire_focus(int id, mm_sound_focus_type_e type, int option, const char *ext_info)
1987 int ret = MM_ERROR_NONE;
1992 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1994 index = _focus_find_index_by_handle(id);
1996 debug_error("Could not find index");
1997 ret = MM_ERROR_INVALID_ARGUMENT;
2000 instance = g_focus_sound_handle[index].focus_tid;
2002 ret = mm_sound_proxy_acquire_focus(instance, id, type, option, ext_info, g_focus_sound_handle[index].is_for_session);
2003 if (ret == MM_ERROR_NONE)
2004 debug_msg("[Client] Success to acquire focus\n");
2006 debug_error("[Client] Error occurred : 0x%x \n",ret);
2009 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2014 int mm_sound_client_release_focus(int id, mm_sound_focus_type_e type, int option, const char *ext_info)
2016 int ret = MM_ERROR_NONE;
2021 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2023 index = _focus_find_index_by_handle(id);
2025 debug_error("Could not find index");
2026 ret = MM_ERROR_INVALID_ARGUMENT;
2029 instance = g_focus_sound_handle[index].focus_tid;
2031 ret = mm_sound_proxy_release_focus(instance, id, type, option, ext_info, g_focus_sound_handle[index].is_for_session);
2032 if (ret == MM_ERROR_NONE)
2033 debug_msg("[Client] Success to release focus\n");
2035 debug_error("[Client] Error occurred : 0x%x \n",ret);
2038 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2043 int mm_sound_client_update_stream_focus_status(int id, unsigned int status)
2045 int ret = MM_ERROR_NONE;
2048 if ((ret = mm_sound_proxy_update_stream_focus_status(id, status)) != MM_ERROR_NONE)
2049 debug_error("[Client] failed to update stream focus status, ret[0x%x]", ret);
2055 int mm_sound_client_set_focus_watch_callback(int pid, mm_sound_focus_type_e focus_type, bool is_for_session, bool is_for_monitor,
2056 mm_sound_focus_changed_watch_cb callback, void* user_data, int *id)
2058 int ret = MM_ERROR_NONE;
2065 return MM_ERROR_INVALID_ARGUMENT;
2067 //pthread_mutex_lock(&g_thread_mutex2);
2071 ret = mm_sound_proxy_get_unique_id(id);
2075 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2077 for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
2078 if (g_focus_sound_handle[index].is_used == false) {
2079 g_focus_sound_handle[index].is_used = true;
2084 g_focus_sound_handle[index].focus_tid = instance;
2085 g_focus_sound_handle[index].handle = *id;
2086 g_focus_sound_handle[index].watch_callback = callback;
2087 g_focus_sound_handle[index].user_data = user_data;
2088 g_focus_sound_handle[index].is_for_session = is_for_session;
2089 g_focus_sound_handle[index].is_for_monitor = is_for_monitor;
2090 g_focus_sound_handle[index].unset_watch_callback_requested = false;
2092 ret = mm_sound_proxy_set_focus_watch_callback(pid, g_focus_sound_handle[index].handle, focus_type,
2093 is_for_session, is_for_monitor, callback, user_data);
2095 if (ret == MM_ERROR_NONE) {
2096 debug_msg("[Client] Success to watch focus");
2097 g_need_emergent_exit = TRUE;
2098 if (_focus_init_context(index)) {
2099 ret = MM_ERROR_SOUND_INTERNAL;
2103 debug_error("[Client] Error occurred : 0x%x",ret);
2107 _focus_init_callback(index, true);
2111 g_focus_sound_handle[index].is_used = false;
2114 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2119 int mm_sound_client_request_unset_focus_watch_callback(int id)
2121 int ret = MM_ERROR_NONE;
2125 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2127 index = _focus_watch_find_index_by_handle(id);
2129 debug_error("Could not find index");
2130 ret = MM_ERROR_INVALID_ARGUMENT;
2133 g_focus_sound_handle[index].unset_watch_callback_requested = true;
2136 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2141 int mm_sound_client_unset_focus_watch_callback(int id)
2143 int ret = MM_ERROR_NONE;
2147 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2149 index = _focus_watch_find_index_by_handle(id);
2151 debug_error("Could not find index");
2152 ret = MM_ERROR_INVALID_ARGUMENT;
2156 g_mutex_lock(&g_focus_sound_handle[index].focus_lock);
2158 g_focus_sound_handle[index].is_used = false;
2160 ret = mm_sound_proxy_unset_focus_watch_callback(g_focus_sound_handle[index].focus_tid,
2161 g_focus_sound_handle[index].handle,
2162 g_focus_sound_handle[index].is_for_session);
2164 if (ret == MM_ERROR_NONE)
2165 debug_msg("[Client] Success to unwatch focus\n");
2167 debug_error("[Client] Error occurred : 0x%x \n",ret);
2170 g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
2172 _focus_deinit_callback(index, true);
2173 g_focus_sound_handle[index].focus_fd = 0;
2174 g_focus_sound_handle[index].focus_tid = 0;
2175 g_focus_sound_handle[index].handle = 0;
2176 _focus_deinit_context(index);
2179 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2184 static gboolean _idle_event_callback(void *data)
2186 focus_idle_event_t *idle_event_data = (focus_idle_event_t*)data;
2187 int ret = MM_ERROR_NONE;
2190 debug_error("data is null");
2194 debug_msg("idle_event_data(%p): type(%d), data(%d)",
2195 idle_event_data, idle_event_data->type, idle_event_data->data);
2197 switch (idle_event_data->type) {
2198 case IDLE_EVENT_TYPE_UNSET_FOCUS_WATCH_CB:
2199 if ((ret = mm_sound_client_unset_focus_watch_callback(idle_event_data->data)))
2200 debug_error("Could not unset focus watch callback, id(%d), ret = %x\n", idle_event_data->data, ret);
2202 case IDLE_EVENT_TYPE_UNREGISTER_FOCUS:
2203 if ((ret = mm_sound_client_unregister_focus(idle_event_data->data)))
2204 debug_error("Could not unregister focus, id(%d), ret = %x\n", idle_event_data->data, ret);
2207 debug_warning("invalid type(%d)", idle_event_data->type);
2211 g_free(idle_event_data);
2213 g_idle_event_src = 0;
2215 MMSOUND_LEAVE_CRITICAL_SECTION(&g_event_mutex);
2220 int mm_sound_client_execute_focus_func_in_main_context(focus_idle_event_type_e type, int data)
2222 focus_idle_event_t *idle_event_data = NULL;
2224 if (IDLE_EVENT_TYPE_UNSET_FOCUS_WATCH_CB > type || IDLE_EVENT_TYPE_MAX < type)
2225 return MM_ERROR_INVALID_ARGUMENT;
2227 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_event_mutex, MM_ERROR_SOUND_INTERNAL);
2229 idle_event_data = g_new0(focus_idle_event_t, 1);
2230 idle_event_data->type = type;
2231 idle_event_data->data = data;
2233 g_idle_event_src = g_idle_add_full(G_PRIORITY_HIGH,
2234 (GSourceFunc)_idle_event_callback,
2235 (gpointer)idle_event_data,
2238 return MM_ERROR_NONE;
2243 int mm_sound_client_add_test_callback(mm_sound_test_cb func, void* user_data, unsigned int *subs_id)
2245 int ret = MM_ERROR_NONE;
2249 ret = mm_sound_proxy_add_test_callback(func, user_data, g_free, subs_id);
2255 int mm_sound_client_remove_test_callback(unsigned int subs_id)
2257 int ret = MM_ERROR_NONE;
2260 ret = mm_sound_proxy_remove_test_callback(subs_id);
2267 int mm_sound_client_test(int a, int b, int* getv)
2269 int ret = MM_ERROR_NONE;
2273 ret = mm_sound_proxy_test(a, b, getv);
2274 debug_log("%d * %d -> result : %d", a, b, *getv);