-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_usbcam_cpp.cpp
246 lines (215 loc) · 6.25 KB
/
test_usbcam_cpp.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
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
#include <fcntl.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <chrono>
#include <linux/videodev2.h>
// #include <opencv2/opencv.hpp>
// #include <opencv2/imgproc/imgproc.hpp>
// #include <opencv2/highgui/highgui.hpp>
using namespace std;
#define REQUEST_BUFFER_NUM 10
static int xioctl(int fd, int request, void *arg)
{
int r;
do {
r = ioctl (fd, request, arg);
if (request == VIDIOC_DQBUF) {
std::cout << "r : " << r << std::endl;
}
if(errno == EAGAIN){
cout << "EAGAIN" << endl;
}else if(errno == EINVAL){
cout << "EINVAL" << endl;
}else if(errno == EIO){
cout << "EIO" << endl;
}else if(errno == EPIPE){
cout << "EPIPE" << endl;
}else if(errno == EACCES){
cout << "EACCESS" << endl;
}else if(errno == EBUSY){
cout << "EBUSY" << endl;
}else{
cout << "other error" << endl;
}
} while (-1 == r && EINTR == errno);
if(r == -1){
cout << "loop breaked and error occurred" << endl;
if(errno == EAGAIN){
cout << "EAGAIN" << endl;
}else if(errno == EINVAL){
cout << "EINVAL" << endl;
}else if(errno == EIO){
cout << "EIO" << endl;
}else if(errno == EPIPE){
cout << "EPIPE" << endl;
}else if(errno == EACCES){
cout << "EACCESS" << endl;
}else if(errno == EBUSY){
cout << "EBUSY" << endl;
}else{
cout << "other error" << endl;
}
}
return r;
}
unsigned char *buffers[4];
int main() {
// 1. Open Video Device.
int fd;
fd = open("/dev/video1", O_RDWR, 0);
if (fd == -1)
{
std::cout << "Failed to open video device." << std::endl;
return 1;
}
// 2. Querying video capabilities.
struct v4l2_capability caps;
memset(&caps, 0, sizeof(caps));
if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &caps))
{
std::cout << "Failed to query capabilities." << std::endl;
return 1;
}
std::cout << "bus_info : " << caps.bus_info << std::endl;
std::cout << "card : " << caps.card << std::endl;
std::cout << "driver : " << caps.driver << std::endl;
std::cout << "version : " << caps.version << std::endl;
// 3. Format Specification.
{
struct v4l2_format fmt;
memset(&(fmt), 0, sizeof(fmt));
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmt.fmt.pix.width = 640;
fmt.fmt.pix.height = 480;
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
fmt.fmt.pix.field = V4L2_FIELD_NONE;
if (-1 == xioctl(fd, VIDIOC_S_FMT, &fmt))
{
std::cout << "Failed to set pixel format." << std::endl;
return 1;
}
}
int got_buffer_num;
// 4. Request Buffer
{
struct v4l2_requestbuffers req;
memset(&(req), 0, sizeof(req));
req.count = REQUEST_BUFFER_NUM;
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
req.memory = V4L2_MEMORY_MMAP;
if (-1 == xioctl(fd, VIDIOC_REQBUFS, &req))
{
std::cout << "Failed to request buffer." << std::endl;
return 1;
}
cout << "we could get " << req.count << " buffers. " << endl;
got_buffer_num = req.count;
}
// 5. Query Buffer
{
for(int bufferindex = 0; bufferindex < got_buffer_num; bufferindex++){
struct v4l2_buffer buf;
memset(&(buf), 0, sizeof(buf));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = bufferindex;
if(-1 == xioctl(fd, VIDIOC_QUERYBUF, &buf))
{
std::cout << "Failed to query buffer." << std::endl;
return 1;
}
std::cout << "buf.length : " << buf.length << std::endl;
std::cout << "buf.m.offset : " << buf.m.offset << std::endl;
buffers[bufferindex] = (unsigned char*)mmap (NULL, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, buf.m.offset);
if (MAP_FAILED == buffers[bufferindex])
cerr << "mmap" << endl;
}
}
// 5.5 QBUF Request
{
for (int i = 0; i < got_buffer_num; ++i) {
struct v4l2_buffer buf;
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = i;
if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
cerr << "VIDIOC_QBUF" << endl;
}
}
// 6. Start Streaming
{
struct v4l2_buffer buf;
memset(&(buf), 0, sizeof(buf));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if(-1 == xioctl(fd, VIDIOC_STREAMON, &buf.type))
{
std::cout << "Start Capture" << std::endl;
return 1;
}
}
char camdata[640*480*2];
std::chrono::system_clock::time_point t1, t2, t3, t4, t5, t6, t7;
for(int i = 0; i < 100; i++){
t1 = std::chrono::system_clock::now();
// 7. Capture Image
// 8. Store Image in OpenCV Data Type
{
{
struct v4l2_buffer buf;
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
fd_set fds;
FD_ZERO(&fds);
FD_SET(fd, &fds);
struct timeval tv = {0};
tv.tv_sec = 2;
int r = select(fd+1, &fds, NULL, NULL, &tv);
if(-1 == r){
std::cout << "Waiting for Frame" << std::endl;
return 1;
}
memset(&(buf), 0, sizeof(buf));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
if(-1 == xioctl(fd, VIDIOC_DQBUF, &buf))
{
std::cout << "Retrieving Frame" << std::endl;
return 1;
}
cout << "buf.index : " << buf.index << endl;
// Connect buffer to queue for next capture.
if (-1 == xioctl(fd, VIDIOC_QBUF, &buf)) {
std::cout << "VIDIOC_QBUF" << std::endl;
}
memcpy(camdata, buffers[buf.index], 640 * 480 * 2);
}
}
t2 = std::chrono::system_clock::now();
// 9. Convert YUYV to BGR and Display Image
{
// cv::Mat rawimg(480, 640, CV_8UC2);
// cv::Mat dstimg(480, 640, CV_8UC2);
// memcpy(rawimg.data, camdata, 640 * 480 * 2);
// cv::cvtColor(rawimg, dstimg, cv::COLOR_YUV2BGR_YUYV);
// cv::imshow("image_test", dstimg);
// cv::waitKey(1);
}
//show fps
double elapsed = (double)std::chrono::duration_cast<std::chrono::milliseconds>(t2-t1).count();
cout << "elapsed:" << elapsed << "[milisec]" << endl;
cout << "fps:" << 1000.0/elapsed << "[fps]" << endl;
}
// 10. Turn off streaming.
struct v4l2_buffer buf;
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
if (-1 == xioctl(fd, VIDIOC_STREAMOFF, &buf.type)) {
std::cout << "VIDIOC_STREAMOFF" << std::endl;
}
return 0;
}