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;
281 if (msg->thumb_size < 0) {
282 thumb_err("recv data is wrong");
283 return MS_MEDIA_ERR_SOCKET_RECEIVE;
286 if (msg->thumb_size > 0) {
287 remain_size = msg->thumb_size;
288 THUMB_MALLOC(buf, remain_size);
290 return MS_MEDIA_ERR_OUT_OF_MEMORY;
292 if (recv(sock, buf, remain_size, 0) < 0) {
293 thumb_stderror("recv failed");
295 return _media_thumb_get_error();
298 SAFE_FREE(msg->thumb_data);
299 THUMB_MALLOC(msg->thumb_data, msg->thumb_size);
300 if (msg->thumb_data != NULL) {
301 memcpy(msg->thumb_data, buf, msg->thumb_size);
305 return MS_MEDIA_ERR_OUT_OF_MEMORY;
311 return MS_MEDIA_ERR_NONE;
314 int _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
316 if (req_msg == NULL || buf == NULL)
317 return MS_MEDIA_ERR_INVALID_PARAMETER;
319 int thumb_data_len = 0;
323 header_size = sizeof(thumbMsg) - sizeof(unsigned char *);
324 thumb_data_len = req_msg->thumb_size;
325 if (thumb_data_len < 0)
326 return MS_MEDIA_ERR_INVALID_PARAMETER;
328 //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);
330 size = header_size + thumb_data_len;
331 THUMB_MALLOC(*buf, size);
336 memcpy(*buf, req_msg, header_size);
337 if (thumb_data_len > 0)
338 memcpy((*buf) + header_size, req_msg->thumb_data, thumb_data_len);
342 return MS_MEDIA_ERR_NONE;
345 gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
350 int err = MS_MEDIA_ERR_NONE;
352 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
353 sock = g_io_channel_unix_get_fd(src);
355 header_size = sizeof(thumbMsg) - sizeof(unsigned char *);
357 thumb_err("_media_thumb_write_socket socket : %d", sock);
359 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
360 thumb_err("_media_thumb_recv_msg failed ");
366 if (recv_msg.status != MS_MEDIA_ERR_NONE) {
367 err = recv_msg.status;
368 thumb_err("Failed to make thumbnail (%d)", err);
371 if (__media_thumb_check_cancel()) {
373 thumbUserData* cb = (thumbUserData*)data;
374 if (cb->func != NULL)
375 cb->func(err, recv_msg.dst_path, cb->user_data);
386 gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
391 int err = MS_MEDIA_ERR_NONE;
393 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
394 sock = g_io_channel_unix_get_fd(src);
396 header_size = sizeof(thumbMsg) - sizeof(unsigned char *);
398 thumb_err("_media_thumb_write_socket socket : %d", sock);
400 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
401 thumb_err("_media_thumb_recv_msg failed ");
402 __media_thumb_pop_raw_data();
407 if (recv_msg.status != MS_MEDIA_ERR_NONE) {
408 err = recv_msg.status;
409 thumb_err("Failed to make thumbnail (%d)", err);
412 if (__media_thumb_check_cancel_for_raw()) {
414 thumbRawUserData* cb = (thumbRawUserData*)data;
415 if (cb->func != NULL)
416 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);
420 __media_thumb_pop_raw_data();
427 static int _media_thumb_send_request()
429 int err = MS_MEDIA_ERR_NONE;
431 struct sockaddr_un serv_addr;
432 thumbReq *req_manager = NULL;
435 err = ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock);
436 if (err != MS_MEDIA_ERR_NONE) {
437 thumb_err("ms_ipc_create_client_socket failed");
441 memset(&serv_addr, 0, sizeof(serv_addr));
442 serv_addr.sun_family = AF_UNIX;
443 SAFE_STRLCPY(serv_addr.sun_path, THUMB_IPC_PATH, sizeof(serv_addr.sun_path));
445 GIOChannel *channel = NULL;
446 channel = g_io_channel_unix_new(sock);
449 /* Connecting to the thumbnail server */
450 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
451 thumb_stderror("connect");
453 err = MS_MEDIA_ERR_PERMISSION_DENIED;
455 err = MS_MEDIA_ERR_SOCKET_CONN;
457 g_io_channel_shutdown(channel, TRUE, NULL);
458 g_io_channel_unref(channel);
464 req_manager = (thumbReq *)g_queue_peek_head(g_manage_queue);
466 if (req_manager == NULL) {
467 thumb_err("queue peek fail");
468 g_io_channel_shutdown(channel, TRUE, NULL);
469 g_io_channel_unref(channel);
471 return MS_MEDIA_ERR_INVALID_PARAMETER;
474 GSource *source = NULL;
475 source = g_io_create_watch(channel, G_IO_IN);
476 g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, req_manager->userData, NULL);
477 source_id = g_source_attach(source, g_main_context_get_thread_default());
480 memset((void *)&req_msg, 0, sizeof(thumbMsg));
484 req_msg.msg_type = req_manager->msg_type;
485 req_msg.request_id = 0;
486 req_msg.uid = req_manager->uid;
487 SAFE_STRLCPY(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
488 req_msg.dst_path[0] = '\0';
489 req_msg.thumb_size = 0;
491 unsigned char *buf = NULL;
493 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
495 if (send(sock, buf, buf_size, 0) < 0) {
496 thumb_err("send failed: %d", errno);
498 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
499 g_io_channel_shutdown(channel, TRUE, NULL);
500 g_io_channel_unref(channel);
502 return MS_MEDIA_ERR_SOCKET_SEND;
506 thumb_dbg("Sending msg to thumbnail daemon is successful");
508 if (req_manager->msg_type == THUMB_REQUEST_DB_INSERT) {
509 req_manager->channel = channel;
510 req_manager->source_id = source_id;
511 req_manager->isRequested = true;
517 static int _media_thumb_raw_data_send_request()
519 int err = MS_MEDIA_ERR_NONE;
521 struct sockaddr_un serv_addr;
522 thumbRawReq *req_manager = NULL;
525 err = ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock);
526 if (err != MS_MEDIA_ERR_NONE) {
527 thumb_err("ms_ipc_create_client_socket failed");
531 memset(&serv_addr, 0, sizeof(serv_addr));
532 serv_addr.sun_family = AF_UNIX;
533 SAFE_STRLCPY(serv_addr.sun_path, THUMB_IPC_PATH, sizeof(serv_addr.sun_path));
535 GIOChannel *channel = NULL;
536 channel = g_io_channel_unix_new(sock);
539 /* Connecting to the thumbnail server */
540 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
541 thumb_stderror("connect error");
543 err = MS_MEDIA_ERR_PERMISSION_DENIED;
545 err = MS_MEDIA_ERR_SOCKET_CONN;
547 g_io_channel_shutdown(channel, TRUE, NULL);
548 g_io_channel_unref(channel);
553 req_manager = (thumbRawReq *)g_queue_peek_head(g_manage_raw_queue);
555 if (req_manager == NULL) {
556 thumb_err("queue peek fail");
557 g_io_channel_shutdown(channel, TRUE, NULL);
558 g_io_channel_unref(channel);
560 return MS_MEDIA_ERR_INVALID_PARAMETER;
563 GSource *source = NULL;
564 source = g_io_create_watch(channel, G_IO_IN);
565 g_source_set_callback(source, (GSourceFunc)_media_thumb_raw_data_write_socket, req_manager->userData, NULL);
566 source_id = g_source_attach(source, g_main_context_get_thread_default());
569 memset((void *)&req_msg, 0, sizeof(thumbMsg));
573 req_msg.msg_type = req_manager->msg_type;
574 req_msg.request_id = req_manager->request_id;
575 req_msg.thumb_width = req_manager->width;
576 req_msg.thumb_height = req_manager->height;
577 req_msg.uid = req_manager->uid;
579 SAFE_STRLCPY(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
580 req_msg.dst_path[0] = '\0';
581 req_msg.thumb_size = 0;
583 unsigned char *buf = NULL;
585 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
587 if (send(sock, buf, buf_size, 0) < 0) {
588 thumb_err("send failed: %d", errno);
590 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
591 g_io_channel_shutdown(channel, TRUE, NULL);
592 g_io_channel_unref(channel);
593 return MS_MEDIA_ERR_SOCKET_SEND;
598 if (req_manager->msg_type == THUMB_REQUEST_RAW_DATA) {
599 req_manager->channel = channel;
600 req_manager->source_id = source_id;
601 req_manager->isRequested = true;
603 return MS_MEDIA_ERR_NONE;
606 int _media_thumb_request_async(int msg_type, unsigned int request_id, const char *origin_path, thumbUserData *userData, uid_t uid)
608 int err = MS_MEDIA_ERR_NONE;
611 if (msg_type == THUMB_REQUEST_CANCEL_MEDIA)
612 return __media_thumb_cancel(request_id);
614 if (g_manage_queue == NULL)
615 g_manage_queue = g_queue_new();
617 thumbReq *thumb_req = NULL;
618 THUMB_MALLOC(thumb_req, sizeof(thumbReq));
619 thumb_retvm_if(thumb_req == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Failed to create request element");
621 thumb_req->msg_type = msg_type;
622 thumb_req->path = g_strdup(origin_path);
623 thumb_req->userData = userData;
624 thumb_req->isCanceled = false;
625 thumb_req->isRequested = false;
626 thumb_req->request_id = request_id;
627 thumb_req->uid = uid;
629 len = g_queue_get_length(g_manage_queue);
630 g_queue_push_tail(g_manage_queue, (gpointer)thumb_req);
633 err = _media_thumb_send_request();
638 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)
640 int err = MS_MEDIA_ERR_NONE;
642 thumbRawReq *thumb_req = NULL;
644 if (msg_type == THUMB_REQUEST_CANCEL_RAW_DATA)
645 return __media_thumb_cancel_raw_data(request_id);
647 if (g_manage_raw_queue == NULL)
648 g_manage_raw_queue = g_queue_new();
650 THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
651 if (thumb_req == NULL) {
652 thumb_err("Failed to create request element");
653 return MS_MEDIA_ERR_INVALID_PARAMETER;
656 thumb_req->msg_type = msg_type;
657 thumb_req->request_id = request_id;
658 thumb_req->path = g_strdup(origin_path);
659 thumb_req->width = width;
660 thumb_req->height = height;
661 thumb_req->userData = userData;
662 thumb_req->isCanceled = false;
663 thumb_req->isRequested = false;
664 thumb_req->uid = uid;
666 len = g_queue_get_length(g_manage_raw_queue);
667 g_queue_push_tail(g_manage_raw_queue, (gpointer)thumb_req);
670 err = _media_thumb_raw_data_send_request();
675 int _media_thumb_request_cancel_all()
677 if (g_manage_raw_queue != NULL) {
678 while (g_queue_get_length(g_manage_raw_queue) > 0) {
679 thumbRawReq *req = (thumbRawReq *)g_queue_peek_tail(g_manage_raw_queue);
681 if (req->isRequested == true) {
682 req->isCanceled = true;
683 thumb_dbg("Last item!!");
687 g_queue_pop_tail(g_manage_raw_queue);
689 SAFE_FREE(req->path);
690 SAFE_FREE(req->userData);
694 thumb_dbg("Queue is empty!!");
700 return MS_MEDIA_ERR_NONE;