gstgiobasesink: Handle incomplete writes in gst_gio_base_sink_render()
authorRobin Burchell <robin.burchell@crimson.no>
Tue, 6 Apr 2021 11:22:15 +0000 (13:22 +0200)
committerGStreamer Marge Bot <gitlab-merge-bot@gstreamer-foundation.org>
Fri, 9 Apr 2021 07:35:07 +0000 (07:35 +0000)
As the comment asked, yes, incomplete writes can happen.
I have encountered this with an sshfs mount, for example.

It seems like g_output_stream_write_all() is designed to handle this case,
by not returning until the requested buffer has been completely written,
or an error occurs, which seems to match up with the desired behaviour.

Fixes https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/-/issues/885

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/-/merge_requests/1100>

gst/gio/gstgiobasesink.c

index 52fd1d7..c74f91a 100644 (file)
@@ -264,7 +264,7 @@ static GstFlowReturn
 gst_gio_base_sink_render (GstBaseSink * base_sink, GstBuffer * buffer)
 {
   GstGioBaseSink *sink = GST_GIO_BASE_SINK (base_sink);
-  gssize written;
+  gsize written;
   GstMapInfo map;
   gboolean success;
   GError *err = NULL;
@@ -277,23 +277,11 @@ gst_gio_base_sink_render (GstBaseSink * base_sink, GstBuffer * buffer)
       "writing %" G_GSIZE_FORMAT " bytes to offset %" G_GUINT64_FORMAT,
       map.size, sink->position);
 
-  written =
-      g_output_stream_write (sink->stream, map.data, map.size, sink->cancel,
-      &err);
+  success =
+      g_output_stream_write_all (sink->stream, map.data, map.size, &written,
+      sink->cancel, &err);
   gst_buffer_unmap (buffer, &map);
 
-  success = (written >= 0);
-
-  if (G_UNLIKELY (success && written < map.size)) {
-    /* FIXME: Can this happen?  Should we handle it gracefully?  gnomevfssink
-     * doesn't... */
-    GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
-        ("Could not write to stream: (short write, only %"
-            G_GSSIZE_FORMAT " bytes of %" G_GSIZE_FORMAT " bytes written)",
-            written, map.size));
-    return GST_FLOW_ERROR;
-  }
-
   if (success) {
     sink->position += written;
     return GST_FLOW_OK;
@@ -301,7 +289,7 @@ gst_gio_base_sink_render (GstBaseSink * base_sink, GstBuffer * buffer)
   } else {
     GstFlowReturn ret;
 
-    if (!gst_gio_error (sink, "g_output_stream_write", &err, &ret)) {
+    if (!gst_gio_error (sink, "g_output_stream_write_all", &err, &ret)) {
       if (GST_GIO_ERROR_MATCHES (err, NO_SPACE)) {
         GST_ELEMENT_ERROR (sink, RESOURCE, NO_SPACE_LEFT, (NULL),
             ("Could not write to stream: %s", err->message));