Uncover MMVideoBuffer in 'waylandsrc'. 21/183821/8
authorSeokHoon Lee <andy.shlee@samsung.com>
Wed, 11 Jul 2018 08:23:30 +0000 (17:23 +0900)
committerSeokHoon Lee <andy.shlee@samsung.com>
Wed, 29 Aug 2018 03:48:53 +0000 (12:48 +0900)
After this patch, without MMVideoBuffer, TBM is sended by GstBuffer.

Signed-off-by: SeokHoon Lee <andy.shlee@samsung.com>
Change-Id: I26782f1fe835f8e962fa52be1e8ea844c75ed99f

configure.ac
waylandsrc/src/Makefile.am
waylandsrc/src/gstwaylandsrc.c
waylandsrc/src/gstwaylandsrc.h

index 0c91739..2e7a8a6 100644 (file)
@@ -155,6 +155,12 @@ dnl make _CFLAGS and _LIBS available
 AC_SUBST(GST_VIDEO_CFLAGS)
 AC_SUBST(GST_VIDEO_LIBS)
 
+PKG_CHECK_MODULES(GST_ALLOCATORS, gstreamer-allocators-$GST_MAJORMINOR >= $GST_REQUIRED)
+
+dnl make _CFLAGS and _LIBS available
+AC_SUBST(GST_ALLOCATORS_CFLAGS)
+AC_SUBST(GST_ALLOCATORS_LIBS)
+
 PKG_CHECK_MODULES(DRM, libdrm)
 AC_SUBST(DRM_CFLAGS)
 AC_SUBST(DRM_LIBS)
index 117f402..5b70fb4 100644 (file)
@@ -18,8 +18,25 @@ libgstwaylandsrc_la_SOURCES = gstwaylandsrc.c
 
 # flags used to compile this plugin
 # add other _CFLAGS and _LIBS as needed
-libgstwaylandsrc_la_CFLAGS = $(GST_CFLAGS) $(GST_PLUGINS_BASE_CFLAGS) $(GST_BASE_CFLAGS) $(WAYLAND_CLIENT_CFLAGS) $(DRM_CFLAGS) $(TBM_CFLAGS) $(MMCOMMON_CFLAGS)
-libgstwaylandsrc_la_LIBADD = $(GST_LIBS) $(GST_PLUGINS_BASE_LIBS) $(GST_BASE_LIBS) $(WAYLAND_CLIENT_LIBS) $(DRM_LIBS) $(TBM_LIBS)
+libgstwaylandsrc_la_CFLAGS = $(TBM_CFLAGS) \
+                                                       $(GST_CFLAGS) \
+                                                       $(GST_BASE_CFLAGS) \
+                                                       $(GST_VIDEO_FLAGS) \
+                                                       $(GST_ALLOCATORS_FLAGS) \
+                                                       $(GST_PLUGINS_BASE_CFLAGS) \
+                                                       $(WAYLAND_CLIENT_CFLAGS) \
+                                                       $(DRM_CFLAGS) \
+                                                       $(MMCOMMON_CFLAGS)
+
+libgstwaylandsrc_la_LIBADD = $(GST_LIBS) \
+                                                       $(GST_BASE_LIBS) \
+                                                       $(GST_PLUGINS_BASE_LIBS) \
+                                                       $(GST_VIDEO_LIBS) \
+                                                       $(GST_ALLOCATORS_LIBS) \
+                                                       $(WAYLAND_CLIENT_LIBS) \
+                                                       $(DRM_LIBS) \
+                                                       $(TBM_LIBS)
+
 libgstwaylandsrc_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
 
 # headers we need but don't want installed
index 8584a8f..0daf817 100644 (file)
@@ -53,8 +53,8 @@ GST_DEBUG_CATEGORY_STATIC (waylandsrc_debug);
 #define DEFAULT_WIDTH 640
 #define DEFAULT_HEIGHT 480
 
-//#define DUMP_BUFFER
-#define USE_MM_VIDEO_BUFFER
+//#define WAYLANDSRC_BUFFER_DUMP
+#define WAYLANDSRC_DUMP_FILE   "/tmp/waylandsrc.dump"
 
 #define C(b,m)              (char)(((b) >> (m)) & 0xFF)
 #define B(c,s)              ((((unsigned int)(c)) & 0xff) << (s))
