2010-12-30 00:41:09 +00:00
|
|
|
/* GStreamer valve element
|
2010-09-30 20:13:23 +00:00
|
|
|
* Copyright 2007-2009 Collabora Ltd
|
2007-10-24 22:41:47 +00:00
|
|
|
* @author: Olivier Crete <olivier.crete@collabora.co.uk>
|
2010-09-30 20:13:23 +00:00
|
|
|
* Copyright 2007-2009 Nokia Corporation
|
2007-10-24 22:41:47 +00:00
|
|
|
*
|
|
|
|
* 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
|
2012-11-03 20:44:48 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2007-10-24 22:41:47 +00:00
|
|
|
*
|
|
|
|
*/
|
2010-09-30 20:13:23 +00:00
|
|
|
|
2009-02-10 18:52:54 +00:00
|
|
|
/**
|
|
|
|
* SECTION:element-valve
|
2017-01-16 14:26:16 +00:00
|
|
|
* @title: valve
|
2009-02-10 18:52:54 +00:00
|
|
|
*
|
2011-01-11 13:10:32 +00:00
|
|
|
* The valve is a simple element that drops buffers when the #GstValve:drop
|
2009-02-10 18:52:54 +00:00
|
|
|
* property is set to %TRUE and lets then through otherwise.
|
|
|
|
*
|
2014-09-11 07:35:17 +00:00
|
|
|
* Any downstream error received while the #GstValve:drop property is %TRUE
|
2009-02-10 18:52:54 +00:00
|
|
|
* is ignored. So downstream element can be set to %GST_STATE_NULL and removed,
|
|
|
|
* without using pad blocking.
|
|
|
|
*/
|
2007-10-24 22:41:47 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "gstvalve.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2010-12-30 00:41:09 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (valve_debug);
|
2007-10-24 22:41:47 +00:00
|
|
|
#define GST_CAT_DEFAULT (valve_debug)
|
|
|
|
|
2020-08-14 08:40:30 +00:00
|
|
|
#define GST_TYPE_VALVE_DROP_MODE (gst_valve_drop_mode_get_type ())
|
|
|
|
static GType
|
|
|
|
gst_valve_drop_mode_get_type (void)
|
|
|
|
{
|
|
|
|
static GType drop_mode_type = 0;
|
|
|
|
static const GEnumValue drop_mode[] = {
|
|
|
|
{GST_VALVE_DROP_MODE_DROP_ALL, "Drop all buffers and events", "drop-all"},
|
|
|
|
{GST_VALVE_DROP_MODE_FORWARD_STICKY_EVENTS,
|
|
|
|
"Drop all buffers but forward sticky events", "forward-sticky-events"},
|
|
|
|
{GST_VALVE_DROP_MODE_TRANSFORM_TO_GAP,
|
|
|
|
"Convert all dropped buffers into gap events and forward sticky events",
|
|
|
|
"transform-to-gap"},
|
|
|
|
{0, NULL, NULL},
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!drop_mode_type) {
|
|
|
|
drop_mode_type = g_enum_register_static ("GstValveDropMode", drop_mode);
|
|
|
|
}
|
|
|
|
return drop_mode_type;
|
|
|
|
}
|
|
|
|
|
2007-10-24 22:41:47 +00:00
|
|
|
static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS_ANY);
|
|
|
|
|
|
|
|
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS_ANY);
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
2010-12-30 00:41:09 +00:00
|
|
|
PROP_0,
|
2020-08-14 08:40:30 +00:00
|
|
|
PROP_DROP,
|
|
|
|
PROP_DROP_MODE
|
2007-10-24 22:41:47 +00:00
|
|
|
};
|
|
|
|
|
2010-12-30 00:41:09 +00:00
|
|
|
#define DEFAULT_DROP FALSE
|
2020-08-14 08:40:30 +00:00
|
|
|
#define DEFAULT_DROP_MODE GST_VALVE_DROP_MODE_DROP_ALL
|
2007-10-24 22:41:47 +00:00
|
|
|
|
2009-02-10 17:55:47 +00:00
|
|
|
static void gst_valve_set_property (GObject * object,
|
2007-10-24 22:41:47 +00:00
|
|
|
guint prop_id, const GValue * value, GParamSpec * pspec);
|
2009-02-10 17:55:47 +00:00
|
|
|
static void gst_valve_get_property (GObject * object,
|
|
|
|
guint prop_id, GValue * value, GParamSpec * pspec);
|
2007-10-24 22:41:47 +00:00
|
|
|
|
2011-11-17 11:40:45 +00:00
|
|
|
static GstFlowReturn gst_valve_chain (GstPad * pad, GstObject * parent,
|
|
|
|
GstBuffer * buffer);
|
2016-03-16 14:13:39 +00:00
|
|
|
static gboolean gst_valve_sink_event (GstPad * pad, GstObject * parent,
|
2011-11-17 11:40:45 +00:00
|
|
|
GstEvent * event);
|
2011-11-16 16:22:56 +00:00
|
|
|
static gboolean gst_valve_query (GstPad * pad, GstObject * parent,
|
|
|
|
GstQuery * query);
|
2007-10-24 22:41:47 +00:00
|
|
|
|
2011-04-18 16:07:06 +00:00
|
|
|
#define _do_init \
|
2009-02-10 17:55:47 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (valve_debug, "valve", 0, "Valve");
|
2011-04-18 16:07:06 +00:00
|
|
|
#define gst_valve_parent_class parent_class
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (GstValve, gst_valve, GST_TYPE_ELEMENT, _do_init);
|
2007-10-24 22:41:47 +00:00
|
|
|
|
|
|
|
static void
|
2009-02-10 17:55:47 +00:00
|
|
|
gst_valve_class_init (GstValveClass * klass)
|
2007-10-24 22:41:47 +00:00
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
2011-04-18 16:07:06 +00:00
|
|
|
GstElementClass *gstelement_class;
|
2007-10-24 22:41:47 +00:00
|
|
|
|
|
|
|
gobject_class = (GObjectClass *) klass;
|
2011-04-18 16:07:06 +00:00
|
|
|
gstelement_class = (GstElementClass *) (klass);
|
2007-10-24 22:41:47 +00:00
|
|
|
|
2010-12-30 00:41:09 +00:00
|
|
|
gobject_class->set_property = gst_valve_set_property;
|
|
|
|
gobject_class->get_property = gst_valve_get_property;
|
2007-10-24 22:41:47 +00:00
|
|
|
|
2010-12-30 00:41:09 +00:00
|
|
|
g_object_class_install_property (gobject_class, PROP_DROP,
|
2010-12-30 00:30:18 +00:00
|
|
|
g_param_spec_boolean ("drop", "Drop buffers and events",
|
|
|
|
"Whether to drop buffers and events or let them through",
|
2014-09-13 19:12:52 +00:00
|
|
|
DEFAULT_DROP, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
2011-04-18 16:07:06 +00:00
|
|
|
|
2020-08-14 08:40:30 +00:00
|
|
|
/**
|
|
|
|
* GstValve:drop-mode
|
|
|
|
*
|
|
|
|
* Drop mode to use. By default all buffers and events are dropped.
|
|
|
|
*
|
|
|
|
* Since: 1.20
|
|
|
|
*/
|
|
|
|
g_object_class_install_property (gobject_class, PROP_DROP_MODE,
|
|
|
|
g_param_spec_enum ("drop-mode", "Drop mode",
|
|
|
|
"The drop mode to use", GST_TYPE_VALVE_DROP_MODE,
|
|
|
|
DEFAULT_DROP_MODE,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
|
|
|
|
GST_PARAM_MUTABLE_READY));
|
|
|
|
|
2016-02-27 15:36:28 +00:00
|
|
|
gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
|
|
|
|
gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
|
2011-04-18 16:07:06 +00:00
|
|
|
|
2012-04-09 12:05:07 +00:00
|
|
|
gst_element_class_set_static_metadata (gstelement_class, "Valve element",
|
2011-04-18 16:07:06 +00:00
|
|
|
"Filter", "Drops buffers and events or lets them through",
|
|
|
|
"Olivier Crete <olivier.crete@collabora.co.uk>");
|
2020-08-14 08:40:30 +00:00
|
|
|
|
|
|
|
gst_type_mark_as_plugin_api (GST_TYPE_VALVE_DROP_MODE, 0);
|
2007-10-24 22:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-04-18 16:07:06 +00:00
|
|
|
gst_valve_init (GstValve * valve)
|
2007-10-24 22:41:47 +00:00
|
|
|
{
|
2007-12-19 20:32:18 +00:00
|
|
|
valve->drop = FALSE;
|
2020-08-14 08:40:30 +00:00
|
|
|
valve->drop_mode = DEFAULT_DROP;
|
2007-12-19 20:32:18 +00:00
|
|
|
valve->discont = FALSE;
|
2007-10-24 22:41:47 +00:00
|
|
|
|
2008-08-20 18:11:02 +00:00
|
|
|
valve->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
|
2011-11-15 10:20:48 +00:00
|
|
|
gst_pad_set_query_function (valve->srcpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_valve_query));
|
2013-11-06 09:15:59 +00:00
|
|
|
GST_PAD_SET_PROXY_CAPS (valve->srcpad);
|
2008-08-20 18:11:02 +00:00
|
|
|
gst_element_add_pad (GST_ELEMENT (valve), valve->srcpad);
|
|
|
|
|
|
|
|
valve->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
|
|
|
|
gst_pad_set_chain_function (valve->sinkpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_valve_chain));
|
|
|
|
gst_pad_set_event_function (valve->sinkpad,
|
2016-03-16 14:13:39 +00:00
|
|
|
GST_DEBUG_FUNCPTR (gst_valve_sink_event));
|
2011-11-15 10:20:48 +00:00
|
|
|
gst_pad_set_query_function (valve->sinkpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_valve_query));
|
2012-01-29 22:58:51 +00:00
|
|
|
GST_PAD_SET_PROXY_CAPS (valve->sinkpad);
|
2013-11-06 09:15:59 +00:00
|
|
|
GST_PAD_SET_PROXY_ALLOCATION (valve->sinkpad);
|
2008-08-20 18:11:02 +00:00
|
|
|
gst_element_add_pad (GST_ELEMENT (valve), valve->sinkpad);
|
2007-10-24 22:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2009-02-10 17:55:47 +00:00
|
|
|
gst_valve_set_property (GObject * object,
|
|
|
|
guint prop_id, const GValue * value, GParamSpec * pspec)
|
2007-10-24 22:41:47 +00:00
|
|
|
{
|
|
|
|
GstValve *valve = GST_VALVE (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
2010-12-30 00:41:09 +00:00
|
|
|
case PROP_DROP:
|
2010-09-30 20:26:19 +00:00
|
|
|
g_atomic_int_set (&valve->drop, g_value_get_boolean (value));
|
2012-01-29 22:58:51 +00:00
|
|
|
gst_pad_push_event (valve->sinkpad, gst_event_new_reconfigure ());
|
2007-10-24 22:41:47 +00:00
|
|
|
break;
|
2020-08-14 08:40:30 +00:00
|
|
|
case PROP_DROP_MODE:
|
|
|
|
valve->drop_mode = g_value_get_enum (value);
|
|
|
|
break;
|
2010-09-28 11:26:11 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
2007-10-24 22:41:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-02-10 17:55:47 +00:00
|
|
|
gst_valve_get_property (GObject * object,
|
|
|
|
guint prop_id, GValue * value, GParamSpec * pspec)
|
2007-10-24 22:41:47 +00:00
|
|
|
{
|
|
|
|
GstValve *valve = GST_VALVE (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
2010-12-30 00:41:09 +00:00
|
|
|
case PROP_DROP:
|
2010-09-30 20:26:19 +00:00
|
|
|
g_value_set_boolean (value, g_atomic_int_get (&valve->drop));
|
2007-10-24 22:41:47 +00:00
|
|
|
break;
|
2020-08-14 08:40:30 +00:00
|
|
|
case PROP_DROP_MODE:
|
|
|
|
g_value_set_enum (value, valve->drop_mode);
|
|
|
|
break;
|
2010-09-28 11:26:11 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
2007-10-24 22:41:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-29 22:58:51 +00:00
|
|
|
|
|
|
|
static gboolean
|
|
|
|
forward_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
|
|
|
|
{
|
|
|
|
GstValve *valve = user_data;
|
|
|
|
|
|
|
|
if (!gst_pad_push_event (valve->srcpad, gst_event_ref (*event)))
|
|
|
|
valve->need_repush_sticky = TRUE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_valve_repush_sticky (GstValve * valve)
|
|
|
|
{
|
|
|
|
valve->need_repush_sticky = FALSE;
|
|
|
|
gst_pad_sticky_events_foreach (valve->sinkpad, forward_sticky_events, valve);
|
|
|
|
}
|
|
|
|
|
2007-10-24 22:41:47 +00:00
|
|
|
static GstFlowReturn
|
2011-11-17 11:40:45 +00:00
|
|
|
gst_valve_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
|
2007-10-24 22:41:47 +00:00
|
|
|
{
|
2011-11-17 11:40:45 +00:00
|
|
|
GstValve *valve = GST_VALVE (parent);
|
2007-10-24 22:41:47 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
|
|
|
|
2010-09-30 20:26:19 +00:00
|
|
|
if (g_atomic_int_get (&valve->drop)) {
|
2020-08-14 08:40:30 +00:00
|
|
|
if (valve->drop_mode == GST_VALVE_DROP_MODE_TRANSFORM_TO_GAP) {
|
|
|
|
GstEvent *ev = gst_event_new_gap (GST_BUFFER_PTS (buffer),
|
|
|
|
GST_BUFFER_DURATION (buffer));
|
|
|
|
gst_pad_push_event (valve->srcpad, ev);
|
|
|
|
}
|
2008-08-20 18:11:02 +00:00
|
|
|
gst_buffer_unref (buffer);
|
2010-09-30 20:22:04 +00:00
|
|
|
valve->discont = TRUE;
|
|
|
|
} else {
|
|
|
|
if (valve->discont) {
|
2011-03-23 19:52:27 +00:00
|
|
|
buffer = gst_buffer_make_writable (buffer);
|
2010-09-30 20:22:04 +00:00
|
|
|
GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
|
2011-01-28 20:36:33 +00:00
|
|
|
valve->discont = FALSE;
|
2010-09-30 20:22:04 +00:00
|
|
|
}
|
|
|
|
|
2012-01-29 22:58:51 +00:00
|
|
|
if (valve->need_repush_sticky)
|
|
|
|
gst_valve_repush_sticky (valve);
|
|
|
|
|
2008-08-20 18:11:02 +00:00
|
|
|
ret = gst_pad_push (valve->srcpad, buffer);
|
2010-09-30 20:22:04 +00:00
|
|
|
}
|
2007-10-24 22:41:47 +00:00
|
|
|
|
2008-12-13 05:31:45 +00:00
|
|
|
|
2010-09-30 20:13:23 +00:00
|
|
|
/* Ignore errors if "drop" was changed while the thread was blocked
|
|
|
|
* downwards
|
|
|
|
*/
|
2010-09-30 20:26:19 +00:00
|
|
|
if (g_atomic_int_get (&valve->drop))
|
2008-12-13 05:31:45 +00:00
|
|
|
ret = GST_FLOW_OK;
|
|
|
|
|
2008-08-20 18:11:02 +00:00
|
|
|
return ret;
|
2007-12-19 20:32:18 +00:00
|
|
|
}
|
|
|
|
|
2020-08-14 08:40:30 +00:00
|
|
|
static inline gboolean
|
|
|
|
gst_valve_event_needs_dropping (GstValve * valve, GstEvent * event)
|
|
|
|
{
|
|
|
|
if (!g_atomic_int_get (&valve->drop))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
switch (valve->drop_mode) {
|
|
|
|
case GST_VALVE_DROP_MODE_DROP_ALL:
|
|
|
|
return TRUE;
|
|
|
|
case GST_VALVE_DROP_MODE_FORWARD_STICKY_EVENTS:
|
|
|
|
return !GST_EVENT_IS_STICKY (event);
|
|
|
|
case GST_VALVE_DROP_MODE_TRANSFORM_TO_GAP:
|
|
|
|
return (!GST_EVENT_IS_STICKY (event) &&
|
|
|
|
GST_EVENT_TYPE (event) != GST_EVENT_GAP);
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
|
|
|
}
|
2020-09-10 17:59:51 +00:00
|
|
|
|
|
|
|
return FALSE;
|
2020-08-14 08:40:30 +00:00
|
|
|
}
|
2007-12-19 20:32:18 +00:00
|
|
|
|
2007-10-24 22:41:47 +00:00
|
|
|
static gboolean
|
2016-03-16 14:13:39 +00:00
|
|
|
gst_valve_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
|
2007-10-24 22:41:47 +00:00
|
|
|
{
|
2020-08-14 08:40:30 +00:00
|
|
|
GstValve *valve = GST_VALVE (parent);
|
|
|
|
gboolean needs_dropping = gst_valve_event_needs_dropping (valve, event);
|
2013-05-26 02:03:53 +00:00
|
|
|
gboolean is_sticky = GST_EVENT_IS_STICKY (event);
|
2007-10-24 22:41:47 +00:00
|
|
|
gboolean ret = TRUE;
|
|
|
|
|
2011-11-17 11:40:45 +00:00
|
|
|
valve = GST_VALVE (parent);
|
2011-11-15 10:20:48 +00:00
|
|
|
|
2020-08-14 08:40:30 +00:00
|
|
|
if (needs_dropping) {
|
2013-05-26 02:03:53 +00:00
|
|
|
valve->need_repush_sticky |= is_sticky;
|
2008-08-20 18:11:02 +00:00
|
|
|
gst_event_unref (event);
|
2012-01-29 22:58:51 +00:00
|
|
|
} else {
|
|
|
|
if (valve->need_repush_sticky)
|
|
|
|
gst_valve_repush_sticky (valve);
|
|
|
|
ret = gst_pad_event_default (pad, parent, event);
|
|
|
|
}
|
2008-08-20 18:11:02 +00:00
|
|
|
|
2010-09-30 20:13:23 +00:00
|
|
|
/* Ignore errors if "drop" was changed while the thread was blocked
|
2020-08-14 08:40:30 +00:00
|
|
|
* downwards, or if we're dropping but forwarding sticky events nonetheless.
|
2010-09-30 20:13:23 +00:00
|
|
|
*/
|
2012-01-29 22:58:51 +00:00
|
|
|
if (g_atomic_int_get (&valve->drop)) {
|
2020-08-14 08:40:30 +00:00
|
|
|
if (valve->drop_mode == GST_VALVE_DROP_MODE_DROP_ALL)
|
|
|
|
valve->need_repush_sticky |= is_sticky;
|
2008-12-13 05:31:45 +00:00
|
|
|
ret = TRUE;
|
2012-01-29 22:58:51 +00:00
|
|
|
}
|
2008-12-13 05:31:45 +00:00
|
|
|
|
2007-10-24 22:41:47 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-01-29 22:58:51 +00:00
|
|
|
|
|
|
|
|
2011-11-15 10:20:48 +00:00
|
|
|
static gboolean
|
2011-11-16 16:22:56 +00:00
|
|
|
gst_valve_query (GstPad * pad, GstObject * parent, GstQuery * query)
|
2008-12-10 22:00:33 +00:00
|
|
|
{
|
2012-01-29 22:58:51 +00:00
|
|
|
GstValve *valve = GST_VALVE (parent);
|
2008-12-10 22:00:33 +00:00
|
|
|
|
2015-03-19 12:51:38 +00:00
|
|
|
if (GST_QUERY_IS_SERIALIZED (query) && g_atomic_int_get (&valve->drop))
|
2012-01-29 22:58:51 +00:00
|
|
|
return FALSE;
|
2008-12-10 22:00:33 +00:00
|
|
|
|
2012-01-29 22:58:51 +00:00
|
|
|
return gst_pad_query_default (pad, parent, query);
|
2008-12-10 22:00:33 +00:00
|
|
|
}
|