Skip to contentSkip to conversation

dcc: Create a stream for non-gl/remote clients that want to use dmabuf (v9)

PATCH New Michael Scherle 1 participant 12 messages Feb 28, 2025 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

cover dcc: Create a stream for non-gl/remote clients that want to use dmabuf (v9)

This merge request is based on Vivek Kasireddy's
[PATCH v8 0/6] dcc: Create a stream for non-gl/remote clients that want to use dmabuf (v8), who gave me the permission to pursue the merge request. I also submitted this directly on gitlab but but I'm now also submitting it via the mailing list .I have made my changes in separate commits, should I merge them with the existing commits?

Original Description:
For clients that cannot accept a dmabuf fd directly (such as those running on a remote system), this patch series provides a way for the Spice server to stream the gl/dmabuf data/buffer instead. This is mostly done by enabling the creation of Gst memory using a dmabuf fd as the source. This ability is useful given that dmabuf is the standard mechanism for sharing buffers between various drivers and userspace in many Graphics and Media usecases. Currently, this is tested with Qemu and remote-viewer using the x264enc/avdec_h264 and msdkh264enc/dec plugins to stream the Guest/VM desktop but it can be easily extended to other plugins and applications. Here is roughly how things work:

The application (e.g, Qemu) chooses its preferred codec (a Gstreamer one) and calls gl_scanout (to update the fd) followed by gl_draw. In response, the Spice server checks to see if the client is capable of accepting a dmabuf fd directly or not. If yes, the fd is forwarded directly to the client; otherwise, a new stream is created. The Spice server then sends the dmabuf fd to the Gstreamer encoder which uses it as an input for creating an encoded buffer which is then sent to the client.
Once the encoding process is done, an async completion cookie is sent to the application.

Here is a link to the previous version that used a drawable to share the dmabuf fd with the Gstreamer encoder:
https://lists.freedesktop.org/archives/spice-devel/2023-January/052948.html
This version is tested together with following (required) patches in qemu:
https://gitlab.freedesktop.org/Vivek/qemu/-/commits/spice_gl_on_v4

Changelog:
v9:

updated the spice-common submodule, which is needed for udev helper to identify GPU vendor include dmabuf encoding only if dmabuf supported and gstreamer >= 1.24
fix a race condition when the video stream is stopped while encoding a frame

v8:

Added a new gstreamer-encoder patch to this series to convert drm format shared by the VMM to the appropriate Gstreamer format.

v7:

Revert back to the previous design where we do not share fd with the stream and scanout is the sole owner of the fd. This is because sharing fd ownership opens up a lot of corner cases.

v6: (Frediano)

Properly share ownership of the dmabuf fd between stream and scanout Ensure that a newly created stream is associated with all connected clients

v5:

Addressed review comments from Frediano mainly regarding adding autoconf support for gstreamer-allocators dependency and not needing to access scanout as part of gl draw operation

v4:

Test with Virgl enabled
Associate dmabuf's y0_top with stream's top_down variable

v3:

