Merge thumbnail-util queue 82/142382/4
authorMinje Ahn <minje.ahn@samsung.com>
Fri, 4 Aug 2017 00:14:50 +0000 (09:14 +0900)
committerMinje Ahn <minje.ahn@samsung.com>
Fri, 4 Aug 2017 01:56:47 +0000 (10:56 +0900)
Change-Id: I6bf5e6c801def81bbc4221e768b8591da553307c
Signed-off-by: Minje Ahn <minje.ahn@samsung.com>
src/ipc/media-thumb-ipc.c

index 78aab2d..a897c18 100755 (executable)
@@ -35,7 +35,6 @@
 #define THUMB_IPC_PATH tzplatform_mkpath(TZ_SYS_RUN, "media-server/media_ipc_thumbcreator.socket")
 
 static GQueue *g_manage_queue = NULL;
-static GQueue *g_request_raw_queue = NULL;
 static GQueue *g_manage_raw_queue = NULL;
 
 
@@ -55,6 +54,7 @@ typedef struct {
        GIOChannel *channel;
        int msg_type;
        bool isCanceled;
+       bool isRequested;
        int request_id;
        int source_id;
        int width;
@@ -146,101 +146,72 @@ int __media_thumb_cancel(unsigned int request_id)
        return MS_MEDIA_ERR_NONE;
 }
 
-int __media_thumb_pop_raw_data_req_queue(int request_id, bool shutdown_channel)
+void __media_thumb_pop_raw_data()
 {
-       int req_len = 0, i;
-
-       req_len = g_queue_get_length(g_request_raw_queue);
-
-       if (req_len <= 0) {
-//             thumb_dbg("There is no request in the queue");
-       } else {
-
-               for (i = 0; i < req_len; i++) {
-                       thumbRawReq *req = NULL;
-                       req = (thumbRawReq *)g_queue_peek_nth(g_request_raw_queue, i);
-                       if (req == NULL) continue;
+       int len = 0;
 
-                       if (request_id == req->request_id) {
-                               if (shutdown_channel) {
-                                       GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
-                                       if (source_id != NULL) {
-                                               g_source_destroy(source_id);
-                                       } else {
-                                               thumb_err("G_SOURCE_ID is NULL");
-                                       }
-
-                                       g_io_channel_shutdown(req->channel, TRUE, NULL);
-                                       g_io_channel_unref(req->channel);
-                               }
-                               g_queue_pop_nth(g_request_raw_queue, i);
+       if (g_manage_raw_queue != NULL) {
+               thumbRawReq *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);
 
-                               SAFE_FREE(req->userData);
-                               SAFE_FREE(req);
+                       g_io_channel_shutdown(req->channel, TRUE, NULL);
+                       g_io_channel_unref(req->channel);
 
-                               break;
+                       if (source_id != NULL) {
+                               g_source_destroy(source_id);
+                       } else {
+                               thumb_err("G_SOURCE_ID is NULL");
                        }
-               }
-               if (i == req_len) {
-//                     thumb_dbg("There's no %s in the queue", path);
-               }
-       }
-
-       return MS_MEDIA_ERR_NONE;
-}
-
-int __media_thumb_check_raw_data_req_queue_for_cancel(int request_id)
-{
-       int req_len = 0;
-
-       req_len = g_queue_get_length(g_request_raw_queue);
-
-       if (req_len <= 0) {
-//             thumb_dbg("There is no request in the queue");
-       } else {
-               thumbRawReq *req = NULL;
-               req = (thumbRawReq *)g_queue_peek_head(g_request_raw_queue);
 
-               if (req != NULL && request_id == req->request_id) {
-                       req->isCanceled = true;
-                       return MS_MEDIA_ERR_NONE;
+                       SAFE_FREE(req->path);
+                       SAFE_FREE(req->userData);
+                       SAFE_FREE(req);
                }
-       }
 
-       return MS_MEDIA_ERR_INTERNAL;
+               /* Check manage queue */
+               len = g_queue_get_length(g_manage_raw_queue);
+               if (len > 0)
+                       _media_thumb_raw_data_send_request();
+       }
 }
 