@@ -168,6 +168,48 @@ static const struct tizen_screenmirror_listener mirror_listener = {
   mirror_handle_stop
 };
 
+
+#ifdef WAYLANDSRC_BUFFER_DUMP
+static int dump_cnt;
+
+void save_waylandsrc_buffer (output_buffer * out_buffer)
+{
+  dump_cnt++;
+
+  if (100 < dump_cnt && dump_cnt < 150) {
+    tbm_bo_handle bo_handle;
+    FILE *fp = fopen (WAYLANDSRC_DUMP_FILE, "a");
+    if (fp == NULL) {
+      GST_ERROR_OBJECT (src, "Failed to open file: %s", strerror (errno));
+      return;
+    }
+
+    bo_handle = tbm_bo_map (out_buffer->bo[0], TBM_DEVICE_CPU, TBM_OPTION_READ);
+
+    if (bo_handle.ptr)
+      fwrite ((char *)bo_handle.ptr, tbm_bo_size(out_buffer->bo[0]), 1, fp);
+    else
+      GST_ERROR_OBJECT (src, "Failed to get tbm bo[0]'s handle: %s", strerror (errno));
+
+    tbm_bo_unmap(out_buffer->bo[0]);
+
+    #ifndef TIZEN_FEATURE_PRODUCT_TM1
+    bo_handle = tbm_bo_map (out_buffer->bo[1], TBM_DEVICE_CPU, TBM_OPTION_READ);
+    if (bo_handle.ptr)
+      fwrite ((char *) bo_handle.ptr, tbm_bo_size(out_buffer->bo[1]), 1, fp);
+    else
+      GST_ERROR_OBJECT (src, "Failed to get tbm bo[1]'s handle:: %s", strerror (errno));
+
+    tbm_bo_unmap(out_buffer->bo[1]);
+    #endif
+
+    GST_ERROR_OBJECT (src, "Dump (%d):%d       %d\n", dump_cnt, out_buffer->size, tbm_bo_size(out_buffer->bo[0]));
+
+    fclose (fp);
+  }
+}
+#endif
+
 int new_calc_plane(int width, int height)
 {
     int mbX, mbY;
@@ -321,107 +363,24 @@ mirror_handle_dequeued (void *data,
                 out_buffer->size, 0, out_buffer->size, (gpointer) out_buffer,
                 gst_wayland_src_gst_buffer_unref));
       } else if (src->format == TBM_FORMAT_NV12) {
-#ifndef USE_MM_VIDEO_BUFFER
-        out_buffer->gst_buffer = gst_buffer_new ();
-        gst_buffer_append_memory (out_buffer->gst_buffer,
-            gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY,
-                tbm_bo_map(out_buffer->bo[0], TBM_DEVICE_CPU, TBM_OPTION_READ).ptr,
-                tbm_bo_size(out_buffer->bo[0]), 0, tbm_bo_size(out_buffer->bo[0]),
-                (gpointer) out_buffer, gst_wayland_src_gst_buffer_unref));
-        gst_buffer_append_memory (out_buffer->gst_buffer,
-            gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY,
-                tbm_bo_map(out_buffer->bo[1], TBM_DEVICE_CPU, TBM_OPTION_READ).ptr,
-                tbm_bo_size(out_buffer->bo[1]), 0, tbm_bo_size(out_buffer->bo[1]),
-                (gpointer) out_buffer, NULL));
-#else
-        MMVideoBuffer *mm_video_buf = NULL;
-        mm_video_buf = (MMVideoBuffer *) malloc (sizeof (MMVideoBuffer));
-        if (mm_video_buf == NULL) {
-          GST_ERROR_OBJECT (src, "failed to alloc MMVideoBuffer");
+        GstMemory *memory = NULL;
+        GST_WARNING_OBJECT (src, "Creating memory by tizen allocator");
+        memory = gst_tizen_allocator_alloc_surface(src->allocator,
+            &src->video_info,
+            out_buffer->surface,
+            (gpointer)out_buffer,
+            (GDestroyNotify)gst_wayland_src_gst_buffer_unref);
+
+        if (!memory) {
+          GST_ERROR_OBJECT (src, "failed to alloc memory by tizen allocator");
           goto FUNC_END;
         }
 
-        memset (mm_video_buf, 0x00, sizeof (MMVideoBuffer));
-
-        mm_video_buf->type = MM_VIDEO_BUFFER_TYPE_TBM_BO;
-        mm_video_buf->handle.bo[0] = out_buffer->bo[0];
-        mm_video_buf->handle.bo[1] = out_buffer->bo[1];
-        GST_INFO_OBJECT (src, "BO : %p %p", mm_video_buf->handle.bo[0], mm_video_buf->handle.bo[1]);
-#ifndef TIZEN_FEATURE_PRODUCT_TM1
-        mm_video_buf->size[0] = gst_calculate_y_size(src->width, src->height); /*(src->width * src->height);*/
-        mm_video_buf->size[1] = gst_calculate_uv_size(src->width, src->height); /*(src->width * (src->height >> 1));*/
-        GST_INFO_OBJECT (src, "Size : %d %d", mm_video_buf->size[0], mm_video_buf->size[1]);
-
-        mm_video_buf->handle.dmabuf_fd[0] = tbm_bo_get_handle(out_buffer->bo[0], TBM_DEVICE_MM).u32;
-        mm_video_buf->handle.dmabuf_fd[1] = tbm_bo_get_handle(out_buffer->bo[1], TBM_DEVICE_MM).u32;
-
-        mm_video_buf->handle.paddr[0] = (tbm_bo_map(mm_video_buf->handle.bo[0], TBM_DEVICE_CPU, TBM_OPTION_WRITE)).ptr;
-        mm_video_buf->handle.paddr[1] = (tbm_bo_map(mm_video_buf->handle.bo[1], TBM_DEVICE_CPU, TBM_OPTION_WRITE)).ptr;
-
-        tbm_bo_unmap (mm_video_buf->handle.bo[0]);
-        tbm_bo_unmap (mm_video_buf->handle.bo[1]);
-#endif
-
-        mm_video_buf->width[0] = src->width;
-        mm_video_buf->height[0] = src->height;
-        mm_video_buf->format = MM_PIXEL_FORMAT_NV12;
-        mm_video_buf->width[1] = src->width;
-        mm_video_buf->height[1] = src->height >> 1;
-
-        mm_video_buf->stride_width[0] = GST_ROUND_UP_16 (mm_video_buf->width[0]);
-        mm_video_buf->stride_height[0] = GST_ROUND_UP_16 (mm_video_buf->height[0]);
-        mm_video_buf->stride_width[1] = GST_ROUND_UP_16 (mm_video_buf->width[1]);
-        mm_video_buf->stride_height[1] = GST_ROUND_UP_16 (mm_video_buf->height[1]);
-        mm_video_buf->is_secured = 0;
-
-#ifdef TIZEN_FEATURE_PRODUCT_TM1
-        mm_video_buf->size[0] = mm_video_buf->stride_width[0] * mm_video_buf->stride_height[0];
-        mm_video_buf->size[1] = mm_video_buf->stride_width[1] * mm_video_buf->stride_height[1];
-        GST_INFO_OBJECT (src, "Size : %d %d", mm_video_buf->size[0], mm_video_buf->size[1]);
-
-        mm_video_buf->data[0] = (tbm_bo_map(mm_video_buf->handle.bo[0], TBM_DEVICE_CPU, TBM_OPTION_WRITE)).ptr;
-        tbm_bo_unmap (mm_video_buf->handle.bo[0]);
-#endif
-
-        out_buffer->gst_buffer = gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY,
-            out_buffer, sizeof (out_buffer), 0, sizeof (out_buffer), (gpointer) out_buffer,
-            gst_wayland_src_gst_buffer_unref);
-
-        gst_buffer_append_memory (out_buffer->gst_buffer,
-            gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY,
-                mm_video_buf, sizeof (*mm_video_buf), 0, sizeof (*mm_video_buf),
-                mm_video_buf, g_free));
-
-#ifdef DUMP_BUFFER
-        static int dump_cnt = 0;
-        void *data, *data1;
-        FILE *fp;
-
-        data = tbm_bo_map (out_buffer->bo[0], TBM_DEVICE_CPU, TBM_OPTION_READ).ptr;
-        if (!data) {
-          GST_ERROR_OBJECT (src, "get tbm bo handle failed: %s", strerror (errno));
-          return;
-        }
-        data1 = tbm_bo_map (out_buffer->bo[1], TBM_DEVICE_CPU, TBM_OPTION_READ).ptr;
-        if (!data1) {
-          GST_ERROR_OBJECT (src, "get tbm bo handle failed: %s", strerror (errno));
-          return;
-        }
-
-        fp = fopen ("/root/raw.dump", "a");
-        if (fp == NULL)
-          return;
-
-        if (100 < dump_cnt  && dump_cnt < 150) {
-          fwrite ((char *) data, tbm_bo_size(out_buffer->bo[0]), 1, fp);
-          fwrite ((char *) data1, tbm_bo_size(out_buffer->bo[1]), 1, fp);
-          GST_ERROR_OBJECT (src, "Dump :%d\n", out_buffer->size);
-        }
-
-        dump_cnt++;
-        fclose (fp);
-#endif
+        out_buffer->gst_buffer = gst_buffer_new ();
+        gst_buffer_append_memory (out_buffer->gst_buffer, memory);
 