Updated the second patch to have a new primary surface created whenever a new stream gets created. This will avoid having to trigger primary surface creation from Qemu. And, this change also fixes the following error seen with v2:
../server/display-channel.cpp:2074:display_channel_create_surface:
condition `!display->priv->surfaces[surface_id]' failed Rebase all patches on master

v2:

Update all patches to address review comments from Frediano Tested this series with msdkh264enc/dec plugins that will be added in another patch series

Michael Scherle (3):
Update spice-common submodule
gstreamer-encoder: Include dmabuf encoding conditionally for Linux
dcc-send: Fix freeze when video stream is stopped during ongoing draw

Vivek Kasireddy (7):
gstreamer-encoder: Use a h/w based encoder with Intel GPUs if possible

    (v5)
  dcc: Check to see if the client supports multiple codecs (v2)
  dcc: Create a stream associated with gl_draw for non-gl clients (v6)
  dcc-send: Encode and send gl_draw stream data to the remote client
    (v4)
  gstreamer-encoder: Add an encoder function that takes dmabuf fd as
    input (v3)

video-stream: Don't stop a stream associated with gl_draw (v2)
gstreamer-encoder: Map the drm format to appropriate Gstreamer format

 configure.ac                     |   2 +-
 meson.build                      |  12 +-
 server/dcc-send.cpp              | 163 ++++++++++---
 server/dcc.cpp                   |  31 ++-
 server/dcc.h                     |   6 +
 server/display-channel-private.h |   1 +
 server/display-channel.cpp       |   1 +
 server/gstreamer-encoder.c       | 389 ++++++++++++++++++++++++++++---
 server/video-encoder.h           |  14 ++
 server/video-stream.cpp          | 201 +++++++++++++---
 server/video-stream.h            |   4 +
 subprojects/spice-common         |   2 +-
 12 files changed, 717 insertions(+), 109 deletions(-)
Signature · 1 line
2.48.1
#1 lore raw

PATCH 1/10 gstreamer-encoder: Use a h/w based encoder with Intel GPUs if possible (v5)

From: Vivek Kasireddy <[email protected]>

Once it is determined that an Intel GPU is available/active (after looking into udev's database), we try to see if there is a h/w based encoder (element) available (in Gstreamer's registry cache) for the user selected video codec. In other words, if we find that the Intel Media SDK Gstreamer plugin (libgstmsdk.so) and associated libraries (such as va or vaapi) are all installed properly, we add the appropriate h/w based encoder and post-processor/converter elements to the pipeline (along with any relevant options) instead of the s/w based elements.

For example, if the user selects h264 as the preferred codec format, msdkh264enc and vapostproc will be preferred instead of x264enc and videoconvert.

v2: (addressed some review comments from Frediano)
- Moved the udev helper into spice-common
- Refactored the code to choose plugins in order msdk > va > vaapi

v3: (Frediano)
- Added relevant encoder options for mjpeg and vp9 codecs (Jin Chung)

v4: (Fixups from Frediano)
- Free the encoder when we cannot find vpp
- Change type and find plugins array length using G_N_ELEMENTS
- Fix gstenc_name UAF by freeing it at the end of the function
- Use g_str_has_prefix instead of strstr
- Include the string "_hw_" in function names that deal with h/w based plugins
- Rebase on master

v5: rebase on master

Cc Frediano Ziglio <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Marc-André Lureau <[email protected]>
Cc Dongwon Kim <[email protected]>
---
 server/gstreamer-encoder.c | 123 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 122 insertions(+), 1 deletion(-)
server/gstreamer-encoder.c +122 -1
index 40882f69..be8e3111 100644
@@ -38,6 +38,7 @@
 #include "red-common.h"
 #include "video-encoder.h"
 #include "utils.h"
+#include "common/udev.h"
 
 
 #define SPICE_GST_DEFAULT_FPS 30
@@ -861,6 +862,116 @@ static const gchar* get_gst_codec_name(const SpiceGstEncoder *encoder)
     }
 }
 
+static const char video_codecs[][8] = {
+    { "" },
+    { "mjpeg" },
+    { "vp8" },
+    { "h264" },
+    { "vp9" },
+    { "h265" },
+};
+
+static bool gst_features_lookup(const gchar *feature_name)
+{
+    GstRegistry *registry;
+    GstPluginFeature *feature;
+
+    registry = gst_registry_get();
+    if (!registry) {
+        return false;
+    }
+
+    feature = gst_registry_lookup_feature(registry, feature_name);
+    if (!feature) {
+        return false;
+    }
+
+    gst_object_unref(feature);
+    return true;
+}
+
+static gchar *find_best_hw_plugin(const gchar *codec_name)
+{
+    static const char plugins[][8] = {"msdk", "va", "vaapi"};
+    gchar *feature_name;
+    int i;
+
+    for (i = 0; i < G_N_ELEMENTS(plugins); i++) {
+        feature_name = !codec_name ? g_strconcat(plugins[i], "postproc", NULL) :
+                       g_strconcat(plugins[i], codec_name, "enc", NULL);
+        if (!gst_features_lookup(feature_name)) {
+            g_free(feature_name);
+            feature_name = NULL;
+            continue;
+        }
+        break;
+    }
+    return feature_name;
+}
+
+static gchar *get_hw_gstenc_opts(gchar *encoder, const gchar *codec_name)
+{
+    gchar *gstenc_opts;
+
+    if (strcmp(codec_name, "mjpeg") == 0) {
+        return g_strdup("");
+    }
+
+    if (g_str_has_prefix(encoder, "msdk")) {
+        if (strcmp(codec_name, "vp9") == 0) {
+            gstenc_opts = g_strdup("async-depth=1 b-frames=0 rate-control=3 target-usage=7");
+        } else {
+            gstenc_opts = g_strdup("async-depth=1 rate-control=3 gop-size=1 tune=16 b-frames=0 target-usage=7 min-qp=15 max-qp=35");
+        }
+    } else if (g_str_has_prefix(encoder, "vaapi")) {
+        if (strcmp(codec_name, "vp9") == 0) {
+            gstenc_opts = g_strdup("tune=3 rate-control=1");
+        } else {
+            gstenc_opts = g_strdup("rate-control=cqp max-bframes=0 min-qp=15 max-qp=35");
+        }
+    } else {
+        if (strcmp(codec_name, "vp9") == 0) {
+            gstenc_opts = g_strdup("min-qp=15 max-qp=35 rate-control=16 ref-frames=0 target-usage=7");
+        } else {
+            gstenc_opts = g_strdup("rate-control=16 b-frames=0 target-usage=7 min-qp=15 max-qp=35");
+        }
+    }
+    return gstenc_opts;
+}
+
+static void try_intel_hw_plugins(const gchar *codec_name, gchar **converter,
+                                 gchar **gstenc_name, gchar **gstenc_opts)
+{
+    gchar *encoder, *vpp;
+
+    if (strcmp(codec_name, "vp8") == 0) {
+        return;
+    }
+
+    encoder = find_best_hw_plugin(codec_name);
+    if (!encoder) {
+        return;
+    }
+    vpp = find_best_hw_plugin(NULL);
+    if (!vpp) {
+        g_free(encoder);
+        return;
+    }
+
+    g_free(*converter);
+    g_free(*gstenc_name);
+    g_free(*gstenc_opts);
+    *gstenc_name = encoder;
+    *gstenc_opts = get_hw_gstenc_opts(encoder, codec_name);
+
+    if (g_str_has_prefix(vpp, "vaapi")) {
+        *converter = g_strconcat(vpp, " ! video/x-raw(memory:VASurface),format=NV12", NULL);
+    } else {
+        *converter = g_strconcat(vpp, " ! video/x-raw(memory:VAMemory),format=NV12", NULL);
+    }
+    g_free(vpp);
+}
+
 /* At this time, only the following formats are supported by x264enc. */
 static const char valid_formats[][10] = {
     { "Y444" },
@@ -900,7 +1011,7 @@ static gchar *get_gst_converter(void)
 
 static gboolean create_pipeline(SpiceGstEncoder *encoder)
 {
-    const gchar* gstenc_name = get_gst_codec_name(encoder);
+    gchar* gstenc_name = g_strdup(get_gst_codec_name(encoder));
     if (!gstenc_name) {
         return FALSE;
     }
@@ -947,10 +1058,18 @@ static gboolean create_pipeline(SpiceGstEncoder *encoder)
     default:
         /* gstreamer_encoder_new() should have rejected this codec type */
         spice_warning("unsupported codec type %d", encoder->base.codec_type);
+        g_free(gstenc_name);
         g_free(converter);
         return FALSE;
     }
 
+    const char *codec_name = video_codecs[encoder->base.codec_type];
+    GpuVendor vendor = spice_udev_detect_gpu(INTEL_VENDOR_ID);
+    if (vendor == VENDOR_GPU_DETECTED) {
+        try_intel_hw_plugins(codec_name, &converter, &gstenc_name,
+                             &gstenc_opts);
+    }
+
     GError *err = NULL;
     gchar *desc = g_strdup_printf("appsrc is-live=true format=time do-timestamp=true name=src !"
                                   " %s ! %s name=encoder %s ! appsink name=sink",
@@ -967,6 +1086,7 @@ static gboolean create_pipeline(SpiceGstEncoder *encoder)
             gst_object_unref(encoder->pipeline);
             encoder->pipeline = NULL;
         }
+        g_free(gstenc_name);
         return FALSE;
     }
     encoder->appsrc = GST_APP_SRC(gst_bin_get_by_name(GST_BIN(encoder->pipeline), "src"));
@@ -1003,6 +1123,7 @@ static gboolean create_pipeline(SpiceGstEncoder *encoder)
                                   SPICE_GST_VIDEO_PIPELINE_BITRATE |
                                   SPICE_GST_VIDEO_PIPELINE_CAPS);
 
+    g_free(gstenc_name);
     return TRUE;
 }
 
Signature · 1 line
2.48.1
#2 lore raw

PATCH 2/10 dcc: Check to see if the client supports multiple codecs (v2)

In reply to Michael Scherle

From: Vivek Kasireddy <[email protected]>

We need to determine if the client is new enough to support multiple codecs -- which might include any of the Gstreamer based ones.

v2: (suggestions and fixups from Frediano)
- Add is_gl_client() method to DisplayChannelClient instead of a dcc_is_gl_client() function.
- Avoid the usage of XXX_CAST macro.

Cc Frediano Ziglio <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Marc-André Lureau <[email protected]>
Cc Dongwon Kim <[email protected]>
---
 server/dcc.cpp | 22 ++++++++++++----------
 server/dcc.h   |  6 ++++++
 2 files changed, 18 insertions(+), 10 deletions(-)
server/dcc.cpp +12 -10
index b04e5bc0..edec429d 100644
@@ -494,29 +494,31 @@ RedSurfaceDestroyItem::RedSurfaceDestroyItem(uint32_t surface_id)
 
 RedPipeItemPtr dcc_gl_scanout_item_new(RedChannelClient *rcc, void *data, int num)
 {
-    /* FIXME: on !unix peer, start streaming with a video codec */
-    if (!red_stream_is_plain_unix(rcc->get_stream()) ||
-        !rcc->test_remote_cap(SPICE_DISPLAY_CAP_GL_SCANOUT)) {
+    auto dcc = static_cast<DisplayChannelClient *>(rcc);
+
+    if (dcc->is_gl_client()) {
+        return red::make_shared<RedGlScanoutUnixItem>();
+    } else if (rcc->test_remote_cap(SPICE_DISPLAY_CAP_MULTI_CODEC)) {
+        return RedPipeItemPtr();
+    } else {
         red_channel_warning(rcc->get_channel(),
-                            "FIXME: client does not support GL scanout");
+                            "Client does not support GL scanout or multiple codecs");
         rcc->disconnect();
         return RedPipeItemPtr();
     }
-
-    return red::make_shared<RedGlScanoutUnixItem>();
 }
 
 XXX_CAST(RedChannelClient, DisplayChannelClient, DISPLAY_CHANNEL_CLIENT);
 
 RedPipeItemPtr dcc_gl_draw_item_new(RedChannelClient *rcc, void *data, int num)
 {
-    DisplayChannelClient *dcc = DISPLAY_CHANNEL_CLIENT(rcc);
+    auto dcc = static_cast<DisplayChannelClient *>(rcc);
     auto draw = static_cast<const SpiceMsgDisplayGlDraw *>(data);
 
-    if (!red_stream_is_plain_unix(rcc->get_stream()) ||
-        !rcc->test_remote_cap(SPICE_DISPLAY_CAP_GL_SCANOUT)) {
+    if (!dcc->is_gl_client() &&
+        !rcc->test_remote_cap(SPICE_DISPLAY_CAP_MULTI_CODEC)) {
         red_channel_warning(rcc->get_channel(),
-                            "FIXME: client does not support GL scanout");
+                            "Client does not support GL scanout or multiple codecs");
         rcc->disconnect();
         return RedPipeItemPtr();
     }
server/dcc.h +6 -0
index bb441905..4e7150d5 100644
@@ -47,6 +47,12 @@ public:
                          spice_wan_compression_t zlib_glz_state);
     virtual void disconnect() override;
 
+    bool is_gl_client()
+    {
+        return red_stream_is_plain_unix(get_stream()) &&
+           test_remote_cap(SPICE_DISPLAY_CAP_GL_SCANOUT);
+    }
+
 protected:
     virtual bool handle_message(uint16_t type, uint32_t size, void *msg) override;
     virtual bool config_socket() override;
Signature · 1 line
2.48.1
#3 lore raw

PATCH 3/10 dcc: Create a stream associated with gl_draw for non-gl clients (v6)

In reply to Michael Scherle

From: Vivek Kasireddy <[email protected]>

For non-gl/remote clients, if there is no stream associated with the DisplayChannel, then we create a new stream. Otherwise, we just update the current stream's timestamp.

v2: (suggestions and fixups from Frediano)
- Moved the gl_draw_stream object from DCC to DC
- Moved the stream initialization code from display_channel_create_stream() into a separate function that is reused when creating gl_draw_stream

v3:
- Create a new primary surface whenever a new stream gets created

v4:
- Use nullptr instead of NULL and true instead of TRUE (Frediano)
- Create the stream as part of gl scanout instead of gl draw operation so that if would be easily possible to obtain key params such as stride, flags, etc
- Store key params such as fd, flags, stride, etc in the stream so that we do not have to look at scanout again

v5: (Frediano)
- No need to pass scanout object to create_gl_draw_stream as it is always NULL
- Don't compute the stream's last_time if drawable is valid
- Let the default input_fps be MAX_FPS
- Use uint32_t type for stride
- Make sure that a newly created stream is provided to all connected clients
- When the scanout's drm_dma_buf_fd is associated with a stream, take an additional reference on the fd to ensure that it is not closed when the stream might still be using it

v6:
- Add a new helper is_new_stream_needed() to determine when to create new stream
- Add more checks to see if the encoder is created or not
- Don't share the dmabuf fd with the stream and instead keep the ownerhip of the fd with the scanout

Cc Frediano Ziglio <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Marc-André Lureau <[email protected]>
Cc Dongwon Kim <[email protected]>
---
 server/dcc.cpp                   |   9 ++
 server/display-channel-private.h |   1 +
 server/display-channel.cpp       |   1 +
 server/video-stream.cpp          | 194 ++++++++++++++++++++++++++-----
 server/video-stream.h            |   4 +
 5 files changed, 180 insertions(+), 29 deletions(-)
server/dcc.cpp +9 -0
index edec429d..58170681 100644
@@ -523,6 +523,15 @@ RedPipeItemPtr dcc_gl_draw_item_new(RedChannelClient *rcc, void *data, int num)
         return RedPipeItemPtr();
     }
 
+    if (!dcc->is_gl_client()) {
+        if (!display_channel_update_gl_draw_stream(dcc, draw)) {
+            red_channel_warning(rcc->get_channel(),
+                                "Cannot update GL stream");
+            rcc->disconnect();
+            return RedPipeItemPtr();
+        }
+    }
+
     dcc->priv->gl_draw_ongoing = TRUE;
     auto item = red::make_shared<RedGlDrawItem>();
     item->draw = *draw;
server/display-channel-private.h +1 -0
index 04ac2c0d..e0693f54 100644
@@ -110,6 +110,7 @@ struct DisplayChannelPrivate
     uint32_t stream_count;
     std::array<VideoStream, NUM_STREAMS> streams_buf;
     VideoStream *free_streams;
+    VideoStream *gl_draw_stream;
     Ring streams;
     std::array<ItemTrace, NUM_TRACE_ITEMS> items_trace;
     uint32_t next_item_trace;
server/display-channel.cpp +1 -0
index 4bd0cf41..53a4724d 100644
@@ -2172,6 +2172,7 @@ display_channel_new(RedsState *reds, QXLInstance *qxl,
                                          video_codecs, n_surfaces);
     if (display) {
         display_channel_set_stream_video(display.get(), stream_video);
+        display->priv->gl_draw_stream = nullptr;
     }
     return display;
 }
server/video-stream.cpp +165 -29

216 lines not on this page · raw message

server/video-stream.h +4 -0
index fc5b03dd..d3f25bb3 100644
@@ -112,6 +112,7 @@ struct VideoStream {
     red_time_t last_time;
     int width;
     int height;
+    uint32_t stride;
     SpiceRect dest_area;
     int top_down;
     VideoStream *next;
@@ -123,6 +124,9 @@ struct VideoStream {
 };
 
 void display_channel_init_video_streams(DisplayChannel *display);
+bool display_channel_create_gl_draw_stream(DisplayChannel *display);
+bool display_channel_update_gl_draw_stream(DisplayChannelClient *dcc,
+                                           const SpiceMsgDisplayGlDraw *draw);
 void video_stream_stop(DisplayChannel *display, VideoStream *stream);
 void video_stream_trace_update(DisplayChannel *display, Drawable *drawable);
 void video_stream_maintenance(DisplayChannel *display, Drawable *candidate,
Signature · 1 line
2.48.1
#4 lore raw

PATCH 4/10 dcc-send: Encode and send gl_draw stream data to the remote client (v4)

In reply to Michael Scherle

From: Vivek Kasireddy <[email protected]>

For remote (or non-gl) clients, if a valid gl_draw stream exists, then we first extract the dmabuf fd associated with the scanout and share it with the encoder along with other key parameters such as stride, width and height. Once the encoder finishes creating an encoded buffer (using the dmabuf fd as input), we then send it over to the client. And, as soon as the encoder notifies that it is no longer using the dmabuf fd, we send a gl_draw_done async to the application.

v2: (suggestions and fixups from Frediano)
- Moved the DisplayStreamData initialization code from red_marshall_stream_data() into a separate function that is reused when marshalling gl_draw_stream.
- Used new/delete instead of g_new/g_free for creating and destroying dmabuf_data object
- s/notify_mem_free/free
s/red_gst_mem_free_cb/red_free_fb
- Removed the usage of opaque from red_free_cb

v3:
- Obtain the key params such as fd, stride, etc from the stream instead of the scanout
- Replace the switch with if in red_marshall_gl_draw_stream() to avoid printing a warning (and spamming the console) when a frame is dropped

v4:
- Slightly improve the readability of red_marshall_gl_draw_stream() by adding checks at the start

Cc Frediano Ziglio <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Marc-André Lureau <[email protected]>
Cc Dongwon Kim <[email protected]>
---
 server/dcc-send.cpp    | 158 +++++++++++++++++++++++++++++++++--------
 server/video-encoder.h |  13 ++++
 2 files changed, 143 insertions(+), 28 deletions(-)
server/dcc-send.cpp +130 -28
index 2c40a231..c418f375 100644
@@ -1637,6 +1637,60 @@ static void red_release_video_encoder_buffer(uint8_t *data, void *opaque)
     buffer->free(buffer);
 }
 
+static void red_init_display_stream_data(DisplayChannelClient *dcc,
+                                         SpiceMarshaller *base_marshaller,
+                                         Drawable *drawable, int stream_id,
+                                         VideoBuffer *outbuf, int is_sized)
+{
+    SpiceMsgDisplayStreamData stream_data;
+    SpiceMsgDisplayStreamDataSized stream_data_sized;
+    SpiceStreamDataHeader *base;
+    SpiceCopy *copy;
+    uint32_t frame_mm_time = reds_get_mm_time();
+    uint16_t msg_type = is_sized ? SPICE_MSG_DISPLAY_STREAM_DATA_SIZED :
+                        SPICE_MSG_DISPLAY_STREAM_DATA;
+
+    dcc->init_send_data(msg_type);
+
+    base = is_sized ? &stream_data_sized.base : &stream_data.base;
+    base->id = stream_id;
+    base->multi_media_time = frame_mm_time;
+    stream_data.data_size = outbuf->size;
+
+    if (is_sized) {
+        copy = &drawable->red_drawable->u.copy;
+        frame_mm_time = drawable->red_drawable->mm_time ?
+                        drawable->red_drawable->mm_time :
+                        reds_get_mm_time();
+
+        stream_data_sized.base.multi_media_time = frame_mm_time;
+        stream_data_sized.width = copy->src_area.right - copy->src_area.left;
+        stream_data_sized.height = copy->src_area.bottom - copy->src_area.top;
+        stream_data_sized.dest = drawable->red_drawable->bbox;
+        stream_data_sized.data_size = outbuf->size;
+
+        spice_debug("stream %d: sized frame: dest ==> ",
+                    stream_data_sized.base.id);
+        rect_debug(&stream_data_sized.dest);
+        spice_marshall_msg_display_stream_data_sized(base_marshaller,
+                                                     &stream_data_sized);
+    } else {
+        spice_marshall_msg_display_stream_data(base_marshaller, &stream_data);
+    }
+
+    spice_marshaller_add_by_ref_full(base_marshaller, outbuf->data,
+                                     outbuf->size,
+                                     &red_release_video_encoder_buffer,
+                                     outbuf);
+#ifdef STREAM_STATS
+    VideoStreamAgent *agent = &dcc->priv->stream_agents[stream_id];
+
+    agent->stats.num_frames_sent++;
+    agent->stats.size_sent += outbuf->size;
+    agent->stats.end = frame_mm_time;
+#endif
+}
+
 static bool red_marshall_stream_data(DisplayChannelClient *dcc,
                                      SpiceMarshaller *base_marshaller,
                                      Drawable *drawable)
@@ -1693,41 +1747,81 @@ static bool red_marshall_stream_data(DisplayChannelClient *dcc,
         return FALSE;
     }
 
-    if (!is_sized) {
-        SpiceMsgDisplayStreamData stream_data;
+    red_init_display_stream_data(dcc, base_marshaller, drawable,
+                                 stream_id, outbuf, is_sized);
+    return TRUE;
+}
 
-        dcc->init_send_data(SPICE_MSG_DISPLAY_STREAM_DATA);
+static void red_free_cb(VideoEncoderDmabufData *dmabuf_data)
+{
+    auto dcc = static_cast<DisplayChannelClient *>(dmabuf_data->dcc);
+    DisplayChannel *display = DCC_TO_DC(dcc);
 
-        stream_data.base.id = stream_id;
-        stream_data.base.multi_media_time = frame_mm_time;
-        stream_data.data_size = outbuf->size;
+    dcc->priv->gl_draw_ongoing = false;
+    display_channel_gl_draw_done(display);
+    delete dmabuf_data;
+}
 
-        spice_marshall_msg_display_stream_data(base_marshaller, &stream_data);
-    } else {
-        SpiceMsgDisplayStreamDataSized stream_data;
+static void red_marshall_gl_draw_stream(DisplayChannelClient *dcc,
+                                        SpiceMarshaller *base_marshaller)
+{
+    DisplayChannel *display = DCC_TO_DC(dcc);
+    VideoStream *stream = display->priv->gl_draw_stream;
+    int stream_id = display_channel_get_video_stream_id(display, stream);
+    VideoStreamAgent *agent = &dcc->priv->stream_agents[stream_id];
 
-        dcc->init_send_data(SPICE_MSG_DISPLAY_STREAM_DATA_SIZED);
+    if (!agent->video_encoder || !agent->video_encoder->encode_dmabuf) {
+        spice_warning("No video encoder available for this stream");
+        return;
+    }
 
-        stream_data.base.id = stream_id;
-        stream_data.base.multi_media_time = frame_mm_time;
-        stream_data.data_size = outbuf->size;
-        stream_data.width = copy->src_area.right - copy->src_area.left;
-        stream_data.height = copy->src_area.bottom - copy->src_area.top;
-        stream_data.dest = drawable->red_drawable->bbox;
+    VideoEncoderDmabufData *dmabuf_data = new VideoEncoderDmabufData;
+    if (!dmabuf_data) {
+        spice_warning("Cannot create memory for dmabuf data");
+        return;
+    }
 
-        spice_debug("stream %d: sized frame: dest ==> ", stream_data.base.id);
-        rect_debug(&stream_data.dest);
-        spice_marshall_msg_display_stream_data_sized(base_marshaller, &stream_data);
+    QXLInstance* qxl = display->priv->qxl;
+    SpiceMsgDisplayGlScanoutUnix *scanout = red_qxl_get_gl_scanout(qxl);
+    if (!scanout) {
+        spice_warning("Cannot access scanout");
+        delete dmabuf_data;
+        return;
     }
-    spice_marshaller_add_by_ref_full(base_marshaller, outbuf->data, outbuf->size,
-                                     &red_release_video_encoder_buffer, outbuf);
+
+    dmabuf_data->drm_dma_buf_fd = scanout->drm_dma_buf_fd;
+    dmabuf_data->width = stream->width;
+    dmabuf_data->height = stream->height;
+    dmabuf_data->stride = stream->stride;
+    dmabuf_data->dcc = dcc;
+    dmabuf_data->free = red_free_cb;
+    red_qxl_put_gl_scanout(qxl, scanout);
+
+    VideoBuffer *outbuf;
+    VideoEncodeResults ret;
+
+    ret = agent->video_encoder->encode_dmabuf(agent->video_encoder,
+                                              reds_get_mm_time(),
+                                              dmabuf_data, &outbuf);
+
+    if (ret != VIDEO_ENCODER_FRAME_ENCODE_DONE) {
+        if (ret == VIDEO_ENCODER_FRAME_DROP) {
 #ifdef STREAM_STATS
-    agent->stats.num_frames_sent++;
-    agent->stats.size_sent += outbuf->size;
-    agent->stats.end = frame_mm_time;
+            agent->stats.num_drops_fps++;
 #endif
+        } else {
+            spice_warning("bad ret value (%d) from VideoEncoder::encode_dmabuf",
+                          ret);
+        }
 
-    return TRUE;
+        dcc->priv->gl_draw_ongoing = false;
+        display_channel_gl_draw_done(display);
+        delete dmabuf_data;
+        return;
+    }
+
+    red_init_display_stream_data(dcc, base_marshaller, nullptr,
+                                 stream_id, outbuf, false);
 }
 
 static inline void marshall_inval_palette(RedChannelClient *rcc,
@@ -2126,6 +2220,8 @@ static void marshall_stream_start(DisplayChannelClient *dcc,
     if (stream->current) {
         RedDrawable *red_drawable = stream->current->red_drawable.get();
         stream_create.clip = red_drawable->clip;
+    } else if (stream == DCC_TO_DC(dcc)->priv->gl_draw_stream){
+        stream_create.clip.type = SPICE_CLIP_TYPE_NONE;
     } else {
         stream_create.clip.type = SPICE_CLIP_TYPE_RECTS;
         clip_rects.num_rects = 0;
@@ -2275,14 +2371,20 @@ static void marshall_gl_scanout(DisplayChannelClient *dcc,
     red_qxl_put_gl_scanout(qxl, scanout);
 }
 
-static void marshall_gl_draw(RedChannelClient *rcc,
+static void marshall_gl_draw(DisplayChannelClient *dcc,
                              SpiceMarshaller *m,
                              RedPipeItem *item)
 {
     auto p = static_cast<RedGlDrawItem*>(item);
 
-    rcc->init_send_data(SPICE_MSG_DISPLAY_GL_DRAW);
-    spice_marshall_msg_display_gl_draw(m, &p->draw);
+    if (dcc->is_gl_client()) {
+        dcc->init_send_data(SPICE_MSG_DISPLAY_GL_DRAW);
+        spice_marshall_msg_display_gl_draw(m, &p->draw);
+    } else if (DCC_TO_DC(dcc)->priv->gl_draw_stream) {
+        red_marshall_gl_draw_stream(dcc, m);
+    } else {
+        spice_warning("nothing to send to the client");
+    }
 }
 
 
server/video-encoder.h +13 -0
index c2bdc811..0261bfca 100644
@@ -56,6 +56,15 @@ typedef struct VideoEncoderStats {
     double avg_quality;
 } VideoEncoderStats;
 
+typedef struct VideoEncoderDmabufData {
+    int drm_dma_buf_fd;
+    uint32_t width;
+    uint32_t height;
+    uint32_t stride;
+    void *dcc;
+    void (*free)(struct VideoEncoderDmabufData*);
+} VideoEncoderDmabufData;
+
 typedef struct VideoEncoder VideoEncoder;
 struct VideoEncoder {
     /* Releases the video encoder's resources */
@@ -84,6 +93,10 @@ struct VideoEncoder {
                                        const SpiceRect *src, int top_down,
                                        gpointer bitmap_opaque, VideoBuffer** outbuf);
 
+    VideoEncodeResults (*encode_dmabuf)(VideoEncoder *encoder, uint32_t frame_mm_time,
+                                        VideoEncoderDmabufData *dmabuf_data,
+                                        VideoBuffer** outbuf);
+
     /*
      * Bit rate control methods.
      */
Signature · 1 line
2.48.1
#5 lore raw

PATCH 5/10 gstreamer-encoder: Add an encoder function that takes dmabuf fd as input (v3)

In reply to Michael Scherle

From: Vivek Kasireddy <[email protected]>

This patch adds a new function to enable the creation of Gst memory with the dmabuf fd as the source by using a dmabuf allocator. And, it also adds a mechanism to register and invoke any callbacks once the Gst memory object is no longer used by the pipeline.

This patch also ensures that the source_fps value is always non-zero.

v2: (suggestions from Frediano)
- Moved the code associated with add_frame() and pipeline configuration into separate functions that are used when encoding dmabuf fd

v3:
- Add the new gstreamer-allocators dependency in autoconf as well (Frediano)
- Ensure that VIDEO_ENCODER_FRAME_UNSUPPORTED is returned when an error is encountered in spice_gst_encoder_encode_dmabuf()

Cc Frediano Ziglio <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Marc-André Lureau <[email protected]>
Cc Dongwon Kim <[email protected]>
---
 configure.ac               |   2 +-
 meson.build                |   2 +-
 server/gstreamer-encoder.c | 165 ++++++++++++++++++++++++++++++-------
 3 files changed, 137 insertions(+), 32 deletions(-)
configure.ac +1 -1
index ff13ee3a..5ad947ca 100644
@@ -99,7 +99,7 @@ AC_ARG_ENABLE(gstreamer,
               [enable_gstreamer="auto"])
 
 if test "x$enable_gstreamer" != "xno" && test "x$enable_gstreamer" != "x0.10"; then
-    SPICE_CHECK_GSTREAMER(GSTREAMER_1_0, 1.0, [gstreamer-1.0 gstreamer-base-1.0 gstreamer-app-1.0 gstreamer-video-1.0],
+    SPICE_CHECK_GSTREAMER(GSTREAMER_1_0, 1.0, [gstreamer-1.0 gstreamer-base-1.0 gstreamer-app-1.0 gstreamer-video-1.0 gstreamer-allocators-1.0],
         [enable_gstreamer="1.0"
          SPICE_CHECK_GSTREAMER_ELEMENTS($GST_INSPECT_1_0, [gst-plugins-base 1.0], [appsrc videoconvert appsink])
          SPICE_CHECK_GSTREAMER_ELEMENTS($GST_INSPECT_1_0, [gstreamer-libav 1.0], [avenc_mjpeg])
meson.build +1 -1
index b1237e61..d6aea60a 100644
@@ -131,7 +131,7 @@ endforeach
 spice_server_has_gstreamer = false
 spice_server_gst_version = get_option('gstreamer')
 if spice_server_gst_version != 'no'
-  gst_deps = ['gstreamer', 'gstreamer-base', 'gstreamer-app', 'gstreamer-video']
+  gst_deps = ['gstreamer', 'gstreamer-base', 'gstreamer-app', 'gstreamer-video', 'gstreamer-allocators']
   foreach dep : gst_deps
     dep = '@0@-@1@'.format(dep, spice_server_gst_version)
     spice_server_deps += dependency(dep)
server/gstreamer-encoder.c +135 -30

233 lines not on this page · raw message

Signature · 1 line
2.48.1
#6 lore raw

PATCH 6/10 video-stream: Don't stop a stream associated with gl_draw (v2)

In reply to Michael Scherle

From: Vivek Kasireddy <[email protected]>

We do not want to stop a stream associated with gl_draw as a result of timeout because we may not get another opportunity to create a new stream if the current one gets stopped. However, when the stream does get stopped for other reasons, we need to clear the gl_draw_stream pointer associated with the relevant DC.

v2: (suggestions from Frediano)
- Don't stop the stream regardless of whether gl_draw is ongoing or not

Cc Frediano Ziglio <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Marc-André Lureau <[email protected]>
Cc Dongwon Kim <[email protected]>
---
 server/video-stream.cpp | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
server/video-stream.cpp +6 -1
index 691c50a3..8418eed3 100644
@@ -116,6 +116,10 @@ void video_stream_stop(DisplayChannel *display, VideoStream *stream)
         }
         dcc->pipe_add(video_stream_destroy_item_new(stream_agent));
         video_stream_agent_stats_print(stream_agent);
+
+        if (stream == display->priv->gl_draw_stream) {
+            display->priv->gl_draw_stream = nullptr;
+        }
     }
     display->priv->streams_size_total -= stream->width * stream->height;
     ring_remove(&stream->link);
@@ -1034,7 +1038,8 @@ void video_stream_timeout(DisplayChannel *display)
     while (item) {
         VideoStream *stream = SPICE_CONTAINEROF(item, VideoStream, link);
         item = ring_next(ring, item);
-        if (now >= (stream->last_time + RED_STREAM_TIMEOUT)) {
+        if (now >= (stream->last_time + RED_STREAM_TIMEOUT) &&
+            stream != display->priv->gl_draw_stream) {
             detach_video_stream_gracefully(display, stream, nullptr);
             video_stream_stop(display, stream);
         }
Signature · 1 line
2.48.1
#7 lore raw

PATCH 7/10 gstreamer-encoder: Map the drm format to appropriate Gstreamer format

In reply to Michael Scherle

From: Vivek Kasireddy <[email protected]>

We need to convert the scanout's drm format to the correct Gstreamer format while configuring the pipeline. This can be done using gst_video_dma_drm_fourcc_to_format() API, which will take the drm fourcc value and return the appropriate Gst format.

---
 server/dcc-send.cpp        |   1 +
 server/gstreamer-encoder.c | 103 +++++++++++++++++++++++++++++++------
 server/video-encoder.h     |   1 +
 3 files changed, 88 insertions(+), 17 deletions(-)
server/dcc-send.cpp +1 -0
index c418f375..7bc20a22 100644
@@ -1790,6 +1790,7 @@ static void red_marshall_gl_draw_stream(DisplayChannelClient *dcc,
     }
 
     dmabuf_data->drm_dma_buf_fd = scanout->drm_dma_buf_fd;
+    dmabuf_data->drm_fourcc_format = scanout->drm_fourcc_format;
     dmabuf_data->width = stream->width;
     dmabuf_data->height = stream->height;
     dmabuf_data->stride = stream->stride;
server/gstreamer-encoder.c +86 -17

163 lines not on this page · raw message

server/video-encoder.h +1 -0
index 0261bfca..a4cebe0a 100644
@@ -61,6 +61,7 @@ typedef struct VideoEncoderDmabufData {
     uint32_t width;
     uint32_t height;
     uint32_t stride;
+    uint32_t drm_fourcc_format;
     void *dcc;
     void (*free)(struct VideoEncoderDmabufData*);
 } VideoEncoderDmabufData;
Signature · 1 line
2.48.1
#8 lore raw

cover dcc: Create a stream for non-gl/remote clients that want to use dmabuf (v9)

In reply to Michael Scherle

Oh I made a mistake and send this to the wrong mailing list, I'm so sorry, I wanted to send it to the spice devel.

Quoted 118 lines · Michael Scherle

This merge request is based on Vivek Kasireddy's
[PATCH v8 0/6] dcc: Create a stream for non-gl/remote clients that want to use dmabuf (v8), who gave me the permission to pursue the merge request. I also submitted this directly on gitlab but but I'm now also submitting it via the mailing list .I have made my changes in separate commits, should I merge them with the existing commits?

Original Description:
For clients that cannot accept a dmabuf fd directly (such as those running on a remote system), this patch series provides a way for the Spice server to stream the gl/dmabuf data/buffer instead. This is mostly done by enabling the creation of Gst memory using a dmabuf fd as the source. This ability is useful given that dmabuf is the standard mechanism for sharing buffers between various drivers and userspace in many Graphics and Media usecases. Currently, this is tested with Qemu and remote-viewer using the x264enc/avdec_h264 and msdkh264enc/dec plugins to stream the Guest/VM desktop but it can be easily extended to other plugins and applications. Here is roughly how things work:

The application (e.g, Qemu) chooses its preferred codec (a Gstreamer one) and calls gl_scanout (to update the fd) followed by gl_draw. In response, the Spice server checks to see if the client is capable of accepting a dmabuf fd directly or not. If yes, the fd is forwarded directly to the client; otherwise, a new stream is created. The Spice server then sends the dmabuf fd to the Gstreamer encoder which uses it as an input for creating an encoded buffer which is then sent to the client.
Once the encoding process is done, an async completion cookie is sent to the application.

Here is a link to the previous version that used a drawable to share the dmabuf fd with the Gstreamer encoder:
https://lists.freedesktop.org/archives/spice-devel/2023-January/052948.html
This version is tested together with following (required) patches in qemu:
https://gitlab.freedesktop.org/Vivek/qemu/-/commits/spice_gl_on_v4

Changelog:
v9:

updated the spice-common submodule, which is needed for udev helper to identify GPU vendor include dmabuf encoding only if dmabuf supported and gstreamer >= 1.24
fix a race condition when the video stream is stopped while encoding a frame

v8:

Added a new gstreamer-encoder patch to this series to convert drm format shared by the VMM to the appropriate Gstreamer format.

v7:

Revert back to the previous design where we do not share fd with the stream and scanout is the sole owner of the fd. This is because sharing fd ownership opens up a lot of corner cases.

v6: (Frediano)

Properly share ownership of the dmabuf fd between stream and scanout Ensure that a newly created stream is associated with all connected clients

v5:

Addressed review comments from Frediano mainly regarding adding autoconf support for gstreamer-allocators dependency and not needing to access scanout as part of gl draw operation

v4:

Test with Virgl enabled
Associate dmabuf's y0_top with stream's top_down variable

v3:

Updated the second patch to have a new primary surface created whenever a new stream gets created. This will avoid having to trigger primary surface creation from Qemu. And, this change also fixes the following error seen with v2:
../server/display-channel.cpp:2074:display_channel_create_surface:
condition `!display->priv->surfaces[surface_id]' failed Rebase all patches on master

