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);
274 if (strlen(msg->org_path) == 0 || strlen(msg->org_path) >= MAX_FILEPATH_LEN) {
275 thumb_err("org_path size is invalid %d", strlen(msg->org_path));
277 return MS_MEDIA_ERR_SOCKET_RECEIVE;
280 /* it can be empty string */
281 if (strlen(msg->dst_path) >= MAX_FILEPATH_LEN) {
282 thumb_err("dst_path size is invalid %d", strlen(msg->dst_path));
284 return MS_MEDIA_ERR_SOCKET_RECEIVE;
288 if (msg->thumb_size < 0) {
289 thumb_err("recv data is wrong");
290 SAFE_FREE(block_buf);
291 return MS_MEDIA_ERR_SOCKET_RECEIVE;
294 if (msg->thumb_size > 0) {
295 remain_size = msg->thumb_size;
296 THUMB_MALLOC(buf, remain_size);
298 SAFE_FREE(block_buf);
299 return MS_MEDIA_ERR_OUT_OF_MEMORY;
302 while (remain_size > 0) {
303 if (remain_size < THUMB_SOCK_BLOCK_SIZE) {
304 block_size = remain_size;
306 if ((recv_msg_len = recv(sock, block_buf, block_size, 0)) < 0) {
307 thumb_stderror("recv failed");
309 SAFE_FREE(block_buf);
310 return _media_thumb_get_error();
312 memcpy(buf+recv_block, block_buf, block_size);
313 recv_block += block_size;
314 remain_size -= block_size;
317 SAFE_FREE(msg->thumb_data);
318 THUMB_MALLOC(msg->thumb_data, msg->thumb_size);
319 if (msg->thumb_data != NULL) {
320 memcpy(msg->thumb_data, buf, msg->thumb_size);
323 SAFE_FREE(block_buf);
325 return MS_MEDIA_ERR_OUT_OF_MEMORY;
330 SAFE_FREE(block_buf);
332 return MS_MEDIA_ERR_NONE;
335 int _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
337 if (req_msg == NULL || buf == NULL)
338 return MS_MEDIA_ERR_INVALID_PARAMETER;
340 int thumb_data_len = 0;
344 header_size = sizeof(thumbMsg) - sizeof(unsigned char *);
345 thumb_data_len = req_msg->thumb_size;
346 if (thumb_data_len < 0)
347 return MS_MEDIA_ERR_INVALID_PARAMETER;
349 //thumb_dbg("Basic Size[%d] org_path[%s] dst_path[%s] thumb_data_len[%d]", header_size, req_msg->org_path, req_msg->dst_path, thumb_data_len);
351 size = header_size + thumb_data_len;
352 THUMB_MALLOC(*buf, size);
357 memcpy(*buf, req_msg, header_size);
358 if (thumb_data_len > 0)
359 memcpy((*buf) + header_size, req_msg->thumb_data, thumb_data_len);
363 return MS_MEDIA_ERR_NONE;
366 gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
371 int err = MS_MEDIA_ERR_NONE;
373 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
374 sock = g_io_channel_unix_get_fd(src);
376 header_size = sizeof(thumbMsg) - sizeof(unsigned char *);
378 thumb_err("_media_thumb_write_socket socket : %d", sock);
380 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
381 thumb_err("_media_thumb_recv_msg failed ");
387 if (recv_msg.status != MS_MEDIA_ERR_NONE) {
388 err = recv_msg.status;
389 thumb_err("Failed to make thumbnail (%d)", err);
392 if (__media_thumb_check_cancel()) {
394 thumbUserData* cb = (thumbUserData*)data;
395 if (cb->func != NULL)
396 cb->func(err, recv_msg.dst_path, cb->user_data);
407 gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
412 int err = MS_MEDIA_ERR_NONE;
414 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
415 sock = g_io_channel_unix_get_fd(src);
417 header_size = sizeof(thumbMsg) - sizeof(unsigned char *);
419 thumb_err("_media_thumb_write_socket socket : %d", sock);
421 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
422 thumb_err("_media_thumb_recv_msg failed ");
423 __media_thumb_pop_raw_data();
428 if (recv_msg.status != MS_MEDIA_ERR_NONE) {
429 err = recv_msg.status;
430 thumb_err("Failed to make thumbnail (%d)", err);
433 if (__media_thumb_check_cancel_for_raw()) {
435 thumbRawUserData* cb = (thumbRawUserData*)data;
436 if (cb->func != NULL)
437 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);
441 __media_thumb_pop_raw_data();
448 static int _media_thumb_send_request()
450 int err = MS_MEDIA_ERR_NONE;
452 struct sockaddr_un serv_addr;
453 thumbReq *req_manager = NULL;
456 err = ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock);
457 if (err != MS_MEDIA_ERR_NONE) {
458 thumb_err("ms_ipc_create_client_socket failed");
462 memset(&serv_addr, 0, sizeof(serv_addr));
463 serv_addr.sun_family = AF_UNIX;
464 SAFE_STRLCPY(serv_addr.sun_path, THUMB_IPC_PATH, sizeof(serv_addr.sun_path));
466 GIOChannel *channel = NULL;
467 channel = g_io_channel_unix_new(sock);
470 /* Connecting to the thumbnail server */
471 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
472 thumb_stderror("connect");
474 err = MS_MEDIA_ERR_PERMISSION_DENIED;
476 err = MS_MEDIA_ERR_SOCKET_CONN;
478 g_io_channel_shutdown(channel, TRUE, NULL);
479 g_io_channel_unref(channel);
485 req_manager = (thumbReq *)g_queue_peek_head(g_manage_queue);
487 if (req_manager == NULL) {
488 thumb_err("queue peek fail");
489 g_io_channel_shutdown(channel, TRUE, NULL);
490 g_io_channel_unref(channel);
492 return MS_MEDIA_ERR_INVALID_PARAMETER;
495 GSource *source = NULL;
496 source = g_io_create_watch(channel, G_IO_IN);
497 g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, req_manager->userData, NULL);
498 source_id = g_source_attach(source, g_main_context_get_thread_default());
501 memset((void *)&req_msg, 0, sizeof(thumbMsg));
505 req_msg.msg_type = req_manager->msg_type;
506 req_msg.request_id = 0;
507 req_msg.uid = req_manager->uid;
508 SAFE_STRLCPY(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
509 req_msg.dst_path[0] = '\0';
510 req_msg.thumb_size = 0;
512 unsigned char *buf = NULL;
514 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
516 if (send(sock, buf, buf_size, 0) != buf_size) {
517 thumb_err("sendto failed: %d", errno);
519 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
520 g_io_channel_shutdown(channel, TRUE, NULL);
521 g_io_channel_unref(channel);
523 return MS_MEDIA_ERR_SOCKET_SEND;
527 thumb_dbg("Sending msg to thumbnail daemon is successful");
529 if (req_manager->msg_type == THUMB_REQUEST_DB_INSERT) {
530 req_manager->channel = channel;
531 req_manager->source_id = source_id;
532 req_manager->isRequested = true;
538 static int _media_thumb_raw_data_send_request()
540 int err = MS_MEDIA_ERR_NONE;
542 struct sockaddr_un serv_addr;
543 thumbRawReq *req_manager = NULL;
546 err = ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock);
547 if (err != MS_MEDIA_ERR_NONE) {
548 thumb_err("ms_ipc_create_client_socket failed");
552 memset(&serv_addr, 0, sizeof(serv_addr));
553 serv_addr.sun_family = AF_UNIX;
554 SAFE_STRLCPY(serv_addr.sun_path, THUMB_IPC_PATH, sizeof(serv_addr.sun_path));
556 GIOChannel *channel = NULL;
557 channel = g_io_channel_unix_new(sock);
560 /* Connecting to the thumbnail server */
561 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
562 thumb_stderror("connect error");
564 err = MS_MEDIA_ERR_PERMISSION_DENIED;
566 err = MS_MEDIA_ERR_SOCKET_CONN;
568 g_io_channel_shutdown(channel, TRUE, NULL);
569 g_io_channel_unref(channel);
574 req_manager = (thumbRawReq *)g_queue_peek_head(g_manage_raw_queue);
576 if (req_manager == NULL) {
577 thumb_err("queue peek fail");
578 g_io_channel_shutdown(channel, TRUE, NULL);
579 g_io_channel_unref(channel);
581 return MS_MEDIA_ERR_INVALID_PARAMETER;
584 GSource *source = NULL;
585 source = g_io_create_watch(channel, G_IO_IN);
586 g_source_set_callback(source, (GSourceFunc)_media_thumb_raw_data_write_socket, req_manager->userData, NULL);
587 source_id = g_source_attach(source, g_main_context_get_thread_default());
590 memset((void *)&req_msg, 0, sizeof(thumbMsg));
594 req_msg.msg_type = req_manager->msg_type;
595 req_msg.request_id = req_manager->request_id;
596 req_msg.thumb_width = req_manager->width;
597 req_msg.thumb_height = req_manager->height;
598 req_msg.uid = req_manager->uid;
600 SAFE_STRLCPY(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
601 req_msg.dst_path[0] = '\0';
602 req_msg.thumb_size = 0;
604 unsigned char *buf = NULL;
606 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
608 if (send(sock, buf, buf_size, 0) != buf_size) {
609 thumb_err("sendto failed: %d", errno);
611 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
612 g_io_channel_shutdown(channel, TRUE, NULL);
613 g_io_channel_unref(channel);
614 return MS_MEDIA_ERR_SOCKET_SEND;
619 if (req_manager->msg_type == THUMB_REQUEST_RAW_DATA) {
620 req_manager->channel = channel;
621 req_manager->source_id = source_id;
622 req_manager->isRequested = true;
624 return MS_MEDIA_ERR_NONE;
627 int _media_thumb_request_async(int msg_type, unsigned int request_id, const char *origin_path, thumbUserData *userData, uid_t uid)
629 int err = MS_MEDIA_ERR_NONE;
632 if (msg_type == THUMB_REQUEST_CANCEL_MEDIA)
633 return __media_thumb_cancel(request_id);
635 if (g_manage_queue == NULL)
636 g_manage_queue = g_queue_new();
638 thumbReq *thumb_req = NULL;
639 THUMB_MALLOC(thumb_req, sizeof(thumbReq));
640 thumb_retvm_if(thumb_req == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Failed to create request element");
642 thumb_req->msg_type = msg_type;
643 thumb_req->path = g_strdup(origin_path);
644 thumb_req->userData = userData;
645 thumb_req->isCanceled = false;
646 thumb_req->isRequested = false;
647 thumb_req->request_id = request_id;
648 thumb_req->uid = uid;
650 len = g_queue_get_length(g_manage_queue);
651 g_queue_push_tail(g_manage_queue, (gpointer)thumb_req);
654 err = _media_thumb_send_request();
659 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)
661 int err = MS_MEDIA_ERR_NONE;
663 thumbRawReq *thumb_req = NULL;
665 if (msg_type == THUMB_REQUEST_CANCEL_RAW_DATA)
666 return __media_thumb_cancel_raw_data(request_id);
668 if (g_manage_raw_queue == NULL)
669 g_manage_raw_queue = g_queue_new();
671 THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
672 if (thumb_req == NULL) {
673 thumb_err("Failed to create request element");
674 return MS_MEDIA_ERR_INVALID_PARAMETER;
677 thumb_req->msg_type = msg_type;
678 thumb_req->request_id = request_id;
679 thumb_req->path = g_strdup(origin_path);
680 thumb_req->width = width;
681 thumb_req->height = height;
682 thumb_req->userData = userData;
683 thumb_req->isCanceled = false;
684 thumb_req->isRequested = false;
685 thumb_req->uid = uid;
687 len = g_queue_get_length(g_manage_raw_queue);
688 g_queue_push_tail(g_manage_raw_queue, (gpointer)thumb_req);
691 err = _media_thumb_raw_data_send_request();
696 int _media_thumb_request_cancel_all()
698 if (g_manage_raw_queue != NULL) {
699 while (g_queue_get_length(g_manage_raw_queue) > 0) {
700 thumbRawReq *req = (thumbRawReq *)g_queue_peek_tail(g_manage_raw_queue);
702 if (req->isRequested == true) {
703 req->isCanceled = true;
704 thumb_dbg("Last item!!");
708 g_queue_pop_tail(g_manage_raw_queue);
710 SAFE_FREE(req->path);
711 SAFE_FREE(req->userData);
715 thumb_dbg("Queue is empty!!");
721 return MS_MEDIA_ERR_NONE;