2 * Copyright (c) 2014 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.
18 #include <media_info_private.h>
22 static int __media_face_check_media_id(const char *media_id)
24 int ret = MEDIA_CONTENT_ERROR_NONE;
25 char *query_str = NULL;
26 sqlite3_stmt *stmt = NULL;
29 media_content_retvm_if(!STRING_VALID(media_id), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid media_id");
31 query_str = sqlite3_mprintf(SELECT_MEDIA_COUNT_FROM_MEDIA_BY_ID, media_id);
32 ret = _content_query_prepare(&stmt, query_str, NULL, NULL);
33 SQLITE3_SAFE_FREE(query_str);
34 media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
36 while (sqlite3_step(stmt) == SQLITE_ROW)
37 item_count = (int)sqlite3_column_int(stmt, 0);
39 SQLITE3_FINALIZE(stmt);
41 media_content_retvm_if(item_count == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid media_id");
43 return MEDIA_CONTENT_ERROR_NONE;
46 int media_face_destroy(media_face_h face)
48 media_face_s *_face = (media_face_s*)face;
50 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
52 SAFE_FREE(_face->media_id);
53 SAFE_FREE(_face->face_tag);
56 return MEDIA_CONTENT_ERROR_NONE;
59 int media_face_clone(media_face_h *dst, media_face_h src)
61 media_face_s *_src = (media_face_s*)src;
63 media_content_retvm_if(src == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid src handle");
65 media_face_s *_dst = (media_face_s *)calloc(1, sizeof(media_face_s));
66 media_content_retvm_if(_dst == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
68 if (STRING_VALID(_src->media_id)) {
69 _dst->media_id = strdup(_src->media_id);
70 if (_dst->media_id == NULL) {
71 media_face_destroy((media_face_h)_dst);
72 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
73 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
77 _dst->face_id = _src->face_id;
78 _dst->face_rect_x = _src->face_rect_x;
79 _dst->face_rect_y = _src->face_rect_y;
80 _dst->face_rect_w = _src->face_rect_w;
81 _dst->face_rect_h = _src->face_rect_h;
82 _dst->orientation = _src->orientation;
84 if (STRING_VALID(_src->face_tag)) {
85 _dst->face_tag = strdup(_src->face_tag);
86 if (_dst->face_tag == NULL) {
87 media_face_destroy((media_face_h)_dst);
88 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
89 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
93 *dst = (media_face_h)_dst;
95 return MEDIA_CONTENT_ERROR_NONE;
98 static void __media_face_convert_itoa(int face_id, char **face_strid)
100 char buf[MAX_SIZE] = {0, };
102 snprintf(buf, MAX_SIZE, "%d", face_id);
103 *face_strid = strndup(buf, strlen(buf));
106 int media_face_get_face_id(media_face_h face, char **face_id)
108 media_face_s* _face = (media_face_s*)face;
110 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
112 if (_face->face_id > 0) {
113 __media_face_convert_itoa(_face->face_id, face_id);
114 media_content_retvm_if(*face_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "Out of memory");
119 return MEDIA_CONTENT_ERROR_NONE;
122 int media_face_get_media_id(media_face_h face, char **media_id)
124 media_face_s* _face = (media_face_s*)face;
126 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
128 if (STRING_VALID(_face->media_id)) {
129 *media_id = strdup(_face->media_id);
130 media_content_retvm_if(*media_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "Out of memory");
135 return MEDIA_CONTENT_ERROR_NONE;
138 int media_face_get_face_rect(media_face_h face, unsigned int *rect_x, unsigned int *rect_y, unsigned int *rect_w, unsigned int *rect_h)
140 media_face_s* _face = (media_face_s*)face;
142 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
143 media_content_retvm_if(!(rect_x && rect_y && rect_w && rect_h), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect");
145 *rect_x = _face->face_rect_x;
146 *rect_y = _face->face_rect_y;
147 *rect_w = _face->face_rect_w;
148 *rect_h = _face->face_rect_h;
150 return MEDIA_CONTENT_ERROR_NONE;
153 int media_face_get_orientation(media_face_h face, media_content_orientation_e *orientation)
155 media_face_s* _face = (media_face_s*)face;
157 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
158 media_content_retvm_if(orientation == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid orientation");
160 *orientation = _face->orientation;
162 return MEDIA_CONTENT_ERROR_NONE;
165 int media_face_get_tag(media_face_h face, char **tag)
167 media_face_s* _face = (media_face_s*)face;
169 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
171 if (STRING_VALID(_face->face_tag)) {
172 *tag = strdup(_face->face_tag);
173 media_content_retvm_if(*tag == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "Out of memory");
178 return MEDIA_CONTENT_ERROR_NONE;
181 int media_face_create(const char *media_id, media_face_h *face)
183 int ret = MEDIA_CONTENT_ERROR_NONE;
185 media_content_retvm_if(!STRING_VALID(media_id), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid media_id");
186 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
188 ret = __media_face_check_media_id(media_id);
189 media_content_retvm_if(ret != MEDIA_CONTENT_ERROR_NONE, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Not exist media_id");
191 media_face_s* _face = calloc(1, sizeof(media_face_s));
192 media_content_retvm_if(_face == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "Out of memory");
194 _face->media_id = strdup(media_id);
196 *face = (media_face_h)_face;
198 return MEDIA_CONTENT_ERROR_NONE;
201 int media_face_set_face_rect(media_face_h face, unsigned int rect_x, unsigned int rect_y, unsigned int rect_w, unsigned int rect_h)
203 media_face_s* _face = (media_face_s*)face;
205 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
206 media_content_retvm_if(rect_w == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect_w");
207 media_content_retvm_if(rect_h == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect_h");
209 _face->face_rect_x = rect_x;
210 _face->face_rect_y = rect_y;
211 _face->face_rect_w = rect_w;
212 _face->face_rect_h = rect_h;
214 return MEDIA_CONTENT_ERROR_NONE;
217 int media_face_set_orientation(media_face_h face, media_content_orientation_e orientation)
219 media_face_s* _face = (media_face_s*)face;
221 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
223 _face->orientation = orientation;
225 return MEDIA_CONTENT_ERROR_NONE;
228 int media_face_set_tag(media_face_h face, const char *tag)
230 media_face_s* _face = (media_face_s*)face;
232 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
234 if (STRING_VALID(tag))
235 _face->face_tag = strdup(tag);
237 _face->face_tag = NULL;
239 return MEDIA_CONTENT_ERROR_NONE;
242 int media_face_insert_to_db(media_face_h face)
244 int ret = MEDIA_CONTENT_ERROR_NONE;
245 char *query_str = NULL;
246 sqlite3_stmt *stmt = NULL;
248 media_face_s* _face = (media_face_s*)face;
250 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
251 media_content_retvm_if(_face->media_id == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid media_id");
252 media_content_retvm_if(_face->face_rect_w == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect_w");
253 media_content_retvm_if(_face->face_rect_h == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect_h");
255 query_str = sqlite3_mprintf(INSERT_FACE_TO_FACE, _face->media_id, _face->face_rect_x, _face->face_rect_y, _face->face_rect_w, _face->face_rect_h, _face->orientation, _face->face_tag);
257 ret = _content_query_sql(query_str);
258 SQLITE3_SAFE_FREE(query_str);
260 query_str = sqlite3_mprintf(SELECT_FACE_ID_USER_MARKED, _face->media_id, _face->face_rect_x, _face->face_rect_y, _face->face_rect_w, _face->face_rect_h, _face->orientation);
261 ret = _content_query_prepare(&stmt, query_str, NULL, NULL);
262 SQLITE3_SAFE_FREE(query_str);
263 media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
265 while (sqlite3_step(stmt) == SQLITE_ROW)
266 _face->face_id = (int)sqlite3_column_int(stmt, 0);
268 SQLITE3_FINALIZE(stmt);
274 int media_face_update_to_db(media_face_h face)
276 int ret = MEDIA_CONTENT_ERROR_NONE;
277 char *query_str = NULL;
279 media_face_s* _face = (media_face_s*)face;
281 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
282 media_content_retvm_if(_face->face_id == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid face_id");
283 media_content_retvm_if(_face->media_id == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid media_id");
284 media_content_retvm_if(_face->face_rect_w == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect_w");
285 media_content_retvm_if(_face->face_rect_h == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect_h");
287 query_str = sqlite3_mprintf(UPDATE_FACE_TO_FACE, _face->face_rect_x, _face->face_rect_y, _face->face_rect_w, _face->face_rect_h, _face->orientation, _face->face_tag, _face->face_id);
289 ret = _content_query_sql(query_str);
290 SQLITE3_SAFE_FREE(query_str);
295 static int __media_face_safe_atoi(const char *buffer, int *si)
299 media_content_retvm_if(buffer == NULL || si == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid parameter");
301 const long sl = strtol(buffer, &end, 10);
303 media_content_retvm_if(end == buffer, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "not a decimal number");
304 media_content_retvm_if('\0' != *end, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "extra characters at end of input: %s", end);
305 media_content_retvm_if((LONG_MIN == sl || LONG_MAX == sl) && (ERANGE == errno), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "out of range of type long");
306 media_content_retvm_if(sl > INT_MAX, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "greater than INT_MAX");
307 media_content_retvm_if(sl < INT_MIN, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "less than INT_MIN");
311 return MEDIA_CONTENT_ERROR_NONE;
314 int media_face_delete_from_db(const char *face_id)
316 int ret = MEDIA_CONTENT_ERROR_NONE;
317 char *query_str = NULL;
318 char *media_id = NULL;
319 int query_face_id = 0;
320 sqlite3_stmt *stmt = NULL;
321 media_content_retvm_if(!STRING_VALID(face_id), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid face_id");
323 ret = __media_face_safe_atoi(face_id, &query_face_id);
324 media_content_retvm_if(ret != MEDIA_CONTENT_ERROR_NONE, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid face_id");
326 /*To refresh face information(User try to start_face_detection again for the already scanned item),
327 mark media modified_time to '0' if user delete system extracted face info. */
329 query_str = sqlite3_mprintf(SELECT_MEDIA_FROM_FACE, query_face_id);
330 ret = _content_query_prepare(&stmt, query_str, NULL, NULL);
331 SQLITE3_SAFE_FREE(query_str);
332 media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
334 while (sqlite3_step(stmt) == SQLITE_ROW)
335 media_id = g_strdup((const char *)sqlite3_column_text(stmt, 0));
336 SQLITE3_FINALIZE(stmt);
338 if (STRING_VALID(media_id)) {
339 query_str = sqlite3_mprintf(UPDATE_MEDIA_INFO_IN_FACE_SCAN_LIST, media_id);
340 ret = _content_query_sql(query_str);
341 SQLITE3_SAFE_FREE(query_str);
345 query_str = sqlite3_mprintf(DELETE_FACE_FROM_FACE, query_face_id);
347 ret = _content_query_sql(query_str);
348 SQLITE3_SAFE_FREE(query_str);
353 int media_face_get_face_count_from_db(filter_h filter, int *face_count)
355 int ret = MEDIA_CONTENT_ERROR_NONE;
357 if (face_count == NULL) {
358 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
359 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
362 ret = _media_db_get_group_count(filter, MEDIA_GROUP_FACE, face_count);
367 int media_face_foreach_face_from_db(filter_h filter, media_face_cb callback, void *user_data)
369 int ret = MEDIA_CONTENT_ERROR_NONE;
371 media_content_retvm_if(callback == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid paramter");
373 ret = _media_db_get_face(NULL, filter, callback, user_data);