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 + 2;
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 + 1, msg->dest_path_size);
235 SAFE_FREE(msg->thumb_data);
236 msg->thumb_data = malloc(msg->thumb_size);
237 memset(msg->thumb_data, 0, msg->thumb_size);
238 memcpy(msg->thumb_data, buf + msg->origin_path_size + msg->dest_path_size + 2, msg->thumb_size);
241 SAFE_FREE(block_buf);
243 return MS_MEDIA_ERR_NONE;
247 _media_thumb_recv_udp_msg(int sock, int header_size, thumbMsg *msg, struct sockaddr_un *from_addr, unsigned int *from_size)
249 int recv_msg_len = 0;
250 unsigned int from_addr_size = sizeof(struct sockaddr_un);
251 unsigned char *buf = NULL;
253 buf = (unsigned char*)malloc(sizeof(thumbMsg));
255 if ((recv_msg_len = recvfrom(sock, buf, sizeof(thumbMsg), 0, (struct sockaddr *)from_addr, &from_addr_size)) < 0) {
256 thumb_stderror("recvform failed");
258 return _media_thumb_get_error();
261 memcpy(msg, buf, header_size);
263 if (msg->origin_path_size <= 0 || msg->origin_path_size > MAX_PATH_SIZE) {
265 thumb_err("msg->origin_path_size is invalid %d", msg->origin_path_size );
266 return MS_MEDIA_ERR_INVALID_PARAMETER;
269 strncpy(msg->org_path, (char*)buf + header_size, msg->origin_path_size);
271 if (msg->dest_path_size <= 0 || msg->dest_path_size > MAX_PATH_SIZE) {
273 thumb_err("msg->origin_path_size is invalid %d", msg->dest_path_size );
274 return MS_MEDIA_ERR_INVALID_PARAMETER;
277 strncpy(msg->dst_path, (char*)buf + header_size + msg->origin_path_size, msg->dest_path_size);
280 *from_size = from_addr_size;
282 return MS_MEDIA_ERR_NONE;
286 _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
288 if (req_msg == NULL || buf == NULL) {
289 return MS_MEDIA_ERR_INVALID_PARAMETER;
292 int org_path_len = 0;
293 int dst_path_len = 0;
294 int thumb_data_len = 0;
298 header_size = sizeof(thumbMsg) -(MAX_FILEPATH_LEN * 2) - sizeof(unsigned char *);
299 org_path_len = req_msg->origin_path_size + 1;
300 dst_path_len = req_msg->dest_path_size + 1;
301 thumb_data_len = req_msg->thumb_size;
303 //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);
305 size = header_size + org_path_len + dst_path_len + thumb_data_len;
312 memcpy(*buf, req_msg, header_size);
313 memcpy((*buf)+header_size, req_msg->org_path, org_path_len);
314 memcpy((*buf)+header_size + org_path_len, req_msg->dst_path, dst_path_len);
315 memcpy((*buf)+header_size + org_path_len + dst_path_len, req_msg->thumb_data, thumb_data_len);
319 return MS_MEDIA_ERR_NONE;
323 _media_thumb_set_buffer_for_response(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
325 if (req_msg == NULL || buf == NULL) {
326 return MS_MEDIA_ERR_INVALID_PARAMETER;
329 int org_path_len = 0;
330 int dst_path_len = 0;
331 int thumb_data_len = 0;
335 header_size = sizeof(thumbMsg) -(MAX_FILEPATH_LEN * 2) - sizeof(unsigned char *);
336 org_path_len = req_msg->origin_path_size + 1;
337 dst_path_len = req_msg->dest_path_size + 1;
340 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);
342 size = header_size + org_path_len + dst_path_len + thumb_data_len;
349 memcpy(*buf, req_msg, header_size);
350 memcpy((*buf)+header_size, req_msg->org_path, org_path_len);
351 memcpy((*buf)+header_size + org_path_len, req_msg->dst_path, dst_path_len);
352 memcpy((*buf)+header_size + org_path_len + dst_path_len, req_msg->thumb_data, thumb_data_len);
356 return MS_MEDIA_ERR_NONE;
361 _media_thumb_set_add_raw_data_buffer(thumbRawAddMsg *req_msg, unsigned char **buf, int *buf_size)
363 if (req_msg == NULL || buf == NULL) {
364 return MS_MEDIA_ERR_INVALID_PARAMETER;
370 header_size = sizeof(thumbRawAddMsg);
371 thumb_len = req_msg->thumb_size;
373 size = header_size + thumb_len;
375 memcpy(*buf, req_msg, header_size);
376 memcpy((*buf)+header_size, req_msg->thumb_data, thumb_len);
380 return MS_MEDIA_ERR_NONE;
384 _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)
387 struct sockaddr_un serv_addr;
388 ms_sock_info_s sock_info;
389 int recv_str_len = 0;
390 int err = MS_MEDIA_ERR_NONE;
392 sock_info.port = MS_THUMB_CREATOR_PORT;
394 err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
395 if (err != MS_MEDIA_ERR_NONE) {
396 thumb_err("ms_ipc_create_client_socket failed");
400 memset(&serv_addr, 0, sizeof(serv_addr));
401 sock = sock_info.sock_fd;
402 serv_addr.sun_family = AF_UNIX;
403 strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcreator.socket");
405 /* Connecting to the thumbnail server */
406 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
407 thumb_stderror("connect");
408 ms_ipc_delete_client_socket(&sock_info);
409 return MS_MEDIA_ERR_SOCKET_CONN;
415 memset((void *)&req_msg, 0, sizeof(thumbMsg));
416 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
418 /* Get PID of client*/
422 /* Set requset message */
423 req_msg.msg_type = msg_type;
424 req_msg.thumb_type = thumb_type;
426 strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
427 req_msg.org_path[strlen(req_msg.org_path)] = '\0';
429 if (msg_type == THUMB_REQUEST_SAVE_FILE) {
430 strncpy(req_msg.dst_path, thumb_path, sizeof(req_msg.dst_path));
431 req_msg.dst_path[strlen(req_msg.dst_path)] = '\0';
434 req_msg.thumb_data = (unsigned char *) "\0";
436 req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
437 req_msg.dest_path_size = strlen(req_msg.dst_path) + 1;
438 req_msg.thumb_size = 1;
440 if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
441 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
442 ms_ipc_delete_client_socket(&sock_info);
443 return MS_MEDIA_ERR_INVALID_PARAMETER;
446 unsigned char *buf = NULL;
450 header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
451 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
453 if (send(sock, buf, buf_size, 0) != buf_size) {
454 thumb_err("sendto failed: %d", errno);
456 ms_ipc_delete_client_socket(&sock_info);
457 return MS_MEDIA_ERR_SOCKET_SEND;
460 thumb_dbg("Sending msg to thumbnail daemon is successful");
464 if(msg_type != THUMB_REQUEST_CANCEL_ALL_RAW_DATA && msg_type != THUMB_REQUEST_CANCEL_ALL) { //No response..
465 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
466 thumb_err("_media_thumb_recv_msg failed ");
467 ms_ipc_delete_client_socket(&sock_info);
471 recv_str_len = strlen(recv_msg.org_path);
472 thumb_dbg_slog("recv %s(%d) from thumb daemon is successful", recv_msg.org_path, recv_str_len);
474 ms_ipc_delete_client_socket(&sock_info);
476 if (recv_str_len > max_length) {
477 thumb_err("user buffer is too small. Output's length is %d", recv_str_len);
478 return MS_MEDIA_ERR_INVALID_PARAMETER;
481 if (recv_msg.status == THUMB_FAIL) {
482 thumb_err("Failed to make thumbnail");
483 return MS_MEDIA_ERR_INVALID_PARAMETER;
486 if (msg_type != THUMB_REQUEST_SAVE_FILE) {
487 strncpy(thumb_path, recv_msg.dst_path, max_length);
490 thumb_info->origin_width = recv_msg.origin_width;
491 thumb_info->origin_height = recv_msg.origin_height;
493 thumb_dbg("No response msg_type:[%d]", msg_type);
495 return MS_MEDIA_ERR_NONE;
498 gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
503 int err = MS_MEDIA_ERR_NONE;
505 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
506 sock = g_io_channel_unix_get_fd(src);
508 header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
510 thumb_err("_media_thumb_write_socket socket : %d", sock);
512 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
513 thumb_err("_media_thumb_recv_msg failed ");
514 if (recv_msg.origin_path_size > 0) {
515 __media_thumb_pop_req_queue(recv_msg.org_path, TRUE);
517 thumb_err("origin path size is wrong.");
523 g_io_channel_shutdown(src, TRUE, NULL);
524 g_io_channel_unref(src);
525 //thumb_dbg("Completed..%s", recv_msg.org_path);
527 if (recv_msg.status == THUMB_FAIL) {
528 thumb_err("Failed to make thumbnail");
529 err = MS_MEDIA_ERR_INTERNAL;
533 thumbUserData* cb = (thumbUserData*)data;
534 if (cb->func != NULL)
535 cb->func(err, recv_msg.dst_path, cb->user_data);
538 __media_thumb_pop_req_queue(recv_msg.org_path, FALSE);
544 gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
549 int err = MS_MEDIA_ERR_NONE;
551 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
552 sock = g_io_channel_unix_get_fd(src);
554 header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
556 thumb_err("_media_thumb_write_socket socket : %d", sock);
558 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
559 thumb_err("_media_thumb_recv_msg failed ");
560 if (recv_msg.request_id > 0) {
561 __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, TRUE);
563 thumb_err("origin path size is wrong.");
569 g_io_channel_shutdown(src, TRUE, NULL);
570 g_io_channel_unref(src);
572 if (recv_msg.status == THUMB_FAIL) {
573 thumb_err("Failed to make thumbnail");
574 err = MS_MEDIA_ERR_INTERNAL;
578 thumbRawUserData* cb = (thumbRawUserData*)data;
579 if (cb->func != NULL)
580 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);
583 __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, FALSE);
587 SAFE_FREE(recv_msg.thumb_data);
592 int _media_thumb_request_async(int msg_type, media_thumb_type thumb_type, const char *origin_path, thumbUserData *userData, uid_t uid)
594 int err = MS_MEDIA_ERR_NONE;
596 struct sockaddr_un serv_addr;
597 ms_sock_info_s sock_info;
599 sock_info.port = MS_THUMB_CREATOR_PORT;
601 if ((msg_type == THUMB_REQUEST_DB_INSERT) && (__media_thumb_check_req_queue(origin_path) < 0)) {
602 return MS_MEDIA_ERR_THUMB_DUPLICATED_REQUEST;
605 err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
606 if(err != MS_MEDIA_ERR_NONE)
608 thumb_err("ms_ipc_create_client_socket failed");
612 memset(&serv_addr, 0, sizeof(serv_addr));
613 sock = sock_info.sock_fd;
614 serv_addr.sun_family = AF_UNIX;
615 strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcreator.socket");
617 GIOChannel *channel = NULL;
618 channel = g_io_channel_unix_new(sock);
621 /* Connecting to the thumbnail server */
622 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
623 thumb_stderror("connect");
624 g_io_channel_shutdown(channel, TRUE, NULL);
625 g_io_channel_unref(channel);
626 return MS_MEDIA_ERR_SOCKET_CONN;
629 if (msg_type != THUMB_REQUEST_CANCEL_MEDIA) {
630 //source_id = g_io_add_watch(channel, G_IO_IN, _media_thumb_write_socket, userData );
632 /* Create new channel to watch udp socket */
633 GSource *source = NULL;
634 source = g_io_create_watch(channel, G_IO_IN);
636 /* Set callback to be called when socket is readable */
637 g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, userData, NULL);
638 source_id = g_source_attach(source, g_main_context_get_thread_default());
642 memset((void *)&req_msg, 0, sizeof(thumbMsg));
646 req_msg.msg_type = msg_type;
647 req_msg.thumb_type = thumb_type;
650 strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
651 req_msg.org_path[strlen(req_msg.org_path)] = '\0';
652 req_msg.dst_path[0] = '\0';
653 req_msg.thumb_data = (unsigned char *)"\0";
655 req_msg.origin_path_size = strlen(req_msg.org_path);
656 req_msg.dest_path_size = 1;
657 req_msg.thumb_size = 1;
659 if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
660 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
661 g_io_channel_shutdown(channel, TRUE, NULL);
662 g_io_channel_unref(channel);
663 ms_ipc_delete_client_socket(&sock_info);
664 return MS_MEDIA_ERR_INVALID_PARAMETER;
667 unsigned char *buf = NULL;
669 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
671 if (send(sock, buf, buf_size, 0) != buf_size) {
672 thumb_err("sendto failed: %d", errno);
674 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
675 g_io_channel_shutdown(channel, TRUE, NULL);
676 g_io_channel_unref(channel);
677 ms_ipc_delete_client_socket(&sock_info);
678 return MS_MEDIA_ERR_SOCKET_SEND;
682 thumb_dbg("Sending msg to thumbnail daemon is successful");
684 if (msg_type == THUMB_REQUEST_CANCEL_MEDIA) {
685 g_io_channel_shutdown(channel, TRUE, NULL);
687 //thumb_dbg("Cancel : %s[%d]", origin_path, sock);
688 __media_thumb_pop_req_queue(origin_path, TRUE);
689 } else if (msg_type == THUMB_REQUEST_DB_INSERT) {
690 if (g_request_queue == NULL) {
691 g_request_queue = g_queue_new();
694 thumbReq *thumb_req = NULL;
695 thumb_req = calloc(1, sizeof(thumbReq));
696 if (thumb_req == NULL) {
697 thumb_err("Failed to create request element");
698 return MS_MEDIA_ERR_INVALID_PARAMETER;
701 thumb_req->channel = channel;
702 thumb_req->path = strdup(origin_path);
703 thumb_req->source_id = source_id;
704 thumb_req->userData = userData;
706 g_queue_push_tail(g_request_queue, (gpointer)thumb_req);
709 return MS_MEDIA_ERR_NONE;
712 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)
714 int err = MS_MEDIA_ERR_NONE;
716 struct sockaddr_un serv_addr;
717 ms_sock_info_s sock_info;
719 sock_info.port = MS_THUMB_CREATOR_PORT;
721 err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
722 if(err != MS_MEDIA_ERR_NONE)
724 thumb_err("ms_ipc_create_client_socket failed");
728 memset(&serv_addr, 0, sizeof(serv_addr));
729 sock = sock_info.sock_fd;
730 serv_addr.sun_family = AF_UNIX;
731 strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcreator.socket");
733 GIOChannel *channel = NULL;
734 channel = g_io_channel_unix_new(sock);
737 /* Connecting to the thumbnail server */
738 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
739 thumb_err("connect error : %s", strerror(errno));
740 g_io_channel_shutdown(channel, TRUE, NULL);
741 g_io_channel_unref(channel);
742 return MS_MEDIA_ERR_SOCKET_CONN;
745 if (msg_type != THUMB_REQUEST_CANCEL_RAW_DATA) {
746 /* Create new channel to watch udp socket */
747 GSource *source = NULL;
748 source = g_io_create_watch(channel, G_IO_IN);
750 /* Set callback to be called when socket is readable */
751 /*NEED UPDATE SOCKET FILE DELETE*/
752 g_source_set_callback(source, (GSourceFunc)_media_thumb_raw_data_write_socket, userData, NULL);
753 source_id = g_source_attach(source, g_main_context_get_thread_default());
757 memset((void *)&req_msg, 0, sizeof(thumbMsg));
761 req_msg.msg_type = msg_type;
762 req_msg.thumb_type = MEDIA_THUMB_LARGE;
763 req_msg.request_id = request_id;
764 req_msg.thumb_width = width;
765 req_msg.thumb_height = height;
768 strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
769 req_msg.org_path[strlen(req_msg.org_path)] = '\0';
770 req_msg.dst_path[0] = '\0';
771 req_msg.thumb_data = (unsigned char *)"\0";
773 req_msg.origin_path_size = strlen(req_msg.org_path);
774 req_msg.dest_path_size = 1;
775 req_msg.thumb_size = 1;
777 if (req_msg.origin_path_size > MAX_PATH_SIZE) {
778 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
779 g_io_channel_shutdown(channel, TRUE, NULL);
780 g_io_channel_unref(channel);
781 return MS_MEDIA_ERR_INVALID_PARAMETER;
784 unsigned char *buf = NULL;
786 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
788 if (send(sock, buf, buf_size, 0) != buf_size) {
789 thumb_err("sendto failed: %d", errno);
791 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
792 g_io_channel_shutdown(channel, TRUE, NULL);
793 g_io_channel_unref(channel);
794 return MS_MEDIA_ERR_SOCKET_SEND;
798 thumb_dbg("Sending msg to thumbnail daemon is successful");
800 if (msg_type == THUMB_REQUEST_CANCEL_RAW_DATA) {
801 g_io_channel_shutdown(channel, TRUE, NULL);
802 __media_thumb_pop_raw_data_req_queue(request_id, TRUE);
803 } else if (msg_type == THUMB_REQUEST_RAW_DATA) {
804 if (g_request_queue == NULL) {
805 g_request_queue = g_queue_new();
807 thumbRawReq *thumb_req = NULL;
808 thumb_req = calloc(1, sizeof(thumbReq));
809 if (thumb_req == NULL) {
810 thumb_err("Failed to create request element");
811 return MS_MEDIA_ERR_INVALID_PARAMETER;
813 thumb_req->channel = channel;
814 thumb_req->request_id = request_id;
815 thumb_req->source_id = source_id;
816 thumb_req->userData = userData;
818 g_queue_push_tail(g_request_queue, (gpointer)thumb_req);
820 return MS_MEDIA_ERR_NONE;