4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hyunjun Ko <zzoon.ko@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
22 #include "media-thumbnail.h"
23 #include "media-thumb-ipc.h"
24 #include "media-thumb-util.h"
25 #include "media-thumb-db.h"
26 #include "media-thumb-debug.h"
34 #define THUMB_SOCK_BLOCK_SIZE 512
35 #define THUMB_IPC_PATH tzplatform_mkpath(TZ_SYS_RUN, "media-server/media_ipc_thumbcreator.socket")
37 static GQueue *g_request_queue = NULL;
38 static GQueue *g_manage_queue = NULL;
39 static GQueue *g_request_raw_queue = NULL;
40 static GQueue *g_manage_raw_queue = NULL;
46 unsigned int request_id;
51 thumbUserData *userData;
64 thumbRawUserData *userData;
67 int _media_thumb_send_request();
68 int _media_thumb_raw_data_send_request();
71 int _media_thumb_get_error()
73 if (errno == EWOULDBLOCK) {
74 thumb_err("Timeout. Can't try any more");
75 return MS_MEDIA_ERR_SOCKET_RECEIVE_TIMEOUT;
77 thumb_stderror("recvfrom failed");
78 return MS_MEDIA_ERR_SOCKET_RECEIVE;
82 void __media_thumb_shutdown_channel(bool only_shutdown)
85 thumbReq *req = (thumbReq *)g_queue_peek_head(g_request_queue);
87 len = g_queue_get_length(g_manage_queue);
91 if (len == 0 && req->isCanceled == true) {
92 thumb_dbg("The manage queue will be released");
94 thumb_dbg("There is remain item in the manage queue");
98 GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
99 if (source_id != NULL) {
100 g_source_destroy(source_id);
102 thumb_err("G_SOURCE_ID is NULL");
105 g_io_channel_shutdown(req->channel, TRUE, NULL);
106 g_io_channel_unref(req->channel);
107 g_queue_pop_head(g_request_queue);
109 SAFE_FREE(req->path);
110 SAFE_FREE(req->userData);
113 if (g_manage_queue && len == 0) {
114 g_queue_free(g_manage_queue);
115 g_manage_queue = NULL;
120 int __media_thumb_pop_req_queue(const char *path)
124 req_len = g_queue_get_length(g_request_queue);
127 // thumb_dbg("There is no request in the queue");
129 __media_thumb_shutdown_channel(true);
132 /* Check manage queue */
133 if (g_manage_queue) {
134 req_len = g_queue_get_length(g_manage_queue);
137 _media_thumb_send_request();
140 return MS_MEDIA_ERR_NONE;
143 int __media_thumb_check_req_queue_for_cancel(unsigned int request_id, const char *path)
147 req_len = g_queue_get_length(g_request_queue);
150 // thumb_dbg("There is no request in the queue");
152 thumbReq *req = NULL;
153 req = (thumbReq *)g_queue_peek_head(g_request_queue);
155 if (req != NULL && strncmp(path, req->path, strlen(path)) == 0 && request_id == req->request_id) {
156 req->isCanceled = true;
157 __media_thumb_shutdown_channel(false);
159 return MS_MEDIA_ERR_NONE;
163 return MS_MEDIA_ERR_INTERNAL;
166 int __media_thumb_pop_manage_queue(unsigned int request_id, const char *path)
171 req_len = g_queue_get_length(g_manage_queue);
174 // thumb_dbg("There is no request in the queue");
176 for (i = 0; i < req_len; i++) {
177 thumbReq *req = NULL;
178 req = (thumbReq *)g_queue_peek_nth(g_manage_queue, i);
179 if (req == NULL) continue;
181 if (strncmp(path, req->path, strlen(path)) == 0 && request_id == req->request_id) {
182 g_queue_pop_nth(g_manage_queue, i);
184 SAFE_FREE(req->path);
185 SAFE_FREE(req->userData);
192 return __media_thumb_check_req_queue_for_cancel(request_id, path);
194 __media_thumb_shutdown_channel(false);
196 return MS_MEDIA_ERR_NONE;
200 return MS_MEDIA_ERR_NONE;
204 int __media_thumb_pop_raw_data_req_queue(int request_id, bool shutdown_channel)
208 req_len = g_queue_get_length(g_request_raw_queue);
211 // thumb_dbg("There is no request in the queue");
214 for (i = 0; i < req_len; i++) {
215 thumbRawReq *req = NULL;
216 req = (thumbRawReq *)g_queue_peek_nth(g_request_raw_queue, i);
217 if (req == NULL) continue;
219 if (request_id == req->request_id) {
220 if (shutdown_channel) {
221 GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
222 if (source_id != NULL) {
223 g_source_destroy(source_id);
225 thumb_err("G_SOURCE_ID is NULL");
228 g_io_channel_shutdown(req->channel, TRUE, NULL);
229 g_io_channel_unref(req->channel);
231 g_queue_pop_nth(g_request_raw_queue, i);
233 SAFE_FREE(req->userData);
240 // thumb_dbg("There's no %s in the queue", path);
244 return MS_MEDIA_ERR_NONE;
247 int __media_thumb_check_raw_data_req_queue_for_cancel(int request_id)
251 req_len = g_queue_get_length(g_request_raw_queue);
254 // thumb_dbg("There is no request in the queue");
256 thumbRawReq *req = NULL;
257 req = (thumbRawReq *)g_queue_peek_head(g_request_raw_queue);
259 if (req != NULL && request_id == req->request_id) {
260 req->isCanceled = true;
261 return MS_MEDIA_ERR_NONE;
265 return MS_MEDIA_ERR_INTERNAL;
268 int __media_thumb_pop_raw_data_manage_queue(int request_id)
273 req_len = g_queue_get_length(g_manage_raw_queue);
276 // thumb_dbg("There is no request in the queue");
279 for (i = 0; i < req_len; i++) {
280 thumbRawReq *req = NULL;
281 req = (thumbRawReq *)g_queue_peek_nth(g_manage_raw_queue, i);
282 if (req == NULL) continue;
284 if (request_id == req->request_id) {
285 g_queue_pop_nth(g_manage_raw_queue, i);
287 SAFE_FREE(req->userData);
294 return __media_thumb_check_raw_data_req_queue_for_cancel(request_id);
298 return MS_MEDIA_ERR_NONE;
301 int __media_thumb_check_req_queue(const char *path)
305 req_len = g_queue_get_length(g_request_queue);
307 // thumb_dbg("Queue length : %d", req_len);
308 // thumb_dbg("Queue path : %s", path);
311 // thumb_dbg("There is no request in the queue");
314 for (i = 0; i < req_len; i++) {
315 thumbReq *req = NULL;
316 req = (thumbReq *)g_queue_peek_nth(g_request_queue, i);
317 if (req == NULL) continue;
319 if (strncmp(path, req->path, strlen(path)) == 0) {
320 //thumb_dbg("Same Request - %s", path);
321 return MS_MEDIA_ERR_INVALID_PARAMETER;
328 if (g_manage_queue != NULL) {
329 req_len = g_queue_get_length(g_manage_queue);
332 // thumb_dbg("There is no request in the queue");
334 for (i = 0; i < req_len; i++) {
335 thumbReq *req = NULL;
336 req = (thumbReq *)g_queue_peek_nth(g_manage_queue, i);
337 if (req == NULL) continue;
339 if (strncmp(path, req->path, strlen(path)) == 0) {
340 //thumb_dbg("Same Request - %s", path);
341 return MS_MEDIA_ERR_INVALID_PARAMETER;
350 return MS_MEDIA_ERR_NONE;
353 bool __media_thumb_check_cancel(void)
355 thumbReq *req = NULL;
356 req = (thumbReq *)g_queue_peek_head(g_request_queue);
368 bool __media_thumb_check_cancel_for_raw(void)
370 thumbRawReq *req = NULL;
371 req = (thumbRawReq *)g_queue_peek_head(g_request_raw_queue);
384 _media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg)
386 int recv_msg_len = 0;
388 int block_size = THUMB_SOCK_BLOCK_SIZE;
390 unsigned char *buf = NULL;
391 unsigned char *block_buf = NULL;
393 THUMB_MALLOC(buf, header_size);
394 THUMB_MALLOC(block_buf, THUMB_SOCK_BLOCK_SIZE);
395 if (buf == NULL || block_buf == NULL) {
396 thumb_err("memory allocation failed");
398 SAFE_FREE(block_buf);
399 return MS_MEDIA_ERR_OUT_OF_MEMORY;
402 if ((recv_msg_len = recv(sock, buf, header_size, 0)) <= 0) {
403 thumb_stderror("recv failed");
405 SAFE_FREE(block_buf);
406 return _media_thumb_get_error();
409 memcpy(msg, buf, header_size);
410 //thumb_dbg("origin_path_size : %d, dest_path_size : %d, thumb_size : %d", msg->origin_path_size, msg->dest_path_size, msg->thumb_size);
413 if (msg->origin_path_size < 0 || msg->dest_path_size < 0 || msg->thumb_size < 0) {
414 thumb_err("recv data is wrong");
415 SAFE_FREE(block_buf);
416 return MS_MEDIA_ERR_SOCKET_RECEIVE;
419 remain_size = msg->origin_path_size + msg->dest_path_size + msg->thumb_size;
420 THUMB_MALLOC(buf, remain_size);
422 SAFE_FREE(block_buf);
423 return MS_MEDIA_ERR_OUT_OF_MEMORY;
426 while (remain_size > 0) {
427 if (remain_size < THUMB_SOCK_BLOCK_SIZE) {
428 block_size = remain_size;
430 if ((recv_msg_len = recv(sock, block_buf, block_size, 0)) < 0) {
431 thumb_stderror("recv failed");
433 SAFE_FREE(block_buf);
434 return _media_thumb_get_error();
436 memcpy(buf+recv_block, block_buf, block_size);
437 recv_block += block_size;
438 remain_size -= block_size;
441 strncpy(msg->org_path, (char *)buf, msg->origin_path_size);
442 strncpy(msg->dst_path, (char *)buf + msg->origin_path_size, msg->dest_path_size);
444 SAFE_FREE(msg->thumb_data);
445 if (msg->thumb_size > 0) {
446 THUMB_MALLOC(msg->thumb_data, msg->thumb_size);
447 if (msg->thumb_data != NULL) {
448 memcpy(msg->thumb_data, buf + msg->origin_path_size + msg->dest_path_size, msg->thumb_size);
451 SAFE_FREE(block_buf);
453 return MS_MEDIA_ERR_OUT_OF_MEMORY;
458 SAFE_FREE(block_buf);
460 return MS_MEDIA_ERR_NONE;
464 _media_thumb_recv_udp_msg(int sock, int header_size, thumbMsg *msg, struct sockaddr_un *from_addr, unsigned int *from_size)
466 int recv_msg_len = 0;
467 unsigned int from_addr_size = sizeof(struct sockaddr_un);
468 unsigned char *buf = NULL;
470 THUMB_MALLOC(buf, sizeof(thumbMsg));
472 thumb_err("memory allocation failed");
473 return MS_MEDIA_ERR_OUT_OF_MEMORY;
476 if ((recv_msg_len = recvfrom(sock, buf, sizeof(thumbMsg), 0, (struct sockaddr *)from_addr, &from_addr_size)) < 0) {
477 thumb_stderror("recvform failed");
479 return _media_thumb_get_error();
482 memcpy(msg, buf, header_size);
484 if (msg->origin_path_size <= 0 || msg->origin_path_size > MAX_PATH_SIZE) {
486 thumb_err("msg->origin_path_size is invalid %d", msg->origin_path_size);
487 return MS_MEDIA_ERR_INVALID_PARAMETER;
490 strncpy(msg->org_path, (char*)buf + header_size, msg->origin_path_size);
492 if (msg->dest_path_size <= 0 || msg->dest_path_size > MAX_PATH_SIZE) {
494 thumb_err("msg->origin_path_size is invalid %d", msg->dest_path_size);
495 return MS_MEDIA_ERR_INVALID_PARAMETER;
498 strncpy(msg->dst_path, (char*)buf + header_size + msg->origin_path_size, msg->dest_path_size);
501 *from_size = from_addr_size;
503 return MS_MEDIA_ERR_NONE;
507 _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
509 if (req_msg == NULL || buf == NULL) {
510 return MS_MEDIA_ERR_INVALID_PARAMETER;
513 int org_path_len = 0;
514 int dst_path_len = 0;
515 int thumb_data_len = 0;
519 header_size = sizeof(thumbMsg) -(MAX_FILEPATH_LEN * 2) - sizeof(unsigned char *);
520 org_path_len = req_msg->origin_path_size;
521 dst_path_len = req_msg->dest_path_size;
522 thumb_data_len = req_msg->thumb_size;
524 //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);
526 size = header_size + org_path_len + dst_path_len + thumb_data_len;
527 THUMB_MALLOC(*buf, size);
532 memcpy(*buf, req_msg, header_size);
533 memcpy((*buf)+header_size, req_msg->org_path, org_path_len);
534 memcpy((*buf)+header_size + org_path_len, req_msg->dst_path, dst_path_len);
535 if (thumb_data_len > 0)
536 memcpy((*buf)+header_size + org_path_len + dst_path_len, req_msg->thumb_data, thumb_data_len);
540 return MS_MEDIA_ERR_NONE;
544 _media_thumb_request(int msg_type, const char *origin_path, char *thumb_path, int max_length, media_thumb_info *thumb_info, uid_t uid)
547 struct sockaddr_un serv_addr;
548 ms_sock_info_s sock_info;
549 int recv_str_len = 0;
550 int err = MS_MEDIA_ERR_NONE;
552 sock_info.port = MS_THUMB_CREATOR_PORT;
554 err = ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock_info);
555 if (err != MS_MEDIA_ERR_NONE) {
556 thumb_err("ms_ipc_create_client_socket failed");
560 memset(&serv_addr, 0, sizeof(serv_addr));
561 sock = sock_info.sock_fd;
562 serv_addr.sun_family = AF_UNIX;
563 strncpy(serv_addr.sun_path, THUMB_IPC_PATH, strlen(THUMB_IPC_PATH));
565 /* Connecting to the thumbnail server */
566 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
567 thumb_stderror("connect");
568 ms_ipc_delete_client_socket(&sock_info);
569 return MS_MEDIA_ERR_SOCKET_CONN;
575 memset((void *)&req_msg, 0, sizeof(thumbMsg));
576 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
578 /* Get PID of client*/
582 /* Set requset message */
583 req_msg.msg_type = msg_type;
585 strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
586 req_msg.org_path[strlen(req_msg.org_path)] = '\0';
588 if (msg_type == THUMB_REQUEST_SAVE_FILE) {
589 strncpy(req_msg.dst_path, thumb_path, sizeof(req_msg.dst_path));
590 req_msg.dst_path[strlen(req_msg.dst_path)] = '\0';
593 req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
594 req_msg.dest_path_size = strlen(req_msg.dst_path) + 1;
595 req_msg.thumb_size = 0;
597 if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
598 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
599 ms_ipc_delete_client_socket(&sock_info);
600 return MS_MEDIA_ERR_INVALID_PARAMETER;
603 unsigned char *buf = NULL;
607 header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
608 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
610 if (send(sock, buf, buf_size, 0) != buf_size) {
611 thumb_err("sendto failed: %d", errno);
613 ms_ipc_delete_client_socket(&sock_info);
614 return MS_MEDIA_ERR_SOCKET_SEND;
617 thumb_dbg("Sending msg to thumbnail daemon is successful");
621 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
622 thumb_err("_media_thumb_recv_msg failed ");
623 ms_ipc_delete_client_socket(&sock_info);
627 recv_str_len = strlen(recv_msg.org_path);
628 thumb_dbg_slog("recv %s(%d) from thumb daemon is successful", recv_msg.org_path, recv_str_len);
630 ms_ipc_delete_client_socket(&sock_info);
632 if (recv_str_len > max_length) {
633 thumb_err("user buffer is too small. Output's length is %d", recv_str_len);
634 SAFE_FREE(recv_msg.thumb_data);
635 return MS_MEDIA_ERR_INVALID_PARAMETER;
638 if (recv_msg.status == THUMB_FAIL) {
639 thumb_err("Failed to make thumbnail");
640 SAFE_FREE(recv_msg.thumb_data);
641 return MS_MEDIA_ERR_INVALID_PARAMETER;
644 if (msg_type != THUMB_REQUEST_SAVE_FILE) {
645 strncpy(thumb_path, recv_msg.dst_path, max_length);
648 thumb_info->origin_width = recv_msg.origin_width;
649 thumb_info->origin_height = recv_msg.origin_height;
651 SAFE_FREE(recv_msg.thumb_data);
653 return MS_MEDIA_ERR_NONE;
656 gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
661 int err = MS_MEDIA_ERR_NONE;
663 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
664 sock = g_io_channel_unix_get_fd(src);
666 header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
668 thumb_err("_media_thumb_write_socket socket : %d", sock);
670 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
671 thumb_err("_media_thumb_recv_msg failed ");
672 if (recv_msg.origin_path_size > 0) {
673 __media_thumb_pop_req_queue(recv_msg.org_path);
675 thumb_err("origin path size is wrong.");
681 //thumb_dbg("Completed..%s", recv_msg.org_path);
683 if (recv_msg.status == THUMB_FAIL) {
684 thumb_err("Failed to make thumbnail");
685 err = MS_MEDIA_ERR_INTERNAL;
688 if (__media_thumb_check_cancel()) {
690 thumbUserData* cb = (thumbUserData*)data;
691 if (cb->func != NULL)
692 cb->func(err, recv_msg.dst_path, cb->user_data);
696 __media_thumb_pop_req_queue(recv_msg.org_path);
703 gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
709 int err = MS_MEDIA_ERR_NONE;
711 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
712 sock = g_io_channel_unix_get_fd(src);
714 header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
716 thumb_err("_media_thumb_write_socket socket : %d", sock);
718 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
719 thumb_err("_media_thumb_recv_msg failed ");
720 if (recv_msg.request_id > 0) {
721 __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, TRUE);
723 thumb_err("origin path size is wrong.");
729 g_io_channel_shutdown(src, TRUE, NULL);
730 g_io_channel_unref(src);
732 if (recv_msg.status == THUMB_FAIL) {
733 thumb_err("Failed to make thumbnail");
734 err = MS_MEDIA_ERR_INTERNAL;
737 if (__media_thumb_check_cancel_for_raw()) {
739 thumbRawUserData* cb = (thumbRawUserData*)data;
740 if (cb->func != NULL)
741 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);
745 __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, FALSE);
749 /* Check manage queue */
750 if (g_manage_raw_queue) {
751 len = g_queue_get_length(g_manage_raw_queue);
754 _media_thumb_raw_data_send_request();
756 g_queue_free(g_manage_raw_queue);
757 g_manage_raw_queue = NULL;
764 int _media_thumb_send_request()
766 int err = MS_MEDIA_ERR_NONE;
768 struct sockaddr_un serv_addr;
769 ms_sock_info_s sock_info;
770 thumbReq *req_manager = NULL;
772 sock_info.port = MS_THUMB_CREATOR_PORT;
774 err = ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock_info);
775 if (err != MS_MEDIA_ERR_NONE) {
776 thumb_err("ms_ipc_create_client_socket failed");
780 memset(&serv_addr, 0, sizeof(serv_addr));
781 sock = sock_info.sock_fd;
782 serv_addr.sun_family = AF_UNIX;
783 strncpy(serv_addr.sun_path, THUMB_IPC_PATH, strlen(THUMB_IPC_PATH));
785 GIOChannel *channel = NULL;
786 channel = g_io_channel_unix_new(sock);
789 /* Connecting to the thumbnail server */
790 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
791 thumb_stderror("connect");
793 err = MS_MEDIA_ERR_PERMISSION_DENIED;
795 err = MS_MEDIA_ERR_SOCKET_CONN;
797 g_io_channel_shutdown(channel, TRUE, NULL);
798 g_io_channel_unref(channel);
799 ms_ipc_delete_client_socket(&sock_info);
804 req_manager = (thumbReq *)g_queue_pop_head(g_manage_queue);
806 if (req_manager == NULL) {
807 thumb_err("queue pop fail");
808 g_io_channel_shutdown(channel, TRUE, NULL);
809 g_io_channel_unref(channel);
810 ms_ipc_delete_client_socket(&sock_info);
811 return MS_MEDIA_ERR_INVALID_PARAMETER;
814 GSource *source = NULL;
815 source = g_io_create_watch(channel, G_IO_IN);
816 g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, req_manager->userData, NULL);
817 source_id = g_source_attach(source, g_main_context_get_thread_default());
821 memset((void *)&req_msg, 0, sizeof(thumbMsg));
825 req_msg.msg_type = req_manager->msg_type;
826 req_msg.request_id = 0;
827 req_msg.uid = req_manager->uid;
828 strncpy(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
829 req_msg.org_path[strlen(req_msg.org_path)] = '\0';
830 req_msg.dst_path[0] = '\0';
831 req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
832 req_msg.dest_path_size = 1;
833 req_msg.thumb_size = 0;
835 if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
836 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
837 g_io_channel_shutdown(channel, TRUE, NULL);
838 g_io_channel_unref(channel);
839 ms_ipc_delete_client_socket(&sock_info);
840 return MS_MEDIA_ERR_INVALID_PARAMETER;
843 unsigned char *buf = NULL;
845 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
847 if (send(sock, buf, buf_size, 0) != buf_size) {
848 thumb_err("sendto failed: %d", errno);
850 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
851 g_io_channel_shutdown(channel, TRUE, NULL);
852 g_io_channel_unref(channel);
853 ms_ipc_delete_client_socket(&sock_info);
854 return MS_MEDIA_ERR_SOCKET_SEND;
858 thumb_dbg("Sending msg to thumbnail daemon is successful");
861 if (req_manager->msg_type == THUMB_REQUEST_DB_INSERT) {
862 if (g_request_queue == NULL) {
863 g_request_queue = g_queue_new();
866 thumbReq *thumb_req = NULL;
867 THUMB_MALLOC(thumb_req, sizeof(thumbReq));
868 if (thumb_req == NULL) {
869 thumb_err("Failed to create request element");
870 return MS_MEDIA_ERR_INVALID_PARAMETER;
873 thumb_req->channel = channel;
874 thumb_req->path = strdup(req_manager->path);
875 thumb_req->source_id = source_id;
876 thumb_req->userData = req_manager->userData;
878 g_queue_push_tail(g_request_queue, (gpointer)thumb_req);
884 int _media_thumb_raw_data_send_request()
886 int err = MS_MEDIA_ERR_NONE;
888 struct sockaddr_un serv_addr;
889 ms_sock_info_s sock_info;
890 thumbRawReq *req_manager = NULL;
892 sock_info.port = MS_THUMB_CREATOR_PORT;
894 err = ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock_info);
895 if (err != MS_MEDIA_ERR_NONE) {
896 thumb_err("ms_ipc_create_client_socket failed");
900 memset(&serv_addr, 0, sizeof(serv_addr));
901 sock = sock_info.sock_fd;
902 serv_addr.sun_family = AF_UNIX;
903 strncpy(serv_addr.sun_path, THUMB_IPC_PATH, strlen(THUMB_IPC_PATH));
905 GIOChannel *channel = NULL;
906 channel = g_io_channel_unix_new(sock);
909 /* Connecting to the thumbnail server */
910 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
911 thumb_stderror("connect error");
913 err = MS_MEDIA_ERR_PERMISSION_DENIED;
915 err = MS_MEDIA_ERR_SOCKET_CONN;
917 g_io_channel_shutdown(channel, TRUE, NULL);
918 g_io_channel_unref(channel);
919 ms_ipc_delete_client_socket(&sock_info);
923 req_manager = (thumbRawReq *)g_queue_pop_head(g_manage_raw_queue);
925 if (req_manager == NULL) {
926 thumb_err("queue pop fail");
927 g_io_channel_shutdown(channel, TRUE, NULL);
928 g_io_channel_unref(channel);
929 ms_ipc_delete_client_socket(&sock_info);
930 return MS_MEDIA_ERR_INVALID_PARAMETER;
933 GSource *source = NULL;
934 source = g_io_create_watch(channel, G_IO_IN);
935 g_source_set_callback(source, (GSourceFunc)_media_thumb_raw_data_write_socket, req_manager->userData, NULL);
936 source_id = g_source_attach(source, g_main_context_get_thread_default());
939 memset((void *)&req_msg, 0, sizeof(thumbMsg));
943 req_msg.msg_type = req_manager->msg_type;
944 req_msg.request_id = req_manager->request_id;
945 req_msg.thumb_width = req_manager->width;
946 req_msg.thumb_height = req_manager->height;
947 req_msg.uid = req_manager->uid;
949 strncpy(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
950 req_msg.org_path[strlen(req_msg.org_path)] = '\0';
951 req_msg.dst_path[0] = '\0';
953 req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
954 req_msg.dest_path_size = 1;
955 req_msg.thumb_size = 0;
957 if (req_msg.origin_path_size > MAX_PATH_SIZE) {
958 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
959 g_io_channel_shutdown(channel, TRUE, NULL);
960 g_io_channel_unref(channel);
961 return MS_MEDIA_ERR_INVALID_PARAMETER;
964 unsigned char *buf = NULL;
966 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
968 if (send(sock, buf, buf_size, 0) != buf_size) {
969 thumb_err("sendto failed: %d", errno);
971 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
972 g_io_channel_shutdown(channel, TRUE, NULL);
973 g_io_channel_unref(channel);
974 return MS_MEDIA_ERR_SOCKET_SEND;
979 if (req_manager->msg_type == THUMB_REQUEST_RAW_DATA) {
980 if (g_request_raw_queue == NULL) {
981 g_request_raw_queue = g_queue_new();
983 thumbRawReq *thumb_req = NULL;
984 THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
985 if (thumb_req == NULL) {
986 thumb_err("Failed to create request element");
987 return MS_MEDIA_ERR_INVALID_PARAMETER;
989 thumb_req->channel = channel;
990 thumb_req->request_id = req_manager->request_id;
991 thumb_req->source_id = source_id;
992 thumb_req->userData = req_manager->userData;
994 g_queue_push_tail(g_request_raw_queue, (gpointer)thumb_req);
996 return MS_MEDIA_ERR_NONE;
999 int _media_thumb_request_async(int msg_type, unsigned int request_id, const char *origin_path, thumbUserData *userData, uid_t uid)
1001 int err = MS_MEDIA_ERR_NONE;
1003 if (g_manage_queue == NULL) {
1004 if (msg_type == THUMB_REQUEST_CANCEL_MEDIA)
1005 return MS_MEDIA_ERR_INTERNAL;
1007 g_manage_queue = g_queue_new();
1009 thumbReq *thumb_req = NULL;
1010 THUMB_MALLOC(thumb_req, sizeof(thumbReq));
1011 if (thumb_req == NULL) {
1012 thumb_err("Failed to create request element");
1013 return MS_MEDIA_ERR_INVALID_PARAMETER;
1016 thumb_req->msg_type = msg_type;
1017 thumb_req->path = strdup(origin_path);
1018 thumb_req->userData = userData;
1019 thumb_req->isCanceled = false;
1020 thumb_req->request_id = request_id;
1021 thumb_req->uid = uid;
1023 g_queue_push_tail(g_manage_queue, (gpointer)thumb_req);
1025 /* directly request at first time */
1026 err = _media_thumb_send_request();
1029 if (msg_type != THUMB_REQUEST_CANCEL_MEDIA) {
1032 if ((msg_type == THUMB_REQUEST_DB_INSERT) && (__media_thumb_check_req_queue(origin_path) < 0)) {
1033 thumb_err("duplicated request");
1034 return MS_MEDIA_ERR_THUMB_DUPLICATED_REQUEST;
1037 thumbReq *thumb_req = NULL;
1038 THUMB_MALLOC(thumb_req, sizeof(thumbReq));
1039 if (thumb_req == NULL) {
1040 thumb_err("Failed to create request element");
1041 return MS_MEDIA_ERR_INVALID_PARAMETER;
1044 thumb_req->msg_type = msg_type;
1045 thumb_req->path = strdup(origin_path);
1046 thumb_req->userData = userData;
1047 thumb_req->isCanceled = false;
1048 thumb_req->request_id = request_id;
1049 thumb_req->uid = uid;
1051 g_queue_push_tail(g_manage_queue, (gpointer)thumb_req);
1054 err = __media_thumb_pop_manage_queue(request_id, origin_path);
1061 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)
1063 int err = MS_MEDIA_ERR_NONE;
1065 if (g_manage_raw_queue == NULL) {
1066 if (msg_type == THUMB_REQUEST_CANCEL_RAW_DATA)
1067 return MS_MEDIA_ERR_INTERNAL;
1069 g_manage_raw_queue = g_queue_new();
1071 thumbRawReq *thumb_req = NULL;
1072 THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
1073 if (thumb_req == NULL) {
1074 thumb_err("Failed to create request element");
1075 return MS_MEDIA_ERR_INVALID_PARAMETER;
1078 thumb_req->msg_type = msg_type;
1079 thumb_req->request_id = request_id;
1080 thumb_req->path = strdup(origin_path);
1081 thumb_req->width = width;
1082 thumb_req->height = height;
1083 thumb_req->userData = userData;
1084 thumb_req->isCanceled = false;
1085 thumb_req->uid = uid;
1087 g_queue_push_tail(g_manage_raw_queue, (gpointer)thumb_req);
1089 /* directly request at first time */
1090 err = _media_thumb_raw_data_send_request();
1093 if (msg_type != THUMB_REQUEST_CANCEL_RAW_DATA) {
1095 thumbRawReq *thumb_req = NULL;
1096 THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
1097 if (thumb_req == NULL) {
1098 thumb_err("Failed to create request element");
1099 return MS_MEDIA_ERR_INVALID_PARAMETER;
1102 thumb_req->msg_type = msg_type;
1103 thumb_req->request_id = request_id;
1104 thumb_req->path = strdup(origin_path);
1105 thumb_req->width = width;
1106 thumb_req->height = height;
1107 thumb_req->userData = userData;
1108 thumb_req->isCanceled = false;
1109 thumb_req->uid = uid;
1111 g_queue_push_tail(g_manage_raw_queue, (gpointer)thumb_req);
1115 err = __media_thumb_pop_raw_data_manage_queue(request_id);
1122 int _media_thumb_request_cancel_all(bool isRaw)
1129 thumbRawReq *tmp_raw_req = NULL;
1130 if (g_manage_raw_queue == NULL) {
1131 thumb_err("manage_queue is NULL");
1132 if (g_request_raw_queue != NULL) {
1133 /* Check request queue */
1134 len = g_queue_get_length(g_request_raw_queue);
1136 tmp_raw_req = g_queue_peek_head(g_request_raw_queue);
1137 if (tmp_raw_req != NULL)
1138 tmp_raw_req->isCanceled = true;
1139 return MS_MEDIA_ERR_NONE;
1142 thumb_err("request_queue is NULL");
1143 return MS_MEDIA_ERR_INTERNAL;
1145 req_len = g_queue_get_length(g_manage_raw_queue);
1146 for (i = 0; i < req_len; i++) {
1147 tmp_raw_req = g_queue_pop_tail(g_manage_raw_queue);
1149 SAFE_FREE(tmp_raw_req->path);
1150 SAFE_FREE(tmp_raw_req->userData);
1151 SAFE_FREE(tmp_raw_req);
1155 g_queue_free(g_manage_raw_queue);
1156 g_manage_raw_queue = NULL;
1157 if (g_request_raw_queue != NULL) {
1158 /* Check request queue */
1159 len = g_queue_get_length(g_request_raw_queue);
1161 tmp_raw_req = g_queue_peek_head(g_request_raw_queue);
1162 if (tmp_raw_req != NULL)
1163 tmp_raw_req->isCanceled = true;
1167 thumbReq *tmp_req = NULL;
1168 if (g_manage_queue == NULL) {
1169 thumb_err("manage_queue is NULL");
1170 if (g_request_queue != NULL) {
1171 /* Check request queue */
1172 len = g_queue_get_length(g_request_queue);
1174 tmp_req = g_queue_peek_head(g_request_queue);
1175 if (tmp_req != NULL)
1176 tmp_req->isCanceled = true;
1177 return MS_MEDIA_ERR_NONE;
1180 thumb_err("request_queue is NULL");
1181 return MS_MEDIA_ERR_INTERNAL;
1183 req_len = g_queue_get_length(g_manage_queue);
1184 for (i = 0; i < req_len; i++) {
1185 tmp_req = g_queue_pop_tail(g_manage_queue);
1187 SAFE_FREE(tmp_req->path);
1188 SAFE_FREE(tmp_req->userData);
1193 g_queue_free(g_manage_queue);
1194 g_manage_queue = NULL;
1195 if (g_request_queue != NULL) {
1196 /* Check request queue */
1197 len = g_queue_get_length(g_request_queue);
1199 tmp_req = g_queue_peek_head(g_request_queue);
1200 if (tmp_req != NULL)
1201 tmp_req->isCanceled = true;
1206 return MS_MEDIA_ERR_NONE;