Change gdk-pixbuf to evas.
[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 "media-thumb-debug.h"
27 #include <glib.h>
28 #include <fcntl.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <grp.h>
32 #include <pwd.h>
33
34 #define GLOBAL_USER    0 //#define     tzplatform_getenv(TZ_GLOBAL) //TODO
35 #define THUMB_SOCK_BLOCK_SIZE 512
36
37 static __thread GQueue *g_request_queue = NULL;
38 typedef struct {
39         GIOChannel *channel;
40         char *path;
41         int source_id;
42         thumbUserData *userData;
43 } thumbReq;
44
45 typedef struct {
46         GIOChannel *channel;
47         int request_id;
48         int source_id;
49         thumbRawUserData *userData;
50 } thumbRawReq;
51
52
53
54 int _media_thumb_get_error()
55 {
56         if (errno == EWOULDBLOCK) {
57                 thumb_err("Timeout. Can't try any more");
58                 return MS_MEDIA_ERR_SOCKET_RECEIVE_TIMEOUT;
59         } else {
60                 thumb_err("recvfrom failed : %s", strerror(errno));
61                 return MS_MEDIA_ERR_SOCKET_RECEIVE;
62         }
63 }
64
65 int __media_thumb_pop_req_queue(const char *path, bool shutdown_channel)
66 {
67         int req_len = 0, i;
68
69         if (g_request_queue == NULL) return MS_MEDIA_ERR_INVALID_PARAMETER;
70         req_len = g_queue_get_length(g_request_queue);
71
72         if (req_len <= 0) {
73 //              thumb_dbg("There is no request in the queue");
74         } else {
75
76                 for (i = 0; i < req_len; i++) {
77                         thumbReq *req = NULL;
78                         req = (thumbReq *)g_queue_peek_nth(g_request_queue, i);
79                         if (req == NULL) continue;
80
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);
86                                         } else {
87                                                 thumb_err("G_SOURCE_ID is NULL");
88                                         }
89
90                                         g_io_channel_shutdown(req->channel, TRUE, NULL);
91                                         g_io_channel_unref(req->channel);
92                                 }
93                                 g_queue_pop_nth(g_request_queue, i);
94
95                                 SAFE_FREE(req->path);
96                                 SAFE_FREE(req->userData);
97                                 SAFE_FREE(req);
98
99                                 break;
100                         }
101                 }
102                 if (i == req_len) {
103 //                      thumb_dbg("There's no %s in the queue", path);
104                 }
105         }
106
107         return MS_MEDIA_ERR_NONE;
108 }
109
110 int __media_thumb_pop_raw_data_req_queue(int request_id, bool shutdown_channel)
111 {
112         int req_len = 0, i;
113
114         if (g_request_queue == NULL) return MS_MEDIA_ERR_INVALID_PARAMETER;
115         req_len = g_queue_get_length(g_request_queue);
116
117         if (req_len <= 0) {
118 //              thumb_dbg("There is no request in the queue");
119         } else {
120
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;
125
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);
131                                         } else {
132                                                 thumb_err("G_SOURCE_ID is NULL");
133                                         }
134
135                                         g_io_channel_shutdown(req->channel, TRUE, NULL);
136                                         g_io_channel_unref(req->channel);
137                                 }
138                                 g_queue_pop_nth(g_request_queue, i);
139
140                                 SAFE_FREE(req->userData);
141                                 SAFE_FREE(req);
142
143                                 break;
144                         }
145                 }
146                 if (i == req_len) {
147 //                      thumb_dbg("There's no %s in the queue", path);
148                 }
149         }
150
151         return MS_MEDIA_ERR_NONE;
152 }
153
154 int __media_thumb_check_req_queue(const char *path)
155 {
156         int req_len = 0, i;
157
158         req_len = g_queue_get_length(g_request_queue);
159
160 //      thumb_dbg("Queue length : %d", req_len);
161 //      thumb_dbg("Queue path : %s", path);
162
163         if (req_len <= 0) {
164 //              thumb_dbg("There is no request in the queue");
165         } else {
166
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;
171
172                         if (strncmp(path, req->path, strlen(path)) == 0) {
173                                 //thumb_dbg("Same Request - %s", path);
174                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
175
176                                 break;
177                         }
178                 }
179         }
180
181         return MS_MEDIA_ERR_NONE;
182 }
183
184 int
185 _media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg)
186 {
187         int recv_msg_len = 0;
188         int remain_size = 0;
189         int block_size = THUMB_SOCK_BLOCK_SIZE;
190         int recv_block = 0;
191         unsigned char *buf = NULL;
192         unsigned char *block_buf = NULL;
193
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");
198                 SAFE_FREE(buf);
199                 SAFE_FREE(block_buf);
200                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
201         }
202
203         if ((recv_msg_len = recv(sock, buf, header_size, 0)) <= 0) {
204                 thumb_stderror("recv failed");
205                 SAFE_FREE(buf);
206                 return _media_thumb_get_error();
207         }
208
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);
211
212         SAFE_FREE(buf);
213
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));
217
218         while(remain_size > 0) {
219                 if(remain_size < THUMB_SOCK_BLOCK_SIZE) {
220                         block_size = remain_size;
221                 }
222                 if ((recv_msg_len = recv(sock, block_buf, block_size, 0)) < 0) {
223                         thumb_err("recv failed : %s", strerror(errno));
224                         SAFE_FREE(buf);
225                         SAFE_FREE(block_buf);
226                         return _media_thumb_get_error();
227                 }
228                 memcpy(buf+recv_block, block_buf, block_size);
229                 recv_block += block_size;
230                 remain_size -= block_size;
231         }
232
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);
235
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);
240
241         SAFE_FREE(buf);
242         SAFE_FREE(block_buf);
243
244         return MS_MEDIA_ERR_NONE;
245 }
246
247 int
248 _media_thumb_recv_udp_msg(int sock, int header_size, thumbMsg *msg, struct sockaddr_un *from_addr, unsigned int *from_size)
249 {
250         int recv_msg_len = 0;
251         unsigned int from_addr_size = sizeof(struct sockaddr_un);
252         unsigned char *buf = NULL;
253
254         buf = (unsigned char*)malloc(sizeof(thumbMsg));
255
256         if ((recv_msg_len = recvfrom(sock, buf, sizeof(thumbMsg), 0, (struct sockaddr *)from_addr, &from_addr_size)) < 0) {
257                 thumb_stderror("recvform failed");
258                 SAFE_FREE(buf);
259                 return _media_thumb_get_error();
260         }
261
262         memcpy(msg, buf, header_size);
263
264         if (msg->origin_path_size <= 0  || msg->origin_path_size > MAX_PATH_SIZE) {
265                 SAFE_FREE(buf);
266                 thumb_err("msg->origin_path_size is invalid %d", msg->origin_path_size );
267                 return MS_MEDIA_ERR_INVALID_PARAMETER;
268         }
269
270         strncpy(msg->org_path, (char*)buf + header_size, msg->origin_path_size);
271
272         if (msg->dest_path_size <= 0  || msg->dest_path_size > MAX_PATH_SIZE) {
273                 SAFE_FREE(buf);
274                 thumb_err("msg->origin_path_size is invalid %d", msg->dest_path_size );
275                 return MS_MEDIA_ERR_INVALID_PARAMETER;
276         }
277
278         strncpy(msg->dst_path, (char*)buf + header_size + msg->origin_path_size, msg->dest_path_size);
279
280         SAFE_FREE(buf);
281         *from_size = from_addr_size;
282
283         return MS_MEDIA_ERR_NONE;
284 }
285
286 int
287 _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
288 {
289         if (req_msg == NULL || buf == NULL) {
290                 return MS_MEDIA_ERR_INVALID_PARAMETER;
291         }
292
293         int org_path_len = 0;
294         int dst_path_len = 0;
295         int thumb_data_len = 0;
296         int size = 0;
297         int header_size = 0;
298
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;
303
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);
305
306         size = header_size + org_path_len + dst_path_len + thumb_data_len;
307         *buf = malloc(size);
308
309         if (*buf == NULL) {
310                 *buf_size = 0;
311                 return 0;
312         }
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);
318
319         *buf_size = size;
320
321         return MS_MEDIA_ERR_NONE;
322 }
323
324 int
325 _media_thumb_set_buffer_for_response(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
326 {
327         if (req_msg == NULL || buf == NULL) {
328                 return MS_MEDIA_ERR_INVALID_PARAMETER;
329         }
330
331         int org_path_len = 0;
332         int dst_path_len = 0;
333         int size = 0;
334         int header_size = 0;
335
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;
339
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);
341
342         size = header_size + org_path_len + dst_path_len;
343         *buf = malloc(size);
344         if (*buf == NULL) {
345                 *buf_size = 0;
346                 return 0;
347         }
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);
351
352         *buf_size = size;
353
354         return MS_MEDIA_ERR_NONE;
355 }
356
357
358 int
359 _media_thumb_set_add_raw_data_buffer(thumbRawAddMsg *req_msg, unsigned char **buf, int *buf_size)
360 {
361         if (req_msg == NULL || buf == NULL) {
362                 return MS_MEDIA_ERR_INVALID_PARAMETER;
363         }
364         int thumb_len = 0;
365         int size = 0;
366         int header_size = 0;
367
368         header_size = sizeof(thumbRawAddMsg);
369         thumb_len = req_msg->thumb_size;
370
371         size = header_size + thumb_len;
372         *buf = malloc(size);
373         memcpy(*buf, req_msg, header_size);
374         memcpy((*buf)+header_size, req_msg->thumb_data, thumb_len);
375
376         *buf_size = size;
377
378         return MS_MEDIA_ERR_NONE;
379 }
380
381 int
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)
383 {
384         int sock = -1;
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;
389         int pid;
390         sock_info.port = MS_THUMB_CREATOR_PORT;
391
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");
395                 return err;
396         }
397
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");
402
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;
408         }
409
410         thumbMsg req_msg;
411         thumbMsg recv_msg;
412
413         memset((void *)&req_msg, 0, sizeof(thumbMsg));
414         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
415
416         /* Get PID of client*/
417         pid = getpid();
418         req_msg.pid = pid;
419
420         /* Set requset message */
421         req_msg.msg_type = msg_type;
422         req_msg.thumb_type = thumb_type;
423         req_msg.uid = uid;
424         strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
425         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
426
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';
430         }
431
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;
435
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;
440         }
441
442         unsigned char *buf = NULL;
443         int buf_size = 0;
444         int header_size = 0;
445
446         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
447         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
448
449         if (send(sock, buf, buf_size, 0) != buf_size) {
450                 thumb_err("sendto failed: %d", errno);
451                 SAFE_FREE(buf);
452                 ms_ipc_delete_client_socket(&sock_info);
453                 return MS_MEDIA_ERR_SOCKET_SEND;
454         }
455
456         thumb_dbg("Sending msg to thumbnail daemon is successful");
457
458         SAFE_FREE(buf);
459
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);
464                         return err;
465                 }
466
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);
469
470                 ms_ipc_delete_client_socket(&sock_info);
471
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;
475                 }
476
477                 if (recv_msg.status == THUMB_FAIL) {
478                         thumb_err("Failed to make thumbnail");
479                         return MS_MEDIA_ERR_INVALID_PARAMETER;
480                 }
481
482                 if (msg_type != THUMB_REQUEST_SAVE_FILE) {
483                         strncpy(thumb_path, recv_msg.dst_path, max_length);
484                 }
485
486                 thumb_info->origin_width = recv_msg.origin_width;
487                 thumb_info->origin_height = recv_msg.origin_height;
488         }else {
489                 thumb_dbg("No response msg_type:[%d]", msg_type);
490         }
491         return MS_MEDIA_ERR_NONE;
492 }
493
494 gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
495 {
496         thumbMsg recv_msg;
497         int header_size = 0;
498         int sock = 0;
499         int err = MS_MEDIA_ERR_NONE;
500
501         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
502         sock = g_io_channel_unix_get_fd(src);
503
504         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
505
506         thumb_err("_media_thumb_write_socket socket : %d", sock);
507
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);
512                 } else {
513                         thumb_err("origin path size is wrong.");
514                 }
515
516                 return FALSE;
517         }
518
519         g_io_channel_shutdown(src, TRUE, NULL);
520         g_io_channel_unref(src);
521         //thumb_dbg("Completed..%s", recv_msg.org_path);
522
523         if (recv_msg.status == THUMB_FAIL) {
524                 thumb_err("Failed to make thumbnail");
525                 err = MS_MEDIA_ERR_INTERNAL;
526         }
527
528         if (data) {
529                 thumbUserData* cb = (thumbUserData*)data;
530                 if (cb->func != NULL)
531                         cb->func(err, recv_msg.dst_path, cb->user_data);
532         }
533
534         __media_thumb_pop_req_queue(recv_msg.org_path, FALSE);
535
536         thumb_dbg("Done");
537         return FALSE;
538 }
539
540 gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
541 {
542         thumbMsg recv_msg;
543         int header_size = 0;
544         int sock = 0;
545         int err = MS_MEDIA_ERR_NONE;
546
547         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
548         sock = g_io_channel_unix_get_fd(src);
549
550         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
551
552         thumb_err("_media_thumb_write_socket socket : %d", sock);
553
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);
558                 } else {
559                         thumb_err("origin path size is wrong.");
560                 }
561
562                 return FALSE;
563         }
564
565         g_io_channel_shutdown(src, TRUE, NULL);
566         g_io_channel_unref(src);
567
568         if (recv_msg.status == THUMB_FAIL) {
569                 thumb_err("Failed to make thumbnail");
570                 err = MS_MEDIA_ERR_INTERNAL;
571         }
572
573         if (data) {
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);
577         }
578
579         __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, FALSE);
580
581         thumb_dbg("Done");
582
583         SAFE_FREE(recv_msg.thumb_data);
584
585         return FALSE;
586 }
587
588 int _media_thumb_request_async(int msg_type, media_thumb_type thumb_type, const char *origin_path, thumbUserData *userData, uid_t uid)
589 {
590         int err = MS_MEDIA_ERR_NONE;
591         int sock = -1;
592         struct sockaddr_un serv_addr;
593         ms_sock_info_s sock_info;
594         int pid;
595         sock_info.port = MS_THUMB_CREATOR_PORT;
596
597         if ((msg_type == THUMB_REQUEST_DB_INSERT) && (__media_thumb_check_req_queue(origin_path) < 0)) {
598                 return MS_MEDIA_ERR_THUMB_DUPLICATED_REQUEST;
599         }
600
601         err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
602         if(err != MS_MEDIA_ERR_NONE)
603         {
604                 thumb_err("ms_ipc_create_client_socket failed");
605                 return err;
606         }
607
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");
612
613         GIOChannel *channel = NULL;
614         channel = g_io_channel_unix_new(sock);
615         int source_id = -1;
616
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;
623         }
624
625         if (msg_type != THUMB_REQUEST_CANCEL_MEDIA) {
626                 //source_id = g_io_add_watch(channel, G_IO_IN, _media_thumb_write_socket, userData );
627
628                 /* Create new channel to watch udp socket */
629                 GSource *source = NULL;
630                 source = g_io_create_watch(channel, G_IO_IN);
631
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());
635         }
636
637         thumbMsg req_msg;
638         memset((void *)&req_msg, 0, sizeof(thumbMsg));
639
640         pid = getpid();
641         req_msg.pid = pid;
642         req_msg.msg_type = msg_type;
643         req_msg.thumb_type = thumb_type;
644         req_msg.uid = uid;
645
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;
652
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;
659         }
660
661         unsigned char *buf = NULL;
662         int buf_size = 0;
663         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
664
665         if (send(sock, buf, buf_size, 0) != buf_size) {
666                 thumb_err("sendto failed: %d", errno);
667                 SAFE_FREE(buf);
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;
673         }
674
675         SAFE_FREE(buf);
676         thumb_dbg("Sending msg to thumbnail daemon is successful");
677
678         if (msg_type == THUMB_REQUEST_CANCEL_MEDIA) {
679                 g_io_channel_shutdown(channel, TRUE, NULL);
680
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();
686                 }
687
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;
693                 }
694
695                 thumb_req->channel = channel;
696                 thumb_req->path = strdup(origin_path);
697                 thumb_req->source_id = source_id;
698                 thumb_req->userData = userData;
699
700                 g_queue_push_tail(g_request_queue, (gpointer)thumb_req);
701         }
702
703         return MS_MEDIA_ERR_NONE;
704 }
705
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)
707 {
708         int err = MS_MEDIA_ERR_NONE;
709         int sock = -1;
710         struct sockaddr_un serv_addr;
711         ms_sock_info_s sock_info;
712         int pid;
713         sock_info.port = MS_THUMB_CREATOR_PORT;
714
715         err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
716         if(err != MS_MEDIA_ERR_NONE)
717         {
718                 thumb_err("ms_ipc_create_client_socket failed");
719                 return err;
720         }
721
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");
726
727         GIOChannel *channel = NULL;
728         channel = g_io_channel_unix_new(sock);
729         int source_id = -1;
730
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;
737         }
738
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);
743
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());
748         }
749
750         thumbMsg req_msg;
751         memset((void *)&req_msg, 0, sizeof(thumbMsg));
752
753         pid = getpid();
754         req_msg.pid = pid;
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;
760         req_msg.uid = uid;
761
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';
765
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;
769
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;
775         }
776
777         unsigned char *buf = NULL;
778         int buf_size = 0;
779         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
780
781         if (send(sock, buf, buf_size, 0) != buf_size) {
782                 thumb_err("sendto failed: %d", errno);
783                 SAFE_FREE(buf);
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;
788         }
789
790         SAFE_FREE(buf);
791         thumb_dbg("Sending msg to thumbnail daemon is successful");
792
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();
799                 }
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;
805                 }
806                 thumb_req->channel = channel;
807                 thumb_req->request_id = request_id;
808                 thumb_req->source_id = source_id;
809                 thumb_req->userData = userData;
810
811                 g_queue_push_tail(g_request_queue, (gpointer)thumb_req);
812         }
813         return MS_MEDIA_ERR_NONE;
814 }