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