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"
46 #if defined(__GSOURCE_CALLBACK__)
50 #define CLIENT_HANDLE_MAX 256
52 #define FOCUS_HANDLE_MAX 512
53 #define FOCUS_HANDLE_INIT_VAL -1
54 #define CONFIG_ENABLE_RETCB
56 #define VOLUME_TYPE_LEN 64
58 struct sigaction system_int_old_action;
59 struct sigaction system_abrt_old_action;
60 struct sigaction system_segv_old_action;
61 struct sigaction system_term_old_action;
62 struct sigaction system_sys_old_action;
63 struct sigaction system_xcpu_old_action;
65 struct callback_data {
72 typedef struct _FocusSource {
77 #define GET_CB_DATA(_cb_data, _func, _userdata, _extradata) \
79 _cb_data = (struct callback_data*) g_malloc0(sizeof(struct callback_data)); \
80 _cb_data->user_cb = _func; \
81 _cb_data->user_data = _userdata; \
82 _cb_data->extra_data = _extradata; \
94 GThread *focus_cb_thread;
95 GMainLoop *focus_loop;
96 mm_sound_focus_changed_cb focus_callback;
97 mm_sound_focus_changed_watch_cb watch_callback;
99 bool unset_watch_callback_requested;
100 } focus_sound_info_t;
107 char stream_type[MAX_STREAM_TYPE_LEN];
108 char ext_info[MM_SOUND_NAME_NUM];
112 typedef gboolean(*focus_callback_handler_t)(gpointer user_data);
114 focus_sound_info_t g_focus_sound_handle[FOCUS_HANDLE_MAX];
115 static pthread_mutex_t g_index_mutex = PTHREAD_MUTEX_INITIALIZER;
116 static pthread_mutex_t g_event_mutex = PTHREAD_MUTEX_INITIALIZER;
117 guint g_idle_event_src;
120 gboolean g_need_emergent_exit = FALSE;
123 /* handle to watch end of playing */
125 /* subscription id to unsubscribe when handle ended */
127 } play_sound_end_callback_data_t;
129 typedef struct _focus_idle_event {
130 focus_idle_event_type_e type;
132 } focus_idle_event_t;
134 void mm_sound_client_cleanup(void)
137 int ret = MM_ERROR_NONE;
139 if (g_need_emergent_exit) {
140 ret = mm_sound_proxy_emergent_exit(getpid());
141 if (ret == MM_ERROR_NONE)
142 debug_msg("Success to emergent_exit");
144 debug_error("Error occurred : 0x%x", ret);
147 if (g_need_emergent_exit)
148 mm_sound_proxy_emergent_exit(getpid());
152 void _system_signal_handler(int signo, siginfo_t *siginfo, void *context)
154 sigset_t old_mask, all_mask;
157 debug_warning("Got signal : signo(%d)", signo);
162 sigfillset(&all_mask);
163 sigprocmask(SIG_BLOCK, &all_mask, &old_mask);
165 mm_sound_client_cleanup();
167 sigprocmask(SIG_SETMASK, &old_mask, NULL);
172 if (system_int_old_action.sa_sigaction)
173 system_int_old_action.sa_sigaction(signo, siginfo, context);
175 sigaction(signo, &system_int_old_action, NULL);
178 if (system_abrt_old_action.sa_sigaction)
179 system_abrt_old_action.sa_sigaction(signo, siginfo, context);
181 sigaction(signo, &system_abrt_old_action, NULL);
184 if (system_segv_old_action.sa_sigaction)
185 system_segv_old_action.sa_sigaction(signo, siginfo, context);
187 sigaction(signo, &system_segv_old_action, NULL);
190 if (system_term_old_action.sa_sigaction)
191 system_term_old_action.sa_sigaction(signo, siginfo, context);
193 sigaction(signo, &system_term_old_action, NULL);
196 if (system_sys_old_action.sa_sigaction)
197 system_sys_old_action.sa_sigaction(signo, siginfo, context);
199 sigaction(signo, &system_sys_old_action, NULL);
202 if (system_xcpu_old_action.sa_sigaction)
203 system_xcpu_old_action.sa_sigaction(signo, siginfo, context);
205 sigaction(signo, &system_xcpu_old_action, NULL);
212 debug_warning("signal handling end");
217 static bool _is_dotnet_app(void)
219 char *is_dotnet = NULL;
221 is_dotnet = getenv("DOTNET_APP");
222 if (is_dotnet && atoi(is_dotnet) == 1)
229 int mm_sound_client_initialize(void)
231 int ret = MM_ERROR_NONE;
232 struct sigaction system_action;
236 mm_sound_proxy_initialize();
237 g_idle_event_src = 0;
240 if (_is_dotnet_app()) {
241 debug_warning("no signal handler for dotnet!!");
245 system_action.sa_sigaction = _system_signal_handler;
246 system_action.sa_flags = SA_NOCLDSTOP | SA_SIGINFO;
248 sigemptyset(&system_action.sa_mask);
250 sigaction(SIGINT, &system_action, &system_int_old_action);
251 sigaction(SIGABRT, &system_action, &system_abrt_old_action);
252 sigaction(SIGSEGV, &system_action, &system_segv_old_action);
253 sigaction(SIGTERM, &system_action, &system_term_old_action);
254 sigaction(SIGSYS, &system_action, &system_sys_old_action);
255 sigaction(SIGXCPU, &system_action, &system_xcpu_old_action);
261 int mm_sound_client_finalize(void)
263 int ret = MM_ERROR_NONE;
267 mm_sound_client_cleanup();
269 ret = mm_sound_proxy_finalize();
272 if (g_idle_event_src > 0) {
273 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_event_mutex, MM_ERROR_SOUND_INTERNAL);
274 g_source_remove(g_idle_event_src);
275 MMSOUND_LEAVE_CRITICAL_SECTION(&g_event_mutex);
280 if (_is_dotnet_app()) {
281 debug_warning("no signal handler for dotnet!!");
286 sigaction(SIGINT, &system_int_old_action, NULL);
287 sigaction(SIGABRT, &system_abrt_old_action, NULL);
288 sigaction(SIGSEGV, &system_segv_old_action, NULL);
289 sigaction(SIGTERM, &system_term_old_action, NULL);
290 sigaction(SIGSYS, &system_sys_old_action, NULL);
291 sigaction(SIGXCPU, &system_xcpu_old_action, NULL);
297 void mm_sound_convert_volume_type_to_stream_type(int volume_type, char *stream_type)
299 switch (volume_type) {
300 case VOLUME_TYPE_SYSTEM:
301 MMSOUND_STRNCPY(stream_type, "system", MAX_STREAM_TYPE_LEN);
303 case VOLUME_TYPE_NOTIFICATION:
304 MMSOUND_STRNCPY(stream_type, "notification", MAX_STREAM_TYPE_LEN);
306 case VOLUME_TYPE_ALARM:
307 MMSOUND_STRNCPY(stream_type, "alarm", MAX_STREAM_TYPE_LEN);
309 case VOLUME_TYPE_RINGTONE:
310 MMSOUND_STRNCPY(stream_type, "ringtone-voip", MAX_STREAM_TYPE_LEN);
312 case VOLUME_TYPE_MEDIA:
313 MMSOUND_STRNCPY(stream_type, "media", MAX_STREAM_TYPE_LEN);
315 case VOLUME_TYPE_CALL:
316 MMSOUND_STRNCPY(stream_type, "call-voice", MAX_STREAM_TYPE_LEN);
318 case VOLUME_TYPE_VOIP:
319 MMSOUND_STRNCPY(stream_type, "voip", MAX_STREAM_TYPE_LEN);
321 case VOLUME_TYPE_VOICE:
322 MMSOUND_STRNCPY(stream_type, "voice-information", MAX_STREAM_TYPE_LEN);
325 MMSOUND_STRNCPY(stream_type, "media", MAX_STREAM_TYPE_LEN);
329 debug_msg("volume type (%d) converted to stream type (%s)", volume_type, stream_type);
333 /*****************************************************************************************
334 DBUS SUPPORTED FUNCTIONS
335 ******************************************************************************************/
336 int mm_sound_client_play_tone_with_stream_info(int tone, char *stream_type, int stream_id,
337 double volume, int duration, int *handle)
339 int ret = MM_ERROR_NONE;
343 ret = mm_sound_proxy_play_tone_with_stream_info(getpid(), tone, stream_type, stream_id, volume, duration, handle);
349 static void _mm_sound_stop_callback_wrapper_func(int ended_handle, void *userdata)
351 struct callback_data *cb_data = (struct callback_data*) userdata;
352 play_sound_end_callback_data_t *end_cb_data;
354 debug_log("ended_handle : %d", ended_handle);
356 if (cb_data == NULL) {
357 debug_warning("stop callback data null");
361 end_cb_data = (play_sound_end_callback_data_t*) cb_data->extra_data;
363 if (ended_handle == end_cb_data->watching_handle) {
364 debug_log("Interested playing handle end : %d", ended_handle);
365 ((mm_sound_stop_callback_func)(cb_data->user_cb))(cb_data->user_data, ended_handle);
366 if (mm_sound_proxy_remove_play_sound_end_callback(end_cb_data->subs_id) != MM_ERROR_NONE)
367 debug_error("mm_sound_client_dbus_remove_play_file_end_callback failed");
369 debug_log("Not interested playing handle : %d", ended_handle);
373 static void play_end_callback_data_free_func(void *data)
375 struct callback_data *cb_data = (struct callback_data*) data;
378 g_free(cb_data->extra_data);
383 int mm_sound_client_play_sound_with_stream_info(MMSoundPlayParam *param, int *handle, char* stream_type, int stream_id)
385 int ret = MM_ERROR_NONE;
386 struct callback_data *cb_data = NULL;
387 play_sound_end_callback_data_t *end_cb_data;
389 ret = mm_sound_proxy_play_sound_with_stream_info(param->filename, param->loop, param->volume,
390 getpid(), handle, stream_type, stream_id);
391 if (ret != MM_ERROR_NONE) {
392 debug_error("Play Sound Failed");
395 if (param->callback) {
396 end_cb_data = (play_sound_end_callback_data_t *) g_malloc0(sizeof(play_sound_end_callback_data_t));
397 end_cb_data->watching_handle = *handle;
398 GET_CB_DATA(cb_data, param->callback, param->data, end_cb_data);
400 ret = mm_sound_proxy_add_play_sound_end_callback(_mm_sound_stop_callback_wrapper_func, cb_data,
401 play_end_callback_data_free_func, &end_cb_data->subs_id);
402 if (ret != MM_ERROR_NONE)
403 debug_error("Add callback for play sound(%d) Failed", *handle);
413 int mm_sound_client_stop_sound(int handle)
415 int ret = MM_ERROR_NONE;
418 if (handle < 0 || handle > CLIENT_HANDLE_MAX) {
419 ret = MM_ERROR_INVALID_ARGUMENT;
423 ret = mm_sound_proxy_stop_sound(handle);
429 static int _mm_sound_client_device_list_dump(GList *device_list)
431 int ret = MM_ERROR_NONE;
433 mm_sound_device_t *device_node = NULL;
436 debug_error("Device list NULL, cannot dump list");
437 return MM_ERROR_SOUND_INTERNAL;
440 debug_log("======================== device list : start ==========================");
441 for (list = device_list; list != NULL; list = list->next) {
442 device_node = (mm_sound_device_t *)list->data;
444 debug_log(" list idx[%d]: type[%17s], id[%02d], io_direction[%d], state[%d], name[%s]",
445 count++, device_node->type, device_node->id, device_node->io_direction,
446 device_node->state, device_node->name);
449 debug_log("======================== device list : end ============================");
454 int mm_sound_client_get_current_connected_device_list(int device_flags, mm_sound_device_list_t *device_list)
456 int ret = MM_ERROR_NONE;
460 debug_error("Device list NULL");
461 ret = MM_ERROR_COMMON_INVALID_ARGUMENT;
465 if ((ret = mm_sound_proxy_get_current_connected_device_list(device_flags, &device_list->list)) != MM_ERROR_NONE) {
466 debug_error("failed to get current connected device list with dbus, ret[0x%x]", ret);
469 if (!device_list->list) {
470 debug_error("Got device list null");
471 ret = MM_ERROR_SOUND_NO_DATA;
474 _mm_sound_client_device_list_dump(device_list->list);
481 int mm_sound_client_get_device_by_id(int device_id, mm_sound_device_t **device)
483 int ret = MM_ERROR_NONE;
487 if ((ret = mm_sound_proxy_get_device_by_id(device_id, device)) != MM_ERROR_NONE)
488 debug_error("failed to get device by id");
495 static bool device_is_match_direction(int direction, int mask)
497 if (mask == DEVICE_IO_DIRECTION_FLAGS || mask == 0)
500 if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_IN_FLAG) && (direction & MM_SOUND_DEVICE_IO_DIRECTION_IN))
502 if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_OUT_FLAG) && (direction & MM_SOUND_DEVICE_IO_DIRECTION_OUT))
504 if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_BOTH_FLAG) && (direction == MM_SOUND_DEVICE_IO_DIRECTION_BOTH))
510 static bool device_is_match_state(int state, int mask)
512 if (mask == DEVICE_STATE_FLAGS || mask == 0)
515 if ((mask & MM_SOUND_DEVICE_STATE_DEACTIVATED_FLAG) && (state == MM_SOUND_DEVICE_STATE_DEACTIVATED))
517 if ((mask & MM_SOUND_DEVICE_STATE_ACTIVATED_FLAG) && (state == MM_SOUND_DEVICE_STATE_ACTIVATED))
523 static bool device_is_match_type(const char *type, int mask)
526 const char *builtin_prefix = "builtin";
528 if (mask == DEVICE_TYPE_FLAGS || mask == 0)
531 is_builtin = !strncmp(type, builtin_prefix, strlen(builtin_prefix));
533 if ((mask & MM_SOUND_DEVICE_TYPE_INTERNAL_FLAG) && (is_builtin))
535 if ((mask & MM_SOUND_DEVICE_TYPE_EXTERNAL_FLAG) && (!is_builtin))
541 static bool device_is_match_with_mask(const char *type, int direction, int state, int mask)
543 if (mask == DEVICE_ALL_FLAG)
546 return (device_is_match_direction(direction, mask & DEVICE_IO_DIRECTION_FLAGS) &&
547 device_is_match_state(state, mask & DEVICE_STATE_FLAGS) &&
548 device_is_match_type(type, mask & DEVICE_TYPE_FLAGS));
551 static int _fill_sound_device(mm_sound_device_t *device_h, int device_id, const char *device_type,
552 int direction, int state, const char *name, int vendor_id, int product_id,
553 int *stream_id, int stream_num, bool is_running)
557 if (stream_num > 0 && stream_id == NULL) {
558 debug_error("stream_num is %d, but stream_id is NULL", stream_num);
562 if (stream_num > MAX_STREAM_ON_DEVICE) {
563 debug_error("too many streams on this device");
567 device_h->id = device_id;
568 device_h->io_direction = direction;
569 device_h->state = state;
570 MMSOUND_STRNCPY(device_h->name, name, MAX_DEVICE_NAME_NUM);
571 MMSOUND_STRNCPY(device_h->type, device_type, MAX_DEVICE_TYPE_STR_LEN);
572 device_h->vendor_id = vendor_id;
573 device_h->product_id = product_id;
574 device_h->is_running = is_running;
576 if (stream_num > 0) {
577 device_h->stream_num = stream_num;
578 debug_log("%d streams on this device", stream_num);
579 for (i = 0; i < stream_num; i++) {
580 debug_log(" stream_id : %d", stream_id[i]);
581 device_h->stream_id[i] = stream_id[i];
584 device_h->stream_num = 0;
585 debug_log("There is no stream on this device");
591 static void _mm_sound_device_connected_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
592 int state, const char *name, int vendor_id, int product_id, bool is_running,
593 int *stream_id, int stream_num, bool is_connected, void *userdata)
595 mm_sound_device_t device_h;
596 struct callback_data *cb_data = (struct callback_data*) userdata;
599 debug_log("[Device %s] id(%d) type(%s) direction(%d) state(%d) name(%s) is_running(%d) vendor-id(%04x) product-id(%04x)",
600 is_connected ? "Connected" : "Disconnected", device_id, device_type, io_direction, state, name,
601 is_running, vendor_id, product_id);
603 if (cb_data == NULL) {
604 debug_warning("device connected changed callback data null");
608 device_flags = (int) cb_data->extra_data;
609 if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
612 if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name,
613 vendor_id, product_id, stream_id, stream_num, is_running) < 0) {
614 debug_error("Failed to fill sound device");
618 ((mm_sound_device_connected_cb)(cb_data->user_cb))(&device_h, is_connected, cb_data->user_data);
621 int mm_sound_client_add_device_connected_callback(int device_flags, mm_sound_device_connected_cb func,
622 void* userdata, unsigned int *subs_id)
624 int ret = MM_ERROR_NONE;
625 struct callback_data *cb_data = NULL;
629 GET_CB_DATA(cb_data, func, userdata, (void*) device_flags);
631 ret = mm_sound_proxy_add_device_connected_callback(_mm_sound_device_connected_callback_wrapper_func,
632 cb_data, g_free, subs_id);
633 if (ret == MM_ERROR_NONE)
634 g_need_emergent_exit = TRUE;
640 int mm_sound_client_remove_device_connected_callback(unsigned int subs_id)
642 int ret = MM_ERROR_NONE;
645 ret = mm_sound_proxy_remove_device_connected_callback(subs_id);
651 static void _mm_sound_device_info_changed_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
652 int state, const char *name, int vendor_id, int product_id, bool is_running,
653 int *stream_id, int stream_num, int changed_device_info_type, void *userdata)
655 mm_sound_device_t device_h;
656 struct callback_data *cb_data = (struct callback_data*) userdata;
659 debug_log("[Device Info Changed] id(%d) type(%s) direction(%d) state(%d) name(%s) is_running(%d) "
660 "vendor-id(%04x) product-id(%04x) changed_info_type(%d)",
661 device_id, device_type, io_direction, state, name, is_running, vendor_id, product_id, changed_device_info_type);
663 if (cb_data == NULL) {
664 debug_warning("device info changed callback data null");
668 device_flags = (int) cb_data->extra_data;
669 if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
672 if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name,
673 vendor_id, product_id, stream_id, stream_num, is_running) < 0) {
674 debug_error("Failed to fill sound device");
678 ((mm_sound_device_info_changed_cb)(cb_data->user_cb))(&device_h, changed_device_info_type, cb_data->user_data);
681 int mm_sound_client_add_device_info_changed_callback(int device_flags, mm_sound_device_info_changed_cb func,
682 void *userdata, unsigned int *subs_id)
684 int ret = MM_ERROR_NONE;
685 struct callback_data *cb_data = (struct callback_data*) userdata;
689 GET_CB_DATA(cb_data, func, userdata, (void *) device_flags);
691 ret = mm_sound_proxy_add_device_info_changed_callback(_mm_sound_device_info_changed_callback_wrapper_func,
692 cb_data, g_free, subs_id);
698 int mm_sound_client_remove_device_info_changed_callback(unsigned int subs_id)
700 int ret = MM_ERROR_NONE;
703 ret = mm_sound_proxy_remove_device_info_changed_callback(subs_id);
710 static void _mm_sound_device_state_changed_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
711 int state, const char *name, int vendor_id, int product_id,
712 bool is_running, int *stream_id, int stream_num, void *userdata)
714 mm_sound_device_t device_h;
715 struct callback_data *cb_data = (struct callback_data*) userdata;
718 debug_log("[Device State Changed] id(%d) type(%s) direction(%d) state(%d) name(%s) is_running(%d) vendor-id(%04x) product-id(%04x)",
719 device_id, device_type, io_direction, state, name, is_running, vendor_id, product_id);
721 if (cb_data == NULL) {
722 debug_warning("device state changed callback data null");
726 device_flags = (int) cb_data->extra_data;
728 if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
731 if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name,
732 vendor_id, product_id, stream_id, stream_num, is_running) < 0) {
733 debug_error("Failed to fill sound device");
737 ((mm_sound_device_state_changed_cb)(cb_data->user_cb))(&device_h, state, cb_data->user_data);
740 int mm_sound_client_add_device_state_changed_callback(int device_flags, mm_sound_device_state_changed_cb func,
741 void *userdata, unsigned int *id)
743 int ret = MM_ERROR_NONE;
744 struct callback_data *cb_data = (struct callback_data*) userdata;
748 GET_CB_DATA(cb_data, func, userdata, (void *) device_flags);
750 ret = mm_sound_proxy_add_device_state_changed_callback(_mm_sound_device_state_changed_callback_wrapper_func,
751 cb_data, g_free, id);
757 int mm_sound_client_remove_device_state_changed_callback(unsigned int id)
759 int ret = MM_ERROR_NONE;
762 ret = mm_sound_proxy_remove_device_state_changed_callback(id);
768 static void _mm_sound_device_running_changed_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
769 int state, const char *name, int vendor_id, int product_id,
770 bool is_running, int *stream_id, int stream_num, void *userdata)
772 mm_sound_device_t device_h;
773 struct callback_data *cb_data = (struct callback_data*) userdata;
776 debug_log("[Device Running Changed] id(%d) type(%s) direction(%d) state(%d) name(%s) is_running(%d) vendor-id(%04x), product-id(%04x)",
777 device_id, device_type, io_direction, state, name, is_running, vendor_id, product_id);
779 if (cb_data == NULL) {
780 debug_warning("device running changed callback data null");
784 device_flags = (int) cb_data->extra_data;
786 if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
789 if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name,
790 vendor_id, product_id, stream_id, stream_num, is_running) < 0) {
791 debug_error("Failed to fill sound device");
795 ((mm_sound_device_running_changed_cb)(cb_data->user_cb))(&device_h, is_running, cb_data->user_data);
798 int mm_sound_client_add_device_running_changed_callback(int device_flags, mm_sound_device_running_changed_cb func,
799 void *userdata, unsigned int *id)
801 int ret = MM_ERROR_NONE;
802 struct callback_data *cb_data = (struct callback_data*) userdata;
806 GET_CB_DATA(cb_data, func, userdata, (void *) device_flags);
808 ret = mm_sound_proxy_add_device_running_changed_callback(_mm_sound_device_running_changed_callback_wrapper_func,
809 cb_data, g_free, id);
815 int mm_sound_client_remove_device_running_changed_callback(unsigned int id)
817 int ret = MM_ERROR_NONE;
820 ret = mm_sound_proxy_remove_device_running_changed_callback(id);
826 int mm_sound_client_is_stream_on_device(int stream_id, int device_id, bool *is_on)
828 int ret = MM_ERROR_NONE;
832 debug_error("Invalid Parameter");
833 ret = MM_ERROR_COMMON_INVALID_ARGUMENT;
837 if ((ret = mm_sound_proxy_is_stream_on_device(stream_id, device_id, is_on)) != MM_ERROR_NONE) {
838 debug_error("failed to query is stream on device, ret[0x%x]", ret);
847 int __convert_volume_type_to_str(int volume_type, char **volume_type_str)
849 int ret = MM_ERROR_NONE;
851 if (!volume_type_str)
852 return MM_ERROR_COMMON_INVALID_ARGUMENT;
854 switch (volume_type) {
855 case VOLUME_TYPE_SYSTEM:
856 *volume_type_str = "system";
858 case VOLUME_TYPE_NOTIFICATION:
859 *volume_type_str = "notification";
861 case VOLUME_TYPE_ALARM:
862 *volume_type_str = "alarm";
864 case VOLUME_TYPE_RINGTONE:
865 *volume_type_str = "ringtone";
867 case VOLUME_TYPE_MEDIA:
868 *volume_type_str = "media";
870 case VOLUME_TYPE_CALL:
871 *volume_type_str = "call";
873 case VOLUME_TYPE_VOIP:
874 *volume_type_str = "voip";
876 case VOLUME_TYPE_VOICE:
877 *volume_type_str = "voice";
880 debug_error("unexpected volume type [%d]", volume_type);
881 return MM_ERROR_SOUND_INTERNAL;
883 if (!strncmp(*volume_type_str, "", VOLUME_TYPE_LEN)) {
884 debug_error("could not find the volume_type[%d] in this switch case statement", volume_type);
885 ret = MM_ERROR_SOUND_INTERNAL;
887 debug_log("volume_type[%s]", *volume_type_str);
892 static int __convert_volume_type_to_int(const char *volume_type_str, volume_type_t *volume_type)
894 int ret = MM_ERROR_NONE;
896 if (!volume_type || !volume_type_str)
897 return MM_ERROR_COMMON_INVALID_ARGUMENT;
899 if (!strncmp(volume_type_str, "system", VOLUME_TYPE_LEN)) {
900 *volume_type = VOLUME_TYPE_SYSTEM;
901 } else if (!strncmp(volume_type_str, "notification", VOLUME_TYPE_LEN)) {
902 *volume_type = VOLUME_TYPE_NOTIFICATION;
903 } else if (!strncmp(volume_type_str, "alarm", VOLUME_TYPE_LEN)) {
904 *volume_type = VOLUME_TYPE_ALARM;
905 } else if (!strncmp(volume_type_str, "ringtone", VOLUME_TYPE_LEN)) {
906 *volume_type = VOLUME_TYPE_RINGTONE;
907 } else if (!strncmp(volume_type_str, "media", VOLUME_TYPE_LEN)) {
908 *volume_type = VOLUME_TYPE_MEDIA;
909 } else if (!strncmp(volume_type_str, "call", VOLUME_TYPE_LEN)) {
910 *volume_type = VOLUME_TYPE_CALL;
911 } else if (!strncmp(volume_type_str, "voip", VOLUME_TYPE_LEN)) {
912 *volume_type = VOLUME_TYPE_VOIP;
913 } else if (!strncmp(volume_type_str, "voice", VOLUME_TYPE_LEN)) {
914 *volume_type = VOLUME_TYPE_VOICE;
916 debug_log("Invalid volume type : [%s]", volume_type_str);
917 ret = MM_ERROR_SOUND_INTERNAL;
923 int mm_sound_client_set_volume_by_type(const int volume_type, const unsigned int volume_level)
925 int ret = MM_ERROR_NONE;
926 char *type_str = NULL;
929 if ((ret = __convert_volume_type_to_str(volume_type, &type_str)) != MM_ERROR_NONE) {
930 debug_error("volume type convert failed");
934 ret = mm_sound_proxy_set_volume_by_type(type_str, volume_level);
941 static void _mm_sound_volume_changed_callback_wrapper_func(const char *direction, const char *volume_type_str,
942 int volume_level, void *userdata)
944 volume_type_t volume_type = 0;
945 struct callback_data *cb_data = (struct callback_data *) userdata;
947 debug_log("direction : %s, volume_type : %s, volume_level : %d",
948 direction, volume_type_str, volume_level);
950 if (cb_data == NULL) {
951 debug_warning("volume changed callback data null");
955 if (__convert_volume_type_to_int(volume_type_str, &volume_type) != MM_ERROR_NONE) {
956 debug_error("volume type convert failed");
959 debug_log("Call volume changed user cb, direction : %s, vol_type : %s(%d), level : %u",
960 direction, volume_type_str, volume_type, volume_level);
961 ((mm_sound_volume_changed_cb)(cb_data->user_cb))(volume_type, volume_level, cb_data->user_data);
964 int mm_sound_client_add_volume_changed_callback(mm_sound_volume_changed_cb func, void* userdata, unsigned int *subs_id)
966 int ret = MM_ERROR_NONE;
967 struct callback_data *cb_data = NULL;
971 GET_CB_DATA(cb_data, func, userdata, NULL);
973 ret = mm_sound_proxy_add_volume_changed_callback(_mm_sound_volume_changed_callback_wrapper_func, cb_data, g_free, subs_id);
980 int mm_sound_client_remove_volume_changed_callback(unsigned int subs_id)
982 int ret = MM_ERROR_NONE;
985 ret = mm_sound_proxy_remove_volume_changed_callback(subs_id);
991 int mm_sound_client_set_filter_by_type(const char *stream_type, const char *filter_name, const char *filter_parameters, const char *filter_group)
993 int ret = MM_ERROR_NONE;
996 ret = mm_sound_proxy_set_filter_by_type(stream_type, filter_name, filter_parameters, filter_group);
1002 int mm_sound_client_unset_filter_by_type(const char *stream_type)
1004 int ret = MM_ERROR_NONE;
1007 ret = mm_sound_proxy_unset_filter_by_type(stream_type);
1013 int mm_sound_client_control_filter_by_type(const char *stream_type, const char *filter_name, const char *filter_controls)
1015 int ret = MM_ERROR_NONE;
1018 ret = mm_sound_proxy_control_filter_by_type(stream_type, filter_name, filter_controls);
1025 static gpointer _focus_thread_func(gpointer data)
1027 unsigned int thread_id = (unsigned int)pthread_self();
1028 GMainLoop *focus_loop = (GMainLoop*)data;
1030 debug_warning(">>> thread id(%u), mainloop[%p]", thread_id, focus_loop);
1032 g_main_loop_run(focus_loop);
1033 debug_warning("<<< quit : thread id(%u), mainloop[%p]", thread_id, focus_loop);
1038 static gboolean _focus_fd_prepare(GSource *source, gint *timeout)
1041 debug_warning("[ PREPARE : %p, (%p, %d)", source, timeout, timeout ? *timeout : -1);
1046 static gboolean _focus_fd_check(GSource * source)
1048 FocusSource* fsource = (FocusSource *)source;
1051 debug_error("GSource is null");
1055 debug_warning("CHECK : %p, 0x%x ]", source, fsource->pollfd.revents);
1057 if (fsource->poll_fd.revents & (POLLIN | POLLPRI))
1063 static gboolean _focus_fd_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
1065 debug_warning("*** DISPATCH : %p, (%p, %p)", source, callback, user_data);
1066 return callback(user_data);
1069 static void _focus_fd_finalize(GSource *source)
1071 debug_warning("### FINALIZE : %p", source);
1074 static int _focus_find_index_by_handle(int handle)
1077 for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1078 if (g_focus_sound_handle[i].focus_callback && handle == g_focus_sound_handle[i].handle) {
1079 /* debug_msg("found index(%d) for handle(%d)", i, handle);*/
1080 return (handle == FOCUS_HANDLE_INIT_VAL) ? -1 : i;
1086 static int _focus_watch_find_index_by_handle(int handle)
1089 for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1090 if (g_focus_sound_handle[i].watch_callback && handle == g_focus_sound_handle[i].handle) {
1091 /* debug_msg("found index(%d) for watch handle(%d)", i, handle);*/
1092 return (handle == FOCUS_HANDLE_INIT_VAL) ? -1 : i;
1098 static gboolean _focus_callback_handler(gpointer user_data)
1100 focus_sound_info_t *focus_handle = (focus_sound_info_t *)user_data;
1104 focus_cb_data_lib cb_data;
1106 debug_log(">>> thread id(%u)", (unsigned int)pthread_self());
1108 if (!focus_handle) {
1109 debug_error("focus_handle is null");
1110 return G_SOURCE_CONTINUE;
1112 poll_fd = &focus_handle->fsrc->poll_fd;
1113 debug_log("focus_handle(%p), poll_fd(%p)", focus_handle, poll_fd);
1115 memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1117 if (poll_fd->revents & (POLLIN | POLLPRI)) {
1118 int changed_state = -1;
1120 count = read(poll_fd->fd, &cb_data, sizeof(cb_data));
1122 char str_error[256];
1123 strerror_r(errno, str_error, sizeof(str_error));
1124 debug_error("GpollFD read fail, errno=%d(%s)", errno, str_error);
1125 return G_SOURCE_CONTINUE;
1127 changed_state = cb_data.state;
1129 g_mutex_lock(&focus_handle->focus_lock);
1131 tid = focus_handle->focus_tid;
1133 if (changed_state != -1) {
1134 debug_msg("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)",
1135 tid, cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type);
1136 if (focus_handle->focus_callback == NULL) {
1137 debug_error("focus callback is null..");
1138 g_mutex_unlock(&focus_handle->focus_lock);
1139 return G_SOURCE_CONTINUE;
1141 debug_msg("[CALLBACK(%p) START]", focus_handle->focus_callback);
1142 (focus_handle->focus_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type,
1143 cb_data.option, cb_data.ext_info, focus_handle->user_data);
1144 debug_msg("[CALLBACK END]");
1146 #ifdef CONFIG_ENABLE_RETCB
1150 unsigned int buf = 0;
1151 char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", focus_handle->focus_tid, cb_data.handle);
1152 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1154 char str_error[256];
1155 strerror_r(errno, str_error, sizeof(str_error));
1156 debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)",
1157 tid, tmpfd, filename2, errno, str_error);
1159 g_mutex_unlock(&focus_handle->focus_lock);
1160 return G_SOURCE_CONTINUE;
1162 /* buf contains data as below,
1163 * |<--12bits--><--4bits (reacquisition)--><--16bits (handle)-->| */
1164 buf = (unsigned int)((0x0000ffff & cb_data.handle) | (focus_handle->auto_reacquire << 16));
1165 rett = write(tmpfd, &buf, sizeof(buf));
1168 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)", tid, rett);
1173 g_mutex_unlock(&focus_handle->focus_lock);
1177 return G_SOURCE_CONTINUE;
1180 static gboolean _focus_watch_callback_handler(gpointer user_data)
1182 focus_sound_info_t *focus_handle = (focus_sound_info_t *)user_data;
1186 focus_cb_data_lib cb_data;
1188 debug_log(">>> thread id(%u)", (unsigned int)pthread_self());
1190 if (!focus_handle) {
1191 debug_error("focus_handle is null");
1192 return G_SOURCE_CONTINUE;
1194 poll_fd = &focus_handle->fsrc->poll_fd;
1195 debug_log("focus_handle(%p), poll_fd(%p)", focus_handle, poll_fd);
1197 memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1199 if (poll_fd->revents & (POLLIN | POLLPRI)) {
1200 count = read(poll_fd->fd, &cb_data, sizeof(cb_data));
1202 char str_error[256];
1203 strerror_r(errno, str_error, sizeof(str_error));
1204 debug_error("GpollFD read fail, errno=%d(%s)", errno, str_error);
1205 return G_SOURCE_CONTINUE;
1208 if (!focus_handle->is_used) {
1209 debug_warning("unsetting watch calllback has been already requested");
1210 goto SKIP_CB_AND_RET;
1213 g_mutex_lock(&focus_handle->focus_lock);
1215 tid = focus_handle->focus_tid;
1217 debug_msg("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)",
1218 tid, cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type);
1220 if (focus_handle->watch_callback == NULL) {
1221 debug_warning("watch callback is null..");
1222 goto SKIP_CB_AND_RET;
1224 if (focus_handle->unset_watch_callback_requested == true) {
1225 debug_warning("unset_watch_callback_requested..");
1226 goto SKIP_CB_AND_RET;
1229 debug_msg("[CALLBACK(%p) START]", focus_handle->watch_callback);
1230 (focus_handle->watch_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type,
1231 cb_data.ext_info, focus_handle->user_data);
1232 debug_msg("[CALLBACK END]");
1235 #ifdef CONFIG_ENABLE_RETCB
1240 char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr", focus_handle->focus_tid, cb_data.handle);
1241 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1243 char str_error[256];
1244 strerror_r(errno, str_error, sizeof(str_error));
1245 debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)",
1246 tid, tmpfd, filename2, errno, str_error);
1248 g_mutex_unlock(&focus_handle->focus_lock);
1249 return G_SOURCE_CONTINUE;
1251 buf = cb_data.handle;
1252 rett = write(tmpfd, &buf, sizeof(buf));
1255 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)", tid, rett);
1260 if (focus_handle->is_used) {
1261 debug_msg("unlock focus_lock = %p", &focus_handle->focus_lock);
1262 g_mutex_unlock(&focus_handle->focus_lock);
1267 return G_SOURCE_CONTINUE;
1270 static void _focus_open_callback(int index, bool is_for_watching)
1277 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1278 debug_error("Invalid focus handle index [%d]", index);
1282 if (is_for_watching) {
1283 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch",
1284 g_focus_sound_handle[index].focus_tid,
1285 g_focus_sound_handle[index].handle);
1287 filename = g_strdup_printf("/tmp/FOCUS.%d.%d",
1288 g_focus_sound_handle[index].focus_tid,
1289 g_focus_sound_handle[index].handle);
1291 pre_mask = umask(0);
1292 if (mknod(filename, S_IFIFO|0666, 0))
1293 debug_error("mknod() failure, errno(%d)", errno);
1295 g_focus_sound_handle[index].focus_fd = open(filename, O_RDWR|O_NONBLOCK);
1296 if (g_focus_sound_handle[index].focus_fd == -1) {
1297 debug_error("Open fail : index(%d), file open error(%d)", index, errno);
1299 debug_log("Open success : index(%d), filename(%s), fd(%d)",
1300 index, filename, g_focus_sound_handle[index].focus_fd);
1305 #ifdef CONFIG_ENABLE_RETCB
1308 if (is_for_watching) {
1309 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr",
1310 g_focus_sound_handle[index].focus_tid,
1311 g_focus_sound_handle[index].handle);
1313 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr",
1314 g_focus_sound_handle[index].focus_tid,
1315 g_focus_sound_handle[index].handle);
1317 pre_mask = umask(0);
1318 if (mknod(filename2, S_IFIFO | 0666, 0))
1319 debug_error("mknod() failure, errno(%d)", errno);
1328 void _focus_close_callback(int index, bool is_for_watching)
1330 char *filename = NULL;
1334 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1335 debug_error("Invalid focus handle index [%d]", index);
1339 if (g_focus_sound_handle[index].focus_fd < 0) {
1340 debug_error("Close fail : index(%d)", index);
1342 close(g_focus_sound_handle[index].focus_fd);
1343 debug_log("Close Success : index(%d)", index);
1346 if (is_for_watching) {
1347 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch",
1348 g_focus_sound_handle[index].focus_tid,
1349 g_focus_sound_handle[index].handle);
1351 filename = g_strdup_printf("/tmp/FOCUS.%d.%d",
1352 g_focus_sound_handle[index].focus_tid,
1353 g_focus_sound_handle[index].handle);
1355 if (remove(filename)) {
1356 debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)",
1362 #ifdef CONFIG_ENABLE_RETCB
1365 if (is_for_watching) {
1366 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr",
1367 g_focus_sound_handle[index].focus_tid,
1368 g_focus_sound_handle[index].handle);
1370 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr",
1371 g_focus_sound_handle[index].focus_tid,
1372 g_focus_sound_handle[index].handle);
1374 if (remove(filename2)) {
1375 debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)",
1386 static GSourceFuncs event_funcs = {
1387 .prepare = _focus_fd_prepare,
1388 .check = _focus_fd_check,
1389 .dispatch = _focus_fd_dispatch,
1390 .finalize = _focus_fd_finalize,
1393 static bool _focus_add_sound_callback(int index, focus_callback_handler_t focus_cb_handler)
1395 FocusSource *fsrc = NULL;
1396 GSource *src = NULL;
1401 g_mutex_init(&g_focus_sound_handle[index].focus_lock);
1403 src = g_source_new(&event_funcs, sizeof(FocusSource));
1405 debug_error("failed to g_source_new for focus source");
1409 fsrc = (FocusSource*) src;
1411 fsrc->poll_fd.fd = g_focus_sound_handle[index].focus_fd;
1412 fsrc->poll_fd.events = (gushort)(POLLIN | POLLPRI);
1413 g_source_add_poll(src, &fsrc->poll_fd);
1415 g_source_set_callback(src, focus_cb_handler, (gpointer)&g_focus_sound_handle[index], NULL);
1417 debug_warning("fsrc(%p), src_funcs(%p), pollfd(%p), fd(%d)",
1418 fsrc, &event_funcs, &fsrc->poll_fd, fsrc->poll_fd.fd);
1420 fsrc_id = g_source_attach(src, g_main_loop_get_context(g_focus_sound_handle[index].focus_loop));
1422 debug_error("failed to attach the source to context");
1425 g_source_unref(src);
1427 g_focus_sound_handle[index].fsrc = fsrc;
1434 g_source_unref(src);
1439 static bool _focus_remove_sound_callback(int index)
1441 focus_sound_info_t *h = NULL;
1445 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1446 debug_error("Invalid focus handle index [%d]", index);
1450 h = &g_focus_sound_handle[index];
1452 g_source_destroy((GSource *)h->fsrc);
1456 h->focus_callback = NULL;
1457 h->watch_callback = NULL;
1459 g_mutex_clear(&h->focus_lock);
1466 static void _focus_add_callback(int index, bool is_for_watching)
1470 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1471 debug_error("Invalid focus handle index [%d]", index);
1475 if (!is_for_watching) {
1476 if (!_focus_add_sound_callback(index, _focus_callback_handler))
1477 debug_error("failed to _focus_add_sound_callback(%p)", _focus_callback_handler);
1479 if (!_focus_add_sound_callback(index, _focus_watch_callback_handler))
1480 debug_error("failed to _focus_add_sound_callback(%p)", _focus_watch_callback_handler);
1485 static void _focus_remove_callback(int index)
1488 if (!_focus_remove_sound_callback(index))
1489 debug_error("failed to __focus_remove_sound_callback()");
1493 static void _focus_init_callback(int index, bool is_for_watching)
1496 _focus_open_callback(index, is_for_watching);
1497 _focus_add_callback(index, is_for_watching);
1501 static void _focus_deinit_callback(int index, bool is_for_watching)
1504 _focus_remove_callback(index);
1505 _focus_close_callback(index, is_for_watching);
1509 #define INTERVAL_MS 20
1510 static int _focus_loop_is_running_timed_wait(GMainLoop *focus_loop, int timeout_ms)
1512 int reduced_time_ms = timeout_ms;
1513 if (!focus_loop || timeout_ms < 0) {
1514 debug_error("invalid argument, focus_loop(%p), timeout_ms(%d)", focus_loop, timeout_ms);
1515 return MM_ERROR_INVALID_ARGUMENT;
1519 if (g_main_loop_is_running(focus_loop))
1520 return MM_ERROR_NONE;
1522 usleep(INTERVAL_MS * 1000);
1523 if (reduced_time_ms < timeout_ms)
1524 debug_warning("reduced_time_ms(%d)", reduced_time_ms);
1525 } while ((reduced_time_ms -= INTERVAL_MS) >= 0);
1527 debug_error("focus_loop is not running for timeout_ms(%d), focus_loop(%p) ", timeout_ms, focus_loop);
1529 return MM_ERROR_SOUND_INTERNAL;
1532 #define LOOP_RUNNING_WAIT_TIME_MS 5000
1533 static int _focus_init_context(int index)
1535 int ret = MM_ERROR_NONE;
1536 GMainContext *focus_context;
1540 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1541 debug_error("index(%d) is not valid", index);
1542 return MM_ERROR_INVALID_ARGUMENT;
1545 focus_context = g_main_context_new();
1546 g_focus_sound_handle[index].focus_loop = g_main_loop_new(focus_context, FALSE);
1547 g_main_context_unref(focus_context);
1548 if (g_focus_sound_handle[index].focus_loop == NULL) {
1549 debug_error("could not create mainloop..");
1553 g_focus_sound_handle[index].focus_cb_thread = g_thread_new("focus-cb-thread",
1555 g_focus_sound_handle[index].focus_loop);
1556 if (g_focus_sound_handle[index].focus_cb_thread == NULL) {
1557 debug_error("could not create thread..");
1561 debug_warning("focus cb thread[%p] with mainloop[%p] is created for index(%d)",
1562 g_focus_sound_handle[index].focus_cb_thread, g_focus_sound_handle[index].focus_loop, index);
1564 if ((ret = _focus_loop_is_running_timed_wait(g_focus_sound_handle[index].focus_loop, LOOP_RUNNING_WAIT_TIME_MS))) {
1565 debug_error("failed to _focus_loop_is_running_timed_wait(), ret[0x%x]", ret);
1571 return MM_ERROR_NONE;
1574 if (g_focus_sound_handle[index].focus_loop) {
1575 g_main_loop_unref(g_focus_sound_handle[index].focus_loop);
1576 g_focus_sound_handle[index].focus_loop = NULL;
1578 return MM_ERROR_SOUND_INTERNAL;
1581 static void _focus_deinit_context(int index)
1585 if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1586 debug_error("index(%d) is not valid", index);
1590 if (!g_focus_sound_handle[index].focus_loop || !g_focus_sound_handle[index].focus_cb_thread) {
1591 debug_error("focus_loop[%p] or focus_cb_thread[%p] is null",
1592 g_focus_sound_handle[index].focus_loop, g_focus_sound_handle[index].focus_cb_thread);
1596 g_main_loop_quit(g_focus_sound_handle[index].focus_loop);
1597 g_thread_join(g_focus_sound_handle[index].focus_cb_thread);
1598 debug_warning("after thread join, thread[%p], mainloop[%p] for index(%d)",
1599 g_focus_sound_handle[index].focus_cb_thread, g_focus_sound_handle[index].focus_loop, index);
1600 g_main_loop_unref(g_focus_sound_handle[index].focus_loop);
1601 g_focus_sound_handle[index].focus_loop = NULL;
1602 g_focus_sound_handle[index].focus_cb_thread = NULL;
1607 int mm_sound_client_get_unique_id(int *id)
1609 int ret = MM_ERROR_NONE;
1614 ret = MM_ERROR_INVALID_ARGUMENT;
1616 ret = mm_sound_proxy_get_unique_id(id);
1623 int mm_sound_client_is_focus_cb_thread(GThread *mine, bool *result)
1625 int ret = MM_ERROR_NONE;
1628 if (!mine || !result)
1629 ret = MM_ERROR_INVALID_ARGUMENT;
1632 for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1633 if (!g_focus_sound_handle[i].is_used)
1635 if (g_focus_sound_handle[i].focus_cb_thread == mine) {
1645 int mm_sound_client_register_focus(int id, int pid, const char *stream_type,
1646 mm_sound_focus_changed_cb callback, void* user_data)
1648 int ret = MM_ERROR_NONE;
1653 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1657 for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
1658 if (g_focus_sound_handle[index].is_used == false) {
1659 g_focus_sound_handle[index].is_used = true;
1664 g_focus_sound_handle[index].focus_tid = instance;
1665 g_focus_sound_handle[index].handle = id;
1666 g_focus_sound_handle[index].focus_callback = callback;
1667 g_focus_sound_handle[index].user_data = user_data;
1668 g_focus_sound_handle[index].auto_reacquire = true;
1670 ret = mm_sound_proxy_register_focus(id, pid, stream_type, callback, user_data);
1671 if (ret == MM_ERROR_NONE) {
1672 debug_msg("Success to register focus");
1673 g_need_emergent_exit = TRUE;
1674 if (_focus_init_context(index)) {
1675 ret = MM_ERROR_SOUND_INTERNAL;
1679 debug_error("Error occurred : 0x%x", ret);
1683 _focus_init_callback(index, false);
1687 g_focus_sound_handle[index].is_used = false;
1689 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1695 int mm_sound_client_unregister_focus(int id)
1697 int ret = MM_ERROR_NONE;
1702 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1704 index = _focus_find_index_by_handle(id);
1706 debug_error("Could not find index");
1707 ret = MM_ERROR_INVALID_ARGUMENT;
1710 instance = g_focus_sound_handle[index].focus_tid;
1712 if (!g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1713 debug_warning("maybe focus_callback is being called, try one more time..");
1714 usleep(2500000); /* 2.5 sec */
1715 if (g_mutex_trylock(&g_focus_sound_handle[index].focus_lock))
1716 debug_msg("finally got focus_lock");
1719 ret = mm_sound_proxy_unregister_focus(instance, id);
1720 if (ret == MM_ERROR_NONE)
1721 debug_msg("Success to unregister focus");
1723 debug_error("Error occurred : 0x%x", ret);
1725 g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
1727 _focus_deinit_callback(index, false);
1728 g_focus_sound_handle[index].focus_fd = 0;
1729 g_focus_sound_handle[index].focus_tid = 0;
1730 g_focus_sound_handle[index].handle = 0;
1731 g_focus_sound_handle[index].is_used = false;
1732 _focus_deinit_context(index);
1736 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1741 int mm_sound_client_set_focus_reacquisition(int id, bool reacquisition)
1743 int ret = MM_ERROR_NONE;
1750 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1752 index = _focus_find_index_by_handle(id);
1754 debug_error("Could not find index");
1755 ret = MM_ERROR_INVALID_ARGUMENT;
1758 instance = g_focus_sound_handle[index].focus_tid;
1760 ret = mm_sound_client_is_focus_cb_thread(g_thread_self(), &result);
1762 debug_error("mm_sound_client_is_focus_cb_thread failed");
1764 } else if (!result) {
1765 ret = mm_sound_proxy_set_focus_reacquisition(instance, id, reacquisition);
1766 if (ret == MM_ERROR_NONE) {
1767 debug_msg("Success to set focus reacquisition to [%d]", reacquisition);
1769 debug_error("Error occurred : 0x%x", ret);
1773 debug_warning("Inside the focus cb thread, set focus reacquisition to [%d]", reacquisition);
1776 g_focus_sound_handle[index].auto_reacquire = reacquisition;
1777 debug_msg("set focus reacquisition(%d) for id(%d)", reacquisition, id);
1780 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1785 int mm_sound_client_get_focus_reacquisition(int id, bool *reacquisition)
1787 int ret = MM_ERROR_NONE;
1792 if (!reacquisition) {
1793 debug_error("Invalid parameter");
1794 return MM_ERROR_INVALID_ARGUMENT;
1797 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1799 index = _focus_find_index_by_handle(id);
1801 debug_error("Could not find index");
1802 ret = MM_ERROR_INVALID_ARGUMENT;
1806 *reacquisition = g_focus_sound_handle[index].auto_reacquire;
1807 debug_msg("get focus reacquisition(%d) for id(%d)", *reacquisition, id);
1810 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1815 int mm_sound_client_get_acquired_focus_stream_type(int focus_type, char **stream_type, int *option, char **ext_info)
1817 int ret = MM_ERROR_NONE;
1818 char *stream_type_str = NULL;
1819 char *ext_info_str = NULL;
1823 ret = mm_sound_proxy_get_acquired_focus_stream_type(focus_type, &stream_type_str, option, &ext_info_str);
1824 if (ret == MM_ERROR_NONE) {
1825 debug_msg("Success to get stream type of acquired focus, stream_type(%s), ext_info(%s)",
1826 stream_type_str, ext_info_str);
1827 *stream_type = strdup(stream_type_str);
1828 *ext_info = strdup(ext_info_str);
1829 g_free(stream_type_str);
1830 g_free(ext_info_str);
1832 debug_error("Error occurred : 0x%x", ret);
1839 int mm_sound_client_acquire_focus(int id, mm_sound_focus_type_e type, int option, const char *ext_info)
1841 int ret = MM_ERROR_NONE;
1846 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1848 index = _focus_find_index_by_handle(id);
1850 debug_error("Could not find index");
1851 ret = MM_ERROR_INVALID_ARGUMENT;
1854 instance = g_focus_sound_handle[index].focus_tid;
1856 ret = mm_sound_proxy_acquire_focus(instance, id, type, option, ext_info);
1857 if (ret == MM_ERROR_NONE)
1858 debug_msg("Success to acquire focus");
1860 debug_error("Error occurred : 0x%x", ret);
1863 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1868 int mm_sound_client_release_focus(int id, mm_sound_focus_type_e type, int option, const char *ext_info)
1870 int ret = MM_ERROR_NONE;
1875 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1877 index = _focus_find_index_by_handle(id);
1879 debug_error("Could not find index");
1880 ret = MM_ERROR_INVALID_ARGUMENT;
1883 instance = g_focus_sound_handle[index].focus_tid;
1885 ret = mm_sound_proxy_release_focus(instance, id, type, option, ext_info);
1886 if (ret == MM_ERROR_NONE)
1887 debug_msg("Success to release focus");
1889 debug_error("Error occurred : 0x%x", ret);
1892 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1897 int mm_sound_client_update_stream_focus_status(int id, unsigned int status)
1899 int ret = MM_ERROR_NONE;
1902 if ((ret = mm_sound_proxy_update_stream_focus_status(id, status)) != MM_ERROR_NONE)
1903 debug_error("failed to update stream focus status, ret[0x%x]", ret);
1909 int mm_sound_client_deliver_focus(int pid, int src_id, int dst_id, mm_sound_focus_type_e focus_type)
1911 int ret = MM_ERROR_NONE;
1914 if ((ret = mm_sound_proxy_deliver_focus(pid, src_id, dst_id, focus_type)) != MM_ERROR_NONE)
1915 debug_error("failed to deliver focus, ret[0x%x]", ret);
1921 int mm_sound_client_set_focus_watch_callback(int pid, mm_sound_focus_type_e focus_type,
1922 mm_sound_focus_changed_watch_cb callback, void* user_data, int *id)
1924 int ret = MM_ERROR_NONE;
1931 return MM_ERROR_INVALID_ARGUMENT;
1935 ret = mm_sound_proxy_get_unique_id(id);
1939 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1941 for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
1942 if (g_focus_sound_handle[index].is_used == false) {
1943 g_focus_sound_handle[index].is_used = true;
1948 g_focus_sound_handle[index].focus_tid = instance;
1949 g_focus_sound_handle[index].handle = *id;
1950 g_focus_sound_handle[index].watch_callback = callback;
1951 g_focus_sound_handle[index].user_data = user_data;
1952 g_focus_sound_handle[index].unset_watch_callback_requested = false;
1954 ret = mm_sound_proxy_set_focus_watch_callback(pid, g_focus_sound_handle[index].handle, focus_type,
1955 callback, user_data);
1957 if (ret == MM_ERROR_NONE) {
1958 debug_msg("Success to watch focus");
1959 g_need_emergent_exit = TRUE;
1960 if (_focus_init_context(index)) {
1961 ret = MM_ERROR_SOUND_INTERNAL;
1965 debug_error("Error occurred : 0x%x", ret);
1969 _focus_init_callback(index, true);
1973 g_focus_sound_handle[index].is_used = false;
1975 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1980 int mm_sound_client_request_unset_focus_watch_callback(int id)
1982 int ret = MM_ERROR_NONE;
1986 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1988 index = _focus_watch_find_index_by_handle(id);
1990 debug_error("Could not find index");
1991 ret = MM_ERROR_INVALID_ARGUMENT;
1994 g_focus_sound_handle[index].unset_watch_callback_requested = true;
1997 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2002 int mm_sound_client_unset_focus_watch_callback(int id)
2004 int ret = MM_ERROR_NONE;
2008 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2010 index = _focus_watch_find_index_by_handle(id);
2012 debug_error("Could not find index");
2013 ret = MM_ERROR_INVALID_ARGUMENT;
2017 g_mutex_lock(&g_focus_sound_handle[index].focus_lock);
2019 g_focus_sound_handle[index].is_used = false;
2021 ret = mm_sound_proxy_unset_focus_watch_callback(g_focus_sound_handle[index].focus_tid,
2022 g_focus_sound_handle[index].handle);
2024 if (ret == MM_ERROR_NONE)
2025 debug_msg("Success to unwatch focus");
2027 debug_error("Error occurred : 0x%x", ret);
2030 g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
2032 _focus_deinit_callback(index, true);
2033 g_focus_sound_handle[index].focus_fd = 0;
2034 g_focus_sound_handle[index].focus_tid = 0;
2035 g_focus_sound_handle[index].handle = 0;
2036 _focus_deinit_context(index);
2039 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2044 static gboolean _idle_event_callback(void *data)
2046 focus_idle_event_t *idle_event_data = (focus_idle_event_t*)data;
2047 int ret = MM_ERROR_NONE;
2050 debug_error("data is null");
2054 debug_msg("idle_event_data(%p): type(%d), data(%d)",
2055 idle_event_data, idle_event_data->type, idle_event_data->data);
2057 switch (idle_event_data->type) {
2058 case IDLE_EVENT_TYPE_UNSET_FOCUS_WATCH_CB:
2059 if ((ret = mm_sound_client_unset_focus_watch_callback(idle_event_data->data)))
2060 debug_error("Could not unset focus watch callback, id(%d), ret = %x", idle_event_data->data, ret);
2062 case IDLE_EVENT_TYPE_UNREGISTER_FOCUS:
2063 if ((ret = mm_sound_client_unregister_focus(idle_event_data->data)))
2064 debug_error("Could not unregister focus, id(%d), ret = %x", idle_event_data->data, ret);
2067 debug_warning("invalid type(%d)", idle_event_data->type);
2071 g_free(idle_event_data);
2073 g_idle_event_src = 0;
2075 MMSOUND_LEAVE_CRITICAL_SECTION(&g_event_mutex);
2080 int mm_sound_client_execute_focus_func_in_main_context(focus_idle_event_type_e type, int data)
2082 focus_idle_event_t *idle_event_data = NULL;
2084 if (IDLE_EVENT_TYPE_MAX < type)
2085 return MM_ERROR_INVALID_ARGUMENT;
2087 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_event_mutex, MM_ERROR_SOUND_INTERNAL);
2089 idle_event_data = g_new0(focus_idle_event_t, 1);
2090 idle_event_data->type = type;
2091 idle_event_data->data = data;
2093 g_idle_event_src = g_idle_add_full(G_PRIORITY_HIGH,
2094 (GSourceFunc)_idle_event_callback,
2095 (gpointer)idle_event_data,
2098 return MM_ERROR_NONE;
2103 int mm_sound_client_add_test_callback(mm_sound_test_cb func, void* user_data, unsigned int *subs_id)
2105 int ret = MM_ERROR_NONE;
2109 ret = mm_sound_proxy_add_test_callback(func, user_data, g_free, subs_id);
2115 int mm_sound_client_remove_test_callback(unsigned int subs_id)
2117 int ret = MM_ERROR_NONE;
2120 ret = mm_sound_proxy_remove_test_callback(subs_id);
2127 int mm_sound_client_test(int a, int b, int* getv)
2129 int ret = MM_ERROR_NONE;
2133 ret = mm_sound_proxy_test(a, b, getv);
2134 debug_log("%d * %d -> result : %d", a, b, *getv);