v2:

Update all patches to address review comments from Frediano Tested this series with msdkh264enc/dec plugins that will be added in another patch series

Michael Scherle (3):
Update spice-common submodule
gstreamer-encoder: Include dmabuf encoding conditionally for Linux
dcc-send: Fix freeze when video stream is stopped during ongoing draw

Vivek Kasireddy (7):
gstreamer-encoder: Use a h/w based encoder with Intel GPUs if possible

    (v5)
  dcc: Check to see if the client supports multiple codecs (v2)
  dcc: Create a stream associated with gl_draw for non-gl clients (v6)
  dcc-send: Encode and send gl_draw stream data to the remote client
    (v4)
  gstreamer-encoder: Add an encoder function that takes dmabuf fd as
    input (v3)

video-stream: Don't stop a stream associated with gl_draw (v2)
gstreamer-encoder: Map the drm format to appropriate Gstreamer format

 configure.ac                     |   2 +-
 meson.build                      |  12 +-
 server/dcc-send.cpp              | 163 ++++++++++---
 server/dcc.cpp                   |  31 ++-
 server/dcc.h                     |   6 +
 server/display-channel-private.h |   1 +
 server/display-channel.cpp       |   1 +
 server/gstreamer-encoder.c       | 389 ++++++++++++++++++++++++++++---
 server/video-encoder.h           |  14 ++
 server/video-stream.cpp          | 201 +++++++++++++---
 server/video-stream.h            |   4 +
 subprojects/spice-common         |   2 +-
 12 files changed, 717 insertions(+), 109 deletions(-)
