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 int __media_thumb_pop_req_queue(const char *path, bool shutdown_channel)
85 req_len = g_queue_get_length(g_request_queue);
88 // thumb_dbg("There is no request in the queue");
91 for (i = 0; i < req_len; i++) {
93 req = (thumbReq *)g_queue_peek_nth(g_request_queue, i);
94 if (req == NULL) continue;
96 if (strncmp(path, req->path, strlen(path)) == 0) {
97 if (shutdown_channel) {
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);
108 g_queue_pop_nth(g_request_queue, i);
110 SAFE_FREE(req->path);
111 SAFE_FREE(req->userData);
118 // thumb_dbg("There's no %s in the queue", path);
122 return MS_MEDIA_ERR_NONE;
125 int __media_thumb_check_req_queue_for_cancel(const char *path)
129 req_len = g_queue_get_length(g_request_queue);
132 // thumb_dbg("There is no request in the queue");
134 thumbReq *req = NULL;
135 req = (thumbReq *)g_queue_peek_head(g_request_queue);
137 if (req != NULL && strncmp(path, req->path, strlen(path)) == 0) {
138 req->isCanceled = true;
139 return MS_MEDIA_ERR_NONE;
143 return MS_MEDIA_ERR_INTERNAL;
146 int __media_thumb_pop_manage_queue(const char *path)
151 req_len = g_queue_get_length(g_manage_queue);
154 // thumb_dbg("There is no request in the queue");
156 for (i = 0; i < req_len; i++) {
157 thumbReq *req = NULL;
158 req = (thumbReq *)g_queue_peek_nth(g_manage_queue, i);
159 if (req == NULL) continue;
161 if (strncmp(path, req->path, strlen(path)) == 0) {
162 g_queue_pop_nth(g_manage_queue, i);
164 SAFE_FREE(req->path);
165 SAFE_FREE(req->userData);
172 return __media_thumb_check_req_queue_for_cancel(path);
176 return MS_MEDIA_ERR_NONE;
180 int __media_thumb_pop_raw_data_req_queue(int request_id, bool shutdown_channel)
184 req_len = g_queue_get_length(g_request_raw_queue);
187 // thumb_dbg("There is no request in the queue");
190 for (i = 0; i < req_len; i++) {
191 thumbRawReq *req = NULL;
192 req = (thumbRawReq *)g_queue_peek_nth(g_request_raw_queue, i);
193 if (req == NULL) continue;
195 if (request_id == req->request_id) {
196 if (shutdown_channel) {
197 GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
198 if (source_id != NULL) {
199 g_source_destroy(source_id);
201 thumb_err("G_SOURCE_ID is NULL");
204 g_io_channel_shutdown(req->channel, TRUE, NULL);
205 g_io_channel_unref(req->channel);
207 g_queue_pop_nth(g_request_raw_queue, i);
209 SAFE_FREE(req->userData);
216 // thumb_dbg("There's no %s in the queue", path);
220 return MS_MEDIA_ERR_NONE;
223 int __media_thumb_check_raw_data_req_queue_for_cancel(int request_id)
227 req_len = g_queue_get_length(g_request_raw_queue);
230 // thumb_dbg("There is no request in the queue");
232 thumbRawReq *req = NULL;
233 req = (thumbRawReq *)g_queue_peek_head(g_request_raw_queue);
235 if (req != NULL && request_id == req->request_id) {
236 req->isCanceled = true;
237 return MS_MEDIA_ERR_NONE;
241 return MS_MEDIA_ERR_INTERNAL;
244 int __media_thumb_pop_raw_data_manage_queue(int request_id)
249 req_len = g_queue_get_length(g_manage_raw_queue);
252 // thumb_dbg("There is no request in the queue");
255 for (i = 0; i < req_len; i++) {
256 thumbRawReq *req = NULL;
257 req = (thumbRawReq *)g_queue_peek_nth(g_manage_raw_queue, i);
258 if (req == NULL) continue;
260 if (request_id == req->request_id) {
261 g_queue_pop_nth(g_manage_raw_queue, i);
263 SAFE_FREE(req->userData);
270 return __media_thumb_check_raw_data_req_queue_for_cancel(request_id);
274 return MS_MEDIA_ERR_NONE;
277 int __media_thumb_check_req_queue(const char *path)
281 req_len = g_queue_get_length(g_request_queue);
283 // thumb_dbg("Queue length : %d", req_len);
284 // thumb_dbg("Queue path : %s", path);
287 // thumb_dbg("There is no request in the queue");
290 for (i = 0; i < req_len; i++) {
291 thumbReq *req = NULL;
292 req = (thumbReq *)g_queue_peek_nth(g_request_queue, i);
293 if (req == NULL) continue;
295 if (strncmp(path, req->path, strlen(path)) == 0) {
296 //thumb_dbg("Same Request - %s", path);
297 return MS_MEDIA_ERR_INVALID_PARAMETER;
304 return MS_MEDIA_ERR_NONE;
307 bool __media_thumb_check_cancel(void)
309 thumbReq *req = NULL;
310 req = (thumbReq *)g_queue_peek_head(g_request_queue);
322 bool __media_thumb_check_cancel_for_raw(void)
324 thumbRawReq *req = NULL;
325 req = (thumbRawReq *)g_queue_peek_head(g_request_raw_queue);
338 _media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg)
340 int recv_msg_len = 0;
342 int block_size = THUMB_SOCK_BLOCK_SIZE;
344 unsigned char *buf = NULL;
345 unsigned char *block_buf = NULL;
347 THUMB_MALLOC(buf, header_size);
348 THUMB_MALLOC(block_buf, THUMB_SOCK_BLOCK_SIZE);
349 if (buf == NULL || block_buf == NULL) {
350 thumb_err("memory allocation failed");
352 SAFE_FREE(block_buf);
353 return MS_MEDIA_ERR_OUT_OF_MEMORY;
356 if ((recv_msg_len = recv(sock, buf, header_size, 0)) <= 0) {
357 thumb_stderror("recv failed");
359 SAFE_FREE(block_buf);
360 return _media_thumb_get_error();
363 memcpy(msg, buf, header_size);
364 //thumb_dbg("origin_path_size : %d, dest_path_size : %d, thumb_size : %d", msg->origin_path_size, msg->dest_path_size, msg->thumb_size);
367 if(msg->origin_path_size < 0 ||msg->dest_path_size < 0 || msg->thumb_size < 0) {
368 thumb_err("recv data is wrong");
369 SAFE_FREE(block_buf);
370 return MS_MEDIA_ERR_SOCKET_RECEIVE;
373 remain_size = msg->origin_path_size + msg->dest_path_size + msg->thumb_size;
374 THUMB_MALLOC(buf, remain_size);
376 SAFE_FREE(block_buf);
377 return MS_MEDIA_ERR_OUT_OF_MEMORY;
380 while(remain_size > 0) {
381 if(remain_size < THUMB_SOCK_BLOCK_SIZE) {
382 block_size = remain_size;
384 if ((recv_msg_len = recv(sock, block_buf, block_size, 0)) < 0) {
385 thumb_stderror("recv failed");
387 SAFE_FREE(block_buf);
388 return _media_thumb_get_error();
390 memcpy(buf+recv_block, block_buf, block_size);
391 recv_block += block_size;
392 remain_size -= block_size;
395 strncpy(msg->org_path, (char *)buf, msg->origin_path_size);
396 strncpy(msg->dst_path, (char *)buf + msg->origin_path_size, msg->dest_path_size);
398 SAFE_FREE(msg->thumb_data);
399 if(msg->thumb_size > 0) {
400 THUMB_MALLOC(msg->thumb_data, msg->thumb_size);
401 if(msg->thumb_data != NULL) {
402 memcpy(msg->thumb_data, buf + msg->origin_path_size + msg->dest_path_size, msg->thumb_size);
405 SAFE_FREE(block_buf);
407 return MS_MEDIA_ERR_OUT_OF_MEMORY;
412 SAFE_FREE(block_buf);
414 return MS_MEDIA_ERR_NONE;
418 _media_thumb_recv_udp_msg(int sock, int header_size, thumbMsg *msg, struct sockaddr_un *from_addr, unsigned int *from_size)
420 int recv_msg_len = 0;
421 unsigned int from_addr_size = sizeof(struct sockaddr_un);
422 unsigned char *buf = NULL;
424 THUMB_MALLOC(buf, sizeof(thumbMsg));
426 thumb_err("memory allocation failed");
427 return MS_MEDIA_ERR_OUT_OF_MEMORY;
430 if ((recv_msg_len = recvfrom(sock, buf, sizeof(thumbMsg), 0, (struct sockaddr *)from_addr, &from_addr_size)) < 0) {
431 thumb_stderror("recvform failed");
433 return _media_thumb_get_error();
436 memcpy(msg, buf, header_size);
438 if (msg->origin_path_size <= 0 || msg->origin_path_size > MAX_PATH_SIZE) {
440 thumb_err("msg->origin_path_size is invalid %d", msg->origin_path_size );
441 return MS_MEDIA_ERR_INVALID_PARAMETER;
444 strncpy(msg->org_path, (char*)buf + header_size, msg->origin_path_size);
446 if (msg->dest_path_size <= 0 || msg->dest_path_size > MAX_PATH_SIZE) {
448 thumb_err("msg->origin_path_size is invalid %d", msg->dest_path_size );
449 return MS_MEDIA_ERR_INVALID_PARAMETER;
452 strncpy(msg->dst_path, (char*)buf + header_size + msg->origin_path_size, msg->dest_path_size);
455 *from_size = from_addr_size;
457 return MS_MEDIA_ERR_NONE;
461 _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
463 if (req_msg == NULL || buf == NULL) {
464 return MS_MEDIA_ERR_INVALID_PARAMETER;
467 int org_path_len = 0;
468 int dst_path_len = 0;
469 int thumb_data_len = 0;
473 header_size = sizeof(thumbMsg) -(MAX_FILEPATH_LEN * 2) - sizeof(unsigned char *);
474 org_path_len = req_msg->origin_path_size;
475 dst_path_len = req_msg->dest_path_size;
476 thumb_data_len = req_msg->thumb_size;
478 //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);
480 size = header_size + org_path_len + dst_path_len + thumb_data_len;
481 THUMB_MALLOC(*buf, size);
486 memcpy(*buf, req_msg, header_size);
487 memcpy((*buf)+header_size, req_msg->org_path, org_path_len);
488 memcpy((*buf)+header_size + org_path_len, req_msg->dst_path, dst_path_len);
489 if(thumb_data_len > 0)
490 memcpy((*buf)+header_size + org_path_len + dst_path_len, req_msg->thumb_data, thumb_data_len);
494 return MS_MEDIA_ERR_NONE;
498 _media_thumb_request(int msg_type, const char *origin_path, char *thumb_path, int max_length, media_thumb_info *thumb_info, uid_t uid)
501 struct sockaddr_un serv_addr;
502 ms_sock_info_s sock_info;
503 int recv_str_len = 0;
504 int err = MS_MEDIA_ERR_NONE;
506 sock_info.port = MS_THUMB_CREATOR_PORT;
508 err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
509 if (err != MS_MEDIA_ERR_NONE) {
510 thumb_err("ms_ipc_create_client_socket failed");
514 memset(&serv_addr, 0, sizeof(serv_addr));
515 sock = sock_info.sock_fd;
516 serv_addr.sun_family = AF_UNIX;
517 strncpy(serv_addr.sun_path, THUMB_IPC_PATH, strlen(THUMB_IPC_PATH));
519 /* Connecting to the thumbnail server */
520 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
521 thumb_stderror("connect");
522 ms_ipc_delete_client_socket(&sock_info);
523 return MS_MEDIA_ERR_SOCKET_CONN;
529 memset((void *)&req_msg, 0, sizeof(thumbMsg));
530 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
532 /* Get PID of client*/
536 /* Set requset message */
537 req_msg.msg_type = msg_type;
539 strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
540 req_msg.org_path[strlen(req_msg.org_path)] = '\0';
542 if (msg_type == THUMB_REQUEST_SAVE_FILE) {
543 strncpy(req_msg.dst_path, thumb_path, sizeof(req_msg.dst_path));
544 req_msg.dst_path[strlen(req_msg.dst_path)] = '\0';
547 req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
548 req_msg.dest_path_size = strlen(req_msg.dst_path) + 1;
549 req_msg.thumb_size = 0;
551 if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
552 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
553 ms_ipc_delete_client_socket(&sock_info);
554 return MS_MEDIA_ERR_INVALID_PARAMETER;
557 unsigned char *buf = NULL;
561 header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
562 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
564 if (send(sock, buf, buf_size, 0) != buf_size) {
565 thumb_err("sendto failed: %d", errno);
567 ms_ipc_delete_client_socket(&sock_info);
568 return MS_MEDIA_ERR_SOCKET_SEND;
571 thumb_dbg("Sending msg to thumbnail daemon is successful");
575 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
576 thumb_err("_media_thumb_recv_msg failed ");
577 ms_ipc_delete_client_socket(&sock_info);
581 recv_str_len = strlen(recv_msg.org_path);
582 thumb_dbg_slog("recv %s(%d) from thumb daemon is successful", recv_msg.org_path, recv_str_len);
584 ms_ipc_delete_client_socket(&sock_info);
586 if (recv_str_len > max_length) {
587 thumb_err("user buffer is too small. Output's length is %d", recv_str_len);
588 SAFE_FREE(recv_msg.thumb_data);
589 return MS_MEDIA_ERR_INVALID_PARAMETER;
592 if (recv_msg.status == THUMB_FAIL) {
593 thumb_err("Failed to make thumbnail");
594 SAFE_FREE(recv_msg.thumb_data);
595 return MS_MEDIA_ERR_INVALID_PARAMETER;
598 if (msg_type != THUMB_REQUEST_SAVE_FILE) {
599 strncpy(thumb_path, recv_msg.dst_path, max_length);
602 thumb_info->origin_width = recv_msg.origin_width;
603 thumb_info->origin_height = recv_msg.origin_height;
605 SAFE_FREE(recv_msg.thumb_data);
607 return MS_MEDIA_ERR_NONE;
610 gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
616 int err = MS_MEDIA_ERR_NONE;
618 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
619 sock = g_io_channel_unix_get_fd(src);
621 header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
623 thumb_err("_media_thumb_write_socket socket : %d", sock);
625 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
626 thumb_err("_media_thumb_recv_msg failed ");
627 if (recv_msg.origin_path_size > 0) {
628 __media_thumb_pop_req_queue(recv_msg.org_path, TRUE);
630 thumb_err("origin path size is wrong.");
636 g_io_channel_shutdown(src, TRUE, NULL);
637 g_io_channel_unref(src);
638 //thumb_dbg("Completed..%s", recv_msg.org_path);
640 if (recv_msg.status == THUMB_FAIL) {
641 thumb_err("Failed to make thumbnail");
642 err = MS_MEDIA_ERR_INTERNAL;
645 if(__media_thumb_check_cancel()) {
647 thumbUserData* cb = (thumbUserData*)data;
648 if (cb->func != NULL)
649 cb->func(err, recv_msg.dst_path, cb->user_data);
653 __media_thumb_pop_req_queue(recv_msg.org_path, FALSE);
657 SAFE_FREE(recv_msg.thumb_data);
659 /* Check manage queue */
661 len = g_queue_get_length(g_manage_queue);
664 _media_thumb_send_request();
666 g_queue_free(g_manage_queue);
667 g_manage_queue = NULL;
674 gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
680 int err = MS_MEDIA_ERR_NONE;
682 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
683 sock = g_io_channel_unix_get_fd(src);
685 header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
687 thumb_err("_media_thumb_write_socket socket : %d", sock);
689 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
690 thumb_err("_media_thumb_recv_msg failed ");
691 if (recv_msg.request_id > 0) {
692 __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, TRUE);
694 thumb_err("origin path size is wrong.");
700 g_io_channel_shutdown(src, TRUE, NULL);
701 g_io_channel_unref(src);
703 if (recv_msg.status == THUMB_FAIL) {
704 thumb_err("Failed to make thumbnail");
705 err = MS_MEDIA_ERR_INTERNAL;
708 if(__media_thumb_check_cancel_for_raw()) {
710 thumbRawUserData* cb = (thumbRawUserData*)data;
711 if (cb->func != NULL)
712 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);
716 __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, FALSE);
720 SAFE_FREE(recv_msg.thumb_data);
722 /* Check manage queue */
723 if(g_manage_raw_queue) {
724 len = g_queue_get_length(g_manage_raw_queue);
727 _media_thumb_raw_data_send_request();
729 g_queue_free(g_manage_raw_queue);
730 g_manage_raw_queue = NULL;
737 int _media_thumb_send_request() {
738 int err = MS_MEDIA_ERR_NONE;
740 struct sockaddr_un serv_addr;
741 ms_sock_info_s sock_info;
742 thumbReq *req_manager = NULL;
744 sock_info.port = MS_THUMB_CREATOR_PORT;
746 err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
747 if(err != MS_MEDIA_ERR_NONE)
749 thumb_err("ms_ipc_create_client_socket failed");
753 memset(&serv_addr, 0, sizeof(serv_addr));
754 sock = sock_info.sock_fd;
755 serv_addr.sun_family = AF_UNIX;
756 strncpy(serv_addr.sun_path, THUMB_IPC_PATH, strlen(THUMB_IPC_PATH));
758 GIOChannel *channel = NULL;
759 channel = g_io_channel_unix_new(sock);
762 /* Connecting to the thumbnail server */
763 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
764 thumb_stderror("connect");
765 g_io_channel_shutdown(channel, TRUE, NULL);
766 g_io_channel_unref(channel);
767 return MS_MEDIA_ERR_SOCKET_CONN;
769 req_manager = (thumbReq *)g_queue_pop_head(g_manage_queue);
770 GSource *source = NULL;
771 source = g_io_create_watch(channel, G_IO_IN);
772 g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, req_manager->userData, NULL);
773 source_id = g_source_attach(source, g_main_context_get_thread_default());
777 memset((void *)&req_msg, 0, sizeof(thumbMsg));
781 req_msg.msg_type = req_manager->msg_type;
782 req_msg.request_id = 0;
783 req_msg.uid = req_manager->uid;
784 strncpy(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
785 req_msg.org_path[strlen(req_msg.org_path)] = '\0';
786 req_msg.dst_path[0] = '\0';
787 req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
788 req_msg.dest_path_size = 1;
789 req_msg.thumb_size = 0;
791 if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
792 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
793 g_io_channel_shutdown(channel, TRUE, NULL);
794 g_io_channel_unref(channel);
795 ms_ipc_delete_client_socket(&sock_info);
796 return MS_MEDIA_ERR_INVALID_PARAMETER;
799 unsigned char *buf = NULL;
801 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
803 if (send(sock, buf, buf_size, 0) != buf_size) {
804 thumb_err("sendto failed: %d", errno);
806 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
807 g_io_channel_shutdown(channel, TRUE, NULL);
808 g_io_channel_unref(channel);
809 ms_ipc_delete_client_socket(&sock_info);
810 return MS_MEDIA_ERR_SOCKET_SEND;
814 thumb_dbg("Sending msg to thumbnail daemon is successful");
817 if (req_manager->msg_type == THUMB_REQUEST_DB_INSERT) {
818 if (g_request_queue == NULL) {
819 g_request_queue = g_queue_new();
822 thumbReq *thumb_req = NULL;
823 THUMB_MALLOC(thumb_req, sizeof(thumbReq));
824 if (thumb_req == NULL) {
825 thumb_err("Failed to create request element");
826 return MS_MEDIA_ERR_INVALID_PARAMETER;
829 thumb_req->channel = channel;
830 thumb_req->path = strdup(req_manager->path);
831 thumb_req->source_id = source_id;
832 thumb_req->userData = req_manager->userData;
834 g_queue_push_tail(g_request_queue, (gpointer)thumb_req);
840 int _media_thumb_raw_data_send_request() {
841 int err = MS_MEDIA_ERR_NONE;
843 struct sockaddr_un serv_addr;
844 ms_sock_info_s sock_info;
845 thumbRawReq *req_manager = NULL;
847 sock_info.port = MS_THUMB_CREATOR_PORT;
849 err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
850 if(err != MS_MEDIA_ERR_NONE)
852 thumb_err("ms_ipc_create_client_socket failed");
856 memset(&serv_addr, 0, sizeof(serv_addr));
857 sock = sock_info.sock_fd;
858 serv_addr.sun_family = AF_UNIX;
859 strncpy(serv_addr.sun_path, THUMB_IPC_PATH, strlen(THUMB_IPC_PATH));
861 GIOChannel *channel = NULL;
862 channel = g_io_channel_unix_new(sock);
865 /* Connecting to the thumbnail server */
866 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
867 thumb_stderror("connect error");
868 g_io_channel_shutdown(channel, TRUE, NULL);
869 g_io_channel_unref(channel);
870 return MS_MEDIA_ERR_SOCKET_CONN;
873 req_manager = (thumbRawReq *)g_queue_pop_head(g_manage_raw_queue);
874 GSource *source = NULL;
875 source = g_io_create_watch(channel, G_IO_IN);
876 g_source_set_callback(source, (GSourceFunc)_media_thumb_raw_data_write_socket, req_manager->userData, NULL);
877 source_id = g_source_attach(source, g_main_context_get_thread_default());
880 memset((void *)&req_msg, 0, sizeof(thumbMsg));
884 req_msg.msg_type = req_manager->msg_type;
885 req_msg.request_id = req_manager->request_id;
886 req_msg.thumb_width = req_manager->width;
887 req_msg.thumb_height = req_manager->height;
888 req_msg.uid = req_manager->uid;
890 strncpy(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
891 req_msg.org_path[strlen(req_msg.org_path)] = '\0';
892 req_msg.dst_path[0] = '\0';
894 req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
895 req_msg.dest_path_size = 1;
896 req_msg.thumb_size = 0;
898 if (req_msg.origin_path_size > MAX_PATH_SIZE) {
899 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
900 g_io_channel_shutdown(channel, TRUE, NULL);
901 g_io_channel_unref(channel);
902 return MS_MEDIA_ERR_INVALID_PARAMETER;
905 unsigned char *buf = NULL;
907 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
909 if (send(sock, buf, buf_size, 0) != buf_size) {
910 thumb_err("sendto failed: %d", errno);
912 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
913 g_io_channel_shutdown(channel, TRUE, NULL);
914 g_io_channel_unref(channel);
915 return MS_MEDIA_ERR_SOCKET_SEND;
920 if (req_manager->msg_type == THUMB_REQUEST_RAW_DATA) {
921 if (g_request_raw_queue == NULL) {
922 g_request_raw_queue = g_queue_new();
924 thumbRawReq *thumb_req = NULL;
925 THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
926 if (thumb_req == NULL) {
927 thumb_err("Failed to create request element");
928 return MS_MEDIA_ERR_INVALID_PARAMETER;
930 thumb_req->channel = channel;
931 thumb_req->request_id = req_manager->request_id;
932 thumb_req->source_id = source_id;
933 thumb_req->userData = req_manager->userData;
935 g_queue_push_tail(g_request_raw_queue, (gpointer)thumb_req);
937 return MS_MEDIA_ERR_NONE;
940 int _media_thumb_request_async(int msg_type, const char *origin_path, thumbUserData *userData, uid_t uid)
942 int err = MS_MEDIA_ERR_NONE;
944 if(g_manage_queue == NULL) {
945 if(msg_type == THUMB_REQUEST_CANCEL_MEDIA)
946 return MS_MEDIA_ERR_INTERNAL;
948 g_manage_queue = g_queue_new();
950 thumbReq *thumb_req = NULL;
951 THUMB_MALLOC(thumb_req, sizeof(thumbReq));
952 if (thumb_req == NULL) {
953 thumb_err("Failed to create request element");
954 return MS_MEDIA_ERR_INVALID_PARAMETER;
957 thumb_req->msg_type = msg_type;
958 thumb_req->path = strdup(origin_path);
959 thumb_req->userData = userData;
960 thumb_req->isCanceled = false;
961 thumb_req->uid = uid;
963 g_queue_push_tail(g_manage_queue, (gpointer)thumb_req);
965 /* directly request at first time */
966 err = _media_thumb_send_request();
969 if(msg_type != THUMB_REQUEST_CANCEL_MEDIA) {
971 if ((msg_type == THUMB_REQUEST_DB_INSERT) && (__media_thumb_check_req_queue(origin_path) < 0)) {
972 thumb_err("duplicated request");
973 return MS_MEDIA_ERR_THUMB_DUPLICATED_REQUEST;
976 thumbReq *thumb_req = NULL;
977 THUMB_MALLOC(thumb_req, sizeof(thumbReq));
978 if (thumb_req == NULL) {
979 thumb_err("Failed to create request element");
980 return MS_MEDIA_ERR_INVALID_PARAMETER;
983 thumb_req->msg_type = msg_type;
984 thumb_req->path = strdup(origin_path);
985 thumb_req->userData = userData;
986 thumb_req->isCanceled = false;
987 thumb_req->uid = uid;
989 g_queue_push_tail(g_manage_queue, (gpointer)thumb_req);
992 err = __media_thumb_pop_manage_queue(origin_path);
999 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)
1001 int err = MS_MEDIA_ERR_NONE;
1003 if(g_manage_raw_queue == NULL) {
1004 if(msg_type == THUMB_REQUEST_CANCEL_RAW_DATA)
1005 return MS_MEDIA_ERR_INTERNAL;
1007 g_manage_raw_queue = g_queue_new();
1009 thumbRawReq *thumb_req = NULL;
1010 THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
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->request_id = request_id;
1018 thumb_req->path = strdup(origin_path);
1019 thumb_req->width = width;
1020 thumb_req->height= height;
1021 thumb_req->userData = userData;
1022 thumb_req->isCanceled = false;
1023 thumb_req->uid = uid;
1025 g_queue_push_tail(g_manage_raw_queue, (gpointer)thumb_req);
1027 /* directly request at first time */
1028 err = _media_thumb_raw_data_send_request();
1031 if(msg_type != THUMB_REQUEST_CANCEL_RAW_DATA) {
1033 thumbRawReq *thumb_req = NULL;
1034 THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
1035 if (thumb_req == NULL) {
1036 thumb_err("Failed to create request element");
1037 return MS_MEDIA_ERR_INVALID_PARAMETER;
1040 thumb_req->msg_type = msg_type;
1041 thumb_req->request_id = request_id;
1042 thumb_req->path = strdup(origin_path);
1043 thumb_req->width = width;
1044 thumb_req->height= height;
1045 thumb_req->userData = userData;
1046 thumb_req->isCanceled = false;
1047 thumb_req->uid = uid;
1049 g_queue_push_tail(g_manage_raw_queue, (gpointer)thumb_req);
1053 err = __media_thumb_pop_raw_data_manage_queue(request_id);
1060 int _media_thumb_request_cancel_all(bool isRaw) {
1066 thumbRawReq *tmp_raw_req = NULL;
1067 if(g_manage_raw_queue == NULL) {
1068 thumb_err("manage_queue is NULL");
1069 if(g_request_raw_queue != NULL) {
1070 /* Check request queue */
1071 len = g_queue_get_length(g_request_raw_queue);
1073 tmp_raw_req = g_queue_peek_head(g_request_raw_queue);
1074 if (tmp_raw_req != NULL)
1075 tmp_raw_req->isCanceled = true;
1076 return MS_MEDIA_ERR_NONE;
1079 thumb_err("request_queue is NULL");
1080 return MS_MEDIA_ERR_INTERNAL;
1082 req_len = g_queue_get_length(g_manage_raw_queue);
1083 for(i=0;i<req_len;i++) {
1084 tmp_raw_req = g_queue_pop_tail(g_manage_raw_queue);
1086 SAFE_FREE(tmp_raw_req->path);
1087 SAFE_FREE(tmp_raw_req->userData);
1088 SAFE_FREE(tmp_raw_req);
1092 g_queue_free (g_manage_raw_queue);
1093 g_manage_raw_queue = NULL;
1094 if(g_request_raw_queue != NULL) {
1095 /* Check request queue */
1096 len = g_queue_get_length(g_request_raw_queue);
1098 tmp_raw_req = g_queue_peek_head(g_request_raw_queue);
1099 if (tmp_raw_req != NULL)
1100 tmp_raw_req->isCanceled = true;
1104 thumbReq *tmp_req = NULL;
1105 if(g_manage_queue == NULL) {
1106 thumb_err("manage_queue is NULL");
1107 if(g_request_queue != NULL) {
1108 /* Check request queue */
1109 len = g_queue_get_length(g_request_queue);
1111 tmp_req = g_queue_peek_head(g_request_queue);
1112 if (tmp_req != NULL)
1113 tmp_req->isCanceled = true;
1114 return MS_MEDIA_ERR_NONE;
1117 thumb_err("request_queue is NULL");
1118 return MS_MEDIA_ERR_INTERNAL;
1120 req_len = g_queue_get_length(g_manage_queue);
1121 for(i=0;i<req_len;i++) {
1122 tmp_req = g_queue_pop_tail(g_manage_queue);
1124 SAFE_FREE(tmp_req->path);
1125 SAFE_FREE(tmp_req->userData);
1130 g_queue_free (g_manage_queue);
1131 g_manage_queue = NULL;
1132 if(g_request_queue != NULL) {
1133 /* Check request queue */
1134 len = g_queue_get_length(g_request_queue);
1136 tmp_req = g_queue_peek_head(g_request_queue);
1137 if (tmp_req != NULL)
1138 tmp_req->isCanceled = true;
1143 return MS_MEDIA_ERR_NONE;