2 * media-thumbnail-server
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 "thumb-server-internal.h"
23 #include "media-thumb-util.h"
24 #include "media-thumb-debug.h"
31 #include <tzplatform_config.h>
37 #define LOG_TAG "MEDIA_THUMBNAIL_SERVER"
38 #define THUMB_DEFAULT_WIDTH 320
39 #define THUMB_DEFAULT_HEIGHT 240
40 #define THUMB_COMM_SOCK_PATH tzplatform_mkpath(TZ_SYS_RUN, "media-server/media_ipc_thumbcomm.socket")
41 #define THUMB_EMPTY_STR ""
43 GMainLoop *g_thumb_server_mainloop; // defined in thumb-server.c as extern
45 static gboolean __thumb_server_send_msg_to_agent(int msg_type);
46 static gboolean _thumb_server_send_deny_message(int sockfd);
48 gboolean _thumb_daemon_start_jobs(gpointer data)
51 __thumb_server_send_msg_to_agent(MS_MSG_THUMB_SERVER_READY);
56 int _thumb_daemon_process_job(thumbMsg *req_msg, thumbMsg *res_msg)
58 int err = MS_MEDIA_ERR_NONE;
60 if (req_msg->msg_type == THUMB_REQUEST_RAW_DATA) {
61 err = _media_thumb_process_raw(req_msg, res_msg);
62 if (err != MS_MEDIA_ERR_NONE)
63 thumb_warn("_media_thumb_process_raw is failed: %d", err);
64 } else if (req_msg->msg_type == THUMB_REQUEST_DB_INSERT) {
65 err = _media_thumb_process(req_msg, res_msg);
66 if (err != MS_MEDIA_ERR_NONE)
67 thumb_warn("_media_thumb_process is failed: %d", err);
73 gboolean _thumb_server_read_socket(GIOChannel *src, GIOCondition condition, gpointer data)
75 struct sockaddr_un client_addr;
76 unsigned int client_addr_len;
79 ms_peer_credentials credentials;
84 memset((void *)&recv_msg, 0, sizeof(recv_msg));
85 memset((void *)&res_msg, 0, sizeof(res_msg));
86 memset((void *)&credentials, 0, sizeof(credentials));
88 sock = g_io_channel_unix_get_fd(src);
90 thumb_err("sock fd is invalid!");
94 client_addr_len = sizeof(client_addr);
96 if ((client_sock = accept(sock, (struct sockaddr*)&client_addr, &client_addr_len)) < 0) {
97 thumb_stderror("accept failed");
101 if (ms_cynara_receive_untrusted_message_thumb(client_sock, &recv_msg, &credentials) != MS_MEDIA_ERR_NONE) {
102 thumb_err("ms_cynara_receive_untrusted_message_thumb failed");
107 if (recv_msg.msg_type != THUMB_REQUEST_KILL_SERVER) {
108 if (ms_cynara_check(&credentials, MEDIA_STORAGE_PRIVILEGE) != MS_MEDIA_ERR_NONE) {
109 thumb_err("Cynara denied access to process request");
110 _thumb_server_send_deny_message(client_sock);
115 thumb_warn("Shutting down...");
116 g_main_loop_quit(g_thumb_server_mainloop);
121 SAFE_FREE(credentials.smack);
122 SAFE_FREE(credentials.uid);
124 thumb_warn_slog("Received [%d] %.*s(%d) from PID(%d)", recv_msg.msg_type, MAX_FILEPATH_LEN, recv_msg.org_path, strlen(recv_msg.org_path), recv_msg.pid);
126 _thumb_daemon_process_job(&recv_msg, &res_msg);
128 if (res_msg.msg_type == 0)
129 res_msg.msg_type = recv_msg.msg_type;
130 res_msg.request_id = recv_msg.request_id;
131 SAFE_STRLCPY(res_msg.org_path, recv_msg.org_path, sizeof(res_msg.org_path));
132 if (res_msg.msg_type != THUMB_RESPONSE_RAW_DATA)
133 res_msg.thumb_size = 0;
135 res_msg.dst_path[0] = '\0';
139 int head_size = sizeof(res_msg) - sizeof(unsigned char *);
140 unsigned char *buf = NULL;
141 _media_thumb_set_buffer(&res_msg, &buf, &buf_size);
142 remain_size = buf_size - head_size;
144 if (send(client_sock, buf, head_size, 0) < 0) {
145 thumb_stderror("send failed");
147 if (remain_size > 0) {
148 if (send(client_sock, buf + head_size, remain_size, 0) < 0) {
149 thumb_stderror("send failed");
151 thumb_dbg_slog("Sent data(%d) from %s", res_msg.thumb_size, res_msg.org_path);
154 thumb_dbg_slog("Sent thumbnail(%s) from %s", res_msg.dst_path, res_msg.org_path);
159 SAFE_FREE(res_msg.thumb_data);
166 static gboolean __thumb_server_send_msg_to_agent(int msg_type)
169 struct sockaddr_un serv_addr;
170 ms_thumb_server_msg send_msg;
172 if (ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock) < 0) {
173 thumb_err("ms_ipc_create_server_socket failed");
177 memset(&serv_addr, 0, sizeof(serv_addr));
179 serv_addr.sun_family = AF_UNIX;
180 SAFE_STRLCPY(serv_addr.sun_path, THUMB_COMM_SOCK_PATH, sizeof(serv_addr.sun_path));
182 /* Connecting to the thumbnail server */
183 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
184 thumb_stderror("connect");
186 return MS_MEDIA_ERR_SOCKET_CONN;
189 send_msg.msg_type = msg_type;
191 if (send(sock, &send_msg, sizeof(ms_thumb_server_msg), 0) < 0) {
192 thumb_stderror("send failed");
197 thumb_dbg("Sending msg to thumbnail agent[%d] is successful", send_msg.msg_type);
203 static gboolean _thumb_server_send_deny_message(int sockfd)
206 int bytes_to_send = sizeof(msg);
208 msg.msg_type = THUMB_RESPONSE;
209 msg.status = MS_MEDIA_ERR_PERMISSION_DENIED;
211 if (send(sockfd, &msg, bytes_to_send, 0) < 0) {
212 thumb_stderror("send failed");
219 gboolean _thumb_server_prepare_socket(int *sock_fd)
223 if (ms_ipc_create_server_socket(MS_THUMB_DAEMON_PORT, &sock) < 0) {
224 thumb_err("ms_ipc_create_server_socket failed");
228 if (ms_cynara_enable_credentials_passing(sock) != MS_MEDIA_ERR_NONE) {
229 thumb_err("ms_cynara_enable_credentials_passing failed");
239 int _thumbnail_get_data(const char *origin_path, char *thumb_path)
241 int err = MS_MEDIA_ERR_NONE;
242 int file_type = THUMB_NONE_TYPE;
244 if (origin_path == NULL) {
245 thumb_err("Original path is null");
246 return MS_MEDIA_ERR_INVALID_PARAMETER;
249 if (!g_file_test(origin_path, G_FILE_TEST_IS_REGULAR)) {
250 thumb_err("Original path (%s) does not exist", origin_path);
251 return MS_MEDIA_ERR_INVALID_PARAMETER;
254 thumb_dbg("Origin path : %s", origin_path);
255 file_type = _media_thumb_get_file_type(origin_path);
257 if (file_type == THUMB_IMAGE_TYPE) {
258 err = _media_thumb_image(origin_path, thumb_path, THUMB_DEFAULT_WIDTH, THUMB_DEFAULT_HEIGHT, NULL);
259 thumb_retvm_if(err != MS_MEDIA_ERR_NONE, err, "_media_thumb_image failed");
260 } else if (file_type == THUMB_VIDEO_TYPE) {
261 err = _media_thumb_video(origin_path, thumb_path, THUMB_DEFAULT_WIDTH, THUMB_DEFAULT_HEIGHT, NULL);
262 thumb_retvm_if(err != MS_MEDIA_ERR_NONE, err, "_media_thumb_video failed");
264 thumb_err("invalid file type");
265 return MS_MEDIA_ERR_THUMB_UNSUPPORTED;
268 thumb_dbg("Thumb data is generated successfully");
270 return MS_MEDIA_ERR_NONE;
273 int _thumbnail_get_raw_data(const char *origin_path, int *width, int *height, unsigned char **data, int *size)
275 int err = MS_MEDIA_ERR_NONE;
276 int thumb_width = -1;
277 int thumb_height = -1;
279 if (origin_path == NULL || *width <= 0 || *height <= 0) {
280 thumb_err("Invalid parameter");
281 return MS_MEDIA_ERR_INVALID_PARAMETER;
284 if (!g_file_test(origin_path, G_FILE_TEST_IS_REGULAR)) {
285 thumb_err("Original path (%s) does not exist", origin_path);
286 return MS_MEDIA_ERR_INVALID_PARAMETER;
289 int file_type = THUMB_NONE_TYPE;
290 media_thumb_info thumb_info = {0,};
291 file_type = _media_thumb_get_file_type(origin_path);
292 thumb_width = *width;
293 thumb_height = *height;
295 if (file_type == THUMB_IMAGE_TYPE) {
296 err = _media_thumb_image(origin_path, NULL, thumb_width, thumb_height, &thumb_info);
297 thumb_retvm_if(err != MS_MEDIA_ERR_NONE, err, "_media_thumb_image failed");
298 } else if (file_type == THUMB_VIDEO_TYPE) {
299 err = _media_thumb_video(origin_path, NULL, thumb_width, thumb_height, &thumb_info);
300 thumb_retvm_if(err != MS_MEDIA_ERR_NONE, err, "_media_thumb_video failed");
302 thumb_err("invalid file type");
303 return MS_MEDIA_ERR_THUMB_UNSUPPORTED;
306 if (size) *size = thumb_info.size;
307 *data = thumb_info.data;
308 *width = thumb_info.width;
309 *height = thumb_info.height;
311 return MS_MEDIA_ERR_NONE;
314 int _media_thumb_process(thumbMsg *req_msg, thumbMsg *res_msg)
316 int err = MS_MEDIA_ERR_NONE;
317 char *thumb_path = NULL;
319 thumb_retvm_if(req_msg == NULL || res_msg == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid msg");
321 const char *origin_path = req_msg->org_path;
322 memset(res_msg->dst_path, 0, MAX_FILEPATH_LEN);
323 thumb_path = res_msg->dst_path;
324 res_msg->status = MS_MEDIA_ERR_NONE;
326 if (!g_file_test(origin_path, G_FILE_TEST_IS_REGULAR)) {
327 thumb_err("origin_path does not exist in file system.");
328 res_msg->status = MS_MEDIA_ERR_FILE_NOT_EXIST;
329 return MS_MEDIA_ERR_FILE_NOT_EXIST;
332 err = _media_thumb_get_thumb_from_db(origin_path, thumb_path, MAX_FILEPATH_LEN, req_msg->uid);
333 if (err == MS_MEDIA_ERR_NONE) {
334 thumb_dbg_slog("Thumb path : %s", thumb_path);
335 return MS_MEDIA_ERR_NONE;
337 if (strlen(thumb_path) == 0) {
338 err = _media_thumb_get_hash_name(origin_path, thumb_path, MAX_FILEPATH_LEN, req_msg->uid);
339 if (err != MS_MEDIA_ERR_NONE) {
340 thumb_err("_media_thumb_get_hash_name failed - %d", err);
341 SAFE_STRLCPY(thumb_path, THUMB_EMPTY_STR, MAX_FILEPATH_LEN);
342 res_msg->status = err;
348 thumb_dbg_slog("Thumb path : %s", thumb_path);
350 err = _thumbnail_get_data(origin_path, thumb_path);
351 if (err != MS_MEDIA_ERR_NONE) {
352 thumb_err("_thumbnail_get_data failed - %d", err);
353 SAFE_STRLCPY(thumb_path, THUMB_EMPTY_STR, MAX_FILEPATH_LEN);
354 res_msg->status = err;
359 res_msg->msg_type = THUMB_RESPONSE;
362 err = _media_thumb_update_db(origin_path, thumb_path, req_msg->uid);
363 if (err != MS_MEDIA_ERR_NONE) {
364 thumb_err("_media_thumb_update_db failed : %d", err);
365 res_msg->status = err;
372 _media_thumb_process_raw(thumbMsg *req_msg, thumbMsg *res_msg)
374 int err = MS_MEDIA_ERR_NONE;
375 unsigned char *data = NULL;
380 thumb_retvm_if(req_msg == NULL || res_msg == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid msg");
382 const char *origin_path = req_msg->org_path;
383 if (req_msg->thumb_width > 0)
384 thumb_w = req_msg->thumb_width;
386 thumb_w = THUMB_DEFAULT_WIDTH;
388 if (req_msg->thumb_height > 0)
389 thumb_h = req_msg->thumb_height;
391 thumb_h = THUMB_DEFAULT_HEIGHT;
393 res_msg->status = MS_MEDIA_ERR_NONE;
394 res_msg->msg_type = THUMB_RESPONSE_RAW_DATA;
396 err = _thumbnail_get_raw_data(origin_path, &thumb_w, &thumb_h, &data, &thumb_size);
397 if (err != MS_MEDIA_ERR_NONE) {
398 thumb_err("_thumbnail_get_data failed - %d", err);
399 res_msg->status = err;
400 res_msg->thumb_size = 0;
406 res_msg->thumb_width = thumb_w;
407 res_msg->thumb_height = thumb_h;
408 res_msg->thumb_size = thumb_size;
409 res_msg->thumb_data = malloc(thumb_size * sizeof(unsigned char));
410 if (res_msg->thumb_data != NULL) {
411 memcpy(res_msg->thumb_data, data, thumb_size);
413 thumb_err("Allocation failed");
414 err = MS_MEDIA_ERR_OUT_OF_MEMORY;