From 9664d1a6b1796491048eb579e919ab592c1ec2f6 Mon Sep 17 00:00:00 2001 From: Vivia Nikolaidou Date: Fri, 29 May 2015 14:27:24 +0300 Subject: [PATCH] error-ignore: New element to convert some GstFlowReturn types into others Can be used to fix misbehaving sinks. It will pass through all buffers until it encounters GST_FLOW_ERROR or GST_FLOW_NOT_NEGOTIATED (configurable). At that point it will unref the buffers and return GST_FLOW_NOT_LINKED (configurable) - until the next READY_TO_PAUSED or FLUSH_STOP. https://bugzilla.gnome.org/show_bug.cgi?id=750098 --- gst/debugutils/Makefile.am | 6 +- gst/debugutils/debugutilsbad.c | 3 + gst/debugutils/gsterrorignore.c | 271 ++++++++++++++++++++++++++++++++++++++++ gst/debugutils/gsterrorignore.h | 60 +++++++++ 4 files changed, 338 insertions(+), 2 deletions(-) create mode 100644 gst/debugutils/gsterrorignore.c create mode 100644 gst/debugutils/gsterrorignore.h diff --git a/gst/debugutils/Makefile.am b/gst/debugutils/Makefile.am index f5f2f29..aff78a2 100644 --- a/gst/debugutils/Makefile.am +++ b/gst/debugutils/Makefile.am @@ -21,7 +21,8 @@ libgstdebugutilsbad_la_SOURCES = \ gstchecksumsink.c \ gstchopmydata.c \ gstcompare.c \ - gstwatchdog.c + gstwatchdog.c \ + gsterrorignore.c nodist_libgstdebugutilsbad_la_SOURCES = $(BUILT_SOURCES) libgstdebugutilsbad_la_CFLAGS = $(GST_CFLAGS) $(GST_BASE_CFLAGS) $(GST_PLUGINS_BASE_CFLAGS) @@ -36,4 +37,5 @@ noinst_HEADERS = fpsdisplaysink.h \ gstchopmydata.h \ gstcompare.h \ gstdebugspy.h \ - gstwatchdog.h + gstwatchdog.h \ + gsterrorignore.h diff --git a/gst/debugutils/debugutilsbad.c b/gst/debugutils/debugutilsbad.c index 2a6e3cc..68edd78 100644 --- a/gst/debugutils/debugutilsbad.c +++ b/gst/debugutils/debugutilsbad.c @@ -28,6 +28,7 @@ GType fps_display_sink_get_type (void); GType gst_chop_my_data_get_type (void); GType gst_compare_get_type (void); GType gst_debug_spy_get_type (void); +GType gst_error_ignore_get_type (void); GType gst_watchdog_get_type (void); static gboolean @@ -45,6 +46,8 @@ plugin_init (GstPlugin * plugin) gst_debug_spy_get_type ()); gst_element_register (plugin, "watchdog", GST_RANK_NONE, gst_watchdog_get_type ()); + gst_element_register (plugin, "errorignore", GST_RANK_NONE, + gst_error_ignore_get_type ()); return TRUE; } diff --git a/gst/debugutils/gsterrorignore.c b/gst/debugutils/gsterrorignore.c new file mode 100644 index 0000000..3c19ed5 --- /dev/null +++ b/gst/debugutils/gsterrorignore.c @@ -0,0 +1,271 @@ +/* + * GStreamer + * Copyright (C) 2015 Vivia Nikolaidou + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +/** + * SECTION:element-errorignore + * + * Passes through all packets, until it encounters GST_FLOW_ERROR or + * GST_FLOW_NOT_NEGOTIATED (configurable). At that point it will unref the + * buffers and return GST_FLOW_OK (configurable) - until the next + * READY_TO_PAUSED, RECONFIGURE or FLUSH_STOP. + * + * Example launch line + * |[ + * gst-launch-1.0 videotestsrc ! errorignore ! autovideosink + * ]| + * + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "gsterrorignore.h" + +#define GST_CAT_DEFAULT gst_error_ignore_debug +GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT); + +enum +{ + PROP_0, + PROP_IGNORE_ERROR, + PROP_IGNORE_NOTLINKED, + PROP_IGNORE_NOTNEGOTIATED, + PROP_CONVERT_TO +}; + +static void gst_error_ignore_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec); +static void gst_error_ignore_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec); + +static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS_ANY); + +static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS_ANY); + +#define parent_class gst_error_ignore_parent_class +G_DEFINE_TYPE (GstErrorIgnore, gst_error_ignore, GST_TYPE_ELEMENT); + +static GstFlowReturn gst_error_ignore_sink_chain (GstPad * pad, + GstObject * parent, GstBuffer * inbuf); +static gboolean gst_error_ignore_sink_event (GstPad * pad, GstObject * parent, + GstEvent * event); +static GstStateChangeReturn gst_error_ignore_change_state (GstElement * element, + GstStateChange transition); + +static void +gst_error_ignore_class_init (GstErrorIgnoreClass * klass) +{ + GstElementClass *gstelement_class; + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + GST_DEBUG_CATEGORY_INIT (gst_error_ignore_debug, "errorignore", 0, + "Convert some GstFlowReturn types into others"); + + gstelement_class = (GstElementClass *) klass; + + gst_element_class_set_static_metadata (gstelement_class, + "Convert some GstFlowReturn types into others", "Generic", + "Pass through all packets but ignore some GstFlowReturn types", + "Vivia Nikolaidou "); + + gst_element_class_add_pad_template (gstelement_class, + gst_static_pad_template_get (&src_template)); + gst_element_class_add_pad_template (gstelement_class, + gst_static_pad_template_get (&sink_template)); + + gstelement_class->change_state = gst_error_ignore_change_state; + + /* define virtual function pointers */ + object_class->set_property = gst_error_ignore_set_property; + object_class->get_property = gst_error_ignore_get_property; + + /* define properties */ + g_object_class_install_property (object_class, PROP_IGNORE_ERROR, + g_param_spec_boolean ("ignore-error", "Ignore GST_FLOW_ERROR", + "Whether to ignore GST_FLOW_ERROR", + TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + + g_object_class_install_property (object_class, PROP_IGNORE_NOTLINKED, + g_param_spec_boolean ("ignore-notlinked", "Ignore GST_FLOW_NOT_LINKED", + "Whether to ignore GST_FLOW_NOT_LINKED", + FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + + g_object_class_install_property (object_class, PROP_IGNORE_NOTNEGOTIATED, + g_param_spec_boolean ("ignore-notnegotiated", + "Ignore GST_FLOW_NOT_NEGOTIATED", + "Whether to ignore GST_FLOW_NOT_NEGOTIATED", + TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + + g_object_class_install_property (object_class, PROP_CONVERT_TO, + g_param_spec_enum ("convert-to", "GstFlowReturn to convert to", + "Which GstFlowReturn value we should convert to when ignoring", + GST_TYPE_FLOW_RETURN, + GST_FLOW_NOT_LINKED, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); +} + +static void +gst_error_ignore_init (GstErrorIgnore * self) +{ + self->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink"); + gst_pad_set_chain_function (self->sinkpad, + GST_DEBUG_FUNCPTR (gst_error_ignore_sink_chain)); + gst_pad_set_event_function (self->sinkpad, + GST_DEBUG_FUNCPTR (gst_error_ignore_sink_event)); + GST_PAD_SET_PROXY_ALLOCATION (self->sinkpad); + GST_PAD_SET_PROXY_CAPS (self->sinkpad); + GST_PAD_SET_PROXY_SCHEDULING (self->sinkpad); + gst_element_add_pad (GST_ELEMENT (self), self->sinkpad); + + self->srcpad = gst_pad_new_from_static_template (&src_template, "src"); + gst_pad_use_fixed_caps (self->srcpad); + GST_PAD_SET_PROXY_ALLOCATION (self->srcpad); + GST_PAD_SET_PROXY_CAPS (self->srcpad); + GST_PAD_SET_PROXY_SCHEDULING (self->srcpad); + gst_element_add_pad (GST_ELEMENT (self), self->srcpad); + + self->keep_pushing = TRUE; + self->ignore_error = TRUE; + self->ignore_notlinked = FALSE; + self->ignore_notnegotiated = TRUE; + self->convert_to = GST_FLOW_NOT_LINKED; +} + +static void +gst_error_ignore_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec) +{ + GstErrorIgnore *self = GST_ERROR_IGNORE (object); + + switch (prop_id) { + case PROP_IGNORE_ERROR: + self->ignore_error = g_value_get_boolean (value); + break; + case PROP_IGNORE_NOTLINKED: + self->ignore_notlinked = g_value_get_boolean (value); + break; + case PROP_IGNORE_NOTNEGOTIATED: + self->ignore_notnegotiated = g_value_get_boolean (value); + break; + case PROP_CONVERT_TO: + self->convert_to = g_value_get_enum (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +gst_error_ignore_get_property (GObject * object, guint prop_id, GValue * value, + GParamSpec * pspec) +{ + GstErrorIgnore *self = GST_ERROR_IGNORE (object); + + switch (prop_id) { + case PROP_IGNORE_ERROR: + g_value_set_boolean (value, self->ignore_error); + break; + case PROP_IGNORE_NOTLINKED: + g_value_set_boolean (value, self->ignore_notlinked); + break; + case PROP_IGNORE_NOTNEGOTIATED: + g_value_set_boolean (value, self->ignore_notnegotiated); + break; + case PROP_CONVERT_TO: + g_value_set_enum (value, self->convert_to); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static gboolean +gst_error_ignore_sink_event (GstPad * pad, GstObject * parent, GstEvent * event) +{ + GstErrorIgnore *self = GST_ERROR_IGNORE (parent); + gboolean ret; + + GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event)); + + switch (GST_EVENT_TYPE (event)) { + case GST_EVENT_FLUSH_STOP: + self->keep_pushing = TRUE; + /* fall through */ + default: + ret = gst_pad_event_default (pad, parent, event); + break; + } + + return ret; +} + +static GstFlowReturn +gst_error_ignore_sink_chain (GstPad * pad, GstObject * parent, + GstBuffer * inbuf) +{ + GstErrorIgnore *self = GST_ERROR_IGNORE (parent); + GstFlowReturn ret = GST_FLOW_OK; + + if (gst_pad_check_reconfigure (pad)) + self->keep_pushing = TRUE; + + if (self->keep_pushing) { + ret = gst_pad_push (self->srcpad, inbuf); + self->keep_pushing = (ret == GST_FLOW_OK); + } else { + gst_buffer_unref (inbuf); + } + + if ((ret == GST_FLOW_ERROR && self->ignore_error) || + (ret == GST_FLOW_NOT_LINKED && self->ignore_notlinked) || + (ret == GST_FLOW_NOT_NEGOTIATED && self->ignore_notnegotiated)) + return self->convert_to; + else + return ret; +} + +static GstStateChangeReturn +gst_error_ignore_change_state (GstElement * element, GstStateChange transition) +{ + GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS; + GstErrorIgnore *self = GST_ERROR_IGNORE (element); + + ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); + if (ret == GST_STATE_CHANGE_FAILURE) + return ret; + + switch (transition) { + case GST_STATE_CHANGE_READY_TO_PAUSED: + self->keep_pushing = TRUE; + break; + default: + break; + } + + return ret; +} diff --git a/gst/debugutils/gsterrorignore.h b/gst/debugutils/gsterrorignore.h new file mode 100644 index 0000000..21d0864 --- /dev/null +++ b/gst/debugutils/gsterrorignore.h @@ -0,0 +1,60 @@ +/* + * GStreamer + * Copyright (C) 2015 Vivia Nikolaidou + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef __GST_ERROR_IGNORE_H__ +#define __GST_ERROR_IGNORE_H__ + +#include + +G_BEGIN_DECLS + +#define GST_TYPE_ERROR_IGNORE (gst_error_ignore_get_type()) +#define GST_ERROR_IGNORE(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_ERROR_IGNORE,GstErrorIgnore)) +#define GST_IS_ERROR_IGNORE(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_ERROR_IGNORE)) +#define GST_ERROR_IGNORE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass) ,GST_TYPE_ERROR_IGNORE,GstErrorIgnoreClass)) +#define GST_IS_ERROR_IGNORE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass) ,GST_TYPE_ERROR_IGNORE)) +#define GST_ERROR_IGNORE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj) ,GST_TYPE_ERROR_IGNORE,GstErrorIgnoreClass)) + +typedef struct _GstErrorIgnore GstErrorIgnore; +typedef struct _GstErrorIgnoreClass GstErrorIgnoreClass; + +struct _GstErrorIgnore { + GstElement parent; + + GstPad *srcpad, *sinkpad; + + gboolean keep_pushing; + gboolean ignore_error; + gboolean ignore_notlinked; + gboolean ignore_notnegotiated; + GstFlowReturn convert_to; +}; + +struct _GstErrorIgnoreClass { + GstElementClass parent_class; +}; + +GType gst_error_ignore_get_type (void); + +gboolean gst_error_ignore_plugin_init (GstPlugin * plugin); + +G_END_DECLS + +#endif /* __GST_ERROR_IGNORE_H__ */ -- 2.7.4