4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hyunjun Ko <zzoon.ko@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
22 #include "media-thumbnail.h"
23 #include "media-thumb-ipc.h"
24 #include "media-thumb-util.h"
25 #include "media-thumb-db.h"
26 #include "media-thumb-debug.h"
34 #define THUMB_SOCK_BLOCK_SIZE 512
35 #define THUMB_IPC_PATH tzplatform_mkpath(TZ_SYS_RUN, "media-server/media_ipc_thumbcreator.socket")
37 static GQueue *g_request_queue = NULL;
38 static GQueue *g_manage_queue = NULL;
39 static GQueue *g_request_raw_queue = NULL;
40 static GQueue *g_manage_raw_queue = NULL;
46 unsigned int request_id;
51 thumbUserData *userData;
64 thumbRawUserData *userData;
67 int _media_thumb_send_request();
68 int _media_thumb_raw_data_send_request();
71 int _media_thumb_get_error()
73 if (errno == EWOULDBLOCK) {
74 thumb_err("Timeout. Can't try any more");
75 return MS_MEDIA_ERR_SOCKET_RECEIVE_TIMEOUT;
77 thumb_stderror("recvfrom failed");
78 return MS_MEDIA_ERR_SOCKET_RECEIVE;
82 void __media_thumb_shutdown_channel(bool only_shutdown)
85 thumbReq *req = (thumbReq *)g_queue_peek_head(g_request_queue);
87 len = g_queue_get_length(g_manage_queue);
91 if (len == 0 && req->isCanceled == true) {
92 thumb_dbg("The manage queue will be released");
94 thumb_dbg("There is remain item in the manage queue");
98 GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
99 if (source_id != NULL) {
100 g_source_destroy(source_id);
102 thumb_err("G_SOURCE_ID is NULL");
105 g_io_channel_shutdown(req->channel, TRUE, NULL);
106 g_io_channel_unref(req->channel);
107 g_queue_pop_head(g_request_queue);
109 SAFE_FREE(req->path);
110 SAFE_FREE(req->userData);
113 if (g_manage_queue && len == 0) {
114 g_queue_free(g_manage_queue);
115 g_manage_queue = NULL;
120 int __media_thumb_pop_req_queue()
124 req_len = g_queue_get_length(g_request_queue);
127 // thumb_dbg("There is no request in the queue");
129 __media_thumb_shutdown_channel(true);
132 /* Check manage queue */
133 if (g_manage_queue) {
134 req_len = g_queue_get_length(g_manage_queue);
137 _media_thumb_send_request();
140 return MS_MEDIA_ERR_NONE;
143 int __media_thumb_check_req_queue_for_cancel(unsigned int request_id, const char *path)
147 req_len = g_queue_get_length(g_request_queue);
150 // thumb_dbg("There is no request in the queue");
152 thumbReq *req = NULL;
153 req = (thumbReq *)g_queue_peek_head(g_request_queue);
155 if (req != NULL && strncmp(path, req->path, strlen(path)) == 0 && request_id == req->request_id) {
156 req->isCanceled = true;
157 __media_thumb_shutdown_channel(false);
159 return MS_MEDIA_ERR_NONE;
163 return MS_MEDIA_ERR_INTERNAL;
166 int __media_thumb_pop_manage_queue(unsigned int request_id, const char *path)
171 req_len = g_queue_get_length(g_manage_queue);
174 // thumb_dbg("There is no request in the queue");
176 for (i = 0; i < req_len; i++) {
177 thumbReq *req = NULL;
178 req = (thumbReq *)g_queue_peek_nth(g_manage_queue, i);
179 if (req == NULL) continue;
181 if (strncmp(path, req->path, strlen(path)) == 0 && request_id == req->request_id) {
182 g_queue_pop_nth(g_manage_queue, i);
184 SAFE_FREE(req->path);
185 SAFE_FREE(req->userData);
192 return __media_thumb_check_req_queue_for_cancel(request_id, path);
194 __media_thumb_shutdown_channel(false);
196 return MS_MEDIA_ERR_NONE;
200 return MS_MEDIA_ERR_NONE;
204 int __media_thumb_pop_raw_data_req_queue(int request_id, bool shutdown_channel)
208 req_len = g_queue_get_length(g_request_raw_queue);
211 // thumb_dbg("There is no request in the queue");
214 for (i = 0; i < req_len; i++) {
215 thumbRawReq *req = NULL;
216 req = (thumbRawReq *)g_queue_peek_nth(g_request_raw_queue, i);
217 if (req == NULL) continue;
219 if (request_id == req->request_id) {
220 if (shutdown_channel) {
221 GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
222 if (source_id != NULL) {
223 g_source_destroy(source_id);
225 thumb_err("G_SOURCE_ID is NULL");
228 g_io_channel_shutdown(req->channel, TRUE, NULL);
229 g_io_channel_unref(req->channel);
231 g_queue_pop_nth(g_request_raw_queue, i);
233 SAFE_FREE(req->userData);
240 // thumb_dbg("There's no %s in the queue", path);
244 return MS_MEDIA_ERR_NONE;
247 int __media_thumb_check_raw_data_req_queue_for_cancel(int request_id)
251 req_len = g_queue_get_length(g_request_raw_queue);
254 // thumb_dbg("There is no request in the queue");
256 thumbRawReq *req = NULL;
257 req = (thumbRawReq *)g_queue_peek_head(g_request_raw_queue);
259 if (req != NULL && request_id == req->request_id) {
260 req->isCanceled = true;
261 return MS_MEDIA_ERR_NONE;
265 return MS_MEDIA_ERR_INTERNAL;
268 int __media_thumb_pop_raw_data_manage_queue(int request_id)
273 req_len = g_queue_get_length(g_manage_raw_queue);
276 // thumb_dbg("There is no request in the queue");
279 for (i = 0; i < req_len; i++) {
280 thumbRawReq *req = NULL;
281 req = (thumbRawReq *)g_queue_peek_nth(g_manage_raw_queue, i);
282 if (req == NULL) continue;
284 if (request_id == req->request_id) {
285 g_queue_pop_nth(g_manage_raw_queue, i);
287 SAFE_FREE(req->userData);
294 return __media_thumb_check_raw_data_req_queue_for_cancel(request_id);
298 return MS_MEDIA_ERR_NONE;
301 int __media_thumb_check_req_queue(const char *path)
305 req_len = g_queue_get_length(g_request_queue);
307 // thumb_dbg("Queue length : %d", req_len);
308 // thumb_dbg("Queue path : %s", path);
311 // thumb_dbg("There is no request in the queue");
314 for (i = 0; i < req_len; i++) {
315 thumbReq *req = NULL;
316 req = (thumbReq *)g_queue_peek_nth(g_request_queue, i);
317 if (req == NULL) continue;
319 if (strncmp(path, req->path, strlen(path)) == 0) {
320 //thumb_dbg("Same Request - %s", path);
321 return MS_MEDIA_ERR_INVALID_PARAMETER;
328 if (g_manage_queue != NULL) {
329 req_len = g_queue_get_length(g_manage_queue);
332 // thumb_dbg("There is no request in the queue");
334 for (i = 0; i < req_len; i++) {
335 thumbReq *req = NULL;
336 req = (thumbReq *)g_queue_peek_nth(g_manage_queue, i);
337 if (req == NULL) continue;
339 if (strncmp(path, req->path, strlen(path)) == 0) {
340 //thumb_dbg("Same Request - %s", path);
341 return MS_MEDIA_ERR_INVALID_PARAMETER;
350 return MS_MEDIA_ERR_NONE;
353 bool __media_thumb_check_cancel(void)
355 thumbReq *req = NULL;
356 req = (thumbReq *)g_queue_peek_head(g_request_queue);
368 bool __media_thumb_check_cancel_for_raw(void)
370 thumbRawReq *req = NULL;
371 req = (thumbRawReq *)g_queue_peek_head(g_request_raw_queue);
383 int _media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg)
385 int recv_msg_len = 0;
387 int block_size = THUMB_SOCK_BLOCK_SIZE;
389 unsigned char *buf = NULL;
390 unsigned char *block_buf = NULL;
392 THUMB_MALLOC(buf, header_size);
393 THUMB_MALLOC(block_buf, THUMB_SOCK_BLOCK_SIZE);
394 if (buf == NULL || block_buf == NULL) {
395 thumb_err("memory allocation failed");
397 SAFE_FREE(block_buf);
398 return MS_MEDIA_ERR_OUT_OF_MEMORY;
401 if ((recv_msg_len = recv(sock, buf, header_size, 0)) <= 0) {
402 thumb_stderror("recv failed");
404 SAFE_FREE(block_buf);
405 return _media_thumb_get_error();
408 memcpy(msg, buf, header_size);
409 //thumb_dbg("origin_path_size : %d, dest_path_size : %d, thumb_size : %d", msg->origin_path_size, msg->dest_path_size, msg->thumb_size);
412 if (msg->origin_path_size < 0 || msg->dest_path_size < 0 || msg->thumb_size < 0) {
413 thumb_err("recv data is wrong");
414 SAFE_FREE(block_buf);
415 return MS_MEDIA_ERR_SOCKET_RECEIVE;
418 remain_size = msg->origin_path_size + msg->dest_path_size + msg->thumb_size;
419 THUMB_MALLOC(buf, remain_size);
421 SAFE_FREE(block_buf);
422 return MS_MEDIA_ERR_OUT_OF_MEMORY;
425 while (remain_size > 0) {
426 if (remain_size < THUMB_SOCK_BLOCK_SIZE) {
427 block_size = remain_size;
429 if ((recv_msg_len = recv(sock, block_buf, block_size, 0)) < 0) {
430 thumb_stderror("recv failed");
432 SAFE_FREE(block_buf);
433 return _media_thumb_get_error();
435 memcpy(buf+recv_block, block_buf, block_size);
436 recv_block += block_size;
437 remain_size -= block_size;
440 SAFE_STRLCPY(msg->org_path, (char *)buf, sizeof(msg->org_path));
441 SAFE_STRLCPY(msg->dst_path, (char *)buf + msg->origin_path_size, sizeof(msg->dst_path));
443 SAFE_FREE(msg->thumb_data);
444 if (msg->thumb_size > 0) {
445 THUMB_MALLOC(msg->thumb_data, msg->thumb_size);
446 if (msg->thumb_data != NULL) {
447 memcpy(msg->thumb_data, buf + msg->origin_path_size + msg->dest_path_size, msg->thumb_size);
450 SAFE_FREE(block_buf);
452 return MS_MEDIA_ERR_OUT_OF_MEMORY;
457 SAFE_FREE(block_buf);
459 return MS_MEDIA_ERR_NONE;
462 int _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
464 if (req_msg == NULL || buf == NULL) {
465 return MS_MEDIA_ERR_INVALID_PARAMETER;
468 int org_path_len = 0;
469 int dst_path_len = 0;
470 int thumb_data_len = 0;
474 header_size = sizeof(thumbMsg) -(MAX_FILEPATH_LEN * 2) - sizeof(unsigned char *);
475 org_path_len = req_msg->origin_path_size;
476 dst_path_len = req_msg->dest_path_size;
477 thumb_data_len = req_msg->thumb_size;
479 //thumb_dbg("Basic Size : %d, org_path : %s[%d], dst_path : %s[%d], thumb_data_len : %d", header_size, req_msg->org_path, org_path_len, req_msg->dst_path, dst_path_len, thumb_data_len);
481 size = header_size + org_path_len + dst_path_len + thumb_data_len;
482 THUMB_MALLOC(*buf, size);
487 memcpy(*buf, req_msg, header_size);
488 memcpy((*buf)+header_size, req_msg->org_path, org_path_len);
489 memcpy((*buf)+header_size + org_path_len, req_msg->dst_path, dst_path_len);
490 if (thumb_data_len > 0)
491 memcpy((*buf)+header_size + org_path_len + dst_path_len, req_msg->thumb_data, thumb_data_len);
495 return MS_MEDIA_ERR_NONE;
498 int _media_thumb_request(int msg_type, const char *origin_path, char *thumb_path, int max_length, uid_t uid)
501 struct sockaddr_un serv_addr;
502 int recv_str_len = 0;
503 int err = MS_MEDIA_ERR_NONE;
506 err = ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock);
507 if (err != MS_MEDIA_ERR_NONE) {
508 thumb_err("ms_ipc_create_client_socket failed");
512 memset(&serv_addr, 0, sizeof(serv_addr));
513 serv_addr.sun_family = AF_UNIX;
514 SAFE_STRLCPY(serv_addr.sun_path, THUMB_IPC_PATH, sizeof(serv_addr.sun_path));
516 /* Connecting to the thumbnail server */
517 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
518 thumb_stderror("connect");
520 return MS_MEDIA_ERR_SOCKET_CONN;
526 memset((void *)&req_msg, 0, sizeof(thumbMsg));
527 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
529 /* Get PID of client*/
533 /* Set requset message */
534 req_msg.msg_type = msg_type;
536 SAFE_STRLCPY(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
537 req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
538 req_msg.dest_path_size = strlen(req_msg.dst_path) + 1;
539 req_msg.thumb_size = 0;
541 if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
542 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
544 return MS_MEDIA_ERR_INVALID_PARAMETER;
547 unsigned char *buf = NULL;
551 header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
552 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
554 if (send(sock, buf, buf_size, 0) != buf_size) {
555 thumb_err("sendto failed: %d", errno);
558 return MS_MEDIA_ERR_SOCKET_SEND;
561 thumb_dbg("Sending msg to thumbnail daemon is successful");
565 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
566 thumb_err("_media_thumb_recv_msg failed ");
571 recv_str_len = strlen(recv_msg.org_path);
572 thumb_dbg_slog("recv %s(%d) from thumb daemon is successful", recv_msg.org_path, recv_str_len);
575 SAFE_FREE(recv_msg.thumb_data);
577 if (recv_str_len > max_length) {
578 thumb_err("user buffer is too small. Output's length is %d", recv_str_len);
579 return MS_MEDIA_ERR_INVALID_PARAMETER;
582 if (recv_msg.status != MS_MEDIA_ERR_NONE) {
583 thumb_err("Failed to make thumbnail");
584 return recv_msg.status;
587 SAFE_STRLCPY(thumb_path, recv_msg.dst_path, max_length);
589 return MS_MEDIA_ERR_NONE;
592 gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
597 int err = MS_MEDIA_ERR_NONE;
599 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
600 sock = g_io_channel_unix_get_fd(src);
602 header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
604 thumb_err("_media_thumb_write_socket socket : %d", sock);
606 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
607 thumb_err("_media_thumb_recv_msg failed ");
608 __media_thumb_pop_req_queue();
613 if (recv_msg.status != MS_MEDIA_ERR_NONE) {
614 err = recv_msg.status;
615 thumb_err("Failed to make thumbnail (%d)", err);
618 if (__media_thumb_check_cancel()) {
620 thumbUserData* cb = (thumbUserData*)data;
621 if (cb->func != NULL)
622 cb->func(err, recv_msg.dst_path, cb->user_data);
626 __media_thumb_pop_req_queue();
633 gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
639 int err = MS_MEDIA_ERR_NONE;
641 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
642 sock = g_io_channel_unix_get_fd(src);
644 header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
646 thumb_err("_media_thumb_write_socket socket : %d", sock);
648 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
649 thumb_err("_media_thumb_recv_msg failed ");
650 if (recv_msg.request_id > 0) {
651 __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, TRUE);
653 thumb_err("origin path size is wrong.");
659 g_io_channel_shutdown(src, TRUE, NULL);
660 g_io_channel_unref(src);
662 if (recv_msg.status != MS_MEDIA_ERR_NONE) {
663 err = recv_msg.status;
664 thumb_err("Failed to make thumbnail (%d)", err);
667 if (__media_thumb_check_cancel_for_raw()) {
669 thumbRawUserData* cb = (thumbRawUserData*)data;
670 if (cb->func != NULL)
671 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);
675 __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, FALSE);
679 /* Check manage queue */
680 if (g_manage_raw_queue) {
681 len = g_queue_get_length(g_manage_raw_queue);
684 _media_thumb_raw_data_send_request();
686 g_queue_free(g_manage_raw_queue);
687 g_manage_raw_queue = NULL;
694 int _media_thumb_send_request()
696 int err = MS_MEDIA_ERR_NONE;
698 struct sockaddr_un serv_addr;
699 thumbReq *req_manager = NULL;
702 err = ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock);
703 if (err != MS_MEDIA_ERR_NONE) {
704 thumb_err("ms_ipc_create_client_socket failed");
708 memset(&serv_addr, 0, sizeof(serv_addr));
709 serv_addr.sun_family = AF_UNIX;
710 SAFE_STRLCPY(serv_addr.sun_path, THUMB_IPC_PATH, sizeof(serv_addr.sun_path));
712 GIOChannel *channel = NULL;
713 channel = g_io_channel_unix_new(sock);
716 /* Connecting to the thumbnail server */
717 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
718 thumb_stderror("connect");
720 err = MS_MEDIA_ERR_PERMISSION_DENIED;
722 err = MS_MEDIA_ERR_SOCKET_CONN;
724 g_io_channel_shutdown(channel, TRUE, NULL);
725 g_io_channel_unref(channel);
731 req_manager = (thumbReq *)g_queue_pop_head(g_manage_queue);
733 if (req_manager == NULL) {
734 thumb_err("queue pop fail");
735 g_io_channel_shutdown(channel, TRUE, NULL);
736 g_io_channel_unref(channel);
738 return MS_MEDIA_ERR_INVALID_PARAMETER;
741 GSource *source = NULL;
742 source = g_io_create_watch(channel, G_IO_IN);
743 g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, req_manager->userData, NULL);
744 source_id = g_source_attach(source, g_main_context_get_thread_default());
748 memset((void *)&req_msg, 0, sizeof(thumbMsg));
752 req_msg.msg_type = req_manager->msg_type;
753 req_msg.request_id = 0;
754 req_msg.uid = req_manager->uid;
755 SAFE_STRLCPY(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
756 req_msg.dst_path[0] = '\0';
757 req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
758 req_msg.dest_path_size = 1;
759 req_msg.thumb_size = 0;
761 if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
762 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
763 g_io_channel_shutdown(channel, TRUE, NULL);
764 g_io_channel_unref(channel);
766 return MS_MEDIA_ERR_INVALID_PARAMETER;
769 unsigned char *buf = NULL;
771 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
773 if (send(sock, buf, buf_size, 0) != buf_size) {
774 thumb_err("sendto failed: %d", errno);
776 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
777 g_io_channel_shutdown(channel, TRUE, NULL);
778 g_io_channel_unref(channel);
780 return MS_MEDIA_ERR_SOCKET_SEND;
784 thumb_dbg("Sending msg to thumbnail daemon is successful");
787 if (req_manager->msg_type == THUMB_REQUEST_DB_INSERT) {
788 if (g_request_queue == NULL) {
789 g_request_queue = g_queue_new();
792 thumbReq *thumb_req = NULL;
793 THUMB_MALLOC(thumb_req, sizeof(thumbReq));
794 if (thumb_req == NULL) {
795 thumb_err("Failed to create request element");
796 return MS_MEDIA_ERR_INVALID_PARAMETER;
799 thumb_req->channel = channel;
800 thumb_req->path = strdup(req_manager->path);
801 thumb_req->source_id = source_id;
802 thumb_req->userData = req_manager->userData;
804 g_queue_push_tail(g_request_queue, (gpointer)thumb_req);
810 int _media_thumb_raw_data_send_request()
812 int err = MS_MEDIA_ERR_NONE;
814 struct sockaddr_un serv_addr;
815 thumbRawReq *req_manager = NULL;
818 err = ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock);
819 if (err != MS_MEDIA_ERR_NONE) {
820 thumb_err("ms_ipc_create_client_socket failed");
824 memset(&serv_addr, 0, sizeof(serv_addr));
825 serv_addr.sun_family = AF_UNIX;
826 SAFE_STRLCPY(serv_addr.sun_path, THUMB_IPC_PATH, sizeof(serv_addr.sun_path));
828 GIOChannel *channel = NULL;
829 channel = g_io_channel_unix_new(sock);
832 /* Connecting to the thumbnail server */
833 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
834 thumb_stderror("connect error");
836 err = MS_MEDIA_ERR_PERMISSION_DENIED;
838 err = MS_MEDIA_ERR_SOCKET_CONN;
840 g_io_channel_shutdown(channel, TRUE, NULL);
841 g_io_channel_unref(channel);
846 req_manager = (thumbRawReq *)g_queue_pop_head(g_manage_raw_queue);
848 if (req_manager == NULL) {
849 thumb_err("queue pop fail");
850 g_io_channel_shutdown(channel, TRUE, NULL);
851 g_io_channel_unref(channel);
853 return MS_MEDIA_ERR_INVALID_PARAMETER;
856 GSource *source = NULL;
857 source = g_io_create_watch(channel, G_IO_IN);
858 g_source_set_callback(source, (GSourceFunc)_media_thumb_raw_data_write_socket, req_manager->userData, NULL);
859 source_id = g_source_attach(source, g_main_context_get_thread_default());
862 memset((void *)&req_msg, 0, sizeof(thumbMsg));
866 req_msg.msg_type = req_manager->msg_type;
867 req_msg.request_id = req_manager->request_id;
868 req_msg.thumb_width = req_manager->width;
869 req_msg.thumb_height = req_manager->height;
870 req_msg.uid = req_manager->uid;
872 SAFE_STRLCPY(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
873 req_msg.dst_path[0] = '\0';
875 req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
876 req_msg.dest_path_size = 1;
877 req_msg.thumb_size = 0;
879 if (req_msg.origin_path_size > MAX_PATH_SIZE) {
880 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
881 g_io_channel_shutdown(channel, TRUE, NULL);
882 g_io_channel_unref(channel);
883 return MS_MEDIA_ERR_INVALID_PARAMETER;
886 unsigned char *buf = NULL;
888 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
890 if (send(sock, buf, buf_size, 0) != buf_size) {
891 thumb_err("sendto failed: %d", errno);
893 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
894 g_io_channel_shutdown(channel, TRUE, NULL);
895 g_io_channel_unref(channel);
896 return MS_MEDIA_ERR_SOCKET_SEND;
901 if (req_manager->msg_type == THUMB_REQUEST_RAW_DATA) {
902 if (g_request_raw_queue == NULL) {
903 g_request_raw_queue = g_queue_new();
905 thumbRawReq *thumb_req = NULL;
906 THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
907 if (thumb_req == NULL) {
908 thumb_err("Failed to create request element");
909 return MS_MEDIA_ERR_INVALID_PARAMETER;
911 thumb_req->channel = channel;
912 thumb_req->request_id = req_manager->request_id;
913 thumb_req->source_id = source_id;
914 thumb_req->userData = req_manager->userData;
916 g_queue_push_tail(g_request_raw_queue, (gpointer)thumb_req);
918 return MS_MEDIA_ERR_NONE;
921 int _media_thumb_request_async(int msg_type, unsigned int request_id, const char *origin_path, thumbUserData *userData, uid_t uid)
923 int err = MS_MEDIA_ERR_NONE;
925 if (g_manage_queue == NULL) {
926 if (msg_type == THUMB_REQUEST_CANCEL_MEDIA)
927 return MS_MEDIA_ERR_INTERNAL;
929 g_manage_queue = g_queue_new();
931 thumbReq *thumb_req = NULL;
932 THUMB_MALLOC(thumb_req, sizeof(thumbReq));
933 if (thumb_req == NULL) {
934 thumb_err("Failed to create request element");
935 return MS_MEDIA_ERR_INVALID_PARAMETER;
938 thumb_req->msg_type = msg_type;
939 thumb_req->path = strdup(origin_path);
940 thumb_req->userData = userData;
941 thumb_req->isCanceled = false;
942 thumb_req->request_id = request_id;
943 thumb_req->uid = uid;
945 g_queue_push_tail(g_manage_queue, (gpointer)thumb_req);
947 /* directly request at first time */
948 err = _media_thumb_send_request();
951 if (msg_type != THUMB_REQUEST_CANCEL_MEDIA) {
954 if ((msg_type == THUMB_REQUEST_DB_INSERT) && (__media_thumb_check_req_queue(origin_path) < 0)) {
955 thumb_err("duplicated request");
956 return MS_MEDIA_ERR_THUMB_DUPLICATED_REQUEST;
959 thumbReq *thumb_req = NULL;
960 THUMB_MALLOC(thumb_req, sizeof(thumbReq));
961 if (thumb_req == NULL) {
962 thumb_err("Failed to create request element");
963 return MS_MEDIA_ERR_INVALID_PARAMETER;
966 thumb_req->msg_type = msg_type;
967 thumb_req->path = strdup(origin_path);
968 thumb_req->userData = userData;
969 thumb_req->isCanceled = false;
970 thumb_req->request_id = request_id;
971 thumb_req->uid = uid;
973 g_queue_push_tail(g_manage_queue, (gpointer)thumb_req);
976 err = __media_thumb_pop_manage_queue(request_id, origin_path);
983 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)
985 int err = MS_MEDIA_ERR_NONE;
987 if (g_manage_raw_queue == NULL) {
988 if (msg_type == THUMB_REQUEST_CANCEL_RAW_DATA)
989 return MS_MEDIA_ERR_INTERNAL;
991 g_manage_raw_queue = g_queue_new();
993 thumbRawReq *thumb_req = NULL;
994 THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
995 if (thumb_req == NULL) {
996 thumb_err("Failed to create request element");
997 return MS_MEDIA_ERR_INVALID_PARAMETER;
1000 thumb_req->msg_type = msg_type;
1001 thumb_req->request_id = request_id;
1002 thumb_req->path = strdup(origin_path);
1003 thumb_req->width = width;
1004 thumb_req->height = height;
1005 thumb_req->userData = userData;
1006 thumb_req->isCanceled = false;
1007 thumb_req->uid = uid;
1009 g_queue_push_tail(g_manage_raw_queue, (gpointer)thumb_req);
1011 /* directly request at first time */
1012 err = _media_thumb_raw_data_send_request();
1015 if (msg_type != THUMB_REQUEST_CANCEL_RAW_DATA) {
1017 thumbRawReq *thumb_req = NULL;
1018 THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
1019 if (thumb_req == NULL) {
1020 thumb_err("Failed to create request element");
1021 return MS_MEDIA_ERR_INVALID_PARAMETER;
1024 thumb_req->msg_type = msg_type;
1025 thumb_req->request_id = request_id;
1026 thumb_req->path = strdup(origin_path);
1027 thumb_req->width = width;
1028 thumb_req->height = height;
1029 thumb_req->userData = userData;
1030 thumb_req->isCanceled = false;
1031 thumb_req->uid = uid;
1033 g_queue_push_tail(g_manage_raw_queue, (gpointer)thumb_req);
1037 err = __media_thumb_pop_raw_data_manage_queue(request_id);
1044 int _media_thumb_request_cancel_all(bool isRaw)
1051 thumbRawReq *tmp_raw_req = NULL;
1052 if (g_manage_raw_queue == NULL) {
1053 thumb_err("manage_queue is NULL");
1054 if (g_request_raw_queue != NULL) {
1055 /* Check request queue */
1056 len = g_queue_get_length(g_request_raw_queue);
1058 tmp_raw_req = g_queue_peek_head(g_request_raw_queue);
1059 if (tmp_raw_req != NULL)
1060 tmp_raw_req->isCanceled = true;
1061 return MS_MEDIA_ERR_NONE;
1064 thumb_err("request_queue is NULL");
1065 return MS_MEDIA_ERR_INTERNAL;
1067 req_len = g_queue_get_length(g_manage_raw_queue);
1068 for (i = 0; i < req_len; i++) {
1069 tmp_raw_req = g_queue_pop_tail(g_manage_raw_queue);
1071 SAFE_FREE(tmp_raw_req->path);
1072 SAFE_FREE(tmp_raw_req->userData);
1073 SAFE_FREE(tmp_raw_req);
1077 g_queue_free(g_manage_raw_queue);
1078 g_manage_raw_queue = NULL;
1079 if (g_request_raw_queue != NULL) {
1080 /* Check request queue */
1081 len = g_queue_get_length(g_request_raw_queue);
1083 tmp_raw_req = g_queue_peek_head(g_request_raw_queue);
1084 if (tmp_raw_req != NULL)
1085 tmp_raw_req->isCanceled = true;
1089 thumbReq *tmp_req = NULL;
1090 if (g_manage_queue == NULL) {
1091 thumb_err("manage_queue is NULL");
1092 if (g_request_queue != NULL) {
1093 /* Check request queue */
1094 len = g_queue_get_length(g_request_queue);
1096 tmp_req = g_queue_peek_head(g_request_queue);
1097 if (tmp_req != NULL)
1098 tmp_req->isCanceled = true;
1099 return MS_MEDIA_ERR_NONE;
1102 thumb_err("request_queue is NULL");
1103 return MS_MEDIA_ERR_INTERNAL;
1105 req_len = g_queue_get_length(g_manage_queue);
1106 for (i = 0; i < req_len; i++) {
1107 tmp_req = g_queue_pop_tail(g_manage_queue);
1109 SAFE_FREE(tmp_req->path);
1110 SAFE_FREE(tmp_req->userData);
1115 g_queue_free(g_manage_queue);
1116 g_manage_queue = NULL;
1117 if (g_request_queue != NULL) {
1118 /* Check request queue */
1119 len = g_queue_get_length(g_request_queue);
1121 tmp_req = g_queue_peek_head(g_request_queue);
1122 if (tmp_req != NULL)
1123 tmp_req->isCanceled = true;
1128 return MS_MEDIA_ERR_NONE;