v4l2videodec: Add ref/unref code to avoid crash
[platform/upstream/gst-plugins-good.git] / sys / v4l2 / gstv4l2bufferpool.c
index 8c10f99..494b5b8 100644 (file)
@@ -45,6 +45,9 @@
 #include "gstv4l2object.h"
 #include "gst/gst-i18n-plugin.h"
 #include <gst/glib-compat-private.h>
+#ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
+#include <gst/allocators/gsttizenmemory.h>
+#endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
 
 GST_DEBUG_CATEGORY_STATIC (v4l2bufferpool_debug);
 GST_DEBUG_CATEGORY_STATIC (CAT_PERFORMANCE);
@@ -52,7 +55,6 @@ GST_DEBUG_CATEGORY_STATIC (CAT_PERFORMANCE);
 
 #define GST_V4L2_IMPORT_QUARK gst_v4l2_buffer_pool_import_quark ()
 
-
 /*
  * GstV4l2BufferPool:
  */
@@ -68,6 +70,76 @@ enum _GstV4l2BufferPoolAcquireFlags
 
 static void gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool,
     GstBuffer * buffer);
+#ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
+typedef struct _GstV4l2TizenBuffer GstV4l2TizenBuffer;
+struct _GstV4l2TizenBuffer {
+  int index;
+  GstBuffer *gst_buffer;
+  GstBuffer *v4l2_buffer;
+  GstV4l2BufferPool *v4l2_pool;
+};
+
+static void gst_v4l2_tizen_buffer_finalize (GstV4l2TizenBuffer *tizen_buffer)
+{
+  GstV4l2BufferPool *pool = NULL;
+
+  if (!tizen_buffer) {
+    GST_ERROR ("NULL buffer");
+    return;
+  }
+
+  pool = tizen_buffer->v4l2_pool;
+
+  gst_v4l2_buffer_pool_release_buffer (GST_BUFFER_POOL_CAST (pool), tizen_buffer->v4l2_buffer);
+
+  g_mutex_lock (&pool->buffer_lock);
+
+  pool->live_buffer_count--;
+
+  GST_DEBUG_OBJECT (pool, "release buffer[%d][tizen:%p,v4l2:%p,gst:%p], live[%d]",
+      tizen_buffer->index, tizen_buffer, tizen_buffer->v4l2_buffer,
+      tizen_buffer->gst_buffer, pool->live_buffer_count);
+
+  g_cond_signal (&pool->buffer_cond);
+
+  g_mutex_unlock (&pool->buffer_lock);
+
+  gst_object_unref (pool);
+
+  g_free(tizen_buffer);
+}
+
+static GstV4l2TizenBuffer *gst_v4l2_tizen_buffer_new (GstBuffer *v4l2_buffer, int index, GstV4l2BufferPool *v4l2_pool)
+{
+  GstV4l2TizenBuffer *tizen_buffer = NULL;
+  GstMemory *memory = NULL;
+
+  tizen_buffer = g_new0 (GstV4l2TizenBuffer, 1);
+  tizen_buffer->index = index;
+  tizen_buffer->v4l2_buffer = v4l2_buffer;
+  tizen_buffer->gst_buffer = gst_buffer_new ();
+  tizen_buffer->v4l2_pool = gst_object_ref (v4l2_pool);
+
+  memory = gst_tizen_allocator_alloc_surface (v4l2_pool->tallocator,
+      &v4l2_pool->obj->info, v4l2_pool->vallocator->groups[index]->surface, (gpointer)tizen_buffer,
+      (GDestroyNotify)gst_v4l2_tizen_buffer_finalize);
+
+  gst_buffer_append_memory (tizen_buffer->gst_buffer, memory);
+  gst_buffer_set_size (tizen_buffer->gst_buffer, v4l2_pool->vallocator->s_info.size);
+
+  g_mutex_lock (&v4l2_pool->buffer_lock);
+
+  v4l2_pool->live_buffer_count++;
+
+  GST_DEBUG_OBJECT (v4l2_pool, "new buffer[tizen:%p,v4l2:%p,gst:%p], size[%d], live[%d]",
+      tizen_buffer, v4l2_buffer, tizen_buffer->gst_buffer,
+      v4l2_pool->vallocator->s_info.size, v4l2_pool->live_buffer_count);
+
+  g_mutex_unlock (&v4l2_pool->buffer_lock);
+
+  return tizen_buffer;
+}
+#endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
 
 static gboolean
 gst_v4l2_is_buffer_valid (GstBuffer * buffer, GstV4l2MemoryGroup ** out_group)
