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("Success to emergnet_exit");
166 debug_error("Error occurred : 0x%x", 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("Success to emergent_exit");
241 debug_error("Error occurred : 0x%x", 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("Read Session Information failed. use default \"media\" type");
349 session_type = MM_SESSION_TYPE_MEDIA;
351 if(MM_ERROR_NONE != mm_session_init(session_type)) {
352 debug_critical("MMSessionInit() failed");
353 return MM_ERROR_POLICY_INTERNAL;
358 // instance = getpid();
359 //debug_log("pid for client ::: [%d]", instance);
362 debug_msg("Input number : %d", 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("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("Read MMSession Type failed. use default \"media\" type");
458 session_type = MM_SESSION_TYPE_MEDIA;
460 if(MM_ERROR_NONE != mm_session_init(session_type)) {
461 debug_critical("MMSessionInit() failed");
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 ==========================");
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]",
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 ============================");
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("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("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 vendor_id, int product_id,
679 int *stream_id, int stream_num)
683 if (stream_num > 0 && stream_id == NULL) {
684 debug_error("stream_num is %d, but stream_id is NULL", stream_num);
688 if (stream_num > MAX_STREAM_ON_DEVICE) {
689 debug_error("too many streams on this device");
693 device_h->id = device_id;
694 device_h->io_direction = direction;
695 device_h->state = state;
696 MMSOUND_STRNCPY(device_h->name, name, MAX_DEVICE_NAME_NUM);
697 MMSOUND_STRNCPY(device_h->type, device_type, MAX_DEVICE_TYPE_STR_LEN);
698 device_h->vendor_id = vendor_id;
699 device_h->product_id = product_id;
701 if (stream_num > 0) {
702 device_h->stream_num = stream_num;
703 debug_log("%d streams on this device", stream_num);
704 for (i = 0; i < stream_num; i++) {
705 debug_log(" stream_id : %d", stream_id[i]);
706 device_h->stream_id[i] = stream_id[i];
709 device_h->stream_num = 0;
710 debug_log("There is no stream on this device");
716 static void _mm_sound_device_connected_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
717 int state, const char *name, int vendor_id, int product_id, int *stream_id, int stream_num,
718 gboolean is_connected, void *userdata)
720 mm_sound_device_t device_h;
721 struct callback_data *cb_data = (struct callback_data*) userdata;
724 debug_log("[Device %s] id(%d) type(%s) direction(%d) state(%d) name(%s) vendor-id(%04x) product-id(%04x)",
725 is_connected ? "Connected" : "Disconnected", device_id, device_type, io_direction,
726 state, name, vendor_id, product_id, is_connected);
728 if (cb_data == NULL) {
729 debug_warning("device connected changed callback data null");
733 device_flags = (int) cb_data->extra_data;
734 if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
737 if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name,
738 vendor_id, product_id, stream_id, stream_num) < 0) {
739 debug_error("Failed to fill sound device");
743 ((mm_sound_device_connected_cb)(cb_data->user_cb))(&device_h, is_connected, cb_data->user_data);
746 int mm_sound_client_add_device_connected_callback(int device_flags, mm_sound_device_connected_cb func,
747 void* userdata, unsigned int *subs_id)
749 int ret = MM_ERROR_NONE;
750 struct callback_data *cb_data = NULL;
754 GET_CB_DATA(cb_data, func, userdata, (void*) device_flags);
756 ret = mm_sound_proxy_add_device_connected_callback(device_flags,
757 _mm_sound_device_connected_callback_wrapper_func,
758 cb_data, g_free, subs_id);
759 if (ret == MM_ERROR_NONE)
760 g_need_emergent_exit = TRUE;
766 int mm_sound_client_remove_device_connected_callback(unsigned int subs_id)
768 int ret = MM_ERROR_NONE;
771 ret = mm_sound_proxy_remove_device_connected_callback(subs_id);
777 static void _mm_sound_device_info_changed_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
778 int state, const char *name, int vendor_id, int product_id, int *stream_id, int stream_num,
779 int changed_device_info_type, void *userdata)
781 mm_sound_device_t device_h;
782 struct callback_data *cb_data = (struct callback_data*) userdata;
785 debug_log("[Device Info Changed] id(%d) type(%s) direction(%d) state(%d) name(%s) "
786 "vendor-id(%04x) product-id(%04x) changed_info_type(%d)",
787 device_id, device_type, io_direction, state, name, vendor_id, product_id, changed_device_info_type);
789 if (cb_data == NULL) {
790 debug_warning("device info changed callback data null");
794 device_flags = (int) cb_data->extra_data;
795 if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
798 if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name,
799 vendor_id, product_id, stream_id, stream_num) < 0) {
800 debug_error("Failed to fill sound device");
804 ((mm_sound_device_info_changed_cb)(cb_data->user_cb))(&device_h, changed_device_info_type, cb_data->user_data);
807 int mm_sound_client_add_device_info_changed_callback(int device_flags, mm_sound_device_info_changed_cb func,
808 void *userdata, unsigned int *subs_id)
810 int ret = MM_ERROR_NONE;
811 struct callback_data *cb_data = (struct callback_data*) userdata;
815 GET_CB_DATA(cb_data, func, userdata, (void *) device_flags);
817 ret = mm_sound_proxy_add_device_info_changed_callback(device_flags,
818 _mm_sound_device_info_changed_callback_wrapper_func,
819 cb_data, g_free, subs_id);
825 int mm_sound_client_remove_device_info_changed_callback(unsigned int subs_id)
827 int ret = MM_ERROR_NONE;
830 ret = mm_sound_proxy_remove_device_info_changed_callback(subs_id);
837 static void _mm_sound_device_state_changed_callback_wrapper_func(int device_id, const char *device_type,
838 int io_direction, int state, const char *name, int vendor_id, int product_id,
839 int *stream_id, int stream_num, void *userdata)
841 mm_sound_device_t device_h;
842 struct callback_data *cb_data = (struct callback_data*) userdata;
845 debug_log("[Device State Changed] id(%d) type(%s) direction(%d) state(%d) name(%s)"
846 "vendor-id(%04x), product-id(%04x)",
847 device_id, device_type, io_direction, state, name, vendor_id, product_id);
849 if (cb_data == NULL) {
850 debug_warning("device state changed callback data null");
854 device_flags = (int) cb_data->extra_data;
856 if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
859 if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name,
860 vendor_id, product_id, stream_id, stream_num) < 0) {
861 debug_error("Failed to fill sound device");
865 ((mm_sound_device_state_changed_cb)(cb_data->user_cb))(&device_h, state, cb_data->user_data);
868 int mm_sound_client_add_device_state_changed_callback(int device_flags, mm_sound_device_state_changed_cb func,
869 void *userdata, unsigned int *id)
871 int ret = MM_ERROR_NONE;
872 struct callback_data *cb_data = (struct callback_data*) userdata;
876 GET_CB_DATA(cb_data, func, userdata, (void *) device_flags);
878 ret = mm_sound_proxy_add_device_state_changed_callback(device_flags,
879 _mm_sound_device_state_changed_callback_wrapper_func,
880 cb_data, g_free, id);
886 int mm_sound_client_remove_device_state_changed_callback(unsigned int id)
888 int ret = MM_ERROR_NONE;
891 ret = mm_sound_proxy_remove_device_state_changed_callback(id);
897 int mm_sound_client_is_stream_on_device(int stream_id, int device_id, bool *is_on)
899 int ret = MM_ERROR_NONE;
903 debug_error("Invalid Parameter");
904 ret = MM_ERROR_COMMON_INVALID_ARGUMENT;
908 if ((ret = mm_sound_proxy_is_stream_on_device(stream_id, device_id, is_on)) != MM_ERROR_NONE) {
909 debug_error("failed to query is stream on device, ret[0x%x]", ret);
918 int __convert_volume_type_to_str(int volume_type, char **volume_type_str)
920 int ret = MM_ERROR_NONE;
922 if (!volume_type_str) {
923 return MM_ERROR_COMMON_INVALID_ARGUMENT;
926 switch (volume_type) {
927 case VOLUME_TYPE_SYSTEM:
928 *volume_type_str = "system";
930 case VOLUME_TYPE_NOTIFICATION:
931 *volume_type_str = "notification";
933 case VOLUME_TYPE_ALARM:
934 *volume_type_str = "alarm";
936 case VOLUME_TYPE_RINGTONE:
937 *volume_type_str = "ringtone";
939 case VOLUME_TYPE_MEDIA:
940 *volume_type_str = "media";
942 case VOLUME_TYPE_CALL:
943 *volume_type_str = "call";
945 case VOLUME_TYPE_VOIP:
946 *volume_type_str = "voip";
948 case VOLUME_TYPE_VOICE:
949 *volume_type_str = "voice";
952 if (!strncmp(*volume_type_str,"", VOLUME_TYPE_LEN)) {
953 debug_error("could not find the volume_type[%d] in this switch case statement", volume_type);
954 ret = MM_ERROR_SOUND_INTERNAL;
956 debug_log("volume_type[%s]", *volume_type_str);
961 static int __convert_volume_type_to_int(const char *volume_type_str, volume_type_t *volume_type)
963 int ret = MM_ERROR_NONE;
965 if (!volume_type || !volume_type_str) {
966 return MM_ERROR_COMMON_INVALID_ARGUMENT;
969 if (!strncmp(volume_type_str, "system", VOLUME_TYPE_LEN)) {
970 *volume_type = VOLUME_TYPE_SYSTEM;
971 } else if (!strncmp(volume_type_str, "notification", VOLUME_TYPE_LEN)) {
972 *volume_type = VOLUME_TYPE_NOTIFICATION;
973 } else if (!strncmp(volume_type_str, "alarm", VOLUME_TYPE_LEN)) {
974 *volume_type = VOLUME_TYPE_ALARM;
975 } else if (!strncmp(volume_type_str, "ringtone", VOLUME_TYPE_LEN)) {
976 *volume_type = VOLUME_TYPE_RINGTONE;
977 } else if (!strncmp(volume_type_str, "media", VOLUME_TYPE_LEN)) {
978 *volume_type = VOLUME_TYPE_MEDIA;
979 } else if (!strncmp(volume_type_str, "call", VOLUME_TYPE_LEN)) {
980 *volume_type = VOLUME_TYPE_CALL;
981 } else if (!strncmp(volume_type_str, "voip", VOLUME_TYPE_LEN)) {
982 *volume_type = VOLUME_TYPE_VOIP;
983 } else if (!strncmp(volume_type_str, "voice", VOLUME_TYPE_LEN)) {
984 *volume_type = VOLUME_TYPE_VOICE;
986 debug_log("Invalid volume type : [%s]", volume_type_str);
987 ret = MM_ERROR_SOUND_INTERNAL;
993 int mm_sound_client_set_volume_by_type(const int volume_type, const unsigned int volume_level)
995 int ret = MM_ERROR_NONE;
996 char *type_str = NULL;
999 if ((ret = __convert_volume_type_to_str(volume_type, &type_str)) != MM_ERROR_NONE) {
1000 debug_error("volume type convert failed");
1004 ret = mm_sound_proxy_set_volume_by_type(type_str, volume_level);
1011 static void _mm_sound_volume_changed_callback_wrapper_func(const char *direction, const char *volume_type_str,
1012 int volume_level, void *userdata)
1014 volume_type_t volume_type = 0;
1015 struct callback_data *cb_data = (struct callback_data *) userdata;
1017 debug_log("direction : %s, volume_type : %s, volume_level : %d",
1018 direction, volume_type_str, volume_level);
1020 if (cb_data == NULL) {
1021 debug_warning("volume changed callback data null");
1025 if (__convert_volume_type_to_int(volume_type_str, &volume_type) != MM_ERROR_NONE) {
1026 debug_error("volume type convert failed");
1029 debug_log("Call volume changed user cb, direction : %s, vol_type : %s(%d), level : %u",
1030 direction, volume_type_str, volume_type, volume_level);
1031 ((mm_sound_volume_changed_cb)(cb_data->user_cb))(volume_type, volume_level, cb_data->user_data);
1034 int mm_sound_client_add_volume_changed_callback(mm_sound_volume_changed_cb func, void* userdata, unsigned int *subs_id)
1036 int ret = MM_ERROR_NONE;
1037 struct callback_data *cb_data = NULL;
1041 GET_CB_DATA(cb_data, func, userdata, NULL);
1043 ret = mm_sound_proxy_add_volume_changed_callback(_mm_sound_volume_changed_callback_wrapper_func, cb_data, g_free, subs_id);
1050 int mm_sound_client_remove_volume_changed_callback(unsigned int subs_id)
1052 int ret = MM_ERROR_NONE;
1055 ret = mm_sound_proxy_remove_volume_changed_callback(subs_id);
1061 int mm_sound_client_set_filter_by_type(const char *stream_type, const char *filter_name, const char *filter_parameters, const char *filter_group)
1063 int ret = MM_ERROR_NONE;
1066 ret = mm_sound_proxy_set_filter_by_type(stream_type, filter_name, filter_parameters, filter_group);
1072 int mm_sound_client_unset_filter_by_type(const char *stream_type)
1074 int ret = MM_ERROR_NONE;
1077 ret = mm_sound_proxy_unset_filter_by_type(stream_type);
1083 int mm_sound_client_control_filter_by_type(const char *stream_type, const char *filter_name, const char *filter_controls)
1085 int ret = MM_ERROR_NONE;
1088 ret = mm_sound_proxy_control_filter_by_type(stream_type, filter_name, filter_controls);
1095 static gboolean _interrupted_completed(gpointer *data)
1098 debug_error("data is null");
1101 if (!g_focus_session_interrupt_info.user_cb) {
1102 debug_error("user_cb is null");
1107 debug_msg("invoke user_cb(%p)", g_focus_session_interrupt_info.user_cb);
1108 (g_focus_session_interrupt_info.user_cb)(FOCUS_IS_RELEASED, (const char *)data, g_focus_session_interrupt_info.user_data);
1109 debug_msg("invoked");
1115 static void _session_interrupted_cb(int id, mm_sound_focus_type_e focus_type, mm_sound_focus_state_e state,
1116 const char *reason_for_change, const char *ext_info, void *user_data)
1118 debug_msg("id(%d), focus_type(%d), state(%d), reason(%s)", id, focus_type, state, reason_for_change);
1120 if (id != g_focus_session_interrupt_info.watch_cb_id) {
1121 debug_error("id is not valid(param id:%d, g_focus_session_interrupt_watch_cb_id:%d)",
1122 id, g_focus_session_interrupt_info.watch_cb_id);
1125 if (!g_focus_session_interrupt_info.user_cb) {
1126 debug_error("user callback is null");
1130 debug_msg(" >>> invoking session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
1131 if (state == FOCUS_IS_RELEASED)
1132 (g_focus_session_interrupt_info.user_cb)(FOCUS_IS_ACQUIRED, reason_for_change, g_focus_session_interrupt_info.user_data);
1134 debug_msg("INTERRUPTED COMPLETED case, append it to idle");
1135 g_idle_add((GSourceFunc)_interrupted_completed, strdup(reason_for_change));
1137 debug_msg(" <<< session interrupt callback finished");
1140 int mm_sound_client_set_session_interrupt_callback(mm_sound_focus_session_interrupt_cb callback, void* user_data)
1142 int ret = MM_ERROR_NONE;
1147 return MM_ERROR_INVALID_ARGUMENT;
1149 /* add internal focus watch callback */
1150 if (g_focus_session_interrupt_info.watch_cb_id == -1) {
1151 if ((ret = mm_sound_client_set_focus_watch_callback(getpid(), FOCUS_FOR_BOTH, true, true, _session_interrupted_cb, NULL,
1152 &g_focus_session_interrupt_info.watch_cb_id))) {
1153 debug_error("failed to mm_sound_client_set_focus_watch_callback(), ret(0x%x)", ret);
1157 g_focus_session_interrupt_info.user_cb = callback;
1158 g_focus_session_interrupt_info.user_data = user_data;
1164 int mm_sound_client_unset_session_interrupt_callback(void)
1166 int ret = MM_ERROR_NONE;
1170 if (!g_focus_session_interrupt_info.user_cb || g_focus_session_interrupt_info.watch_cb_id == -1) {
1171 debug_error("no callback to unset");
1172 return MM_ERROR_SOUND_INTERNAL;
1175 /* remove internal focus watch callback */
1176 if ((ret = mm_sound_client_unset_focus_watch_callback(g_focus_session_interrupt_info.watch_cb_id))) {
1177 debug_error("failed to mm_sound_client_unset_focus_watch_callback(), id(%d), ret(0x%x)",
1178 g_focus_session_interrupt_info.watch_cb_id, ret);
1181 g_focus_session_interrupt_info.watch_cb_id = -1;
1182 g_focus_session_interrupt_info.user_cb = NULL;
1183 g_focus_session_interrupt_info.user_data = NULL;
1189 static gpointer _focus_thread_func(gpointer data)
1191 unsigned int thread_id = (unsigned int)pthread_self();
1192 GMainLoop *focus_loop = (GMainLoop*)data;
1194 debug_warning(">>> thread id(%u), mainloop[%p]", thread_id, focus_loop);
1196 g_main_loop_run(focus_loop);
1197 debug_warning("<<< quit : thread id(%u), mainloop[%p]", thread_id, focus_loop);
1202 static gboolean _focus_fd_prepare(GSource *source, gint *timeout)
1205 debug_warning("[ PREPARE : %p, (%p, %d)", source, timeout, timeout? *timeout : -1);
1210 static gboolean _focus_fd_check(GSource * source)
1212 FocusSource* fsource = (FocusSource *)source;
1215 debug_error("GSource is null");
1219 debug_warning("CHECK : %p, 0x%x ]", source, fsource->pollfd.revents);
1221 if (fsource->poll_fd.revents & (POLLIN | POLLPRI))
1227 static gboolean _focus_fd_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
1229 debug_warning("*** DISPATCH : %p, (%p, %p)", source, callback, user_data);
1230 return callback(user_data);
1233 static void _focus_fd_finalize(GSource *source)
1235 debug_warning("### FINALIZE : %p", source);
1238 static int _focus_find_index_by_handle(int handle)
1241 for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1242 if (g_focus_sound_handle[i].focus_callback && handle == g_focus_sound_handle[i].handle) {
1243 /* debug_msg("found index(%d) for handle(%d)", i, handle);*/
1244 return (handle == FOCUS_HANDLE_INIT_VAL)? -1 : i;
1250 static int _focus_watch_find_index_by_handle(int handle)
1253 for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1254 if (g_focus_sound_handle[i].watch_callback && handle == g_focus_sound_handle[i].handle) {
1255 /* debug_msg("found index(%d) for watch handle(%d)", i, handle);*/
1256 return (handle == FOCUS_HANDLE_INIT_VAL)? -1 : i;
1262 static gboolean _focus_callback_handler(gpointer user_data)
1264 focus_sound_info_t *focus_handle = (focus_sound_info_t *)user_data;
1268 focus_cb_data_lib cb_data;
1270 debug_log(">>> thread id(%u)", (unsigned int)pthread_self());
1272 if (!focus_handle) {
1273 debug_error("focus_handle is null");
1274 return G_SOURCE_CONTINUE;
1276 poll_fd = &focus_handle->fsrc->poll_fd;
1277 debug_log("focus_handle(%p), poll_fd(%p)", focus_handle, poll_fd);
1279 memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1281 if (poll_fd->revents & (POLLIN | POLLPRI)) {
1282 int changed_state = -1;
1284 count = read(poll_fd->fd, &cb_data, sizeof(cb_data));
1286 char str_error[256];
1287 strerror_r(errno, str_error, sizeof(str_error));
1288 debug_error("GpollFD read fail, errno=%d(%s)",errno, str_error);
1289 return G_SOURCE_CONTINUE;
1291 changed_state = cb_data.state;
1293 g_mutex_lock(&focus_handle->focus_lock);
1295 tid = focus_handle->focus_tid;
1297 if (changed_state != -1) {
1298 debug_msg("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)",
1299 tid, cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type);
1300 if (focus_handle->focus_callback == NULL) {
1301 debug_error("focus callback is null..");
1302 g_mutex_unlock(&focus_handle->focus_lock);
1303 return G_SOURCE_CONTINUE;
1305 debug_msg("[CALLBACK(%p) START]", focus_handle->focus_callback);
1306 (focus_handle->focus_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type,
1307 cb_data.option, cb_data.ext_info, focus_handle->user_data);
1308 debug_msg("[CALLBACK END]");
1310 #ifdef CONFIG_ENABLE_RETCB
1314 unsigned int buf = 0;
1315 char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", focus_handle->focus_tid, cb_data.handle);
1316 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1318 char str_error[256];
1319 strerror_r(errno, str_error, sizeof(str_error));
1320 debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)",
1321 tid, tmpfd, filename2, errno, str_error);
1323 g_mutex_unlock(&focus_handle->focus_lock);
1324 return G_SOURCE_CONTINUE;
1326 /* buf contains data as below,
1327 * |<--12bits--><--4bits (reacquisition)--><--16bits (handle)-->| */
1328 buf = (unsigned int)((0x0000ffff & cb_data.handle) | (focus_handle->auto_reacquire << 16));
1329 rett = write(tmpfd, &buf, sizeof(buf));
1332 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)", tid, rett);
1337 g_mutex_unlock(&focus_handle->focus_lock);
1341 return G_SOURCE_CONTINUE;
1344 static gboolean _focus_watch_callback_handler(gpointer user_data)
1346 focus_sound_info_t *focus_handle = (focus_sound_info_t *)user_data;
1350 focus_cb_data_lib cb_data;
1352 debug_log(">>> thread id(%u)", (unsigned int)pthread_self());
1354 if (!focus_handle) {
1355 debug_error("focus_handle is null");
1356 return G_SOURCE_CONTINUE;
1358 poll_fd = &focus_handle->fsrc->poll_fd;
1359 debug_log("focus_handle(%p), poll_fd(%p)", focus_handle, poll_fd);
1361 memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1363 if (poll_fd->revents & (POLLIN | POLLPRI)) {
1364 count = read(poll_fd->fd, &cb_data, sizeof(cb_data));
1366 char str_error[256];
1367 strerror_r(errno, str_error, sizeof(str_error));
1368 debug_error("GpollFD read fail, errno=%d(%s)",errno, str_error);
1369 return G_SOURCE_CONTINUE;
1372 if (!focus_handle->is_used) {
1373 debug_warning("unsetting watch calllback has been already requested");
1374 goto SKIP_CB_AND_RET;
1377 g_mutex_lock(&focus_handle->focus_lock);
1379 tid = focus_handle->focus_tid;
1381 debug_msg("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)",
1382 tid, cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type);
1384 if (focus_handle->watch_callback == NULL) {
1385 debug_warning("watch callback is null..");
1386 goto SKIP_CB_AND_RET;
1388 if (focus_handle->unset_watch_callback_requested == true) {
1389 debug_warning("unset_watch_callback_requested..");
1390 goto SKIP_CB_AND_RET;
1393 debug_msg("[CALLBACK(%p) START]", focus_handle->watch_callback);
1394 (focus_handle->watch_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type,
1395 cb_data.ext_info, focus_handle->user_data);
1396 debug_msg("[CALLBACK END]");
1399 #ifdef CONFIG_ENABLE_RETCB
1404 char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr", focus_handle->focus_tid, cb_data.handle);
1405 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1407 char str_error[256];
1408 strerror_r(errno, str_error, sizeof(str_error));
1409 debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)",
1410 tid, tmpfd, filename2, errno, str_error);
1412 g_mutex_unlock(&focus_handle->focus_lock);
1413 return G_SOURCE_CONTINUE;
1415 buf = cb_data.handle;
1416 rett = write(tmpfd, &buf, sizeof(buf));
1419 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)", tid, rett);
1424 if (focus_handle->is_used) {
1425 debug_msg("unlock focus_lock = %p", &focus_handle->focus_lock);
1426 g_mutex_unlock(&focus_handle->focus_lock);
1431 return G_SOURCE_CONTINUE;
1434 static void _focus_open_callback(int index, bool is_for_watching)
1441 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1442 debug_error("Invalid focus handle index [%d]", index);
1446 if (is_for_watching) {
1447 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch",
1448 g_focus_sound_handle[index].focus_tid,
1449 g_focus_sound_handle[index].handle);
1451 filename = g_strdup_printf("/tmp/FOCUS.%d.%d",
1452 g_focus_sound_handle[index].focus_tid,
1453 g_focus_sound_handle[index].handle);
1455 pre_mask = umask(0);
1456 if (mknod(filename, S_IFIFO|0666, 0)) {
1457 debug_error("mknod() failure, errno(%d)", errno);
1460 g_focus_sound_handle[index].focus_fd = open(filename, O_RDWR|O_NONBLOCK);
1461 if (g_focus_sound_handle[index].focus_fd == -1) {
1462 debug_error("Open fail : index(%d), file open error(%d)", index, errno);
1464 debug_log("Open success : index(%d), filename(%s), fd(%d)",
1465 index, filename, g_focus_sound_handle[index].focus_fd);
1470 #ifdef CONFIG_ENABLE_RETCB
1473 if (is_for_watching) {
1474 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr",
1475 g_focus_sound_handle[index].focus_tid,
1476 g_focus_sound_handle[index].handle);
1478 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr",
1479 g_focus_sound_handle[index].focus_tid,
1480 g_focus_sound_handle[index].handle);
1482 pre_mask = umask(0);
1483 if (mknod(filename2, S_IFIFO | 0666, 0)) {
1484 debug_error("mknod() failure, errno(%d)", errno);
1494 void _focus_close_callback(int index, bool is_for_watching)
1496 char *filename = NULL;
1500 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1501 debug_error("Invalid focus handle index [%d]", index);
1505 if (g_focus_sound_handle[index].focus_fd < 0) {
1506 debug_error("Close fail : index(%d)", index);
1508 close(g_focus_sound_handle[index].focus_fd);
1509 debug_log("Close Success : index(%d)", index);
1512 if (is_for_watching) {
1513 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch",
1514 g_focus_sound_handle[index].focus_tid,
1515 g_focus_sound_handle[index].handle);
1517 filename = g_strdup_printf("/tmp/FOCUS.%d.%d",
1518 g_focus_sound_handle[index].focus_tid,
1519 g_focus_sound_handle[index].handle);
1521 if (remove(filename)) {
1522 debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)",
1528 #ifdef CONFIG_ENABLE_RETCB
1531 if (is_for_watching) {
1532 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr",
1533 g_focus_sound_handle[index].focus_tid,
1534 g_focus_sound_handle[index].handle);
1536 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr",
1537 g_focus_sound_handle[index].focus_tid,
1538 g_focus_sound_handle[index].handle);
1540 if (remove(filename2)) {
1541 debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)",
1552 static GSourceFuncs event_funcs = {
1553 .prepare = _focus_fd_prepare,
1554 .check = _focus_fd_check,
1555 .dispatch = _focus_fd_dispatch,
1556 .finalize = _focus_fd_finalize,
1559 static bool _focus_add_sound_callback(int index, focus_callback_handler_t focus_cb_handler)
1561 FocusSource *fsrc = NULL;
1562 GSource *src = NULL;
1567 g_mutex_init(&g_focus_sound_handle[index].focus_lock);
1569 src = g_source_new(&event_funcs, sizeof(FocusSource));
1571 debug_error("failed to g_source_new for focus source");
1575 fsrc = (FocusSource*) src;
1577 fsrc->poll_fd.fd = g_focus_sound_handle[index].focus_fd;
1578 fsrc->poll_fd.events = (gushort)(POLLIN | POLLPRI);
1579 g_source_add_poll(src, &fsrc->poll_fd);
1581 g_source_set_callback(src, focus_cb_handler, (gpointer)&g_focus_sound_handle[index], NULL);
1583 debug_warning("fsrc(%p), src_funcs(%p), pollfd(%p), fd(%d)",
1584 fsrc, &event_funcs, &fsrc->poll_fd, fsrc->poll_fd.fd);
1586 fsrc_id = g_source_attach(src, g_main_loop_get_context(g_focus_sound_handle[index].focus_loop));
1588 debug_error("failed to attach the source to context");
1591 g_source_unref(src);
1593 g_focus_sound_handle[index].fsrc = fsrc;
1600 g_source_unref(src);
1605 static bool _focus_remove_sound_callback(int index)
1607 focus_sound_info_t *h = NULL;
1611 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1612 debug_error("Invalid focus handle index [%d]", index);
1616 h = &g_focus_sound_handle[index];
1618 g_source_destroy((GSource *)h->fsrc);
1622 h->focus_callback = NULL;
1623 h->watch_callback = NULL;
1625 g_mutex_clear(&h->focus_lock);
1632 static void _focus_add_callback(int index, bool is_for_watching)
1636 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1637 debug_error("Invalid focus handle index [%d]", index);
1641 if (!is_for_watching) {
1642 if (!_focus_add_sound_callback(index, _focus_callback_handler))
1643 debug_error("failed to _focus_add_sound_callback(%p)", _focus_callback_handler);
1644 } else { // need to check if it's necessary
1645 if (!_focus_add_sound_callback(index, _focus_watch_callback_handler))
1646 debug_error("failed to _focus_add_sound_callback(%p)", _focus_watch_callback_handler);
1651 static void _focus_remove_callback(int index)
1654 if (!_focus_remove_sound_callback(index))
1655 debug_error("failed to __focus_remove_sound_callback()");
1659 static void _focus_init_callback(int index, bool is_for_watching)
1662 _focus_open_callback(index, is_for_watching);
1663 _focus_add_callback(index, is_for_watching);
1667 static void _focus_deinit_callback(int index, bool is_for_watching)
1670 _focus_remove_callback(index);
1671 _focus_close_callback(index, is_for_watching);
1675 #define INTERVAL_MS 20
1676 static int _focus_loop_is_running_timed_wait(GMainLoop *focus_loop, int timeout_ms)
1678 int reduced_time_ms = timeout_ms;
1679 if (!focus_loop || timeout_ms < 0) {
1680 debug_error("invalid argument, focus_loop(%p), timeout_ms(%d)", focus_loop, timeout_ms);
1681 return MM_ERROR_INVALID_ARGUMENT;
1685 if (g_main_loop_is_running(focus_loop))
1686 return MM_ERROR_NONE;
1688 usleep(INTERVAL_MS * 1000);
1689 if (reduced_time_ms < timeout_ms)
1690 debug_warning("reduced_time_ms(%d)", reduced_time_ms);
1691 } while ((reduced_time_ms -= INTERVAL_MS) >= 0);
1693 debug_error("focus_loop is not running for timeout_ms(%d), focus_loop(%p) ", timeout_ms, focus_loop);
1695 return MM_ERROR_SOUND_INTERNAL;
1698 #define LOOP_RUNNING_WAIT_TIME_MS 200
1699 static int _focus_init_context(int index)
1701 int ret = MM_ERROR_NONE;
1702 GMainContext *focus_context;
1706 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1707 debug_error("index(%d) is not valid", index);
1708 return MM_ERROR_INVALID_ARGUMENT;
1711 focus_context = g_main_context_new();
1712 g_focus_sound_handle[index].focus_loop = g_main_loop_new(focus_context, FALSE);
1713 g_main_context_unref(focus_context);
1714 if (g_focus_sound_handle[index].focus_loop == NULL) {
1715 debug_error("could not create mainloop..");
1719 g_focus_sound_handle[index].focus_cb_thread = g_thread_new("focus-cb-thread",
1721 g_focus_sound_handle[index].focus_loop);
1722 if (g_focus_sound_handle[index].focus_cb_thread == NULL) {
1723 debug_error("could not create thread..");
1727 debug_warning("focus cb thread[%p] with mainloop[%p] is created for index(%d)",
1728 g_focus_sound_handle[index].focus_cb_thread, g_focus_sound_handle[index].focus_loop, index);
1730 if ((ret = _focus_loop_is_running_timed_wait(g_focus_sound_handle[index].focus_loop, LOOP_RUNNING_WAIT_TIME_MS))) {
1731 debug_error("failed to _focus_loop_is_running_timed_wait(), ret[0x%x]", ret);
1737 return MM_ERROR_NONE;
1740 if (g_focus_sound_handle[index].focus_loop) {
1741 g_main_loop_unref(g_focus_sound_handle[index].focus_loop);
1742 g_focus_sound_handle[index].focus_loop = NULL;
1744 return MM_ERROR_SOUND_INTERNAL;
1747 static void _focus_deinit_context(int index)
1751 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1752 debug_error("index(%d) is not valid", index);
1756 if (!g_focus_sound_handle[index].focus_loop || !g_focus_sound_handle[index].focus_cb_thread) {
1757 debug_error("focus_loop[%p] or focus_cb_thread[%p] is null",
1758 g_focus_sound_handle[index].focus_loop, g_focus_sound_handle[index].focus_cb_thread);
1762 g_main_loop_quit(g_focus_sound_handle[index].focus_loop);
1763 g_thread_join(g_focus_sound_handle[index].focus_cb_thread);
1764 debug_warning("after thread join, thread[%p], mainloop[%p] for index(%d)",
1765 g_focus_sound_handle[index].focus_cb_thread, g_focus_sound_handle[index].focus_loop, index);
1766 g_main_loop_unref(g_focus_sound_handle[index].focus_loop);
1767 g_focus_sound_handle[index].focus_loop = NULL;
1768 g_focus_sound_handle[index].focus_cb_thread = NULL;
1773 int mm_sound_client_get_unique_id(int *id)
1775 int ret = MM_ERROR_NONE;
1780 ret = MM_ERROR_INVALID_ARGUMENT;
1782 ret = mm_sound_proxy_get_unique_id(id);
1789 int mm_sound_client_is_focus_cb_thread(GThread *mine, bool *result)
1791 int ret = MM_ERROR_NONE;
1794 if (!mine || !result)
1795 ret = MM_ERROR_INVALID_ARGUMENT;
1798 for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1799 if (!g_focus_sound_handle[i].is_used)
1801 if (g_focus_sound_handle[i].focus_cb_thread == mine) {
1811 int mm_sound_client_register_focus(int id, int pid, const char *stream_type, bool is_for_session,
1812 mm_sound_focus_changed_cb callback, void* user_data)
1814 int ret = MM_ERROR_NONE;
1819 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1823 for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
1824 if (g_focus_sound_handle[index].is_used == false) {
1825 g_focus_sound_handle[index].is_used = true;
1830 g_focus_sound_handle[index].focus_tid = instance;
1831 g_focus_sound_handle[index].handle = id;
1832 g_focus_sound_handle[index].focus_callback = callback;
1833 g_focus_sound_handle[index].user_data = user_data;
1834 g_focus_sound_handle[index].is_for_session = is_for_session;
1835 g_focus_sound_handle[index].auto_reacquire = true;
1837 ret = mm_sound_proxy_register_focus(id, pid, stream_type, callback, is_for_session, user_data);
1838 if (ret == MM_ERROR_NONE) {
1839 debug_msg("Success to register focus");
1840 g_need_emergent_exit = TRUE;
1841 if (_focus_init_context(index)) {
1842 ret = MM_ERROR_SOUND_INTERNAL;
1846 debug_error("Error occurred : 0x%x",ret);
1850 _focus_init_callback(index, false);
1854 g_focus_sound_handle[index].is_used = false;
1857 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1863 int mm_sound_client_unregister_focus(int id)
1865 int ret = MM_ERROR_NONE;
1870 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1872 index = _focus_find_index_by_handle(id);
1874 debug_error("Could not find index");
1875 ret = MM_ERROR_INVALID_ARGUMENT;
1878 instance = g_focus_sound_handle[index].focus_tid;
1880 if (!g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1881 debug_warning("maybe focus_callback is being called, try one more time..");
1882 usleep(2500000); // 2.5 sec
1883 if (g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1884 debug_msg("finally got focus_lock");
1888 ret = mm_sound_proxy_unregister_focus(instance, id, g_focus_sound_handle[index].is_for_session);
1889 if (ret == MM_ERROR_NONE)
1890 debug_msg("Success to unregister focus");
1892 debug_error("Error occurred : 0x%x", ret);
1894 g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
1896 _focus_deinit_callback(index, false);
1897 g_focus_sound_handle[index].focus_fd = 0;
1898 g_focus_sound_handle[index].focus_tid = 0;
1899 g_focus_sound_handle[index].handle = 0;
1900 g_focus_sound_handle[index].is_used = false;
1901 _focus_deinit_context(index);
1905 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1910 int mm_sound_client_set_focus_reacquisition(int id, bool reacquisition, bool is_for_session)
1912 int ret = MM_ERROR_NONE;
1918 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1920 index = _focus_find_index_by_handle(id);
1922 debug_error("Could not find index");
1923 ret = MM_ERROR_INVALID_ARGUMENT;
1926 instance = g_focus_sound_handle[index].focus_tid;
1928 ret = mm_sound_client_is_focus_cb_thread(g_thread_self(), &result);
1930 debug_error("mm_sound_client_is_focus_cb_thread failed");
1932 } else if (!result) {
1933 ret = mm_sound_proxy_set_focus_reacquisition(instance, id, reacquisition, is_for_session);
1934 if (ret == MM_ERROR_NONE) {
1935 debug_msg("Success to set focus reacquisition to [%d]", reacquisition);
1937 debug_error("Error occurred : 0x%x",ret);
1941 debug_warning("Inside the focus cb thread, set focus reacquisition to [%d]", reacquisition);
1944 g_focus_sound_handle[index].auto_reacquire = reacquisition;
1945 debug_msg("set focus reacquisition(%d) for id(%d)", reacquisition, id);
1948 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1953 int mm_sound_client_get_focus_reacquisition(int id, bool *reacquisition)
1955 int ret = MM_ERROR_NONE;
1960 if (!reacquisition) {
1961 debug_error("Invalid parameter");
1962 return MM_ERROR_INVALID_ARGUMENT;
1965 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1967 index = _focus_find_index_by_handle(id);
1969 debug_error("Could not find index");
1970 ret = MM_ERROR_INVALID_ARGUMENT;
1974 *reacquisition = g_focus_sound_handle[index].auto_reacquire;
1975 debug_msg("get focus reacquisition(%d) for id(%d)", *reacquisition, id);
1978 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1983 int mm_sound_client_get_acquired_focus_stream_type(int focus_type, char **stream_type, int *option, char **ext_info)
1985 int ret = MM_ERROR_NONE;
1986 char *stream_type_str = NULL;
1987 char *ext_info_str = NULL;
1991 ret = mm_sound_proxy_get_acquired_focus_stream_type(focus_type, &stream_type_str, option, &ext_info_str);
1992 if (ret == MM_ERROR_NONE) {
1993 debug_msg("Success to get stream type of acquired focus, stream_type(%s), ext_info(%s)",
1994 stream_type_str, ext_info_str);
1995 *stream_type = strdup(stream_type_str);
1996 *ext_info = strdup(ext_info_str);
1997 g_free(stream_type_str);
1998 g_free(ext_info_str);
2000 debug_error("Error occurred : 0x%x",ret);
2007 int mm_sound_client_acquire_focus(int id, mm_sound_focus_type_e type, int option, const char *ext_info)
2009 int ret = MM_ERROR_NONE;
2014 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2016 index = _focus_find_index_by_handle(id);
2018 debug_error("Could not find index");
2019 ret = MM_ERROR_INVALID_ARGUMENT;
2022 instance = g_focus_sound_handle[index].focus_tid;
2024 ret = mm_sound_proxy_acquire_focus(instance, id, type, option, ext_info, g_focus_sound_handle[index].is_for_session);
2025 if (ret == MM_ERROR_NONE)
2026 debug_msg("Success to acquire focus");
2028 debug_error("Error occurred : 0x%x", ret);
2031 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2036 int mm_sound_client_release_focus(int id, mm_sound_focus_type_e type, int option, const char *ext_info)
2038 int ret = MM_ERROR_NONE;
2043 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2045 index = _focus_find_index_by_handle(id);
2047 debug_error("Could not find index");
2048 ret = MM_ERROR_INVALID_ARGUMENT;
2051 instance = g_focus_sound_handle[index].focus_tid;
2053 ret = mm_sound_proxy_release_focus(instance, id, type, option, ext_info, g_focus_sound_handle[index].is_for_session);
2054 if (ret == MM_ERROR_NONE)
2055 debug_msg("Success to release focus");
2057 debug_error("Error occurred : 0x%x",ret);
2060 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2065 int mm_sound_client_update_stream_focus_status(int id, unsigned int status)
2067 int ret = MM_ERROR_NONE;
2070 if ((ret = mm_sound_proxy_update_stream_focus_status(id, status)) != MM_ERROR_NONE)
2071 debug_error("failed to update stream focus status, ret[0x%x]", ret);
2077 int mm_sound_client_deliver_focus(int pid, int src_id, int dst_id, mm_sound_focus_type_e focus_type)
2079 int ret = MM_ERROR_NONE;
2082 if ((ret = mm_sound_proxy_deliver_focus(pid, src_id, dst_id, focus_type)) != MM_ERROR_NONE)
2083 debug_error("failed to deliver focus, ret[0x%x]", ret);
2089 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,
2090 mm_sound_focus_changed_watch_cb callback, void* user_data, int *id)
2092 int ret = MM_ERROR_NONE;
2099 return MM_ERROR_INVALID_ARGUMENT;
2101 //pthread_mutex_lock(&g_thread_mutex2);
2105 ret = mm_sound_proxy_get_unique_id(id);
2109 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2111 for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
2112 if (g_focus_sound_handle[index].is_used == false) {
2113 g_focus_sound_handle[index].is_used = true;
2118 g_focus_sound_handle[index].focus_tid = instance;
2119 g_focus_sound_handle[index].handle = *id;
2120 g_focus_sound_handle[index].watch_callback = callback;
2121 g_focus_sound_handle[index].user_data = user_data;
2122 g_focus_sound_handle[index].is_for_session = is_for_session;
2123 g_focus_sound_handle[index].is_for_monitor = is_for_monitor;
2124 g_focus_sound_handle[index].unset_watch_callback_requested = false;
2126 ret = mm_sound_proxy_set_focus_watch_callback(pid, g_focus_sound_handle[index].handle, focus_type,
2127 is_for_session, is_for_monitor, callback, user_data);
2129 if (ret == MM_ERROR_NONE) {
2130 debug_msg("Success to watch focus");
2131 g_need_emergent_exit = TRUE;
2132 if (_focus_init_context(index)) {
2133 ret = MM_ERROR_SOUND_INTERNAL;
2137 debug_error("Error occurred : 0x%x",ret);
2141 _focus_init_callback(index, true);
2145 g_focus_sound_handle[index].is_used = false;
2148 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2153 int mm_sound_client_request_unset_focus_watch_callback(int id)
2155 int ret = MM_ERROR_NONE;
2159 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2161 index = _focus_watch_find_index_by_handle(id);
2163 debug_error("Could not find index");
2164 ret = MM_ERROR_INVALID_ARGUMENT;
2167 g_focus_sound_handle[index].unset_watch_callback_requested = true;
2170 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2175 int mm_sound_client_unset_focus_watch_callback(int id)
2177 int ret = MM_ERROR_NONE;
2181 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2183 index = _focus_watch_find_index_by_handle(id);
2185 debug_error("Could not find index");
2186 ret = MM_ERROR_INVALID_ARGUMENT;
2190 g_mutex_lock(&g_focus_sound_handle[index].focus_lock);
2192 g_focus_sound_handle[index].is_used = false;
2194 ret = mm_sound_proxy_unset_focus_watch_callback(g_focus_sound_handle[index].focus_tid,
2195 g_focus_sound_handle[index].handle,
2196 g_focus_sound_handle[index].is_for_session);
2198 if (ret == MM_ERROR_NONE)
2199 debug_msg("Success to unwatch focus");
2201 debug_error("Error occurred : 0x%x",ret);
2204 g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
2206 _focus_deinit_callback(index, true);
2207 g_focus_sound_handle[index].focus_fd = 0;
2208 g_focus_sound_handle[index].focus_tid = 0;
2209 g_focus_sound_handle[index].handle = 0;
2210 _focus_deinit_context(index);
2213 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2218 static gboolean _idle_event_callback(void *data)
2220 focus_idle_event_t *idle_event_data = (focus_idle_event_t*)data;
2221 int ret = MM_ERROR_NONE;
2224 debug_error("data is null");
2228 debug_msg("idle_event_data(%p): type(%d), data(%d)",
2229 idle_event_data, idle_event_data->type, idle_event_data->data);
2231 switch (idle_event_data->type) {
2232 case IDLE_EVENT_TYPE_UNSET_FOCUS_WATCH_CB:
2233 if ((ret = mm_sound_client_unset_focus_watch_callback(idle_event_data->data)))
2234 debug_error("Could not unset focus watch callback, id(%d), ret = %x", idle_event_data->data, ret);
2236 case IDLE_EVENT_TYPE_UNREGISTER_FOCUS:
2237 if ((ret = mm_sound_client_unregister_focus(idle_event_data->data)))
2238 debug_error("Could not unregister focus, id(%d), ret = %x", idle_event_data->data, ret);
2241 debug_warning("invalid type(%d)", idle_event_data->type);
2245 g_free(idle_event_data);
2247 g_idle_event_src = 0;
2249 MMSOUND_LEAVE_CRITICAL_SECTION(&g_event_mutex);
2254 int mm_sound_client_execute_focus_func_in_main_context(focus_idle_event_type_e type, int data)
2256 focus_idle_event_t *idle_event_data = NULL;
2258 if (IDLE_EVENT_TYPE_MAX < type)
2259 return MM_ERROR_INVALID_ARGUMENT;
2261 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_event_mutex, MM_ERROR_SOUND_INTERNAL);
2263 idle_event_data = g_new0(focus_idle_event_t, 1);
2264 idle_event_data->type = type;
2265 idle_event_data->data = data;
2267 g_idle_event_src = g_idle_add_full(G_PRIORITY_HIGH,
2268 (GSourceFunc)_idle_event_callback,
2269 (gpointer)idle_event_data,
2272 return MM_ERROR_NONE;
2277 int mm_sound_client_add_test_callback(mm_sound_test_cb func, void* user_data, unsigned int *subs_id)
2279 int ret = MM_ERROR_NONE;
2283 ret = mm_sound_proxy_add_test_callback(func, user_data, g_free, subs_id);
2289 int mm_sound_client_remove_test_callback(unsigned int subs_id)
2291 int ret = MM_ERROR_NONE;
2294 ret = mm_sound_proxy_remove_test_callback(subs_id);
2301 int mm_sound_client_test(int a, int b, int* getv)
2303 int ret = MM_ERROR_NONE;
2307 ret = mm_sound_proxy_test(a, b, getv);
2308 debug_log("%d * %d -> result : %d", a, b, *getv);