4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hyunjun Ko <zzoon.ko@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
22 #include "media-thumbnail.h"
23 #include "media-thumb-ipc.h"
24 #include "media-thumb-util.h"
25 #include "media-thumb-db.h"
33 #define GLOBAL_USER 0 //#define tzplatform_getenv(TZ_GLOBAL) //TODO
35 static __thread GQueue *g_request_queue = NULL;
43 _media_thumb_create_socket(int sock_type, int *sock)
47 if ((sock_fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
48 thumb_err("socket failed: %s", strerror(errno));
49 return MEDIA_THUMB_ERROR_NETWORK;
52 if (sock_type == CLIENT_SOCKET) {
54 #ifdef _USE_MEDIA_UTIL_
55 struct timeval tv_timeout = { MS_TIMEOUT_SEC_10, 0 };
57 struct timeval tv_timeout = { TIMEOUT_SEC, 0 };
60 if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &tv_timeout, sizeof(tv_timeout)) == -1) {
61 thumb_err("setsockopt failed: %s", strerror(errno));
63 return MEDIA_THUMB_ERROR_NETWORK;
65 } else if (sock_type == SERVER_SOCKET) {
69 if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &n_reuse, sizeof(n_reuse)) == -1) {
70 thumb_err("setsockopt failed: %s", strerror(errno));
72 return MEDIA_THUMB_ERROR_NETWORK;
78 return MEDIA_THUMB_ERROR_NONE;
83 _media_thumb_create_udp_socket(int *sock)
87 if ((sock_fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
88 thumb_err("socket failed: %s", strerror(errno));
89 return MEDIA_THUMB_ERROR_NETWORK;
92 #ifdef _USE_MEDIA_UTIL_
93 struct timeval tv_timeout = { MS_TIMEOUT_SEC_10, 0 };
95 struct timeval tv_timeout = { TIMEOUT_SEC, 0 };
98 if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &tv_timeout, sizeof(tv_timeout)) == -1) {
99 thumb_err("setsockopt failed: %s", strerror(errno));
101 return MEDIA_THUMB_ERROR_NETWORK;
106 return MEDIA_THUMB_ERROR_NONE;
109 int _media_thumb_get_error()
111 if (errno == EWOULDBLOCK) {
112 thumb_err("Timeout. Can't try any more");
113 return MEDIA_THUMB_ERROR_TIMEOUT;
115 thumb_err("recvfrom failed : %s", strerror(errno));
116 return MEDIA_THUMB_ERROR_NETWORK;
120 int __media_thumb_pop_req_queue(const char *path, bool shutdown_channel)
124 if (g_request_queue == NULL) return -1;
125 req_len = g_queue_get_length(g_request_queue);
127 // thumb_dbg("Queue length : %d", req_len);
128 // thumb_dbg("Queue path : %s", path);
131 // thumb_dbg("There is no request in the queue");
134 for (i = 0; i < req_len; i++) {
135 thumbReq *req = NULL;
136 req = (thumbReq *)g_queue_peek_nth(g_request_queue, i);
137 if (req == NULL) continue;
139 if (strncmp(path, req->path, strlen(path)) == 0) {
140 //thumb_dbg("Popped %s", path);
141 if (shutdown_channel) {
142 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id));
144 g_io_channel_shutdown(req->channel, TRUE, NULL);
145 g_io_channel_unref(req->channel);
147 g_queue_pop_nth(g_request_queue, i);
149 SAFE_FREE(req->path);
156 // thumb_dbg("There's no %s in the queue", path);
160 return MEDIA_THUMB_ERROR_NONE;
163 int __media_thumb_check_req_queue(const char *path)
167 if (g_request_queue == NULL) return MEDIA_THUMB_ERROR_NONE;
168 req_len = g_queue_get_length(g_request_queue);
170 // thumb_dbg("Queue length : %d", req_len);
171 // thumb_dbg("Queue path : %s", path);
174 // thumb_dbg("There is no request in the queue");
177 for (i = 0; i < req_len; i++) {
178 thumbReq *req = NULL;
179 req = (thumbReq *)g_queue_peek_nth(g_request_queue, i);
180 if (req == NULL) continue;
182 if (strncmp(path, req->path, strlen(path)) == 0) {
183 //thumb_dbg("Same Request - %s", path);
191 return MEDIA_THUMB_ERROR_NONE;
195 _media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg)
197 int recv_msg_len = 0;
198 unsigned char *buf = NULL;
200 buf = (unsigned char*)malloc(header_size);
202 if ((recv_msg_len = recv(sock, buf, header_size, 0)) <= 0) {
203 thumb_err("recvfrom failed : %s", strerror(errno));
205 return _media_thumb_get_error();
208 memcpy(msg, buf, header_size);
209 //thumb_dbg("origin_path_size : %d, dest_path_size : %d", msg->origin_path_size, msg->dest_path_size);
213 buf = (unsigned char*)malloc(msg->origin_path_size);
215 if ((recv_msg_len = recv(sock, buf, msg->origin_path_size, 0)) < 0) {
216 thumb_err("recvfrom failed : %s", strerror(errno));
218 return _media_thumb_get_error();
221 strncpy(msg->org_path, (char*)buf, msg->origin_path_size);
222 //thumb_dbg("original path : %s", msg->org_path);
225 buf = (unsigned char*)malloc(msg->dest_path_size);
227 if ((recv_msg_len = recv(sock, buf, msg->dest_path_size, 0)) < 0) {
228 thumb_err("recvfrom failed : %s", strerror(errno));
230 return _media_thumb_get_error();
233 strncpy(msg->dst_path, (char*)buf, msg->dest_path_size);
234 //thumb_dbg("destination path : %s", msg->dst_path);
237 return MEDIA_THUMB_ERROR_NONE;
240 #ifdef _USE_UDS_SOCKET_
242 _media_thumb_recv_udp_msg(int sock, int header_size, thumbMsg *msg, struct sockaddr_un *from_addr, unsigned int *from_size)
245 _media_thumb_recv_udp_msg(int sock, int header_size, thumbMsg *msg, struct sockaddr_in *from_addr, unsigned int *from_size)
248 int recv_msg_len = 0;
249 #ifdef _USE_UDS_SOCKET_
250 unsigned int from_addr_size = sizeof(struct sockaddr_un);
252 unsigned int from_addr_size = sizeof(struct sockaddr_in);
254 unsigned char *buf = NULL;
256 buf = (unsigned char*)malloc(sizeof(thumbMsg));
258 if ((recv_msg_len = recvfrom(sock, buf, sizeof(thumbMsg), 0, (struct sockaddr *)from_addr, &from_addr_size)) < 0) {
259 thumb_err("recvfrom failed : %s", strerror(errno));
261 return _media_thumb_get_error();
264 memcpy(msg, buf, header_size);
266 if (msg->origin_path_size <= 0 || msg->origin_path_size > MAX_PATH_SIZE) {
268 thumb_err("msg->origin_path_size is invalid %d", msg->origin_path_size );
269 return MEDIA_THUMB_ERROR_NETWORK;
272 strncpy(msg->org_path, (char*)buf + header_size, msg->origin_path_size);
273 //thumb_dbg("original path : %s", msg->org_path);
275 if (msg->dest_path_size <= 0 || msg->dest_path_size > MAX_PATH_SIZE) {
277 thumb_err("msg->origin_path_size is invalid %d", msg->dest_path_size );
278 return MEDIA_THUMB_ERROR_NETWORK;
281 strncpy(msg->dst_path, (char*)buf + header_size + msg->origin_path_size, msg->dest_path_size);
282 //thumb_dbg("destination path : %s", msg->dst_path);
285 *from_size = from_addr_size;
287 return MEDIA_THUMB_ERROR_NONE;
291 _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
293 if (req_msg == NULL || buf == NULL) {
297 int org_path_len = 0;
298 int dst_path_len = 0;
302 header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2;
303 org_path_len = strlen(req_msg->org_path) + 1;
304 dst_path_len = strlen(req_msg->dst_path) + 1;
306 //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);
308 size = header_size + org_path_len + dst_path_len;
310 memcpy(*buf, req_msg, header_size);
311 memcpy((*buf)+header_size, req_msg->org_path, org_path_len);
312 memcpy((*buf)+header_size + org_path_len, req_msg->dst_path, dst_path_len);
320 _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)
323 #ifdef _USE_UDS_SOCKET_
324 struct sockaddr_un serv_addr;
325 #elif defined(_USE_UDS_SOCKET_TCP_)
326 struct sockaddr_un serv_addr;
328 const char *serv_ip = "127.0.0.1";
329 struct sockaddr_in serv_addr;
332 int recv_str_len = 0;
337 #ifdef _USE_MEDIA_UTIL_
338 #ifdef _USE_UDS_SOCKET_
339 if (ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock, MS_THUMB_CREATOR_PORT) < 0) {
340 #elif defined(_USE_UDS_SOCKET_TCP_)
341 if (ms_ipc_create_client_tcp_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock, MS_THUMB_CREATOR_TCP_PORT) < 0) {
343 if (ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock) < 0) {
345 thumb_err("ms_ipc_create_client_socket failed");
346 return MEDIA_THUMB_ERROR_NETWORK;
349 /* Creaete a TCP socket */
350 if (_media_thumb_create_socket(CLIENT_SOCKET, &sock) < 0) {
351 thumb_err("_media_thumb_create_socket failed");
352 return MEDIA_THUMB_ERROR_NETWORK;
356 memset(&serv_addr, 0, sizeof(serv_addr));
357 #ifdef _USE_UDS_SOCKET_
358 serv_addr.sun_family = AF_UNIX;
359 #elif defined(_USE_UDS_SOCKET_TCP_)
360 serv_addr.sun_family = AF_UNIX;
362 serv_addr.sin_family = AF_INET;
363 serv_addr.sin_addr.s_addr = inet_addr(serv_ip);
366 #ifdef _USE_MEDIA_UTIL_
367 #ifdef _USE_UDS_SOCKET_
368 strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcreator.socket");
369 #elif defined(_USE_UDS_SOCKET_TCP_)
371 strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcreator.socket");
373 serv_addr.sin_port = htons(MS_THUMB_CREATOR_PORT);
376 serv_addr.sin_port = htons(THUMB_DAEMON_PORT);
379 /* Connecting to the thumbnail server */
380 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
381 thumb_err("connect error : %s", strerror(errno));
382 return MEDIA_THUMB_ERROR_NETWORK;
388 memset((void *)&req_msg, 0, sizeof(thumbMsg));
389 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
391 /* Get PID of client*/
395 /* Set requset message */
396 req_msg.msg_type = msg_type;
397 req_msg.thumb_type = thumb_type;
399 strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
400 req_msg.org_path[strlen(req_msg.org_path)] = '\0';
402 if (msg_type == THUMB_REQUEST_SAVE_FILE) {
403 strncpy(req_msg.dst_path, thumb_path, sizeof(req_msg.dst_path));
404 req_msg.dst_path[strlen(req_msg.dst_path)] = '\0';
407 req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
408 req_msg.dest_path_size = strlen(req_msg.dst_path) + 1;
410 if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
411 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
413 return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
416 unsigned char *buf = NULL;
420 header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2;
421 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
423 if (send(sock, buf, buf_size, 0) != buf_size) {
424 thumb_err("sendto failed: %d\n", errno);
427 return MEDIA_THUMB_ERROR_NETWORK;
430 thumb_dbg("Sending msg to thumbnail daemon is successful");
434 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
435 thumb_err("_media_thumb_recv_msg failed ");
440 recv_str_len = strlen(recv_msg.org_path);
441 thumb_dbg("recv %s(%d) from thumb daemon is successful", recv_msg.org_path, recv_str_len);
445 if (recv_str_len > max_length) {
446 thumb_err("user buffer is too small. Output's length is %d", recv_str_len);
447 return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
450 if (recv_msg.status == THUMB_FAIL) {
451 thumb_err("Failed to make thumbnail");
455 if (msg_type != THUMB_REQUEST_SAVE_FILE) {
456 strncpy(thumb_path, recv_msg.dst_path, max_length);
459 thumb_info->origin_width = recv_msg.origin_width;
460 thumb_info->origin_height = recv_msg.origin_height;
465 static int _mkdir(const char *dir, mode_t mode) {
470 snprintf(tmp, sizeof(tmp),"%s",dir);
472 if(tmp[len - 1] == '/')
474 for(p = tmp + 1; *p; p++)
480 return mkdir(tmp, mode);
483 static char* _media_thumb_get_default_path(uid_t uid)
485 char *result_psswd = NULL;
486 struct group *grpinfo = NULL;
489 result_psswd = strdup(THUMB_DEFAULT_PATH);
490 grpinfo = getgrnam("users");
491 if(grpinfo == NULL) {
492 thumb_err("getgrnam(users) returns NULL !");
498 struct passwd *userinfo = getpwuid(uid);
499 if(userinfo == NULL) {
500 thumb_err("getpwuid(%d) returns NULL !", uid);
503 grpinfo = getgrnam("users");
504 if(grpinfo == NULL) {
505 thumb_err("getgrnam(users) returns NULL !");
508 // Compare git_t type and not group name
509 if (grpinfo->gr_gid != userinfo->pw_gid) {
510 thumb_err("UID [%d] does not belong to 'users' group!", uid);
513 asprintf(&result_psswd, "%s/data/file-manager-service/.thumb/phone", userinfo->pw_dir);
517 _mkdir(result_psswd,S_IRWXU | S_IRWXG | S_IRWXO);
523 _media_thumb_process(thumbMsg *req_msg, thumbMsg *res_msg, uid_t uid)
526 GdkPixbuf *gdkdata = NULL;
533 char *thumb_path = NULL;
534 int need_update_db = 0;
537 if (req_msg == NULL || res_msg == NULL) {
538 thumb_err("Invalid msg!");
539 return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
542 int msg_type = req_msg->msg_type;
543 media_thumb_type thumb_type = req_msg->thumb_type;
544 const char *origin_path = req_msg->org_path;
546 // Currently, The color space that is supported by the gdk-pixbuf is only RGB.
547 media_thumb_format thumb_format = MEDIA_THUMB_RGB888;
549 thumb_path = res_msg->dst_path;
550 thumb_path[0] = '\0';
551 max_length = sizeof(res_msg->dst_path);
553 err = _media_thumb_db_connect(uid);
555 thumb_err("_media_thumb_mb_svc_connect failed: %d", err);
556 return MEDIA_THUMB_ERROR_DB;
559 if (msg_type == THUMB_REQUEST_DB_INSERT) {
560 err = _media_thumb_get_thumb_from_db_with_size(origin_path, thumb_path, max_length, &need_update_db, &origin_w, &origin_h);
562 res_msg->origin_width = origin_w;
563 res_msg->origin_height = origin_h;
564 _media_thumb_db_disconnect();
565 return MEDIA_THUMB_ERROR_NONE;
567 if (strlen(thumb_path) == 0) {
568 err = _media_thumb_get_hash_name(origin_path, thumb_path, max_length,uid);
570 thumb_err("_media_thumb_get_hash_name failed - %d\n", err);
571 strncpy(thumb_path, _media_thumb_get_default_path(uid), max_length);
572 _media_thumb_db_disconnect();
576 thumb_path[strlen(thumb_path)] = '\0';
580 } else if (msg_type == THUMB_REQUEST_SAVE_FILE) {
581 strncpy(thumb_path, req_msg->dst_path, max_length);
583 } else if (msg_type == THUMB_REQUEST_ALL_MEDIA) {
584 err = _media_thumb_get_hash_name(origin_path, thumb_path, max_length,uid);
586 thumb_err("_media_thumb_get_hash_name failed - %d\n", err);
587 strncpy(thumb_path, _media_thumb_get_default_path(uid), max_length);
588 _media_thumb_db_disconnect();
592 thumb_path[strlen(thumb_path)] = '\0';
595 if (g_file_test(thumb_path,
596 G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
597 thumb_warn("thumb path already exists in file system.. remove the existed file");
598 _media_thumb_remove_file(thumb_path);
601 err = _thumbnail_get_data(origin_path, thumb_type, thumb_format, &gdkdata, &thumb_size, &thumb_w, &thumb_h, &origin_w, &origin_h, &alpha, uid);
603 thumb_err("_thumbnail_get_data failed - %d\n", err);
604 if ( gdkdata != NULL ){
605 g_object_unref(gdkdata);
607 strncpy(thumb_path, _media_thumb_get_default_path(uid), max_length);
608 _media_thumb_db_disconnect();
612 //thumb_dbg("Size : %d, W:%d, H:%d", thumb_size, thumb_w, thumb_h);
613 //thumb_dbg("Origin W:%d, Origin H:%d\n", origin_w, origin_h);
614 //thumb_dbg("Thumb : %s", thumb_path);
616 res_msg->msg_type = THUMB_RESPONSE;
617 res_msg->thumb_size = thumb_size;
618 res_msg->thumb_width = thumb_w;
619 res_msg->thumb_height = thumb_h;
620 res_msg->origin_width = origin_w;
621 res_msg->origin_height = origin_h;
623 /* If the image is transparent PNG format, make png file as thumbnail of this image */
626 err = _media_thumb_get_file_ext(origin_path, file_ext, sizeof(file_ext));
627 if (strncasecmp(file_ext, "png", 3) == 0) {
628 int len = strlen(thumb_path);
629 thumb_path[len - 3] = 'p';
630 thumb_path[len - 2] = 'n';
631 thumb_path[len - 1] = 'g';
633 thumb_dbg("Thumb path is changed : %s", thumb_path);
636 err = _media_thumb_save_to_file_with_gdk(gdkdata, thumb_w, thumb_h, alpha, thumb_path);
638 thumb_err("save_to_file_with_gdk failed - %d\n", err);
639 if ( gdkdata != NULL ){
640 g_object_unref(gdkdata);
643 if (msg_type == THUMB_REQUEST_DB_INSERT || msg_type == THUMB_REQUEST_ALL_MEDIA)
644 strncpy(thumb_path, _media_thumb_get_default_path(uid), max_length);
646 _media_thumb_db_disconnect();
649 thumb_dbg("file save success\n");
654 fd = open(thumb_path, O_WRONLY);
656 thumb_warn("open failed");
660 thumb_warn("fsync failed");
667 g_object_unref(gdkdata);
669 /* DB update if needed */
670 if (need_update_db == 1) {
671 err = _media_thumb_update_db(origin_path, thumb_path, res_msg->origin_width, res_msg->origin_height, uid);
673 thumb_err("_media_thumb_update_db failed : %d", err);
677 _media_thumb_db_disconnect();
682 gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
686 int recv_str_len = 0;
688 int err = MEDIA_THUMB_ERROR_NONE;
690 memset((void *)&recv_msg, 0, sizeof(thumbMsg));
691 sock = g_io_channel_unix_get_fd(src);
693 header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2;
695 thumb_err("_media_thumb_write_socket socket : %d", sock);
697 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
698 thumb_err("_media_thumb_recv_msg failed ");
699 g_io_channel_shutdown(src, TRUE, NULL);
703 recv_str_len = strlen(recv_msg.dst_path);
704 thumb_dbg("recv %s(%d) in [ %s ] from thumb daemon is successful", recv_msg.dst_path, recv_str_len, recv_msg.org_path);
706 g_io_channel_shutdown(src, TRUE, NULL);
708 //thumb_dbg("Completed..%s", recv_msg.org_path);
709 __media_thumb_pop_req_queue(recv_msg.org_path, FALSE);
711 if (recv_msg.status == THUMB_FAIL) {
712 thumb_err("Failed to make thumbnail");
713 err = MEDIA_THUMB_ERROR_UNSUPPORTED;
719 thumbUserData* cb = (thumbUserData*)data;
720 cb->func(err, recv_msg.dst_path, cb->user_data);
730 _media_thumb_request_async(int msg_type, media_thumb_type thumb_type, const char *origin_path, thumbUserData *userData, uid_t uid)
733 #ifdef _USE_UDS_SOCKET_
734 struct sockaddr_un serv_addr;
735 #elif defined(_USE_UDS_SOCKET_TCP_)
736 struct sockaddr_un serv_addr;
738 const char *serv_ip = "127.0.0.1";
739 struct sockaddr_in serv_addr;
744 if ((msg_type == THUMB_REQUEST_DB_INSERT) && (__media_thumb_check_req_queue(origin_path) < 0)) {
745 return MEDIA_THUMB_ERROR_DUPLICATED_REQUEST;
748 #ifdef _USE_MEDIA_UTIL_
749 #ifdef _USE_UDS_SOCKET_
750 if (ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock, MS_THUMB_CREATOR_PORT) < 0) {
751 #elif defined(_USE_UDS_SOCKET_TCP_)
752 if (ms_ipc_create_client_tcp_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock, MS_THUMB_CREATOR_TCP_PORT) < 0) {
754 if (ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock) < 0) {
756 thumb_err("ms_ipc_create_client_socket failed");
757 return MEDIA_THUMB_ERROR_NETWORK;
760 /* Creaete a TCP socket */
761 if (_media_thumb_create_socket(CLIENT_SOCKET, &sock) < 0) {
762 thumb_err("_media_thumb_create_socket failed");
763 return MEDIA_THUMB_ERROR_NETWORK;
767 GIOChannel *channel = NULL;
768 channel = g_io_channel_unix_new(sock);
772 memset(&serv_addr, 0, sizeof(serv_addr));
773 #ifdef _USE_UDS_SOCKET_
774 serv_addr.sun_family = AF_UNIX;
775 #elif defined(_USE_UDS_SOCKET_TCP_)
776 serv_addr.sun_family = AF_UNIX;
778 serv_addr.sin_family = AF_INET;
779 serv_addr.sin_addr.s_addr = inet_addr(serv_ip);
782 #ifdef _USE_MEDIA_UTIL_
783 #ifdef _USE_UDS_SOCKET_
784 strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcreator.socket");
785 #elif defined(_USE_UDS_SOCKET_TCP_)
786 strcpy(serv_addr.sun_path, "/tmp/media_ipc_thumbcreator.dat");
788 serv_addr.sin_port = htons(MS_THUMB_CREATOR_PORT);
791 serv_addr.sin_port = htons(THUMB_DAEMON_PORT);
794 /* Connecting to the thumbnail server */
795 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
796 thumb_err("connect error : %s", strerror(errno));
797 g_io_channel_shutdown(channel, TRUE, NULL);
798 return MEDIA_THUMB_ERROR_NETWORK;
801 if (msg_type != THUMB_REQUEST_CANCEL_MEDIA) {
802 //source_id = g_io_add_watch(channel, G_IO_IN, _media_thumb_write_socket, userData );
804 /* Create new channel to watch udp socket */
805 GSource *source = NULL;
806 source = g_io_create_watch(channel, G_IO_IN);
808 /* Set callback to be called when socket is readable */
809 g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, userData, NULL);
810 source_id = g_source_attach(source, g_main_context_get_thread_default());
814 memset((void *)&req_msg, 0, sizeof(thumbMsg));
818 req_msg.msg_type = msg_type;
819 req_msg.thumb_type = thumb_type;
822 strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
823 req_msg.org_path[strlen(req_msg.org_path)] = '\0';
825 req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
826 req_msg.dest_path_size = strlen(req_msg.dst_path) + 1;
828 if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
829 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
830 g_io_channel_shutdown(channel, TRUE, NULL);
831 return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
834 unsigned char *buf = NULL;
836 _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
838 //thumb_dbg("buffer size : %d", buf_size);
840 if (send(sock, buf, buf_size, 0) != buf_size) {
841 thumb_err("sendto failed: %d\n", errno);
843 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
844 g_io_channel_shutdown(channel, TRUE, NULL);
845 return MEDIA_THUMB_ERROR_NETWORK;
849 thumb_dbg("Sending msg to thumbnail daemon is successful");
852 if (msg_type == THUMB_REQUEST_CANCEL_MEDIA) {
853 g_io_channel_shutdown(channel, TRUE, NULL);
856 if (msg_type == THUMB_REQUEST_CANCEL_MEDIA) {
857 g_io_channel_shutdown(channel, TRUE, NULL);
859 //thumb_dbg("Cancel : %s[%d]", origin_path, sock);
860 __media_thumb_pop_req_queue(origin_path, TRUE);
861 } else if (msg_type == THUMB_REQUEST_DB_INSERT) {
862 if (g_request_queue == NULL) {
863 g_request_queue = g_queue_new();
866 thumbReq *thumb_req = NULL;
867 thumb_req = calloc(1, sizeof(thumbReq));
868 if (thumb_req == NULL) {
869 thumb_err("Failed to create request element");
873 thumb_req->channel = channel;
874 thumb_req->path = strdup(origin_path);
875 thumb_req->source_id = source_id;
877 //thumb_dbg("Push : %s [%d]", origin_path, sock);
878 g_queue_push_tail(g_request_queue, (gpointer)thumb_req);