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