548f6668519c45a28dac27a6cf2ae96af76093dd
[platform/core/api/thumbnail-util.git] / src / thumbnail_util.c
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include <media-thumbnail.h>
18 #include <media-util.h>
19 #include <thumbnail_util.h>
20 #include <thumbnail_util_private.h>
21
22 #define MAX_SIZE 16
23
24 int __thumbnail_util_error_capi(int content_error)
25 {
26         /*Error None*/
27         if(content_error == MS_MEDIA_ERR_NONE)
28                 return THUMBNAIL_UTIL_ERROR_NONE;
29
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;
35
36         else if(content_error == MS_MEDIA_ERR_OUT_OF_MEMORY)
37                 return THUMBNAIL_UTIL_ERROR_OUT_OF_MEMORY;
38
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;
42
43         /* MEDIA SERVER error*/
44         else if(content_error == MS_MEDIA_ERR_PERMISSION_DENIED)
45                 return THUMBNAIL_UTIL_ERROR_PERMISSION_DENIED;
46
47         /*ETC*/
48         return THUMBNAIL_UTIL_ERROR_INVALID_OPERATION;
49 }
50
51 void __thumbnail_util_convert_itoa(int request_id, char **req_str)
52 {
53         char *buf = NULL;
54
55         buf = malloc(MAX_SIZE * sizeof(char));
56
57         if (buf != NULL) {
58                 snprintf(buf, MAX_SIZE, "%d", request_id);
59                 *req_str = strndup(buf, strlen(buf));
60                 SAFE_FREE(buf);
61         }
62 }
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)
64 {
65         thumbnail_extract_cb_s *_thumb_cb = (thumbnail_extract_cb_s *)user_data;
66         char *request_id_str = NULL;
67
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);
73
74         }
75         SAFE_FREE(_thumb_cb);
76 }
77
78 int thumbnail_util_create(thumbnail_h *thumb)
79 {
80         int ret = THUMBNAIL_UTIL_ERROR_NONE;
81
82         if (thumb == NULL) {
83                 thumbnail_util_error("INVALID_PARAMETER(0x%08x)", THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
84                 return THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
85         }
86
87         thumbnail_s *_thumb = (thumbnail_s *)malloc(sizeof(thumbnail_s));
88         if (_thumb == NULL) {
89                 thumbnail_util_error("OUT_OF_MEMORY(0x%08x)", THUMBNAIL_UTIL_ERROR_OUT_OF_MEMORY);
90                 return THUMBNAIL_UTIL_ERROR_OUT_OF_MEMORY;
91         }
92
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;
99
100         *thumb = (thumbnail_h)_thumb;
101
102         return ret;
103 }
104
105 int thumbnail_util_extract(thumbnail_h thumb, thumbnail_extracted_cb callback, void *user_data, char **request_id)
106 {
107         int ret = THUMBNAIL_UTIL_ERROR_NONE;
108         int res = 0;
109         static int g_thumbnail_req_id = 0;
110         thumbnail_s *_thumb = (thumbnail_s *)thumb;
111
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));
117
118                 if (_thumb_cb != NULL) {
119                         _thumb_cb->handle = _thumb;
120                         _thumb_cb->user_data = user_data;
121                         _thumb_cb->thumb_extract_cb = callback;
122                 }
123
124                 if (_thumb->dst_width == 0 || _thumb->dst_height == 0) {
125                         _thumb->dst_width = 320;
126                         _thumb->dst_height = 240;
127                 }
128
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);
131         } else {
132                 thumbnail_util_error("INVALID_PARAMETER(0x%08x)", THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
133                 ret = THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
134         }
135
136         return ret;
137 }
138
139 int thumbnail_util_set_path(thumbnail_h thumb, const char *path)
140 {
141         int ret = THUMBNAIL_UTIL_ERROR_NONE;
142         thumbnail_s *_thumb = (thumbnail_s *)thumb;
143
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));
148         } else {
149                 thumbnail_util_error("INVALID_PARAMETER(0x%08x)", THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
150                 ret = THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
151         }
152
153         return ret;
154 }
155
156 int thumbnail_util_set_size(thumbnail_h thumb, int width, int height)
157 {
158         int ret = THUMBNAIL_UTIL_ERROR_NONE;
159         thumbnail_s *_thumb = (thumbnail_s *)thumb;
160
161         if (_thumb != NULL && width > 0 && height > 0) {
162                 _thumb->dst_width = width;
163                 _thumb->dst_height = height;
164         } else {
165                 thumbnail_util_error("INVALID_PARAMETER(0x%08x)", THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
166                 ret = THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
167         }
168
169         return ret;
170 }
171
172 int thumbnail_util_cancel(thumbnail_h thumb, const char *request_id)
173 {
174         int ret = THUMBNAIL_UTIL_ERROR_NONE;
175         thumbnail_s *_thumb = (thumbnail_s *)thumb;
176
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);
180         } else {
181                 thumbnail_util_error("INVALID_PARAMETER(0x%08x)", THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
182                 ret = THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
183         }
184
185         return ret;
186 }
187
188 int thumbnail_util_cancel_all(thumbnail_h thumb)
189 {
190         int ret = THUMBNAIL_UTIL_ERROR_NONE;
191         thumbnail_s *_thumb = (thumbnail_s *)thumb;
192
193         if (_thumb != NULL) {
194                 ret = thumbnail_request_cancel_all(true);
195         } else {
196                 thumbnail_util_error("INVALID_PARAMETER(0x%08x)", THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
197                 ret = THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
198         }
199
200         return ret;
201 }
202
203
204 int thumbnail_util_destroy(thumbnail_h thumb)
205 {
206         int ret = THUMBNAIL_UTIL_ERROR_NONE;
207         thumbnail_s *_thumb = (thumbnail_s *)thumb;
208
209         if (_thumb) {
210                 SAFE_FREE(_thumb->file_path);
211                 SAFE_FREE(_thumb->thumb_data);
212                 SAFE_FREE(_thumb);
213         } else {
214                 thumbnail_util_error("INVALID_PARAMETER(0x%08x)", THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
215                 ret = THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
216         }
217
218         return ret;
219 }