v4l2: Add v4l2sink element
[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  * gstv4l2src.h: BT8x8/V4L2 source element
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 <gstv4l2bufferpool.h>
34 #include "gstv4l2src.h"
35 #include "gstv4l2sink.h"
36 #include "v4l2_calls.h"
37 #include "gst/gst-i18n-plugin.h"
38
39
40 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
41 #define GST_CAT_DEFAULT v4l2_debug
42
43
44 /*
45  * GstV4l2Buffer:
46  */
47
48 static GstBufferClass *v4l2buffer_parent_class = NULL;
49
50 static void
51 gst_v4l2_buffer_finalize (GstV4l2Buffer * buffer)
52 {
53   GstV4l2BufferPool *pool;
54   gboolean resuscitated = FALSE;
55   gint index;
56
57   pool = buffer->pool;
58
59   index = buffer->vbuffer.index;
60
61   GST_LOG_OBJECT (pool->v4l2elem, "finalizing buffer %p %d", buffer, index);
62
63   GST_V4L2_BUFFER_POOL_LOCK (pool);
64   if (GST_BUFFER_SIZE (buffer) != 0) {
65     /* BUFFER_SIZE is only set if the frame was dequeued */
66     pool->num_live_buffers--;
67     GST_DEBUG_OBJECT (pool->v4l2elem, "num_live_buffers--: %d",
68         pool->num_live_buffers);
69   }
70
71   if (pool->running) {
72     if (pool->requeuebuf) {
73       if (!gst_v4l2_buffer_pool_qbuf (pool, buffer)) {
74         GST_WARNING ("could not requeue buffer %p %d", buffer, index);
75       } else {
76         resuscitated = TRUE;
77       }
78     } else {
79       resuscitated = TRUE;
80       /* XXX double check this... I think it is ok to not synchronize this
81        * w.r.t. destruction of the pool, since the buffer is still live and
82        * the buffer holds a ref to the pool..
83        */
84       g_async_queue_push (pool->avail_buffers, buffer);
85     }
86   } else {
87     GST_LOG_OBJECT (pool->v4l2elem, "the pool is shutting down");
88   }
89
90   if (resuscitated) {
91     /* FIXME: check that the caps didn't change */
92     GST_LOG_OBJECT (pool->v4l2elem, "reviving buffer %p, %d", buffer, index);
93     gst_buffer_ref (GST_BUFFER (buffer));
94     GST_BUFFER_SIZE (buffer) = 0;
95     pool->buffers[index] = buffer;
96   }
97
98   GST_V4L2_BUFFER_POOL_UNLOCK (pool);
99
100   if (!resuscitated) {
101     GST_LOG_OBJECT (pool->v4l2elem, "buffer %p not recovered, unmapping",
102         buffer);
103     gst_mini_object_unref (GST_MINI_OBJECT (pool));
104     v4l2_munmap ((void *) GST_BUFFER_DATA (buffer), buffer->vbuffer.length);
105
106     GST_MINI_OBJECT_CLASS (v4l2buffer_parent_class)->finalize (GST_MINI_OBJECT
107         (buffer));
108   }
109 }
110
111 static void
112 gst_v4l2_buffer_class_init (gpointer g_class, gpointer class_data)
113 {
114   GstMiniObjectClass *mini_object_class = GST_MINI_OBJECT_CLASS (g_class);
115
116   v4l2buffer_parent_class = g_type_class_peek_parent (g_class);
117
118   mini_object_class->finalize = (GstMiniObjectFinalizeFunction)
119       gst_v4l2_buffer_finalize;
120 }
121
122 GType
123 gst_v4l2_buffer_get_type (void)
124 {
125   static GType _gst_v4l2_buffer_type;
126
127   if (G_UNLIKELY (_gst_v4l2_buffer_type == 0)) {
128     static const GTypeInfo v4l2_buffer_info = {
129       sizeof (GstBufferClass),
130       NULL,
131       NULL,
132       gst_v4l2_buffer_class_init,
133       NULL,
134       NULL,
135       sizeof (GstV4l2Buffer),
136       0,
137       NULL,
138       NULL
139     };
140     _gst_v4l2_buffer_type = g_type_register_static (GST_TYPE_BUFFER,
141         "GstV4l2Buffer", &v4l2_buffer_info, 0);
142   }
143   return _gst_v4l2_buffer_type;
144 }
145
146 static GstV4l2Buffer *
147 gst_v4l2_buffer_new (GstV4l2BufferPool * pool, guint index, GstCaps * caps)
148 {
149   GstV4l2Buffer *ret;
150   guint8 *data;
151
152   ret = (GstV4l2Buffer *) gst_mini_object_new (GST_TYPE_V4L2_BUFFER);
153
154   GST_LOG_OBJECT (pool->v4l2elem, "creating buffer %u, %p in pool %p", index,
155       ret, pool);
156
157   ret->pool =
158       (GstV4l2BufferPool *) gst_mini_object_ref (GST_MINI_OBJECT (pool));
159
160   ret->vbuffer.index = index;
161   ret->vbuffer.type = pool->type;
162   ret->vbuffer.memory = V4L2_MEMORY_MMAP;
163
164   if (v4l2_ioctl (pool->video_fd, VIDIOC_QUERYBUF, &ret->vbuffer) < 0)
165     goto querybuf_failed;
166
167   GST_LOG_OBJECT (pool->v4l2elem, "  index:     %u", ret->vbuffer.index);
168   GST_LOG_OBJECT (pool->v4l2elem, "  type:      %d", ret->vbuffer.type);
169   GST_LOG_OBJECT (pool->v4l2elem, "  bytesused: %u", ret->vbuffer.bytesused);
170   GST_LOG_OBJECT (pool->v4l2elem, "  flags:     %08x", ret->vbuffer.flags);
171   GST_LOG_OBJECT (pool->v4l2elem, "  field:     %d", ret->vbuffer.field);
172   GST_LOG_OBJECT (pool->v4l2elem, "  memory:    %d", ret->vbuffer.memory);
173   if (ret->vbuffer.memory == V4L2_MEMORY_MMAP)
174     GST_LOG_OBJECT (pool->v4l2elem, "  MMAP offset:  %u",
175         ret->vbuffer.m.offset);
176   GST_LOG_OBJECT (pool->v4l2elem, "  length:    %u", ret->vbuffer.length);
177   GST_LOG_OBJECT (pool->v4l2elem, "  input:     %u", ret->vbuffer.input);
178
179   data = (guint8 *) v4l2_mmap (0, ret->vbuffer.length,
180       PROT_READ | PROT_WRITE, MAP_SHARED, pool->video_fd,
181       ret->vbuffer.m.offset);
182
183   if (data == MAP_FAILED)
184     goto mmap_failed;
185
186   GST_BUFFER_DATA (ret) = data;
187   GST_BUFFER_SIZE (ret) = ret->vbuffer.length;
188
189   GST_BUFFER_FLAG_SET (ret, GST_BUFFER_FLAG_READONLY);
190
191   gst_buffer_set_caps (GST_BUFFER (ret), caps);
192
193   return ret;
194
195   /* ERRORS */
196 querybuf_failed:
197   {
198     gint errnosave = errno;
199
200     GST_WARNING ("Failed QUERYBUF: %s", g_strerror (errnosave));
201     gst_buffer_unref (GST_BUFFER (ret));
202     errno = errnosave;
203     return NULL;
204   }
205 mmap_failed:
206   {
207     gint errnosave = errno;
208
209     GST_WARNING ("Failed to mmap: %s", g_strerror (errnosave));
210     gst_buffer_unref (GST_BUFFER (ret));
211     errno = errnosave;
212     return NULL;
213   }
214 }
215
216
217 /*
218  * GstV4l2BufferPool:
219  */
220
221 static GstMiniObjectClass *buffer_pool_parent_class = NULL;
222
223 static void
224 gst_v4l2_buffer_pool_finalize (GstV4l2BufferPool * pool)
225 {
226   g_mutex_free (pool->lock);
227   pool->lock = NULL;
228
229   g_async_queue_unref (pool->avail_buffers);
230   pool->avail_buffers = NULL;
231
232   if (pool->video_fd >= 0)
233     v4l2_close (pool->video_fd);
234
235   if (pool->buffers) {
236     g_free (pool->buffers);
237     pool->buffers = NULL;
238   }
239
240   GST_MINI_OBJECT_CLASS (buffer_pool_parent_class)->finalize (GST_MINI_OBJECT
241       (pool));
242 }
243
244 static void
245 gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool, gpointer g_class)
246 {
247   pool->lock = g_mutex_new ();
248   pool->running = FALSE;
249   pool->num_live_buffers = 0;
250 }
251
252 static void
253 gst_v4l2_buffer_pool_class_init (gpointer g_class, gpointer class_data)
254 {
255   GstMiniObjectClass *mini_object_class = GST_MINI_OBJECT_CLASS (g_class);
256
257   buffer_pool_parent_class = g_type_class_peek_parent (g_class);
258
259   mini_object_class->finalize = (GstMiniObjectFinalizeFunction)
260       gst_v4l2_buffer_pool_finalize;
261 }
262
263 GType
264 gst_v4l2_buffer_pool_get_type (void)
265 {
266   static GType _gst_v4l2_buffer_pool_type;
267
268   if (G_UNLIKELY (_gst_v4l2_buffer_pool_type == 0)) {
269     static const GTypeInfo v4l2_buffer_pool_info = {
270       sizeof (GstBufferClass),
271       NULL,
272       NULL,
273       gst_v4l2_buffer_pool_class_init,
274       NULL,
275       NULL,
276       sizeof (GstV4l2BufferPool),
277       0,
278       (GInstanceInitFunc) gst_v4l2_buffer_pool_init,
279       NULL
280     };
281     _gst_v4l2_buffer_pool_type = g_type_register_static (GST_TYPE_MINI_OBJECT,
282         "GstV4l2BufferPool", &v4l2_buffer_pool_info, 0);
283   }
284   return _gst_v4l2_buffer_pool_type;
285 }
286
287
288 /* this is somewhat of a hack.. but better to keep the hack in
289  * one place than copy/pasting it around..
290  */
291 static GstV4l2Object *
292 get_v4l2_object (GstElement * v4l2elem)
293 {
294   GstV4l2Object *v4l2object = NULL;
295   if (GST_IS_V4L2SRC (v4l2elem)) {
296     v4l2object = (GST_V4L2SRC (v4l2elem))->v4l2object;
297   } else if (GST_IS_V4L2SINK (v4l2elem)) {
298     v4l2object = (GST_V4L2SINK (v4l2elem))->v4l2object;
299   } else {
300     GST_ERROR_OBJECT (v4l2elem, "unknown v4l2 element");
301   }
302   return v4l2object;
303 }
304
305
306
307 /**
308  * Construct a new buffer pool
309  *
310  * @v4l2elem  the v4l2 element (src or sink) that owns this pool
311  * @fd   the video device file descriptor
312  * @num_buffers  the requested number of buffers in the pool
313  * @caps  the caps to set on the buffer
314  * @requeuebuf  if <code>TRUE</code>, and if the pool is still in the
315  *   <code>running</code> state, a buffer with no remaining references
316  *   is immediately passed back to v4l2 (<code>VIDIOC_QBUF</code>),
317  *   otherwise it is returned to the pool of available buffers
318  *   (which can be accessed via <code>gst_v4l2_buffer_pool_get()</code>.
319  */
320 GstV4l2BufferPool *
321 gst_v4l2_buffer_pool_new (GstElement * v4l2elem, gint fd, gint num_buffers,
322     GstCaps * caps, gboolean requeuebuf, enum v4l2_buf_type type)
323 {
324   GstV4l2BufferPool *pool;
325   gint n;
326   struct v4l2_requestbuffers breq;
327
328   pool = (GstV4l2BufferPool *) gst_mini_object_new (GST_TYPE_V4L2_BUFFER_POOL);
329
330   pool->video_fd = v4l2_dup (fd);
331   if (pool->video_fd < 0)
332     goto dup_failed;
333
334
335   /* first, lets request buffers, and see how many we can get: */
336   GST_DEBUG_OBJECT (v4l2elem, "STREAMING, requesting %d MMAP buffers",
337       num_buffers);
338
339   memset (&breq, 0, sizeof (struct v4l2_requestbuffers));
340   breq.type = type;
341   breq.count = num_buffers;
342   breq.memory = V4L2_MEMORY_MMAP;
343
344   if (v4l2_ioctl (fd, VIDIOC_REQBUFS, &breq) < 0)
345     goto reqbufs_failed;
346
347   GST_LOG_OBJECT (v4l2elem, " count:  %u", breq.count);
348   GST_LOG_OBJECT (v4l2elem, " type:   %d", breq.type);
349   GST_LOG_OBJECT (v4l2elem, " memory: %d", breq.memory);
350
351   if (breq.count < GST_V4L2_MIN_BUFFERS)
352     goto no_buffers;
353
354   if (num_buffers != breq.count) {
355     GST_WARNING_OBJECT (v4l2elem, "using %u buffers instead", breq.count);
356     num_buffers = breq.count;
357   }
358
359   pool->v4l2elem = v4l2elem;
360   pool->requeuebuf = requeuebuf;
361   pool->type = type;
362   pool->buffer_count = num_buffers;
363   pool->buffers = g_new0 (GstV4l2Buffer *, num_buffers);
364   pool->avail_buffers = g_async_queue_new ();
365
366   /* now, map the buffers: */
367   for (n = 0; n < num_buffers; n++) {
368     pool->buffers[n] = gst_v4l2_buffer_new (pool, n, caps);
369     if (!pool->buffers[n])
370       goto buffer_new_failed;
371     g_async_queue_push (pool->avail_buffers, pool->buffers[n]);
372   }
373
374   return pool;
375
376   /* ERRORS */
377 dup_failed:
378   {
379     gint errnosave = errno;
380
381     gst_mini_object_unref (GST_MINI_OBJECT (pool));
382
383     errno = errnosave;
384
385     return NULL;
386   }
387 reqbufs_failed:
388   {
389     GstV4l2Object *v4l2object = get_v4l2_object (v4l2elem);
390     GST_ELEMENT_ERROR (v4l2elem, RESOURCE, READ,
391         (_("Could not get buffers from device '%s'."),
392             v4l2object->videodev),
393         ("error requesting %d buffers: %s", num_buffers, g_strerror (errno)));
394     return NULL;
395   }
396 no_buffers:
397   {
398     GstV4l2Object *v4l2object = get_v4l2_object (v4l2elem);
399     GST_ELEMENT_ERROR (v4l2elem, RESOURCE, READ,
400         (_("Could not get enough buffers from device '%s'."),
401             v4l2object->videodev),
402         ("we received %d from device '%s', we want at least %d",
403             breq.count, v4l2object->videodev, GST_V4L2_MIN_BUFFERS));
404     return NULL;
405   }
406 buffer_new_failed:
407   {
408     gint errnosave = errno;
409
410     gst_v4l2_buffer_pool_destroy (pool);
411
412     errno = errnosave;
413
414     return NULL;
415   }
416 }
417
418
419 void
420 gst_v4l2_buffer_pool_destroy (GstV4l2BufferPool * pool)
421 {
422   gint n;
423
424   GST_V4L2_BUFFER_POOL_LOCK (pool);
425   pool->running = FALSE;
426   GST_V4L2_BUFFER_POOL_UNLOCK (pool);
427
428   GST_DEBUG_OBJECT (pool->v4l2elem, "destroy pool");
429
430   /* after this point, no more buffers will be queued or dequeued; no buffer
431    * from pool->buffers that is NULL will be set to a buffer, and no buffer that
432    * is not NULL will be pushed out. */
433
434   /* miniobjects have no dispose, so they can't break ref-cycles, as buffers ref
435    * the pool, we need to unref the buffer to properly finalize te pool */
436   for (n = 0; n < pool->buffer_count; n++) {
437     GstBuffer *buf;
438
439     GST_V4L2_BUFFER_POOL_LOCK (pool);
440     buf = GST_BUFFER (pool->buffers[n]);
441     GST_V4L2_BUFFER_POOL_UNLOCK (pool);
442
443     if (buf)
444       /* we own the ref if the buffer is in pool->buffers; drop it. */
445       gst_buffer_unref (buf);
446   }
447
448   gst_mini_object_unref (GST_MINI_OBJECT (pool));
449 }
450
451 /**
452  * Get an available buffer in the pool
453  *
454  * @pool   the "this" object
455  * @blocking   if <code>TRUE</code>, then suspend until a buffer is available
456  */
457 GstV4l2Buffer *
458 gst_v4l2_buffer_pool_get (GstV4l2BufferPool * pool, gboolean blocking)
459 {
460   GstV4l2Buffer *buf = NULL;
461
462   do {
463     buf = g_async_queue_try_pop (pool->avail_buffers);
464
465     /* if there isn't a buffer avail, let's try to dequeue one:
466      */
467     if (blocking && !buf) {
468       GST_DEBUG_OBJECT (pool->v4l2elem, "No buffers available.. need to dqbuf");
469       buf = gst_v4l2_buffer_pool_dqbuf (pool);
470
471       /* note: if we get a buf, we don't want to use it directly (because
472        * someone else could still hold a ref).. but instead we release our
473        * reference to it, and if no one else holds a ref it will be returned
474        * to the pool of available buffers..  and if not, we keep looping.
475        */
476       if (buf) {
477         gst_buffer_unref (GST_BUFFER (buf));
478         buf = NULL;
479       }
480     } else {
481       break;
482     }
483   } while (1);
484
485   if (buf) {
486     pool->num_live_buffers++;
487     GST_DEBUG_OBJECT (pool->v4l2elem, "num_live_buffers++: %d",
488         pool->num_live_buffers);
489     GST_BUFFER_SIZE (buf) = buf->vbuffer.length;
490   }
491
492   pool->running = TRUE;
493
494
495   return buf;
496 }
497
498
499 /**
500  * Queue a buffer to the driver
501  *
502  * @pool   the "this" object
503  * @buf    the buffer to queue
504  */
505 gboolean
506 gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool * pool, GstV4l2Buffer * buf)
507 {
508   GST_LOG_OBJECT (pool->v4l2elem, "enqueue pool buffer %d", buf->vbuffer.index);
509
510   if (v4l2_ioctl (pool->video_fd, VIDIOC_QBUF, &buf->vbuffer) < 0)
511     return FALSE;
512
513   pool->num_live_buffers--;
514   GST_DEBUG_OBJECT (pool->v4l2elem, "num_live_buffers--: %d",
515       pool->num_live_buffers);
516
517   return TRUE;
518 }
519
520 /**
521  * Dequeue a buffer from the driver.  Some generic error handling is done in
522  * this function, but any error handling specific to v4l2src (capture) or
523  * v4l2sink (output) can be done outside this function by checking 'errno'
524  *
525  * @pool   the "this" object
526  */
527 GstV4l2Buffer *
528 gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool)
529 {
530   GstV4l2Object *v4l2object = get_v4l2_object (pool->v4l2elem);
531   GstV4l2Buffer *pool_buffer;
532   struct v4l2_buffer buffer;
533
534   memset (&buffer, 0x00, sizeof (buffer));
535   buffer.type = pool->type;
536   buffer.memory = V4L2_MEMORY_MMAP;
537
538
539   if (v4l2_ioctl (pool->video_fd, VIDIOC_DQBUF, &buffer) >= 0) {
540
541     GST_V4L2_BUFFER_POOL_LOCK (pool);
542
543     /* get our GstBuffer with that index from the pool, if the buffer was
544      * outstanding we have a serious problem.
545      */
546     pool_buffer = pool->buffers[buffer.index];
547
548     if (pool_buffer == NULL) {
549       GST_ELEMENT_ERROR (pool->v4l2elem, RESOURCE, FAILED,
550           (_("Failed trying to get video frames from device '%s'."),
551               v4l2object->videodev),
552           (_("No free buffers found in the pool at index %d."), buffer.index));
553       GST_V4L2_BUFFER_POOL_UNLOCK (pool);
554       return NULL;
555     }
556
557     GST_LOG_OBJECT (pool->v4l2elem,
558         "grabbed frame %d (ix=%d), flags %08x, pool-ct=%d, buffer=%p",
559         buffer.sequence, buffer.index, buffer.flags, pool->num_live_buffers,
560         pool_buffer);
561
562     pool->num_live_buffers++;
563     GST_DEBUG_OBJECT (pool->v4l2elem, "num_live_buffers++: %d",
564         pool->num_live_buffers);
565
566     GST_V4L2_BUFFER_POOL_UNLOCK (pool);
567
568     /* this can change at every frame, esp. with jpeg */
569     GST_BUFFER_SIZE (pool_buffer) = buffer.bytesused;
570
571     return pool_buffer;
572   }
573
574
575   GST_WARNING_OBJECT (pool->v4l2elem,
576       "problem grabbing frame %d (ix=%d), pool-ct=%d, buf.flags=%d",
577       buffer.sequence, buffer.index,
578       GST_MINI_OBJECT_REFCOUNT (pool), buffer.flags);
579
580   switch (errno) {
581     case EAGAIN:
582       GST_WARNING_OBJECT (pool->v4l2elem,
583           "Non-blocking I/O has been selected using O_NONBLOCK and"
584           " no buffer was in the outgoing queue. device %s",
585           v4l2object->videodev);
586       break;
587     case EINVAL:
588       GST_ELEMENT_ERROR (pool->v4l2elem, RESOURCE, FAILED,
589           (_("Failed trying to get video frames from device '%s'."),
590               v4l2object->videodev),
591           (_("The buffer type is not supported, or the index is out of bounds,"
592                   " or no buffers have been allocated yet, or the userptr"
593                   " or length are invalid. device %s"), v4l2object->videodev));
594       break;
595     case ENOMEM:
596       GST_ELEMENT_ERROR (pool->v4l2elem, RESOURCE, FAILED,
597           (_("Failed trying to get video frames from device '%s'. Not enough memory."), v4l2object->videodev), (_("insufficient memory to enqueue a user pointer buffer. device %s."), v4l2object->videodev));
598       break;
599     case EIO:
600       GST_INFO_OBJECT (pool->v4l2elem,
601           "VIDIOC_DQBUF failed due to an internal error."
602           " Can also indicate temporary problems like signal loss."
603           " Note the driver might dequeue an (empty) buffer despite"
604           " returning an error, or even stop capturing."
605           " device %s", v4l2object->videodev);
606       /* have we de-queued a buffer ? */
607       if (!(buffer.flags & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))) {
608         GST_DEBUG_OBJECT (pool->v4l2elem, "reenqueing buffer");
609         /* FIXME ... should we do something here? */
610       }
611       break;
612     case EINTR:
613       GST_WARNING_OBJECT (pool->v4l2elem,
614           "could not sync on a buffer on device %s", v4l2object->videodev);
615       break;
616     default:
617       GST_WARNING_OBJECT (pool->v4l2elem,
618           "Grabbing frame got interrupted on %s unexpectedly. %d: %s.",
619           v4l2object->videodev, errno, g_strerror (errno));
620       break;
621   }
622
623   return NULL;
624 }
625
626 gint
627 gst_v4l2_buffer_pool_available_buffers (GstV4l2BufferPool * pool)
628 {
629   return pool->buffer_count - pool->num_live_buffers;
630 }