Apply cynara
[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_request(int msg_type, const char *origin_path, char *thumb_path, int max_length, media_thumb_info *thumb_info, uid_t uid)
326 {
327         int sock = -1;
328         struct sockaddr_un serv_addr;
329         ms_sock_info_s sock_info;
330         int recv_str_len = 0;
331         int err = MS_MEDIA_ERR_NONE;
332         int pid;
333         sock_info.port = MS_THUMB_CREATOR_PORT;
334
335         err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
336         if (err != MS_MEDIA_ERR_NONE) {
337                 thumb_err("ms_ipc_create_client_socket failed");
338                 return err;
339         }
340
341         memset(&serv_addr, 0, sizeof(serv_addr));
342         sock = sock_info.sock_fd;
343         serv_addr.sun_family = AF_UNIX;
344         strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcreator.socket");
345
346         /* Connecting to the thumbnail server */
347         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
348                 thumb_stderror("connect");
349                 ms_ipc_delete_client_socket(&sock_info);
350                 return MS_MEDIA_ERR_SOCKET_CONN;
351         }
352
353         thumbMsg req_msg;
354         thumbMsg recv_msg;
355
356         memset((void *)&req_msg, 0, sizeof(thumbMsg));
357         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
358
359         /* Get PID of client*/
360         pid = getpid();
361         req_msg.pid = pid;
362
363         /* Set requset message */
364         req_msg.msg_type = msg_type;
365         req_msg.uid = uid;
366         strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
367         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
368
369         if (msg_type == THUMB_REQUEST_SAVE_FILE) {
370                 strncpy(req_msg.dst_path, thumb_path, sizeof(req_msg.dst_path));
371                 req_msg.dst_path[strlen(req_msg.dst_path)] = '\0';
372         }
373
374         req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
375         req_msg.dest_path_size = strlen(req_msg.dst_path) + 1;
376         req_msg.thumb_size = 0;
377
378         if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
379                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
380                 ms_ipc_delete_client_socket(&sock_info);
381                 return MS_MEDIA_ERR_INVALID_PARAMETER;
382         }
383
384         unsigned char *buf = NULL;
385         int buf_size = 0;
386         int header_size = 0;
387
388         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
389         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
390
391         if (send(sock, buf, buf_size, 0) != buf_size) {
392                 thumb_err("sendto failed: %d", errno);
393                 SAFE_FREE(buf);
394                 ms_ipc_delete_client_socket(&sock_info);
395                 return MS_MEDIA_ERR_SOCKET_SEND;
396         }
397
398         thumb_dbg("Sending msg to thumbnail daemon is successful");
399
400         SAFE_FREE(buf);
401
402         if(msg_type != THUMB_REQUEST_CANCEL_ALL_RAW_DATA && msg_type != THUMB_REQUEST_CANCEL_ALL) {             //No response..
403                 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
404                         thumb_err("_media_thumb_recv_msg failed ");
405                         ms_ipc_delete_client_socket(&sock_info);
406                         return err;
407                 }
408
409                 recv_str_len = strlen(recv_msg.org_path);
410                 thumb_dbg_slog("recv %s(%d) from thumb daemon is successful", recv_msg.org_path, recv_str_len);
411
412                 ms_ipc_delete_client_socket(&sock_info);
413
414                 if (recv_str_len > max_length) {
415                         thumb_err("user buffer is too small. Output's length is %d", recv_str_len);
416                         return MS_MEDIA_ERR_INVALID_PARAMETER;
417                 }
418
419                 if (recv_msg.status == THUMB_FAIL) {
420                         thumb_err("Failed to make thumbnail");
421                         return MS_MEDIA_ERR_INVALID_PARAMETER;
422                 }
423
424                 if (msg_type != THUMB_REQUEST_SAVE_FILE) {
425                         strncpy(thumb_path, recv_msg.dst_path, max_length);
426                 }
427
428                 thumb_info->origin_width = recv_msg.origin_width;
429                 thumb_info->origin_height = recv_msg.origin_height;
430         }else {
431                 thumb_dbg("No response msg_type:[%d]", msg_type);
432         }
433         return MS_MEDIA_ERR_NONE;
434 }
435
436 gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
437 {
438         thumbMsg recv_msg;
439         int header_size = 0;
440         int sock = 0;
441         int err = MS_MEDIA_ERR_NONE;
442
443         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
444         sock = g_io_channel_unix_get_fd(src);
445
446         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
447
448         thumb_err("_media_thumb_write_socket socket : %d", sock);
449
450         if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
451                 thumb_err("_media_thumb_recv_msg failed ");
452                 if (recv_msg.origin_path_size > 0) {
453                         __media_thumb_pop_req_queue(recv_msg.org_path, TRUE);
454                 } else {
455                         thumb_err("origin path size is wrong.");
456                 }
457
458                 return FALSE;
459         }
460
461         g_io_channel_shutdown(src, TRUE, NULL);
462         g_io_channel_unref(src);
463         //thumb_dbg("Completed..%s", recv_msg.org_path);
464
465         if (recv_msg.status == THUMB_FAIL) {
466                 thumb_err("Failed to make thumbnail");
467                 err = MS_MEDIA_ERR_INTERNAL;
468         }
469
470         if (data) {
471                 thumbUserData* cb = (thumbUserData*)data;
472                 if (cb->func != NULL)
473                         cb->func(err, recv_msg.dst_path, cb->user_data);
474         }
475
476         __media_thumb_pop_req_queue(recv_msg.org_path, FALSE);
477
478         thumb_dbg("Done");
479         return FALSE;
480 }
481
482 gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
483 {
484         thumbMsg recv_msg;
485         int header_size = 0;
486         int sock = 0;
487         int err = MS_MEDIA_ERR_NONE;
488
489         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
490         sock = g_io_channel_unix_get_fd(src);
491
492         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
493
494         thumb_err("_media_thumb_write_socket socket : %d", sock);
495
496         if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
497                 thumb_err("_media_thumb_recv_msg failed ");
498                 if (recv_msg.request_id > 0) {
499                         __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, TRUE);
500                 } else {
501                         thumb_err("origin path size is wrong.");
502                 }
503
504                 return FALSE;
505         }
506
507         g_io_channel_shutdown(src, TRUE, NULL);
508         g_io_channel_unref(src);
509
510         if (recv_msg.status == THUMB_FAIL) {
511                 thumb_err("Failed to make thumbnail");
512                 err = MS_MEDIA_ERR_INTERNAL;
513         }
514
515         if (data) {
516                 thumbRawUserData* cb = (thumbRawUserData*)data;
517                 if (cb->func != NULL)
518                         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);
519         }
520
521         __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, FALSE);
522
523         thumb_dbg("Done");
524
525         SAFE_FREE(recv_msg.thumb_data);
526
527         return FALSE;
528 }
529
530 int _media_thumb_request_async(int msg_type, const char *origin_path, thumbUserData *userData, uid_t uid)
531 {
532         int err = MS_MEDIA_ERR_NONE;
533         int sock = -1;
534         struct sockaddr_un serv_addr;
535         ms_sock_info_s sock_info;
536         int pid;
537         sock_info.port = MS_THUMB_CREATOR_PORT;
538
539         if ((msg_type == THUMB_REQUEST_DB_INSERT) && (__media_thumb_check_req_queue(origin_path) < 0)) {
540                 return MS_MEDIA_ERR_THUMB_DUPLICATED_REQUEST;
541         }
542
543         err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
544         if(err != MS_MEDIA_ERR_NONE)
545         {
546                 thumb_err("ms_ipc_create_client_socket failed");
547                 return err;
548         }
549
550         memset(&serv_addr, 0, sizeof(serv_addr));
551         sock = sock_info.sock_fd;
552         serv_addr.sun_family = AF_UNIX;
553         strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcreator.socket");
554
555         GIOChannel *channel = NULL;
556         channel = g_io_channel_unix_new(sock);
557         int source_id = -1;
558
559         /* Connecting to the thumbnail server */
560         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
561                 thumb_stderror("connect");
562                 g_io_channel_shutdown(channel, TRUE, NULL);
563                 g_io_channel_unref(channel);
564                 return MS_MEDIA_ERR_SOCKET_CONN;
565         }
566
567         if (msg_type != THUMB_REQUEST_CANCEL_MEDIA) {
568                 /* Create new channel to watch udp socket */
569                 GSource *source = NULL;
570                 source = g_io_create_watch(channel, G_IO_IN);
571
572                 /* Set callback to be called when socket is readable */
573                 g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, userData, NULL);
574                 source_id = g_source_attach(source, g_main_context_get_thread_default());
575         }
576
577         thumbMsg req_msg;
578         memset((void *)&req_msg, 0, sizeof(thumbMsg));
579
580         pid = getpid();
581         req_msg.pid = pid;
582         req_msg.msg_type = msg_type;
583         req_msg.uid = uid;
584
585         strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
586         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
587         req_msg.dst_path[0] = '\0';
588         req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
589         req_msg.dest_path_size = 1;
590         req_msg.thumb_size = 0;
591
592         if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
593                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
594                 g_io_channel_shutdown(channel, TRUE, NULL);
595                 g_io_channel_unref(channel);
596                 ms_ipc_delete_client_socket(&sock_info);
597                 return MS_MEDIA_ERR_INVALID_PARAMETER;
598         }
599
600         unsigned char *buf = NULL;
601         int buf_size = 0;
602         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
603
604         if (send(sock, buf, buf_size, 0) != buf_size) {
605                 thumb_err("sendto failed: %d", errno);
606                 SAFE_FREE(buf);
607                 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
608                 g_io_channel_shutdown(channel, TRUE, NULL);
609                 g_io_channel_unref(channel);
610                 ms_ipc_delete_client_socket(&sock_info);
611                 return MS_MEDIA_ERR_SOCKET_SEND;
612         }
613
614         SAFE_FREE(buf);
615         thumb_dbg("Sending msg to thumbnail daemon is successful");
616
617         if (msg_type == THUMB_REQUEST_CANCEL_MEDIA) {
618                 g_io_channel_shutdown(channel, TRUE, NULL);
619                 __media_thumb_pop_req_queue(origin_path, TRUE);
620         } else if (msg_type == THUMB_REQUEST_DB_INSERT) {
621                 if (g_request_queue == NULL) {
622                         g_request_queue = g_queue_new();
623                 }
624
625                 thumbReq *thumb_req = NULL;
626                 thumb_req = calloc(1, sizeof(thumbReq));
627                 if (thumb_req == NULL) {
628                         thumb_err("Failed to create request element");
629                         return MS_MEDIA_ERR_INVALID_PARAMETER;
630                 }
631
632                 thumb_req->channel = channel;
633                 thumb_req->path = strdup(origin_path);
634                 thumb_req->source_id = source_id;
635                 thumb_req->userData = userData;
636
637                 g_queue_push_tail(g_request_queue, (gpointer)thumb_req);
638         }
639
640         return MS_MEDIA_ERR_NONE;
641 }
642
643 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)
644 {
645         int err = MS_MEDIA_ERR_NONE;
646         int sock = -1;
647         struct sockaddr_un serv_addr;
648         ms_sock_info_s sock_info;
649         int pid;
650         sock_info.port = MS_THUMB_CREATOR_PORT;
651
652         err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
653         if(err != MS_MEDIA_ERR_NONE)
654         {
655                 thumb_err("ms_ipc_create_client_socket failed");
656                 return err;
657         }
658
659         memset(&serv_addr, 0, sizeof(serv_addr));
660         sock = sock_info.sock_fd;
661         serv_addr.sun_family = AF_UNIX;
662         strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcreator.socket");
663
664         GIOChannel *channel = NULL;
665         channel = g_io_channel_unix_new(sock);
666         int source_id = -1;
667
668         /* Connecting to the thumbnail server */
669         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
670                 thumb_err("connect error : %s", strerror(errno));
671                 g_io_channel_shutdown(channel, TRUE, NULL);
672                 g_io_channel_unref(channel);
673                 return MS_MEDIA_ERR_SOCKET_CONN;
674         }
675
676         if (msg_type != THUMB_REQUEST_CANCEL_RAW_DATA) {
677                 /* Create new channel to watch udp socket */
678                 GSource *source = NULL;
679                 source = g_io_create_watch(channel, G_IO_IN);
680
681                 /* Set callback to be called when socket is readable */
682                 /*NEED UPDATE SOCKET FILE DELETE*/
683                 g_source_set_callback(source, (GSourceFunc)_media_thumb_raw_data_write_socket, userData, NULL);
684                 source_id = g_source_attach(source, g_main_context_get_thread_default());
685         }
686
687         thumbMsg req_msg;
688         memset((void *)&req_msg, 0, sizeof(thumbMsg));
689
690         pid = getpid();
691         req_msg.pid = pid;
692         req_msg.msg_type = msg_type;
693         req_msg.request_id = request_id;
694         req_msg.thumb_width = width;
695         req_msg.thumb_height = height;
696         req_msg.uid = uid;
697
698         strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
699         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
700         req_msg.dst_path[0] = '\0';
701
702         req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
703         req_msg.dest_path_size = 1;
704         req_msg.thumb_size = 0;
705
706         if (req_msg.origin_path_size > MAX_PATH_SIZE) {
707                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
708                 g_io_channel_shutdown(channel, TRUE, NULL);
709                 g_io_channel_unref(channel);
710                 return MS_MEDIA_ERR_INVALID_PARAMETER;
711         }
712
713         unsigned char *buf = NULL;
714         int buf_size = 0;
715         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
716
717         if (send(sock, buf, buf_size, 0) != buf_size) {
718                 thumb_err("sendto failed: %d", errno);
719                 SAFE_FREE(buf);
720                 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
721                 g_io_channel_shutdown(channel, TRUE, NULL);
722                 g_io_channel_unref(channel);
723                 return MS_MEDIA_ERR_SOCKET_SEND;
724         }
725
726         SAFE_FREE(buf);
727         thumb_dbg("Sending msg to thumbnail daemon is successful");
728
729         if (msg_type == THUMB_REQUEST_CANCEL_RAW_DATA) {
730                 g_io_channel_shutdown(channel, TRUE, NULL);
731                 __media_thumb_pop_raw_data_req_queue(request_id, TRUE);
732         } else if (msg_type == THUMB_REQUEST_RAW_DATA) {
733                 if (g_request_queue == NULL) {
734                         g_request_queue = g_queue_new();
735                 }
736                 thumbRawReq *thumb_req = NULL;
737                 thumb_req = calloc(1, sizeof(thumbReq));
738                 if (thumb_req == NULL) {
739                         thumb_err("Failed to create request element");
740                         return MS_MEDIA_ERR_INVALID_PARAMETER;
741                 }
742                 thumb_req->channel = channel;
743                 thumb_req->request_id = request_id;
744                 thumb_req->source_id = source_id;
745                 thumb_req->userData = userData;
746
747                 g_queue_push_tail(g_request_queue, (gpointer)thumb_req);
748         }
749         return MS_MEDIA_ERR_NONE;
750 }