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