}
+static void __recorder_add_msg_to_queue(recorder_cb_info_s *cb_info, int api, int event, int event_class, char *msg)
+{
+ recorder_message_s *rec_msg = NULL;
+
+ if (!cb_info || !msg) {
+ LOGE("NULL pointer %p %p", cb_info, msg);
+ return;
+ }
+
+ rec_msg = g_new0(recorder_message_s, 1);
+ if (!rec_msg) {
+ LOGE("failed to alloc rec_msg for [%s]", msg);
+ return;
+ }
+
+ rec_msg->api = api;
+ rec_msg->event = event;
+ rec_msg->event_class = event_class;
+
+ strncpy(rec_msg->recv_msg, msg, sizeof(rec_msg->recv_msg) - 1);
+
+ /*LOGD("add recorder message to queue : api %d, event %d, event_class %d", api, event, event_class);*/
+
+ if (event == MUSE_RECORDER_EVENT_TYPE_AUDIO_STREAM) {
+ g_mutex_lock(&cb_info->audio_stream_cb_info.mutex);
+ g_queue_push_tail(cb_info->audio_stream_cb_info.queue, (gpointer)rec_msg);
+ g_cond_signal(&cb_info->audio_stream_cb_info.cond);
+ g_mutex_unlock(&cb_info->audio_stream_cb_info.mutex);
+ } else {
+ g_mutex_lock(&cb_info->msg_handler_info.mutex);
+ g_queue_push_tail(cb_info->msg_handler_info.queue, (gpointer)rec_msg);
+ g_cond_signal(&cb_info->msg_handler_info.cond);
+ g_mutex_unlock(&cb_info->msg_handler_info.mutex);
+ }
+
+ rec_msg = NULL;
+
+ return;
+}
+
+
static void *_recorder_msg_handler_func(gpointer data)
{
- int ret = 0;
int api = 0;
- int event = 0;
- int event_class = 0;
+ int type = 0;
recorder_message_s *rec_msg = NULL;
recorder_idle_event_s *rec_idle_event = NULL;
- recorder_cb_info_s *cb_info = (recorder_cb_info_s *)data;
+ recorder_msg_handler_info_s *handler_info = (recorder_msg_handler_info_s *)data;
+ recorder_cb_info_s *cb_info = NULL;
- if (cb_info == NULL) {
- LOGE("cb_info NULL");
+ if (!handler_info || !handler_info->cb_info) {
+ LOGE("NULL handler %p", handler_info);
return NULL;
}
- LOGD("start");
+ cb_info = (recorder_cb_info_s *)handler_info->cb_info;
+ type = handler_info->type;
- g_mutex_lock(&cb_info->msg_handler_mutex);
+ LOGD("t:%d start", type);
- while (g_atomic_int_get(&cb_info->msg_handler_running)) {
- if (g_queue_is_empty(cb_info->msg_queue)) {
+ g_mutex_lock(&handler_info->mutex);
+
+ while (g_atomic_int_get(&handler_info->running)) {
+ if (g_queue_is_empty(handler_info->queue)) {
/*LOGD("signal wait...");*/
- g_cond_wait(&cb_info->msg_handler_cond, &cb_info->msg_handler_mutex);
+ g_cond_wait(&handler_info->cond, &handler_info->mutex);
/*LOGD("signal received");*/
- if (g_atomic_int_get(&cb_info->msg_handler_running) == 0) {
+ if (g_atomic_int_get(&handler_info->running) == 0) {
LOGD("stop event thread");
break;
}
}
- rec_msg = (recorder_message_s *)g_queue_pop_head(cb_info->msg_queue);
- g_mutex_unlock(&cb_info->msg_handler_mutex);
+ rec_msg = (recorder_message_s *)g_queue_pop_head(handler_info->queue);
+ g_mutex_unlock(&handler_info->mutex);
if (rec_msg == NULL) {
LOGE("NULL message");
- g_mutex_lock(&cb_info->msg_handler_mutex);
+ g_mutex_lock(&handler_info->mutex);
continue;
}
+
api = rec_msg->api;
+
if (api < MUSE_RECORDER_API_MAX) {
+ int ret = 0;
+
g_mutex_lock(&cb_info->api_mutex[api]);
+
if (muse_recorder_msg_get(ret, rec_msg->recv_msg)) {
cb_info->api_ret[api] = ret;
cb_info->api_activating[api] = 1;
g_cond_signal(&cb_info->api_cond[api]);
} else {
- LOGE("failed to get ret for api %d, msg %s", rec_msg->api, rec_msg->recv_msg);
+ LOGE("t:%d failed to get ret for api %d, msg %s", type, api, rec_msg->recv_msg);
}
g_mutex_unlock(&cb_info->api_mutex[api]);
} else if (api == MUSE_RECORDER_CB_EVENT) {
- event = -1;
- event_class = -1;
-
- if (!muse_recorder_msg_get(event, rec_msg->recv_msg) ||
- !muse_recorder_msg_get(event_class, rec_msg->recv_msg)) {
- LOGE("get failed : event %d, class %d", event, event_class);
-
- g_free(rec_msg);
- rec_msg = NULL;
-
- g_mutex_lock(&cb_info->msg_handler_mutex);
- continue;
- }
-
- switch (event_class) {
+ switch (rec_msg->event_class) {
case MUSE_RECORDER_EVENT_CLASS_THREAD_SUB:
- _recorder_client_user_callback(cb_info, rec_msg->recv_msg, event);
+ _recorder_client_user_callback(cb_info, rec_msg->recv_msg, rec_msg->event);
break;
case MUSE_RECORDER_EVENT_CLASS_THREAD_MAIN:
rec_idle_event = g_new0(recorder_idle_event_s, 1);
break;
}
- rec_idle_event->event = event;
+ rec_idle_event->event = rec_msg->event;
rec_idle_event->cb_info = cb_info;
g_mutex_init(&rec_idle_event->event_mutex);
- memcpy(rec_idle_event->recv_msg, rec_msg->recv_msg, sizeof(rec_idle_event->recv_msg));
+ strncpy(rec_idle_event->recv_msg, rec_msg->recv_msg, sizeof(rec_idle_event->recv_msg) - 1);
- /*LOGD("add recorder event[%d, %p] to IDLE", event, rec_idle_event);*/
+ /*LOGD("add recorder event[%d, %p] to IDLE", rec_msg->event, rec_idle_event);*/
g_mutex_lock(&cb_info->idle_event_mutex);
cb_info->idle_event_list = g_list_append(cb_info->idle_event_list, (gpointer)rec_idle_event);
NULL);
break;
default:
- LOGE("unknown event class %d", event_class);
+ LOGE("unknown event class %d", rec_msg->event_class);
break;
}
} else {
g_free(rec_msg);
rec_msg = NULL;
- g_mutex_lock(&cb_info->msg_handler_mutex);
+ g_mutex_lock(&handler_info->mutex);
}
/* remove remained event */
- while (!g_queue_is_empty(cb_info->msg_queue)) {
- rec_msg = (recorder_message_s *)g_queue_pop_head(cb_info->msg_queue);
+ while (!g_queue_is_empty(handler_info->queue)) {
+ rec_msg = (recorder_message_s *)g_queue_pop_head(handler_info->queue);
if (rec_msg) {
LOGD("remove message %p", rec_msg);
free(rec_msg);
}
}
- g_mutex_unlock(&cb_info->msg_handler_mutex);
+ g_mutex_unlock(&handler_info->mutex);
LOGD("return");
int ret = 0;
int api = 0;
int api_class = 0;
+ int event = 0;
+ int event_class = 0;
int num_token = 0;
int str_pos = 0;
int prev_pos = 0;
char *error_msg = NULL;
char **parse_str = NULL;
recorder_cb_info_s *cb_info = (recorder_cb_info_s *)data;
- recorder_message_s *rec_msg = NULL;
if (cb_info == NULL) {
LOGE("cb_info NULL");
continue;
}
- if (api != MUSE_RECORDER_CB_EVENT) {
+ if (api == MUSE_RECORDER_CB_EVENT) {
+ if (!muse_recorder_msg_get(event, parse_str[i]) ||
+ !muse_recorder_msg_get(event_class, parse_str[i])) {
+ LOGE("failed to get event or event_class [%s]", parse_str[i]);
+ continue;
+ }
+ } else {
if (!muse_recorder_msg_get(api_class, parse_str[i])) {
- LOGE("failed to get recorder api_class");
+ LOGE("failed to get api_class [%s]", parse_str[i]);
continue;
}
}
g_cond_signal(&cb_info->api_cond[api]);
g_mutex_unlock(&cb_info->api_mutex[api]);
- } else if (api_class == MUSE_RECORDER_API_CLASS_THREAD_SUB ||
- api == MUSE_RECORDER_CB_EVENT) {
- rec_msg = g_new0(recorder_message_s, 1);
- if (rec_msg == NULL) {
- LOGE("failed to alloc rec_msg");
- continue;
- }
-
- rec_msg->api = api;
- memcpy(rec_msg->recv_msg, parse_str[i], sizeof(rec_msg->recv_msg));
-
- /*LOGD("add recorder message to queue : api %d", api);*/
-
- g_mutex_lock(&cb_info->msg_handler_mutex);
- g_queue_push_tail(cb_info->msg_queue, (gpointer)rec_msg);
- g_cond_signal(&cb_info->msg_handler_cond);
- g_mutex_unlock(&cb_info->msg_handler_mutex);
-
- rec_msg = NULL;
+ } else if (api_class == MUSE_RECORDER_API_CLASS_THREAD_SUB || api == MUSE_RECORDER_CB_EVENT) {
+ __recorder_add_msg_to_queue(cb_info, api, event, event_class, parse_str[i]);
} else {
LOGW("unknown recorder api %d and api_class %d", api, api_class);
}
if (!cb_info->is_server_connected) {
/* send error msg for server disconnection */
error_msg = muse_core_msg_json_factory_new(MUSE_RECORDER_CB_EVENT,
- MUSE_TYPE_INT, PARAM_EVENT, MUSE_RECORDER_EVENT_TYPE_ERROR,
- MUSE_TYPE_INT, PARAM_EVENT_CLASS, MUSE_RECORDER_EVENT_CLASS_THREAD_MAIN,
MUSE_TYPE_INT, "error", RECORDER_ERROR_SERVICE_DISCONNECTED,
MUSE_TYPE_INT, "current_state", RECORDER_STATE_NONE,
NULL);
goto CB_HANDLER_EXIT;
}
- rec_msg = g_new0(recorder_message_s, 1);
- if (rec_msg == NULL) {
- LOGE("failed to alloc rec_msg");
- goto CB_HANDLER_EXIT;
- }
-
- rec_msg->api = MUSE_RECORDER_CB_EVENT;
- memcpy(rec_msg->recv_msg, error_msg, sizeof(rec_msg->recv_msg));
-
- /*LOGD("add recorder message to queue : api %d", api);*/
+ LOGE("add error msg for service disconnection done");
- g_mutex_lock(&cb_info->msg_handler_mutex);
- g_queue_push_tail(cb_info->msg_queue, (gpointer)rec_msg);
- g_cond_signal(&cb_info->msg_handler_cond);
- g_mutex_unlock(&cb_info->msg_handler_mutex);
+ __recorder_add_msg_to_queue(cb_info,
+ MUSE_RECORDER_CB_EVENT,
+ MUSE_RECORDER_EVENT_TYPE_ERROR,
+ MUSE_RECORDER_EVENT_CLASS_THREAD_MAIN,
+ error_msg);
- rec_msg = NULL;
+ muse_core_msg_json_factory_free(error_msg);
+ error_msg = NULL;
LOGE("add error msg for service disconnection done");
}
CB_HANDLER_EXIT:
- if (error_msg) {
- muse_core_msg_json_factory_free(error_msg);
- error_msg = NULL;
- }
-
if (parse_str) {
for (i = 0 ; i < RECORDER_PARSE_STRING_SIZE ; i++) {
if (parse_str[i]) {
return NULL;
}
-static recorder_cb_info_s *_recorder_client_callback_new(gint sockfd)
+
+static bool __create_msg_handler_thread(recorder_msg_handler_info_s *handler_info,
+ int type, const char *thread_name, recorder_cb_info_s *cb_info)
+{
+ if (!handler_info || !thread_name || !cb_info) {
+ LOGE("t:%d NULL %p %p %p",
+ type, handler_info, thread_name, cb_info);
+ return false;
+ }
+
+ LOGD("t:%d", type);
+
+ handler_info->type = type;
+ handler_info->queue = g_queue_new();
+ if (handler_info->queue == NULL) {
+ LOGE("t:%d queue failed", type);
+ return false;
+ }
+
+ g_mutex_init(&handler_info->mutex);
+ g_cond_init(&handler_info->cond);
+
+ handler_info->cb_info = (void *)cb_info;
+ g_atomic_int_set(&handler_info->running, 1);
+
+ handler_info->thread = g_thread_try_new(thread_name,
+ _recorder_msg_handler_func, (gpointer)handler_info, NULL);
+ if (handler_info->thread == NULL) {
+ LOGE("t:%d thread failed", type);
+
+ g_mutex_clear(&handler_info->mutex);
+ g_cond_clear(&handler_info->cond);
+ g_queue_free(handler_info->queue);
+ handler_info->queue = NULL;
+
+ return false;
+ }
+
+ LOGD("t:%d done", type);
+
+ return true;
+}
+
+
+static void __destroy_msg_handler_thread(recorder_msg_handler_info_s *handler_info)
+{
+ int type = 0;
+
+ if (!handler_info) {
+ LOGE("NULL handler");
+ return;
+ }
+
+ if (!handler_info->thread) {
+ LOGW("thread is not created");
+ return;
+ }
+
+ type = handler_info->type;
+
+ LOGD("t:%d thread %p", type, handler_info->thread);
+
+ g_mutex_lock(&handler_info->mutex);
+ g_atomic_int_set(&handler_info->running, 0);
+ g_cond_signal(&handler_info->cond);
+ g_mutex_unlock(&handler_info->mutex);
+
+ g_thread_join(handler_info->thread);
+ g_thread_unref(handler_info->thread);
+ handler_info->thread = NULL;
+
+ g_mutex_clear(&handler_info->mutex);
+ g_cond_clear(&handler_info->cond);
+ g_queue_free(handler_info->queue);
+ handler_info->queue = NULL;
+
+ LOGD("t:%d done", type);
+
+ return;
+}
+
+
+static recorder_cb_info_s *_recorder_client_callback_new(gint sockfd, bool need_msg_handler_thread)
{
recorder_cb_info_s *cb_info = NULL;
- gint *tmp_activating = NULL;
- gint *tmp_ret = NULL;
gint i = 0;
g_return_val_if_fail(sockfd > 0, NULL);
goto ErrorExit;
}
- g_mutex_init(&cb_info->msg_handler_mutex);
- g_cond_init(&cb_info->msg_handler_cond);
- g_mutex_init(&cb_info->idle_event_mutex);
- g_cond_init(&cb_info->idle_event_cond);
-
for (i = 0 ; i < MUSE_RECORDER_API_MAX ; i++) {
g_mutex_init(&cb_info->api_mutex[i]);
g_cond_init(&cb_info->api_cond[i]);
}
- tmp_activating = g_new0(gint, MUSE_RECORDER_API_MAX);
- if (tmp_activating == NULL) {
- LOGE("tmp_activating failed");
- goto ErrorExit;
- }
-
- tmp_ret = g_new0(gint, MUSE_RECORDER_API_MAX);
- if (tmp_ret == NULL) {
- LOGE("tmp_ret failed");
- goto ErrorExit;
- }
+ g_mutex_init(&cb_info->idle_event_mutex);
+ g_cond_init(&cb_info->idle_event_cond);
- cb_info->msg_queue = g_queue_new();
- if (cb_info->msg_queue == NULL) {
- LOGE("msg_queue new failed");
- goto ErrorExit;
- }
+ if (need_msg_handler_thread) {
+ /* message handler thread */
+ if (!__create_msg_handler_thread(&cb_info->msg_handler_info,
+ _RECORDER_MESSAGE_HANDLER_TYPE_GENERAL, "recorder_msg_handler", cb_info)) {
+ LOGE("msg_handler_info failed");
+ goto ErrorExit;
+ }
- g_atomic_int_set(&cb_info->msg_handler_running, 1);
- cb_info->msg_handler_thread = g_thread_try_new("recorder_msg_handler",
- _recorder_msg_handler_func, (gpointer)cb_info, NULL);
- if (cb_info->msg_handler_thread == NULL) {
- LOGE("message handler thread creation failed");
- goto ErrorExit;
+ /* message handler thread for audio stream callback */
+ if (!__create_msg_handler_thread(&cb_info->audio_stream_cb_info,
+ _RECORDER_MESSAGE_HANDLER_TYPE_AUDIO_STREAM_CB, "recorder_msg_handler:audio_stream_cb", cb_info)) {
+ LOGE("audio_stream_cb_info failed");
+ goto ErrorExit;
+ }
}
cb_info->fd = sockfd;
- cb_info->api_activating = tmp_activating;
- cb_info->api_ret = tmp_ret;
+ /* message receive thread */
g_atomic_int_set(&cb_info->msg_recv_running, 1);
cb_info->msg_recv_thread = g_thread_try_new("recorder_msg_recv",
_recorder_msg_recv_func, (gpointer)cb_info, NULL);
ErrorExit:
if (cb_info) {
- if (cb_info->msg_handler_thread) {
- g_mutex_lock(&cb_info->msg_handler_mutex);
- g_atomic_int_set(&cb_info->msg_handler_running, 0);
- g_cond_signal(&cb_info->msg_handler_cond);
- g_mutex_unlock(&cb_info->msg_handler_mutex);
-
- g_thread_join(cb_info->msg_handler_thread);
- g_thread_unref(cb_info->msg_handler_thread);
- cb_info->msg_handler_thread = NULL;
- }
+ __destroy_msg_handler_thread(&cb_info->msg_handler_info);
+ __destroy_msg_handler_thread(&cb_info->audio_stream_cb_info);
- for (i = 0 ; i < MUSE_RECORDER_API_MAX ; i++) {
- g_mutex_clear(&cb_info->api_mutex[i]);
- g_cond_clear(&cb_info->api_cond[i]);
- }
-
- g_mutex_clear(&cb_info->msg_handler_mutex);
- g_cond_clear(&cb_info->msg_handler_cond);
g_mutex_clear(&cb_info->idle_event_mutex);
g_cond_clear(&cb_info->idle_event_cond);
- if (cb_info->msg_queue) {
- g_queue_free(cb_info->msg_queue);
- cb_info->msg_queue = NULL;
+ for (i = 0 ; i < MUSE_RECORDER_API_MAX ; i++) {
+ g_mutex_clear(&cb_info->api_mutex[i]);
+ g_cond_clear(&cb_info->api_cond[i]);
}
g_free(cb_info);
cb_info = NULL;
}
- if (tmp_activating) {
- g_free(tmp_activating);
- tmp_activating = NULL;
- }
- if (tmp_ret) {
- g_free(tmp_ret);
- tmp_ret = NULL;
- }
-
return NULL;
}
g_thread_unref(cb_info->msg_recv_thread);
cb_info->msg_recv_thread = NULL;
- LOGD("msg thread removed");
+ LOGD("msg_recv thread removed");
- g_mutex_lock(&cb_info->msg_handler_mutex);
- g_atomic_int_set(&cb_info->msg_handler_running, 0);
- g_cond_signal(&cb_info->msg_handler_cond);
- g_mutex_unlock(&cb_info->msg_handler_mutex);
+ __destroy_msg_handler_thread(&cb_info->msg_handler_info);
+ __destroy_msg_handler_thread(&cb_info->audio_stream_cb_info);
- g_thread_join(cb_info->msg_handler_thread);
- g_thread_unref(cb_info->msg_handler_thread);
- cb_info->msg_handler_thread = NULL;
-
- g_queue_free(cb_info->msg_queue);
- cb_info->msg_queue = NULL;
- g_mutex_clear(&cb_info->msg_handler_mutex);
- g_cond_clear(&cb_info->msg_handler_cond);
g_mutex_clear(&cb_info->idle_event_mutex);
g_cond_clear(&cb_info->idle_event_cond);
g_cond_clear(&cb_info->api_cond[i]);
}
- LOGD("event thread removed");
-
if (cb_info->fd > -1) {
muse_core_connection_close(cb_info->fd);
cb_info->fd = -1;
tbm_bufmgr_deinit(cb_info->bufmgr);
cb_info->bufmgr = NULL;
}
- if (cb_info->api_activating) {
- g_free(cb_info->api_activating);
- cb_info->api_activating = NULL;
- }
- if (cb_info->api_ret) {
- g_free(cb_info->api_ret);
- cb_info->api_ret = NULL;
- }
if (cb_info->get_filename) {
free(cb_info->get_filename);
cb_info->get_filename = NULL;
goto _ERR_RECORDER_EXIT;
}
- pc->cb_info = _recorder_client_callback_new(sock_fd);
+ pc->cb_info = _recorder_client_callback_new(sock_fd, true);
if (pc->cb_info == NULL) {
ret = RECORDER_ERROR_OUT_OF_MEMORY;
goto _ERR_RECORDER_EXIT;