Change from INET tcp to Unix domain socket
[platform/core/multimedia/libmedia-thumbnail.git] / src / ipc / media-thumb-ipc.c
1 /*
2  * libmedia-thumbnail
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@samsung.com>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
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 <glib.h>
27 #include <fcntl.h>
28 #include <string.h>
29 #include <errno.h>
30
31 static __thread GQueue *g_request_queue = NULL;
32 typedef struct {
33         GIOChannel *channel;
34         char *path;
35         int source_id;
36 } thumbReq;
37
38 int
39 _media_thumb_create_socket(int sock_type, int *sock)
40 {
41         int sock_fd = 0;
42
43         if ((sock_fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
44                 thumb_err("socket failed: %s", strerror(errno));
45                 return MEDIA_THUMB_ERROR_NETWORK;
46         }
47
48         if (sock_type == CLIENT_SOCKET) {
49
50 #ifdef _USE_MEDIA_UTIL_
51                 struct timeval tv_timeout = { MS_TIMEOUT_SEC_10, 0 };
52 #else
53                 struct timeval tv_timeout = { TIMEOUT_SEC, 0 };
54 #endif
55
56                 if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &tv_timeout, sizeof(tv_timeout)) == -1) {
57                         thumb_err("setsockopt failed: %s", strerror(errno));
58                         close(sock_fd);
59                         return MEDIA_THUMB_ERROR_NETWORK;
60                 }
61         } else if (sock_type == SERVER_SOCKET) {
62
63                 int n_reuse = 1;
64
65                 if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &n_reuse, sizeof(n_reuse)) == -1) {
66                         thumb_err("setsockopt failed: %s", strerror(errno));
67                         close(sock_fd);
68                         return MEDIA_THUMB_ERROR_NETWORK;
69                 }
70         }
71
72         *sock = sock_fd;
73
74         return MEDIA_THUMB_ERROR_NONE;
75 }
76
77
78 int
79 _media_thumb_create_udp_socket(int *sock)
80 {
81         int sock_fd = 0;
82
83         if ((sock_fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
84                 thumb_err("socket failed: %s", strerror(errno));
85                 return MEDIA_THUMB_ERROR_NETWORK;
86         }
87
88 #ifdef _USE_MEDIA_UTIL_
89         struct timeval tv_timeout = { MS_TIMEOUT_SEC_10, 0 };
90 #else
91         struct timeval tv_timeout = { TIMEOUT_SEC, 0 };
92 #endif
93
94         if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &tv_timeout, sizeof(tv_timeout)) == -1) {
95                 thumb_err("setsockopt failed: %s", strerror(errno));
96                 close(sock_fd);
97                 return MEDIA_THUMB_ERROR_NETWORK;
98         }
99
100         *sock = sock_fd;
101
102         return MEDIA_THUMB_ERROR_NONE;
103 }
104
105 int _media_thumb_get_error()
106 {
107         if (errno == EWOULDBLOCK) {
108                 thumb_err("Timeout. Can't try any more");
109                 return MEDIA_THUMB_ERROR_TIMEOUT;
110         } else {
111                 thumb_err("recvfrom failed : %s", strerror(errno));
112                 return MEDIA_THUMB_ERROR_NETWORK;
113         }
114 }
115
116 int __media_thumb_pop_req_queue(const char *path, bool shutdown_channel)
117 {
118         int req_len = 0, i;
119
120         if (g_request_queue == NULL) return -1;
121         req_len = g_queue_get_length(g_request_queue);
122
123 //      thumb_dbg("Queue length : %d", req_len);
124 //      thumb_dbg("Queue path : %s", path);
125
126         if (req_len <= 0) {
127 //              thumb_dbg("There is no request in the queue");
128         } else {
129
130                 for (i = 0; i < req_len; i++) {
131                         thumbReq *req = NULL;
132                         req = (thumbReq *)g_queue_peek_nth(g_request_queue, i);
133                         if (req == NULL) continue;
134
135                         if (strncmp(path, req->path, strlen(path)) == 0) {
136                                 //thumb_dbg("Popped %s", path);
137                                 if (shutdown_channel) {
138                                         g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id));
139
140                                         g_io_channel_shutdown(req->channel, TRUE, NULL);
141                                         g_io_channel_unref(req->channel);
142                                 }
143                                 g_queue_pop_nth(g_request_queue, i);
144
145                                 SAFE_FREE(req->path);
146                                 SAFE_FREE(req);
147
148                                 break;
149                         }
150                 }
151                 if (i == req_len) {
152 //                      thumb_dbg("There's no %s in the queue", path);
153                 }
154         }
155
156         return MEDIA_THUMB_ERROR_NONE;
157 }
158
159 int __media_thumb_check_req_queue(const char *path)
160 {
161         int req_len = 0, i;
162
163         req_len = g_queue_get_length(g_request_queue);
164
165 //      thumb_dbg("Queue length : %d", req_len);
166 //      thumb_dbg("Queue path : %s", path);
167
168         if (req_len <= 0) {
169 //              thumb_dbg("There is no request in the queue");
170         } else {
171
172                 for (i = 0; i < req_len; i++) {
173                         thumbReq *req = NULL;
174                         req = (thumbReq *)g_queue_peek_nth(g_request_queue, i);
175                         if (req == NULL) continue;
176
177                         if (strncmp(path, req->path, strlen(path)) == 0) {
178                                 //thumb_dbg("Same Request - %s", path);
179                                 return -1;
180
181                                 break;
182                         }
183                 }
184         }
185
186         return MEDIA_THUMB_ERROR_NONE;
187 }
188
189 int
190 _media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg)
191 {
192         int recv_msg_len = 0;
193         unsigned char *buf = NULL;
194
195         buf = (unsigned char*)malloc(header_size);
196
197         if ((recv_msg_len = recv(sock, buf, header_size, 0)) <= 0) {
198                 thumb_err("recvfrom failed : %s", strerror(errno));
199                 SAFE_FREE(buf);
200                 return _media_thumb_get_error();
201         }
202
203         memcpy(msg, buf, header_size);
204         //thumb_dbg("origin_path_size : %d, dest_path_size : %d", msg->origin_path_size, msg->dest_path_size);
205
206         SAFE_FREE(buf);
207
208         buf = (unsigned char*)malloc(msg->origin_path_size);
209
210         if ((recv_msg_len = recv(sock, buf, msg->origin_path_size, 0)) < 0) {
211                 thumb_err("recvfrom failed : %s", strerror(errno));
212                 SAFE_FREE(buf);
213                 return _media_thumb_get_error();
214         }
215
216         strncpy(msg->org_path, (char*)buf, msg->origin_path_size);
217         //thumb_dbg("original path : %s", msg->org_path);
218
219         SAFE_FREE(buf);
220         buf = (unsigned char*)malloc(msg->dest_path_size);
221
222         if ((recv_msg_len = recv(sock, buf, msg->dest_path_size, 0)) < 0) {
223                 thumb_err("recvfrom failed : %s", strerror(errno));
224                 SAFE_FREE(buf);
225                 return _media_thumb_get_error();
226         }
227
228         strncpy(msg->dst_path, (char*)buf, msg->dest_path_size);
229         //thumb_dbg("destination path : %s", msg->dst_path);
230
231         SAFE_FREE(buf);
232         return MEDIA_THUMB_ERROR_NONE;
233 }
234
235 #ifdef _USE_UDS_SOCKET_
236 int
237 _media_thumb_recv_udp_msg(int sock, int header_size, thumbMsg *msg, struct sockaddr_un *from_addr, unsigned int *from_size)
238 #else
239 int
240 _media_thumb_recv_udp_msg(int sock, int header_size, thumbMsg *msg, struct sockaddr_in *from_addr, unsigned int *from_size)
241 #endif
242 {
243         int recv_msg_len = 0;
244 #ifdef _USE_UDS_SOCKET_
245         unsigned int from_addr_size = sizeof(struct sockaddr_un);
246 #else
247         unsigned int from_addr_size = sizeof(struct sockaddr_in);
248 #endif
249         unsigned char *buf = NULL;
250
251         buf = (unsigned char*)malloc(sizeof(thumbMsg));
252
253         if ((recv_msg_len = recvfrom(sock, buf, sizeof(thumbMsg), 0, (struct sockaddr *)from_addr, &from_addr_size)) < 0) {
254                 thumb_err("recvfrom failed : %s", strerror(errno));
255                 SAFE_FREE(buf);
256                 return _media_thumb_get_error();
257         }
258
259         memcpy(msg, buf, header_size);
260
261         if (msg->origin_path_size <= 0  || msg->origin_path_size > MAX_PATH_SIZE) {
262                 SAFE_FREE(buf);
263                 thumb_err("msg->origin_path_size is invalid %d", msg->origin_path_size );
264                 return MEDIA_THUMB_ERROR_NETWORK;
265         }
266
267         strncpy(msg->org_path, (char*)buf + header_size, msg->origin_path_size);
268         //thumb_dbg("original path : %s", msg->org_path);
269
270         if (msg->dest_path_size <= 0  || msg->dest_path_size > MAX_PATH_SIZE) {
271                 SAFE_FREE(buf);
272                 thumb_err("msg->origin_path_size is invalid %d", msg->dest_path_size );
273                 return MEDIA_THUMB_ERROR_NETWORK;
274         }
275
276         strncpy(msg->dst_path, (char*)buf + header_size + msg->origin_path_size, msg->dest_path_size);
277         //thumb_dbg("destination path : %s", msg->dst_path);
278
279         SAFE_FREE(buf);
280         *from_size = from_addr_size;
281
282         return MEDIA_THUMB_ERROR_NONE;
283 }
284
285 int
286 _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
287 {
288         if (req_msg == NULL || buf == NULL) {
289                 return -1;
290         }
291
292         int org_path_len = 0;
293         int dst_path_len = 0;
294         int size = 0;
295         int header_size = 0;
296
297         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2;
298         org_path_len = strlen(req_msg->org_path) + 1;
299         dst_path_len = strlen(req_msg->dst_path) + 1;
300
301         //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);
302
303         size = header_size + org_path_len + dst_path_len;
304         *buf = malloc(size);
305         memcpy(*buf, req_msg, header_size);
306         memcpy((*buf)+header_size, req_msg->org_path, org_path_len);
307         memcpy((*buf)+header_size + org_path_len, req_msg->dst_path, dst_path_len);
308
309         *buf_size = size;
310
311         return 0;
312 }
313
314 int
315 _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)
316 {
317         int sock;
318 #ifdef _USE_UDS_SOCKET_
319         struct sockaddr_un serv_addr;
320 #elif defined(_USE_UDS_SOCKET_TCP_)
321         struct sockaddr_un serv_addr;
322 #else
323         const char *serv_ip = "127.0.0.1";
324         struct sockaddr_in serv_addr;
325 #endif
326
327         int recv_str_len = 0;
328         int err;
329         int pid;
330
331
332 #ifdef _USE_MEDIA_UTIL_
333 #ifdef _USE_UDS_SOCKET_
334         if (ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock, MS_THUMB_CREATOR_PORT) < 0) {
335 #elif defined(_USE_UDS_SOCKET_TCP_)
336         if (ms_ipc_create_client_tcp_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock, MS_THUMB_CREATOR_TCP_PORT) < 0) {
337 #else
338         if (ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock) < 0) {
339 #endif
340                 thumb_err("ms_ipc_create_client_socket failed");
341                 return MEDIA_THUMB_ERROR_NETWORK;
342         }
343 #else
344         /* Creaete a TCP socket */
345         if (_media_thumb_create_socket(CLIENT_SOCKET, &sock) < 0) {
346                 thumb_err("_media_thumb_create_socket failed");
347                 return MEDIA_THUMB_ERROR_NETWORK;
348         }
349 #endif
350
351         memset(&serv_addr, 0, sizeof(serv_addr));
352 #ifdef _USE_UDS_SOCKET_
353         serv_addr.sun_family = AF_UNIX;
354 #elif defined(_USE_UDS_SOCKET_TCP_)
355         serv_addr.sun_family = AF_UNIX;
356 #else
357         serv_addr.sin_family = AF_INET;
358         serv_addr.sin_addr.s_addr = inet_addr(serv_ip);
359 #endif
360
361 #ifdef _USE_MEDIA_UTIL_
362 #ifdef _USE_UDS_SOCKET_
363         strcpy(serv_addr.sun_path, "/tmp/media_ipc_thumbcreator.dat");
364 #elif defined(_USE_UDS_SOCKET_TCP_)
365         thumb_dbg("");
366         strcpy(serv_addr.sun_path, "/tmp/media_ipc_thumbcreator.dat");
367 #else
368         serv_addr.sin_port = htons(MS_THUMB_CREATOR_PORT);
369 #endif
370 #else
371         serv_addr.sin_port = htons(THUMB_DAEMON_PORT);
372 #endif
373
374         /* Connecting to the thumbnail server */
375         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
376                 thumb_err("connect error : %s", strerror(errno));
377                 return MEDIA_THUMB_ERROR_NETWORK;
378         }
379
380         thumbMsg req_msg;
381         thumbMsg recv_msg;
382
383         memset((void *)&req_msg, 0, sizeof(thumbMsg));
384         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
385
386         /* Get PID of client*/
387         pid = getpid();
388         req_msg.pid = pid;
389
390         /* Set requset message */
391         req_msg.msg_type = msg_type;
392         req_msg.thumb_type = thumb_type;
393         strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
394         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
395
396         if (msg_type == THUMB_REQUEST_SAVE_FILE) {
397                 strncpy(req_msg.dst_path, thumb_path, sizeof(req_msg.dst_path));
398                 req_msg.dst_path[strlen(req_msg.dst_path)] = '\0';
399         }
400
401         req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
402         req_msg.dest_path_size = strlen(req_msg.dst_path) + 1;
403
404         if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
405                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
406                 close(sock);
407                 return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
408         }
409
410         unsigned char *buf = NULL;
411         int buf_size = 0;
412         int header_size = 0;
413
414         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2;
415         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
416
417         if (send(sock, buf, buf_size, 0) != buf_size) {
418                 thumb_err("sendto failed: %d\n", errno);
419                 SAFE_FREE(buf);
420                 close(sock);
421                 return MEDIA_THUMB_ERROR_NETWORK;
422         }
423
424         thumb_dbg("Sending msg to thumbnail daemon is successful");
425
426         SAFE_FREE(buf);
427
428         if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
429                 thumb_err("_media_thumb_recv_msg failed ");
430                 close(sock);
431                 return err;
432         }
433
434         recv_str_len = strlen(recv_msg.org_path);
435         thumb_dbg("recv %s(%d) from thumb daemon is successful", recv_msg.org_path, recv_str_len);
436
437         close(sock);
438
439         if (recv_str_len > max_length) {
440                 thumb_err("user buffer is too small. Output's length is %d", recv_str_len);
441                 return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
442         }
443
444         if (recv_msg.status == THUMB_FAIL) {
445                 thumb_err("Failed to make thumbnail");
446                 return -1;
447         }
448
449         if (msg_type != THUMB_REQUEST_SAVE_FILE) {
450                 strncpy(thumb_path, recv_msg.dst_path, max_length);
451         }
452
453         thumb_info->origin_width = recv_msg.origin_width;
454         thumb_info->origin_height = recv_msg.origin_height;
455
456         return 0;
457 }
458
459 int
460 _media_thumb_process(thumbMsg *req_msg, thumbMsg *res_msg)
461 {
462         int err = -1;
463         unsigned char *data = NULL;
464         int thumb_size = 0;
465         int thumb_w = 0;
466         int thumb_h = 0;
467         int origin_w = 0;
468         int origin_h = 0;
469         int max_length = 0;
470         char *thumb_path = NULL;
471         int need_update_db = 0;
472         int alpha = 0;
473
474         if (req_msg == NULL || res_msg == NULL) {
475                 thumb_err("Invalid msg!");
476                 return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
477         }
478
479         int msg_type = req_msg->msg_type;
480         media_thumb_type thumb_type = req_msg->thumb_type;
481         const char *origin_path = req_msg->org_path;
482
483         media_thumb_format thumb_format = MEDIA_THUMB_BGRA;
484
485         thumb_path = res_msg->dst_path;
486         thumb_path[0] = '\0';
487         max_length = sizeof(res_msg->dst_path);
488
489         err = _media_thumb_db_connect();
490         if (err < 0) {
491                 thumb_err("_media_thumb_mb_svc_connect failed: %d", err);
492                 return MEDIA_THUMB_ERROR_DB;
493         }
494
495         if (msg_type == THUMB_REQUEST_DB_INSERT) {
496                 err = _media_thumb_get_thumb_from_db_with_size(origin_path, thumb_path, max_length, &need_update_db, &origin_w, &origin_h);
497                 if (err == 0) {
498                         res_msg->origin_width = origin_w;
499                         res_msg->origin_height = origin_h;
500                         _media_thumb_db_disconnect();
501                         return MEDIA_THUMB_ERROR_NONE;
502                 } else {
503                         if (strlen(thumb_path) == 0) {
504                                 err = _media_thumb_get_hash_name(origin_path, thumb_path, max_length);
505                                 if (err < 0) {
506                                         thumb_err("_media_thumb_get_hash_name failed - %d\n", err);
507                                         strncpy(thumb_path, THUMB_DEFAULT_PATH, max_length);
508                                         _media_thumb_db_disconnect();
509                                         return err;
510                                 }
511
512                                 thumb_path[strlen(thumb_path)] = '\0';
513                         }
514                 }
515
516         } else if (msg_type == THUMB_REQUEST_SAVE_FILE) {
517                 strncpy(thumb_path, req_msg->dst_path, max_length);
518
519         } else if (msg_type == THUMB_REQUEST_ALL_MEDIA) {
520                 err = _media_thumb_get_hash_name(origin_path, thumb_path, max_length);
521                 if (err < 0) {
522                         thumb_err("_media_thumb_get_hash_name failed - %d\n", err);
523                         strncpy(thumb_path, THUMB_DEFAULT_PATH, max_length);
524                         _media_thumb_db_disconnect();
525                         return err;
526                 }
527
528                 thumb_path[strlen(thumb_path)] = '\0';
529         }
530
531         thumb_dbg("Thumb path : %s", thumb_path);
532
533         if (g_file_test(thumb_path, 
534                                         G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
535                 thumb_warn("thumb path already exists in file system.. remove the existed file");
536                 _media_thumb_remove_file(thumb_path);
537         }
538
539         err = _thumbnail_get_data(origin_path, thumb_type, thumb_format, &data, &thumb_size, &thumb_w, &thumb_h, &origin_w, &origin_h, &alpha);
540         if (err < 0) {
541                 thumb_err("_thumbnail_get_data failed - %d\n", err);
542                 SAFE_FREE(data);
543
544                 strncpy(thumb_path, THUMB_DEFAULT_PATH, max_length);
545                 _media_thumb_db_disconnect();
546                 return err;
547         }
548
549         //thumb_dbg("Size : %d, W:%d, H:%d", thumb_size, thumb_w, thumb_h);
550         //thumb_dbg("Origin W:%d, Origin H:%d\n", origin_w, origin_h);
551         //thumb_dbg("Thumb : %s", thumb_path);
552
553         res_msg->msg_type = THUMB_RESPONSE;
554         res_msg->thumb_size = thumb_size;
555         res_msg->thumb_width = thumb_w;
556         res_msg->thumb_height = thumb_h;
557         res_msg->origin_width = origin_w;
558         res_msg->origin_height = origin_h;
559
560         /* If the image is transparent PNG format, make png file as thumbnail of this image */
561         if (alpha) {
562                 char file_ext[10];
563                 err = _media_thumb_get_file_ext(origin_path, file_ext, sizeof(file_ext));
564                 if (strncasecmp(file_ext, "png", 3) == 0) {
565                         int len = strlen(thumb_path);
566                         thumb_path[len - 3] = 'p';
567                         thumb_path[len - 2] = 'n';
568                         thumb_path[len - 1] = 'g';
569                 }
570                 thumb_dbg("Thumb path is changed : %s", thumb_path);
571         }
572
573         err = _media_thumb_save_to_file_with_evas(data, thumb_w, thumb_h, alpha, thumb_path);
574         if (err < 0) {
575                 thumb_err("save_to_file_with_evas failed - %d\n", err);
576                 SAFE_FREE(data);
577
578                 if (msg_type == THUMB_REQUEST_DB_INSERT || msg_type == THUMB_REQUEST_ALL_MEDIA)
579                         strncpy(thumb_path, THUMB_DEFAULT_PATH, max_length);
580
581                 _media_thumb_db_disconnect();
582                 return err;
583         } else {
584                 thumb_dbg("file save success\n");
585         }
586
587         /* fsync */
588         int fd = 0;
589         fd = open(thumb_path, O_WRONLY);
590         if (fd < 0) {
591                 thumb_warn("open failed");
592         } else {
593                 err = fsync(fd);
594                 if (err == -1) {
595                         thumb_warn("fsync failed");
596                 }
597
598                 close(fd);
599         }
600         /* End of fsync */
601
602         SAFE_FREE(data);
603
604         /* DB update if needed */
605         if (need_update_db == 1) {
606                 err = _media_thumb_update_db(origin_path, thumb_path, res_msg->origin_width, res_msg->origin_height);
607                 if (err < 0) {
608                         thumb_err("_media_thumb_update_db failed : %d", err);
609                 }
610         }
611
612         _media_thumb_db_disconnect();
613
614         return 0;
615 }
616
617 gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
618 {
619         thumbMsg recv_msg;
620         int header_size = 0;
621         int recv_str_len = 0;
622         int sock = 0;
623         int err = MEDIA_THUMB_ERROR_NONE;
624
625         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
626         sock = g_io_channel_unix_get_fd(src);
627
628         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2;
629
630         thumb_err("_media_thumb_write_socket socket : %d", sock);
631
632         if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
633                 thumb_err("_media_thumb_recv_msg failed ");
634                 g_io_channel_shutdown(src, TRUE, NULL);
635                 return FALSE;
636         }
637
638         recv_str_len = strlen(recv_msg.dst_path);
639         thumb_dbg("recv %s(%d) in  [ %s ] from thumb daemon is successful", recv_msg.dst_path, recv_str_len, recv_msg.org_path);
640
641         g_io_channel_shutdown(src, TRUE, NULL);
642
643         //thumb_dbg("Completed..%s", recv_msg.org_path);
644         __media_thumb_pop_req_queue(recv_msg.org_path, FALSE);
645
646         if (recv_msg.status == THUMB_FAIL) {
647                 thumb_err("Failed to make thumbnail");
648                 err = MEDIA_THUMB_ERROR_UNSUPPORTED;
649                 goto callback;
650         }
651
652 callback:
653         if (data) {
654                 thumbUserData* cb = (thumbUserData*)data;
655                 cb->func(err, recv_msg.dst_path, cb->user_data);
656                 free(cb);
657                 cb = NULL;
658         }
659
660         thumb_dbg("Done");
661         return FALSE;
662 }
663
664 int
665 _media_thumb_request_async(int msg_type, media_thumb_type thumb_type, const char *origin_path, thumbUserData *userData)
666 {
667         int sock;
668 #ifdef _USE_UDS_SOCKET_
669         struct sockaddr_un serv_addr;
670 #elif defined(_USE_UDS_SOCKET_TCP_)
671         struct sockaddr_un serv_addr;
672 #else
673         const char *serv_ip = "127.0.0.1";
674         struct sockaddr_in serv_addr;
675 #endif
676
677         int pid;
678
679         if ((msg_type == THUMB_REQUEST_DB_INSERT) && (__media_thumb_check_req_queue(origin_path) < 0)) {
680                 return MEDIA_THUMB_ERROR_DUPLICATED_REQUEST;
681         }
682
683 #ifdef _USE_MEDIA_UTIL_
684 #ifdef _USE_UDS_SOCKET_
685         if (ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock, MS_THUMB_CREATOR_PORT) < 0) {
686 #elif defined(_USE_UDS_SOCKET_TCP_)
687         if (ms_ipc_create_client_tcp_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock, MS_THUMB_CREATOR_TCP_PORT) < 0) {
688 #else
689         if (ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock) < 0) {
690 #endif
691                 thumb_err("ms_ipc_create_client_socket failed");
692                 return MEDIA_THUMB_ERROR_NETWORK;
693         }
694 #else
695         /* Creaete a TCP socket */
696         if (_media_thumb_create_socket(CLIENT_SOCKET, &sock) < 0) {
697                 thumb_err("_media_thumb_create_socket failed");
698                 return MEDIA_THUMB_ERROR_NETWORK;
699         }
700 #endif
701
702         GIOChannel *channel = NULL;
703         channel = g_io_channel_unix_new(sock);
704         int source_id = -1;
705
706
707         memset(&serv_addr, 0, sizeof(serv_addr));
708 #ifdef _USE_UDS_SOCKET_
709         serv_addr.sun_family = AF_UNIX;
710 #elif defined(_USE_UDS_SOCKET_TCP_)
711         serv_addr.sun_family = AF_UNIX;
712 #else
713         serv_addr.sin_family = AF_INET;
714         serv_addr.sin_addr.s_addr = inet_addr(serv_ip);
715 #endif
716
717 #ifdef _USE_MEDIA_UTIL_
718 #ifdef _USE_UDS_SOCKET_
719         strcpy(serv_addr.sun_path, "/tmp/media_ipc_thumbcreator.dat");
720 #elif defined(_USE_UDS_SOCKET_TCP_)
721         strcpy(serv_addr.sun_path, "/tmp/media_ipc_thumbcreator.dat");
722 #else
723         serv_addr.sin_port = htons(MS_THUMB_CREATOR_PORT);
724 #endif
725 #else
726         serv_addr.sin_port = htons(THUMB_DAEMON_PORT);
727 #endif
728
729         /* Connecting to the thumbnail server */
730         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
731                 thumb_err("connect error : %s", strerror(errno));
732                 g_io_channel_shutdown(channel, TRUE, NULL);
733                 return MEDIA_THUMB_ERROR_NETWORK;
734         }
735
736         if (msg_type != THUMB_REQUEST_CANCEL_MEDIA) {
737                 //source_id = g_io_add_watch(channel, G_IO_IN, _media_thumb_write_socket, userData );
738
739                 /* Create new channel to watch udp socket */
740                 GSource *source = NULL;
741                 source = g_io_create_watch(channel, G_IO_IN);
742
743                 /* Set callback to be called when socket is readable */
744                 g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, userData, NULL);
745                 source_id = g_source_attach(source, g_main_context_get_thread_default());
746         }
747
748         thumbMsg req_msg;
749         memset((void *)&req_msg, 0, sizeof(thumbMsg));
750
751         pid = getpid();
752         req_msg.pid = pid;
753         req_msg.msg_type = msg_type;
754         req_msg.thumb_type = thumb_type;
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
758         req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
759         req_msg.dest_path_size = strlen(req_msg.dst_path) + 1;
760
761         if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
762                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
763                 g_io_channel_shutdown(channel, TRUE, NULL);
764                 return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
765         }
766
767         unsigned char *buf = NULL;
768         int buf_size = 0;
769         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
770
771         //thumb_dbg("buffer size : %d", buf_size);
772
773         if (send(sock, buf, buf_size, 0) != buf_size) {
774                 thumb_err("sendto failed: %d\n", errno);
775                 SAFE_FREE(buf);
776                 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
777                 g_io_channel_shutdown(channel, TRUE, NULL);
778                 return MEDIA_THUMB_ERROR_NETWORK;
779         }
780
781         SAFE_FREE(buf);
782         thumb_dbg("Sending msg to thumbnail daemon is successful");
783
784 #if 0
785         if (msg_type == THUMB_REQUEST_CANCEL_MEDIA) {
786                 g_io_channel_shutdown(channel, TRUE, NULL);
787         }
788 #else
789         if (msg_type == THUMB_REQUEST_CANCEL_MEDIA) {
790                 g_io_channel_shutdown(channel, TRUE, NULL);
791
792                 //thumb_dbg("Cancel : %s[%d]", origin_path, sock);
793                 __media_thumb_pop_req_queue(origin_path, TRUE);
794         } else if (msg_type == THUMB_REQUEST_DB_INSERT) {
795                 if (g_request_queue == NULL) {
796                         g_request_queue = g_queue_new();
797                 }
798
799                 thumbReq *thumb_req = NULL;
800                 thumb_req = calloc(1, sizeof(thumbReq));
801                 if (thumb_req == NULL) {
802                         thumb_err("Failed to create request element");
803                         return 0;
804                 }
805
806                 thumb_req->channel = channel;
807                 thumb_req->path = strdup(origin_path);
808                 thumb_req->source_id = source_id;
809
810                 //thumb_dbg("Push : %s [%d]", origin_path, sock);
811                 g_queue_push_tail(g_request_queue, (gpointer)thumb_req);
812         }
813 #endif
814
815         return 0;
816 }
817