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>
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)
35 item_count = (int)sqlite3_column_int(stmt, 0);
37 SQLITE3_FINALIZE(stmt);
39 media_content_retvm_if(item_count == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid media_id");
41 return MEDIA_CONTENT_ERROR_NONE;
44 int media_face_destroy(media_face_h face)
46 media_face_s *_face = (media_face_s*)face;
48 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
50 SAFE_FREE(_face->face_id);
51 SAFE_FREE(_face->media_id);
52 SAFE_FREE(_face->face_tag);
55 return MEDIA_CONTENT_ERROR_NONE;
58 int media_face_clone(media_face_h *dst, media_face_h src)
60 media_face_s *_src = (media_face_s*)src;
62 media_content_retvm_if(src == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid src handle");
64 media_face_s *_dst = (media_face_s *)calloc(1, sizeof(media_face_s));
65 media_content_retvm_if(_dst == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
67 if (STRING_VALID(_src->face_id)) {
68 _dst->face_id = strdup(_src->face_id);
69 if (_dst->face_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 if (STRING_VALID(_src->media_id)) {
77 _dst->media_id = strdup(_src->media_id);
78 if (_dst->media_id == NULL) {
79 media_face_destroy((media_face_h)_dst);
80 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
81 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
85 _dst->face_rect_x = _src->face_rect_x;
86 _dst->face_rect_y = _src->face_rect_y;
87 _dst->face_rect_w = _src->face_rect_w;
88 _dst->face_rect_h = _src->face_rect_h;
89 _dst->orientation = _src->orientation;
91 if (STRING_VALID(_src->face_tag)) {
92 _dst->face_tag = strdup(_src->face_tag);
93 if (_dst->face_tag == NULL) {
94 media_face_destroy((media_face_h)_dst);
95 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
96 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
100 *dst = (media_face_h)_dst;
102 return MEDIA_CONTENT_ERROR_NONE;
105 int media_face_get_face_id(media_face_h face, char **face_id)
107 media_face_s* _face = (media_face_s*)face;
109 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
111 if (STRING_VALID(_face->face_id)) {
112 *face_id = strdup(_face->face_id);
113 media_content_retvm_if(*face_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "Out of memory");
118 return MEDIA_CONTENT_ERROR_NONE;
121 int media_face_get_media_id(media_face_h face, char **media_id)
123 media_face_s* _face = (media_face_s*)face;
125 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
127 if (STRING_VALID(_face->media_id)) {
128 *media_id = strdup(_face->media_id);
129 media_content_retvm_if(*media_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "Out of memory");
134 return MEDIA_CONTENT_ERROR_NONE;
137 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)
139 media_face_s* _face = (media_face_s*)face;
141 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
142 media_content_retvm_if(!(rect_x && rect_y && rect_w && rect_h), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect");
144 *rect_x = _face->face_rect_x;
145 *rect_y = _face->face_rect_y;
146 *rect_w = _face->face_rect_w;
147 *rect_h = _face->face_rect_h;
149 return MEDIA_CONTENT_ERROR_NONE;
152 int media_face_get_orientation(media_face_h face, media_content_orientation_e *orientation)
154 media_face_s* _face = (media_face_s*)face;
156 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
157 media_content_retvm_if(orientation == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid orientation");
159 *orientation = _face->orientation;
161 return MEDIA_CONTENT_ERROR_NONE;
164 int media_face_get_tag(media_face_h face, char **tag)
166 media_face_s* _face = (media_face_s*)face;
168 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
170 if (STRING_VALID(_face->face_tag)) {
171 *tag = strdup(_face->face_tag);
172 media_content_retvm_if(*tag == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "Out of memory");
177 return MEDIA_CONTENT_ERROR_NONE;
180 int media_face_create(const char *media_id, media_face_h *face)
182 int ret = MEDIA_CONTENT_ERROR_NONE;
184 media_content_retvm_if(!STRING_VALID(media_id), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid media_id");
185 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
187 ret = __media_face_check_media_id(media_id);
188 media_content_retvm_if(ret != MEDIA_CONTENT_ERROR_NONE, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Not exist media_id");
190 media_face_s* _face = calloc(1, sizeof(media_face_s));
191 media_content_retvm_if(_face == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "Out of memory");
193 _face->media_id = strdup(media_id);
195 *face = (media_face_h)_face;
197 return MEDIA_CONTENT_ERROR_NONE;
200 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)
202 media_face_s* _face = (media_face_s*)face;
204 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
205 media_content_retvm_if(rect_w == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect_w");
206 media_content_retvm_if(rect_h == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect_h");
208 _face->face_rect_x = rect_x;
209 _face->face_rect_y = rect_y;
210 _face->face_rect_w = rect_w;
211 _face->face_rect_h = rect_h;
213 return MEDIA_CONTENT_ERROR_NONE;
216 int media_face_set_orientation(media_face_h face, media_content_orientation_e orientation)
218 media_face_s* _face = (media_face_s*)face;
220 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
222 _face->orientation = orientation;
224 return MEDIA_CONTENT_ERROR_NONE;
227 int media_face_set_tag(media_face_h face, const char *tag)
229 media_face_s* _face = (media_face_s*)face;
231 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
233 if (STRING_VALID(tag))
234 _face->face_tag = strdup(tag);
236 _face->face_tag = NULL;
238 return MEDIA_CONTENT_ERROR_NONE;
241 int media_face_insert_to_db(media_face_h face)
243 int ret = MEDIA_CONTENT_ERROR_NONE;
244 char *query_str = NULL;
245 char *face_uuid = NULL;
247 media_face_s* _face = (media_face_s*)face;
249 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
250 media_content_retvm_if(_face->media_id == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid media_id");
251 media_content_retvm_if(_face->face_rect_w == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect_w");
252 media_content_retvm_if(_face->face_rect_h == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect_h");
254 ret = media_svc_generate_uuid(&face_uuid);
255 media_content_retvm_if(ret != MS_MEDIA_ERR_NONE, MEDIA_CONTENT_ERROR_INVALID_OPERATION, "Fail to generate face_id");
257 SAFE_FREE(_face->face_id);
258 _face->face_id = strdup(face_uuid);
259 SAFE_FREE(face_uuid);
261 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);
263 ret = _content_query_sql(query_str);
264 SQLITE3_SAFE_FREE(query_str);
269 int media_face_update_to_db(media_face_h face)
271 int ret = MEDIA_CONTENT_ERROR_NONE;
272 char *query_str = NULL;
274 media_face_s* _face = (media_face_s*)face;
276 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
277 media_content_retvm_if(_face->face_id == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid face_id");
278 media_content_retvm_if(_face->media_id == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid media_id");
279 media_content_retvm_if(_face->face_rect_w == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect_w");
280 media_content_retvm_if(_face->face_rect_h == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect_h");
282 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);
284 ret = _content_query_sql(query_str);
285 SQLITE3_SAFE_FREE(query_str);
290 int media_face_delete_from_db(const char *face_id)
292 int ret = MEDIA_CONTENT_ERROR_NONE;
293 char *query_str = NULL;
295 media_content_retvm_if(!STRING_VALID(face_id), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid face_id");
297 query_str = sqlite3_mprintf(DELETE_FACE_FROM_FACE, face_id);
299 ret = _content_query_sql(query_str);
300 SQLITE3_SAFE_FREE(query_str);