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 GLOBAL_USER 0 //#define tzplatform_getenv(TZ_GLOBAL) //TODO
35 #define THUMB_SOCK_BLOCK_SIZE 512
37 static __thread GQueue *g_request_queue = NULL;
42 thumbUserData *userData;
49 thumbRawUserData *userData;
54 int _media_thumb_get_error()
56 if (errno == EWOULDBLOCK) {
57 thumb_err("Timeout. Can't try any more");
58 return MS_MEDIA_ERR_SOCKET_RECEIVE_TIMEOUT;
60 thumb_err("recvfrom failed : %s", strerror(errno));
61 return MS_MEDIA_ERR_SOCKET_RECEIVE;
65 int __media_thumb_pop_req_queue(const char *path, bool shutdown_channel)
69 if (g_request_queue == NULL) return MS_MEDIA_ERR_INVALID_PARAMETER;
70 req_len = g_queue_get_length(g_request_queue);
73 // thumb_dbg("There is no request in the queue");
76 for (i = 0; i < req_len; i++) {
78 req = (thumbReq *)g_queue_peek_nth(g_request_queue, i);
79 if (req == NULL) continue;
81 if (strncmp(path, req->path, strlen(path)) == 0) {
82 if (shutdown_channel) {
83 GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
84 if (source_id != NULL) {
85 g_source_destroy(source_id);
87 thumb_err("G_SOURCE_ID is NULL");
90 g_io_channel_shutdown(req->channel, TRUE, NULL);
91 g_io_channel_unref(req->channel);
93 g_queue_pop_nth(g_request_queue, i);
96 SAFE_FREE(req->userData);
103 // thumb_dbg("There's no %s in the queue", path);
107 return MS_MEDIA_ERR_NONE;
110 int __media_thumb_pop_raw_data_req_queue(int request_id, bool shutdown_channel)
114 if (g_request_queue == NULL) return MS_MEDIA_ERR_INVALID_PARAMETER;
115 req_len = g_queue_get_length(g_request_queue);
118 // thumb_dbg("There is no request in the queue");
121 for (i = 0; i < req_len; i++) {
122 thumbRawReq *req = NULL;
123 req = (thumbRawReq *)g_queue_peek_nth(g_request_queue, i);
124 if (req == NULL) continue;
126 if (request_id == req->request_id) {
127 if (shutdown_channel) {
128 GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
129 if (source_id != NULL) {
130 g_source_destroy(source_id);
132 thumb_err("G_SOURCE_ID is NULL");
135 g_io_channel_shutdown(req->channel, TRUE, NULL);
136 g_io_channel_unref(req->channel);
138 g_queue_pop_nth(g_request_queue, i);
140 SAFE_FREE(req->userData);
147 // thumb_dbg("There's no %s in the queue", path);
151 return MS_MEDIA_ERR_NONE;
154 int __media_thumb_check_req_queue(const char *path)
158 req_len = g_queue_get_length(g_request_queue);
160 // thumb_dbg("Queue length : %d", req_len);
161 // thumb_dbg("Queue path : %s", path);
164 // thumb_dbg("There is no request in the queue");
167 for (i = 0; i < req_len; i++) {
168 thumbReq *req = NULL;
169 req = (thumbReq *)g_queue_peek_nth(g_request_queue, i);
170 if (req == NULL) continue;
172 if (strncmp(path, req->path, strlen(path)) == 0) {
173 //thumb_dbg("Same Request - %s", path);
174 return MS_MEDIA_ERR_INVALID_PARAMETER;
181 return MS_MEDIA_ERR_NONE;
185 _media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg)
187 int recv_msg_len = 0;
189 int block_size = THUMB_SOCK_BLOCK_SIZE;
191 unsigned char *buf = NULL;
192 unsigned char *block_buf = NULL;
194 buf = (unsigned char*)malloc(header_size * sizeof(unsigned char));
195 block_buf = (unsigned char*)malloc(THUMB_SOCK_BLOCK_SIZE * sizeof(unsigned char));
196 if (buf == NULL || block_buf == NULL) {
197 thumb_err("memory allocation failed");
199 SAFE_FREE(block_buf);
200 return MS_MEDIA_ERR_OUT_OF_MEMORY;
203 if ((recv_msg_len = recv(sock, buf, header_size, 0)) <= 0) {
204 thumb_stderror("recv failed");
206 return _media_thumb_get_error();
209 memcpy(msg, buf, header_size);
210 //thumb_dbg("origin_path_size : %d, dest_path_size : %d, thumb_size : %d", msg->origin_path_size, msg->dest_path_size, msg->thumb_size);
214 remain_size = msg->origin_path_size + msg->dest_path_size + msg->thumb_size;
215 buf = malloc(remain_size * sizeof(unsigned char));
216 memset(buf, 0, remain_size * sizeof(unsigned char));
218 while(remain_size > 0) {
219 if(remain_size < THUMB_SOCK_BLOCK_SIZE) {
220 block_size = remain_size;
222 if ((recv_msg_len = recv(sock, block_buf, block_size, 0)) < 0) {
223 thumb_err("recv failed : %s", strerror(errno));
225 SAFE_FREE(block_buf);
226 return _media_thumb_get_error();
228 memcpy(buf+recv_block, block_buf, block_size);
229 recv_block += block_size;
230 remain_size -= block_size;
233 strncpy(msg->org_path, (char *)buf, msg->origin_path_size);
234 strncpy(msg->dst_path, (char *)buf + msg->origin_path_size, msg->dest_path_size);
236 SAFE_FREE(msg->thumb_data);
237 msg->thumb_data = malloc(msg->thumb_size);
238 memset(msg->thumb_data, 0, msg->thumb_size);
239 memcpy(msg->thumb_data, buf + msg->origin_path_size + msg->dest_path_size, msg->thumb_size);
242 SAFE_FREE(block_buf);
244 return MS_MEDIA_ERR_NONE;
248 _media_thumb_recv_udp_msg(int sock, int header_size, thumbMsg *msg, struct sockaddr_un *from_addr, unsigned int *from_size)
250 int recv_msg_len = 0;
251 unsigned int from_addr_size = sizeof(struct sockaddr_un);
252 unsigned char *buf = NULL;
254 buf = (unsigned char*)malloc(sizeof(thumbMsg));
256 if ((recv_msg_len = recvfrom(sock, buf, sizeof(thumbMsg), 0, (struct sockaddr *)from_addr, &from_addr_size)) < 0) {
257 thumb_stderror("recvform failed");
259 return _media_thumb_get_error();
262 memcpy(msg, buf, header_size);
264 if (msg->origin_path_size <= 0 || msg->origin_path_size > MAX_PATH_SIZE) {
266 thumb_err("msg->origin_path_size is invalid %d", msg->origin_path_size );
267 return MS_MEDIA_ERR_INVALID_PARAMETER;
270 strncpy(msg->org_path, (char*)buf + header_size, msg->origin_path_size);
272 if (msg->dest_path_size <= 0 || msg->dest_path_size > MAX_PATH_SIZE) {
274 thumb_err("msg->origin_path_size is invalid %d", msg->dest_path_size );
275 return MS_MEDIA_ERR_INVALID_PARAMETER;
278 strncpy(msg->dst_path, (char*)buf + header_size + msg->origin_path_size, msg->dest_path_size);
281 *from_size = from_addr_size;
283 return MS_MEDIA_ERR_NONE;
287 _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
289 if (req_msg == NULL || buf == NULL) {
290 return MS_MEDIA_ERR_INVALID_PARAMETER;
293 int org_path_len = 0;
294 int dst_path_len = 0;
295 int thumb_data_len = 0;
299 header_size = sizeof(thumbMsg) -(MAX_FILEPATH_LEN * 2) - sizeof(unsigned char *);
300 org_path_len = req_msg->origin_path_size;
301 dst_path_len = req_msg->dest_path_size;
302 thumb_data_len = req_msg->thumb_size;
304 //thumb_dbg("Basic Size : %d, org_path : %s[%d], dst_path : %s[%d], thumb_data_len : %d", header_size, req_msg->org_path, org_path_len, req_msg->dst_path, dst_path_len, thumb_data_len);
306 size = header_size + org_path_len + dst_path_len + thumb_data_len;
313 memcpy(*buf, req_msg, header_size);
314 memcpy((*buf)+header_size, req_msg->org_path, org_path_len);
315 memcpy((*buf)+header_size + org_path_len, req_msg->dst_path, dst_path_len);
316 if(thumb_data_len > 0)
317 memcpy((*buf)+header_size + org_path_len + dst_path_len, req_msg->thumb_data, thumb_data_len);
321 return MS_MEDIA_ERR_NONE;
325 _media_thumb_set_buffer_for_response(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
327 if (req_msg == NULL || buf == NULL) {
328 return MS_MEDIA_ERR_INVALID_PARAMETER;
331 int org_path_len = 0;
332 int dst_path_len = 0;
336 header_size = sizeof(thumbMsg) -(MAX_FILEPATH_LEN * 2) - sizeof(unsigned char *);
337 org_path_len = req_msg->origin_path_size;
338 dst_path_len = req_msg->dest_path_size;
340 thumb_dbg("Basic Size : %d, org_path : %s[%d], dst_path : %s[%d]", header_size, req_msg->org_path, org_path_len, req_msg->dst_path, dst_path_len);
342 size = header_size + org_path_len + dst_path_len;
348 memcpy(*buf, req_msg, header_size);
349 memcpy((*buf)+header_size, req_msg->org_path, org_path_len);
350 memcpy((*buf)+header_size + org_path_len, req_msg->dst_path, dst_path_len);
354 return MS_MEDIA_ERR_NONE;
359 _media_thumb_set_add_raw_data_buffer(thumbRawAddMsg *req_msg, unsigned char **buf, int *buf_size)
361 if (req_msg == NULL || buf == NULL) {
362 return MS_MEDIA_ERR_INVALID_PARAMETER;
368 header_size = sizeof(thumbRawAddMsg);
369 thumb_len = req_msg->thumb_size;
371 size = header_size + thumb_len;
373 memcpy(*buf, req_msg, header_size);
374 memcpy((*buf)+header_size, req_msg->thumb_data, thumb_len);
378 return MS_MEDIA_ERR_NONE;
382 _media_thumb_request(int msg_type, const char *origin_path, char *thumb_path, int max_length, media_thumb_info *thumb_info, uid_t uid)
385 struct sockaddr_un serv_addr;
386 ms_sock_info_s sock_info;
387 int recv_str_len = 0;
388 int err = MS_MEDIA_ERR_NONE;
390 sock_info.port = MS_THUMB_CREATOR_PORT;
392 err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
393 if (err != MS_MEDIA_ERR_NONE) {
394 thumb_err("ms_ipc_create_client_socket failed");
398 memset(&serv_addr, 0, sizeof(serv_addr));
399 sock = sock_info.sock_fd;
400 serv_addr.sun_family = AF_UNIX;
401 strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcreator.socket");
403 /* Connecting to the thumbnail server */
404 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
405 thumb_stderror("connect");
406 ms_ipc_delete_client_socket(&sock_info);
407 return MS_MEDIA_ERR_SOCKET_CONN;
413 memset((void *)&req_msg, 0, sizeof(thumbMsg));
414 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
416 /* Get PID of client*/
420 /* Set requset message */
421 req_msg.msg_type = msg_type;
423 strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
424 req_msg.org_path[strlen(req_msg.org_path)] = '\0';
426 if (msg_type == THUMB_REQUEST_SAVE_FILE) {
427 strncpy(req_msg.dst_path, thumb_path, sizeof(req_msg.dst_path));
428 req_msg.dst_path[strlen(req_msg.dst_path)] = '\0';
431 req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
432 req_msg.dest_path_size = strlen(req_msg.dst_path) + 1;
433 req_msg.thumb_size = 0;
435 if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
436 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
437 ms_ipc_delete_client_socket(&sock_info);
438 return MS_MEDIA_ERR_INVALID_PARAMETER;
441 unsigned char *buf = NULL;
445 header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
446 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
448 if (send(sock, buf, buf_size, 0) != buf_size) {
449 thumb_err("sendto failed: %d", errno);
451 ms_ipc_delete_client_socket(&sock_info);
452 return MS_MEDIA_ERR_SOCKET_SEND;
455 thumb_dbg("Sending msg to thumbnail daemon is successful");
459 if(msg_type != THUMB_REQUEST_CANCEL_ALL_RAW_DATA && msg_type != THUMB_REQUEST_CANCEL_ALL) { //No response..
460 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
461 thumb_err("_media_thumb_recv_msg failed ");
462 ms_ipc_delete_client_socket(&sock_info);
466 recv_str_len = strlen(recv_msg.org_path);
467 thumb_dbg_slog("recv %s(%d) from thumb daemon is successful", recv_msg.org_path, recv_str_len);
469 ms_ipc_delete_client_socket(&sock_info);
471 if (recv_str_len > max_length) {
472 thumb_err("user buffer is too small. Output's length is %d", recv_str_len);
473 return MS_MEDIA_ERR_INVALID_PARAMETER;
476 if (recv_msg.status == THUMB_FAIL) {
477 thumb_err("Failed to make thumbnail");
478 return MS_MEDIA_ERR_INVALID_PARAMETER;
481 if (msg_type != THUMB_REQUEST_SAVE_FILE) {
482 strncpy(thumb_path, recv_msg.dst_path, max_length);
485 thumb_info->origin_width = recv_msg.origin_width;
486 thumb_info->origin_height = recv_msg.origin_height;
488 thumb_dbg("No response msg_type:[%d]", msg_type);
490 return MS_MEDIA_ERR_NONE;
493 gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
498 int err = MS_MEDIA_ERR_NONE;
500 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
501 sock = g_io_channel_unix_get_fd(src);
503 header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
505 thumb_err("_media_thumb_write_socket socket : %d", sock);
507 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
508 thumb_err("_media_thumb_recv_msg failed ");
509 if (recv_msg.origin_path_size > 0) {
510 __media_thumb_pop_req_queue(recv_msg.org_path, TRUE);
512 thumb_err("origin path size is wrong.");
518 g_io_channel_shutdown(src, TRUE, NULL);
519 g_io_channel_unref(src);
520 //thumb_dbg("Completed..%s", recv_msg.org_path);
522 if (recv_msg.status == THUMB_FAIL) {
523 thumb_err("Failed to make thumbnail");
524 err = MS_MEDIA_ERR_INTERNAL;
528 thumbUserData* cb = (thumbUserData*)data;
529 if (cb->func != NULL)
530 cb->func(err, recv_msg.dst_path, cb->user_data);
533 __media_thumb_pop_req_queue(recv_msg.org_path, FALSE);
539 gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
544 int err = MS_MEDIA_ERR_NONE;
546 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
547 sock = g_io_channel_unix_get_fd(src);
549 header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
551 thumb_err("_media_thumb_write_socket socket : %d", sock);
553 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
554 thumb_err("_media_thumb_recv_msg failed ");
555 if (recv_msg.request_id > 0) {
556 __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, TRUE);
558 thumb_err("origin path size is wrong.");
564 g_io_channel_shutdown(src, TRUE, NULL);
565 g_io_channel_unref(src);
567 if (recv_msg.status == THUMB_FAIL) {
568 thumb_err("Failed to make thumbnail");
569 err = MS_MEDIA_ERR_INTERNAL;
573 thumbRawUserData* cb = (thumbRawUserData*)data;
574 if (cb->func != NULL)
575 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);
578 __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, FALSE);
582 SAFE_FREE(recv_msg.thumb_data);
587 int _media_thumb_request_async(int msg_type, const char *origin_path, thumbUserData *userData, uid_t uid)
589 int err = MS_MEDIA_ERR_NONE;
591 struct sockaddr_un serv_addr;
592 ms_sock_info_s sock_info;
594 sock_info.port = MS_THUMB_CREATOR_PORT;
596 if ((msg_type == THUMB_REQUEST_DB_INSERT) && (__media_thumb_check_req_queue(origin_path) < 0)) {
597 return MS_MEDIA_ERR_THUMB_DUPLICATED_REQUEST;
600 err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
601 if(err != MS_MEDIA_ERR_NONE)
603 thumb_err("ms_ipc_create_client_socket failed");
607 memset(&serv_addr, 0, sizeof(serv_addr));
608 sock = sock_info.sock_fd;
609 serv_addr.sun_family = AF_UNIX;
610 strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcreator.socket");
612 GIOChannel *channel = NULL;
613 channel = g_io_channel_unix_new(sock);
616 /* Connecting to the thumbnail server */
617 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
618 thumb_stderror("connect");
619 g_io_channel_shutdown(channel, TRUE, NULL);
620 g_io_channel_unref(channel);
621 return MS_MEDIA_ERR_SOCKET_CONN;
624 if (msg_type != THUMB_REQUEST_CANCEL_MEDIA) {
625 /* Create new channel to watch udp socket */
626 GSource *source = NULL;
627 source = g_io_create_watch(channel, G_IO_IN);
629 /* Set callback to be called when socket is readable */
630 g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, userData, NULL);
631 source_id = g_source_attach(source, g_main_context_get_thread_default());
635 memset((void *)&req_msg, 0, sizeof(thumbMsg));
639 req_msg.msg_type = msg_type;
642 strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
643 req_msg.org_path[strlen(req_msg.org_path)] = '\0';
644 req_msg.dst_path[0] = '\0';
645 req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
646 req_msg.dest_path_size = 1;
647 req_msg.thumb_size = 0;
649 if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
650 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
651 g_io_channel_shutdown(channel, TRUE, NULL);
652 g_io_channel_unref(channel);
653 ms_ipc_delete_client_socket(&sock_info);
654 return MS_MEDIA_ERR_INVALID_PARAMETER;
657 unsigned char *buf = NULL;
659 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
661 if (send(sock, buf, buf_size, 0) != buf_size) {
662 thumb_err("sendto failed: %d", errno);
664 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
665 g_io_channel_shutdown(channel, TRUE, NULL);
666 g_io_channel_unref(channel);
667 ms_ipc_delete_client_socket(&sock_info);
668 return MS_MEDIA_ERR_SOCKET_SEND;
672 thumb_dbg("Sending msg to thumbnail daemon is successful");
674 if (msg_type == THUMB_REQUEST_CANCEL_MEDIA) {
675 g_io_channel_shutdown(channel, TRUE, NULL);
676 __media_thumb_pop_req_queue(origin_path, TRUE);
677 } else if (msg_type == THUMB_REQUEST_DB_INSERT) {
678 if (g_request_queue == NULL) {
679 g_request_queue = g_queue_new();
682 thumbReq *thumb_req = NULL;
683 thumb_req = calloc(1, sizeof(thumbReq));
684 if (thumb_req == NULL) {
685 thumb_err("Failed to create request element");
686 return MS_MEDIA_ERR_INVALID_PARAMETER;
689 thumb_req->channel = channel;
690 thumb_req->path = strdup(origin_path);
691 thumb_req->source_id = source_id;
692 thumb_req->userData = userData;
694 g_queue_push_tail(g_request_queue, (gpointer)thumb_req);
697 return MS_MEDIA_ERR_NONE;
700 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)
702 int err = MS_MEDIA_ERR_NONE;
704 struct sockaddr_un serv_addr;
705 ms_sock_info_s sock_info;
707 sock_info.port = MS_THUMB_CREATOR_PORT;
709 err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
710 if(err != MS_MEDIA_ERR_NONE)
712 thumb_err("ms_ipc_create_client_socket failed");
716 memset(&serv_addr, 0, sizeof(serv_addr));
717 sock = sock_info.sock_fd;
718 serv_addr.sun_family = AF_UNIX;
719 strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcreator.socket");
721 GIOChannel *channel = NULL;
722 channel = g_io_channel_unix_new(sock);
725 /* Connecting to the thumbnail server */
726 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
727 thumb_err("connect error : %s", strerror(errno));
728 g_io_channel_shutdown(channel, TRUE, NULL);
729 g_io_channel_unref(channel);
730 return MS_MEDIA_ERR_SOCKET_CONN;
733 if (msg_type != THUMB_REQUEST_CANCEL_RAW_DATA) {
734 /* Create new channel to watch udp socket */
735 GSource *source = NULL;
736 source = g_io_create_watch(channel, G_IO_IN);
738 /* Set callback to be called when socket is readable */
739 /*NEED UPDATE SOCKET FILE DELETE*/
740 g_source_set_callback(source, (GSourceFunc)_media_thumb_raw_data_write_socket, userData, NULL);
741 source_id = g_source_attach(source, g_main_context_get_thread_default());
745 memset((void *)&req_msg, 0, sizeof(thumbMsg));
749 req_msg.msg_type = msg_type;
750 req_msg.request_id = request_id;
751 req_msg.thumb_width = width;
752 req_msg.thumb_height = height;
755 strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
756 req_msg.org_path[strlen(req_msg.org_path)] = '\0';
757 req_msg.dst_path[0] = '\0';
759 req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
760 req_msg.dest_path_size = 1;
761 req_msg.thumb_size = 0;
763 if (req_msg.origin_path_size > MAX_PATH_SIZE) {
764 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
765 g_io_channel_shutdown(channel, TRUE, NULL);
766 g_io_channel_unref(channel);
767 return MS_MEDIA_ERR_INVALID_PARAMETER;
770 unsigned char *buf = NULL;
772 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
774 if (send(sock, buf, buf_size, 0) != buf_size) {
775 thumb_err("sendto failed: %d", errno);
777 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
778 g_io_channel_shutdown(channel, TRUE, NULL);
779 g_io_channel_unref(channel);
780 return MS_MEDIA_ERR_SOCKET_SEND;
784 thumb_dbg("Sending msg to thumbnail daemon is successful");
786 if (msg_type == THUMB_REQUEST_CANCEL_RAW_DATA) {
787 g_io_channel_shutdown(channel, TRUE, NULL);
788 __media_thumb_pop_raw_data_req_queue(request_id, TRUE);
789 } else if (msg_type == THUMB_REQUEST_RAW_DATA) {
790 if (g_request_queue == NULL) {
791 g_request_queue = g_queue_new();
793 thumbRawReq *thumb_req = NULL;
794 thumb_req = calloc(1, sizeof(thumbReq));
795 if (thumb_req == NULL) {
796 thumb_err("Failed to create request element");
797 return MS_MEDIA_ERR_INVALID_PARAMETER;
799 thumb_req->channel = channel;
800 thumb_req->request_id = request_id;
801 thumb_req->source_id = source_id;
802 thumb_req->userData = userData;
804 g_queue_push_tail(g_request_queue, (gpointer)thumb_req);
806 return MS_MEDIA_ERR_NONE;