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)
249 unsigned char *buf = NULL;
251 THUMB_MALLOC(buf, header_size);
253 thumb_err("memory allocation failed");
255 return MS_MEDIA_ERR_OUT_OF_MEMORY;
258 if (recv(sock, buf, header_size, 0) < 0) {
259 thumb_stderror("recv failed");
261 return _media_thumb_get_error();
264 memcpy(msg, buf, header_size);
266 if (strlen(msg->org_path) == 0 || strlen(msg->org_path) >= MAX_FILEPATH_LEN) {
267 thumb_err("org_path size is invalid %d", strlen(msg->org_path));
269 return MS_MEDIA_ERR_SOCKET_RECEIVE;
272 /* it can be empty string */
273 if (strlen(msg->dst_path) >= MAX_FILEPATH_LEN) {
274 thumb_err("dst_path size is invalid %d", strlen(msg->dst_path));
276 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);
289 return MS_MEDIA_ERR_OUT_OF_MEMORY;
291 if (recv(sock, buf, remain_size, 0) < 0) {
292 thumb_stderror("recv failed");
294 return _media_thumb_get_error();
297 SAFE_FREE(msg->thumb_data);
298 THUMB_MALLOC(msg->thumb_data, msg->thumb_size);
299 if (msg->thumb_data != NULL) {
300 memcpy(msg->thumb_data, buf, msg->thumb_size);
304 return MS_MEDIA_ERR_OUT_OF_MEMORY;
310 return MS_MEDIA_ERR_NONE;
313 int _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
315 if (req_msg == NULL || buf == NULL)
316 return MS_MEDIA_ERR_INVALID_PARAMETER;
318 int thumb_data_len = 0;
322 header_size = sizeof(thumbMsg) - sizeof(unsigned char *);
323 thumb_data_len = req_msg->thumb_size;
324 if (thumb_data_len < 0)
325 return MS_MEDIA_ERR_INVALID_PARAMETER;
327 //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);
329 size = header_size + thumb_data_len;
330 THUMB_MALLOC(*buf, size);
335 memcpy(*buf, req_msg, header_size);
336 if (thumb_data_len > 0)
337 memcpy((*buf) + header_size, req_msg->thumb_data, thumb_data_len);
341 return MS_MEDIA_ERR_NONE;
344 gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
349 int err = MS_MEDIA_ERR_NONE;
351 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
352 sock = g_io_channel_unix_get_fd(src);
354 header_size = sizeof(thumbMsg) - sizeof(unsigned char *);
356 thumb_err("_media_thumb_write_socket socket : %d", sock);
358 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
359 thumb_err("_media_thumb_recv_msg failed ");
365 if (recv_msg.status != MS_MEDIA_ERR_NONE) {
366 err = recv_msg.status;
367 thumb_err("Failed to make thumbnail (%d)", err);
370 if (__media_thumb_check_cancel()) {
372 thumbUserData* cb = (thumbUserData*)data;
373 if (cb->func != NULL)
374 cb->func(err, recv_msg.dst_path, cb->user_data);
385 gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
390 int err = MS_MEDIA_ERR_NONE;
392 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
393 sock = g_io_channel_unix_get_fd(src);
395 header_size = sizeof(thumbMsg) - sizeof(unsigned char *);
397 thumb_err("_media_thumb_write_socket socket : %d", sock);
399 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
400 thumb_err("_media_thumb_recv_msg failed ");
401 __media_thumb_pop_raw_data();
406 if (recv_msg.status != MS_MEDIA_ERR_NONE) {
407 err = recv_msg.status;
408 thumb_err("Failed to make thumbnail (%d)", err);
411 if (__media_thumb_check_cancel_for_raw()) {
413 thumbRawUserData* cb = (thumbRawUserData*)data;
414 if (cb->func != NULL)
415 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);
419 __media_thumb_pop_raw_data();
426 static int _media_thumb_send_request()
428 int err = MS_MEDIA_ERR_NONE;
430 struct sockaddr_un serv_addr;
431 thumbReq *req_manager = NULL;
434 err = ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock);
435 if (err != MS_MEDIA_ERR_NONE) {
436 thumb_err("ms_ipc_create_client_socket failed");
440 memset(&serv_addr, 0, sizeof(serv_addr));
441 serv_addr.sun_family = AF_UNIX;
442 SAFE_STRLCPY(serv_addr.sun_path, THUMB_IPC_PATH, sizeof(serv_addr.sun_path));
444 GIOChannel *channel = NULL;
445 channel = g_io_channel_unix_new(sock);
448 /* Connecting to the thumbnail server */
449 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
450 thumb_stderror("connect");
452 err = MS_MEDIA_ERR_PERMISSION_DENIED;
454 err = MS_MEDIA_ERR_SOCKET_CONN;
456 g_io_channel_shutdown(channel, TRUE, NULL);
457 g_io_channel_unref(channel);
463 req_manager = (thumbReq *)g_queue_peek_head(g_manage_queue);
465 if (req_manager == NULL) {
466 thumb_err("queue peek fail");
467 g_io_channel_shutdown(channel, TRUE, NULL);
468 g_io_channel_unref(channel);
470 return MS_MEDIA_ERR_INVALID_PARAMETER;
473 GSource *source = NULL;
474 source = g_io_create_watch(channel, G_IO_IN);
475 g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, req_manager->userData, NULL);
476 source_id = g_source_attach(source, g_main_context_get_thread_default());
479 memset((void *)&req_msg, 0, sizeof(thumbMsg));
483 req_msg.msg_type = req_manager->msg_type;
484 req_msg.request_id = 0;
485 req_msg.uid = req_manager->uid;
486 SAFE_STRLCPY(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
487 req_msg.dst_path[0] = '\0';
488 req_msg.thumb_size = 0;
490 unsigned char *buf = NULL;
492 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
494 if (send(sock, buf, buf_size, 0) < 0) {
495 thumb_err("send failed: %d", errno);
497 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
498 g_io_channel_shutdown(channel, TRUE, NULL);
499 g_io_channel_unref(channel);
501 return MS_MEDIA_ERR_SOCKET_SEND;
505 thumb_dbg("Sending msg to thumbnail daemon is successful");
507 if (req_manager->msg_type == THUMB_REQUEST_DB_INSERT) {
508 req_manager->channel = channel;
509 req_manager->source_id = source_id;
510 req_manager->isRequested = true;
516 static int _media_thumb_raw_data_send_request()
518 int err = MS_MEDIA_ERR_NONE;
520 struct sockaddr_un serv_addr;
521 thumbRawReq *req_manager = NULL;
524 err = ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock);
525 if (err != MS_MEDIA_ERR_NONE) {
526 thumb_err("ms_ipc_create_client_socket failed");
530 memset(&serv_addr, 0, sizeof(serv_addr));
531 serv_addr.sun_family = AF_UNIX;
532 SAFE_STRLCPY(serv_addr.sun_path, THUMB_IPC_PATH, sizeof(serv_addr.sun_path));
534 GIOChannel *channel = NULL;
535 channel = g_io_channel_unix_new(sock);
538 /* Connecting to the thumbnail server */
539 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
540 thumb_stderror("connect error");
542 err = MS_MEDIA_ERR_PERMISSION_DENIED;
544 err = MS_MEDIA_ERR_SOCKET_CONN;
546 g_io_channel_shutdown(channel, TRUE, NULL);
547 g_io_channel_unref(channel);
552 req_manager = (thumbRawReq *)g_queue_peek_head(g_manage_raw_queue);
554 if (req_manager == NULL) {
555 thumb_err("queue peek fail");
556 g_io_channel_shutdown(channel, TRUE, NULL);
557 g_io_channel_unref(channel);
559 return MS_MEDIA_ERR_INVALID_PARAMETER;
562 GSource *source = NULL;
563 source = g_io_create_watch(channel, G_IO_IN);
564 g_source_set_callback(source, (GSourceFunc)_media_thumb_raw_data_write_socket, req_manager->userData, NULL);
565 source_id = g_source_attach(source, g_main_context_get_thread_default());
568 memset((void *)&req_msg, 0, sizeof(thumbMsg));
572 req_msg.msg_type = req_manager->msg_type;
573 req_msg.request_id = req_manager->request_id;
574 req_msg.thumb_width = req_manager->width;
575 req_msg.thumb_height = req_manager->height;
576 req_msg.uid = req_manager->uid;
578 SAFE_STRLCPY(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
579 req_msg.dst_path[0] = '\0';
580 req_msg.thumb_size = 0;
582 unsigned char *buf = NULL;
584 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
586 if (send(sock, buf, buf_size, 0) < 0) {
587 thumb_err("send failed: %d", errno);
589 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
590 g_io_channel_shutdown(channel, TRUE, NULL);
591 g_io_channel_unref(channel);
592 return MS_MEDIA_ERR_SOCKET_SEND;
597 if (req_manager->msg_type == THUMB_REQUEST_RAW_DATA) {
598 req_manager->channel = channel;
599 req_manager->source_id = source_id;
600 req_manager->isRequested = true;
602 return MS_MEDIA_ERR_NONE;
605 int _media_thumb_request_async(int msg_type, unsigned int request_id, const char *origin_path, thumbUserData *userData, uid_t uid)
607 int err = MS_MEDIA_ERR_NONE;
610 if (msg_type == THUMB_REQUEST_CANCEL_MEDIA)
611 return __media_thumb_cancel(request_id);
613 if (g_manage_queue == NULL)
614 g_manage_queue = g_queue_new();
616 thumbReq *thumb_req = NULL;
617 THUMB_MALLOC(thumb_req, sizeof(thumbReq));
618 thumb_retvm_if(thumb_req == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Failed to create request element");
620 thumb_req->msg_type = msg_type;
621 thumb_req->path = g_strdup(origin_path);
622 thumb_req->userData = userData;
623 thumb_req->isCanceled = false;
624 thumb_req->isRequested = false;
625 thumb_req->request_id = request_id;
626 thumb_req->uid = uid;
628 len = g_queue_get_length(g_manage_queue);
629 g_queue_push_tail(g_manage_queue, (gpointer)thumb_req);
632 err = _media_thumb_send_request();
637 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)
639 int err = MS_MEDIA_ERR_NONE;
641 thumbRawReq *thumb_req = NULL;
643 if (msg_type == THUMB_REQUEST_CANCEL_RAW_DATA)
644 return __media_thumb_cancel_raw_data(request_id);
646 if (g_manage_raw_queue == NULL)
647 g_manage_raw_queue = g_queue_new();
649 THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
650 if (thumb_req == NULL) {
651 thumb_err("Failed to create request element");
652 return MS_MEDIA_ERR_INVALID_PARAMETER;
655 thumb_req->msg_type = msg_type;
656 thumb_req->request_id = request_id;
657 thumb_req->path = g_strdup(origin_path);
658 thumb_req->width = width;
659 thumb_req->height = height;
660 thumb_req->userData = userData;
661 thumb_req->isCanceled = false;
662 thumb_req->isRequested = false;
663 thumb_req->uid = uid;
665 len = g_queue_get_length(g_manage_raw_queue);
666 g_queue_push_tail(g_manage_raw_queue, (gpointer)thumb_req);
669 err = _media_thumb_raw_data_send_request();
674 int _media_thumb_request_cancel_all()
676 if (g_manage_raw_queue != NULL) {
677 while (g_queue_get_length(g_manage_raw_queue) > 0) {
678 thumbRawReq *req = (thumbRawReq *)g_queue_peek_tail(g_manage_raw_queue);
680 if (req->isRequested == true) {
681 req->isCanceled = true;
682 thumb_dbg("Last item!!");
686 g_queue_pop_tail(g_manage_raw_queue);
688 SAFE_FREE(req->path);
689 SAFE_FREE(req->userData);
693 thumb_dbg("Queue is empty!!");
699 return MS_MEDIA_ERR_NONE;