-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbackend_v4l.c
258 lines (221 loc) · 7.45 KB
/
backend_v4l.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
#include "interface.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <math.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/poll.h>
#include <time.h>
#include <unistd.h>
#include <linux/videodev2.h>
// Parameters for PS3 Eye
#define CAMERA_WIDTH 320
#define CAMERA_HEIGHT 240
#define CAMERA_PIXELFORMAT V4L2_PIX_FMT_SGBRG8
#define CAMERA_FIELD V4L2_FIELD_NONE
#define CAMERA_TIMEPERFRAME_NUM 1000
#define CAMERA_TIMEPERFRAME_DENOM 187000
#define THRESHOLD 0.3
#define NUM_BUFS 5
struct buf {
void *data;
size_t len;
};
struct state {
int fd;
struct buf bufs[NUM_BUFS];
enum WhatToDo output_state;
struct analysis control;
};
static int ioctl_loop(int fd, unsigned long int req, void *arg) {
// In case frontend has weird signal settings, check for EINTR
while (1) {
int r = ioctl(fd, req, arg);
if (r == -1 && errno == EINTR) {
continue;
}
return r;
}
}
void *setup_backend(int camera) {
struct state *s = calloc(1, sizeof(struct state));
char devname[50];
sprintf(devname, "/dev/video%d", camera);
s->fd = open(devname, O_RDWR | O_NONBLOCK, 0);
if (s->fd == -1) {
fprintf(stderr, "Failed to open fd at %s: %s\n", devname,
strerror(errno));
goto fail_free;
}
struct v4l2_capability cap;
if (ioctl_loop(s->fd, VIDIOC_QUERYCAP, &cap) < 0) {
fprintf(stderr, "Not a video device: %s\n", strerror(errno));
goto fail_vfd;
}
uint32_t needed_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
if ((cap.capabilities & needed_caps) != needed_caps) {
fprintf(stderr, "Lacks needed capabilities: %x\n", cap.capabilities);
goto fail_vfd;
}
struct v4l2_streamparm sparm;
memset(&sparm, 0, sizeof(sparm));
sparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
sparm.parm.capture.timeperframe.numerator = CAMERA_TIMEPERFRAME_NUM;
sparm.parm.capture.timeperframe.denominator = CAMERA_TIMEPERFRAME_DENOM;
if (ioctl_loop(s->fd, VIDIOC_S_PARM, &sparm) < 0) {
fprintf(stderr, "Failed to set FPS: %s\n", strerror(errno));
goto fail_vfd;
}
if (ioctl_loop(s->fd, VIDIOC_G_PARM, &sparm) < 0) {
fprintf(stderr, "Failed to get FPS: %s\n", strerror(errno));
goto fail_vfd;
}
fprintf(stderr, "Camera FPS is: %f\n",
sparm.parm.capture.timeperframe.denominator /
(double)sparm.parm.capture.timeperframe.numerator);
struct v4l2_format fmt;
memset(&fmt, 0, sizeof(fmt));
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmt.fmt.pix.width = CAMERA_WIDTH;
fmt.fmt.pix.height = CAMERA_HEIGHT;
fmt.fmt.pix.pixelformat = CAMERA_PIXELFORMAT;
fmt.fmt.pix.field = CAMERA_FIELD;
if (ioctl_loop(s->fd, VIDIOC_S_FMT, &fmt) < 0) {
fprintf(stderr, "Failed to set video format: %s\n", strerror(errno));
goto fail_vfd;
}
fprintf(stderr,
"width=%d/%d height=%d/%d pixelfmt=%x/%x field=%d/%d colorspace=%d "
"xfer_func=%d\n",
fmt.fmt.pix.width, CAMERA_WIDTH, fmt.fmt.pix.height, CAMERA_HEIGHT,
fmt.fmt.pix.pixelformat, CAMERA_PIXELFORMAT, fmt.fmt.pix.field,
CAMERA_FIELD, fmt.fmt.pix.colorspace, fmt.fmt.pix.xfer_func);
if (fmt.fmt.pix.width != CAMERA_WIDTH ||
fmt.fmt.pix.height != CAMERA_HEIGHT ||
fmt.fmt.pix.field != CAMERA_FIELD) {
fprintf(stderr, "Video does not accept the hard-coded dimensions: %s\n",
strerror(errno));
goto fail_vfd;
}
struct v4l2_requestbuffers reqbufs;
memset(&reqbufs, 0, sizeof(reqbufs));
reqbufs.count = NUM_BUFS;
reqbufs.memory = V4L2_MEMORY_MMAP;
reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (ioctl_loop(s->fd, VIDIOC_REQBUFS, &reqbufs) < 0) {
fprintf(stderr, "Failed to request buffers: %s\n", strerror(errno));
goto fail_vfd;
}
for (int i = 0; i < NUM_BUFS; i++) {
struct v4l2_buffer buf;
memset(&buf, 0, sizeof(buf));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = i;
if (ioctl_loop(s->fd, VIDIOC_QUERYBUF, &buf) < 0) {
fprintf(stderr, "Failed to query buffers info %d/%d: %s\n", i,
NUM_BUFS, strerror(errno));
goto fail_bufs;
}
if (buf.length == 0) {
fprintf(stderr, "Zero buffer length\n");
goto fail_bufs;
}
s->bufs[i].len = buf.length;
s->bufs[i].data = mmap(NULL, buf.length, PROT_READ | PROT_WRITE,
MAP_SHARED, s->fd, buf.m.offset);
if (s->bufs[i].data == MAP_FAILED) {
fprintf(stderr, "Failed to map buffer\n");
goto fail_bufs;
}
if (ioctl_loop(s->fd, VIDIOC_QBUF, &buf) < 0) {
fprintf(stderr, "Failed to queue buffer: %s\n", strerror(errno));
goto fail_bufs;
}
}
enum v4l2_buf_type buftype;
buftype = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (ioctl_loop(s->fd, VIDIOC_STREAMON, &buftype) < 0) {
fprintf(stderr, "Failed to start stream1: %s\n", strerror(errno));
goto fail_bufs;
}
s->output_state = DisplayLight;
if (setup_analysis(&s->control) < 0) {
goto fail_bufs;
}
fprintf(stderr, "All set up\n");
return s;
fail_bufs:
for (int i = 0; i < NUM_BUFS; i++) {
if (s->bufs[i].len) {
munmap(s->bufs[i].data, s->bufs[i].len);
}
}
fail_vfd:
close(s->fd);
fail_free:
free(s);
return NULL;
}
enum WhatToDo update_backend(void *state) {
struct state *s = (struct state *)state;
struct pollfd pfd;
pfd.fd = s->fd;
pfd.events = POLLIN;
int p = poll(&pfd, 1, 1); // 1 msec max timeout
if (p < 0) {
fprintf(stderr, "Poll failed: %s\n", strerror(errno));
goto end;
} else if (p == 0) {
goto end;
}
struct v4l2_buffer buf;
memset(&buf, 0, sizeof(buf));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
if (ioctl_loop(s->fd, VIDIOC_DQBUF, &buf) < 0) {
if (errno == EAGAIN) {
goto end;
} else {
fprintf(stderr, "Dequeue failed: %s\n", strerror(errno));
goto end;
}
}
struct timespec captime;
clock_gettime(CLOCK_MONOTONIC, &captime);
int length = s->bufs[buf.index].len;
uint8_t *data = (uint8_t *)s->bufs[buf.index].data;
// TODO: does interpreting the colors & Bayer layout make sense,
// or should the fact that we just feed light/dark inputs mean that
// we can safely average pixel values and get 'good-enough' results?
uint64_t net_val = 0;
for (int i = 0; i < length; i++) {
net_val += data[i];
}
double avg_val = net_val / (double)(length) / 255.0;
if (ioctl_loop(s->fd, VIDIOC_QBUF, &buf) < 0) {
fprintf(stderr, "Requeue failed: %s\n", strerror(errno));
}
s->output_state = update_analysis(&s->control, captime, avg_val, THRESHOLD);
end:
return s->output_state;
}
void cleanup_backend(void *state) {
struct state *s = state;
enum v4l2_buf_type type;
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
ioctl_loop(s->fd, VIDIOC_STREAMOFF, &type);
for (int i = 0; i < NUM_BUFS; i++) {
if (s->bufs[i].len) {
munmap(s->bufs[i].data, s->bufs[i].len);
}
}
close(s->fd);
cleanup_analysis(&s->control);
free(s);
}