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 mm_sound_client_cleanup(void)
151 int ret = MM_ERROR_NONE;
153 if (g_need_emergent_exit) {
154 ret = mm_sound_proxy_emergent_exit(getpid());
155 if (ret == MM_ERROR_NONE)
156 debug_msg("Success to emergnet_exit");
158 debug_error("Error occurred : 0x%x", ret);
162 void _system_signal_handler(int signo, siginfo_t *siginfo, void *context)
164 sigset_t old_mask, all_mask;
166 debug_warning("Got signal : signo(%d)", signo);
170 sigfillset(&all_mask);
171 sigprocmask(SIG_BLOCK, &all_mask, &old_mask);
173 mm_sound_client_cleanup();
175 sigprocmask(SIG_SETMASK, &old_mask, NULL);
180 if (system_int_old_action.sa_sigaction)
181 system_int_old_action.sa_sigaction(signo, siginfo, context);
183 sigaction(signo, &system_int_old_action, NULL);
186 if (system_abrt_old_action.sa_sigaction)
187 system_abrt_old_action.sa_sigaction(signo, siginfo, context);
189 sigaction(signo, &system_abrt_old_action, NULL);
192 if (system_segv_old_action.sa_sigaction)
193 system_segv_old_action.sa_sigaction(signo, siginfo, context);
195 sigaction(signo, &system_segv_old_action, NULL);
198 if (system_term_old_action.sa_sigaction)
199 system_term_old_action.sa_sigaction(signo, siginfo, context);
201 sigaction(signo, &system_term_old_action, NULL);
204 if (system_sys_old_action.sa_sigaction)
205 system_sys_old_action.sa_sigaction(signo, siginfo, context);
207 sigaction(signo, &system_sys_old_action, NULL);
210 if (system_xcpu_old_action.sa_sigaction)
211 system_xcpu_old_action.sa_sigaction(signo, siginfo, context);
213 sigaction(signo, &system_xcpu_old_action, NULL);
219 debug_warning("signal handling end");
223 static bool _is_dotnet_app(void)
225 char *is_dotnet = NULL;
227 is_dotnet = getenv("DOTNET_APP");
228 if (is_dotnet && atoi(is_dotnet) == 1)
235 int mm_sound_client_initialize(void)
237 int ret = MM_ERROR_NONE;
238 struct sigaction system_action;
242 mm_sound_proxy_initialize();
243 g_idle_event_src = 0;
246 if (_is_dotnet_app()) {
247 debug_warning("no signal handler for dotnet!!");
251 system_action.sa_sigaction = _system_signal_handler;
252 system_action.sa_flags = SA_NOCLDSTOP | SA_SIGINFO;
254 sigemptyset(&system_action.sa_mask);
256 sigaction(SIGINT, &system_action, &system_int_old_action);
257 sigaction(SIGABRT, &system_action, &system_abrt_old_action);
258 sigaction(SIGSEGV, &system_action, &system_segv_old_action);
259 sigaction(SIGTERM, &system_action, &system_term_old_action);
260 sigaction(SIGSYS, &system_action, &system_sys_old_action);
261 sigaction(SIGXCPU, &system_action, &system_xcpu_old_action);
267 int mm_sound_client_finalize(void)
269 int ret = MM_ERROR_NONE;
273 mm_sound_client_cleanup();
275 ret = mm_sound_proxy_finalize();
278 if (g_idle_event_src > 0) {
279 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_event_mutex, MM_ERROR_SOUND_INTERNAL);
280 g_source_remove(g_idle_event_src);
281 MMSOUND_LEAVE_CRITICAL_SECTION(&g_event_mutex);
286 if (_is_dotnet_app()) {
287 debug_warning("no signal handler for dotnet!!");
292 sigaction(SIGINT, &system_int_old_action, NULL);
293 sigaction(SIGABRT, &system_abrt_old_action, NULL);
294 sigaction(SIGSEGV, &system_segv_old_action, NULL);
295 sigaction(SIGTERM, &system_term_old_action, NULL);
296 sigaction(SIGSYS, &system_sys_old_action, NULL);
297 sigaction(SIGXCPU, &system_xcpu_old_action, NULL);
303 void mm_sound_convert_volume_type_to_stream_type(int volume_type, char *stream_type)
305 switch (volume_type) {
306 case VOLUME_TYPE_SYSTEM:
307 MMSOUND_STRNCPY(stream_type, "system", MAX_STREAM_TYPE_LEN);
309 case VOLUME_TYPE_NOTIFICATION:
310 MMSOUND_STRNCPY(stream_type, "notification", MAX_STREAM_TYPE_LEN);
312 case VOLUME_TYPE_ALARM:
313 MMSOUND_STRNCPY(stream_type, "alarm", MAX_STREAM_TYPE_LEN);
315 case VOLUME_TYPE_RINGTONE:
316 MMSOUND_STRNCPY(stream_type, "ringtone-voip", MAX_STREAM_TYPE_LEN);
318 case VOLUME_TYPE_MEDIA:
319 MMSOUND_STRNCPY(stream_type, "media", MAX_STREAM_TYPE_LEN);
321 case VOLUME_TYPE_CALL:
322 MMSOUND_STRNCPY(stream_type, "call-voice", MAX_STREAM_TYPE_LEN);
324 case VOLUME_TYPE_VOIP:
325 MMSOUND_STRNCPY(stream_type, "voip", MAX_STREAM_TYPE_LEN);
327 case VOLUME_TYPE_VOICE:
328 MMSOUND_STRNCPY(stream_type, "voice-information", MAX_STREAM_TYPE_LEN);
331 MMSOUND_STRNCPY(stream_type, "media", MAX_STREAM_TYPE_LEN);
335 debug_msg("volume type (%d) converted to stream type (%s)", volume_type, stream_type);
339 /*****************************************************************************************
340 DBUS SUPPORTED FUNCTIONS
341 ******************************************************************************************/
343 void _mm_sound_client_focus_signal_callback(mm_sound_signal_name_t signal, int value, void *user_data)
345 int ret = MM_ERROR_NONE;
348 debug_msg("focus signal received, value = %d", value);
351 ret = mm_sound_proxy_clear_focus(getpid());
353 debug_error("clear focus failed ret = 0x%x", ret);
354 mm_sound_unsubscribe_signal(g_focus_signal_handle);
355 g_focus_signal_handle = 0;
360 int mm_sound_client_play_tone(int number, int volume_config, double volume,
361 int time, int *handle, bool enable_session)
363 int ret = MM_ERROR_NONE;
364 // int instance = -1; /* instance is unique to communicate with server : client message queue filter type */
365 int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(volume_config);
366 char stream_type[MAX_STREAM_TYPE_LEN] = {0, };
370 /* read session information */
371 int session_type = MM_SESSION_TYPE_MEDIA;
372 int session_options = 0;
373 int is_focus_registered = 0;
375 ret = mm_sound_get_signal_value(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &is_focus_registered);
377 debug_error("mm_sound_get_signal_value failed [0x%x]", ret);
378 return MM_ERROR_POLICY_INTERNAL;
381 if (is_focus_registered)
382 enable_session = false;
384 if (enable_session) {
385 if (MM_ERROR_NONE != _mm_session_util_read_information(-1, &session_type, &session_options)) {
386 debug_warning("Read Session Information failed. use default \"media\" type");
387 session_type = MM_SESSION_TYPE_MEDIA;
389 if (MM_ERROR_NONE != mm_session_init(session_type)) {
390 debug_critical("MMSessionInit() failed");
391 return MM_ERROR_POLICY_INTERNAL;
396 // instance = getpid();
397 //debug_log("pid for client ::: [%d]", instance);
400 debug_msg("Input number : %d", number);
401 /* Send req memory */
403 mm_sound_convert_volume_type_to_stream_type(volume_type, stream_type);
404 ret = mm_sound_proxy_play_tone(number, time, volume, volume_config,
405 session_type, session_options, getpid(), enable_session, handle, stream_type, -1);
407 if (enable_session && !g_focus_signal_handle) {
408 ret = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &g_focus_signal_handle,
409 _mm_sound_client_focus_signal_callback, NULL);
411 debug_error("mm_sound_subscribe_signal failed [0x%x]", ret);
412 return MM_ERROR_POLICY_INTERNAL;
421 int mm_sound_client_play_tone_with_stream_info(int tone, char *stream_type, int stream_id,
422 double volume, int duration, int *handle)
424 int ret = MM_ERROR_NONE;
428 ret = mm_sound_proxy_play_tone_with_stream_info(getpid(), tone, stream_type, stream_id, volume, duration, handle);
434 static void _mm_sound_stop_callback_wrapper_func(int ended_handle, void *userdata)
436 struct callback_data *cb_data = (struct callback_data*) userdata;
437 play_sound_end_callback_data_t *end_cb_data;
439 debug_log("ended_handle : %d", ended_handle);
441 if (cb_data == NULL) {
442 debug_warning("stop callback data null");
446 end_cb_data = (play_sound_end_callback_data_t*) cb_data->extra_data;
448 if (ended_handle == end_cb_data->watching_handle) {
449 debug_log("Interested playing handle end : %d", ended_handle);
450 ((mm_sound_stop_callback_func)(cb_data->user_cb))(cb_data->user_data, ended_handle);
451 if (mm_sound_proxy_remove_play_sound_end_callback(end_cb_data->subs_id) != MM_ERROR_NONE)
452 debug_error("mm_sound_client_dbus_remove_play_file_end_callback failed");
454 debug_log("Not interested playing handle : %d", ended_handle);
458 static void play_end_callback_data_free_func(void *data)
460 struct callback_data *cb_data = (struct callback_data*) data;
463 g_free(cb_data->extra_data);
468 int mm_sound_client_play_sound(MMSoundPlayParam *param, int tone, int *handle)
470 int ret = MM_ERROR_NONE;
471 int session_type = MM_SESSION_TYPE_MEDIA;
472 int session_options = 0;
473 int is_focus_registered = 0;
474 // int instance = -1; /* instance is unique to communicate with server : client message queue filter type */
475 int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(param->volume_config);
476 char stream_type[MAX_STREAM_TYPE_LEN] = {0, };
477 struct callback_data *cb_data = NULL;
478 play_sound_end_callback_data_t *end_cb_data;
482 /* read session information */
484 ret = mm_sound_get_signal_value(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &is_focus_registered);
486 debug_error("mm_sound_get_signal_value failed [0x%x]", ret);
487 return MM_ERROR_POLICY_INTERNAL;
490 if (is_focus_registered)
491 param->skip_session = true;
493 if (param->skip_session == false) {
494 if (MM_ERROR_NONE != _mm_session_util_read_information(-1, &session_type, &session_options)) {
495 debug_warning("Read MMSession Type failed. use default \"media\" type");
496 session_type = MM_SESSION_TYPE_MEDIA;
498 if (MM_ERROR_NONE != mm_session_init(session_type)) {
499 debug_critical("MMSessionInit() failed");
500 return MM_ERROR_POLICY_INTERNAL;
506 if ((param->mem_ptr && param->mem_size)) {
507 // Play memory, deprecated
508 return MM_ERROR_INVALID_ARGUMENT;
511 mm_sound_convert_volume_type_to_stream_type(volume_type, stream_type);
512 ret = mm_sound_proxy_play_sound(param->filename, tone, param->loop, param->volume, param->volume_config,
513 session_type, session_options, getpid(), param->skip_session, handle, stream_type, -1);
514 if (ret != MM_ERROR_NONE) {
515 debug_error("Play Sound Failed");
518 if (param->callback) {
519 end_cb_data = (play_sound_end_callback_data_t *) g_malloc0(sizeof(play_sound_end_callback_data_t));
520 end_cb_data->watching_handle = *handle;
521 GET_CB_DATA(cb_data, param->callback, param->data, end_cb_data);
523 ret = mm_sound_proxy_add_play_sound_end_callback(_mm_sound_stop_callback_wrapper_func, cb_data,
524 play_end_callback_data_free_func, &end_cb_data->subs_id);
525 if (ret != MM_ERROR_NONE) {
526 debug_error("Add callback for play sound(%d) Failed", *handle);
530 if (!param->skip_session && !g_focus_signal_handle) {
531 ret = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &g_focus_signal_handle,
532 _mm_sound_client_focus_signal_callback, NULL);
534 debug_error("mm_sound_subscribe_signal failed [0x%x]", ret);
535 return MM_ERROR_POLICY_INTERNAL;
546 int mm_sound_client_play_sound_with_stream_info(MMSoundPlayParam *param, int *handle, char* stream_type, int stream_id)
548 int ret = MM_ERROR_NONE;
549 struct callback_data *cb_data = NULL;
550 play_sound_end_callback_data_t *end_cb_data;
552 ret = mm_sound_proxy_play_sound_with_stream_info(param->filename, param->loop, param->volume,
553 getpid(), handle, stream_type, stream_id);
554 if (ret != MM_ERROR_NONE) {
555 debug_error("Play Sound Failed");
558 if (param->callback) {
559 end_cb_data = (play_sound_end_callback_data_t *) g_malloc0(sizeof(play_sound_end_callback_data_t));
560 end_cb_data->watching_handle = *handle;
561 GET_CB_DATA(cb_data, param->callback, param->data, end_cb_data);
563 ret = mm_sound_proxy_add_play_sound_end_callback(_mm_sound_stop_callback_wrapper_func, cb_data,
564 play_end_callback_data_free_func, &end_cb_data->subs_id);
565 if (ret != MM_ERROR_NONE) {
566 debug_error("Add callback for play sound(%d) Failed", *handle);
577 int mm_sound_client_stop_sound(int handle)
579 int ret = MM_ERROR_NONE;
582 if (handle < 0 || handle > CLIENT_HANDLE_MAX) {
583 ret = MM_ERROR_INVALID_ARGUMENT;
587 ret = mm_sound_proxy_stop_sound(handle);
593 static int _mm_sound_client_device_list_dump(GList *device_list)
595 int ret = MM_ERROR_NONE;
597 mm_sound_device_t *device_node = NULL;
600 debug_error("Device list NULL, cannot dump list");
601 return MM_ERROR_SOUND_INTERNAL;
604 debug_log("======================== device list : start ==========================");
605 for (list = device_list; list != NULL; list = list->next) {
606 device_node = (mm_sound_device_t *)list->data;
608 debug_log(" list idx[%d]: type[%17s], id[%02d], io_direction[%d], state[%d], name[%s]",
609 count++, device_node->type, device_node->id, device_node->io_direction,
610 device_node->state, device_node->name);
613 debug_log("======================== device list : end ============================");
618 int mm_sound_client_get_current_connected_device_list(int device_flags, mm_sound_device_list_t *device_list)
620 int ret = MM_ERROR_NONE;
624 debug_error("Device list NULL");
625 ret = MM_ERROR_COMMON_INVALID_ARGUMENT;
629 if ((ret = mm_sound_proxy_get_current_connected_device_list(device_flags, &device_list->list)) != MM_ERROR_NONE) {
630 debug_error("failed to get current connected device list with dbus, ret[0x%x]", ret);
633 if (!device_list->list) {
634 debug_error("Got device list null");
635 ret = MM_ERROR_SOUND_NO_DATA;
638 _mm_sound_client_device_list_dump(device_list->list);
645 int mm_sound_client_get_device_by_id(int device_id, mm_sound_device_t **device)
647 int ret = MM_ERROR_NONE;
651 if ((ret = mm_sound_proxy_get_device_by_id(device_id, device)) != MM_ERROR_NONE)
652 debug_error("failed to get device by id");
659 static bool device_is_match_direction(int direction, int mask)
661 if (mask == DEVICE_IO_DIRECTION_FLAGS || mask == 0)
664 if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_IN_FLAG) && (direction & MM_SOUND_DEVICE_IO_DIRECTION_IN))
666 if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_OUT_FLAG) && (direction & MM_SOUND_DEVICE_IO_DIRECTION_OUT))
668 if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_BOTH_FLAG) && (direction == MM_SOUND_DEVICE_IO_DIRECTION_BOTH))
674 static bool device_is_match_state(int state, int mask)
676 if (mask == DEVICE_STATE_FLAGS || mask == 0)
679 if ((mask & MM_SOUND_DEVICE_STATE_DEACTIVATED_FLAG) && (state == MM_SOUND_DEVICE_STATE_DEACTIVATED))
681 if ((mask & MM_SOUND_DEVICE_STATE_ACTIVATED_FLAG) && (state == MM_SOUND_DEVICE_STATE_ACTIVATED))
687 static bool device_is_match_type(const char *type, int mask)
690 const char *builtin_prefix = "builtin";
692 if (mask == DEVICE_TYPE_FLAGS || mask == 0)
695 is_builtin = !strncmp(type, builtin_prefix, strlen(builtin_prefix));
697 if ((mask & MM_SOUND_DEVICE_TYPE_INTERNAL_FLAG) && (is_builtin))
699 if ((mask & MM_SOUND_DEVICE_TYPE_EXTERNAL_FLAG) && (!is_builtin))
705 static bool device_is_match_with_mask(const char *type, int direction, int state, int mask)
707 if (mask == DEVICE_ALL_FLAG)
710 return (device_is_match_direction(direction, mask & DEVICE_IO_DIRECTION_FLAGS) &&
711 device_is_match_state(state, mask & DEVICE_STATE_FLAGS) &&
712 device_is_match_type(type, mask & DEVICE_TYPE_FLAGS));
715 static int _fill_sound_device(mm_sound_device_t *device_h, int device_id, const char *device_type,
716 int direction, int state, const char *name, int vendor_id, int product_id,
717 int *stream_id, int stream_num)
721 if (stream_num > 0 && stream_id == NULL) {
722 debug_error("stream_num is %d, but stream_id is NULL", stream_num);
726 if (stream_num > MAX_STREAM_ON_DEVICE) {
727 debug_error("too many streams on this device");
731 device_h->id = device_id;
732 device_h->io_direction = direction;
733 device_h->state = state;
734 MMSOUND_STRNCPY(device_h->name, name, MAX_DEVICE_NAME_NUM);
735 MMSOUND_STRNCPY(device_h->type, device_type, MAX_DEVICE_TYPE_STR_LEN);
736 device_h->vendor_id = vendor_id;
737 device_h->product_id = product_id;
739 if (stream_num > 0) {
740 device_h->stream_num = stream_num;
741 debug_log("%d streams on this device", stream_num);
742 for (i = 0; i < stream_num; i++) {
743 debug_log(" stream_id : %d", stream_id[i]);
744 device_h->stream_id[i] = stream_id[i];
747 device_h->stream_num = 0;
748 debug_log("There is no stream on this device");
754 static void _mm_sound_device_connected_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
755 int state, const char *name, int vendor_id, int product_id, int *stream_id, int stream_num,
756 gboolean is_connected, void *userdata)
758 mm_sound_device_t device_h;
759 struct callback_data *cb_data = (struct callback_data*) userdata;
762 debug_log("[Device %s] id(%d) type(%s) direction(%d) state(%d) name(%s) vendor-id(%04x) product-id(%04x)",
763 is_connected ? "Connected" : "Disconnected", device_id, device_type, io_direction,
764 state, name, vendor_id, product_id, is_connected);
766 if (cb_data == NULL) {
767 debug_warning("device connected changed callback data null");
771 device_flags = (int) cb_data->extra_data;
772 if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
775 if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name,
776 vendor_id, product_id, stream_id, stream_num) < 0) {
777 debug_error("Failed to fill sound device");
781 ((mm_sound_device_connected_cb)(cb_data->user_cb))(&device_h, is_connected, cb_data->user_data);
784 int mm_sound_client_add_device_connected_callback(int device_flags, mm_sound_device_connected_cb func,
785 void* userdata, unsigned int *subs_id)
787 int ret = MM_ERROR_NONE;
788 struct callback_data *cb_data = NULL;
792 GET_CB_DATA(cb_data, func, userdata, (void*) device_flags);
794 ret = mm_sound_proxy_add_device_connected_callback(device_flags,
795 _mm_sound_device_connected_callback_wrapper_func,
796 cb_data, g_free, subs_id);
797 if (ret == MM_ERROR_NONE)
798 g_need_emergent_exit = TRUE;
804 int mm_sound_client_remove_device_connected_callback(unsigned int subs_id)
806 int ret = MM_ERROR_NONE;
809 ret = mm_sound_proxy_remove_device_connected_callback(subs_id);
815 static void _mm_sound_device_info_changed_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
816 int state, const char *name, int vendor_id, int product_id, int *stream_id, int stream_num,
817 int changed_device_info_type, void *userdata)
819 mm_sound_device_t device_h;
820 struct callback_data *cb_data = (struct callback_data*) userdata;
823 debug_log("[Device Info Changed] id(%d) type(%s) direction(%d) state(%d) name(%s) "
824 "vendor-id(%04x) product-id(%04x) changed_info_type(%d)",
825 device_id, device_type, io_direction, state, name, vendor_id, product_id, changed_device_info_type);
827 if (cb_data == NULL) {
828 debug_warning("device info changed callback data null");
832 device_flags = (int) cb_data->extra_data;
833 if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
836 if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name,
837 vendor_id, product_id, stream_id, stream_num) < 0) {
838 debug_error("Failed to fill sound device");
842 ((mm_sound_device_info_changed_cb)(cb_data->user_cb))(&device_h, changed_device_info_type, cb_data->user_data);
845 int mm_sound_client_add_device_info_changed_callback(int device_flags, mm_sound_device_info_changed_cb func,
846 void *userdata, unsigned int *subs_id)
848 int ret = MM_ERROR_NONE;
849 struct callback_data *cb_data = (struct callback_data*) userdata;
853 GET_CB_DATA(cb_data, func, userdata, (void *) device_flags);
855 ret = mm_sound_proxy_add_device_info_changed_callback(device_flags,
856 _mm_sound_device_info_changed_callback_wrapper_func,
857 cb_data, g_free, subs_id);
863 int mm_sound_client_remove_device_info_changed_callback(unsigned int subs_id)
865 int ret = MM_ERROR_NONE;
868 ret = mm_sound_proxy_remove_device_info_changed_callback(subs_id);
875 static void _mm_sound_device_state_changed_callback_wrapper_func(int device_id, const char *device_type,
876 int io_direction, int state, const char *name, int vendor_id, int product_id,
877 int *stream_id, int stream_num, void *userdata)
879 mm_sound_device_t device_h;
880 struct callback_data *cb_data = (struct callback_data*) userdata;
883 debug_log("[Device State Changed] id(%d) type(%s) direction(%d) state(%d) name(%s)"
884 "vendor-id(%04x), product-id(%04x)",
885 device_id, device_type, io_direction, state, name, vendor_id, product_id);
887 if (cb_data == NULL) {
888 debug_warning("device state changed callback data null");
892 device_flags = (int) cb_data->extra_data;
894 if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
897 if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name,
898 vendor_id, product_id, stream_id, stream_num) < 0) {
899 debug_error("Failed to fill sound device");
903 ((mm_sound_device_state_changed_cb)(cb_data->user_cb))(&device_h, state, cb_data->user_data);
906 int mm_sound_client_add_device_state_changed_callback(int device_flags, mm_sound_device_state_changed_cb func,
907 void *userdata, unsigned int *id)
909 int ret = MM_ERROR_NONE;
910 struct callback_data *cb_data = (struct callback_data*) userdata;
914 GET_CB_DATA(cb_data, func, userdata, (void *) device_flags);
916 ret = mm_sound_proxy_add_device_state_changed_callback(device_flags,
917 _mm_sound_device_state_changed_callback_wrapper_func,
918 cb_data, g_free, id);
924 int mm_sound_client_remove_device_state_changed_callback(unsigned int id)
926 int ret = MM_ERROR_NONE;
929 ret = mm_sound_proxy_remove_device_state_changed_callback(id);
935 int mm_sound_client_is_stream_on_device(int stream_id, int device_id, bool *is_on)
937 int ret = MM_ERROR_NONE;
941 debug_error("Invalid Parameter");
942 ret = MM_ERROR_COMMON_INVALID_ARGUMENT;
946 if ((ret = mm_sound_proxy_is_stream_on_device(stream_id, device_id, is_on)) != MM_ERROR_NONE) {
947 debug_error("failed to query is stream on device, ret[0x%x]", ret);
956 int __convert_volume_type_to_str(int volume_type, char **volume_type_str)
958 int ret = MM_ERROR_NONE;
960 if (!volume_type_str) {
961 return MM_ERROR_COMMON_INVALID_ARGUMENT;
964 switch (volume_type) {
965 case VOLUME_TYPE_SYSTEM:
966 *volume_type_str = "system";
968 case VOLUME_TYPE_NOTIFICATION:
969 *volume_type_str = "notification";
971 case VOLUME_TYPE_ALARM:
972 *volume_type_str = "alarm";
974 case VOLUME_TYPE_RINGTONE:
975 *volume_type_str = "ringtone";
977 case VOLUME_TYPE_MEDIA:
978 *volume_type_str = "media";
980 case VOLUME_TYPE_CALL:
981 *volume_type_str = "call";
983 case VOLUME_TYPE_VOIP:
984 *volume_type_str = "voip";
986 case VOLUME_TYPE_VOICE:
987 *volume_type_str = "voice";
990 debug_error("unexpected volume type [%d]", volume_type);
991 return MM_ERROR_SOUND_INTERNAL;
993 if (!strncmp(*volume_type_str, "", VOLUME_TYPE_LEN)) {
994 debug_error("could not find the volume_type[%d] in this switch case statement", volume_type);
995 ret = MM_ERROR_SOUND_INTERNAL;
997 debug_log("volume_type[%s]", *volume_type_str);
1002 static int __convert_volume_type_to_int(const char *volume_type_str, volume_type_t *volume_type)
1004 int ret = MM_ERROR_NONE;
1006 if (!volume_type || !volume_type_str) {
1007 return MM_ERROR_COMMON_INVALID_ARGUMENT;
1010 if (!strncmp(volume_type_str, "system", VOLUME_TYPE_LEN)) {
1011 *volume_type = VOLUME_TYPE_SYSTEM;
1012 } else if (!strncmp(volume_type_str, "notification", VOLUME_TYPE_LEN)) {
1013 *volume_type = VOLUME_TYPE_NOTIFICATION;
1014 } else if (!strncmp(volume_type_str, "alarm", VOLUME_TYPE_LEN)) {
1015 *volume_type = VOLUME_TYPE_ALARM;
1016 } else if (!strncmp(volume_type_str, "ringtone", VOLUME_TYPE_LEN)) {
1017 *volume_type = VOLUME_TYPE_RINGTONE;
1018 } else if (!strncmp(volume_type_str, "media", VOLUME_TYPE_LEN)) {
1019 *volume_type = VOLUME_TYPE_MEDIA;
1020 } else if (!strncmp(volume_type_str, "call", VOLUME_TYPE_LEN)) {
1021 *volume_type = VOLUME_TYPE_CALL;
1022 } else if (!strncmp(volume_type_str, "voip", VOLUME_TYPE_LEN)) {
1023 *volume_type = VOLUME_TYPE_VOIP;
1024 } else if (!strncmp(volume_type_str, "voice", VOLUME_TYPE_LEN)) {
1025 *volume_type = VOLUME_TYPE_VOICE;
1027 debug_log("Invalid volume type : [%s]", volume_type_str);
1028 ret = MM_ERROR_SOUND_INTERNAL;
1034 int mm_sound_client_set_volume_by_type(const int volume_type, const unsigned int volume_level)
1036 int ret = MM_ERROR_NONE;
1037 char *type_str = NULL;
1040 if ((ret = __convert_volume_type_to_str(volume_type, &type_str)) != MM_ERROR_NONE) {
1041 debug_error("volume type convert failed");
1045 ret = mm_sound_proxy_set_volume_by_type(type_str, volume_level);
1052 static void _mm_sound_volume_changed_callback_wrapper_func(const char *direction, const char *volume_type_str,
1053 int volume_level, void *userdata)
1055 volume_type_t volume_type = 0;
1056 struct callback_data *cb_data = (struct callback_data *) userdata;
1058 debug_log("direction : %s, volume_type : %s, volume_level : %d",
1059 direction, volume_type_str, volume_level);
1061 if (cb_data == NULL) {
1062 debug_warning("volume changed callback data null");
1066 if (__convert_volume_type_to_int(volume_type_str, &volume_type) != MM_ERROR_NONE) {
1067 debug_error("volume type convert failed");
1070 debug_log("Call volume changed user cb, direction : %s, vol_type : %s(%d), level : %u",
1071 direction, volume_type_str, volume_type, volume_level);
1072 ((mm_sound_volume_changed_cb)(cb_data->user_cb))(volume_type, volume_level, cb_data->user_data);
1075 int mm_sound_client_add_volume_changed_callback(mm_sound_volume_changed_cb func, void* userdata, unsigned int *subs_id)
1077 int ret = MM_ERROR_NONE;
1078 struct callback_data *cb_data = NULL;
1082 GET_CB_DATA(cb_data, func, userdata, NULL);
1084 ret = mm_sound_proxy_add_volume_changed_callback(_mm_sound_volume_changed_callback_wrapper_func, cb_data, g_free, subs_id);
1091 int mm_sound_client_remove_volume_changed_callback(unsigned int subs_id)
1093 int ret = MM_ERROR_NONE;
1096 ret = mm_sound_proxy_remove_volume_changed_callback(subs_id);
1102 int mm_sound_client_set_filter_by_type(const char *stream_type, const char *filter_name, const char *filter_parameters, const char *filter_group)
1104 int ret = MM_ERROR_NONE;
1107 ret = mm_sound_proxy_set_filter_by_type(stream_type, filter_name, filter_parameters, filter_group);
1113 int mm_sound_client_unset_filter_by_type(const char *stream_type)
1115 int ret = MM_ERROR_NONE;
1118 ret = mm_sound_proxy_unset_filter_by_type(stream_type);
1124 int mm_sound_client_control_filter_by_type(const char *stream_type, const char *filter_name, const char *filter_controls)
1126 int ret = MM_ERROR_NONE;
1129 ret = mm_sound_proxy_control_filter_by_type(stream_type, filter_name, filter_controls);
1136 static gboolean _interrupted_completed(gpointer *data)
1139 debug_error("data is null");
1142 if (!g_focus_session_interrupt_info.user_cb) {
1143 debug_error("user_cb is null");
1148 debug_msg("invoke user_cb(%p)", g_focus_session_interrupt_info.user_cb);
1149 (g_focus_session_interrupt_info.user_cb)(FOCUS_IS_RELEASED, (const char *)data, g_focus_session_interrupt_info.user_data);
1150 debug_msg("invoked");
1156 static void _session_interrupted_cb(int id, mm_sound_focus_type_e focus_type, mm_sound_focus_state_e state,
1157 const char *reason_for_change, const char *ext_info, void *user_data)
1159 debug_msg("id(%d), focus_type(%d), state(%d), reason(%s)", id, focus_type, state, reason_for_change);
1161 if (id != g_focus_session_interrupt_info.watch_cb_id) {
1162 debug_error("id is not valid(param id:%d, g_focus_session_interrupt_watch_cb_id:%d)",
1163 id, g_focus_session_interrupt_info.watch_cb_id);
1166 if (!g_focus_session_interrupt_info.user_cb) {
1167 debug_error("user callback is null");
1171 debug_msg(" >>> invoking session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
1172 if (state == FOCUS_IS_RELEASED)
1173 (g_focus_session_interrupt_info.user_cb)(FOCUS_IS_ACQUIRED, reason_for_change, g_focus_session_interrupt_info.user_data);
1175 debug_msg("INTERRUPTED COMPLETED case, append it to idle");
1176 g_idle_add((GSourceFunc)_interrupted_completed, strdup(reason_for_change));
1178 debug_msg(" <<< session interrupt callback finished");
1181 int mm_sound_client_set_session_interrupt_callback(mm_sound_focus_session_interrupt_cb callback, void* user_data)
1183 int ret = MM_ERROR_NONE;
1188 return MM_ERROR_INVALID_ARGUMENT;
1190 /* add internal focus watch callback */
1191 if (g_focus_session_interrupt_info.watch_cb_id == -1) {
1192 if ((ret = mm_sound_client_set_focus_watch_callback(getpid(), FOCUS_FOR_BOTH, true, true, _session_interrupted_cb, NULL,
1193 &g_focus_session_interrupt_info.watch_cb_id))) {
1194 debug_error("failed to mm_sound_client_set_focus_watch_callback(), ret(0x%x)", ret);
1198 g_focus_session_interrupt_info.user_cb = callback;
1199 g_focus_session_interrupt_info.user_data = user_data;
1205 int mm_sound_client_unset_session_interrupt_callback(void)
1207 int ret = MM_ERROR_NONE;
1211 if (!g_focus_session_interrupt_info.user_cb || g_focus_session_interrupt_info.watch_cb_id == -1) {
1212 debug_error("no callback to unset");
1213 return MM_ERROR_SOUND_INTERNAL;
1216 /* remove internal focus watch callback */
1217 if ((ret = mm_sound_client_unset_focus_watch_callback(g_focus_session_interrupt_info.watch_cb_id))) {
1218 debug_error("failed to mm_sound_client_unset_focus_watch_callback(), id(%d), ret(0x%x)",
1219 g_focus_session_interrupt_info.watch_cb_id, ret);
1222 g_focus_session_interrupt_info.watch_cb_id = -1;
1223 g_focus_session_interrupt_info.user_cb = NULL;
1224 g_focus_session_interrupt_info.user_data = NULL;
1230 static gpointer _focus_thread_func(gpointer data)
1232 unsigned int thread_id = (unsigned int)pthread_self();
1233 GMainLoop *focus_loop = (GMainLoop*)data;
1235 debug_warning(">>> thread id(%u), mainloop[%p]", thread_id, focus_loop);
1237 g_main_loop_run(focus_loop);
1238 debug_warning("<<< quit : thread id(%u), mainloop[%p]", thread_id, focus_loop);
1243 static gboolean _focus_fd_prepare(GSource *source, gint *timeout)
1246 debug_warning("[ PREPARE : %p, (%p, %d)", source, timeout, timeout ? *timeout : -1);
1251 static gboolean _focus_fd_check(GSource * source)
1253 FocusSource* fsource = (FocusSource *)source;
1256 debug_error("GSource is null");
1260 debug_warning("CHECK : %p, 0x%x ]", source, fsource->pollfd.revents);
1262 if (fsource->poll_fd.revents & (POLLIN | POLLPRI))
1268 static gboolean _focus_fd_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
1270 debug_warning("*** DISPATCH : %p, (%p, %p)", source, callback, user_data);
1271 return callback(user_data);
1274 static void _focus_fd_finalize(GSource *source)
1276 debug_warning("### FINALIZE : %p", source);
1279 static int _focus_find_index_by_handle(int handle)
1282 for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1283 if (g_focus_sound_handle[i].focus_callback && handle == g_focus_sound_handle[i].handle) {
1284 /* debug_msg("found index(%d) for handle(%d)", i, handle);*/
1285 return (handle == FOCUS_HANDLE_INIT_VAL) ? -1 : i;
1291 static int _focus_watch_find_index_by_handle(int handle)
1294 for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1295 if (g_focus_sound_handle[i].watch_callback && handle == g_focus_sound_handle[i].handle) {
1296 /* debug_msg("found index(%d) for watch handle(%d)", i, handle);*/
1297 return (handle == FOCUS_HANDLE_INIT_VAL) ? -1 : i;
1303 static gboolean _focus_callback_handler(gpointer user_data)
1305 focus_sound_info_t *focus_handle = (focus_sound_info_t *)user_data;
1309 focus_cb_data_lib cb_data;
1311 debug_log(">>> thread id(%u)", (unsigned int)pthread_self());
1313 if (!focus_handle) {
1314 debug_error("focus_handle is null");
1315 return G_SOURCE_CONTINUE;
1317 poll_fd = &focus_handle->fsrc->poll_fd;
1318 debug_log("focus_handle(%p), poll_fd(%p)", focus_handle, poll_fd);
1320 memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1322 if (poll_fd->revents & (POLLIN | POLLPRI)) {
1323 int changed_state = -1;
1325 count = read(poll_fd->fd, &cb_data, sizeof(cb_data));
1327 char str_error[256];
1328 strerror_r(errno, str_error, sizeof(str_error));
1329 debug_error("GpollFD read fail, errno=%d(%s)", errno, str_error);
1330 return G_SOURCE_CONTINUE;
1332 changed_state = cb_data.state;
1334 g_mutex_lock(&focus_handle->focus_lock);
1336 tid = focus_handle->focus_tid;
1338 if (changed_state != -1) {
1339 debug_msg("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)",
1340 tid, cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type);
1341 if (focus_handle->focus_callback == NULL) {
1342 debug_error("focus callback is null..");
1343 g_mutex_unlock(&focus_handle->focus_lock);
1344 return G_SOURCE_CONTINUE;
1346 debug_msg("[CALLBACK(%p) START]", focus_handle->focus_callback);
1347 (focus_handle->focus_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type,
1348 cb_data.option, cb_data.ext_info, focus_handle->user_data);
1349 debug_msg("[CALLBACK END]");
1351 #ifdef CONFIG_ENABLE_RETCB
1355 unsigned int buf = 0;
1356 char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", focus_handle->focus_tid, cb_data.handle);
1357 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1359 char str_error[256];
1360 strerror_r(errno, str_error, sizeof(str_error));
1361 debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)",
1362 tid, tmpfd, filename2, errno, str_error);
1364 g_mutex_unlock(&focus_handle->focus_lock);
1365 return G_SOURCE_CONTINUE;
1367 /* buf contains data as below,
1368 * |<--12bits--><--4bits (reacquisition)--><--16bits (handle)-->| */
1369 buf = (unsigned int)((0x0000ffff & cb_data.handle) | (focus_handle->auto_reacquire << 16));
1370 rett = write(tmpfd, &buf, sizeof(buf));
1373 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)", tid, rett);
1378 g_mutex_unlock(&focus_handle->focus_lock);
1382 return G_SOURCE_CONTINUE;
1385 static gboolean _focus_watch_callback_handler(gpointer user_data)
1387 focus_sound_info_t *focus_handle = (focus_sound_info_t *)user_data;
1391 focus_cb_data_lib cb_data;
1393 debug_log(">>> thread id(%u)", (unsigned int)pthread_self());
1395 if (!focus_handle) {
1396 debug_error("focus_handle is null");
1397 return G_SOURCE_CONTINUE;
1399 poll_fd = &focus_handle->fsrc->poll_fd;
1400 debug_log("focus_handle(%p), poll_fd(%p)", focus_handle, poll_fd);
1402 memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1404 if (poll_fd->revents & (POLLIN | POLLPRI)) {
1405 count = read(poll_fd->fd, &cb_data, sizeof(cb_data));
1407 char str_error[256];
1408 strerror_r(errno, str_error, sizeof(str_error));
1409 debug_error("GpollFD read fail, errno=%d(%s)", errno, str_error);
1410 return G_SOURCE_CONTINUE;
1413 if (!focus_handle->is_used) {
1414 debug_warning("unsetting watch calllback has been already requested");
1415 goto SKIP_CB_AND_RET;
1418 g_mutex_lock(&focus_handle->focus_lock);
1420 tid = focus_handle->focus_tid;
1422 debug_msg("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)",
1423 tid, cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type);
1425 if (focus_handle->watch_callback == NULL) {
1426 debug_warning("watch callback is null..");
1427 goto SKIP_CB_AND_RET;
1429 if (focus_handle->unset_watch_callback_requested == true) {
1430 debug_warning("unset_watch_callback_requested..");
1431 goto SKIP_CB_AND_RET;
1434 debug_msg("[CALLBACK(%p) START]", focus_handle->watch_callback);
1435 (focus_handle->watch_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type,
1436 cb_data.ext_info, focus_handle->user_data);
1437 debug_msg("[CALLBACK END]");
1440 #ifdef CONFIG_ENABLE_RETCB
1445 char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr", focus_handle->focus_tid, cb_data.handle);
1446 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1448 char str_error[256];
1449 strerror_r(errno, str_error, sizeof(str_error));
1450 debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)",
1451 tid, tmpfd, filename2, errno, str_error);
1453 g_mutex_unlock(&focus_handle->focus_lock);
1454 return G_SOURCE_CONTINUE;
1456 buf = cb_data.handle;
1457 rett = write(tmpfd, &buf, sizeof(buf));
1460 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)", tid, rett);
1465 if (focus_handle->is_used) {
1466 debug_msg("unlock focus_lock = %p", &focus_handle->focus_lock);
1467 g_mutex_unlock(&focus_handle->focus_lock);
1472 return G_SOURCE_CONTINUE;
1475 static void _focus_open_callback(int index, bool is_for_watching)
1482 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1483 debug_error("Invalid focus handle index [%d]", index);
1487 if (is_for_watching) {
1488 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch",
1489 g_focus_sound_handle[index].focus_tid,
1490 g_focus_sound_handle[index].handle);
1492 filename = g_strdup_printf("/tmp/FOCUS.%d.%d",
1493 g_focus_sound_handle[index].focus_tid,
1494 g_focus_sound_handle[index].handle);
1496 pre_mask = umask(0);
1497 if (mknod(filename, S_IFIFO|0666, 0)) {
1498 debug_error("mknod() failure, errno(%d)", errno);
1501 g_focus_sound_handle[index].focus_fd = open(filename, O_RDWR|O_NONBLOCK);
1502 if (g_focus_sound_handle[index].focus_fd == -1) {
1503 debug_error("Open fail : index(%d), file open error(%d)", index, errno);
1505 debug_log("Open success : index(%d), filename(%s), fd(%d)",
1506 index, filename, g_focus_sound_handle[index].focus_fd);
1511 #ifdef CONFIG_ENABLE_RETCB
1514 if (is_for_watching) {
1515 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr",
1516 g_focus_sound_handle[index].focus_tid,
1517 g_focus_sound_handle[index].handle);
1519 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr",
1520 g_focus_sound_handle[index].focus_tid,
1521 g_focus_sound_handle[index].handle);
1523 pre_mask = umask(0);
1524 if (mknod(filename2, S_IFIFO | 0666, 0)) {
1525 debug_error("mknod() failure, errno(%d)", errno);
1535 void _focus_close_callback(int index, bool is_for_watching)
1537 char *filename = NULL;
1541 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1542 debug_error("Invalid focus handle index [%d]", index);
1546 if (g_focus_sound_handle[index].focus_fd < 0) {
1547 debug_error("Close fail : index(%d)", index);
1549 close(g_focus_sound_handle[index].focus_fd);
1550 debug_log("Close Success : index(%d)", index);
1553 if (is_for_watching) {
1554 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch",
1555 g_focus_sound_handle[index].focus_tid,
1556 g_focus_sound_handle[index].handle);
1558 filename = g_strdup_printf("/tmp/FOCUS.%d.%d",
1559 g_focus_sound_handle[index].focus_tid,
1560 g_focus_sound_handle[index].handle);
1562 if (remove(filename)) {
1563 debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)",
1569 #ifdef CONFIG_ENABLE_RETCB
1572 if (is_for_watching) {
1573 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr",
1574 g_focus_sound_handle[index].focus_tid,
1575 g_focus_sound_handle[index].handle);
1577 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr",
1578 g_focus_sound_handle[index].focus_tid,
1579 g_focus_sound_handle[index].handle);
1581 if (remove(filename2)) {
1582 debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)",
1593 static GSourceFuncs event_funcs = {
1594 .prepare = _focus_fd_prepare,
1595 .check = _focus_fd_check,
1596 .dispatch = _focus_fd_dispatch,
1597 .finalize = _focus_fd_finalize,
1600 static bool _focus_add_sound_callback(int index, focus_callback_handler_t focus_cb_handler)
1602 FocusSource *fsrc = NULL;
1603 GSource *src = NULL;
1608 g_mutex_init(&g_focus_sound_handle[index].focus_lock);
1610 src = g_source_new(&event_funcs, sizeof(FocusSource));
1612 debug_error("failed to g_source_new for focus source");
1616 fsrc = (FocusSource*) src;
1618 fsrc->poll_fd.fd = g_focus_sound_handle[index].focus_fd;
1619 fsrc->poll_fd.events = (gushort)(POLLIN | POLLPRI);
1620 g_source_add_poll(src, &fsrc->poll_fd);
1622 g_source_set_callback(src, focus_cb_handler, (gpointer)&g_focus_sound_handle[index], NULL);
1624 debug_warning("fsrc(%p), src_funcs(%p), pollfd(%p), fd(%d)",
1625 fsrc, &event_funcs, &fsrc->poll_fd, fsrc->poll_fd.fd);
1627 fsrc_id = g_source_attach(src, g_main_loop_get_context(g_focus_sound_handle[index].focus_loop));
1629 debug_error("failed to attach the source to context");
1632 g_source_unref(src);
1634 g_focus_sound_handle[index].fsrc = fsrc;
1641 g_source_unref(src);
1646 static bool _focus_remove_sound_callback(int index)
1648 focus_sound_info_t *h = NULL;
1652 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1653 debug_error("Invalid focus handle index [%d]", index);
1657 h = &g_focus_sound_handle[index];
1659 g_source_destroy((GSource *)h->fsrc);
1663 h->focus_callback = NULL;
1664 h->watch_callback = NULL;
1666 g_mutex_clear(&h->focus_lock);
1673 static void _focus_add_callback(int index, bool is_for_watching)
1677 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1678 debug_error("Invalid focus handle index [%d]", index);
1682 if (!is_for_watching) {
1683 if (!_focus_add_sound_callback(index, _focus_callback_handler))
1684 debug_error("failed to _focus_add_sound_callback(%p)", _focus_callback_handler);
1685 } else { // need to check if it's necessary
1686 if (!_focus_add_sound_callback(index, _focus_watch_callback_handler))
1687 debug_error("failed to _focus_add_sound_callback(%p)", _focus_watch_callback_handler);
1692 static void _focus_remove_callback(int index)
1695 if (!_focus_remove_sound_callback(index))
1696 debug_error("failed to __focus_remove_sound_callback()");
1700 static void _focus_init_callback(int index, bool is_for_watching)
1703 _focus_open_callback(index, is_for_watching);
1704 _focus_add_callback(index, is_for_watching);
1708 static void _focus_deinit_callback(int index, bool is_for_watching)
1711 _focus_remove_callback(index);
1712 _focus_close_callback(index, is_for_watching);
1716 #define INTERVAL_MS 20
1717 static int _focus_loop_is_running_timed_wait(GMainLoop *focus_loop, int timeout_ms)
1719 int reduced_time_ms = timeout_ms;
1720 if (!focus_loop || timeout_ms < 0) {
1721 debug_error("invalid argument, focus_loop(%p), timeout_ms(%d)", focus_loop, timeout_ms);
1722 return MM_ERROR_INVALID_ARGUMENT;
1726 if (g_main_loop_is_running(focus_loop))
1727 return MM_ERROR_NONE;
1729 usleep(INTERVAL_MS * 1000);
1730 if (reduced_time_ms < timeout_ms)
1731 debug_warning("reduced_time_ms(%d)", reduced_time_ms);
1732 } while ((reduced_time_ms -= INTERVAL_MS) >= 0);
1734 debug_error("focus_loop is not running for timeout_ms(%d), focus_loop(%p) ", timeout_ms, focus_loop);
1736 return MM_ERROR_SOUND_INTERNAL;
1739 #define LOOP_RUNNING_WAIT_TIME_MS 200
1740 static int _focus_init_context(int index)
1742 int ret = MM_ERROR_NONE;
1743 GMainContext *focus_context;
1747 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1748 debug_error("index(%d) is not valid", index);
1749 return MM_ERROR_INVALID_ARGUMENT;
1752 focus_context = g_main_context_new();
1753 g_focus_sound_handle[index].focus_loop = g_main_loop_new(focus_context, FALSE);
1754 g_main_context_unref(focus_context);
1755 if (g_focus_sound_handle[index].focus_loop == NULL) {
1756 debug_error("could not create mainloop..");
1760 g_focus_sound_handle[index].focus_cb_thread = g_thread_new("focus-cb-thread",
1762 g_focus_sound_handle[index].focus_loop);
1763 if (g_focus_sound_handle[index].focus_cb_thread == NULL) {
1764 debug_error("could not create thread..");
1768 debug_warning("focus cb thread[%p] with mainloop[%p] is created for index(%d)",
1769 g_focus_sound_handle[index].focus_cb_thread, g_focus_sound_handle[index].focus_loop, index);
1771 if ((ret = _focus_loop_is_running_timed_wait(g_focus_sound_handle[index].focus_loop, LOOP_RUNNING_WAIT_TIME_MS))) {
1772 debug_error("failed to _focus_loop_is_running_timed_wait(), ret[0x%x]", ret);
1778 return MM_ERROR_NONE;
1781 if (g_focus_sound_handle[index].focus_loop) {
1782 g_main_loop_unref(g_focus_sound_handle[index].focus_loop);
1783 g_focus_sound_handle[index].focus_loop = NULL;
1785 return MM_ERROR_SOUND_INTERNAL;
1788 static void _focus_deinit_context(int index)
1792 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1793 debug_error("index(%d) is not valid", index);
1797 if (!g_focus_sound_handle[index].focus_loop || !g_focus_sound_handle[index].focus_cb_thread) {
1798 debug_error("focus_loop[%p] or focus_cb_thread[%p] is null",
1799 g_focus_sound_handle[index].focus_loop, g_focus_sound_handle[index].focus_cb_thread);
1803 g_main_loop_quit(g_focus_sound_handle[index].focus_loop);
1804 g_thread_join(g_focus_sound_handle[index].focus_cb_thread);
1805 debug_warning("after thread join, thread[%p], mainloop[%p] for index(%d)",
1806 g_focus_sound_handle[index].focus_cb_thread, g_focus_sound_handle[index].focus_loop, index);
1807 g_main_loop_unref(g_focus_sound_handle[index].focus_loop);
1808 g_focus_sound_handle[index].focus_loop = NULL;
1809 g_focus_sound_handle[index].focus_cb_thread = NULL;
1814 int mm_sound_client_get_unique_id(int *id)
1816 int ret = MM_ERROR_NONE;
1821 ret = MM_ERROR_INVALID_ARGUMENT;
1823 ret = mm_sound_proxy_get_unique_id(id);
1830 int mm_sound_client_is_focus_cb_thread(GThread *mine, bool *result)
1832 int ret = MM_ERROR_NONE;
1835 if (!mine || !result)
1836 ret = MM_ERROR_INVALID_ARGUMENT;
1839 for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1840 if (!g_focus_sound_handle[i].is_used)
1842 if (g_focus_sound_handle[i].focus_cb_thread == mine) {
1852 int mm_sound_client_register_focus(int id, int pid, const char *stream_type, bool is_for_session,
1853 mm_sound_focus_changed_cb callback, void* user_data)
1855 int ret = MM_ERROR_NONE;
1860 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1864 for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
1865 if (g_focus_sound_handle[index].is_used == false) {
1866 g_focus_sound_handle[index].is_used = true;
1871 g_focus_sound_handle[index].focus_tid = instance;
1872 g_focus_sound_handle[index].handle = id;
1873 g_focus_sound_handle[index].focus_callback = callback;
1874 g_focus_sound_handle[index].user_data = user_data;
1875 g_focus_sound_handle[index].is_for_session = is_for_session;
1876 g_focus_sound_handle[index].auto_reacquire = true;
1878 ret = mm_sound_proxy_register_focus(id, pid, stream_type, callback, is_for_session, user_data);
1879 if (ret == MM_ERROR_NONE) {
1880 debug_msg("Success to register focus");
1881 g_need_emergent_exit = TRUE;
1882 if (_focus_init_context(index)) {
1883 ret = MM_ERROR_SOUND_INTERNAL;
1887 debug_error("Error occurred : 0x%x", ret);
1891 _focus_init_callback(index, false);
1895 g_focus_sound_handle[index].is_used = false;
1898 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1904 int mm_sound_client_unregister_focus(int id)
1906 int ret = MM_ERROR_NONE;
1911 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1913 index = _focus_find_index_by_handle(id);
1915 debug_error("Could not find index");
1916 ret = MM_ERROR_INVALID_ARGUMENT;
1919 instance = g_focus_sound_handle[index].focus_tid;
1921 if (!g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1922 debug_warning("maybe focus_callback is being called, try one more time..");
1923 usleep(2500000); // 2.5 sec
1924 if (g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1925 debug_msg("finally got focus_lock");
1929 ret = mm_sound_proxy_unregister_focus(instance, id, g_focus_sound_handle[index].is_for_session);
1930 if (ret == MM_ERROR_NONE)
1931 debug_msg("Success to unregister focus");
1933 debug_error("Error occurred : 0x%x", ret);
1935 g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
1937 _focus_deinit_callback(index, false);
1938 g_focus_sound_handle[index].focus_fd = 0;
1939 g_focus_sound_handle[index].focus_tid = 0;
1940 g_focus_sound_handle[index].handle = 0;
1941 g_focus_sound_handle[index].is_used = false;
1942 _focus_deinit_context(index);
1946 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1951 int mm_sound_client_set_focus_reacquisition(int id, bool reacquisition, bool is_for_session)
1953 int ret = MM_ERROR_NONE;
1960 /* Since the muse server which uses this library for multiple handles executes requests from clients serially,
1961 * we can skip locking/unlocking the mutex for this case. */
1962 if (!is_for_session)
1963 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1965 index = _focus_find_index_by_handle(id);
1967 debug_error("Could not find index");
1968 ret = MM_ERROR_INVALID_ARGUMENT;
1971 instance = g_focus_sound_handle[index].focus_tid;
1973 ret = mm_sound_client_is_focus_cb_thread(g_thread_self(), &result);
1975 debug_error("mm_sound_client_is_focus_cb_thread failed");
1977 } else if (!result) {
1978 ret = mm_sound_proxy_set_focus_reacquisition(instance, id, reacquisition, is_for_session);
1979 if (ret == MM_ERROR_NONE) {
1980 debug_msg("Success to set focus reacquisition to [%d]", reacquisition);
1982 debug_error("Error occurred : 0x%x", ret);
1986 debug_warning("Inside the focus cb thread, set focus reacquisition to [%d]", reacquisition);
1989 g_focus_sound_handle[index].auto_reacquire = reacquisition;
1990 debug_msg("set focus reacquisition(%d) for id(%d)", reacquisition, id);
1993 if (!is_for_session)
1994 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1999 int mm_sound_client_get_focus_reacquisition(int id, bool *reacquisition)
2001 int ret = MM_ERROR_NONE;
2006 if (!reacquisition) {
2007 debug_error("Invalid parameter");
2008 return MM_ERROR_INVALID_ARGUMENT;
2011 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2013 index = _focus_find_index_by_handle(id);
2015 debug_error("Could not find index");
2016 ret = MM_ERROR_INVALID_ARGUMENT;
2020 *reacquisition = g_focus_sound_handle[index].auto_reacquire;
2021 debug_msg("get focus reacquisition(%d) for id(%d)", *reacquisition, id);
2024 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2029 int mm_sound_client_get_acquired_focus_stream_type(int focus_type, char **stream_type, int *option, char **ext_info)
2031 int ret = MM_ERROR_NONE;
2032 char *stream_type_str = NULL;
2033 char *ext_info_str = NULL;
2037 ret = mm_sound_proxy_get_acquired_focus_stream_type(focus_type, &stream_type_str, option, &ext_info_str);
2038 if (ret == MM_ERROR_NONE) {
2039 debug_msg("Success to get stream type of acquired focus, stream_type(%s), ext_info(%s)",
2040 stream_type_str, ext_info_str);
2041 *stream_type = strdup(stream_type_str);
2042 *ext_info = strdup(ext_info_str);
2043 g_free(stream_type_str);
2044 g_free(ext_info_str);
2046 debug_error("Error occurred : 0x%x", ret);
2053 int mm_sound_client_acquire_focus(int id, mm_sound_focus_type_e type, int option, const char *ext_info)
2055 int ret = MM_ERROR_NONE;
2060 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2062 index = _focus_find_index_by_handle(id);
2064 debug_error("Could not find index");
2065 ret = MM_ERROR_INVALID_ARGUMENT;
2068 instance = g_focus_sound_handle[index].focus_tid;
2070 ret = mm_sound_proxy_acquire_focus(instance, id, type, option, ext_info, g_focus_sound_handle[index].is_for_session);
2071 if (ret == MM_ERROR_NONE)
2072 debug_msg("Success to acquire focus");
2074 debug_error("Error occurred : 0x%x", ret);
2077 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2082 int mm_sound_client_release_focus(int id, mm_sound_focus_type_e type, int option, const char *ext_info)
2084 int ret = MM_ERROR_NONE;
2089 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2091 index = _focus_find_index_by_handle(id);
2093 debug_error("Could not find index");
2094 ret = MM_ERROR_INVALID_ARGUMENT;
2097 instance = g_focus_sound_handle[index].focus_tid;
2099 ret = mm_sound_proxy_release_focus(instance, id, type, option, ext_info, g_focus_sound_handle[index].is_for_session);
2100 if (ret == MM_ERROR_NONE)
2101 debug_msg("Success to release focus");
2103 debug_error("Error occurred : 0x%x", ret);
2106 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2111 int mm_sound_client_update_stream_focus_status(int id, unsigned int status)
2113 int ret = MM_ERROR_NONE;
2116 if ((ret = mm_sound_proxy_update_stream_focus_status(id, status)) != MM_ERROR_NONE)
2117 debug_error("failed to update stream focus status, ret[0x%x]", ret);
2123 int mm_sound_client_deliver_focus(int pid, int src_id, int dst_id, mm_sound_focus_type_e focus_type)
2125 int ret = MM_ERROR_NONE;
2128 if ((ret = mm_sound_proxy_deliver_focus(pid, src_id, dst_id, focus_type)) != MM_ERROR_NONE)
2129 debug_error("failed to deliver focus, ret[0x%x]", ret);
2135 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,
2136 mm_sound_focus_changed_watch_cb callback, void* user_data, int *id)
2138 int ret = MM_ERROR_NONE;
2145 return MM_ERROR_INVALID_ARGUMENT;
2147 //pthread_mutex_lock(&g_thread_mutex2);
2151 ret = mm_sound_proxy_get_unique_id(id);
2155 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2157 for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
2158 if (g_focus_sound_handle[index].is_used == false) {
2159 g_focus_sound_handle[index].is_used = true;
2164 g_focus_sound_handle[index].focus_tid = instance;
2165 g_focus_sound_handle[index].handle = *id;
2166 g_focus_sound_handle[index].watch_callback = callback;
2167 g_focus_sound_handle[index].user_data = user_data;
2168 g_focus_sound_handle[index].is_for_session = is_for_session;
2169 g_focus_sound_handle[index].is_for_monitor = is_for_monitor;
2170 g_focus_sound_handle[index].unset_watch_callback_requested = false;
2172 ret = mm_sound_proxy_set_focus_watch_callback(pid, g_focus_sound_handle[index].handle, focus_type,
2173 is_for_session, is_for_monitor, callback, user_data);
2175 if (ret == MM_ERROR_NONE) {
2176 debug_msg("Success to watch focus");
2177 g_need_emergent_exit = TRUE;
2178 if (_focus_init_context(index)) {
2179 ret = MM_ERROR_SOUND_INTERNAL;
2183 debug_error("Error occurred : 0x%x", ret);
2187 _focus_init_callback(index, true);
2191 g_focus_sound_handle[index].is_used = false;
2194 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2199 int mm_sound_client_request_unset_focus_watch_callback(int id)
2201 int ret = MM_ERROR_NONE;
2205 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2207 index = _focus_watch_find_index_by_handle(id);
2209 debug_error("Could not find index");
2210 ret = MM_ERROR_INVALID_ARGUMENT;
2213 g_focus_sound_handle[index].unset_watch_callback_requested = true;
2216 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2221 int mm_sound_client_unset_focus_watch_callback(int id)
2223 int ret = MM_ERROR_NONE;
2227 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2229 index = _focus_watch_find_index_by_handle(id);
2231 debug_error("Could not find index");
2232 ret = MM_ERROR_INVALID_ARGUMENT;
2236 g_mutex_lock(&g_focus_sound_handle[index].focus_lock);
2238 g_focus_sound_handle[index].is_used = false;
2240 ret = mm_sound_proxy_unset_focus_watch_callback(g_focus_sound_handle[index].focus_tid,
2241 g_focus_sound_handle[index].handle,
2242 g_focus_sound_handle[index].is_for_session);
2244 if (ret == MM_ERROR_NONE)
2245 debug_msg("Success to unwatch focus");
2247 debug_error("Error occurred : 0x%x", ret);
2250 g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
2252 _focus_deinit_callback(index, true);
2253 g_focus_sound_handle[index].focus_fd = 0;
2254 g_focus_sound_handle[index].focus_tid = 0;
2255 g_focus_sound_handle[index].handle = 0;
2256 _focus_deinit_context(index);
2259 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2264 static gboolean _idle_event_callback(void *data)
2266 focus_idle_event_t *idle_event_data = (focus_idle_event_t*)data;
2267 int ret = MM_ERROR_NONE;
2270 debug_error("data is null");
2274 debug_msg("idle_event_data(%p): type(%d), data(%d)",
2275 idle_event_data, idle_event_data->type, idle_event_data->data);
2277 switch (idle_event_data->type) {
2278 case IDLE_EVENT_TYPE_UNSET_FOCUS_WATCH_CB:
2279 if ((ret = mm_sound_client_unset_focus_watch_callback(idle_event_data->data)))
2280 debug_error("Could not unset focus watch callback, id(%d), ret = %x", idle_event_data->data, ret);
2282 case IDLE_EVENT_TYPE_UNREGISTER_FOCUS:
2283 if ((ret = mm_sound_client_unregister_focus(idle_event_data->data)))
2284 debug_error("Could not unregister focus, id(%d), ret = %x", idle_event_data->data, ret);
2287 debug_warning("invalid type(%d)", idle_event_data->type);
2291 g_free(idle_event_data);
2293 g_idle_event_src = 0;
2295 MMSOUND_LEAVE_CRITICAL_SECTION(&g_event_mutex);
2300 int mm_sound_client_execute_focus_func_in_main_context(focus_idle_event_type_e type, int data)
2302 focus_idle_event_t *idle_event_data = NULL;
2304 if (IDLE_EVENT_TYPE_MAX < type)
2305 return MM_ERROR_INVALID_ARGUMENT;
2307 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_event_mutex, MM_ERROR_SOUND_INTERNAL);
2309 idle_event_data = g_new0(focus_idle_event_t, 1);
2310 idle_event_data->type = type;
2311 idle_event_data->data = data;
2313 g_idle_event_src = g_idle_add_full(G_PRIORITY_HIGH,
2314 (GSourceFunc)_idle_event_callback,
2315 (gpointer)idle_event_data,
2318 return MM_ERROR_NONE;
2323 int mm_sound_client_add_test_callback(mm_sound_test_cb func, void* user_data, unsigned int *subs_id)
2325 int ret = MM_ERROR_NONE;
2329 ret = mm_sound_proxy_add_test_callback(func, user_data, g_free, subs_id);
2335 int mm_sound_client_remove_test_callback(unsigned int subs_id)
2337 int ret = MM_ERROR_NONE;
2340 ret = mm_sound_proxy_remove_test_callback(subs_id);
2347 int mm_sound_client_test(int a, int b, int* getv)
2349 int ret = MM_ERROR_NONE;
2353 ret = mm_sound_proxy_test(a, b, getv);
2354 debug_log("%d * %d -> result : %d", a, b, *getv);