#9 lore raw

PATCH 8/10 Update spice-common submodule

In reply to Michael Scherle

This brings in the following changes:
common: Add a udev helper to identify GPU Vendor
build: Avoid Meson warning
Drop Python 2 from m4/spice-deps.m4 Stop using Python six package
codegen: Use context manager when opening files

---
 subprojects/spice-common | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
subprojects/spice-common +1 -1
index 58d375e5..8c0319e3 160000
@@ -1 +1 @@
-Subproject commit 58d375e5eadc6fb9e587e99fd81adcb95d01e8d6
+Subproject commit 8c0319e31df967e41c74f4121cbdb3b785fe114e
Signature · 1 line
2.48.1
#10 lore raw

PATCH 9/10 gstreamer-encoder: Include dmabuf encoding conditionally for Linux

In reply to Michael Scherle

add dmabuf encoding if drm/drm_fourcc.h is present and gstreamer is at least 1.24 due to gst_video_dma_drm_fourcc_to_format().

---
 meson.build                | 10 +++++++++-
 server/gstreamer-encoder.c | 34 +++++++++++++++++++++++++++-------
 2 files changed, 36 insertions(+), 8 deletions(-)
meson.build +9 -1
index d6aea60a..d5591d2a 100644
@@ -131,15 +131,23 @@ endforeach
 spice_server_has_gstreamer = false
 spice_server_gst_version = get_option('gstreamer')
 if spice_server_gst_version != 'no'
