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
|
|
|
|
*
|
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)
|
|
|
|
|
|
|
|
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,
|
|
|
|
PROP_DROP
|
2007-10-24 22:41:47 +00:00
|
|
|
};
|
|
|
|
|
2010-12-30 00:41:09 +00:00
|
|
|
#define DEFAULT_DROP FALSE
|
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
|
|
|
|
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>");
|
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;
|
|
|
|
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;
|
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;
|
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)) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2011-11-15 10:20:48 +00:00
|
|
|
GstValve *valve;
|
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
|
|
|
|
2012-01-29 22:58:51 +00:00
|
|
|
if (g_atomic_int_get (&valve->drop)) {
|
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
|
|
|
|
* downwards.
|
|
|
|
*/
|
2012-01-29 22:58:51 +00:00
|
|
|
if (g_atomic_int_get (&valve->drop)) {
|
2013-05-26 02:03:53 +00:00
|
|
|
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
|
|
|
}
|