3ead390968f256f1c5e52ceab1c07ce2298f0fc1
[platform/upstream/gst-plugins-good.git] / sys / v4l2 / gstv4l2bufferpool.c
1 /* GStreamer
2  *
3  * Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4  *               2006 Edgard Lima <edgard.lima@indt.org.br>
5  *               2009 Texas Instruments, Inc - http://www.ti.com/
6  *
7  * gstv4l2bufferpool.c V4L2 buffer pool class
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22  * Boston, MA 02110-1301, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #include <sys/mman.h>
30 #include <string.h>
31 #include <unistd.h>
32 #if HAVE_DECL_V4L2_MEMORY_DMABUF
33 #include <fcntl.h>
34 #endif
35
36 #include "gst/video/video.h"
37 #include "gst/video/gstvideometa.h"
38 #include "gst/video/gstvideopool.h"
39 #include "gst/allocators/gstdmabuf.h"
40
41 #include <gstv4l2bufferpool.h>
42
43 #include "v4l2_calls.h"
44 #include "gst/gst-i18n-plugin.h"
45 #include <gst/glib-compat-private.h>
46
47 /* videodev2.h is not versioned and we can't easily check for the presence
48  * of enum values at compile time, but the V4L2_CAP_VIDEO_OUTPUT_OVERLAY define
49  * was added in the same commit as V4L2_FIELD_INTERLACED_{TB,BT} (b2787845) */
50 #ifndef V4L2_CAP_VIDEO_OUTPUT_OVERLAY
51 #define V4L2_FIELD_INTERLACED_TB 8
52 #define V4L2_FIELD_INTERLACED_BT 9
53 #endif
54
55
56 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
57 #define GST_CAT_DEFAULT v4l2_debug
58
59 /*
60  * GstV4l2Buffer:
61  */
62 GType
63 gst_v4l2_meta_api_get_type (void)
64 {
65   static volatile GType type;
66   static const gchar *tags[] = { "memory", NULL };
67
68   if (g_once_init_enter (&type)) {
69     GType _type = gst_meta_api_type_register ("GstV4l2MetaAPI", tags);
70     g_once_init_leave (&type, _type);
71   }
72   return type;
73 }
74
75 const GstMetaInfo *
76 gst_v4l2_meta_get_info (void)
77 {
78   static const GstMetaInfo *meta_info = NULL;
79
80   if (g_once_init_enter (&meta_info)) {
81     const GstMetaInfo *meta =
82         gst_meta_register (gst_v4l2_meta_api_get_type (), "GstV4l2Meta",
83         sizeof (GstV4l2Meta), (GstMetaInitFunction) NULL,
84         (GstMetaFreeFunction) NULL, (GstMetaTransformFunction) NULL);
85     g_once_init_leave (&meta_info, meta);
86   }
87   return meta_info;
88 }
89
90 /*
91  * GstV4l2BufferPool:
92  */
93 #define gst_v4l2_buffer_pool_parent_class parent_class
94 G_DEFINE_TYPE (GstV4l2BufferPool, gst_v4l2_buffer_pool, GST_TYPE_BUFFER_POOL);
95
96 static void gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool,
97     GstBuffer * buffer);
98
99 static void
100 gst_v4l2_buffer_pool_free_buffer (GstBufferPool * bpool, GstBuffer * buffer)
101 {
102   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
103   GstV4l2Object *obj;
104
105   obj = pool->obj;
106
107   switch (obj->mode) {
108     case GST_V4L2_IO_RW:
109     case GST_V4L2_IO_DMABUF:
110       break;
111     case GST_V4L2_IO_MMAP:
112     {
113       GstV4l2Meta *meta;
114       gint index;
115
116       meta = GST_V4L2_META_GET (buffer);
117       g_assert (meta != NULL);
118
119       index = meta->vbuffer.index;
120       GST_LOG_OBJECT (pool,
121           "unmap buffer %p idx %d (data %p, len %u)", buffer,
122           index, meta->mem, meta->vbuffer.length);
123
124       v4l2_munmap (meta->mem, meta->vbuffer.length);
125       pool->buffers[index] = NULL;
126       break;
127     }
128     case GST_V4L2_IO_USERPTR:
129     default:
130       g_assert_not_reached ();
131       break;
132   }
133   gst_buffer_unref (buffer);
134 }
135
136 static GstFlowReturn
137 gst_v4l2_buffer_pool_alloc_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
138     GstBufferPoolAcquireParams * params)
139 {
140   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
141   GstBuffer *newbuf;
142   GstV4l2Meta *meta;
143   GstV4l2Object *obj;
144   GstVideoInfo *info;
145   guint index;
146
147   obj = pool->obj;
148   info = &obj->info;
149
150   switch (obj->mode) {
151     case GST_V4L2_IO_RW:
152     {
153       newbuf =
154           gst_buffer_new_allocate (pool->allocator, pool->size, &pool->params);
155       break;
156     }
157     case GST_V4L2_IO_MMAP:
158     case GST_V4L2_IO_DMABUF:
159     {
160 #ifdef VIDIOC_CREATE_BUFS
161       if (pool->num_allocated == pool->num_buffers) {
162         struct v4l2_create_buffers create_bufs;
163
164         memset (&create_bufs, 0, sizeof (struct v4l2_create_buffers));
165         create_bufs.count = 1;
166         create_bufs.memory = V4L2_MEMORY_MMAP;
167         create_bufs.format.type = obj->type;
168
169         if (v4l2_ioctl (pool->video_fd, VIDIOC_G_FMT, &create_bufs.format) < 0)
170           goto g_fmt_failed;
171
172         if (v4l2_ioctl (pool->video_fd, VIDIOC_CREATE_BUFS, &create_bufs) < 0)
173           goto create_bufs_failed;
174
175         GST_LOG_OBJECT (pool, "created buffer with index: %u",
176             create_bufs.index);
177         pool->num_buffers++;
178         pool->buffers = g_renew (GstBuffer *, pool->buffers, pool->num_buffers);
179         pool->buffers[pool->num_buffers - 1] = NULL;
180       }
181 #endif
182       newbuf = gst_buffer_new ();
183       meta = GST_V4L2_META_ADD (newbuf);
184
185       index = pool->num_allocated;
186
187       GST_LOG_OBJECT (pool, "creating buffer %u, %p", index, newbuf);
188
189       meta->vbuffer.index = index;
190       meta->vbuffer.type = obj->type;
191       meta->vbuffer.memory = V4L2_MEMORY_MMAP;
192
193       if (v4l2_ioctl (pool->video_fd, VIDIOC_QUERYBUF, &meta->vbuffer) < 0)
194         goto querybuf_failed;
195
196       GST_LOG_OBJECT (pool, "  index:     %u", meta->vbuffer.index);
197       GST_LOG_OBJECT (pool, "  type:      %d", meta->vbuffer.type);
198       GST_LOG_OBJECT (pool, "  bytesused: %u", meta->vbuffer.bytesused);
199       GST_LOG_OBJECT (pool, "  flags:     %08x", meta->vbuffer.flags);
200       GST_LOG_OBJECT (pool, "  field:     %d", meta->vbuffer.field);
201       GST_LOG_OBJECT (pool, "  memory:    %d", meta->vbuffer.memory);
202       if (meta->vbuffer.memory == V4L2_MEMORY_MMAP)
203         GST_LOG_OBJECT (pool, "  MMAP offset:  %u", meta->vbuffer.m.offset);
204
205       if (obj->mode == GST_V4L2_IO_MMAP) {
206         meta->mem = v4l2_mmap (0, meta->vbuffer.length,
207             PROT_READ | PROT_WRITE, MAP_SHARED, pool->video_fd,
208             meta->vbuffer.m.offset);
209         if (meta->mem == MAP_FAILED)
210           goto mmap_failed;
211
212         gst_buffer_append_memory (newbuf,
213             gst_memory_new_wrapped (GST_MEMORY_FLAG_NO_SHARE,
214                 meta->mem, meta->vbuffer.length, 0, meta->vbuffer.length, NULL,
215                 NULL));
216       }
217 #if HAVE_DECL_V4L2_MEMORY_DMABUF
218       if (obj->mode == GST_V4L2_IO_DMABUF) {
219         struct v4l2_exportbuffer expbuf;
220
221         memset (&expbuf, 0, sizeof (struct v4l2_exportbuffer));
222         expbuf.type = meta->vbuffer.type;
223         expbuf.index = meta->vbuffer.index;
224         expbuf.flags = O_CLOEXEC;
225         if (v4l2_ioctl (pool->video_fd, VIDIOC_EXPBUF, &expbuf) < 0)
226           goto expbuf_failed;
227
228         meta->vbuffer.memory = V4L2_MEMORY_DMABUF;
229         gst_buffer_append_memory (newbuf,
230             gst_dmabuf_allocator_alloc (pool->allocator, expbuf.fd,
231                 meta->vbuffer.length));
232       }
233 #endif
234       /* add metadata to raw video buffers */
235       if (pool->add_videometa && info->finfo) {
236         const GstVideoFormatInfo *finfo = info->finfo;
237         gsize offset[GST_VIDEO_MAX_PLANES];
238         gint width, height, n_planes, offs, i, stride[GST_VIDEO_MAX_PLANES];
239
240         width = GST_VIDEO_INFO_WIDTH (info);
241         height = GST_VIDEO_INFO_HEIGHT (info);
242         n_planes = GST_VIDEO_INFO_N_PLANES (info);
243
244         GST_DEBUG_OBJECT (pool, "adding video meta, bytesperline %d",
245             obj->bytesperline);
246
247         offs = 0;
248         for (i = 0; i < n_planes; i++) {
249           offset[i] = offs;
250           stride[i] =
251               GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, i, obj->bytesperline);
252
253           offs +=
254               stride[i] * GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i, height);
255         }
256         gst_buffer_add_video_meta_full (newbuf, GST_VIDEO_FRAME_FLAG_NONE,
257             GST_VIDEO_INFO_FORMAT (info), width, height, n_planes,
258             offset, stride);
259       }
260       break;
261     }
262     case GST_V4L2_IO_USERPTR:
263     default:
264       newbuf = NULL;
265       g_assert_not_reached ();
266   }
267
268   pool->num_allocated++;
269
270   *buffer = newbuf;
271
272   return GST_FLOW_OK;
273
274   /* ERRORS */
275 #ifdef VIDIOC_CREATE_BUFS
276 g_fmt_failed:
277   {
278     gint errnosave = errno;
279
280     GST_WARNING ("Failed G_FMT: %s", g_strerror (errnosave));
281     errno = errnosave;
282     return GST_FLOW_ERROR;
283   }
284 create_bufs_failed:
285   {
286     gint errnosave = errno;
287
288     GST_WARNING ("Failed CREATE_BUFS: %s", g_strerror (errnosave));
289     errno = errnosave;
290     return GST_FLOW_ERROR;
291   }
292 #endif
293 querybuf_failed:
294   {
295     gint errnosave = errno;
296
297     GST_WARNING ("Failed QUERYBUF: %s", g_strerror (errnosave));
298     gst_buffer_unref (newbuf);
299     errno = errnosave;
300     return GST_FLOW_ERROR;
301   }
302 mmap_failed:
303   {
304     gint errnosave = errno;
305
306     GST_WARNING ("Failed to mmap: %s", g_strerror (errnosave));
307     gst_buffer_unref (newbuf);
308     errno = errnosave;
309     return GST_FLOW_ERROR;
310   }
311 #if HAVE_DECL_V4L2_MEMORY_DMABUF
312 expbuf_failed:
313   {
314     gint errnosave = errno;
315
316     GST_WARNING ("Failed EXPBUF: %s", g_strerror (errnosave));
317     gst_buffer_unref (newbuf);
318     errno = errnosave;
319     return GST_FLOW_ERROR;
320   }
321 #endif
322 }
323
324 static gboolean
325 gst_v4l2_buffer_pool_set_config (GstBufferPool * bpool, GstStructure * config)
326 {
327   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
328   GstV4l2Object *obj = pool->obj;
329   GstCaps *caps;
330   guint size, min_buffers, max_buffers, num_buffers, copy_threshold;
331   GstAllocator *allocator;
332   GstAllocationParams params;
333   struct v4l2_requestbuffers breq;
334
335   GST_DEBUG_OBJECT (pool, "set config");
336
337   pool->add_videometa =
338       gst_buffer_pool_config_has_option (config,
339       GST_BUFFER_POOL_OPTION_VIDEO_META);
340
341   if (!pool->add_videometa &&
342       GST_VIDEO_INFO_FORMAT (&obj->info) != GST_VIDEO_FORMAT_ENCODED) {
343     gint stride;
344
345     /* we don't have video metadata, and we are not dealing with raw video,
346      * see if the strides are compatible */
347     stride = GST_VIDEO_INFO_PLANE_STRIDE (&obj->info, 0);
348
349     GST_DEBUG_OBJECT (pool, "no videometadata, checking strides %d and %u",
350         stride, obj->bytesperline);
351
352     if (stride != obj->bytesperline)
353       goto missing_video_api;
354   }
355
356   /* parse the config and keep around */
357   if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
358           &max_buffers))
359     goto wrong_config;
360
361   if (!gst_buffer_pool_config_get_allocator (config, &allocator, &params))
362     goto wrong_config;
363
364   GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
365
366   switch (obj->mode) {
367     case GST_V4L2_IO_RW:
368       /* we preallocate 1 buffer, this value also instructs the latency
369        * calculation to have 1 frame latency max */
370       num_buffers = 1;
371       copy_threshold = 0;
372       break;
373     case GST_V4L2_IO_DMABUF:
374     case GST_V4L2_IO_MMAP:
375     {
376       /* request a reasonable number of buffers when no max specified. We will
377        * copy when we run out of buffers */
378       if (max_buffers == 0)
379         num_buffers = 4;
380       else
381         num_buffers = max_buffers;
382
383       /* first, lets request buffers, and see how many we can get: */
384       GST_DEBUG_OBJECT (pool, "starting, requesting %d MMAP buffers",
385           num_buffers);
386
387       memset (&breq, 0, sizeof (struct v4l2_requestbuffers));
388       breq.type = obj->type;
389       breq.count = num_buffers;
390       breq.memory = V4L2_MEMORY_MMAP;
391
392       if (v4l2_ioctl (pool->video_fd, VIDIOC_REQBUFS, &breq) < 0)
393         goto reqbufs_failed;
394
395       GST_LOG_OBJECT (pool, " count:  %u", breq.count);
396       GST_LOG_OBJECT (pool, " type:   %d", breq.type);
397       GST_LOG_OBJECT (pool, " memory: %d", breq.memory);
398
399       if (breq.count < GST_V4L2_MIN_BUFFERS)
400         goto no_buffers;
401
402       if (num_buffers != breq.count) {
403         GST_WARNING_OBJECT (pool, "using %u buffers instead", breq.count);
404         num_buffers = breq.count;
405       }
406       /* update min buffers with the amount of buffers we just reserved. We need
407        * to configure this value in the bufferpool so that the default start
408        * implementation calls our allocate function */
409       min_buffers = breq.count;
410
411       if (max_buffers == 0 || num_buffers < max_buffers) {
412         /* if we are asked to provide more buffers than we have allocated, start
413          * copying buffers when we only have 2 buffers left in the pool */
414         copy_threshold = 2;
415       } else {
416         /* we are certain that we have enough buffers so we don't need to
417          * copy */
418         copy_threshold = 0;
419       }
420       break;
421     }
422     case GST_V4L2_IO_USERPTR:
423     default:
424       num_buffers = 0;
425       copy_threshold = 0;
426       g_assert_not_reached ();
427       break;
428   }
429
430   pool->size = size;
431   pool->num_buffers = num_buffers;
432   pool->copy_threshold = copy_threshold;
433
434   if (obj->mode == GST_V4L2_IO_DMABUF)
435     allocator = gst_dmabuf_allocator_obtain ();
436
437   if (pool->allocator)
438     gst_object_unref (pool->allocator);
439   if ((pool->allocator = allocator))
440     gst_object_ref (allocator);
441   pool->params = params;
442
443   gst_buffer_pool_config_set_params (config, caps, size, min_buffers,
444       max_buffers);
445
446   return GST_BUFFER_POOL_CLASS (parent_class)->set_config (bpool, config);
447
448   /* ERRORS */
449 missing_video_api:
450   {
451     GST_ERROR_OBJECT (pool, "missing GstMetaVideo API in config, "
452         "default stride: %d, wanted stride %u",
453         GST_VIDEO_INFO_PLANE_STRIDE (&obj->info, 0), obj->bytesperline);
454     return FALSE;
455   }
456 wrong_config:
457   {
458     GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
459     return FALSE;
460   }
461 reqbufs_failed:
462   {
463     GST_ERROR_OBJECT (pool,
464         "error requesting %d buffers: %s", num_buffers, g_strerror (errno));
465     return FALSE;
466   }
467 no_buffers:
468   {
469     GST_ERROR_OBJECT (pool,
470         "we received %d from device '%s', we want at least %d",
471         breq.count, obj->videodev, GST_V4L2_MIN_BUFFERS);
472     return FALSE;
473   }
474 }
475
476 static gboolean
477 start_streaming (GstV4l2BufferPool * pool)
478 {
479   GstV4l2Object *obj = pool->obj;
480
481   switch (obj->mode) {
482     case GST_V4L2_IO_RW:
483       break;
484     case GST_V4L2_IO_MMAP:
485     case GST_V4L2_IO_USERPTR:
486     case GST_V4L2_IO_DMABUF:
487       GST_DEBUG_OBJECT (pool, "STREAMON");
488       if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMON, &obj->type) < 0)
489         goto start_failed;
490       break;
491     default:
492       g_assert_not_reached ();
493       break;
494   }
495
496   pool->streaming = TRUE;
497
498   return TRUE;
499
500   /* ERRORS */
501 start_failed:
502   {
503     GST_ERROR_OBJECT (pool, "error with STREAMON %d (%s)", errno,
504         g_strerror (errno));
505     return FALSE;
506   }
507 }
508
509 static gboolean
510 gst_v4l2_buffer_pool_start (GstBufferPool * bpool)
511 {
512   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
513   GstV4l2Object *obj = pool->obj;
514
515   pool->obj = obj;
516   pool->buffers = g_new0 (GstBuffer *, pool->num_buffers);
517   pool->num_allocated = 0;
518
519   /* now, allocate the buffers: */
520   if (!GST_BUFFER_POOL_CLASS (parent_class)->start (bpool))
521     goto start_failed;
522
523   /* we can start capturing now, we wait for the playback case until we queued
524    * the first buffer */
525   if (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
526     if (!start_streaming (pool))
527       goto start_failed;
528
529   gst_poll_set_flushing (obj->poll, FALSE);
530
531   return TRUE;
532
533   /* ERRORS */
534 start_failed:
535   {
536     GST_ERROR_OBJECT (pool, "failed to start streaming");
537     return FALSE;
538   }
539 }
540
541 static void
542 gst_v4l2_buffer_pool_free_buffers (GstV4l2BufferPool * pool)
543 {
544   if (pool->num_buffers > 0) {
545     struct v4l2_requestbuffers breq;
546     memset (&breq, 0, sizeof (struct v4l2_requestbuffers));
547     breq.type = pool->obj->type;
548     breq.count = 0;
549     breq.memory = V4L2_MEMORY_MMAP;
550     if (v4l2_ioctl (pool->video_fd, VIDIOC_REQBUFS, &breq) < 0) {
551       GST_ERROR_OBJECT (pool, "error releasing buffers: %s",
552           g_strerror (errno));
553     }
554     pool->num_buffers = 0;
555   }
556 }
557
558 static gboolean
559 gst_v4l2_buffer_pool_stop (GstBufferPool * bpool)
560 {
561   gboolean ret;
562   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
563   GstV4l2Object *obj = pool->obj;
564   guint n;
565
566   GST_DEBUG_OBJECT (pool, "stopping pool");
567
568   gst_poll_set_flushing (obj->poll, TRUE);
569
570   if (pool->streaming) {
571     switch (obj->mode) {
572       case GST_V4L2_IO_RW:
573         break;
574       case GST_V4L2_IO_MMAP:
575       case GST_V4L2_IO_USERPTR:
576       case GST_V4L2_IO_DMABUF:
577         /* we actually need to sync on all queued buffers but not
578          * on the non-queued ones */
579         GST_DEBUG_OBJECT (pool, "STREAMOFF");
580         if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMOFF, &obj->type) < 0)
581           goto stop_failed;
582         break;
583       default:
584         g_assert_not_reached ();
585         break;
586     }
587     pool->streaming = FALSE;
588   }
589
590   /* first free the buffers in the queue */
591   ret = GST_BUFFER_POOL_CLASS (parent_class)->stop (bpool);
592
593   /* then free the remaining buffers */
594   for (n = 0; n < pool->num_buffers; n++) {
595     if (pool->buffers[n])
596       gst_v4l2_buffer_pool_free_buffer (bpool, pool->buffers[n]);
597   }
598   pool->num_queued = 0;
599   g_free (pool->buffers);
600   pool->buffers = NULL;
601
602   gst_v4l2_buffer_pool_free_buffers (pool);
603
604   return ret;
605
606   /* ERRORS */
607 stop_failed:
608   {
609     GST_ERROR_OBJECT (pool, "error with STREAMOFF %d (%s)", errno,
610         g_strerror (errno));
611     return FALSE;
612   }
613 }
614
615 static GstFlowReturn
616 gst_v4l2_object_poll (GstV4l2Object * v4l2object)
617 {
618   gint ret;
619
620   if (v4l2object->can_poll_device) {
621     GST_LOG_OBJECT (v4l2object->element, "polling device");
622     ret = gst_poll_wait (v4l2object->poll, GST_CLOCK_TIME_NONE);
623     if (G_UNLIKELY (ret < 0)) {
624       if (errno == EBUSY)
625         goto stopped;
626       if (errno == ENXIO) {
627         GST_WARNING_OBJECT (v4l2object->element,
628             "v4l2 device doesn't support polling. Disabling");
629         v4l2object->can_poll_device = FALSE;
630       } else {
631         if (errno != EAGAIN && errno != EINTR)
632           goto select_error;
633       }
634     }
635   }
636   return GST_FLOW_OK;
637
638   /* ERRORS */
639 stopped:
640   {
641     GST_DEBUG ("stop called");
642     return GST_FLOW_FLUSHING;
643   }
644 select_error:
645   {
646     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ, (NULL),
647         ("poll error %d: %s (%d)", ret, g_strerror (errno), errno));
648     return GST_FLOW_ERROR;
649   }
650 }
651
652 static GstFlowReturn
653 gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool * pool, GstBuffer * buf)
654 {
655   GstV4l2Meta *meta;
656   gint index;
657
658   meta = GST_V4L2_META_GET (buf);
659   if (meta == NULL) {
660     GST_LOG_OBJECT (pool, "unref copied buffer %p", buf);
661     /* no meta, it was a copied buffer that we can unref */
662     gst_buffer_unref (buf);
663     return GST_FLOW_OK;
664   }
665
666   index = meta->vbuffer.index;
667   meta->vbuffer.bytesused = gst_buffer_get_size (buf);
668
669   GST_LOG_OBJECT (pool,
670       "enqueue buffer %p, index:%d, queued:%d, flags:%08x used:%d",
671       buf, index, pool->num_queued, meta->vbuffer.flags,
672       meta->vbuffer.bytesused);
673
674   if (pool->buffers[index] != NULL)
675     goto already_queued;
676
677   GST_LOG_OBJECT (pool, "doing QBUF");
678   if (v4l2_ioctl (pool->video_fd, VIDIOC_QBUF, &meta->vbuffer) < 0)
679     goto queue_failed;
680
681   pool->buffers[index] = buf;
682   pool->num_queued++;
683
684   return GST_FLOW_OK;
685
686   /* ERRORS */
687 already_queued:
688   {
689     GST_WARNING_OBJECT (pool, "the buffer was already queued");
690     return GST_FLOW_ERROR;
691   }
692 queue_failed:
693   {
694     GST_WARNING_OBJECT (pool, "could not queue a buffer %d (%s)", errno,
695         g_strerror (errno));
696     return GST_FLOW_ERROR;
697   }
698 }
699
700 static GstFlowReturn
701 gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer)
702 {
703   GstFlowReturn res;
704   GstBuffer *outbuf;
705   struct v4l2_buffer vbuffer;
706   GstV4l2Object *obj = pool->obj;
707   GstClockTime timestamp;
708
709   if ((res = gst_v4l2_object_poll (obj)) != GST_FLOW_OK)
710     goto poll_error;
711
712   memset (&vbuffer, 0x00, sizeof (vbuffer));
713   vbuffer.type = obj->type;
714 #if HAVE_DECL_V4L2_MEMORY_DMABUF
715   if (obj->mode == GST_V4L2_IO_DMABUF)
716     vbuffer.memory = V4L2_MEMORY_DMABUF;
717   else
718 #endif
719     vbuffer.memory = V4L2_MEMORY_MMAP;
720
721   GST_LOG_OBJECT (pool, "doing DQBUF");
722   if (v4l2_ioctl (pool->video_fd, VIDIOC_DQBUF, &vbuffer) < 0)
723     goto error;
724
725   /* get our GstBuffer with that index from the pool, if the buffer was
726    * outstanding we have a serious problem.
727    */
728   outbuf = pool->buffers[vbuffer.index];
729   if (outbuf == NULL)
730     goto no_buffer;
731
732   /* mark the buffer outstanding */
733   pool->buffers[vbuffer.index] = NULL;
734   pool->num_queued--;
735
736   timestamp = GST_TIMEVAL_TO_TIME (vbuffer.timestamp);
737
738   GST_LOG_OBJECT (pool,
739       "dequeued buffer %p seq:%d (ix=%d), used %d, flags %08x, ts %"
740       GST_TIME_FORMAT ", pool-queued=%d, buffer=%p", outbuf, vbuffer.sequence,
741       vbuffer.index, vbuffer.bytesused, vbuffer.flags,
742       GST_TIME_ARGS (timestamp), pool->num_queued, outbuf);
743
744   /* set top/bottom field first if v4l2_buffer has the information */
745   if (vbuffer.field == V4L2_FIELD_INTERLACED_TB) {
746     GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
747   }
748   if (vbuffer.field == V4L2_FIELD_INTERLACED_BT) {
749     GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
750   }
751
752   /* this can change at every frame, esp. with jpeg */
753   if (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
754     gst_buffer_resize (outbuf, 0, vbuffer.bytesused);
755
756   GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
757
758   *buffer = outbuf;
759
760   return GST_FLOW_OK;
761
762   /* ERRORS */
763 poll_error:
764   {
765     GST_DEBUG_OBJECT (pool, "poll error %s", gst_flow_get_name (res));
766     return res;
767   }
768 error:
769   {
770     GST_WARNING_OBJECT (pool,
771         "problem dequeuing frame %d (ix=%d), pool-ct=%d, buf.flags=%d",
772         vbuffer.sequence, vbuffer.index,
773         GST_MINI_OBJECT_REFCOUNT (pool), vbuffer.flags);
774
775     switch (errno) {
776       case EAGAIN:
777         GST_WARNING_OBJECT (pool,
778             "Non-blocking I/O has been selected using O_NONBLOCK and"
779             " no buffer was in the outgoing queue. device %s", obj->videodev);
780         break;
781       case EINVAL:
782         GST_ERROR_OBJECT (pool,
783             "The buffer type is not supported, or the index is out of bounds, "
784             "or no buffers have been allocated yet, or the userptr "
785             "or length are invalid. device %s", obj->videodev);
786         break;
787       case ENOMEM:
788         GST_ERROR_OBJECT (pool,
789             "insufficient memory to enqueue a user pointer buffer");
790         break;
791       case EIO:
792         GST_INFO_OBJECT (pool,
793             "VIDIOC_DQBUF failed due to an internal error."
794             " Can also indicate temporary problems like signal loss."
795             " Note the driver might dequeue an (empty) buffer despite"
796             " returning an error, or even stop capturing."
797             " device %s", obj->videodev);
798         /* have we de-queued a buffer ? */
799         if (!(vbuffer.flags & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))) {
800           GST_DEBUG_OBJECT (pool, "reenqueing buffer");
801           /* FIXME ... should we do something here? */
802         }
803         break;
804       case EINTR:
805         GST_WARNING_OBJECT (pool,
806             "could not sync on a buffer on device %s", obj->videodev);
807         break;
808       default:
809         GST_WARNING_OBJECT (pool,
810             "Grabbing frame got interrupted on %s unexpectedly. %d: %s.",
811             obj->videodev, errno, g_strerror (errno));
812         break;
813     }
814     return GST_FLOW_ERROR;
815   }
816 no_buffer:
817   {
818     GST_ERROR_OBJECT (pool, "No free buffer found in the pool at index %d.",
819         vbuffer.index);
820     return GST_FLOW_ERROR;
821   }
822 }
823
824 static GstFlowReturn
825 gst_v4l2_buffer_pool_acquire_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
826     GstBufferPoolAcquireParams * params)
827 {
828   GstFlowReturn ret;
829   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
830   GstV4l2Object *obj = pool->obj;
831
832   GST_DEBUG_OBJECT (pool, "acquire");
833
834   if (GST_BUFFER_POOL_IS_FLUSHING (bpool))
835     goto flushing;
836
837   switch (obj->type) {
838     case V4L2_BUF_TYPE_VIDEO_CAPTURE:
839       /* capture, This function should return a buffer with new captured data */
840       switch (obj->mode) {
841         case GST_V4L2_IO_RW:
842           /* take empty buffer from the pool */
843           ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
844               buffer, params);
845           break;
846         case GST_V4L2_IO_DMABUF:
847         case GST_V4L2_IO_MMAP:
848           /* just dequeue a buffer, we basically use the queue of v4l2 as the
849            * storage for our buffers. This function does poll first so we can
850            * interrupt it fine. */
851           ret = gst_v4l2_buffer_pool_dqbuf (pool, buffer);
852           if (G_UNLIKELY (ret != GST_FLOW_OK))
853             goto done;
854
855           /* start copying buffers when we are running low on buffers */
856           if (pool->num_queued < pool->copy_threshold) {
857             GstBuffer *copy;
858 #ifdef VIDIOC_CREATE_BUFS
859             if (pool->can_alloc) {
860               if (GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
861                       &copy, params) == GST_FLOW_OK) {
862                 gst_v4l2_buffer_pool_release_buffer (bpool, copy);
863                 break;
864               } else {
865                 pool->can_alloc = FALSE;
866               }
867             }
868 #endif
869
870             /* copy the memory */
871             copy = gst_buffer_copy (*buffer);
872             GST_LOG_OBJECT (pool, "copy buffer %p->%p", *buffer, copy);
873
874             /* and requeue so that we can continue capturing */
875             ret = gst_v4l2_buffer_pool_qbuf (pool, *buffer);
876             *buffer = copy;
877           }
878           break;
879
880         case GST_V4L2_IO_USERPTR:
881         default:
882           ret = GST_FLOW_ERROR;
883           g_assert_not_reached ();
884           break;
885       }
886       break;
887
888     case V4L2_BUF_TYPE_VIDEO_OUTPUT:
889       /* playback, This function should return an empty buffer */
890       switch (obj->mode) {
891         case GST_V4L2_IO_RW:
892           /* get an empty buffer */
893           ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
894               buffer, params);
895           break;
896
897         case GST_V4L2_IO_MMAP:
898           /* get a free unqueued buffer */
899           ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
900               buffer, params);
901           break;
902
903         case GST_V4L2_IO_USERPTR:
904         default:
905           ret = GST_FLOW_ERROR;
906           g_assert_not_reached ();
907           break;
908       }
909       break;
910
911     default:
912       ret = GST_FLOW_ERROR;
913       g_assert_not_reached ();
914       break;
915   }
916 done:
917   return ret;
918
919   /* ERRORS */
920 flushing:
921   {
922     GST_DEBUG_OBJECT (pool, "We are flushing");
923     return GST_FLOW_FLUSHING;
924   }
925 }
926
927 static void
928 gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool, GstBuffer * buffer)
929 {
930   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
931   GstV4l2Object *obj = pool->obj;
932
933   GST_DEBUG_OBJECT (pool, "release buffer %p", buffer);
934
935   switch (obj->type) {
936     case V4L2_BUF_TYPE_VIDEO_CAPTURE:
937       /* capture, put the buffer back in the queue so that we can refill it
938        * later. */
939       switch (obj->mode) {
940         case GST_V4L2_IO_RW:
941           /* release back in the pool */
942           GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool, buffer);
943           break;
944
945         case GST_V4L2_IO_DMABUF:
946         case GST_V4L2_IO_MMAP:
947           /* queue back in the device */
948           gst_v4l2_buffer_pool_qbuf (pool, buffer);
949           break;
950
951         case GST_V4L2_IO_USERPTR:
952         default:
953           g_assert_not_reached ();
954           break;
955       }
956       break;
957
958     case V4L2_BUF_TYPE_VIDEO_OUTPUT:
959       switch (obj->mode) {
960         case GST_V4L2_IO_RW:
961           /* release back in the pool */
962           GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool, buffer);
963           break;
964
965         case GST_V4L2_IO_MMAP:
966         {
967           GstV4l2Meta *meta;
968           guint index;
969
970           meta = GST_V4L2_META_GET (buffer);
971           g_assert (meta != NULL);
972
973           index = meta->vbuffer.index;
974
975           if (pool->buffers[index] == NULL) {
976             GST_LOG_OBJECT (pool, "buffer %u not queued, putting on free list",
977                 index);
978
979             /* reset to the full length, in case it was changed */
980             gst_buffer_resize (buffer, 0, meta->vbuffer.length);
981
982             /* playback, put the buffer back in the queue to refill later. */
983             GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool,
984                 buffer);
985           } else {
986             /* the buffer is queued in the device but maybe not played yet. We just
987              * leave it there and not make it available for future calls to acquire
988              * for now. The buffer will be dequeued and reused later. */
989             GST_LOG_OBJECT (pool, "buffer %u is queued", index);
990           }
991           break;
992         }
993
994         case GST_V4L2_IO_USERPTR:
995         default:
996           g_assert_not_reached ();
997           break;
998       }
999       break;
1000
1001     default:
1002       g_assert_not_reached ();
1003       break;
1004   }
1005 }
1006
1007 static void
1008 gst_v4l2_buffer_pool_finalize (GObject * object)
1009 {
1010   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
1011
1012   gst_v4l2_buffer_pool_free_buffers (pool);
1013
1014   if (pool->video_fd >= 0)
1015     v4l2_close (pool->video_fd);
1016   if (pool->allocator)
1017     gst_object_unref (pool->allocator);
1018   g_free (pool->buffers);
1019
1020   gst_object_unref (pool->obj->element);
1021
1022   G_OBJECT_CLASS (parent_class)->finalize (object);
1023 }
1024
1025 static void
1026 gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool)
1027 {
1028 }
1029
1030 static void
1031 gst_v4l2_buffer_pool_class_init (GstV4l2BufferPoolClass * klass)
1032 {
1033   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1034   GstBufferPoolClass *bufferpool_class = GST_BUFFER_POOL_CLASS (klass);
1035
1036   object_class->finalize = gst_v4l2_buffer_pool_finalize;
1037
1038   bufferpool_class->start = gst_v4l2_buffer_pool_start;
1039   bufferpool_class->stop = gst_v4l2_buffer_pool_stop;
1040   bufferpool_class->set_config = gst_v4l2_buffer_pool_set_config;
1041   bufferpool_class->alloc_buffer = gst_v4l2_buffer_pool_alloc_buffer;
1042   bufferpool_class->acquire_buffer = gst_v4l2_buffer_pool_acquire_buffer;
1043   bufferpool_class->release_buffer = gst_v4l2_buffer_pool_release_buffer;
1044   bufferpool_class->free_buffer = gst_v4l2_buffer_pool_free_buffer;
1045 }
1046
1047 /**
1048  * gst_v4l2_buffer_pool_new:
1049  * @obj:  the v4l2 object owning the pool
1050  *
1051  * Construct a new buffer pool.
1052  *
1053  * Returns: the new pool, use gst_object_unref() to free resources
1054  */
1055 GstBufferPool *
1056 gst_v4l2_buffer_pool_new (GstV4l2Object * obj, GstCaps * caps)
1057 {
1058   GstV4l2BufferPool *pool;
1059   GstStructure *s;
1060   gint fd;
1061
1062   fd = v4l2_dup (obj->video_fd);
1063   if (fd < 0)
1064     goto dup_failed;
1065
1066   pool = (GstV4l2BufferPool *) g_object_new (GST_TYPE_V4L2_BUFFER_POOL, NULL);
1067   pool->video_fd = fd;
1068   pool->obj = obj;
1069   pool->can_alloc = TRUE;
1070
1071   s = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (pool));
1072   gst_buffer_pool_config_set_params (s, caps, obj->sizeimage, 2, 0);
1073   gst_buffer_pool_set_config (GST_BUFFER_POOL_CAST (pool), s);
1074
1075   gst_object_ref (obj->element);
1076
1077   return GST_BUFFER_POOL (pool);
1078
1079   /* ERRORS */
1080 dup_failed:
1081   {
1082     GST_DEBUG ("failed to dup fd %d (%s)", errno, g_strerror (errno));
1083     return NULL;
1084   }
1085 }
1086
1087 static GstFlowReturn
1088 gst_v4l2_do_read (GstV4l2BufferPool * pool, GstBuffer * buf)
1089 {
1090   GstFlowReturn res;
1091   GstV4l2Object *obj = pool->obj;
1092   gint amount;
1093   GstMapInfo map;
1094   gint toread;
1095
1096   toread = obj->sizeimage;
1097
1098   GST_LOG_OBJECT (pool, "reading %d bytes into buffer %p", toread, buf);
1099
1100   gst_buffer_map (buf, &map, GST_MAP_WRITE);
1101
1102   do {
1103     if ((res = gst_v4l2_object_poll (obj)) != GST_FLOW_OK)
1104       goto poll_error;
1105
1106     amount = v4l2_read (obj->video_fd, map.data, toread);
1107
1108     if (amount == toread) {
1109       break;
1110     } else if (amount == -1) {
1111       if (errno == EAGAIN || errno == EINTR) {
1112         continue;
1113       } else
1114         goto read_error;
1115     } else {
1116       /* short reads can happen if a signal interrupts the read */
1117       continue;
1118     }
1119   } while (TRUE);
1120
1121   GST_LOG_OBJECT (pool, "read %d bytes", amount);
1122   gst_buffer_unmap (buf, &map);
1123   gst_buffer_resize (buf, 0, amount);
1124
1125   return GST_FLOW_OK;
1126
1127   /* ERRORS */
1128 poll_error:
1129   {
1130     GST_DEBUG ("poll error %s", gst_flow_get_name (res));
1131     goto cleanup;
1132   }
1133 read_error:
1134   {
1135     GST_ELEMENT_ERROR (obj->element, RESOURCE, READ,
1136         (_("Error reading %d bytes from device '%s'."),
1137             toread, obj->videodev), GST_ERROR_SYSTEM);
1138     res = GST_FLOW_ERROR;
1139     goto cleanup;
1140   }
1141 cleanup:
1142   {
1143     gst_buffer_unmap (buf, &map);
1144     gst_buffer_resize (buf, 0, 0);
1145     return res;
1146   }
1147 }
1148
1149 /**
1150  * gst_v4l2_buffer_pool_process:
1151  * @bpool: a #GstBufferPool
1152  * @buf: a #GstBuffer
1153  *
1154  * Process @buf in @bpool. For capture devices, this functions fills @buf with
1155  * data from the device. For output devices, this functions send the contents of
1156  * @buf to the device for playback.
1157  *
1158  * Returns: %GST_FLOW_OK on success.
1159  */
1160 GstFlowReturn
1161 gst_v4l2_buffer_pool_process (GstV4l2BufferPool * pool, GstBuffer * buf)
1162 {
1163   GstFlowReturn ret = GST_FLOW_OK;
1164   GstBufferPool *bpool = GST_BUFFER_POOL_CAST (pool);
1165   GstV4l2Object *obj = pool->obj;
1166
1167   GST_DEBUG_OBJECT (pool, "process buffer %p", buf);
1168
1169   switch (obj->type) {
1170     case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1171       /* capture */
1172       switch (obj->mode) {
1173         case GST_V4L2_IO_RW:
1174           /* capture into the buffer */
1175           ret = gst_v4l2_do_read (pool, buf);
1176           break;
1177
1178         case GST_V4L2_IO_MMAP:
1179         {
1180           GstBuffer *tmp;
1181
1182           if (buf->pool == bpool)
1183             /* nothing, data was inside the buffer when we did _acquire() */
1184             goto done;
1185
1186           /* buffer not from our pool, grab a frame and copy it into the target */
1187           if ((ret = gst_v4l2_buffer_pool_dqbuf (pool, &tmp)) != GST_FLOW_OK)
1188             goto done;
1189
1190           if (!gst_v4l2_object_copy (obj, buf, tmp))
1191             goto copy_failed;
1192
1193           /* an queue the buffer again after the copy */
1194           if ((ret = gst_v4l2_buffer_pool_qbuf (pool, tmp)) != GST_FLOW_OK)
1195             goto done;
1196           break;
1197         }
1198
1199         case GST_V4L2_IO_USERPTR:
1200         default:
1201           g_assert_not_reached ();
1202           break;
1203       }
1204       break;
1205
1206     case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1207       /* playback */
1208       switch (obj->mode) {
1209         case GST_V4L2_IO_RW:
1210           /* FIXME, do write() */
1211           GST_WARNING_OBJECT (pool, "implement write()");
1212           break;
1213         case GST_V4L2_IO_DMABUF:
1214         case GST_V4L2_IO_MMAP:
1215         {
1216           GstBuffer *to_queue;
1217
1218           if (buf->pool == bpool) {
1219             /* nothing, we can queue directly */
1220             to_queue = gst_buffer_ref (buf);
1221             GST_LOG_OBJECT (pool, "processing buffer from our pool");
1222           } else {
1223             GST_LOG_OBJECT (pool, "alloc buffer from our pool");
1224             if (!gst_buffer_pool_is_active (bpool)) {
1225               GstStructure *config;
1226
1227               /* this pool was not activated, configure and activate */
1228               GST_DEBUG_OBJECT (pool, "activating pool");
1229
1230               config = gst_buffer_pool_get_config (bpool);
1231               gst_buffer_pool_config_add_option (config,
1232                   GST_BUFFER_POOL_OPTION_VIDEO_META);
1233               gst_buffer_pool_set_config (bpool, config);
1234
1235               if (!gst_buffer_pool_set_active (bpool, TRUE))
1236                 goto activate_failed;
1237             }
1238
1239             /* this can block if all buffers are outstanding which would be
1240              * strange because we would expect the upstream element to have
1241              * allocated them and returned to us.. */
1242             ret = gst_buffer_pool_acquire_buffer (bpool, &to_queue, NULL);
1243             if (ret != GST_FLOW_OK)
1244               goto acquire_failed;
1245
1246             /* copy into it and queue */
1247             if (!gst_v4l2_object_copy (obj, to_queue, buf))
1248               goto copy_failed;
1249           }
1250
1251           if ((ret = gst_v4l2_buffer_pool_qbuf (pool, to_queue)) != GST_FLOW_OK)
1252             goto done;
1253
1254           /* if we are not streaming yet (this is the first buffer, start
1255            * streaming now */
1256           if (!pool->streaming)
1257             if (!start_streaming (pool))
1258               goto start_failed;
1259
1260           if (pool->num_queued == pool->num_allocated) {
1261             GstBuffer *out;
1262             /* all buffers are queued, try to dequeue one and release it back
1263              * into the pool so that _acquire can get to it again. */
1264             ret = gst_v4l2_buffer_pool_dqbuf (pool, &out);
1265             if (ret != GST_FLOW_OK)
1266               goto done;
1267
1268             /* release the rendered buffer back into the pool. This wakes up any
1269              * thread waiting for a buffer in _acquire(). If the buffer still has
1270              * a pool then this will happen when the refcount reaches 0 */
1271             if (!out->pool)
1272               gst_v4l2_buffer_pool_release_buffer (bpool, out);
1273           }
1274           gst_buffer_unref (to_queue);
1275           break;
1276         }
1277
1278         case GST_V4L2_IO_USERPTR:
1279         default:
1280           g_assert_not_reached ();
1281           break;
1282       }
1283       break;
1284     default:
1285       g_assert_not_reached ();
1286       break;
1287   }
1288 done:
1289   return ret;
1290
1291   /* ERRORS */
1292 activate_failed:
1293   {
1294     GST_ERROR_OBJECT (obj->element, "failed to activate pool");
1295     return GST_FLOW_ERROR;
1296   }
1297 acquire_failed:
1298   {
1299     GST_WARNING_OBJECT (obj->element, "failed to acquire a buffer: %s",
1300         gst_flow_get_name (ret));
1301     return ret;
1302   }
1303 copy_failed:
1304   {
1305     GST_ERROR_OBJECT (obj->element, "failed to copy data");
1306     return GST_FLOW_ERROR;
1307   }
1308 start_failed:
1309   {
1310     GST_ERROR_OBJECT (obj->element, "failed to start streaming");
1311     return GST_FLOW_ERROR;
1312   }
1313 }