-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathv4l2.cpp
429 lines (385 loc) · 13.2 KB
/
v4l2.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
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
//=============================================================================
// Cam
// Webcam client
//
// Copyright (C) 2016 Werner Schweer
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2
// as published by the Free Software Foundation and appearing in
// the file LICENCE.GPL
//=============================================================================
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/videodev2.h>
#include "v4l2.h"
//---------------------------------------------------------
// V4l2
//---------------------------------------------------------
V4l2::V4l2()
{
}
V4l2::~V4l2()
{
close();
}
//---------------------------------------------------------
// open
//---------------------------------------------------------
bool V4l2::open(const QString& p)
{
if (fd != -1)
return false;
path = p;
char* videodevice = path.toLocal8Bit().data();
fd = ::open(videodevice, O_RDWR);
return fd != -1;
}
//---------------------------------------------------------
// close
//---------------------------------------------------------
bool V4l2::close()
{
if (fd == -1)
return false;
int rv = ::close(fd);
fd = -1;
return rv != -1;
}
//---------------------------------------------------------
// canVideoCapture
//---------------------------------------------------------
bool V4l2::canVideoCapture()
{
struct v4l2_capability cap;
memset(&cap, 0, sizeof(struct v4l2_capability));
int ret = ioctl(fd, VIDIOC_QUERYCAP, &cap);
if (ret < 0) {
fprintf(stderr, "Camera: <%s>: unable to query device.\n", qPrintable(path));
return false;
}
return cap.capabilities & V4L2_CAP_VIDEO_CAPTURE;
}
//---------------------------------------------------------
// canStreaming
//---------------------------------------------------------
bool V4l2::canStreaming()
{
struct v4l2_capability cap;
memset(&cap, 0, sizeof(struct v4l2_capability));
int ret = ioctl(fd, VIDIOC_QUERYCAP, &cap);
if (ret < 0) {
fprintf(stderr, "Camera: <%s>: unable to query device.\n", qPrintable(path));
return false;
}
return cap.capabilities & V4L2_CAP_STREAMING;
}
//---------------------------------------------------------
// setMjpegFormat
//---------------------------------------------------------
bool V4l2::setMjpegFormat(int w, int h)
{
struct v4l2_format fmt;
memset(&fmt, 0, sizeof(struct v4l2_format));
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmt.fmt.pix.width = w;
fmt.fmt.pix.height = h;
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
fmt.fmt.pix.field = V4L2_FIELD_ANY;
int ret = ioctl(fd, VIDIOC_S_FMT, &fmt);
if (ret < 0) {
fprintf(stderr, "Camera <%s> does not support format MJPEG: %s.\n", qPrintable(path), strerror(errno));
return false;
}
if ((int(fmt.fmt.pix.width) != w) || (int(fmt.fmt.pix.height) != h)) {
fprintf(stderr, " format %d x %d unavailable, get %d x %d \n",
w, h, fmt.fmt.pix.width, fmt.fmt.pix.height);
}
return true;
}
//---------------------------------------------------------
// setFramerate
//---------------------------------------------------------
bool V4l2::setFramerate(int fps)
{
struct v4l2_streamparm setfps;
memset(&setfps, 0, sizeof(setfps));
setfps.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
setfps.parm.capture.timeperframe.numerator = 1;
setfps.parm.capture.timeperframe.denominator = fps;
int ret = ioctl(fd, VIDIOC_S_PARM, &setfps);
return ret >= 0;
}
//---------------------------------------------------------
// isControl
// return >= 0 ok otherwhise -1
//---------------------------------------------------------
int V4l2::isControl(int control, struct v4l2_queryctrl* queryctrl)
{
int err = 0;
queryctrl->id = control;
if ((err = ioctl(fd, VIDIOC_QUERYCTRL, queryctrl)) < 0)
printf("ioctl querycontrol error %d \n", errno);
else if (queryctrl->flags & V4L2_CTRL_FLAG_DISABLED)
printf("control %s disabled \n", (char*) queryctrl->name);
else if (queryctrl->flags & V4L2_CTRL_TYPE_BOOLEAN)
return 1;
else if (queryctrl->type & V4L2_CTRL_TYPE_INTEGER)
return 0;
else
printf("contol %s unsupported \n", (char*) queryctrl->name);
return -1;
}
//---------------------------------------------------------
// getControl
//---------------------------------------------------------
int V4l2::getControl(int control)
{
struct v4l2_queryctrl queryctrl;
struct v4l2_control control_s;
int err;
if (isControl(control, &queryctrl) < 0)
return -1;
control_s.id = control;
if ((err = ioctl(fd, VIDIOC_G_CTRL, &control_s)) < 0) {
printf("ioctl get control error\n");
return -1;
}
return control_s.value;
}
//---------------------------------------------------------
// setControl
//---------------------------------------------------------
int V4l2::setControl(int control, int value)
{
struct v4l2_control control_s;
struct v4l2_queryctrl queryctrl;
int min, max;
int err;
if (isControl(control, &queryctrl) < 0)
return -1;
min = queryctrl.minimum;
max = queryctrl.maximum;
if ((value >= min) && (value <= max)) {
control_s.id = control;
control_s.value = value;
if ((err = ioctl(fd, VIDIOC_S_CTRL, &control_s)) < 0) {
printf("ioctl set control error\n");
return -1;
}
}
return 0;
}
//---------------------------------------------------------
// upControl
//---------------------------------------------------------
int V4l2::upControl(int control)
{
struct v4l2_control control_s;
struct v4l2_queryctrl queryctrl;
int max, current, step;
int err;
if (isControl(control, &queryctrl) < 0)
return -1;
max = queryctrl.maximum;
step = queryctrl.step;
current = getControl(control);
current += step;
if (current <= max) {
control_s.id = control;
control_s.value = current;
if ((err = ioctl(fd, VIDIOC_S_CTRL, &control_s)) < 0) {
printf("ioctl set control error\n");
return -1;
}
printf ("Control name:%s set to value:%d\n", queryctrl.name, control_s.value);
}
else {
printf ("Control name:%s already has max value:%d \n", queryctrl.name, max);
}
return control_s.value;
}
//---------------------------------------------------------
// downControl
//---------------------------------------------------------
int V4l2::downControl(int control)
{
struct v4l2_control control_s;
struct v4l2_queryctrl queryctrl;
int min, current, step;
int err;
if (isControl(control, &queryctrl) < 0)
return -1;
min = queryctrl.minimum;
step = queryctrl.step;
current = getControl(control);
current -= step;
if (current >= min) {
control_s.id = control;
control_s.value = current;
if ((err = ioctl(fd, VIDIOC_S_CTRL, &control_s)) < 0) {
printf("ioctl set control error\n");
return -1;
}
printf ("Control name:%s set to value:%d\n", queryctrl.name, control_s.value);
}
else
printf ("Control name:%s already has min value:%d \n", queryctrl.name, min);
return control_s.value;
}
//---------------------------------------------------------
// toggleControl
//---------------------------------------------------------
int V4l2::toggleControl(int control)
{
struct v4l2_control control_s;
struct v4l2_queryctrl queryctrl;
int current;
int err;
if (isControl(control, &queryctrl) != 1)
return -1;
current = getControl(control);
control_s.id = control;
control_s.value = !current;
if ((err = ioctl(fd, VIDIOC_S_CTRL, &control_s)) < 0) {
printf("ioctl toggle control error\n");
return -1;
}
return control_s.value;
}
//---------------------------------------------------------
// resetControl
//---------------------------------------------------------
bool V4l2::resetControl(int control)
{
struct v4l2_queryctrl queryctrl;
if (isControl(control, &queryctrl) < 0)
return false;
int val_def = queryctrl.default_value;
struct v4l2_control control_s;
control_s.id = control;
control_s.value = val_def;
int err = ioctl(fd, VIDIOC_S_CTRL, &control_s);
if (err < 0) {
printf("ioctl reset control error\n");
return false;
}
return true;
}
//---------------------------------------------------------
// grab
// try reading a picture into tmpbuffer
// return image size or -1 on error
//---------------------------------------------------------
#define HEADERFRAME1 0xaf
QImage V4l2::grab()
{
struct v4l2_buffer buf;
memset(&buf, 0, sizeof(struct v4l2_buffer));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
int ret = ioctl(fd, VIDIOC_DQBUF, &buf);
if (ret < 0) {
printf("Unable to dequeue buffer: %s\n", strerror(errno));
return QImage();
}
int size = buf.bytesused;
if (size <= HEADERFRAME1) {
printf("Ignoring empty buffer ...\n");
return QImage();
}
QByteArray data((const char*)mem[buf.index], size);
QImage image = QImage::fromData(data, "mjpeg");
ret = ioctl(fd, VIDIOC_QBUF, &buf);
if (ret < 0)
printf("Unable to requeue buffer (%d).\n", errno);
return image;
}
//---------------------------------------------------------
// initBuffers
//---------------------------------------------------------
bool V4l2::initBuffers()
{
/*
* request buffers
*/
struct v4l2_requestbuffers rb;
memset(&rb, 0, sizeof(struct v4l2_requestbuffers));
rb.count = NB_BUFFER;
rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
rb.memory = V4L2_MEMORY_MMAP;
int ret = ioctl(fd, VIDIOC_REQBUFS, &rb);
if (ret < 0) {
fprintf(stderr, "Unable to allocate buffers: %s\n", strerror(errno));
return false;
}
/*
* map the buffers
*/
for (int i = 0; i < NB_BUFFER; i++) {
struct v4l2_buffer buf;
memset(&buf, 0, sizeof(struct v4l2_buffer));
buf.index = i;
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
ret = ioctl(fd, VIDIOC_QUERYBUF, &buf);
if (ret < 0) {
fprintf(stderr, "Unable to query buffer: %s\n", strerror(errno));
return false;
}
mem[i] = mmap(0, buf.length, PROT_READ, MAP_SHARED, fd, buf.m.offset);
if (mem[i] == MAP_FAILED) {
fprintf(stderr, "Unable to map buffer: %s\n", strerror(errno));
return false;
}
}
/*
* Queue the buffers.
*/
for (int i = 0; i < NB_BUFFER; ++i) {
struct v4l2_buffer buf;
memset(&buf, 0, sizeof(struct v4l2_buffer));
buf.index = i;
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
ret = ioctl(fd, VIDIOC_QBUF, &buf);
if (ret < 0) {
fprintf(stderr, "Unable to queue buffer: %s\n", strerror(errno));
return false;
}
}
return true;
}
//---------------------------------------------------------
// freeBuffers
//---------------------------------------------------------
bool V4l2::freeBuffers()
{
/*
* unmap the buffers
*/
for (int i = 0; i < NB_BUFFER; i++) {
struct v4l2_buffer buf;
memset(&buf, 0, sizeof(struct v4l2_buffer));
buf.index = i;
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
int ret = ioctl(fd, VIDIOC_QUERYBUF, &buf);
if (ret < 0) {
fprintf(stderr, "Unable to query buffer: %s\n", strerror(errno));
return false;
}
ret = munmap(mem[i], buf.length);
if (ret) {
fprintf(stderr, "Unable to unmap buffer: %s\n", strerror(errno));
return false;
}
}
return true;
}