Improve thumb-server
[platform/core/multimedia/libmedia-thumbnail.git] / src / ipc / media-thumb-ipc.c
index 7f4c381..dfbab42 100755 (executable)
 #include <grp.h>
 #include <pwd.h>
 
-#define GLOBAL_USER    0 //#define     tzplatform_getenv(TZ_GLOBAL) //TODO
+#define THUMB_IPC_PATH tzplatform_mkpath(TZ_SYS_RUN, "media-server/media_ipc_thumbcreator.socket")
+
+static GQueue *g_manage_queue = NULL;
+static GQueue *g_manage_raw_queue = NULL;
+
 
-static __thread GQueue *g_request_queue = NULL;
 typedef struct {
        GIOChannel *channel;
-       char *path;
+       int msg_type;
+       unsigned int request_id;
+       bool isCanceled;
+       bool isRequested;
        int source_id;
+       uid_t uid;
+       char *path;
+       thumbUserData *userData;
 } thumbReq;
 
-int _media_thumb_create_socket(int sock_type, int *sock)
-{
-       int sock_fd = 0;
-
-       if ((sock_fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
-               thumb_err("socket failed: %s", strerror(errno));
-               return MS_MEDIA_ERR_SOCKET_CONN;
-       }
-
-       if (sock_type == CLIENT_SOCKET) {
-               struct timeval tv_timeout = { MS_TIMEOUT_SEC_10, 0 };
-
-               if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &tv_timeout, sizeof(tv_timeout)) == -1) {
-                       thumb_err("setsockopt failed: %s", strerror(errno));
-                       close(sock_fd);
-                       return MS_MEDIA_ERR_SOCKET_CONN;
-               }
-       } else if (sock_type == SERVER_SOCKET) {
-
-               int n_reuse = 1;
-
-               if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &n_reuse, sizeof(n_reuse)) == -1) {
-                       thumb_err("setsockopt failed: %s", strerror(errno));
-                       close(sock_fd);
-                       return MS_MEDIA_ERR_SOCKET_CONN;
-               }
-       }
-
-       *sock = sock_fd;
-
-       return MS_MEDIA_ERR_NONE;
-}
+typedef struct {
+       GIOChannel *channel;
+       int msg_type;
+       bool isCanceled;
+       bool isRequested;
+       int request_id;
+       int source_id;
+       int width;
+       int height;
+       uid_t uid;
+       char *path;
+       thumbRawUserData *userData;
+} thumbRawReq;
 
+static int __media_thumb_send_request(void);
+static int __media_thumb_raw_data_send_request(void);
 
-int _media_thumb_create_udp_socket(int *sock)
+static int __media_thumb_cancel(unsigned int request_id)
 {
-       int sock_fd = 0;
+       int len = 0, i;
+       thumbReq *req = NULL;
 
-       if ((sock_fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
-               thumb_err("socket failed: %s", strerror(errno));
-               return MS_MEDIA_ERR_SOCKET_CONN;
-       }
+       thumb_retv_if(!g_manage_queue, MS_MEDIA_ERR_NONE);
 
-       struct timeval tv_timeout = { MS_TIMEOUT_SEC_10, 0 };
+       len = g_queue_get_length(g_manage_queue);
 
-       if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &tv_timeout, sizeof(tv_timeout)) == -1) {
-               thumb_err("setsockopt failed: %s", strerror(errno));
-               close(sock_fd);
-               return MS_MEDIA_ERR_SOCKET_CONN;
-       }
+       for (i = 0; i < len; i++) {
+               req = (thumbReq *)g_queue_peek_nth(g_manage_queue, i);
+               if (!req)
+                       continue;
 
-       *sock = sock_fd;
+               if (req->request_id == request_id) {
+                       if (req->isRequested) {
+                               req->isCanceled = true;
+                       } else {
+                               g_queue_pop_nth(g_manage_queue, i);
 
-       return MS_MEDIA_ERR_NONE;
-}
+                               SAFE_FREE(req->path);
+                               SAFE_FREE(req->userData);
+                               SAFE_FREE(req);
+                       }
 
-int _media_thumb_get_error()
-{
-       if (errno == EWOULDBLOCK) {
-               thumb_err("Timeout. Can't try any more");
-               return MS_MEDIA_ERR_SOCKET_RECEIVE_TIMEOUT;
-       } else {
-               thumb_err("recvfrom failed : %s", strerror(errno));
-               return MS_MEDIA_ERR_SOCKET_RECEIVE;
+                       return MS_MEDIA_ERR_NONE;
+               }
        }
+
+       return MS_MEDIA_ERR_INTERNAL;
 }
 