+#ifdef WAYLANDSRC_BUFFER_DUMP
+        save_waylandsrc_buffer (out_buffer);
 #endif
       }
     } else {
@@ -677,34 +636,24 @@ tbm_buffer_create (GstWaylandSrc * src)
       break;
     case TBM_FORMAT_NV12:
     case TBM_FORMAT_NV21:
-      info.width = src->width;
-      info.height = src->height;
-      info.format = src->format;
-      info.bpp = tbm_surface_internal_get_bpp(info.format);
-      info.num_planes = 2;
-      info.planes[0].stride = info.width;
-      info.planes[0].size = gst_calculate_y_size(info.planes[0].stride, info.height);//info.planes[0].stride * info.height
-      info.planes[0].offset = 0;
-      info.planes[1].stride = info.width;
-      info.planes[1].size = gst_calculate_uv_size(info.planes[1].stride, info.height);//info.planes[1].stride * (info.height >> 1);
-      info.planes[1].offset = 0;
-      info.size = info.planes[0].size + info.planes[1].size;
+
+      GST_WARNING_OBJECT (src, "BO image size : %d %d %d %d", src->ts_info.planes[0].size, src->ts_info.planes[1].size, src->ts_info.num_planes, src->ts_info.size);
 
 #ifdef TIZEN_FEATURE_PRODUCT_TM1
       out_buffer->bo[0] =
-          tbm_bo_alloc (bufmgr, info.size, TBM_BO_DEFAULT);
+          tbm_bo_alloc (bufmgr, src->ts_info.planes[0].size + src->ts_info.planes[1].size, TBM_BO_DEFAULT);
       if (out_buffer->bo[0] == NULL)
         goto failed;
 
       bo_num = 1;
 #else
       out_buffer->bo[0] =
-          tbm_bo_alloc (bufmgr, info.planes[0].size, TBM_BO_DEFAULT);
+          tbm_bo_alloc (bufmgr, src->ts_info.planes[0].size, TBM_BO_DEFAULT);
       if (out_buffer->bo[0] == NULL)
         goto failed;
 
       out_buffer->bo[1] =
-          tbm_bo_alloc (bufmgr, info.planes[1].size, TBM_BO_DEFAULT);
+          tbm_bo_alloc (bufmgr, src->ts_info.planes[1].size, TBM_BO_DEFAULT);
       if (out_buffer->bo[1] == NULL)
         goto failed;
 
@@ -712,7 +661,7 @@ tbm_buffer_create (GstWaylandSrc * src)
 #endif
 
       out_buffer->surface =
-          tbm_surface_internal_create_with_bos(&info, out_buffer->bo, bo_num);
+          tbm_surface_internal_create_with_bos(&src->ts_info, out_buffer->bo, bo_num);
 
       if (out_buffer->surface == NULL)
         goto failed;
@@ -724,7 +673,7 @@ tbm_buffer_create (GstWaylandSrc * src)
 
       wl_proxy_set_queue ((struct wl_proxy *)out_buffer->wl_buffer, src->queue);
 
-      out_buffer->size = info.size;
+      out_buffer->size = src->ts_info.size;
       break;
     default:
       GST_WARNING_OBJECT (src, "unknown format");
@@ -885,6 +834,9 @@ gst_wayland_src_init (GstWaylandSrc * src)
   src->height = DEFAULT_HEIGHT;
 
   src->use_tbm = TRUE;
+
+  src->allocator = gst_tizen_allocator_new();
+  GST_WARNING_OBJECT (src, "Allocator: New ptr is %" GST_PTR_FORMAT, src->allocator);
 }
 
 static void
