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 "/var/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;
50 thumbUserData *userData;
63 thumbRawUserData *userData;
66 int _media_thumb_send_request();
67 int _media_thumb_raw_data_send_request();
70 int _media_thumb_get_error()
72 if (errno == EWOULDBLOCK) {
73 thumb_err("Timeout. Can't try any more");
74 return MS_MEDIA_ERR_SOCKET_RECEIVE_TIMEOUT;
76 thumb_stderror("recvfrom failed");
77 return MS_MEDIA_ERR_SOCKET_RECEIVE;
81 void __media_thumb_shutdown_channel(bool only_shutdown)
84 thumbReq *req = (thumbReq *)g_queue_peek_head(g_request_queue);
86 len = g_queue_get_length(g_manage_queue);
90 if (len == 0 && req->isCanceled == true) {
91 thumb_dbg("The manage queue will be released");
93 thumb_dbg("There is remain item in the manage queue");
97 GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
98 if (source_id != NULL) {
99 g_source_destroy(source_id);
101 thumb_err("G_SOURCE_ID is NULL");
104 g_io_channel_shutdown(req->channel, TRUE, NULL);
105 g_io_channel_unref(req->channel);
106 g_queue_pop_head(g_request_queue);
108 SAFE_FREE(req->path);
109 SAFE_FREE(req->userData);
112 if (g_manage_queue && len == 0) {
113 g_queue_free(g_manage_queue);
114 g_manage_queue = NULL;
119 int __media_thumb_pop_req_queue(const char *path)
123 req_len = g_queue_get_length(g_request_queue);
126 // thumb_dbg("There is no request in the queue");
128 __media_thumb_shutdown_channel(true);
131 /* Check manage queue */
132 if (g_manage_queue) {
133 req_len = g_queue_get_length(g_manage_queue);
136 _media_thumb_send_request();
139 return MS_MEDIA_ERR_NONE;
142 int __media_thumb_check_req_queue_for_cancel(const char *path)
146 req_len = g_queue_get_length(g_request_queue);
149 // thumb_dbg("There is no request in the queue");
151 thumbReq *req = NULL;
152 req = (thumbReq *)g_queue_peek_head(g_request_queue);
154 if (req != NULL && strncmp(path, req->path, strlen(path)) == 0) {
155 req->isCanceled = true;
156 __media_thumb_shutdown_channel(false);
158 return MS_MEDIA_ERR_NONE;
162 return MS_MEDIA_ERR_INTERNAL;
165 int __media_thumb_pop_manage_queue(const char *path)
170 req_len = g_queue_get_length(g_manage_queue);
173 // thumb_dbg("There is no request in the queue");
175 for (i = 0; i < req_len; i++) {
176 thumbReq *req = NULL;
177 req = (thumbReq *)g_queue_peek_nth(g_manage_queue, i);
178 if (req == NULL) continue;
180 if (strncmp(path, req->path, strlen(path)) == 0) {
181 g_queue_pop_nth(g_manage_queue, i);
183 SAFE_FREE(req->path);
184 SAFE_FREE(req->userData);
191 return __media_thumb_check_req_queue_for_cancel(path);
193 __media_thumb_shutdown_channel(false);
195 return MS_MEDIA_ERR_NONE;
199 return MS_MEDIA_ERR_NONE;
203 int __media_thumb_pop_raw_data_req_queue(int request_id, bool shutdown_channel)
207 req_len = g_queue_get_length(g_request_raw_queue);
210 // thumb_dbg("There is no request in the queue");
213 for (i = 0; i < req_len; i++) {
214 thumbRawReq *req = NULL;
215 req = (thumbRawReq *)g_queue_peek_nth(g_request_raw_queue, i);
216 if (req == NULL) continue;
218 if (request_id == req->request_id) {
219 if (shutdown_channel) {
220 GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
221 if (source_id != NULL) {
222 g_source_destroy(source_id);
224 thumb_err("G_SOURCE_ID is NULL");
227 g_io_channel_shutdown(req->channel, TRUE, NULL);
228 g_io_channel_unref(req->channel);
230 g_queue_pop_nth(g_request_raw_queue, i);
232 SAFE_FREE(req->userData);
239 // thumb_dbg("There's no %s in the queue", path);
243 return MS_MEDIA_ERR_NONE;
246 int __media_thumb_check_raw_data_req_queue_for_cancel(int request_id)
250 req_len = g_queue_get_length(g_request_raw_queue);
253 // thumb_dbg("There is no request in the queue");
255 thumbRawReq *req = NULL;
256 req = (thumbRawReq *)g_queue_peek_head(g_request_raw_queue);
258 if (req != NULL && request_id == req->request_id) {
259 req->isCanceled = true;
260 return MS_MEDIA_ERR_NONE;
264 return MS_MEDIA_ERR_INTERNAL;
267 int __media_thumb_pop_raw_data_manage_queue(int request_id)
272 req_len = g_queue_get_length(g_manage_raw_queue);
275 // thumb_dbg("There is no request in the queue");
278 for (i = 0; i < req_len; i++) {
279 thumbRawReq *req = NULL;
280 req = (thumbRawReq *)g_queue_peek_nth(g_manage_raw_queue, i);
281 if (req == NULL) continue;
283 if (request_id == req->request_id) {
284 g_queue_pop_nth(g_manage_raw_queue, i);
286 SAFE_FREE(req->userData);
293 return __media_thumb_check_raw_data_req_queue_for_cancel(request_id);
297 return MS_MEDIA_ERR_NONE;
300 int __media_thumb_check_req_queue(const char *path)
304 req_len = g_queue_get_length(g_request_queue);
306 // thumb_dbg("Queue length : %d", req_len);
307 // thumb_dbg("Queue path : %s", path);
310 // thumb_dbg("There is no request in the queue");
313 for (i = 0; i < req_len; i++) {
314 thumbReq *req = NULL;
315 req = (thumbReq *)g_queue_peek_nth(g_request_queue, i);
316 if (req == NULL) continue;
318 if (strncmp(path, req->path, strlen(path)) == 0) {
319 //thumb_dbg("Same Request - %s", path);
320 return MS_MEDIA_ERR_INVALID_PARAMETER;
327 return MS_MEDIA_ERR_NONE;
330 bool __media_thumb_check_cancel(void)
332 thumbReq *req = NULL;
333 req = (thumbReq *)g_queue_peek_head(g_request_queue);
345 bool __media_thumb_check_cancel_for_raw(void)
347 thumbRawReq *req = NULL;
348 req = (thumbRawReq *)g_queue_peek_head(g_request_raw_queue);
361 _media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg)
363 int recv_msg_len = 0;
365 int block_size = THUMB_SOCK_BLOCK_SIZE;
367 unsigned char *buf = NULL;
368 unsigned char *block_buf = NULL;
370 THUMB_MALLOC(buf, header_size);
371 THUMB_MALLOC(block_buf, THUMB_SOCK_BLOCK_SIZE);
372 if (buf == NULL || block_buf == NULL) {
373 thumb_err("memory allocation failed");
375 SAFE_FREE(block_buf);
376 return MS_MEDIA_ERR_OUT_OF_MEMORY;
379 if ((recv_msg_len = recv(sock, buf, header_size, 0)) <= 0) {
380 thumb_stderror("recv failed");
382 SAFE_FREE(block_buf);
383 return _media_thumb_get_error();
386 memcpy(msg, buf, header_size);
387 //thumb_dbg("origin_path_size : %d, dest_path_size : %d, thumb_size : %d", msg->origin_path_size, msg->dest_path_size, msg->thumb_size);
390 if (msg->origin_path_size < 0 || msg->dest_path_size < 0 || msg->thumb_size < 0) {
391 thumb_err("recv data is wrong");
392 SAFE_FREE(block_buf);
393 return MS_MEDIA_ERR_SOCKET_RECEIVE;
396 remain_size = msg->origin_path_size + msg->dest_path_size + msg->thumb_size;
397 THUMB_MALLOC(buf, remain_size);
399 SAFE_FREE(block_buf);
400 return MS_MEDIA_ERR_OUT_OF_MEMORY;
403 while (remain_size > 0) {
404 if (remain_size < THUMB_SOCK_BLOCK_SIZE) {
405 block_size = remain_size;
407 if ((recv_msg_len = recv(sock, block_buf, block_size, 0)) < 0) {
408 thumb_stderror("recv failed");
410 SAFE_FREE(block_buf);
411 return _media_thumb_get_error();
413 memcpy(buf+recv_block, block_buf, block_size);
414 recv_block += block_size;
415 remain_size -= block_size;
418 strncpy(msg->org_path, (char *)buf, msg->origin_path_size);
419 strncpy(msg->dst_path, (char *)buf + msg->origin_path_size, msg->dest_path_size);
421 SAFE_FREE(msg->thumb_data);
422 if (msg->thumb_size > 0) {
423 THUMB_MALLOC(msg->thumb_data, msg->thumb_size);
424 if (msg->thumb_data != NULL) {
425 memcpy(msg->thumb_data, buf + msg->origin_path_size + msg->dest_path_size, msg->thumb_size);
428 SAFE_FREE(block_buf);
430 return MS_MEDIA_ERR_OUT_OF_MEMORY;
435 SAFE_FREE(block_buf);
437 return MS_MEDIA_ERR_NONE;
441 _media_thumb_recv_udp_msg(int sock, int header_size, thumbMsg *msg, struct sockaddr_un *from_addr, unsigned int *from_size)
443 int recv_msg_len = 0;
444 unsigned int from_addr_size = sizeof(struct sockaddr_un);
445 unsigned char *buf = NULL;
447 THUMB_MALLOC(buf, sizeof(thumbMsg));
449 thumb_err("memory allocation failed");
450 return MS_MEDIA_ERR_OUT_OF_MEMORY;
453 if ((recv_msg_len = recvfrom(sock, buf, sizeof(thumbMsg), 0, (struct sockaddr *)from_addr, &from_addr_size)) < 0) {
454 thumb_stderror("recvform failed");
456 return _media_thumb_get_error();
459 memcpy(msg, buf, header_size);
461 if (msg->origin_path_size <= 0 || msg->origin_path_size > MAX_PATH_SIZE) {
463 thumb_err("msg->origin_path_size is invalid %d", msg->origin_path_size);
464 return MS_MEDIA_ERR_INVALID_PARAMETER;
467 strncpy(msg->org_path, (char*)buf + header_size, msg->origin_path_size);
469 if (msg->dest_path_size <= 0 || msg->dest_path_size > MAX_PATH_SIZE) {
471 thumb_err("msg->origin_path_size is invalid %d", msg->dest_path_size);
472 return MS_MEDIA_ERR_INVALID_PARAMETER;
475 strncpy(msg->dst_path, (char*)buf + header_size + msg->origin_path_size, msg->dest_path_size);
478 *from_size = from_addr_size;
480 return MS_MEDIA_ERR_NONE;
484 _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
486 if (req_msg == NULL || buf == NULL) {
487 return MS_MEDIA_ERR_INVALID_PARAMETER;
490 int org_path_len = 0;
491 int dst_path_len = 0;
492 int thumb_data_len = 0;
496 header_size = sizeof(thumbMsg) -(MAX_FILEPATH_LEN * 2) - sizeof(unsigned char *);
497 org_path_len = req_msg->origin_path_size;
498 dst_path_len = req_msg->dest_path_size;
499 thumb_data_len = req_msg->thumb_size;
501 //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);
503 size = header_size + org_path_len + dst_path_len + thumb_data_len;
504 THUMB_MALLOC(*buf, size);
509 memcpy(*buf, req_msg, header_size);
510 memcpy((*buf)+header_size, req_msg->org_path, org_path_len);
511 memcpy((*buf)+header_size + org_path_len, req_msg->dst_path, dst_path_len);
512 if (thumb_data_len > 0)
513 memcpy((*buf)+header_size + org_path_len + dst_path_len, req_msg->thumb_data, thumb_data_len);
517 return MS_MEDIA_ERR_NONE;
521 _media_thumb_request(int msg_type, const char *origin_path, char *thumb_path, int max_length, media_thumb_info *thumb_info, uid_t uid)
524 struct sockaddr_un serv_addr;
525 ms_sock_info_s sock_info;
526 int recv_str_len = 0;
527 int err = MS_MEDIA_ERR_NONE;
529 sock_info.port = MS_THUMB_CREATOR_PORT;
531 err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
532 if (err != MS_MEDIA_ERR_NONE) {
533 thumb_err("ms_ipc_create_client_socket failed");
537 memset(&serv_addr, 0, sizeof(serv_addr));
538 sock = sock_info.sock_fd;
539 serv_addr.sun_family = AF_UNIX;
540 strncpy(serv_addr.sun_path, THUMB_IPC_PATH, strlen(THUMB_IPC_PATH));
542 /* Connecting to the thumbnail server */
543 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
544 thumb_stderror("connect");
545 ms_ipc_delete_client_socket(&sock_info);
546 return MS_MEDIA_ERR_SOCKET_CONN;
552 memset((void *)&req_msg, 0, sizeof(thumbMsg));
553 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
555 /* Get PID of client*/
559 /* Set requset message */
560 req_msg.msg_type = msg_type;
562 strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
563 req_msg.org_path[strlen(req_msg.org_path)] = '\0';
565 if (msg_type == THUMB_REQUEST_SAVE_FILE) {
566 strncpy(req_msg.dst_path, thumb_path, sizeof(req_msg.dst_path));
567 req_msg.dst_path[strlen(req_msg.dst_path)] = '\0';
570 req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
571 req_msg.dest_path_size = strlen(req_msg.dst_path) + 1;
572 req_msg.thumb_size = 0;
574 if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
575 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
576 ms_ipc_delete_client_socket(&sock_info);
577 return MS_MEDIA_ERR_INVALID_PARAMETER;
580 unsigned char *buf = NULL;
584 header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
585 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
587 if (send(sock, buf, buf_size, 0) != buf_size) {
588 thumb_err("sendto failed: %d", errno);
590 ms_ipc_delete_client_socket(&sock_info);
591 return MS_MEDIA_ERR_SOCKET_SEND;
594 thumb_dbg("Sending msg to thumbnail daemon is successful");
598 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
599 thumb_err("_media_thumb_recv_msg failed ");
600 ms_ipc_delete_client_socket(&sock_info);
604 recv_str_len = strlen(recv_msg.org_path);
605 thumb_dbg_slog("recv %s(%d) from thumb daemon is successful", recv_msg.org_path, recv_str_len);
607 ms_ipc_delete_client_socket(&sock_info);
609 if (recv_str_len > max_length) {
610 thumb_err("user buffer is too small. Output's length is %d", recv_str_len);
611 SAFE_FREE(recv_msg.thumb_data);
612 return MS_MEDIA_ERR_INVALID_PARAMETER;
615 if (recv_msg.status == THUMB_FAIL) {
616 thumb_err("Failed to make thumbnail");
617 SAFE_FREE(recv_msg.thumb_data);
618 return MS_MEDIA_ERR_INVALID_PARAMETER;
621 if (msg_type != THUMB_REQUEST_SAVE_FILE) {
622 strncpy(thumb_path, recv_msg.dst_path, max_length);
625 thumb_info->origin_width = recv_msg.origin_width;
626 thumb_info->origin_height = recv_msg.origin_height;
628 SAFE_FREE(recv_msg.thumb_data);
630 return MS_MEDIA_ERR_NONE;
633 gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
638 int err = MS_MEDIA_ERR_NONE;
640 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
641 sock = g_io_channel_unix_get_fd(src);
643 header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
645 thumb_err("_media_thumb_write_socket socket : %d", sock);
647 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
648 thumb_err("_media_thumb_recv_msg failed ");
649 if (recv_msg.origin_path_size > 0) {
650 __media_thumb_pop_req_queue(recv_msg.org_path);
652 thumb_err("origin path size is wrong.");
658 //thumb_dbg("Completed..%s", recv_msg.org_path);
660 if (recv_msg.status == THUMB_FAIL) {
661 thumb_err("Failed to make thumbnail");
662 err = MS_MEDIA_ERR_INTERNAL;
665 if (__media_thumb_check_cancel()) {
667 thumbUserData* cb = (thumbUserData*)data;
668 if (cb->func != NULL)
669 cb->func(err, recv_msg.dst_path, cb->user_data);
673 __media_thumb_pop_req_queue(recv_msg.org_path);
680 gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
686 int err = MS_MEDIA_ERR_NONE;
688 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
689 sock = g_io_channel_unix_get_fd(src);
691 header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
693 thumb_err("_media_thumb_write_socket socket : %d", sock);
695 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
696 thumb_err("_media_thumb_recv_msg failed ");
697 if (recv_msg.request_id > 0) {
698 __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, TRUE);
700 thumb_err("origin path size is wrong.");
706 g_io_channel_shutdown(src, TRUE, NULL);
707 g_io_channel_unref(src);
709 if (recv_msg.status == THUMB_FAIL) {
710 thumb_err("Failed to make thumbnail");
711 err = MS_MEDIA_ERR_INTERNAL;
714 if (__media_thumb_check_cancel_for_raw()) {
716 thumbRawUserData* cb = (thumbRawUserData*)data;
717 if (cb->func != NULL)
718 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);
722 __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, FALSE);
726 SAFE_FREE(recv_msg.thumb_data);
728 /* Check manage queue */
729 if (g_manage_raw_queue) {
730 len = g_queue_get_length(g_manage_raw_queue);
733 _media_thumb_raw_data_send_request();
735 g_queue_free(g_manage_raw_queue);
736 g_manage_raw_queue = NULL;
743 int _media_thumb_send_request()
745 int err = MS_MEDIA_ERR_NONE;
747 struct sockaddr_un serv_addr;
748 ms_sock_info_s sock_info;
749 thumbReq *req_manager = NULL;
751 sock_info.port = MS_THUMB_CREATOR_PORT;
753 err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
754 if (err != MS_MEDIA_ERR_NONE) {
755 thumb_err("ms_ipc_create_client_socket failed");
759 memset(&serv_addr, 0, sizeof(serv_addr));
760 sock = sock_info.sock_fd;
761 serv_addr.sun_family = AF_UNIX;
762 strncpy(serv_addr.sun_path, THUMB_IPC_PATH, strlen(THUMB_IPC_PATH));
764 GIOChannel *channel = NULL;
765 channel = g_io_channel_unix_new(sock);
768 /* Connecting to the thumbnail server */
769 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
770 thumb_stderror("connect");
771 g_io_channel_shutdown(channel, TRUE, NULL);
772 g_io_channel_unref(channel);
773 ms_ipc_delete_client_socket(&sock_info);
774 return MS_MEDIA_ERR_SOCKET_CONN;
777 req_manager = (thumbReq *)g_queue_pop_head(g_manage_queue);
779 if (req_manager == NULL) {
780 thumb_err("queue pop fail");
781 g_io_channel_shutdown(channel, TRUE, NULL);
782 g_io_channel_unref(channel);
783 ms_ipc_delete_client_socket(&sock_info);
784 return MS_MEDIA_ERR_INVALID_PARAMETER;
787 GSource *source = NULL;
788 source = g_io_create_watch(channel, G_IO_IN);
789 g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, req_manager->userData, NULL);
790 source_id = g_source_attach(source, g_main_context_get_thread_default());
794 memset((void *)&req_msg, 0, sizeof(thumbMsg));
798 req_msg.msg_type = req_manager->msg_type;
799 req_msg.request_id = 0;
800 req_msg.uid = req_manager->uid;
801 strncpy(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
802 req_msg.org_path[strlen(req_msg.org_path)] = '\0';
803 req_msg.dst_path[0] = '\0';
804 req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
805 req_msg.dest_path_size = 1;
806 req_msg.thumb_size = 0;
808 if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
809 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
810 g_io_channel_shutdown(channel, TRUE, NULL);
811 g_io_channel_unref(channel);
812 ms_ipc_delete_client_socket(&sock_info);
813 return MS_MEDIA_ERR_INVALID_PARAMETER;
816 unsigned char *buf = NULL;
818 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
820 if (send(sock, buf, buf_size, 0) != buf_size) {
821 thumb_err("sendto failed: %d", errno);
823 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
824 g_io_channel_shutdown(channel, TRUE, NULL);
825 g_io_channel_unref(channel);
826 ms_ipc_delete_client_socket(&sock_info);
827 return MS_MEDIA_ERR_SOCKET_SEND;
831 thumb_dbg("Sending msg to thumbnail daemon is successful");
834 if (req_manager->msg_type == THUMB_REQUEST_DB_INSERT) {
835 if (g_request_queue == NULL) {
836 g_request_queue = g_queue_new();
839 thumbReq *thumb_req = NULL;
840 THUMB_MALLOC(thumb_req, sizeof(thumbReq));
841 if (thumb_req == NULL) {
842 thumb_err("Failed to create request element");
843 return MS_MEDIA_ERR_INVALID_PARAMETER;
846 thumb_req->channel = channel;
847 thumb_req->path = strdup(req_manager->path);
848 thumb_req->source_id = source_id;
849 thumb_req->userData = req_manager->userData;
851 g_queue_push_tail(g_request_queue, (gpointer)thumb_req);
857 int _media_thumb_raw_data_send_request()
859 int err = MS_MEDIA_ERR_NONE;
861 struct sockaddr_un serv_addr;
862 ms_sock_info_s sock_info;
863 thumbRawReq *req_manager = NULL;
865 sock_info.port = MS_THUMB_CREATOR_PORT;
867 err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
868 if (err != MS_MEDIA_ERR_NONE) {
869 thumb_err("ms_ipc_create_client_socket failed");
873 memset(&serv_addr, 0, sizeof(serv_addr));
874 sock = sock_info.sock_fd;
875 serv_addr.sun_family = AF_UNIX;
876 strncpy(serv_addr.sun_path, THUMB_IPC_PATH, strlen(THUMB_IPC_PATH));
878 GIOChannel *channel = NULL;
879 channel = g_io_channel_unix_new(sock);
882 /* Connecting to the thumbnail server */
883 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
884 thumb_stderror("connect error");
885 g_io_channel_shutdown(channel, TRUE, NULL);
886 g_io_channel_unref(channel);
887 ms_ipc_delete_client_socket(&sock_info);
888 return MS_MEDIA_ERR_SOCKET_CONN;
891 req_manager = (thumbRawReq *)g_queue_pop_head(g_manage_raw_queue);
893 if (req_manager == NULL) {
894 thumb_err("queue pop fail");
895 g_io_channel_shutdown(channel, TRUE, NULL);
896 g_io_channel_unref(channel);
897 ms_ipc_delete_client_socket(&sock_info);
898 return MS_MEDIA_ERR_INVALID_PARAMETER;
901 GSource *source = NULL;
902 source = g_io_create_watch(channel, G_IO_IN);
903 g_source_set_callback(source, (GSourceFunc)_media_thumb_raw_data_write_socket, req_manager->userData, NULL);
904 source_id = g_source_attach(source, g_main_context_get_thread_default());
907 memset((void *)&req_msg, 0, sizeof(thumbMsg));
911 req_msg.msg_type = req_manager->msg_type;
912 req_msg.request_id = req_manager->request_id;
913 req_msg.thumb_width = req_manager->width;
914 req_msg.thumb_height = req_manager->height;
915 req_msg.uid = req_manager->uid;
917 strncpy(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
918 req_msg.org_path[strlen(req_msg.org_path)] = '\0';
919 req_msg.dst_path[0] = '\0';
921 req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
922 req_msg.dest_path_size = 1;
923 req_msg.thumb_size = 0;
925 if (req_msg.origin_path_size > MAX_PATH_SIZE) {
926 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
927 g_io_channel_shutdown(channel, TRUE, NULL);
928 g_io_channel_unref(channel);
929 return MS_MEDIA_ERR_INVALID_PARAMETER;
932 unsigned char *buf = NULL;
934 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
936 if (send(sock, buf, buf_size, 0) != buf_size) {
937 thumb_err("sendto failed: %d", errno);
939 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
940 g_io_channel_shutdown(channel, TRUE, NULL);
941 g_io_channel_unref(channel);
942 return MS_MEDIA_ERR_SOCKET_SEND;
947 if (req_manager->msg_type == THUMB_REQUEST_RAW_DATA) {
948 if (g_request_raw_queue == NULL) {
949 g_request_raw_queue = g_queue_new();
951 thumbRawReq *thumb_req = NULL;
952 THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
953 if (thumb_req == NULL) {
954 thumb_err("Failed to create request element");
955 return MS_MEDIA_ERR_INVALID_PARAMETER;
957 thumb_req->channel = channel;
958 thumb_req->request_id = req_manager->request_id;
959 thumb_req->source_id = source_id;
960 thumb_req->userData = req_manager->userData;
962 g_queue_push_tail(g_request_raw_queue, (gpointer)thumb_req);
964 return MS_MEDIA_ERR_NONE;
967 int _media_thumb_request_async(int msg_type, const char *origin_path, thumbUserData *userData, uid_t uid)
969 int err = MS_MEDIA_ERR_NONE;
971 if (g_manage_queue == NULL) {
972 if (msg_type == THUMB_REQUEST_CANCEL_MEDIA)
973 return MS_MEDIA_ERR_INTERNAL;
975 g_manage_queue = g_queue_new();
977 thumbReq *thumb_req = NULL;
978 THUMB_MALLOC(thumb_req, sizeof(thumbReq));
979 if (thumb_req == NULL) {
980 thumb_err("Failed to create request element");
981 return MS_MEDIA_ERR_INVALID_PARAMETER;
984 thumb_req->msg_type = msg_type;
985 thumb_req->path = strdup(origin_path);
986 thumb_req->userData = userData;
987 thumb_req->isCanceled = false;
988 thumb_req->uid = uid;
990 g_queue_push_tail(g_manage_queue, (gpointer)thumb_req);
992 /* directly request at first time */
993 err = _media_thumb_send_request();
996 if (msg_type != THUMB_REQUEST_CANCEL_MEDIA) {
998 if ((msg_type == THUMB_REQUEST_DB_INSERT) && (__media_thumb_check_req_queue(origin_path) < 0)) {
999 thumb_err("duplicated request");
1000 return MS_MEDIA_ERR_THUMB_DUPLICATED_REQUEST;
1003 thumbReq *thumb_req = NULL;
1004 THUMB_MALLOC(thumb_req, sizeof(thumbReq));
1005 if (thumb_req == NULL) {
1006 thumb_err("Failed to create request element");
1007 return MS_MEDIA_ERR_INVALID_PARAMETER;
1010 thumb_req->msg_type = msg_type;
1011 thumb_req->path = strdup(origin_path);
1012 thumb_req->userData = userData;
1013 thumb_req->isCanceled = false;
1014 thumb_req->uid = uid;
1016 g_queue_push_tail(g_manage_queue, (gpointer)thumb_req);
1019 err = __media_thumb_pop_manage_queue(origin_path);
1026 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)
1028 int err = MS_MEDIA_ERR_NONE;
1030 if (g_manage_raw_queue == NULL) {
1031 if (msg_type == THUMB_REQUEST_CANCEL_RAW_DATA)
1032 return MS_MEDIA_ERR_INTERNAL;
1034 g_manage_raw_queue = g_queue_new();
1036 thumbRawReq *thumb_req = NULL;
1037 THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
1038 if (thumb_req == NULL) {
1039 thumb_err("Failed to create request element");
1040 return MS_MEDIA_ERR_INVALID_PARAMETER;
1043 thumb_req->msg_type = msg_type;
1044 thumb_req->request_id = request_id;
1045 thumb_req->path = strdup(origin_path);
1046 thumb_req->width = width;
1047 thumb_req->height = height;
1048 thumb_req->userData = userData;
1049 thumb_req->isCanceled = false;
1050 thumb_req->uid = uid;
1052 g_queue_push_tail(g_manage_raw_queue, (gpointer)thumb_req);
1054 /* directly request at first time */
1055 err = _media_thumb_raw_data_send_request();
1058 if (msg_type != THUMB_REQUEST_CANCEL_RAW_DATA) {
1060 thumbRawReq *thumb_req = NULL;
1061 THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
1062 if (thumb_req == NULL) {
1063 thumb_err("Failed to create request element");
1064 return MS_MEDIA_ERR_INVALID_PARAMETER;
1067 thumb_req->msg_type = msg_type;
1068 thumb_req->request_id = request_id;
1069 thumb_req->path = strdup(origin_path);
1070 thumb_req->width = width;
1071 thumb_req->height = height;
1072 thumb_req->userData = userData;
1073 thumb_req->isCanceled = false;
1074 thumb_req->uid = uid;
1076 g_queue_push_tail(g_manage_raw_queue, (gpointer)thumb_req);
1080 err = __media_thumb_pop_raw_data_manage_queue(request_id);
1087 int _media_thumb_request_cancel_all(bool isRaw)
1094 thumbRawReq *tmp_raw_req = NULL;
1095 if (g_manage_raw_queue == NULL) {
1096 thumb_err("manage_queue is NULL");
1097 if (g_request_raw_queue != NULL) {
1098 /* Check request queue */
1099 len = g_queue_get_length(g_request_raw_queue);
1101 tmp_raw_req = g_queue_peek_head(g_request_raw_queue);
1102 if (tmp_raw_req != NULL)
1103 tmp_raw_req->isCanceled = true;
1104 return MS_MEDIA_ERR_NONE;
1107 thumb_err("request_queue is NULL");
1108 return MS_MEDIA_ERR_INTERNAL;
1110 req_len = g_queue_get_length(g_manage_raw_queue);
1111 for (i = 0; i < req_len; i++) {
1112 tmp_raw_req = g_queue_pop_tail(g_manage_raw_queue);
1114 SAFE_FREE(tmp_raw_req->path);
1115 SAFE_FREE(tmp_raw_req->userData);
1116 SAFE_FREE(tmp_raw_req);
1120 g_queue_free(g_manage_raw_queue);
1121 g_manage_raw_queue = NULL;
1122 if (g_request_raw_queue != NULL) {
1123 /* Check request queue */
1124 len = g_queue_get_length(g_request_raw_queue);
1126 tmp_raw_req = g_queue_peek_head(g_request_raw_queue);
1127 if (tmp_raw_req != NULL)
1128 tmp_raw_req->isCanceled = true;
1132 thumbReq *tmp_req = NULL;
1133 if (g_manage_queue == NULL) {
1134 thumb_err("manage_queue is NULL");
1135 if (g_request_queue != NULL) {
1136 /* Check request queue */
1137 len = g_queue_get_length(g_request_queue);
1139 tmp_req = g_queue_peek_head(g_request_queue);
1140 if (tmp_req != NULL)
1141 tmp_req->isCanceled = true;
1142 return MS_MEDIA_ERR_NONE;
1145 thumb_err("request_queue is NULL");
1146 return MS_MEDIA_ERR_INTERNAL;
1148 req_len = g_queue_get_length(g_manage_queue);
1149 for (i = 0; i < req_len; i++) {
1150 tmp_req = g_queue_pop_tail(g_manage_queue);
1152 SAFE_FREE(tmp_req->path);
1153 SAFE_FREE(tmp_req->userData);
1158 g_queue_free(g_manage_queue);
1159 g_manage_queue = NULL;
1160 if (g_request_queue != NULL) {
1161 /* Check request queue */
1162 len = g_queue_get_length(g_request_queue);
1164 tmp_req = g_queue_peek_head(g_request_queue);
1165 if (tmp_req != NULL)
1166 tmp_req->isCanceled = true;
1171 return MS_MEDIA_ERR_NONE;