[Spice-devel] [PATCH spice 09/13] server: Add GStreamer 1.0 support to the GStreamer video encoder. (take 4)

Francois Gouget fgouget at codeweavers.com
Tue Jul 21 11:01:33 PDT 2015


Signed-off-by: Francois Gouget <fgouget at codeweavers.com>
---

By default GStreamer 1.0 is used if available, otherwise GStreamer 0.10 
is used and Spice is compiled without GStreamer support as a last 
resort. It's possible to explicitly require a specific Gstreamer version 
with --enable-gstreamer=1.0 and --enable-gstreamer=0.10, or for any 
version with --enable-gstreamer=yes.

If you get an error while building the pipeline for MJPEG, then it 
probably means you need the fix for this bug:
https://bugzilla.gnome.org/show_bug.cgi?id=750398

Changes since take 3:
 - Check whether g_get_num_processors() is available for compatibility
   with glib 2.35 and older.

 - avenc_mjpeg needs the clock to be removed just like ffenc_mjpeg, its 
   0.10 counterpart.

 - Support for the new output buffer handling.


 configure.ac               | 35 +++++++++++++++----
 server/Makefile.am         | 12 +++++--
 server/gstreamer_encoder.c | 84 +++++++++++++++++++++++++++++++++++++++++-----
 server/red_dispatcher.c    |  2 +-
 4 files changed, 116 insertions(+), 17 deletions(-)

diff --git a/configure.ac b/configure.ac
index e5be699..702cd49 100644
--- a/configure.ac
+++ b/configure.ac
@@ -81,14 +81,32 @@ SPICE_CHECK_SMARTCARD([SMARTCARD])
 AM_CONDITIONAL(SUPPORT_SMARTCARD, test "x$have_smartcard" = "xyes")
 
 AC_ARG_ENABLE(gstreamer,
-              AS_HELP_STRING([--enable-gstreamer=@<:@auto/yes/no@:>@],
-                             [Enable GStreamer 0.10 support]),
+              AS_HELP_STRING([--enable-gstreamer=@<:@auto/0.10/1.0/yes/no@:>@],
+                             [Enable GStreamer support]),
               [],
               [enable_gstreamer="auto"])
 