@@ -611,7 +683,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 +722,19 @@ 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 num_queued;
+        guint i, n = 0;
+
+        num_queued = g_atomic_int_get (&pool->num_queued);
+        if (num_queued < pool->num_allocated)
+          n = pool->num_allocated - num_queued;
+
         /* 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 < n; i++)
+          gst_v4l2_buffer_pool_resurrect_buffer (pool);
       }
 
       if (obj->ioctl (pool->video_fd, VIDIOC_STREAMON, &obj->type) < 0)
@@ -686,10 +765,38 @@ gst_v4l2_buffer_pool_streamoff (GstV4l2BufferPool * pool)
   GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
   GstV4l2Object *obj = pool->obj;
   gint i;
+#ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
+  gint64 end_time = 0;
+#endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
 
   if (!pool->streaming)
     return;
 
+#ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
+  if (obj->tbm_output && !V4L2_TYPE_IS_OUTPUT(pool->obj->type)) {
+    g_mutex_lock (&pool->buffer_lock);
+
+    GST_INFO_OBJECT (pool, "live buffer[%d]", pool->live_buffer_count);
+
+    if (pool->live_buffer_count > 0) {
+      end_time = g_get_monotonic_time () + G_TIME_SPAN_SECOND;
+
+      do {
+        GST_WARNING_OBJECT (pool, "wait for live buffer[%d]", pool->live_buffer_count);
+
+        if (!g_cond_wait_until (&pool->buffer_cond, &pool->buffer_lock, end_time)) {
+          GST_ERROR_OBJECT (pool, "failed to wait live buffer[%d]", pool->live_buffer_count);
+          break;
+        }
+
+        GST_WARNING_OBJECT (pool, "signal received, check again : live count[%d]",
+            pool->live_buffer_count);
+      } while (pool->live_buffer_count > 0);
+    }
+
+    g_mutex_unlock (&pool->buffer_lock);
+  }
+#endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
   switch (obj->mode) {
     case GST_V4L2_IO_MMAP:
     case GST_V4L2_IO_USERPTR:
@@ -742,10 +849,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 +898,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 +996,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);
   }
 
@@ -912,6 +1033,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 +1062,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 +1083,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;
+}
+
+gboolean
+gst_v4l2_buffer_pool_orphan (GstBufferPool ** bpool)
+{
+  GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (*bpool);
+  gboolean ret;
 
-    vret = gst_v4l2_allocator_stop (pool->vallocator);
+  if (!GST_V4L2_ALLOCATOR_CAN_ORPHAN_BUFS (pool->vallocator))
+    return FALSE;
 
-    if (vret == GST_V4L2_BUSY)
-      GST_WARNING_OBJECT (pool, "some buffers are still outstanding");
+  if (g_getenv ("GST_V4L2_FORCE_DRAIN"))
+    return FALSE;
 
-    ret = (vret == GST_V4L2_OK);
-  }
+  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;
 }
 
@@ -1148,6 +1320,9 @@ gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer,
   GstVideoMeta *vmeta;
   gsize size;
   gint i;
+#ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
+  GstV4l2TizenBuffer *tizen_buffer = NULL;
+#endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
 
   if ((res = gst_v4l2_buffer_pool_poll (pool, wait)) < GST_FLOW_OK)
     goto poll_failed;
@@ -1280,6 +1455,16 @@ gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer,
   GST_BUFFER_OFFSET (outbuf) = group->buffer.sequence;
   GST_BUFFER_OFFSET_END (outbuf) = group->buffer.sequence + 1;
 
+#ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
+  if (group->surface) {
+    tizen_buffer = gst_v4l2_tizen_buffer_new (outbuf, group->buffer.index, pool);
+    if (!tizen_buffer) {
+      GST_ERROR_OBJECT (pool, "tizen buffer failed for index[%d]", group->buffer.index);
+      goto no_buffer;
+    }
+    outbuf = tizen_buffer->gst_buffer;
+  }
+#endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
 done:
   *buffer = outbuf;
 
@@ -1318,7 +1503,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 +1581,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:
@@ -1510,6 +1703,14 @@ gst_v4l2_buffer_pool_dispose (GObject * object)
     gst_object_unref (pool->allocator);
   pool->allocator = NULL;
 
+#ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
+  g_cond_clear (&pool->buffer_cond);
+  g_mutex_clear (&pool->buffer_lock);
+
+  if (pool->tallocator)
+    gst_object_unref (pool->tallocator);
+  pool->tallocator = NULL;
+#endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
   if (pool->other_pool)
     gst_object_unref (pool->other_pool);
   pool->other_pool = NULL;
@@ -1546,6 +1747,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
@@ -1614,6 +1816,14 @@ gst_v4l2_buffer_pool_new (GstV4l2Object * obj, GstCaps * caps)
   pool->obj = obj;
   pool->can_poll_device = TRUE;
 
+#ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
+  pool->tallocator = gst_tizen_allocator_new ();
+  if (pool->tallocator == NULL)
+    goto allocator_failed;
+
+  g_mutex_init (&pool->buffer_lock);
+  g_cond_init (&pool->buffer_cond);
+#endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
   pool->vallocator = gst_v4l2_allocator_new (GST_OBJECT (pool), obj);
   if (pool->vallocator == NULL)
     goto allocator_failed;
@@ -1636,6 +1846,12 @@ dup_failed:
   }
 allocator_failed:
   {
+#ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
+    if (pool->tallocator) {
+      gst_object_unref (pool->tallocator);
+      pool->tallocator = NULL;
+    }
+#endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
     GST_ERROR_OBJECT (pool, "Failed to create V4L2 allocator");
     gst_object_unref (pool);
     return NULL;
@@ -1761,7 +1977,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;
               }
@@ -1772,7 +1988,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;
               }
@@ -1805,11 +2021,19 @@ gst_v4l2_buffer_pool_process (GstV4l2BufferPool * pool, GstBuffer ** buf)
             if (GST_V4L2_IS_M2M (obj->device_caps))
               goto eos;
           }
-
+#ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
+          if (pool->obj->tbm_output && pool->obj->mode == GST_V4L2_IO_DMABUF) {
+            gst_buffer_unref (*buf);
+            *buf = tmp;
+          } else {
+#endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
           ret = gst_v4l2_buffer_pool_copy_buffer (pool, *buf, tmp);
 
           /* an queue the buffer again after the copy */
           gst_v4l2_buffer_pool_release_buffer (bpool, tmp);
+#ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
+          }
+#endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
 
           if (ret != GST_FLOW_OK)
             goto copy_failed;