1 /* GStreamer valve element
2 * Copyright 2007-2009 Collabora Ltd
3 * @author: Olivier Crete <olivier.crete@collabora.co.uk>
4 * Copyright 2007-2009 Nokia Corporation
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 * SECTION:element-valve
26 * The valve is a simple element that drops buffers when the #GstValve:drop
27 * property is set to %TRUE and lets then through otherwise.
29 * Any downstream error received while the #GstValve:drop property is %FALSE
30 * is ignored. So downstream element can be set to %GST_STATE_NULL and removed,
31 * without using pad blocking.
33 * This element was previously part of gst-plugins-farsight, and then
36 * Documentation last reviewed on 2010-12-30 (0.10.31)
49 GST_DEBUG_CATEGORY_STATIC (valve_debug);
50 #define GST_CAT_DEFAULT (valve_debug)
52 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
57 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
68 #define DEFAULT_DROP FALSE
70 static void gst_valve_set_property (GObject * object,
71 guint prop_id, const GValue * value, GParamSpec * pspec);
72 static void gst_valve_get_property (GObject * object,
73 guint prop_id, GValue * value, GParamSpec * pspec);
75 static GstFlowReturn gst_valve_chain (GstPad * pad, GstObject * parent,
77 static gboolean gst_valve_sink_event (GstPad * pad, GstObject * parent,
79 static gboolean gst_valve_query (GstPad * pad, GstObject * parent,
83 GST_DEBUG_CATEGORY_INIT (valve_debug, "valve", 0, "Valve");
84 #define gst_valve_parent_class parent_class
85 G_DEFINE_TYPE_WITH_CODE (GstValve, gst_valve, GST_TYPE_ELEMENT, _do_init);
88 gst_valve_class_init (GstValveClass * klass)
90 GObjectClass *gobject_class;
91 GstElementClass *gstelement_class;
93 gobject_class = (GObjectClass *) klass;
94 gstelement_class = (GstElementClass *) (klass);
96 gobject_class->set_property = gst_valve_set_property;
97 gobject_class->get_property = gst_valve_get_property;
99 g_object_class_install_property (gobject_class, PROP_DROP,
100 g_param_spec_boolean ("drop", "Drop buffers and events",
101 "Whether to drop buffers and events or let them through",
102 DEFAULT_DROP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
104 gst_element_class_add_pad_template (gstelement_class,
105 gst_static_pad_template_get (&srctemplate));
106 gst_element_class_add_pad_template (gstelement_class,
107 gst_static_pad_template_get (&sinktemplate));
109 gst_element_class_set_details_simple (gstelement_class, "Valve element",
110 "Filter", "Drops buffers and events or lets them through",
111 "Olivier Crete <olivier.crete@collabora.co.uk>");
115 gst_valve_init (GstValve * valve)
118 valve->discont = FALSE;
120 valve->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
121 gst_pad_set_query_function (valve->srcpad,
122 GST_DEBUG_FUNCPTR (gst_valve_query));
123 gst_element_add_pad (GST_ELEMENT (valve), valve->srcpad);
125 valve->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
126 gst_pad_set_chain_function (valve->sinkpad,
127 GST_DEBUG_FUNCPTR (gst_valve_chain));
128 gst_pad_set_event_function (valve->sinkpad,
129 GST_DEBUG_FUNCPTR (gst_valve_sink_event));
130 gst_pad_set_query_function (valve->sinkpad,
131 GST_DEBUG_FUNCPTR (gst_valve_query));
132 gst_element_add_pad (GST_ELEMENT (valve), valve->sinkpad);
137 gst_valve_set_property (GObject * object,
138 guint prop_id, const GValue * value, GParamSpec * pspec)
140 GstValve *valve = GST_VALVE (object);
144 g_atomic_int_set (&valve->drop, g_value_get_boolean (value));
147 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
153 gst_valve_get_property (GObject * object,
154 guint prop_id, GValue * value, GParamSpec * pspec)
156 GstValve *valve = GST_VALVE (object);
160 g_value_set_boolean (value, g_atomic_int_get (&valve->drop));
163 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
169 gst_valve_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
171 GstValve *valve = GST_VALVE (parent);
172 GstFlowReturn ret = GST_FLOW_OK;
174 if (g_atomic_int_get (&valve->drop)) {
175 gst_buffer_unref (buffer);
176 valve->discont = TRUE;
178 if (valve->discont) {
179 buffer = gst_buffer_make_writable (buffer);
180 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
181 valve->discont = FALSE;
184 ret = gst_pad_push (valve->srcpad, buffer);
188 /* Ignore errors if "drop" was changed while the thread was blocked
191 if (g_atomic_int_get (&valve->drop))
199 gst_valve_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
204 valve = GST_VALVE (parent);
206 if (g_atomic_int_get (&valve->drop))
207 gst_event_unref (event);
209 ret = gst_pad_push_event (valve->srcpad, event);
211 /* Ignore errors if "drop" was changed while the thread was blocked
214 if (g_atomic_int_get (&valve->drop))
221 gst_valve_query (GstPad * pad, GstObject * parent, GstQuery * query)
227 valve = GST_VALVE (parent);
229 otherpad = (pad == valve->sinkpad ? valve->srcpad : valve->sinkpad);
231 switch (GST_QUERY_TYPE (query)) {
233 if (!(res = gst_pad_peer_query (otherpad, query)))
234 res = gst_pad_query_default (pad, parent, query);
237 res = gst_pad_peer_query (otherpad, query);