-int __media_thumb_pop_req_queue(const char *path, bool shutdown_channel)
+static int __media_thumb_cancel_raw_data(int request_id)
 {
-       int req_len = 0, i;
+       int len = 0, i;
+       thumbRawReq *req = NULL;
 
-       if (g_request_queue == NULL) return MS_MEDIA_ERR_INVALID_PARAMETER;
-       req_len = g_queue_get_length(g_request_queue);
-
-       if (req_len <= 0) {
-//             thumb_dbg("There is no request in the queue");
-       } else {
+       thumb_retv_if(!g_manage_raw_queue, MS_MEDIA_ERR_NONE);
 
-               for (i = 0; i < req_len; i++) {
-                       thumbReq *req = NULL;
-                       req = (thumbReq *)g_queue_peek_nth(g_request_queue, i);
-                       if (req == NULL) continue;
+       len = g_queue_get_length(g_manage_raw_queue);
 
-                       if (strncmp(path, req->path, strlen(path)) == 0) {
-                               if (shutdown_channel) {
-                                       g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id));
+       for (i = 0; i < len; i++) {
+               req = (thumbRawReq *)g_queue_peek_nth(g_manage_raw_queue, i);
+               if (!req)
+                       continue;
 
-                                       g_io_channel_shutdown(req->channel, TRUE, NULL);
-                                       g_io_channel_unref(req->channel);
-                               }
-                               g_queue_pop_nth(g_request_queue, i);
+               if (req->request_id == request_id) {
+                       if (req->isRequested) {
+                               req->isCanceled = true;
+                       } else {
+                               g_queue_pop_nth(g_manage_raw_queue, i);
 
                                SAFE_FREE(req->path);
+                               SAFE_FREE(req->userData);
                                SAFE_FREE(req);
-
-                               break;
                        }
-               }
-               if (i == req_len) {
-//                     thumb_dbg("There's no %s in the queue", path);
+
+                       return MS_MEDIA_ERR_NONE;
                }
        }
 
-       return MS_MEDIA_ERR_NONE;
+       return MS_MEDIA_ERR_INTERNAL;
 }
 
-int __media_thumb_check_req_queue(const char *path)
+static bool __media_thumb_is_canceled(void)
 {
-       int req_len = 0, i;
+       thumbReq *req = (thumbReq *)g_queue_peek_head(g_manage_queue);
 
-       if (g_request_queue == NULL) return MS_MEDIA_ERR_NONE;
-       req_len = g_queue_get_length(g_request_queue);
+       if (!req)
+               return true;
 
-//     thumb_dbg("Queue length : %d", req_len);
-//     thumb_dbg("Queue path : %s", path);
-
-       if (req_len <= 0) {
-//             thumb_dbg("There is no request in the queue");
-       } else {
-
-               for (i = 0; i < req_len; i++) {
-                       thumbReq *req = NULL;
-                       req = (thumbReq *)g_queue_peek_nth(g_request_queue, i);
-                       if (req == NULL) continue;
+       return req->isCanceled;
+}
 
-                       if (strncmp(path, req->path, strlen(path)) == 0) {
-                               //thumb_dbg("Same Request - %s", path);
-                               return MS_MEDIA_ERR_INVALID_PARAMETER;
+static bool __media_thumb_is_canceled_for_raw(void)
+{
+       thumbRawReq *req = (thumbRawReq *)g_queue_peek_head(g_manage_raw_queue);
 
-                               break;
-                       }
-               }
-       }
+       if (!req)
+               return true;
 
-       return MS_MEDIA_ERR_NONE;
+       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 recv_msg_len = 0;
+       int remain_size = 0;
+       int recv_len = 0;
+       int recv_pos = 0;
        unsigned char *buf = NULL;
 
-       buf = (unsigned char*)malloc(header_size);
+       THUMB_MALLOC(buf, header_size);
+       thumb_retv_if(!buf, MS_MEDIA_ERR_OUT_OF_MEMORY);
 
-       if ((recv_msg_len = recv(sock, buf, header_size, 0)) <= 0) {
-               thumb_stderror("recv failed");
-               SAFE_FREE(buf);
-               return _media_thumb_get_error();
-       }
-
-       memcpy(msg, buf, header_size);
-       //thumb_dbg("origin_path_size : %d, dest_path_size : %d", msg->origin_path_size, msg->dest_path_size);
-
-       SAFE_FREE(buf);
-
-       buf = (unsigned char*)malloc(msg->origin_path_size);
-
-       if ((recv_msg_len = recv(sock, buf, msg->origin_path_size, 0)) < 0) {
-               thumb_err("recvfrom failed : %s", strerror(errno));
-               SAFE_FREE(buf);
-               return _media_thumb_get_error();
-       }
-
-       strncpy(msg->org_path, (char*)buf, msg->origin_path_size);
-       //thumb_dbg("original path : %s", msg->org_path);
-
-       SAFE_FREE(buf);
-       buf = (unsigned char*)malloc(msg->dest_path_size);
-
-       if ((recv_msg_len = recv(sock, buf, msg->dest_path_size, 0)) < 0) {
-               thumb_err("recvfrom failed : %s", strerror(errno));
-               SAFE_FREE(buf);
-               return _media_thumb_get_error();
+       while (header_size > 0) {
+               if ((recv_len = recv(sock, buf + recv_pos, header_size, 0)) < 0) {
+                       thumb_stderror("recv failed");
+                       SAFE_FREE(buf);
+                       return MS_MEDIA_ERR_IPC;
+               }
+               header_size -= recv_len;
+               recv_pos += recv_len;
        }
 
-       strncpy(msg->dst_path, (char*)buf, msg->dest_path_size);
-       //thumb_dbg("destination path : %s", msg->dst_path);
+       header_size = recv_pos;
+       recv_pos = 0;
 
+       memcpy(msg, buf, header_size);
        SAFE_FREE(buf);
-       return MS_MEDIA_ERR_NONE;
-}
-
-int
-_media_thumb_recv_udp_msg(int sock, int header_size, thumbMsg *msg, struct sockaddr_un *from_addr, unsigned int *from_size)
-{
-       int recv_msg_len = 0;
-       unsigned int from_addr_size = sizeof(struct sockaddr_un);
-       unsigned char *buf = NULL;
 
-       buf = (unsigned char*)malloc(sizeof(thumbMsg));
+       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 ((recv_msg_len = recvfrom(sock, buf, sizeof(thumbMsg), 0, (struct sockaddr *)from_addr, &from_addr_size)) < 0) {
-               thumb_stderror("recvform failed");
-               SAFE_FREE(buf);
-               return _media_thumb_get_error();
-       }
+       remain_size = msg->thumb_size;
+       THUMB_MALLOC(buf, remain_size);
+       thumb_retv_if(!buf, MS_MEDIA_ERR_OUT_OF_MEMORY);
 
-       memcpy(msg, buf, header_size);
+       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;
+               }
 
-       if (msg->origin_path_size <= 0  || msg->origin_path_size > MAX_PATH_SIZE) {
-               SAFE_FREE(buf);
-               thumb_err("msg->origin_path_size is invalid %d", msg->origin_path_size );
-               return MS_MEDIA_ERR_INVALID_PARAMETER;
+               fsync(sock);
+               remain_size -= recv_len;
+               recv_pos += recv_len;
        }
 
-       strncpy(msg->org_path, (char*)buf + header_size, msg->origin_path_size);
-       //thumb_dbg("original path : %s", msg->org_path);
-
-       if (msg->dest_path_size <= 0  || msg->dest_path_size > MAX_PATH_SIZE) {
+       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);
-               thumb_err("msg->origin_path_size is invalid %d", msg->dest_path_size );
-               return MS_MEDIA_ERR_INVALID_PARAMETER;
+               return MS_MEDIA_ERR_OUT_OF_MEMORY;
        }
 
-       strncpy(msg->dst_path, (char*)buf + header_size + msg->origin_path_size, msg->dest_path_size);
-       //thumb_dbg("destination path : %s", msg->dst_path);
-
        SAFE_FREE(buf);
-       *from_size = from_addr_size;
 
        return MS_MEDIA_ERR_NONE;
 }
 
