Add testcase for multimedia HAL APIs
[platform/core/multimedia/mm-hal-interface.git] / testcase / camera / camera_hal_interface.c
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Contact: Jeongmo Yang <jm80.yang@samsung.com>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include <errno.h>
24 #include <dlfcn.h>
25 #include <glib.h>
26 #include <dlog.h>
27 #include "camera_hal_interface.h"
28
29 #ifdef LOG_TAG
30 #undef LOG_TAG
31 #endif /* LOG_TAG */
32 #define LOG_TAG "CAMERA_HAL_INTF"
33
34 #define LIB_TIZEN_CAMERA "libtizen-camera.so"
35
36 struct _camera_hal_interface {
37         void *dl_handle;
38         void *hal_handle;
39         camera_interface_t intf;
40 };
41
42
43 int camera_hal_interface_init(camera_hal_interface **h)
44 {
45         int ret = CAMERA_ERROR_NONE;
46         camera_hal_interface *tmp_h = NULL;
47
48         if (h == NULL) {
49                 LOGE("invalid parameter for camera_hal_interface");
50                 return CAMERA_ERROR_INVALID_PARAMETER;
51         }
52
53         tmp_h = g_new0(camera_hal_interface, 1);
54         if (tmp_h == NULL) {
55                 LOGE("failed to allocate hal interface");
56                 return CAMERA_ERROR_OUT_OF_MEMORY;
57         }
58
59         tmp_h->dl_handle = dlopen(LIB_TIZEN_CAMERA, RTLD_NOW);
60         if (tmp_h->dl_handle) {
61                 tmp_h->intf.init = dlsym(tmp_h->dl_handle, "camera_init");
62                 tmp_h->intf.deinit = dlsym(tmp_h->dl_handle, "camera_deinit");
63                 tmp_h->intf.get_device_list = dlsym(tmp_h->dl_handle, "camera_get_device_list");
64                 tmp_h->intf.open_device = dlsym(tmp_h->dl_handle, "camera_open_device");
65                 tmp_h->intf.close_device = dlsym(tmp_h->dl_handle, "camera_close_device");
66                 tmp_h->intf.add_message_callback = dlsym(tmp_h->dl_handle, "camera_add_message_callback");
67                 tmp_h->intf.remove_message_callback = dlsym(tmp_h->dl_handle, "camera_remove_message_callback");
68                 tmp_h->intf.set_preview_stream_format = dlsym(tmp_h->dl_handle, "camera_set_preview_stream_format");
69                 tmp_h->intf.get_preview_stream_format = dlsym(tmp_h->dl_handle, "camera_get_preview_stream_format");
70                 tmp_h->intf.start_preview = dlsym(tmp_h->dl_handle, "camera_start_preview");
71                 tmp_h->intf.release_preview_buffer = dlsym(tmp_h->dl_handle, "camera_release_preview_buffer");
72                 tmp_h->intf.stop_preview = dlsym(tmp_h->dl_handle, "camera_stop_preview");
73                 tmp_h->intf.start_auto_focus = dlsym(tmp_h->dl_handle, "camera_start_auto_focus");
74                 tmp_h->intf.stop_auto_focus = dlsym(tmp_h->dl_handle, "camera_stop_auto_focus");
75                 tmp_h->intf.start_capture = dlsym(tmp_h->dl_handle, "camera_start_capture");
76                 tmp_h->intf.stop_capture = dlsym(tmp_h->dl_handle, "camera_stop_capture");
77                 tmp_h->intf.set_video_stream_format = dlsym(tmp_h->dl_handle, "camera_set_video_stream_format");
78                 tmp_h->intf.get_video_stream_format = dlsym(tmp_h->dl_handle, "camera_get_video_stream_format");
79                 tmp_h->intf.start_record = dlsym(tmp_h->dl_handle, "camera_start_record");
80                 tmp_h->intf.release_video_buffer = dlsym(tmp_h->dl_handle, "camera_release_video_buffer");
81                 tmp_h->intf.stop_record = dlsym(tmp_h->dl_handle, "camera_stop_record");
82                 tmp_h->intf.set_command = dlsym(tmp_h->dl_handle, "camera_set_command");
83                 tmp_h->intf.get_command = dlsym(tmp_h->dl_handle, "camera_get_command");
84                 tmp_h->intf.set_batch_command = dlsym(tmp_h->dl_handle, "camera_set_batch_command");
85
86                 if (tmp_h->intf.init == NULL || tmp_h->intf.deinit == NULL) {
87                         LOGE("could not get mandatory funtion. %p %1p", tmp_h->intf.init, tmp_h->intf.deinit);
88                         ret = CAMERA_ERROR_INTERNAL;
89                         goto _CAMERA_HAL_INTERFACE_GET_FAILED;
90                 }
91
92                 if (tmp_h->intf.init) {
93                         ret = tmp_h->intf.init(&tmp_h->hal_handle);
94                         if (ret != CAMERA_ERROR_NONE) {
95                                 LOGE("camera_init failed 0x%x", ret);
96                                 goto _CAMERA_HAL_INTERFACE_GET_FAILED;
97                         }
98                 } else {
99                         LOGE("no camera_init function");
100                         ret = CAMERA_ERROR_INTERNAL;
101                         goto _CAMERA_HAL_INTERFACE_GET_FAILED;
102                 }
103         } else {
104                 LOGE("dlopen failed [%s][errno %d]", LIB_TIZEN_CAMERA, errno);
105                 ret = CAMERA_ERROR_DEVICE_NOT_SUPPORTED;
106                 goto _CAMERA_HAL_INTERFACE_GET_FAILED;
107         }
108
109         *h = tmp_h;
110
111         return ret;
112
113 _CAMERA_HAL_INTERFACE_GET_FAILED:
114         if (tmp_h) {
115                 if (tmp_h->dl_handle)
116                         dlclose(tmp_h->dl_handle);
117
118                 g_free(tmp_h);
119         }
120
121         return ret;
122 }
123
124
125 int camera_hal_interface_deinit(camera_hal_interface *h)
126 {
127         int ret = CAMERA_ERROR_NONE;
128
129         if (h == NULL) {
130                 LOGE("NULL interface");
131                 return CAMERA_ERROR_INVALID_PARAMETER;
132         }
133
134         if (h->dl_handle) {
135                 ret = h->intf.deinit(h->hal_handle);
136                 if (ret != CAMERA_ERROR_NONE) {
137                         LOGE("camera_deinit failed 0x%x", ret);
138                         return ret;
139                 }
140
141                 h->hal_handle = NULL;
142
143                 dlclose(h->dl_handle);
144                 h->dl_handle = NULL;
145         }
146
147         g_free(h);
148
149         return CAMERA_ERROR_NONE;
150 }
151
152
153 int camera_hal_interface_get_device_list(camera_hal_interface *h, camera_device_list_t *device_list)
154 {
155         int ret = CAMERA_ERROR_NONE;
156
157         if (h == NULL) {
158                 LOGE("NULL interface");
159                 return CAMERA_ERROR_INVALID_PARAMETER;
160         }
161
162         if (h->intf.get_device_list) {
163                 ret = h->intf.get_device_list(h->hal_handle, device_list);
164         } else {
165                 LOGE("camera_get_device_list not implemented");
166                 ret = CAMERA_ERROR_NOT_IMPLEMENTED;
167         }
168
169         return ret;
170 }
171
172
173 int camera_hal_interface_open_device(camera_hal_interface *h, int device_index)
174 {
175         int ret = CAMERA_ERROR_NONE;
176
177         if (h == NULL) {
178                 LOGE("NULL interface");
179                 return CAMERA_ERROR_INVALID_PARAMETER;
180         }
181
182         if (h->intf.open_device) {
183                 ret = h->intf.open_device(h->hal_handle, device_index);
184         } else {
185                 LOGE("camera_open_device not implemented");
186                 ret = CAMERA_ERROR_NOT_IMPLEMENTED;
187         }
188
189         return ret;
190 }
191
192
193 int camera_hal_interface_close_device(camera_hal_interface *h)
194 {
195         int ret = CAMERA_ERROR_NONE;
196
197         if (h == NULL) {
198                 LOGE("NULL interface");
199                 return CAMERA_ERROR_INVALID_PARAMETER;
200         }
201
202         if (h->intf.close_device) {
203                 ret = h->intf.close_device(h->hal_handle);
204         } else {
205                 LOGE("camera_close_device not implemented");
206                 ret = CAMERA_ERROR_NOT_IMPLEMENTED;
207         }
208
209         return ret;
210 }
211
212
213 int camera_hal_interface_add_message_callback(camera_hal_interface *h, camera_message_cb callback, void *user_data, uint32_t *cb_id)
214 {
215         int ret = CAMERA_ERROR_NONE;
216
217         if (h == NULL) {
218                 LOGE("NULL interface");
219                 return CAMERA_ERROR_INVALID_PARAMETER;
220         }
221
222         if (h->intf.add_message_callback) {
223                 ret = h->intf.add_message_callback(h->hal_handle, callback, user_data, cb_id);
224         } else {
225                 LOGE("camera_add_message_callback not implemented");
226                 ret = CAMERA_ERROR_NOT_IMPLEMENTED;
227         }
228
229         return ret;
230 }
231
232
233 int camera_hal_interface_remove_message_callback(camera_hal_interface *h, uint32_t cb_id)
234 {
235         int ret = CAMERA_ERROR_NONE;
236
237         if (h == NULL) {
238                 LOGE("NULL interface");
239                 return CAMERA_ERROR_INVALID_PARAMETER;
240         }
241
242         if (h->intf.remove_message_callback) {
243                 ret = h->intf.remove_message_callback(h->hal_handle, cb_id);
244         } else {
245                 LOGE("camera_remove_message_callback not implemented");
246                 ret = CAMERA_ERROR_NOT_IMPLEMENTED;
247         }
248
249         return ret;
250 }
251
252
253 int camera_hal_interface_set_preview_stream_format(camera_hal_interface *h, camera_format_t *format)
254 {
255         int ret = CAMERA_ERROR_NONE;
256
257         if (h == NULL) {
258                 LOGE("NULL interface");
259                 return CAMERA_ERROR_INVALID_PARAMETER;
260         }
261
262         if (h->intf.set_preview_stream_format) {
263                 ret = h->intf.set_preview_stream_format(h->hal_handle, format);
264         } else {
265                 LOGE("camera_set_preview_stream_format not implemented");
266                 ret = CAMERA_ERROR_NOT_IMPLEMENTED;
267         }
268
269         return ret;
270 }
271
272
273 int camera_hal_interface_get_preview_stream_format(camera_hal_interface *h, camera_format_t *format)
274 {
275         int ret = CAMERA_ERROR_NONE;
276
277         if (h == NULL) {
278                 LOGE("NULL interface");
279                 return CAMERA_ERROR_INVALID_PARAMETER;
280         }
281
282         if (h->intf.get_preview_stream_format) {
283                 ret = h->intf.get_preview_stream_format(h->hal_handle, format);
284         } else {
285                 LOGE("camera_get_preview_stream_format not implemented");
286                 ret = CAMERA_ERROR_NOT_IMPLEMENTED;
287         }
288
289         return ret;
290 }
291
292
293 int camera_hal_interface_start_preview(camera_hal_interface *h, camera_preview_frame_cb callback, void *user_data)
294 {
295         int ret = CAMERA_ERROR_NONE;
296
297         if (h == NULL) {
298                 LOGE("NULL interface");
299                 return CAMERA_ERROR_INVALID_PARAMETER;
300         }
301
302         if (h->intf.start_preview) {
303                 ret = h->intf.start_preview(h->hal_handle, callback, user_data);
304         } else {
305                 LOGE("camera_start_preview not implemented");
306                 ret = CAMERA_ERROR_NOT_IMPLEMENTED;
307         }
308
309         return ret;
310 }
311
312
313 int camera_hal_interface_release_preview_buffer(camera_hal_interface *h, int buffer_index)
314 {
315         int ret = CAMERA_ERROR_NONE;
316
317         if (h == NULL) {
318                 LOGE("NULL interface");
319                 return CAMERA_ERROR_INVALID_PARAMETER;
320         }
321
322         if (h->intf.release_preview_buffer) {
323                 ret = h->intf.release_preview_buffer(h->hal_handle, buffer_index);
324         } else {
325                 LOGE("camera_release_preview_buffer not implemented");
326                 ret = CAMERA_ERROR_NOT_IMPLEMENTED;
327         }
328
329         return ret;
330 }
331
332
333 int camera_hal_interface_stop_preview(camera_hal_interface *h)
334 {
335         int ret = CAMERA_ERROR_NONE;
336
337         if (h == NULL) {
338                 LOGE("NULL interface");
339                 return CAMERA_ERROR_INVALID_PARAMETER;
340         }
341
342         if (h->intf.stop_preview) {
343                 ret = h->intf.stop_preview(h->hal_handle);
344         } else {
345                 LOGE("camera_stop_preview not implemented");
346                 ret = CAMERA_ERROR_NOT_IMPLEMENTED;
347         }
348
349         return ret;
350 }
351
352
353 int camera_hal_interface_start_auto_focus(camera_hal_interface *h)
354 {
355         int ret = CAMERA_ERROR_NONE;
356
357         if (h == NULL) {
358                 LOGE("NULL interface");
359                 return CAMERA_ERROR_INVALID_PARAMETER;
360         }
361
362         if (h->intf.start_auto_focus) {
363                 ret = h->intf.start_auto_focus(h->hal_handle);
364         } else {
365                 LOGE("camera_start_auto_focus not implemented");
366                 ret = CAMERA_ERROR_NOT_IMPLEMENTED;
367         }
368
369         return ret;
370 }
371
372
373 int camera_hal_interface_stop_auto_focus(camera_hal_interface *h)
374 {
375         int ret = CAMERA_ERROR_NONE;
376
377         if (h == NULL) {
378                 LOGE("NULL interface");
379                 return CAMERA_ERROR_INVALID_PARAMETER;
380         }
381
382         if (h->intf.stop_auto_focus) {
383                 ret = h->intf.stop_auto_focus(h->hal_handle);
384         } else {
385                 LOGE("camera_stop_auto_focus not implemented");
386                 ret = CAMERA_ERROR_NOT_IMPLEMENTED;
387         }
388
389         return ret;
390 }
391
392
393 int camera_hal_interface_start_capture(camera_hal_interface *h, camera_capture_cb callback, void *user_data)
394 {
395         int ret = CAMERA_ERROR_NONE;
396
397         if (h == NULL) {
398                 LOGE("NULL interface");
399                 return CAMERA_ERROR_INVALID_PARAMETER;
400         }
401
402         if (h->intf.start_capture) {
403                 ret = h->intf.start_capture(h->hal_handle, callback, user_data);
404         } else {
405                 LOGE("camera_start_capture not implemented");
406                 ret = CAMERA_ERROR_NOT_IMPLEMENTED;
407         }
408
409         return ret;
410 }
411
412
413 int camera_hal_interface_stop_capture(camera_hal_interface *h)
414 {
415         int ret = CAMERA_ERROR_NONE;
416
417         if (h == NULL) {
418                 LOGE("NULL interface");
419                 return CAMERA_ERROR_INVALID_PARAMETER;
420         }
421
422         if (h->intf.stop_capture) {
423                 ret = h->intf.stop_capture(h->hal_handle);
424         } else {
425                 LOGE("camera_stop_capture not implemented");
426                 ret = CAMERA_ERROR_NOT_IMPLEMENTED;
427         }
428
429         return ret;
430 }
431
432
433 int camera_hal_interface_set_video_stream_format(camera_hal_interface *h, camera_format_t *format)
434 {
435         int ret = CAMERA_ERROR_NONE;
436
437         if (h == NULL) {
438                 LOGE("NULL interface");
439                 return CAMERA_ERROR_INVALID_PARAMETER;
440         }
441
442         if (h->intf.set_video_stream_format) {
443                 ret = h->intf.set_video_stream_format(h->hal_handle, format);
444         } else {
445                 LOGE("camera_set_video_stream_format not implemented");
446                 ret = CAMERA_ERROR_NOT_IMPLEMENTED;
447         }
448
449         return ret;
450 }
451
452
453 int camera_hal_interface_get_video_stream_format(camera_hal_interface *h, camera_format_t *format)
454 {
455         int ret = CAMERA_ERROR_NONE;
456
457         if (h == NULL) {
458                 LOGE("NULL interface");
459                 return CAMERA_ERROR_INVALID_PARAMETER;
460         }
461
462         if (h->intf.get_video_stream_format) {
463                 ret = h->intf.get_video_stream_format(h->hal_handle, format);
464         } else {
465                 LOGE("camera_get_video_stream_format not implemented");
466                 ret = CAMERA_ERROR_NOT_IMPLEMENTED;
467         }
468
469         return ret;
470 }
471
472
473 int camera_hal_interface_start_record(camera_hal_interface *h, camera_video_frame_cb callback, void *user_data)
474 {
475         int ret = CAMERA_ERROR_NONE;
476
477         if (h == NULL) {
478                 LOGE("NULL interface");
479                 return CAMERA_ERROR_INVALID_PARAMETER;
480         }
481
482         if (h->intf.start_record) {
483                 ret = h->intf.start_record(h->hal_handle, callback, user_data);
484         } else {
485                 LOGE("camera_start_record not implemented");
486                 ret = CAMERA_ERROR_NOT_IMPLEMENTED;
487         }
488
489         return ret;
490 }
491
492
493 int camera_hal_interface_release_video_buffer(camera_hal_interface *h, int buffer_index)
494 {
495         int ret = CAMERA_ERROR_NONE;
496
497         if (h == NULL) {
498                 LOGE("NULL interface");
499                 return CAMERA_ERROR_INVALID_PARAMETER;
500         }
501
502         if (h->intf.release_video_buffer) {
503                 ret = h->intf.release_video_buffer(h->hal_handle, buffer_index);
504         } else {
505                 LOGE("camera_release_video_buffer not implemented");
506                 ret = CAMERA_ERROR_NOT_IMPLEMENTED;
507         }
508
509         return ret;
510 }
511
512
513 int camera_hal_interface_stop_record(camera_hal_interface *h)
514 {
515         int ret = CAMERA_ERROR_NONE;
516
517         if (h == NULL) {
518                 LOGE("NULL interface");
519                 return CAMERA_ERROR_INVALID_PARAMETER;
520         }
521
522         if (h->intf.stop_record) {
523                 ret = h->intf.stop_record(h->hal_handle);
524         } else {
525                 LOGE("camera_stop_record not implemented");
526                 ret = CAMERA_ERROR_NOT_IMPLEMENTED;
527         }
528
529         return ret;
530 }
531
532
533 int camera_hal_interface_set_command(camera_hal_interface *h, int64_t command, void *value)
534 {
535         int ret = CAMERA_ERROR_NONE;
536
537         if (h == NULL) {
538                 LOGE("NULL interface");
539                 return CAMERA_ERROR_INVALID_PARAMETER;
540         }
541
542         if (h->intf.set_command) {
543                 ret = h->intf.set_command(h->hal_handle, command, value);
544         } else {
545                 LOGE("camera_set_command not implemented");
546                 ret = CAMERA_ERROR_NOT_IMPLEMENTED;
547         }
548
549         return ret;
550 }
551
552
553 int camera_hal_interface_get_command(camera_hal_interface *h, int64_t command, void *value)
554 {
555         int ret = CAMERA_ERROR_NONE;
556
557         if (h == NULL) {
558                 LOGE("NULL interface");
559                 return CAMERA_ERROR_INVALID_PARAMETER;
560         }
561
562         if (h->intf.get_command) {
563                 ret = h->intf.get_command(h->hal_handle, command, value);
564         } else {
565                 LOGE("camera_get_command not implemented");
566                 ret = CAMERA_ERROR_NOT_IMPLEMENTED;
567         }
568
569         return ret;
570 }
571
572
573 int camera_hal_interface_set_batch_command(camera_hal_interface *h, camera_batch_command_control_t *batch_command, int64_t *error_command)
574 {
575         int ret = CAMERA_ERROR_NONE;
576
577         if (h == NULL) {
578                 LOGE("NULL interface");
579                 return CAMERA_ERROR_INVALID_PARAMETER;
580         }
581
582         if (h->intf.set_batch_command) {
583                 ret = h->intf.set_batch_command(h->hal_handle, batch_command, error_command);
584         } else {
585                 LOGE("camera_set_batch_command not implemented");
586                 ret = CAMERA_ERROR_NOT_IMPLEMENTED;
587         }
588
589         return ret;
590 }
591