Patch below:
[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, 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.uid = uid;
423         strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
424         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
425
426         if (msg_type == THUMB_REQUEST_SAVE_FILE) {
427                 strncpy(req_msg.dst_path, thumb_path, sizeof(req_msg.dst_path));
428                 req_msg.dst_path[strlen(req_msg.dst_path)] = '\0';
429         }
430
431         req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
432         req_msg.dest_path_size = strlen(req_msg.dst_path) + 1;
433         req_msg.thumb_size = 0;
434
435         if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
436                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
437                 ms_ipc_delete_client_socket(&sock_info);
438                 return MS_MEDIA_ERR_INVALID_PARAMETER;
439         }
440
441         unsigned char *buf = NULL;
442         int buf_size = 0;
443         int header_size = 0;
444
445         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
446         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
447
448         if (send(sock, buf, buf_size, 0) != buf_size) {
449                 thumb_err("sendto failed: %d", errno);
450                 SAFE_FREE(buf);
451                 ms_ipc_delete_client_socket(&sock_info);
452                 return MS_MEDIA_ERR_SOCKET_SEND;
453         }
454
455         thumb_dbg("Sending msg to thumbnail daemon is successful");
456
457         SAFE_FREE(buf);
458
459         if(msg_type != THUMB_REQUEST_CANCEL_ALL_RAW_DATA && msg_type != THUMB_REQUEST_CANCEL_ALL) {             //No response..
460                 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
461                         thumb_err("_media_thumb_recv_msg failed ");
462                         ms_ipc_delete_client_socket(&sock_info);
463                         return err;
464                 }
465
466                 recv_str_len = strlen(recv_msg.org_path);
467                 thumb_dbg_slog("recv %s(%d) from thumb daemon is successful", recv_msg.org_path, recv_str_len);
468
469                 ms_ipc_delete_client_socket(&sock_info);
470
471                 if (recv_str_len > max_length) {
472                         thumb_err("user buffer is too small. Output's length is %d", recv_str_len);
473                         return MS_MEDIA_ERR_INVALID_PARAMETER;
474                 }
475
476                 if (recv_msg.status == THUMB_FAIL) {
477                         thumb_err("Failed to make thumbnail");
478                         return MS_MEDIA_ERR_INVALID_PARAMETER;
479                 }
480
481                 if (msg_type != THUMB_REQUEST_SAVE_FILE) {
482                         strncpy(thumb_path, recv_msg.dst_path, max_length);
483                 }
484
485                 thumb_info->origin_width = recv_msg.origin_width;
486                 thumb_info->origin_height = recv_msg.origin_height;
487         }else {
488                 thumb_dbg("No response msg_type:[%d]", msg_type);
489         }
490         return MS_MEDIA_ERR_NONE;
491 }
492
493 gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
494 {
495         thumbMsg recv_msg;
496         int header_size = 0;
497         int sock = 0;
498         int err = MS_MEDIA_ERR_NONE;
499
500         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
501         sock = g_io_channel_unix_get_fd(src);
502
503         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
504
505         thumb_err("_media_thumb_write_socket socket : %d", sock);
506
507         if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
508                 thumb_err("_media_thumb_recv_msg failed ");
509                 if (recv_msg.origin_path_size > 0) {
510                         __media_thumb_pop_req_queue(recv_msg.org_path, TRUE);
511                 } else {
512                         thumb_err("origin path size is wrong.");
513                 }
514
515                 return FALSE;
516         }
517
518         g_io_channel_shutdown(src, TRUE, NULL);
519         g_io_channel_unref(src);
520         //thumb_dbg("Completed..%s", recv_msg.org_path);
521
522         if (recv_msg.status == THUMB_FAIL) {
523                 thumb_err("Failed to make thumbnail");
524                 err = MS_MEDIA_ERR_INTERNAL;
525         }
526
527         if (data) {
528                 thumbUserData* cb = (thumbUserData*)data;
529                 if (cb->func != NULL)
530                         cb->func(err, recv_msg.dst_path, cb->user_data);
531         }
532
533         __media_thumb_pop_req_queue(recv_msg.org_path, FALSE);
534
535         thumb_dbg("Done");
536         return FALSE;
537 }
538
539 gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
540 {
541         thumbMsg recv_msg;
542         int header_size = 0;
543         int sock = 0;
544         int err = MS_MEDIA_ERR_NONE;
545
546         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
547         sock = g_io_channel_unix_get_fd(src);
548
549         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
550
551         thumb_err("_media_thumb_write_socket socket : %d", sock);
552
553         if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
554                 thumb_err("_media_thumb_recv_msg failed ");
555                 if (recv_msg.request_id > 0) {
556                         __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, TRUE);
557                 } else {
558                         thumb_err("origin path size is wrong.");
559                 }
560
561                 return FALSE;
562         }
563
564         g_io_channel_shutdown(src, TRUE, NULL);
565         g_io_channel_unref(src);
566
567         if (recv_msg.status == THUMB_FAIL) {
568                 thumb_err("Failed to make thumbnail");
569                 err = MS_MEDIA_ERR_INTERNAL;
570         }
571
572         if (data) {
573                 thumbRawUserData* cb = (thumbRawUserData*)data;
574                 if (cb->func != NULL)
575                         cb->func(err, recv_msg.request_id, recv_msg.org_path, recv_msg.thumb_width, recv_msg.thumb_height, recv_msg.thumb_data, recv_msg.thumb_size, cb->user_data);
576         }
577
578         __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, FALSE);
579
580         thumb_dbg("Done");
581
582         SAFE_FREE(recv_msg.thumb_data);
583
584         return FALSE;
585 }
586
587 int _media_thumb_request_async(int msg_type, const char *origin_path, thumbUserData *userData, uid_t uid)
588 {
589         int err = MS_MEDIA_ERR_NONE;
590         int sock = -1;
591         struct sockaddr_un serv_addr;
592         ms_sock_info_s sock_info;
593         int pid;
594         sock_info.port = MS_THUMB_CREATOR_PORT;
595
596         if ((msg_type == THUMB_REQUEST_DB_INSERT) && (__media_thumb_check_req_queue(origin_path) < 0)) {
597                 return MS_MEDIA_ERR_THUMB_DUPLICATED_REQUEST;
598         }
599
600         err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
601         if(err != MS_MEDIA_ERR_NONE)
602         {
603                 thumb_err("ms_ipc_create_client_socket failed");
604                 return err;
605         }
606
607         memset(&serv_addr, 0, sizeof(serv_addr));
608         sock = sock_info.sock_fd;
609         serv_addr.sun_family = AF_UNIX;
610         strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcreator.socket");
611
612         GIOChannel *channel = NULL;
613         channel = g_io_channel_unix_new(sock);
614         int source_id = -1;
615
616         /* Connecting to the thumbnail server */
617         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
618                 thumb_stderror("connect");
619                 g_io_channel_shutdown(channel, TRUE, NULL);
620                 g_io_channel_unref(channel);
621                 return MS_MEDIA_ERR_SOCKET_CONN;
622         }
623
624         if (msg_type != THUMB_REQUEST_CANCEL_MEDIA) {
625                 /* Create new channel to watch udp socket */
626                 GSource *source = NULL;
627                 source = g_io_create_watch(channel, G_IO_IN);
628
629                 /* Set callback to be called when socket is readable */
630                 g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, userData, NULL);
631                 source_id = g_source_attach(source, g_main_context_get_thread_default());
632         }
633
634         thumbMsg req_msg;
635         memset((void *)&req_msg, 0, sizeof(thumbMsg));
636
637         pid = getpid();
638         req_msg.pid = pid;
639         req_msg.msg_type = msg_type;
640         req_msg.uid = uid;
641
642         strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
643         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
644         req_msg.dst_path[0] = '\0';
645         req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
646         req_msg.dest_path_size = 1;
647         req_msg.thumb_size = 0;
648
649         if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
650                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
651                 g_io_channel_shutdown(channel, TRUE, NULL);
652                 g_io_channel_unref(channel);
653                 ms_ipc_delete_client_socket(&sock_info);
654                 return MS_MEDIA_ERR_INVALID_PARAMETER;
655         }
656
657         unsigned char *buf = NULL;
658         int buf_size = 0;
659         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
660
661         if (send(sock, buf, buf_size, 0) != buf_size) {
662                 thumb_err("sendto failed: %d", errno);
663                 SAFE_FREE(buf);
664                 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
665                 g_io_channel_shutdown(channel, TRUE, NULL);
666                 g_io_channel_unref(channel);
667                 ms_ipc_delete_client_socket(&sock_info);
668                 return MS_MEDIA_ERR_SOCKET_SEND;
669         }
670
671         SAFE_FREE(buf);
672         thumb_dbg("Sending msg to thumbnail daemon is successful");
673
674         if (msg_type == THUMB_REQUEST_CANCEL_MEDIA) {
675                 g_io_channel_shutdown(channel, TRUE, NULL);
676                 __media_thumb_pop_req_queue(origin_path, TRUE);
677         } else if (msg_type == THUMB_REQUEST_DB_INSERT) {
678                 if (g_request_queue == NULL) {
679                         g_request_queue = g_queue_new();
680                 }
681
682                 thumbReq *thumb_req = NULL;
683                 thumb_req = calloc(1, sizeof(thumbReq));
684                 if (thumb_req == NULL) {
685                         thumb_err("Failed to create request element");
686                         return MS_MEDIA_ERR_INVALID_PARAMETER;
687                 }
688
689                 thumb_req->channel = channel;
690                 thumb_req->path = strdup(origin_path);
691                 thumb_req->source_id = source_id;
692                 thumb_req->userData = userData;
693
694                 g_queue_push_tail(g_request_queue, (gpointer)thumb_req);
695         }
696
697         return MS_MEDIA_ERR_NONE;
698 }
699
700 int _media_thumb_request_raw_data_async(int msg_type, int request_id, const char *origin_path, int width, int height, thumbRawUserData *userData, uid_t uid)
701 {
702         int err = MS_MEDIA_ERR_NONE;
703         int sock = -1;
704         struct sockaddr_un serv_addr;
705         ms_sock_info_s sock_info;
706         int pid;
707         sock_info.port = MS_THUMB_CREATOR_PORT;
708
709         err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
710         if(err != MS_MEDIA_ERR_NONE)
711         {
712                 thumb_err("ms_ipc_create_client_socket failed");
713                 return err;
714         }
715
716         memset(&serv_addr, 0, sizeof(serv_addr));
717         sock = sock_info.sock_fd;
718         serv_addr.sun_family = AF_UNIX;
719         strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcreator.socket");
720
721         GIOChannel *channel = NULL;
722         channel = g_io_channel_unix_new(sock);
723         int source_id = -1;
724
725         /* Connecting to the thumbnail server */
726         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
727                 thumb_err("connect error : %s", strerror(errno));
728                 g_io_channel_shutdown(channel, TRUE, NULL);
729                 g_io_channel_unref(channel);
730                 return MS_MEDIA_ERR_SOCKET_CONN;
731         }
732
733         if (msg_type != THUMB_REQUEST_CANCEL_RAW_DATA) {
734                 /* Create new channel to watch udp socket */
735                 GSource *source = NULL;
736                 source = g_io_create_watch(channel, G_IO_IN);
737
738                 /* Set callback to be called when socket is readable */
739                 /*NEED UPDATE SOCKET FILE DELETE*/
740                 g_source_set_callback(source, (GSourceFunc)_media_thumb_raw_data_write_socket, userData, NULL);
741                 source_id = g_source_attach(source, g_main_context_get_thread_default());
742         }
743
744         thumbMsg req_msg;
745         memset((void *)&req_msg, 0, sizeof(thumbMsg));
746
747         pid = getpid();
748         req_msg.pid = pid;
749         req_msg.msg_type = msg_type;
750         req_msg.request_id = request_id;
751         req_msg.thumb_width = width;
752         req_msg.thumb_height = height;
753         req_msg.uid = uid;
754
755         strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
756         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
757         req_msg.dst_path[0] = '\0';
758
759         req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
760         req_msg.dest_path_size = 1;
761         req_msg.thumb_size = 0;
762
763         if (req_msg.origin_path_size > MAX_PATH_SIZE) {
764                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
765                 g_io_channel_shutdown(channel, TRUE, NULL);
766                 g_io_channel_unref(channel);
767                 return MS_MEDIA_ERR_INVALID_PARAMETER;
768         }
769
770         unsigned char *buf = NULL;
771         int buf_size = 0;
772         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
773
774         if (send(sock, buf, buf_size, 0) != buf_size) {
775                 thumb_err("sendto failed: %d", errno);
776                 SAFE_FREE(buf);
777                 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
778                 g_io_channel_shutdown(channel, TRUE, NULL);
779                 g_io_channel_unref(channel);
780                 return MS_MEDIA_ERR_SOCKET_SEND;
781         }
782
783         SAFE_FREE(buf);
784         thumb_dbg("Sending msg to thumbnail daemon is successful");
785
786         if (msg_type == THUMB_REQUEST_CANCEL_RAW_DATA) {
787                 g_io_channel_shutdown(channel, TRUE, NULL);
788                 __media_thumb_pop_raw_data_req_queue(request_id, TRUE);
789         } else if (msg_type == THUMB_REQUEST_RAW_DATA) {
790                 if (g_request_queue == NULL) {
791                         g_request_queue = g_queue_new();
792                 }
793                 thumbRawReq *thumb_req = NULL;
794                 thumb_req = calloc(1, sizeof(thumbReq));
795                 if (thumb_req == NULL) {
796                         thumb_err("Failed to create request element");
797                         return MS_MEDIA_ERR_INVALID_PARAMETER;
798                 }
799                 thumb_req->channel = channel;
800                 thumb_req->request_id = request_id;
801                 thumb_req->source_id = source_id;
802                 thumb_req->userData = userData;
803
804                 g_queue_push_tail(g_request_queue, (gpointer)thumb_req);
805         }
806         return MS_MEDIA_ERR_NONE;
807 }