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 10240
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)
250 int block_size = THUMB_SOCK_BLOCK_SIZE;
252 unsigned char *buf = NULL;
253 unsigned char *block_buf = NULL;
255 THUMB_MALLOC(buf, header_size);
256 THUMB_MALLOC(block_buf, THUMB_SOCK_BLOCK_SIZE);
257 if (buf == NULL || block_buf == NULL) {
258 thumb_err("memory allocation failed");
260 SAFE_FREE(block_buf);
261 return MS_MEDIA_ERR_OUT_OF_MEMORY;
264 if (recv(sock, buf, header_size, 0) < 0) {
265 thumb_stderror("recv failed");
267 SAFE_FREE(block_buf);
268 return _media_thumb_get_error();
271 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));
276 SAFE_FREE(block_buf);
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));
283 SAFE_FREE(block_buf);
284 return MS_MEDIA_ERR_SOCKET_RECEIVE;
287 if (msg->thumb_size < 0) {
288 thumb_err("recv data is wrong");
289 SAFE_FREE(block_buf);
290 return MS_MEDIA_ERR_SOCKET_RECEIVE;
293 if (msg->thumb_size > 0) {
294 remain_size = msg->thumb_size;
295 THUMB_MALLOC(buf, remain_size);
297 SAFE_FREE(block_buf);
298 return MS_MEDIA_ERR_OUT_OF_MEMORY;
301 while (remain_size > 0) {
302 if (remain_size < THUMB_SOCK_BLOCK_SIZE) {
303 block_size = remain_size;
305 if (recv(sock, block_buf, block_size, 0) < 0) {
306 thumb_stderror("recv failed");
308 SAFE_FREE(block_buf);
309 return _media_thumb_get_error();
311 memcpy(buf+recv_block, block_buf, block_size);
312 recv_block += block_size;
313 remain_size -= block_size;
316 SAFE_FREE(msg->thumb_data);
317 THUMB_MALLOC(msg->thumb_data, msg->thumb_size);
318 if (msg->thumb_data != NULL) {
319 memcpy(msg->thumb_data, buf, msg->thumb_size);
322 SAFE_FREE(block_buf);
324 return MS_MEDIA_ERR_OUT_OF_MEMORY;
329 SAFE_FREE(block_buf);
331 return MS_MEDIA_ERR_NONE;
334 int _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
336 if (req_msg == NULL || buf == NULL)
337 return MS_MEDIA_ERR_INVALID_PARAMETER;
339 int thumb_data_len = 0;
343 header_size = sizeof(thumbMsg) - sizeof(unsigned char *);
344 thumb_data_len = req_msg->thumb_size;
345 if (thumb_data_len < 0)
346 return MS_MEDIA_ERR_INVALID_PARAMETER;
348 //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);
350 size = header_size + thumb_data_len;
351 THUMB_MALLOC(*buf, size);
356 memcpy(*buf, req_msg, header_size);
357 if (thumb_data_len > 0)
358 memcpy((*buf) + header_size, req_msg->thumb_data, thumb_data_len);
362 return MS_MEDIA_ERR_NONE;
365 gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
370 int err = MS_MEDIA_ERR_NONE;
372 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
373 sock = g_io_channel_unix_get_fd(src);
375 header_size = sizeof(thumbMsg) - sizeof(unsigned char *);
377 thumb_err("_media_thumb_write_socket socket : %d", sock);
379 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
380 thumb_err("_media_thumb_recv_msg failed ");
386 if (recv_msg.status != MS_MEDIA_ERR_NONE) {
387 err = recv_msg.status;
388 thumb_err("Failed to make thumbnail (%d)", err);
391 if (__media_thumb_check_cancel()) {
393 thumbUserData* cb = (thumbUserData*)data;
394 if (cb->func != NULL)
395 cb->func(err, recv_msg.dst_path, cb->user_data);
406 gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
411 int err = MS_MEDIA_ERR_NONE;
413 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
414 sock = g_io_channel_unix_get_fd(src);
416 header_size = sizeof(thumbMsg) - sizeof(unsigned char *);
418 thumb_err("_media_thumb_write_socket socket : %d", sock);
420 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
421 thumb_err("_media_thumb_recv_msg failed ");
422 __media_thumb_pop_raw_data();
427 if (recv_msg.status != MS_MEDIA_ERR_NONE) {
428 err = recv_msg.status;
429 thumb_err("Failed to make thumbnail (%d)", err);
432 if (__media_thumb_check_cancel_for_raw()) {
434 thumbRawUserData* cb = (thumbRawUserData*)data;
435 if (cb->func != NULL)
436 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);
440 __media_thumb_pop_raw_data();
447 static int _media_thumb_send_request()
449 int err = MS_MEDIA_ERR_NONE;
451 struct sockaddr_un serv_addr;
452 thumbReq *req_manager = NULL;
455 err = ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock);
456 if (err != MS_MEDIA_ERR_NONE) {
457 thumb_err("ms_ipc_create_client_socket failed");
461 memset(&serv_addr, 0, sizeof(serv_addr));
462 serv_addr.sun_family = AF_UNIX;
463 SAFE_STRLCPY(serv_addr.sun_path, THUMB_IPC_PATH, sizeof(serv_addr.sun_path));
465 GIOChannel *channel = NULL;
466 channel = g_io_channel_unix_new(sock);
469 /* Connecting to the thumbnail server */
470 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
471 thumb_stderror("connect");
473 err = MS_MEDIA_ERR_PERMISSION_DENIED;
475 err = MS_MEDIA_ERR_SOCKET_CONN;
477 g_io_channel_shutdown(channel, TRUE, NULL);
478 g_io_channel_unref(channel);
484 req_manager = (thumbReq *)g_queue_peek_head(g_manage_queue);
486 if (req_manager == NULL) {
487 thumb_err("queue peek fail");
488 g_io_channel_shutdown(channel, TRUE, NULL);
489 g_io_channel_unref(channel);
491 return MS_MEDIA_ERR_INVALID_PARAMETER;
494 GSource *source = NULL;
495 source = g_io_create_watch(channel, G_IO_IN);
496 g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, req_manager->userData, NULL);
497 source_id = g_source_attach(source, g_main_context_get_thread_default());
500 memset((void *)&req_msg, 0, sizeof(thumbMsg));
504 req_msg.msg_type = req_manager->msg_type;
505 req_msg.request_id = 0;
506 req_msg.uid = req_manager->uid;
507 SAFE_STRLCPY(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
508 req_msg.dst_path[0] = '\0';
509 req_msg.thumb_size = 0;
511 unsigned char *buf = NULL;
513 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
515 if (send(sock, buf, buf_size, 0) < 0) {
516 thumb_err("send failed: %d", errno);
518 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
519 g_io_channel_shutdown(channel, TRUE, NULL);
520 g_io_channel_unref(channel);
522 return MS_MEDIA_ERR_SOCKET_SEND;
526 thumb_dbg("Sending msg to thumbnail daemon is successful");
528 if (req_manager->msg_type == THUMB_REQUEST_DB_INSERT) {
529 req_manager->channel = channel;
530 req_manager->source_id = source_id;
531 req_manager->isRequested = true;
537 static int _media_thumb_raw_data_send_request()
539 int err = MS_MEDIA_ERR_NONE;
541 struct sockaddr_un serv_addr;
542 thumbRawReq *req_manager = NULL;
545 err = ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock);
546 if (err != MS_MEDIA_ERR_NONE) {
547 thumb_err("ms_ipc_create_client_socket failed");
551 memset(&serv_addr, 0, sizeof(serv_addr));
552 serv_addr.sun_family = AF_UNIX;
553 SAFE_STRLCPY(serv_addr.sun_path, THUMB_IPC_PATH, sizeof(serv_addr.sun_path));
555 GIOChannel *channel = NULL;
556 channel = g_io_channel_unix_new(sock);
559 /* Connecting to the thumbnail server */
560 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
561 thumb_stderror("connect error");
563 err = MS_MEDIA_ERR_PERMISSION_DENIED;
565 err = MS_MEDIA_ERR_SOCKET_CONN;
567 g_io_channel_shutdown(channel, TRUE, NULL);
568 g_io_channel_unref(channel);
573 req_manager = (thumbRawReq *)g_queue_peek_head(g_manage_raw_queue);
575 if (req_manager == NULL) {
576 thumb_err("queue peek fail");
577 g_io_channel_shutdown(channel, TRUE, NULL);
578 g_io_channel_unref(channel);
580 return MS_MEDIA_ERR_INVALID_PARAMETER;
583 GSource *source = NULL;
584 source = g_io_create_watch(channel, G_IO_IN);
585 g_source_set_callback(source, (GSourceFunc)_media_thumb_raw_data_write_socket, req_manager->userData, NULL);
586 source_id = g_source_attach(source, g_main_context_get_thread_default());
589 memset((void *)&req_msg, 0, sizeof(thumbMsg));
593 req_msg.msg_type = req_manager->msg_type;
594 req_msg.request_id = req_manager->request_id;
595 req_msg.thumb_width = req_manager->width;
596 req_msg.thumb_height = req_manager->height;
597 req_msg.uid = req_manager->uid;
599 SAFE_STRLCPY(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
600 req_msg.dst_path[0] = '\0';
601 req_msg.thumb_size = 0;
603 unsigned char *buf = NULL;
605 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
607 if (send(sock, buf, buf_size, 0) < 0) {
608 thumb_err("send failed: %d", errno);
610 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
611 g_io_channel_shutdown(channel, TRUE, NULL);
612 g_io_channel_unref(channel);
613 return MS_MEDIA_ERR_SOCKET_SEND;
618 if (req_manager->msg_type == THUMB_REQUEST_RAW_DATA) {
619 req_manager->channel = channel;
620 req_manager->source_id = source_id;
621 req_manager->isRequested = true;
623 return MS_MEDIA_ERR_NONE;
626 int _media_thumb_request_async(int msg_type, unsigned int request_id, const char *origin_path, thumbUserData *userData, uid_t uid)
628 int err = MS_MEDIA_ERR_NONE;
631 if (msg_type == THUMB_REQUEST_CANCEL_MEDIA)
632 return __media_thumb_cancel(request_id);
634 if (g_manage_queue == NULL)
635 g_manage_queue = g_queue_new();
637 thumbReq *thumb_req = NULL;
638 THUMB_MALLOC(thumb_req, sizeof(thumbReq));
639 thumb_retvm_if(thumb_req == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Failed to create request element");
641 thumb_req->msg_type = msg_type;
642 thumb_req->path = g_strdup(origin_path);
643 thumb_req->userData = userData;
644 thumb_req->isCanceled = false;
645 thumb_req->isRequested = false;
646 thumb_req->request_id = request_id;
647 thumb_req->uid = uid;
649 len = g_queue_get_length(g_manage_queue);
650 g_queue_push_tail(g_manage_queue, (gpointer)thumb_req);
653 err = _media_thumb_send_request();
658 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)
660 int err = MS_MEDIA_ERR_NONE;
662 thumbRawReq *thumb_req = NULL;
664 if (msg_type == THUMB_REQUEST_CANCEL_RAW_DATA)
665 return __media_thumb_cancel_raw_data(request_id);
667 if (g_manage_raw_queue == NULL)
668 g_manage_raw_queue = g_queue_new();
670 THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
671 if (thumb_req == NULL) {
672 thumb_err("Failed to create request element");
673 return MS_MEDIA_ERR_INVALID_PARAMETER;
676 thumb_req->msg_type = msg_type;
677 thumb_req->request_id = request_id;
678 thumb_req->path = g_strdup(origin_path);
679 thumb_req->width = width;
680 thumb_req->height = height;
681 thumb_req->userData = userData;
682 thumb_req->isCanceled = false;
683 thumb_req->isRequested = false;
684 thumb_req->uid = uid;
686 len = g_queue_get_length(g_manage_raw_queue);
687 g_queue_push_tail(g_manage_raw_queue, (gpointer)thumb_req);
690 err = _media_thumb_raw_data_send_request();
695 int _media_thumb_request_cancel_all()
697 if (g_manage_raw_queue != NULL) {
698 while (g_queue_get_length(g_manage_raw_queue) > 0) {
699 thumbRawReq *req = (thumbRawReq *)g_queue_peek_tail(g_manage_raw_queue);
701 if (req->isRequested == true) {
702 req->isCanceled = true;
703 thumb_dbg("Last item!!");
707 g_queue_pop_tail(g_manage_raw_queue);
709 SAFE_FREE(req->path);
710 SAFE_FREE(req->userData);
714 thumb_dbg("Queue is empty!!");
720 return MS_MEDIA_ERR_NONE;