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"
31 static __thread GQueue *g_request_queue = NULL;
39 _media_thumb_create_socket(int sock_type, int *sock)
43 if ((sock_fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
44 thumb_err("socket failed: %s", strerror(errno));
45 return MEDIA_THUMB_ERROR_NETWORK;
48 if (sock_type == CLIENT_SOCKET) {
50 #ifdef _USE_MEDIA_UTIL_
51 struct timeval tv_timeout = { MS_TIMEOUT_SEC_10, 0 };
53 struct timeval tv_timeout = { TIMEOUT_SEC, 0 };
56 if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &tv_timeout, sizeof(tv_timeout)) == -1) {
57 thumb_err("setsockopt failed: %s", strerror(errno));
59 return MEDIA_THUMB_ERROR_NETWORK;
61 } else if (sock_type == SERVER_SOCKET) {
65 if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &n_reuse, sizeof(n_reuse)) == -1) {
66 thumb_err("setsockopt failed: %s", strerror(errno));
68 return MEDIA_THUMB_ERROR_NETWORK;
74 return MEDIA_THUMB_ERROR_NONE;
79 _media_thumb_create_udp_socket(int *sock)
83 if ((sock_fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
84 thumb_err("socket failed: %s", strerror(errno));
85 return MEDIA_THUMB_ERROR_NETWORK;
88 #ifdef _USE_MEDIA_UTIL_
89 struct timeval tv_timeout = { MS_TIMEOUT_SEC_10, 0 };
91 struct timeval tv_timeout = { TIMEOUT_SEC, 0 };
94 if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &tv_timeout, sizeof(tv_timeout)) == -1) {
95 thumb_err("setsockopt failed: %s", strerror(errno));
97 return MEDIA_THUMB_ERROR_NETWORK;
102 return MEDIA_THUMB_ERROR_NONE;
105 int _media_thumb_get_error()
107 if (errno == EWOULDBLOCK) {
108 thumb_err("Timeout. Can't try any more");
109 return MEDIA_THUMB_ERROR_TIMEOUT;
111 thumb_err("recvfrom failed : %s", strerror(errno));
112 return MEDIA_THUMB_ERROR_NETWORK;
116 int __media_thumb_pop_req_queue(const char *path, bool shutdown_channel)
120 if (g_request_queue == NULL) return -1;
121 req_len = g_queue_get_length(g_request_queue);
123 // thumb_dbg("Queue length : %d", req_len);
124 // thumb_dbg("Queue path : %s", path);
127 // thumb_dbg("There is no request in the queue");
130 for (i = 0; i < req_len; i++) {
131 thumbReq *req = NULL;
132 req = (thumbReq *)g_queue_peek_nth(g_request_queue, i);
133 if (req == NULL) continue;
135 if (strncmp(path, req->path, strlen(path)) == 0) {
136 //thumb_dbg("Popped %s", path);
137 if (shutdown_channel) {
138 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id));
140 g_io_channel_shutdown(req->channel, TRUE, NULL);
141 g_io_channel_unref(req->channel);
143 g_queue_pop_nth(g_request_queue, i);
145 SAFE_FREE(req->path);
152 // thumb_dbg("There's no %s in the queue", path);
156 return MEDIA_THUMB_ERROR_NONE;
159 int __media_thumb_check_req_queue(const char *path)
163 req_len = g_queue_get_length(g_request_queue);
165 // thumb_dbg("Queue length : %d", req_len);
166 // thumb_dbg("Queue path : %s", path);
169 // thumb_dbg("There is no request in the queue");
172 for (i = 0; i < req_len; i++) {
173 thumbReq *req = NULL;
174 req = (thumbReq *)g_queue_peek_nth(g_request_queue, i);
175 if (req == NULL) continue;
177 if (strncmp(path, req->path, strlen(path)) == 0) {
178 //thumb_dbg("Same Request - %s", path);
186 return MEDIA_THUMB_ERROR_NONE;
190 _media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg)
192 int recv_msg_len = 0;
193 unsigned char *buf = NULL;
195 buf = (unsigned char*)malloc(header_size);
197 if ((recv_msg_len = recv(sock, buf, header_size, 0)) <= 0) {
198 thumb_err("recvfrom failed : %s", strerror(errno));
200 return _media_thumb_get_error();
203 memcpy(msg, buf, header_size);
204 //thumb_dbg("origin_path_size : %d, dest_path_size : %d", msg->origin_path_size, msg->dest_path_size);
208 buf = (unsigned char*)malloc(msg->origin_path_size);
210 if ((recv_msg_len = recv(sock, buf, msg->origin_path_size, 0)) < 0) {
211 thumb_err("recvfrom failed : %s", strerror(errno));
213 return _media_thumb_get_error();
216 strncpy(msg->org_path, (char*)buf, msg->origin_path_size);
217 //thumb_dbg("original path : %s", msg->org_path);
220 buf = (unsigned char*)malloc(msg->dest_path_size);
222 if ((recv_msg_len = recv(sock, buf, msg->dest_path_size, 0)) < 0) {
223 thumb_err("recvfrom failed : %s", strerror(errno));
225 return _media_thumb_get_error();
228 strncpy(msg->dst_path, (char*)buf, msg->dest_path_size);
229 //thumb_dbg("destination path : %s", msg->dst_path);
232 return MEDIA_THUMB_ERROR_NONE;
235 #ifdef _USE_UDS_SOCKET_
237 _media_thumb_recv_udp_msg(int sock, int header_size, thumbMsg *msg, struct sockaddr_un *from_addr, unsigned int *from_size)
240 _media_thumb_recv_udp_msg(int sock, int header_size, thumbMsg *msg, struct sockaddr_in *from_addr, unsigned int *from_size)
243 int recv_msg_len = 0;
244 #ifdef _USE_UDS_SOCKET_
245 unsigned int from_addr_size = sizeof(struct sockaddr_un);
247 unsigned int from_addr_size = sizeof(struct sockaddr_in);
249 unsigned char *buf = NULL;
251 buf = (unsigned char*)malloc(sizeof(thumbMsg));
253 if ((recv_msg_len = recvfrom(sock, buf, sizeof(thumbMsg), 0, (struct sockaddr *)from_addr, &from_addr_size)) < 0) {
254 thumb_err("recvfrom failed : %s", strerror(errno));
256 return _media_thumb_get_error();
259 memcpy(msg, buf, header_size);
261 if (msg->origin_path_size <= 0 || msg->origin_path_size > MAX_PATH_SIZE) {
263 thumb_err("msg->origin_path_size is invalid %d", msg->origin_path_size );
264 return MEDIA_THUMB_ERROR_NETWORK;
267 strncpy(msg->org_path, (char*)buf + header_size, msg->origin_path_size);
268 //thumb_dbg("original path : %s", msg->org_path);
270 if (msg->dest_path_size <= 0 || msg->dest_path_size > MAX_PATH_SIZE) {
272 thumb_err("msg->origin_path_size is invalid %d", msg->dest_path_size );
273 return MEDIA_THUMB_ERROR_NETWORK;
276 strncpy(msg->dst_path, (char*)buf + header_size + msg->origin_path_size, msg->dest_path_size);
277 //thumb_dbg("destination path : %s", msg->dst_path);
280 *from_size = from_addr_size;
282 return MEDIA_THUMB_ERROR_NONE;
286 _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
288 if (req_msg == NULL || buf == NULL) {
292 int org_path_len = 0;
293 int dst_path_len = 0;
297 header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2;
298 org_path_len = strlen(req_msg->org_path) + 1;
299 dst_path_len = strlen(req_msg->dst_path) + 1;
301 //thumb_dbg("Basic Size : %d, org_path : %s[%d], dst_path : %s[%d]", header_size, req_msg->org_path, org_path_len, req_msg->dst_path, dst_path_len);
303 size = header_size + org_path_len + dst_path_len;
305 memcpy(*buf, req_msg, header_size);
306 memcpy((*buf)+header_size, req_msg->org_path, org_path_len);
307 memcpy((*buf)+header_size + org_path_len, req_msg->dst_path, dst_path_len);
315 _media_thumb_request(int msg_type, media_thumb_type thumb_type, const char *origin_path, char *thumb_path, int max_length, media_thumb_info *thumb_info)
318 #ifdef _USE_UDS_SOCKET_
319 struct sockaddr_un serv_addr;
320 #elif defined(_USE_UDS_SOCKET_TCP_)
321 struct sockaddr_un serv_addr;
323 const char *serv_ip = "127.0.0.1";
324 struct sockaddr_in serv_addr;
327 int recv_str_len = 0;
332 #ifdef _USE_MEDIA_UTIL_
333 #ifdef _USE_UDS_SOCKET_
334 if (ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock, MS_THUMB_CREATOR_PORT) < 0) {
335 #elif defined(_USE_UDS_SOCKET_TCP_)
336 if (ms_ipc_create_client_tcp_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock, MS_THUMB_CREATOR_TCP_PORT) < 0) {
338 if (ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock) < 0) {
340 thumb_err("ms_ipc_create_client_socket failed");
341 return MEDIA_THUMB_ERROR_NETWORK;
344 /* Creaete a TCP socket */
345 if (_media_thumb_create_socket(CLIENT_SOCKET, &sock) < 0) {
346 thumb_err("_media_thumb_create_socket failed");
347 return MEDIA_THUMB_ERROR_NETWORK;
351 memset(&serv_addr, 0, sizeof(serv_addr));
352 #ifdef _USE_UDS_SOCKET_
353 serv_addr.sun_family = AF_UNIX;
354 #elif defined(_USE_UDS_SOCKET_TCP_)
355 serv_addr.sun_family = AF_UNIX;
357 serv_addr.sin_family = AF_INET;
358 serv_addr.sin_addr.s_addr = inet_addr(serv_ip);
361 #ifdef _USE_MEDIA_UTIL_
362 #ifdef _USE_UDS_SOCKET_
363 strcpy(serv_addr.sun_path, "/tmp/media_ipc_thumbcreator.dat");
364 #elif defined(_USE_UDS_SOCKET_TCP_)
366 strcpy(serv_addr.sun_path, "/tmp/media_ipc_thumbcreator.dat");
368 serv_addr.sin_port = htons(MS_THUMB_CREATOR_PORT);
371 serv_addr.sin_port = htons(THUMB_DAEMON_PORT);
374 /* Connecting to the thumbnail server */
375 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
376 thumb_err("connect error : %s", strerror(errno));
377 return MEDIA_THUMB_ERROR_NETWORK;
383 memset((void *)&req_msg, 0, sizeof(thumbMsg));
384 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
386 /* Get PID of client*/
390 /* Set requset message */
391 req_msg.msg_type = msg_type;
392 req_msg.thumb_type = thumb_type;
393 strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
394 req_msg.org_path[strlen(req_msg.org_path)] = '\0';
396 if (msg_type == THUMB_REQUEST_SAVE_FILE) {
397 strncpy(req_msg.dst_path, thumb_path, sizeof(req_msg.dst_path));
398 req_msg.dst_path[strlen(req_msg.dst_path)] = '\0';
401 req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
402 req_msg.dest_path_size = strlen(req_msg.dst_path) + 1;
404 if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
405 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
407 return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
410 unsigned char *buf = NULL;
414 header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2;
415 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
417 if (send(sock, buf, buf_size, 0) != buf_size) {
418 thumb_err("sendto failed: %d\n", errno);
421 return MEDIA_THUMB_ERROR_NETWORK;
424 thumb_dbg("Sending msg to thumbnail daemon is successful");
428 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
429 thumb_err("_media_thumb_recv_msg failed ");
434 recv_str_len = strlen(recv_msg.org_path);
435 thumb_dbg("recv %s(%d) from thumb daemon is successful", recv_msg.org_path, recv_str_len);
439 if (recv_str_len > max_length) {
440 thumb_err("user buffer is too small. Output's length is %d", recv_str_len);
441 return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
444 if (recv_msg.status == THUMB_FAIL) {
445 thumb_err("Failed to make thumbnail");
449 if (msg_type != THUMB_REQUEST_SAVE_FILE) {
450 strncpy(thumb_path, recv_msg.dst_path, max_length);
453 thumb_info->origin_width = recv_msg.origin_width;
454 thumb_info->origin_height = recv_msg.origin_height;
460 _media_thumb_process(thumbMsg *req_msg, thumbMsg *res_msg)
463 unsigned char *data = NULL;
470 char *thumb_path = NULL;
471 int need_update_db = 0;
474 if (req_msg == NULL || res_msg == NULL) {
475 thumb_err("Invalid msg!");
476 return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
479 int msg_type = req_msg->msg_type;
480 media_thumb_type thumb_type = req_msg->thumb_type;
481 const char *origin_path = req_msg->org_path;
483 media_thumb_format thumb_format = MEDIA_THUMB_BGRA;
485 thumb_path = res_msg->dst_path;
486 thumb_path[0] = '\0';
487 max_length = sizeof(res_msg->dst_path);
489 err = _media_thumb_db_connect();
491 thumb_err("_media_thumb_mb_svc_connect failed: %d", err);
492 return MEDIA_THUMB_ERROR_DB;
495 if (msg_type == THUMB_REQUEST_DB_INSERT) {
496 err = _media_thumb_get_thumb_from_db_with_size(origin_path, thumb_path, max_length, &need_update_db, &origin_w, &origin_h);
498 res_msg->origin_width = origin_w;
499 res_msg->origin_height = origin_h;
500 _media_thumb_db_disconnect();
501 return MEDIA_THUMB_ERROR_NONE;
503 if (strlen(thumb_path) == 0) {
504 err = _media_thumb_get_hash_name(origin_path, thumb_path, max_length);
506 thumb_err("_media_thumb_get_hash_name failed - %d\n", err);
507 strncpy(thumb_path, THUMB_DEFAULT_PATH, max_length);
508 _media_thumb_db_disconnect();
512 thumb_path[strlen(thumb_path)] = '\0';
516 } else if (msg_type == THUMB_REQUEST_SAVE_FILE) {
517 strncpy(thumb_path, req_msg->dst_path, max_length);
519 } else if (msg_type == THUMB_REQUEST_ALL_MEDIA) {
520 err = _media_thumb_get_hash_name(origin_path, thumb_path, max_length);
522 thumb_err("_media_thumb_get_hash_name failed - %d\n", err);
523 strncpy(thumb_path, THUMB_DEFAULT_PATH, max_length);
524 _media_thumb_db_disconnect();
528 thumb_path[strlen(thumb_path)] = '\0';
531 thumb_dbg("Thumb path : %s", thumb_path);
533 if (g_file_test(thumb_path,
534 G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
535 thumb_warn("thumb path already exists in file system.. remove the existed file");
536 _media_thumb_remove_file(thumb_path);
539 err = _thumbnail_get_data(origin_path, thumb_type, thumb_format, &data, &thumb_size, &thumb_w, &thumb_h, &origin_w, &origin_h, &alpha);
541 thumb_err("_thumbnail_get_data failed - %d\n", err);
544 strncpy(thumb_path, THUMB_DEFAULT_PATH, max_length);
545 _media_thumb_db_disconnect();
549 //thumb_dbg("Size : %d, W:%d, H:%d", thumb_size, thumb_w, thumb_h);
550 //thumb_dbg("Origin W:%d, Origin H:%d\n", origin_w, origin_h);
551 //thumb_dbg("Thumb : %s", thumb_path);
553 res_msg->msg_type = THUMB_RESPONSE;
554 res_msg->thumb_size = thumb_size;
555 res_msg->thumb_width = thumb_w;
556 res_msg->thumb_height = thumb_h;
557 res_msg->origin_width = origin_w;
558 res_msg->origin_height = origin_h;
560 /* If the image is transparent PNG format, make png file as thumbnail of this image */
563 err = _media_thumb_get_file_ext(origin_path, file_ext, sizeof(file_ext));
564 if (strncasecmp(file_ext, "png", 3) == 0) {
565 int len = strlen(thumb_path);
566 thumb_path[len - 3] = 'p';
567 thumb_path[len - 2] = 'n';
568 thumb_path[len - 1] = 'g';
570 thumb_dbg("Thumb path is changed : %s", thumb_path);
573 err = _media_thumb_save_to_file_with_evas(data, thumb_w, thumb_h, alpha, thumb_path);
575 thumb_err("save_to_file_with_evas failed - %d\n", err);
578 if (msg_type == THUMB_REQUEST_DB_INSERT || msg_type == THUMB_REQUEST_ALL_MEDIA)
579 strncpy(thumb_path, THUMB_DEFAULT_PATH, max_length);
581 _media_thumb_db_disconnect();
584 thumb_dbg("file save success\n");
589 fd = open(thumb_path, O_WRONLY);
591 thumb_warn("open failed");
595 thumb_warn("fsync failed");
604 /* DB update if needed */
605 if (need_update_db == 1) {
606 err = _media_thumb_update_db(origin_path, thumb_path, res_msg->origin_width, res_msg->origin_height);
608 thumb_err("_media_thumb_update_db failed : %d", err);
612 _media_thumb_db_disconnect();
617 gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
621 int recv_str_len = 0;
623 int err = MEDIA_THUMB_ERROR_NONE;
625 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
626 sock = g_io_channel_unix_get_fd(src);
628 header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2;
630 thumb_err("_media_thumb_write_socket socket : %d", sock);
632 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
633 thumb_err("_media_thumb_recv_msg failed ");
634 g_io_channel_shutdown(src, TRUE, NULL);
638 recv_str_len = strlen(recv_msg.dst_path);
639 thumb_dbg("recv %s(%d) in [ %s ] from thumb daemon is successful", recv_msg.dst_path, recv_str_len, recv_msg.org_path);
641 g_io_channel_shutdown(src, TRUE, NULL);
643 //thumb_dbg("Completed..%s", recv_msg.org_path);
644 __media_thumb_pop_req_queue(recv_msg.org_path, FALSE);
646 if (recv_msg.status == THUMB_FAIL) {
647 thumb_err("Failed to make thumbnail");
648 err = MEDIA_THUMB_ERROR_UNSUPPORTED;
654 thumbUserData* cb = (thumbUserData*)data;
655 cb->func(err, recv_msg.dst_path, cb->user_data);
665 _media_thumb_request_async(int msg_type, media_thumb_type thumb_type, const char *origin_path, thumbUserData *userData)
668 #ifdef _USE_UDS_SOCKET_
669 struct sockaddr_un serv_addr;
670 #elif defined(_USE_UDS_SOCKET_TCP_)
671 struct sockaddr_un serv_addr;
673 const char *serv_ip = "127.0.0.1";
674 struct sockaddr_in serv_addr;
679 if ((msg_type == THUMB_REQUEST_DB_INSERT) && (__media_thumb_check_req_queue(origin_path) < 0)) {
680 return MEDIA_THUMB_ERROR_DUPLICATED_REQUEST;
683 #ifdef _USE_MEDIA_UTIL_
684 #ifdef _USE_UDS_SOCKET_
685 if (ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock, MS_THUMB_CREATOR_PORT) < 0) {
686 #elif defined(_USE_UDS_SOCKET_TCP_)
687 if (ms_ipc_create_client_tcp_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock, MS_THUMB_CREATOR_TCP_PORT) < 0) {
689 if (ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock) < 0) {
691 thumb_err("ms_ipc_create_client_socket failed");
692 return MEDIA_THUMB_ERROR_NETWORK;
695 /* Creaete a TCP socket */
696 if (_media_thumb_create_socket(CLIENT_SOCKET, &sock) < 0) {
697 thumb_err("_media_thumb_create_socket failed");
698 return MEDIA_THUMB_ERROR_NETWORK;
702 GIOChannel *channel = NULL;
703 channel = g_io_channel_unix_new(sock);
707 memset(&serv_addr, 0, sizeof(serv_addr));
708 #ifdef _USE_UDS_SOCKET_
709 serv_addr.sun_family = AF_UNIX;
710 #elif defined(_USE_UDS_SOCKET_TCP_)
711 serv_addr.sun_family = AF_UNIX;
713 serv_addr.sin_family = AF_INET;
714 serv_addr.sin_addr.s_addr = inet_addr(serv_ip);
717 #ifdef _USE_MEDIA_UTIL_
718 #ifdef _USE_UDS_SOCKET_
719 strcpy(serv_addr.sun_path, "/tmp/media_ipc_thumbcreator.dat");
720 #elif defined(_USE_UDS_SOCKET_TCP_)
721 strcpy(serv_addr.sun_path, "/tmp/media_ipc_thumbcreator.dat");
723 serv_addr.sin_port = htons(MS_THUMB_CREATOR_PORT);
726 serv_addr.sin_port = htons(THUMB_DAEMON_PORT);
729 /* Connecting to the thumbnail server */
730 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
731 thumb_err("connect error : %s", strerror(errno));
732 g_io_channel_shutdown(channel, TRUE, NULL);
733 return MEDIA_THUMB_ERROR_NETWORK;
736 if (msg_type != THUMB_REQUEST_CANCEL_MEDIA) {
737 //source_id = g_io_add_watch(channel, G_IO_IN, _media_thumb_write_socket, userData );
739 /* Create new channel to watch udp socket */
740 GSource *source = NULL;
741 source = g_io_create_watch(channel, G_IO_IN);
743 /* Set callback to be called when socket is readable */
744 g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, userData, NULL);
745 source_id = g_source_attach(source, g_main_context_get_thread_default());
749 memset((void *)&req_msg, 0, sizeof(thumbMsg));
753 req_msg.msg_type = msg_type;
754 req_msg.thumb_type = thumb_type;
755 strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
756 req_msg.org_path[strlen(req_msg.org_path)] = '\0';
758 req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
759 req_msg.dest_path_size = strlen(req_msg.dst_path) + 1;
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 return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
767 unsigned char *buf = NULL;
769 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
771 //thumb_dbg("buffer size : %d", buf_size);
773 if (send(sock, buf, buf_size, 0) != buf_size) {
774 thumb_err("sendto failed: %d\n", 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 return MEDIA_THUMB_ERROR_NETWORK;
782 thumb_dbg("Sending msg to thumbnail daemon is successful");
785 if (msg_type == THUMB_REQUEST_CANCEL_MEDIA) {
786 g_io_channel_shutdown(channel, TRUE, NULL);
789 if (msg_type == THUMB_REQUEST_CANCEL_MEDIA) {
790 g_io_channel_shutdown(channel, TRUE, NULL);
792 //thumb_dbg("Cancel : %s[%d]", origin_path, sock);
793 __media_thumb_pop_req_queue(origin_path, TRUE);
794 } else if (msg_type == THUMB_REQUEST_DB_INSERT) {
795 if (g_request_queue == NULL) {
796 g_request_queue = g_queue_new();
799 thumbReq *thumb_req = NULL;
800 thumb_req = calloc(1, sizeof(thumbReq));
801 if (thumb_req == NULL) {
802 thumb_err("Failed to create request element");
806 thumb_req->channel = channel;
807 thumb_req->path = strdup(origin_path);
808 thumb_req->source_id = source_id;
810 //thumb_dbg("Push : %s [%d]", origin_path, sock);
811 g_queue_push_tail(g_request_queue, (gpointer)thumb_req);