bufferpool: improve _new function
[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., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, 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
33 #include "gst/video/video.h"
34 #include "gst/video/gstmetavideo.h"
35
36 #include <gstv4l2bufferpool.h>
37
38 #include "gstv4l2src.h"
39 #include "gstv4l2sink.h"
40 #include "v4l2_calls.h"
41 #include "gst/gst-i18n-plugin.h"
42
43 /* videodev2.h is not versioned and we can't easily check for the presence
44  * of enum values at compile time, but the V4L2_CAP_VIDEO_OUTPUT_OVERLAY define
45  * was added in the same commit as V4L2_FIELD_INTERLACED_{TB,BT} (b2787845) */
46 #ifndef V4L2_CAP_VIDEO_OUTPUT_OVERLAY
47 #define V4L2_FIELD_INTERLACED_TB 8
48 #define V4L2_FIELD_INTERLACED_BT 9
49 #endif
50
51
52 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
53 #define GST_CAT_DEFAULT v4l2_debug
54
55 /*
56  * GstV4l2Buffer:
57  */
58 const GstMetaInfo *
59 gst_meta_v4l2_get_info (void)
60 {
61   static const GstMetaInfo *meta_info = NULL;
62
63   if (meta_info == NULL) {
64     meta_info =
65         gst_meta_register ("GstMetaV4l2", "GstMetaV4l2",
66         sizeof (GstMetaV4l2), (GstMetaInitFunction) NULL,
67         (GstMetaFreeFunction) NULL, (GstMetaCopyFunction) NULL,
68         (GstMetaTransformFunction) NULL);
69   }
70   return meta_info;
71 }
72
73 /*
74  * GstV4l2BufferPool:
75  */
76 #define gst_v4l2_buffer_pool_parent_class parent_class
77 G_DEFINE_TYPE (GstV4l2BufferPool, gst_v4l2_buffer_pool, GST_TYPE_BUFFER_POOL);
78
79 static void gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool,
80     GstBuffer * buffer);
81
82 static void
83 gst_v4l2_buffer_pool_free_buffer (GstBufferPool * bpool, GstBuffer * buffer)
84 {
85   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
86   gint index;
87   GstMetaV4l2 *meta;
88   GstV4l2Object *obj;
89
90   meta = GST_META_V4L2_GET (buffer);
91   g_assert (meta != NULL);
92
93   obj = pool->obj;
94
95   index = meta->vbuffer.index;
96   GST_LOG_OBJECT (pool, "finalizing buffer %p %d", buffer, index);
97   pool->buffers[index] = NULL;
98
99   GST_LOG_OBJECT (pool,
100       "buffer %p (data %p, len %u) freed, unmapping",
101       buffer, meta->mem, meta->vbuffer.length);
102   v4l2_munmap (meta->mem, meta->vbuffer.length);
103
104   gst_buffer_unref (buffer);
105 }
106
107 static GstFlowReturn
108 gst_v4l2_buffer_pool_alloc_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
109     GstBufferPoolParams * params)
110 {
111   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
112   GstBuffer *newbuf;
113   GstMetaV4l2 *meta;
114   GstV4l2Object *obj;
115   GstVideoInfo *info;
116   guint index;
117
118   obj = pool->obj;
119   info = &obj->info;
120
121   newbuf = gst_buffer_new ();
122   meta = GST_META_V4L2_ADD (newbuf);
123
124   index = pool->index;
125
126   GST_LOG_OBJECT (pool, "creating buffer %u, %p", index, newbuf, pool);
127
128   meta->vbuffer.index = index;
129   meta->vbuffer.type = obj->type;
130   meta->vbuffer.memory = V4L2_MEMORY_MMAP;
131
132   if (v4l2_ioctl (pool->video_fd, VIDIOC_QUERYBUF, &meta->vbuffer) < 0)
133     goto querybuf_failed;
134
135   GST_LOG_OBJECT (pool, "  index:     %u", meta->vbuffer.index);
136   GST_LOG_OBJECT (pool, "  type:      %d", meta->vbuffer.type);
137   GST_LOG_OBJECT (pool, "  bytesused: %u", meta->vbuffer.bytesused);
138   GST_LOG_OBJECT (pool, "  flags:     %08x", meta->vbuffer.flags);
139   GST_LOG_OBJECT (pool, "  field:     %d", meta->vbuffer.field);
140   GST_LOG_OBJECT (pool, "  memory:    %d", meta->vbuffer.memory);
141   if (meta->vbuffer.memory == V4L2_MEMORY_MMAP)
142     GST_LOG_OBJECT (pool, "  MMAP offset:  %u", meta->vbuffer.m.offset);
143   GST_LOG_OBJECT (pool, "  length:    %u", meta->vbuffer.length);
144   GST_LOG_OBJECT (pool, "  input:     %u", meta->vbuffer.input);
145
146   meta->mem = v4l2_mmap (0, meta->vbuffer.length,
147       PROT_READ | PROT_WRITE, MAP_SHARED, pool->video_fd,
148       meta->vbuffer.m.offset);
149   if (meta->mem == MAP_FAILED)
150     goto mmap_failed;
151
152   gst_buffer_take_memory (newbuf, -1,
153       gst_memory_new_wrapped (0,
154           meta->mem, NULL, meta->vbuffer.length, 0, meta->vbuffer.length));
155
156   /* add metadata to raw video buffers */
157   if (info->finfo) {
158     gsize offset[GST_VIDEO_MAX_PLANES];
159     gint stride[GST_VIDEO_MAX_PLANES];
160
161     offset[0] = 0;
162     stride[0] = obj->bytesperline;
163
164     GST_DEBUG_OBJECT (pool, "adding video meta");
165     gst_buffer_add_meta_video_full (newbuf, info->flags,
166         GST_VIDEO_INFO_FORMAT (info), GST_VIDEO_INFO_WIDTH (info),
167         GST_VIDEO_INFO_HEIGHT (info), GST_VIDEO_INFO_N_PLANES (info),
168         offset, stride);
169   }
170
171   pool->index++;
172
173   *buffer = newbuf;
174
175   return GST_FLOW_OK;
176
177   /* ERRORS */
178 querybuf_failed:
179   {
180     gint errnosave = errno;
181
182     GST_WARNING ("Failed QUERYBUF: %s", g_strerror (errnosave));
183     gst_buffer_unref (newbuf);
184     errno = errnosave;
185     return GST_FLOW_ERROR;
186   }
187 mmap_failed:
188   {
189     gint errnosave = errno;
190
191     GST_WARNING ("Failed to mmap: %s", g_strerror (errnosave));
192     gst_buffer_unref (newbuf);
193     errno = errnosave;
194     return GST_FLOW_ERROR;
195   }
196 }
197
198 static gboolean
199 gst_v4l2_buffer_pool_set_config (GstBufferPool * bpool, GstStructure * config)
200 {
201   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
202   const GstCaps *caps;
203   guint size, min_buffers, max_buffers;
204   guint prefix, align;
205
206   GST_DEBUG_OBJECT (pool, "set config");
207
208   /* parse the config and keep around */
209   if (!gst_buffer_pool_config_get (config, &caps, &size, &min_buffers,
210           &max_buffers, &prefix, &align))
211     goto wrong_config;
212
213   GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
214
215   pool->min_buffers = min_buffers;
216   pool->max_buffers = max_buffers;
217
218   return TRUE;
219
220 wrong_config:
221   {
222     GST_WARNING_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
223     return FALSE;
224   }
225 }
226
227 static gboolean
228 gst_v4l2_buffer_pool_start (GstBufferPool * bpool)
229 {
230   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
231   GstV4l2Object *obj = pool->obj;
232   gint n;
233   struct v4l2_requestbuffers breq;
234   gint num_buffers;
235
236   num_buffers = pool->max_buffers;
237
238   /* first, lets request buffers, and see how many we can get: */
239   GST_DEBUG_OBJECT (pool, "starting, requesting %d MMAP buffers", num_buffers);
240
241   memset (&breq, 0, sizeof (struct v4l2_requestbuffers));
242   breq.type = obj->type;
243   breq.count = num_buffers;
244   breq.memory = V4L2_MEMORY_MMAP;
245
246   if (v4l2_ioctl (pool->video_fd, VIDIOC_REQBUFS, &breq) < 0)
247     goto reqbufs_failed;
248
249   GST_LOG_OBJECT (pool, " count:  %u", breq.count);
250   GST_LOG_OBJECT (pool, " type:   %d", breq.type);
251   GST_LOG_OBJECT (pool, " memory: %d", breq.memory);
252
253   if (breq.count < GST_V4L2_MIN_BUFFERS)
254     goto no_buffers;
255
256   if (num_buffers != breq.count) {
257     GST_WARNING_OBJECT (pool, "using %u buffers instead", breq.count);
258     num_buffers = breq.count;
259   }
260
261   pool->obj = obj;
262   pool->requeuebuf = (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ? TRUE : FALSE);
263   pool->num_buffers = num_buffers;
264   pool->buffers = g_new0 (GstBuffer *, num_buffers);
265   pool->index = 0;
266
267   /* now, map the buffers: */
268   for (n = 0; n < num_buffers; n++) {
269     GstBuffer *buffer;
270
271     if (gst_v4l2_buffer_pool_alloc_buffer (bpool, &buffer, NULL) != GST_FLOW_OK)
272       goto buffer_new_failed;
273
274     gst_v4l2_buffer_pool_release_buffer (bpool, buffer);
275   }
276   return TRUE;
277
278   /* ERRORS */
279 reqbufs_failed:
280   {
281     GST_ERROR_OBJECT (pool,
282         "error requesting %d buffers: %s", num_buffers, g_strerror (errno));
283     return FALSE;
284   }
285 no_buffers:
286   {
287     GST_ERROR_OBJECT (pool,
288         "we received %d from device '%s', we want at least %d",
289         breq.count, obj->videodev, GST_V4L2_MIN_BUFFERS);
290     return FALSE;
291   }
292 buffer_new_failed:
293   {
294     GST_ERROR_OBJECT (pool, "failed to create a buffer");
295     return FALSE;
296   }
297 }
298
299 static gboolean
300 gst_v4l2_buffer_pool_stop (GstBufferPool * bpool)
301 {
302   gboolean ret;
303   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
304   guint n;
305
306   GST_DEBUG_OBJECT (pool, "stopping pool");
307
308   /* first free the buffers in the queue */
309   ret = GST_BUFFER_POOL_CLASS (parent_class)->stop (bpool);
310
311   /* then free the remaining buffers */
312   for (n = 0; n < pool->num_buffers; n++) {
313     if (pool->buffers[n])
314       gst_v4l2_buffer_pool_free_buffer (bpool, pool->buffers[n]);
315   }
316   return ret;
317 }
318
319 static GstFlowReturn
320 gst_v4l2_buffer_pool_dqbuf (GstBufferPool * bpool, GstBuffer ** buffer)
321 {
322   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
323   GstBuffer *outbuf;
324   struct v4l2_buffer vbuffer;
325   GstV4l2Object *obj = pool->obj;
326
327   memset (&vbuffer, 0x00, sizeof (vbuffer));
328   vbuffer.type = obj->type;
329   vbuffer.memory = V4L2_MEMORY_MMAP;
330
331   if (v4l2_ioctl (pool->video_fd, VIDIOC_DQBUF, &vbuffer) < 0)
332     goto error;
333
334   /* get our GstBuffer with that index from the pool, if the buffer was
335    * outstanding we have a serious problem.
336    */
337   outbuf = pool->buffers[vbuffer.index];
338   if (outbuf == NULL)
339     goto no_buffers;
340
341   /* mark the buffer outstanding */
342   pool->buffers[vbuffer.index] = NULL;
343
344   GST_LOG_OBJECT (pool,
345       "dequeued frame %d (ix=%d), used %d, flags %08x, pool-queued=%d, buffer=%p",
346       vbuffer.sequence, vbuffer.index, vbuffer.bytesused, vbuffer.flags,
347       pool->num_queued, outbuf);
348
349   pool->num_queued--;
350   GST_DEBUG_OBJECT (pool, "num_queued: %d", pool->num_queued);
351
352   /* set top/bottom field first if v4l2_buffer has the information */
353   if (vbuffer.field == V4L2_FIELD_INTERLACED_TB)
354     GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_TFF);
355   if (vbuffer.field == V4L2_FIELD_INTERLACED_BT)
356     GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_TFF);
357
358   /* this can change at every frame, esp. with jpeg */
359   if (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
360     gst_buffer_resize (outbuf, 0, vbuffer.bytesused);
361   else
362     gst_buffer_resize (outbuf, 0, vbuffer.length);
363
364   *buffer = outbuf;
365
366   return GST_FLOW_OK;
367
368   /* ERRORS */
369 error:
370   {
371     GST_WARNING_OBJECT (pool,
372         "problem dequeuing frame %d (ix=%d), pool-ct=%d, buf.flags=%d",
373         vbuffer.sequence, vbuffer.index,
374         GST_MINI_OBJECT_REFCOUNT (pool), vbuffer.flags);
375
376     switch (errno) {
377       case EAGAIN:
378         GST_WARNING_OBJECT (pool,
379             "Non-blocking I/O has been selected using O_NONBLOCK and"
380             " no buffer was in the outgoing queue. device %s", obj->videodev);
381         break;
382       case EINVAL:
383         GST_ERROR_OBJECT (pool,
384             "The buffer type is not supported, or the index is out of bounds, "
385             "or no buffers have been allocated yet, or the userptr "
386             "or length are invalid. device %s", obj->videodev);
387         break;
388       case ENOMEM:
389         GST_ERROR_OBJECT (pool,
390             "insufficient memory to enqueue a user pointer buffer");
391         break;
392       case EIO:
393         GST_INFO_OBJECT (pool,
394             "VIDIOC_DQBUF failed due to an internal error."
395             " Can also indicate temporary problems like signal loss."
396             " Note the driver might dequeue an (empty) buffer despite"
397             " returning an error, or even stop capturing."
398             " device %s", obj->videodev);
399         /* have we de-queued a buffer ? */
400         if (!(vbuffer.flags & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))) {
401           GST_DEBUG_OBJECT (pool, "reenqueing buffer");
402           /* FIXME ... should we do something here? */
403         }
404         break;
405       case EINTR:
406         GST_WARNING_OBJECT (pool,
407             "could not sync on a buffer on device %s", obj->videodev);
408         break;
409       default:
410         GST_WARNING_OBJECT (pool,
411             "Grabbing frame got interrupted on %s unexpectedly. %d: %s.",
412             obj->videodev, errno, g_strerror (errno));
413         break;
414     }
415     return GST_FLOW_ERROR;
416   }
417 no_buffers:
418   {
419     GST_ERROR_OBJECT (pool, "No free buffers found in the pool at index %d.",
420         vbuffer.index);
421     return GST_FLOW_ERROR;
422   }
423 }
424
425 static GstFlowReturn
426 gst_v4l2_buffer_pool_acquire_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
427     GstBufferPoolParams * params)
428 {
429   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
430   GstFlowReturn ret;
431
432   GST_DEBUG_OBJECT (pool, "acquire");
433
434   if (GST_BUFFER_POOL_IS_FLUSHING (bpool))
435     goto flushing;
436
437   if (pool->requeuebuf)
438     ret = gst_v4l2_buffer_pool_dqbuf (bpool, buffer);
439   else {
440     if (pool->num_queued == pool->num_buffers) {
441       ret = gst_v4l2_buffer_pool_dqbuf (bpool, buffer);
442     }
443     ret =
444         GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool, buffer,
445         params);
446   }
447
448   return ret;
449
450   /* ERRORS */
451 flushing:
452   {
453     GST_DEBUG_OBJECT (bpool, "We are flushing");
454     return GST_FLOW_WRONG_STATE;
455   }
456 }
457
458 static void
459 gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool, GstBuffer * buffer)
460 {
461   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
462
463   GST_DEBUG_OBJECT (pool, "release");
464
465   if (pool->requeuebuf)
466     gst_v4l2_buffer_pool_qbuf (bpool, buffer);
467   else
468     GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool, buffer);
469 }
470
471 static void
472 gst_v4l2_buffer_pool_finalize (GObject * object)
473 {
474   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
475
476   if (pool->video_fd >= 0)
477     v4l2_close (pool->video_fd);
478
479   if (pool->buffers) {
480     g_free (pool->buffers);
481     pool->buffers = NULL;
482   }
483
484   G_OBJECT_CLASS (parent_class)->finalize (object);
485 }
486
487 static void
488 gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool)
489 {
490 }
491
492 static void
493 gst_v4l2_buffer_pool_class_init (GstV4l2BufferPoolClass * klass)
494 {
495   GObjectClass *object_class = G_OBJECT_CLASS (klass);
496   GstBufferPoolClass *bufferpool_class = GST_BUFFER_POOL_CLASS (klass);
497
498   object_class->finalize = gst_v4l2_buffer_pool_finalize;
499
500   bufferpool_class->start = gst_v4l2_buffer_pool_start;
501   bufferpool_class->stop = gst_v4l2_buffer_pool_stop;
502   bufferpool_class->set_config = gst_v4l2_buffer_pool_set_config;
503   bufferpool_class->alloc_buffer = gst_v4l2_buffer_pool_alloc_buffer;
504   bufferpool_class->acquire_buffer = gst_v4l2_buffer_pool_acquire_buffer;
505   bufferpool_class->release_buffer = gst_v4l2_buffer_pool_release_buffer;
506   bufferpool_class->free_buffer = gst_v4l2_buffer_pool_free_buffer;
507 }
508
509 /**
510  * gst_v4l2_buffer_pool_new:
511  * @obj:  the v4l2 object owning the pool
512  * @num_buffers:  the requested number of buffers in the pool
513  * @requeuebuf: if %TRUE, and if the pool is still in the running state, a
514  *  buffer with no remaining references is immediately passed back to v4l2
515  *  (VIDIOC_QBUF), otherwise it is returned to the pool of available buffers
516  *  (which can be accessed via gst_v4l2_buffer_pool_get().
517  *
518  * Construct a new buffer pool.
519  *
520  * Returns: the new pool, use gst_v4l2_buffer_pool_destroy() to free resources
521  */
522 GstBufferPool *
523 gst_v4l2_buffer_pool_new (GstV4l2Object * obj)
524 {
525   GstV4l2BufferPool *pool;
526   gint fd;
527
528   fd = v4l2_dup (obj->video_fd);
529   if (fd < 0)
530     goto dup_failed;
531
532   pool = (GstV4l2BufferPool *) g_object_new (GST_TYPE_V4L2_BUFFER_POOL, NULL);
533   pool->video_fd = fd;
534   pool->obj = obj;
535
536   return GST_BUFFER_POOL_CAST (pool);
537
538   /* ERRORS */
539 dup_failed:
540   {
541     GST_DEBUG ("failed to dup fd %d (%s)", errno, g_strerror (errno));
542     return NULL;
543   }
544 }
545
546 /**
547  * gst_v4l2_buffer_pool_qbuf:
548  * @pool: the pool
549  * @buf: the buffer to queue
550  *
551  * Queue a buffer to the driver
552  *
553  * Returns: %TRUE for success
554  */
555 gboolean
556 gst_v4l2_buffer_pool_qbuf (GstBufferPool * bpool, GstBuffer * buf)
557 {
558   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
559   GstMetaV4l2 *meta;
560   gint index;
561
562   meta = GST_META_V4L2_GET (buf);
563   g_assert (meta != NULL);
564
565   index = meta->vbuffer.index;
566
567   GST_LOG_OBJECT (pool, "enqueue pool buffer %d", index);
568
569   if (pool->buffers[index] != NULL)
570     goto already_queued;
571
572   if (v4l2_ioctl (pool->video_fd, VIDIOC_QBUF, &meta->vbuffer) < 0)
573     goto queue_failed;
574
575   pool->buffers[index] = buf;
576
577   pool->num_queued++;
578   GST_DEBUG_OBJECT (pool, "num_queued: %d", pool->num_queued);
579
580   return TRUE;
581
582   /* ERRORS */
583 already_queued:
584   {
585     GST_WARNING_OBJECT (pool, "the buffer was already queued");
586     return FALSE;
587   }
588 queue_failed:
589   {
590     GST_WARNING_OBJECT (pool, "could not queue a buffer");
591     return FALSE;
592   }
593 }
594
595 /**
596  * gst_v4l2_buffer_pool_available_buffers:
597  * @pool: the pool
598  *
599  * Check the number of buffers available to the driver, ie. buffers that
600  * have been QBUF'd but not yet DQBUF'd.
601  *
602  * Returns: the number of buffers available.
603  */
604 gint
605 gst_v4l2_buffer_pool_available_buffers (GstBufferPool * bpool)
606 {
607   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
608
609   return pool->num_queued;
610 }