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.
17 #include <media_face.h>
18 #include <media_info_private.h>
20 static int __media_face_check_media_id(const char *media_id)
22 int ret = MEDIA_CONTENT_ERROR_NONE;
23 char *query_str = NULL;
24 sqlite3_stmt *stmt = NULL;
27 media_content_retvm_if(!STRING_VALID(media_id), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid media_id");
29 query_str = sqlite3_mprintf(SELECT_MEDIA_COUNT_FROM_MEDIA_BY_ID, media_id);
30 ret = _content_query_prepare(&stmt, query_str, NULL, NULL);
31 SQLITE3_SAFE_FREE(query_str);
32 media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
34 while(sqlite3_step(stmt) == SQLITE_ROW)
36 item_count = (int)sqlite3_column_int(stmt, 0);
38 SQLITE3_FINALIZE(stmt);
40 media_content_retvm_if(item_count == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid media_id");
42 return MEDIA_CONTENT_ERROR_NONE;
45 int media_face_clone(media_face_h *dst, media_face_h src)
47 media_face_s *_src = (media_face_s*)src;
49 media_content_retvm_if(src == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid src handle");
51 media_face_s *_dst = (media_face_s *)calloc(1, sizeof(media_face_s));
52 media_content_retvm_if(_dst == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
54 if (STRING_VALID(_src->face_id))
56 _dst->face_id = strdup(_src->face_id);
57 if(_dst->face_id== NULL)
59 media_face_destroy((media_face_h)_dst);
60 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
61 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
65 if (STRING_VALID(_src->media_id))
67 _dst->media_id = strdup(_src->media_id);
68 if(_dst->media_id== NULL)
70 media_face_destroy((media_face_h)_dst);
71 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
72 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
76 _dst->face_rect_x = _src->face_rect_x;
77 _dst->face_rect_y = _src->face_rect_y;
78 _dst->face_rect_w = _src->face_rect_w;
79 _dst->face_rect_h = _src->face_rect_h;
80 _dst->orientation = _src->orientation;
82 if (STRING_VALID(_src->face_tag))
84 _dst->face_tag = strdup(_src->face_tag);
85 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 int media_face_destroy(media_face_h face)
100 media_face_s *_face = (media_face_s*)face;
102 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
104 SAFE_FREE(_face->face_id);
105 SAFE_FREE(_face->media_id);
106 SAFE_FREE(_face->face_tag);
109 return MEDIA_CONTENT_ERROR_NONE;
112 int media_face_get_face_id(media_face_h face, char **face_id)
114 media_face_s* _face = (media_face_s*)face;
116 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
118 if(STRING_VALID(_face->face_id))
120 *face_id = strdup(_face->face_id);
121 media_content_retvm_if(*face_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "Out of memory");
128 return MEDIA_CONTENT_ERROR_NONE;
131 int media_face_get_media_id(media_face_h face, char **media_id)
133 media_face_s* _face = (media_face_s*)face;
135 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
137 if(STRING_VALID(_face->media_id))
139 *media_id = strdup(_face->media_id);
140 media_content_retvm_if(*media_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "Out of memory");
147 return MEDIA_CONTENT_ERROR_NONE;
150 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)
152 media_face_s* _face = (media_face_s*)face;
154 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
155 media_content_retvm_if(!(rect_x && rect_y && rect_w && rect_h), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect");
157 *rect_x = _face->face_rect_x;
158 *rect_y = _face->face_rect_y;
159 *rect_w = _face->face_rect_w;
160 *rect_h = _face->face_rect_h;
162 return MEDIA_CONTENT_ERROR_NONE;
165 int media_face_get_orientation(media_face_h face, media_content_orientation_e *orientation)
167 media_face_s* _face = (media_face_s*)face;
169 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
170 media_content_retvm_if(orientation == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid orientation");
172 *orientation = _face->orientation;
174 return MEDIA_CONTENT_ERROR_NONE;
177 int media_face_get_tag(media_face_h face, char **tag)
179 media_face_s* _face = (media_face_s*)face;
181 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
183 if (STRING_VALID(_face->face_tag))
185 *tag = strdup(_face->face_tag);
186 media_content_retvm_if(*tag == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "Out of memory");
193 return MEDIA_CONTENT_ERROR_NONE;
196 int media_face_create_handle(const char *media_id, media_face_h *face)
198 int ret = MEDIA_CONTENT_ERROR_NONE;
200 media_content_retvm_if(!STRING_VALID(media_id), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid media_id");
201 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
203 ret = __media_face_check_media_id(media_id);
204 media_content_retvm_if(ret != MEDIA_CONTENT_ERROR_NONE, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Not exist media_id");
206 media_face_s* _face = calloc(1, sizeof(media_face_s));
207 media_content_retvm_if(_face == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "Out of memory");
209 _face->media_id = strdup(media_id);
211 *face = (media_face_h)_face;
213 return MEDIA_CONTENT_ERROR_NONE;
216 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)
218 media_face_s* _face = (media_face_s*)face;
220 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
221 media_content_retvm_if(rect_w == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect_w");
222 media_content_retvm_if(rect_h == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect_h");
224 _face->face_rect_x = rect_x;
225 _face->face_rect_y = rect_y;
226 _face->face_rect_w = rect_w;
227 _face->face_rect_h = rect_h;
229 return MEDIA_CONTENT_ERROR_NONE;
232 int media_face_set_orientation(media_face_h face, media_content_orientation_e orientation)
234 media_face_s* _face = (media_face_s*)face;
236 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
238 _face->orientation = orientation;
240 return MEDIA_CONTENT_ERROR_NONE;
243 int media_face_set_tag(media_face_h face, const char *tag)
245 media_face_s* _face = (media_face_s*)face;
247 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
249 if (STRING_VALID(tag))
250 _face->face_tag = strdup(tag);
252 _face->face_tag = NULL;
254 return MEDIA_CONTENT_ERROR_NONE;
257 int media_face_insert_to_db(media_face_h face)
259 int ret = MEDIA_CONTENT_ERROR_NONE;
260 char *query_str = NULL;
261 char *face_uuid = NULL;
263 media_face_s* _face = (media_face_s*)face;
265 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
266 media_content_retvm_if(_face->media_id == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid media_id");
267 media_content_retvm_if(_face->face_rect_w == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect_w");
268 media_content_retvm_if(_face->face_rect_h == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect_h");
270 ret = media_svc_generate_uuid(&face_uuid);
271 media_content_retvm_if(ret != MS_MEDIA_ERR_NONE, MEDIA_CONTENT_ERROR_INVALID_OPERATION, "Fail to generate face_id");
273 SAFE_FREE(_face->face_id);
274 _face->face_id = strdup(face_uuid);
275 SAFE_FREE(face_uuid);
277 query_str = sqlite3_mprintf(INSERT_FACE_TO_FACE, _face->face_id, _face->media_id, _face->face_rect_x, _face->face_rect_y, _face->face_rect_w, _face->face_rect_h, _face->orientation, _face->face_tag);
279 ret = _content_query_sql(query_str);
280 SQLITE3_SAFE_FREE(query_str);
285 int media_face_update_to_db(media_face_h face)
287 int ret = MEDIA_CONTENT_ERROR_NONE;
288 char *query_str = NULL;
290 media_face_s* _face = (media_face_s*)face;
292 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
293 media_content_retvm_if(_face->face_id == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid face_id");
294 media_content_retvm_if(_face->media_id == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid media_id");
295 media_content_retvm_if(_face->face_rect_w == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect_w");
296 media_content_retvm_if(_face->face_rect_h == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect_h");
298 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);
300 ret = _content_query_sql(query_str);
301 SQLITE3_SAFE_FREE(query_str);
306 int media_face_delete_from_db(const char *face_id)
308 int ret = MEDIA_CONTENT_ERROR_NONE;
309 char *query_str = NULL;
311 media_content_retvm_if(!STRING_VALID(face_id), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid face_id");
313 query_str = sqlite3_mprintf(DELETE_FACE_FROM_FACE, face_id);
315 ret = _content_query_sql(query_str);
316 SQLITE3_SAFE_FREE(query_str);