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"
34 #define LOG_TAG "MEDIA_THUMBNAIL_SERVER"
36 static __thread char **arr_path;
37 static __thread uid_t *arr_uid;
38 static __thread int g_idx = 0;
39 static __thread int g_cur_idx = 0;
41 GMainLoop *g_thumb_server_mainloop; // defined in thumb-server.c as extern
43 #ifdef _USE_MEDIA_UTIL_
44 gboolean _thumb_server_send_msg_to_agent(int msg_type);
46 void _thumb_daemon_stop_job();
48 gboolean _thumb_daemon_start_jobs(gpointer data)
52 #ifdef _USE_MEDIA_UTIL_
53 _thumb_server_send_msg_to_agent(MS_MSG_THUMB_SERVER_READY);
58 void _thumb_daemon_finish_jobs()
60 sqlite3 *sqlite_db_handle = _media_thumb_db_get_handle();
62 if (sqlite_db_handle != NULL) {
63 _media_thumb_db_disconnect();
64 thumb_dbg("sqlite3 handle is alive. So disconnect to sqlite3");
67 g_main_loop_quit(g_thumb_server_mainloop);
72 int _thumb_daemon_mmc_status()
77 err = vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &status);
80 } else if (err == -1) {
81 thumb_err("vconf_get_int failed : %d", err);
83 thumb_err("vconf_get_int Unexpected error code: %d", err);
89 void _thumb_daemon_mmc_eject_vconf_cb(void *data)
94 thumb_warn("_thumb_daemon_vconf_cb called");
96 err = vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &status);
98 if (status == VCONFKEY_SYSMAN_MMC_REMOVED || status == VCONFKEY_SYSMAN_MMC_INSERTED_NOT_MOUNTED) {
99 thumb_warn("SD card is ejected or not mounted. So media-thumbnail-server stops jobs to extract all thumbnails");
101 _thumb_daemon_stop_job();
103 } else if (err == -1) {
104 thumb_err("vconf_get_int failed : %d", err);
106 thumb_err("vconf_get_int Unexpected error code: %d", err);
112 void _thumb_daemon_vconf_cb(void *data)
117 thumb_warn("_thumb_daemon_vconf_cb called");
119 err = vconf_get_int(VCONFKEY_SYSMAN_MMC_FORMAT, &status);
121 if (status == VCONFKEY_SYSMAN_MMC_FORMAT_COMPLETED) {
122 thumb_warn("SD card format is completed. So media-thumbnail-server stops jobs to extract all thumbnails");
124 _thumb_daemon_stop_job();
126 thumb_dbg("not completed");
128 } else if (err == -1) {
129 thumb_err("vconf_get_int failed : %d", err);
131 thumb_err("vconf_get_int Unexpected error code: %d", err);
137 void _thumb_daemon_stop_job()
142 thumb_warn("There are %d jobs in the queue. But all jobs will be stopped", g_idx - g_cur_idx);
144 for (i = g_cur_idx; i < g_idx; i++) {
145 path = arr_path[g_cur_idx++];
152 int _thumb_daemon_process_job(thumbMsg *req_msg, thumbMsg *res_msg, uid_t uid)
156 err = _media_thumb_process(req_msg, res_msg, uid);
158 if (req_msg->msg_type == THUMB_REQUEST_SAVE_FILE) {
159 thumb_err("_media_thumb_process is failed: %d", err);
160 res_msg->status = THUMB_FAIL;
162 thumb_warn("_media_thumb_process is failed: %d, So use default thumb", err);
163 res_msg->status = THUMB_SUCCESS;
166 res_msg->status = THUMB_SUCCESS;
172 int _thumb_daemon_all_extract(uid_t uid)
176 char query_string[MAX_PATH_SIZE + 1] = { 0, };
177 char path[MAX_PATH_SIZE + 1] = { 0, };
178 sqlite3 *sqlite_db_handle = NULL;
179 sqlite3_stmt *sqlite_stmt = NULL;
181 err = _media_thumb_db_connect(uid);
183 thumb_err("_media_thumb_db_connect failed: %d", err);
184 return MEDIA_THUMB_ERROR_DB;
187 sqlite_db_handle = _media_thumb_db_get_handle();
188 if (sqlite_db_handle == NULL) {
189 thumb_err("sqlite handle is NULL");
190 return MEDIA_THUMB_ERROR_DB;
193 if (_thumb_daemon_mmc_status() == VCONFKEY_SYSMAN_MMC_MOUNTED) {
194 snprintf(query_string, sizeof(query_string), SELECT_PATH_FROM_UNEXTRACTED_THUMB_MEDIA);
196 snprintf(query_string, sizeof(query_string), SELECT_PATH_FROM_UNEXTRACTED_THUMB_INTERNAL_MEDIA);
199 thumb_warn("Query: %s", query_string);
201 err = sqlite3_prepare_v2(sqlite_db_handle, query_string, strlen(query_string), &sqlite_stmt, NULL);
202 if (SQLITE_OK != err) {
203 thumb_err("prepare error [%s]\n", sqlite3_errmsg(sqlite_db_handle));
204 _media_thumb_db_disconnect();
205 return MEDIA_THUMB_ERROR_DB;
209 err = sqlite3_step(sqlite_stmt);
210 if (err != SQLITE_ROW) {
211 thumb_dbg("end of row [%s]\n", sqlite3_errmsg(sqlite_db_handle));
215 strncpy(path, (const char *)sqlite3_column_text(sqlite_stmt, 0), sizeof(path));
216 path[sizeof(path) - 1] = '\0';
217 count = sqlite3_column_int(sqlite_stmt, 1);
219 thumb_dbg("Path : %s", path);
222 arr_path = (char**)malloc(sizeof(char*));
223 arr_uid = (uid_t*)malloc(sizeof(uid_t));
225 arr_path = (char**)realloc(arr_path, (g_idx + 1) * sizeof(char*));
226 arr_uid = (uid_t*)realloc(arr_uid, (g_idx + 1) * sizeof(uid_t));
228 arr_uid[g_idx] = uid;
229 arr_path[g_idx++] = strdup(path);
232 sqlite3_finalize(sqlite_stmt);
233 _media_thumb_db_disconnect();
235 return MEDIA_THUMB_ERROR_NONE;
238 int _thumb_daemon_process_queue_jobs(gpointer data)
244 if (g_cur_idx < g_idx) {
245 thumb_dbg("There are %d jobs in the queue", g_idx - g_cur_idx);
246 thumb_dbg("Current idx : [%d]", g_cur_idx);
247 uid = arr_uid[g_cur_idx];
248 path = arr_path[g_cur_idx++];
250 thumbMsg recv_msg, res_msg;
251 memset(&recv_msg, 0x00, sizeof(thumbMsg));
252 memset(&res_msg, 0x00, sizeof(thumbMsg));
254 recv_msg.msg_type = THUMB_REQUEST_DB_INSERT;
255 recv_msg.thumb_type = MEDIA_THUMB_LARGE;
256 strncpy(recv_msg.org_path, path, sizeof(recv_msg.org_path));
257 recv_msg.org_path[sizeof(recv_msg.org_path) - 1] = '\0';
259 _thumb_daemon_process_job(&recv_msg, &res_msg,uid );
261 if (res_msg.status == THUMB_SUCCESS) {
263 err = _media_thumb_db_connect(uid);
265 thumb_err("_media_thumb_mb_svc_connect failed: %d", err);
269 /* Need to update DB once generating thumb is done */
270 err = _media_thumb_update_db(recv_msg.org_path,
272 res_msg.origin_width,
273 res_msg.origin_height,
276 thumb_err("_media_thumb_update_db failed : %d", err);
279 _media_thumb_db_disconnect();
286 thumb_warn("Deleting array");
289 //_media_thumb_db_disconnect();
291 _thumb_server_send_msg_to_agent(MS_MSG_THUMB_EXTRACT_ALL_DONE); // MS_MSG_THUMB_EXTRACT_ALL_DONE
299 gboolean _thumb_server_read_socket(GIOChannel *src,
300 GIOCondition condition,
303 #ifdef _USE_UDS_SOCKET_
304 struct sockaddr_un client_addr;
306 struct sockaddr_in client_addr;
308 unsigned int client_addr_len;
309 #ifndef _USE_MEDIA_UTIL_
319 memset((void *)&recv_msg, 0, sizeof(recv_msg));
320 memset((void *)&res_msg, 0, sizeof(res_msg));
322 sock = g_io_channel_unix_get_fd(src);
324 thumb_err("sock fd is invalid!");
328 header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2;
330 #ifndef _USE_MEDIA_UTIL_
331 if ((client_sock = accept(sock, (struct sockaddr*)&client_addr, &client_addr_len)) < 0) {
332 thumb_err("accept failed : %s", strerror(errno));
336 thumb_dbg("Client[%d] is accepted", client_sock);
339 if (_media_thumb_recv_msg(client_sock, header_size, &recv_msg) < 0) {
340 thumb_err("_media_thumb_recv_msg failed ");
346 if (_media_thumb_recv_udp_msg(sock, header_size, &recv_msg, &client_addr, &client_addr_len) < 0) {
347 thumb_err("_media_thumb_recv_udp_msg failed");
352 thumb_warn("Received [%d] %s(%d) from PID(%d) \n", recv_msg.msg_type, recv_msg.org_path, strlen(recv_msg.org_path), recv_msg.pid);
354 if (recv_msg.msg_type == THUMB_REQUEST_ALL_MEDIA) {
355 thumb_dbg("All thumbnails are being extracted now");
356 _thumb_daemon_all_extract(recv_msg.uid);
357 g_idle_add(_thumb_daemon_process_queue_jobs, NULL);
358 } else if(recv_msg.msg_type == THUMB_REQUEST_KILL_SERVER) {
359 thumb_warn("received KILL msg from thumbnail agent.");
361 long start = thumb_get_debug_time();
363 _thumb_daemon_process_job(&recv_msg, &res_msg,recv_msg.uid);
365 long end = thumb_get_debug_time();
366 thumb_dbg("Time : %f (%s)", ((double)(end - start) / (double)CLOCKS_PER_SEC), recv_msg.org_path);
369 res_msg.msg_type = recv_msg.msg_type;
370 strncpy(res_msg.org_path, recv_msg.org_path, recv_msg.origin_path_size);
371 res_msg.origin_path_size = recv_msg.origin_path_size;
372 res_msg.dest_path_size = strlen(res_msg.dst_path) + 1;
375 unsigned char *buf = NULL;
376 _media_thumb_set_buffer(&res_msg, &buf, &buf_size);
378 //thumb_dbg("buffer size : %d", buf_size);
380 #ifndef _USE_MEDIA_UTIL_
381 if (send(client_sock, buf, buf_size, 0) != buf_size) {
382 thumb_err("sendto failed : %s", strerror(errno));
384 thumb_dbg("Sent %s(%d) \n", res_msg.dst_path, strlen(res_msg.dst_path));
389 #ifdef _USE_UDS_SOCKET_
390 thumb_dbg("+++++++++++++++++++++%s", client_addr.sun_path);
392 if (sendto(sock, buf, buf_size, 0, (struct sockaddr *)&client_addr, sizeof(client_addr)) != buf_size) {
393 thumb_err("sendto failed: %s\n", strerror(errno));
399 thumb_warn("Sent %s(%d)", res_msg.dst_path, strlen(res_msg.dst_path));
403 if(recv_msg.msg_type == THUMB_REQUEST_KILL_SERVER) {
404 thumb_warn("Shutting down...");
405 g_main_loop_quit(g_thumb_server_mainloop);
411 #ifdef _USE_MEDIA_UTIL_
412 gboolean _thumb_server_send_msg_to_agent(int msg_type)
415 const char *serv_ip = "127.0.0.1";
416 #ifdef _USE_UDS_SOCKET_
417 struct sockaddr_un serv_addr;
419 struct sockaddr_in serv_addr;
421 ms_thumb_server_msg send_msg;
423 #ifdef _USE_UDS_SOCKET_
424 if (ms_ipc_create_client_socket(MS_PROTOCOL_UDP, MS_TIMEOUT_SEC_10, &sock, MS_THUMB_COMM_PORT) < 0) {
426 if (ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock) < 0) {
428 thumb_err("ms_ipc_create_server_socket failed");
432 memset(&serv_addr, 0, sizeof(serv_addr));
433 #ifdef _USE_UDS_SOCKET_
434 serv_addr.sun_family = AF_UNIX;
435 strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcomm.socket");
437 serv_addr.sin_family = AF_INET;
438 serv_addr.sin_addr.s_addr = inet_addr(serv_ip);
439 serv_addr.sin_port = htons(MS_THUMB_COMM_PORT);
442 send_msg.msg_type = msg_type;
445 if (connect(sock, &serv_addr, sizeof(serv_addr)) < 0) {
446 thumb_err("connect failed [%s]",strerror(errno));
451 if (send(sock, &send_msg, sizeof(ms_thumb_server_msg), 0) != sizeof(ms_thumb_server_msg)) {
452 thumb_err("sendto failed: %s\n", strerror(errno));
457 thumb_dbg("Sending msg to thumbnail agent[%d] is successful", send_msg.msg_type);
464 gboolean _thumb_server_prepare_socket(int *sock_fd)
467 unsigned short serv_port;
472 memset((void *)&recv_msg, 0, sizeof(recv_msg));
473 memset((void *)&res_msg, 0, sizeof(res_msg));
474 #ifdef _USE_MEDIA_UTIL_
475 serv_port = MS_THUMB_DAEMON_PORT;
477 if (ms_ipc_create_server_socket(MS_PROTOCOL_UDP, serv_port, &sock) < 0) {
478 thumb_err("ms_ipc_create_server_socket failed");
482 char thumb_path[MAX_PATH_SIZE + 1];
483 #ifdef _USE_UDS_SOCKET_
484 struct sockaddr_un serv_addr;
486 struct sockaddr_in serv_addr;
488 serv_port = THUMB_DAEMON_PORT;
490 /* Creaete a TCP socket */
491 if (_media_thumb_create_socket(SERVER_SOCKET, &sock) < 0) {
492 thumb_err("_media_thumb_create_socket failed");
496 memset(thumb_path, 0, sizeof(thumb_path));
497 memset(&serv_addr, 0, sizeof(serv_addr));
498 serv_addr.sin_family = AF_INET;
499 serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
500 serv_addr.sin_port = htons(serv_port);
502 /* Bind to the local address */
503 if (bind(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
504 thumb_err("bind failed : %s", strerror(errno));
508 thumb_dbg("bind success");
511 if (listen(sock, SOMAXCONN) < 0) {
512 thumb_err("listen failed : %s", strerror(errno));
515 thumb_dbg("Listening...");