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, media_thumb_type thumb_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;
422 req_msg.thumb_type = thumb_type;
424 strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
425 req_msg.org_path[strlen(req_msg.org_path)] = '\0';
427 if (msg_type == THUMB_REQUEST_SAVE_FILE) {
428 strncpy(req_msg.dst_path, thumb_path, sizeof(req_msg.dst_path));
429 req_msg.dst_path[strlen(req_msg.dst_path)] = '\0';
432 req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
433 req_msg.dest_path_size = strlen(req_msg.dst_path) + 1;
434 req_msg.thumb_size = 0;
436 if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
437 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
438 ms_ipc_delete_client_socket(&sock_info);
439 return MS_MEDIA_ERR_INVALID_PARAMETER;
442 unsigned char *buf = NULL;
446 header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
447 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
449 if (send(sock, buf, buf_size, 0) != buf_size) {
450 thumb_err("sendto failed: %d", errno);
452 ms_ipc_delete_client_socket(&sock_info);
453 return MS_MEDIA_ERR_SOCKET_SEND;
456 thumb_dbg("Sending msg to thumbnail daemon is successful");
460 if(msg_type != THUMB_REQUEST_CANCEL_ALL_RAW_DATA && msg_type != THUMB_REQUEST_CANCEL_ALL) { //No response..
461 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
462 thumb_err("_media_thumb_recv_msg failed ");
463 ms_ipc_delete_client_socket(&sock_info);
467 recv_str_len = strlen(recv_msg.org_path);
468 thumb_dbg_slog("recv %s(%d) from thumb daemon is successful", recv_msg.org_path, recv_str_len);
470 ms_ipc_delete_client_socket(&sock_info);
472 if (recv_str_len > max_length) {
473 thumb_err("user buffer is too small. Output's length is %d", recv_str_len);
474 return MS_MEDIA_ERR_INVALID_PARAMETER;
477 if (recv_msg.status == THUMB_FAIL) {
478 thumb_err("Failed to make thumbnail");
479 return MS_MEDIA_ERR_INVALID_PARAMETER;
482 if (msg_type != THUMB_REQUEST_SAVE_FILE) {
483 strncpy(thumb_path, recv_msg.dst_path, max_length);
486 thumb_info->origin_width = recv_msg.origin_width;
487 thumb_info->origin_height = recv_msg.origin_height;
489 thumb_dbg("No response msg_type:[%d]", msg_type);
491 return MS_MEDIA_ERR_NONE;
494 gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
499 int err = MS_MEDIA_ERR_NONE;
501 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
502 sock = g_io_channel_unix_get_fd(src);
504 header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
506 thumb_err("_media_thumb_write_socket socket : %d", sock);
508 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
509 thumb_err("_media_thumb_recv_msg failed ");
510 if (recv_msg.origin_path_size > 0) {
511 __media_thumb_pop_req_queue(recv_msg.org_path, TRUE);
513 thumb_err("origin path size is wrong.");
519 g_io_channel_shutdown(src, TRUE, NULL);
520 g_io_channel_unref(src);
521 //thumb_dbg("Completed..%s", recv_msg.org_path);
523 if (recv_msg.status == THUMB_FAIL) {
524 thumb_err("Failed to make thumbnail");
525 err = MS_MEDIA_ERR_INTERNAL;
529 thumbUserData* cb = (thumbUserData*)data;
530 if (cb->func != NULL)
531 cb->func(err, recv_msg.dst_path, cb->user_data);
534 __media_thumb_pop_req_queue(recv_msg.org_path, FALSE);
540 gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
545 int err = MS_MEDIA_ERR_NONE;
547 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
548 sock = g_io_channel_unix_get_fd(src);
550 header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
552 thumb_err("_media_thumb_write_socket socket : %d", sock);
554 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
555 thumb_err("_media_thumb_recv_msg failed ");
556 if (recv_msg.request_id > 0) {
557 __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, TRUE);
559 thumb_err("origin path size is wrong.");
565 g_io_channel_shutdown(src, TRUE, NULL);
566 g_io_channel_unref(src);
568 if (recv_msg.status == THUMB_FAIL) {
569 thumb_err("Failed to make thumbnail");
570 err = MS_MEDIA_ERR_INTERNAL;
574 thumbRawUserData* cb = (thumbRawUserData*)data;
575 if (cb->func != NULL)
576 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);
579 __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, FALSE);
583 SAFE_FREE(recv_msg.thumb_data);
588 int _media_thumb_request_async(int msg_type, media_thumb_type thumb_type, const char *origin_path, thumbUserData *userData, uid_t uid)
590 int err = MS_MEDIA_ERR_NONE;
592 struct sockaddr_un serv_addr;
593 ms_sock_info_s sock_info;
595 sock_info.port = MS_THUMB_CREATOR_PORT;
597 if ((msg_type == THUMB_REQUEST_DB_INSERT) && (__media_thumb_check_req_queue(origin_path) < 0)) {
598 return MS_MEDIA_ERR_THUMB_DUPLICATED_REQUEST;
601 err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
602 if(err != MS_MEDIA_ERR_NONE)
604 thumb_err("ms_ipc_create_client_socket failed");
608 memset(&serv_addr, 0, sizeof(serv_addr));
609 sock = sock_info.sock_fd;
610 serv_addr.sun_family = AF_UNIX;
611 strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcreator.socket");
613 GIOChannel *channel = NULL;
614 channel = g_io_channel_unix_new(sock);
617 /* Connecting to the thumbnail server */
618 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
619 thumb_stderror("connect");
620 g_io_channel_shutdown(channel, TRUE, NULL);
621 g_io_channel_unref(channel);
622 return MS_MEDIA_ERR_SOCKET_CONN;
625 if (msg_type != THUMB_REQUEST_CANCEL_MEDIA) {
626 //source_id = g_io_add_watch(channel, G_IO_IN, _media_thumb_write_socket, userData );
628 /* Create new channel to watch udp socket */
629 GSource *source = NULL;
630 source = g_io_create_watch(channel, G_IO_IN);
632 /* Set callback to be called when socket is readable */
633 g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, userData, NULL);
634 source_id = g_source_attach(source, g_main_context_get_thread_default());
638 memset((void *)&req_msg, 0, sizeof(thumbMsg));
642 req_msg.msg_type = msg_type;
643 req_msg.thumb_type = thumb_type;
646 strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
647 req_msg.org_path[strlen(req_msg.org_path)] = '\0';
648 req_msg.dst_path[0] = '\0';
649 req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
650 req_msg.dest_path_size = 1;
651 req_msg.thumb_size = 0;
653 if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
654 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
655 g_io_channel_shutdown(channel, TRUE, NULL);
656 g_io_channel_unref(channel);
657 ms_ipc_delete_client_socket(&sock_info);
658 return MS_MEDIA_ERR_INVALID_PARAMETER;
661 unsigned char *buf = NULL;
663 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
665 if (send(sock, buf, buf_size, 0) != buf_size) {
666 thumb_err("sendto failed: %d", errno);
668 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
669 g_io_channel_shutdown(channel, TRUE, NULL);
670 g_io_channel_unref(channel);
671 ms_ipc_delete_client_socket(&sock_info);
672 return MS_MEDIA_ERR_SOCKET_SEND;
676 thumb_dbg("Sending msg to thumbnail daemon is successful");
678 if (msg_type == THUMB_REQUEST_CANCEL_MEDIA) {
679 g_io_channel_shutdown(channel, TRUE, NULL);
681 //thumb_dbg("Cancel : %s[%d]", origin_path, sock);
682 __media_thumb_pop_req_queue(origin_path, TRUE);
683 } else if (msg_type == THUMB_REQUEST_DB_INSERT) {
684 if (g_request_queue == NULL) {
685 g_request_queue = g_queue_new();
688 thumbReq *thumb_req = NULL;
689 thumb_req = calloc(1, sizeof(thumbReq));
690 if (thumb_req == NULL) {
691 thumb_err("Failed to create request element");
692 return MS_MEDIA_ERR_INVALID_PARAMETER;
695 thumb_req->channel = channel;
696 thumb_req->path = strdup(origin_path);
697 thumb_req->source_id = source_id;
698 thumb_req->userData = userData;
700 g_queue_push_tail(g_request_queue, (gpointer)thumb_req);
703 return MS_MEDIA_ERR_NONE;
706 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)
708 int err = MS_MEDIA_ERR_NONE;
710 struct sockaddr_un serv_addr;
711 ms_sock_info_s sock_info;
713 sock_info.port = MS_THUMB_CREATOR_PORT;
715 err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
716 if(err != MS_MEDIA_ERR_NONE)
718 thumb_err("ms_ipc_create_client_socket failed");
722 memset(&serv_addr, 0, sizeof(serv_addr));
723 sock = sock_info.sock_fd;
724 serv_addr.sun_family = AF_UNIX;
725 strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcreator.socket");
727 GIOChannel *channel = NULL;
728 channel = g_io_channel_unix_new(sock);
731 /* Connecting to the thumbnail server */
732 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
733 thumb_err("connect error : %s", strerror(errno));
734 g_io_channel_shutdown(channel, TRUE, NULL);
735 g_io_channel_unref(channel);
736 return MS_MEDIA_ERR_SOCKET_CONN;
739 if (msg_type != THUMB_REQUEST_CANCEL_RAW_DATA) {
740 /* Create new channel to watch udp socket */
741 GSource *source = NULL;
742 source = g_io_create_watch(channel, G_IO_IN);
744 /* Set callback to be called when socket is readable */
745 /*NEED UPDATE SOCKET FILE DELETE*/
746 g_source_set_callback(source, (GSourceFunc)_media_thumb_raw_data_write_socket, userData, NULL);
747 source_id = g_source_attach(source, g_main_context_get_thread_default());
751 memset((void *)&req_msg, 0, sizeof(thumbMsg));
755 req_msg.msg_type = msg_type;
756 req_msg.thumb_type = MEDIA_THUMB_LARGE;
757 req_msg.request_id = request_id;
758 req_msg.thumb_width = width;
759 req_msg.thumb_height = height;
762 strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
763 req_msg.org_path[strlen(req_msg.org_path)] = '\0';
764 req_msg.dst_path[0] = '\0';
766 req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
767 req_msg.dest_path_size = 1;
768 req_msg.thumb_size = 0;
770 if (req_msg.origin_path_size > MAX_PATH_SIZE) {
771 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
772 g_io_channel_shutdown(channel, TRUE, NULL);
773 g_io_channel_unref(channel);
774 return MS_MEDIA_ERR_INVALID_PARAMETER;
777 unsigned char *buf = NULL;
779 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
781 if (send(sock, buf, buf_size, 0) != buf_size) {
782 thumb_err("sendto failed: %d", errno);
784 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
785 g_io_channel_shutdown(channel, TRUE, NULL);
786 g_io_channel_unref(channel);
787 return MS_MEDIA_ERR_SOCKET_SEND;
791 thumb_dbg("Sending msg to thumbnail daemon is successful");
793 if (msg_type == THUMB_REQUEST_CANCEL_RAW_DATA) {
794 g_io_channel_shutdown(channel, TRUE, NULL);
795 __media_thumb_pop_raw_data_req_queue(request_id, TRUE);
796 } else if (msg_type == THUMB_REQUEST_RAW_DATA) {
797 if (g_request_queue == NULL) {
798 g_request_queue = g_queue_new();
800 thumbRawReq *thumb_req = NULL;
801 thumb_req = calloc(1, sizeof(thumbReq));
802 if (thumb_req == NULL) {
803 thumb_err("Failed to create request element");
804 return MS_MEDIA_ERR_INVALID_PARAMETER;
806 thumb_req->channel = channel;
807 thumb_req->request_id = request_id;
808 thumb_req->source_id = source_id;
809 thumb_req->userData = userData;
811 g_queue_push_tail(g_request_queue, (gpointer)thumb_req);
813 return MS_MEDIA_ERR_NONE;