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 int media_face_clone(media_face_h *dst, media_face_h src)
22 media_face_s *_src = (media_face_s*)src;
24 media_content_retvm_if(src == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid src handle");
26 media_face_s *_dst = (media_face_s *)calloc(1, sizeof(media_face_s));
27 media_content_retvm_if(_dst == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
29 if (STRING_VALID(_src->face_id))
31 _dst->face_id = strdup(_src->face_id);
32 if(_dst->face_id== NULL)
34 media_face_destroy((media_face_h)_dst);
35 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
36 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
40 if (STRING_VALID(_src->media_id))
42 _dst->media_id = strdup(_src->media_id);
43 if(_dst->media_id== NULL)
45 media_face_destroy((media_face_h)_dst);
46 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
47 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
51 _dst->face_rect_x = _src->face_rect_x;
52 _dst->face_rect_y = _src->face_rect_y;
53 _dst->face_rect_w = _src->face_rect_w;
54 _dst->face_rect_h = _src->face_rect_h;
55 _dst->orientation = _src->orientation;
57 if (STRING_VALID(_src->face_tag))
59 _dst->face_tag = strdup(_src->face_tag);
60 if(_dst->face_tag== NULL)
62 media_face_destroy((media_face_h)_dst);
63 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
64 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
68 *dst = (media_face_h)_dst;
70 return MEDIA_CONTENT_ERROR_NONE;
73 int media_face_destroy(media_face_h face)
75 media_face_s *_face = (media_face_s*)face;
77 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
79 SAFE_FREE(_face->face_id);
80 SAFE_FREE(_face->media_id);
81 SAFE_FREE(_face->face_tag);
84 return MEDIA_CONTENT_ERROR_NONE;
87 int media_face_get_face_id(media_face_h face, char **face_id)
89 media_face_s* _face = (media_face_s*)face;
91 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
93 if(STRING_VALID(_face->face_id))
95 *face_id = strdup(_face->face_id);
96 media_content_retvm_if(*face_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "Out of memory");
103 return MEDIA_CONTENT_ERROR_NONE;
106 int media_face_get_media_id(media_face_h face, char **media_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(STRING_VALID(_face->media_id))
114 *media_id = strdup(_face->media_id);
115 media_content_retvm_if(*media_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "Out of memory");
122 return MEDIA_CONTENT_ERROR_NONE;
125 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)
127 media_face_s* _face = (media_face_s*)face;
129 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
130 media_content_retvm_if(!(rect_x && rect_y && rect_w && rect_h), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect");
132 *rect_x = _face->face_rect_x;
133 *rect_y = _face->face_rect_y;
134 *rect_w = _face->face_rect_w;
135 *rect_h = _face->face_rect_h;
137 return MEDIA_CONTENT_ERROR_NONE;
140 int media_face_get_orientation(media_face_h face, media_content_orientation_e *orientation)
142 media_face_s* _face = (media_face_s*)face;
144 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
145 media_content_retvm_if(orientation == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid orientation");
147 *orientation = _face->orientation;
149 return MEDIA_CONTENT_ERROR_NONE;
152 int media_face_get_tag(media_face_h face, char **tag)
154 media_face_s* _face = (media_face_s*)face;
156 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
158 if (STRING_VALID(_face->face_tag))
160 *tag = strdup(_face->face_tag);
161 media_content_retvm_if(*tag == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "Out of memory");
168 return MEDIA_CONTENT_ERROR_NONE;
171 int media_face_create_handle(media_face_h *face)
173 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
175 media_face_s* _face = calloc(1, sizeof(media_face_s));
176 media_content_retvm_if(_face == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "Out of memory");
178 *face = (media_face_h)_face;
180 return MEDIA_CONTENT_ERROR_NONE;
183 int media_face_set_media_id(media_face_h face, const char *media_id)
185 int ret = MEDIA_CONTENT_ERROR_NONE;
186 media_face_s* _face = (media_face_s*)face;
187 char *query_str = NULL;
188 sqlite3_stmt *stmt = NULL;
191 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
192 media_content_retvm_if(!STRING_VALID(media_id), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid media_id");
194 query_str = sqlite3_mprintf(SELECT_MEDIA_COUNT_FROM_MEDIA_BY_ID, media_id);
195 ret = _content_query_prepare(&stmt, query_str, NULL, NULL);
196 SQLITE3_SAFE_FREE(query_str);
197 media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
199 while(sqlite3_step(stmt) == SQLITE_ROW)
201 item_count = (int)sqlite3_column_int(stmt, 0);
203 SQLITE3_FINALIZE(stmt);
205 media_content_retvm_if(item_count == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid media_id");
207 SAFE_FREE(_face->media_id);
209 _face->media_id = strdup(media_id);
210 media_content_retvm_if(_face->media_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "Out of memory");
212 return MEDIA_CONTENT_ERROR_NONE;
215 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)
217 media_face_s* _face = (media_face_s*)face;
219 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
220 media_content_retvm_if(rect_w == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect_w");
221 media_content_retvm_if(rect_h == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect_h");
223 _face->face_rect_x = rect_x;
224 _face->face_rect_y = rect_y;
225 _face->face_rect_w = rect_w;
226 _face->face_rect_h = rect_h;
228 return MEDIA_CONTENT_ERROR_NONE;
231 int media_face_set_orientation(media_face_h face, media_content_orientation_e orientation)
233 media_face_s* _face = (media_face_s*)face;
235 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
237 _face->orientation = orientation;
239 return MEDIA_CONTENT_ERROR_NONE;
242 int media_face_set_tag(media_face_h face, const char *tag)
244 media_face_s* _face = (media_face_s*)face;
246 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
248 if (STRING_VALID(tag))
249 _face->face_tag = strdup(tag);
251 _face->face_tag = NULL;
253 return MEDIA_CONTENT_ERROR_NONE;
256 int media_face_insert_to_db(media_face_h face)
258 int ret = MEDIA_CONTENT_ERROR_NONE;
259 char *query_str = NULL;
260 char *face_uuid = NULL;
262 media_face_s* _face = (media_face_s*)face;
264 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
265 media_content_retvm_if(_face->media_id == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid media_id");
266 media_content_retvm_if(_face->face_rect_w == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect_w");
267 media_content_retvm_if(_face->face_rect_w == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect_h");
269 ret = media_svc_generate_uuid(&face_uuid);
270 media_content_retvm_if(ret != MS_MEDIA_ERR_NONE, MEDIA_CONTENT_ERROR_INVALID_OPERATION, "Fail to generate face_id");
272 SAFE_FREE(_face->face_id);
273 _face->face_id = strdup(face_uuid);
274 SAFE_FREE(face_uuid);
276 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);
278 ret = _content_query_sql(query_str);
279 SQLITE3_SAFE_FREE(query_str);
284 int media_face_update_to_db(media_face_h face)
286 int ret = MEDIA_CONTENT_ERROR_NONE;
287 char *query_str = NULL;
289 media_face_s* _face = (media_face_s*)face;
291 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
292 media_content_retvm_if(_face->face_id == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid face_id");
293 media_content_retvm_if(_face->media_id == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid media_id");
295 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);
297 ret = _content_query_sql(query_str);
298 SQLITE3_SAFE_FREE(query_str);
303 int media_face_delete_from_db(const char *face_id)
305 int ret = MEDIA_CONTENT_ERROR_NONE;
306 char *query_str = NULL;
308 media_content_retvm_if(!STRING_VALID(face_id), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid face_id");
310 query_str = sqlite3_mprintf(DELETE_FACE_FROM_FACE, face_id);
312 ret = _content_query_sql(query_str);
313 SQLITE3_SAFE_FREE(query_str);