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
|
// python-v4l2capture
// Python extension to capture video with video4linux2
//
// 2009, 2010, 2011 Fredrik Portstrom
//
// I, the copyright holder of this file, hereby release it into the
// public domain. This applies worldwide. In case this is not legally
// possible: I grant anyone the right to use this work for any
// purpose, without any conditions, unless such conditions are
// required by law.
#include <Python.h>
#include <fcntl.h>
#include <linux/videodev2.h>
#include <sys/mman.h>
// LIBV4L is not needed for MJPEG capture.
#undef USE_LIBV4L
#ifdef USE_LIBV4L
#include <libv4l2.h>
#else
#include <sys/ioctl.h>
#define v4l2_close close
#define v4l2_ioctl ioctl
#define v4l2_mmap mmap
#define v4l2_munmap munmap
#define v4l2_open open
#endif
#define ASSERT_OPEN if(self->fd < 0) \
{ \
PyErr_SetString(PyExc_ValueError, \
"I/O operation on closed file"); \
return NULL; \
}
struct buffer {
void *start;
size_t length;
};
typedef struct {
PyObject_HEAD
int fd;
struct buffer *buffers;
int buffer_count;
} Video_device;
struct capability {
int id;
const char *name;
};
static struct capability capabilities[] = {
{ V4L2_CAP_ASYNCIO, "asyncio" },
{ V4L2_CAP_AUDIO, "audio" },
{ V4L2_CAP_HW_FREQ_SEEK, "hw_freq_seek" },
{ V4L2_CAP_RADIO, "radio" },
{ V4L2_CAP_RDS_CAPTURE, "rds_capture" },
{ V4L2_CAP_READWRITE, "readwrite" },
{ V4L2_CAP_SLICED_VBI_CAPTURE, "sliced_vbi_capture" },
{ V4L2_CAP_SLICED_VBI_OUTPUT, "sliced_vbi_output" },
{ V4L2_CAP_STREAMING, "streaming" },
{ V4L2_CAP_TUNER, "tuner" },
{ V4L2_CAP_VBI_CAPTURE, "vbi_capture" },
{ V4L2_CAP_VBI_OUTPUT, "vbi_output" },
{ V4L2_CAP_VIDEO_CAPTURE, "video_capture" },
{ V4L2_CAP_VIDEO_OUTPUT, "video_output" },
{ V4L2_CAP_VIDEO_OUTPUT_OVERLAY, "video_output_overlay" },
{ V4L2_CAP_VIDEO_OVERLAY, "video_overlay" }
};
static int my_ioctl(int fd, int request, void *arg)
{
// Retry ioctl until it returns without being interrupted.
for(;;)
{
int result = v4l2_ioctl(fd, request, arg);
if(!result)
{
return 0;
}
if(errno != EINTR)
{
PyErr_SetFromErrno(PyExc_IOError);
return 1;
}
}
}
static void Video_device_unmap(Video_device *self)
{
int i;
for(i = 0; i < self->buffer_count; i++)
{
v4l2_munmap(self->buffers[i].start, self->buffers[i].length);
}
free(self->buffers);
self->buffers = NULL;
}
static void Video_device_dealloc(Video_device *self)
{
if(self->fd >= 0)
{
if(self->buffers)
{
Video_device_unmap(self);
}
v4l2_close(self->fd);
}
self->ob_type->tp_free((PyObject *)self);
}
static int Video_device_init(Video_device *self, PyObject *args,
PyObject *kwargs)
{
const char *device_path;
if(!PyArg_ParseTuple(args, "s", &device_path))
{
return -1;
}
int fd = v4l2_open(device_path, O_RDWR | O_NONBLOCK);
if(fd < 0)
{
PyErr_SetFromErrnoWithFilename(PyExc_IOError, (char *)device_path);
return -1;
}
self->fd = fd;
self->buffers = NULL;
self->buffer_count = 0;
return 0;
}
static PyObject *Video_device_close(Video_device *self)
{
if(self->fd >= 0)
{
if(self->buffers)
{
Video_device_unmap(self);
}
v4l2_close(self->fd);
self->fd = -1;
}
Py_RETURN_NONE;
}
static PyObject *Video_device_fileno(Video_device *self)
{
ASSERT_OPEN;
return PyInt_FromLong(self->fd);
}
static PyObject *Video_device_get_info(Video_device *self)
{
ASSERT_OPEN;
struct v4l2_capability caps;
if(my_ioctl(self->fd, VIDIOC_QUERYCAP, &caps))
{
return NULL;
}
PyObject *set = PySet_New(NULL);
if(!set)
{
return NULL;
}
struct capability *capability = capabilities;
while((void *)capability < (void *)capabilities + sizeof(capabilities))
{
if(caps.capabilities & capability->id)
{
PyObject *s = PyString_FromString(capability->name);
if(!s)
{
Py_DECREF(set);
return NULL;
}
PySet_Add(set, s);
}
capability++;
}
return Py_BuildValue("sssO", caps.driver, caps.card, caps.bus_info, set);
}
static PyObject *Video_device_set_format(Video_device *self, PyObject *args)
{
int size_x;
int size_y;
if(!PyArg_ParseTuple(args, "ii", &size_x, &size_y))
{
return NULL;
}
struct v4l2_format format;
format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
format.fmt.pix.width = size_x;
format.fmt.pix.height = size_y;
format.fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
format.fmt.pix.field = V4L2_FIELD_NONE;
format.fmt.pix.bytesperline = 0;
if(my_ioctl(self->fd, VIDIOC_S_FMT, &format))
{
return NULL;
}
return Py_BuildValue("ii", format.fmt.pix.width, format.fmt.pix.height);
}
static PyObject *Video_device_set_fps(Video_device *self, PyObject *args)
{
int fps;
if(!PyArg_ParseTuple(args, "i", &fps))
{
return NULL;
}
struct v4l2_streamparm setfps;
memset(&setfps, 0, sizeof(struct v4l2_streamparm));
setfps.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
setfps.parm.capture.timeperframe.numerator = 1;
setfps.parm.capture.timeperframe.denominator = fps;
if(my_ioctl(self->fd, VIDIOC_S_PARM, &setfps)){
return NULL;
}
return Py_BuildValue("i",setfps.parm.capture.timeperframe.denominator);
}
static PyObject *Video_device_start(Video_device *self)
{
ASSERT_OPEN;
enum v4l2_buf_type type;
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if(my_ioctl(self->fd, VIDIOC_STREAMON, &type))
{
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *Video_device_stop(Video_device *self)
{
ASSERT_OPEN;
enum v4l2_buf_type type;
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if(my_ioctl(self->fd, VIDIOC_STREAMOFF, &type))
{
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *Video_device_create_buffers(Video_device *self, PyObject *args)
{
int buffer_count;
if(!PyArg_ParseTuple(args, "I", &buffer_count))
{
return NULL;
}
ASSERT_OPEN;
if(self->buffers)
{
PyErr_SetString(PyExc_ValueError, "Buffers are already created");
return NULL;
}
struct v4l2_requestbuffers reqbuf;
reqbuf.count = buffer_count;
reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
reqbuf.memory = V4L2_MEMORY_MMAP;
if(my_ioctl(self->fd, VIDIOC_REQBUFS, &reqbuf))
{
return NULL;
}
if(!reqbuf.count)
{
PyErr_SetString(PyExc_IOError, "Not enough buffer memory");
return NULL;
}
self->buffers = malloc(reqbuf.count * sizeof(struct buffer));
if(!self->buffers)
{
PyErr_NoMemory();
return NULL;
}
int i;
for(i = 0; i < reqbuf.count; i++)
{
struct v4l2_buffer buffer;
buffer.index = i;
buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buffer.memory = V4L2_MEMORY_MMAP;
if(my_ioctl(self->fd, VIDIOC_QUERYBUF, &buffer))
{
return NULL;
}
self->buffers[i].length = buffer.length;
self->buffers[i].start = v4l2_mmap(NULL, buffer.length,
PROT_READ | PROT_WRITE, MAP_SHARED, self->fd, buffer.m.offset);
if(self->buffers[i].start == MAP_FAILED)
{
PyErr_SetFromErrno(PyExc_IOError);
Video_device_unmap(self);
return NULL;
}
++self->buffer_count;
}
Py_RETURN_NONE;
}
static PyObject *Video_device_queue_all_buffers(Video_device *self)
{
if(!self->buffers)
{
ASSERT_OPEN;
PyErr_SetString(PyExc_ValueError, "Buffers have not been created");
return NULL;
}
int i;
int buffer_count = self->buffer_count;
for(i = 0; i < buffer_count; i++)
{
struct v4l2_buffer buffer;
buffer.index = i;
buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buffer.memory = V4L2_MEMORY_MMAP;
if(my_ioctl(self->fd, VIDIOC_QBUF, &buffer))
{
return NULL;
}
}
Py_RETURN_NONE;
}
static PyObject *Video_device_read_internal(Video_device *self, int queue)
{
if(!self->buffers)
{
ASSERT_OPEN;
PyErr_SetString(PyExc_ValueError, "Buffers have not been created");
return NULL;
}
struct v4l2_buffer buffer;
buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buffer.memory = V4L2_MEMORY_MMAP;
if(my_ioctl(self->fd, VIDIOC_DQBUF, &buffer))
{
return NULL;
}
PyObject *result = PyString_FromStringAndSize(
self->buffers[buffer.index].start, buffer.bytesused);
if(!result)
{
return NULL;
}
if(queue && my_ioctl(self->fd, VIDIOC_QBUF, &buffer))
{
return NULL;
}
return result;
}
static PyObject *Video_device_read(Video_device *self)
{
return Video_device_read_internal(self, 0);
}
static PyObject *Video_device_read_and_queue(Video_device *self)
{
return Video_device_read_internal(self, 1);
}
static PyMethodDef Video_device_methods[] = {
{"close", (PyCFunction)Video_device_close, METH_NOARGS,
"close()\n\n"
"Close video device. Subsequent calls to other methods will fail."},
{"fileno", (PyCFunction)Video_device_fileno, METH_NOARGS,
"fileno() -> integer \"file descriptor\".\n\n"
"This enables video devices to be passed select.select for waiting "
"until a frame is available for reading."},
{"get_info", (PyCFunction)Video_device_get_info, METH_NOARGS,
"get_info() -> driver, card, bus_info, capabilities\n\n"
"Returns three strings with information about the video device, and one "
"set containing strings identifying the capabilities of the video "
"device."},
{"set_format", (PyCFunction)Video_device_set_format, METH_VARARGS,
"set_format(size_x, size_y) -> size_x, size_y\n\n"
"Request the video device to set image size and format. The device may "
"choose another size than requested and will return its choice. The "
"image format will be MJPEG."},
{"set_fps", (PyCFunction)Video_device_set_fps, METH_VARARGS,
"set_fps(fps) -> fps \n\n"
"Request the video device to set frame per seconds.The device may "
"choose another frame rate than requested and will return its choice. " },
{"start", (PyCFunction)Video_device_start, METH_NOARGS,
"start()\n\n"
"Start video capture."},
{"stop", (PyCFunction)Video_device_stop, METH_NOARGS,
"stop()\n\n"
"Stop video capture."},
{"create_buffers", (PyCFunction)Video_device_create_buffers, METH_VARARGS,
"create_buffers(count)\n\n"
"Create buffers used for capturing image data. Can only be called once "
"for each video device object."},
{"queue_all_buffers", (PyCFunction)Video_device_queue_all_buffers,
METH_NOARGS,
"queue_all_buffers()\n\n"
"Let the video device fill all buffers created."},
{"read", (PyCFunction)Video_device_read, METH_NOARGS,
"read() -> string\n\n"
"Reads image data from a buffer that has been filled by the video "
"device. The image data is in MJPEG format. "
"The buffer is removed from the queue. Fails if no buffer "
"is filled. Use select.select to check for filled buffers."},
{"read_and_queue", (PyCFunction)Video_device_read_and_queue, METH_NOARGS,
"read_and_queue()\n\n"
"Same as 'read', but adds the buffer back to the queue so the video "
"device can fill it again."},
{NULL}
};
static PyTypeObject Video_device_type = {
PyObject_HEAD_INIT(NULL)
0, "v4l2capture.Video_device", sizeof(Video_device), 0,
(destructor)Video_device_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, Py_TPFLAGS_DEFAULT, "Video_device(path)\n\nOpens the video device at "
"the given path and returns an object that can capture images. The "
"constructor and all methods except close may raise IOError.", 0, 0, 0,
0, 0, 0, Video_device_methods, 0, 0, 0, 0, 0, 0, 0,
(initproc)Video_device_init
};
static PyMethodDef module_methods[] = {
{NULL}
};
PyMODINIT_FUNC initv4l2capture(void)
{
Video_device_type.tp_new = PyType_GenericNew;
if(PyType_Ready(&Video_device_type) < 0)
{
return;
}
PyObject *module = Py_InitModule3("v4l2capture", module_methods,
"Capture video with video4linux2.");
if(!module)
{
return;
}
Py_INCREF(&Video_device_type);
PyModule_AddObject(module, "Video_device", (PyObject *)&Video_device_type);
}
|