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_manage_queue = NULL;
38 static GQueue *g_manage_raw_queue = NULL;
44 unsigned int request_id;
50 thumbUserData *userData;
64 thumbRawUserData *userData;
67 static int _media_thumb_send_request();
68 static 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_pop()
86 if (g_manage_queue != NULL) {
87 thumbReq *req = (thumbReq *)g_queue_pop_head(g_manage_queue);
89 GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
91 g_io_channel_shutdown(req->channel, TRUE, NULL);
92 g_io_channel_unref(req->channel);
94 if (source_id != NULL) {
95 g_source_destroy(source_id);
97 thumb_err("G_SOURCE_ID is NULL");
100 SAFE_FREE(req->path);
101 SAFE_FREE(req->userData);
105 /* Check manage queue */
106 len = g_queue_get_length(g_manage_queue);
108 _media_thumb_send_request();
112 int __media_thumb_cancel(unsigned int request_id)
117 if (g_manage_queue != NULL) {
118 len = g_queue_get_length(g_manage_queue);
120 for (i = 0; i < len; i++) {
121 thumbReq *req = NULL;
122 req = (thumbReq *)g_queue_peek_nth(g_manage_queue, i);
123 if (req == NULL) continue;
125 if (req->request_id == request_id) {
126 if (req->isRequested == true) {
127 req->isCanceled = true;
129 g_queue_pop_nth(g_manage_queue, i);
131 SAFE_FREE(req->path);
132 SAFE_FREE(req->userData);
144 return MS_MEDIA_ERR_INTERNAL;
146 return MS_MEDIA_ERR_NONE;
149 void __media_thumb_pop_raw_data()
153 if (g_manage_raw_queue != NULL) {
154 thumbRawReq *req = (thumbRawReq *)g_queue_pop_head(g_manage_raw_queue);
156 GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
158 g_io_channel_shutdown(req->channel, TRUE, NULL);
159 g_io_channel_unref(req->channel);
161 if (source_id != NULL) {
162 g_source_destroy(source_id);
164 thumb_err("G_SOURCE_ID is NULL");
167 SAFE_FREE(req->path);
168 SAFE_FREE(req->userData);
172 /* Check manage queue */
173 len = g_queue_get_length(g_manage_raw_queue);
175 _media_thumb_raw_data_send_request();
179 int __media_thumb_cancel_raw_data(int request_id)
184 if (g_manage_raw_queue != NULL) {
185 len = g_queue_get_length(g_manage_raw_queue);
187 for (i = 0; i < len; i++) {
188 thumbRawReq *req = NULL;
189 req = (thumbRawReq *)g_queue_peek_nth(g_manage_raw_queue, i);
190 if (req == NULL) continue;
192 if (req->request_id == request_id) {
193 if (req->isRequested == true) {
194 req->isCanceled = true;
196 g_queue_pop_nth(g_manage_raw_queue, i);
198 SAFE_FREE(req->path);
199 SAFE_FREE(req->userData);
211 return MS_MEDIA_ERR_INTERNAL;
213 return MS_MEDIA_ERR_NONE;
217 bool __media_thumb_check_cancel(void)
219 thumbReq *req = NULL;
220 req = (thumbReq *)g_queue_peek_head(g_manage_queue);
232 bool __media_thumb_check_cancel_for_raw(void)
234 thumbRawReq *req = NULL;
235 req = (thumbRawReq *)g_queue_peek_head(g_manage_raw_queue);
247 int _media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg)
249 int recv_msg_len = 0;
251 int block_size = THUMB_SOCK_BLOCK_SIZE;
253 unsigned char *buf = NULL;
254 unsigned char *block_buf = NULL;
256 THUMB_MALLOC(buf, header_size);
257 THUMB_MALLOC(block_buf, THUMB_SOCK_BLOCK_SIZE);
258 if (buf == NULL || block_buf == NULL) {
259 thumb_err("memory allocation failed");
261 SAFE_FREE(block_buf);
262 return MS_MEDIA_ERR_OUT_OF_MEMORY;
265 if ((recv_msg_len = recv(sock, buf, header_size, 0)) <= 0) {
266 thumb_stderror("recv failed");
268 SAFE_FREE(block_buf);
269 return _media_thumb_get_error();
272 memcpy(msg, buf, header_size);
273 //thumb_dbg("origin_path_size : %d, dest_path_size : %d, thumb_size : %d", msg->origin_path_size, msg->dest_path_size, msg->thumb_size);
276 if (msg->origin_path_size < 0 || msg->dest_path_size < 0 || msg->thumb_size < 0) {
277 thumb_err("recv data is wrong");
278 SAFE_FREE(block_buf);
279 return MS_MEDIA_ERR_SOCKET_RECEIVE;
282 remain_size = msg->origin_path_size + msg->dest_path_size + msg->thumb_size;
283 THUMB_MALLOC(buf, remain_size);
285 SAFE_FREE(block_buf);
286 return MS_MEDIA_ERR_OUT_OF_MEMORY;
289 while (remain_size > 0) {
290 if (remain_size < THUMB_SOCK_BLOCK_SIZE) {
291 block_size = remain_size;
293 if ((recv_msg_len = recv(sock, block_buf, block_size, 0)) < 0) {
294 thumb_stderror("recv failed");
296 SAFE_FREE(block_buf);
297 return _media_thumb_get_error();
299 memcpy(buf+recv_block, block_buf, block_size);
300 recv_block += block_size;
301 remain_size -= block_size;
304 SAFE_STRLCPY(msg->org_path, (char *)buf, sizeof(msg->org_path));
305 SAFE_STRLCPY(msg->dst_path, (char *)buf + msg->origin_path_size, sizeof(msg->dst_path));
307 SAFE_FREE(msg->thumb_data);
308 if (msg->thumb_size > 0) {
309 THUMB_MALLOC(msg->thumb_data, msg->thumb_size);
310 if (msg->thumb_data != NULL) {
311 memcpy(msg->thumb_data, buf + msg->origin_path_size + msg->dest_path_size, msg->thumb_size);
314 SAFE_FREE(block_buf);
316 return MS_MEDIA_ERR_OUT_OF_MEMORY;
321 SAFE_FREE(block_buf);
323 return MS_MEDIA_ERR_NONE;
326 int _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
328 if (req_msg == NULL || buf == NULL) {
329 return MS_MEDIA_ERR_INVALID_PARAMETER;
332 int org_path_len = 0;
333 int dst_path_len = 0;
334 int thumb_data_len = 0;
338 header_size = sizeof(thumbMsg) -(MAX_FILEPATH_LEN * 2) - sizeof(unsigned char *);
339 org_path_len = req_msg->origin_path_size;
340 dst_path_len = req_msg->dest_path_size;
341 thumb_data_len = req_msg->thumb_size;
343 //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);
345 size = header_size + org_path_len + dst_path_len + thumb_data_len;
346 THUMB_MALLOC(*buf, size);
351 memcpy(*buf, req_msg, header_size);
352 memcpy((*buf)+header_size, req_msg->org_path, org_path_len);
353 memcpy((*buf)+header_size + org_path_len, req_msg->dst_path, dst_path_len);
354 if (thumb_data_len > 0)
355 memcpy((*buf)+header_size + org_path_len + dst_path_len, req_msg->thumb_data, thumb_data_len);
359 return MS_MEDIA_ERR_NONE;
362 gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
367 int err = MS_MEDIA_ERR_NONE;
369 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
370 sock = g_io_channel_unix_get_fd(src);
372 header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
374 thumb_err("_media_thumb_write_socket socket : %d", sock);
376 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
377 thumb_err("_media_thumb_recv_msg failed ");
383 if (recv_msg.status != MS_MEDIA_ERR_NONE) {
384 err = recv_msg.status;
385 thumb_err("Failed to make thumbnail (%d)", err);
388 if (__media_thumb_check_cancel()) {
390 thumbUserData* cb = (thumbUserData*)data;
391 if (cb->func != NULL)
392 cb->func(err, recv_msg.dst_path, cb->user_data);
403 gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
408 int err = MS_MEDIA_ERR_NONE;
410 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
411 sock = g_io_channel_unix_get_fd(src);
413 header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
415 thumb_err("_media_thumb_write_socket socket : %d", sock);
417 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
418 thumb_err("_media_thumb_recv_msg failed ");
419 __media_thumb_pop_raw_data();
424 if (recv_msg.status != MS_MEDIA_ERR_NONE) {
425 err = recv_msg.status;
426 thumb_err("Failed to make thumbnail (%d)", err);
429 if (__media_thumb_check_cancel_for_raw()) {
431 thumbRawUserData* cb = (thumbRawUserData*)data;
432 if (cb->func != NULL)
433 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);
437 __media_thumb_pop_raw_data();
444 static int _media_thumb_send_request()
446 int err = MS_MEDIA_ERR_NONE;
448 struct sockaddr_un serv_addr;
449 thumbReq *req_manager = NULL;
452 err = ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock);
453 if (err != MS_MEDIA_ERR_NONE) {
454 thumb_err("ms_ipc_create_client_socket failed");
458 memset(&serv_addr, 0, sizeof(serv_addr));
459 serv_addr.sun_family = AF_UNIX;
460 SAFE_STRLCPY(serv_addr.sun_path, THUMB_IPC_PATH, sizeof(serv_addr.sun_path));
462 GIOChannel *channel = NULL;
463 channel = g_io_channel_unix_new(sock);
466 /* Connecting to the thumbnail server */
467 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
468 thumb_stderror("connect");
470 err = MS_MEDIA_ERR_PERMISSION_DENIED;
472 err = MS_MEDIA_ERR_SOCKET_CONN;
474 g_io_channel_shutdown(channel, TRUE, NULL);
475 g_io_channel_unref(channel);
481 req_manager = (thumbReq *)g_queue_peek_head(g_manage_queue);
483 if (req_manager == NULL) {
484 thumb_err("queue peek fail");
485 g_io_channel_shutdown(channel, TRUE, NULL);
486 g_io_channel_unref(channel);
488 return MS_MEDIA_ERR_INVALID_PARAMETER;
491 GSource *source = NULL;
492 source = g_io_create_watch(channel, G_IO_IN);
493 g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, req_manager->userData, NULL);
494 source_id = g_source_attach(source, g_main_context_get_thread_default());
498 memset((void *)&req_msg, 0, sizeof(thumbMsg));
502 req_msg.msg_type = req_manager->msg_type;
503 req_msg.request_id = 0;
504 req_msg.uid = req_manager->uid;
505 SAFE_STRLCPY(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
506 req_msg.dst_path[0] = '\0';
507 req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
508 req_msg.dest_path_size = 1;
509 req_msg.thumb_size = 0;
511 if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
512 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
513 g_io_channel_shutdown(channel, TRUE, NULL);
514 g_io_channel_unref(channel);
516 return MS_MEDIA_ERR_INVALID_PARAMETER;
519 unsigned char *buf = NULL;
521 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
523 if (send(sock, buf, buf_size, 0) != buf_size) {
524 thumb_err("sendto failed: %d", errno);
526 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
527 g_io_channel_shutdown(channel, TRUE, NULL);
528 g_io_channel_unref(channel);
530 return MS_MEDIA_ERR_SOCKET_SEND;
534 thumb_dbg("Sending msg to thumbnail daemon is successful");
536 if (req_manager->msg_type == THUMB_REQUEST_DB_INSERT) {
537 req_manager->channel = channel;
538 req_manager->source_id = source_id;
539 req_manager->isRequested = true;
545 static int _media_thumb_raw_data_send_request()
547 int err = MS_MEDIA_ERR_NONE;
549 struct sockaddr_un serv_addr;
550 thumbRawReq *req_manager = NULL;
553 err = ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock);
554 if (err != MS_MEDIA_ERR_NONE) {
555 thumb_err("ms_ipc_create_client_socket failed");
559 memset(&serv_addr, 0, sizeof(serv_addr));
560 serv_addr.sun_family = AF_UNIX;
561 SAFE_STRLCPY(serv_addr.sun_path, THUMB_IPC_PATH, sizeof(serv_addr.sun_path));
563 GIOChannel *channel = NULL;
564 channel = g_io_channel_unix_new(sock);
567 /* Connecting to the thumbnail server */
568 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
569 thumb_stderror("connect error");
571 err = MS_MEDIA_ERR_PERMISSION_DENIED;
573 err = MS_MEDIA_ERR_SOCKET_CONN;
575 g_io_channel_shutdown(channel, TRUE, NULL);
576 g_io_channel_unref(channel);
581 req_manager = (thumbRawReq *)g_queue_peek_head(g_manage_raw_queue);
583 if (req_manager == NULL) {
584 thumb_err("queue peek fail");
585 g_io_channel_shutdown(channel, TRUE, NULL);
586 g_io_channel_unref(channel);
588 return MS_MEDIA_ERR_INVALID_PARAMETER;
591 GSource *source = NULL;
592 source = g_io_create_watch(channel, G_IO_IN);
593 g_source_set_callback(source, (GSourceFunc)_media_thumb_raw_data_write_socket, req_manager->userData, NULL);
594 source_id = g_source_attach(source, g_main_context_get_thread_default());
597 memset((void *)&req_msg, 0, sizeof(thumbMsg));
601 req_msg.msg_type = req_manager->msg_type;
602 req_msg.request_id = req_manager->request_id;
603 req_msg.thumb_width = req_manager->width;
604 req_msg.thumb_height = req_manager->height;
605 req_msg.uid = req_manager->uid;
607 SAFE_STRLCPY(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
608 req_msg.dst_path[0] = '\0';
610 req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
611 req_msg.dest_path_size = 1;
612 req_msg.thumb_size = 0;
614 if (req_msg.origin_path_size > MAX_PATH_SIZE) {
615 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
616 g_io_channel_shutdown(channel, TRUE, NULL);
617 g_io_channel_unref(channel);
618 return MS_MEDIA_ERR_INVALID_PARAMETER;
621 unsigned char *buf = NULL;
623 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
625 if (send(sock, buf, buf_size, 0) != buf_size) {
626 thumb_err("sendto failed: %d", errno);
628 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
629 g_io_channel_shutdown(channel, TRUE, NULL);
630 g_io_channel_unref(channel);
631 return MS_MEDIA_ERR_SOCKET_SEND;
636 if (req_manager->msg_type == THUMB_REQUEST_RAW_DATA) {
637 req_manager->channel = channel;
638 req_manager->source_id = source_id;
639 req_manager->isRequested = true;
641 return MS_MEDIA_ERR_NONE;
644 int _media_thumb_request_async(int msg_type, unsigned int request_id, const char *origin_path, thumbUserData *userData, uid_t uid)
646 int err = MS_MEDIA_ERR_NONE;
649 if (msg_type == THUMB_REQUEST_CANCEL_MEDIA)
650 return __media_thumb_cancel(request_id);
652 if (g_manage_queue == NULL)
653 g_manage_queue = g_queue_new();
655 thumbReq *thumb_req = NULL;
656 THUMB_MALLOC(thumb_req, sizeof(thumbReq));
657 thumb_retvm_if(thumb_req == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Failed to create request element");
659 thumb_req->msg_type = msg_type;
660 thumb_req->path = g_strdup(origin_path);
661 thumb_req->userData = userData;
662 thumb_req->isCanceled = false;
663 thumb_req->isRequested = false;
664 thumb_req->request_id = request_id;
665 thumb_req->uid = uid;
667 len = g_queue_get_length(g_manage_queue);
668 g_queue_push_tail(g_manage_queue, (gpointer)thumb_req);
671 err = _media_thumb_send_request();
676 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)
678 int err = MS_MEDIA_ERR_NONE;
680 thumbRawReq *thumb_req = NULL;
682 if (msg_type == THUMB_REQUEST_CANCEL_RAW_DATA)
683 return __media_thumb_cancel_raw_data(request_id);
685 if (g_manage_raw_queue == NULL)
686 g_manage_raw_queue = g_queue_new();
688 THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
689 if (thumb_req == NULL) {
690 thumb_err("Failed to create request element");
691 return MS_MEDIA_ERR_INVALID_PARAMETER;
694 thumb_req->msg_type = msg_type;
695 thumb_req->request_id = request_id;
696 thumb_req->path = g_strdup(origin_path);
697 thumb_req->width = width;
698 thumb_req->height = height;
699 thumb_req->userData = userData;
700 thumb_req->isCanceled = false;
701 thumb_req->isRequested = false;
702 thumb_req->uid = uid;
704 len = g_queue_get_length(g_manage_raw_queue);
705 g_queue_push_tail(g_manage_raw_queue, (gpointer)thumb_req);
708 err = _media_thumb_raw_data_send_request();
713 int _media_thumb_request_cancel_all()
715 if (g_manage_raw_queue != NULL) {
716 while (g_queue_get_length(g_manage_raw_queue) > 0) {
717 thumbRawReq *req = (thumbRawReq *)g_queue_peek_tail(g_manage_raw_queue);
719 if (req->isRequested == true) {
720 req->isCanceled = true;
721 thumb_dbg("Last item!!");
725 g_queue_pop_tail(g_manage_raw_queue);
727 SAFE_FREE(req->path);
728 SAFE_FREE(req->userData);
732 thumb_dbg("Queue is empty!!");
738 return MS_MEDIA_ERR_NONE;