-int
-_media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
+int _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
 {
-       if (req_msg == NULL || buf == NULL) {
+       if (req_msg == NULL || buf == NULL)
                return MS_MEDIA_ERR_INVALID_PARAMETER;
-       }
 
-       int org_path_len = 0;
-       int dst_path_len = 0;
+       int thumb_data_len = 0;
        int size = 0;
        int header_size = 0;
 
-       header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2;
-       org_path_len = strlen(req_msg->org_path) + 1;
-       dst_path_len = strlen(req_msg->dst_path) + 1;
-
-       //thumb_dbg("Basic Size : %d, org_path : %s[%d], dst_path : %s[%d]", header_size, req_msg->org_path, org_path_len, req_msg->dst_path, dst_path_len);
+       header_size = sizeof(thumbMsg) - sizeof(unsigned char *);
+       thumb_data_len = req_msg->thumb_size;
+       if (thumb_data_len < 0)
+               return MS_MEDIA_ERR_INVALID_PARAMETER;
 
-       size = header_size + org_path_len + dst_path_len;
-       *buf = malloc(size);
+       size = header_size + thumb_data_len;
+       THUMB_MALLOC(*buf, size);
+       if (*buf == NULL) {
+               *buf_size = 0;
+               return 0;
+       }
        memcpy(*buf, req_msg, header_size);
-       memcpy((*buf)+header_size, req_msg->org_path, org_path_len);
-       memcpy((*buf)+header_size + org_path_len, req_msg->dst_path, dst_path_len);
+       if (thumb_data_len > 0)
+               memcpy((*buf) + header_size, req_msg->thumb_data, thumb_data_len);
 
        *buf_size = size;
 
        return MS_MEDIA_ERR_NONE;
 }
 
-int
-_media_thumb_request(int msg_type, media_thumb_type thumb_type, const char *origin_path, char *thumb_path, int max_length, media_thumb_info *thumb_info, uid_t uid)
+static void __media_thumb_pop(void)
 {
-       int sock;
-       struct sockaddr_un serv_addr;
-       int recv_str_len = 0;
-       int err = MS_MEDIA_ERR_NONE;
-       int pid;
-
-       if (ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock, MS_THUMB_CREATOR_PORT) < 0) {
-               thumb_err("ms_ipc_create_client_socket failed");
-               return MS_MEDIA_ERR_SOCKET_CONN;
-       }
-
-       memset(&serv_addr, 0, sizeof(serv_addr));
-       serv_addr.sun_family = AF_UNIX;
-       strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcreator.socket");
-
-       /* Connecting to the thumbnail server */
-       if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
-               thumb_stderror("connect");
-               return MS_MEDIA_ERR_SOCKET_CONN;
-       }
+       int len = 0;
+       int sock = 0;
+       thumbReq *req = NULL;
+       GSource *source_id = NULL;
 
-       thumbMsg req_msg;
-       thumbMsg recv_msg;
+       if (!g_manage_queue)
+               return;
 
-       memset((void *)&req_msg, 0, sizeof(thumbMsg));
-       memset((void *)&recv_msg, 0, sizeof(thumbMsg));
-
-       /* Get PID of client*/
-       pid = getpid();
-       req_msg.pid = pid;
+       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);
 
-       /* Set requset message */
-       req_msg.msg_type = msg_type;
-       req_msg.thumb_type = thumb_type;
-       req_msg.uid = uid;
-       strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
-       req_msg.org_path[strlen(req_msg.org_path)] = '\0';
+               g_io_channel_shutdown(req->channel, TRUE, NULL);
+               g_io_channel_unref(req->channel);
+               close(sock);
+               if (source_id)
+                       g_source_destroy(source_id);
 