@@ -941,6 +893,12 @@ gst_wayland_src_finalize (GObject * object)
   if (src->display) {
     gst_wayland_src_disconnect (src);
   }
+
+  if (src->allocator) {
+    GST_WARNING_OBJECT (src, "Allocator: Unref");
+    gst_object_unref(src->allocator);
+    src->allocator = NULL;
+  }
 }
 
 static gboolean
@@ -1103,6 +1061,8 @@ gst_wayland_src_set_caps (GstBaseSrc * psrc, GstCaps * caps)
   gint height = 0;
   gboolean ret = TRUE;
   const gchar *media_type = NULL;
+  int tbm_ret = 0;
+  tbm_surface_h t_surface = NULL;
 
   GST_WARNING_OBJECT (src, "set_caps : %" GST_PTR_FORMAT, caps);
   if (!src->display)
@@ -1161,11 +1121,36 @@ gst_wayland_src_set_caps (GstBaseSrc * psrc, GstCaps * caps)
   if (src->use_tbm && src->format == FOURCC_ARGB)
     src->format = TBM_FORMAT_ARGB8888;
 
+  /* get GstVideoInfo */
+  if (!gst_video_info_from_caps(&src->video_info, caps)) {
+    GST_ERROR_OBJECT(src, "get video info failed (caps[%p,%"GST_PTR_FORMAT"])", caps, caps);
+    return FALSE;
+  }
+
+  /* Create tbm_surface_info */
+  t_surface = tbm_surface_create(src->width, src->height, TBM_FORMAT_NV12);
+  if (!t_surface) {
+    GST_ERROR_OBJECT(src, "tbm surface create failed");
+    return FALSE;
+  }
+
+  memset(&src->ts_info, 0x0, sizeof(tbm_surface_info_s));
+
+  tbm_ret = tbm_surface_get_info(t_surface, &src->ts_info);
+  tbm_surface_destroy(t_surface);
+  t_surface = NULL;
+
+  if (tbm_ret != TBM_SURFACE_ERROR_NONE) {
+    GST_ERROR_OBJECT(src, "tbm surface info get failed");
+    return FALSE;
+  }
+
   GST_INFO_OBJECT (src, "format:%c%c%c%c, width: %d, height: %d",
       FOURCC_STR (src->format), src->width, src->height);
   GST_INFO_OBJECT (src, "FPS %d/%d", src->fps_n, src->fps_d);
   gst_wayland_src_thread_start (src);
 
