+ ret = mm_sound_proxy_remove_volume_changed_callback(subs_id);
+
+ debug_fleave();
+ return ret;
+}
+
+#ifdef USE_FOCUS
+int mm_sound_client_set_session_interrupt_callback(mm_sound_focus_session_interrupt_cb callback, void* user_data)
+{
+ int ret = MM_ERROR_NONE;
+
+ debug_fenter();
+
+ if (!callback)
+ return MM_ERROR_INVALID_ARGUMENT;
+
+ g_focus_session_interrupt_info.user_cb = callback;
+ g_focus_session_interrupt_info.user_data = user_data;
+
+ debug_fleave();
+ return ret;
+}
+
+int mm_sound_client_unset_session_interrupt_callback(void)
+{
+ int ret = MM_ERROR_NONE;
+
+ debug_fenter();
+
+ if (!g_focus_session_interrupt_info.user_cb) {
+ debug_error("no callback to unset");
+ return MM_ERROR_SOUND_INTERNAL;
+ }
+
+ g_focus_session_interrupt_info.user_cb = NULL;
+ g_focus_session_interrupt_info.user_data = NULL;
+
+ debug_fleave();
+ return ret;
+}
+
+static gpointer _focus_thread_func(gpointer data)
+{
+ unsigned int thread_id = (unsigned int)pthread_self();
+ debug_warning(">>> thread func..ID of this thread(%u), mainloop(%p)", thread_id, g_focus_loop);
+ if (g_focus_loop)
+ g_main_loop_run(g_focus_loop);
+
+ debug_warning("<<< quit thread func..(%u), mainloop(%p)", thread_id, g_focus_loop);
+ return NULL;
+}
+
+static gboolean _focus_fd_check(GSource * source)
+{
+ GSList *fd_list;
+ GPollFD *temp;
+
+ if (!source) {
+ debug_error("GSource is null");
+ return FALSE;
+ }
+ fd_list = source->poll_fds;
+ if (!fd_list) {
+ debug_error("fd_list is null");
+ return FALSE;
+ }
+ do {
+ temp = (GPollFD*)fd_list->data;
+ if (!temp) {
+ debug_error("fd_list->data is null");
+ return FALSE;
+ }
+ if (temp->revents & (POLLIN | POLLPRI)) {
+ return TRUE;
+ }
+ fd_list = fd_list->next;
+ } while (fd_list);
+
+ return FALSE; /* there is no change in any fd state */
+}
+
+static gboolean _focus_fd_prepare(GSource *source, gint *timeout)
+{
+ return FALSE;
+}
+
+static gboolean _focus_fd_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
+{
+ callback(user_data);
+ return TRUE;
+}
+
+static int _focus_find_index_by_handle(int handle)
+{
+ int i = 0;
+ for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
+ if (g_focus_sound_handle[i].focus_callback && handle == g_focus_sound_handle[i].handle) {
+ /* debug_msg("found index(%d) for handle(%d)", i, handle);*/
+ return (handle == FOCUS_HANDLE_INIT_VAL)? -1 : i;
+ }
+ }
+ return -1;
+}
+
+static int _focus_watch_find_index_by_handle(int handle)
+{
+ int i = 0;
+ for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
+ if (g_focus_sound_handle[i].watch_callback && handle == g_focus_sound_handle[i].handle) {
+ /* debug_msg("found index(%d) for watch handle(%d)", i, handle);*/
+ return (handle == FOCUS_HANDLE_INIT_VAL)? -1 : i;
+ }
+ }
+ return -1;
+}
+
+static gboolean _focus_callback_handler(gpointer d)
+{
+ GPollFD *data = (GPollFD*)d;
+ int count;
+ int tid = 0;
+ int focus_index = 0;
+ focus_cb_data_lib cb_data;
+ debug_log(">>> focus_callback_handler()..ID of this thread(%u)\n", (unsigned int)pthread_self());
+
+ memset(&cb_data, 0, sizeof(focus_cb_data_lib));
+
+ if (!data) {
+ debug_error("GPollFd is null");
+ return FALSE;
+ }
+ if (data->revents & (POLLIN | POLLPRI)) {
+ int changed_state = -1;
+
+ count = read(data->fd, &cb_data, sizeof(cb_data));
+ if (count < 0){
+ char str_error[256];
+ strerror_r(errno, str_error, sizeof(str_error));
+ debug_error("GpollFD read fail, errno=%d(%s)",errno, str_error);
+ return FALSE;
+ }
+ changed_state = cb_data.state;
+ focus_index = _focus_find_index_by_handle(cb_data.handle);
+ if (focus_index == -1) {
+ debug_error("Could not find index");
+ return FALSE;
+ }
+
+ g_mutex_lock(&g_focus_sound_handle[focus_index].focus_lock);
+
+ tid = g_focus_sound_handle[focus_index].focus_tid;
+
+ if (changed_state != -1) {
+ 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);
+ if (g_focus_sound_handle[focus_index].focus_callback == NULL) {
+ debug_error("callback is null..");
+ g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
+ return FALSE;
+ }
+ debug_msg("[CALLBACK(%p) START]",g_focus_sound_handle[focus_index].focus_callback);
+ (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);
+ debug_msg("[CALLBACK END]");
+ if (g_focus_session_interrupt_info.user_cb) {
+ debug_msg("sending session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
+ (g_focus_session_interrupt_info.user_cb)(cb_data.state, cb_data.stream_type, false, g_focus_session_interrupt_info.user_data);
+ }
+ }
+#ifdef CONFIG_ENABLE_RETCB
+ {
+ int rett = 0;
+ int tmpfd = -1;
+ unsigned int buf = 0;
+ char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", g_focus_sound_handle[focus_index].focus_tid, cb_data.handle);
+ tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
+ if (tmpfd < 0) {
+ char str_error[256];
+ strerror_r(errno, str_error, sizeof(str_error));
+ debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n", tid, tmpfd, filename2, errno, str_error);
+ g_free(filename2);
+ g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
+ return FALSE;
+ }
+ /* buf contains data as below,
+ * |<--12bits--><--4bits (reacquisition)--><--16bits (handle)-->| */
+ buf = (unsigned int)((0x0000ffff & cb_data.handle) | (g_focus_sound_handle[focus_index].auto_reacquire << 16));
+ rett = write(tmpfd, &buf, sizeof(buf));
+ close(tmpfd);
+ g_free(filename2);
+ debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
+ }
+#endif
+ }
+
+ g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
+
+ return TRUE;
+}
+
+static gboolean _focus_watch_callback_handler(gpointer d)
+{
+ GPollFD *data = (GPollFD*)d;
+ int count;
+ int tid = 0;
+ int focus_index = 0;
+ focus_cb_data_lib cb_data;
+
+ debug_fenter();
+
+ memset(&cb_data, 0, sizeof(focus_cb_data_lib));
+
+ if (!data) {
+ debug_error("GPollFd is null");
+ return FALSE;
+ }
+ if (data->revents & (POLLIN | POLLPRI)) {
+ count = read(data->fd, &cb_data, sizeof(cb_data));
+ if (count < 0){
+ char str_error[256];
+ strerror_r(errno, str_error, sizeof(str_error));
+ debug_error("GpollFD read fail, errno=%d(%s)",errno, str_error);
+ return FALSE;
+ }
+
+ focus_index = _focus_watch_find_index_by_handle(cb_data.handle);
+ if (focus_index == -1) {
+ debug_error("Could not find index");
+ return FALSE;
+ }
+
+ if (!g_focus_sound_handle[focus_index].is_used) {
+ debug_warning("unsetting watch calllback has been already requested");
+ goto SKIP_CB_AND_RET;
+ }
+
+ debug_msg("lock focus_lock = %p", &g_focus_sound_handle[focus_index].focus_lock);
+ g_mutex_lock(&g_focus_sound_handle[focus_index].focus_lock);
+
+ tid = g_focus_sound_handle[focus_index].focus_tid;
+
+ 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);
+
+ if (g_focus_sound_handle[focus_index].watch_callback == NULL) {
+ debug_msg("callback is null..");
+ } else {
+ debug_msg("[CALLBACK(%p) START]",g_focus_sound_handle[focus_index].watch_callback);
+ (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);
+ debug_msg("[CALLBACK END]");
+ if (g_focus_session_interrupt_info.user_cb) {
+ debug_msg("sending session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
+ (g_focus_session_interrupt_info.user_cb)(cb_data.state, cb_data.stream_type, true, g_focus_session_interrupt_info.user_data);
+ }
+ }
+
+SKIP_CB_AND_RET:
+#ifdef CONFIG_ENABLE_RETCB
+ {
+ int rett = 0;
+ int tmpfd = -1;
+ int buf = -1;
+ char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr", g_focus_sound_handle[focus_index].focus_tid, cb_data.handle);
+ tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
+ if (tmpfd < 0) {
+ char str_error[256];
+ strerror_r(errno, str_error, sizeof(str_error));
+ debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n", tid, tmpfd, filename2, errno, str_error);
+ g_free(filename2);
+ g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
+ return FALSE;
+ }
+ buf = cb_data.handle;
+ rett = write(tmpfd, &buf, sizeof(buf));
+ close(tmpfd);
+ g_free(filename2);
+ debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
+ }
+#endif
+ }
+
+ if (g_focus_sound_handle[focus_index].is_used) {
+ debug_msg("unlock focus_lock = %p", &g_focus_sound_handle[focus_index].focus_lock);
+ g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
+ }
+
+ debug_fleave();
+
+ return TRUE;
+}
+
+static void _focus_open_callback(int index, bool is_for_watching)
+{
+ mode_t pre_mask;
+ char *filename;
+
+ debug_fenter();
+
+ if (is_for_watching) {
+ filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
+ } else {
+ filename = g_strdup_printf("/tmp/FOCUS.%d.%d", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
+ }
+ pre_mask = umask(0);
+ if (mknod(filename, S_IFIFO|0666, 0)) {
+ debug_error("mknod() failure, errno(%d)", errno);
+ }
+ umask(pre_mask);
+ g_focus_sound_handle[index].focus_fd = open(filename, O_RDWR|O_NONBLOCK);
+ if (g_focus_sound_handle[index].focus_fd == -1) {
+ debug_error("Open fail : index(%d), file open error(%d)", index, errno);
+ } else {
+ debug_log("Open success : index(%d), filename(%s), fd(%d)", index, filename, g_focus_sound_handle[index].focus_fd);
+ }
+ g_free(filename);
+ filename = NULL;
+
+#ifdef CONFIG_ENABLE_RETCB
+ char *filename2;
+
+ if (is_for_watching) {
+ filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
+ } else {
+ filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
+ }
+ pre_mask = umask(0);
+ if (mknod(filename2, S_IFIFO | 0666, 0)) {
+ debug_error("mknod() failure, errno(%d)", errno);
+ }
+ umask(pre_mask);
+ g_free(filename2);
+ filename2 = NULL;
+#endif
+ debug_fleave();
+
+}
+
+void _focus_close_callback(int index, bool is_for_watching)
+{
+ char *filename = NULL;
+
+ debug_fenter();
+
+ if (g_focus_sound_handle[index].focus_fd < 0) {
+ debug_error("Close fail : index(%d)", index);
+ } else {
+ close(g_focus_sound_handle[index].focus_fd);
+ debug_log("Close Success : index(%d)", index);
+ }
+
+ if (is_for_watching) {
+ filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
+ } else {
+ filename = g_strdup_printf("/tmp/FOCUS.%d.%d", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
+ }
+ if (remove(filename)) {
+ debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)", filename, errno);
+ }
+ g_free(filename);
+ filename = NULL;
+
+#ifdef CONFIG_ENABLE_RETCB
+ char *filename2;
+
+ if (is_for_watching) {
+ filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
+ } else {
+ filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
+ }
+ if (remove(filename2)) {
+ debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)", filename2, errno);
+ }
+ g_free(filename2);
+ filename2 = NULL;
+
+ debug_fleave();
+#endif
+
+}
+
+static bool _focus_add_sound_callback(int index, int fd, gushort events, focus_gLoopPollHandler_t p_gloop_poll_handler )
+{
+ GSource* g_src = NULL;
+ GSourceFuncs *g_src_funcs = NULL; /* handler function */
+ guint g_src_id = 0;
+ GPollFD *g_poll_fd = NULL; /* file descriptor */
+
+ debug_fenter();
+
+ g_mutex_init(&g_focus_sound_handle[index].focus_lock);
+
+ /* 1. make GSource Object */
+ g_src_funcs = (GSourceFuncs *)g_malloc(sizeof(GSourceFuncs));
+ if (!g_src_funcs) {
+ debug_error("failed to g_malloc for g_src_funcs");
+ goto ERROR;
+ }
+
+ g_src_funcs->prepare = _focus_fd_prepare;
+ g_src_funcs->check = _focus_fd_check;
+ g_src_funcs->dispatch = _focus_fd_dispatch;
+ g_src_funcs->finalize = NULL;
+ g_src = g_source_new(g_src_funcs, sizeof(GSource));
+ if (!g_src) {
+ debug_error("failed to g_source_new for g_src");
+ goto ERROR;
+ }
+
+ /* 2. add file description which used in g_loop() */
+ g_poll_fd = (GPollFD *)g_malloc(sizeof(GPollFD));
+ if (!g_poll_fd) {
+ debug_error("failed to g_malloc for g_poll_fd");
+ goto ERROR;
+ }
+ g_poll_fd->fd = fd;
+ g_poll_fd->events = events;
+
+ /* 3. combine g_source object and file descriptor */
+ g_source_add_poll(g_src, g_poll_fd);
+ g_src_id = g_source_attach(g_src, g_main_loop_get_context(g_focus_loop));
+ if (!g_src_id) {
+ debug_error("failed to attach the source to context");
+ goto ERROR;
+ }
+
+ /* 4. set callback */
+ g_source_set_callback(g_src, p_gloop_poll_handler,(gpointer)g_poll_fd, NULL);
+
+ debug_log("g_malloc : g_src_funcs(%p), g_poll_fd(%p)", g_src_funcs, g_poll_fd);
+
+ /* 5. store to global handle */
+ g_focus_sound_handle[index].focus_src = g_src;
+ g_focus_sound_handle[index].g_src_funcs = g_src_funcs;
+ g_focus_sound_handle[index].g_poll_fd = g_poll_fd;
+
+ debug_fleave();
+ return true;
+
+ERROR:
+ if (g_src_funcs)
+ g_free(g_src_funcs);
+ if (g_poll_fd)
+ g_free(g_poll_fd);
+ if (g_src)
+ g_source_unref(g_src);
+
+ return false;
+}
+
+static bool _focus_remove_sound_callback(int index, gushort events)
+{
+ bool ret = true;
+
+ debug_fenter();
+
+ g_mutex_clear(&g_focus_sound_handle[index].focus_lock);
+
+ GSourceFuncs *g_src_funcs = g_focus_sound_handle[index].g_src_funcs;
+ GPollFD *g_poll_fd = g_focus_sound_handle[index].g_poll_fd; /* store file descriptor */
+ if (!g_poll_fd) {
+ debug_error("g_poll_fd is null..");
+ ret = false;
+ goto RELEASE;
+ }
+ g_poll_fd->fd = g_focus_sound_handle[index].focus_fd;
+ g_poll_fd->events = events;
+
+ if (!g_focus_sound_handle[index].focus_src) {
+ debug_error("g_focus_sound_handle[%d].focus_src is null..", index);
+ ret = false;
+ goto RELEASE;
+ }
+ g_source_remove_poll(g_focus_sound_handle[index].focus_src, g_poll_fd);
+ debug_log("g_source_remove_poll : fd(%d), event(%x)", g_poll_fd->fd, g_poll_fd->events);
+
+RELEASE:
+ if (g_focus_sound_handle[index].focus_src)
+ g_source_destroy(g_focus_sound_handle[index].focus_src);
+
+ debug_log("g_free : g_src_funcs(%x), g_poll_fd(%x)", g_src_funcs, g_poll_fd);
+ if (g_src_funcs) {
+ g_free(g_src_funcs);
+ g_src_funcs = NULL;
+ }
+ if (g_poll_fd) {
+ g_free(g_poll_fd);
+ g_poll_fd = NULL;
+ }
+
+ g_focus_sound_handle[index].g_src_funcs = NULL;
+ g_focus_sound_handle[index].g_poll_fd = NULL;
+ g_focus_sound_handle[index].focus_src = NULL;
+ g_focus_sound_handle[index].focus_callback = NULL;
+ g_focus_sound_handle[index].watch_callback = NULL;
+
+ debug_fleave();
+ return ret;
+}
+
+
+static void _focus_add_callback(int index, bool is_for_watching)
+{
+ debug_fenter();
+ if (!is_for_watching) {
+ if (!_focus_add_sound_callback(index, g_focus_sound_handle[index].focus_fd, (gushort)POLLIN | POLLPRI, _focus_callback_handler)) {
+ debug_error("failed to _focus_add_sound_callback()");
+ //return false;
+ }
+ } else { // need to check if it's necessary
+ if (!_focus_add_sound_callback(index, g_focus_sound_handle[index].focus_fd, (gushort)POLLIN | POLLPRI, _focus_watch_callback_handler)) {
+ debug_error("failed to _focus_add_sound_callback()");
+ //return false;
+ }
+ }
+ debug_fleave();
+}
+
+static void _focus_remove_callback(int index)
+{
+ debug_fenter();
+ if (!_focus_remove_sound_callback(index, (gushort)POLLIN | POLLPRI)) {
+ debug_error("failed to __focus_remove_sound_callback()");
+ //return false;
+ }
+ debug_fleave();
+}
+
+static void _focus_init_callback(int index, bool is_for_watching)
+{
+ debug_fenter();
+ _focus_open_callback(index, is_for_watching);
+ _focus_add_callback(index, is_for_watching);
+ debug_fleave();
+}
+
+static void _focus_destroy_callback(int index, bool is_for_watching)
+{
+ debug_fenter();
+ _focus_remove_callback(index);
+ _focus_close_callback(index, is_for_watching);
+ debug_fleave();
+}
+
+int mm_sound_client_get_unique_id(int *id)
+{
+ int ret = MM_ERROR_NONE;
+
+ debug_fenter();
+
+ if (!id)
+ ret = MM_ERROR_INVALID_ARGUMENT;
+ else
+ ret = mm_sound_proxy_get_unique_id(id);
+
+ debug_fleave();
+
+ return ret;
+}
+
+int mm_sound_client_is_focus_cb_thread(GThread *mine, bool *result)
+{
+ int ret = MM_ERROR_NONE;
+
+ if (!mine || !result)
+ ret = MM_ERROR_INVALID_ARGUMENT;
+ else {
+ if (mine == g_focus_thread)
+ *result = true;
+ else
+ *result = false;
+ }
+
+ return ret;
+}
+
+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)
+{
+ int ret = MM_ERROR_NONE;
+ int instance;
+ int index = 0;
+
+ debug_fenter();
+ MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
+
+ instance = pid;
+
+ for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
+ if (g_focus_sound_handle[index].is_used == false) {
+ g_focus_sound_handle[index].is_used = true;
+ break;
+ }
+ }
+
+ g_focus_sound_handle[index].focus_tid = instance;
+ g_focus_sound_handle[index].handle = id;
+ g_focus_sound_handle[index].focus_callback = callback;
+ g_focus_sound_handle[index].user_data = user_data;
+ g_focus_sound_handle[index].is_for_session = is_for_session;
+ g_focus_sound_handle[index].auto_reacquire = true;
+
+ ret = mm_sound_proxy_register_focus(id, pid, stream_type, callback, is_for_session, user_data);
+
+ if (ret == MM_ERROR_NONE) {
+ debug_msg("[Client] Success to register focus\n");
+ g_need_emergent_exit = TRUE;
+ if (!g_focus_thread) {
+ GMainContext* focus_context = g_main_context_new ();
+ g_focus_loop = g_main_loop_new (focus_context, FALSE);
+ g_main_context_unref(focus_context);
+ if (g_focus_loop == NULL) {
+ debug_error("could not create mainloop..");
+ ret = MM_ERROR_SOUND_INTERNAL;
+ goto cleanup;
+ }
+
+ g_focus_thread = g_thread_new("focus-cb-thread", _focus_thread_func, NULL);
+ if (g_focus_thread == NULL) {
+ debug_error("could not create thread..");
+ g_main_loop_unref(g_focus_loop);
+ ret = MM_ERROR_SOUND_INTERNAL;
+ goto cleanup;
+ }
+ } else {
+ debug_warning("focus thread(%p) with mainloop(%p) exists, skip thread creation",
+ g_focus_thread, g_focus_loop);
+ }
+ } else {
+ debug_error("[Client] Error occurred : 0x%x \n",ret);
+ goto cleanup;
+ }
+
+ _focus_init_callback(index, false);
+
+cleanup:
+
+ if (ret) {
+ g_focus_sound_handle[index].is_used = false;
+ }
+
+ MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
+ debug_fleave();
+
+ return ret;
+}
+
+int mm_sound_client_unregister_focus(int id)
+{
+ int ret = MM_ERROR_NONE;
+ int instance;
+ int index = -1;
+
+ debug_fenter();
+ MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
+
+ index = _focus_find_index_by_handle(id);
+ if (index == -1) {
+ debug_error("Could not find index");
+ ret = MM_ERROR_INVALID_ARGUMENT;
+ goto cleanup;
+ }
+ instance = g_focus_sound_handle[index].focus_tid;
+
+ if (!g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
+ debug_warning("maybe focus_callback is being called, try one more time..");
+ usleep(2500000); // 2.5 sec
+ if (g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
+ debug_msg("finally got focus_lock");
+ }
+ }
+
+ ret = mm_sound_proxy_unregister_focus(instance, id, g_focus_sound_handle[index].is_for_session);
+
+ if (ret == MM_ERROR_NONE)
+ debug_msg("[Client] Success to unregister focus\n");
+ else
+ debug_error("[Client] Error occurred : 0x%x \n",ret);
+
+ g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
+
+ _focus_destroy_callback(index, false);
+ g_focus_sound_handle[index].focus_fd = 0;
+ g_focus_sound_handle[index].focus_tid = 0;
+ g_focus_sound_handle[index].handle = 0;
+ g_focus_sound_handle[index].is_used = false;
+cleanup:
+ MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
+ debug_fleave();
+ return ret;
+}
+
+int mm_sound_client_set_focus_reacquisition(int id, bool reacquisition)
+{
+ int ret = MM_ERROR_NONE;
+ int instance;
+ int index = -1;
+ bool result;
+
+ debug_fenter();
+ MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
+
+ index = _focus_find_index_by_handle(id);
+ if (index == -1) {
+ debug_error("Could not find index");
+ ret = MM_ERROR_INVALID_ARGUMENT;
+ goto cleanup;
+ }
+ instance = g_focus_sound_handle[index].focus_tid;
+
+ ret = mm_sound_client_is_focus_cb_thread(g_thread_self(), &result);
+ if (ret) {
+ debug_error("[Client] mm_sound_client_is_focus_cb_thread failed");
+ goto cleanup;
+ } else if (!result) {
+ ret = mm_sound_proxy_set_foucs_reacquisition(instance, id, reacquisition);
+ if (ret == MM_ERROR_NONE) {
+ debug_msg("[Client] Success to set focus reacquisition to [%d]\n", reacquisition);
+ } else {
+ debug_error("[Client] Error occurred : 0x%x \n",ret);
+ goto cleanup;
+ }
+ } else {
+ debug_warning("[Client] Inside the focus cb thread, set focus reacquisition to [%d]\n", reacquisition);
+ }
+
+ g_focus_sound_handle[index].auto_reacquire = reacquisition;
+
+cleanup:
+ MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
+ debug_fleave();
+ return ret;
+}
+
+int mm_sound_client_get_focus_reacquisition(int id, bool *reacquisition)
+{
+ int ret = MM_ERROR_NONE;
+ int index = -1;
+
+ debug_fenter();
+
+ if (!reacquisition) {
+ debug_error("Invalid parameter");
+ return MM_ERROR_INVALID_ARGUMENT;
+ }
+
+ MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
+
+ index = _focus_find_index_by_handle(id);
+ if (index == -1) {
+ debug_error("Could not find index");
+ ret = MM_ERROR_INVALID_ARGUMENT;
+ goto cleanup;
+ }
+
+ *reacquisition = g_focus_sound_handle[index].auto_reacquire;
+
+cleanup:
+ MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
+ debug_fleave();
+ return ret;
+}
+
+int mm_sound_client_get_acquired_focus_stream_type(int focus_type, char **stream_type, int *option, char **ext_info)
+{
+ int ret = MM_ERROR_NONE;
+ char *ext_str = NULL;
+
+ debug_fenter();
+
+ ret = mm_sound_proxy_get_acquired_focus_stream_type(focus_type, stream_type, option, &ext_str);
+ if (ret == MM_ERROR_NONE) {
+ debug_msg("[Client] Success to get stream type of acquired focus, stream_type(%s), ext_info(%s)\n", *stream_type, ext_str);
+ *ext_info = strdup(ext_str);
+ g_free(ext_str);
+ } else {
+ debug_error("[Client] Error occurred : 0x%x \n",ret);
+ }
+
+ debug_fleave();
+ return ret;
+}
+
+int mm_sound_client_acquire_focus(int id, mm_sound_focus_type_e type, int option, const char *ext_info)
+{
+ int ret = MM_ERROR_NONE;
+ int instance;
+ int index = -1;
+
+ debug_fenter();
+ MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
+
+ index = _focus_find_index_by_handle(id);
+ if (index == -1) {
+ debug_error("Could not find index");
+ ret = MM_ERROR_INVALID_ARGUMENT;
+ goto cleanup;
+ }
+ instance = g_focus_sound_handle[index].focus_tid;
+
+ ret = mm_sound_proxy_acquire_focus(instance, id, type, option, ext_info, g_focus_sound_handle[index].is_for_session);
+ if (ret == MM_ERROR_NONE)
+ debug_msg("[Client] Success to acquire focus\n");
+ else
+ debug_error("[Client] Error occurred : 0x%x \n",ret);
+
+cleanup:
+ MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
+ debug_fleave();
+ return ret;
+}
+
+int mm_sound_client_release_focus(int id, mm_sound_focus_type_e type, int option, const char *ext_info)
+{
+ int ret = MM_ERROR_NONE;
+ int instance;
+ int index = -1;
+
+ debug_fenter();
+ MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
+
+ index = _focus_find_index_by_handle(id);
+ if (index == -1) {
+ debug_error("Could not find index");
+ ret = MM_ERROR_INVALID_ARGUMENT;
+ goto cleanup;
+ }
+ instance = g_focus_sound_handle[index].focus_tid;
+
+ ret = mm_sound_proxy_release_focus(instance, id, type, option, ext_info, g_focus_sound_handle[index].is_for_session);
+ if (ret == MM_ERROR_NONE)
+ debug_msg("[Client] Success to release focus\n");
+ else
+ debug_error("[Client] Error occurred : 0x%x \n",ret);
+
+cleanup:
+ MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
+ debug_fleave();
+ return ret;
+}
+
+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)
+{
+ int ret = MM_ERROR_NONE;
+ int instance;
+ int index = 0;
+
+ debug_fenter();
+
+ if (!id)
+ return MM_ERROR_INVALID_ARGUMENT;
+
+ //pthread_mutex_lock(&g_thread_mutex2);
+
+ instance = pid;
+
+ ret = mm_sound_proxy_get_unique_id(id);
+ if (ret)
+ return ret;
+
+ MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
+
+ for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
+ if (g_focus_sound_handle[index].is_used == false) {
+ g_focus_sound_handle[index].is_used = true;
+ break;
+ }
+ }
+
+ g_focus_sound_handle[index].focus_tid = instance;
+ g_focus_sound_handle[index].handle = *id;
+ g_focus_sound_handle[index].watch_callback = callback;
+ g_focus_sound_handle[index].user_data = user_data;
+ g_focus_sound_handle[index].is_for_session = is_for_session;
+
+ ret = mm_sound_proxy_set_focus_watch_callback(pid, g_focus_sound_handle[index].handle, focus_type, callback, is_for_session, user_data);
+
+ if (ret == MM_ERROR_NONE) {
+ debug_msg("[Client] Success to watch focus");
+ g_need_emergent_exit = TRUE;
+ if (!g_focus_thread) {
+ GMainContext* focus_context = g_main_context_new ();
+ g_focus_loop = g_main_loop_new (focus_context, FALSE);
+ g_main_context_unref(focus_context);
+ if (g_focus_loop == NULL) {
+ debug_error("could not create mainloop..");
+ ret = MM_ERROR_SOUND_INTERNAL;
+ goto cleanup;
+ }
+
+ g_focus_thread = g_thread_new("focus-cb-thread", _focus_thread_func, NULL);
+ if (g_focus_thread == NULL) {
+ debug_error ("could not create thread..");
+ g_main_loop_unref(g_focus_loop);
+ ret = MM_ERROR_SOUND_INTERNAL;
+ goto cleanup;
+ }
+ } else {
+ debug_warning("focus thread(%p) with mainloop(%p) exists, skip thread creation",
+ g_focus_thread, g_focus_loop);
+ }
+ } else {
+ debug_error("[Client] Error occurred : 0x%x",ret);
+ goto cleanup;
+ }
+
+ _focus_init_callback(index, true);
+
+cleanup:
+
+ if (ret) {
+ g_focus_sound_handle[index].is_used = false;
+ }
+
+ MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
+ debug_fleave();
+ return ret;
+}
+
+int mm_sound_client_unset_focus_watch_callback(int id)
+{
+ int ret = MM_ERROR_NONE;
+ int index = -1;
+ debug_fenter();
+ MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
+
+ index = _focus_watch_find_index_by_handle(id);
+ if (index == -1) {
+ debug_error("Could not find index");
+ ret = MM_ERROR_INVALID_ARGUMENT;
+ goto cleanup;
+ }
+
+ g_mutex_lock(&g_focus_sound_handle[index].focus_lock);
+
+ g_focus_sound_handle[index].is_used = false;
+
+ 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);
+
+ if (ret == MM_ERROR_NONE)
+ debug_msg("[Client] Success to unwatch focus\n");
+ else
+ debug_error("[Client] Error occurred : 0x%x \n",ret);
+
+
+ g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);