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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, 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 %TRUE
30 * is ignored. So downstream element can be set to %GST_STATE_NULL and removed,
31 * without using pad blocking.
42 GST_DEBUG_CATEGORY_STATIC (valve_debug);
43 #define GST_CAT_DEFAULT (valve_debug)
45 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
50 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
61 #define DEFAULT_DROP FALSE
63 static void gst_valve_set_property (GObject * object,
64 guint prop_id, const GValue * value, GParamSpec * pspec);
65 static void gst_valve_get_property (GObject * object,
66 guint prop_id, GValue * value, GParamSpec * pspec);
68 static GstFlowReturn gst_valve_chain (GstPad * pad, GstObject * parent,
70 static gboolean gst_valve_event (GstPad * pad, GstObject * parent,
72 static gboolean gst_valve_query (GstPad * pad, GstObject * parent,
76 GST_DEBUG_CATEGORY_INIT (valve_debug, "valve", 0, "Valve");
77 #define gst_valve_parent_class parent_class
78 G_DEFINE_TYPE_WITH_CODE (GstValve, gst_valve, GST_TYPE_ELEMENT, _do_init);
81 gst_valve_class_init (GstValveClass * klass)
83 GObjectClass *gobject_class;
84 GstElementClass *gstelement_class;
86 gobject_class = (GObjectClass *) klass;
87 gstelement_class = (GstElementClass *) (klass);
89 gobject_class->set_property = gst_valve_set_property;
90 gobject_class->get_property = gst_valve_get_property;
92 g_object_class_install_property (gobject_class, PROP_DROP,
93 g_param_spec_boolean ("drop", "Drop buffers and events",
94 "Whether to drop buffers and events or let them through",
95 DEFAULT_DROP, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
96 G_PARAM_STATIC_STRINGS));
98 gst_element_class_add_pad_template (gstelement_class,
99 gst_static_pad_template_get (&srctemplate));
100 gst_element_class_add_pad_template (gstelement_class,
101 gst_static_pad_template_get (&sinktemplate));
103 gst_element_class_set_static_metadata (gstelement_class, "Valve element",
104 "Filter", "Drops buffers and events or lets them through",
105 "Olivier Crete <olivier.crete@collabora.co.uk>");
109 gst_valve_init (GstValve * valve)
112 valve->discont = FALSE;
114 valve->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
115 gst_pad_set_event_function (valve->srcpad,
116 GST_DEBUG_FUNCPTR (gst_valve_event));
117 gst_pad_set_query_function (valve->srcpad,
118 GST_DEBUG_FUNCPTR (gst_valve_query));
119 GST_PAD_SET_PROXY_CAPS (valve->srcpad);
120 gst_element_add_pad (GST_ELEMENT (valve), valve->srcpad);
122 valve->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
123 gst_pad_set_chain_function (valve->sinkpad,
124 GST_DEBUG_FUNCPTR (gst_valve_chain));
125 gst_pad_set_event_function (valve->sinkpad,
126 GST_DEBUG_FUNCPTR (gst_valve_event));
127 gst_pad_set_query_function (valve->sinkpad,
128 GST_DEBUG_FUNCPTR (gst_valve_query));
129 GST_PAD_SET_PROXY_CAPS (valve->sinkpad);
130 GST_PAD_SET_PROXY_ALLOCATION (valve->sinkpad);
131 gst_element_add_pad (GST_ELEMENT (valve), valve->sinkpad);
136 gst_valve_set_property (GObject * object,
137 guint prop_id, const GValue * value, GParamSpec * pspec)
139 GstValve *valve = GST_VALVE (object);
143 g_atomic_int_set (&valve->drop, g_value_get_boolean (value));
144 gst_pad_push_event (valve->sinkpad, gst_event_new_reconfigure ());
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);
170 forward_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
172 GstValve *valve = user_data;
174 if (!gst_pad_push_event (valve->srcpad, gst_event_ref (*event)))
175 valve->need_repush_sticky = TRUE;
181 gst_valve_repush_sticky (GstValve * valve)
183 valve->need_repush_sticky = FALSE;
184 gst_pad_sticky_events_foreach (valve->sinkpad, forward_sticky_events, valve);
188 gst_valve_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
190 GstValve *valve = GST_VALVE (parent);
191 GstFlowReturn ret = GST_FLOW_OK;
193 if (g_atomic_int_get (&valve->drop)) {
194 gst_buffer_unref (buffer);
195 valve->discont = TRUE;
197 if (valve->discont) {
198 buffer = gst_buffer_make_writable (buffer);
199 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
200 valve->discont = FALSE;
203 if (valve->need_repush_sticky)
204 gst_valve_repush_sticky (valve);
206 ret = gst_pad_push (valve->srcpad, buffer);
210 /* Ignore errors if "drop" was changed while the thread was blocked
213 if (g_atomic_int_get (&valve->drop))
221 gst_valve_event (GstPad * pad, GstObject * parent, GstEvent * event)
224 gboolean is_sticky = GST_EVENT_IS_STICKY (event);
227 valve = GST_VALVE (parent);
229 if (g_atomic_int_get (&valve->drop)) {
230 valve->need_repush_sticky |= is_sticky;
231 gst_event_unref (event);
233 if (valve->need_repush_sticky)
234 gst_valve_repush_sticky (valve);
235 ret = gst_pad_event_default (pad, parent, event);
238 /* Ignore errors if "drop" was changed while the thread was blocked
241 if (g_atomic_int_get (&valve->drop)) {
242 valve->need_repush_sticky |= is_sticky;
252 gst_valve_query (GstPad * pad, GstObject * parent, GstQuery * query)
254 GstValve *valve = GST_VALVE (parent);
256 if (GST_QUERY_IS_SERIALIZED (query) && g_atomic_int_get (&valve->drop))
259 return gst_pad_query_default (pad, parent, query);