-if test "x$enable_gstreamer" != "xno"; then
+if test "x$enable_gstreamer" != "xno" && test "x$enable_gstreamer" != "x0.10"; then
+    PKG_CHECK_MODULES(GSTREAMER_1_0, [gstreamer-1.0, gstreamer-app-1.0],
+                      [enable_gstreamer="1.0"
+                       have_gstreamer_1_0="yes"],
+                      [have_gstreamer_1_0="no"])
+    if test "x$have_gstreamer_1_0" = "xyes"; then
+        AC_SUBST(GSTREAMER_1_0_CFLAGS)
+        AC_SUBST(GSTREAMER_1_0_LIBS)
+        AS_VAR_APPEND([SPICE_REQUIRES], [" gstreamer-1.0 gstreamer-app-1.0"])
+        AC_DEFINE([HAVE_GSTREAMER_1_0], [1], [Define if supporting GStreamer 1.0])
+    elif test "x$enable_gstreamer" = "x1.0"; then
+        AC_MSG_ERROR([GStreamer 1.0 support requested but not found. You may set GSTREAMER_1_0_CFLAGS and GSTREAMER_1_0_LIBS to avoid the need to call pkg-config.])
+    fi
+else
+    have_gstreamer_1_0="no"
+fi
+AM_CONDITIONAL(SUPPORT_GSTREAMER_1_0, test "x$have_gstreamer_1_0" = "xyes")
+
+if test "x$enable_gstreamer" != "xno" && test "x$enable_gstreamer" != "x1.0"; then
     PKG_CHECK_MODULES(GSTREAMER_0_10, [gstreamer-0.10, gstreamer-app-0.10],
-                      [enable_gstreamer="yes"
+                      [enable_gstreamer="0.10"
                        have_gstreamer_0_10="yes"],
                       [have_gstreamer_0_10="no"])
     if test "x$have_gstreamer_0_10" = "xyes"; then
@@ -96,7 +114,7 @@ if test "x$enable_gstreamer" != "xno"; then
         AC_SUBST(GSTREAMER_0_10_LIBS)
         AS_VAR_APPEND([SPICE_REQUIRES], [" gstreamer-0.10 gstreamer-app-0.10"])
         AC_DEFINE([HAVE_GSTREAMER_0_10], [1], [Define if supporting GStreamer 0.10])
-    elif test "x$enable_gstreamer" = "xyes"; then
+    elif test "x$enable_gstreamer" = "x0.10"; then
         AC_MSG_ERROR([GStreamer 0.10 support requested but not found. You may set GSTREAMER_0_10_CFLAGS and GSTREAMER_0_10_LIBS to avoid the need to call pkg-config.])
     fi
 else
@@ -104,6 +122,11 @@ else
 fi
 AM_CONDITIONAL(SUPPORT_GSTREAMER_0_10, test "x$have_gstreamer_0_10" = "xyes")
 
+if test "x$enable_gstreamer" = "xyes"; then
+    AC_MSG_ERROR("GStreamer support requested but not found")
+fi
+AS_IF([test "x$enable_gstreamer" = "xauto"], [enable_gstreamer="no"])
+
 AC_ARG_ENABLE([automated_tests],
               AS_HELP_STRING([--enable-automated-tests], [Enable automated tests using spicy-screenshot (part of spice--gtk)]),,
               [enable_automated_tests="no"])
@@ -339,7 +362,7 @@ echo "
 
         Smartcard:                ${have_smartcard}
 
-        GStreamer 0.10:           ${have_gstreamer_0_10}
+        GStreamer:                ${enable_gstreamer}
 
         SASL support:             ${enable_sasl}
 
diff --git a/server/Makefile.am b/server/Makefile.am
index 4921bc3..9fb0c8e 100644
--- a/server/Makefile.am
+++ b/server/Makefile.am
@@ -12,6 +12,7 @@ AM_CPPFLAGS =					\
 	$(SLIRP_CFLAGS)				\
 	$(SMARTCARD_CFLAGS)			\
 	$(GSTREAMER_0_10_CFLAGS)		\
+	$(GSTREAMER_1_0_CFLAGS)			\
 	$(SSL_CFLAGS)				\
 	$(VISIBILITY_HIDDEN_CFLAGS)		\
 	$(WARN_CFLAGS)				\
@@ -42,7 +43,8 @@ libspice_server_la_LIBADD =						\
 	$(PIXMAN_LIBS)							\
 	$(SASL_LIBS)							\
 	$(SLIRP_LIBS)							\
-	$(GSTREAMER_0_10_LIBS)							\
+	$(GSTREAMER_0_10_LIBS)						\
+	$(GSTREAMER_1_0_LIBS)						\
 	$(SSL_LIBS)							\
 	$(Z_LIBS)							\
 	$(SPICE_NONPKGCONFIG_LIBS)					\
@@ -142,7 +144,13 @@ endif
 
 if SUPPORT_GSTREAMER_0_10
 libspice_server_la_SOURCES +=	\
-	gstreamer_encoder.c		\
+	gstreamer_encoder.c			\
+	$(NULL)
+endif
+
+if SUPPORT_GSTREAMER_1_0
+libspice_server_la_SOURCES +=	\
+	gstreamer_encoder.c			\
 	$(NULL)
 endif
 
diff --git a/server/gstreamer_encoder.c b/server/gstreamer_encoder.c
index f7938ae..ac8f5c0 100644
--- a/server/gstreamer_encoder.c
+++ b/server/gstreamer_encoder.c
@@ -49,6 +49,9 @@ typedef struct {
 struct GstVideoBuffer {
     VideoBuffer base;
     GstBuffer *gst_buffer;
+#ifndef HAVE_GSTREAMER_0_10
+    GstMapInfo map_info;
+#endif
     gboolean persistent;
 };
 
@@ -105,6 +108,9 @@ static inline GstVideoBuffer* gst_video_buffer_ref(GstVideoBuffer *buffer)
 static void gst_video_buffer_unref(GstVideoBuffer *buffer)
 {
     if (--buffer->base.ref_count == 0) {
+#ifndef HAVE_GSTREAMER_0_10
+        gst_buffer_unmap(buffer->gst_buffer, &buffer->map_info);
+#endif
         gst_buffer_unref(buffer->gst_buffer);
         if (!buffer->persistent) {
             free(buffer);
@@ -203,7 +209,13 @@ static SpiceFormatForGStreamer *map_format(SpiceBitmapFmt format)
 
 static gboolean set_appsrc_caps(GstEncoder *encoder)
 {
-    GstCaps *new_caps = gst_caps_new_simple("video/x-raw-rgb",
+    GstCaps *new_caps = gst_caps_new_simple(
+#ifdef HAVE_GSTREAMER_0_10
+        "video/x-raw-rgb",
+#else
+        "video/x-raw",
+        "format", G_TYPE_STRING, "BGRx",
+#endif
         "bpp", G_TYPE_INT, encoder->format->bpp,
         "depth", G_TYPE_INT, encoder->format->depth,
         "width", G_TYPE_INT, encoder->width,
@@ -235,7 +247,11 @@ static gboolean construct_pipeline(GstEncoder *encoder, const SpiceBitmap *bitma
     switch (encoder->base.codec_type)
     {
     case SPICE_VIDEO_CODEC_TYPE_MJPEG:
+#ifdef HAVE_GSTREAMER_0_10
         gstenc_name = "ffenc_mjpeg";
+#else
+        gstenc_name = "avenc_mjpeg";
+#endif
         no_clock = TRUE;
         break;
     case SPICE_VIDEO_CODEC_TYPE_VP8:
@@ -245,9 +261,14 @@ static gboolean construct_pipeline(GstEncoder *encoder, const SpiceBitmap *bitma
         spice_warning("unsupported codec type %d", encoder->base.codec_type);
         return FALSE;
     }
+#ifdef HAVE_GSTREAMER_0_10
+    const gchar *converter = "ffmpegcolorspace";
+#else
+    const gchar *converter = "videoconvert";
+#endif
 
     GError *err = NULL;
-    gchar *desc = g_strdup_printf("appsrc name=src format=2 do-timestamp=true ! ffmpegcolorspace ! %s name=encoder ! appsink name=sink", gstenc_name);
+    gchar *desc = g_strdup_printf("appsrc name=src format=2 do-timestamp=true ! %s ! %s name=encoder ! appsink name=sink", converter, gstenc_name);
     spice_debug("GStreamer pipeline: %s", desc);
     encoder->pipeline = gst_parse_launch_full(desc, NULL, GST_PARSE_FLAG_FATAL_ERRORS, &err);
     g_free(desc);
@@ -262,8 +283,13 @@ static gboolean construct_pipeline(GstEncoder *encoder, const SpiceBitmap *bitma
 
     /* Configure the encoders for a zero-frame latency, and real-time speed */
     adjust_bit_rate(encoder);
-    g_object_set(G_OBJECT(encoder->gstenc), "bitrate", encoder->bit_rate, NULL);
-    if (encoder->base.codec_type == SPICE_VIDEO_CODEC_TYPE_VP8) {
+    switch (encoder->base.codec_type) {
+    case SPICE_VIDEO_CODEC_TYPE_MJPEG:
+        g_object_set(G_OBJECT(encoder->gstenc),
+                     "bitrate", encoder->bit_rate,
+                     NULL);
+        break;
+    case SPICE_VIDEO_CODEC_TYPE_VP8: {
         /* See http://www.webmproject.org/docs/encoder-parameters/ */
 #ifdef HAVE_G_GET_NUMPROCESSORS
         int core_count = g_get_num_processors();
@@ -271,13 +297,27 @@ static gboolean construct_pipeline(GstEncoder *encoder, const SpiceBitmap *bitma
         int core_count = 1;
 #endif
         g_object_set(G_OBJECT(encoder->gstenc),
+#ifdef HAVE_GSTREAMER_0_10
                      "mode", 1, /* CBR */
+                     "bitrate", encoder->bit_rate,
                      "max-latency", 0,
                      "speed", 7,
+#else
+                     "end-usage", 1, /* CBR */
+                     "target-bitrate", encoder->bit_rate,
+                     "lag-in-frames", 0,
+                     "deadline", GST_SECOND / get_source_fps(encoder) / 2,
+#endif
                      "resize-allowed", TRUE,
                      "threads", core_count - 1,
                      NULL);
-   }
+        break;
+        }
+    default:
+        spice_warning("unknown encoder type %d", encoder->base.codec_type);
+        reset_pipeline(encoder);
+        return FALSE;
+    }
 
     /* Set the source caps */
     encoder->src_caps = NULL;
@@ -323,7 +363,14 @@ static int push_raw_frame(GstEncoder *encoder, const SpiceBitmap *bitmap,
     const uint32_t stream_stride = (src->right - src->left) * encoder->format->bpp / 8;
     uint32_t len = stream_stride * stream_height;
     GstBuffer *buffer = gst_buffer_new_and_alloc(len);
-    uint8_t *dst = GST_BUFFER_DATA(buffer);
+#ifdef HAVE_GSTREAMER_0_10
+    uint8_t *b = GST_BUFFER_DATA(buffer);
+#else
+    GstMapInfo map;
+    gst_buffer_map(buffer, &map, GST_MAP_WRITE);
+    uint8_t *b = map.data;
+#endif
+    uint8_t *dst = b;
 
     /* Note that we should not reorder the lines, even if top_down is false.
      * It just changes the number of lines to skip at the start of the bitmap.
@@ -382,10 +429,13 @@ static int push_raw_frame(GstEncoder *encoder, const SpiceBitmap *bitmap,
             dst += stream_stride;
             chunk_offset += bitmap->stride;
         }
-        spice_assert(dst - GST_BUFFER_DATA(buffer) == len);
+        spice_assert(dst - b == len);
     }
-
+#ifdef HAVE_GSTREAMER_0_10
     gst_buffer_set_caps(buffer, encoder->src_caps);
+#else
+    gst_buffer_unmap(buffer, &map);
+#endif
     GST_BUFFER_OFFSET(buffer) = encoder->frame++;
 
     GstFlowReturn ret = gst_app_src_push_buffer(encoder->appsrc, buffer);
@@ -408,6 +458,7 @@ static int pull_compressed_buffer(GstEncoder *encoder, GstVideoBuffer **buffer)
     } else {
         video_buffer = create_gst_video_buffer(FALSE);
     }
+#ifdef HAVE_GSTREAMER_0_10
     video_buffer->gst_buffer = gst_app_sink_pull_buffer(encoder->appsink);
     if (video_buffer->gst_buffer) {
         video_buffer->base.data = GST_BUFFER_DATA(video_buffer->gst_buffer);
@@ -416,6 +467,23 @@ static int pull_compressed_buffer(GstEncoder *encoder, GstVideoBuffer **buffer)
         return VIDEO_ENCODER_FRAME_ENCODE_DONE;
     }
     video_buffer->base.unref(video_buffer);
+#else
+    GstSample *sample = gst_app_sink_pull_sample(encoder->appsink);
+    if (sample) {
+        video_buffer->gst_buffer = gst_sample_get_buffer(sample);
+        if (video_buffer->gst_buffer) {
+            gst_buffer_ref(video_buffer->gst_buffer);
+            video_buffer->base.size = gst_buffer_get_size(video_buffer->gst_buffer);
+            gst_buffer_map(video_buffer->gst_buffer, &video_buffer->map_info,
+                           GST_MAP_READ);
+            video_buffer->base.data = video_buffer->map_info.data;
+            *buffer = video_buffer;
+            gst_sample_unref(sample);
+            return VIDEO_ENCODER_FRAME_ENCODE_DONE;
+        }
+        gst_sample_unref(sample);
+    }
+#endif
     return VIDEO_ENCODER_FRAME_UNSUPPORTED;
 }
 
diff --git a/server/red_dispatcher.c b/server/red_dispatcher.c
index dc198e9..d896d00 100644
--- a/server/red_dispatcher.c
+++ b/server/red_dispatcher.c
@@ -258,7 +258,7 @@ static const EnumNames video_encoder_names[] = {
 
 static create_video_encoder_proc video_encoder_procs[] = {
     &create_mjpeg_encoder,
-#ifdef HAVE_GSTREAMER_0_10
+#if defined(HAVE_GSTREAMER_0_10) || defined(HAVE_GSTREAMER_1_0)
     &create_gstreamer_encoder,
 #else
     NULL,
-- 
2.1.4



More information about the Spice-devel mailing list