+  GST_WARNING_OBJECT(src, "LEAVED - ret %d", ret);
   return TRUE;
 }
 
@@ -1186,6 +1171,11 @@ gst_wayland_src_start (GstBaseSrc * basesrc)
 
   src->last_frame_no = -1;
 
+#ifdef WAYLANDSRC_BUFFER_DUMP
+  dump_cnt = 0;
+  remove(WAYLANDSRC_DUMP_FILE);
+#endif
+
   return TRUE;
 }
 
index 16dd1f4..260d7de 100644 (file)
 G_BEGIN_DECLS
 #include <gst/gst.h>
 #include <gst/base/gstpushsrc.h>
+#include <gst/video/video-format.h>
+#include <gst/video/video-info.h>
+#include <gst/allocators/gsttizenmemory.h>
+
 #include <wayland-client.h>
 #include <tbm_bufmgr.h>
+#include <tbm_surface.h>
+#include <tbm_surface_internal.h>
+
 #define GST_TYPE_WAYLAND_SRC \
            (gst_wayland_src_get_type())
 #define GST_WAYLAND_SRC(obj) \
@@ -119,6 +126,10 @@ struct _GstWaylandSrc
   guint height;
   guint32 format;
   struct wayland_tbm_client *tbm_client;
+
+  GstAllocator *allocator;
+  GstVideoInfo video_info;
+  tbm_surface_info_s ts_info;
 };
 
 struct _GstWaylandSrcClass