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(void);
67 static int __media_thumb_raw_data_send_request(void);
70 static int __media_thumb_get_error(void)
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 static void __media_thumb_pop(void)
90 req = (thumbReq *)g_queue_pop_head(g_manage_queue);
92 GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
93 sock = g_io_channel_unix_get_fd(req->channel);
95 g_io_channel_shutdown(req->channel, TRUE, NULL);
96 g_io_channel_unref(req->channel);
99 g_source_destroy(source_id);
101 thumb_err("G_SOURCE_ID is NULL");
104 SAFE_FREE(req->path);
105 SAFE_FREE(req->userData);
109 /* Check manage queue */
110 len = g_queue_get_length(g_manage_queue);
112 __media_thumb_send_request();
115 static int __media_thumb_cancel(unsigned int request_id)
119 thumb_retv_if(!g_manage_queue, MS_MEDIA_ERR_NONE);
121 len = g_queue_get_length(g_manage_queue);
123 for (i = 0; i < len; i++) {
124 thumbReq *req = NULL;
125 req = (thumbReq *)g_queue_peek_nth(g_manage_queue, i);
126 if (req == NULL) continue;
128 if (req->request_id == request_id) {
129 if (req->isRequested) {
130 req->isCanceled = true;
132 g_queue_pop_nth(g_manage_queue, i);
134 SAFE_FREE(req->path);
135 SAFE_FREE(req->userData);
139 return MS_MEDIA_ERR_NONE;
143 return MS_MEDIA_ERR_INTERNAL;
146 static void __media_thumb_pop_raw_data(void)
150 thumbRawReq *req = NULL;
152 if (!g_manage_raw_queue)
155 req = (thumbRawReq *)g_queue_pop_head(g_manage_raw_queue);
157 GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
158 sock = g_io_channel_unix_get_fd(req->channel);
160 g_io_channel_shutdown(req->channel, TRUE, NULL);
161 g_io_channel_unref(req->channel);
164 g_source_destroy(source_id);
166 thumb_err("G_SOURCE_ID is NULL");
169 SAFE_FREE(req->path);
170 SAFE_FREE(req->userData);
174 /* Check manage queue */
175 len = g_queue_get_length(g_manage_raw_queue);
177 __media_thumb_raw_data_send_request();
180 static int __media_thumb_cancel_raw_data(int request_id)
184 thumb_retv_if(!g_manage_raw_queue, MS_MEDIA_ERR_NONE);
186 len = g_queue_get_length(g_manage_raw_queue);
188 for (i = 0; i < len; i++) {
189 thumbRawReq *req = NULL;
190 req = (thumbRawReq *)g_queue_peek_nth(g_manage_raw_queue, i);
191 if (req == NULL) continue;
193 if (req->request_id == request_id) {
194 if (req->isRequested) {
195 req->isCanceled = true;
197 g_queue_pop_nth(g_manage_raw_queue, i);
199 SAFE_FREE(req->path);
200 SAFE_FREE(req->userData);
204 return MS_MEDIA_ERR_NONE;
208 return MS_MEDIA_ERR_INTERNAL;
211 static bool __media_thumb_check_cancel(void)
213 thumbReq *req = NULL;
214 req = (thumbReq *)g_queue_peek_head(g_manage_queue);
225 static bool __media_thumb_check_cancel_for_raw(void)
227 thumbRawReq *req = NULL;
228 req = (thumbRawReq *)g_queue_peek_head(g_manage_raw_queue);
239 int _media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg)
244 unsigned char *buf = NULL;
246 THUMB_MALLOC(buf, header_size);
248 thumb_err("memory allocation failed");
250 return MS_MEDIA_ERR_OUT_OF_MEMORY;
253 while (header_size > 0) {
254 if ((recv_len = recv(sock, buf + recv_pos, header_size, 0)) < 0) {
255 thumb_stderror("recv failed");
257 return __media_thumb_get_error();
259 header_size -= recv_len;
260 recv_pos += recv_len;
263 header_size = recv_pos;
266 memcpy(msg, buf, header_size);
269 if (strlen(msg->org_path) == 0 || strlen(msg->org_path) >= MAX_FILEPATH_LEN) {
270 thumb_err("org_path size is invalid %zu", strlen(msg->org_path));
271 return MS_MEDIA_ERR_SOCKET_RECEIVE;
274 /* it can be empty string */
275 if (strlen(msg->dst_path) >= MAX_FILEPATH_LEN) {
276 thumb_err("dst_path size is invalid %zu", strlen(msg->dst_path));
277 return MS_MEDIA_ERR_SOCKET_RECEIVE;
280 if (msg->thumb_size < 0) {
281 thumb_err("recv data is wrong");
282 return MS_MEDIA_ERR_SOCKET_RECEIVE;
285 if (msg->thumb_size > 0) {
286 remain_size = msg->thumb_size;
287 THUMB_MALLOC(buf, remain_size);
288 thumb_retv_if(!buf, MS_MEDIA_ERR_OUT_OF_MEMORY);
290 while (remain_size > 0) {
291 if ((recv_len = recv(sock, buf + recv_pos, remain_size, 0)) < 0) {
292 thumb_stderror("recv failed");
294 return __media_thumb_get_error();
298 remain_size -= recv_len;
299 recv_pos += recv_len;
302 SAFE_FREE(msg->thumb_data);
303 THUMB_MALLOC(msg->thumb_data, msg->thumb_size);
304 if (msg->thumb_data != NULL) {
305 memcpy(msg->thumb_data, buf, msg->thumb_size);
309 return MS_MEDIA_ERR_OUT_OF_MEMORY;
315 return MS_MEDIA_ERR_NONE;
318 int _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
320 if (req_msg == NULL || buf == NULL)
321 return MS_MEDIA_ERR_INVALID_PARAMETER;
323 int thumb_data_len = 0;
327 header_size = sizeof(thumbMsg) - sizeof(unsigned char *);
328 thumb_data_len = req_msg->thumb_size;
329 if (thumb_data_len < 0)
330 return MS_MEDIA_ERR_INVALID_PARAMETER;
332 //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);
334 size = header_size + thumb_data_len;
335 THUMB_MALLOC(*buf, size);
340 memcpy(*buf, req_msg, header_size);
341 if (thumb_data_len > 0)
342 memcpy((*buf) + header_size, req_msg->thumb_data, thumb_data_len);
346 return MS_MEDIA_ERR_NONE;
349 gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
354 int err = MS_MEDIA_ERR_NONE;
356 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
357 sock = g_io_channel_unix_get_fd(src);
359 header_size = sizeof(thumbMsg) - sizeof(unsigned char *);
361 thumb_err("_media_thumb_write_socket socket : %d", sock);
363 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
364 thumb_err("_media_thumb_recv_msg failed ");
367 return G_SOURCE_REMOVE;
370 if (recv_msg.status != MS_MEDIA_ERR_NONE) {
371 err = recv_msg.status;
372 thumb_err("Failed to make thumbnail (%d)", err);
375 if (__media_thumb_check_cancel()) {
377 thumbUserData* cb = (thumbUserData*)data;
378 if (cb->func != NULL)
379 cb->func(err, recv_msg.dst_path, cb->user_data);
387 return G_SOURCE_REMOVE;
390 gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
395 int err = MS_MEDIA_ERR_NONE;
397 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
398 sock = g_io_channel_unix_get_fd(src);
400 header_size = sizeof(thumbMsg) - sizeof(unsigned char *);
402 thumb_err("_media_thumb_write_socket socket : %d", sock);
404 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
405 thumb_err("_media_thumb_recv_msg failed ");
406 __media_thumb_pop_raw_data();
408 return G_SOURCE_REMOVE;
411 if (recv_msg.status != MS_MEDIA_ERR_NONE) {
412 err = recv_msg.status;
413 thumb_err("Failed to make thumbnail (%d)", err);
416 if (__media_thumb_check_cancel_for_raw()) {
418 thumbRawUserData* cb = (thumbRawUserData*)data;
419 if (cb->func != NULL)
420 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);
424 __media_thumb_pop_raw_data();
428 return G_SOURCE_REMOVE;
431 static int __media_thumb_send_request(void)
433 int err = MS_MEDIA_ERR_NONE;
435 struct sockaddr_un serv_addr;
436 thumbReq *req_manager = NULL;
439 err = ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock);
440 thumb_retvm_if(err != MS_MEDIA_ERR_NONE, err, "ms_ipc_create_client_socket failed");
442 memset(&serv_addr, 0, sizeof(serv_addr));
443 serv_addr.sun_family = AF_UNIX;
444 SAFE_STRLCPY(serv_addr.sun_path, THUMB_IPC_PATH, sizeof(serv_addr.sun_path));
446 GIOChannel *channel = NULL;
447 channel = g_io_channel_unix_new(sock);
450 /* Connecting to the thumbnail server */
451 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
452 thumb_stderror("connect");
454 err = MS_MEDIA_ERR_PERMISSION_DENIED;
456 err = MS_MEDIA_ERR_SOCKET_CONN;
458 g_io_channel_shutdown(channel, TRUE, NULL);
459 g_io_channel_unref(channel);
465 req_manager = (thumbReq *)g_queue_peek_head(g_manage_queue);
467 if (req_manager == NULL) {
468 thumb_err("queue peek fail");
469 g_io_channel_shutdown(channel, TRUE, NULL);
470 g_io_channel_unref(channel);
472 return MS_MEDIA_ERR_INVALID_PARAMETER;
475 GSource *source = NULL;
476 source = g_io_create_watch(channel, G_IO_IN);
477 g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, req_manager->userData, NULL);
478 source_id = g_source_attach(source, g_main_context_get_thread_default());
481 memset((void *)&req_msg, 0, sizeof(thumbMsg));
485 req_msg.msg_type = req_manager->msg_type;
486 req_msg.request_id = 0;
487 req_msg.uid = req_manager->uid;
488 SAFE_STRLCPY(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
489 req_msg.dst_path[0] = '\0';
490 req_msg.thumb_size = 0;
492 unsigned char *buf = NULL;
494 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
496 if (send(sock, buf, buf_size, 0) < 0) {
497 thumb_err("send failed: %d", errno);
499 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
500 g_io_channel_shutdown(channel, TRUE, NULL);
501 g_io_channel_unref(channel);
503 return MS_MEDIA_ERR_SOCKET_SEND;
507 thumb_dbg("Sending msg to thumbnail daemon is successful");
509 if (req_manager->msg_type == THUMB_REQUEST_DB_INSERT) {
510 req_manager->channel = channel;
511 req_manager->source_id = source_id;
512 req_manager->isRequested = true;
518 static int __media_thumb_raw_data_send_request(void)
520 int err = MS_MEDIA_ERR_NONE;
522 struct sockaddr_un serv_addr;
523 thumbRawReq *req_manager = NULL;
526 err = ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock);
527 thumb_retvm_if(err != MS_MEDIA_ERR_NONE, err, "ms_ipc_create_client_socket failed");
529 memset(&serv_addr, 0, sizeof(serv_addr));
530 serv_addr.sun_family = AF_UNIX;
531 SAFE_STRLCPY(serv_addr.sun_path, THUMB_IPC_PATH, sizeof(serv_addr.sun_path));
533 GIOChannel *channel = NULL;
534 channel = g_io_channel_unix_new(sock);
537 /* Connecting to the thumbnail server */
538 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
539 thumb_stderror("connect error");
541 err = MS_MEDIA_ERR_PERMISSION_DENIED;
543 err = MS_MEDIA_ERR_SOCKET_CONN;
545 g_io_channel_shutdown(channel, TRUE, NULL);
546 g_io_channel_unref(channel);
551 req_manager = (thumbRawReq *)g_queue_peek_head(g_manage_raw_queue);
553 if (req_manager == NULL) {
554 thumb_err("queue peek fail");
555 g_io_channel_shutdown(channel, TRUE, NULL);
556 g_io_channel_unref(channel);
558 return MS_MEDIA_ERR_INVALID_PARAMETER;
561 GSource *source = NULL;
562 source = g_io_create_watch(channel, G_IO_IN);
563 g_source_set_callback(source, (GSourceFunc)_media_thumb_raw_data_write_socket, req_manager->userData, NULL);
564 source_id = g_source_attach(source, g_main_context_get_thread_default());
567 memset((void *)&req_msg, 0, sizeof(thumbMsg));
571 req_msg.msg_type = req_manager->msg_type;
572 req_msg.request_id = req_manager->request_id;
573 req_msg.thumb_width = req_manager->width;
574 req_msg.thumb_height = req_manager->height;
575 req_msg.uid = req_manager->uid;
577 SAFE_STRLCPY(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
578 req_msg.dst_path[0] = '\0';
579 req_msg.thumb_size = 0;
581 unsigned char *buf = NULL;
583 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
585 if (send(sock, buf, buf_size, 0) < 0) {
586 thumb_err("send failed: %d", errno);
588 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
589 g_io_channel_shutdown(channel, TRUE, NULL);
590 g_io_channel_unref(channel);
591 return MS_MEDIA_ERR_SOCKET_SEND;
596 if (req_manager->msg_type == THUMB_REQUEST_RAW_DATA) {
597 req_manager->channel = channel;
598 req_manager->source_id = source_id;
599 req_manager->isRequested = true;
601 return MS_MEDIA_ERR_NONE;
604 int _media_thumb_request_async(int msg_type, unsigned int request_id, const char *origin_path, thumbUserData *userData, uid_t uid)
606 int err = MS_MEDIA_ERR_NONE;
609 if (msg_type == THUMB_REQUEST_CANCEL_MEDIA)
610 return __media_thumb_cancel(request_id);
612 if (g_manage_queue == NULL)
613 g_manage_queue = g_queue_new();
615 thumbReq *thumb_req = NULL;
616 THUMB_MALLOC(thumb_req, sizeof(thumbReq));
617 thumb_retvm_if(thumb_req == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Failed to create request element");
619 thumb_req->msg_type = msg_type;
620 thumb_req->path = g_strdup(origin_path);
621 thumb_req->userData = userData;
622 thumb_req->isCanceled = false;
623 thumb_req->isRequested = false;
624 thumb_req->request_id = request_id;
625 thumb_req->uid = uid;
627 len = g_queue_get_length(g_manage_queue);
628 g_queue_push_tail(g_manage_queue, (gpointer)thumb_req);
631 err = __media_thumb_send_request();
636 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)
638 int err = MS_MEDIA_ERR_NONE;
640 thumbRawReq *thumb_req = NULL;
642 if (msg_type == THUMB_REQUEST_CANCEL_RAW_DATA)
643 return __media_thumb_cancel_raw_data(request_id);
645 if (g_manage_raw_queue == NULL)
646 g_manage_raw_queue = g_queue_new();
648 THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
649 if (thumb_req == NULL) {
650 thumb_err("Failed to create request element");
651 return MS_MEDIA_ERR_INVALID_PARAMETER;
654 thumb_req->msg_type = msg_type;
655 thumb_req->request_id = request_id;
656 thumb_req->path = g_strdup(origin_path);
657 thumb_req->width = width;
658 thumb_req->height = height;
659 thumb_req->userData = userData;
660 thumb_req->isCanceled = false;
661 thumb_req->isRequested = false;
662 thumb_req->uid = uid;
664 len = g_queue_get_length(g_manage_raw_queue);
665 g_queue_push_tail(g_manage_raw_queue, (gpointer)thumb_req);
668 err = __media_thumb_raw_data_send_request();