+  gstreamer_version = ''
   gst_deps = ['gstreamer', 'gstreamer-base', 'gstreamer-app', 'gstreamer-video', 'gstreamer-allocators']
   foreach dep : gst_deps
     dep = '@0@-@1@'.format(dep, spice_server_gst_version)
-    spice_server_deps += dependency(dep)
+    dep = dependency(dep)
+    if gstreamer_version == '' or  dep.version().version_compare('< ' + gstreamer_version)
+      gstreamer_version = dep.version()
+    endif
+    spice_server_deps += dep
   endforeach
   spice_server_deps += dependency('orc-0.4')
 
   gst_def = 'HAVE_GSTREAMER_1_0'
   spice_server_config_data.set(gst_def, '1')
+  if compiler.has_header('drm/drm_fourcc.h') and gstreamer_version.version_compare('>= 1.24')
+     spice_server_config_data.set('HAVE_GSTREAMER_DMABUF_ENCODING', '1')
+  endif
   spice_server_has_gstreamer = true
 endif
 
server/gstreamer-encoder.c +27 -7
index 196b27ab..a83cf846 100644
@@ -27,11 +27,13 @@
 #  pragma GCC diagnostic ignored "-Wunused-const-variable"
 #endif
 #include <gst/gst.h>
-#include <gst/allocators/gstdmabuf.h>
 #include <gst/app/gstappsrc.h>
 #include <gst/app/gstappsink.h>
 #include <gst/video/video.h>
