From 1dbcc2241014a5090093bbccce4878010763d2ea Mon Sep 17 00:00:00 2001 From: Minje Ahn Date: Thu, 26 Sep 2019 15:18:02 +0900 Subject: Improve thumb-server Change-Id: I827538cf8d3c634878d4d2da3fab71c8c89d0316 Signed-off-by: Minje Ahn --- server/include/thumb-server-internal.h | 7 - server/thumb-server-internal.c | 371 ++++++++++++++++----------------- src/include/ipc/media-thumb-ipc.h | 1 - src/ipc/media-thumb-ipc.c | 317 ++++++++++++---------------- 4 files changed, 311 insertions(+), 385 deletions(-) diff --git a/server/include/thumb-server-internal.h b/server/include/thumb-server-internal.h index 927f9e0..5d862c9 100755 --- a/server/include/thumb-server-internal.h +++ b/server/include/thumb-server-internal.h @@ -30,11 +30,4 @@ gboolean _thumb_daemon_start_jobs(gpointer data); gboolean _thumb_server_prepare_socket(int *sock_fd); gboolean _thumb_server_read_socket(GIOChannel *src, GIOCondition condition, gpointer data); -int _thumbnail_get_data(const char *origin_path, char *thumb_path); - -int _thumbnail_get_raw_data(const char *origin_path, int *width, int *height, unsigned char **data, int *size); - -int _media_thumb_process(thumbMsg *req_msg, thumbMsg *res_msg); -int _media_thumb_process_raw(thumbMsg *req_msg, thumbMsg *res_msg); - #endif /*_THUMB_DAEMON_INTERNAL_H_*/ diff --git a/server/thumb-server-internal.c b/server/thumb-server-internal.c index 3d97cb4..8aecc6b 100755 --- a/server/thumb-server-internal.c +++ b/server/thumb-server-internal.c @@ -40,6 +40,182 @@ #define THUMB_COMM_SOCK_PATH tzplatform_mkpath(TZ_SYS_RUN, "media-server/media_ipc_thumbcomm.socket") #define THUMB_EMPTY_STR "" +static int __thumbnail_get_data(const char *origin_path, char *thumb_path) +{ + int err = MS_MEDIA_ERR_NONE; + int file_type = THUMB_NONE_TYPE; + + thumb_retvm_if(!origin_path, MS_MEDIA_ERR_INVALID_PARAMETER, "Original path is null"); + thumb_retvm_if(!g_file_test(origin_path, G_FILE_TEST_IS_REGULAR), MS_MEDIA_ERR_INVALID_PARAMETER, "Original path(%s) does not exist", origin_path); + thumb_dbg("Origin path : %s", origin_path); + + file_type = _media_thumb_get_file_type(origin_path); + + if (file_type == THUMB_IMAGE_TYPE) { + err = _media_thumb_image(origin_path, thumb_path, THUMB_DEFAULT_WIDTH, THUMB_DEFAULT_HEIGHT, NULL); + thumb_retvm_if(err != MS_MEDIA_ERR_NONE, err, "_media_thumb_image failed"); + } else if (file_type == THUMB_VIDEO_TYPE) { + err = _media_thumb_video(origin_path, thumb_path, THUMB_DEFAULT_WIDTH, THUMB_DEFAULT_HEIGHT, NULL); + thumb_retvm_if(err != MS_MEDIA_ERR_NONE, err, "_media_thumb_video failed"); + } else { + thumb_err("invalid file type"); + return MS_MEDIA_ERR_THUMB_UNSUPPORTED; + } + + thumb_dbg("Thumb data is generated successfully"); + + return MS_MEDIA_ERR_NONE; +} + +static int __thumbnail_get_raw_data(const char *origin_path, int *width, int *height, unsigned char **data, int *size) +{ + int err = MS_MEDIA_ERR_NONE; + int thumb_width = -1; + int thumb_height = -1; + + if (origin_path == NULL || *width <= 0 || *height <= 0) { + thumb_err("Invalid parameter"); + return MS_MEDIA_ERR_INVALID_PARAMETER; + } + + if (!g_file_test(origin_path, G_FILE_TEST_IS_REGULAR)) { + thumb_err("Original path (%s) does not exist", origin_path); + return MS_MEDIA_ERR_INVALID_PARAMETER; + } + + int file_type = THUMB_NONE_TYPE; + media_thumb_info thumb_info = {0,}; + file_type = _media_thumb_get_file_type(origin_path); + thumb_width = *width; + thumb_height = *height; + + if (file_type == THUMB_IMAGE_TYPE) { + err = _media_thumb_image(origin_path, NULL, thumb_width, thumb_height, &thumb_info); + thumb_retvm_if(err != MS_MEDIA_ERR_NONE, err, "_media_thumb_image failed"); + } else if (file_type == THUMB_VIDEO_TYPE) { + err = _media_thumb_video(origin_path, NULL, thumb_width, thumb_height, &thumb_info); + thumb_retvm_if(err != MS_MEDIA_ERR_NONE, err, "_media_thumb_video failed"); + } else { + thumb_err("invalid file type"); + return MS_MEDIA_ERR_THUMB_UNSUPPORTED; + } + + if (size) *size = thumb_info.size; + *data = thumb_info.data; + *width = thumb_info.width; + *height = thumb_info.height; + + return MS_MEDIA_ERR_NONE; +} + +static int __media_thumb_process(thumbMsg *req_msg, thumbMsg *res_msg) +{ + int err = MS_MEDIA_ERR_NONE; + + thumb_retvm_if(!req_msg || !res_msg, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid msg"); + + memset(res_msg->dst_path, 0, MAX_FILEPATH_LEN); + res_msg->status = MS_MEDIA_ERR_NONE; + + if (!g_file_test(req_msg->org_path, G_FILE_TEST_IS_REGULAR)) { + thumb_err("origin_path does not exist in file system."); + res_msg->status = MS_MEDIA_ERR_FILE_NOT_EXIST; + return MS_MEDIA_ERR_FILE_NOT_EXIST; + } + + err = _media_thumb_get_thumb_from_db(req_msg->org_path, res_msg->dst_path, MAX_FILEPATH_LEN, req_msg->uid); + thumb_retvm_if(err == MS_MEDIA_ERR_NONE, err, "Already exists"); + if (strlen(res_msg->dst_path) == 0) { + err = _media_thumb_get_hash_name(req_msg->org_path, res_msg->dst_path, MAX_FILEPATH_LEN, req_msg->uid); + if (err != MS_MEDIA_ERR_NONE) { + thumb_err("_media_thumb_get_hash_name failed - %d", err); + SAFE_STRLCPY(res_msg->dst_path, THUMB_EMPTY_STR, MAX_FILEPATH_LEN); + res_msg->status = err; + return err; + } + } + + thumb_dbg_slog("Thumb path : %s", res_msg->dst_path); + + err = __thumbnail_get_data(req_msg->org_path, res_msg->dst_path); + if (err != MS_MEDIA_ERR_NONE) { + thumb_err("_thumbnail_get_data failed - %d", err); + SAFE_STRLCPY(res_msg->dst_path, THUMB_EMPTY_STR, MAX_FILEPATH_LEN); + res_msg->status = err; + + goto DB_UPDATE; + } + + res_msg->msg_type = THUMB_RESPONSE; + +DB_UPDATE: + err = _media_thumb_update_db(req_msg->org_path, res_msg->dst_path, req_msg->uid); + if (err != MS_MEDIA_ERR_NONE) { + thumb_err("_media_thumb_update_db failed : %d", err); + res_msg->status = err; + } + + return err; +} + +static int __media_thumb_process_raw(thumbMsg *req_msg, thumbMsg *res_msg) +{ + int err = MS_MEDIA_ERR_NONE; + unsigned char *data = NULL; + int thumb_size = 0; + int thumb_w = 0; + int thumb_h = 0; + + thumb_retvm_if(!req_msg || !res_msg, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid msg"); + + thumb_w = (req_msg->thumb_width > 0) ? req_msg->thumb_width : THUMB_DEFAULT_WIDTH; + thumb_h = (req_msg->thumb_height > 0) ? req_msg->thumb_height : THUMB_DEFAULT_HEIGHT; + + res_msg->status = MS_MEDIA_ERR_NONE; + res_msg->msg_type = THUMB_RESPONSE_RAW_DATA; + + err = __thumbnail_get_raw_data(req_msg->org_path, &thumb_w, &thumb_h, &data, &thumb_size); + if (err != MS_MEDIA_ERR_NONE) { + thumb_err("_thumbnail_get_data failed - %d", err); + res_msg->status = err; + res_msg->thumb_size = 0; + goto ERROR; + } + + res_msg->thumb_width = thumb_w; + res_msg->thumb_height = thumb_h; + res_msg->thumb_size = thumb_size; + res_msg->thumb_data = malloc(thumb_size * sizeof(unsigned char)); + if (res_msg->thumb_data) { + memcpy(res_msg->thumb_data, data, thumb_size); + } else { + thumb_err("Allocation failed"); + err = MS_MEDIA_ERR_OUT_OF_MEMORY; + } + +ERROR: + SAFE_FREE(data); + + return err; +} + +static int __thumb_daemon_process_job(thumbMsg *req_msg, thumbMsg *res_msg) +{ + int err = MS_MEDIA_ERR_NONE; + + if (req_msg->msg_type == THUMB_REQUEST_RAW_DATA) { + err = __media_thumb_process_raw(req_msg, res_msg); + if (err != MS_MEDIA_ERR_NONE) + thumb_err("_media_thumb_process_raw is failed: %d", err); + } else if (req_msg->msg_type == THUMB_REQUEST_DB_INSERT) { + err = __media_thumb_process(req_msg, res_msg); + if (err != MS_MEDIA_ERR_NONE) + thumb_err("_media_thumb_process is failed: %d", err); + } + + return err; +} + static void __thumb_server_send_deny_message(int sockfd) { thumbMsg msg = {0}; @@ -87,23 +263,6 @@ ERROR: return G_SOURCE_REMOVE; } -int _thumb_daemon_process_job(thumbMsg *req_msg, thumbMsg *res_msg) -{ - int err = MS_MEDIA_ERR_NONE; - - if (req_msg->msg_type == THUMB_REQUEST_RAW_DATA) { - err = _media_thumb_process_raw(req_msg, res_msg); - if (err != MS_MEDIA_ERR_NONE) - thumb_warn("_media_thumb_process_raw is failed: %d", err); - } else if (req_msg->msg_type == THUMB_REQUEST_DB_INSERT) { - err = _media_thumb_process(req_msg, res_msg); - if (err != MS_MEDIA_ERR_NONE) - thumb_warn("_media_thumb_process is failed: %d", err); - } - - return err; -} - gboolean _thumb_server_read_socket(GIOChannel *src, GIOCondition condition, gpointer data) { struct sockaddr_un client_addr; @@ -157,7 +316,7 @@ gboolean _thumb_server_read_socket(GIOChannel *src, GIOCondition condition, gpoi thumb_warn_slog("Received [%d] %.*s(%zu) from PID(%d)", recv_msg.msg_type, MAX_FILEPATH_LEN, recv_msg.org_path, strlen(recv_msg.org_path), recv_msg.pid); - _thumb_daemon_process_job(&recv_msg, &res_msg); + __thumb_daemon_process_job(&recv_msg, &res_msg); if (res_msg.msg_type == 0) res_msg.msg_type = recv_msg.msg_type; @@ -207,179 +366,3 @@ gboolean _thumb_server_prepare_socket(int *sock_fd) return TRUE; } - -int _thumbnail_get_data(const char *origin_path, char *thumb_path) -{ - int err = MS_MEDIA_ERR_NONE; - int file_type = THUMB_NONE_TYPE; - - thumb_retvm_if(!origin_path, MS_MEDIA_ERR_INVALID_PARAMETER, "Original path is null"); - thumb_retvm_if(!g_file_test(origin_path, G_FILE_TEST_IS_REGULAR), MS_MEDIA_ERR_INVALID_PARAMETER, "Original path(%s) does not exist", origin_path); - thumb_dbg("Origin path : %s", origin_path); - - file_type = _media_thumb_get_file_type(origin_path); - - if (file_type == THUMB_IMAGE_TYPE) { - err = _media_thumb_image(origin_path, thumb_path, THUMB_DEFAULT_WIDTH, THUMB_DEFAULT_HEIGHT, NULL); - thumb_retvm_if(err != MS_MEDIA_ERR_NONE, err, "_media_thumb_image failed"); - } else if (file_type == THUMB_VIDEO_TYPE) { - err = _media_thumb_video(origin_path, thumb_path, THUMB_DEFAULT_WIDTH, THUMB_DEFAULT_HEIGHT, NULL); - thumb_retvm_if(err != MS_MEDIA_ERR_NONE, err, "_media_thumb_video failed"); - } else { - thumb_err("invalid file type"); - return MS_MEDIA_ERR_THUMB_UNSUPPORTED; - } - - thumb_dbg("Thumb data is generated successfully"); - - return MS_MEDIA_ERR_NONE; -} - -int _thumbnail_get_raw_data(const char *origin_path, int *width, int *height, unsigned char **data, int *size) -{ - int err = MS_MEDIA_ERR_NONE; - int thumb_width = -1; - int thumb_height = -1; - - if (origin_path == NULL || *width <= 0 || *height <= 0) { - thumb_err("Invalid parameter"); - return MS_MEDIA_ERR_INVALID_PARAMETER; - } - - if (!g_file_test(origin_path, G_FILE_TEST_IS_REGULAR)) { - thumb_err("Original path (%s) does not exist", origin_path); - return MS_MEDIA_ERR_INVALID_PARAMETER; - } - - int file_type = THUMB_NONE_TYPE; - media_thumb_info thumb_info = {0,}; - file_type = _media_thumb_get_file_type(origin_path); - thumb_width = *width; - thumb_height = *height; - - if (file_type == THUMB_IMAGE_TYPE) { - err = _media_thumb_image(origin_path, NULL, thumb_width, thumb_height, &thumb_info); - thumb_retvm_if(err != MS_MEDIA_ERR_NONE, err, "_media_thumb_image failed"); - } else if (file_type == THUMB_VIDEO_TYPE) { - err = _media_thumb_video(origin_path, NULL, thumb_width, thumb_height, &thumb_info); - thumb_retvm_if(err != MS_MEDIA_ERR_NONE, err, "_media_thumb_video failed"); - } else { - thumb_err("invalid file type"); - return MS_MEDIA_ERR_THUMB_UNSUPPORTED; - } - - if (size) *size = thumb_info.size; - *data = thumb_info.data; - *width = thumb_info.width; - *height = thumb_info.height; - - return MS_MEDIA_ERR_NONE; -} - -int _media_thumb_process(thumbMsg *req_msg, thumbMsg *res_msg) -{ - int err = MS_MEDIA_ERR_NONE; - char *thumb_path = NULL; - - thumb_retvm_if(req_msg == NULL || res_msg == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid msg"); - - const char *origin_path = req_msg->org_path; - memset(res_msg->dst_path, 0, MAX_FILEPATH_LEN); - thumb_path = res_msg->dst_path; - res_msg->status = MS_MEDIA_ERR_NONE; - - if (!g_file_test(origin_path, G_FILE_TEST_IS_REGULAR)) { - thumb_err("origin_path does not exist in file system."); - res_msg->status = MS_MEDIA_ERR_FILE_NOT_EXIST; - return MS_MEDIA_ERR_FILE_NOT_EXIST; - } - - err = _media_thumb_get_thumb_from_db(origin_path, thumb_path, MAX_FILEPATH_LEN, req_msg->uid); - if (err == MS_MEDIA_ERR_NONE) { - thumb_dbg_slog("Thumb path : %s", thumb_path); - return MS_MEDIA_ERR_NONE; - } else { - if (strlen(thumb_path) == 0) { - err = _media_thumb_get_hash_name(origin_path, thumb_path, MAX_FILEPATH_LEN, req_msg->uid); - if (err != MS_MEDIA_ERR_NONE) { - thumb_err("_media_thumb_get_hash_name failed - %d", err); - SAFE_STRLCPY(thumb_path, THUMB_EMPTY_STR, MAX_FILEPATH_LEN); - res_msg->status = err; - return err; - } - } - } - - thumb_dbg_slog("Thumb path : %s", thumb_path); - - err = _thumbnail_get_data(origin_path, thumb_path); - if (err != MS_MEDIA_ERR_NONE) { - thumb_err("_thumbnail_get_data failed - %d", err); - SAFE_STRLCPY(thumb_path, THUMB_EMPTY_STR, MAX_FILEPATH_LEN); - res_msg->status = err; - - goto DB_UPDATE; - } - - res_msg->msg_type = THUMB_RESPONSE; - -DB_UPDATE: - err = _media_thumb_update_db(origin_path, thumb_path, req_msg->uid); - if (err != MS_MEDIA_ERR_NONE) { - thumb_err("_media_thumb_update_db failed : %d", err); - res_msg->status = err; - } - - return err; -} - -int -_media_thumb_process_raw(thumbMsg *req_msg, thumbMsg *res_msg) -{ - int err = MS_MEDIA_ERR_NONE; - unsigned char *data = NULL; - int thumb_size = 0; - int thumb_w = 0; - int thumb_h = 0; - - thumb_retvm_if(req_msg == NULL || res_msg == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid msg"); - - const char *origin_path = req_msg->org_path; - if (req_msg->thumb_width > 0) - thumb_w = req_msg->thumb_width; - else - thumb_w = THUMB_DEFAULT_WIDTH; - - if (req_msg->thumb_height > 0) - thumb_h = req_msg->thumb_height; - else - thumb_h = THUMB_DEFAULT_HEIGHT; - - res_msg->status = MS_MEDIA_ERR_NONE; - res_msg->msg_type = THUMB_RESPONSE_RAW_DATA; - - err = _thumbnail_get_raw_data(origin_path, &thumb_w, &thumb_h, &data, &thumb_size); - if (err != MS_MEDIA_ERR_NONE) { - thumb_err("_thumbnail_get_data failed - %d", err); - res_msg->status = err; - res_msg->thumb_size = 0; - SAFE_FREE(data); - - return err; - } - - res_msg->thumb_width = thumb_w; - res_msg->thumb_height = thumb_h; - res_msg->thumb_size = thumb_size; - res_msg->thumb_data = malloc(thumb_size * sizeof(unsigned char)); - if (res_msg->thumb_data != NULL) { - memcpy(res_msg->thumb_data, data, thumb_size); - } else { - thumb_err("Allocation failed"); - err = MS_MEDIA_ERR_OUT_OF_MEMORY; - } - - SAFE_FREE(data); - - return err; -} diff --git a/src/include/ipc/media-thumb-ipc.h b/src/include/ipc/media-thumb-ipc.h index 7d0357d..b242b8e 100755 --- a/src/include/ipc/media-thumb-ipc.h +++ b/src/include/ipc/media-thumb-ipc.h @@ -38,7 +38,6 @@ #define MAX_PATH_SIZE 4096 -int _media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg); int _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size); int _media_thumb_request_async(int msg_type, unsigned int request_id, const char *origin_path, thumbUserData *userData, uid_t uid); diff --git a/src/ipc/media-thumb-ipc.c b/src/ipc/media-thumb-ipc.c index 64afe5a..dfbab42 100755 --- a/src/ipc/media-thumb-ipc.c +++ b/src/ipc/media-thumb-ipc.c @@ -66,64 +66,19 @@ typedef struct { static int __media_thumb_send_request(void); static int __media_thumb_raw_data_send_request(void); - -static int __media_thumb_get_error(void) -{ - if (errno == EWOULDBLOCK) { - thumb_err("Timeout. Can't try any more"); - return MS_MEDIA_ERR_SOCKET_RECEIVE_TIMEOUT; - } else { - thumb_stderror("recvfrom failed"); - return MS_MEDIA_ERR_SOCKET_RECEIVE; - } -} - -static void __media_thumb_pop(void) -{ - int len = 0; - int sock = 0; - thumbReq *req = NULL; - - if (!g_manage_queue) - return; - - req = (thumbReq *)g_queue_pop_head(g_manage_queue); - if (req != NULL) { - GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id); - sock = g_io_channel_unix_get_fd(req->channel); - - g_io_channel_shutdown(req->channel, TRUE, NULL); - g_io_channel_unref(req->channel); - close(sock); - if (source_id) { - g_source_destroy(source_id); - } else { - thumb_err("G_SOURCE_ID is NULL"); - } - - SAFE_FREE(req->path); - SAFE_FREE(req->userData); - SAFE_FREE(req); - } - - /* Check manage queue */ - len = g_queue_get_length(g_manage_queue); - if (len > 0) - __media_thumb_send_request(); -} - static int __media_thumb_cancel(unsigned int request_id) { int len = 0, i; + thumbReq *req = NULL; thumb_retv_if(!g_manage_queue, MS_MEDIA_ERR_NONE); len = g_queue_get_length(g_manage_queue); for (i = 0; i < len; i++) { - thumbReq *req = NULL; req = (thumbReq *)g_queue_peek_nth(g_manage_queue, i); - if (req == NULL) continue; + if (!req) + continue; if (req->request_id == request_id) { if (req->isRequested) { @@ -143,52 +98,19 @@ static int __media_thumb_cancel(unsigned int request_id) return MS_MEDIA_ERR_INTERNAL; } -static void __media_thumb_pop_raw_data(void) -{ - int len = 0; - int sock = 0; - thumbRawReq *req = NULL; - - if (!g_manage_raw_queue) - return; - - req = (thumbRawReq *)g_queue_pop_head(g_manage_raw_queue); - if (req != NULL) { - GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id); - sock = g_io_channel_unix_get_fd(req->channel); - - g_io_channel_shutdown(req->channel, TRUE, NULL); - g_io_channel_unref(req->channel); - close(sock); - if (source_id) { - g_source_destroy(source_id); - } else { - thumb_err("G_SOURCE_ID is NULL"); - } - - SAFE_FREE(req->path); - SAFE_FREE(req->userData); - SAFE_FREE(req); - } - - /* Check manage queue */ - len = g_queue_get_length(g_manage_raw_queue); - if (len > 0) - __media_thumb_raw_data_send_request(); -} - static int __media_thumb_cancel_raw_data(int request_id) { int len = 0, i; + thumbRawReq *req = NULL; thumb_retv_if(!g_manage_raw_queue, MS_MEDIA_ERR_NONE); len = g_queue_get_length(g_manage_raw_queue); for (i = 0; i < len; i++) { - thumbRawReq *req = NULL; req = (thumbRawReq *)g_queue_peek_nth(g_manage_raw_queue, i); - if (req == NULL) continue; + if (!req) + continue; if (req->request_id == request_id) { if (req->isRequested) { @@ -208,35 +130,27 @@ static int __media_thumb_cancel_raw_data(int request_id) return MS_MEDIA_ERR_INTERNAL; } -static bool __media_thumb_check_cancel(void) +static bool __media_thumb_is_canceled(void) { - thumbReq *req = NULL; - req = (thumbReq *)g_queue_peek_head(g_manage_queue); + thumbReq *req = (thumbReq *)g_queue_peek_head(g_manage_queue); if (!req) - return false; - - if (req->isCanceled) - return false; - else return true; + + return req->isCanceled; } -static bool __media_thumb_check_cancel_for_raw(void) +static bool __media_thumb_is_canceled_for_raw(void) { - thumbRawReq *req = NULL; - req = (thumbRawReq *)g_queue_peek_head(g_manage_raw_queue); + thumbRawReq *req = (thumbRawReq *)g_queue_peek_head(g_manage_raw_queue); if (!req) - return false; - - if (req->isCanceled) - return false; - else return true; + + return req->isCanceled; } -int _media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg) +static int __media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg) { int remain_size = 0; int recv_len = 0; @@ -244,17 +158,13 @@ int _media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg) unsigned char *buf = NULL; THUMB_MALLOC(buf, header_size); - if (buf == NULL) { - thumb_err("memory allocation failed"); - SAFE_FREE(buf); - return MS_MEDIA_ERR_OUT_OF_MEMORY; - } + thumb_retv_if(!buf, MS_MEDIA_ERR_OUT_OF_MEMORY); while (header_size > 0) { if ((recv_len = recv(sock, buf + recv_pos, header_size, 0)) < 0) { thumb_stderror("recv failed"); SAFE_FREE(buf); - return __media_thumb_get_error(); + return MS_MEDIA_ERR_IPC; } header_size -= recv_len; recv_pos += recv_len; @@ -266,48 +176,34 @@ int _media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg) memcpy(msg, buf, header_size); SAFE_FREE(buf); - if (strlen(msg->org_path) == 0 || strlen(msg->org_path) >= MAX_FILEPATH_LEN) { - thumb_err("org_path size is invalid %zu", strlen(msg->org_path)); - return MS_MEDIA_ERR_SOCKET_RECEIVE; - } - - /* it can be empty string */ - if (strlen(msg->dst_path) >= MAX_FILEPATH_LEN) { - thumb_err("dst_path size is invalid %zu", strlen(msg->dst_path)); - return MS_MEDIA_ERR_SOCKET_RECEIVE; - } + thumb_retvm_if(strlen(msg->org_path) == 0 || strlen(msg->org_path) >= MAX_FILEPATH_LEN, MS_MEDIA_ERR_IPC, "Invalid org_path"); + thumb_retvm_if(strlen(msg->dst_path) >= MAX_FILEPATH_LEN, MS_MEDIA_ERR_IPC, "Invalid dst_path"); + thumb_retvm_if(msg->thumb_size < 0, MS_MEDIA_ERR_IPC, "Invalid thumb_size"); + thumb_retv_if(msg->thumb_size == 0, MS_MEDIA_ERR_NONE); - if (msg->thumb_size < 0) { - thumb_err("recv data is wrong"); - return MS_MEDIA_ERR_SOCKET_RECEIVE; - } + remain_size = msg->thumb_size; + THUMB_MALLOC(buf, remain_size); + thumb_retv_if(!buf, MS_MEDIA_ERR_OUT_OF_MEMORY); - if (msg->thumb_size > 0) { - remain_size = msg->thumb_size; - THUMB_MALLOC(buf, remain_size); - thumb_retv_if(!buf, MS_MEDIA_ERR_OUT_OF_MEMORY); - - while (remain_size > 0) { - if ((recv_len = recv(sock, buf + recv_pos, remain_size, 0)) < 0) { - thumb_stderror("recv failed"); - SAFE_FREE(buf); - return __media_thumb_get_error(); - } - - fsync(sock); - remain_size -= recv_len; - recv_pos += recv_len; + while (remain_size > 0) { + if ((recv_len = recv(sock, buf + recv_pos, remain_size, 0)) < 0) { + thumb_stderror("recv failed"); + SAFE_FREE(buf); + return MS_MEDIA_ERR_IPC; } - SAFE_FREE(msg->thumb_data); - THUMB_MALLOC(msg->thumb_data, msg->thumb_size); - if (msg->thumb_data != NULL) { - memcpy(msg->thumb_data, buf, msg->thumb_size); - } else { - SAFE_FREE(buf); + fsync(sock); + remain_size -= recv_len; + recv_pos += recv_len; + } - return MS_MEDIA_ERR_OUT_OF_MEMORY; - } + SAFE_FREE(msg->thumb_data); + THUMB_MALLOC(msg->thumb_data, msg->thumb_size); + if (msg->thumb_data) { + memcpy(msg->thumb_data, buf, msg->thumb_size); + } else { + SAFE_FREE(buf); + return MS_MEDIA_ERR_OUT_OF_MEMORY; } SAFE_FREE(buf); @@ -329,8 +225,6 @@ int _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_siz if (thumb_data_len < 0) return MS_MEDIA_ERR_INVALID_PARAMETER; - //thumb_dbg("Basic Size[%d] org_path[%s] dst_path[%s] thumb_data_len[%d]", header_size, req_msg->org_path, req_msg->dst_path, thumb_data_len); - size = header_size + thumb_data_len; THUMB_MALLOC(*buf, size); if (*buf == NULL) { @@ -346,25 +240,86 @@ int _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_siz return MS_MEDIA_ERR_NONE; } -gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data) +static void __media_thumb_pop(void) +{ + int len = 0; + int sock = 0; + thumbReq *req = NULL; + GSource *source_id = NULL; + + if (!g_manage_queue) + return; + + req = (thumbReq *)g_queue_pop_head(g_manage_queue); + if (req) { + source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id); + sock = g_io_channel_unix_get_fd(req->channel); + + g_io_channel_shutdown(req->channel, TRUE, NULL); + g_io_channel_unref(req->channel); + close(sock); + if (source_id) + g_source_destroy(source_id); + + SAFE_FREE(req->path); + SAFE_FREE(req->userData); + SAFE_FREE(req); + } + + /* Check manage queue */ + len = g_queue_get_length(g_manage_queue); + if (len > 0) + __media_thumb_send_request(); +} + +static void __media_thumb_pop_raw_data(void) +{ + int len = 0; + int sock = 0; + thumbRawReq *req = NULL; + GSource *source_id = NULL; + + if (!g_manage_raw_queue) + return; + + req = (thumbRawReq *)g_queue_pop_head(g_manage_raw_queue); + if (req) { + source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id); + sock = g_io_channel_unix_get_fd(req->channel); + + g_io_channel_shutdown(req->channel, TRUE, NULL); + g_io_channel_unref(req->channel); + close(sock); + if (source_id) + g_source_destroy(source_id); + + SAFE_FREE(req->path); + SAFE_FREE(req->userData); + SAFE_FREE(req); + } + + /* Check manage queue */ + len = g_queue_get_length(g_manage_raw_queue); + if (len > 0) + __media_thumb_raw_data_send_request(); +} + +static gboolean __media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data) { thumbMsg recv_msg; int header_size = 0; int sock = 0; int err = MS_MEDIA_ERR_NONE; + thumbUserData *userdata = NULL; memset((void *)&recv_msg, 0, sizeof(thumbMsg)); sock = g_io_channel_unix_get_fd(src); header_size = sizeof(thumbMsg) - sizeof(unsigned char *); - thumb_err("_media_thumb_write_socket socket : %d", sock); - - if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) { + if ((err = __media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) { thumb_err("_media_thumb_recv_msg failed "); - __media_thumb_pop(); - - return G_SOURCE_REMOVE; + goto NEXT; } if (recv_msg.status != MS_MEDIA_ERR_NONE) { @@ -372,27 +327,27 @@ gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpoi thumb_err("Failed to make thumbnail (%d)", err); } - if (__media_thumb_check_cancel()) { - if (data) { - thumbUserData* cb = (thumbUserData*)data; - if (cb->func != NULL) - cb->func(err, recv_msg.dst_path, cb->user_data); - } - } + if (__media_thumb_is_canceled() || !data) + goto NEXT; - __media_thumb_pop(); + userdata = (thumbUserData*)data; + if (userdata->func) + userdata->func(err, recv_msg.dst_path, userdata->user_data); thumb_dbg("Done"); +NEXT: + __media_thumb_pop(); return G_SOURCE_REMOVE; } -gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condition, gpointer data) +static gboolean __media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condition, gpointer data) { thumbMsg recv_msg; int header_size = 0; int sock = 0; int err = MS_MEDIA_ERR_NONE; + thumbRawUserData *userdata = NULL; memset((void *)&recv_msg, 0, sizeof(thumbMsg)); sock = g_io_channel_unix_get_fd(src); @@ -401,11 +356,9 @@ gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condit thumb_err("_media_thumb_write_socket socket : %d", sock); - if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) { + if ((err = __media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) { thumb_err("_media_thumb_recv_msg failed "); - __media_thumb_pop_raw_data(); - - return G_SOURCE_REMOVE; + goto NEXT; } if (recv_msg.status != MS_MEDIA_ERR_NONE) { @@ -413,17 +366,16 @@ gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condit thumb_err("Failed to make thumbnail (%d)", err); } - if (__media_thumb_check_cancel_for_raw()) { - if (data) { - thumbRawUserData* cb = (thumbRawUserData*)data; - if (cb->func != NULL) - cb->func(err, recv_msg.request_id, recv_msg.org_path, recv_msg.thumb_width, recv_msg.thumb_height, recv_msg.thumb_data, recv_msg.thumb_size, cb->user_data); - } - } + if (__media_thumb_is_canceled_for_raw() || !data) + goto NEXT; - __media_thumb_pop_raw_data(); + userdata = (thumbRawUserData*)data; + if (userdata->func) + userdata->func(err, recv_msg.request_id, recv_msg.org_path, recv_msg.thumb_width, recv_msg.thumb_height, recv_msg.thumb_data, recv_msg.thumb_size, userdata->user_data); thumb_dbg("Done"); +NEXT: + __media_thumb_pop_raw_data(); return G_SOURCE_REMOVE; } @@ -453,7 +405,7 @@ static int __media_thumb_send_request(void) if (errno == EACCES) err = MS_MEDIA_ERR_PERMISSION_DENIED; else - err = MS_MEDIA_ERR_SOCKET_CONN; + err = MS_MEDIA_ERR_IPC; g_io_channel_shutdown(channel, TRUE, NULL); g_io_channel_unref(channel); @@ -463,8 +415,7 @@ static int __media_thumb_send_request(void) } req_manager = (thumbReq *)g_queue_peek_head(g_manage_queue); - - if (req_manager == NULL) { + if (!req_manager) { thumb_err("queue peek fail"); g_io_channel_shutdown(channel, TRUE, NULL); g_io_channel_unref(channel); @@ -474,7 +425,7 @@ static int __media_thumb_send_request(void) GSource *source = NULL; source = g_io_create_watch(channel, G_IO_IN); - g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, req_manager->userData, NULL); + g_source_set_callback(source, (GSourceFunc)__media_thumb_write_socket, req_manager->userData, NULL); source_id = g_source_attach(source, g_main_context_get_thread_default()); thumbMsg req_msg; @@ -500,7 +451,7 @@ static int __media_thumb_send_request(void) g_io_channel_shutdown(channel, TRUE, NULL); g_io_channel_unref(channel); close(sock); - return MS_MEDIA_ERR_SOCKET_SEND; + return MS_MEDIA_ERR_IPC; } SAFE_FREE(buf); @@ -540,7 +491,7 @@ static int __media_thumb_raw_data_send_request(void) if (errno == EACCES) err = MS_MEDIA_ERR_PERMISSION_DENIED; else - err = MS_MEDIA_ERR_SOCKET_CONN; + err = MS_MEDIA_ERR_IPC; g_io_channel_shutdown(channel, TRUE, NULL); g_io_channel_unref(channel); @@ -550,7 +501,7 @@ static int __media_thumb_raw_data_send_request(void) req_manager = (thumbRawReq *)g_queue_peek_head(g_manage_raw_queue); - if (req_manager == NULL) { + if (!req_manager) { thumb_err("queue peek fail"); g_io_channel_shutdown(channel, TRUE, NULL); g_io_channel_unref(channel); @@ -560,7 +511,7 @@ static int __media_thumb_raw_data_send_request(void) GSource *source = NULL; source = g_io_create_watch(channel, G_IO_IN); - g_source_set_callback(source, (GSourceFunc)_media_thumb_raw_data_write_socket, req_manager->userData, NULL); + g_source_set_callback(source, (GSourceFunc)__media_thumb_raw_data_write_socket, req_manager->userData, NULL); source_id = g_source_attach(source, g_main_context_get_thread_default()); thumbMsg req_msg; @@ -588,7 +539,7 @@ static int __media_thumb_raw_data_send_request(void) g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id)); g_io_channel_shutdown(channel, TRUE, NULL); g_io_channel_unref(channel); - return MS_MEDIA_ERR_SOCKET_SEND; + return MS_MEDIA_ERR_IPC; } SAFE_FREE(buf); -- cgit v1.2.3