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 int __thumbnail_util_error_capi(int content_error)
27 if (content_error == MS_MEDIA_ERR_NONE)
28 return THUMBNAIL_UTIL_ERROR_NONE;
30 /* Internal operation error*/
31 else if ((content_error == MS_MEDIA_ERR_INVALID_PARAMETER) ||
32 (content_error == MS_MEDIA_ERR_INVALID_PATH) ||
33 (content_error == MS_MEDIA_ERR_THUMB_DUPLICATED_REQUEST))
34 return THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
36 else if (content_error == MS_MEDIA_ERR_OUT_OF_MEMORY)
37 return THUMBNAIL_UTIL_ERROR_OUT_OF_MEMORY;
39 /* IPC operation error*/
40 else if ((content_error <= MS_MEDIA_ERR_SOCKET_CONN) && (content_error >= MS_MEDIA_ERR_SOCKET_INTERNAL))
41 return THUMBNAIL_UTIL_ERROR_INVALID_OPERATION;
43 /* MEDIA SERVER error*/
44 else if (content_error == MS_MEDIA_ERR_PERMISSION_DENIED)
45 return THUMBNAIL_UTIL_ERROR_PERMISSION_DENIED;
48 return THUMBNAIL_UTIL_ERROR_INVALID_OPERATION;
51 void __thumbnail_util_convert_itoa(int request_id, char **req_str)
55 buf = malloc(MAX_SIZE * sizeof(char));
58 snprintf(buf, MAX_SIZE, "%d", request_id);
59 *req_str = strndup(buf, strlen(buf));
63 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)
65 thumbnail_extract_cb_s *_thumb_cb = (thumbnail_extract_cb_s *)user_data;
66 char *request_id_str = NULL;
68 if (_thumb_cb != NULL) {
69 /*media_content_debug("error [%d], thumbnail_size [%d]", error, thumb_size); */
70 __thumbnail_util_convert_itoa(request_id, &request_id_str);
71 if (_thumb_cb->thumb_extract_cb)
72 _thumb_cb->thumb_extract_cb(error, request_id_str, thumb_width, thumb_height, thumb_data, thumb_size, _thumb_cb->user_data);
78 int thumbnail_util_create(thumbnail_h *thumb)
80 int ret = THUMBNAIL_UTIL_ERROR_NONE;
83 thumbnail_util_error("INVALID_PARAMETER(0x%08x)", THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
84 return THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
87 thumbnail_s *_thumb = (thumbnail_s *)malloc(sizeof(thumbnail_s));
89 thumbnail_util_error("OUT_OF_MEMORY(0x%08x)", THUMBNAIL_UTIL_ERROR_OUT_OF_MEMORY);
90 return THUMBNAIL_UTIL_ERROR_OUT_OF_MEMORY;
93 _thumb->request_id = 0;
94 _thumb->file_path = NULL;
95 _thumb->dst_width = 0;
96 _thumb->dst_height = 0;
97 _thumb->thumb_size = 0;
98 _thumb->thumb_data = NULL;
100 *thumb = (thumbnail_h)_thumb;
105 int thumbnail_util_extract(thumbnail_h thumb, thumbnail_extracted_cb callback, void *user_data, char **request_id)
107 int ret = THUMBNAIL_UTIL_ERROR_NONE;
109 static int g_thumbnail_req_id = 0;
110 thumbnail_s *_thumb = (thumbnail_s *)thumb;
112 if (_thumb != NULL && STRING_VALID(_thumb->file_path)) {
113 g_thumbnail_req_id++;
114 _thumb->request_id = g_thumbnail_req_id;
115 __thumbnail_util_convert_itoa(_thumb->request_id, request_id);
116 thumbnail_extract_cb_s *_thumb_cb = (thumbnail_extract_cb_s *)calloc(1, sizeof(thumbnail_extract_cb_s));
118 if (_thumb_cb != NULL) {
119 _thumb_cb->handle = _thumb;
120 _thumb_cb->user_data = user_data;
121 _thumb_cb->thumb_extract_cb = callback;
124 if (_thumb->dst_width == 0 || _thumb->dst_height == 0) {
125 _thumb->dst_width = 320;
126 _thumb->dst_height = 240;
129 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));
130 ret = __thumbnail_util_error_capi(res);
132 thumbnail_util_error("INVALID_PARAMETER(0x%08x)", THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
133 ret = THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
139 int thumbnail_util_set_path(thumbnail_h thumb, const char *path)
141 int ret = THUMBNAIL_UTIL_ERROR_NONE;
142 thumbnail_s *_thumb = (thumbnail_s *)thumb;
144 if (_thumb != NULL && path != NULL) {
145 if (_thumb->file_path)
146 SAFE_FREE(_thumb->file_path);
147 _thumb->file_path = strndup(path, strlen(path));
149 thumbnail_util_error("INVALID_PARAMETER(0x%08x)", THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
150 ret = THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
156 int thumbnail_util_set_size(thumbnail_h thumb, int width, int height)
158 int ret = THUMBNAIL_UTIL_ERROR_NONE;
159 thumbnail_s *_thumb = (thumbnail_s *)thumb;
161 if (_thumb != NULL && width > 0 && height > 0) {
162 _thumb->dst_width = width;
163 _thumb->dst_height = height;
165 thumbnail_util_error("INVALID_PARAMETER(0x%08x)", THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
166 ret = THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
172 int thumbnail_util_cancel(thumbnail_h thumb, const char *request_id)
174 int ret = THUMBNAIL_UTIL_ERROR_NONE;
175 thumbnail_s *_thumb = (thumbnail_s *)thumb;
177 if (_thumb != NULL && STRING_VALID(request_id)) {
178 unsigned int request_id_integer = atoi(request_id);
179 ret = thumbnail_request_cancel_raw_data(request_id_integer);
181 thumbnail_util_error("INVALID_PARAMETER(0x%08x)", THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
182 ret = THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
188 int thumbnail_util_cancel_all(thumbnail_h thumb)
190 int ret = THUMBNAIL_UTIL_ERROR_NONE;
191 thumbnail_s *_thumb = (thumbnail_s *)thumb;
193 if (_thumb != NULL) {
194 ret = thumbnail_request_cancel_all(true);
196 thumbnail_util_error("INVALID_PARAMETER(0x%08x)", THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
197 ret = THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
204 int thumbnail_util_destroy(thumbnail_h thumb)
206 int ret = THUMBNAIL_UTIL_ERROR_NONE;
207 thumbnail_s *_thumb = (thumbnail_s *)thumb;
210 SAFE_FREE(_thumb->file_path);
211 SAFE_FREE(_thumb->thumb_data);
214 thumbnail_util_error("INVALID_PARAMETER(0x%08x)", THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
215 ret = THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;