v4l2bufferpool: Queue number of allocated buffers to capture
[platform/upstream/gst-plugins-good.git] / sys / v4l2 / gstv4l2bufferpool.c
index 9eba369..49f222b 100644 (file)
@@ -611,7 +611,7 @@ wrong_config:
 }
 
 static GstFlowReturn
-gst_v4l2_buffer_pool_resurect_buffer (GstV4l2BufferPool * pool)
+gst_v4l2_buffer_pool_resurrect_buffer (GstV4l2BufferPool * pool)
 {
   GstBufferPoolAcquireParams params = { 0 };
   GstBuffer *buffer = NULL;
@@ -650,12 +650,14 @@ gst_v4l2_buffer_pool_streamon (GstV4l2BufferPool * pool)
     case GST_V4L2_IO_DMABUF:
     case GST_V4L2_IO_DMABUF_IMPORT:
       if (!V4L2_TYPE_IS_OUTPUT (pool->obj->type)) {
+        guint i;
+
         /* For captures, we need to enqueue buffers before we start streaming,
          * so the driver don't underflow immediatly. As we have put then back
-         * into the base class queue, resurect them, then releasing will queue
+         * into the base class queue, resurrect them, then releasing will queue
          * them back. */
-        while (gst_v4l2_buffer_pool_resurect_buffer (pool) == GST_FLOW_OK)
-          continue;
+        for (i = 0; i < pool->num_allocated; i++)
+          gst_v4l2_buffer_pool_resurrect_buffer (pool);
       }
 
       if (obj->ioctl (pool->video_fd, VIDIOC_STREAMON, &obj->type) < 0)
@@ -742,10 +744,23 @@ gst_v4l2_buffer_pool_start (GstBufferPool * bpool)
 
   GST_DEBUG_OBJECT (pool, "activating pool");
 
-  if (pool->other_pool)
+  if (pool->other_pool) {
+    GstBuffer *buffer;
+
     if (!gst_buffer_pool_set_active (pool->other_pool, TRUE))
       goto other_pool_failed;
 
+    if (gst_buffer_pool_acquire_buffer (pool->other_pool, &buffer, NULL) !=
+        GST_FLOW_OK)
+      goto other_pool_failed;
+
+    if (!gst_v4l2_object_try_import (obj, buffer)) {
+      gst_buffer_unref (buffer);
+      goto cannot_import;
+    }
+    gst_buffer_unref (buffer);
+  }
+
   config = gst_buffer_pool_get_config (bpool);
   if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
           &max_buffers))
@@ -778,6 +793,7 @@ gst_v4l2_buffer_pool_start (GstBufferPool * bpool)
 
       count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
           V4L2_MEMORY_MMAP);
+      pool->num_allocated = count;
 
       if (count < GST_V4L2_MIN_BUFFERS) {
         min_buffers = count;
@@ -875,7 +891,7 @@ gst_v4l2_buffer_pool_start (GstBufferPool * bpool)
 
     pool->group_released_handler =
         g_signal_connect_swapped (pool->vallocator, "group-released",
-        G_CALLBACK (gst_v4l2_buffer_pool_resurect_buffer), pool);
+        G_CALLBACK (gst_v4l2_buffer_pool_resurrect_buffer), pool);
     ret = gst_v4l2_buffer_pool_streamon (pool);
   }
 
@@ -903,7 +919,7 @@ start_failed:
   }
 other_pool_failed:
   {
-    GST_ERROR_OBJECT (pool, "failed to active the other pool %"
+    GST_ERROR_OBJECT (pool, "failed to activate the other pool %"
         GST_PTR_FORMAT, pool->other_pool);
     return FALSE;
   }
@@ -912,6 +928,27 @@ queue_failed:
     GST_ERROR_OBJECT (pool, "failed to queue buffers into the capture queue");
     return FALSE;
   }
+cannot_import:
+  {
+    GST_ERROR_OBJECT (pool, "cannot import buffers from downstream pool");
+    return FALSE;
+  }
+}
+
+static gboolean
+gst_v4l2_buffer_pool_vallocator_stop (GstV4l2BufferPool * pool)
+{
+  GstV4l2Return vret;
+
+  if (!pool->vallocator)
+    return TRUE;
+
+  vret = gst_v4l2_allocator_stop (pool->vallocator);
+
+  if (vret == GST_V4L2_BUSY)
+    GST_WARNING_OBJECT (pool, "some buffers are still outstanding");
+
+  return (vret == GST_V4L2_OK);
 }
 
 static gboolean
@@ -920,6 +957,9 @@ gst_v4l2_buffer_pool_stop (GstBufferPool * bpool)
   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
   gboolean ret;
 
