Merge branch 'upstream/1.22.7' into tizen_gst_1.22.7
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-good / sys / v4l2 / gstv4l2bufferpool.c
index 8c6eba1..a5a9757 100644 (file)
 #include "gstv4l2object.h"
 #include <glib/gi18n-lib.h>
 #include <gst/glib-compat-private.h>
+#ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
+#include <stdio.h>
+#include <gst/allocators/gsttizenmemory.h>
+
+#define TIZEN_BUFFER_DUMP_PATH "/tmp/v4l2_output.raw"
+#endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
 
 GST_DEBUG_CATEGORY_STATIC (v4l2bufferpool_debug);
 GST_DEBUG_CATEGORY_STATIC (CAT_PERFORMANCE);
@@ -52,7 +58,6 @@ GST_DEBUG_CATEGORY_STATIC (CAT_PERFORMANCE);
 
 #define GST_V4L2_IMPORT_QUARK gst_v4l2_buffer_pool_import_quark ()
 
-
 /*
  * GstV4l2BufferPool:
  */
@@ -83,6 +88,117 @@ enum _GstV4l2BufferState
 static void gst_v4l2_buffer_pool_complete_release_buffer (GstBufferPool * bpool,
     GstBuffer * buffer, gboolean queued);
 
+#ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
+static void gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool,
+    GstBuffer * buffer);
+
+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;
+}
+
+static void
+gst_v4l2_tizen_buffer_dump (tbm_surface_h surface)
+{
+  int i;
+  FILE *fp;
+  tbm_surface_info_s sinfo;
+
+  if (!surface) {
+    GST_WARNING ("NULL surface");
+    return;
+  }
+
+  memset (&sinfo, 0x0, sizeof(tbm_surface_info_s));
+
+  if (tbm_surface_get_info (surface, &sinfo) != TBM_ERROR_NONE) {
+    GST_ERROR ("get tbm surface[%p] info failed", surface);
+    return;
+  }
+
+  fp = fopen (TIZEN_BUFFER_DUMP_PATH, "a");
+  if (!fp) {
+    GST_ERROR ("file open failed[%s], errno[%d]", TIZEN_BUFFER_DUMP_PATH, errno);
+    return;
+  }
+
+  for (i = 0 ; i < sinfo.num_planes ; i++) {
+    GST_DEBUG ("[%d] %ux%u, stride[%u], size[%u], offset[%u]",
+      i, sinfo.width, sinfo.height, sinfo.planes[i].stride,
+      sinfo.planes[i].size, sinfo.planes[i].offset);
+    fwrite (sinfo.planes[i].ptr, 1, sinfo.planes[i].size, fp);
+  }
+
+  fclose(fp);
+}
+#endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
+
 static gboolean
 gst_v4l2_is_buffer_valid (GstBuffer * buffer, GstV4l2MemoryGroup ** out_group)
 {
@@ -717,15 +833,54 @@ streamon_failed:
 
 /* Call with streamlock held, or when streaming threads are down */
 static void
+#ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
+gst_v4l2_buffer_pool_streamoff (GstV4l2BufferPool * pool, gboolean is_locked)
+#else
 gst_v4l2_buffer_pool_streamoff (GstV4l2BufferPool * pool)
+#endif
 {
   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], is_locked[%d]",
+      pool->live_buffer_count, is_locked);
+
+    if (pool->live_buffer_count > 0) {
+      end_time = g_get_monotonic_time () + G_TIME_SPAN_SECOND;
+
+      if (is_locked)
+        GST_OBJECT_UNLOCK (pool);
+
+      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);
+
+      if (is_locked)
+        GST_OBJECT_LOCK (pool);
+    }
+
+    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:
@@ -1013,7 +1168,11 @@ gst_v4l2_buffer_pool_stop (GstBufferPool * bpool)
   }
 
   if (!pool->orphaned)
+#ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
+    gst_v4l2_buffer_pool_streamoff (pool, FALSE);
+#else
     gst_v4l2_buffer_pool_streamoff (pool);
+#endif
 
   ret = GST_BUFFER_POOL_CLASS (parent_class)->stop (bpool);
 
@@ -1050,7 +1209,11 @@ gst_v4l2_buffer_pool_orphan (GstV4l2Object * v4l2object)
    * workaround of not being able to use the pool hidden activation lock. */
   GST_OBJECT_LOCK (pool);
 
+#ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
+  gst_v4l2_buffer_pool_streamoff (pool, TRUE);
+#else
   gst_v4l2_buffer_pool_streamoff (pool);
+#endif
   ret = gst_v4l2_allocator_orphan (pool->vallocator);
   if (ret)
     pool->orphaned = TRUE;
@@ -1240,6 +1403,9 @@ gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer,
   const GstVideoInfo *info = &obj->info;
   gint i;
   gint old_buffer_state;
+#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;
@@ -1401,6 +1567,20 @@ gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer,
       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
   }
 
+#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;
+
+    if (pool->tbm_output_dump)
+      gst_v4l2_tizen_buffer_dump (group->surface);
+  }
+#endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
+
   if (group->buffer.flags & V4L2_BUF_FLAG_ERROR)
     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_CORRUPTED);
 
@@ -1544,6 +1724,14 @@ gst_v4l2_buffer_pool_complete_release_buffer (GstBufferPool * bpool,
   GST_DEBUG_OBJECT (pool, "complete release buffer %p (queued = %s)", buffer,
       queued ? "yes" : "no");
 
+  /* 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:
@@ -1683,6 +1871,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;
@@ -1778,6 +1974,18 @@ gst_v4l2_buffer_pool_new (GstV4l2Object * obj, GstCaps * caps)
   pool->video_fd = fd;
   pool->obj = obj;
 
+#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);
+
+  pool->tbm_output_dump = obj->tbm_output_dump;
+
+  GST_INFO ("tbm output dump [%d]", pool->tbm_output_dump);
+#endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
   pool->vallocator = gst_v4l2_allocator_new (GST_OBJECT (pool), obj);
   if (pool->vallocator == NULL)
     goto allocator_failed;
@@ -1800,6 +2008,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;
@@ -1980,12 +2194,19 @@ gst_v4l2_buffer_pool_process (GstV4l2BufferPool * pool, GstBuffer ** buf,
               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_complete_release_buffer (bpool, tmp, FALSE);
-
+#ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
+          }
+#endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
           if (ret != GST_FLOW_OK)
             goto copy_failed;
           break;
@@ -2281,7 +2502,11 @@ gst_v4l2_buffer_pool_flush (GstV4l2Object * v4l2object)
 
   pool = GST_V4L2_BUFFER_POOL (bpool);
 
+#ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
+  gst_v4l2_buffer_pool_streamoff (pool, FALSE);
+#else
   gst_v4l2_buffer_pool_streamoff (pool);
+#endif
 
   if (!V4L2_TYPE_IS_OUTPUT (pool->obj->type)) {
     ret = gst_v4l2_buffer_pool_flush_events (v4l2object);