-       if (msg_type == THUMB_REQUEST_SAVE_FILE) {
-               strncpy(req_msg.dst_path, thumb_path, sizeof(req_msg.dst_path));
-               req_msg.dst_path[strlen(req_msg.dst_path)] = '\0';
+               SAFE_FREE(req->path);
+               SAFE_FREE(req->userData);
+               SAFE_FREE(req);
        }
 
-       req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
-       req_msg.dest_path_size = strlen(req_msg.dst_path) + 1;
+       /* Check manage queue */
+       len = g_queue_get_length(g_manage_queue);
+       if (len > 0)
+               __media_thumb_send_request();
+}
 
-       if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
-               thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
-               close(sock);
-               return MS_MEDIA_ERR_INVALID_PARAMETER;
-       }
+static void __media_thumb_pop_raw_data(void)
+{
+       int len = 0;
+       int sock = 0;
+       thumbRawReq *req = NULL;
+       GSource *source_id = NULL;
 
-       unsigned char *buf = NULL;
-       int buf_size = 0;
-       int header_size = 0;
+       if (!g_manage_raw_queue)
+               return;
 
-       header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2;
-       _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
+       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);
 
-       if (send(sock, buf, buf_size, 0) != buf_size) {
-               thumb_err("sendto failed: %d", errno);
-               SAFE_FREE(buf);
+               g_io_channel_shutdown(req->channel, TRUE, NULL);
+               g_io_channel_unref(req->channel);
                close(sock);
-               return MS_MEDIA_ERR_SOCKET_SEND;
-       }
+               if (source_id)
+                       g_source_destroy(source_id);
 
-       thumb_dbg("Sending msg to thumbnail daemon is successful");
+               SAFE_FREE(req->path);
+               SAFE_FREE(req->userData);
+               SAFE_FREE(req);
+       }
 
-       SAFE_FREE(buf);
+       /* Check manage queue */
+       len = g_queue_get_length(g_manage_raw_queue);
+       if (len > 0)
+               __media_thumb_raw_data_send_request();
+}
 
-       if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
-               thumb_err("_media_thumb_recv_msg failed ");
-               close(sock);
-               return err;
-       }
+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;
 
-       recv_str_len = strlen(recv_msg.org_path);
-       thumb_dbg_slog("recv %s(%d) from thumb daemon is successful", recv_msg.org_path, recv_str_len);
+       memset((void *)&recv_msg, 0, sizeof(thumbMsg));
+       sock = g_io_channel_unix_get_fd(src);
 
-       close(sock);
+       header_size = sizeof(thumbMsg) - sizeof(unsigned char *);
 