+  if (pool->orphaned)
+    return gst_v4l2_buffer_pool_vallocator_stop (pool);
+
   GST_DEBUG_OBJECT (pool, "stopping pool");
 
   if (pool->group_released_handler > 0) {
@@ -938,17 +978,44 @@ gst_v4l2_buffer_pool_stop (GstBufferPool * bpool)
 
   ret = GST_BUFFER_POOL_CLASS (parent_class)->stop (bpool);
 
-  if (ret && pool->vallocator) {
-    GstV4l2Return vret;
+  if (ret)
+    ret = gst_v4l2_buffer_pool_vallocator_stop (pool);
+
+  return ret;
+}
 
-    vret = gst_v4l2_allocator_stop (pool->vallocator);
+gboolean
+gst_v4l2_buffer_pool_orphan (GstBufferPool ** bpool)
+{
+  GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (*bpool);
+  gboolean ret;
 
-    if (vret == GST_V4L2_BUSY)
-      GST_WARNING_OBJECT (pool, "some buffers are still outstanding");
+  if (!GST_V4L2_ALLOCATOR_CAN_ORPHAN_BUFS (pool->vallocator))
+    return FALSE;
 
-    ret = (vret == GST_V4L2_OK);
-  }
+  if (g_getenv ("GST_V4L2_FORCE_DRAIN"))
+    return FALSE;
+
+  GST_DEBUG_OBJECT (pool, "orphaning pool");
+
+  gst_buffer_pool_set_active (*bpool, FALSE);
+  /*
+   * If the buffer pool has outstanding buffers, it will not be stopped
+   * by the base class when set inactive. Stop it manually and mark it
+   * as orphaned
+   */
+  ret = gst_v4l2_buffer_pool_stop (*bpool);
+  if (!ret)
+    ret = gst_v4l2_allocator_orphan (pool->vallocator);
+
+  if (!ret)
+    goto orphan_failed;
 
+  pool->orphaned = TRUE;
+  gst_object_unref (*bpool);
+  *bpool = NULL;
+
+orphan_failed:
   return ret;
 }
 
@@ -1318,7 +1385,7 @@ gst_v4l2_buffer_pool_acquire_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
 
   GST_DEBUG_OBJECT (pool, "acquire");
 
-  /* If this is being called to resurect a lost buffer */
+  /* If this is being called to resurrect a lost buffer */
   if (params && params->flags & GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT) {
     ret = pclass->acquire_buffer (bpool, buffer, params);
     goto done;
@@ -1396,6 +1463,14 @@ gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool, GstBuffer * buffer)
 
   GST_DEBUG_OBJECT (pool, "release buffer %p", buffer);
 
+  /* If the buffer's pool has been orphaned, dispose of it so that
+   * the pool resources can be freed */
+  if (pool->orphaned) {
+    GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
+    pclass->release_buffer (bpool, buffer);
+    return;
+  }
+
   switch (obj->type) {
     case V4L2_BUF_TYPE_VIDEO_CAPTURE:
     case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
@@ -1414,11 +1489,14 @@ gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool, GstBuffer * buffer)
         {
           GstV4l2MemoryGroup *group;
           if (gst_v4l2_is_buffer_valid (buffer, &group)) {
+            GstFlowReturn ret = GST_FLOW_OK;
+
             gst_v4l2_allocator_reset_group (pool->vallocator, group);
             /* queue back in the device */
             if (pool->other_pool)
-              gst_v4l2_buffer_pool_prepare_buffer (pool, buffer, NULL);
-            if (gst_v4l2_buffer_pool_qbuf (pool, buffer, group) != GST_FLOW_OK)
+              ret = gst_v4l2_buffer_pool_prepare_buffer (pool, buffer, NULL);
+            if (ret != GST_FLOW_OK ||
+                gst_v4l2_buffer_pool_qbuf (pool, buffer, group) != GST_FLOW_OK)
               pclass->release_buffer (bpool, buffer);
           } else {
             /* Simply release invalide/modified buffer, the allocator will
@@ -1543,6 +1621,7 @@ gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool)
   pool->can_poll_device = TRUE;
   g_cond_init (&pool->empty_cond);
   pool->empty = TRUE;
+  pool->orphaned = FALSE;
 }
 
 static void
@@ -1758,7 +1837,7 @@ gst_v4l2_buffer_pool_process (GstV4l2BufferPool * pool, GstBuffer ** buf)
             /* If we have no more buffer, and can allocate it time to do so */
             if (num_queued == 0) {
               if (GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP)) {
-                ret = gst_v4l2_buffer_pool_resurect_buffer (pool);
+                ret = gst_v4l2_buffer_pool_resurrect_buffer (pool);
                 if (ret == GST_FLOW_OK)
                   goto done;
               }
@@ -1769,7 +1848,7 @@ gst_v4l2_buffer_pool_process (GstV4l2BufferPool * pool, GstBuffer ** buf)
               GstBuffer *copy;
 
               if (GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP)) {
-                ret = gst_v4l2_buffer_pool_resurect_buffer (pool);
+                ret = gst_v4l2_buffer_pool_resurrect_buffer (pool);
                 if (ret == GST_FLOW_OK)
                   goto done;
               }