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;
103 bool is_for_session; /* will be removed when the session concept is completely left out*/
104 } focus_sound_info_t;
111 char stream_type[MAX_STREAM_TYPE_LEN];
112 char ext_info[MM_SOUND_NAME_NUM];
117 mm_sound_focus_session_interrupt_cb user_cb;
119 } focus_session_interrupt_info_t;
121 typedef gboolean (*focus_callback_handler_t)(gpointer user_data);
123 focus_sound_info_t g_focus_sound_handle[FOCUS_HANDLE_MAX];
124 focus_session_interrupt_info_t g_focus_session_interrupt_info = {NULL, NULL};
125 static pthread_mutex_t g_index_mutex = PTHREAD_MUTEX_INITIALIZER;
126 static pthread_mutex_t g_event_mutex = PTHREAD_MUTEX_INITIALIZER;
127 guint g_focus_signal_handle;
128 guint g_idle_event_src;
131 gboolean g_need_emergent_exit = FALSE;
134 /* handle to watch end of playing */
136 /* subscription id to unsubscribe when handle ended */
138 } play_sound_end_callback_data_t;
140 typedef struct _focus_idle_event {
141 focus_idle_event_type_e type;
143 } focus_idle_event_t;
145 void _system_signal_handler(int signo)
147 int ret = MM_ERROR_NONE;
148 sigset_t old_mask, all_mask;
150 debug_warning("Got signal : signo(%d)", signo);
154 sigfillset(&all_mask);
155 sigprocmask(SIG_BLOCK, &all_mask, &old_mask);
157 if (g_need_emergent_exit) {
158 ret = mm_sound_proxy_emergent_exit(getpid());
159 if (ret == MM_ERROR_NONE)
160 debug_msg("[Client] Success to emergnet_exit\n");
162 debug_error("[Client] Error occurred : 0x%x \n",ret);
165 sigprocmask(SIG_SETMASK, &old_mask, NULL);
170 sigaction(SIGINT, &system_int_old_action, NULL);
174 sigaction(SIGABRT, &system_abrt_old_action, NULL);
178 sigaction(SIGSEGV, &system_segv_old_action, NULL);
182 sigaction(SIGTERM, &system_term_old_action, NULL);
186 sigaction(SIGSYS, &system_sys_old_action, NULL);
190 sigaction(SIGXCPU, &system_xcpu_old_action, NULL);
197 debug_warning("signal handling end");
200 int mm_sound_client_initialize(void)
202 int ret = MM_ERROR_NONE;
205 mm_sound_proxy_initialize();
206 g_idle_event_src = 0;
209 struct sigaction system_action;
210 system_action.sa_handler = _system_signal_handler;
211 system_action.sa_flags = SA_NOCLDSTOP;
213 sigemptyset(&system_action.sa_mask);
215 sigaction(SIGINT, &system_action, &system_int_old_action);
216 sigaction(SIGABRT, &system_action, &system_abrt_old_action);
217 sigaction(SIGSEGV, &system_action, &system_segv_old_action);
218 sigaction(SIGTERM, &system_action, &system_term_old_action);
219 sigaction(SIGSYS, &system_action, &system_sys_old_action);
220 sigaction(SIGXCPU, &system_action, &system_xcpu_old_action);
226 int mm_sound_client_finalize(void)
228 int ret = MM_ERROR_NONE;
232 if (g_need_emergent_exit) {
233 ret = mm_sound_proxy_emergent_exit(getpid());
234 if (ret == MM_ERROR_NONE)
235 debug_msg("[Client] Success to emergnet_exit\n");
237 debug_error("[Client] Error occurred : 0x%x \n",ret);
240 sigaction(SIGINT, &system_int_old_action, NULL);
241 sigaction(SIGABRT, &system_abrt_old_action, NULL);
242 sigaction(SIGSEGV, &system_segv_old_action, NULL);
243 sigaction(SIGTERM, &system_term_old_action, NULL);
244 sigaction(SIGSYS, &system_sys_old_action, NULL);
245 sigaction(SIGXCPU, &system_xcpu_old_action, NULL);
247 ret = mm_sound_proxy_finalize();
250 if (g_idle_event_src > 0) {
251 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_event_mutex, MM_ERROR_SOUND_INTERNAL);
252 g_source_remove(g_idle_event_src);
253 MMSOUND_LEAVE_CRITICAL_SECTION(&g_event_mutex);
261 void mm_sound_convert_volume_type_to_stream_type(int volume_type, char *stream_type)
263 switch (volume_type) {
264 case VOLUME_TYPE_SYSTEM:
265 MMSOUND_STRNCPY(stream_type, "system", MM_SOUND_STREAM_TYPE_LEN);
267 case VOLUME_TYPE_NOTIFICATION:
268 MMSOUND_STRNCPY(stream_type, "notification", MM_SOUND_STREAM_TYPE_LEN);
270 case VOLUME_TYPE_ALARM:
271 MMSOUND_STRNCPY(stream_type, "alarm", MM_SOUND_STREAM_TYPE_LEN);
273 case VOLUME_TYPE_RINGTONE:
274 MMSOUND_STRNCPY(stream_type, "ringtone-voip", MM_SOUND_STREAM_TYPE_LEN);
276 case VOLUME_TYPE_MEDIA:
277 MMSOUND_STRNCPY(stream_type, "media", MM_SOUND_STREAM_TYPE_LEN);
279 case VOLUME_TYPE_CALL:
280 MMSOUND_STRNCPY(stream_type, "call-voice", MM_SOUND_STREAM_TYPE_LEN);
282 case VOLUME_TYPE_VOIP:
283 MMSOUND_STRNCPY(stream_type, "voip", MM_SOUND_STREAM_TYPE_LEN);
285 case VOLUME_TYPE_VOICE:
286 MMSOUND_STRNCPY(stream_type, "voice-information", MM_SOUND_STREAM_TYPE_LEN);
289 MMSOUND_STRNCPY(stream_type, "media", MM_SOUND_STREAM_TYPE_LEN);
293 debug_msg("volume type (%d) converted to stream type (%s)", volume_type, stream_type);
297 /*****************************************************************************************
298 DBUS SUPPORTED FUNCTIONS
299 ******************************************************************************************/
301 void _mm_sound_client_focus_signal_callback(mm_sound_signal_name_t signal, int value, void *user_data)
303 int ret = MM_ERROR_NONE;
306 debug_msg("focus signal received, value = %d", value);
309 ret = mm_sound_proxy_clear_focus(getpid());
311 debug_error("clear focus failed ret = 0x%x", ret);
312 mm_sound_unsubscribe_signal(g_focus_signal_handle);
313 g_focus_signal_handle = 0;
318 int mm_sound_client_play_tone(int number, int volume_config, double volume,
319 int time, int *handle, bool enable_session)
321 int ret = MM_ERROR_NONE;
322 // int instance = -1; /* instance is unique to communicate with server : client message queue filter type */
323 int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(volume_config);
324 char stream_type[MM_SOUND_STREAM_TYPE_LEN] = {0, };
328 /* read session information */
329 int session_type = MM_SESSION_TYPE_MEDIA;
330 int session_options = 0;
331 int is_focus_registered = 0;
333 ret = mm_sound_get_signal_value(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &is_focus_registered);
335 debug_error("mm_sound_get_signal_value failed [0x%x]", ret);
336 return MM_ERROR_POLICY_INTERNAL;
339 if (is_focus_registered)
340 enable_session = false;
342 if (enable_session) {
343 if (MM_ERROR_NONE != _mm_session_util_read_information(-1, &session_type, &session_options)) {
344 debug_warning("[Client] Read Session Information failed. use default \"media\" type\n");
345 session_type = MM_SESSION_TYPE_MEDIA;
347 if(MM_ERROR_NONE != mm_session_init(session_type)) {
348 debug_critical("[Client] MMSessionInit() failed\n");
349 return MM_ERROR_POLICY_INTERNAL;
354 // instance = getpid();
355 //debug_log("[Client] pid for client ::: [%d]\n", instance);
358 debug_msg("[Client] Input number : %d\n", number);
359 /* Send req memory */
361 mm_sound_convert_volume_type_to_stream_type(volume_type, stream_type);
362 ret = mm_sound_proxy_play_tone(number, time, volume, volume_config,
363 session_type, session_options, getpid(), enable_session, handle, stream_type, -1);
365 if (enable_session && !g_focus_signal_handle) {
366 ret = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &g_focus_signal_handle,
367 _mm_sound_client_focus_signal_callback, NULL);
369 debug_error("mm_sound_subscribe_signal failed [0x%x]", ret);
370 return MM_ERROR_POLICY_INTERNAL;
379 int mm_sound_client_play_tone_with_stream_info(int tone, char *stream_type, int stream_id,
380 double volume, int duration, int *handle)
382 int ret = MM_ERROR_NONE;
386 ret = mm_sound_proxy_play_tone_with_stream_info(getpid(), tone, stream_type, stream_id, volume, duration, handle);
392 static void _mm_sound_stop_callback_wrapper_func(int ended_handle, void *userdata)
394 struct callback_data *cb_data = (struct callback_data*) userdata;
395 play_sound_end_callback_data_t *end_cb_data;
397 debug_log("[Wrapper CB][Play Stop] ended_handle : %d", ended_handle);
399 if (cb_data == NULL) {
400 debug_warning("stop callback data null");
404 end_cb_data = (play_sound_end_callback_data_t*) cb_data->extra_data;
406 if (ended_handle == end_cb_data->watching_handle) {
407 debug_log("Interested playing handle end : %d", ended_handle);
408 ((mm_sound_stop_callback_func)(cb_data->user_cb))(cb_data->user_data, ended_handle);
409 if (mm_sound_proxy_remove_play_sound_end_callback(end_cb_data->subs_id) != MM_ERROR_NONE)
410 debug_error("mm_sound_client_dbus_remove_play_file_end_callback failed");
412 debug_log("Not interested playing handle : %d", ended_handle);
416 static void play_end_callback_data_free_func(void *data)
418 struct callback_data *cb_data = (struct callback_data*) data;
421 g_free(cb_data->extra_data);
426 int mm_sound_client_play_sound(MMSoundPlayParam *param, int tone, int *handle)
428 int ret = MM_ERROR_NONE;
429 int session_type = MM_SESSION_TYPE_MEDIA;
430 int session_options = 0;
431 int is_focus_registered = 0;
432 // int instance = -1; /* instance is unique to communicate with server : client message queue filter type */
433 int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(param->volume_config);
434 char stream_type[MM_SOUND_STREAM_TYPE_LEN] = {0, };
435 struct callback_data *cb_data = NULL;
436 play_sound_end_callback_data_t *end_cb_data;
440 /* read session information */
442 ret = mm_sound_get_signal_value(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &is_focus_registered);
444 debug_error("mm_sound_get_signal_value failed [0x%x]", ret);
445 return MM_ERROR_POLICY_INTERNAL;
448 if (is_focus_registered)
449 param->skip_session = true;
451 if (param->skip_session == false) {
452 if(MM_ERROR_NONE != _mm_session_util_read_information(-1, &session_type, &session_options)) {
453 debug_warning("[Client] Read MMSession Type failed. use default \"media\" type\n");
454 session_type = MM_SESSION_TYPE_MEDIA;
456 if(MM_ERROR_NONE != mm_session_init(session_type)) {
457 debug_critical("[Client] MMSessionInit() failed\n");
458 return MM_ERROR_POLICY_INTERNAL;
464 if ((param->mem_ptr && param->mem_size)) {
465 // Play memory, deprecated
466 return MM_ERROR_INVALID_ARGUMENT;
469 mm_sound_convert_volume_type_to_stream_type(volume_type, stream_type);
470 ret = mm_sound_proxy_play_sound(param->filename, tone, param->loop, param->volume, param->volume_config,
471 session_type, session_options, getpid(), param->skip_session, handle, stream_type, -1);
472 if (ret != MM_ERROR_NONE) {
473 debug_error("Play Sound Failed");
476 if (param->callback) {
477 end_cb_data = (play_sound_end_callback_data_t *) g_malloc0(sizeof(play_sound_end_callback_data_t));
478 end_cb_data->watching_handle = *handle;
479 GET_CB_DATA(cb_data, param->callback, param->data, end_cb_data);
481 ret = mm_sound_proxy_add_play_sound_end_callback(_mm_sound_stop_callback_wrapper_func, cb_data,
482 play_end_callback_data_free_func, &end_cb_data->subs_id);
483 if (ret != MM_ERROR_NONE) {
484 debug_error("Add callback for play sound(%d) Failed", *handle);
488 if (!param->skip_session && !g_focus_signal_handle) {
489 ret = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &g_focus_signal_handle,
490 _mm_sound_client_focus_signal_callback, NULL);
492 debug_error("mm_sound_subscribe_signal failed [0x%x]", ret);
493 return MM_ERROR_POLICY_INTERNAL;
504 int mm_sound_client_play_sound_with_stream_info(MMSoundPlayParam *param, int *handle, char* stream_type, int stream_id)
506 int ret = MM_ERROR_NONE;
507 struct callback_data *cb_data = NULL;
508 play_sound_end_callback_data_t *end_cb_data;
510 ret = mm_sound_proxy_play_sound_with_stream_info(param->filename, param->loop, param->volume,
511 getpid(), handle, stream_type, stream_id);
512 if (ret != MM_ERROR_NONE) {
513 debug_error("Play Sound Failed");
516 if (param->callback) {
517 end_cb_data = (play_sound_end_callback_data_t *) g_malloc0(sizeof(play_sound_end_callback_data_t));
518 end_cb_data->watching_handle = *handle;
519 GET_CB_DATA(cb_data, param->callback, param->data, end_cb_data);
521 ret = mm_sound_proxy_add_play_sound_end_callback(_mm_sound_stop_callback_wrapper_func, cb_data,
522 play_end_callback_data_free_func, &end_cb_data->subs_id);
523 if (ret != MM_ERROR_NONE) {
524 debug_error("Add callback for play sound(%d) Failed", *handle);
535 int mm_sound_client_stop_sound(int handle)
537 int ret = MM_ERROR_NONE;
540 if (handle < 0 || handle > CLIENT_HANDLE_MAX) {
541 ret = MM_ERROR_INVALID_ARGUMENT;
545 ret = mm_sound_proxy_stop_sound(handle);
551 static int _mm_sound_client_device_list_dump (GList *device_list)
553 int ret = MM_ERROR_NONE;
555 mm_sound_device_t *device_node = NULL;
558 debug_error("Device list NULL, cannot dump list");
559 return MM_ERROR_SOUND_INTERNAL;
562 debug_log("======================== device list : start ==========================\n");
563 for (list = device_list; list != NULL; list = list->next) {
564 device_node = (mm_sound_device_t *)list->data;
566 debug_log(" list idx[%d]: type[%17s], id[%02d], io_direction[%d], state[%d], name[%s]\n",
567 count++, device_node->type, device_node->id, device_node->io_direction,
568 device_node->state, device_node->name);
571 debug_log("======================== device list : end ============================\n");
576 int mm_sound_client_get_current_connected_device_list(int device_flags, mm_sound_device_list_t *device_list)
578 int ret = MM_ERROR_NONE;
582 debug_error("Device list NULL");
583 ret = MM_ERROR_COMMON_INVALID_ARGUMENT;
587 if ((ret = mm_sound_proxy_get_current_connected_device_list(device_flags, &device_list->list)) != MM_ERROR_NONE) {
588 debug_error("[Client] failed to get current connected device list with dbus, ret[0x%x]", ret);
591 if (!device_list->list) {
592 debug_error("Got device list null");
593 ret = MM_ERROR_SOUND_NO_DATA;
596 _mm_sound_client_device_list_dump(device_list->list);
603 static bool device_is_match_direction(int direction, int mask)
605 if (mask == DEVICE_IO_DIRECTION_FLAGS || mask == 0)
608 if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_IN_FLAG) && (direction & MM_SOUND_DEVICE_IO_DIRECTION_IN))
610 if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_OUT_FLAG) && (direction & MM_SOUND_DEVICE_IO_DIRECTION_OUT))
612 if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_BOTH_FLAG) && (direction == MM_SOUND_DEVICE_IO_DIRECTION_BOTH))
618 static bool device_is_match_state(int state, int mask)
620 if (mask == DEVICE_STATE_FLAGS || mask == 0)
623 if ((mask & MM_SOUND_DEVICE_STATE_DEACTIVATED_FLAG) && (state == MM_SOUND_DEVICE_STATE_DEACTIVATED))
625 if ((mask & MM_SOUND_DEVICE_STATE_ACTIVATED_FLAG) && (state == MM_SOUND_DEVICE_STATE_ACTIVATED))
631 static bool device_is_match_type(const char *type, int mask)
634 const char *builtin_prefix = "builtin";
636 if (mask == DEVICE_TYPE_FLAGS || mask == 0)
639 is_builtin = !strncmp(type, builtin_prefix, strlen(builtin_prefix));
641 if ((mask & MM_SOUND_DEVICE_TYPE_INTERNAL_FLAG) && (is_builtin))
643 if ((mask & MM_SOUND_DEVICE_TYPE_EXTERNAL_FLAG) && (!is_builtin))
649 static bool device_is_match_with_mask(const char *type, int direction, int state, int mask)
651 if (mask == DEVICE_ALL_FLAG)
654 return (device_is_match_direction(direction, mask & DEVICE_IO_DIRECTION_FLAGS) &&
655 device_is_match_state(state, mask & DEVICE_STATE_FLAGS) &&
656 device_is_match_type(type, mask & DEVICE_TYPE_FLAGS));
659 static int _fill_sound_device(mm_sound_device_t *device_h, int device_id, const char *device_type,
660 int direction, int state, const char *name, int *stream_id, int stream_num)
664 if (stream_num > 0 && stream_id == NULL) {
665 debug_error("stream_num is %d, but stream_id is NULL", stream_num);
669 if (stream_num > MAX_STREAM_ON_DEVICE) {
670 debug_error("too many streams on this device");
674 device_h->id = device_id;
675 device_h->io_direction = direction;
676 device_h->state = state;
677 MMSOUND_STRNCPY(device_h->name, name, MAX_DEVICE_NAME_NUM);
678 MMSOUND_STRNCPY(device_h->type, device_type, MAX_DEVICE_TYPE_STR_LEN);
680 if (stream_num > 0) {
681 device_h->stream_num = stream_num;
682 debug_log("%d streams on this device", stream_num);
683 for (i = 0; i < stream_num; i++) {
684 debug_log(" stream_id : %d", stream_id[i]);
685 device_h->stream_id[i] = stream_id[i];
688 device_h->stream_num = 0;
689 debug_log("There is no stream on this device");
695 static void _mm_sound_device_connected_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
696 int state, const char *name, int *stream_id, int stream_num,
697 gboolean is_connected, void *userdata)
699 mm_sound_device_t device_h;
700 struct callback_data *cb_data = (struct callback_data*) userdata;
703 debug_log("[Device %s] id(%d) type(%s) direction(%d) state(%d) name(%s)",
704 is_connected ? "Connected" : "Disconnected", device_id, device_type, io_direction,
705 state, name, is_connected);
707 if (cb_data == NULL) {
708 debug_warning("device connected changed callback data null");
712 device_flags = (int) cb_data->extra_data;
713 if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
716 if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name, stream_id, stream_num) < 0) {
717 debug_error("Failed to fill sound device");
721 ((mm_sound_device_connected_cb)(cb_data->user_cb))(&device_h, is_connected, cb_data->user_data);
724 int mm_sound_client_add_device_connected_callback(int device_flags, mm_sound_device_connected_cb func,
725 void* userdata, unsigned int *subs_id)
727 int ret = MM_ERROR_NONE;
728 struct callback_data *cb_data = NULL;
732 GET_CB_DATA(cb_data, func, userdata, (void*) device_flags);
734 ret = mm_sound_proxy_add_device_connected_callback(device_flags,
735 _mm_sound_device_connected_callback_wrapper_func,
736 cb_data, g_free, subs_id);
737 if (ret == MM_ERROR_NONE)
738 g_need_emergent_exit = TRUE;
744 int mm_sound_client_remove_device_connected_callback(unsigned int subs_id)
746 int ret = MM_ERROR_NONE;
749 ret = mm_sound_proxy_remove_device_connected_callback(subs_id);
755 static void _mm_sound_device_info_changed_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
756 int state, const char *name, int *stream_id, int stream_num,
757 int changed_device_info_type, void *userdata)
759 mm_sound_device_t device_h;
760 struct callback_data *cb_data = (struct callback_data*) userdata;
763 debug_log("[Device Info Changed] id(%d) type(%s) direction(%d) state(%d) name(%s) changed_info_type(%d)",
764 device_id, device_type, io_direction, state, name, changed_device_info_type);
766 if (cb_data == NULL) {
767 debug_warning("device info 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, stream_id, stream_num) < 0) {
776 debug_error("Failed to fill sound device");
780 ((mm_sound_device_info_changed_cb)(cb_data->user_cb))(&device_h, changed_device_info_type, cb_data->user_data);
783 int mm_sound_client_add_device_info_changed_callback(int device_flags, mm_sound_device_info_changed_cb func,
784 void *userdata, unsigned int *subs_id)
786 int ret = MM_ERROR_NONE;
787 struct callback_data *cb_data = (struct callback_data*) userdata;
791 GET_CB_DATA(cb_data, func, userdata, (void *) device_flags);
793 ret = mm_sound_proxy_add_device_info_changed_callback(device_flags,
794 _mm_sound_device_info_changed_callback_wrapper_func,
795 cb_data, g_free, subs_id);
801 int mm_sound_client_remove_device_info_changed_callback(unsigned int subs_id)
803 int ret = MM_ERROR_NONE;
806 ret = mm_sound_proxy_remove_device_info_changed_callback(subs_id);
813 static void _mm_sound_device_state_changed_callback_wrapper_func(int device_id, const char *device_type,
814 int io_direction, int state, const char *name,
815 int *stream_id, int stream_num, void *userdata)
817 mm_sound_device_t device_h;
818 struct callback_data *cb_data = (struct callback_data*) userdata;
821 debug_log("[Device State Changed] id(%d) type(%s) direction(%d) state(%d) name(%s)",
822 device_id, device_type, io_direction, state, name);
824 if (cb_data == NULL) {
825 debug_warning("device state changed callback data null");
829 device_flags = (int) cb_data->extra_data;
831 if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
834 if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name, stream_id, stream_num) < 0) {
835 debug_error("Failed to fill sound device");
839 ((mm_sound_device_state_changed_cb)(cb_data->user_cb))(&device_h, state, cb_data->user_data);
842 int mm_sound_client_add_device_state_changed_callback(int device_flags, mm_sound_device_state_changed_cb func,
843 void *userdata, unsigned int *id)
845 int ret = MM_ERROR_NONE;
846 struct callback_data *cb_data = (struct callback_data*) userdata;
850 GET_CB_DATA(cb_data, func, userdata, (void *) device_flags);
852 ret = mm_sound_proxy_add_device_state_changed_callback(device_flags,
853 _mm_sound_device_state_changed_callback_wrapper_func,
854 cb_data, g_free, id);
860 int mm_sound_client_remove_device_state_changed_callback(unsigned int id)
862 int ret = MM_ERROR_NONE;
865 ret = mm_sound_proxy_remove_device_state_changed_callback(id);
871 int mm_sound_client_is_stream_on_device(int stream_id, int device_id, bool *is_on)
873 int ret = MM_ERROR_NONE;
877 debug_error("Invalid Parameter");
878 ret = MM_ERROR_COMMON_INVALID_ARGUMENT;
882 if ((ret = mm_sound_proxy_is_stream_on_device(stream_id, device_id, is_on)) != MM_ERROR_NONE) {
883 debug_error("[Client] failed to query is stream on device, ret[0x%x]", ret);
892 int __convert_volume_type_to_str(int volume_type, char **volume_type_str)
894 int ret = MM_ERROR_NONE;
896 if (!volume_type_str) {
897 return MM_ERROR_COMMON_INVALID_ARGUMENT;
900 switch (volume_type) {
901 case VOLUME_TYPE_SYSTEM:
902 *volume_type_str = "system";
904 case VOLUME_TYPE_NOTIFICATION:
905 *volume_type_str = "notification";
907 case VOLUME_TYPE_ALARM:
908 *volume_type_str = "alarm";
910 case VOLUME_TYPE_RINGTONE:
911 *volume_type_str = "ringtone";
913 case VOLUME_TYPE_MEDIA:
914 *volume_type_str = "media";
916 case VOLUME_TYPE_CALL:
917 *volume_type_str = "call";
919 case VOLUME_TYPE_VOIP:
920 *volume_type_str = "voip";
922 case VOLUME_TYPE_VOICE:
923 *volume_type_str = "voice";
926 if (!strncmp(*volume_type_str,"", VOLUME_TYPE_LEN)) {
927 debug_error("could not find the volume_type[%d] in this switch case statement", volume_type);
928 ret = MM_ERROR_SOUND_INTERNAL;
930 debug_log("volume_type[%s]", *volume_type_str);
935 static int __convert_volume_type_to_int(const char *volume_type_str, volume_type_t *volume_type)
937 int ret = MM_ERROR_NONE;
939 if (!volume_type || !volume_type_str) {
940 return MM_ERROR_COMMON_INVALID_ARGUMENT;
943 if (!strncmp(volume_type_str, "system", VOLUME_TYPE_LEN)) {
944 *volume_type = VOLUME_TYPE_SYSTEM;
945 } else if (!strncmp(volume_type_str, "notification", VOLUME_TYPE_LEN)) {
946 *volume_type = VOLUME_TYPE_NOTIFICATION;
947 } else if (!strncmp(volume_type_str, "alarm", VOLUME_TYPE_LEN)) {
948 *volume_type = VOLUME_TYPE_ALARM;
949 } else if (!strncmp(volume_type_str, "ringtone", VOLUME_TYPE_LEN)) {
950 *volume_type = VOLUME_TYPE_RINGTONE;
951 } else if (!strncmp(volume_type_str, "media", VOLUME_TYPE_LEN)) {
952 *volume_type = VOLUME_TYPE_MEDIA;
953 } else if (!strncmp(volume_type_str, "call", VOLUME_TYPE_LEN)) {
954 *volume_type = VOLUME_TYPE_CALL;
955 } else if (!strncmp(volume_type_str, "voip", VOLUME_TYPE_LEN)) {
956 *volume_type = VOLUME_TYPE_VOIP;
957 } else if (!strncmp(volume_type_str, "voice", VOLUME_TYPE_LEN)) {
958 *volume_type = VOLUME_TYPE_VOICE;
960 debug_log("Invalid volume type : [%s]", volume_type_str);
961 ret = MM_ERROR_SOUND_INTERNAL;
967 int mm_sound_client_set_volume_by_type(const int volume_type, const unsigned int volume_level)
969 int ret = MM_ERROR_NONE;
970 char *type_str = NULL;
973 if ((ret = __convert_volume_type_to_str(volume_type, &type_str)) != MM_ERROR_NONE) {
974 debug_error("volume type convert failed");
978 ret = mm_sound_proxy_set_volume_by_type(type_str, volume_level);
985 static void _mm_sound_volume_changed_callback_wrapper_func(const char *direction, const char *volume_type_str,
986 int volume_level, void *userdata)
988 volume_type_t volume_type = 0;
989 struct callback_data *cb_data = (struct callback_data *) userdata;
991 debug_log("[Wrapper CB][Volume Changed] direction : %s, volume_type : %s, volume_level : %d",
992 direction, volume_type_str, volume_level);
994 if (cb_data == NULL) {
995 debug_warning("volume changed callback data null");
999 if (__convert_volume_type_to_int(volume_type_str, &volume_type) != MM_ERROR_NONE) {
1000 debug_error("volume type convert failed");
1003 debug_log("Call volume changed user cb, direction : %s, vol_type : %s(%d), level : %u",
1004 direction, volume_type_str, volume_type, volume_level);
1005 ((mm_sound_volume_changed_cb)(cb_data->user_cb))(volume_type, volume_level, cb_data->user_data);
1008 int mm_sound_client_add_volume_changed_callback(mm_sound_volume_changed_cb func, void* userdata, unsigned int *subs_id)
1010 int ret = MM_ERROR_NONE;
1011 struct callback_data *cb_data = NULL;
1015 GET_CB_DATA(cb_data, func, userdata, NULL);
1017 ret = mm_sound_proxy_add_volume_changed_callback(_mm_sound_volume_changed_callback_wrapper_func, cb_data, g_free, subs_id);
1024 int mm_sound_client_remove_volume_changed_callback(unsigned int subs_id)
1026 int ret = MM_ERROR_NONE;
1029 ret = mm_sound_proxy_remove_volume_changed_callback(subs_id);
1036 int mm_sound_client_set_session_interrupt_callback(mm_sound_focus_session_interrupt_cb callback, void* user_data)
1038 int ret = MM_ERROR_NONE;
1043 return MM_ERROR_INVALID_ARGUMENT;
1045 g_focus_session_interrupt_info.user_cb = callback;
1046 g_focus_session_interrupt_info.user_data = user_data;
1052 int mm_sound_client_unset_session_interrupt_callback(void)
1054 int ret = MM_ERROR_NONE;
1058 if (!g_focus_session_interrupt_info.user_cb) {
1059 debug_error("no callback to unset");
1060 return MM_ERROR_SOUND_INTERNAL;
1063 g_focus_session_interrupt_info.user_cb = NULL;
1064 g_focus_session_interrupt_info.user_data = NULL;
1070 static gpointer _focus_thread_func(gpointer data)
1072 unsigned int thread_id = (unsigned int)pthread_self();
1073 GMainLoop *focus_loop = (GMainLoop*)data;
1075 debug_warning(">>> thread func : thread id(%u), mainloop[%p]", thread_id, focus_loop);
1077 g_main_loop_run(focus_loop);
1078 debug_warning("<<< quit thread func : thread id(%u), mainloop[%p]", thread_id, focus_loop);
1083 static gboolean _focus_fd_prepare(GSource *source, gint *timeout)
1086 debug_warning("[ PREPARE : %p, (%p, %d)", source, timeout, timeout? *timeout : -1);
1091 static gboolean _focus_fd_check(GSource * source)
1093 FocusSource* fsource = (FocusSource *)source;
1096 debug_error("GSource is null");
1100 debug_warning("CHECK : %p, 0x%x ]", source, fsource->pollfd.revents);
1102 if (fsource->poll_fd.revents & (POLLIN | POLLPRI))
1108 static gboolean _focus_fd_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
1110 debug_warning("*** DISPATCH : %p, (%p, %p)", source, callback, user_data);
1111 return callback(user_data);
1114 static void _focus_fd_finalize(GSource *source)
1116 debug_warning("### FINALIZE : %p", source);
1119 static int _focus_find_index_by_handle(int handle)
1122 for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1123 if (g_focus_sound_handle[i].focus_callback && handle == g_focus_sound_handle[i].handle) {
1124 /* debug_msg("found index(%d) for handle(%d)", i, handle);*/
1125 return (handle == FOCUS_HANDLE_INIT_VAL)? -1 : i;
1131 static int _focus_watch_find_index_by_handle(int handle)
1134 for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1135 if (g_focus_sound_handle[i].watch_callback && handle == g_focus_sound_handle[i].handle) {
1136 /* debug_msg("found index(%d) for watch handle(%d)", i, handle);*/
1137 return (handle == FOCUS_HANDLE_INIT_VAL)? -1 : i;
1143 static gboolean _focus_callback_handler(gpointer user_data)
1145 focus_sound_info_t *focus_handle = (focus_sound_info_t *)user_data;
1149 focus_cb_data_lib cb_data;
1151 debug_log(">>> _focus_callback_handler(), this thread id(%u)\n", (unsigned int)pthread_self());
1153 memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1154 if (!focus_handle) {
1155 debug_error("focus_handle is null");
1156 return G_SOURCE_CONTINUE;
1158 if (!(poll_fd = &focus_handle->fsrc->poll_fd)) {
1159 debug_error("poll_fd is null");
1160 return G_SOURCE_CONTINUE;
1162 debug_log("focus_handle(%p), poll_fd(%p)", focus_handle, poll_fd);
1164 if (poll_fd->revents & (POLLIN | POLLPRI)) {
1165 int changed_state = -1;
1167 count = read(poll_fd->fd, &cb_data, sizeof(cb_data));
1169 char str_error[256];
1170 strerror_r(errno, str_error, sizeof(str_error));
1171 debug_error("GpollFD read fail, errno=%d(%s)",errno, str_error);
1172 return G_SOURCE_CONTINUE;
1174 changed_state = cb_data.state;
1176 g_mutex_lock(&focus_handle->focus_lock);
1178 tid = focus_handle->focus_tid;
1180 if (changed_state != -1) {
1181 debug_msg("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)",
1182 tid, cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type);
1183 if (focus_handle->focus_callback == NULL) {
1184 debug_error("focus callback is null..");
1185 g_mutex_unlock(&focus_handle->focus_lock);
1186 return G_SOURCE_CONTINUE;
1188 debug_msg("[CALLBACK(%p) START]", focus_handle->focus_callback);
1189 (focus_handle->focus_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type,
1190 cb_data.option, cb_data.ext_info, focus_handle->user_data);
1191 debug_msg("[CALLBACK END]");
1192 if (g_focus_session_interrupt_info.user_cb) {
1193 debug_msg("sending session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
1194 g_focus_session_interrupt_info.user_cb(cb_data.state, cb_data.stream_type, false,
1195 g_focus_session_interrupt_info.user_data);
1198 #ifdef CONFIG_ENABLE_RETCB
1202 unsigned int buf = 0;
1203 char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", focus_handle->focus_tid, cb_data.handle);
1204 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1206 char str_error[256];
1207 strerror_r(errno, str_error, sizeof(str_error));
1208 debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n",
1209 tid, tmpfd, filename2, errno, str_error);
1211 g_mutex_unlock(&focus_handle->focus_lock);
1212 return G_SOURCE_CONTINUE;
1214 /* buf contains data as below,
1215 * |<--12bits--><--4bits (reacquisition)--><--16bits (handle)-->| */
1216 buf = (unsigned int)((0x0000ffff & cb_data.handle) | (focus_handle->auto_reacquire << 16));
1217 rett = write(tmpfd, &buf, sizeof(buf));
1220 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
1225 g_mutex_unlock(&focus_handle->focus_lock);
1229 return G_SOURCE_CONTINUE;
1232 static gboolean _focus_watch_callback_handler(gpointer user_data)
1234 focus_sound_info_t *focus_handle = (focus_sound_info_t *)user_data;
1238 focus_cb_data_lib cb_data;
1240 debug_log(">>> _focus_watch_callback_handler(), this thread id(%u)\n", (unsigned int)pthread_self());
1242 memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1243 if (!focus_handle) {
1244 debug_error("focus_handle is null");
1247 if (!(poll_fd = &focus_handle->fsrc->poll_fd)) {
1248 debug_error("g_poll_fd is null");
1251 debug_log("focus_handle(%p), poll_fd(%p)", focus_handle, poll_fd);
1253 if (poll_fd->revents & (POLLIN | POLLPRI)) {
1254 count = read(poll_fd->fd, &cb_data, sizeof(cb_data));
1256 char str_error[256];
1257 strerror_r(errno, str_error, sizeof(str_error));
1258 debug_error("GpollFD read fail, errno=%d(%s)",errno, str_error);
1259 return G_SOURCE_CONTINUE;
1262 if (!focus_handle->is_used) {
1263 debug_warning("unsetting watch calllback has been already requested");
1264 goto SKIP_CB_AND_RET;
1267 g_mutex_lock(&focus_handle->focus_lock);
1269 tid = focus_handle->focus_tid;
1271 debug_msg("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)",
1272 tid, cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type);
1274 if (focus_handle->watch_callback == NULL) {
1275 debug_msg("watch callback is null..");
1277 debug_msg("[CALLBACK(%p) START]", focus_handle->watch_callback);
1278 (focus_handle->watch_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type,
1279 cb_data.ext_info, focus_handle->user_data);
1280 debug_msg("[CALLBACK END]");
1281 if (g_focus_session_interrupt_info.user_cb) {
1282 debug_msg("sending session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
1283 (g_focus_session_interrupt_info.user_cb)(cb_data.state, cb_data.stream_type, true,
1284 g_focus_session_interrupt_info.user_data);
1289 #ifdef CONFIG_ENABLE_RETCB
1294 char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr", focus_handle->focus_tid, cb_data.handle);
1295 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1297 char str_error[256];
1298 strerror_r(errno, str_error, sizeof(str_error));
1299 debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n",
1300 tid, tmpfd, filename2, errno, str_error);
1302 g_mutex_unlock(&focus_handle->focus_lock);
1303 return G_SOURCE_CONTINUE;
1305 buf = cb_data.handle;
1306 rett = write(tmpfd, &buf, sizeof(buf));
1309 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
1314 if (focus_handle->is_used) {
1315 debug_msg("unlock focus_lock = %p", &focus_handle->focus_lock);
1316 g_mutex_unlock(&focus_handle->focus_lock);
1321 return G_SOURCE_CONTINUE;
1324 static void _focus_open_callback(int index, bool is_for_watching)
1331 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1332 debug_error("Invalid focus handle index [%d]", index);
1336 if (is_for_watching) {
1337 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch",
1338 g_focus_sound_handle[index].focus_tid,
1339 g_focus_sound_handle[index].handle);
1341 filename = g_strdup_printf("/tmp/FOCUS.%d.%d",
1342 g_focus_sound_handle[index].focus_tid,
1343 g_focus_sound_handle[index].handle);
1345 pre_mask = umask(0);
1346 if (mknod(filename, S_IFIFO|0666, 0)) {
1347 debug_error("mknod() failure, errno(%d)", errno);
1350 g_focus_sound_handle[index].focus_fd = open(filename, O_RDWR|O_NONBLOCK);
1351 if (g_focus_sound_handle[index].focus_fd == -1) {
1352 debug_error("Open fail : index(%d), file open error(%d)", index, errno);
1354 debug_log("Open success : index(%d), filename(%s), fd(%d)",
1355 index, filename, g_focus_sound_handle[index].focus_fd);
1360 #ifdef CONFIG_ENABLE_RETCB
1363 if (is_for_watching) {
1364 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr",
1365 g_focus_sound_handle[index].focus_tid,
1366 g_focus_sound_handle[index].handle);
1368 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr",
1369 g_focus_sound_handle[index].focus_tid,
1370 g_focus_sound_handle[index].handle);
1372 pre_mask = umask(0);
1373 if (mknod(filename2, S_IFIFO | 0666, 0)) {
1374 debug_error("mknod() failure, errno(%d)", errno);
1384 void _focus_close_callback(int index, bool is_for_watching)
1386 char *filename = NULL;
1390 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1391 debug_error("Invalid focus handle index [%d]", index);
1395 if (g_focus_sound_handle[index].focus_fd < 0) {
1396 debug_error("Close fail : index(%d)", index);
1398 close(g_focus_sound_handle[index].focus_fd);
1399 debug_log("Close Success : index(%d)", index);
1402 if (is_for_watching) {
1403 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch",
1404 g_focus_sound_handle[index].focus_tid,
1405 g_focus_sound_handle[index].handle);
1407 filename = g_strdup_printf("/tmp/FOCUS.%d.%d",
1408 g_focus_sound_handle[index].focus_tid,
1409 g_focus_sound_handle[index].handle);
1411 if (remove(filename)) {
1412 debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)",
1418 #ifdef CONFIG_ENABLE_RETCB
1421 if (is_for_watching) {
1422 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr",
1423 g_focus_sound_handle[index].focus_tid,
1424 g_focus_sound_handle[index].handle);
1426 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr",
1427 g_focus_sound_handle[index].focus_tid,
1428 g_focus_sound_handle[index].handle);
1430 if (remove(filename2)) {
1431 debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)",
1442 static GSourceFuncs event_funcs = {
1443 .prepare = _focus_fd_prepare,
1444 .check = _focus_fd_check,
1445 .dispatch = _focus_fd_dispatch,
1446 .finalize = _focus_fd_finalize,
1449 static bool _focus_add_sound_callback(int index, focus_callback_handler_t focus_cb_handler)
1451 FocusSource *fsrc = NULL;
1452 GSource *src = NULL;
1457 g_mutex_init(&g_focus_sound_handle[index].focus_lock);
1459 src = g_source_new(&event_funcs, sizeof(FocusSource));
1461 debug_error("failed to g_source_new for focus source");
1465 fsrc = (FocusSource*) src;
1467 fsrc->poll_fd.fd = g_focus_sound_handle[index].focus_fd;
1468 fsrc->poll_fd.events = (gushort)(POLLIN | POLLPRI);
1469 g_source_add_poll(src, &fsrc->poll_fd);
1471 g_source_set_callback(src, focus_cb_handler, (gpointer)&g_focus_sound_handle[index], NULL);
1473 debug_warning("fsrc(%p), src_funcs(%p), pollfd(%p), fd(%d)",
1474 fsrc, &event_funcs, &fsrc->poll_fd, fsrc->poll_fd.fd);
1476 fsrc_id = g_source_attach(src, g_main_loop_get_context(g_focus_sound_handle[index].focus_loop));
1478 debug_error("failed to attach the source to context");
1481 g_source_unref(src);
1483 g_focus_sound_handle[index].fsrc = fsrc;
1490 g_source_unref(src);
1495 static bool _focus_remove_sound_callback(int index)
1497 focus_sound_info_t *h = NULL;
1501 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1502 debug_error("Invalid focus handle index [%d]", index);
1506 h = &g_focus_sound_handle[index];
1508 g_source_destroy((GSource *)h->fsrc);
1512 h->focus_callback = NULL;
1513 h->watch_callback = NULL;
1515 g_mutex_clear(&h->focus_lock);
1522 static void _focus_add_callback(int index, bool is_for_watching)
1526 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1527 debug_error("Invalid focus handle index [%d]", index);
1531 if (!is_for_watching) {
1532 if (!_focus_add_sound_callback(index, _focus_callback_handler))
1533 debug_error("failed to _focus_add_sound_callback(%p)", _focus_callback_handler);
1534 } else { // need to check if it's necessary
1535 if (!_focus_add_sound_callback(index, _focus_watch_callback_handler))
1536 debug_error("failed to _focus_add_sound_callback(%p)", _focus_watch_callback_handler);
1541 static void _focus_remove_callback(int index)
1544 if (!_focus_remove_sound_callback(index))
1545 debug_error("failed to __focus_remove_sound_callback()");
1549 static void _focus_init_callback(int index, bool is_for_watching)
1552 _focus_open_callback(index, is_for_watching);
1553 _focus_add_callback(index, is_for_watching);
1557 static void _focus_deinit_callback(int index, bool is_for_watching)
1560 _focus_remove_callback(index);
1561 _focus_close_callback(index, is_for_watching);
1565 static int _focus_init_context(int index)
1567 GMainContext *focus_context;
1571 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1572 debug_error("index(%d) is not valid", index);
1573 return MM_ERROR_INVALID_ARGUMENT;
1576 focus_context = g_main_context_new();
1577 g_focus_sound_handle[index].focus_loop = g_main_loop_new(focus_context, FALSE);
1578 g_main_context_unref(focus_context);
1579 if (g_focus_sound_handle[index].focus_loop == NULL) {
1580 debug_error("could not create mainloop..");
1581 return MM_ERROR_SOUND_INTERNAL;
1584 g_focus_sound_handle[index].focus_cb_thread = g_thread_new("focus-cb-thread",
1586 g_focus_sound_handle[index].focus_loop);
1587 if (g_focus_sound_handle[index].focus_cb_thread == NULL) {
1588 debug_error("could not create thread..");
1589 g_main_loop_unref(g_focus_sound_handle[index].focus_loop);
1590 return MM_ERROR_SOUND_INTERNAL;
1593 debug_warning("focus cb thread[%p] with mainloop[%p] is created for index(%d)",
1594 g_focus_sound_handle[index].focus_cb_thread, g_focus_sound_handle[index].focus_loop, index);
1598 return MM_ERROR_NONE;
1601 static void _focus_deinit_context(int index)
1605 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1606 debug_error("index(%d) is not valid", index);
1610 if (!g_focus_sound_handle[index].focus_loop || !g_focus_sound_handle[index].focus_cb_thread) {
1611 debug_error("focus_loop[%p] or focus_cb_thread[%p] is null",
1612 g_focus_sound_handle[index].focus_loop, g_focus_sound_handle[index].focus_cb_thread);
1616 g_main_loop_quit(g_focus_sound_handle[index].focus_loop);
1617 g_thread_join(g_focus_sound_handle[index].focus_cb_thread);
1618 debug_warning("after thread join, thread[%p], mainloop[%p] for index(%d)",
1619 g_focus_sound_handle[index].focus_cb_thread, g_focus_sound_handle[index].focus_loop, index);
1620 g_main_loop_unref(g_focus_sound_handle[index].focus_loop);
1621 g_focus_sound_handle[index].focus_loop = NULL;
1622 g_focus_sound_handle[index].focus_cb_thread = NULL;
1627 int mm_sound_client_get_unique_id(int *id)
1629 int ret = MM_ERROR_NONE;
1634 ret = MM_ERROR_INVALID_ARGUMENT;
1636 ret = mm_sound_proxy_get_unique_id(id);
1643 int mm_sound_client_is_focus_cb_thread(GThread *mine, bool *result)
1645 int ret = MM_ERROR_NONE;
1648 if (!mine || !result)
1649 ret = MM_ERROR_INVALID_ARGUMENT;
1652 for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1653 if (!g_focus_sound_handle[i].is_used)
1655 if (g_focus_sound_handle[i].focus_cb_thread == mine) {
1665 int mm_sound_client_register_focus(int id, int pid, const char *stream_type,
1666 mm_sound_focus_changed_cb callback,
1667 bool is_for_session, void* user_data)
1669 int ret = MM_ERROR_NONE;
1674 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1678 for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
1679 if (g_focus_sound_handle[index].is_used == false) {
1680 g_focus_sound_handle[index].is_used = true;
1685 g_focus_sound_handle[index].focus_tid = instance;
1686 g_focus_sound_handle[index].handle = id;
1687 g_focus_sound_handle[index].focus_callback = callback;
1688 g_focus_sound_handle[index].user_data = user_data;
1689 g_focus_sound_handle[index].is_for_session = is_for_session;
1690 g_focus_sound_handle[index].auto_reacquire = true;
1692 ret = mm_sound_proxy_register_focus(id, pid, stream_type, callback, is_for_session, user_data);
1693 if (ret == MM_ERROR_NONE) {
1694 debug_msg("[Client] Success to register focus\n");
1695 g_need_emergent_exit = TRUE;
1696 if (_focus_init_context(index)) {
1697 ret = MM_ERROR_SOUND_INTERNAL;
1701 debug_error("[Client] Error occurred : 0x%x \n",ret);
1705 _focus_init_callback(index, false);
1709 g_focus_sound_handle[index].is_used = false;
1712 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1718 int mm_sound_client_unregister_focus(int id)
1720 int ret = MM_ERROR_NONE;
1725 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1727 index = _focus_find_index_by_handle(id);
1729 debug_error("Could not find index");
1730 ret = MM_ERROR_INVALID_ARGUMENT;
1733 instance = g_focus_sound_handle[index].focus_tid;
1735 if (!g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1736 debug_warning("maybe focus_callback is being called, try one more time..");
1737 usleep(2500000); // 2.5 sec
1738 if (g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1739 debug_msg("finally got focus_lock");
1743 ret = mm_sound_proxy_unregister_focus(instance, id, g_focus_sound_handle[index].is_for_session);
1744 if (ret == MM_ERROR_NONE)
1745 debug_msg("[Client] Success to unregister focus\n");
1747 debug_error("[Client] Error occurred : 0x%x \n",ret);
1749 g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
1751 _focus_deinit_callback(index, false);
1752 g_focus_sound_handle[index].focus_fd = 0;
1753 g_focus_sound_handle[index].focus_tid = 0;
1754 g_focus_sound_handle[index].handle = 0;
1755 g_focus_sound_handle[index].is_used = false;
1756 _focus_deinit_context(index);
1760 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1765 int mm_sound_client_set_focus_reacquisition(int id, bool reacquisition)
1767 int ret = MM_ERROR_NONE;
1773 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1775 index = _focus_find_index_by_handle(id);
1777 debug_error("Could not find index");
1778 ret = MM_ERROR_INVALID_ARGUMENT;
1781 instance = g_focus_sound_handle[index].focus_tid;
1783 ret = mm_sound_client_is_focus_cb_thread(g_thread_self(), &result);
1785 debug_error("[Client] mm_sound_client_is_focus_cb_thread failed");
1787 } else if (!result) {
1788 ret = mm_sound_proxy_set_foucs_reacquisition(instance, id, reacquisition);
1789 if (ret == MM_ERROR_NONE) {
1790 debug_msg("[Client] Success to set focus reacquisition to [%d]\n", reacquisition);
1792 debug_error("[Client] Error occurred : 0x%x \n",ret);
1796 debug_warning("[Client] Inside the focus cb thread, set focus reacquisition to [%d]\n", reacquisition);
1799 g_focus_sound_handle[index].auto_reacquire = reacquisition;
1800 debug_msg("[Client] set focus reacquisition(%d) for id(%d)\n", reacquisition, id);
1803 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1808 int mm_sound_client_get_focus_reacquisition(int id, bool *reacquisition)
1810 int ret = MM_ERROR_NONE;
1815 if (!reacquisition) {
1816 debug_error("Invalid parameter");
1817 return MM_ERROR_INVALID_ARGUMENT;
1820 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1822 index = _focus_find_index_by_handle(id);
1824 debug_error("Could not find index");
1825 ret = MM_ERROR_INVALID_ARGUMENT;
1829 *reacquisition = g_focus_sound_handle[index].auto_reacquire;
1830 debug_msg("[Client] get focus reacquisition(%d) for id(%d)\n", *reacquisition, id);
1833 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1838 int mm_sound_client_get_acquired_focus_stream_type(int focus_type, char **stream_type, int *option, char **ext_info)
1840 int ret = MM_ERROR_NONE;
1841 char *ext_str = NULL;
1845 ret = mm_sound_proxy_get_acquired_focus_stream_type(focus_type, stream_type, option, &ext_str);
1846 if (ret == MM_ERROR_NONE) {
1847 debug_msg("[Client] Success to get stream type of acquired focus, stream_type(%s), ext_info(%s)\n",
1848 *stream_type, ext_str);
1849 *ext_info = strdup(ext_str);
1852 debug_error("[Client] Error occurred : 0x%x \n",ret);
1859 int mm_sound_client_acquire_focus(int id, mm_sound_focus_type_e type, int option, const char *ext_info)
1861 int ret = MM_ERROR_NONE;
1866 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1868 index = _focus_find_index_by_handle(id);
1870 debug_error("Could not find index");
1871 ret = MM_ERROR_INVALID_ARGUMENT;
1874 instance = g_focus_sound_handle[index].focus_tid;
1876 ret = mm_sound_proxy_acquire_focus(instance, id, type, option, ext_info, g_focus_sound_handle[index].is_for_session);
1877 if (ret == MM_ERROR_NONE)
1878 debug_msg("[Client] Success to acquire focus\n");
1880 debug_error("[Client] Error occurred : 0x%x \n",ret);
1883 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1888 int mm_sound_client_release_focus(int id, mm_sound_focus_type_e type, int option, const char *ext_info)
1890 int ret = MM_ERROR_NONE;
1895 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1897 index = _focus_find_index_by_handle(id);
1899 debug_error("Could not find index");
1900 ret = MM_ERROR_INVALID_ARGUMENT;
1903 instance = g_focus_sound_handle[index].focus_tid;
1905 ret = mm_sound_proxy_release_focus(instance, id, type, option, ext_info, g_focus_sound_handle[index].is_for_session);
1906 if (ret == MM_ERROR_NONE)
1907 debug_msg("[Client] Success to release focus\n");
1909 debug_error("[Client] Error occurred : 0x%x \n",ret);
1912 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1917 int mm_sound_client_update_stream_focus_status(int id, unsigned int status)
1919 int ret = MM_ERROR_NONE;
1922 if ((ret = mm_sound_proxy_update_stream_focus_status(id, status)) != MM_ERROR_NONE)
1923 debug_error("[Client] failed to update stream focus status, ret[0x%x]", ret);
1929 int mm_sound_client_set_focus_watch_callback(int pid, mm_sound_focus_type_e focus_type,
1930 mm_sound_focus_changed_watch_cb callback,
1931 bool is_for_session, void* user_data, int *id)
1933 int ret = MM_ERROR_NONE;
1940 return MM_ERROR_INVALID_ARGUMENT;
1942 //pthread_mutex_lock(&g_thread_mutex2);
1946 ret = mm_sound_proxy_get_unique_id(id);
1950 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1952 for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
1953 if (g_focus_sound_handle[index].is_used == false) {
1954 g_focus_sound_handle[index].is_used = true;
1959 g_focus_sound_handle[index].focus_tid = instance;
1960 g_focus_sound_handle[index].handle = *id;
1961 g_focus_sound_handle[index].watch_callback = callback;
1962 g_focus_sound_handle[index].user_data = user_data;
1963 g_focus_sound_handle[index].is_for_session = is_for_session;
1965 ret = mm_sound_proxy_set_focus_watch_callback(pid, g_focus_sound_handle[index].handle, focus_type,
1966 callback, is_for_session, user_data);
1968 if (ret == MM_ERROR_NONE) {
1969 debug_msg("[Client] Success to watch focus");
1970 g_need_emergent_exit = TRUE;
1971 if (_focus_init_context(index)) {
1972 ret = MM_ERROR_SOUND_INTERNAL;
1976 debug_error("[Client] Error occurred : 0x%x",ret);
1980 _focus_init_callback(index, true);
1984 g_focus_sound_handle[index].is_used = false;
1987 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1992 int mm_sound_client_unset_focus_watch_callback(int id)
1994 int ret = MM_ERROR_NONE;
1997 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1999 index = _focus_watch_find_index_by_handle(id);
2001 debug_error("Could not find index");
2002 ret = MM_ERROR_INVALID_ARGUMENT;
2006 g_mutex_lock(&g_focus_sound_handle[index].focus_lock);
2008 g_focus_sound_handle[index].is_used = false;
2010 ret = mm_sound_proxy_unset_focus_watch_callback(g_focus_sound_handle[index].focus_tid,
2011 g_focus_sound_handle[index].handle,
2012 g_focus_sound_handle[index].is_for_session);
2014 if (ret == MM_ERROR_NONE)
2015 debug_msg("[Client] Success to unwatch focus\n");
2017 debug_error("[Client] Error occurred : 0x%x \n",ret);
2020 g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
2022 _focus_deinit_callback(index, true);
2023 g_focus_sound_handle[index].focus_fd = 0;
2024 g_focus_sound_handle[index].focus_tid = 0;
2025 g_focus_sound_handle[index].handle = 0;
2026 _focus_deinit_context(index);
2029 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2034 static gboolean _idle_event_callback(void *data)
2036 focus_idle_event_t *idle_event_data = (focus_idle_event_t*)data;
2037 int ret = MM_ERROR_NONE;
2040 debug_error("data is null");
2044 debug_msg("idle_event_data(%p): type(%d), data(%d)",
2045 idle_event_data, idle_event_data->type, idle_event_data->data);
2047 switch (idle_event_data->type) {
2048 case IDLE_EVENT_TYPE_UNSET_FOCUS_WATCH_CB:
2049 if ((ret = mm_sound_client_unset_focus_watch_callback(idle_event_data->data)))
2050 debug_error("Could not unset focus watch callback, id(%d), ret = %x\n", idle_event_data->data, ret);
2052 case IDLE_EVENT_TYPE_UNREGISTER_FOCUS:
2053 if ((ret = mm_sound_client_unregister_focus(idle_event_data->data)))
2054 debug_error("Could not unregister focus, id(%d), ret = %x\n", idle_event_data->data, ret);
2057 debug_warning("invalid type(%d)", idle_event_data->type);
2061 g_free(idle_event_data);
2063 g_idle_event_src = 0;
2065 MMSOUND_LEAVE_CRITICAL_SECTION(&g_event_mutex);
2070 int mm_sound_client_execute_focus_func_in_main_context(focus_idle_event_type_e type, int data)
2072 focus_idle_event_t *idle_event_data = NULL;
2074 if (IDLE_EVENT_TYPE_UNSET_FOCUS_WATCH_CB > type || IDLE_EVENT_TYPE_MAX < type)
2075 return MM_ERROR_INVALID_ARGUMENT;
2077 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_event_mutex, MM_ERROR_SOUND_INTERNAL);
2079 idle_event_data = g_new0(focus_idle_event_t, 1);
2080 idle_event_data->type = type;
2081 idle_event_data->data = data;
2083 g_idle_event_src = g_idle_add_full(G_PRIORITY_HIGH,
2084 (GSourceFunc)_idle_event_callback,
2085 (gpointer)idle_event_data,
2088 return MM_ERROR_NONE;
2093 int mm_sound_client_add_test_callback(mm_sound_test_cb func, void* user_data, unsigned int *subs_id)
2095 int ret = MM_ERROR_NONE;
2099 ret = mm_sound_proxy_add_test_callback(func, user_data, g_free, subs_id);
2105 int mm_sound_client_remove_test_callback(unsigned int subs_id)
2107 int ret = MM_ERROR_NONE;
2110 ret = mm_sound_proxy_remove_test_callback(subs_id);
2117 int mm_sound_client_test(int a, int b, int* getv)
2119 int ret = MM_ERROR_NONE;
2123 ret = mm_sound_proxy_test(a, b, getv);
2124 debug_log("%d * %d -> result : %d", a, b, *getv);