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
27 * The valve is a simple element that drops buffers when the #GstValve:drop
28 * property is set to %TRUE and lets then through otherwise.
30 * Any downstream error received while the #GstValve:drop property is %TRUE
31 * is ignored. So downstream element can be set to %GST_STATE_NULL and removed,
32 * without using pad blocking.
43 GST_DEBUG_CATEGORY_STATIC (valve_debug);
44 #define GST_CAT_DEFAULT (valve_debug)
46 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
51 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
62 #define DEFAULT_DROP FALSE
64 static void gst_valve_set_property (GObject * object,
65 guint prop_id, const GValue * value, GParamSpec * pspec);
66 static void gst_valve_get_property (GObject * object,
67 guint prop_id, GValue * value, GParamSpec * pspec);
69 static GstFlowReturn gst_valve_chain (GstPad * pad, GstObject * parent,
71 static gboolean gst_valve_sink_event (GstPad * pad, GstObject * parent,
73 static gboolean gst_valve_query (GstPad * pad, GstObject * parent,
77 GST_DEBUG_CATEGORY_INIT (valve_debug, "valve", 0, "Valve");
78 #define gst_valve_parent_class parent_class
79 G_DEFINE_TYPE_WITH_CODE (GstValve, gst_valve, GST_TYPE_ELEMENT, _do_init);
82 gst_valve_class_init (GstValveClass * klass)
84 GObjectClass *gobject_class;
85 GstElementClass *gstelement_class;
87 gobject_class = (GObjectClass *) klass;
88 gstelement_class = (GstElementClass *) (klass);
90 gobject_class->set_property = gst_valve_set_property;
91 gobject_class->get_property = gst_valve_get_property;
93 g_object_class_install_property (gobject_class, PROP_DROP,
94 g_param_spec_boolean ("drop", "Drop buffers and events",
95 "Whether to drop buffers and events or let them through",
96 DEFAULT_DROP, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
97 G_PARAM_STATIC_STRINGS));
99 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
100 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
102 gst_element_class_set_static_metadata (gstelement_class, "Valve element",
103 "Filter", "Drops buffers and events or lets them through",
104 "Olivier Crete <olivier.crete@collabora.co.uk>");
108 gst_valve_init (GstValve * valve)
111 valve->discont = FALSE;
113 valve->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
114 gst_pad_set_query_function (valve->srcpad,
115 GST_DEBUG_FUNCPTR (gst_valve_query));
116 GST_PAD_SET_PROXY_CAPS (valve->srcpad);
117 gst_element_add_pad (GST_ELEMENT (valve), valve->srcpad);
119 valve->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
120 gst_pad_set_chain_function (valve->sinkpad,
121 GST_DEBUG_FUNCPTR (gst_valve_chain));
122 gst_pad_set_event_function (valve->sinkpad,
123 GST_DEBUG_FUNCPTR (gst_valve_sink_event));
124 gst_pad_set_query_function (valve->sinkpad,
125 GST_DEBUG_FUNCPTR (gst_valve_query));
126 GST_PAD_SET_PROXY_CAPS (valve->sinkpad);
127 GST_PAD_SET_PROXY_ALLOCATION (valve->sinkpad);
128 gst_element_add_pad (GST_ELEMENT (valve), valve->sinkpad);
133 gst_valve_set_property (GObject * object,
134 guint prop_id, const GValue * value, GParamSpec * pspec)
136 GstValve *valve = GST_VALVE (object);
140 g_atomic_int_set (&valve->drop, g_value_get_boolean (value));
141 gst_pad_push_event (valve->sinkpad, gst_event_new_reconfigure ());
144 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
150 gst_valve_get_property (GObject * object,
151 guint prop_id, GValue * value, GParamSpec * pspec)
153 GstValve *valve = GST_VALVE (object);
157 g_value_set_boolean (value, g_atomic_int_get (&valve->drop));
160 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
167 forward_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
169 GstValve *valve = user_data;
171 if (!gst_pad_push_event (valve->srcpad, gst_event_ref (*event)))
172 valve->need_repush_sticky = TRUE;
178 gst_valve_repush_sticky (GstValve * valve)
180 valve->need_repush_sticky = FALSE;
181 gst_pad_sticky_events_foreach (valve->sinkpad, forward_sticky_events, valve);
185 gst_valve_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
187 GstValve *valve = GST_VALVE (parent);
188 GstFlowReturn ret = GST_FLOW_OK;
190 if (g_atomic_int_get (&valve->drop)) {
191 gst_buffer_unref (buffer);
192 valve->discont = TRUE;
194 if (valve->discont) {
195 buffer = gst_buffer_make_writable (buffer);
196 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
197 valve->discont = FALSE;
200 if (valve->need_repush_sticky)
201 gst_valve_repush_sticky (valve);
203 ret = gst_pad_push (valve->srcpad, buffer);
207 /* Ignore errors if "drop" was changed while the thread was blocked
210 if (g_atomic_int_get (&valve->drop))
218 gst_valve_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
221 gboolean is_sticky = GST_EVENT_IS_STICKY (event);
224 valve = GST_VALVE (parent);
226 if (g_atomic_int_get (&valve->drop)) {
227 valve->need_repush_sticky |= is_sticky;
228 gst_event_unref (event);
230 if (valve->need_repush_sticky)
231 gst_valve_repush_sticky (valve);
232 ret = gst_pad_event_default (pad, parent, event);
235 /* Ignore errors if "drop" was changed while the thread was blocked
238 if (g_atomic_int_get (&valve->drop)) {
239 valve->need_repush_sticky |= is_sticky;
249 gst_valve_query (GstPad * pad, GstObject * parent, GstQuery * query)
251 GstValve *valve = GST_VALVE (parent);
253 if (GST_QUERY_IS_SERIALIZED (query) && g_atomic_int_get (&valve->drop))
256 return gst_pad_query_default (pad, parent, query);