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()
85 if (g_manage_queue != NULL) {
86 thumbReq *req = (thumbReq *)g_queue_pop_head(g_manage_queue);
88 GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
90 g_io_channel_shutdown(req->channel, TRUE, NULL);
91 g_io_channel_unref(req->channel);
93 if (source_id != NULL) {
94 g_source_destroy(source_id);
96 thumb_err("G_SOURCE_ID is NULL");
100 SAFE_FREE(req->userData);
104 /* Check manage queue */
105 len = g_queue_get_length(g_manage_queue);
107 _media_thumb_send_request();
111 int __media_thumb_cancel(unsigned int request_id)
116 if (g_manage_queue != NULL) {
117 len = g_queue_get_length(g_manage_queue);
119 for (i = 0; i < len; i++) {
120 thumbReq *req = NULL;
121 req = (thumbReq *)g_queue_peek_nth(g_manage_queue, i);
122 if (req == NULL) continue;
124 if (req->request_id == request_id) {
125 if (req->isRequested == true) {
126 req->isCanceled = true;
128 g_queue_pop_nth(g_manage_queue, i);
130 SAFE_FREE(req->path);
131 SAFE_FREE(req->userData);
143 return MS_MEDIA_ERR_INTERNAL;
145 return MS_MEDIA_ERR_NONE;
148 void __media_thumb_pop_raw_data()
152 if (g_manage_raw_queue != NULL) {
153 thumbRawReq *req = (thumbRawReq *)g_queue_pop_head(g_manage_raw_queue);
155 GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
157 g_io_channel_shutdown(req->channel, TRUE, NULL);
158 g_io_channel_unref(req->channel);
160 if (source_id != NULL) {
161 g_source_destroy(source_id);
163 thumb_err("G_SOURCE_ID is NULL");
166 SAFE_FREE(req->path);
167 SAFE_FREE(req->userData);
171 /* Check manage queue */
172 len = g_queue_get_length(g_manage_raw_queue);
174 _media_thumb_raw_data_send_request();
178 int __media_thumb_cancel_raw_data(int request_id)
183 if (g_manage_raw_queue != NULL) {
184 len = g_queue_get_length(g_manage_raw_queue);
186 for (i = 0; i < len; i++) {
187 thumbRawReq *req = NULL;
188 req = (thumbRawReq *)g_queue_peek_nth(g_manage_raw_queue, i);
189 if (req == NULL) continue;
191 if (req->request_id == request_id) {
192 if (req->isRequested == true) {
193 req->isCanceled = true;
195 g_queue_pop_nth(g_manage_raw_queue, i);
197 SAFE_FREE(req->path);
198 SAFE_FREE(req->userData);
210 return MS_MEDIA_ERR_INTERNAL;
212 return MS_MEDIA_ERR_NONE;
216 bool __media_thumb_check_cancel(void)
218 thumbReq *req = NULL;
219 req = (thumbReq *)g_queue_peek_head(g_manage_queue);
231 bool __media_thumb_check_cancel_for_raw(void)
233 thumbRawReq *req = NULL;
234 req = (thumbRawReq *)g_queue_peek_head(g_manage_raw_queue);
246 int _media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg)
251 unsigned char *buf = NULL;
253 THUMB_MALLOC(buf, header_size);
255 thumb_err("memory allocation failed");
257 return MS_MEDIA_ERR_OUT_OF_MEMORY;
260 while (header_size > 0) {
261 if ((recv_len = recv(sock, buf + recv_pos, header_size, 0)) < 0) {
262 thumb_stderror("recv failed");
264 return _media_thumb_get_error();
266 header_size -= recv_len;
267 recv_pos += recv_len;
270 header_size = recv_pos;
273 memcpy(msg, buf, header_size);
276 if (strlen(msg->org_path) == 0 || strlen(msg->org_path) >= MAX_FILEPATH_LEN) {
277 thumb_err("org_path size is invalid %d", strlen(msg->org_path));
278 return MS_MEDIA_ERR_SOCKET_RECEIVE;
281 /* it can be empty string */
282 if (strlen(msg->dst_path) >= MAX_FILEPATH_LEN) {
283 thumb_err("dst_path size is invalid %d", strlen(msg->dst_path));
284 return MS_MEDIA_ERR_SOCKET_RECEIVE;
287 if (msg->thumb_size < 0) {
288 thumb_err("recv data is wrong");
289 return MS_MEDIA_ERR_SOCKET_RECEIVE;
292 if (msg->thumb_size > 0) {
293 remain_size = msg->thumb_size;
294 THUMB_MALLOC(buf, remain_size);
296 return MS_MEDIA_ERR_OUT_OF_MEMORY;
299 while (remain_size > 0) {
300 if ((recv_len = recv(sock, buf + recv_pos, remain_size, 0)) < 0) {
301 thumb_stderror("recv failed");
303 return _media_thumb_get_error();
307 remain_size -= recv_len;
308 recv_pos += recv_len;
311 SAFE_FREE(msg->thumb_data);
312 THUMB_MALLOC(msg->thumb_data, msg->thumb_size);
313 if (msg->thumb_data != NULL) {
314 memcpy(msg->thumb_data, buf, msg->thumb_size);
318 return MS_MEDIA_ERR_OUT_OF_MEMORY;
324 return MS_MEDIA_ERR_NONE;
327 int _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
329 if (req_msg == NULL || buf == NULL)
330 return MS_MEDIA_ERR_INVALID_PARAMETER;
332 int thumb_data_len = 0;
336 header_size = sizeof(thumbMsg) - sizeof(unsigned char *);
337 thumb_data_len = req_msg->thumb_size;
338 if (thumb_data_len < 0)
339 return MS_MEDIA_ERR_INVALID_PARAMETER;
341 //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);
343 size = header_size + thumb_data_len;
344 THUMB_MALLOC(*buf, size);
349 memcpy(*buf, req_msg, header_size);
350 if (thumb_data_len > 0)
351 memcpy((*buf) + header_size, req_msg->thumb_data, thumb_data_len);
355 return MS_MEDIA_ERR_NONE;
358 gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
363 int err = MS_MEDIA_ERR_NONE;
365 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
366 sock = g_io_channel_unix_get_fd(src);
368 header_size = sizeof(thumbMsg) - sizeof(unsigned char *);
370 thumb_err("_media_thumb_write_socket socket : %d", sock);
372 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
373 thumb_err("_media_thumb_recv_msg failed ");
379 if (recv_msg.status != MS_MEDIA_ERR_NONE) {
380 err = recv_msg.status;
381 thumb_err("Failed to make thumbnail (%d)", err);
384 if (__media_thumb_check_cancel()) {
386 thumbUserData* cb = (thumbUserData*)data;
387 if (cb->func != NULL)
388 cb->func(err, recv_msg.dst_path, cb->user_data);
399 gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
404 int err = MS_MEDIA_ERR_NONE;
406 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
407 sock = g_io_channel_unix_get_fd(src);
409 header_size = sizeof(thumbMsg) - sizeof(unsigned char *);
411 thumb_err("_media_thumb_write_socket socket : %d", sock);
413 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
414 thumb_err("_media_thumb_recv_msg failed ");
415 __media_thumb_pop_raw_data();
420 if (recv_msg.status != MS_MEDIA_ERR_NONE) {
421 err = recv_msg.status;
422 thumb_err("Failed to make thumbnail (%d)", err);
425 if (__media_thumb_check_cancel_for_raw()) {
427 thumbRawUserData* cb = (thumbRawUserData*)data;
428 if (cb->func != NULL)
429 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);
433 __media_thumb_pop_raw_data();
440 static int _media_thumb_send_request()
442 int err = MS_MEDIA_ERR_NONE;
444 struct sockaddr_un serv_addr;
445 thumbReq *req_manager = NULL;
448 err = ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock);
449 if (err != MS_MEDIA_ERR_NONE) {
450 thumb_err("ms_ipc_create_client_socket failed");
454 memset(&serv_addr, 0, sizeof(serv_addr));
455 serv_addr.sun_family = AF_UNIX;
456 SAFE_STRLCPY(serv_addr.sun_path, THUMB_IPC_PATH, sizeof(serv_addr.sun_path));
458 GIOChannel *channel = NULL;
459 channel = g_io_channel_unix_new(sock);
462 /* Connecting to the thumbnail server */
463 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
464 thumb_stderror("connect");
466 err = MS_MEDIA_ERR_PERMISSION_DENIED;
468 err = MS_MEDIA_ERR_SOCKET_CONN;
470 g_io_channel_shutdown(channel, TRUE, NULL);
471 g_io_channel_unref(channel);
477 req_manager = (thumbReq *)g_queue_peek_head(g_manage_queue);
479 if (req_manager == NULL) {
480 thumb_err("queue peek fail");
481 g_io_channel_shutdown(channel, TRUE, NULL);
482 g_io_channel_unref(channel);
484 return MS_MEDIA_ERR_INVALID_PARAMETER;
487 GSource *source = NULL;
488 source = g_io_create_watch(channel, G_IO_IN);
489 g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, req_manager->userData, NULL);
490 source_id = g_source_attach(source, g_main_context_get_thread_default());
493 memset((void *)&req_msg, 0, sizeof(thumbMsg));
497 req_msg.msg_type = req_manager->msg_type;
498 req_msg.request_id = 0;
499 req_msg.uid = req_manager->uid;
500 SAFE_STRLCPY(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
501 req_msg.dst_path[0] = '\0';
502 req_msg.thumb_size = 0;
504 unsigned char *buf = NULL;
506 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
508 if (send(sock, buf, buf_size, 0) < 0) {
509 thumb_err("send failed: %d", errno);
511 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
512 g_io_channel_shutdown(channel, TRUE, NULL);
513 g_io_channel_unref(channel);
515 return MS_MEDIA_ERR_SOCKET_SEND;
519 thumb_dbg("Sending msg to thumbnail daemon is successful");
521 if (req_manager->msg_type == THUMB_REQUEST_DB_INSERT) {
522 req_manager->channel = channel;
523 req_manager->source_id = source_id;
524 req_manager->isRequested = true;
530 static int _media_thumb_raw_data_send_request()
532 int err = MS_MEDIA_ERR_NONE;
534 struct sockaddr_un serv_addr;
535 thumbRawReq *req_manager = NULL;
538 err = ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock);
539 if (err != MS_MEDIA_ERR_NONE) {
540 thumb_err("ms_ipc_create_client_socket failed");
544 memset(&serv_addr, 0, sizeof(serv_addr));
545 serv_addr.sun_family = AF_UNIX;
546 SAFE_STRLCPY(serv_addr.sun_path, THUMB_IPC_PATH, sizeof(serv_addr.sun_path));
548 GIOChannel *channel = NULL;
549 channel = g_io_channel_unix_new(sock);
552 /* Connecting to the thumbnail server */
553 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
554 thumb_stderror("connect error");
556 err = MS_MEDIA_ERR_PERMISSION_DENIED;
558 err = MS_MEDIA_ERR_SOCKET_CONN;
560 g_io_channel_shutdown(channel, TRUE, NULL);
561 g_io_channel_unref(channel);
566 req_manager = (thumbRawReq *)g_queue_peek_head(g_manage_raw_queue);
568 if (req_manager == NULL) {
569 thumb_err("queue peek fail");
570 g_io_channel_shutdown(channel, TRUE, NULL);
571 g_io_channel_unref(channel);
573 return MS_MEDIA_ERR_INVALID_PARAMETER;
576 GSource *source = NULL;
577 source = g_io_create_watch(channel, G_IO_IN);
578 g_source_set_callback(source, (GSourceFunc)_media_thumb_raw_data_write_socket, req_manager->userData, NULL);
579 source_id = g_source_attach(source, g_main_context_get_thread_default());
582 memset((void *)&req_msg, 0, sizeof(thumbMsg));
586 req_msg.msg_type = req_manager->msg_type;
587 req_msg.request_id = req_manager->request_id;
588 req_msg.thumb_width = req_manager->width;
589 req_msg.thumb_height = req_manager->height;
590 req_msg.uid = req_manager->uid;
592 SAFE_STRLCPY(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
593 req_msg.dst_path[0] = '\0';
594 req_msg.thumb_size = 0;
596 unsigned char *buf = NULL;
598 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
600 if (send(sock, buf, buf_size, 0) < 0) {
601 thumb_err("send failed: %d", errno);
603 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
604 g_io_channel_shutdown(channel, TRUE, NULL);
605 g_io_channel_unref(channel);
606 return MS_MEDIA_ERR_SOCKET_SEND;
611 if (req_manager->msg_type == THUMB_REQUEST_RAW_DATA) {
612 req_manager->channel = channel;
613 req_manager->source_id = source_id;
614 req_manager->isRequested = true;
616 return MS_MEDIA_ERR_NONE;
619 int _media_thumb_request_async(int msg_type, unsigned int request_id, const char *origin_path, thumbUserData *userData, uid_t uid)
621 int err = MS_MEDIA_ERR_NONE;
624 if (msg_type == THUMB_REQUEST_CANCEL_MEDIA)
625 return __media_thumb_cancel(request_id);
627 if (g_manage_queue == NULL)
628 g_manage_queue = g_queue_new();
630 thumbReq *thumb_req = NULL;
631 THUMB_MALLOC(thumb_req, sizeof(thumbReq));
632 thumb_retvm_if(thumb_req == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Failed to create request element");
634 thumb_req->msg_type = msg_type;
635 thumb_req->path = g_strdup(origin_path);
636 thumb_req->userData = userData;
637 thumb_req->isCanceled = false;
638 thumb_req->isRequested = false;
639 thumb_req->request_id = request_id;
640 thumb_req->uid = uid;
642 len = g_queue_get_length(g_manage_queue);
643 g_queue_push_tail(g_manage_queue, (gpointer)thumb_req);
646 err = _media_thumb_send_request();
651 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)
653 int err = MS_MEDIA_ERR_NONE;
655 thumbRawReq *thumb_req = NULL;
657 if (msg_type == THUMB_REQUEST_CANCEL_RAW_DATA)
658 return __media_thumb_cancel_raw_data(request_id);
660 if (g_manage_raw_queue == NULL)
661 g_manage_raw_queue = g_queue_new();
663 THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
664 if (thumb_req == NULL) {
665 thumb_err("Failed to create request element");
666 return MS_MEDIA_ERR_INVALID_PARAMETER;
669 thumb_req->msg_type = msg_type;
670 thumb_req->request_id = request_id;
671 thumb_req->path = g_strdup(origin_path);
672 thumb_req->width = width;
673 thumb_req->height = height;
674 thumb_req->userData = userData;
675 thumb_req->isCanceled = false;
676 thumb_req->isRequested = false;
677 thumb_req->uid = uid;
679 len = g_queue_get_length(g_manage_raw_queue);
680 g_queue_push_tail(g_manage_raw_queue, (gpointer)thumb_req);
683 err = _media_thumb_raw_data_send_request();
688 int _media_thumb_request_cancel_all()
690 if (g_manage_raw_queue != NULL) {
691 while (g_queue_get_length(g_manage_raw_queue) > 0) {
692 thumbRawReq *req = (thumbRawReq *)g_queue_peek_tail(g_manage_raw_queue);
694 if (req->isRequested == true) {
695 req->isCanceled = true;
696 thumb_dbg("Last item!!");
700 g_queue_pop_tail(g_manage_raw_queue);
702 SAFE_FREE(req->path);
703 SAFE_FREE(req->userData);
707 thumb_dbg("Queue is empty!!");
713 return MS_MEDIA_ERR_NONE;