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);
69 static int __media_thumb_cancel(unsigned int request_id)
74 thumb_retv_if(!g_manage_queue, MS_MEDIA_ERR_NONE);
76 len = g_queue_get_length(g_manage_queue);
78 for (i = 0; i < len; i++) {
79 req = (thumbReq *)g_queue_peek_nth(g_manage_queue, i);
83 if (req->request_id == request_id) {
84 if (req->isRequested) {
85 req->isCanceled = true;
87 g_queue_pop_nth(g_manage_queue, i);
90 SAFE_FREE(req->userData);
94 return MS_MEDIA_ERR_NONE;
98 return MS_MEDIA_ERR_INTERNAL;
101 static int __media_thumb_cancel_raw_data(int request_id)
104 thumbRawReq *req = NULL;
106 thumb_retv_if(!g_manage_raw_queue, MS_MEDIA_ERR_NONE);
108 len = g_queue_get_length(g_manage_raw_queue);
110 for (i = 0; i < len; i++) {
111 req = (thumbRawReq *)g_queue_peek_nth(g_manage_raw_queue, i);
115 if (req->request_id == request_id) {
116 if (req->isRequested) {
117 req->isCanceled = true;
119 g_queue_pop_nth(g_manage_raw_queue, i);
121 SAFE_FREE(req->path);
122 SAFE_FREE(req->userData);
126 return MS_MEDIA_ERR_NONE;
130 return MS_MEDIA_ERR_INTERNAL;
133 static bool __media_thumb_is_canceled(void)
135 thumbReq *req = (thumbReq *)g_queue_peek_head(g_manage_queue);
140 return req->isCanceled;
143 static bool __media_thumb_is_canceled_for_raw(void)
145 thumbRawReq *req = (thumbRawReq *)g_queue_peek_head(g_manage_raw_queue);
150 return req->isCanceled;
153 static int __media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg)
158 unsigned char *buf = NULL;
160 THUMB_MALLOC(buf, header_size);
161 thumb_retv_if(!buf, MS_MEDIA_ERR_OUT_OF_MEMORY);
163 while (header_size > 0) {
164 if ((recv_len = recv(sock, buf + recv_pos, header_size, 0)) < 0) {
165 thumb_stderror("recv failed");
167 return MS_MEDIA_ERR_IPC;
169 header_size -= recv_len;
170 recv_pos += recv_len;
173 header_size = recv_pos;
176 memcpy(msg, buf, header_size);
179 thumb_retvm_if(strlen(msg->org_path) == 0 || strlen(msg->org_path) >= MAX_FILEPATH_LEN, MS_MEDIA_ERR_IPC, "Invalid org_path");
180 thumb_retvm_if(strlen(msg->dst_path) >= MAX_FILEPATH_LEN, MS_MEDIA_ERR_IPC, "Invalid dst_path");
181 thumb_retvm_if(msg->thumb_size < 0, MS_MEDIA_ERR_IPC, "Invalid thumb_size");
182 thumb_retv_if(msg->thumb_size == 0, MS_MEDIA_ERR_NONE);
184 remain_size = msg->thumb_size;
185 THUMB_MALLOC(buf, remain_size);
186 thumb_retv_if(!buf, MS_MEDIA_ERR_OUT_OF_MEMORY);
188 while (remain_size > 0) {
189 if ((recv_len = recv(sock, buf + recv_pos, remain_size, 0)) < 0) {
190 thumb_stderror("recv failed");
192 return MS_MEDIA_ERR_IPC;
196 remain_size -= recv_len;
197 recv_pos += recv_len;
200 SAFE_FREE(msg->thumb_data);
201 THUMB_MALLOC(msg->thumb_data, msg->thumb_size);
202 if (msg->thumb_data) {
203 memcpy(msg->thumb_data, buf, msg->thumb_size);
206 return MS_MEDIA_ERR_OUT_OF_MEMORY;
211 return MS_MEDIA_ERR_NONE;
214 int _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
216 if (req_msg == NULL || buf == NULL)
217 return MS_MEDIA_ERR_INVALID_PARAMETER;
219 int thumb_data_len = 0;
223 header_size = sizeof(thumbMsg) - sizeof(unsigned char *);
224 thumb_data_len = req_msg->thumb_size;
225 if (thumb_data_len < 0)
226 return MS_MEDIA_ERR_INVALID_PARAMETER;
228 size = header_size + thumb_data_len;
229 THUMB_MALLOC(*buf, size);
234 memcpy(*buf, req_msg, header_size);
235 if (thumb_data_len > 0)
236 memcpy((*buf) + header_size, req_msg->thumb_data, thumb_data_len);
240 return MS_MEDIA_ERR_NONE;
243 static void __media_thumb_pop(void)
247 thumbReq *req = NULL;
248 GSource *source_id = NULL;
253 req = (thumbReq *)g_queue_pop_head(g_manage_queue);
255 source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
256 sock = g_io_channel_unix_get_fd(req->channel);
258 g_io_channel_shutdown(req->channel, TRUE, NULL);
259 g_io_channel_unref(req->channel);
262 g_source_destroy(source_id);
264 SAFE_FREE(req->path);
265 SAFE_FREE(req->userData);
269 /* Check manage queue */
270 len = g_queue_get_length(g_manage_queue);
272 __media_thumb_send_request();
275 static void __media_thumb_pop_raw_data(void)
279 thumbRawReq *req = NULL;
280 GSource *source_id = NULL;
282 if (!g_manage_raw_queue)
285 req = (thumbRawReq *)g_queue_pop_head(g_manage_raw_queue);
287 source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
288 sock = g_io_channel_unix_get_fd(req->channel);
290 g_io_channel_shutdown(req->channel, TRUE, NULL);
291 g_io_channel_unref(req->channel);
294 g_source_destroy(source_id);
296 SAFE_FREE(req->path);
297 SAFE_FREE(req->userData);
301 /* Check manage queue */
302 len = g_queue_get_length(g_manage_raw_queue);
304 __media_thumb_raw_data_send_request();
307 static gboolean __media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
312 int err = MS_MEDIA_ERR_NONE;
313 thumbUserData *userdata = NULL;
315 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
316 sock = g_io_channel_unix_get_fd(src);
318 header_size = sizeof(thumbMsg) - sizeof(unsigned char *);
320 if ((err = __media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
321 thumb_err("_media_thumb_recv_msg failed ");
325 if (recv_msg.status != MS_MEDIA_ERR_NONE) {
326 err = recv_msg.status;
327 thumb_err("Failed to make thumbnail (%d)", err);
330 if (__media_thumb_is_canceled() || !data)
333 userdata = (thumbUserData*)data;
335 userdata->func(err, recv_msg.dst_path, userdata->user_data);
341 return G_SOURCE_REMOVE;
344 static gboolean __media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
349 int err = MS_MEDIA_ERR_NONE;
350 thumbRawUserData *userdata = NULL;
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 ");
364 if (recv_msg.status != MS_MEDIA_ERR_NONE) {
365 err = recv_msg.status;
366 thumb_err("Failed to make thumbnail (%d)", err);
369 if (__media_thumb_is_canceled_for_raw() || !data)
372 userdata = (thumbRawUserData*)data;
374 userdata->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, userdata->user_data);
378 __media_thumb_pop_raw_data();
380 return G_SOURCE_REMOVE;
383 static int __media_thumb_send_request(void)
385 int err = MS_MEDIA_ERR_NONE;
387 struct sockaddr_un serv_addr;
388 thumbReq *req_manager = NULL;
391 err = ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock);
392 thumb_retvm_if(err != MS_MEDIA_ERR_NONE, err, "ms_ipc_create_client_socket failed");
394 memset(&serv_addr, 0, sizeof(serv_addr));
395 serv_addr.sun_family = AF_UNIX;
396 SAFE_STRLCPY(serv_addr.sun_path, THUMB_IPC_PATH, sizeof(serv_addr.sun_path));
398 GIOChannel *channel = NULL;
399 channel = g_io_channel_unix_new(sock);
402 /* Connecting to the thumbnail server */
403 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
404 thumb_stderror("connect");
406 err = MS_MEDIA_ERR_PERMISSION_DENIED;
408 err = MS_MEDIA_ERR_IPC;
410 g_io_channel_shutdown(channel, TRUE, NULL);
411 g_io_channel_unref(channel);
417 req_manager = (thumbReq *)g_queue_peek_head(g_manage_queue);
419 thumb_err("queue peek fail");
420 g_io_channel_shutdown(channel, TRUE, NULL);
421 g_io_channel_unref(channel);
423 return MS_MEDIA_ERR_INVALID_PARAMETER;
426 GSource *source = NULL;
427 source = g_io_create_watch(channel, G_IO_IN);
428 g_source_set_callback(source, (GSourceFunc)__media_thumb_write_socket, req_manager->userData, NULL);
429 source_id = g_source_attach(source, g_main_context_get_thread_default());
432 memset((void *)&req_msg, 0, sizeof(thumbMsg));
436 req_msg.msg_type = req_manager->msg_type;
437 req_msg.request_id = 0;
438 req_msg.uid = req_manager->uid;
439 SAFE_STRLCPY(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
440 req_msg.dst_path[0] = '\0';
441 req_msg.thumb_size = 0;
443 unsigned char *buf = NULL;
445 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
447 if (send(sock, buf, buf_size, 0) < 0) {
448 thumb_err("send failed: %d", errno);
450 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
451 g_io_channel_shutdown(channel, TRUE, NULL);
452 g_io_channel_unref(channel);
454 return MS_MEDIA_ERR_IPC;
458 thumb_dbg("Sending msg to thumbnail daemon is successful");
460 if (req_manager->msg_type == THUMB_REQUEST_DB_INSERT) {
461 req_manager->channel = channel;
462 req_manager->source_id = source_id;
463 req_manager->isRequested = true;
469 static int __media_thumb_raw_data_send_request(void)
471 int err = MS_MEDIA_ERR_NONE;
473 struct sockaddr_un serv_addr;
474 thumbRawReq *req_manager = NULL;
477 err = ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock);
478 thumb_retvm_if(err != MS_MEDIA_ERR_NONE, err, "ms_ipc_create_client_socket failed");
480 memset(&serv_addr, 0, sizeof(serv_addr));
481 serv_addr.sun_family = AF_UNIX;
482 SAFE_STRLCPY(serv_addr.sun_path, THUMB_IPC_PATH, sizeof(serv_addr.sun_path));
484 GIOChannel *channel = NULL;
485 channel = g_io_channel_unix_new(sock);
488 /* Connecting to the thumbnail server */
489 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
490 thumb_stderror("connect error");
492 err = MS_MEDIA_ERR_PERMISSION_DENIED;
494 err = MS_MEDIA_ERR_IPC;
496 g_io_channel_shutdown(channel, TRUE, NULL);
497 g_io_channel_unref(channel);
502 req_manager = (thumbRawReq *)g_queue_peek_head(g_manage_raw_queue);
505 thumb_err("queue peek fail");
506 g_io_channel_shutdown(channel, TRUE, NULL);
507 g_io_channel_unref(channel);
509 return MS_MEDIA_ERR_INVALID_PARAMETER;
512 GSource *source = NULL;
513 source = g_io_create_watch(channel, G_IO_IN);
514 g_source_set_callback(source, (GSourceFunc)__media_thumb_raw_data_write_socket, req_manager->userData, NULL);
515 source_id = g_source_attach(source, g_main_context_get_thread_default());
518 memset((void *)&req_msg, 0, sizeof(thumbMsg));
522 req_msg.msg_type = req_manager->msg_type;
523 req_msg.request_id = req_manager->request_id;
524 req_msg.thumb_width = req_manager->width;
525 req_msg.thumb_height = req_manager->height;
526 req_msg.uid = req_manager->uid;
528 SAFE_STRLCPY(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
529 req_msg.dst_path[0] = '\0';
530 req_msg.thumb_size = 0;
532 unsigned char *buf = NULL;
534 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
536 if (send(sock, buf, buf_size, 0) < 0) {
537 thumb_err("send failed: %d", errno);
539 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
540 g_io_channel_shutdown(channel, TRUE, NULL);
541 g_io_channel_unref(channel);
542 return MS_MEDIA_ERR_IPC;
547 if (req_manager->msg_type == THUMB_REQUEST_RAW_DATA) {
548 req_manager->channel = channel;
549 req_manager->source_id = source_id;
550 req_manager->isRequested = true;
552 return MS_MEDIA_ERR_NONE;
555 int _media_thumb_request_async(int msg_type, unsigned int request_id, const char *origin_path, thumbUserData *userData, uid_t uid)
557 int err = MS_MEDIA_ERR_NONE;
560 if (msg_type == THUMB_REQUEST_CANCEL_MEDIA)
561 return __media_thumb_cancel(request_id);
563 if (g_manage_queue == NULL)
564 g_manage_queue = g_queue_new();
566 thumbReq *thumb_req = NULL;
567 THUMB_MALLOC(thumb_req, sizeof(thumbReq));
568 thumb_retvm_if(thumb_req == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Failed to create request element");
570 thumb_req->msg_type = msg_type;
571 thumb_req->path = g_strdup(origin_path);
572 thumb_req->userData = userData;
573 thumb_req->isCanceled = false;
574 thumb_req->isRequested = false;
575 thumb_req->request_id = request_id;
576 thumb_req->uid = uid;
578 len = g_queue_get_length(g_manage_queue);
579 g_queue_push_tail(g_manage_queue, (gpointer)thumb_req);
582 err = __media_thumb_send_request();
587 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)
589 int err = MS_MEDIA_ERR_NONE;
591 thumbRawReq *thumb_req = NULL;
593 if (msg_type == THUMB_REQUEST_CANCEL_RAW_DATA)
594 return __media_thumb_cancel_raw_data(request_id);
596 if (g_manage_raw_queue == NULL)
597 g_manage_raw_queue = g_queue_new();
599 THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
600 if (thumb_req == NULL) {
601 thumb_err("Failed to create request element");
602 return MS_MEDIA_ERR_INVALID_PARAMETER;
605 thumb_req->msg_type = msg_type;
606 thumb_req->request_id = request_id;
607 thumb_req->path = g_strdup(origin_path);
608 thumb_req->width = width;
609 thumb_req->height = height;
610 thumb_req->userData = userData;
611 thumb_req->isCanceled = false;
612 thumb_req->isRequested = false;
613 thumb_req->uid = uid;
615 len = g_queue_get_length(g_manage_raw_queue);
616 g_queue_push_tail(g_manage_raw_queue, (gpointer)thumb_req);
619 err = __media_thumb_raw_data_send_request();