-       if (recv_str_len > max_length) {
-               thumb_err("user buffer is too small. Output's length is %d", recv_str_len);
-               return MS_MEDIA_ERR_INVALID_PARAMETER;
+       if ((err = __media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
+               thumb_err("_media_thumb_recv_msg failed ");
+               goto NEXT;
        }
 
-       if (recv_msg.status == THUMB_FAIL) {
-               thumb_err("Failed to make thumbnail");
-               return MS_MEDIA_ERR_INVALID_PARAMETER;
+       if (recv_msg.status != MS_MEDIA_ERR_NONE) {
+               err = recv_msg.status;
+               thumb_err("Failed to make thumbnail (%d)", err);
        }
 
-       if (msg_type != THUMB_REQUEST_SAVE_FILE) {
-               strncpy(thumb_path, recv_msg.dst_path, max_length);
-       }
+       if (__media_thumb_is_canceled() || !data)
+               goto NEXT;
 
-       thumb_info->origin_width = recv_msg.origin_width;
-       thumb_info->origin_height = recv_msg.origin_height;
+       userdata = (thumbUserData*)data;
+       if (userdata->func)
+               userdata->func(err, recv_msg.dst_path, userdata->user_data);
 
-       return MS_MEDIA_ERR_NONE;
-}
+       thumb_dbg("Done");
+NEXT:
+       __media_thumb_pop();
 
-static int _mkdir(const char *dir, mode_t mode) {
-        char tmp[256];
-        char *p = NULL;
-        size_t len;
-
-        snprintf(tmp, sizeof(tmp),"%s",dir);
-        len = strlen(tmp);
-        if(tmp[len - 1] == '/')
-                tmp[len - 1] = 0;
-        for(p = tmp + 1; *p; p++)
-                if(*p == '/') {
-                        *p = 0;
-                        mkdir(tmp, mode);
-                        *p = '/';
-                }
-        return mkdir(tmp, mode);
+       return G_SOURCE_REMOVE;
 }
 
-static char* _media_thumb_get_default_path(uid_t uid)
+static gboolean __media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
 {
-       char *result_psswd = NULL;
-       struct group *grpinfo = NULL;
-       if(uid == getuid())
-       {
-               result_psswd = strdup(THUMB_DEFAULT_PATH);
-               grpinfo = getgrnam("users");
-               if(grpinfo == NULL) {
-                       thumb_err("getgrnam(users) returns NULL !");
-                       return NULL;
-               }
-       }
-       else
-       {
-               struct passwd *userinfo = getpwuid(uid);
-               if(userinfo == NULL) {
-                       thumb_err("getpwuid(%d) returns NULL !", uid);
-                       return NULL;
-               }
-               grpinfo = getgrnam("users");
-               if(grpinfo == NULL) {
-                       thumb_err("getgrnam(users) returns NULL !");
-                       return NULL;
-               }
-               // Compare git_t type and not group name
-               if (grpinfo->gr_gid != userinfo->pw_gid) {
-                       thumb_err("UID [%d] does not belong to 'users' group!", uid);
-                       return NULL;
-               }
-               asprintf(&result_psswd, "%s/data/file-manager-service/.thumb/phone", userinfo->pw_dir);
-       }
-
-       /* create dir */
-       _mkdir(result_psswd,S_IRWXU | S_IRWXG | S_IRWXO);
-
-       return result_psswd;
-}
+       thumbMsg recv_msg;
+       int header_size = 0;
+       int sock = 0;
+       int err = MS_MEDIA_ERR_NONE;
+       thumbRawUserData *userdata = NULL;
 
-int _media_thumb_process(thumbMsg *req_msg, thumbMsg *res_msg, uid_t uid)
-{
-       int err = -1;
-       GdkPixbuf *gdkdata = NULL;
-       int thumb_size = 0;
-       int thumb_w = 0;
-       int thumb_h = 0;
-       int origin_w = 0;
-       int origin_h = 0;
-       int max_length = 0;
-       char *thumb_path = NULL;
-       int need_update_db = 0;
-       int alpha = 0;
-
-       if (req_msg == NULL || res_msg == NULL) {
-               thumb_err("Invalid msg!");
-               return MS_MEDIA_ERR_INVALID_PARAMETER;
-       }
+       memset((void *)&recv_msg, 0, sizeof(thumbMsg));
+       sock = g_io_channel_unix_get_fd(src);
 
-       int msg_type = req_msg->msg_type;
-       media_thumb_type thumb_type = req_msg->thumb_type;
-       const char *origin_path = req_msg->org_path;
+       header_size = sizeof(thumbMsg) - sizeof(unsigned char *);
 
-       // Currently, The color space that is supported by the gdk-pixbuf is only RGB.
-       media_thumb_format thumb_format = MEDIA_THUMB_RGB888;
+       thumb_err("_media_thumb_write_socket socket : %d", sock);
 
-       thumb_path = res_msg->dst_path;
-       thumb_path[0] = '\0';
-       max_length = sizeof(res_msg->dst_path);
+       if ((err = __media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
+               thumb_err("_media_thumb_recv_msg failed ");
+               goto NEXT;
+       }
 
-       err = _media_thumb_db_connect(uid);
-       if (err != MS_MEDIA_ERR_NONE) {
-               thumb_err("_media_thumb_mb_svc_connect failed: %d", err);
-               return MS_MEDIA_ERR_DB_CONNECT_FAIL;
+       if (recv_msg.status != MS_MEDIA_ERR_NONE) {
+               err = recv_msg.status;
+               thumb_err("Failed to make thumbnail (%d)", err);
        }
 
-       if (msg_type == THUMB_REQUEST_DB_INSERT) {
-               err = _media_thumb_get_thumb_from_db_with_size(origin_path, thumb_path, max_length, &need_update_db, &origin_w, &origin_h);
-               if (err == MS_MEDIA_ERR_NONE) {
-                       res_msg->origin_width = origin_w;
-                       res_msg->origin_height = origin_h;
-                       _media_thumb_db_disconnect();
-                       return MS_MEDIA_ERR_NONE;
-               } else {
-                       if (strlen(thumb_path) == 0) {
-                               err = _media_thumb_get_hash_name(origin_path, thumb_path, max_length,uid);
-                               if (err != MS_MEDIA_ERR_NONE) {
-                                       thumb_err("_media_thumb_get_hash_name failed - %d\n", err);
-                                       strncpy(thumb_path, _media_thumb_get_default_path(uid), max_length);
-                                       _media_thumb_db_disconnect();
-                                       return err;
-                               }
-
-                               thumb_path[strlen(thumb_path)] = '\0';
-                       }
-               }
+       if (__media_thumb_is_canceled_for_raw() || !data)
+               goto NEXT;
 
-       } else if (msg_type == THUMB_REQUEST_SAVE_FILE) {
-               strncpy(thumb_path, req_msg->dst_path, max_length);
+       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);
 
-       } else if (msg_type == THUMB_REQUEST_ALL_MEDIA) {
-               err = _media_thumb_get_hash_name(origin_path, thumb_path, max_length,uid);
-               if (err < 0) {
-                       thumb_err("_media_thumb_get_hash_name failed - %d\n", err);
-                       strncpy(thumb_path, _media_thumb_get_default_path(uid), max_length);
-                       _media_thumb_db_disconnect();
-                       return err;
-               }
+       thumb_dbg("Done");
+NEXT:
+       __media_thumb_pop_raw_data();
 
-               thumb_path[strlen(thumb_path)] = '\0';
-       }
+       return G_SOURCE_REMOVE;
+}
 
-       if (g_file_test(thumb_path, 
-                                       G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
-               thumb_warn("thumb path already exists in file system.. remove the existed file");
-               _media_thumb_remove_file(thumb_path);
-       }
+static int __media_thumb_send_request(void)
+{
+       int err = MS_MEDIA_ERR_NONE;
+       int sock = -1;
+       struct sockaddr_un serv_addr;
+       thumbReq *req_manager = NULL;
+       int pid;
 
-       err = _thumbnail_get_data(origin_path, thumb_type, thumb_format, &gdkdata, &thumb_size, &thumb_w, &thumb_h, &origin_w, &origin_h, &alpha, uid);
-       if (err != MS_MEDIA_ERR_NONE) {
-               thumb_err("_thumbnail_get_data failed - %d\n", err);
-               if ( gdkdata != NULL ){
-                       g_object_unref(gdkdata);
-               }
-               strncpy(thumb_path, _media_thumb_get_default_path(uid), max_length);
-               _media_thumb_db_disconnect();
-               return err;
-       }
+       err = ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock);
+       thumb_retvm_if(err != MS_MEDIA_ERR_NONE, err, "ms_ipc_create_client_socket failed");
 
-       //thumb_dbg("Size : %d, W:%d, H:%d", thumb_size, thumb_w, thumb_h);
-       //thumb_dbg("Origin W:%d, Origin H:%d\n", origin_w, origin_h);
-       //thumb_dbg("Thumb : %s", thumb_path);
-
-       res_msg->msg_type = THUMB_RESPONSE;
-       res_msg->thumb_size = thumb_size;
-       res_msg->thumb_width = thumb_w;
-       res_msg->thumb_height = thumb_h;
-       res_msg->origin_width = origin_w;
-       res_msg->origin_height = origin_h;
-
-       /* If the image is transparent PNG format, make png file as thumbnail of this image */
-       if (alpha) {
-               char file_ext[10];
-               err = _media_thumb_get_file_ext(origin_path, file_ext, sizeof(file_ext));
-               if (strncasecmp(file_ext, "png", 3) == 0) {
-                       int len = strlen(thumb_path);
-                       thumb_path[len - 3] = 'p';
-                       thumb_path[len - 2] = 'n';
-                       thumb_path[len - 1] = 'g';
-               }
-               thumb_dbg("Thumb path is changed : %s", thumb_path);
-       }
+       memset(&serv_addr, 0, sizeof(serv_addr));
+       serv_addr.sun_family = AF_UNIX;
+       SAFE_STRLCPY(serv_addr.sun_path, THUMB_IPC_PATH, sizeof(serv_addr.sun_path));
 
-       err = _media_thumb_save_to_file_with_gdk(gdkdata, thumb_w, thumb_h, alpha, thumb_path);
-       if (err < 0) {
-               thumb_err("save_to_file_with_gdk failed - %d\n", err);
-               if ( gdkdata != NULL ){
-                       g_object_unref(gdkdata);
-               }
+       GIOChannel *channel = NULL;
+       channel = g_io_channel_unix_new(sock);
+       int source_id = -1;
 
-               if (msg_type == THUMB_REQUEST_DB_INSERT || msg_type == THUMB_REQUEST_ALL_MEDIA)
-                       strncpy(thumb_path, _media_thumb_get_default_path(uid), max_length);
+       /* Connecting to the thumbnail server */
+       if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
+               thumb_stderror("connect");
+               if (errno == EACCES)
+                       err = MS_MEDIA_ERR_PERMISSION_DENIED;
+               else
+                       err = MS_MEDIA_ERR_IPC;
 
-               _media_thumb_db_disconnect();
-               return err;
-       } else {
-               thumb_dbg("file save success\n");
-       }
+               g_io_channel_shutdown(channel, TRUE, NULL);
+               g_io_channel_unref(channel);
+               close(sock);
 
-       /* fsync */
-       int fd = 0;
-       fd = open(thumb_path, O_WRONLY);
-       if (fd < 0) {
-               thumb_warn("open failed");
-       } else {
-               err = fsync(fd);
-               if (err == -1) {
-                       thumb_warn("fsync failed");
-               }
-               close(fd);
+               return err;
        }
-       /* End of fsync */
-
-       g_object_unref(gdkdata);
 
-       /* DB update if needed */
-       if (need_update_db == 1) {
-               err = _media_thumb_update_db(origin_path, thumb_path, res_msg->origin_width, res_msg->origin_height, uid);
-               if (err != MS_MEDIA_ERR_NONE) {
-                       thumb_err("_media_thumb_update_db failed : %d", err);
-               }
+       req_manager = (thumbReq *)g_queue_peek_head(g_manage_queue);
+       if (!req_manager) {
+               thumb_err("queue peek fail");
+               g_io_channel_shutdown(channel, TRUE, NULL);
+               g_io_channel_unref(channel);
+               close(sock);
+               return MS_MEDIA_ERR_INVALID_PARAMETER;
        }
 
-       _media_thumb_db_disconnect();
-
-       return MS_MEDIA_ERR_NONE;
-}
+       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);
+       source_id = g_source_attach(source, g_main_context_get_thread_default());
 
-gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
-{
-       thumbMsg recv_msg;
-       int header_size = 0;
-       int recv_str_len = 0;
-       int sock = 0;
-       int err = MS_MEDIA_ERR_NONE;
-
-       memset((void *)&recv_msg, 0, sizeof(thumbMsg));
-       sock = g_io_channel_unix_get_fd(src);
+       thumbMsg req_msg;
+       memset((void *)&req_msg, 0, sizeof(thumbMsg));
 
-       header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2;
+       pid = getpid();
+       req_msg.pid = pid;
+       req_msg.msg_type = req_manager->msg_type;
+       req_msg.request_id = 0;
+       req_msg.uid = req_manager->uid;
+       SAFE_STRLCPY(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
+       req_msg.dst_path[0] = '\0';
+       req_msg.thumb_size = 0;
 
-       thumb_err("_media_thumb_write_socket socket : %d", sock);
+       unsigned char *buf = NULL;
+       int buf_size = 0;
+       _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
 
-       if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
-               thumb_err("_media_thumb_recv_msg failed ");
-               g_io_channel_shutdown(src, TRUE, NULL);
-               return FALSE;
+       if (send(sock, buf, buf_size, 0) < 0) {
+               thumb_err("send failed: %d", errno);
+               SAFE_FREE(buf);
+               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);
+               close(sock);
+               return MS_MEDIA_ERR_IPC;
        }
 
-       recv_str_len = strlen(recv_msg.dst_path);
-       thumb_dbg("recv %s(%d) in  [ %s ] from thumb daemon is successful", recv_msg.dst_path, recv_str_len, recv_msg.org_path);
-
-       g_io_channel_shutdown(src, TRUE, NULL);
-
-       //thumb_dbg("Completed..%s", recv_msg.org_path);
-       __media_thumb_pop_req_queue(recv_msg.org_path, FALSE);
-
-       if (recv_msg.status == THUMB_FAIL) {
-               thumb_err("Failed to make thumbnail");
-               err = MS_MEDIA_ERR_INTERNAL;
-               goto callback;
-       }
+       SAFE_FREE(buf);
+       thumb_dbg("Sending msg to thumbnail daemon is successful");
 
-callback:
-       if (data) {
-               thumbUserData* cb = (thumbUserData*)data;
-               cb->func(err, recv_msg.dst_path, cb->user_data);
-               free(cb);
-               cb = NULL;
+       if (req_manager->msg_type == THUMB_REQUEST_DB_INSERT) {
+               req_manager->channel = channel;
+               req_manager->source_id = source_id;
+               req_manager->isRequested = true;
        }
 
-       thumb_dbg("Done");
-       return FALSE;
+       return err;
 }
 
-int
-_media_thumb_request_async(int msg_type, media_thumb_type thumb_type, const char *origin_path, thumbUserData *userData, uid_t uid)
+static int __media_thumb_raw_data_send_request(void)
 {
-       int sock;
+       int err = MS_MEDIA_ERR_NONE;
+       int sock = -1;
        struct sockaddr_un serv_addr;
+       thumbRawReq *req_manager = NULL;
        int pid;
 
-       if ((msg_type == THUMB_REQUEST_DB_INSERT) && (__media_thumb_check_req_queue(origin_path) < 0)) {
-               return MS_MEDIA_ERR_THUMB_DUPLICATED_REQUEST;
-       }
-
-       if (ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock, MS_THUMB_CREATOR_PORT) < 0) {
-               thumb_err("ms_ipc_create_client_socket failed");
-               return MS_MEDIA_ERR_SOCKET_CONN;
-       }
-       GIOChannel *channel = NULL;
-       channel = g_io_channel_unix_new(sock);
-       int source_id = -1;
-
+       err = ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock);
+       thumb_retvm_if(err != MS_MEDIA_ERR_NONE, err, "ms_ipc_create_client_socket failed");
 
        memset(&serv_addr, 0, sizeof(serv_addr));
        serv_addr.sun_family = AF_UNIX;
+       SAFE_STRLCPY(serv_addr.sun_path, THUMB_IPC_PATH, sizeof(serv_addr.sun_path));
 
-       strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcreator.socket");
+       GIOChannel *channel = NULL;
+       channel = g_io_channel_unix_new(sock);
+       int source_id = -1;
 
        /* Connecting to the thumbnail server */
        if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
-               thumb_err("connect error : %s", strerror(errno));
+               thumb_stderror("connect error");
+               if (errno == EACCES)
+                       err = MS_MEDIA_ERR_PERMISSION_DENIED;
+               else
+                       err = MS_MEDIA_ERR_IPC;
+
                g_io_channel_shutdown(channel, TRUE, NULL);
-               return MS_MEDIA_ERR_SOCKET_CONN;
+               g_io_channel_unref(channel);
+               close(sock);
+               return err;
        }
 
-       if (msg_type != THUMB_REQUEST_CANCEL_MEDIA) {
-               //source_id = g_io_add_watch(channel, G_IO_IN, _media_thumb_write_socket, userData );
-
-               /* Create new channel to watch udp socket */
-               GSource *source = NULL;
-               source = g_io_create_watch(channel, G_IO_IN);
+       req_manager = (thumbRawReq *)g_queue_peek_head(g_manage_raw_queue);
 
-               /* Set callback to be called when socket is readable */
-               g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, userData, NULL);
-               source_id = g_source_attach(source, g_main_context_get_thread_default());
+       if (!req_manager) {
+               thumb_err("queue peek fail");
+               g_io_channel_shutdown(channel, TRUE, NULL);
+               g_io_channel_unref(channel);
+               close(sock);
+               return MS_MEDIA_ERR_INVALID_PARAMETER;
        }
 
+       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);
+       source_id = g_source_attach(source, g_main_context_get_thread_default());
+
        thumbMsg req_msg;
        memset((void *)&req_msg, 0, sizeof(thumbMsg));
 
        pid = getpid();
        req_msg.pid = pid;
-       req_msg.msg_type = msg_type;
-       req_msg.thumb_type = thumb_type;
-       req_msg.uid = uid;
-
-       strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
-       req_msg.org_path[strlen(req_msg.org_path)] = '\0';
+       req_msg.msg_type = req_manager->msg_type;
+       req_msg.request_id = req_manager->request_id;
+       req_msg.thumb_width = req_manager->width;
+       req_msg.thumb_height = req_manager->height;
+       req_msg.uid = req_manager->uid;
 
-       req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
-       req_msg.dest_path_size = strlen(req_msg.dst_path) + 1;
-
-       if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
-               thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
-               g_io_channel_shutdown(channel, TRUE, NULL);
-               return MS_MEDIA_ERR_INVALID_PARAMETER;
-       }
+       SAFE_STRLCPY(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
+       req_msg.dst_path[0] = '\0';
+       req_msg.thumb_size = 0;
 
        unsigned char *buf = NULL;
        int buf_size = 0;
        _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
 
-       if (send(sock, buf, buf_size, 0) != buf_size) {
-               thumb_err("sendto failed: %d", errno);
+       if (send(sock, buf, buf_size, 0) < 0) {
+               thumb_err("send failed: %d", errno);
                SAFE_FREE(buf);
                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);
-               return MS_MEDIA_ERR_SOCKET_SEND;
+               g_io_channel_unref(channel);
+               return MS_MEDIA_ERR_IPC;
        }
 
        SAFE_FREE(buf);
-       thumb_dbg("Sending msg to thumbnail daemon is successful");
 
-#if 0
-       if (msg_type == THUMB_REQUEST_CANCEL_MEDIA) {
-               g_io_channel_shutdown(channel, TRUE, NULL);
+       if (req_manager->msg_type == THUMB_REQUEST_RAW_DATA) {
+               req_manager->channel = channel;
+               req_manager->source_id = source_id;
+               req_manager->isRequested = true;
        }
-#else
-       if (msg_type == THUMB_REQUEST_CANCEL_MEDIA) {
-               g_io_channel_shutdown(channel, TRUE, NULL);
+       return MS_MEDIA_ERR_NONE;
+}
 
-               //thumb_dbg("Cancel : %s[%d]", origin_path, sock);
-               __media_thumb_pop_req_queue(origin_path, TRUE);
-       } else if (msg_type == THUMB_REQUEST_DB_INSERT) {
-               if (g_request_queue == NULL) {
-                       g_request_queue = g_queue_new();
-               }
+int _media_thumb_request_async(int msg_type, unsigned int request_id, const char *origin_path, thumbUserData *userData, uid_t uid)
+{
+       int err = MS_MEDIA_ERR_NONE;
+       int len = 0;
 
-               thumbReq *thumb_req = NULL;
-               thumb_req = calloc(1, sizeof(thumbReq));
-               if (thumb_req == NULL) {
-                       thumb_err("Failed to create request element");
-                       return MS_MEDIA_ERR_NONE;
-               }
+       if (msg_type == THUMB_REQUEST_CANCEL_MEDIA)
+               return __media_thumb_cancel(request_id);
+
+       if (g_manage_queue == NULL)
+               g_manage_queue = g_queue_new();
+
+       thumbReq *thumb_req = NULL;
+       THUMB_MALLOC(thumb_req, sizeof(thumbReq));
+       thumb_retvm_if(thumb_req == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Failed to create request element");
+
+       thumb_req->msg_type = msg_type;
+       thumb_req->path = g_strdup(origin_path);
+       thumb_req->userData = userData;
+       thumb_req->isCanceled = false;
+       thumb_req->isRequested = false;
+       thumb_req->request_id = request_id;
+       thumb_req->uid = uid;
+
+       len = g_queue_get_length(g_manage_queue);
+       g_queue_push_tail(g_manage_queue, (gpointer)thumb_req);
 
-               thumb_req->channel = channel;
-               thumb_req->path = strdup(origin_path);
-               thumb_req->source_id = source_id;
+       if (len == 0)
+               err = __media_thumb_send_request();
 
-               //thumb_dbg("Push : %s [%d]", origin_path, sock);
-               g_queue_push_tail(g_request_queue, (gpointer)thumb_req);
+       return err;
+}
+
+int _media_thumb_request_raw_data_async(int msg_type, int request_id, const char *origin_path, int width, int height, thumbRawUserData *userData, uid_t uid)
+{
+       int err = MS_MEDIA_ERR_NONE;
+       int len = 0;
+       thumbRawReq *thumb_req = NULL;
+
+       if (msg_type == THUMB_REQUEST_CANCEL_RAW_DATA)
+               return __media_thumb_cancel_raw_data(request_id);
+
+       if (g_manage_raw_queue == NULL)
+               g_manage_raw_queue = g_queue_new();
+
+       THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
+       if (thumb_req == NULL) {
+               thumb_err("Failed to create request element");
+               return MS_MEDIA_ERR_INVALID_PARAMETER;
        }
-#endif
 
-       return MS_MEDIA_ERR_NONE;
+       thumb_req->msg_type = msg_type;
+       thumb_req->request_id = request_id;
+       thumb_req->path = g_strdup(origin_path);
+       thumb_req->width = width;
+       thumb_req->height = height;
+       thumb_req->userData = userData;
+       thumb_req->isCanceled = false;
+       thumb_req->isRequested = false;
+       thumb_req->uid = uid;
+
+       len = g_queue_get_length(g_manage_raw_queue);
+       g_queue_push_tail(g_manage_raw_queue, (gpointer)thumb_req);
+
+       if (len == 0)
+               err = __media_thumb_raw_data_send_request();
+
+       return err;
 }