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, siginfo_t *siginfo, void *context)
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 if (system_int_old_action.sa_sigaction)
175 system_int_old_action.sa_sigaction(signo, siginfo, context);
177 sigaction(signo, &system_int_old_action, NULL);
180 if (system_abrt_old_action.sa_sigaction)
181 system_abrt_old_action.sa_sigaction(signo, siginfo, context);
183 sigaction(signo, &system_abrt_old_action, NULL);
186 if (system_segv_old_action.sa_sigaction)
187 system_segv_old_action.sa_sigaction(signo, siginfo, context);
189 sigaction(signo, &system_segv_old_action, NULL);
192 if (system_term_old_action.sa_sigaction)
193 system_term_old_action.sa_sigaction(signo, siginfo, context);
195 sigaction(signo, &system_term_old_action, NULL);
198 if (system_sys_old_action.sa_sigaction)
199 system_sys_old_action.sa_sigaction(signo, siginfo, context);
201 sigaction(signo, &system_sys_old_action, NULL);
204 if (system_xcpu_old_action.sa_sigaction)
205 system_xcpu_old_action.sa_sigaction(signo, siginfo, context);
207 sigaction(signo, &system_xcpu_old_action, NULL);
213 debug_warning("signal handling end");
216 int mm_sound_client_initialize(void)
218 int ret = MM_ERROR_NONE;
221 mm_sound_proxy_initialize();
222 g_idle_event_src = 0;
225 struct sigaction system_action;
226 system_action.sa_sigaction = _system_signal_handler;
227 system_action.sa_flags = SA_NOCLDSTOP | SA_SIGINFO;
229 sigemptyset(&system_action.sa_mask);
231 sigaction(SIGINT, &system_action, &system_int_old_action);
232 sigaction(SIGABRT, &system_action, &system_abrt_old_action);
233 sigaction(SIGSEGV, &system_action, &system_segv_old_action);
234 sigaction(SIGTERM, &system_action, &system_term_old_action);
235 sigaction(SIGSYS, &system_action, &system_sys_old_action);
236 sigaction(SIGXCPU, &system_action, &system_xcpu_old_action);
242 int mm_sound_client_finalize(void)
244 int ret = MM_ERROR_NONE;
248 if (g_need_emergent_exit) {
249 ret = mm_sound_proxy_emergent_exit(getpid());
250 if (ret == MM_ERROR_NONE)
251 debug_msg("Success to emergent_exit");
253 debug_error("Error occurred : 0x%x", ret);
256 sigaction(SIGINT, &system_int_old_action, NULL);
257 sigaction(SIGABRT, &system_abrt_old_action, NULL);
258 sigaction(SIGSEGV, &system_segv_old_action, NULL);
259 sigaction(SIGTERM, &system_term_old_action, NULL);
260 sigaction(SIGSYS, &system_sys_old_action, NULL);
261 sigaction(SIGXCPU, &system_xcpu_old_action, NULL);
263 ret = mm_sound_proxy_finalize();
266 if (g_idle_event_src > 0) {
267 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_event_mutex, MM_ERROR_SOUND_INTERNAL);
268 g_source_remove(g_idle_event_src);
269 MMSOUND_LEAVE_CRITICAL_SECTION(&g_event_mutex);
277 void mm_sound_convert_volume_type_to_stream_type(int volume_type, char *stream_type)
279 switch (volume_type) {
280 case VOLUME_TYPE_SYSTEM:
281 MMSOUND_STRNCPY(stream_type, "system", MAX_STREAM_TYPE_LEN);
283 case VOLUME_TYPE_NOTIFICATION:
284 MMSOUND_STRNCPY(stream_type, "notification", MAX_STREAM_TYPE_LEN);
286 case VOLUME_TYPE_ALARM:
287 MMSOUND_STRNCPY(stream_type, "alarm", MAX_STREAM_TYPE_LEN);
289 case VOLUME_TYPE_RINGTONE:
290 MMSOUND_STRNCPY(stream_type, "ringtone-voip", MAX_STREAM_TYPE_LEN);
292 case VOLUME_TYPE_MEDIA:
293 MMSOUND_STRNCPY(stream_type, "media", MAX_STREAM_TYPE_LEN);
295 case VOLUME_TYPE_CALL:
296 MMSOUND_STRNCPY(stream_type, "call-voice", MAX_STREAM_TYPE_LEN);
298 case VOLUME_TYPE_VOIP:
299 MMSOUND_STRNCPY(stream_type, "voip", MAX_STREAM_TYPE_LEN);
301 case VOLUME_TYPE_VOICE:
302 MMSOUND_STRNCPY(stream_type, "voice-information", MAX_STREAM_TYPE_LEN);
305 MMSOUND_STRNCPY(stream_type, "media", MAX_STREAM_TYPE_LEN);
309 debug_msg("volume type (%d) converted to stream type (%s)", volume_type, stream_type);
313 /*****************************************************************************************
314 DBUS SUPPORTED FUNCTIONS
315 ******************************************************************************************/
317 void _mm_sound_client_focus_signal_callback(mm_sound_signal_name_t signal, int value, void *user_data)
319 int ret = MM_ERROR_NONE;
322 debug_msg("focus signal received, value = %d", value);
325 ret = mm_sound_proxy_clear_focus(getpid());
327 debug_error("clear focus failed ret = 0x%x", ret);
328 mm_sound_unsubscribe_signal(g_focus_signal_handle);
329 g_focus_signal_handle = 0;
334 int mm_sound_client_play_tone(int number, int volume_config, double volume,
335 int time, int *handle, bool enable_session)
337 int ret = MM_ERROR_NONE;
338 // int instance = -1; /* instance is unique to communicate with server : client message queue filter type */
339 int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(volume_config);
340 char stream_type[MAX_STREAM_TYPE_LEN] = {0, };
344 /* read session information */
345 int session_type = MM_SESSION_TYPE_MEDIA;
346 int session_options = 0;
347 int is_focus_registered = 0;
349 ret = mm_sound_get_signal_value(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &is_focus_registered);
351 debug_error("mm_sound_get_signal_value failed [0x%x]", ret);
352 return MM_ERROR_POLICY_INTERNAL;
355 if (is_focus_registered)
356 enable_session = false;
358 if (enable_session) {
359 if (MM_ERROR_NONE != _mm_session_util_read_information(-1, &session_type, &session_options)) {
360 debug_warning("Read Session Information failed. use default \"media\" type");
361 session_type = MM_SESSION_TYPE_MEDIA;
363 if(MM_ERROR_NONE != mm_session_init(session_type)) {
364 debug_critical("MMSessionInit() failed");
365 return MM_ERROR_POLICY_INTERNAL;
370 // instance = getpid();
371 //debug_log("pid for client ::: [%d]", instance);
374 debug_msg("Input number : %d", number);
375 /* Send req memory */
377 mm_sound_convert_volume_type_to_stream_type(volume_type, stream_type);
378 ret = mm_sound_proxy_play_tone(number, time, volume, volume_config,
379 session_type, session_options, getpid(), enable_session, handle, stream_type, -1);
381 if (enable_session && !g_focus_signal_handle) {
382 ret = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &g_focus_signal_handle,
383 _mm_sound_client_focus_signal_callback, NULL);
385 debug_error("mm_sound_subscribe_signal failed [0x%x]", ret);
386 return MM_ERROR_POLICY_INTERNAL;
395 int mm_sound_client_play_tone_with_stream_info(int tone, char *stream_type, int stream_id,
396 double volume, int duration, int *handle)
398 int ret = MM_ERROR_NONE;
402 ret = mm_sound_proxy_play_tone_with_stream_info(getpid(), tone, stream_type, stream_id, volume, duration, handle);
408 static void _mm_sound_stop_callback_wrapper_func(int ended_handle, void *userdata)
410 struct callback_data *cb_data = (struct callback_data*) userdata;
411 play_sound_end_callback_data_t *end_cb_data;
413 debug_log("ended_handle : %d", ended_handle);
415 if (cb_data == NULL) {
416 debug_warning("stop callback data null");
420 end_cb_data = (play_sound_end_callback_data_t*) cb_data->extra_data;
422 if (ended_handle == end_cb_data->watching_handle) {
423 debug_log("Interested playing handle end : %d", ended_handle);
424 ((mm_sound_stop_callback_func)(cb_data->user_cb))(cb_data->user_data, ended_handle);
425 if (mm_sound_proxy_remove_play_sound_end_callback(end_cb_data->subs_id) != MM_ERROR_NONE)
426 debug_error("mm_sound_client_dbus_remove_play_file_end_callback failed");
428 debug_log("Not interested playing handle : %d", ended_handle);
432 static void play_end_callback_data_free_func(void *data)
434 struct callback_data *cb_data = (struct callback_data*) data;
437 g_free(cb_data->extra_data);
442 int mm_sound_client_play_sound(MMSoundPlayParam *param, int tone, int *handle)
444 int ret = MM_ERROR_NONE;
445 int session_type = MM_SESSION_TYPE_MEDIA;
446 int session_options = 0;
447 int is_focus_registered = 0;
448 // int instance = -1; /* instance is unique to communicate with server : client message queue filter type */
449 int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(param->volume_config);
450 char stream_type[MAX_STREAM_TYPE_LEN] = {0, };
451 struct callback_data *cb_data = NULL;
452 play_sound_end_callback_data_t *end_cb_data;
456 /* read session information */
458 ret = mm_sound_get_signal_value(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &is_focus_registered);
460 debug_error("mm_sound_get_signal_value failed [0x%x]", ret);
461 return MM_ERROR_POLICY_INTERNAL;
464 if (is_focus_registered)
465 param->skip_session = true;
467 if (param->skip_session == false) {
468 if(MM_ERROR_NONE != _mm_session_util_read_information(-1, &session_type, &session_options)) {
469 debug_warning("Read MMSession Type failed. use default \"media\" type");
470 session_type = MM_SESSION_TYPE_MEDIA;
472 if(MM_ERROR_NONE != mm_session_init(session_type)) {
473 debug_critical("MMSessionInit() failed");
474 return MM_ERROR_POLICY_INTERNAL;
480 if ((param->mem_ptr && param->mem_size)) {
481 // Play memory, deprecated
482 return MM_ERROR_INVALID_ARGUMENT;
485 mm_sound_convert_volume_type_to_stream_type(volume_type, stream_type);
486 ret = mm_sound_proxy_play_sound(param->filename, tone, param->loop, param->volume, param->volume_config,
487 session_type, session_options, getpid(), param->skip_session, handle, stream_type, -1);
488 if (ret != MM_ERROR_NONE) {
489 debug_error("Play Sound Failed");
492 if (param->callback) {
493 end_cb_data = (play_sound_end_callback_data_t *) g_malloc0(sizeof(play_sound_end_callback_data_t));
494 end_cb_data->watching_handle = *handle;
495 GET_CB_DATA(cb_data, param->callback, param->data, end_cb_data);
497 ret = mm_sound_proxy_add_play_sound_end_callback(_mm_sound_stop_callback_wrapper_func, cb_data,
498 play_end_callback_data_free_func, &end_cb_data->subs_id);
499 if (ret != MM_ERROR_NONE) {
500 debug_error("Add callback for play sound(%d) Failed", *handle);
504 if (!param->skip_session && !g_focus_signal_handle) {
505 ret = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &g_focus_signal_handle,
506 _mm_sound_client_focus_signal_callback, NULL);
508 debug_error("mm_sound_subscribe_signal failed [0x%x]", ret);
509 return MM_ERROR_POLICY_INTERNAL;
520 int mm_sound_client_play_sound_with_stream_info(MMSoundPlayParam *param, int *handle, char* stream_type, int stream_id)
522 int ret = MM_ERROR_NONE;
523 struct callback_data *cb_data = NULL;
524 play_sound_end_callback_data_t *end_cb_data;
526 ret = mm_sound_proxy_play_sound_with_stream_info(param->filename, param->loop, param->volume,
527 getpid(), handle, stream_type, stream_id);
528 if (ret != MM_ERROR_NONE) {
529 debug_error("Play Sound Failed");
532 if (param->callback) {
533 end_cb_data = (play_sound_end_callback_data_t *) g_malloc0(sizeof(play_sound_end_callback_data_t));
534 end_cb_data->watching_handle = *handle;
535 GET_CB_DATA(cb_data, param->callback, param->data, end_cb_data);
537 ret = mm_sound_proxy_add_play_sound_end_callback(_mm_sound_stop_callback_wrapper_func, cb_data,
538 play_end_callback_data_free_func, &end_cb_data->subs_id);
539 if (ret != MM_ERROR_NONE) {
540 debug_error("Add callback for play sound(%d) Failed", *handle);
551 int mm_sound_client_stop_sound(int handle)
553 int ret = MM_ERROR_NONE;
556 if (handle < 0 || handle > CLIENT_HANDLE_MAX) {
557 ret = MM_ERROR_INVALID_ARGUMENT;
561 ret = mm_sound_proxy_stop_sound(handle);
567 static int _mm_sound_client_device_list_dump (GList *device_list)
569 int ret = MM_ERROR_NONE;
571 mm_sound_device_t *device_node = NULL;
574 debug_error("Device list NULL, cannot dump list");
575 return MM_ERROR_SOUND_INTERNAL;
578 debug_log("======================== device list : start ==========================");
579 for (list = device_list; list != NULL; list = list->next) {
580 device_node = (mm_sound_device_t *)list->data;
582 debug_log(" list idx[%d]: type[%17s], id[%02d], io_direction[%d], state[%d], name[%s]",
583 count++, device_node->type, device_node->id, device_node->io_direction,
584 device_node->state, device_node->name);
587 debug_log("======================== device list : end ============================");
592 int mm_sound_client_get_current_connected_device_list(int device_flags, mm_sound_device_list_t *device_list)
594 int ret = MM_ERROR_NONE;
598 debug_error("Device list NULL");
599 ret = MM_ERROR_COMMON_INVALID_ARGUMENT;
603 if ((ret = mm_sound_proxy_get_current_connected_device_list(device_flags, &device_list->list)) != MM_ERROR_NONE) {
604 debug_error("failed to get current connected device list with dbus, ret[0x%x]", ret);
607 if (!device_list->list) {
608 debug_error("Got device list null");
609 ret = MM_ERROR_SOUND_NO_DATA;
612 _mm_sound_client_device_list_dump(device_list->list);
619 int mm_sound_client_get_device_by_id(int device_id, mm_sound_device_t **device)
621 int ret = MM_ERROR_NONE;
625 if ((ret = mm_sound_proxy_get_device_by_id(device_id, device)) != MM_ERROR_NONE)
626 debug_error("failed to get device by id");
633 static bool device_is_match_direction(int direction, int mask)
635 if (mask == DEVICE_IO_DIRECTION_FLAGS || mask == 0)
638 if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_IN_FLAG) && (direction & MM_SOUND_DEVICE_IO_DIRECTION_IN))
640 if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_OUT_FLAG) && (direction & MM_SOUND_DEVICE_IO_DIRECTION_OUT))
642 if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_BOTH_FLAG) && (direction == MM_SOUND_DEVICE_IO_DIRECTION_BOTH))
648 static bool device_is_match_state(int state, int mask)
650 if (mask == DEVICE_STATE_FLAGS || mask == 0)
653 if ((mask & MM_SOUND_DEVICE_STATE_DEACTIVATED_FLAG) && (state == MM_SOUND_DEVICE_STATE_DEACTIVATED))
655 if ((mask & MM_SOUND_DEVICE_STATE_ACTIVATED_FLAG) && (state == MM_SOUND_DEVICE_STATE_ACTIVATED))
661 static bool device_is_match_type(const char *type, int mask)
664 const char *builtin_prefix = "builtin";
666 if (mask == DEVICE_TYPE_FLAGS || mask == 0)
669 is_builtin = !strncmp(type, builtin_prefix, strlen(builtin_prefix));
671 if ((mask & MM_SOUND_DEVICE_TYPE_INTERNAL_FLAG) && (is_builtin))
673 if ((mask & MM_SOUND_DEVICE_TYPE_EXTERNAL_FLAG) && (!is_builtin))
679 static bool device_is_match_with_mask(const char *type, int direction, int state, int mask)
681 if (mask == DEVICE_ALL_FLAG)
684 return (device_is_match_direction(direction, mask & DEVICE_IO_DIRECTION_FLAGS) &&
685 device_is_match_state(state, mask & DEVICE_STATE_FLAGS) &&
686 device_is_match_type(type, mask & DEVICE_TYPE_FLAGS));
689 static int _fill_sound_device(mm_sound_device_t *device_h, int device_id, const char *device_type,
690 int direction, int state, const char *name, int vendor_id, int product_id,
691 int *stream_id, int stream_num)
695 if (stream_num > 0 && stream_id == NULL) {
696 debug_error("stream_num is %d, but stream_id is NULL", stream_num);
700 if (stream_num > MAX_STREAM_ON_DEVICE) {
701 debug_error("too many streams on this device");
705 device_h->id = device_id;
706 device_h->io_direction = direction;
707 device_h->state = state;
708 MMSOUND_STRNCPY(device_h->name, name, MAX_DEVICE_NAME_NUM);
709 MMSOUND_STRNCPY(device_h->type, device_type, MAX_DEVICE_TYPE_STR_LEN);
710 device_h->vendor_id = vendor_id;
711 device_h->product_id = product_id;
713 if (stream_num > 0) {
714 device_h->stream_num = stream_num;
715 debug_log("%d streams on this device", stream_num);
716 for (i = 0; i < stream_num; i++) {
717 debug_log(" stream_id : %d", stream_id[i]);
718 device_h->stream_id[i] = stream_id[i];
721 device_h->stream_num = 0;
722 debug_log("There is no stream on this device");
728 static void _mm_sound_device_connected_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
729 int state, const char *name, int vendor_id, int product_id, int *stream_id, int stream_num,
730 gboolean is_connected, void *userdata)
732 mm_sound_device_t device_h;
733 struct callback_data *cb_data = (struct callback_data*) userdata;
736 debug_log("[Device %s] id(%d) type(%s) direction(%d) state(%d) name(%s) vendor-id(%04x) product-id(%04x)",
737 is_connected ? "Connected" : "Disconnected", device_id, device_type, io_direction,
738 state, name, vendor_id, product_id, is_connected);
740 if (cb_data == NULL) {
741 debug_warning("device connected changed callback data null");
745 device_flags = (int) cb_data->extra_data;
746 if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
749 if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name,
750 vendor_id, product_id, stream_id, stream_num) < 0) {
751 debug_error("Failed to fill sound device");
755 ((mm_sound_device_connected_cb)(cb_data->user_cb))(&device_h, is_connected, cb_data->user_data);
758 int mm_sound_client_add_device_connected_callback(int device_flags, mm_sound_device_connected_cb func,
759 void* userdata, unsigned int *subs_id)
761 int ret = MM_ERROR_NONE;
762 struct callback_data *cb_data = NULL;
766 GET_CB_DATA(cb_data, func, userdata, (void*) device_flags);
768 ret = mm_sound_proxy_add_device_connected_callback(device_flags,
769 _mm_sound_device_connected_callback_wrapper_func,
770 cb_data, g_free, subs_id);
771 if (ret == MM_ERROR_NONE)
772 g_need_emergent_exit = TRUE;
778 int mm_sound_client_remove_device_connected_callback(unsigned int subs_id)
780 int ret = MM_ERROR_NONE;
783 ret = mm_sound_proxy_remove_device_connected_callback(subs_id);
789 static void _mm_sound_device_info_changed_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
790 int state, const char *name, int vendor_id, int product_id, int *stream_id, int stream_num,
791 int changed_device_info_type, void *userdata)
793 mm_sound_device_t device_h;
794 struct callback_data *cb_data = (struct callback_data*) userdata;
797 debug_log("[Device Info Changed] id(%d) type(%s) direction(%d) state(%d) name(%s) "
798 "vendor-id(%04x) product-id(%04x) changed_info_type(%d)",
799 device_id, device_type, io_direction, state, name, vendor_id, product_id, changed_device_info_type);
801 if (cb_data == NULL) {
802 debug_warning("device info changed callback data null");
806 device_flags = (int) cb_data->extra_data;
807 if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
810 if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name,
811 vendor_id, product_id, stream_id, stream_num) < 0) {
812 debug_error("Failed to fill sound device");
816 ((mm_sound_device_info_changed_cb)(cb_data->user_cb))(&device_h, changed_device_info_type, cb_data->user_data);
819 int mm_sound_client_add_device_info_changed_callback(int device_flags, mm_sound_device_info_changed_cb func,
820 void *userdata, unsigned int *subs_id)
822 int ret = MM_ERROR_NONE;
823 struct callback_data *cb_data = (struct callback_data*) userdata;
827 GET_CB_DATA(cb_data, func, userdata, (void *) device_flags);
829 ret = mm_sound_proxy_add_device_info_changed_callback(device_flags,
830 _mm_sound_device_info_changed_callback_wrapper_func,
831 cb_data, g_free, subs_id);
837 int mm_sound_client_remove_device_info_changed_callback(unsigned int subs_id)
839 int ret = MM_ERROR_NONE;
842 ret = mm_sound_proxy_remove_device_info_changed_callback(subs_id);
849 static void _mm_sound_device_state_changed_callback_wrapper_func(int device_id, const char *device_type,
850 int io_direction, int state, const char *name, int vendor_id, int product_id,
851 int *stream_id, int stream_num, void *userdata)
853 mm_sound_device_t device_h;
854 struct callback_data *cb_data = (struct callback_data*) userdata;
857 debug_log("[Device State Changed] id(%d) type(%s) direction(%d) state(%d) name(%s)"
858 "vendor-id(%04x), product-id(%04x)",
859 device_id, device_type, io_direction, state, name, vendor_id, product_id);
861 if (cb_data == NULL) {
862 debug_warning("device state changed callback data null");
866 device_flags = (int) cb_data->extra_data;
868 if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
871 if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name,
872 vendor_id, product_id, stream_id, stream_num) < 0) {
873 debug_error("Failed to fill sound device");
877 ((mm_sound_device_state_changed_cb)(cb_data->user_cb))(&device_h, state, cb_data->user_data);
880 int mm_sound_client_add_device_state_changed_callback(int device_flags, mm_sound_device_state_changed_cb func,
881 void *userdata, unsigned int *id)
883 int ret = MM_ERROR_NONE;
884 struct callback_data *cb_data = (struct callback_data*) userdata;
888 GET_CB_DATA(cb_data, func, userdata, (void *) device_flags);
890 ret = mm_sound_proxy_add_device_state_changed_callback(device_flags,
891 _mm_sound_device_state_changed_callback_wrapper_func,
892 cb_data, g_free, id);
898 int mm_sound_client_remove_device_state_changed_callback(unsigned int id)
900 int ret = MM_ERROR_NONE;
903 ret = mm_sound_proxy_remove_device_state_changed_callback(id);
909 int mm_sound_client_is_stream_on_device(int stream_id, int device_id, bool *is_on)
911 int ret = MM_ERROR_NONE;
915 debug_error("Invalid Parameter");
916 ret = MM_ERROR_COMMON_INVALID_ARGUMENT;
920 if ((ret = mm_sound_proxy_is_stream_on_device(stream_id, device_id, is_on)) != MM_ERROR_NONE) {
921 debug_error("failed to query is stream on device, ret[0x%x]", ret);
930 int __convert_volume_type_to_str(int volume_type, char **volume_type_str)
932 int ret = MM_ERROR_NONE;
934 if (!volume_type_str) {
935 return MM_ERROR_COMMON_INVALID_ARGUMENT;
938 switch (volume_type) {
939 case VOLUME_TYPE_SYSTEM:
940 *volume_type_str = "system";
942 case VOLUME_TYPE_NOTIFICATION:
943 *volume_type_str = "notification";
945 case VOLUME_TYPE_ALARM:
946 *volume_type_str = "alarm";
948 case VOLUME_TYPE_RINGTONE:
949 *volume_type_str = "ringtone";
951 case VOLUME_TYPE_MEDIA:
952 *volume_type_str = "media";
954 case VOLUME_TYPE_CALL:
955 *volume_type_str = "call";
957 case VOLUME_TYPE_VOIP:
958 *volume_type_str = "voip";
960 case VOLUME_TYPE_VOICE:
961 *volume_type_str = "voice";
964 if (!strncmp(*volume_type_str,"", VOLUME_TYPE_LEN)) {
965 debug_error("could not find the volume_type[%d] in this switch case statement", volume_type);
966 ret = MM_ERROR_SOUND_INTERNAL;
968 debug_log("volume_type[%s]", *volume_type_str);
973 static int __convert_volume_type_to_int(const char *volume_type_str, volume_type_t *volume_type)
975 int ret = MM_ERROR_NONE;
977 if (!volume_type || !volume_type_str) {
978 return MM_ERROR_COMMON_INVALID_ARGUMENT;
981 if (!strncmp(volume_type_str, "system", VOLUME_TYPE_LEN)) {
982 *volume_type = VOLUME_TYPE_SYSTEM;
983 } else if (!strncmp(volume_type_str, "notification", VOLUME_TYPE_LEN)) {
984 *volume_type = VOLUME_TYPE_NOTIFICATION;
985 } else if (!strncmp(volume_type_str, "alarm", VOLUME_TYPE_LEN)) {
986 *volume_type = VOLUME_TYPE_ALARM;
987 } else if (!strncmp(volume_type_str, "ringtone", VOLUME_TYPE_LEN)) {
988 *volume_type = VOLUME_TYPE_RINGTONE;
989 } else if (!strncmp(volume_type_str, "media", VOLUME_TYPE_LEN)) {
990 *volume_type = VOLUME_TYPE_MEDIA;
991 } else if (!strncmp(volume_type_str, "call", VOLUME_TYPE_LEN)) {
992 *volume_type = VOLUME_TYPE_CALL;
993 } else if (!strncmp(volume_type_str, "voip", VOLUME_TYPE_LEN)) {
994 *volume_type = VOLUME_TYPE_VOIP;
995 } else if (!strncmp(volume_type_str, "voice", VOLUME_TYPE_LEN)) {
996 *volume_type = VOLUME_TYPE_VOICE;
998 debug_log("Invalid volume type : [%s]", volume_type_str);
999 ret = MM_ERROR_SOUND_INTERNAL;
1005 int mm_sound_client_set_volume_by_type(const int volume_type, const unsigned int volume_level)
1007 int ret = MM_ERROR_NONE;
1008 char *type_str = NULL;
1011 if ((ret = __convert_volume_type_to_str(volume_type, &type_str)) != MM_ERROR_NONE) {
1012 debug_error("volume type convert failed");
1016 ret = mm_sound_proxy_set_volume_by_type(type_str, volume_level);
1023 static void _mm_sound_volume_changed_callback_wrapper_func(const char *direction, const char *volume_type_str,
1024 int volume_level, void *userdata)
1026 volume_type_t volume_type = 0;
1027 struct callback_data *cb_data = (struct callback_data *) userdata;
1029 debug_log("direction : %s, volume_type : %s, volume_level : %d",
1030 direction, volume_type_str, volume_level);
1032 if (cb_data == NULL) {
1033 debug_warning("volume changed callback data null");
1037 if (__convert_volume_type_to_int(volume_type_str, &volume_type) != MM_ERROR_NONE) {
1038 debug_error("volume type convert failed");
1041 debug_log("Call volume changed user cb, direction : %s, vol_type : %s(%d), level : %u",
1042 direction, volume_type_str, volume_type, volume_level);
1043 ((mm_sound_volume_changed_cb)(cb_data->user_cb))(volume_type, volume_level, cb_data->user_data);
1046 int mm_sound_client_add_volume_changed_callback(mm_sound_volume_changed_cb func, void* userdata, unsigned int *subs_id)
1048 int ret = MM_ERROR_NONE;
1049 struct callback_data *cb_data = NULL;
1053 GET_CB_DATA(cb_data, func, userdata, NULL);
1055 ret = mm_sound_proxy_add_volume_changed_callback(_mm_sound_volume_changed_callback_wrapper_func, cb_data, g_free, subs_id);
1062 int mm_sound_client_remove_volume_changed_callback(unsigned int subs_id)
1064 int ret = MM_ERROR_NONE;
1067 ret = mm_sound_proxy_remove_volume_changed_callback(subs_id);
1073 int mm_sound_client_set_filter_by_type(const char *stream_type, const char *filter_name, const char *filter_parameters, const char *filter_group)
1075 int ret = MM_ERROR_NONE;
1078 ret = mm_sound_proxy_set_filter_by_type(stream_type, filter_name, filter_parameters, filter_group);
1084 int mm_sound_client_unset_filter_by_type(const char *stream_type)
1086 int ret = MM_ERROR_NONE;
1089 ret = mm_sound_proxy_unset_filter_by_type(stream_type);
1095 int mm_sound_client_control_filter_by_type(const char *stream_type, const char *filter_name, const char *filter_controls)
1097 int ret = MM_ERROR_NONE;
1100 ret = mm_sound_proxy_control_filter_by_type(stream_type, filter_name, filter_controls);
1107 static gboolean _interrupted_completed(gpointer *data)
1110 debug_error("data is null");
1113 if (!g_focus_session_interrupt_info.user_cb) {
1114 debug_error("user_cb is null");
1119 debug_msg("invoke user_cb(%p)", g_focus_session_interrupt_info.user_cb);
1120 (g_focus_session_interrupt_info.user_cb)(FOCUS_IS_RELEASED, (const char *)data, g_focus_session_interrupt_info.user_data);
1121 debug_msg("invoked");
1127 static void _session_interrupted_cb(int id, mm_sound_focus_type_e focus_type, mm_sound_focus_state_e state,
1128 const char *reason_for_change, const char *ext_info, void *user_data)
1130 debug_msg("id(%d), focus_type(%d), state(%d), reason(%s)", id, focus_type, state, reason_for_change);
1132 if (id != g_focus_session_interrupt_info.watch_cb_id) {
1133 debug_error("id is not valid(param id:%d, g_focus_session_interrupt_watch_cb_id:%d)",
1134 id, g_focus_session_interrupt_info.watch_cb_id);
1137 if (!g_focus_session_interrupt_info.user_cb) {
1138 debug_error("user callback is null");
1142 debug_msg(" >>> invoking session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
1143 if (state == FOCUS_IS_RELEASED)
1144 (g_focus_session_interrupt_info.user_cb)(FOCUS_IS_ACQUIRED, reason_for_change, g_focus_session_interrupt_info.user_data);
1146 debug_msg("INTERRUPTED COMPLETED case, append it to idle");
1147 g_idle_add((GSourceFunc)_interrupted_completed, strdup(reason_for_change));
1149 debug_msg(" <<< session interrupt callback finished");
1152 int mm_sound_client_set_session_interrupt_callback(mm_sound_focus_session_interrupt_cb callback, void* user_data)
1154 int ret = MM_ERROR_NONE;
1159 return MM_ERROR_INVALID_ARGUMENT;
1161 /* add internal focus watch callback */
1162 if (g_focus_session_interrupt_info.watch_cb_id == -1) {
1163 if ((ret = mm_sound_client_set_focus_watch_callback(getpid(), FOCUS_FOR_BOTH, true, true, _session_interrupted_cb, NULL,
1164 &g_focus_session_interrupt_info.watch_cb_id))) {
1165 debug_error("failed to mm_sound_client_set_focus_watch_callback(), ret(0x%x)", ret);
1169 g_focus_session_interrupt_info.user_cb = callback;
1170 g_focus_session_interrupt_info.user_data = user_data;
1176 int mm_sound_client_unset_session_interrupt_callback(void)
1178 int ret = MM_ERROR_NONE;
1182 if (!g_focus_session_interrupt_info.user_cb || g_focus_session_interrupt_info.watch_cb_id == -1) {
1183 debug_error("no callback to unset");
1184 return MM_ERROR_SOUND_INTERNAL;
1187 /* remove internal focus watch callback */
1188 if ((ret = mm_sound_client_unset_focus_watch_callback(g_focus_session_interrupt_info.watch_cb_id))) {
1189 debug_error("failed to mm_sound_client_unset_focus_watch_callback(), id(%d), ret(0x%x)",
1190 g_focus_session_interrupt_info.watch_cb_id, ret);
1193 g_focus_session_interrupt_info.watch_cb_id = -1;
1194 g_focus_session_interrupt_info.user_cb = NULL;
1195 g_focus_session_interrupt_info.user_data = NULL;
1201 static gpointer _focus_thread_func(gpointer data)
1203 unsigned int thread_id = (unsigned int)pthread_self();
1204 GMainLoop *focus_loop = (GMainLoop*)data;
1206 debug_warning(">>> thread id(%u), mainloop[%p]", thread_id, focus_loop);
1208 g_main_loop_run(focus_loop);
1209 debug_warning("<<< quit : thread id(%u), mainloop[%p]", thread_id, focus_loop);
1214 static gboolean _focus_fd_prepare(GSource *source, gint *timeout)
1217 debug_warning("[ PREPARE : %p, (%p, %d)", source, timeout, timeout? *timeout : -1);
1222 static gboolean _focus_fd_check(GSource * source)
1224 FocusSource* fsource = (FocusSource *)source;
1227 debug_error("GSource is null");
1231 debug_warning("CHECK : %p, 0x%x ]", source, fsource->pollfd.revents);
1233 if (fsource->poll_fd.revents & (POLLIN | POLLPRI))
1239 static gboolean _focus_fd_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
1241 debug_warning("*** DISPATCH : %p, (%p, %p)", source, callback, user_data);
1242 return callback(user_data);
1245 static void _focus_fd_finalize(GSource *source)
1247 debug_warning("### FINALIZE : %p", source);
1250 static int _focus_find_index_by_handle(int handle)
1253 for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1254 if (g_focus_sound_handle[i].focus_callback && handle == g_focus_sound_handle[i].handle) {
1255 /* debug_msg("found index(%d) for handle(%d)", i, handle);*/
1256 return (handle == FOCUS_HANDLE_INIT_VAL)? -1 : i;
1262 static int _focus_watch_find_index_by_handle(int handle)
1265 for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1266 if (g_focus_sound_handle[i].watch_callback && handle == g_focus_sound_handle[i].handle) {
1267 /* debug_msg("found index(%d) for watch handle(%d)", i, handle);*/
1268 return (handle == FOCUS_HANDLE_INIT_VAL)? -1 : i;
1274 static gboolean _focus_callback_handler(gpointer user_data)
1276 focus_sound_info_t *focus_handle = (focus_sound_info_t *)user_data;
1280 focus_cb_data_lib cb_data;
1282 debug_log(">>> thread id(%u)", (unsigned int)pthread_self());
1284 if (!focus_handle) {
1285 debug_error("focus_handle is null");
1286 return G_SOURCE_CONTINUE;
1288 poll_fd = &focus_handle->fsrc->poll_fd;
1289 debug_log("focus_handle(%p), poll_fd(%p)", focus_handle, poll_fd);
1291 memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1293 if (poll_fd->revents & (POLLIN | POLLPRI)) {
1294 int changed_state = -1;
1296 count = read(poll_fd->fd, &cb_data, sizeof(cb_data));
1298 char str_error[256];
1299 strerror_r(errno, str_error, sizeof(str_error));
1300 debug_error("GpollFD read fail, errno=%d(%s)",errno, str_error);
1301 return G_SOURCE_CONTINUE;
1303 changed_state = cb_data.state;
1305 g_mutex_lock(&focus_handle->focus_lock);
1307 tid = focus_handle->focus_tid;
1309 if (changed_state != -1) {
1310 debug_msg("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)",
1311 tid, cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type);
1312 if (focus_handle->focus_callback == NULL) {
1313 debug_error("focus callback is null..");
1314 g_mutex_unlock(&focus_handle->focus_lock);
1315 return G_SOURCE_CONTINUE;
1317 debug_msg("[CALLBACK(%p) START]", focus_handle->focus_callback);
1318 (focus_handle->focus_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type,
1319 cb_data.option, cb_data.ext_info, focus_handle->user_data);
1320 debug_msg("[CALLBACK END]");
1322 #ifdef CONFIG_ENABLE_RETCB
1326 unsigned int buf = 0;
1327 char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", focus_handle->focus_tid, cb_data.handle);
1328 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1330 char str_error[256];
1331 strerror_r(errno, str_error, sizeof(str_error));
1332 debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)",
1333 tid, tmpfd, filename2, errno, str_error);
1335 g_mutex_unlock(&focus_handle->focus_lock);
1336 return G_SOURCE_CONTINUE;
1338 /* buf contains data as below,
1339 * |<--12bits--><--4bits (reacquisition)--><--16bits (handle)-->| */
1340 buf = (unsigned int)((0x0000ffff & cb_data.handle) | (focus_handle->auto_reacquire << 16));
1341 rett = write(tmpfd, &buf, sizeof(buf));
1344 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)", tid, rett);
1349 g_mutex_unlock(&focus_handle->focus_lock);
1353 return G_SOURCE_CONTINUE;
1356 static gboolean _focus_watch_callback_handler(gpointer user_data)
1358 focus_sound_info_t *focus_handle = (focus_sound_info_t *)user_data;
1362 focus_cb_data_lib cb_data;
1364 debug_log(">>> thread id(%u)", (unsigned int)pthread_self());
1366 if (!focus_handle) {
1367 debug_error("focus_handle is null");
1368 return G_SOURCE_CONTINUE;
1370 poll_fd = &focus_handle->fsrc->poll_fd;
1371 debug_log("focus_handle(%p), poll_fd(%p)", focus_handle, poll_fd);
1373 memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1375 if (poll_fd->revents & (POLLIN | POLLPRI)) {
1376 count = read(poll_fd->fd, &cb_data, sizeof(cb_data));
1378 char str_error[256];
1379 strerror_r(errno, str_error, sizeof(str_error));
1380 debug_error("GpollFD read fail, errno=%d(%s)",errno, str_error);
1381 return G_SOURCE_CONTINUE;
1384 if (!focus_handle->is_used) {
1385 debug_warning("unsetting watch calllback has been already requested");
1386 goto SKIP_CB_AND_RET;
1389 g_mutex_lock(&focus_handle->focus_lock);
1391 tid = focus_handle->focus_tid;
1393 debug_msg("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)",
1394 tid, cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type);
1396 if (focus_handle->watch_callback == NULL) {
1397 debug_warning("watch callback is null..");
1398 goto SKIP_CB_AND_RET;
1400 if (focus_handle->unset_watch_callback_requested == true) {
1401 debug_warning("unset_watch_callback_requested..");
1402 goto SKIP_CB_AND_RET;
1405 debug_msg("[CALLBACK(%p) START]", focus_handle->watch_callback);
1406 (focus_handle->watch_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type,
1407 cb_data.ext_info, focus_handle->user_data);
1408 debug_msg("[CALLBACK END]");
1411 #ifdef CONFIG_ENABLE_RETCB
1416 char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr", focus_handle->focus_tid, cb_data.handle);
1417 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1419 char str_error[256];
1420 strerror_r(errno, str_error, sizeof(str_error));
1421 debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)",
1422 tid, tmpfd, filename2, errno, str_error);
1424 g_mutex_unlock(&focus_handle->focus_lock);
1425 return G_SOURCE_CONTINUE;
1427 buf = cb_data.handle;
1428 rett = write(tmpfd, &buf, sizeof(buf));
1431 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)", tid, rett);
1436 if (focus_handle->is_used) {
1437 debug_msg("unlock focus_lock = %p", &focus_handle->focus_lock);
1438 g_mutex_unlock(&focus_handle->focus_lock);
1443 return G_SOURCE_CONTINUE;
1446 static void _focus_open_callback(int index, bool is_for_watching)
1453 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1454 debug_error("Invalid focus handle index [%d]", index);
1458 if (is_for_watching) {
1459 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch",
1460 g_focus_sound_handle[index].focus_tid,
1461 g_focus_sound_handle[index].handle);
1463 filename = g_strdup_printf("/tmp/FOCUS.%d.%d",
1464 g_focus_sound_handle[index].focus_tid,
1465 g_focus_sound_handle[index].handle);
1467 pre_mask = umask(0);
1468 if (mknod(filename, S_IFIFO|0666, 0)) {
1469 debug_error("mknod() failure, errno(%d)", errno);
1472 g_focus_sound_handle[index].focus_fd = open(filename, O_RDWR|O_NONBLOCK);
1473 if (g_focus_sound_handle[index].focus_fd == -1) {
1474 debug_error("Open fail : index(%d), file open error(%d)", index, errno);
1476 debug_log("Open success : index(%d), filename(%s), fd(%d)",
1477 index, filename, g_focus_sound_handle[index].focus_fd);
1482 #ifdef CONFIG_ENABLE_RETCB
1485 if (is_for_watching) {
1486 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr",
1487 g_focus_sound_handle[index].focus_tid,
1488 g_focus_sound_handle[index].handle);
1490 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr",
1491 g_focus_sound_handle[index].focus_tid,
1492 g_focus_sound_handle[index].handle);
1494 pre_mask = umask(0);
1495 if (mknod(filename2, S_IFIFO | 0666, 0)) {
1496 debug_error("mknod() failure, errno(%d)", errno);
1506 void _focus_close_callback(int index, bool is_for_watching)
1508 char *filename = NULL;
1512 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1513 debug_error("Invalid focus handle index [%d]", index);
1517 if (g_focus_sound_handle[index].focus_fd < 0) {
1518 debug_error("Close fail : index(%d)", index);
1520 close(g_focus_sound_handle[index].focus_fd);
1521 debug_log("Close Success : index(%d)", index);
1524 if (is_for_watching) {
1525 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch",
1526 g_focus_sound_handle[index].focus_tid,
1527 g_focus_sound_handle[index].handle);
1529 filename = g_strdup_printf("/tmp/FOCUS.%d.%d",
1530 g_focus_sound_handle[index].focus_tid,
1531 g_focus_sound_handle[index].handle);
1533 if (remove(filename)) {
1534 debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)",
1540 #ifdef CONFIG_ENABLE_RETCB
1543 if (is_for_watching) {
1544 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr",
1545 g_focus_sound_handle[index].focus_tid,
1546 g_focus_sound_handle[index].handle);
1548 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr",
1549 g_focus_sound_handle[index].focus_tid,
1550 g_focus_sound_handle[index].handle);
1552 if (remove(filename2)) {
1553 debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)",
1564 static GSourceFuncs event_funcs = {
1565 .prepare = _focus_fd_prepare,
1566 .check = _focus_fd_check,
1567 .dispatch = _focus_fd_dispatch,
1568 .finalize = _focus_fd_finalize,
1571 static bool _focus_add_sound_callback(int index, focus_callback_handler_t focus_cb_handler)
1573 FocusSource *fsrc = NULL;
1574 GSource *src = NULL;
1579 g_mutex_init(&g_focus_sound_handle[index].focus_lock);
1581 src = g_source_new(&event_funcs, sizeof(FocusSource));
1583 debug_error("failed to g_source_new for focus source");
1587 fsrc = (FocusSource*) src;
1589 fsrc->poll_fd.fd = g_focus_sound_handle[index].focus_fd;
1590 fsrc->poll_fd.events = (gushort)(POLLIN | POLLPRI);
1591 g_source_add_poll(src, &fsrc->poll_fd);
1593 g_source_set_callback(src, focus_cb_handler, (gpointer)&g_focus_sound_handle[index], NULL);
1595 debug_warning("fsrc(%p), src_funcs(%p), pollfd(%p), fd(%d)",
1596 fsrc, &event_funcs, &fsrc->poll_fd, fsrc->poll_fd.fd);
1598 fsrc_id = g_source_attach(src, g_main_loop_get_context(g_focus_sound_handle[index].focus_loop));
1600 debug_error("failed to attach the source to context");
1603 g_source_unref(src);
1605 g_focus_sound_handle[index].fsrc = fsrc;
1612 g_source_unref(src);
1617 static bool _focus_remove_sound_callback(int index)
1619 focus_sound_info_t *h = NULL;
1623 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1624 debug_error("Invalid focus handle index [%d]", index);
1628 h = &g_focus_sound_handle[index];
1630 g_source_destroy((GSource *)h->fsrc);
1634 h->focus_callback = NULL;
1635 h->watch_callback = NULL;
1637 g_mutex_clear(&h->focus_lock);
1644 static void _focus_add_callback(int index, bool is_for_watching)
1648 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1649 debug_error("Invalid focus handle index [%d]", index);
1653 if (!is_for_watching) {
1654 if (!_focus_add_sound_callback(index, _focus_callback_handler))
1655 debug_error("failed to _focus_add_sound_callback(%p)", _focus_callback_handler);
1656 } else { // need to check if it's necessary
1657 if (!_focus_add_sound_callback(index, _focus_watch_callback_handler))
1658 debug_error("failed to _focus_add_sound_callback(%p)", _focus_watch_callback_handler);
1663 static void _focus_remove_callback(int index)
1666 if (!_focus_remove_sound_callback(index))
1667 debug_error("failed to __focus_remove_sound_callback()");
1671 static void _focus_init_callback(int index, bool is_for_watching)
1674 _focus_open_callback(index, is_for_watching);
1675 _focus_add_callback(index, is_for_watching);
1679 static void _focus_deinit_callback(int index, bool is_for_watching)
1682 _focus_remove_callback(index);
1683 _focus_close_callback(index, is_for_watching);
1687 #define INTERVAL_MS 20
1688 static int _focus_loop_is_running_timed_wait(GMainLoop *focus_loop, int timeout_ms)
1690 int reduced_time_ms = timeout_ms;
1691 if (!focus_loop || timeout_ms < 0) {
1692 debug_error("invalid argument, focus_loop(%p), timeout_ms(%d)", focus_loop, timeout_ms);
1693 return MM_ERROR_INVALID_ARGUMENT;
1697 if (g_main_loop_is_running(focus_loop))
1698 return MM_ERROR_NONE;
1700 usleep(INTERVAL_MS * 1000);
1701 if (reduced_time_ms < timeout_ms)
1702 debug_warning("reduced_time_ms(%d)", reduced_time_ms);
1703 } while ((reduced_time_ms -= INTERVAL_MS) >= 0);
1705 debug_error("focus_loop is not running for timeout_ms(%d), focus_loop(%p) ", timeout_ms, focus_loop);
1707 return MM_ERROR_SOUND_INTERNAL;
1710 #define LOOP_RUNNING_WAIT_TIME_MS 200
1711 static int _focus_init_context(int index)
1713 int ret = MM_ERROR_NONE;
1714 GMainContext *focus_context;
1718 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1719 debug_error("index(%d) is not valid", index);
1720 return MM_ERROR_INVALID_ARGUMENT;
1723 focus_context = g_main_context_new();
1724 g_focus_sound_handle[index].focus_loop = g_main_loop_new(focus_context, FALSE);
1725 g_main_context_unref(focus_context);
1726 if (g_focus_sound_handle[index].focus_loop == NULL) {
1727 debug_error("could not create mainloop..");
1731 g_focus_sound_handle[index].focus_cb_thread = g_thread_new("focus-cb-thread",
1733 g_focus_sound_handle[index].focus_loop);
1734 if (g_focus_sound_handle[index].focus_cb_thread == NULL) {
1735 debug_error("could not create thread..");
1739 debug_warning("focus cb thread[%p] with mainloop[%p] is created for index(%d)",
1740 g_focus_sound_handle[index].focus_cb_thread, g_focus_sound_handle[index].focus_loop, index);
1742 if ((ret = _focus_loop_is_running_timed_wait(g_focus_sound_handle[index].focus_loop, LOOP_RUNNING_WAIT_TIME_MS))) {
1743 debug_error("failed to _focus_loop_is_running_timed_wait(), ret[0x%x]", ret);
1749 return MM_ERROR_NONE;
1752 if (g_focus_sound_handle[index].focus_loop) {
1753 g_main_loop_unref(g_focus_sound_handle[index].focus_loop);
1754 g_focus_sound_handle[index].focus_loop = NULL;
1756 return MM_ERROR_SOUND_INTERNAL;
1759 static void _focus_deinit_context(int index)
1763 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1764 debug_error("index(%d) is not valid", index);
1768 if (!g_focus_sound_handle[index].focus_loop || !g_focus_sound_handle[index].focus_cb_thread) {
1769 debug_error("focus_loop[%p] or focus_cb_thread[%p] is null",
1770 g_focus_sound_handle[index].focus_loop, g_focus_sound_handle[index].focus_cb_thread);
1774 g_main_loop_quit(g_focus_sound_handle[index].focus_loop);
1775 g_thread_join(g_focus_sound_handle[index].focus_cb_thread);
1776 debug_warning("after thread join, thread[%p], mainloop[%p] for index(%d)",
1777 g_focus_sound_handle[index].focus_cb_thread, g_focus_sound_handle[index].focus_loop, index);
1778 g_main_loop_unref(g_focus_sound_handle[index].focus_loop);
1779 g_focus_sound_handle[index].focus_loop = NULL;
1780 g_focus_sound_handle[index].focus_cb_thread = NULL;
1785 int mm_sound_client_get_unique_id(int *id)
1787 int ret = MM_ERROR_NONE;
1792 ret = MM_ERROR_INVALID_ARGUMENT;
1794 ret = mm_sound_proxy_get_unique_id(id);
1801 int mm_sound_client_is_focus_cb_thread(GThread *mine, bool *result)
1803 int ret = MM_ERROR_NONE;
1806 if (!mine || !result)
1807 ret = MM_ERROR_INVALID_ARGUMENT;
1810 for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1811 if (!g_focus_sound_handle[i].is_used)
1813 if (g_focus_sound_handle[i].focus_cb_thread == mine) {
1823 int mm_sound_client_register_focus(int id, int pid, const char *stream_type, bool is_for_session,
1824 mm_sound_focus_changed_cb callback, void* user_data)
1826 int ret = MM_ERROR_NONE;
1831 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1835 for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
1836 if (g_focus_sound_handle[index].is_used == false) {
1837 g_focus_sound_handle[index].is_used = true;
1842 g_focus_sound_handle[index].focus_tid = instance;
1843 g_focus_sound_handle[index].handle = id;
1844 g_focus_sound_handle[index].focus_callback = callback;
1845 g_focus_sound_handle[index].user_data = user_data;
1846 g_focus_sound_handle[index].is_for_session = is_for_session;
1847 g_focus_sound_handle[index].auto_reacquire = true;
1849 ret = mm_sound_proxy_register_focus(id, pid, stream_type, callback, is_for_session, user_data);
1850 if (ret == MM_ERROR_NONE) {
1851 debug_msg("Success to register focus");
1852 g_need_emergent_exit = TRUE;
1853 if (_focus_init_context(index)) {
1854 ret = MM_ERROR_SOUND_INTERNAL;
1858 debug_error("Error occurred : 0x%x",ret);
1862 _focus_init_callback(index, false);
1866 g_focus_sound_handle[index].is_used = false;
1869 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1875 int mm_sound_client_unregister_focus(int id)
1877 int ret = MM_ERROR_NONE;
1882 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1884 index = _focus_find_index_by_handle(id);
1886 debug_error("Could not find index");
1887 ret = MM_ERROR_INVALID_ARGUMENT;
1890 instance = g_focus_sound_handle[index].focus_tid;
1892 if (!g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1893 debug_warning("maybe focus_callback is being called, try one more time..");
1894 usleep(2500000); // 2.5 sec
1895 if (g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1896 debug_msg("finally got focus_lock");
1900 ret = mm_sound_proxy_unregister_focus(instance, id, g_focus_sound_handle[index].is_for_session);
1901 if (ret == MM_ERROR_NONE)
1902 debug_msg("Success to unregister focus");
1904 debug_error("Error occurred : 0x%x", ret);
1906 g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
1908 _focus_deinit_callback(index, false);
1909 g_focus_sound_handle[index].focus_fd = 0;
1910 g_focus_sound_handle[index].focus_tid = 0;
1911 g_focus_sound_handle[index].handle = 0;
1912 g_focus_sound_handle[index].is_used = false;
1913 _focus_deinit_context(index);
1917 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1922 int mm_sound_client_set_focus_reacquisition(int id, bool reacquisition, bool is_for_session)
1924 int ret = MM_ERROR_NONE;
1931 /* Since the muse server which uses this library for multiple handles executes requests from clients serially,
1932 * we can skip locking/unlocking the mutex for this case. */
1933 if (!is_for_session)
1934 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1936 index = _focus_find_index_by_handle(id);
1938 debug_error("Could not find index");
1939 ret = MM_ERROR_INVALID_ARGUMENT;
1942 instance = g_focus_sound_handle[index].focus_tid;
1944 ret = mm_sound_client_is_focus_cb_thread(g_thread_self(), &result);
1946 debug_error("mm_sound_client_is_focus_cb_thread failed");
1948 } else if (!result) {
1949 ret = mm_sound_proxy_set_focus_reacquisition(instance, id, reacquisition, is_for_session);
1950 if (ret == MM_ERROR_NONE) {
1951 debug_msg("Success to set focus reacquisition to [%d]", reacquisition);
1953 debug_error("Error occurred : 0x%x",ret);
1957 debug_warning("Inside the focus cb thread, set focus reacquisition to [%d]", reacquisition);
1960 g_focus_sound_handle[index].auto_reacquire = reacquisition;
1961 debug_msg("set focus reacquisition(%d) for id(%d)", reacquisition, id);
1964 if (!is_for_session)
1965 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1970 int mm_sound_client_get_focus_reacquisition(int id, bool *reacquisition)
1972 int ret = MM_ERROR_NONE;
1977 if (!reacquisition) {
1978 debug_error("Invalid parameter");
1979 return MM_ERROR_INVALID_ARGUMENT;
1982 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1984 index = _focus_find_index_by_handle(id);
1986 debug_error("Could not find index");
1987 ret = MM_ERROR_INVALID_ARGUMENT;
1991 *reacquisition = g_focus_sound_handle[index].auto_reacquire;
1992 debug_msg("get focus reacquisition(%d) for id(%d)", *reacquisition, id);
1995 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2000 int mm_sound_client_get_acquired_focus_stream_type(int focus_type, char **stream_type, int *option, char **ext_info)
2002 int ret = MM_ERROR_NONE;
2003 char *stream_type_str = NULL;
2004 char *ext_info_str = NULL;
2008 ret = mm_sound_proxy_get_acquired_focus_stream_type(focus_type, &stream_type_str, option, &ext_info_str);
2009 if (ret == MM_ERROR_NONE) {
2010 debug_msg("Success to get stream type of acquired focus, stream_type(%s), ext_info(%s)",
2011 stream_type_str, ext_info_str);
2012 *stream_type = strdup(stream_type_str);
2013 *ext_info = strdup(ext_info_str);
2014 g_free(stream_type_str);
2015 g_free(ext_info_str);
2017 debug_error("Error occurred : 0x%x",ret);
2024 int mm_sound_client_acquire_focus(int id, mm_sound_focus_type_e type, int option, const char *ext_info)
2026 int ret = MM_ERROR_NONE;
2031 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2033 index = _focus_find_index_by_handle(id);
2035 debug_error("Could not find index");
2036 ret = MM_ERROR_INVALID_ARGUMENT;
2039 instance = g_focus_sound_handle[index].focus_tid;
2041 ret = mm_sound_proxy_acquire_focus(instance, id, type, option, ext_info, g_focus_sound_handle[index].is_for_session);
2042 if (ret == MM_ERROR_NONE)
2043 debug_msg("Success to acquire focus");
2045 debug_error("Error occurred : 0x%x", ret);
2048 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2053 int mm_sound_client_release_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_release_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 release focus");
2074 debug_error("Error occurred : 0x%x",ret);
2077 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2082 int mm_sound_client_update_stream_focus_status(int id, unsigned int status)
2084 int ret = MM_ERROR_NONE;
2087 if ((ret = mm_sound_proxy_update_stream_focus_status(id, status)) != MM_ERROR_NONE)
2088 debug_error("failed to update stream focus status, ret[0x%x]", ret);
2094 int mm_sound_client_deliver_focus(int pid, int src_id, int dst_id, mm_sound_focus_type_e focus_type)
2096 int ret = MM_ERROR_NONE;
2099 if ((ret = mm_sound_proxy_deliver_focus(pid, src_id, dst_id, focus_type)) != MM_ERROR_NONE)
2100 debug_error("failed to deliver focus, ret[0x%x]", ret);
2106 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,
2107 mm_sound_focus_changed_watch_cb callback, void* user_data, int *id)
2109 int ret = MM_ERROR_NONE;
2116 return MM_ERROR_INVALID_ARGUMENT;
2118 //pthread_mutex_lock(&g_thread_mutex2);
2122 ret = mm_sound_proxy_get_unique_id(id);
2126 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2128 for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
2129 if (g_focus_sound_handle[index].is_used == false) {
2130 g_focus_sound_handle[index].is_used = true;
2135 g_focus_sound_handle[index].focus_tid = instance;
2136 g_focus_sound_handle[index].handle = *id;
2137 g_focus_sound_handle[index].watch_callback = callback;
2138 g_focus_sound_handle[index].user_data = user_data;
2139 g_focus_sound_handle[index].is_for_session = is_for_session;
2140 g_focus_sound_handle[index].is_for_monitor = is_for_monitor;
2141 g_focus_sound_handle[index].unset_watch_callback_requested = false;
2143 ret = mm_sound_proxy_set_focus_watch_callback(pid, g_focus_sound_handle[index].handle, focus_type,
2144 is_for_session, is_for_monitor, callback, user_data);
2146 if (ret == MM_ERROR_NONE) {
2147 debug_msg("Success to watch focus");
2148 g_need_emergent_exit = TRUE;
2149 if (_focus_init_context(index)) {
2150 ret = MM_ERROR_SOUND_INTERNAL;
2154 debug_error("Error occurred : 0x%x",ret);
2158 _focus_init_callback(index, true);
2162 g_focus_sound_handle[index].is_used = false;
2165 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2170 int mm_sound_client_request_unset_focus_watch_callback(int id)
2172 int ret = MM_ERROR_NONE;
2176 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2178 index = _focus_watch_find_index_by_handle(id);
2180 debug_error("Could not find index");
2181 ret = MM_ERROR_INVALID_ARGUMENT;
2184 g_focus_sound_handle[index].unset_watch_callback_requested = true;
2187 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2192 int mm_sound_client_unset_focus_watch_callback(int id)
2194 int ret = MM_ERROR_NONE;
2198 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2200 index = _focus_watch_find_index_by_handle(id);
2202 debug_error("Could not find index");
2203 ret = MM_ERROR_INVALID_ARGUMENT;
2207 g_mutex_lock(&g_focus_sound_handle[index].focus_lock);
2209 g_focus_sound_handle[index].is_used = false;
2211 ret = mm_sound_proxy_unset_focus_watch_callback(g_focus_sound_handle[index].focus_tid,
2212 g_focus_sound_handle[index].handle,
2213 g_focus_sound_handle[index].is_for_session);
2215 if (ret == MM_ERROR_NONE)
2216 debug_msg("Success to unwatch focus");
2218 debug_error("Error occurred : 0x%x",ret);
2221 g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
2223 _focus_deinit_callback(index, true);
2224 g_focus_sound_handle[index].focus_fd = 0;
2225 g_focus_sound_handle[index].focus_tid = 0;
2226 g_focus_sound_handle[index].handle = 0;
2227 _focus_deinit_context(index);
2230 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2235 static gboolean _idle_event_callback(void *data)
2237 focus_idle_event_t *idle_event_data = (focus_idle_event_t*)data;
2238 int ret = MM_ERROR_NONE;
2241 debug_error("data is null");
2245 debug_msg("idle_event_data(%p): type(%d), data(%d)",
2246 idle_event_data, idle_event_data->type, idle_event_data->data);
2248 switch (idle_event_data->type) {
2249 case IDLE_EVENT_TYPE_UNSET_FOCUS_WATCH_CB:
2250 if ((ret = mm_sound_client_unset_focus_watch_callback(idle_event_data->data)))
2251 debug_error("Could not unset focus watch callback, id(%d), ret = %x", idle_event_data->data, ret);
2253 case IDLE_EVENT_TYPE_UNREGISTER_FOCUS:
2254 if ((ret = mm_sound_client_unregister_focus(idle_event_data->data)))
2255 debug_error("Could not unregister focus, id(%d), ret = %x", idle_event_data->data, ret);
2258 debug_warning("invalid type(%d)", idle_event_data->type);
2262 g_free(idle_event_data);
2264 g_idle_event_src = 0;
2266 MMSOUND_LEAVE_CRITICAL_SECTION(&g_event_mutex);
2271 int mm_sound_client_execute_focus_func_in_main_context(focus_idle_event_type_e type, int data)
2273 focus_idle_event_t *idle_event_data = NULL;
2275 if (IDLE_EVENT_TYPE_MAX < type)
2276 return MM_ERROR_INVALID_ARGUMENT;
2278 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_event_mutex, MM_ERROR_SOUND_INTERNAL);
2280 idle_event_data = g_new0(focus_idle_event_t, 1);
2281 idle_event_data->type = type;
2282 idle_event_data->data = data;
2284 g_idle_event_src = g_idle_add_full(G_PRIORITY_HIGH,
2285 (GSourceFunc)_idle_event_callback,
2286 (gpointer)idle_event_data,
2289 return MM_ERROR_NONE;
2294 int mm_sound_client_add_test_callback(mm_sound_test_cb func, void* user_data, unsigned int *subs_id)
2296 int ret = MM_ERROR_NONE;
2300 ret = mm_sound_proxy_add_test_callback(func, user_data, g_free, subs_id);
2306 int mm_sound_client_remove_test_callback(unsigned int subs_id)
2308 int ret = MM_ERROR_NONE;
2311 ret = mm_sound_proxy_remove_test_callback(subs_id);
2318 int mm_sound_client_test(int a, int b, int* getv)
2320 int ret = MM_ERROR_NONE;
2324 ret = mm_sound_proxy_test(a, b, getv);
2325 debug_log("%d * %d -> result : %d", a, b, *getv);