v4l2codecs: pool: Create new buffer when pool is empty
authorNicolas Dufresne <nicolas.dufresne@collabora.com>
Wed, 18 Mar 2020 21:03:51 +0000 (17:03 -0400)
committerNicolas Dufresne <nicolas.dufresne@collabora.com>
Tue, 31 Mar 2020 13:34:05 +0000 (09:34 -0400)
This simply create an empty GstBuffer when the pool is empty. This way it's up
to the allocator to grow or wait if we ran out of memory.

sys/v4l2codecs/gstv4l2codecpool.c

index 60795ff..69a01d4 100644 (file)
@@ -33,22 +33,37 @@ struct _GstV4l2CodecPool
 
 G_DEFINE_TYPE (GstV4l2CodecPool, gst_v4l2_codec_pool, GST_TYPE_BUFFER_POOL);
 
+static GstBuffer *
+gst_v4l2_codec_pool_create_empty_buffer (void)
+{
+  GstVideoMeta *vmeta;
+  GstBuffer *buffer = gst_buffer_new ();
+
+  vmeta = gst_buffer_add_video_meta (buffer, 0, GST_VIDEO_FORMAT_NV12, 1, 1);
+  GST_META_FLAG_SET (vmeta, GST_META_FLAG_POOLED);
+
+  return buffer;
+}
+
 static GstFlowReturn
 gst_v4l2_codec_pool_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
     GstBufferPoolAcquireParams * params)
 {
   GstV4l2CodecPool *self = GST_V4L2_CODEC_POOL (pool);
-  GstBuffer *buf = gst_atomic_queue_pop (self->queue);
+  GstBuffer *buf;
   GstVideoMeta *vmeta;
 
   /* A GstVideoInfo must be set before buffer can be acquired */
   g_return_val_if_fail (self->vinfo, GST_FLOW_ERROR);
 
+  buf = gst_atomic_queue_pop (self->queue);
   if (!buf)
-    return GST_FLOW_ERROR;
+    buf = gst_v4l2_codec_pool_create_empty_buffer ();
 
-  if (!gst_v4l2_codec_allocator_prepare_buffer (self->allocator, buf))
+  if (!gst_v4l2_codec_allocator_prepare_buffer (self->allocator, buf)) {
+    gst_atomic_queue_push (self->queue, buf);
     return GST_FLOW_ERROR;
+  }
 
   vmeta = gst_buffer_get_video_meta (buf);
   vmeta->format = GST_VIDEO_INFO_FORMAT (self->vinfo);
@@ -130,12 +145,7 @@ gst_v4l2_codec_pool_new (GstV4l2CodecAllocator * allocator,
 
   pool_size = gst_v4l2_codec_allocator_get_pool_size (allocator);
   for (gsize i = 0; i < pool_size; i++) {
-    GstVideoMeta *vmeta;
-    GstBuffer *buffer = gst_buffer_new ();
-
-    vmeta = gst_buffer_add_video_meta (buffer, 0, GST_VIDEO_FORMAT_NV12, 1, 1);
-    GST_META_FLAG_SET (vmeta, GST_META_FLAG_POOLED);
-
+    GstBuffer *buffer = gst_v4l2_codec_pool_create_empty_buffer ();
     gst_atomic_queue_push (pool->queue, buffer);
   }