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 int mm_sound_client_get_device_by_id(int device_id, mm_sound_device_t **device)
609 int ret = MM_ERROR_NONE;
613 if ((ret = mm_sound_proxy_get_device_by_id(device_id, device)) != MM_ERROR_NONE)
614 debug_error("[Client] failed to get device by id");
621 static bool device_is_match_direction(int direction, int mask)
623 if (mask == DEVICE_IO_DIRECTION_FLAGS || mask == 0)
626 if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_IN_FLAG) && (direction & MM_SOUND_DEVICE_IO_DIRECTION_IN))
628 if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_OUT_FLAG) && (direction & MM_SOUND_DEVICE_IO_DIRECTION_OUT))
630 if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_BOTH_FLAG) && (direction == MM_SOUND_DEVICE_IO_DIRECTION_BOTH))
636 static bool device_is_match_state(int state, int mask)
638 if (mask == DEVICE_STATE_FLAGS || mask == 0)
641 if ((mask & MM_SOUND_DEVICE_STATE_DEACTIVATED_FLAG) && (state == MM_SOUND_DEVICE_STATE_DEACTIVATED))
643 if ((mask & MM_SOUND_DEVICE_STATE_ACTIVATED_FLAG) && (state == MM_SOUND_DEVICE_STATE_ACTIVATED))
649 static bool device_is_match_type(const char *type, int mask)
652 const char *builtin_prefix = "builtin";
654 if (mask == DEVICE_TYPE_FLAGS || mask == 0)
657 is_builtin = !strncmp(type, builtin_prefix, strlen(builtin_prefix));
659 if ((mask & MM_SOUND_DEVICE_TYPE_INTERNAL_FLAG) && (is_builtin))
661 if ((mask & MM_SOUND_DEVICE_TYPE_EXTERNAL_FLAG) && (!is_builtin))
667 static bool device_is_match_with_mask(const char *type, int direction, int state, int mask)
669 if (mask == DEVICE_ALL_FLAG)
672 return (device_is_match_direction(direction, mask & DEVICE_IO_DIRECTION_FLAGS) &&
673 device_is_match_state(state, mask & DEVICE_STATE_FLAGS) &&
674 device_is_match_type(type, mask & DEVICE_TYPE_FLAGS));
677 static int _fill_sound_device(mm_sound_device_t *device_h, int device_id, const char *device_type,
678 int direction, int state, const char *name, int *stream_id, int stream_num)
682 if (stream_num > 0 && stream_id == NULL) {
683 debug_error("stream_num is %d, but stream_id is NULL", stream_num);
687 if (stream_num > MAX_STREAM_ON_DEVICE) {
688 debug_error("too many streams on this device");
692 device_h->id = device_id;
693 device_h->io_direction = direction;
694 device_h->state = state;
695 MMSOUND_STRNCPY(device_h->name, name, MAX_DEVICE_NAME_NUM);
696 MMSOUND_STRNCPY(device_h->type, device_type, MAX_DEVICE_TYPE_STR_LEN);
698 if (stream_num > 0) {
699 device_h->stream_num = stream_num;
700 debug_log("%d streams on this device", stream_num);
701 for (i = 0; i < stream_num; i++) {
702 debug_log(" stream_id : %d", stream_id[i]);
703 device_h->stream_id[i] = stream_id[i];
706 device_h->stream_num = 0;
707 debug_log("There is no stream on this device");
713 static void _mm_sound_device_connected_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
714 int state, const char *name, int *stream_id, int stream_num,
715 gboolean is_connected, void *userdata)
717 mm_sound_device_t device_h;
718 struct callback_data *cb_data = (struct callback_data*) userdata;
721 debug_log("[Device %s] id(%d) type(%s) direction(%d) state(%d) name(%s)",
722 is_connected ? "Connected" : "Disconnected", device_id, device_type, io_direction,
723 state, name, is_connected);
725 if (cb_data == NULL) {
726 debug_warning("device connected changed callback data null");
730 device_flags = (int) cb_data->extra_data;
731 if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
734 if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name, stream_id, stream_num) < 0) {
735 debug_error("Failed to fill sound device");
739 ((mm_sound_device_connected_cb)(cb_data->user_cb))(&device_h, is_connected, cb_data->user_data);
742 int mm_sound_client_add_device_connected_callback(int device_flags, mm_sound_device_connected_cb func,
743 void* userdata, unsigned int *subs_id)
745 int ret = MM_ERROR_NONE;
746 struct callback_data *cb_data = NULL;
750 GET_CB_DATA(cb_data, func, userdata, (void*) device_flags);
752 ret = mm_sound_proxy_add_device_connected_callback(device_flags,
753 _mm_sound_device_connected_callback_wrapper_func,
754 cb_data, g_free, subs_id);
755 if (ret == MM_ERROR_NONE)
756 g_need_emergent_exit = TRUE;
762 int mm_sound_client_remove_device_connected_callback(unsigned int subs_id)
764 int ret = MM_ERROR_NONE;
767 ret = mm_sound_proxy_remove_device_connected_callback(subs_id);
773 static void _mm_sound_device_info_changed_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
774 int state, const char *name, int *stream_id, int stream_num,
775 int changed_device_info_type, void *userdata)
777 mm_sound_device_t device_h;
778 struct callback_data *cb_data = (struct callback_data*) userdata;
781 debug_log("[Device Info Changed] id(%d) type(%s) direction(%d) state(%d) name(%s) changed_info_type(%d)",
782 device_id, device_type, io_direction, state, name, changed_device_info_type);
784 if (cb_data == NULL) {
785 debug_warning("device info changed callback data null");
789 device_flags = (int) cb_data->extra_data;
790 if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
793 if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name, stream_id, stream_num) < 0) {
794 debug_error("Failed to fill sound device");
798 ((mm_sound_device_info_changed_cb)(cb_data->user_cb))(&device_h, changed_device_info_type, cb_data->user_data);
801 int mm_sound_client_add_device_info_changed_callback(int device_flags, mm_sound_device_info_changed_cb func,
802 void *userdata, unsigned int *subs_id)
804 int ret = MM_ERROR_NONE;
805 struct callback_data *cb_data = (struct callback_data*) userdata;
809 GET_CB_DATA(cb_data, func, userdata, (void *) device_flags);
811 ret = mm_sound_proxy_add_device_info_changed_callback(device_flags,
812 _mm_sound_device_info_changed_callback_wrapper_func,
813 cb_data, g_free, subs_id);
819 int mm_sound_client_remove_device_info_changed_callback(unsigned int subs_id)
821 int ret = MM_ERROR_NONE;
824 ret = mm_sound_proxy_remove_device_info_changed_callback(subs_id);
831 static void _mm_sound_device_state_changed_callback_wrapper_func(int device_id, const char *device_type,
832 int io_direction, int state, const char *name,
833 int *stream_id, int stream_num, void *userdata)
835 mm_sound_device_t device_h;
836 struct callback_data *cb_data = (struct callback_data*) userdata;
839 debug_log("[Device State Changed] id(%d) type(%s) direction(%d) state(%d) name(%s)",
840 device_id, device_type, io_direction, state, name);
842 if (cb_data == NULL) {
843 debug_warning("device state changed callback data null");
847 device_flags = (int) cb_data->extra_data;
849 if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
852 if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name, stream_id, stream_num) < 0) {
853 debug_error("Failed to fill sound device");
857 ((mm_sound_device_state_changed_cb)(cb_data->user_cb))(&device_h, state, cb_data->user_data);
860 int mm_sound_client_add_device_state_changed_callback(int device_flags, mm_sound_device_state_changed_cb func,
861 void *userdata, unsigned int *id)
863 int ret = MM_ERROR_NONE;
864 struct callback_data *cb_data = (struct callback_data*) userdata;
868 GET_CB_DATA(cb_data, func, userdata, (void *) device_flags);
870 ret = mm_sound_proxy_add_device_state_changed_callback(device_flags,
871 _mm_sound_device_state_changed_callback_wrapper_func,
872 cb_data, g_free, id);
878 int mm_sound_client_remove_device_state_changed_callback(unsigned int id)
880 int ret = MM_ERROR_NONE;
883 ret = mm_sound_proxy_remove_device_state_changed_callback(id);
889 int mm_sound_client_is_stream_on_device(int stream_id, int device_id, bool *is_on)
891 int ret = MM_ERROR_NONE;
895 debug_error("Invalid Parameter");
896 ret = MM_ERROR_COMMON_INVALID_ARGUMENT;
900 if ((ret = mm_sound_proxy_is_stream_on_device(stream_id, device_id, is_on)) != MM_ERROR_NONE) {
901 debug_error("[Client] failed to query is stream on device, ret[0x%x]", ret);
910 int __convert_volume_type_to_str(int volume_type, char **volume_type_str)
912 int ret = MM_ERROR_NONE;
914 if (!volume_type_str) {
915 return MM_ERROR_COMMON_INVALID_ARGUMENT;
918 switch (volume_type) {
919 case VOLUME_TYPE_SYSTEM:
920 *volume_type_str = "system";
922 case VOLUME_TYPE_NOTIFICATION:
923 *volume_type_str = "notification";
925 case VOLUME_TYPE_ALARM:
926 *volume_type_str = "alarm";
928 case VOLUME_TYPE_RINGTONE:
929 *volume_type_str = "ringtone";
931 case VOLUME_TYPE_MEDIA:
932 *volume_type_str = "media";
934 case VOLUME_TYPE_CALL:
935 *volume_type_str = "call";
937 case VOLUME_TYPE_VOIP:
938 *volume_type_str = "voip";
940 case VOLUME_TYPE_VOICE:
941 *volume_type_str = "voice";
944 if (!strncmp(*volume_type_str,"", VOLUME_TYPE_LEN)) {
945 debug_error("could not find the volume_type[%d] in this switch case statement", volume_type);
946 ret = MM_ERROR_SOUND_INTERNAL;
948 debug_log("volume_type[%s]", *volume_type_str);
953 static int __convert_volume_type_to_int(const char *volume_type_str, volume_type_t *volume_type)
955 int ret = MM_ERROR_NONE;
957 if (!volume_type || !volume_type_str) {
958 return MM_ERROR_COMMON_INVALID_ARGUMENT;
961 if (!strncmp(volume_type_str, "system", VOLUME_TYPE_LEN)) {
962 *volume_type = VOLUME_TYPE_SYSTEM;
963 } else if (!strncmp(volume_type_str, "notification", VOLUME_TYPE_LEN)) {
964 *volume_type = VOLUME_TYPE_NOTIFICATION;
965 } else if (!strncmp(volume_type_str, "alarm", VOLUME_TYPE_LEN)) {
966 *volume_type = VOLUME_TYPE_ALARM;
967 } else if (!strncmp(volume_type_str, "ringtone", VOLUME_TYPE_LEN)) {
968 *volume_type = VOLUME_TYPE_RINGTONE;
969 } else if (!strncmp(volume_type_str, "media", VOLUME_TYPE_LEN)) {
970 *volume_type = VOLUME_TYPE_MEDIA;
971 } else if (!strncmp(volume_type_str, "call", VOLUME_TYPE_LEN)) {
972 *volume_type = VOLUME_TYPE_CALL;
973 } else if (!strncmp(volume_type_str, "voip", VOLUME_TYPE_LEN)) {
974 *volume_type = VOLUME_TYPE_VOIP;
975 } else if (!strncmp(volume_type_str, "voice", VOLUME_TYPE_LEN)) {
976 *volume_type = VOLUME_TYPE_VOICE;
978 debug_log("Invalid volume type : [%s]", volume_type_str);
979 ret = MM_ERROR_SOUND_INTERNAL;
985 int mm_sound_client_set_volume_by_type(const int volume_type, const unsigned int volume_level)
987 int ret = MM_ERROR_NONE;
988 char *type_str = NULL;
991 if ((ret = __convert_volume_type_to_str(volume_type, &type_str)) != MM_ERROR_NONE) {
992 debug_error("volume type convert failed");
996 ret = mm_sound_proxy_set_volume_by_type(type_str, volume_level);
1003 static void _mm_sound_volume_changed_callback_wrapper_func(const char *direction, const char *volume_type_str,
1004 int volume_level, void *userdata)
1006 volume_type_t volume_type = 0;
1007 struct callback_data *cb_data = (struct callback_data *) userdata;
1009 debug_log("[Wrapper CB][Volume Changed] direction : %s, volume_type : %s, volume_level : %d",
1010 direction, volume_type_str, volume_level);
1012 if (cb_data == NULL) {
1013 debug_warning("volume changed callback data null");
1017 if (__convert_volume_type_to_int(volume_type_str, &volume_type) != MM_ERROR_NONE) {
1018 debug_error("volume type convert failed");
1021 debug_log("Call volume changed user cb, direction : %s, vol_type : %s(%d), level : %u",
1022 direction, volume_type_str, volume_type, volume_level);
1023 ((mm_sound_volume_changed_cb)(cb_data->user_cb))(volume_type, volume_level, cb_data->user_data);
1026 int mm_sound_client_add_volume_changed_callback(mm_sound_volume_changed_cb func, void* userdata, unsigned int *subs_id)
1028 int ret = MM_ERROR_NONE;
1029 struct callback_data *cb_data = NULL;
1033 GET_CB_DATA(cb_data, func, userdata, NULL);
1035 ret = mm_sound_proxy_add_volume_changed_callback(_mm_sound_volume_changed_callback_wrapper_func, cb_data, g_free, subs_id);
1042 int mm_sound_client_remove_volume_changed_callback(unsigned int subs_id)
1044 int ret = MM_ERROR_NONE;
1047 ret = mm_sound_proxy_remove_volume_changed_callback(subs_id);
1053 int mm_sound_client_set_filter_by_type(const char *stream_type, const char *filter_name, const char *filter_parameters, const char *filter_group)
1055 int ret = MM_ERROR_NONE;
1058 ret = mm_sound_proxy_set_filter_by_type(stream_type, filter_name, filter_parameters, filter_group);
1064 int mm_sound_client_unset_filter_by_type(const char *stream_type)
1066 int ret = MM_ERROR_NONE;
1069 ret = mm_sound_proxy_unset_filter_by_type(stream_type);
1075 int mm_sound_client_control_filter_by_type(const char *stream_type, const char *filter_name, const char *filter_controls)
1077 int ret = MM_ERROR_NONE;
1080 ret = mm_sound_proxy_control_filter_by_type(stream_type, filter_name, filter_controls);
1087 static gboolean _interrupted_completed(gpointer *data)
1090 debug_error("data is null");
1093 if (!g_focus_session_interrupt_info.user_cb) {
1094 debug_error("user_cb is null");
1099 debug_msg("invoke user_cb(%p)", g_focus_session_interrupt_info.user_cb);
1100 (g_focus_session_interrupt_info.user_cb)(FOCUS_IS_RELEASED, (const char *)data, g_focus_session_interrupt_info.user_data);
1101 debug_msg("invoked");
1107 static void _session_interrupted_cb(int id, mm_sound_focus_type_e focus_type, mm_sound_focus_state_e state,
1108 const char *reason_for_change, const char *ext_info, void *user_data)
1110 debug_msg("SESSION INTERRUPTED CB is called : id(%d), focus_type(%d), state(%d), reason(%s)", id, focus_type, state, reason_for_change);
1112 if (id != g_focus_session_interrupt_info.watch_cb_id) {
1113 debug_error("id is not valid(param id:%d, g_focus_session_interrupt_watch_cb_id:%d)",
1114 id, g_focus_session_interrupt_info.watch_cb_id);
1117 if (!g_focus_session_interrupt_info.user_cb) {
1118 debug_error("user callback is null");
1122 debug_msg(" >>> invoking session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
1123 if (state == FOCUS_IS_RELEASED)
1124 (g_focus_session_interrupt_info.user_cb)(FOCUS_IS_ACQUIRED, reason_for_change, g_focus_session_interrupt_info.user_data);
1126 debug_msg("INTERRUPTED COMPLETED case, append it to idle");
1127 g_idle_add((GSourceFunc)_interrupted_completed, strdup(reason_for_change));
1129 debug_msg(" <<< session interrupt callback finished");
1132 int mm_sound_client_set_session_interrupt_callback(mm_sound_focus_session_interrupt_cb callback, void* user_data)
1134 int ret = MM_ERROR_NONE;
1139 return MM_ERROR_INVALID_ARGUMENT;
1141 /* add internal focus watch callback */
1142 if (g_focus_session_interrupt_info.watch_cb_id == -1) {
1143 if ((ret = mm_sound_client_set_focus_watch_callback(getpid(), FOCUS_FOR_BOTH, true, true, _session_interrupted_cb, NULL,
1144 &g_focus_session_interrupt_info.watch_cb_id))) {
1145 debug_error("failed to mm_sound_client_set_focus_watch_callback(), ret(0x%x)", ret);
1149 g_focus_session_interrupt_info.user_cb = callback;
1150 g_focus_session_interrupt_info.user_data = user_data;
1156 int mm_sound_client_unset_session_interrupt_callback(void)
1158 int ret = MM_ERROR_NONE;
1162 if (!g_focus_session_interrupt_info.user_cb || g_focus_session_interrupt_info.watch_cb_id == -1) {
1163 debug_error("no callback to unset");
1164 return MM_ERROR_SOUND_INTERNAL;
1167 /* remove internal focus watch callback */
1168 if ((ret = mm_sound_client_unset_focus_watch_callback(g_focus_session_interrupt_info.watch_cb_id))) {
1169 debug_error("failed to mm_sound_client_unset_focus_watch_callback(), id(%d), ret(0x%x)",
1170 g_focus_session_interrupt_info.watch_cb_id, ret);
1173 g_focus_session_interrupt_info.watch_cb_id = -1;
1174 g_focus_session_interrupt_info.user_cb = NULL;
1175 g_focus_session_interrupt_info.user_data = NULL;
1181 static gpointer _focus_thread_func(gpointer data)
1183 unsigned int thread_id = (unsigned int)pthread_self();
1184 GMainLoop *focus_loop = (GMainLoop*)data;
1186 debug_warning(">>> thread func : thread id(%u), mainloop[%p]", thread_id, focus_loop);
1188 g_main_loop_run(focus_loop);
1189 debug_warning("<<< quit thread func : thread id(%u), mainloop[%p]", thread_id, focus_loop);
1194 static gboolean _focus_fd_prepare(GSource *source, gint *timeout)
1197 debug_warning("[ PREPARE : %p, (%p, %d)", source, timeout, timeout? *timeout : -1);
1202 static gboolean _focus_fd_check(GSource * source)
1204 FocusSource* fsource = (FocusSource *)source;
1207 debug_error("GSource is null");
1211 debug_warning("CHECK : %p, 0x%x ]", source, fsource->pollfd.revents);
1213 if (fsource->poll_fd.revents & (POLLIN | POLLPRI))
1219 static gboolean _focus_fd_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
1221 debug_warning("*** DISPATCH : %p, (%p, %p)", source, callback, user_data);
1222 return callback(user_data);
1225 static void _focus_fd_finalize(GSource *source)
1227 debug_warning("### FINALIZE : %p", source);
1230 static int _focus_find_index_by_handle(int handle)
1233 for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1234 if (g_focus_sound_handle[i].focus_callback && handle == g_focus_sound_handle[i].handle) {
1235 /* debug_msg("found index(%d) for handle(%d)", i, handle);*/
1236 return (handle == FOCUS_HANDLE_INIT_VAL)? -1 : i;
1242 static int _focus_watch_find_index_by_handle(int handle)
1245 for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1246 if (g_focus_sound_handle[i].watch_callback && handle == g_focus_sound_handle[i].handle) {
1247 /* debug_msg("found index(%d) for watch handle(%d)", i, handle);*/
1248 return (handle == FOCUS_HANDLE_INIT_VAL)? -1 : i;
1254 static gboolean _focus_callback_handler(gpointer user_data)
1256 focus_sound_info_t *focus_handle = (focus_sound_info_t *)user_data;
1260 focus_cb_data_lib cb_data;
1262 debug_log(">>> _focus_callback_handler(), this thread id(%u)\n", (unsigned int)pthread_self());
1264 if (!focus_handle) {
1265 debug_error("focus_handle is null");
1266 return G_SOURCE_CONTINUE;
1268 poll_fd = &focus_handle->fsrc->poll_fd;
1269 debug_log("focus_handle(%p), poll_fd(%p)", focus_handle, poll_fd);
1271 memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1273 if (poll_fd->revents & (POLLIN | POLLPRI)) {
1274 int changed_state = -1;
1276 count = read(poll_fd->fd, &cb_data, sizeof(cb_data));
1278 char str_error[256];
1279 strerror_r(errno, str_error, sizeof(str_error));
1280 debug_error("GpollFD read fail, errno=%d(%s)",errno, str_error);
1281 return G_SOURCE_CONTINUE;
1283 changed_state = cb_data.state;
1285 g_mutex_lock(&focus_handle->focus_lock);
1287 tid = focus_handle->focus_tid;
1289 if (changed_state != -1) {
1290 debug_msg("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)",
1291 tid, cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type);
1292 if (focus_handle->focus_callback == NULL) {
1293 debug_error("focus callback is null..");
1294 g_mutex_unlock(&focus_handle->focus_lock);
1295 return G_SOURCE_CONTINUE;
1297 debug_msg("[CALLBACK(%p) START]", focus_handle->focus_callback);
1298 (focus_handle->focus_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type,
1299 cb_data.option, cb_data.ext_info, focus_handle->user_data);
1300 debug_msg("[CALLBACK END]");
1302 #ifdef CONFIG_ENABLE_RETCB
1306 unsigned int buf = 0;
1307 char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", focus_handle->focus_tid, cb_data.handle);
1308 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1310 char str_error[256];
1311 strerror_r(errno, str_error, sizeof(str_error));
1312 debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n",
1313 tid, tmpfd, filename2, errno, str_error);
1315 g_mutex_unlock(&focus_handle->focus_lock);
1316 return G_SOURCE_CONTINUE;
1318 /* buf contains data as below,
1319 * |<--12bits--><--4bits (reacquisition)--><--16bits (handle)-->| */
1320 buf = (unsigned int)((0x0000ffff & cb_data.handle) | (focus_handle->auto_reacquire << 16));
1321 rett = write(tmpfd, &buf, sizeof(buf));
1324 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
1329 g_mutex_unlock(&focus_handle->focus_lock);
1333 return G_SOURCE_CONTINUE;
1336 static gboolean _focus_watch_callback_handler(gpointer user_data)
1338 focus_sound_info_t *focus_handle = (focus_sound_info_t *)user_data;
1342 focus_cb_data_lib cb_data;
1344 debug_log(">>> _focus_watch_callback_handler(), this thread id(%u)\n", (unsigned int)pthread_self());
1346 if (!focus_handle) {
1347 debug_error("focus_handle is null");
1348 return G_SOURCE_CONTINUE;
1350 poll_fd = &focus_handle->fsrc->poll_fd;
1351 debug_log("focus_handle(%p), poll_fd(%p)", focus_handle, poll_fd);
1353 memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1355 if (poll_fd->revents & (POLLIN | POLLPRI)) {
1356 count = read(poll_fd->fd, &cb_data, sizeof(cb_data));
1358 char str_error[256];
1359 strerror_r(errno, str_error, sizeof(str_error));
1360 debug_error("GpollFD read fail, errno=%d(%s)",errno, str_error);
1361 return G_SOURCE_CONTINUE;
1364 if (!focus_handle->is_used) {
1365 debug_warning("unsetting watch calllback has been already requested");
1366 goto SKIP_CB_AND_RET;
1369 g_mutex_lock(&focus_handle->focus_lock);
1371 tid = focus_handle->focus_tid;
1373 debug_msg("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)",
1374 tid, cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type);
1376 if (focus_handle->watch_callback == NULL) {
1377 debug_warning("watch callback is null..");
1378 goto SKIP_CB_AND_RET;
1380 if (focus_handle->unset_watch_callback_requested == true) {
1381 debug_warning("unset_watch_callback_requested..");
1382 goto SKIP_CB_AND_RET;
1385 debug_msg("[CALLBACK(%p) START]", focus_handle->watch_callback);
1386 (focus_handle->watch_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type,
1387 cb_data.ext_info, focus_handle->user_data);
1388 debug_msg("[CALLBACK END]");
1391 #ifdef CONFIG_ENABLE_RETCB
1396 char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr", focus_handle->focus_tid, cb_data.handle);
1397 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1399 char str_error[256];
1400 strerror_r(errno, str_error, sizeof(str_error));
1401 debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n",
1402 tid, tmpfd, filename2, errno, str_error);
1404 g_mutex_unlock(&focus_handle->focus_lock);
1405 return G_SOURCE_CONTINUE;
1407 buf = cb_data.handle;
1408 rett = write(tmpfd, &buf, sizeof(buf));
1411 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
1416 if (focus_handle->is_used) {
1417 debug_msg("unlock focus_lock = %p", &focus_handle->focus_lock);
1418 g_mutex_unlock(&focus_handle->focus_lock);
1423 return G_SOURCE_CONTINUE;
1426 static void _focus_open_callback(int index, bool is_for_watching)
1433 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1434 debug_error("Invalid focus handle index [%d]", index);
1438 if (is_for_watching) {
1439 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch",
1440 g_focus_sound_handle[index].focus_tid,
1441 g_focus_sound_handle[index].handle);
1443 filename = g_strdup_printf("/tmp/FOCUS.%d.%d",
1444 g_focus_sound_handle[index].focus_tid,
1445 g_focus_sound_handle[index].handle);
1447 pre_mask = umask(0);
1448 if (mknod(filename, S_IFIFO|0666, 0)) {
1449 debug_error("mknod() failure, errno(%d)", errno);
1452 g_focus_sound_handle[index].focus_fd = open(filename, O_RDWR|O_NONBLOCK);
1453 if (g_focus_sound_handle[index].focus_fd == -1) {
1454 debug_error("Open fail : index(%d), file open error(%d)", index, errno);
1456 debug_log("Open success : index(%d), filename(%s), fd(%d)",
1457 index, filename, g_focus_sound_handle[index].focus_fd);
1462 #ifdef CONFIG_ENABLE_RETCB
1465 if (is_for_watching) {
1466 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr",
1467 g_focus_sound_handle[index].focus_tid,
1468 g_focus_sound_handle[index].handle);
1470 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr",
1471 g_focus_sound_handle[index].focus_tid,
1472 g_focus_sound_handle[index].handle);
1474 pre_mask = umask(0);
1475 if (mknod(filename2, S_IFIFO | 0666, 0)) {
1476 debug_error("mknod() failure, errno(%d)", errno);
1486 void _focus_close_callback(int index, bool is_for_watching)
1488 char *filename = NULL;
1492 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1493 debug_error("Invalid focus handle index [%d]", index);
1497 if (g_focus_sound_handle[index].focus_fd < 0) {
1498 debug_error("Close fail : index(%d)", index);
1500 close(g_focus_sound_handle[index].focus_fd);
1501 debug_log("Close Success : index(%d)", index);
1504 if (is_for_watching) {
1505 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch",
1506 g_focus_sound_handle[index].focus_tid,
1507 g_focus_sound_handle[index].handle);
1509 filename = g_strdup_printf("/tmp/FOCUS.%d.%d",
1510 g_focus_sound_handle[index].focus_tid,
1511 g_focus_sound_handle[index].handle);
1513 if (remove(filename)) {
1514 debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)",
1520 #ifdef CONFIG_ENABLE_RETCB
1523 if (is_for_watching) {
1524 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr",
1525 g_focus_sound_handle[index].focus_tid,
1526 g_focus_sound_handle[index].handle);
1528 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr",
1529 g_focus_sound_handle[index].focus_tid,
1530 g_focus_sound_handle[index].handle);
1532 if (remove(filename2)) {
1533 debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)",
1544 static GSourceFuncs event_funcs = {
1545 .prepare = _focus_fd_prepare,
1546 .check = _focus_fd_check,
1547 .dispatch = _focus_fd_dispatch,
1548 .finalize = _focus_fd_finalize,
1551 static bool _focus_add_sound_callback(int index, focus_callback_handler_t focus_cb_handler)
1553 FocusSource *fsrc = NULL;
1554 GSource *src = NULL;
1559 g_mutex_init(&g_focus_sound_handle[index].focus_lock);
1561 src = g_source_new(&event_funcs, sizeof(FocusSource));
1563 debug_error("failed to g_source_new for focus source");
1567 fsrc = (FocusSource*) src;
1569 fsrc->poll_fd.fd = g_focus_sound_handle[index].focus_fd;
1570 fsrc->poll_fd.events = (gushort)(POLLIN | POLLPRI);
1571 g_source_add_poll(src, &fsrc->poll_fd);
1573 g_source_set_callback(src, focus_cb_handler, (gpointer)&g_focus_sound_handle[index], NULL);
1575 debug_warning("fsrc(%p), src_funcs(%p), pollfd(%p), fd(%d)",
1576 fsrc, &event_funcs, &fsrc->poll_fd, fsrc->poll_fd.fd);
1578 fsrc_id = g_source_attach(src, g_main_loop_get_context(g_focus_sound_handle[index].focus_loop));
1580 debug_error("failed to attach the source to context");
1583 g_source_unref(src);
1585 g_focus_sound_handle[index].fsrc = fsrc;
1592 g_source_unref(src);
1597 static bool _focus_remove_sound_callback(int index)
1599 focus_sound_info_t *h = NULL;
1603 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1604 debug_error("Invalid focus handle index [%d]", index);
1608 h = &g_focus_sound_handle[index];
1610 g_source_destroy((GSource *)h->fsrc);
1614 h->focus_callback = NULL;
1615 h->watch_callback = NULL;
1617 g_mutex_clear(&h->focus_lock);
1624 static void _focus_add_callback(int index, bool is_for_watching)
1628 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1629 debug_error("Invalid focus handle index [%d]", index);
1633 if (!is_for_watching) {
1634 if (!_focus_add_sound_callback(index, _focus_callback_handler))
1635 debug_error("failed to _focus_add_sound_callback(%p)", _focus_callback_handler);
1636 } else { // need to check if it's necessary
1637 if (!_focus_add_sound_callback(index, _focus_watch_callback_handler))
1638 debug_error("failed to _focus_add_sound_callback(%p)", _focus_watch_callback_handler);
1643 static void _focus_remove_callback(int index)
1646 if (!_focus_remove_sound_callback(index))
1647 debug_error("failed to __focus_remove_sound_callback()");
1651 static void _focus_init_callback(int index, bool is_for_watching)
1654 _focus_open_callback(index, is_for_watching);
1655 _focus_add_callback(index, is_for_watching);
1659 static void _focus_deinit_callback(int index, bool is_for_watching)
1662 _focus_remove_callback(index);
1663 _focus_close_callback(index, is_for_watching);
1667 #define INTERVAL_MS 20
1668 static int _focus_loop_is_running_timed_wait(GMainLoop *focus_loop, int timeout_ms)
1670 int reduced_time_ms = timeout_ms;
1671 if (!focus_loop || timeout_ms < 0) {
1672 debug_error("invalid argument, focus_loop(%p), timeout_ms(%d)", focus_loop, timeout_ms);
1673 return MM_ERROR_INVALID_ARGUMENT;
1677 if (g_main_loop_is_running(focus_loop))
1678 return MM_ERROR_NONE;
1680 usleep(INTERVAL_MS * 1000);
1681 if (reduced_time_ms < timeout_ms)
1682 debug_warning("reduced_time_ms(%d)", reduced_time_ms);
1683 } while ((reduced_time_ms -= INTERVAL_MS) >= 0);
1685 debug_error("focus_loop is not running for timeout_ms(%d), focus_loop(%p) ", timeout_ms, focus_loop);
1687 return MM_ERROR_SOUND_INTERNAL;
1690 #define LOOP_RUNNING_WAIT_TIME_MS 200
1691 static int _focus_init_context(int index)
1693 int ret = MM_ERROR_NONE;
1694 GMainContext *focus_context;
1698 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1699 debug_error("index(%d) is not valid", index);
1700 return MM_ERROR_INVALID_ARGUMENT;
1703 focus_context = g_main_context_new();
1704 g_focus_sound_handle[index].focus_loop = g_main_loop_new(focus_context, FALSE);
1705 g_main_context_unref(focus_context);
1706 if (g_focus_sound_handle[index].focus_loop == NULL) {
1707 debug_error("could not create mainloop..");
1711 g_focus_sound_handle[index].focus_cb_thread = g_thread_new("focus-cb-thread",
1713 g_focus_sound_handle[index].focus_loop);
1714 if (g_focus_sound_handle[index].focus_cb_thread == NULL) {
1715 debug_error("could not create thread..");
1719 debug_warning("focus cb thread[%p] with mainloop[%p] is created for index(%d)",
1720 g_focus_sound_handle[index].focus_cb_thread, g_focus_sound_handle[index].focus_loop, index);
1722 if ((ret = _focus_loop_is_running_timed_wait(g_focus_sound_handle[index].focus_loop, LOOP_RUNNING_WAIT_TIME_MS))) {
1723 debug_error("failed to _focus_loop_is_running_timed_wait(), ret[0x%x]", ret);
1729 return MM_ERROR_NONE;
1732 if (g_focus_sound_handle[index].focus_loop) {
1733 g_main_loop_unref(g_focus_sound_handle[index].focus_loop);
1734 g_focus_sound_handle[index].focus_loop = NULL;
1736 return MM_ERROR_SOUND_INTERNAL;
1739 static void _focus_deinit_context(int index)
1743 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1744 debug_error("index(%d) is not valid", index);
1748 if (!g_focus_sound_handle[index].focus_loop || !g_focus_sound_handle[index].focus_cb_thread) {
1749 debug_error("focus_loop[%p] or focus_cb_thread[%p] is null",
1750 g_focus_sound_handle[index].focus_loop, g_focus_sound_handle[index].focus_cb_thread);
1754 g_main_loop_quit(g_focus_sound_handle[index].focus_loop);
1755 g_thread_join(g_focus_sound_handle[index].focus_cb_thread);
1756 debug_warning("after thread join, thread[%p], mainloop[%p] for index(%d)",
1757 g_focus_sound_handle[index].focus_cb_thread, g_focus_sound_handle[index].focus_loop, index);
1758 g_main_loop_unref(g_focus_sound_handle[index].focus_loop);
1759 g_focus_sound_handle[index].focus_loop = NULL;
1760 g_focus_sound_handle[index].focus_cb_thread = NULL;
1765 int mm_sound_client_get_unique_id(int *id)
1767 int ret = MM_ERROR_NONE;
1772 ret = MM_ERROR_INVALID_ARGUMENT;
1774 ret = mm_sound_proxy_get_unique_id(id);
1781 int mm_sound_client_is_focus_cb_thread(GThread *mine, bool *result)
1783 int ret = MM_ERROR_NONE;
1786 if (!mine || !result)
1787 ret = MM_ERROR_INVALID_ARGUMENT;
1790 for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1791 if (!g_focus_sound_handle[i].is_used)
1793 if (g_focus_sound_handle[i].focus_cb_thread == mine) {
1803 int mm_sound_client_register_focus(int id, int pid, const char *stream_type, bool is_for_session,
1804 mm_sound_focus_changed_cb callback, void* user_data)
1806 int ret = MM_ERROR_NONE;
1811 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1815 for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
1816 if (g_focus_sound_handle[index].is_used == false) {
1817 g_focus_sound_handle[index].is_used = true;
1822 g_focus_sound_handle[index].focus_tid = instance;
1823 g_focus_sound_handle[index].handle = id;
1824 g_focus_sound_handle[index].focus_callback = callback;
1825 g_focus_sound_handle[index].user_data = user_data;
1826 g_focus_sound_handle[index].is_for_session = is_for_session;
1827 g_focus_sound_handle[index].auto_reacquire = true;
1829 ret = mm_sound_proxy_register_focus(id, pid, stream_type, callback, is_for_session, user_data);
1830 if (ret == MM_ERROR_NONE) {
1831 debug_msg("[Client] Success to register focus\n");
1832 g_need_emergent_exit = TRUE;
1833 if (_focus_init_context(index)) {
1834 ret = MM_ERROR_SOUND_INTERNAL;
1838 debug_error("[Client] Error occurred : 0x%x \n",ret);
1842 _focus_init_callback(index, false);
1846 g_focus_sound_handle[index].is_used = false;
1849 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1855 int mm_sound_client_unregister_focus(int id)
1857 int ret = MM_ERROR_NONE;
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;
1870 instance = g_focus_sound_handle[index].focus_tid;
1872 if (!g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1873 debug_warning("maybe focus_callback is being called, try one more time..");
1874 usleep(2500000); // 2.5 sec
1875 if (g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1876 debug_msg("finally got focus_lock");
1880 ret = mm_sound_proxy_unregister_focus(instance, id, g_focus_sound_handle[index].is_for_session);
1881 if (ret == MM_ERROR_NONE)
1882 debug_msg("[Client] Success to unregister focus\n");
1884 debug_error("[Client] Error occurred : 0x%x \n",ret);
1886 g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
1888 _focus_deinit_callback(index, false);
1889 g_focus_sound_handle[index].focus_fd = 0;
1890 g_focus_sound_handle[index].focus_tid = 0;
1891 g_focus_sound_handle[index].handle = 0;
1892 g_focus_sound_handle[index].is_used = false;
1893 _focus_deinit_context(index);
1897 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1902 int mm_sound_client_set_focus_reacquisition(int id, bool reacquisition, bool is_for_session)
1904 int ret = MM_ERROR_NONE;
1910 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1912 index = _focus_find_index_by_handle(id);
1914 debug_error("Could not find index");
1915 ret = MM_ERROR_INVALID_ARGUMENT;
1918 instance = g_focus_sound_handle[index].focus_tid;
1920 ret = mm_sound_client_is_focus_cb_thread(g_thread_self(), &result);
1922 debug_error("[Client] mm_sound_client_is_focus_cb_thread failed");
1924 } else if (!result) {
1925 ret = mm_sound_proxy_set_focus_reacquisition(instance, id, reacquisition, is_for_session);
1926 if (ret == MM_ERROR_NONE) {
1927 debug_msg("[Client] Success to set focus reacquisition to [%d]\n", reacquisition);
1929 debug_error("[Client] Error occurred : 0x%x \n",ret);
1933 debug_warning("[Client] Inside the focus cb thread, set focus reacquisition to [%d]\n", reacquisition);
1936 g_focus_sound_handle[index].auto_reacquire = reacquisition;
1937 debug_msg("[Client] set focus reacquisition(%d) for id(%d)\n", reacquisition, id);
1940 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1945 int mm_sound_client_get_focus_reacquisition(int id, bool *reacquisition)
1947 int ret = MM_ERROR_NONE;
1952 if (!reacquisition) {
1953 debug_error("Invalid parameter");
1954 return MM_ERROR_INVALID_ARGUMENT;
1957 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1959 index = _focus_find_index_by_handle(id);
1961 debug_error("Could not find index");
1962 ret = MM_ERROR_INVALID_ARGUMENT;
1966 *reacquisition = g_focus_sound_handle[index].auto_reacquire;
1967 debug_msg("[Client] get focus reacquisition(%d) for id(%d)\n", *reacquisition, id);
1970 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1975 int mm_sound_client_get_acquired_focus_stream_type(int focus_type, char **stream_type, int *option, char **ext_info)
1977 int ret = MM_ERROR_NONE;
1978 char *stream_type_str = NULL;
1979 char *ext_info_str = NULL;
1983 ret = mm_sound_proxy_get_acquired_focus_stream_type(focus_type, &stream_type_str, option, &ext_info_str);
1984 if (ret == MM_ERROR_NONE) {
1985 debug_msg("[Client] Success to get stream type of acquired focus, stream_type(%s), ext_info(%s)\n",
1986 stream_type_str, ext_info_str);
1987 *stream_type = strdup(stream_type_str);
1988 *ext_info = strdup(ext_info_str);
1989 g_free(stream_type_str);
1990 g_free(ext_info_str);
1992 debug_error("[Client] Error occurred : 0x%x \n",ret);
1999 int mm_sound_client_acquire_focus(int id, mm_sound_focus_type_e type, int option, const char *ext_info)
2001 int ret = MM_ERROR_NONE;
2006 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2008 index = _focus_find_index_by_handle(id);
2010 debug_error("Could not find index");
2011 ret = MM_ERROR_INVALID_ARGUMENT;
2014 instance = g_focus_sound_handle[index].focus_tid;
2016 ret = mm_sound_proxy_acquire_focus(instance, id, type, option, ext_info, g_focus_sound_handle[index].is_for_session);
2017 if (ret == MM_ERROR_NONE)
2018 debug_msg("[Client] Success to acquire focus\n");
2020 debug_error("[Client] Error occurred : 0x%x \n",ret);
2023 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2028 int mm_sound_client_release_focus(int id, mm_sound_focus_type_e type, int option, const char *ext_info)
2030 int ret = MM_ERROR_NONE;
2035 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2037 index = _focus_find_index_by_handle(id);
2039 debug_error("Could not find index");
2040 ret = MM_ERROR_INVALID_ARGUMENT;
2043 instance = g_focus_sound_handle[index].focus_tid;
2045 ret = mm_sound_proxy_release_focus(instance, id, type, option, ext_info, g_focus_sound_handle[index].is_for_session);
2046 if (ret == MM_ERROR_NONE)
2047 debug_msg("[Client] Success to release focus\n");
2049 debug_error("[Client] Error occurred : 0x%x \n",ret);
2052 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2057 int mm_sound_client_update_stream_focus_status(int id, unsigned int status)
2059 int ret = MM_ERROR_NONE;
2062 if ((ret = mm_sound_proxy_update_stream_focus_status(id, status)) != MM_ERROR_NONE)
2063 debug_error("[Client] failed to update stream focus status, ret[0x%x]", ret);
2069 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,
2070 mm_sound_focus_changed_watch_cb callback, void* user_data, int *id)
2072 int ret = MM_ERROR_NONE;
2079 return MM_ERROR_INVALID_ARGUMENT;
2081 //pthread_mutex_lock(&g_thread_mutex2);
2085 ret = mm_sound_proxy_get_unique_id(id);
2089 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2091 for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
2092 if (g_focus_sound_handle[index].is_used == false) {
2093 g_focus_sound_handle[index].is_used = true;
2098 g_focus_sound_handle[index].focus_tid = instance;
2099 g_focus_sound_handle[index].handle = *id;
2100 g_focus_sound_handle[index].watch_callback = callback;
2101 g_focus_sound_handle[index].user_data = user_data;
2102 g_focus_sound_handle[index].is_for_session = is_for_session;
2103 g_focus_sound_handle[index].is_for_monitor = is_for_monitor;
2104 g_focus_sound_handle[index].unset_watch_callback_requested = false;
2106 ret = mm_sound_proxy_set_focus_watch_callback(pid, g_focus_sound_handle[index].handle, focus_type,
2107 is_for_session, is_for_monitor, callback, user_data);
2109 if (ret == MM_ERROR_NONE) {
2110 debug_msg("[Client] Success to watch focus");
2111 g_need_emergent_exit = TRUE;
2112 if (_focus_init_context(index)) {
2113 ret = MM_ERROR_SOUND_INTERNAL;
2117 debug_error("[Client] Error occurred : 0x%x",ret);
2121 _focus_init_callback(index, true);
2125 g_focus_sound_handle[index].is_used = false;
2128 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2133 int mm_sound_client_request_unset_focus_watch_callback(int id)
2135 int ret = MM_ERROR_NONE;
2139 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2141 index = _focus_watch_find_index_by_handle(id);
2143 debug_error("Could not find index");
2144 ret = MM_ERROR_INVALID_ARGUMENT;
2147 g_focus_sound_handle[index].unset_watch_callback_requested = true;
2150 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2155 int mm_sound_client_unset_focus_watch_callback(int id)
2157 int ret = MM_ERROR_NONE;
2161 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2163 index = _focus_watch_find_index_by_handle(id);
2165 debug_error("Could not find index");
2166 ret = MM_ERROR_INVALID_ARGUMENT;
2170 g_mutex_lock(&g_focus_sound_handle[index].focus_lock);
2172 g_focus_sound_handle[index].is_used = false;
2174 ret = mm_sound_proxy_unset_focus_watch_callback(g_focus_sound_handle[index].focus_tid,
2175 g_focus_sound_handle[index].handle,
2176 g_focus_sound_handle[index].is_for_session);
2178 if (ret == MM_ERROR_NONE)
2179 debug_msg("[Client] Success to unwatch focus\n");
2181 debug_error("[Client] Error occurred : 0x%x \n",ret);
2184 g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
2186 _focus_deinit_callback(index, true);
2187 g_focus_sound_handle[index].focus_fd = 0;
2188 g_focus_sound_handle[index].focus_tid = 0;
2189 g_focus_sound_handle[index].handle = 0;
2190 _focus_deinit_context(index);
2193 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2198 static gboolean _idle_event_callback(void *data)
2200 focus_idle_event_t *idle_event_data = (focus_idle_event_t*)data;
2201 int ret = MM_ERROR_NONE;
2204 debug_error("data is null");
2208 debug_msg("idle_event_data(%p): type(%d), data(%d)",
2209 idle_event_data, idle_event_data->type, idle_event_data->data);
2211 switch (idle_event_data->type) {
2212 case IDLE_EVENT_TYPE_UNSET_FOCUS_WATCH_CB:
2213 if ((ret = mm_sound_client_unset_focus_watch_callback(idle_event_data->data)))
2214 debug_error("Could not unset focus watch callback, id(%d), ret = %x\n", idle_event_data->data, ret);
2216 case IDLE_EVENT_TYPE_UNREGISTER_FOCUS:
2217 if ((ret = mm_sound_client_unregister_focus(idle_event_data->data)))
2218 debug_error("Could not unregister focus, id(%d), ret = %x\n", idle_event_data->data, ret);
2221 debug_warning("invalid type(%d)", idle_event_data->type);
2225 g_free(idle_event_data);
2227 g_idle_event_src = 0;
2229 MMSOUND_LEAVE_CRITICAL_SECTION(&g_event_mutex);
2234 int mm_sound_client_execute_focus_func_in_main_context(focus_idle_event_type_e type, int data)
2236 focus_idle_event_t *idle_event_data = NULL;
2238 if (IDLE_EVENT_TYPE_UNSET_FOCUS_WATCH_CB > type || IDLE_EVENT_TYPE_MAX < type)
2239 return MM_ERROR_INVALID_ARGUMENT;
2241 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_event_mutex, MM_ERROR_SOUND_INTERNAL);
2243 idle_event_data = g_new0(focus_idle_event_t, 1);
2244 idle_event_data->type = type;
2245 idle_event_data->data = data;
2247 g_idle_event_src = g_idle_add_full(G_PRIORITY_HIGH,
2248 (GSourceFunc)_idle_event_callback,
2249 (gpointer)idle_event_data,
2252 return MM_ERROR_NONE;
2257 int mm_sound_client_add_test_callback(mm_sound_test_cb func, void* user_data, unsigned int *subs_id)
2259 int ret = MM_ERROR_NONE;
2263 ret = mm_sound_proxy_add_test_callback(func, user_data, g_free, subs_id);
2269 int mm_sound_client_remove_test_callback(unsigned int subs_id)
2271 int ret = MM_ERROR_NONE;
2274 ret = mm_sound_proxy_remove_test_callback(subs_id);
2281 int mm_sound_client_test(int a, int b, int* getv)
2283 int ret = MM_ERROR_NONE;
2287 ret = mm_sound_proxy_test(a, b, getv);
2288 debug_log("%d * %d -> result : %d", a, b, *getv);