+#ifdef HAVE_GSTREAMER_DMABUF_ENCODING
 #include <gst/video/video-info-dma.h>
+#include <gst/allocators/gstdmabuf.h>
+#endif
 #include <orc/orcprogram.h>
 #if defined(__GNUC__) && (__GNUC__ >= 6)
 #  pragma GCC diagnostic pop
@@ -41,7 +43,12 @@
 #include "video-encoder.h"
 #include "utils.h"
 #include "common/udev.h"
+#ifdef HAVE_GSTREAMER_DMABUF_ENCODING
 #include "drm/drm_fourcc.h"
+#endif
+#ifndef DRM_FORMAT_INVALID
+#define DRM_FORMAT_INVALID 0
+#endif
 
 #define SPICE_GST_DEFAULT_FPS 30
 
@@ -793,6 +800,7 @@ static const SpiceFormatForGStreamer *map_format(SpiceBitmapFmt format)
     return GSTREAMER_FORMAT_INVALID;
 }
 
+#ifdef HAVE_GSTREAMER_DMABUF_ENCODING
 static SpiceFormatForGStreamer drm_format_map[] =  {
     DRM_FMT_DESC(DRM_FORMAT_INVALID,  0),
     DRM_FMT_DESC(DRM_FORMAT_XRGB8888, 32),
@@ -822,7 +830,7 @@ static const SpiceFormatForGStreamer *map_drm_format(uint32_t fourcc)
                 }
 
                 drm_format_map[i].gst_format = gst_format;
