Fixed memory leak and added null parameter handling
[platform/core/api/mediavision.git] / src / mv_surveillance.c
1 /**
2  * Copyright (c) 2015 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 "mv_surveillance.h"
18
19 #include "mv_surveillance_private.h"
20 #include "mv_private.h"
21
22 #ifdef MEDIA_VISION_SURVEILLANCE_LICENSE_PORT
23
24 /* Include headers of licensed surveillance module here. */
25 #include "mv_surveillance_lic.h"
26
27 #else
28
29 /* Include headers of open surveillance module here. */
30 #include "mv_surveillance_open.h"
31
32 #endif /* MEDIA_VISION_SURVEILLANCE_LICENSE_PORT */
33
34 /**
35  * @file  mv_surveillance.c
36  * @brief This file contains the porting layer for Media Vision surveillance module.
37  */
38
39 static size_t __mv_surveillance_id_counter = 0;
40
41 int mv_surveillance_event_trigger_create(
42                 const char *event_type,
43                 mv_surveillance_event_trigger_h * trigger)
44 {
45         MEDIA_VISION_SUPPORT_CHECK(__mv_face_check_system_info_feature_supported());
46         MEDIA_VISION_SUPPORT_CHECK(__mv_image_check_system_info_feature_supported());
47         MEDIA_VISION_NULL_ARG_CHECK(event_type);
48         MEDIA_VISION_NULL_ARG_CHECK(trigger);
49         MEDIA_VISION_FUNCTION_ENTER();
50
51         mv_surveillance_event_trigger_s *handle =
52                         (mv_surveillance_event_trigger_s *) malloc(
53                                         sizeof(mv_surveillance_event_trigger_s));
54         if (NULL == handle) {
55                 LOGE("[%s] malloc fail", __func__);
56                 return MEDIA_VISION_ERROR_OUT_OF_MEMORY;
57         }
58
59         memset(handle, 0, sizeof(mv_surveillance_event_trigger_s));
60
61         /* default values: */
62         handle->trigger_id = ++__mv_surveillance_id_counter;
63         handle->event_type = strndup(event_type, 255);
64         handle->number_of_roi_points = 0;
65         handle->roi = NULL;
66
67         *trigger = (mv_surveillance_event_trigger_h) handle;
68
69         MEDIA_VISION_FUNCTION_LEAVE();
70         return MEDIA_VISION_ERROR_NONE;
71 }
72
73 int mv_surveillance_event_trigger_destroy(
74                 mv_surveillance_event_trigger_h trigger)
75 {
76         MEDIA_VISION_SUPPORT_CHECK(__mv_face_check_system_info_feature_supported());
77         MEDIA_VISION_SUPPORT_CHECK(__mv_image_check_system_info_feature_supported());
78         MEDIA_VISION_NULL_ARG_CHECK(trigger);
79         MEDIA_VISION_FUNCTION_ENTER();
80
81         mv_surveillance_event_trigger_s *handle =
82                         (mv_surveillance_event_trigger_s *) trigger;
83         free(handle->event_type);
84         free(handle->roi);
85         free((mv_surveillance_event_trigger_s *) trigger);
86
87         MEDIA_VISION_FUNCTION_LEAVE();
88         return MEDIA_VISION_ERROR_NONE;
89 }
90
91 int mv_surveillance_get_event_trigger_type(
92                 mv_surveillance_event_trigger_h trigger,
93                 char **event_type)
94 {
95         MEDIA_VISION_SUPPORT_CHECK(__mv_face_check_system_info_feature_supported());
96         MEDIA_VISION_SUPPORT_CHECK(__mv_image_check_system_info_feature_supported());
97         MEDIA_VISION_INSTANCE_CHECK(trigger);
98         MEDIA_VISION_NULL_ARG_CHECK(event_type);
99         MEDIA_VISION_FUNCTION_ENTER();
100
101         mv_surveillance_event_trigger_s *handle =
102                         (mv_surveillance_event_trigger_s *)trigger;
103         *event_type = strndup(handle->event_type, 255);
104
105         MEDIA_VISION_FUNCTION_LEAVE();
106         return MEDIA_VISION_ERROR_NONE;
107 }
108
109 int mv_surveillance_set_event_trigger_roi(
110                 mv_surveillance_event_trigger_h trigger,
111                 int number_of_points,
112                 mv_point_s *roi)
113 {
114         MEDIA_VISION_SUPPORT_CHECK(__mv_face_check_system_info_feature_supported());
115         MEDIA_VISION_SUPPORT_CHECK(__mv_image_check_system_info_feature_supported());
116         MEDIA_VISION_INSTANCE_CHECK(trigger);
117         MEDIA_VISION_NULL_ARG_CHECK(roi);
118         MEDIA_VISION_FUNCTION_ENTER();
119
120         mv_surveillance_event_trigger_s *handle =
121                         (mv_surveillance_event_trigger_s *)trigger;
122
123         if (handle->roi) {
124                 free(handle->roi);
125                 handle->roi = NULL;
126         }
127
128         handle->number_of_roi_points = number_of_points;
129         handle->roi = (mv_point_s*) malloc(sizeof(mv_point_s) * number_of_points);
130
131         if (NULL == handle->roi) {
132                 LOGE("[%s] malloc fail", __func__);
133                 return MEDIA_VISION_ERROR_OUT_OF_MEMORY;
134         }
135
136         int i = 0;
137         for (; i < number_of_points; ++i) {
138                 handle->roi[i].x = roi[i].x;
139                 handle->roi[i].y = roi[i].y;
140         }
141
142         MEDIA_VISION_FUNCTION_LEAVE();
143         return MEDIA_VISION_ERROR_NONE;
144 }
145
146 int mv_surveillance_get_event_trigger_roi(
147                 mv_surveillance_event_trigger_h trigger,
148                 int *number_of_points,
149                 mv_point_s ** roi)
150 {
151         MEDIA_VISION_SUPPORT_CHECK(__mv_face_check_system_info_feature_supported());
152         MEDIA_VISION_SUPPORT_CHECK(__mv_image_check_system_info_feature_supported());
153         MEDIA_VISION_INSTANCE_CHECK(trigger);
154         MEDIA_VISION_NULL_ARG_CHECK(number_of_points);
155         MEDIA_VISION_NULL_ARG_CHECK(roi);
156         MEDIA_VISION_FUNCTION_ENTER();
157
158         mv_surveillance_event_trigger_s *handle =
159                         (mv_surveillance_event_trigger_s *) trigger;
160
161         *number_of_points = handle->number_of_roi_points;
162         if (0 == *number_of_points) {
163                 MEDIA_VISION_FUNCTION_LEAVE();
164                 return MEDIA_VISION_ERROR_NONE;
165         }
166
167         *roi = (mv_point_s *) malloc(
168                                         sizeof(mv_point_s) * handle->number_of_roi_points);
169
170         int i = 0;
171         for (; i < handle->number_of_roi_points; ++i) {
172                 (*roi)[i].x = handle->roi[i].x;
173                 (*roi)[i].y = handle->roi[i].y;
174         }
175
176         MEDIA_VISION_FUNCTION_LEAVE();
177         return MEDIA_VISION_ERROR_NONE;
178 }
179
180 int mv_surveillance_subscribe_event_trigger(
181                 mv_surveillance_event_trigger_h event_trigger,
182                 int video_stream_id,
183                 mv_engine_config_h engine_cfg,
184                 mv_surveillance_event_occurred_cb callback,
185                 void *user_data)
186 {
187         MEDIA_VISION_SUPPORT_CHECK(__mv_face_check_system_info_feature_supported());
188         MEDIA_VISION_SUPPORT_CHECK(__mv_image_check_system_info_feature_supported());
189         MEDIA_VISION_INSTANCE_CHECK(event_trigger);
190         MEDIA_VISION_NULL_ARG_CHECK(callback);
191         MEDIA_VISION_FUNCTION_ENTER();
192
193 #ifdef MEDIA_VISION_SURVEILLANCE_LICENSE_PORT
194
195         /* Use licensed surveillance functionality here. */
196         const int ret = mv_surveillance_subscribe_event_trigger_lic(
197                                         event_trigger,
198                                         video_stream_id,
199                                         engine_cfg,
200                                         callback,
201                                         user_data);
202
203 #else
204
205         /* Use open surveillance functionality here. */
206         const int ret = mv_surveillance_subscribe_event_trigger_open(
207                                         event_trigger,
208                                         video_stream_id,
209                                         engine_cfg,
210                                         callback,
211                                         user_data);
212
213 #endif /* MEDIA_VISION_SURVEILLANCE_LICENSE_PORT */
214
215         MEDIA_VISION_FUNCTION_LEAVE();
216         return ret;
217 }
218
219 int mv_surveillance_unsubscribe_event_trigger(
220                 mv_surveillance_event_trigger_h event_trigger,
221                 int video_stream_id)
222 {
223         MEDIA_VISION_SUPPORT_CHECK(__mv_face_check_system_info_feature_supported());
224         MEDIA_VISION_SUPPORT_CHECK(__mv_image_check_system_info_feature_supported());
225         MEDIA_VISION_INSTANCE_CHECK(event_trigger);
226         MEDIA_VISION_FUNCTION_ENTER();
227
228 #ifdef MEDIA_VISION_SURVEILLANCE_LICENSE_PORT
229
230         /* Use licensed surveillance functionality here. */
231         const int ret = mv_surveillance_unsubscribe_event_trigger_lic(
232                                         event_trigger,
233                                         video_stream_id);
234
235 #else
236
237         /* Use open surveillance functionality here. */
238         const int ret = mv_surveillance_unsubscribe_event_trigger_open(
239                                         event_trigger,
240                                         video_stream_id);
241
242 #endif /* MEDIA_VISION_SURVEILLANCE_LICENSE_PORT */
243
244         MEDIA_VISION_FUNCTION_LEAVE();
245         return ret;
246 }
247
248 int mv_surveillance_push_source(
249                 mv_source_h source,
250                 int video_stream_id)
251 {
252         MEDIA_VISION_SUPPORT_CHECK(__mv_face_check_system_info_feature_supported());
253         MEDIA_VISION_SUPPORT_CHECK(__mv_image_check_system_info_feature_supported());
254         MEDIA_VISION_INSTANCE_CHECK(source);
255         MEDIA_VISION_FUNCTION_ENTER();
256
257 #ifdef MEDIA_VISION_SURVEILLANCE_LICENSE_PORT
258
259         /* Use licensed surveillance functionality here. */
260         const int ret = mv_surveillance_push_source_lic(source, video_stream_id);
261
262 #else
263
264         /* Use open surveillance functionality here. */
265         const int ret = mv_surveillance_push_source_open(source, video_stream_id);
266
267 #endif /* MEDIA_VISION_SURVEILLANCE_LICENSE_PORT */
268
269         MEDIA_VISION_FUNCTION_LEAVE();
270         return ret;
271 }
272
273 int mv_surveillance_foreach_supported_event_type(
274                 mv_surveillance_event_type_cb callback,
275                 void *user_data)
276 {
277         MEDIA_VISION_SUPPORT_CHECK(__mv_face_check_system_info_feature_supported());
278         MEDIA_VISION_SUPPORT_CHECK(__mv_image_check_system_info_feature_supported());
279         MEDIA_VISION_NULL_ARG_CHECK(callback);
280         MEDIA_VISION_FUNCTION_ENTER();
281
282 #ifdef MEDIA_VISION_SURVEILLANCE_LICENSE_PORT
283
284         /* Use licensed surveillance functionality here. */
285         const int ret = mv_surveillance_foreach_event_type_lic(
286                                         callback,
287                                         user_data);
288
289 #else
290
291         /* Use open surveillance functionality here. */
292         const int ret = mv_surveillance_foreach_event_type_open(
293                                         callback,
294                                         user_data);
295
296 #endif /* MEDIA_VISION_SURVEILLANCE_LICENSE_PORT */
297
298         MEDIA_VISION_FUNCTION_LEAVE();
299         return ret;
300 }
301
302 int mv_surveillance_foreach_event_result_name(
303                 const char *event_type,
304                 mv_surveillance_event_result_name_cb callback,
305                 void *user_data)
306 {
307         MEDIA_VISION_SUPPORT_CHECK(__mv_face_check_system_info_feature_supported());
308         MEDIA_VISION_SUPPORT_CHECK(__mv_image_check_system_info_feature_supported());
309         MEDIA_VISION_NULL_ARG_CHECK(event_type);
310         MEDIA_VISION_NULL_ARG_CHECK(callback);
311         MEDIA_VISION_FUNCTION_ENTER();
312
313 #ifdef MEDIA_VISION_SURVEILLANCE_LICENSE_PORT
314
315         /* Use licensed surveillance functionality here. */
316         const int ret = mv_surveillance_foreach_event_result_value_name_lic(
317                                         event_type,
318                                         callback,
319                                         user_data);
320
321 #else
322
323         /* Use open surveillance functionality here. */
324         const int ret = mv_surveillance_foreach_event_result_value_name_open(
325                                         event_type,
326                                         callback,
327                                         user_data);
328
329 #endif /* MEDIA_VISION_SURVEILLANCE_LICENSE_PORT */
330
331         MEDIA_VISION_FUNCTION_LEAVE();
332
333         return ret;
334 }
335
336 int mv_surveillance_get_result_value(
337                 mv_surveillance_result_h result,
338                 const char *value_name,
339                 void *value)
340 {
341         MEDIA_VISION_SUPPORT_CHECK(__mv_face_check_system_info_feature_supported());
342         MEDIA_VISION_SUPPORT_CHECK(__mv_image_check_system_info_feature_supported());
343         MEDIA_VISION_INSTANCE_CHECK(result);
344         MEDIA_VISION_NULL_ARG_CHECK(value_name);
345         MEDIA_VISION_NULL_ARG_CHECK(value);
346         MEDIA_VISION_FUNCTION_ENTER();
347
348 #ifdef MEDIA_VISION_SURVEILLANCE_LICENSE_PORT
349
350         /* Use licensed surveillance functionality here. */
351         const int ret = mv_surveillance_get_result_value_lic(
352                                         result,
353                                         value_name,
354                                         value);
355
356 #else
357
358         /* Use open surveillance functionality here. */
359         const int ret = mv_surveillance_get_result_value_open(
360                                         result,
361                                         value_name,
362                                         value);
363
364 #endif /* MEDIA_VISION_SURVEILLANCE_LICENSE_PORT */
365
366         MEDIA_VISION_FUNCTION_LEAVE();
367         return ret;
368 }