giostreamsink: Add a property to close stream on stop()

Back in the mists of time[1], we switched `giostream*` elements to not close the
stream on stop() so that applications that needed a handle to the stream after
the element stopped had it.

Unfortunately, we also have cases[2] where waiting for the element to be
finalized is too late for the stream to be closed.

In order to not change the behaviour of the element, we add a property to allow
users to select the desired behaviour.

[1]: https://bugzilla.gnome.org/show_bug.cgi?id=587896
[2]: gst-plugins-rs#423

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/5372>
This commit is contained in:
Arun Raghavan 2023-09-27 13:45:26 +02:00 committed by GStreamer Marge Bot
parent 974e908c61
commit ca337002f1
4 changed files with 85 additions and 5 deletions

View file

@ -2811,7 +2811,21 @@
"GInitiallyUnowned", "GInitiallyUnowned",
"GObject" "GObject"
], ],
"kind": "object" "kind": "object",
"properties": {
"close-on-stop": {
"blurb": "Close the stream when the element stops (i.e. goes from READY to NULL) rather than when the element is disposed)",
"conditionally-available": false,
"construct": false,
"construct-only": false,
"controllable": false,
"default": "true",
"mutable": "null",
"readable": true,
"type": "gboolean",
"writable": true
}
}
}, },
"GstGioBaseSrc": { "GstGioBaseSrc": {
"hierarchy": [ "hierarchy": [

View file

@ -29,6 +29,14 @@
GST_DEBUG_CATEGORY_STATIC (gst_gio_base_sink_debug); GST_DEBUG_CATEGORY_STATIC (gst_gio_base_sink_debug);
#define GST_CAT_DEFAULT gst_gio_base_sink_debug #define GST_CAT_DEFAULT gst_gio_base_sink_debug
#define DEFAULT_CLOSE_ON_STOP FALSE
enum
{
PROP_0,
PROP_CLOSE_ON_STOP,
};
static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink", static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK, GST_PAD_SINK,
GST_PAD_ALWAYS, GST_PAD_ALWAYS,
@ -38,6 +46,10 @@ static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
G_DEFINE_TYPE (GstGioBaseSink, gst_gio_base_sink, GST_TYPE_BASE_SINK); G_DEFINE_TYPE (GstGioBaseSink, gst_gio_base_sink, GST_TYPE_BASE_SINK);
static void gst_gio_base_sink_finalize (GObject * object); static void gst_gio_base_sink_finalize (GObject * object);
static void gst_gio_base_sink_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec);
static void gst_gio_base_sink_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec);
static gboolean gst_gio_base_sink_start (GstBaseSink * base_sink); static gboolean gst_gio_base_sink_start (GstBaseSink * base_sink);
static gboolean gst_gio_base_sink_stop (GstBaseSink * base_sink); static gboolean gst_gio_base_sink_stop (GstBaseSink * base_sink);
static gboolean gst_gio_base_sink_unlock (GstBaseSink * base_sink); static gboolean gst_gio_base_sink_unlock (GstBaseSink * base_sink);
@ -59,9 +71,27 @@ gst_gio_base_sink_class_init (GstGioBaseSinkClass * klass)
"GIO base sink"); "GIO base sink");
gobject_class->finalize = gst_gio_base_sink_finalize; gobject_class->finalize = gst_gio_base_sink_finalize;
gobject_class->get_property = gst_gio_base_sink_get_property;
gobject_class->set_property = gst_gio_base_sink_set_property;
gst_element_class_add_static_pad_template (gstelement_class, &sink_factory); gst_element_class_add_static_pad_template (gstelement_class, &sink_factory);
/**
* GstGioBaseSink:close-on-stop:
*
* Determines whether the stream is closed with the element "stops" (i.e. goes
* from the READY to NULL state), or when the element is disposed. Which of
* these behaviours is desirable depends on the lifecycle of the underlying
* stream that the element works with.
*
* Since: 1.24
*/
g_object_class_install_property (gobject_class, PROP_CLOSE_ON_STOP,
g_param_spec_boolean ("close-on-stop", "Close stream on stop",
"Close the stream when the element stops (i.e. goes from READY to "
"NULL) rather than when the element is disposed)",
DEFAULT_CLOSE_ON_STOP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_gio_base_sink_start); gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_gio_base_sink_start);
gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_gio_base_sink_stop); gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_gio_base_sink_stop);
gstbasesink_class->unlock = GST_DEBUG_FUNCPTR (gst_gio_base_sink_unlock); gstbasesink_class->unlock = GST_DEBUG_FUNCPTR (gst_gio_base_sink_unlock);
@ -80,6 +110,8 @@ gst_gio_base_sink_init (GstGioBaseSink * sink)
gst_base_sink_set_sync (GST_BASE_SINK (sink), FALSE); gst_base_sink_set_sync (GST_BASE_SINK (sink), FALSE);
sink->cancel = g_cancellable_new (); sink->cancel = g_cancellable_new ();
// FALSE is the historical default for this class
sink->close_on_stop = DEFAULT_CLOSE_ON_STOP;
} }
static void static void
@ -100,6 +132,38 @@ gst_gio_base_sink_finalize (GObject * object)
GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object)); GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
} }
static void
gst_gio_base_sink_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec)
{
GstGioBaseSink *sink = GST_GIO_BASE_SINK (object);
switch (prop_id) {
case PROP_CLOSE_ON_STOP:
g_value_set_boolean (value, sink->close_on_stop);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_gio_base_sink_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
GstGioBaseSink *sink = GST_GIO_BASE_SINK (object);
switch (prop_id) {
case PROP_CLOSE_ON_STOP:
sink->close_on_stop = g_value_get_boolean (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static gboolean static gboolean
gst_gio_base_sink_start (GstBaseSink * base_sink) gst_gio_base_sink_start (GstBaseSink * base_sink)
{ {
@ -129,11 +193,10 @@ static gboolean
gst_gio_base_sink_stop (GstBaseSink * base_sink) gst_gio_base_sink_stop (GstBaseSink * base_sink)
{ {
GstGioBaseSink *sink = GST_GIO_BASE_SINK (base_sink); GstGioBaseSink *sink = GST_GIO_BASE_SINK (base_sink);
GstGioBaseSinkClass *klass = GST_GIO_BASE_SINK_GET_CLASS (sink);
gboolean success; gboolean success;
GError *err = NULL; GError *err = NULL;
if (klass->close_on_stop && G_IS_OUTPUT_STREAM (sink->stream)) { if (sink->close_on_stop && G_IS_OUTPUT_STREAM (sink->stream)) {
GST_DEBUG_OBJECT (sink, "closing stream"); GST_DEBUG_OBJECT (sink, "closing stream");
/* FIXME: can block but unfortunately we can't use async operations /* FIXME: can block but unfortunately we can't use async operations

View file

@ -55,6 +55,7 @@ struct _GstGioBaseSink
/* < private > */ /* < private > */
GOutputStream *stream; GOutputStream *stream;
gboolean close_on_stop;
}; };
struct _GstGioBaseSinkClass struct _GstGioBaseSinkClass
@ -62,7 +63,6 @@ struct _GstGioBaseSinkClass
GstBaseSinkClass parent_class; GstBaseSinkClass parent_class;
GOutputStream * (*get_stream) (GstGioBaseSink *bsink); GOutputStream * (*get_stream) (GstGioBaseSink *bsink);
gboolean close_on_stop;
}; };
GType gst_gio_base_sink_get_type (void); GType gst_gio_base_sink_get_type (void);

View file

@ -144,12 +144,15 @@ gst_gio_sink_class_init (GstGioSinkClass * klass)
gstgiobasesink_class->get_stream = gstgiobasesink_class->get_stream =
GST_DEBUG_FUNCPTR (gst_gio_sink_get_stream); GST_DEBUG_FUNCPTR (gst_gio_sink_get_stream);
gstgiobasesink_class->close_on_stop = TRUE;
} }
static void static void
gst_gio_sink_init (GstGioSink * sink) gst_gio_sink_init (GstGioSink * sink)
{ {
GstGioBaseSink *bsink = GST_GIO_BASE_SINK (sink);
// TRUE is the historical default for this element
bsink->close_on_stop = TRUE;
} }
static void static void