-int __media_thumb_pop_raw_data_manage_queue(int request_id)
+int __media_thumb_cancel_raw_data(int request_id)
 {
-       int req_len = 0, i;
-       int flag = false;
-
-       req_len = g_queue_get_length(g_manage_raw_queue);
+       int len = 0, i;
+       bool flag = false;
 
-       if (req_len < 0) {
-//             thumb_dbg("There is no request in the queue");
-       } else {
+       if (g_manage_raw_queue != NULL) {
+               len = g_queue_get_length(g_manage_raw_queue);
 
-               for (i = 0; i < req_len; i++) {
+               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 (request_id == req->request_id) {
-                               g_queue_pop_nth(g_manage_raw_queue, i);
+                       if (req->request_id == request_id) {
+                               if (req->isRequested == true) {
+                                       req->isCanceled = true;
+                               } else {
+                                       g_queue_pop_nth(g_manage_raw_queue, i);
+
+                                       SAFE_FREE(req->path);
+                                       SAFE_FREE(req->userData);
+                                       SAFE_FREE(req);
+                               }
 
-                               SAFE_FREE(req->userData);
-                               SAFE_FREE(req);
                                flag = true;
+
                                break;
                        }
                }
-               if (!flag) {
-                       return __media_thumb_check_raw_data_req_queue_for_cancel(request_id);
-               }
        }
 
+       if (flag == false)
+               return MS_MEDIA_ERR_INTERNAL;
+
        return MS_MEDIA_ERR_NONE;
+
 }
 
 bool __media_thumb_check_cancel(void)
@@ -261,7 +232,7 @@ bool __media_thumb_check_cancel(void)
 bool __media_thumb_check_cancel_for_raw(void)
 {
        thumbRawReq *req = NULL;
-       req = (thumbRawReq *)g_queue_peek_head(g_request_raw_queue);
+       req = (thumbRawReq *)g_queue_peek_head(g_manage_raw_queue);
 
        if (req == NULL) {
                return false;
@@ -528,7 +499,6 @@ gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condit
        thumbMsg recv_msg;
        int header_size = 0;
        int sock = 0;
-       int len = -1;
        int err = MS_MEDIA_ERR_NONE;
 
        memset((void *)&recv_msg, 0, sizeof(thumbMsg));
@@ -540,18 +510,11 @@ gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condit
 
        if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
                thumb_err("_media_thumb_recv_msg failed ");
-               if (recv_msg.request_id > 0) {
-                       __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, TRUE);
-               } else {
-                       thumb_err("origin path size is wrong.");
-               }
+               __media_thumb_pop_raw_data();
 
                return FALSE;
        }
 
-       g_io_channel_shutdown(src, TRUE, NULL);
-       g_io_channel_unref(src);
-
        if (recv_msg.status != MS_MEDIA_ERR_NONE) {
                err = recv_msg.status;
                thumb_err("Failed to make thumbnail (%d)", err);
@@ -565,22 +528,10 @@ gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condit
                }
        }
 
-       __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, FALSE);
+       __media_thumb_pop_raw_data();
 
        thumb_dbg("Done");
 
-       /* Check manage queue */
-       if (g_manage_raw_queue) {
-               len = g_queue_get_length(g_manage_raw_queue);
-
-               if (len > 0) {
-                       _media_thumb_raw_data_send_request();
-               } else {
-                       g_queue_free(g_manage_raw_queue);
-                       g_manage_raw_queue = NULL;
-               }
-       }
-
        return FALSE;
 }
 
@@ -721,10 +672,10 @@ static int _media_thumb_raw_data_send_request()
                return err;
        }
 
-       req_manager = (thumbRawReq *)g_queue_pop_head(g_manage_raw_queue);
+       req_manager = (thumbRawReq *)g_queue_peek_head(g_manage_raw_queue);
 
        if (req_manager == NULL) {
-               thumb_err("queue pop fail");
+               thumb_err("queue peek fail");
                g_io_channel_shutdown(channel, TRUE, NULL);
                g_io_channel_unref(channel);
                close(sock);
@@ -777,21 +728,9 @@ static int _media_thumb_raw_data_send_request()
        SAFE_FREE(buf);
 
        if (req_manager->msg_type == THUMB_REQUEST_RAW_DATA) {
-               if (g_request_raw_queue == NULL) {
-                       g_request_raw_queue = g_queue_new();
-               }
-               thumbRawReq *thumb_req = NULL;
-               THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
-               if (thumb_req == NULL) {
-                       thumb_err("Failed to create request element");
-                       return MS_MEDIA_ERR_INVALID_PARAMETER;
-               }
-               thumb_req->channel = channel;
-               thumb_req->request_id = req_manager->request_id;
-               thumb_req->source_id = source_id;
-               thumb_req->userData = req_manager->userData;
-
-               g_queue_push_tail(g_request_raw_queue, (gpointer)thumb_req);
+               req_manager->channel = channel;
+               req_manager->source_id = source_id;
+               req_manager->isRequested = true;
        }
        return MS_MEDIA_ERR_NONE;
 }
