2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <media-thumbnail.h>
18 #include <media-util.h>
19 #include <thumbnail_util.h>
20 #include <thumbnail_util_private.h>
24 #define MAX_PATH_SIZE 4096
26 int __thumbnail_util_replace_path(const char *path, char *replace_path)
28 int ret = THUMBNAIL_UTIL_ERROR_NONE;
29 char *old_path = NULL;
31 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &old_path);
32 if (ret != STORAGE_ERROR_NONE) {
33 thumbnail_util_error("storage_get_directory failed");
34 return THUMBNAIL_UTIL_ERROR_INVALID_OPERATION;
37 if (strncmp(path, old_path, strlen(old_path)) == 0) {
38 thumbnail_util_sec_debug("Old path[%s]", path);
39 snprintf(replace_path, MAX_PATH_SIZE, "%s%s", tzplatform_getenv(TZ_USER_CONTENT), path + strlen(old_path));
41 snprintf(replace_path, MAX_PATH_SIZE, "%s", path);
46 if (!STRING_VALID(replace_path)) {
47 thumbnail_util_error("replace failed");
48 return THUMBNAIL_UTIL_ERROR_INVALID_OPERATION;
51 return THUMBNAIL_UTIL_ERROR_NONE;
54 int __thumbnail_util_error_capi(int content_error)
57 if (content_error == MS_MEDIA_ERR_NONE)
58 return THUMBNAIL_UTIL_ERROR_NONE;
60 /* Internal operation error*/
61 else if ((content_error == MS_MEDIA_ERR_INVALID_PARAMETER) ||
62 (content_error == MS_MEDIA_ERR_INVALID_PATH) ||
63 (content_error == MS_MEDIA_ERR_THUMB_DUPLICATED_REQUEST))
64 return THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
66 else if (content_error == MS_MEDIA_ERR_OUT_OF_MEMORY)
67 return THUMBNAIL_UTIL_ERROR_OUT_OF_MEMORY;
69 /* IPC operation error*/
70 else if ((content_error <= MS_MEDIA_ERR_SOCKET_CONN) && (content_error >= MS_MEDIA_ERR_SOCKET_INTERNAL))
71 return THUMBNAIL_UTIL_ERROR_INVALID_OPERATION;
73 /* MEDIA SERVER error*/
74 else if (content_error == MS_MEDIA_ERR_PERMISSION_DENIED)
75 return THUMBNAIL_UTIL_ERROR_PERMISSION_DENIED;
78 return THUMBNAIL_UTIL_ERROR_INVALID_OPERATION;
81 void __thumbnail_util_convert_itoa(int request_id, char **req_str)
83 char buf[MAX_SIZE] = {0, };
85 snprintf(buf, MAX_SIZE, "%d", request_id);
86 *req_str = strndup(buf, strlen(buf));
88 void __thumbnail_util_extract_completed_cb(int error, int request_id, const char *path, int thumb_width, int thumb_height, unsigned char *thumb_data, int thumb_size, void *user_data)
90 thumbnail_extract_cb_s *_thumb_cb = (thumbnail_extract_cb_s *)user_data;
91 char *request_id_str = NULL;
93 if (_thumb_cb != NULL) {
94 __thumbnail_util_convert_itoa(request_id, &request_id_str);
95 if (_thumb_cb->thumb_extract_cb)
96 _thumb_cb->thumb_extract_cb(error, request_id_str, thumb_width, thumb_height, thumb_data, thumb_size, _thumb_cb->user_data);
102 int thumbnail_util_create(thumbnail_h *thumb)
104 int ret = THUMBNAIL_UTIL_ERROR_NONE;
107 thumbnail_util_error("INVALID_PARAMETER(0x%08x)", THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
108 return THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
111 thumbnail_s *_thumb = (thumbnail_s *)malloc(sizeof(thumbnail_s));
112 if (_thumb == NULL) {
113 thumbnail_util_error("OUT_OF_MEMORY(0x%08x)", THUMBNAIL_UTIL_ERROR_OUT_OF_MEMORY);
114 return THUMBNAIL_UTIL_ERROR_OUT_OF_MEMORY;
117 _thumb->request_id = 0;
118 _thumb->file_path = NULL;
119 _thumb->dst_width = 0;
120 _thumb->dst_height = 0;
121 _thumb->thumb_size = 0;
122 _thumb->thumb_data = NULL;
124 *thumb = (thumbnail_h)_thumb;
129 int thumbnail_util_extract(thumbnail_h thumb, thumbnail_extracted_cb callback, void *user_data, char **request_id)
131 int ret = THUMBNAIL_UTIL_ERROR_NONE;
133 static int g_thumbnail_req_id = 0;
134 thumbnail_s *_thumb = (thumbnail_s *)thumb;
136 if (_thumb != NULL && STRING_VALID(_thumb->file_path)) {
137 g_thumbnail_req_id++;
138 _thumb->request_id = g_thumbnail_req_id;
139 __thumbnail_util_convert_itoa(_thumb->request_id, request_id);
140 thumbnail_extract_cb_s *_thumb_cb = (thumbnail_extract_cb_s *)calloc(1, sizeof(thumbnail_extract_cb_s));
142 if (_thumb_cb != NULL) {
143 _thumb_cb->handle = _thumb;
144 _thumb_cb->user_data = user_data;
145 _thumb_cb->thumb_extract_cb = callback;
148 if (_thumb->dst_width == 0 || _thumb->dst_height == 0) {
149 _thumb->dst_width = 320;
150 _thumb->dst_height = 240;
153 res = thumbnail_request_extract_raw_data_async(_thumb->request_id, _thumb->file_path, _thumb->dst_width, _thumb->dst_height, (ThumbRawFunc)__thumbnail_util_extract_completed_cb, (void *)_thumb_cb, tzplatform_getuid(TZ_USER_NAME));
154 ret = __thumbnail_util_error_capi(res);
156 thumbnail_util_error("INVALID_PARAMETER(0x%08x)", THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
157 ret = THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
163 int thumbnail_util_set_path(thumbnail_h thumb, const char *path)
165 int ret = THUMBNAIL_UTIL_ERROR_NONE;
166 char repl_path[MAX_PATH_SIZE + 1] = {0, };
167 thumbnail_s *_thumb = (thumbnail_s *)thumb;
169 if (_thumb != NULL && path != NULL) {
170 SAFE_FREE(_thumb->file_path);
171 memset(repl_path, 0, sizeof(repl_path));
172 ret = __thumbnail_util_replace_path(path, repl_path);
173 if (ret != THUMBNAIL_UTIL_ERROR_NONE) {
174 thumbnail_util_error("Convert path failed");
175 _thumb->file_path = NULL;
177 _thumb->file_path = strndup(repl_path, strlen(repl_path));
180 thumbnail_util_error("INVALID_PARAMETER(0x%08x)", THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
181 ret = THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
187 int thumbnail_util_set_size(thumbnail_h thumb, int width, int height)
189 int ret = THUMBNAIL_UTIL_ERROR_NONE;
190 thumbnail_s *_thumb = (thumbnail_s *)thumb;
192 if (_thumb != NULL && width > 0 && height > 0) {
193 _thumb->dst_width = width;
194 _thumb->dst_height = height;
196 thumbnail_util_error("INVALID_PARAMETER(0x%08x)", THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
197 ret = THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
203 int thumbnail_util_cancel(thumbnail_h thumb, const char *request_id)
205 int ret = THUMBNAIL_UTIL_ERROR_NONE;
206 thumbnail_s *_thumb = (thumbnail_s *)thumb;
208 if (_thumb != NULL && STRING_VALID(request_id)) {
209 unsigned int request_id_integer = atoi(request_id);
210 ret = thumbnail_request_cancel_raw_data(request_id_integer);
212 thumbnail_util_error("INVALID_PARAMETER(0x%08x)", THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
213 ret = THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
219 int thumbnail_util_cancel_all(thumbnail_h thumb)
221 int ret = THUMBNAIL_UTIL_ERROR_NONE;
222 thumbnail_s *_thumb = (thumbnail_s *)thumb;
224 if (_thumb != NULL) {
225 ret = thumbnail_request_cancel_all(true);
227 thumbnail_util_error("INVALID_PARAMETER(0x%08x)", THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
228 ret = THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
235 int thumbnail_util_destroy(thumbnail_h thumb)
237 int ret = THUMBNAIL_UTIL_ERROR_NONE;
238 thumbnail_s *_thumb = (thumbnail_s *)thumb;
241 SAFE_FREE(_thumb->file_path);
242 SAFE_FREE(_thumb->thumb_data);
245 thumbnail_util_error("INVALID_PARAMETER(0x%08x)", THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
246 ret = THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;