forked from mpromonet/libv4l2cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
130 lines (118 loc) · 3.66 KB
/
main.cpp
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
/* ---------------------------------------------------------------------------
** This software is in the public domain, furnished "as is", without technical
** support, and with no warranty, express or implied, as to its usefulness for
** any purpose.
**
** main.cpp
**
** test V4L2 capture device
**
** -------------------------------------------------------------------------*/
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include "logger.h"
#include "V4l2Capture.h"
int stop=0;
/* ---------------------------------------------------------------------------
** SIGINT handler
** -------------------------------------------------------------------------*/
void sighandler(int)
{
printf("SIGINT\n");
stop =1;
}
/* ---------------------------------------------------------------------------
** main
** -------------------------------------------------------------------------*/
int main(int argc, char* argv[])
{
int verbose = 0;
const char *in_devname = "/dev/video0";
V4l2IoType ioTypeIn = IOTYPE_MMAP;
int format = 0;
int width = 0;
int height = 0;
int fps = 0;
int framecount = 0;
int c = 0;
while ((c = getopt (argc, argv, "x:hv:" "G:f:r")) != -1)
{
switch (c)
{
case 'v': verbose = 1; if (optarg && *optarg=='v') verbose++; break;
case 'r': ioTypeIn = IOTYPE_READWRITE ; break;
case 'G': sscanf(optarg,"%dx%dx%d", &width, &height, &fps) ; break;
case 'f': format = V4l2Device::fourcc(optarg) ; break;
case 'x': sscanf(optarg,"%d", &framecount) ; break;
case 'h':
{
std::cout << argv[0] << " [-v[v]] [-G <width>x<height>x<fps>] [-f format] [device] [-r]" << std::endl;
std::cout << "\t -G <width>x<height>x<fps> : set capture resolution" << std::endl;
std::cout << "\t -v : verbose " << std::endl;
std::cout << "\t -vv : very verbose " << std::endl;
std::cout << "\t -r : V4L2 capture using read interface (default use memory mapped buffers)" << std::endl;
std::cout << "\t -x <count> : read <count> frames and save them in current dir." << std::endl;
std::cout << "\t device : V4L2 capture device (default "<< in_devname << ")" << std::endl;
exit(0);
}
}
}
if (optind<argc)
{
in_devname = argv[optind];
optind++;
}
// initialize log4cpp
initLogger(verbose);
// init V4L2 capture interface
V4L2DeviceParameters param(in_devname, format, width, height, fps, ioTypeIn, verbose);
V4l2Capture* videoCapture = V4l2Capture::create(param);
if (videoCapture == NULL)
{
LOG(WARN) << "Cannot reading from V4L2 capture interface for device:" << in_devname;
}
else
{
timeval tv;
LOG(NOTICE) << "Start reading from " << in_devname;
signal(SIGINT,sighandler);
while (!stop)
{
tv.tv_sec=1;
tv.tv_usec=0;
int ret = videoCapture->isReadable(&tv);
if (ret == 1)
{
char buffer[videoCapture->getBufferSize()];
int rsize = videoCapture->read(buffer, sizeof(buffer));
if (rsize == -1)
{
LOG(NOTICE) << "stop " << strerror(errno);
stop=1;
}
else
{
LOG(NOTICE) << "size:" << rsize;
static int stop_count = 0;
if(framecount and stop_count < framecount){
std::string filename = "Frame" + std::to_string(stop_count) + '.' + V4l2Device::fourcc(videoCapture->getFormat());
stop_count++;
FILE *fp = fopen(filename.c_str(), "wb");
fwrite(buffer, 1, rsize, fp);
fclose(fp);
LOG(NOTICE) << "saved:\t" << filename;
}
}
}
else if (ret == -1)
{
LOG(NOTICE) << "stop " << strerror(errno);
stop=1;
}
}
delete videoCapture;
}
return 0;
}