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 #define GET_CB_DATA(_cb_data, _func, _userdata, _extradata) \
77 _cb_data = (struct callback_data*) g_malloc0(sizeof(struct callback_data)); \
78 _cb_data->user_cb = _func; \
79 _cb_data->user_data = _userdata; \
80 _cb_data->extra_data = _extradata; \
88 GSourceFuncs* g_src_funcs;
94 mm_sound_focus_changed_cb focus_callback;
95 mm_sound_focus_changed_watch_cb watch_callback;
98 bool is_for_session; /* will be removed when the session concept is completely left out*/
106 char stream_type[MAX_STREAM_TYPE_LEN];
107 char ext_info[MM_SOUND_NAME_NUM];
112 mm_sound_focus_session_interrupt_cb user_cb;
114 } focus_session_interrupt_info_t;
116 typedef gboolean (*focus_gLoopPollHandler_t)(gpointer d);
118 GThread *g_focus_thread;
119 GMainLoop *g_focus_loop;
120 focus_sound_info_t g_focus_sound_handle[FOCUS_HANDLE_MAX];
121 focus_session_interrupt_info_t g_focus_session_interrupt_info = {NULL, NULL};
122 static pthread_mutex_t g_index_mutex = PTHREAD_MUTEX_INITIALIZER;
123 static pthread_mutex_t g_event_mutex = PTHREAD_MUTEX_INITIALIZER;
124 guint g_focus_signal_handle;
125 guint g_idle_event_src;
128 gboolean g_need_emergent_exit = FALSE;
131 /* handle to watch end of playing */
133 /* subscription id to unsubscribe when handle ended */
135 } play_sound_end_callback_data_t;
137 typedef struct _focus_idle_event {
138 focus_idle_event_type_e type;
140 } focus_idle_event_t;
142 void _system_signal_handler(int signo)
144 int ret = MM_ERROR_NONE;
145 sigset_t old_mask, all_mask;
147 debug_warning("Got signal : signo(%d)", signo);
151 sigfillset(&all_mask);
152 sigprocmask(SIG_BLOCK, &all_mask, &old_mask);
154 if (g_need_emergent_exit) {
155 ret = mm_sound_proxy_emergent_exit(getpid());
156 if (ret == MM_ERROR_NONE)
157 debug_msg("[Client] Success to emergnet_exit\n");
159 debug_error("[Client] Error occurred : 0x%x \n",ret);
162 sigprocmask(SIG_SETMASK, &old_mask, NULL);
167 sigaction(SIGINT, &system_int_old_action, NULL);
171 sigaction(SIGABRT, &system_abrt_old_action, NULL);
175 sigaction(SIGSEGV, &system_segv_old_action, NULL);
179 sigaction(SIGTERM, &system_term_old_action, NULL);
183 sigaction(SIGSYS, &system_sys_old_action, NULL);
187 sigaction(SIGXCPU, &system_xcpu_old_action, NULL);
194 debug_warning("signal handling end");
197 int mm_sound_client_initialize(void)
199 int ret = MM_ERROR_NONE;
202 mm_sound_proxy_initialize();
203 g_idle_event_src = 0;
206 struct sigaction system_action;
207 system_action.sa_handler = _system_signal_handler;
208 system_action.sa_flags = SA_NOCLDSTOP;
210 sigemptyset(&system_action.sa_mask);
212 sigaction(SIGINT, &system_action, &system_int_old_action);
213 sigaction(SIGABRT, &system_action, &system_abrt_old_action);
214 sigaction(SIGSEGV, &system_action, &system_segv_old_action);
215 sigaction(SIGTERM, &system_action, &system_term_old_action);
216 sigaction(SIGSYS, &system_action, &system_sys_old_action);
217 sigaction(SIGXCPU, &system_action, &system_xcpu_old_action);
223 int mm_sound_client_finalize(void)
225 int ret = MM_ERROR_NONE;
229 if (g_need_emergent_exit) {
230 ret = mm_sound_proxy_emergent_exit(getpid());
231 if (ret == MM_ERROR_NONE)
232 debug_msg("[Client] Success to emergnet_exit\n");
234 debug_error("[Client] Error occurred : 0x%x \n",ret);
237 sigaction(SIGINT, &system_int_old_action, NULL);
238 sigaction(SIGABRT, &system_abrt_old_action, NULL);
239 sigaction(SIGSEGV, &system_segv_old_action, NULL);
240 sigaction(SIGTERM, &system_term_old_action, NULL);
241 sigaction(SIGSYS, &system_sys_old_action, NULL);
242 sigaction(SIGXCPU, &system_xcpu_old_action, NULL);
244 ret = mm_sound_proxy_finalize();
249 if (g_focus_thread) {
250 g_main_loop_quit(g_focus_loop);
251 g_thread_join(g_focus_thread);
252 debug_log("after thread join");
253 g_main_loop_unref(g_focus_loop);
254 g_focus_thread = NULL;
257 if (g_idle_event_src > 0) {
258 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_event_mutex, MM_ERROR_SOUND_INTERNAL);
259 g_source_remove(g_idle_event_src);
260 MMSOUND_LEAVE_CRITICAL_SECTION(&g_event_mutex);
269 void mm_sound_convert_volume_type_to_stream_type(int volume_type, char *stream_type)
271 switch (volume_type) {
272 case VOLUME_TYPE_SYSTEM:
273 MMSOUND_STRNCPY(stream_type, "system", MM_SOUND_STREAM_TYPE_LEN);
275 case VOLUME_TYPE_NOTIFICATION:
276 MMSOUND_STRNCPY(stream_type, "notification", MM_SOUND_STREAM_TYPE_LEN);
278 case VOLUME_TYPE_ALARM:
279 MMSOUND_STRNCPY(stream_type, "alarm", MM_SOUND_STREAM_TYPE_LEN);
281 case VOLUME_TYPE_RINGTONE:
282 MMSOUND_STRNCPY(stream_type, "ringtone-voip", MM_SOUND_STREAM_TYPE_LEN);
284 case VOLUME_TYPE_MEDIA:
285 MMSOUND_STRNCPY(stream_type, "media", MM_SOUND_STREAM_TYPE_LEN);
287 case VOLUME_TYPE_CALL:
288 MMSOUND_STRNCPY(stream_type, "call-voice", MM_SOUND_STREAM_TYPE_LEN);
290 case VOLUME_TYPE_VOIP:
291 MMSOUND_STRNCPY(stream_type, "voip", MM_SOUND_STREAM_TYPE_LEN);
293 case VOLUME_TYPE_VOICE:
294 MMSOUND_STRNCPY(stream_type, "voice-recognition", MM_SOUND_STREAM_TYPE_LEN);
297 MMSOUND_STRNCPY(stream_type, "media", MM_SOUND_STREAM_TYPE_LEN);
301 debug_msg("volume type (%d) converted to stream type (%s)", volume_type, stream_type);
305 /*****************************************************************************************
306 DBUS SUPPORTED FUNCTIONS
307 ******************************************************************************************/
309 void _mm_sound_client_focus_signal_callback(mm_sound_signal_name_t signal, int value, void *user_data)
311 int ret = MM_ERROR_NONE;
314 debug_msg("focus signal received, value = %d", value);
317 ret = mm_sound_proxy_clear_focus(getpid());
319 debug_error("clear focus failed ret = 0x%x", ret);
320 mm_sound_unsubscribe_signal(g_focus_signal_handle);
321 g_focus_signal_handle = 0;
326 int mm_sound_client_play_tone(int number, int volume_config, double volume, int time, int *handle, bool enable_session)
328 int ret = MM_ERROR_NONE;
329 // int instance = -1; /* instance is unique to communicate with server : client message queue filter type */
330 int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(volume_config);
331 char stream_type[MM_SOUND_STREAM_TYPE_LEN] = {0, };
335 /* read session information */
336 int session_type = MM_SESSION_TYPE_MEDIA;
337 int session_options = 0;
338 int is_focus_registered = 0;
340 ret = mm_sound_get_signal_value(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &is_focus_registered);
342 debug_error("mm_sound_get_signal_value failed [0x%x]", ret);
343 return MM_ERROR_POLICY_INTERNAL;
346 if (is_focus_registered)
347 enable_session = false;
349 if (enable_session) {
350 if (MM_ERROR_NONE != _mm_session_util_read_information(-1, &session_type, &session_options)) {
351 debug_warning("[Client] Read Session Information failed. use default \"media\" type\n");
352 session_type = MM_SESSION_TYPE_MEDIA;
354 if(MM_ERROR_NONE != mm_session_init(session_type)) {
355 debug_critical("[Client] MMSessionInit() failed\n");
356 return MM_ERROR_POLICY_INTERNAL;
361 // instance = getpid();
362 //debug_log("[Client] pid for client ::: [%d]\n", instance);
365 debug_msg("[Client] Input number : %d\n", number);
366 /* Send req memory */
368 mm_sound_convert_volume_type_to_stream_type(volume_type, stream_type);
369 ret = mm_sound_proxy_play_tone(number, time, volume, volume_config,
370 session_type, session_options, getpid(), enable_session, handle, stream_type, -1);
372 if (enable_session && !g_focus_signal_handle) {
373 ret = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &g_focus_signal_handle, _mm_sound_client_focus_signal_callback, NULL);
375 debug_error("mm_sound_subscribe_signal failed [0x%x]", ret);
376 return MM_ERROR_POLICY_INTERNAL;
385 int mm_sound_client_play_tone_with_stream_info(int tone, char *stream_type, int stream_id, double volume, int duration, int *handle)
387 int ret = MM_ERROR_NONE;
391 ret = mm_sound_proxy_play_tone_with_stream_info(getpid(), tone, stream_type, stream_id, volume, duration, handle);
397 static void _mm_sound_stop_callback_wrapper_func(int ended_handle, void *userdata)
399 struct callback_data *cb_data = (struct callback_data*) userdata;
400 play_sound_end_callback_data_t *end_cb_data;
402 debug_log("[Wrapper CB][Play Stop] ended_handle : %d", ended_handle);
404 if (cb_data == NULL) {
405 debug_warning("stop callback data null");
409 end_cb_data = (play_sound_end_callback_data_t*) cb_data->extra_data;
411 if (ended_handle == end_cb_data->watching_handle) {
412 debug_log("Interested playing handle end : %d", ended_handle);
413 ((mm_sound_stop_callback_func)(cb_data->user_cb))(cb_data->user_data, ended_handle);
414 if (mm_sound_proxy_remove_play_sound_end_callback(end_cb_data->subs_id) != MM_ERROR_NONE)
415 debug_error("mm_sound_client_dbus_remove_play_file_end_callback failed");
417 debug_log("Not interested playing handle : %d", ended_handle);
421 static void play_end_callback_data_free_func(void *data)
423 struct callback_data *cb_data = (struct callback_data*) data;
426 g_free(cb_data->extra_data);
431 int mm_sound_client_play_sound(MMSoundPlayParam *param, int tone, int *handle)
433 int ret = MM_ERROR_NONE;
434 int session_type = MM_SESSION_TYPE_MEDIA;
435 int session_options = 0;
436 int is_focus_registered = 0;
437 // int instance = -1; /* instance is unique to communicate with server : client message queue filter type */
438 int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(param->volume_config);
439 char stream_type[MM_SOUND_STREAM_TYPE_LEN] = {0, };
440 struct callback_data *cb_data = NULL;
441 play_sound_end_callback_data_t *end_cb_data;
445 /* read session information */
447 ret = mm_sound_get_signal_value(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &is_focus_registered);
449 debug_error("mm_sound_get_signal_value failed [0x%x]", ret);
450 return MM_ERROR_POLICY_INTERNAL;
453 if (is_focus_registered)
454 param->skip_session = true;
456 if (param->skip_session == false) {
457 if(MM_ERROR_NONE != _mm_session_util_read_information(-1, &session_type, &session_options)) {
458 debug_warning("[Client] Read MMSession Type failed. use default \"media\" type\n");
459 session_type = MM_SESSION_TYPE_MEDIA;
461 if(MM_ERROR_NONE != mm_session_init(session_type)) {
462 debug_critical("[Client] MMSessionInit() failed\n");
463 return MM_ERROR_POLICY_INTERNAL;
469 if ((param->mem_ptr && param->mem_size)) {
470 // Play memory, deprecated
471 return MM_ERROR_INVALID_ARGUMENT;
474 mm_sound_convert_volume_type_to_stream_type(volume_type, stream_type);
475 ret = mm_sound_proxy_play_sound(param->filename, tone, param->loop, param->volume, param->volume_config,
476 session_type, session_options, getpid(), param->skip_session, handle, stream_type, -1);
477 if (ret != MM_ERROR_NONE) {
478 debug_error("Play Sound Failed");
481 if (param->callback) {
482 end_cb_data = (play_sound_end_callback_data_t *) g_malloc0(sizeof(play_sound_end_callback_data_t));
483 end_cb_data->watching_handle = *handle;
484 GET_CB_DATA(cb_data, param->callback, param->data, end_cb_data);
486 ret = mm_sound_proxy_add_play_sound_end_callback(_mm_sound_stop_callback_wrapper_func, cb_data, play_end_callback_data_free_func, &end_cb_data->subs_id);
487 if (ret != MM_ERROR_NONE) {
488 debug_error("Add callback for play sound(%d) Failed", *handle);
492 if (!param->skip_session && !g_focus_signal_handle) {
493 ret = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &g_focus_signal_handle, _mm_sound_client_focus_signal_callback, NULL);
495 debug_error("mm_sound_subscribe_signal failed [0x%x]", ret);
496 return MM_ERROR_POLICY_INTERNAL;
507 int mm_sound_client_play_sound_with_stream_info(MMSoundPlayParam *param, int *handle, char* stream_type, int stream_id)
509 int ret = MM_ERROR_NONE;
510 struct callback_data *cb_data = NULL;
511 play_sound_end_callback_data_t *end_cb_data;
513 ret = mm_sound_proxy_play_sound_with_stream_info(param->filename, param->loop, param->volume,
514 getpid(), handle, stream_type, stream_id);
515 if (ret != MM_ERROR_NONE) {
516 debug_error("Play Sound Failed");
519 if (param->callback) {
520 end_cb_data = (play_sound_end_callback_data_t *) g_malloc0(sizeof(play_sound_end_callback_data_t));
521 end_cb_data->watching_handle = *handle;
522 GET_CB_DATA(cb_data, param->callback, param->data, end_cb_data);
524 ret = mm_sound_proxy_add_play_sound_end_callback(_mm_sound_stop_callback_wrapper_func, cb_data, play_end_callback_data_free_func, &end_cb_data->subs_id);
525 if (ret != MM_ERROR_NONE) {
526 debug_error("Add callback for play sound(%d) Failed", *handle);
537 int mm_sound_client_stop_sound(int handle)
539 int ret = MM_ERROR_NONE;
542 if (handle < 0 || handle > CLIENT_HANDLE_MAX) {
543 ret = MM_ERROR_INVALID_ARGUMENT;
547 ret = mm_sound_proxy_stop_sound(handle);
553 static int _mm_sound_client_device_list_dump (GList *device_list)
555 int ret = MM_ERROR_NONE;
557 mm_sound_device_t *device_node = NULL;
560 debug_error("Device list NULL, cannot dump list");
561 return MM_ERROR_SOUND_INTERNAL;
564 debug_log("======================== device list : start ==========================\n");
565 for (list = device_list; list != NULL; list = list->next) {
566 device_node = (mm_sound_device_t *)list->data;
568 debug_log(" list idx[%d]: type[%17s], id[%02d], io_direction[%d], state[%d], name[%s]\n",
569 count++, device_node->type, device_node->id, device_node->io_direction, device_node->state, device_node->name);
572 debug_log("======================== device list : end ============================\n");
577 int mm_sound_client_get_current_connected_device_list(int device_flags, mm_sound_device_list_t *device_list)
579 int ret = MM_ERROR_NONE;
583 debug_error("Device list NULL");
584 ret = MM_ERROR_COMMON_INVALID_ARGUMENT;
588 if ((ret = mm_sound_proxy_get_current_connected_device_list(device_flags, &device_list->list)) != MM_ERROR_NONE) {
589 debug_error("[Client] failed to get current connected device list with dbus, ret[0x%x]", ret);
592 if (!device_list->list) {
593 debug_error("Got device list null");
594 ret = MM_ERROR_SOUND_NO_DATA;
597 _mm_sound_client_device_list_dump(device_list->list);
604 static bool device_is_match_direction(int direction, int mask)
606 if (mask == DEVICE_IO_DIRECTION_FLAGS || mask == 0)
609 if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_IN_FLAG) && (direction & MM_SOUND_DEVICE_IO_DIRECTION_IN))
611 if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_OUT_FLAG) && (direction & MM_SOUND_DEVICE_IO_DIRECTION_OUT))
613 if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_BOTH_FLAG) && (direction == MM_SOUND_DEVICE_IO_DIRECTION_BOTH))
619 static bool device_is_match_state(int state, int mask)
621 if (mask == DEVICE_STATE_FLAGS || mask == 0)
624 if ((mask & MM_SOUND_DEVICE_STATE_DEACTIVATED_FLAG) && (state == MM_SOUND_DEVICE_STATE_DEACTIVATED))
626 if ((mask & MM_SOUND_DEVICE_STATE_ACTIVATED_FLAG) && (state == MM_SOUND_DEVICE_STATE_ACTIVATED))
632 static bool device_is_match_type(const char *type, int mask)
635 const char *builtin_prefix = "builtin";
637 if (mask == DEVICE_TYPE_FLAGS || mask == 0)
640 is_builtin = !strncmp(type, builtin_prefix, strlen(builtin_prefix));
642 if ((mask & MM_SOUND_DEVICE_TYPE_INTERNAL_FLAG) && (is_builtin))
644 if ((mask & MM_SOUND_DEVICE_TYPE_EXTERNAL_FLAG) && (!is_builtin))
650 static bool device_is_match_with_mask(const char *type, int direction, int state, int mask)
652 if (mask == DEVICE_ALL_FLAG)
655 return (device_is_match_direction(direction, mask & DEVICE_IO_DIRECTION_FLAGS) &&
656 device_is_match_state(state, mask & DEVICE_STATE_FLAGS) &&
657 device_is_match_type(type, mask & DEVICE_TYPE_FLAGS));
660 static int _fill_sound_device(mm_sound_device_t *device_h, int device_id, const char *device_type,
661 int direction, int state, const char *name, int *stream_id, int stream_num)
665 if (stream_num > 0 && stream_id == NULL) {
666 debug_error("stream_num is %d, but stream_id is NULL", stream_num);
670 if (stream_num > MAX_STREAM_ON_DEVICE) {
671 debug_error("too many streams on this device");
675 device_h->id = device_id;
676 device_h->io_direction = direction;
677 device_h->state = state;
678 MMSOUND_STRNCPY(device_h->name, name, MAX_DEVICE_NAME_NUM);
679 MMSOUND_STRNCPY(device_h->type, device_type, MAX_DEVICE_TYPE_STR_LEN);
681 if (stream_num > 0) {
682 device_h->stream_num = stream_num;
683 debug_log("%d streams on this device", stream_num);
684 for (i = 0; i < stream_num; i++) {
685 debug_log(" stream_id : %d", stream_id[i]);
686 device_h->stream_id[i] = stream_id[i];
689 device_h->stream_num = 0;
690 debug_log("There is no stream on this device");
696 static void _mm_sound_device_connected_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
697 int state, const char *name, int *stream_id, int stream_num, 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, void* userdata, unsigned int *subs_id)
726 int ret = MM_ERROR_NONE;
727 struct callback_data *cb_data = NULL;
731 GET_CB_DATA(cb_data, func, userdata, (void*) device_flags);
733 ret = mm_sound_proxy_add_device_connected_callback(device_flags, _mm_sound_device_connected_callback_wrapper_func, cb_data, g_free, subs_id);
734 if (ret == MM_ERROR_NONE)
735 g_need_emergent_exit = TRUE;
741 int mm_sound_client_remove_device_connected_callback(unsigned int subs_id)
743 int ret = MM_ERROR_NONE;
746 ret = mm_sound_proxy_remove_device_connected_callback(subs_id);
752 static void _mm_sound_device_info_changed_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
753 int state, const char *name, int *stream_id, int stream_num, int changed_device_info_type, void *userdata)
755 mm_sound_device_t device_h;
756 struct callback_data *cb_data = (struct callback_data*) userdata;
759 debug_log("[Device Info Changed] id(%d) type(%s) direction(%d) state(%d) name(%s) changed_info_type(%d)",
760 device_id, device_type, io_direction, state, name, changed_device_info_type);
762 if (cb_data == NULL) {
763 debug_warning("device info changed callback data null");
767 device_flags = (int) cb_data->extra_data;
768 if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
771 if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name, stream_id, stream_num) < 0) {
772 debug_error("Failed to fill sound device");
776 ((mm_sound_device_info_changed_cb)(cb_data->user_cb))(&device_h, changed_device_info_type, cb_data->user_data);
779 int mm_sound_client_add_device_info_changed_callback(int device_flags, mm_sound_device_info_changed_cb func, void *userdata, unsigned int *subs_id)
781 int ret = MM_ERROR_NONE;
782 struct callback_data *cb_data = (struct callback_data*) userdata;
786 GET_CB_DATA(cb_data, func, userdata, (void *) device_flags);
788 ret = mm_sound_proxy_add_device_info_changed_callback(device_flags, _mm_sound_device_info_changed_callback_wrapper_func, cb_data, g_free, subs_id);
794 int mm_sound_client_remove_device_info_changed_callback(unsigned int subs_id)
796 int ret = MM_ERROR_NONE;
799 ret = mm_sound_proxy_remove_device_info_changed_callback(subs_id);
806 static void _mm_sound_device_state_changed_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
807 int state, const char *name, int *stream_id, int stream_num, void *userdata)
809 mm_sound_device_t device_h;
810 struct callback_data *cb_data = (struct callback_data*) userdata;
813 debug_log("[Device State Changed] id(%d) type(%s) direction(%d) state(%d) name(%s)",
814 device_id, device_type, io_direction, state, name);
816 if (cb_data == NULL) {
817 debug_warning("device state changed callback data null");
821 device_flags = (int) cb_data->extra_data;
823 if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
826 if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name, stream_id, stream_num) < 0) {
827 debug_error("Failed to fill sound device");
831 ((mm_sound_device_state_changed_cb)(cb_data->user_cb))(&device_h, state, cb_data->user_data);
834 int mm_sound_client_add_device_state_changed_callback(int device_flags, mm_sound_device_state_changed_cb func, void *userdata, unsigned int *id)
836 int ret = MM_ERROR_NONE;
837 struct callback_data *cb_data = (struct callback_data*) userdata;
841 GET_CB_DATA(cb_data, func, userdata, (void *) device_flags);
843 ret = mm_sound_proxy_add_device_state_changed_callback(device_flags, _mm_sound_device_state_changed_callback_wrapper_func, cb_data, g_free, id);
849 int mm_sound_client_remove_device_state_changed_callback(unsigned int id)
851 int ret = MM_ERROR_NONE;
854 ret = mm_sound_proxy_remove_device_state_changed_callback(id);
860 int mm_sound_client_is_stream_on_device(int stream_id, int device_id, bool *is_on)
862 int ret = MM_ERROR_NONE;
866 debug_error("Invalid Parameter");
867 ret = MM_ERROR_COMMON_INVALID_ARGUMENT;
871 if ((ret = mm_sound_proxy_is_stream_on_device(stream_id, device_id, is_on)) != MM_ERROR_NONE) {
872 debug_error("[Client] failed to query is stream on device, ret[0x%x]", ret);
881 int __convert_volume_type_to_str(int volume_type, char **volume_type_str)
883 int ret = MM_ERROR_NONE;
885 if (!volume_type_str) {
886 return MM_ERROR_COMMON_INVALID_ARGUMENT;
889 switch (volume_type) {
890 case VOLUME_TYPE_SYSTEM:
891 *volume_type_str = "system";
893 case VOLUME_TYPE_NOTIFICATION:
894 *volume_type_str = "notification";
896 case VOLUME_TYPE_ALARM:
897 *volume_type_str = "alarm";
899 case VOLUME_TYPE_RINGTONE:
900 *volume_type_str = "ringtone";
902 case VOLUME_TYPE_MEDIA:
903 *volume_type_str = "media";
905 case VOLUME_TYPE_CALL:
906 *volume_type_str = "call";
908 case VOLUME_TYPE_VOIP:
909 *volume_type_str = "voip";
911 case VOLUME_TYPE_VOICE:
912 *volume_type_str = "voice";
915 if (!strncmp(*volume_type_str,"", VOLUME_TYPE_LEN)) {
916 debug_error("could not find the volume_type[%d] in this switch case statement", volume_type);
917 ret = MM_ERROR_SOUND_INTERNAL;
919 debug_log("volume_type[%s]", *volume_type_str);
924 static int __convert_volume_type_to_int(const char *volume_type_str, volume_type_t *volume_type)
926 int ret = MM_ERROR_NONE;
928 if (!volume_type || !volume_type_str) {
929 return MM_ERROR_COMMON_INVALID_ARGUMENT;
932 if (!strncmp(volume_type_str, "system", VOLUME_TYPE_LEN)) {
933 *volume_type = VOLUME_TYPE_SYSTEM;
934 } else if (!strncmp(volume_type_str, "notification", VOLUME_TYPE_LEN)) {
935 *volume_type = VOLUME_TYPE_NOTIFICATION;
936 } else if (!strncmp(volume_type_str, "alarm", VOLUME_TYPE_LEN)) {
937 *volume_type = VOLUME_TYPE_ALARM;
938 } else if (!strncmp(volume_type_str, "ringtone", VOLUME_TYPE_LEN)) {
939 *volume_type = VOLUME_TYPE_RINGTONE;
940 } else if (!strncmp(volume_type_str, "media", VOLUME_TYPE_LEN)) {
941 *volume_type = VOLUME_TYPE_MEDIA;
942 } else if (!strncmp(volume_type_str, "call", VOLUME_TYPE_LEN)) {
943 *volume_type = VOLUME_TYPE_CALL;
944 } else if (!strncmp(volume_type_str, "voip", VOLUME_TYPE_LEN)) {
945 *volume_type = VOLUME_TYPE_VOIP;
946 } else if (!strncmp(volume_type_str, "voice", VOLUME_TYPE_LEN)) {
947 *volume_type = VOLUME_TYPE_VOICE;
949 debug_log("Invalid volume type : [%s]", volume_type_str);
950 ret = MM_ERROR_SOUND_INTERNAL;
956 int mm_sound_client_set_volume_by_type(const int volume_type, const unsigned int volume_level)
958 int ret = MM_ERROR_NONE;
959 char *type_str = NULL;
962 if ((ret = __convert_volume_type_to_str(volume_type, &type_str)) != MM_ERROR_NONE) {
963 debug_error("volume type convert failed");
967 ret = mm_sound_proxy_set_volume_by_type(type_str, volume_level);
974 static void _mm_sound_volume_changed_callback_wrapper_func(const char *direction, const char *volume_type_str, int volume_level, void *userdata)
976 volume_type_t volume_type = 0;
977 struct callback_data *cb_data = (struct callback_data *) userdata;
979 debug_log("[Wrapper CB][Volume Changed] direction : %s, volume_type : %s, volume_level : %d", direction, volume_type_str, volume_level);
981 if (cb_data == NULL) {
982 debug_warning("volume changed callback data null");
986 if (__convert_volume_type_to_int(volume_type_str, &volume_type) != MM_ERROR_NONE) {
987 debug_error("volume type convert failed");
990 debug_log("Call volume changed user cb, direction : %s, vol_type : %s(%d), level : %u", direction, volume_type_str, volume_type, volume_level);
991 ((mm_sound_volume_changed_cb)(cb_data->user_cb))(volume_type, volume_level, cb_data->user_data);
994 int mm_sound_client_add_volume_changed_callback(mm_sound_volume_changed_cb func, void* userdata, unsigned int *subs_id)
996 int ret = MM_ERROR_NONE;
997 struct callback_data *cb_data = NULL;
1001 GET_CB_DATA(cb_data, func, userdata, NULL);
1003 ret = mm_sound_proxy_add_volume_changed_callback(_mm_sound_volume_changed_callback_wrapper_func, cb_data, g_free, subs_id);
1010 int mm_sound_client_remove_volume_changed_callback(unsigned int subs_id)
1012 int ret = MM_ERROR_NONE;
1015 ret = mm_sound_proxy_remove_volume_changed_callback(subs_id);
1022 int mm_sound_client_set_session_interrupt_callback(mm_sound_focus_session_interrupt_cb callback, void* user_data)
1024 int ret = MM_ERROR_NONE;
1029 return MM_ERROR_INVALID_ARGUMENT;
1031 g_focus_session_interrupt_info.user_cb = callback;
1032 g_focus_session_interrupt_info.user_data = user_data;
1038 int mm_sound_client_unset_session_interrupt_callback(void)
1040 int ret = MM_ERROR_NONE;
1044 if (!g_focus_session_interrupt_info.user_cb) {
1045 debug_error("no callback to unset");
1046 return MM_ERROR_SOUND_INTERNAL;
1049 g_focus_session_interrupt_info.user_cb = NULL;
1050 g_focus_session_interrupt_info.user_data = NULL;
1056 static gpointer _focus_thread_func(gpointer data)
1058 unsigned int thread_id = (unsigned int)pthread_self();
1059 debug_warning(">>> thread func..ID of this thread(%u), mainloop(%p)", thread_id, g_focus_loop);
1061 g_main_loop_run(g_focus_loop);
1063 debug_warning("<<< quit thread func..(%u), mainloop(%p)", thread_id, g_focus_loop);
1067 static gboolean _focus_fd_check(GSource * source)
1073 debug_error("GSource is null");
1076 fd_list = source->poll_fds;
1078 debug_error("fd_list is null");
1082 temp = (GPollFD*)fd_list->data;
1084 debug_error("fd_list->data is null");
1087 if (temp->revents & (POLLIN | POLLPRI)) {
1090 fd_list = fd_list->next;
1093 return FALSE; /* there is no change in any fd state */
1096 static gboolean _focus_fd_prepare(GSource *source, gint *timeout)
1101 static gboolean _focus_fd_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
1103 callback(user_data);
1107 static int _focus_find_index_by_handle(int handle)
1110 for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1111 if (g_focus_sound_handle[i].focus_callback && handle == g_focus_sound_handle[i].handle) {
1112 /* debug_msg("found index(%d) for handle(%d)", i, handle);*/
1113 return (handle == FOCUS_HANDLE_INIT_VAL)? -1 : i;
1119 static int _focus_watch_find_index_by_handle(int handle)
1122 for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1123 if (g_focus_sound_handle[i].watch_callback && handle == g_focus_sound_handle[i].handle) {
1124 /* debug_msg("found index(%d) for watch handle(%d)", i, handle);*/
1125 return (handle == FOCUS_HANDLE_INIT_VAL)? -1 : i;
1131 static gboolean _focus_callback_handler(gpointer d)
1133 GPollFD *data = (GPollFD*)d;
1136 int focus_index = 0;
1137 focus_cb_data_lib cb_data;
1138 debug_log(">>> focus_callback_handler()..ID of this thread(%u)\n", (unsigned int)pthread_self());
1140 memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1143 debug_error("GPollFd is null");
1146 if (data->revents & (POLLIN | POLLPRI)) {
1147 int changed_state = -1;
1149 count = read(data->fd, &cb_data, sizeof(cb_data));
1151 char str_error[256];
1152 strerror_r(errno, str_error, sizeof(str_error));
1153 debug_error("GpollFD read fail, errno=%d(%s)",errno, str_error);
1156 changed_state = cb_data.state;
1157 focus_index = _focus_find_index_by_handle(cb_data.handle);
1158 if (focus_index == -1) {
1159 debug_error("Could not find index");
1163 g_mutex_lock(&g_focus_sound_handle[focus_index].focus_lock);
1165 tid = g_focus_sound_handle[focus_index].focus_tid;
1167 if (changed_state != -1) {
1168 debug_msg("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)", tid, cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type);
1169 if (g_focus_sound_handle[focus_index].focus_callback == NULL) {
1170 debug_error("callback is null..");
1171 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1174 debug_msg("[CALLBACK(%p) START]",g_focus_sound_handle[focus_index].focus_callback);
1175 (g_focus_sound_handle[focus_index].focus_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type, cb_data.option, cb_data.ext_info, g_focus_sound_handle[focus_index].user_data);
1176 debug_msg("[CALLBACK END]");
1177 if (g_focus_session_interrupt_info.user_cb) {
1178 debug_msg("sending session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
1179 (g_focus_session_interrupt_info.user_cb)(cb_data.state, cb_data.stream_type, false, g_focus_session_interrupt_info.user_data);
1182 #ifdef CONFIG_ENABLE_RETCB
1186 unsigned int buf = 0;
1187 char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", g_focus_sound_handle[focus_index].focus_tid, cb_data.handle);
1188 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1190 char str_error[256];
1191 strerror_r(errno, str_error, sizeof(str_error));
1192 debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n", tid, tmpfd, filename2, errno, str_error);
1194 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1197 /* buf contains data as below,
1198 * |<--12bits--><--4bits (reacquisition)--><--16bits (handle)-->| */
1199 buf = (unsigned int)((0x0000ffff & cb_data.handle) | (g_focus_sound_handle[focus_index].auto_reacquire << 16));
1200 rett = write(tmpfd, &buf, sizeof(buf));
1203 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
1208 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1213 static gboolean _focus_watch_callback_handler(gpointer d)
1215 GPollFD *data = (GPollFD*)d;
1218 int focus_index = 0;
1219 focus_cb_data_lib cb_data;
1223 memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1226 debug_error("GPollFd is null");
1229 if (data->revents & (POLLIN | POLLPRI)) {
1230 count = read(data->fd, &cb_data, sizeof(cb_data));
1232 char str_error[256];
1233 strerror_r(errno, str_error, sizeof(str_error));
1234 debug_error("GpollFD read fail, errno=%d(%s)",errno, str_error);
1238 focus_index = _focus_watch_find_index_by_handle(cb_data.handle);
1239 if (focus_index == -1) {
1240 debug_error("Could not find index");
1244 if (!g_focus_sound_handle[focus_index].is_used) {
1245 debug_warning("unsetting watch calllback has been already requested");
1246 goto SKIP_CB_AND_RET;
1249 debug_msg("lock focus_lock = %p", &g_focus_sound_handle[focus_index].focus_lock);
1250 g_mutex_lock(&g_focus_sound_handle[focus_index].focus_lock);
1252 tid = g_focus_sound_handle[focus_index].focus_tid;
1254 debug_msg("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)", tid, cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type);
1256 if (g_focus_sound_handle[focus_index].watch_callback == NULL) {
1257 debug_msg("callback is null..");
1259 debug_msg("[CALLBACK(%p) START]",g_focus_sound_handle[focus_index].watch_callback);
1260 (g_focus_sound_handle[focus_index].watch_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type, cb_data.ext_info, g_focus_sound_handle[focus_index].user_data);
1261 debug_msg("[CALLBACK END]");
1262 if (g_focus_session_interrupt_info.user_cb) {
1263 debug_msg("sending session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
1264 (g_focus_session_interrupt_info.user_cb)(cb_data.state, cb_data.stream_type, true, g_focus_session_interrupt_info.user_data);
1269 #ifdef CONFIG_ENABLE_RETCB
1274 char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr", g_focus_sound_handle[focus_index].focus_tid, cb_data.handle);
1275 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1277 char str_error[256];
1278 strerror_r(errno, str_error, sizeof(str_error));
1279 debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n", tid, tmpfd, filename2, errno, str_error);
1281 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1284 buf = cb_data.handle;
1285 rett = write(tmpfd, &buf, sizeof(buf));
1288 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
1293 if (g_focus_sound_handle[focus_index].is_used) {
1294 debug_msg("unlock focus_lock = %p", &g_focus_sound_handle[focus_index].focus_lock);
1295 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1303 static void _focus_open_callback(int index, bool is_for_watching)
1310 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1311 debug_error("Invalid focus handle index [%d]", index);
1315 if (is_for_watching) {
1316 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1318 filename = g_strdup_printf("/tmp/FOCUS.%d.%d", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1320 pre_mask = umask(0);
1321 if (mknod(filename, S_IFIFO|0666, 0)) {
1322 debug_error("mknod() failure, errno(%d)", errno);
1325 g_focus_sound_handle[index].focus_fd = open(filename, O_RDWR|O_NONBLOCK);
1326 if (g_focus_sound_handle[index].focus_fd == -1) {
1327 debug_error("Open fail : index(%d), file open error(%d)", index, errno);
1329 debug_log("Open success : index(%d), filename(%s), fd(%d)", index, filename, g_focus_sound_handle[index].focus_fd);
1334 #ifdef CONFIG_ENABLE_RETCB
1337 if (is_for_watching) {
1338 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1340 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1342 pre_mask = umask(0);
1343 if (mknod(filename2, S_IFIFO | 0666, 0)) {
1344 debug_error("mknod() failure, errno(%d)", errno);
1354 void _focus_close_callback(int index, bool is_for_watching)
1356 char *filename = NULL;
1360 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1361 debug_error("Invalid focus handle index [%d]", index);
1365 if (g_focus_sound_handle[index].focus_fd < 0) {
1366 debug_error("Close fail : index(%d)", index);
1368 close(g_focus_sound_handle[index].focus_fd);
1369 debug_log("Close Success : index(%d)", index);
1372 if (is_for_watching) {
1373 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1375 filename = g_strdup_printf("/tmp/FOCUS.%d.%d", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1377 if (remove(filename)) {
1378 debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)", filename, errno);
1383 #ifdef CONFIG_ENABLE_RETCB
1386 if (is_for_watching) {
1387 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1389 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1391 if (remove(filename2)) {
1392 debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)", filename2, errno);
1402 static bool _focus_add_sound_callback(int index, int fd, gushort events, focus_gLoopPollHandler_t p_gloop_poll_handler )
1404 GSource* g_src = NULL;
1405 GSourceFuncs *g_src_funcs = NULL; /* handler function */
1407 GPollFD *g_poll_fd = NULL; /* file descriptor */
1411 g_mutex_init(&g_focus_sound_handle[index].focus_lock);
1413 /* 1. make GSource Object */
1414 g_src_funcs = (GSourceFuncs *)g_malloc(sizeof(GSourceFuncs));
1416 debug_error("failed to g_malloc for g_src_funcs");
1420 g_src_funcs->prepare = _focus_fd_prepare;
1421 g_src_funcs->check = _focus_fd_check;
1422 g_src_funcs->dispatch = _focus_fd_dispatch;
1423 g_src_funcs->finalize = NULL;
1424 g_src = g_source_new(g_src_funcs, sizeof(GSource));
1426 debug_error("failed to g_source_new for g_src");
1430 /* 2. add file description which used in g_loop() */
1431 g_poll_fd = (GPollFD *)g_malloc(sizeof(GPollFD));
1433 debug_error("failed to g_malloc for g_poll_fd");
1437 g_poll_fd->events = events;
1439 /* 3. combine g_source object and file descriptor */
1440 g_source_add_poll(g_src, g_poll_fd);
1441 g_src_id = g_source_attach(g_src, g_main_loop_get_context(g_focus_loop));
1443 debug_error("failed to attach the source to context");
1447 /* 4. set callback */
1448 g_source_set_callback(g_src, p_gloop_poll_handler,(gpointer)g_poll_fd, NULL);
1450 debug_log("g_malloc : g_src_funcs(%p), g_poll_fd(%p)", g_src_funcs, g_poll_fd);
1452 /* 5. store to global handle */
1453 g_focus_sound_handle[index].focus_src = g_src;
1454 g_focus_sound_handle[index].g_src_funcs = g_src_funcs;
1455 g_focus_sound_handle[index].g_poll_fd = g_poll_fd;
1461 g_free(g_src_funcs);
1464 g_source_unref(g_src);
1469 static bool _focus_remove_sound_callback(int index)
1472 focus_sound_info_t *h = NULL;
1476 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1477 debug_error("Invalid focus handle index [%d]", index);
1481 h = &g_focus_sound_handle[index];
1483 if (h->focus_src && h->g_poll_fd) {
1484 debug_log("g_source_remove_poll : fd(%d), event(0x%x)",
1485 h->g_poll_fd->fd, h->g_poll_fd->events);
1486 g_source_remove_poll(h->focus_src, h->g_poll_fd);
1488 debug_error("[%d] focus_src[%p], g_poll_fd[%p]",
1489 index, h->focus_src, h->g_poll_fd);
1494 g_source_destroy(h->focus_src);
1495 g_source_unref(h->focus_src);
1496 h->focus_src = NULL;
1499 debug_log("g_free : g_src_funcs(%p), g_poll_fd(%p)",
1500 h->g_src_funcs, h->g_poll_fd);
1502 g_free(h->g_src_funcs);
1503 h->g_src_funcs = NULL;
1505 g_free(h->g_poll_fd);
1506 h->g_poll_fd = NULL;
1508 h->focus_callback = NULL;
1509 h->watch_callback = NULL;
1511 g_mutex_clear(&h->focus_lock);
1519 static void _focus_add_callback(int index, bool is_for_watching)
1523 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1524 debug_error("Invalid focus handle index [%d]", index);
1528 if (!is_for_watching) {
1529 if (!_focus_add_sound_callback(index, g_focus_sound_handle[index].focus_fd,
1530 (gushort)POLLIN | POLLPRI, _focus_callback_handler))
1531 debug_error("failed to _focus_add_sound_callback(%p)", _focus_callback_handler);
1532 } else { // need to check if it's necessary
1533 if (!_focus_add_sound_callback(index, g_focus_sound_handle[index].focus_fd,
1534 (gushort)POLLIN | POLLPRI, _focus_watch_callback_handler))
1535 debug_error("failed to _focus_add_sound_callback(%p)", _focus_watch_callback_handler);
1540 static void _focus_remove_callback(int index)
1543 if (!_focus_remove_sound_callback(index))
1544 debug_error("failed to __focus_remove_sound_callback()");
1548 static void _focus_init_callback(int index, bool is_for_watching)
1551 _focus_open_callback(index, is_for_watching);
1552 _focus_add_callback(index, is_for_watching);
1556 static void _focus_destroy_callback(int index, bool is_for_watching)
1559 _focus_remove_callback(index);
1560 _focus_close_callback(index, is_for_watching);
1564 int mm_sound_client_get_unique_id(int *id)
1566 int ret = MM_ERROR_NONE;
1571 ret = MM_ERROR_INVALID_ARGUMENT;
1573 ret = mm_sound_proxy_get_unique_id(id);
1580 int mm_sound_client_is_focus_cb_thread(GThread *mine, bool *result)
1582 int ret = MM_ERROR_NONE;
1584 if (!mine || !result)
1585 ret = MM_ERROR_INVALID_ARGUMENT;
1587 if (mine == g_focus_thread)
1596 int mm_sound_client_register_focus(int id, int pid, const char *stream_type, mm_sound_focus_changed_cb callback, bool is_for_session, void* user_data)
1598 int ret = MM_ERROR_NONE;
1603 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1607 for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
1608 if (g_focus_sound_handle[index].is_used == false) {
1609 g_focus_sound_handle[index].is_used = true;
1614 g_focus_sound_handle[index].focus_tid = instance;
1615 g_focus_sound_handle[index].handle = id;
1616 g_focus_sound_handle[index].focus_callback = callback;
1617 g_focus_sound_handle[index].user_data = user_data;
1618 g_focus_sound_handle[index].is_for_session = is_for_session;
1619 g_focus_sound_handle[index].auto_reacquire = true;
1621 ret = mm_sound_proxy_register_focus(id, pid, stream_type, callback, is_for_session, user_data);
1623 if (ret == MM_ERROR_NONE) {
1624 debug_msg("[Client] Success to register focus\n");
1625 g_need_emergent_exit = TRUE;
1626 if (!g_focus_thread) {
1627 GMainContext* focus_context = g_main_context_new ();
1628 g_focus_loop = g_main_loop_new (focus_context, FALSE);
1629 g_main_context_unref(focus_context);
1630 if (g_focus_loop == NULL) {
1631 debug_error("could not create mainloop..");
1632 ret = MM_ERROR_SOUND_INTERNAL;
1636 g_focus_thread = g_thread_new("focus-cb-thread", _focus_thread_func, NULL);
1637 if (g_focus_thread == NULL) {
1638 debug_error("could not create thread..");
1639 g_main_loop_unref(g_focus_loop);
1640 ret = MM_ERROR_SOUND_INTERNAL;
1644 debug_warning("focus thread(%p) with mainloop(%p) exists, skip thread creation",
1645 g_focus_thread, g_focus_loop);
1648 debug_error("[Client] Error occurred : 0x%x \n",ret);
1652 _focus_init_callback(index, false);
1657 g_focus_sound_handle[index].is_used = false;
1660 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1666 int mm_sound_client_unregister_focus(int id)
1668 int ret = MM_ERROR_NONE;
1673 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1675 index = _focus_find_index_by_handle(id);
1677 debug_error("Could not find index");
1678 ret = MM_ERROR_INVALID_ARGUMENT;
1681 instance = g_focus_sound_handle[index].focus_tid;
1683 if (!g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1684 debug_warning("maybe focus_callback is being called, try one more time..");
1685 usleep(2500000); // 2.5 sec
1686 if (g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1687 debug_msg("finally got focus_lock");
1691 ret = mm_sound_proxy_unregister_focus(instance, id, g_focus_sound_handle[index].is_for_session);
1693 if (ret == MM_ERROR_NONE)
1694 debug_msg("[Client] Success to unregister focus\n");
1696 debug_error("[Client] Error occurred : 0x%x \n",ret);
1698 g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
1700 _focus_destroy_callback(index, false);
1701 g_focus_sound_handle[index].focus_fd = 0;
1702 g_focus_sound_handle[index].focus_tid = 0;
1703 g_focus_sound_handle[index].handle = 0;
1704 g_focus_sound_handle[index].is_used = false;
1706 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1711 int mm_sound_client_set_focus_reacquisition(int id, bool reacquisition)
1713 int ret = MM_ERROR_NONE;
1719 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1721 index = _focus_find_index_by_handle(id);
1723 debug_error("Could not find index");
1724 ret = MM_ERROR_INVALID_ARGUMENT;
1727 instance = g_focus_sound_handle[index].focus_tid;
1729 ret = mm_sound_client_is_focus_cb_thread(g_thread_self(), &result);
1731 debug_error("[Client] mm_sound_client_is_focus_cb_thread failed");
1733 } else if (!result) {
1734 ret = mm_sound_proxy_set_foucs_reacquisition(instance, id, reacquisition);
1735 if (ret == MM_ERROR_NONE) {
1736 debug_msg("[Client] Success to set focus reacquisition to [%d]\n", reacquisition);
1738 debug_error("[Client] Error occurred : 0x%x \n",ret);
1742 debug_warning("[Client] Inside the focus cb thread, set focus reacquisition to [%d]\n", reacquisition);
1745 g_focus_sound_handle[index].auto_reacquire = reacquisition;
1748 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1753 int mm_sound_client_get_focus_reacquisition(int id, bool *reacquisition)
1755 int ret = MM_ERROR_NONE;
1760 if (!reacquisition) {
1761 debug_error("Invalid parameter");
1762 return MM_ERROR_INVALID_ARGUMENT;
1765 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1767 index = _focus_find_index_by_handle(id);
1769 debug_error("Could not find index");
1770 ret = MM_ERROR_INVALID_ARGUMENT;
1774 *reacquisition = g_focus_sound_handle[index].auto_reacquire;
1777 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1782 int mm_sound_client_get_acquired_focus_stream_type(int focus_type, char **stream_type, int *option, char **ext_info)
1784 int ret = MM_ERROR_NONE;
1785 char *ext_str = NULL;
1789 ret = mm_sound_proxy_get_acquired_focus_stream_type(focus_type, stream_type, option, &ext_str);
1790 if (ret == MM_ERROR_NONE) {
1791 debug_msg("[Client] Success to get stream type of acquired focus, stream_type(%s), ext_info(%s)\n", *stream_type, ext_str);
1792 *ext_info = strdup(ext_str);
1795 debug_error("[Client] Error occurred : 0x%x \n",ret);
1802 int mm_sound_client_acquire_focus(int id, mm_sound_focus_type_e type, int option, const char *ext_info)
1804 int ret = MM_ERROR_NONE;
1809 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1811 index = _focus_find_index_by_handle(id);
1813 debug_error("Could not find index");
1814 ret = MM_ERROR_INVALID_ARGUMENT;
1817 instance = g_focus_sound_handle[index].focus_tid;
1819 ret = mm_sound_proxy_acquire_focus(instance, id, type, option, ext_info, g_focus_sound_handle[index].is_for_session);
1820 if (ret == MM_ERROR_NONE)
1821 debug_msg("[Client] Success to acquire focus\n");
1823 debug_error("[Client] Error occurred : 0x%x \n",ret);
1826 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1831 int mm_sound_client_release_focus(int id, mm_sound_focus_type_e type, int option, const char *ext_info)
1833 int ret = MM_ERROR_NONE;
1838 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1840 index = _focus_find_index_by_handle(id);
1842 debug_error("Could not find index");
1843 ret = MM_ERROR_INVALID_ARGUMENT;
1846 instance = g_focus_sound_handle[index].focus_tid;
1848 ret = mm_sound_proxy_release_focus(instance, id, type, option, ext_info, g_focus_sound_handle[index].is_for_session);
1849 if (ret == MM_ERROR_NONE)
1850 debug_msg("[Client] Success to release focus\n");
1852 debug_error("[Client] Error occurred : 0x%x \n",ret);
1855 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1860 int mm_sound_client_set_focus_watch_callback(int pid, mm_sound_focus_type_e focus_type, mm_sound_focus_changed_watch_cb callback, bool is_for_session, void* user_data, int *id)
1862 int ret = MM_ERROR_NONE;
1869 return MM_ERROR_INVALID_ARGUMENT;
1871 //pthread_mutex_lock(&g_thread_mutex2);
1875 ret = mm_sound_proxy_get_unique_id(id);
1879 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1881 for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
1882 if (g_focus_sound_handle[index].is_used == false) {
1883 g_focus_sound_handle[index].is_used = true;
1888 g_focus_sound_handle[index].focus_tid = instance;
1889 g_focus_sound_handle[index].handle = *id;
1890 g_focus_sound_handle[index].watch_callback = callback;
1891 g_focus_sound_handle[index].user_data = user_data;
1892 g_focus_sound_handle[index].is_for_session = is_for_session;
1894 ret = mm_sound_proxy_set_focus_watch_callback(pid, g_focus_sound_handle[index].handle, focus_type, callback, is_for_session, user_data);
1896 if (ret == MM_ERROR_NONE) {
1897 debug_msg("[Client] Success to watch focus");
1898 g_need_emergent_exit = TRUE;
1899 if (!g_focus_thread) {
1900 GMainContext* focus_context = g_main_context_new ();
1901 g_focus_loop = g_main_loop_new (focus_context, FALSE);
1902 g_main_context_unref(focus_context);
1903 if (g_focus_loop == NULL) {
1904 debug_error("could not create mainloop..");
1905 ret = MM_ERROR_SOUND_INTERNAL;
1909 g_focus_thread = g_thread_new("focus-cb-thread", _focus_thread_func, NULL);
1910 if (g_focus_thread == NULL) {
1911 debug_error ("could not create thread..");
1912 g_main_loop_unref(g_focus_loop);
1913 ret = MM_ERROR_SOUND_INTERNAL;
1917 debug_warning("focus thread(%p) with mainloop(%p) exists, skip thread creation",
1918 g_focus_thread, g_focus_loop);
1921 debug_error("[Client] Error occurred : 0x%x",ret);
1925 _focus_init_callback(index, true);
1930 g_focus_sound_handle[index].is_used = false;
1933 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1938 int mm_sound_client_unset_focus_watch_callback(int id)
1940 int ret = MM_ERROR_NONE;
1943 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1945 index = _focus_watch_find_index_by_handle(id);
1947 debug_error("Could not find index");
1948 ret = MM_ERROR_INVALID_ARGUMENT;
1952 g_mutex_lock(&g_focus_sound_handle[index].focus_lock);
1954 g_focus_sound_handle[index].is_used = false;
1956 ret = mm_sound_proxy_unset_focus_watch_callback(g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle, g_focus_sound_handle[index].is_for_session);
1958 if (ret == MM_ERROR_NONE)
1959 debug_msg("[Client] Success to unwatch focus\n");
1961 debug_error("[Client] Error occurred : 0x%x \n",ret);
1964 g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
1966 _focus_destroy_callback(index, true);
1967 g_focus_sound_handle[index].focus_fd = 0;
1968 g_focus_sound_handle[index].focus_tid = 0;
1969 g_focus_sound_handle[index].handle = 0;
1971 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1976 static gboolean _idle_event_callback(void *data)
1978 focus_idle_event_t *idle_event_data = (focus_idle_event_t*)data;
1979 int ret = MM_ERROR_NONE;
1982 debug_error("data is null");
1986 debug_msg("idle_event_data(%p): type(%d), data(%d)",
1987 idle_event_data, idle_event_data->type, idle_event_data->data);
1989 switch (idle_event_data->type) {
1990 case IDLE_EVENT_TYPE_UNSET_FOCUS_WATCH_CB:
1991 if ((ret = mm_sound_client_unset_focus_watch_callback(idle_event_data->data)))
1992 debug_error("Could not unset focus watch callback, id(%d), ret = %x\n", idle_event_data->data, ret);
1994 case IDLE_EVENT_TYPE_UNREGISTER_FOCUS:
1995 if ((ret = mm_sound_client_unregister_focus(idle_event_data->data)))
1996 debug_error("Could not unregister focus, id(%d), ret = %x\n", idle_event_data->data, ret);
1999 debug_warning("invalid type(%d)", idle_event_data->type);
2003 g_free(idle_event_data);
2005 g_idle_event_src = 0;
2007 MMSOUND_LEAVE_CRITICAL_SECTION(&g_event_mutex);
2012 int mm_sound_client_execute_focus_func_in_main_context(focus_idle_event_type_e type, int data)
2014 focus_idle_event_t *idle_event_data = NULL;
2016 if (IDLE_EVENT_TYPE_UNSET_FOCUS_WATCH_CB > type || IDLE_EVENT_TYPE_MAX < type)
2017 return MM_ERROR_INVALID_ARGUMENT;
2019 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_event_mutex, MM_ERROR_SOUND_INTERNAL);
2021 idle_event_data = g_new0(focus_idle_event_t, 1);
2022 idle_event_data->type = type;
2023 idle_event_data->data = data;
2025 g_idle_event_src = g_idle_add_full(G_PRIORITY_HIGH,
2026 (GSourceFunc)_idle_event_callback,
2027 (gpointer)idle_event_data,
2030 return MM_ERROR_NONE;
2035 int mm_sound_client_add_test_callback(mm_sound_test_cb func, void* user_data, unsigned int *subs_id)
2037 int ret = MM_ERROR_NONE;
2041 ret = mm_sound_proxy_add_test_callback(func, user_data, g_free, subs_id);
2047 int mm_sound_client_remove_test_callback(unsigned int subs_id)
2049 int ret = MM_ERROR_NONE;
2052 ret = mm_sound_proxy_remove_test_callback(subs_id);
2059 int mm_sound_client_test(int a, int b, int* getv)
2061 int ret = MM_ERROR_NONE;
2065 ret = mm_sound_proxy_test(a, b, getv);
2066 debug_log("%d * %d -> result : %d", a, b, *getv);