-
Notifications
You must be signed in to change notification settings - Fork 7
/
v4l2_core.c
2739 lines (2391 loc) · 55.2 KB
/
v4l2_core.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************#
# guvcview http://guvcview.sourceforge.net #
# #
# Paulo Assis <pj.assis@gmail.com> #
# #
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 2 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the Free Software #
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #
# #
********************************************************************************/
/*******************************************************************************#
# #
# V4L2 core library #
# #
********************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <libv4l2.h>
#include <sys/mman.h>
#include <sys/select.h>
#include <errno.h>
#include <assert.h>
/* support for internationalization - i18n */
#include <locale.h>
#include <libintl.h>
#include "gview.h"
#include "gviewv4l2core.h"
#include "v4l2_core.h"
#include "save_image.h"
#include "soft_autofocus.h"
#include "core_time.h"
#include "uvc_h264.h"
#include "frame_decoder.h"
#include "control_profile.h"
#include "v4l2_formats.h"
#include "v4l2_controls.h"
#include "v4l2_devices.h"
#include "../config.h"
#ifndef GETTEXT_PACKAGE_V4L2CORE
#define GETTEXT_PACKAGE_V4L2CORE "gview_v4l2core"
#endif
#define __PMUTEX &(vd->mutex)
/*verbosity (global scope)*/
int verbosity = 0;
/*requested format data*/
static int my_pixelformat = 0;
static int my_width = 0;
static int my_height = 0;
static uint64_t fps_ref_ts = 0;
static uint32_t fps_frame_count = 0;
static uint8_t flag_fps_change = 0; /*set to 1 to request a fps change*/
static uint8_t disable_libv4l2 = 0; /*set to 1 to disable libv4l2 calls*/
static int frame_queue_size = 1; /*just one frame in queue (enough for a single thread)*/
/*
* ioctl with a number of retries in the case of I/O failure
* args:
* fd - device descriptor
* IOCTL_X - ioctl reference
* arg - pointer to ioctl data
*
* asserts:
* none
*
* returns - ioctl result
*/
int xioctl(int fd, int IOCTL_X, void *arg)
{
int ret = 0;
int tries= IOCTL_RETRY;
do
{
if(!disable_libv4l2)
ret = v4l2_ioctl(fd, IOCTL_X, arg);
else
ret = ioctl(fd, IOCTL_X, arg);
}
while (ret && tries-- &&
((errno == EINTR) || (errno == EAGAIN) || (errno == ETIMEDOUT)));
if (ret && (tries <= 0)) fprintf(stderr, "V4L2_CORE: ioctl (%i) retried %i times - giving up: %s)\n", IOCTL_X, IOCTL_RETRY, strerror(errno));
return (ret);
}
/*
* v4l2core constructor (called before dlopen or main)
* args:
* none
*
* asserts:
* none
*
* returns: none
*/
void __attribute__ ((constructor)) v4l2core_init()
{
//initialize device list (with udev monitoring)
v4l2core_init_device_list();
/*set defaults*/
frame_queue_size = 1;
disable_libv4l2 = 0;
}
/*
* v4l2core destructor (called before dlclose os exit)
* args:
* none
*
* asserts:
* none
*
* returns: none
*/
void __attribute__ ((destructor)) v4l2core_fini()
{
//close and free the device list
if(verbosity > 2)
printf("V4L2_CORE: closing device list\n");
v4l2core_close_v4l2_device_list();
}
/*
* Query video device capabilities and supported formats
* args:
* vd - pointer to v4l2 device handler
*
* asserts:
* vd is not null
* vd->fd is valid ( > 0 )
*
* returns: error code (E_OK)
*/
static int check_v4l2_dev(v4l2_dev_t *vd)
{
/*assertions*/
assert(vd != NULL);
assert(vd->fd > 0);
memset(&vd->cap, 0, sizeof(struct v4l2_capability));
if ( xioctl(vd->fd, VIDIOC_QUERYCAP, &vd->cap) < 0 )
{
fprintf( stderr, "V4L2_CORE: (VIDIOC_QUERYCAP) error: %s\n", strerror(errno));
return E_QUERYCAP_ERR;
}
if ( ( vd->cap.capabilities & V4L2_CAP_VIDEO_CAPTURE ) == 0)
{
fprintf(stderr, "V4L2_CORE: Error opening device %s: video capture not supported.\n",
vd->videodevice);
return E_QUERYCAP_ERR;
}
if (!(vd->cap.capabilities & V4L2_CAP_STREAMING))
{
fprintf(stderr, "V4L2_CORE: %s does not support streaming i/o\n",
vd->videodevice);
return E_QUERYCAP_ERR;
}
if(vd->cap_meth == IO_READ)
{
vd->mem[vd->buf.index] = NULL;
if (!(vd->cap.capabilities & V4L2_CAP_READWRITE))
{
fprintf(stderr, "V4L2_CORE: %s does not support read, try with mmap\n",
vd->videodevice);
return E_READ_ERR;
}
}
if(verbosity > 0)
printf("V4L2_CORE: Init. %s (location: %s)\n", vd->cap.card, vd->cap.bus_info);
/*enumerate frame formats supported by device*/
int ret = enum_frame_formats(vd);
if(ret != E_OK)
{
fprintf(stderr, "V4L2_CORE: no valid frame formats (with valid sizes) found for device\n");
return ret;
}
/*add h264 (uvc muxed) to format list if supported by device*/
add_h264_format(vd);
/*enumerate device controls*/
enumerate_v4l2_control(vd);
/*gets the current control values and sets their flags*/
get_v4l2_control_values(vd);
/*if we have a focus control initiate the software autofocus*/
if(vd->has_focus_control_id)
{
if(v4l2core_soft_autofocus_init (vd) != E_OK)
vd->has_focus_control_id = 0;
}
return E_OK;
}
/*
* unmaps v4l2 buffers
* args:
* vd - pointer to v4l2 device handler
*
* asserts:
* vd is not null
*
* returns: error code (0- E_OK)
*/
static int unmap_buff(v4l2_dev_t *vd)
{
/*assertions*/
assert(vd != NULL);
if(verbosity > 2)
printf("V4L2_CORE: unmapping v4l2 buffers\n");
int i=0;
int ret=E_OK;
switch(vd->cap_meth)
{
case IO_READ:
break;
case IO_MMAP:
for (i = 0; i < NB_BUFFER; i++)
{
// unmap old buffer
if((vd->mem[i] != MAP_FAILED) && vd->buff_length[i])
if((ret=v4l2_munmap(vd->mem[i], vd->buff_length[i]))<0)
{
fprintf(stderr, "V4L2_CORE: couldn't unmap buff: %s\n", strerror(errno));
}
}
}
return ret;
}
/*
* maps v4l2 buffers
* args:
* vd - pointer to v4l2 device handler
*
* asserts:
* vd is not null
*
* returns: error code (0- E_OK)
*/
static int map_buff(v4l2_dev_t *vd)
{
/*assertions*/
assert(vd != NULL);
if(verbosity > 2)
printf("V4L2_CORE: mapping v4l2 buffers\n");
int i = 0;
// map new buffer
for (i = 0; i < NB_BUFFER; i++)
{
vd->mem[i] = v4l2_mmap( NULL, // start anywhere
vd->buff_length[i],
PROT_READ | PROT_WRITE,
MAP_SHARED,
vd->fd,
vd->buff_offset[i]);
if (vd->mem[i] == MAP_FAILED)
{
fprintf(stderr, "V4L2_CORE: Unable to map buffer: %s\n", strerror(errno));
return E_MMAP_ERR;
}
if(verbosity > 1)
printf("V4L2_CORE: mapped buffer[%i] with length %i to pos %p\n",
i,
vd->buff_length[i],
vd->mem[i]);
}
return (E_OK);
}
/*
* Query and map buffers
* args:
* vd - pointer to v4l2 device handler
*
* asserts:
* vd is not null
*
* returns: error code (0- E_OK)
*/
static int query_buff(v4l2_dev_t *vd)
{
/*assertions*/
assert(vd != NULL);
if(verbosity > 2)
printf("V4L2_CORE: query v4l2 buffers\n");
int i=0;
int ret=E_OK;
switch(vd->cap_meth)
{
case IO_READ:
break;
case IO_MMAP:
for (i = 0; i < NB_BUFFER; i++)
{
memset(&vd->buf, 0, sizeof(struct v4l2_buffer));
vd->buf.index = i;
vd->buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
//vd->buf.flags = V4L2_BUF_FLAG_TIMECODE;
//vd->buf.timecode = vd->timecode;
//vd->buf.timestamp.tv_sec = 0;
//vd->buf.timestamp.tv_usec = 0;
vd->buf.memory = V4L2_MEMORY_MMAP;
ret = xioctl(vd->fd, VIDIOC_QUERYBUF, &vd->buf);
if (ret < 0)
{
fprintf(stderr, "V4L2_CORE: (VIDIOC_QUERYBUF) Unable to query buffer[%i]: %s\n", i, strerror(errno));
if(errno == EINVAL)
fprintf(stderr, " try with read method instead\n");
return E_QUERYBUF_ERR;
}
if (vd->buf.length <= 0)
fprintf(stderr, "V4L2_CORE: (VIDIOC_QUERYBUF) - buffer length is %i\n",
vd->buf.length);
vd->buff_length[i] = vd->buf.length;
vd->buff_offset[i] = vd->buf.m.offset;
}
// map the new buffers
if(map_buff(vd) != 0)
ret = E_MMAP_ERR;
break;
}
for(i = 0; i < vd->frame_queue_size; ++i)
vd->frame_queue[i].raw_frame_max_size = vd->buf.length;
return ret;
}
/*
* Queue Buffers
* args:
* vd - pointer to v4l2 device handler
*
* asserts:
* vd is not null
*
* returns: error code (0- E_OK)
*/
static int queue_buff(v4l2_dev_t *vd)
{
/*assertions*/
assert(vd != NULL);
if(verbosity > 2)
printf("V4L2_CORE: queue v4l2 buffers\n");
int i=0;
int ret=E_OK;
switch(vd->cap_meth)
{
case IO_READ:
break;
case IO_MMAP:
default:
for (i = 0; i < NB_BUFFER; ++i)
{
memset(&vd->buf, 0, sizeof(struct v4l2_buffer));
vd->buf.index = i;
vd->buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
//vd->buf.flags = V4L2_BUF_FLAG_TIMECODE;
//vd->buf.timecode = vd->timecode;
//vd->buf.timestamp.tv_sec = 0;
//vd->buf.timestamp.tv_usec = 0;
vd->buf.memory = V4L2_MEMORY_MMAP;
ret = xioctl(vd->fd, VIDIOC_QBUF, &vd->buf);
if (ret < 0)
{
fprintf(stderr, "V4L2_CORE: (VIDIOC_QBUF) Unable to queue buffer: %s\n", strerror(errno));
return E_QBUF_ERR;
}
}
vd->buf.index = 0; /*reset index*/
}
return ret;
}
/*
* do a VIDIOC_S_PARM ioctl for setting frame rate
* args:
* vd - pointer to v4l2 device handler
*
* asserts:
* vd is not null
*
* returns: error code
*/
static int do_v4l2_framerate_update(v4l2_dev_t *vd)
{
/*asserts*/
assert(vd != NULL);
int ret = 0;
/*get the current stream parameters*/
vd->streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
ret = xioctl(vd->fd, VIDIOC_G_PARM, &vd->streamparm);
if (ret < 0)
{
fprintf(stderr, "V4L2_CORE: (VIDIOC_G_PARM) error: %s\n", strerror(errno));
fprintf(stderr, "V4L2_CORE: Unable to set %d/%d fps\n", vd->fps_num, vd->fps_denom);
return ret;
}
if (!(vd->streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME))
{
fprintf(stderr, "V4L2_CORE: V4L2_CAP_TIMEPERFRAME not supported\n");
}
vd->streamparm.parm.capture.timeperframe.numerator = vd->fps_num;
vd->streamparm.parm.capture.timeperframe.denominator = vd->fps_denom;
/*request the new frame rate*/
ret = xioctl(vd->fd, VIDIOC_S_PARM, &vd->streamparm);
if (ret < 0)
{
fprintf(stderr, "V4L2_CORE: (VIDIOC_S_PARM) error: %s\n", strerror(errno));
fprintf(stderr, "V4L2_CORE: Unable to set %d/%d fps\n", vd->fps_num, vd->fps_denom);
}
return ret;
}
/*
* sets video device frame rate
* args:
* vd - pointer to v4l2 device handler
*
* asserts:
* vd is not null
*
* returns: VIDIOC_S_PARM ioctl result value
* (sets vd->fps_denom and vd->fps_num to device value)
*/
static int set_v4l2_framerate (v4l2_dev_t *vd)
{
/*assertions*/
assert(vd != NULL);
if(verbosity > 2)
printf("V4L2_CORE: trying to change fps to %i/%i\n", vd->fps_num, vd->fps_denom);
int ret = 0;
/*lock the mutex*/
__LOCK_MUTEX( __PMUTEX );
/*store streaming flag*/
uint8_t stream_status = vd->streaming;
/*try to stop the video stream*/
if(stream_status == STRM_OK)
v4l2core_stop_stream(vd);
switch(vd->cap_meth)
{
case IO_READ:
ret = do_v4l2_framerate_update(vd);
break;
case IO_MMAP:
if(stream_status == STRM_OK)
{
/*unmap the buffers*/
unmap_buff(vd);
}
ret = do_v4l2_framerate_update(vd);
/*
* For uvc muxed H264 stream
* since we are restarting the video stream and codec values will be reset
* commit the codec data again
*/
if(vd->requested_fmt == V4L2_PIX_FMT_H264 && h264_get_support() == H264_MUXED)
{
if(verbosity > 0)
printf("V4L2_CORE: setting muxed H264 stream in MJPG container\n");
set_h264_muxed_format(vd);
}
break;
}
if(stream_status == STRM_OK)
{
query_buff(vd); /*also mmaps the buffers*/
queue_buff(vd);
}
/*try to start the video stream*/
if(stream_status == STRM_OK)
v4l2core_start_stream(vd);
/*unlock the mutex*/
__UNLOCK_MUTEX( __PMUTEX );
return ret;
}
/*
* checks if frame data is available
* args:
* vd - pointer to v4l2 device handler
*
* asserts:
* vd is not null
*
* returns: error code (0- E_OK)
*/
static int check_frame_available(v4l2_dev_t *vd)
{
/*asserts*/
assert(vd != NULL);
int ret = E_OK;
fd_set rdset;
struct timeval timeout;
/*lock the mutex*/
__LOCK_MUTEX( __PMUTEX );
int stream_state = vd->streaming;
/*unlock the mutex*/
__UNLOCK_MUTEX( __PMUTEX );
/*make sure streaming is on*/
if(stream_state != STRM_OK)
{
if(stream_state == STRM_REQ_STOP)
v4l2core_stop_stream(vd);
fprintf(stderr, "V4L2_CORE: (get_v4l2_frame) video stream must be started first\n");
return E_NO_STREAM_ERR;
}
/*a fps change was requested while streaming*/
if(flag_fps_change > 0)
{
if(verbosity > 2)
printf("V4L2_CORE: fps change request detected\n");
set_v4l2_framerate(vd);
flag_fps_change = 0;
}
FD_ZERO(&rdset);
FD_SET(vd->fd, &rdset);
timeout.tv_sec = 1; /* 1 sec timeout*/
timeout.tv_usec = 0;
/* select - wait for data or timeout*/
ret = select(vd->fd + 1, &rdset, NULL, NULL, &timeout);
if (ret < 0)
{
fprintf(stderr, "V4L2_CORE: Could not grab image (select error): %s\n", strerror(errno));
return E_SELECT_ERR;
}
if (ret == 0)
{
fprintf(stderr, "V4L2_CORE: Could not grab image (select timeout): %s\n", strerror(errno));
return E_SELECT_TIMEOUT_ERR;
}
if ((ret > 0) && (FD_ISSET(vd->fd, &rdset)))
return E_OK;
return E_UNKNOWN_ERR;
}
/*
* set verbosity
* args:
* level - verbosity level
*
* asserts:
* none
*
* returns void
*/
void v4l2core_set_verbosity(int level)
{
verbosity = level;
}
/*
* set frame queue size (set before v4l2core_init_dev)
* args:
* size - size in frames of frame queue
*
* asserts:
* none
*
* returns void
*/
void v4l2core_set_frame_queue_size(int size)
{
frame_queue_size = size;
}
/*
* disable libv4l2 calls
* args:
* none
*
* asserts:
* none
*
* returns void
*/
void v4l2core_disable_libv4l2()
{
disable_libv4l2 = 1;
}
/*
* enable libv4l2 calls (default)
* args:
* none
*
* asserts:
* none
*
* returns void
*/
void v4l2core_enable_libv4l2()
{
disable_libv4l2 = 0;
}
/*
* set v4l2 capture method to use
* args:
* vd - pointer to v4l2 device handler
* method - capture method (IO_READ or IO_MMAP)
*
* asserts:
* vd is not null
*
* returns: none
*/
void v4l2core_set_capture_method(v4l2_dev_t *vd, int method)
{
/*asserts*/
assert(vd != NULL);
vd->cap_meth = method;
}
/*
* define fps values
* args:
* vd - pointer to v4l2 device handler
* num - fps numerator
* denom - fps denominator
*
* asserts:
* vd is not null
*
* returns - void
*/
void v4l2core_define_fps(v4l2_dev_t *vd, int num, int denom)
{
/*assertions*/
assert(vd != NULL);
if(num > 0)
vd->fps_num = num;
if(denom > 0)
vd->fps_denom = denom;
if(verbosity > 2)
printf("V4L2_CORE: fps configured to %i/%i\n", vd->fps_num, vd->fps_denom);
}
/*
* get requested fps numerator
* args:
* vd - pointer to v4l2 device handler
*
* asserts:
* vd is not null
*
* returns - requested fps numerator
*/
int v4l2core_get_fps_num(v4l2_dev_t *vd)
{
/*assertions*/
assert(vd != NULL);
return vd->fps_num;
}
/*
* get requested fps denominator
* args:
* vd - pointer to v4l2 device handler
*
* asserts:
* vd is not null
*
* returns - requested fps denominator
*/
int v4l2core_get_fps_denom(v4l2_dev_t *vd)
{
/*assertions*/
assert(vd != NULL);
return vd->fps_denom;
}
/*
* get real fps
* args:
* vd - pointer to v4l2 device handler
*
* asserts:
* vd is not null
*
* returns: double with real fps value
*/
double v4l2core_get_realfps(v4l2_dev_t *vd)
{
/*assertions*/
assert(vd != NULL);
return(vd->real_fps);
}
/*
* get videodevice name
* args:
* vd - pointer to v4l2 device handler
*
* asserts:
* vd is not null
*
* return: string with videodevice name
*/
const char *v4l2core_get_videodevice(v4l2_dev_t *vd)
{
/*assertions*/
assert(vd != NULL);
return (const char *) vd->videodevice;
}
/*
* get device available number of formats
* args:
* vd - pointer to v4l2 device handler
*
* asserts:
* vd is not null
*
* returns - number of formats for device
*/
int v4l2core_get_number_formats(v4l2_dev_t *vd)
{
/*assertions*/
assert(vd != NULL);
return vd->numb_formats;
}
/*
* get has_pantilt_id flag
* args:
* vd - pointer to v4l2 device handler
*
* asserts:
* vd is not null
*
* returns: has_pantilt_id flag
*/
int v4l2core_has_pantilt_id(v4l2_dev_t *vd)
{
/*assertions*/
assert(vd != NULL);
return vd->has_pantilt_control_id;
}
/*
* get has_focus_control_id flag
* args:
* vd -pointer to device data
*
* asserts:
* vd is not null
*
* returns: has_focus_control_id flag
*/
int v4l2core_has_focus_control_id(v4l2_dev_t *vd)
{
/*assertions*/
assert(vd != NULL);
return vd->has_focus_control_id;
}
/*
* sets bayer pixel order
* args:
* vd - pointer to v4l2 device handler
* order - pixel order
*
* asserts:
* vd is not null
*
* returns - void
*/
void v4l2core_set_bayer_pix_order(v4l2_dev_t *vd, uint8_t order)
{
/*assertions*/
assert(vd != NULL);
vd->bayer_pix_order = order;
}
/*
* gets bayer pixel order
* args:
* vd - pointer to v4l2 device handler
*
* asserts:
* vd is not null
*
* returns - bayer pixel order
*/
uint8_t v4l2core_get_bayer_pix_order(v4l2_dev_t *vd)
{
/*assertions*/
assert(vd != NULL);
return vd->bayer_pix_order;
}
/*
* flags bayer mode
* args:
* vd -pointer to device data
* flag - 1 if we are streaming bayer data (0 otherwise)
*
* asserts:
* vd is not null
*
* returns - void
*/
void v4l2core_set_isbayer(v4l2_dev_t *vd, uint8_t flag)
{
/*assertions*/
assert(vd != NULL);
vd->isbayer = flag;
}
/*
* gets bayer pixel order
* args:
* vd - pointer to v4l2 device handler
*
* asserts:
* vd is not null
*
* returns - isbayer flag
*/
uint8_t v4l2core_get_isbayer(v4l2_dev_t *vd)
{
/*assertions*/
assert(vd != NULL);
return vd->isbayer;
}
/*
* gets current device index
* args:
* vd - pointer to v4l2 device handler
*
* asserts:
* vd is not null
*
* returns - device index
*/
int v4l2core_get_this_device_index(v4l2_dev_t *vd)
{
/*assertions*/
assert(vd != NULL);
return vd->this_device;
}
/*
* Start video stream
* args:
* vd - pointer to v4l2 device handler
*
* asserts:
* vd is not null
*
* returns: VIDIOC_STREAMON ioctl result (E_OK or E_STREAMON_ERR)
*/
int v4l2core_start_stream(v4l2_dev_t *vd)
{
/*assertions*/
assert(vd != NULL);
if(vd->streaming == STRM_OK)
{
fprintf(stderr, "V4L2_CORE: (stream already started) stream_status = STRM_OK\n");
return E_OK;
}
int type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
int ret=E_OK;
switch(vd->cap_meth)
{
case IO_READ:
//do nothing
break;
case IO_MMAP:
default:
ret = xioctl(vd->fd, VIDIOC_STREAMON, &type);
if (ret < 0)
{
fprintf(stderr, "V4L2_CORE: (VIDIOC_STREAMON) Unable to start stream: %s \n", strerror(errno));
return E_STREAMON_ERR;
}
break;
}
vd->streaming = STRM_OK;
if(verbosity > 2)
printf("V4L2_CORE: (VIDIOC_STREAMON) stream_status = STRM_OK\n");
return ret;
}
/*
* request video stream to stop
* args:
* vd - pointer to v4l2 device handler
*
* asserts:
* vd is not null
*
* returns: error code (0 -OK)
*/
int v4l2core_request_stop_stream(v4l2_dev_t *vd)
{