v4l2allocator: Add debug assert to detect calls in the wrong state
[platform/upstream/gst-plugins-good.git] / sys / v4l2 / gstv4l2allocator.c
1 /*
2  * Copyright (C) 2014 Collabora Ltd.
3  *     Author: Nicolas Dufresne <nicolas.dufresne@collabora.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  *
20  */
21
22 #include "config.h"
23
24 #include "ext/videodev2.h"
25 #include "gstv4l2allocator.h"
26 #include "v4l2_calls.h"
27
28 #include <gst/allocators/gstdmabuf.h>
29
30 #include <fcntl.h>
31 #include <string.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <sys/mman.h>
35
36 #define GST_V4L2_MEMORY_TYPE "V4l2Memory"
37
38 #define gst_v4l2_allocator_parent_class parent_class
39 G_DEFINE_TYPE (GstV4l2Allocator, gst_v4l2_allocator, GST_TYPE_ALLOCATOR);
40
41 GST_DEBUG_CATEGORY_STATIC (v4l2allocator_debug);
42 #define GST_CAT_DEFAULT v4l2allocator_debug
43
44 #define UNSET_QUEUED(buffer) \
45     ((buffer).flags &= ~(V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))
46
47 #define SET_QUEUED(buffer) ((buffer).flags |= V4L2_BUF_FLAG_QUEUED)
48
49 #define IS_QUEUED(buffer) \
50     ((buffer).flags & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))
51
52 enum
53 {
54   GROUP_RELEASED,
55   LAST_SIGNAL
56 };
57
58 static guint gst_v4l2_allocator_signals[LAST_SIGNAL] = { 0 };
59
60 static void gst_v4l2_allocator_release (GstV4l2Allocator * allocator,
61     GstV4l2Memory * mem);
62
63 static const gchar *
64 memory_type_to_str (guint32 memory)
65 {
66   switch (memory) {
67     case V4L2_MEMORY_MMAP:
68       return "mmap";
69     case V4L2_MEMORY_USERPTR:
70       return "userptr";
71     case V4L2_MEMORY_DMABUF:
72       return "dmabuf";
73     default:
74       return "unknown";
75   }
76 }
77
78 /*************************************/
79 /* GstV4lMemory implementation */
80 /*************************************/
81
82 static gpointer
83 _v4l2mem_map (GstV4l2Memory * mem, gsize maxsize, GstMapFlags flags)
84 {
85   gpointer data = NULL;
86
87   switch (mem->group->buffer.memory) {
88     case V4L2_MEMORY_MMAP:
89     case V4L2_MEMORY_USERPTR:
90       data = mem->data;
91       break;
92     case V4L2_MEMORY_DMABUF:
93       /* v4l2 dmabuf memory are not shared with downstream */
94       g_assert_not_reached ();
95       break;
96     default:
97       GST_WARNING ("Unknown memory type %i", mem->group->buffer.memory);
98       break;
99   }
100   return data;
101 }
102
103 static gboolean
104 _v4l2mem_unmap (GstV4l2Memory * mem)
105 {
106   gboolean ret = FALSE;
107
108   switch (mem->group->buffer.memory) {
109     case V4L2_MEMORY_MMAP:
110     case V4L2_MEMORY_USERPTR:
111       ret = TRUE;
112       break;
113     case V4L2_MEMORY_DMABUF:
114       /* v4l2 dmabuf memory are not share with downstream */
115       g_assert_not_reached ();
116       break;
117     default:
118       GST_WARNING ("Unknown memory type %i", mem->group->buffer.memory);
119       break;
120   }
121   return ret;
122 }
123
124 static gboolean
125 _v4l2mem_dispose (GstV4l2Memory * mem)
126 {
127   GstV4l2Allocator *allocator = (GstV4l2Allocator *) mem->mem.allocator;
128   GstV4l2MemoryGroup *group = mem->group;
129   gboolean ret;
130
131   if (group->mem[mem->plane]) {
132     /* We may have a dmabuf, replace it with returned original memory */
133     group->mem[mem->plane] = gst_memory_ref ((GstMemory *) mem);
134     gst_v4l2_allocator_release (allocator, mem);
135     ret = FALSE;
136   } else {
137     gst_object_ref (allocator);
138     ret = TRUE;
139   }
140
141   return ret;
142 }
143
144 static void
145 _v4l2mem_free (GstV4l2Memory * mem)
146 {
147   if (mem->dmafd >= 0)
148     close (mem->dmafd);
149   g_slice_free (GstV4l2Memory, mem);
150 }
151
152 static inline GstV4l2Memory *
153 _v4l2mem_new (GstMemoryFlags flags, GstAllocator * allocator,
154     GstMemory * parent, gsize maxsize, gsize align, gsize offset, gsize size,
155     gint plane, gpointer data, int dmafd, GstV4l2MemoryGroup * group)
156 {
157   GstV4l2Memory *mem;
158
159   mem = g_slice_new0 (GstV4l2Memory);
160   gst_memory_init (GST_MEMORY_CAST (mem),
161       flags, allocator, parent, maxsize, align, offset, size);
162
163   if (parent == NULL)
164     mem->mem.mini_object.dispose =
165         (GstMiniObjectDisposeFunction) _v4l2mem_dispose;
166
167   mem->plane = plane;
168   mem->data = data;
169   mem->dmafd = dmafd;
170   mem->group = group;
171
172   return mem;
173 }
174
175 static GstV4l2Memory *
176 _v4l2mem_share (GstV4l2Memory * mem, gssize offset, gsize size)
177 {
178   GstV4l2Memory *sub;
179   GstMemory *parent;
180
181   /* find the real parent */
182   if ((parent = mem->mem.parent) == NULL)
183     parent = (GstMemory *) mem;
184
185   if (size == -1)
186     size = mem->mem.size - offset;
187
188   /* the shared memory is always readonly */
189   sub = _v4l2mem_new (GST_MINI_OBJECT_FLAGS (parent) |
190       GST_MINI_OBJECT_FLAG_LOCK_READONLY, mem->mem.allocator, parent,
191       mem->mem.maxsize, mem->mem.align, offset, size, mem->plane, mem->data,
192       -1, mem->group);
193
194   return sub;
195 }
196
197 static gboolean
198 _v4l2mem_is_span (GstV4l2Memory * mem1, GstV4l2Memory * mem2, gsize * offset)
199 {
200   if (offset)
201     *offset = mem1->mem.offset - mem1->mem.parent->offset;
202
203   /* and memory is contiguous */
204   return mem1->mem.offset + mem1->mem.size == mem2->mem.offset;
205 }
206
207 static void
208 _v4l2mem_parent_to_dmabuf (GstV4l2Memory * mem, GstMemory * dma_mem)
209 {
210   gst_memory_lock (&mem->mem, GST_LOCK_FLAG_EXCLUSIVE);
211   dma_mem->parent = gst_memory_ref (&mem->mem);
212 }
213
214 gboolean
215 gst_is_v4l2_memory (GstMemory * mem)
216 {
217   return gst_memory_is_type (mem, GST_V4L2_MEMORY_TYPE);
218 }
219
220
221 /*************************************/
222 /* GstV4l2MemoryGroup implementation */
223 /*************************************/
224
225 static void
226 gst_v4l2_memory_group_free (GstV4l2MemoryGroup * group)
227 {
228   gint i;
229
230   for (i = 0; i < group->n_mem; i++) {
231     GstMemory *mem = group->mem[i];
232     group->mem[i] = NULL;
233     if (mem)
234       gst_memory_unref (mem);
235   }
236
237   g_slice_free (GstV4l2MemoryGroup, group);
238 }
239
240 static GstV4l2MemoryGroup *
241 gst_v4l2_memory_group_new (GstV4l2Allocator * allocator, guint32 index)
242 {
243   gint video_fd = allocator->video_fd;
244   guint32 memory = allocator->memory;
245   struct v4l2_format *format = &allocator->format;
246   GstV4l2MemoryGroup *group;
247   gsize img_size, buf_size;
248
249   group = g_slice_new0 (GstV4l2MemoryGroup);
250
251   group->buffer.type = format->type;
252   group->buffer.index = index;
253   group->buffer.memory = memory;
254
255   if (V4L2_TYPE_IS_MULTIPLANAR (format->type)) {
256     group->n_mem = group->buffer.length = format->fmt.pix_mp.num_planes;
257     group->buffer.m.planes = group->planes;
258   } else {
259     group->n_mem = 1;
260   }
261
262   if (v4l2_ioctl (video_fd, VIDIOC_QUERYBUF, &group->buffer) < 0)
263     goto querybuf_failed;
264
265   /* Check that provided size matches the format we have negotiation. Failing
266    * there usually means a driver of libv4l bug. */
267   if (V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
268     gint i;
269
270     for (i = 0; i < group->n_mem; i++) {
271       img_size = allocator->format.fmt.pix_mp.plane_fmt[i].sizeimage;
272       buf_size = group->planes[i].length;
273       if (buf_size < img_size)
274         goto buffer_too_short;
275     }
276   } else {
277     img_size = allocator->format.fmt.pix.sizeimage;
278     buf_size = group->buffer.length;
279     if (buf_size < img_size)
280       goto buffer_too_short;
281   }
282
283   /* We save non planar buffer information into the multi-planar plane array
284    * to avoid duplicating the code later */
285   if (!V4L2_TYPE_IS_MULTIPLANAR (format->type)) {
286     group->planes[0].bytesused = group->buffer.bytesused;
287     group->planes[0].length = group->buffer.length;
288     g_assert (sizeof (group->planes[0].m) == sizeof (group->buffer.m));
289     memcpy (&group->planes[0].m, &group->buffer.m, sizeof (group->buffer.m));
290   }
291
292   GST_LOG_OBJECT (allocator, "Got %s buffer", memory_type_to_str (memory));
293   GST_LOG_OBJECT (allocator, "  index:     %u", group->buffer.index);
294   GST_LOG_OBJECT (allocator, "  type:      %d", group->buffer.type);
295   GST_LOG_OBJECT (allocator, "  flags:     %08x", group->buffer.flags);
296   GST_LOG_OBJECT (allocator, "  field:     %d", group->buffer.field);
297   GST_LOG_OBJECT (allocator, "  memory:    %d", group->buffer.memory);
298   GST_LOG_OBJECT (allocator, "  planes:    %d", group->n_mem);
299
300 #ifndef GST_DISABLE_GST_DEBUG
301   if (memory == V4L2_MEMORY_MMAP) {
302     gint i;
303     for (i = 0; i < group->n_mem; i++) {
304       GST_LOG_OBJECT (allocator, "  [%u] bytesused: %u, length: %u", i,
305           group->planes[i].bytesused, group->planes[i].length);
306       GST_LOG_OBJECT (allocator, "  [%u] MMAP offset:  %u", i,
307           group->planes[i].m.mem_offset);
308     }
309   }
310 #endif
311
312   return group;
313
314 querybuf_failed:
315   {
316     GST_ERROR ("error querying buffer %d: %s", index, g_strerror (errno));
317     goto failed;
318   }
319 buffer_too_short:
320   {
321     GST_ERROR ("buffer size %" G_GSIZE_FORMAT
322         " is smaller then negotiated size %" G_GSIZE_FORMAT
323         ", this is usually the result of a bug in the v4l2 driver or libv4l.",
324         buf_size, img_size);
325     goto failed;
326   }
327 failed:
328   gst_v4l2_memory_group_free (group);
329   return NULL;
330 }
331
332
333 /*************************************/
334 /* GstV4lAllocator implementation    */
335 /*************************************/
336
337 static void
338 gst_v4l2_allocator_release (GstV4l2Allocator * allocator, GstV4l2Memory * mem)
339 {
340   GstV4l2MemoryGroup *group = mem->group;
341
342   GST_LOG_OBJECT (allocator, "plane %i of buffer %u released",
343       mem->plane, group->buffer.index);
344
345   switch (allocator->memory) {
346     case V4L2_MEMORY_DMABUF:
347       close (mem->dmafd);
348       mem->dmafd = -1;
349       break;
350     case V4L2_MEMORY_USERPTR:
351       mem->data = NULL;
352       break;
353     default:
354       break;
355   }
356
357   /* When all memory are back, put the group back in the free queue */
358   if (g_atomic_int_dec_and_test (&group->mems_allocated)) {
359     GST_LOG_OBJECT (allocator, "buffer %u released", group->buffer.index);
360     gst_atomic_queue_push (allocator->free_queue, group);
361     g_signal_emit (allocator, gst_v4l2_allocator_signals[GROUP_RELEASED], 0);
362   }
363
364   /* Keep last, allocator may be freed after this call */
365   g_object_unref (allocator);
366 }
367
368 static void
369 gst_v4l2_allocator_free (GstAllocator * gallocator, GstMemory * gmem)
370 {
371   GstV4l2Allocator *allocator = (GstV4l2Allocator *) gallocator;
372   GstV4l2Memory *mem = (GstV4l2Memory *) gmem;
373   GstV4l2MemoryGroup *group = mem->group;
374
375   GST_LOG_OBJECT (allocator, "freeing plane %i of buffer %u",
376       mem->plane, group->buffer.index);
377
378   switch (allocator->memory) {
379     case V4L2_MEMORY_MMAP:
380       if (mem->data) {
381         v4l2_munmap (mem->data, group->planes[mem->plane].length);
382       } else if (group->planes[mem->plane].m.fd > 0) {
383         close (group->planes[mem->plane].m.fd);
384       }
385       break;
386     default:
387       /* Nothing to do */
388       break;
389   }
390
391   _v4l2mem_free (mem);
392 }
393
394 static void
395 gst_v4l2_allocator_dispose (GObject * obj)
396 {
397   GstV4l2Allocator *allocator = (GstV4l2Allocator *) obj;
398   gint i;
399
400   GST_LOG_OBJECT (obj, "called");
401
402   for (i = 0; i < allocator->count; i++) {
403     GstV4l2MemoryGroup *group = allocator->groups[i];
404     allocator->groups[i] = NULL;
405     if (group)
406       gst_v4l2_memory_group_free (group);
407   }
408
409   G_OBJECT_CLASS (parent_class)->dispose (obj);
410 }
411
412 static void
413 gst_v4l2_allocator_finalize (GObject * obj)
414 {
415   GstV4l2Allocator *allocator = (GstV4l2Allocator *) obj;
416
417   GST_LOG_OBJECT (obj, "called");
418
419   v4l2_close (allocator->video_fd);
420   gst_atomic_queue_unref (allocator->free_queue);
421
422   G_OBJECT_CLASS (parent_class)->finalize (obj);
423 }
424
425 static void
426 gst_v4l2_allocator_class_init (GstV4l2AllocatorClass * klass)
427 {
428   GObjectClass *object_class;
429   GstAllocatorClass *allocator_class;
430
431   allocator_class = (GstAllocatorClass *) klass;
432   object_class = (GObjectClass *) klass;
433
434   allocator_class->alloc = NULL;
435   allocator_class->free = gst_v4l2_allocator_free;
436
437   object_class->dispose = gst_v4l2_allocator_dispose;
438   object_class->finalize = gst_v4l2_allocator_finalize;
439
440   gst_v4l2_allocator_signals[GROUP_RELEASED] = g_signal_new ("group-released",
441       G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL,
442       G_TYPE_NONE, 0);
443
444   GST_DEBUG_CATEGORY_INIT (v4l2allocator_debug, "v4l2allocator", 0,
445       "V4L2 Allocator");
446 }
447
448 static void
449 gst_v4l2_allocator_init (GstV4l2Allocator * allocator)
450 {
451   GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
452
453   alloc->mem_type = GST_V4L2_MEMORY_TYPE;
454   alloc->mem_map = (GstMemoryMapFunction) _v4l2mem_map;
455   alloc->mem_unmap = (GstMemoryUnmapFunction) _v4l2mem_unmap;
456   alloc->mem_share = (GstMemoryShareFunction) _v4l2mem_share;
457   alloc->mem_is_span = (GstMemoryIsSpanFunction) _v4l2mem_is_span;
458   /* Use the default, fallback copy function */
459
460   allocator->free_queue = gst_atomic_queue_new (VIDEO_MAX_FRAME);
461
462   GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
463 }
464
465 #define GST_V4L2_ALLOCATOR_PROBE(obj,type) \
466     gst_v4l2_allocator_probe ((obj), V4L2_MEMORY_ ## type, \
467         GST_V4L2_ALLOCATOR_FLAG_ ## type ## _REQBUFS, \
468         GST_V4L2_ALLOCATOR_FLAG_ ## type ## _CREATE_BUFS)
469 static guint32
470 gst_v4l2_allocator_probe (GstV4l2Allocator * allocator, guint32 memory,
471     guint32 breq_flag, guint32 bcreate_flag)
472 {
473   struct v4l2_requestbuffers breq = { 0 };
474   guint32 flags = 0;
475
476   breq.type = allocator->type;
477   breq.count = 0;
478   breq.memory = memory;
479
480   if (v4l2_ioctl (allocator->video_fd, VIDIOC_REQBUFS, &breq) == 0) {
481     struct v4l2_create_buffers bcreate = { 0 };
482
483     flags |= breq_flag;
484
485     bcreate.memory = V4L2_MEMORY_MMAP;
486     bcreate.format = allocator->format;
487
488     if ((v4l2_ioctl (allocator->video_fd, VIDIOC_CREATE_BUFS, &bcreate) == 0))
489       flags |= bcreate_flag;
490   }
491
492   return flags;
493 }
494
495 static GstV4l2MemoryGroup *
496 gst_v4l2_allocator_create_buf (GstV4l2Allocator * allocator)
497 {
498   struct v4l2_create_buffers bcreate = { 0 };
499   GstV4l2MemoryGroup *group = NULL;
500
501   GST_OBJECT_LOCK (allocator);
502
503   if (!g_atomic_int_get (&allocator->active))
504     goto done;
505
506   bcreate.memory = allocator->memory;
507   bcreate.format = allocator->format;
508   bcreate.count = 1;
509
510   if (!allocator->can_allocate)
511     goto done;
512
513   if (v4l2_ioctl (allocator->video_fd, VIDIOC_CREATE_BUFS, &bcreate) < 0)
514     goto create_bufs_failed;
515
516   group = gst_v4l2_memory_group_new (allocator, bcreate.index);
517
518   if (group) {
519     allocator->groups[bcreate.index] = group;
520     allocator->count++;
521   }
522
523 done:
524   GST_OBJECT_UNLOCK (allocator);
525   return group;
526
527 create_bufs_failed:
528   {
529     GST_WARNING_OBJECT (allocator, "error creating a new buffer: %s",
530         g_strerror (errno));
531     goto done;
532   }
533 }
534
535 static GstV4l2MemoryGroup *
536 gst_v4l2_allocator_alloc (GstV4l2Allocator * allocator)
537 {
538   GstV4l2MemoryGroup *group;
539
540   if (!g_atomic_int_get (&allocator->active))
541     return NULL;
542
543   group = gst_atomic_queue_pop (allocator->free_queue);
544
545   if (group == NULL) {
546     if (allocator->can_allocate) {
547       group = gst_v4l2_allocator_create_buf (allocator);
548
549       /* Don't hammer on CREATE_BUFS */
550       if (group == NULL)
551         allocator->can_allocate = FALSE;
552     }
553   }
554
555   return group;
556 }
557
558 static void
559 gst_v4l2_allocator_reset_size (GstV4l2Allocator * allocator,
560     GstV4l2MemoryGroup * group)
561 {
562   gsize size;
563   gboolean imported = FALSE;
564
565   switch (allocator->memory) {
566     case V4L2_MEMORY_USERPTR:
567     case V4L2_MEMORY_DMABUF:
568       imported = TRUE;
569       break;
570   }
571
572   if (V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
573     gint i;
574
575     for (i = 0; i < group->n_mem; i++) {
576       size = allocator->format.fmt.pix_mp.plane_fmt[i].sizeimage;
577
578       if (imported)
579         group->mem[i]->maxsize = size;
580
581       gst_memory_resize (group->mem[i], 0, size);
582     }
583
584   } else {
585     size = allocator->format.fmt.pix.sizeimage;
586
587     if (imported)
588       group->mem[0]->maxsize = size;
589
590     gst_memory_resize (group->mem[0], 0, size);
591   }
592 }
593
594 static void
595 _cleanup_failed_alloc (GstV4l2Allocator * allocator, GstV4l2MemoryGroup * group)
596 {
597   if (group->mems_allocated > 0) {
598     gint i;
599     /* If one or more mmap worked, we need to unref the memory, otherwise
600      * they will keep a ref on the allocator and leak it. This will put back
601      * the group into the free_queue */
602     for (i = 0; i < group->n_mem; i++)
603       gst_memory_unref (group->mem[i]);
604   } else {
605     /* Otherwise, group has to be on free queue for _stop() to work */
606     gst_atomic_queue_push (allocator->free_queue, group);
607   }
608 }
609
610
611
612 GstV4l2Allocator *
613 gst_v4l2_allocator_new (GstObject * parent, gint video_fd,
614     struct v4l2_format *format)
615 {
616   GstV4l2Allocator *allocator;
617   guint32 flags = 0;
618   gchar *name, *parent_name;
619
620   parent_name = gst_object_get_name (parent);
621   name = g_strconcat (parent_name, ":allocator", NULL);
622   g_free (parent_name);
623
624   allocator = g_object_new (GST_TYPE_V4L2_ALLOCATOR, "name", name, NULL);
625   g_free (name);
626
627   /* Save everything */
628   allocator->video_fd = v4l2_dup (video_fd);
629   allocator->type = format->type;
630   allocator->format = *format;
631
632   flags |= GST_V4L2_ALLOCATOR_PROBE (allocator, MMAP);
633   flags |= GST_V4L2_ALLOCATOR_PROBE (allocator, USERPTR);
634   flags |= GST_V4L2_ALLOCATOR_PROBE (allocator, DMABUF);
635
636   GST_OBJECT_FLAG_SET (allocator, flags);
637
638   if (flags == 0)
639     goto not_supported;
640
641   return allocator;
642
643 not_supported:
644   {
645     GST_ERROR_OBJECT (allocator,
646         "No memory model supported by GStreamer for this device");
647     g_object_unref (allocator);
648     return NULL;
649   }
650 }
651
652 guint
653 gst_v4l2_allocator_start (GstV4l2Allocator * allocator, guint32 count,
654     guint32 memory)
655 {
656   struct v4l2_requestbuffers breq = { count, allocator->type, memory };
657   gboolean can_allocate;
658   gint i;
659
660   g_return_val_if_fail (count != 0, 0);
661
662   GST_OBJECT_LOCK (allocator);
663
664   if (g_atomic_int_get (&allocator->active))
665     goto already_active;
666
667   if (v4l2_ioctl (allocator->video_fd, VIDIOC_REQBUFS, &breq) < 0)
668     goto reqbufs_failed;
669
670   if (breq.count < 1)
671     goto out_of_memory;
672
673   switch (memory) {
674     case V4L2_MEMORY_MMAP:
675       can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (allocator, MMAP);
676       break;
677     case V4L2_MEMORY_USERPTR:
678       can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (allocator, USERPTR);
679       break;
680     case V4L2_MEMORY_DMABUF:
681       can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (allocator, DMABUF);
682       break;
683     default:
684       can_allocate = FALSE;
685       break;
686   }
687
688   GST_DEBUG_OBJECT (allocator, "allocated %u %s buffers out of %u requested",
689       breq.count, memory_type_to_str (memory), count);
690
691   allocator->can_allocate = can_allocate;
692   allocator->count = breq.count;
693   allocator->memory = memory;
694
695   /* Create memory groups */
696   for (i = 0; i < allocator->count; i++) {
697     allocator->groups[i] = gst_v4l2_memory_group_new (allocator, i);
698     if (allocator->groups[i] == NULL)
699       goto error;
700
701     gst_atomic_queue_push (allocator->free_queue, allocator->groups[i]);
702   }
703
704   g_atomic_int_set (&allocator->active, TRUE);
705
706 done:
707   GST_OBJECT_UNLOCK (allocator);
708   return breq.count;
709
710 already_active:
711   {
712     GST_ERROR_OBJECT (allocator,
713         "error requesting %d buffers: %s", count, g_strerror (errno));
714     goto error;
715   }
716 reqbufs_failed:
717   {
718     GST_ERROR_OBJECT (allocator,
719         "error requesting %d buffers: %s", count, g_strerror (errno));
720     goto error;
721   }
722 out_of_memory:
723   {
724     GST_ERROR_OBJECT (allocator, "Not enough memory to allocate buffers");
725     goto error;
726   }
727 error:
728   {
729     breq.count = 0;
730     goto done;
731   }
732 }
733
734 GstV4l2Return
735 gst_v4l2_allocator_stop (GstV4l2Allocator * allocator)
736 {
737   struct v4l2_requestbuffers breq = { 0, allocator->type, allocator->memory };
738   gint i = 0;
739   GstV4l2Return ret = GST_V4L2_OK;
740
741   GST_DEBUG_OBJECT (allocator, "stop allocator");
742
743   GST_OBJECT_LOCK (allocator);
744
745   if (!g_atomic_int_get (&allocator->active))
746     goto done;
747
748   if (gst_atomic_queue_length (allocator->free_queue) != allocator->count) {
749     GST_DEBUG_OBJECT (allocator, "allocator is still in use");
750     ret = GST_V4L2_BUSY;
751     goto done;
752   }
753
754   while (gst_atomic_queue_pop (allocator->free_queue)) {
755     /* nothing */
756   };
757
758   for (i = 0; i < allocator->count; i++) {
759     GstV4l2MemoryGroup *group = allocator->groups[i];
760     allocator->groups[i] = NULL;
761     if (group)
762       gst_v4l2_memory_group_free (group);
763   }
764
765   if (v4l2_ioctl (allocator->video_fd, VIDIOC_REQBUFS, &breq) < 0)
766     goto reqbufs_failed;
767
768   allocator->count = 0;
769
770   g_atomic_int_set (&allocator->active, FALSE);
771
772 done:
773   GST_OBJECT_UNLOCK (allocator);
774   return ret;
775
776 reqbufs_failed:
777   {
778     GST_ERROR_OBJECT (allocator,
779         "error releasing buffers buffers: %s", g_strerror (errno));
780     ret = GST_V4L2_ERROR;
781     goto done;
782   }
783 }
784
785 GstV4l2MemoryGroup *
786 gst_v4l2_allocator_alloc_mmap (GstV4l2Allocator * allocator)
787 {
788   GstV4l2MemoryGroup *group;
789   gint i;
790
791   g_return_val_if_fail (allocator->memory == V4L2_MEMORY_MMAP, NULL);
792
793   group = gst_v4l2_allocator_alloc (allocator);
794
795   if (group == NULL)
796     return NULL;
797
798   for (i = 0; i < group->n_mem; i++) {
799     if (group->mem[i] == NULL) {
800       gpointer data;
801       data = v4l2_mmap (NULL, group->planes[i].length, PROT_READ | PROT_WRITE,
802           MAP_SHARED, allocator->video_fd, group->planes[i].m.mem_offset);
803
804       if (data == MAP_FAILED)
805         goto mmap_failed;
806
807       GST_LOG_OBJECT (allocator,
808           "mmap buffer length %d, data offset %d, plane %d",
809           group->planes[i].length, group->planes[i].data_offset, i);
810
811       group->mem[i] = (GstMemory *) _v4l2mem_new (0, GST_ALLOCATOR (allocator),
812           NULL, group->planes[i].length, 0, 0, group->planes[i].length, i,
813           data, -1, group);
814     } else {
815       /* Take back the allocator reference */
816       gst_object_ref (allocator);
817     }
818
819     group->mems_allocated++;
820   }
821
822   /* Ensure group size. Unlike GST, v4l2 have size (bytesused) initially set
823    * to 0. As length might be bigger then the expected size exposed in the
824    * format, we simply set bytesused initially and reset it here for
825    * simplicity */
826   gst_v4l2_allocator_reset_size (allocator, group);
827
828   return group;
829
830 mmap_failed:
831   {
832     GST_ERROR_OBJECT (allocator, "Failed to mmap buffer: %s",
833         g_strerror (errno));
834     _cleanup_failed_alloc (allocator, group);
835     return NULL;
836   }
837 }
838
839 GstV4l2MemoryGroup *
840 gst_v4l2_allocator_alloc_dmabuf (GstV4l2Allocator * allocator,
841     GstAllocator * dmabuf_allocator)
842 {
843   GstV4l2MemoryGroup *group;
844   gint i;
845
846   g_return_val_if_fail (allocator->memory == V4L2_MEMORY_MMAP, NULL);
847
848   group = gst_v4l2_allocator_alloc (allocator);
849
850   if (group == NULL)
851     return NULL;
852
853   for (i = 0; i < group->n_mem; i++) {
854     GstV4l2Memory *mem;
855     GstMemory *dma_mem;
856     gint dmafd;
857
858     if (group->mem[i] == NULL) {
859       struct v4l2_exportbuffer expbuf = { 0 };
860
861       expbuf.type = allocator->type;
862       expbuf.index = group->buffer.index;
863       expbuf.plane = i;
864       expbuf.flags = O_CLOEXEC | O_RDWR;
865
866       if (v4l2_ioctl (allocator->video_fd, VIDIOC_EXPBUF, &expbuf) < 0)
867         goto expbuf_failed;
868
869       GST_LOG_OBJECT (allocator, "exported DMABUF as fd %i plane %d",
870           expbuf.fd, i);
871
872       group->mem[i] = (GstMemory *) _v4l2mem_new (0, GST_ALLOCATOR (allocator),
873           NULL, group->planes[i].length, 0, 0, group->planes[i].length, i,
874           NULL, expbuf.fd, group);
875     } else {
876       /* Take back the allocator reference */
877       gst_object_ref (allocator);
878     }
879
880     g_assert (gst_is_v4l2_memory (group->mem[i]));
881     mem = (GstV4l2Memory *) group->mem[i];
882
883     if ((dmafd = dup (mem->dmafd)) < 0)
884       goto dup_failed;
885
886     dma_mem = gst_dmabuf_allocator_alloc (dmabuf_allocator, dmafd,
887         mem->mem.maxsize);
888     _v4l2mem_parent_to_dmabuf (mem, dma_mem);
889
890     group->mem[i] = dma_mem;
891     group->mems_allocated++;
892   }
893
894   gst_v4l2_allocator_reset_size (allocator, group);
895
896   return group;
897
898 expbuf_failed:
899   {
900     GST_ERROR_OBJECT (allocator, "Failed to export DMABUF: %s",
901         g_strerror (errno));
902     goto cleanup;
903   }
904 dup_failed:
905   {
906     GST_ERROR_OBJECT (allocator, "Failed to dup DMABUF descriptor: %s",
907         g_strerror (errno));
908     goto cleanup;
909   }
910 cleanup:
911   {
912     _cleanup_failed_alloc (allocator, group);
913     return NULL;
914   }
915 }
916
917 static void
918 gst_v4l2_allocator_clear_dmabufin (GstV4l2Allocator * allocator,
919     GstV4l2MemoryGroup * group)
920 {
921   GstV4l2Memory *mem;
922   gint i;
923
924   g_return_if_fail (allocator->memory == V4L2_MEMORY_DMABUF);
925
926   for (i = 0; i < group->n_mem; i++) {
927
928     mem = (GstV4l2Memory *) group->mem[i];
929
930     GST_LOG_OBJECT (allocator, "clearing DMABUF import, fd %i plane %d",
931         mem->dmafd, i);
932
933     if (mem->dmafd >= 0)
934       close (mem->dmafd);
935
936     /* Update memory */
937     mem->mem.maxsize = 0;
938     mem->mem.offset = 0;
939     mem->mem.size = 0;
940     mem->dmafd = -1;
941
942     /* Update v4l2 structure */
943     group->planes[i].length = 0;
944     group->planes[i].bytesused = 0;
945     group->planes[i].m.fd = -1;
946     group->planes[i].data_offset = 0;
947   }
948
949   if (!V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
950     group->buffer.bytesused = 0;
951     group->buffer.length = 0;
952     group->buffer.m.fd = -1;
953   }
954 }
955
956 GstV4l2MemoryGroup *
957 gst_v4l2_allocator_alloc_dmabufin (GstV4l2Allocator * allocator)
958 {
959   GstV4l2MemoryGroup *group;
960   gint i;
961
962   g_return_val_if_fail (allocator->memory == V4L2_MEMORY_DMABUF, NULL);
963
964   group = gst_v4l2_allocator_alloc (allocator);
965
966   if (group == NULL)
967     return NULL;
968
969   GST_LOG_OBJECT (allocator, "allocating empty DMABUF import group");
970
971   for (i = 0; i < group->n_mem; i++) {
972     if (group->mem[i] == NULL) {
973       group->mem[i] = (GstMemory *) _v4l2mem_new (0, GST_ALLOCATOR (allocator),
974           NULL, 0, 0, 0, 0, i, NULL, -1, group);
975     } else {
976       /* Take back the allocator reference */
977       gst_object_ref (allocator);
978     }
979
980     group->mems_allocated++;
981   }
982
983   gst_v4l2_allocator_clear_dmabufin (allocator, group);
984
985   return group;
986 }
987
988 static void
989 gst_v4l2_allocator_clear_userptr (GstV4l2Allocator * allocator,
990     GstV4l2MemoryGroup * group)
991 {
992   GstV4l2Memory *mem;
993   gint i;
994
995   g_return_if_fail (allocator->memory == V4L2_MEMORY_USERPTR);
996
997   for (i = 0; i < group->n_mem; i++) {
998     mem = (GstV4l2Memory *) group->mem[i];
999
1000     GST_LOG_OBJECT (allocator, "clearing USERPTR %p plane %d size %"
1001         G_GSIZE_FORMAT, mem->data, i, mem->mem.size);
1002
1003     mem->mem.maxsize = 0;
1004     mem->mem.size = 0;
1005     mem->data = NULL;
1006
1007     group->planes[i].length = 0;
1008     group->planes[i].bytesused = 0;
1009     group->planes[i].m.userptr = 0;
1010   }
1011
1012   if (!V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
1013     group->buffer.bytesused = 0;
1014     group->buffer.length = 0;
1015     group->buffer.m.userptr = 0;
1016   }
1017 }
1018
1019 GstV4l2MemoryGroup *
1020 gst_v4l2_allocator_alloc_userptr (GstV4l2Allocator * allocator)
1021 {
1022   GstV4l2MemoryGroup *group;
1023   gint i;
1024
1025   g_return_val_if_fail (allocator->memory == V4L2_MEMORY_USERPTR, NULL);
1026
1027   group = gst_v4l2_allocator_alloc (allocator);
1028
1029   if (group == NULL)
1030     return NULL;
1031
1032   GST_LOG_OBJECT (allocator, "allocating empty USERPTR group");
1033
1034   for (i = 0; i < group->n_mem; i++) {
1035
1036     if (group->mem[i] == NULL) {
1037       group->mem[i] = (GstMemory *) _v4l2mem_new (0, GST_ALLOCATOR (allocator),
1038           NULL, 0, 0, 0, 0, i, NULL, -1, group);
1039     } else {
1040       /* Take back the allocator reference */
1041       gst_object_ref (allocator);
1042     }
1043
1044     group->mems_allocated++;
1045   }
1046
1047   gst_v4l2_allocator_clear_userptr (allocator, group);
1048
1049   return group;
1050 }
1051
1052 gboolean
1053 gst_v4l2_allocator_import_dmabuf (GstV4l2Allocator * allocator,
1054     GstV4l2MemoryGroup * group, gint n_mem, GstMemory ** dma_mem)
1055 {
1056   GstV4l2Memory *mem;
1057   gint i;
1058
1059   g_return_val_if_fail (allocator->memory == V4L2_MEMORY_DMABUF, FALSE);
1060
1061   if (group->n_mem != n_mem)
1062     goto n_mem_missmatch;
1063
1064   for (i = 0; i < group->n_mem; i++) {
1065     gint dmafd;
1066     gsize size, offset, maxsize;
1067
1068     if (!gst_is_dmabuf_memory (dma_mem[i]))
1069       goto not_dmabuf;
1070
1071     size = gst_memory_get_sizes (dma_mem[i], &offset, &maxsize);
1072
1073     if ((dmafd = dup (gst_dmabuf_memory_get_fd (dma_mem[i]))) < 0)
1074       goto dup_failed;
1075
1076     GST_LOG_OBJECT (allocator, "imported DMABUF as fd %i plane %d", dmafd, i);
1077
1078     mem = (GstV4l2Memory *) group->mem[i];
1079
1080     /* Update memory */
1081     mem->mem.maxsize = maxsize;
1082     mem->mem.offset = offset;
1083     mem->mem.size = size;
1084     mem->dmafd = dmafd;
1085
1086     /* Update v4l2 structure */
1087     group->planes[i].length = maxsize;
1088     group->planes[i].bytesused = size;
1089     group->planes[i].m.fd = dmafd;
1090     group->planes[i].data_offset = offset;
1091   }
1092
1093   /* Copy into buffer structure if not using planes */
1094   if (!V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
1095     group->buffer.bytesused = group->planes[0].bytesused;
1096     group->buffer.length = group->planes[0].length;
1097     group->buffer.m.fd = group->planes[0].m.userptr;
1098   } else {
1099     group->buffer.length = group->n_mem;
1100   }
1101
1102   return TRUE;
1103
1104 n_mem_missmatch:
1105   {
1106     GST_ERROR_OBJECT (allocator, "Got %i dmabuf but needed %i", n_mem,
1107         group->n_mem);
1108     return FALSE;
1109   }
1110 not_dmabuf:
1111   {
1112     GST_ERROR_OBJECT (allocator, "Memory %i is not of DMABUF", i);
1113     return FALSE;
1114   }
1115 dup_failed:
1116   {
1117     GST_ERROR_OBJECT (allocator, "Failed to dup DMABUF descriptor: %s",
1118         g_strerror (errno));
1119     return FALSE;
1120   }
1121 }
1122
1123 gboolean
1124 gst_v4l2_allocator_import_userptr (GstV4l2Allocator * allocator,
1125     GstV4l2MemoryGroup * group, gsize img_size, int n_planes,
1126     gpointer * data, gsize * offset)
1127 {
1128   GstV4l2Memory *mem;
1129   gint i;
1130
1131   g_return_val_if_fail (allocator->memory == V4L2_MEMORY_USERPTR, FALSE);
1132
1133   /* TODO Support passing N plane from 1 memory to MPLANE v4l2 format */
1134   if (n_planes != group->n_mem)
1135     goto n_mem_missmatch;
1136
1137   for (i = 0; i < group->n_mem; i++) {
1138     gsize size, maxsize;
1139
1140     if (V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
1141       struct v4l2_pix_format_mplane *pix = &allocator->format.fmt.pix_mp;
1142       maxsize = pix->plane_fmt[i].sizeimage;
1143     } else {
1144       maxsize = allocator->format.fmt.pix.sizeimage;
1145     }
1146
1147     if ((i + 1) == n_planes) {
1148       size = img_size - offset[i];
1149     } else {
1150       size = offset[i + 1] - offset[i];
1151     }
1152
1153     g_assert (size <= img_size);
1154
1155     GST_LOG_OBJECT (allocator, "imported USERPTR %p plane %d size %"
1156         G_GSIZE_FORMAT, data[i], i, size);
1157
1158     mem = (GstV4l2Memory *) group->mem[i];
1159
1160     mem->mem.maxsize = maxsize;
1161     mem->mem.size = size;
1162     mem->data = data[i];
1163
1164     group->planes[i].length = maxsize;
1165     group->planes[i].bytesused = size;
1166     group->planes[i].m.userptr = (unsigned long) data[i];
1167     group->planes[i].data_offset = 0;
1168   }
1169
1170   /* Copy into buffer structure if not using planes */
1171   if (!V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
1172     group->buffer.bytesused = group->planes[0].bytesused;
1173     group->buffer.length = group->planes[0].length;
1174     group->buffer.m.userptr = group->planes[0].m.userptr;
1175   } else {
1176     group->buffer.length = group->n_mem;
1177   }
1178
1179   return TRUE;
1180
1181 n_mem_missmatch:
1182   {
1183     GST_ERROR_OBJECT (allocator, "Got %i userptr plane while driver need %i",
1184         n_planes, group->n_mem);
1185     return FALSE;
1186   }
1187 }
1188
1189 void
1190 gst_v4l2_allocator_flush (GstV4l2Allocator * allocator)
1191 {
1192   gint i;
1193
1194   GST_OBJECT_LOCK (allocator);
1195
1196   if (!g_atomic_int_get (&allocator->active))
1197     goto done;
1198
1199   for (i = 0; i < allocator->count; i++) {
1200     GstV4l2MemoryGroup *group = allocator->groups[i];
1201     gint n;
1202
1203     if (IS_QUEUED (group->buffer)) {
1204       UNSET_QUEUED (group->buffer);
1205
1206       gst_v4l2_allocator_reset_group (allocator, group);
1207
1208       for (n = 0; n < group->n_mem; n++)
1209         gst_memory_unref (group->mem[n]);
1210     }
1211   }
1212
1213 done:
1214   GST_OBJECT_UNLOCK (allocator);
1215 }
1216
1217 gboolean
1218 gst_v4l2_allocator_qbuf (GstV4l2Allocator * allocator,
1219     GstV4l2MemoryGroup * group)
1220 {
1221   gboolean ret = TRUE;
1222   gint i;
1223
1224   g_return_val_if_fail (g_atomic_int_get (&allocator->active), FALSE);
1225
1226   /* Buffer already queued */
1227   if (IS_QUEUED (group->buffer))
1228     return TRUE;
1229
1230   /* update sizes */
1231   if (V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
1232     for (i = 0; i < group->n_mem; i++)
1233       group->planes[i].bytesused =
1234           gst_memory_get_sizes (group->mem[i], NULL, NULL);
1235   } else {
1236     group->buffer.bytesused = gst_memory_get_sizes (group->mem[0], NULL, NULL);
1237   }
1238
1239   if (v4l2_ioctl (allocator->video_fd, VIDIOC_QBUF, &group->buffer) < 0) {
1240     GST_ERROR_OBJECT (allocator, "failed queing buffer %i: %s",
1241         group->buffer.index, g_strerror (errno));
1242     ret = FALSE;
1243     if (IS_QUEUED (group->buffer)) {
1244       GST_DEBUG_OBJECT (allocator,
1245           "driver pretends buffer is queued even if queue failed");
1246       UNSET_QUEUED (group->buffer);
1247     }
1248     goto done;
1249   }
1250
1251   GST_LOG_OBJECT (allocator, "queued buffer %i (flags 0x%X)",
1252       group->buffer.index, group->buffer.flags);
1253
1254   if (!IS_QUEUED (group->buffer)) {
1255     GST_DEBUG_OBJECT (allocator,
1256         "driver pretends buffer is not queued even if queue succeeded");
1257     SET_QUEUED (group->buffer);
1258   }
1259
1260   /* Ensure the memory will stay around and is RO */
1261   for (i = 0; i < group->n_mem; i++)
1262     gst_memory_ref (group->mem[i]);
1263
1264 done:
1265   return ret;
1266 }
1267
1268 GstV4l2MemoryGroup *
1269 gst_v4l2_allocator_dqbuf (GstV4l2Allocator * allocator)
1270 {
1271   struct v4l2_buffer buffer = { 0 };
1272   struct v4l2_plane planes[VIDEO_MAX_PLANES] = { {0} };
1273   gint i;
1274
1275   GstV4l2MemoryGroup *group = NULL;
1276
1277   g_return_val_if_fail (g_atomic_int_get (&allocator->active), FALSE);
1278
1279   buffer.type = allocator->type;
1280   buffer.memory = allocator->memory;
1281
1282   if (V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
1283     buffer.length = allocator->format.fmt.pix_mp.num_planes;
1284     buffer.m.planes = planes;
1285   }
1286
1287   if (v4l2_ioctl (allocator->video_fd, VIDIOC_DQBUF, &buffer) < 0)
1288     goto error;
1289
1290   group = allocator->groups[buffer.index];
1291   group->buffer = buffer;
1292
1293   GST_LOG_OBJECT (allocator, "dequeued buffer %i (flags 0x%X)", buffer.index,
1294       buffer.flags);
1295
1296   if (IS_QUEUED (group->buffer)) {
1297     GST_DEBUG_OBJECT (allocator,
1298         "driver pretends buffer is queued even if dequeue succeeded");
1299     UNSET_QUEUED (group->buffer);
1300   }
1301
1302   if (V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
1303     group->buffer.m.planes = group->planes;
1304     memcpy (group->planes, buffer.m.planes, sizeof (planes));
1305   } else {
1306     group->planes[0].bytesused = group->buffer.bytesused;
1307     group->planes[0].length = group->buffer.length;
1308     g_assert (sizeof (group->planes[0].m) == sizeof (group->buffer.m));
1309     memcpy (&group->planes[0].m, &group->buffer.m, sizeof (group->buffer.m));
1310   }
1311
1312   /* And update memory size */
1313   if (V4L2_TYPE_IS_OUTPUT (allocator->type)) {
1314     gst_v4l2_allocator_reset_size (allocator, group);
1315   } else {
1316     /* for capture, simply read the size */
1317     for (i = 0; i < group->n_mem; i++) {
1318       gst_memory_resize (group->mem[i], 0, group->planes[i].bytesused);
1319     }
1320   }
1321
1322   /* Release the memory, possibly making it RW again */
1323   for (i = 0; i < group->n_mem; i++)
1324     gst_memory_unref (group->mem[i]);
1325
1326   return group;
1327
1328 error:
1329   GST_ERROR_OBJECT (allocator, "failed dequeuing a %s buffer: %s",
1330       memory_type_to_str (allocator->memory), g_strerror (errno));
1331
1332   switch (errno) {
1333     case EAGAIN:
1334       GST_WARNING_OBJECT (allocator,
1335           "Non-blocking I/O has been selected using O_NONBLOCK and"
1336           " no buffer was in the outgoing queue.");
1337       break;
1338     case EINVAL:
1339       GST_ERROR_OBJECT (allocator,
1340           "The buffer type is not supported, or the index is out of bounds, "
1341           "or no buffers have been allocated yet, or the userptr "
1342           "or length are invalid.");
1343       break;
1344     case ENOMEM:
1345       GST_ERROR_OBJECT (allocator,
1346           "insufficient memory to enqueue a user pointer buffer");
1347       break;
1348     case EIO:
1349       GST_INFO_OBJECT (allocator,
1350           "VIDIOC_DQBUF failed due to an internal error."
1351           " Can also indicate temporary problems like signal loss."
1352           " Note the driver might dequeue an (empty) buffer despite"
1353           " returning an error, or even stop capturing.");
1354       /* have we de-queued a buffer ? */
1355       if (!IS_QUEUED (buffer)) {
1356         GST_DEBUG_OBJECT (allocator, "reenqueing buffer");
1357         /* FIXME ... should we do something here? */
1358       }
1359       break;
1360     case EINTR:
1361       GST_WARNING_OBJECT (allocator, "could not sync on a buffer on device");
1362       break;
1363     default:
1364       GST_WARNING_OBJECT (allocator,
1365           "Grabbing frame got interrupted unexpectedly. %d: %s.", errno,
1366           g_strerror (errno));
1367       break;
1368   }
1369
1370   return NULL;
1371 }
1372
1373 void
1374 gst_v4l2_allocator_reset_group (GstV4l2Allocator * allocator,
1375     GstV4l2MemoryGroup * group)
1376 {
1377   switch (allocator->memory) {
1378     case V4L2_MEMORY_USERPTR:
1379       gst_v4l2_allocator_clear_userptr (allocator, group);
1380       break;
1381     case V4L2_MEMORY_DMABUF:
1382       gst_v4l2_allocator_clear_dmabufin (allocator, group);
1383       break;
1384     case V4L2_MEMORY_MMAP:
1385       break;
1386     default:
1387       g_assert_not_reached ();
1388       break;
1389   }
1390
1391   gst_v4l2_allocator_reset_size (allocator, group);
1392 }
1393
1394 gsize
1395 gst_v4l2_allocator_num_allocated (GstV4l2Allocator * allocator)
1396 {
1397   gsize num_allocated;
1398
1399   GST_OBJECT_LOCK (allocator);
1400
1401   num_allocated = allocator->count;
1402
1403   GST_OBJECT_UNLOCK (allocator);
1404
1405   return num_allocated;
1406 }