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 */
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 return MS_MEDIA_ERR_SOCKET_CONN;
775 req_manager = (thumbReq *)g_queue_pop_head(g_manage_queue);
776 GSource *source = NULL;
777 source = g_io_create_watch(channel, G_IO_IN);
778 g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, req_manager->userData, NULL);
779 source_id = g_source_attach(source, g_main_context_get_thread_default());
783 memset((void *)&req_msg, 0, sizeof(thumbMsg));
787 req_msg.msg_type = req_manager->msg_type;
788 req_msg.request_id = 0;
789 req_msg.uid = req_manager->uid;
790 strncpy(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
791 req_msg.org_path[strlen(req_msg.org_path)] = '\0';
792 req_msg.dst_path[0] = '\0';
793 req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
794 req_msg.dest_path_size = 1;
795 req_msg.thumb_size = 0;
797 if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
798 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
799 g_io_channel_shutdown(channel, TRUE, NULL);
800 g_io_channel_unref(channel);
801 ms_ipc_delete_client_socket(&sock_info);
802 return MS_MEDIA_ERR_INVALID_PARAMETER;
805 unsigned char *buf = NULL;
807 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
809 if (send(sock, buf, buf_size, 0) != buf_size) {
810 thumb_err("sendto failed: %d", errno);
812 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
813 g_io_channel_shutdown(channel, TRUE, NULL);
814 g_io_channel_unref(channel);
815 ms_ipc_delete_client_socket(&sock_info);
816 return MS_MEDIA_ERR_SOCKET_SEND;
820 thumb_dbg("Sending msg to thumbnail daemon is successful");
823 if (req_manager->msg_type == THUMB_REQUEST_DB_INSERT) {
824 if (g_request_queue == NULL) {
825 g_request_queue = g_queue_new();
828 thumbReq *thumb_req = NULL;
829 THUMB_MALLOC(thumb_req, sizeof(thumbReq));
830 if (thumb_req == NULL) {
831 thumb_err("Failed to create request element");
832 return MS_MEDIA_ERR_INVALID_PARAMETER;
835 thumb_req->channel = channel;
836 thumb_req->path = strdup(req_manager->path);
837 thumb_req->source_id = source_id;
838 thumb_req->userData = req_manager->userData;
840 g_queue_push_tail(g_request_queue, (gpointer)thumb_req);
846 int _media_thumb_raw_data_send_request()
848 int err = MS_MEDIA_ERR_NONE;
850 struct sockaddr_un serv_addr;
851 ms_sock_info_s sock_info;
852 thumbRawReq *req_manager = NULL;
854 sock_info.port = MS_THUMB_CREATOR_PORT;
856 err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
857 if (err != MS_MEDIA_ERR_NONE) {
858 thumb_err("ms_ipc_create_client_socket failed");
862 memset(&serv_addr, 0, sizeof(serv_addr));
863 sock = sock_info.sock_fd;
864 serv_addr.sun_family = AF_UNIX;
865 strncpy(serv_addr.sun_path, THUMB_IPC_PATH, strlen(THUMB_IPC_PATH));
867 GIOChannel *channel = NULL;
868 channel = g_io_channel_unix_new(sock);
871 /* Connecting to the thumbnail server */
872 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
873 thumb_stderror("connect error");
874 g_io_channel_shutdown(channel, TRUE, NULL);
875 g_io_channel_unref(channel);
876 return MS_MEDIA_ERR_SOCKET_CONN;
879 req_manager = (thumbRawReq *)g_queue_pop_head(g_manage_raw_queue);
880 GSource *source = NULL;
881 source = g_io_create_watch(channel, G_IO_IN);
882 g_source_set_callback(source, (GSourceFunc)_media_thumb_raw_data_write_socket, req_manager->userData, NULL);
883 source_id = g_source_attach(source, g_main_context_get_thread_default());
886 memset((void *)&req_msg, 0, sizeof(thumbMsg));
890 req_msg.msg_type = req_manager->msg_type;
891 req_msg.request_id = req_manager->request_id;
892 req_msg.thumb_width = req_manager->width;
893 req_msg.thumb_height = req_manager->height;
894 req_msg.uid = req_manager->uid;
896 strncpy(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
897 req_msg.org_path[strlen(req_msg.org_path)] = '\0';
898 req_msg.dst_path[0] = '\0';
900 req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
901 req_msg.dest_path_size = 1;
902 req_msg.thumb_size = 0;
904 if (req_msg.origin_path_size > MAX_PATH_SIZE) {
905 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
906 g_io_channel_shutdown(channel, TRUE, NULL);
907 g_io_channel_unref(channel);
908 return MS_MEDIA_ERR_INVALID_PARAMETER;
911 unsigned char *buf = NULL;
913 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
915 if (send(sock, buf, buf_size, 0) != buf_size) {
916 thumb_err("sendto failed: %d", errno);
918 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
919 g_io_channel_shutdown(channel, TRUE, NULL);
920 g_io_channel_unref(channel);
921 return MS_MEDIA_ERR_SOCKET_SEND;
926 if (req_manager->msg_type == THUMB_REQUEST_RAW_DATA) {
927 if (g_request_raw_queue == NULL) {
928 g_request_raw_queue = g_queue_new();
930 thumbRawReq *thumb_req = NULL;
931 THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
932 if (thumb_req == NULL) {
933 thumb_err("Failed to create request element");
934 return MS_MEDIA_ERR_INVALID_PARAMETER;
936 thumb_req->channel = channel;
937 thumb_req->request_id = req_manager->request_id;
938 thumb_req->source_id = source_id;
939 thumb_req->userData = req_manager->userData;
941 g_queue_push_tail(g_request_raw_queue, (gpointer)thumb_req);
943 return MS_MEDIA_ERR_NONE;
946 int _media_thumb_request_async(int msg_type, const char *origin_path, thumbUserData *userData, uid_t uid)
948 int err = MS_MEDIA_ERR_NONE;
950 if (g_manage_queue == NULL) {
951 if (msg_type == THUMB_REQUEST_CANCEL_MEDIA)
952 return MS_MEDIA_ERR_INTERNAL;
954 g_manage_queue = g_queue_new();
956 thumbReq *thumb_req = NULL;
957 THUMB_MALLOC(thumb_req, sizeof(thumbReq));
958 if (thumb_req == NULL) {
959 thumb_err("Failed to create request element");
960 return MS_MEDIA_ERR_INVALID_PARAMETER;
963 thumb_req->msg_type = msg_type;
964 thumb_req->path = strdup(origin_path);
965 thumb_req->userData = userData;
966 thumb_req->isCanceled = false;
967 thumb_req->uid = uid;
969 g_queue_push_tail(g_manage_queue, (gpointer)thumb_req);
971 /* directly request at first time */
972 err = _media_thumb_send_request();
975 if (msg_type != THUMB_REQUEST_CANCEL_MEDIA) {
977 if ((msg_type == THUMB_REQUEST_DB_INSERT) && (__media_thumb_check_req_queue(origin_path) < 0)) {
978 thumb_err("duplicated request");
979 return MS_MEDIA_ERR_THUMB_DUPLICATED_REQUEST;
982 thumbReq *thumb_req = NULL;
983 THUMB_MALLOC(thumb_req, sizeof(thumbReq));
984 if (thumb_req == NULL) {
985 thumb_err("Failed to create request element");
986 return MS_MEDIA_ERR_INVALID_PARAMETER;
989 thumb_req->msg_type = msg_type;
990 thumb_req->path = strdup(origin_path);
991 thumb_req->userData = userData;
992 thumb_req->isCanceled = false;
993 thumb_req->uid = uid;
995 g_queue_push_tail(g_manage_queue, (gpointer)thumb_req);
998 err = __media_thumb_pop_manage_queue(origin_path);
1005 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)
1007 int err = MS_MEDIA_ERR_NONE;
1009 if (g_manage_raw_queue == NULL) {
1010 if (msg_type == THUMB_REQUEST_CANCEL_RAW_DATA)
1011 return MS_MEDIA_ERR_INTERNAL;
1013 g_manage_raw_queue = g_queue_new();
1015 thumbRawReq *thumb_req = NULL;
1016 THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
1017 if (thumb_req == NULL) {
1018 thumb_err("Failed to create request element");
1019 return MS_MEDIA_ERR_INVALID_PARAMETER;
1022 thumb_req->msg_type = msg_type;
1023 thumb_req->request_id = request_id;
1024 thumb_req->path = strdup(origin_path);
1025 thumb_req->width = width;
1026 thumb_req->height = height;
1027 thumb_req->userData = userData;
1028 thumb_req->isCanceled = false;
1029 thumb_req->uid = uid;
1031 g_queue_push_tail(g_manage_raw_queue, (gpointer)thumb_req);
1033 /* directly request at first time */
1034 err = _media_thumb_raw_data_send_request();
1037 if (msg_type != THUMB_REQUEST_CANCEL_RAW_DATA) {
1039 thumbRawReq *thumb_req = NULL;
1040 THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
1041 if (thumb_req == NULL) {
1042 thumb_err("Failed to create request element");
1043 return MS_MEDIA_ERR_INVALID_PARAMETER;
1046 thumb_req->msg_type = msg_type;
1047 thumb_req->request_id = request_id;
1048 thumb_req->path = strdup(origin_path);
1049 thumb_req->width = width;
1050 thumb_req->height = height;
1051 thumb_req->userData = userData;
1052 thumb_req->isCanceled = false;
1053 thumb_req->uid = uid;
1055 g_queue_push_tail(g_manage_raw_queue, (gpointer)thumb_req);
1059 err = __media_thumb_pop_raw_data_manage_queue(request_id);
1066 int _media_thumb_request_cancel_all(bool isRaw)
1073 thumbRawReq *tmp_raw_req = NULL;
1074 if (g_manage_raw_queue == NULL) {
1075 thumb_err("manage_queue is NULL");
1076 if (g_request_raw_queue != NULL) {
1077 /* Check request queue */
1078 len = g_queue_get_length(g_request_raw_queue);
1080 tmp_raw_req = g_queue_peek_head(g_request_raw_queue);
1081 if (tmp_raw_req != NULL)
1082 tmp_raw_req->isCanceled = true;
1083 return MS_MEDIA_ERR_NONE;
1086 thumb_err("request_queue is NULL");
1087 return MS_MEDIA_ERR_INTERNAL;
1089 req_len = g_queue_get_length(g_manage_raw_queue);
1090 for (i = 0; i < req_len; i++) {
1091 tmp_raw_req = g_queue_pop_tail(g_manage_raw_queue);
1093 SAFE_FREE(tmp_raw_req->path);
1094 SAFE_FREE(tmp_raw_req->userData);
1095 SAFE_FREE(tmp_raw_req);
1099 g_queue_free(g_manage_raw_queue);
1100 g_manage_raw_queue = NULL;
1101 if (g_request_raw_queue != NULL) {
1102 /* Check request queue */
1103 len = g_queue_get_length(g_request_raw_queue);
1105 tmp_raw_req = g_queue_peek_head(g_request_raw_queue);
1106 if (tmp_raw_req != NULL)
1107 tmp_raw_req->isCanceled = true;
1111 thumbReq *tmp_req = NULL;
1112 if (g_manage_queue == NULL) {
1113 thumb_err("manage_queue is NULL");
1114 if (g_request_queue != NULL) {
1115 /* Check request queue */
1116 len = g_queue_get_length(g_request_queue);
1118 tmp_req = g_queue_peek_head(g_request_queue);
1119 if (tmp_req != NULL)
1120 tmp_req->isCanceled = true;
1121 return MS_MEDIA_ERR_NONE;
1124 thumb_err("request_queue is NULL");
1125 return MS_MEDIA_ERR_INTERNAL;
1127 req_len = g_queue_get_length(g_manage_queue);
1128 for (i = 0; i < req_len; i++) {
1129 tmp_req = g_queue_pop_tail(g_manage_queue);
1131 SAFE_FREE(tmp_req->path);
1132 SAFE_FREE(tmp_req->userData);
1137 g_queue_free(g_manage_queue);
1138 g_manage_queue = NULL;
1139 if (g_request_queue != NULL) {
1140 /* Check request queue */
1141 len = g_queue_get_length(g_request_queue);
1143 tmp_req = g_queue_peek_head(g_request_queue);
1144 if (tmp_req != NULL)
1145 tmp_req->isCanceled = true;
1150 return MS_MEDIA_ERR_NONE;