]> git.sesse.net Git - vlc/blob - modules/access/v4l2/video.c
v4l2: map buffers before queueing them
[vlc] / modules / access / v4l2 / video.c
1 /*****************************************************************************
2  * video.c : Video4Linux2 input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2002-2009 the VideoLAN team
5  * Copyright (C) 2011-2012 RĂ©mi Denis-Courmont
6  *
7  * Authors: Benjamin Pracht <bigben at videolan dot org>
8  *          Richard Hosking <richard at hovis dot net>
9  *          Antoine Cellerier <dionoea at videolan d.t org>
10  *          Dennis Lou <dlou99 at yahoo dot com>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License as published by
14  * the Free Software Foundation; either version 2.1 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this program; if not, write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <errno.h>
32 #include <sys/ioctl.h>
33 #include <sys/mman.h>
34 #include <poll.h>
35
36 #include <vlc_common.h>
37 #include <vlc_block.h>
38
39 #include "v4l2.h"
40
41 static int SetupStandard (vlc_object_t *obj, int fd,
42                           const struct v4l2_input *restrict input)
43 {
44 #ifdef V4L2_IN_CAP_STD
45     if (!(input->capabilities & V4L2_IN_CAP_STD))
46     {
47         msg_Dbg (obj, "no video standard selection");
48         return 0;
49     }
50 #else
51     (void) input;
52     msg_Dbg (obj, "video standard selection unknown");
53 #endif
54     v4l2_std_id std = var_InheritStandard (obj, CFG_PREFIX"standard");
55     if (std == V4L2_STD_UNKNOWN)
56     {
57         msg_Warn (obj, "video standard not set");
58         return 0;
59     }
60     if (v4l2_ioctl (fd, VIDIOC_S_STD, &std) < 0)
61     {
62         msg_Err (obj, "cannot set video standard 0x%"PRIx64": %m", std);
63         return -1;
64     }
65     msg_Dbg (obj, "video standard set to 0x%"PRIx64":", std);
66     return 0;
67 }
68
69 static int SetupAudio (vlc_object_t *obj, int fd,
70                        const struct v4l2_input *restrict input)
71 {
72     if (input->audioset == 0)
73     {
74         msg_Dbg (obj, "no audio input available");
75         return 0;
76     }
77     msg_Dbg (obj, "available audio inputs: 0x%08"PRIX32, input->audioset);
78
79     uint32_t idx = var_InheritInteger (obj, CFG_PREFIX"audio-input");
80     if (idx == (uint32_t)-1)
81     {
82         msg_Dbg (obj, "no audio input selected");
83         return 0;
84     }
85     if (((1 << idx) & input->audioset) == 0)
86     {
87         msg_Warn (obj, "skipped unavailable audio input %"PRIu32, idx);
88         return -1;
89     }
90
91     /* TODO: Enumerate other selectable audio inputs. How to expose them? */
92     struct v4l2_audio enumaudio = { .index = idx };
93
94     if (v4l2_ioctl (fd, VIDIOC_ENUMAUDIO, &enumaudio) < 0)
95     {
96         msg_Err (obj, "cannot get audio input %"PRIu32" properties: %m", idx);
97         return -1;
98     }
99
100     msg_Dbg (obj, "audio input %s (%"PRIu32") is %s"
101              " (capabilities: 0x%08"PRIX32")", enumaudio.name, enumaudio.index,
102              (enumaudio.capability & V4L2_AUDCAP_STEREO) ? "Stereo" : "Mono",
103              enumaudio.capability);
104     if (enumaudio.capability & V4L2_AUDCAP_AVL)
105         msg_Dbg (obj, " supports Automatic Volume Level");
106
107     /* TODO: AVL mode */
108     struct v4l2_audio audio = { .index = idx };
109
110     if (v4l2_ioctl (fd, VIDIOC_S_AUDIO, &audio) < 0)
111     {
112         msg_Err (obj, "cannot select audio input %"PRIu32": %m", idx);
113         return -1;
114     }
115     msg_Dbg (obj, "selected audio input %"PRIu32, idx);
116     return 0;
117 }
118
119 static int SetupTuner (vlc_object_t *obj, int fd,
120                        const struct v4l2_input *restrict input)
121 {
122     switch (input->type)
123     {
124         case V4L2_INPUT_TYPE_TUNER:
125             msg_Dbg (obj, "tuning required: tuner %"PRIu32, input->tuner);
126             break;
127         case V4L2_INPUT_TYPE_CAMERA:
128             msg_Dbg (obj, "no tuning required (analog baseband input)");
129             return 0;
130         default:
131             msg_Err (obj, "unknown input tuning type %"PRIu32, input->type);
132             return 0; // hopefully we can stream regardless...
133     }
134
135     struct v4l2_tuner tuner = { .index = input->tuner };
136
137     if (v4l2_ioctl (fd, VIDIOC_G_TUNER, &tuner) < 0)
138     {
139         msg_Err (obj, "cannot get tuner %"PRIu32" properties: %m",
140                  input->tuner);
141         return -1;
142     }
143
144     /* NOTE: This is overkill. Only video devices currently work, so the
145      * type is always analog TV. */
146     const char *typename, *mult;
147     switch (tuner.type)
148     {
149         case V4L2_TUNER_RADIO:
150             typename = "Radio";
151             break;
152         case V4L2_TUNER_ANALOG_TV:
153             typename = "Analog TV";
154             break;
155         default:
156             typename = "unknown";
157     }
158     mult = (tuner.capability & V4L2_TUNER_CAP_LOW) ? "" : "k";
159
160     msg_Dbg (obj, "tuner %s (%"PRIu32") is %s", tuner.name, tuner.index,
161              typename);
162     msg_Dbg (obj, " ranges from %u.%u %sHz to %u.%u %sHz",
163              (tuner.rangelow * 125) >> 1, (tuner.rangelow & 1) * 5, mult,
164              (tuner.rangehigh * 125) >> 1, (tuner.rangehigh & 1) * 5,
165              mult);
166
167     /* TODO: only set video standard if the tuner requires it */
168
169     /* Configure the audio mode */
170     /* TODO: Ideally, L1 would be selected for stereo tuners, and L1_L2
171      * for mono tuners. When dual-mono is detected after tuning on a stereo
172      * tuner, we would fallback to L1_L2 too. Then we would flag dual-mono
173      * for the audio E/S. Unfortunately, we have no access to the audio E/S
174      * here (it belongs in the slave audio input...). */
175     tuner.audmode = var_InheritInteger (obj, CFG_PREFIX"tuner-audio-mode");
176     memset (tuner.reserved, 0, sizeof (tuner.reserved));
177
178     if (tuner.capability & V4L2_TUNER_CAP_LANG1)
179         msg_Dbg (obj, " supports primary audio language");
180     else if (tuner.audmode == V4L2_TUNER_MODE_LANG1)
181     {
182         msg_Warn (obj, " falling back to stereo mode");
183         tuner.audmode = V4L2_TUNER_MODE_STEREO;
184     }
185     if (tuner.capability & V4L2_TUNER_CAP_LANG2)
186         msg_Dbg (obj, " supports secondary audio language or program");
187     if (tuner.capability & V4L2_TUNER_CAP_STEREO)
188         msg_Dbg (obj, " supports stereo audio");
189     else if (tuner.audmode == V4L2_TUNER_MODE_STEREO)
190     {
191         msg_Warn (obj, " falling back to mono mode");
192         tuner.audmode = V4L2_TUNER_MODE_MONO;
193     }
194
195     if (v4l2_ioctl (fd, VIDIOC_S_TUNER, &tuner) < 0)
196     {
197         msg_Err (obj, "cannot set tuner %"PRIu32" audio mode: %m",
198                  input->tuner);
199         return -1;
200     }
201     msg_Dbg (obj, "tuner %"PRIu32" audio mode %u set", input->tuner,
202              tuner.audmode);
203
204     /* Tune to the requested frequency */
205     uint32_t freq = var_InheritInteger (obj, CFG_PREFIX"tuner-frequency");
206     if (freq != (uint32_t)-1)
207     {
208         struct v4l2_frequency frequency = {
209             .tuner = input->tuner,
210             .type = V4L2_TUNER_ANALOG_TV,
211             .frequency = freq * 125 / 2
212         };
213
214         if (v4l2_ioctl (fd, VIDIOC_S_FREQUENCY, &frequency) < 0)
215         {
216             msg_Err (obj, "cannot tuner tuner %u to frequency %u %sHz: %m",
217                      input->tuner, freq, mult);
218             return -1;
219         }
220         msg_Dbg (obj, "tuner %"PRIu32" tuned to frequency %"PRIu32" %sHz",
221                  input->tuner, freq, mult);
222     }
223     else
224         msg_Dbg (obj, "tuner not tuned");
225     return 0;
226 }
227
228 static int ResetCrop (vlc_object_t *obj, int fd)
229 {
230     struct v4l2_cropcap cropcap = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
231
232     /* In theory, this ioctl() must work for all video capture devices.
233      * In practice, it does not. */
234     if (v4l2_ioctl (fd, VIDIOC_CROPCAP, &cropcap) < 0)
235     {
236         msg_Warn (obj, "cannot get cropping properties: %m");
237         return -1;
238     }
239
240     /* Reset to the default cropping rectangle */
241     struct v4l2_crop crop = {
242         .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
243         .c = cropcap.defrect,
244     };
245
246     if (v4l2_ioctl (fd, VIDIOC_S_CROP, &crop) < 0)
247     {
248         msg_Warn (obj, "cannot reset cropping limits: %m");
249         return -1;
250     }
251     return 0;
252 }
253
254 int SetupInput (vlc_object_t *obj, int fd)
255 {
256     struct v4l2_input input;
257
258     input.index = var_InheritInteger (obj, CFG_PREFIX"input");
259     if (v4l2_ioctl (fd, VIDIOC_ENUMINPUT, &input) < 0)
260     {
261         msg_Err (obj, "invalid video input %"PRIu32": %m", input.index);
262         return -1;
263     }
264
265     const char *typename = "unknown";
266     switch (input.type)
267     {
268         case V4L2_INPUT_TYPE_TUNER:
269             typename = "tuner";
270             break;
271         case V4L2_INPUT_TYPE_CAMERA:
272             typename = "camera";
273             break;
274     }
275
276     msg_Dbg (obj, "video input %s (%"PRIu32") is %s", input.name,
277              input.index, typename);
278
279     /* Select input */
280     if (v4l2_ioctl (fd, VIDIOC_S_INPUT, &input.index) < 0)
281     {
282         msg_Err (obj, "cannot select input %"PRIu32": %m", input.index);
283         return -1;
284     }
285     msg_Dbg (obj, "selected input %"PRIu32, input.index);
286
287     SetupStandard (obj, fd, &input);
288     SetupTuner (obj, fd, &input);
289     SetupAudio (obj, fd, &input);
290     return 0;
291 }
292
293 /** Compares two V4L2 fractions. */
294 static int64_t fcmp (const struct v4l2_fract *a,
295                      const struct v4l2_fract *b)
296 {
297     return (uint64_t)a->numerator * b->denominator
298          - (uint64_t)b->numerator * a->denominator;
299 }
300
301 static const struct v4l2_fract infinity = { 1, 0 };
302
303 /**
304  * Finds the highest frame rate possible of a certain V4L2 format.
305  * @param fmt V4L2 capture format [IN]
306  * @param it V4L2 frame interval [OUT]
307  * @return 0 on success, -1 on error.
308  */
309 static int FindMaxRate (vlc_object_t *obj, int fd,
310                         const struct v4l2_format *restrict fmt,
311                         struct v4l2_fract *restrict it)
312 {
313     struct v4l2_frmivalenum fie = {
314         .pixel_format = fmt->fmt.pix.pixelformat,
315         .width = fmt->fmt.pix.width,
316         .height = fmt->fmt.pix.height,
317     };
318     /* Mind that maximum rate means minimum interval */
319
320     if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &fie) < 0)
321     {
322         msg_Dbg (obj, "  unknown frame intervals: %m");
323         /* Frame intervals cannot be enumerated. Set the format and then
324          * get the streaming parameters to figure out the default frame
325          * interval. This is not necessarily the maximum though. */
326         struct v4l2_format dummy_fmt = *fmt;
327         struct v4l2_streamparm parm = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
328
329         if (v4l2_ioctl (fd, VIDIOC_S_FMT, &dummy_fmt) < 0
330          || v4l2_ioctl (fd, VIDIOC_G_PARM, &parm) < 0)
331         {
332             *it = infinity;
333             return -1;
334         }
335
336         *it = parm.parm.capture.timeperframe;
337         msg_Dbg (obj, "  %s frame interval: %"PRIu32"/%"PRIu32,
338                  (parm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME)
339                  ? "default" : "constant", it->numerator, it->denominator);
340     }
341     else
342     switch (fie.type)
343     {
344         case V4L2_FRMIVAL_TYPE_DISCRETE:
345             *it = infinity;
346             do
347             {
348                 if (fcmp (&fie.discrete, it) < 0)
349                     *it = fie.discrete;
350                 fie.index++;
351             }
352             while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &fie) >= 0);
353
354             msg_Dbg (obj, "  %s frame interval: %"PRIu32"/%"PRIu32,
355                      "discrete", it->numerator, it->denominator);
356             break;
357
358         case V4L2_FRMIVAL_TYPE_STEPWISE:
359         case V4L2_FRMIVAL_TYPE_CONTINUOUS:
360             msg_Dbg (obj, "  frame intervals from %"PRIu32"/%"PRIu32
361                      "to %"PRIu32"/%"PRIu32" supported",
362                      fie.stepwise.min.numerator, fie.stepwise.min.denominator,
363                      fie.stepwise.max.numerator, fie.stepwise.max.denominator);
364             if (fie.type == V4L2_FRMIVAL_TYPE_STEPWISE)
365                 msg_Dbg (obj, "  with %"PRIu32"/%"PRIu32" step",
366                          fie.stepwise.step.numerator,
367                          fie.stepwise.step.denominator);
368             *it = fie.stepwise.min;
369             break;
370     }
371     return 0;
372 }
373
374 #undef SetupFormat
375 /**
376  * Finds the best possible frame rate and resolution.
377  * @param fourcc pixel format
378  * @param fmt V4L2 capture format [OUT]
379  * @param parm V4L2 capture streaming parameters [OUT]
380  * @return 0 on success, -1 on failure.
381  */
382 int SetupFormat (vlc_object_t *obj, int fd, uint32_t fourcc,
383                  struct v4l2_format *restrict fmt,
384                  struct v4l2_streamparm *restrict parm)
385 {
386     memset (fmt, 0, sizeof (*fmt));
387     fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
388     memset (parm, 0, sizeof (*parm));
389     parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
390
391     if (v4l2_ioctl (fd, VIDIOC_G_FMT, fmt) < 0)
392     {
393         msg_Err (obj, "cannot get default format: %m");
394         return -1;
395     }
396     fmt->fmt.pix.pixelformat = fourcc;
397
398     struct v4l2_frmsizeenum fse = {
399         .pixel_format = fourcc,
400     };
401     struct v4l2_fract best_it = infinity;
402     uint64_t best_area = 0;
403
404     uint32_t width = var_InheritInteger (obj, CFG_PREFIX"width");
405     uint32_t height = var_InheritInteger (obj, CFG_PREFIX"height");
406     if (width > 0 && height > 0)
407     {
408         fmt->fmt.pix.width = width;
409         fmt->fmt.pix.height = height;
410         msg_Dbg (obj, " requested frame size: %"PRIu32"x%"PRIu32,
411                  width, height);
412         FindMaxRate (obj, fd, fmt, &best_it);
413     }
414     else
415     if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &fse) < 0)
416     {
417         /* Fallback to current format, try to maximize frame rate */
418         msg_Dbg (obj, " unknown frame sizes: %m");
419         msg_Dbg (obj, " current frame size: %"PRIu32"x%"PRIu32,
420                  fmt->fmt.pix.width, fmt->fmt.pix.height);
421         FindMaxRate (obj, fd, fmt, &best_it);
422     }
423     else
424     switch (fse.type)
425     {
426         case V4L2_FRMSIZE_TYPE_DISCRETE:
427             do
428             {
429                 struct v4l2_fract cur_it;
430
431                 msg_Dbg (obj, " frame size %"PRIu32"x%"PRIu32,
432                          fse.discrete.width, fse.discrete.height);
433                 FindMaxRate (obj, fd, fmt, &cur_it);
434
435                 int64_t c = fcmp (&cur_it, &best_it);
436                 uint64_t area = fse.discrete.width * fse.discrete.height;
437                 if (c < 0 || (c == 0 && area > best_area))
438                 {
439                     best_it = cur_it;
440                     best_area = area;
441                     fmt->fmt.pix.width = fse.discrete.width;
442                     fmt->fmt.pix.height = fse.discrete.height;
443                 }
444
445                 fse.index++;
446             }
447             while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &fse) >= 0);
448
449             msg_Dbg (obj, " best discrete frame size: %"PRIu32"x%"PRIu32,
450                      fmt->fmt.pix.width, fmt->fmt.pix.height);
451             break;
452
453         case V4L2_FRMSIZE_TYPE_STEPWISE:
454         case V4L2_FRMSIZE_TYPE_CONTINUOUS:
455             msg_Dbg (obj, " frame sizes from %"PRIu32"x%"PRIu32" to "
456                      "%"PRIu32"x%"PRIu32" supported",
457                      fse.stepwise.min_width, fse.stepwise.min_height,
458                      fse.stepwise.max_width, fse.stepwise.max_height);
459             if (fse.type == V4L2_FRMSIZE_TYPE_STEPWISE)
460                 msg_Dbg (obj, "  with %"PRIu32"x%"PRIu32" steps",
461                          fse.stepwise.step_width, fse.stepwise.step_height);
462
463             /* FIXME: slow and dumb */
464             for (uint32_t width =  fse.stepwise.min_width;
465                           width <= fse.stepwise.max_width;
466                           width += fse.stepwise.step_width)
467                 for (uint32_t height =  fse.stepwise.min_height;
468                               height <= fse.stepwise.max_width;
469                               height += fse.stepwise.step_height)
470                 {
471                     struct v4l2_fract cur_it;
472
473                     FindMaxRate (obj, fd, fmt, &cur_it);
474
475                     int64_t c = fcmp (&cur_it, &best_it);
476                     uint64_t area = width * height;
477
478                     if (c < 0 || (c == 0 && area > best_area))
479                     {
480                         best_it = cur_it;
481                         best_area = area;
482                         fmt->fmt.pix.width = width;
483                         fmt->fmt.pix.height = height;
484                     }
485                 }
486
487             msg_Dbg (obj, " best frame size: %"PRIu32"x%"PRIu32,
488                      fmt->fmt.pix.width, fmt->fmt.pix.height);
489             break;
490     }
491
492     /* Set the final format */
493     if (v4l2_ioctl (fd, VIDIOC_S_FMT, fmt) < 0)
494     {
495         msg_Err (obj, "cannot set format: %m");
496         return -1;
497     }
498
499     /* Now that the final format is set, fetch and override parameters */
500     if (v4l2_ioctl (fd, VIDIOC_G_PARM, parm) < 0)
501     {
502         msg_Err (obj, "cannot get streaming parameters: %m");
503         return -1;
504     }
505     parm->parm.capture.capturemode = 0; /* normal video mode */
506     parm->parm.capture.extendedmode = 0;
507     if (best_it.denominator != 0)
508         parm->parm.capture.timeperframe = best_it;
509     if (v4l2_ioctl (fd, VIDIOC_S_PARM, parm) < 0)
510         msg_Warn (obj, "cannot set streaming parameters: %m");
511
512     ResetCrop (obj, fd); /* crop depends on frame size */
513
514     return 0;
515 }
516
517
518 /*****************************************************************************
519  * GrabVideo: Grab a video frame
520  *****************************************************************************/
521 block_t *GrabVideo (vlc_object_t *demux, int fd,
522                     const struct buffer_t *restrict bufv)
523 {
524     struct v4l2_buffer buf = {
525         .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
526         .memory = V4L2_MEMORY_MMAP,
527     };
528
529     /* Wait for next frame */
530     if (v4l2_ioctl (fd, VIDIOC_DQBUF, &buf) < 0)
531     {
532         switch (errno)
533         {
534             case EAGAIN:
535                 return NULL;
536             case EIO:
537                 /* Could ignore EIO, see spec. */
538                 /* fall through */
539             default:
540                 msg_Err (demux, "dequeue error: %m");
541                 return NULL;
542         }
543     }
544
545     /* Copy frame */
546     block_t *block = block_Alloc (buf.bytesused);
547     if (unlikely(block == NULL))
548         return NULL;
549     memcpy (block->p_buffer, bufv[buf.index].start, buf.bytesused);
550
551     /* Unlock */
552     if (v4l2_ioctl (fd, VIDIOC_QBUF, &buf) < 0)
553     {
554         msg_Err (demux, "queue error: %m");
555         block_Release (block);
556         return NULL;
557     }
558     return block;
559 }
560
561 /**
562  * Allocates memory-mapped buffers, queues them and start streaming.
563  * @param n requested buffers count [IN], allocated buffers count [OUT]
564  * @return array of allocated buffers (use free()), or NULL on error.
565  */
566 struct buffer_t *StartMmap (vlc_object_t *obj, int fd, uint32_t *restrict n)
567 {
568     struct v4l2_requestbuffers req = {
569         .count = *n,
570         .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
571         .memory = V4L2_MEMORY_MMAP,
572     };
573
574     if (v4l2_ioctl (fd, VIDIOC_REQBUFS, &req) < 0)
575     {
576         msg_Err (obj, "cannot allocate buffers: %m" );
577         return NULL;
578     }
579
580     if (req.count < 2)
581     {
582         msg_Err (obj, "cannot allocate enough buffers");
583         return NULL;
584     }
585
586     struct buffer_t *bufv = malloc (req.count * sizeof (*bufv));
587     if (unlikely(bufv == NULL))
588         return NULL;
589
590     uint32_t bufc = 0;
591     while (bufc < req.count)
592     {
593         struct v4l2_buffer buf = {
594             .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
595             .memory = V4L2_MEMORY_MMAP,
596             .index = bufc,
597         };
598
599         if (v4l2_ioctl (fd, VIDIOC_QUERYBUF, &buf) < 0)
600         {
601             msg_Err (obj, "cannot query buffer %"PRIu32": %m", bufc);
602             goto error;
603         }
604
605         bufv[bufc].start = v4l2_mmap (NULL, buf.length, PROT_READ | PROT_WRITE,
606                                       MAP_SHARED, fd, buf.m.offset);
607         if (bufv[bufc].start == MAP_FAILED)
608         {
609             msg_Err (obj, "cannot map buffer %"PRIu32": %m", bufc);
610             goto error;
611         }
612         bufv[bufc].length = buf.length;
613         bufc++;
614
615         /* Some drivers refuse to queue buffers before they are mapped. Bug? */
616         if (v4l2_ioctl (fd, VIDIOC_QBUF, &buf) < 0)
617         {
618             msg_Err (obj, "cannot queue buffer %"PRIu32": %m", bufc);
619             goto error;
620         }
621     }
622
623     enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
624     if (v4l2_ioctl (fd, VIDIOC_STREAMON, &type) < 0)
625     {
626         msg_Err (obj, "cannot start streaming: %m");
627         goto error;
628     }
629     *n = bufc;
630     return bufv;
631 error:
632     StopMmap (fd, bufv, bufc);
633     return NULL;
634 }
635
636 void StopMmap (int fd, struct buffer_t *bufv, uint32_t bufc)
637 {
638     enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
639
640     /* STREAMOFF implicitly dequeues all buffers */
641     v4l2_ioctl (fd, VIDIOC_STREAMOFF, &type);
642     for (uint32_t i = bufc; i < bufc; i++)
643         v4l2_munmap (bufv[i].start, bufv[i].length);
644     free (bufv);
645 }