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;
237 _media_thumb_recv_udp_msg(int sock, int header_size, thumbMsg *msg, struct sockaddr_in *from_addr, unsigned int *from_size)
239 int recv_msg_len = 0;
240 unsigned int from_addr_size = sizeof(struct sockaddr_in);
241 unsigned char *buf = NULL;
243 buf = (unsigned char*)malloc(sizeof(thumbMsg));
245 if ((recv_msg_len = recvfrom(sock, buf, sizeof(thumbMsg), 0, (struct sockaddr *)from_addr, &from_addr_size)) < 0) {
246 thumb_err("recvfrom failed : %s", strerror(errno));
248 return _media_thumb_get_error();
251 memcpy(msg, buf, header_size);
253 if (msg->origin_path_size <= 0 || msg->origin_path_size > MAX_PATH_SIZE) {
255 thumb_err("msg->origin_path_size is invalid %d", msg->origin_path_size );
256 return MEDIA_THUMB_ERROR_NETWORK;
259 strncpy(msg->org_path, (char*)buf + header_size, msg->origin_path_size);
260 //thumb_dbg("original path : %s", msg->org_path);
262 if (msg->dest_path_size <= 0 || msg->dest_path_size > MAX_PATH_SIZE) {
264 thumb_err("msg->origin_path_size is invalid %d", msg->dest_path_size );
265 return MEDIA_THUMB_ERROR_NETWORK;
268 strncpy(msg->dst_path, (char*)buf + header_size + msg->origin_path_size, msg->dest_path_size);
269 //thumb_dbg("destination path : %s", msg->dst_path);
272 *from_size = from_addr_size;
274 return MEDIA_THUMB_ERROR_NONE;
278 _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
280 if (req_msg == NULL || buf == NULL) {
284 int org_path_len = 0;
285 int dst_path_len = 0;
289 header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2;
290 org_path_len = strlen(req_msg->org_path) + 1;
291 dst_path_len = strlen(req_msg->dst_path) + 1;
293 //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);
295 size = header_size + org_path_len + dst_path_len;
297 memcpy(*buf, req_msg, header_size);
298 memcpy((*buf)+header_size, req_msg->org_path, org_path_len);
299 memcpy((*buf)+header_size + org_path_len, req_msg->dst_path, dst_path_len);
307 _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)
310 const char *serv_ip = "127.0.0.1";
311 struct sockaddr_in serv_addr;
313 int recv_str_len = 0;
318 #ifdef _USE_MEDIA_UTIL_
319 if (ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock) < 0) {
320 thumb_err("ms_ipc_create_client_socket failed");
321 return MEDIA_THUMB_ERROR_NETWORK;
324 /* Creaete a TCP socket */
325 if (_media_thumb_create_socket(CLIENT_SOCKET, &sock) < 0) {
326 thumb_err("_media_thumb_create_socket failed");
327 return MEDIA_THUMB_ERROR_NETWORK;
331 memset(&serv_addr, 0, sizeof(serv_addr));
332 serv_addr.sin_family = AF_INET;
333 serv_addr.sin_addr.s_addr = inet_addr(serv_ip);
334 #ifdef _USE_MEDIA_UTIL_
335 serv_addr.sin_port = htons(MS_THUMB_CREATOR_PORT);
337 serv_addr.sin_port = htons(THUMB_DAEMON_PORT);
340 /* Connecting to the thumbnail server */
341 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
342 thumb_err("connect error : %s", strerror(errno));
343 return MEDIA_THUMB_ERROR_NETWORK;
349 memset((void *)&req_msg, 0, sizeof(thumbMsg));
350 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
352 /* Get PID of client*/
356 /* Set requset message */
357 req_msg.msg_type = msg_type;
358 req_msg.thumb_type = thumb_type;
359 strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
360 req_msg.org_path[strlen(req_msg.org_path)] = '\0';
362 if (msg_type == THUMB_REQUEST_SAVE_FILE) {
363 strncpy(req_msg.dst_path, thumb_path, sizeof(req_msg.dst_path));
364 req_msg.dst_path[strlen(req_msg.dst_path)] = '\0';
367 req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
368 req_msg.dest_path_size = strlen(req_msg.dst_path) + 1;
370 if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
371 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
373 return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
376 unsigned char *buf = NULL;
380 header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2;
381 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
383 if (send(sock, buf, buf_size, 0) != buf_size) {
384 thumb_err("sendto failed: %d\n", errno);
387 return MEDIA_THUMB_ERROR_NETWORK;
390 thumb_dbg("Sending msg to thumbnail daemon is successful");
394 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
395 thumb_err("_media_thumb_recv_msg failed ");
400 recv_str_len = strlen(recv_msg.org_path);
401 thumb_dbg("recv %s(%d) from thumb daemon is successful", recv_msg.org_path, recv_str_len);
405 if (recv_str_len > max_length) {
406 thumb_err("user buffer is too small. Output's length is %d", recv_str_len);
407 return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
410 if (recv_msg.status == THUMB_FAIL) {
411 thumb_err("Failed to make thumbnail");
415 if (msg_type != THUMB_REQUEST_SAVE_FILE) {
416 strncpy(thumb_path, recv_msg.dst_path, max_length);
419 thumb_info->origin_width = recv_msg.origin_width;
420 thumb_info->origin_height = recv_msg.origin_height;
426 _media_thumb_process(thumbMsg *req_msg, thumbMsg *res_msg)
429 unsigned char *data = NULL;
436 char *thumb_path = NULL;
437 int need_update_db = 0;
440 if (req_msg == NULL || res_msg == NULL) {
441 thumb_err("Invalid msg!");
442 return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
445 int msg_type = req_msg->msg_type;
446 media_thumb_type thumb_type = req_msg->thumb_type;
447 const char *origin_path = req_msg->org_path;
449 media_thumb_format thumb_format = MEDIA_THUMB_BGRA;
451 thumb_path = res_msg->dst_path;
452 thumb_path[0] = '\0';
453 max_length = sizeof(res_msg->dst_path);
455 err = _media_thumb_db_connect();
457 thumb_err("_media_thumb_mb_svc_connect failed: %d", err);
458 return MEDIA_THUMB_ERROR_DB;
461 if (msg_type == THUMB_REQUEST_DB_INSERT) {
462 err = _media_thumb_get_thumb_from_db_with_size(origin_path, thumb_path, max_length, &need_update_db, &origin_w, &origin_h);
464 res_msg->origin_width = origin_w;
465 res_msg->origin_height = origin_h;
466 _media_thumb_db_disconnect();
467 return MEDIA_THUMB_ERROR_NONE;
469 if (strlen(thumb_path) == 0) {
470 err = _media_thumb_get_hash_name(origin_path, thumb_path, max_length);
472 thumb_err("_media_thumb_get_hash_name failed - %d\n", err);
473 strncpy(thumb_path, THUMB_DEFAULT_PATH, max_length);
474 _media_thumb_db_disconnect();
478 thumb_path[strlen(thumb_path)] = '\0';
482 } else if (msg_type == THUMB_REQUEST_SAVE_FILE) {
483 strncpy(thumb_path, req_msg->dst_path, max_length);
485 } else if (msg_type == THUMB_REQUEST_ALL_MEDIA) {
486 err = _media_thumb_get_hash_name(origin_path, thumb_path, max_length);
488 thumb_err("_media_thumb_get_hash_name failed - %d\n", err);
489 strncpy(thumb_path, THUMB_DEFAULT_PATH, max_length);
490 _media_thumb_db_disconnect();
494 thumb_path[strlen(thumb_path)] = '\0';
497 thumb_dbg("Thumb path : %s", thumb_path);
499 if (g_file_test(thumb_path,
500 G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
501 thumb_warn("thumb path already exists in file system.. remove the existed file");
502 _media_thumb_remove_file(thumb_path);
505 err = _thumbnail_get_data(origin_path, thumb_type, thumb_format, &data, &thumb_size, &thumb_w, &thumb_h, &origin_w, &origin_h, &alpha);
507 thumb_err("_thumbnail_get_data failed - %d\n", err);
510 strncpy(thumb_path, THUMB_DEFAULT_PATH, max_length);
511 _media_thumb_db_disconnect();
515 //thumb_dbg("Size : %d, W:%d, H:%d", thumb_size, thumb_w, thumb_h);
516 //thumb_dbg("Origin W:%d, Origin H:%d\n", origin_w, origin_h);
517 //thumb_dbg("Thumb : %s", thumb_path);
519 res_msg->msg_type = THUMB_RESPONSE;
520 res_msg->thumb_size = thumb_size;
521 res_msg->thumb_width = thumb_w;
522 res_msg->thumb_height = thumb_h;
523 res_msg->origin_width = origin_w;
524 res_msg->origin_height = origin_h;
526 /* If the image is transparent PNG format, make png file as thumbnail of this image */
529 err = _media_thumb_get_file_ext(origin_path, file_ext, sizeof(file_ext));
530 if (strncasecmp(file_ext, "png", 3) == 0) {
531 int len = strlen(thumb_path);
532 thumb_path[len - 3] = 'p';
533 thumb_path[len - 2] = 'n';
534 thumb_path[len - 1] = 'g';
536 thumb_dbg("Thumb path is changed : %s", thumb_path);
539 err = _media_thumb_save_to_file_with_evas(data, thumb_w, thumb_h, alpha, thumb_path);
541 thumb_err("save_to_file_with_evas failed - %d\n", err);
544 if (msg_type == THUMB_REQUEST_DB_INSERT || msg_type == THUMB_REQUEST_ALL_MEDIA)
545 strncpy(thumb_path, THUMB_DEFAULT_PATH, max_length);
547 _media_thumb_db_disconnect();
550 thumb_dbg("file save success\n");
555 fd = open(thumb_path, O_WRONLY);
557 thumb_warn("open failed");
561 thumb_warn("fsync failed");
570 /* DB update if needed */
571 if (need_update_db == 1) {
572 err = _media_thumb_update_db(origin_path, thumb_path, res_msg->origin_width, res_msg->origin_height);
574 thumb_err("_media_thumb_update_db failed : %d", err);
578 _media_thumb_db_disconnect();
583 gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
587 int recv_str_len = 0;
589 int err = MEDIA_THUMB_ERROR_NONE;
591 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
592 sock = g_io_channel_unix_get_fd(src);
594 header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2;
596 thumb_err("_media_thumb_write_socket socket : %d", sock);
598 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
599 thumb_err("_media_thumb_recv_msg failed ");
600 g_io_channel_shutdown(src, TRUE, NULL);
604 recv_str_len = strlen(recv_msg.dst_path);
605 thumb_dbg("recv %s(%d) in [ %s ] from thumb daemon is successful", recv_msg.dst_path, recv_str_len, recv_msg.org_path);
607 g_io_channel_shutdown(src, TRUE, NULL);
609 //thumb_dbg("Completed..%s", recv_msg.org_path);
610 __media_thumb_pop_req_queue(recv_msg.org_path, FALSE);
612 if (recv_msg.status == THUMB_FAIL) {
613 thumb_err("Failed to make thumbnail");
614 err = MEDIA_THUMB_ERROR_UNSUPPORTED;
620 thumbUserData* cb = (thumbUserData*)data;
621 cb->func(err, recv_msg.dst_path, cb->user_data);
631 _media_thumb_request_async(int msg_type, media_thumb_type thumb_type, const char *origin_path, thumbUserData *userData)
634 const char *serv_ip = "127.0.0.1";
635 struct sockaddr_in serv_addr;
639 if ((msg_type == THUMB_REQUEST_DB_INSERT) && (__media_thumb_check_req_queue(origin_path) < 0)) {
640 return MEDIA_THUMB_ERROR_DUPLICATED_REQUEST;
643 #ifdef _USE_MEDIA_UTIL_
644 if (ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock) < 0) {
645 thumb_err("ms_ipc_create_client_socket failed");
646 return MEDIA_THUMB_ERROR_NETWORK;
649 /* Creaete a TCP socket */
650 if (_media_thumb_create_socket(CLIENT_SOCKET, &sock) < 0) {
651 thumb_err("_media_thumb_create_socket failed");
652 return MEDIA_THUMB_ERROR_NETWORK;
656 GIOChannel *channel = NULL;
657 channel = g_io_channel_unix_new(sock);
661 memset(&serv_addr, 0, sizeof(serv_addr));
662 serv_addr.sin_family = AF_INET;
663 serv_addr.sin_addr.s_addr = inet_addr(serv_ip);
664 #ifdef _USE_MEDIA_UTIL_
665 serv_addr.sin_port = htons(MS_THUMB_CREATOR_PORT);
667 serv_addr.sin_port = htons(THUMB_DAEMON_PORT);
670 /* Connecting to the thumbnail server */
671 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
672 thumb_err("connect error : %s", strerror(errno));
673 g_io_channel_shutdown(channel, TRUE, NULL);
674 return MEDIA_THUMB_ERROR_NETWORK;
677 if (msg_type != THUMB_REQUEST_CANCEL_MEDIA) {
678 //source_id = g_io_add_watch(channel, G_IO_IN, _media_thumb_write_socket, userData );
680 /* Create new channel to watch udp socket */
681 GSource *source = NULL;
682 source = g_io_create_watch(channel, G_IO_IN);
684 /* Set callback to be called when socket is readable */
685 g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, userData, NULL);
686 source_id = g_source_attach(source, g_main_context_get_thread_default());
690 memset((void *)&req_msg, 0, sizeof(thumbMsg));
694 req_msg.msg_type = msg_type;
695 req_msg.thumb_type = thumb_type;
696 strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
697 req_msg.org_path[strlen(req_msg.org_path)] = '\0';
699 req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
700 req_msg.dest_path_size = strlen(req_msg.dst_path) + 1;
702 if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
703 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
704 g_io_channel_shutdown(channel, TRUE, NULL);
705 return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
708 unsigned char *buf = NULL;
710 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
712 //thumb_dbg("buffer size : %d", buf_size);
714 if (send(sock, buf, buf_size, 0) != buf_size) {
715 thumb_err("sendto failed: %d\n", errno);
717 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
718 g_io_channel_shutdown(channel, TRUE, NULL);
719 return MEDIA_THUMB_ERROR_NETWORK;
723 thumb_dbg("Sending msg to thumbnail daemon is successful");
726 if (msg_type == THUMB_REQUEST_CANCEL_MEDIA) {
727 g_io_channel_shutdown(channel, TRUE, NULL);
730 if (msg_type == THUMB_REQUEST_CANCEL_MEDIA) {
731 g_io_channel_shutdown(channel, TRUE, NULL);
733 //thumb_dbg("Cancel : %s[%d]", origin_path, sock);
734 __media_thumb_pop_req_queue(origin_path, TRUE);
735 } else if (msg_type == THUMB_REQUEST_DB_INSERT) {
736 if (g_request_queue == NULL) {
737 g_request_queue = g_queue_new();
740 thumbReq *thumb_req = NULL;
741 thumb_req = calloc(1, sizeof(thumbReq));
742 if (thumb_req == NULL) {
743 thumb_err("Failed to create request element");
747 thumb_req->channel = channel;
748 thumb_req->path = strdup(origin_path);
749 thumb_req->source_id = source_id;
751 //thumb_dbg("Push : %s [%d]", origin_path, sock);
752 g_queue_push_tail(g_request_queue, (gpointer)thumb_req);