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_IPC_PATH tzplatform_mkpath(TZ_SYS_RUN, "media-server/media_ipc_thumbcreator.socket")
36 static GQueue *g_manage_queue = NULL;
37 static GQueue *g_manage_raw_queue = NULL;
43 unsigned int request_id;
49 thumbUserData *userData;
63 thumbRawUserData *userData;
66 static int _media_thumb_send_request();
67 static 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 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);
90 sock = g_io_channel_unix_get_fd(req->channel);
92 g_io_channel_shutdown(req->channel, TRUE, NULL);
93 g_io_channel_unref(req->channel);
95 if (source_id != NULL) {
96 g_source_destroy(source_id);
98 thumb_err("G_SOURCE_ID is NULL");
101 SAFE_FREE(req->path);
102 SAFE_FREE(req->userData);
106 /* Check manage queue */
107 len = g_queue_get_length(g_manage_queue);
109 _media_thumb_send_request();
113 int __media_thumb_cancel(unsigned int request_id)
118 if (g_manage_queue != NULL) {
119 len = g_queue_get_length(g_manage_queue);
121 for (i = 0; i < len; i++) {
122 thumbReq *req = NULL;
123 req = (thumbReq *)g_queue_peek_nth(g_manage_queue, i);
124 if (req == NULL) continue;
126 if (req->request_id == request_id) {
127 if (req->isRequested == true) {
128 req->isCanceled = true;
130 g_queue_pop_nth(g_manage_queue, i);
132 SAFE_FREE(req->path);
133 SAFE_FREE(req->userData);
145 return MS_MEDIA_ERR_INTERNAL;
147 return MS_MEDIA_ERR_NONE;
150 void __media_thumb_pop_raw_data()
155 if (g_manage_raw_queue != NULL) {
156 thumbRawReq *req = (thumbRawReq *)g_queue_pop_head(g_manage_raw_queue);
158 GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
159 sock = g_io_channel_unix_get_fd(req->channel);
161 g_io_channel_shutdown(req->channel, TRUE, NULL);
162 g_io_channel_unref(req->channel);
164 if (source_id != NULL) {
165 g_source_destroy(source_id);
167 thumb_err("G_SOURCE_ID is NULL");
170 SAFE_FREE(req->path);
171 SAFE_FREE(req->userData);
175 /* Check manage queue */
176 len = g_queue_get_length(g_manage_raw_queue);
178 _media_thumb_raw_data_send_request();
182 int __media_thumb_cancel_raw_data(int request_id)
187 if (g_manage_raw_queue != NULL) {
188 len = g_queue_get_length(g_manage_raw_queue);
190 for (i = 0; i < len; i++) {
191 thumbRawReq *req = NULL;
192 req = (thumbRawReq *)g_queue_peek_nth(g_manage_raw_queue, i);
193 if (req == NULL) continue;
195 if (req->request_id == request_id) {
196 if (req->isRequested == true) {
197 req->isCanceled = true;
199 g_queue_pop_nth(g_manage_raw_queue, i);
201 SAFE_FREE(req->path);
202 SAFE_FREE(req->userData);
214 return MS_MEDIA_ERR_INTERNAL;
216 return MS_MEDIA_ERR_NONE;
220 bool __media_thumb_check_cancel(void)
222 thumbReq *req = NULL;
223 req = (thumbReq *)g_queue_peek_head(g_manage_queue);
235 bool __media_thumb_check_cancel_for_raw(void)
237 thumbRawReq *req = NULL;
238 req = (thumbRawReq *)g_queue_peek_head(g_manage_raw_queue);
250 int _media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg)
255 unsigned char *buf = NULL;
257 THUMB_MALLOC(buf, header_size);
259 thumb_err("memory allocation failed");
261 return MS_MEDIA_ERR_OUT_OF_MEMORY;
264 while (header_size > 0) {
265 if ((recv_len = recv(sock, buf + recv_pos, header_size, 0)) < 0) {
266 thumb_stderror("recv failed");
268 return _media_thumb_get_error();
270 header_size -= recv_len;
271 recv_pos += recv_len;
274 header_size = recv_pos;
277 memcpy(msg, buf, header_size);
280 if (strlen(msg->org_path) == 0 || strlen(msg->org_path) >= MAX_FILEPATH_LEN) {
281 thumb_err("org_path size is invalid %zu", strlen(msg->org_path));
282 return MS_MEDIA_ERR_SOCKET_RECEIVE;
285 /* it can be empty string */
286 if (strlen(msg->dst_path) >= MAX_FILEPATH_LEN) {
287 thumb_err("dst_path size is invalid %zu", strlen(msg->dst_path));
288 return MS_MEDIA_ERR_SOCKET_RECEIVE;
291 if (msg->thumb_size < 0) {
292 thumb_err("recv data is wrong");
293 return MS_MEDIA_ERR_SOCKET_RECEIVE;
296 if (msg->thumb_size > 0) {
297 remain_size = msg->thumb_size;
298 THUMB_MALLOC(buf, remain_size);
300 return MS_MEDIA_ERR_OUT_OF_MEMORY;
303 while (remain_size > 0) {
304 if ((recv_len = recv(sock, buf + recv_pos, remain_size, 0)) < 0) {
305 thumb_stderror("recv failed");
307 return _media_thumb_get_error();
311 remain_size -= recv_len;
312 recv_pos += recv_len;
315 SAFE_FREE(msg->thumb_data);
316 THUMB_MALLOC(msg->thumb_data, msg->thumb_size);
317 if (msg->thumb_data != NULL) {
318 memcpy(msg->thumb_data, buf, msg->thumb_size);
322 return MS_MEDIA_ERR_OUT_OF_MEMORY;
328 return MS_MEDIA_ERR_NONE;
331 int _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
333 if (req_msg == NULL || buf == NULL)
334 return MS_MEDIA_ERR_INVALID_PARAMETER;
336 int thumb_data_len = 0;
340 header_size = sizeof(thumbMsg) - sizeof(unsigned char *);
341 thumb_data_len = req_msg->thumb_size;
342 if (thumb_data_len < 0)
343 return MS_MEDIA_ERR_INVALID_PARAMETER;
345 //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);
347 size = header_size + thumb_data_len;
348 THUMB_MALLOC(*buf, size);
353 memcpy(*buf, req_msg, header_size);
354 if (thumb_data_len > 0)
355 memcpy((*buf) + header_size, 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) - 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) - 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());
497 memset((void *)&req_msg, 0, sizeof(thumbMsg));
501 req_msg.msg_type = req_manager->msg_type;
502 req_msg.request_id = 0;
503 req_msg.uid = req_manager->uid;
504 SAFE_STRLCPY(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
505 req_msg.dst_path[0] = '\0';
506 req_msg.thumb_size = 0;
508 unsigned char *buf = NULL;
510 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
512 if (send(sock, buf, buf_size, 0) < 0) {
513 thumb_err("send failed: %d", errno);
515 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
516 g_io_channel_shutdown(channel, TRUE, NULL);
517 g_io_channel_unref(channel);
519 return MS_MEDIA_ERR_SOCKET_SEND;
523 thumb_dbg("Sending msg to thumbnail daemon is successful");
525 if (req_manager->msg_type == THUMB_REQUEST_DB_INSERT) {
526 req_manager->channel = channel;
527 req_manager->source_id = source_id;
528 req_manager->isRequested = true;
534 static int _media_thumb_raw_data_send_request()
536 int err = MS_MEDIA_ERR_NONE;
538 struct sockaddr_un serv_addr;
539 thumbRawReq *req_manager = NULL;
542 err = ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock);
543 if (err != MS_MEDIA_ERR_NONE) {
544 thumb_err("ms_ipc_create_client_socket failed");
548 memset(&serv_addr, 0, sizeof(serv_addr));
549 serv_addr.sun_family = AF_UNIX;
550 SAFE_STRLCPY(serv_addr.sun_path, THUMB_IPC_PATH, sizeof(serv_addr.sun_path));
552 GIOChannel *channel = NULL;
553 channel = g_io_channel_unix_new(sock);
556 /* Connecting to the thumbnail server */
557 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
558 thumb_stderror("connect error");
560 err = MS_MEDIA_ERR_PERMISSION_DENIED;
562 err = MS_MEDIA_ERR_SOCKET_CONN;
564 g_io_channel_shutdown(channel, TRUE, NULL);
565 g_io_channel_unref(channel);
570 req_manager = (thumbRawReq *)g_queue_peek_head(g_manage_raw_queue);
572 if (req_manager == NULL) {
573 thumb_err("queue peek fail");
574 g_io_channel_shutdown(channel, TRUE, NULL);
575 g_io_channel_unref(channel);
577 return MS_MEDIA_ERR_INVALID_PARAMETER;
580 GSource *source = NULL;
581 source = g_io_create_watch(channel, G_IO_IN);
582 g_source_set_callback(source, (GSourceFunc)_media_thumb_raw_data_write_socket, req_manager->userData, NULL);
583 source_id = g_source_attach(source, g_main_context_get_thread_default());
586 memset((void *)&req_msg, 0, sizeof(thumbMsg));
590 req_msg.msg_type = req_manager->msg_type;
591 req_msg.request_id = req_manager->request_id;
592 req_msg.thumb_width = req_manager->width;
593 req_msg.thumb_height = req_manager->height;
594 req_msg.uid = req_manager->uid;
596 SAFE_STRLCPY(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
597 req_msg.dst_path[0] = '\0';
598 req_msg.thumb_size = 0;
600 unsigned char *buf = NULL;
602 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
604 if (send(sock, buf, buf_size, 0) < 0) {
605 thumb_err("send failed: %d", errno);
607 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
608 g_io_channel_shutdown(channel, TRUE, NULL);
609 g_io_channel_unref(channel);
610 return MS_MEDIA_ERR_SOCKET_SEND;
615 if (req_manager->msg_type == THUMB_REQUEST_RAW_DATA) {
616 req_manager->channel = channel;
617 req_manager->source_id = source_id;
618 req_manager->isRequested = true;
620 return MS_MEDIA_ERR_NONE;
623 int _media_thumb_request_async(int msg_type, unsigned int request_id, const char *origin_path, thumbUserData *userData, uid_t uid)
625 int err = MS_MEDIA_ERR_NONE;
628 if (msg_type == THUMB_REQUEST_CANCEL_MEDIA)
629 return __media_thumb_cancel(request_id);
631 if (g_manage_queue == NULL)
632 g_manage_queue = g_queue_new();
634 thumbReq *thumb_req = NULL;
635 THUMB_MALLOC(thumb_req, sizeof(thumbReq));
636 thumb_retvm_if(thumb_req == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Failed to create request element");
638 thumb_req->msg_type = msg_type;
639 thumb_req->path = g_strdup(origin_path);
640 thumb_req->userData = userData;
641 thumb_req->isCanceled = false;
642 thumb_req->isRequested = false;
643 thumb_req->request_id = request_id;
644 thumb_req->uid = uid;
646 len = g_queue_get_length(g_manage_queue);
647 g_queue_push_tail(g_manage_queue, (gpointer)thumb_req);
650 err = _media_thumb_send_request();
655 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)
657 int err = MS_MEDIA_ERR_NONE;
659 thumbRawReq *thumb_req = NULL;
661 if (msg_type == THUMB_REQUEST_CANCEL_RAW_DATA)
662 return __media_thumb_cancel_raw_data(request_id);
664 if (g_manage_raw_queue == NULL)
665 g_manage_raw_queue = g_queue_new();
667 THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
668 if (thumb_req == NULL) {
669 thumb_err("Failed to create request element");
670 return MS_MEDIA_ERR_INVALID_PARAMETER;
673 thumb_req->msg_type = msg_type;
674 thumb_req->request_id = request_id;
675 thumb_req->path = g_strdup(origin_path);
676 thumb_req->width = width;
677 thumb_req->height = height;
678 thumb_req->userData = userData;
679 thumb_req->isCanceled = false;
680 thumb_req->isRequested = false;
681 thumb_req->uid = uid;
683 len = g_queue_get_length(g_manage_raw_queue);
684 g_queue_push_tail(g_manage_raw_queue, (gpointer)thumb_req);
687 err = _media_thumb_raw_data_send_request();