-                strncpy(drm_format_map[i].format,
+                g_strlcpy(drm_format_map[i].format,
                         gst_video_format_to_string(gst_format),
                         format_size - 1);
                 drm_format_map[i].format[format_size - 1] = '\0';
@@ -833,6 +841,7 @@ static const SpiceFormatForGStreamer *map_drm_format(uint32_t fourcc)
 
     return GSTREAMER_DRM_FORMAT_INVALID;
 }
+#endif
 
 static void set_appsrc_caps(SpiceGstEncoder *encoder)
 {
@@ -1579,7 +1588,9 @@ static void spice_gst_encoder_destroy(VideoEncoder *video_encoder)
 {
     SpiceGstEncoder *encoder = (SpiceGstEncoder*)video_encoder;
 
+#ifdef HAVE_GSTREAMER_DMABUF_ENCODING
     gst_object_unref(encoder->allocator);
+#endif
     free_pipeline(encoder);
     pthread_mutex_destroy(&encoder->outbuf_mutex);
     pthread_cond_destroy(&encoder->outbuf_cond);
@@ -1619,12 +1630,17 @@ spice_gst_encoder_configure_pipeline(SpiceGstEncoder *encoder,
 
     if (spice_format != SPICE_BITMAP_FMT_INVALID) {
         format = map_format(spice_format);
-    } else if (drm_format != DRM_FORMAT_INVALID) {
+    }
+#ifdef HAVE_GSTREAMER_DMABUF_ENCODING
+    else if (drm_format != DRM_FORMAT_INVALID) {
         format = map_drm_format(drm_format);
     }
-
-    if (format == GSTREAMER_FORMAT_INVALID ||
-        format == GSTREAMER_DRM_FORMAT_INVALID) {
+#endif
+    if (format == GSTREAMER_FORMAT_INVALID
+#ifdef HAVE_GSTREAMER_DMABUF_ENCODING
+        || format == GSTREAMER_DRM_FORMAT_INVALID
+#endif
+        ) {
         spice_warning("unable to map format type %d or %u",
                       spice_format, drm_format);
         encoder->errors = 4;
@@ -1738,6 +1754,7 @@ spice_gst_encoder_encode_frame(VideoEncoder *video_encoder,
     return rc;
 }
 
+#ifdef HAVE_GSTREAMER_DMABUF_ENCODING
 static void
 spice_gst_mem_free_cb(VideoEncoderDmabufData *dmabuf_data, GstMiniObject *obj)
 {
@@ -1806,6 +1823,7 @@ spice_gst_encoder_encode_dmabuf(VideoEncoder *video_encoder,
     spice_gst_encoder_add_frame(encoder, outbuf, start, frame_mm_time);
     return rc;
 }
+#endif
 
 static void spice_gst_encoder_client_stream_report(VideoEncoder *video_encoder,
                                              uint32_t num_frames,
@@ -1997,7 +2015,10 @@ VideoEncoder *gstreamer_encoder_new(SpiceVideoCodecType codec_type,
     SpiceGstEncoder *encoder = g_new0(SpiceGstEncoder, 1);
     encoder->base.destroy = spice_gst_encoder_destroy;
     encoder->base.encode_frame = spice_gst_encoder_encode_frame;
+#ifdef HAVE_GSTREAMER_DMABUF_ENCODING
     encoder->base.encode_dmabuf = spice_gst_encoder_encode_dmabuf;
+    encoder->allocator = gst_dmabuf_allocator_new();
+#endif
     encoder->base.client_stream_report = spice_gst_encoder_client_stream_report;
     encoder->base.notify_server_frame_drop = spice_gst_encoder_notify_server_frame_drop;
     encoder->base.get_bit_rate = spice_gst_encoder_get_bit_rate;
@@ -2010,7 +2031,6 @@ VideoEncoder *gstreamer_encoder_new(SpiceVideoCodecType codec_type,
     encoder->bitmap_ref = bitmap_ref;
     encoder->bitmap_unref = bitmap_unref;
     encoder->format = GSTREAMER_FORMAT_INVALID;
-    encoder->allocator = gst_dmabuf_allocator_new();
     pthread_mutex_init(&encoder->outbuf_mutex, NULL);
     pthread_cond_init(&encoder->outbuf_cond, NULL);
 
Signature · 1 line
2.48.1
#11 lore raw

PATCH 10/10 dcc-send: Fix freeze when video stream is stopped during ongoing draw

In reply to Michael Scherle

Prevent a freeze that occurs if the video stream is stopped while a gl draw is in progress (e.g., when the client requests a new codec). Ensure proper cleanup of the ongoing gl draw.

---
 server/dcc-send.cpp | 4 ++++
 1 file changed, 4 insertions(+)
server/dcc-send.cpp +4 -0
index 7bc20a22..71873563 100644
@@ -2383,6 +2383,10 @@ static void marshall_gl_draw(DisplayChannelClient *dcc,
         spice_marshall_msg_display_gl_draw(m, &p->draw);
     } else if (DCC_TO_DC(dcc)->priv->gl_draw_stream) {
         red_marshall_gl_draw_stream(dcc, m);
+    } else if (dcc->priv->gl_draw_ongoing) {
+        DisplayChannel *display = DCC_TO_DC(dcc);
+        dcc->priv->gl_draw_ongoing = false;
+        display_channel_gl_draw_done(display);
     } else {
         spice_warning("nothing to send to the client");
     }
Signature · 1 line
2.48.1