@@ -831,106 +770,63 @@ int _media_thumb_request_async(int msg_type, unsigned int request_id, const char
 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 (g_manage_raw_queue == NULL) {
-               if (msg_type == THUMB_REQUEST_CANCEL_RAW_DATA)
-                       return MS_MEDIA_ERR_INTERNAL;
+       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();
 
-               thumbRawReq *thumb_req = NULL;
-               THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
-               if (thumb_req == NULL) {
-                       thumb_err("Failed to create request element");
-                       return MS_MEDIA_ERR_INVALID_PARAMETER;
-               }
+       THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
+       if (thumb_req == NULL) {
+               thumb_err("Failed to create request element");
+               return MS_MEDIA_ERR_INVALID_PARAMETER;
+       }
 
-               thumb_req->msg_type = msg_type;
-               thumb_req->request_id = request_id;
-               thumb_req->path = strdup(origin_path);
-               thumb_req->width = width;
-               thumb_req->height = height;
-               thumb_req->userData = userData;
-               thumb_req->isCanceled = false;
-               thumb_req->uid = uid;
+       thumb_req->msg_type = msg_type;
+       thumb_req->request_id = request_id;
+       thumb_req->path = 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;
 
-               g_queue_push_tail(g_manage_raw_queue, (gpointer)thumb_req);
+       len = g_queue_get_length(g_manage_raw_queue);
+       g_queue_push_tail(g_manage_raw_queue, (gpointer)thumb_req);
 
-               /* directly request at first time */
+       if (len == 0)
                err = _media_thumb_raw_data_send_request();
 
-       } else {
-               if (msg_type != THUMB_REQUEST_CANCEL_RAW_DATA) {
-                       /* Enqueue */
-                       thumbRawReq *thumb_req = NULL;
-                       THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
-                       if (thumb_req == NULL) {
-                               thumb_err("Failed to create request element");
-                               return MS_MEDIA_ERR_INVALID_PARAMETER;
-                       }
-
-                       thumb_req->msg_type = msg_type;
-                       thumb_req->request_id = request_id;
-                       thumb_req->path = strdup(origin_path);
-                       thumb_req->width = width;
-                       thumb_req->height = height;
-                       thumb_req->userData = userData;
-                       thumb_req->isCanceled = false;
-                       thumb_req->uid = uid;
-
-                       g_queue_push_tail(g_manage_raw_queue, (gpointer)thumb_req);
-
-               } else {
-                       /* Dequeue */
-                       err = __media_thumb_pop_raw_data_manage_queue(request_id);
-               }
-       }
-
        return err;
 }
 
 int _media_thumb_request_cancel_all()
 {
-       int i;
-       int req_len = -1;
-       int len = 0;
-       thumbRawReq *tmp_raw_req = NULL;
-
-       if (g_manage_raw_queue == NULL) {
-               thumb_err("manage_queue is NULL");
-               if (g_request_raw_queue != NULL) {
-                       /* Check request queue */
-                       len = g_queue_get_length(g_request_raw_queue);
-                       if (len > 0) {
-                               tmp_raw_req = g_queue_peek_head(g_request_raw_queue);
-                               if (tmp_raw_req != NULL)
-                                       tmp_raw_req->isCanceled = true;
-                               return MS_MEDIA_ERR_NONE;
+       if (g_manage_raw_queue != NULL) {
+               while (g_queue_get_length(g_manage_raw_queue) > 0) {
+                       thumbRawReq *req = (thumbRawReq *)g_queue_peek_tail(g_manage_raw_queue);
+                       if (req != NULL) {
+                               if (req->isRequested == true) {
+                                       req->isCanceled = true;
+                                       thumb_dbg("Last item!!");
+
+                                       break;
+                               } else {
+                                       g_queue_pop_tail(g_manage_raw_queue);
+
+                                       SAFE_FREE(req->path);
+                                       SAFE_FREE(req->userData);
+                                       SAFE_FREE(req);
+                               }
+                       } else {
+                               thumb_dbg("Queue is empty!!");
+                               break;
                        }
                }
-               thumb_err("request_queue is NULL");
-               return MS_MEDIA_ERR_INTERNAL;
-       }
-       req_len = g_queue_get_length(g_manage_raw_queue);
-       for (i = 0; i < req_len; i++) {
-               tmp_raw_req = g_queue_pop_tail(g_manage_raw_queue);
-               if (tmp_raw_req) {
-                       SAFE_FREE(tmp_raw_req->path);
-                       SAFE_FREE(tmp_raw_req->userData);
-                       SAFE_FREE(tmp_raw_req);
-                       tmp_raw_req = NULL;
-               }
-       }
-       g_queue_free(g_manage_raw_queue);
-       g_manage_raw_queue = NULL;
-       if (g_request_raw_queue != NULL) {
-               /* Check request queue */
-               len = g_queue_get_length(g_request_raw_queue);
-               if (len > 0) {
-                       tmp_raw_req = g_queue_peek_head(g_request_raw_queue);
-                       if (tmp_raw_req != NULL)
-                               tmp_raw_req->isCanceled = true;
-               }
        }
 
        return MS_MEDIA_ERR_NONE;