2 * Farsight Voice+Video library
4 * Copyright 2007 Collabora Ltd,
5 * Copyright 2007 Nokia Corporation
6 * @author: Olivier Crete <olivier.crete@collabora.co.uk>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
25 * 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 %FALSE
31 * is ignored. So downstream element can be set to %GST_STATE_NULL and removed,
32 * without using pad blocking.
34 * Last reviewed on 2008-02-10 (0.10.11)
45 GST_DEBUG_CATEGORY (valve_debug);
46 #define GST_CAT_DEFAULT (valve_debug)
48 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
53 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
58 /* Valve signals and args */
74 static void gst_valve_set_property (GObject * object,
75 guint prop_id, const GValue * value, GParamSpec * pspec);
76 static void gst_valve_get_property (GObject * object,
77 guint prop_id, GValue * value, GParamSpec * pspec);
79 static gboolean gst_valve_event (GstPad * pad, GstEvent * event);
80 static GstFlowReturn gst_valve_buffer_alloc (GstPad * pad, guint64 offset,
81 guint size, GstCaps * caps, GstBuffer ** buf);
82 static GstFlowReturn gst_valve_chain (GstPad * pad, GstBuffer * buffer);
83 static GstCaps *gst_valve_getcaps (GstPad * pad);
88 GST_DEBUG_CATEGORY_INIT (valve_debug, "valve", 0, "Valve");
91 GST_BOILERPLATE_FULL (GstValve, gst_valve, GstElement,
92 GST_TYPE_ELEMENT, _do_init);
95 gst_valve_base_init (gpointer klass)
97 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
99 gst_element_class_add_pad_template (element_class,
100 gst_static_pad_template_get (&srctemplate));
101 gst_element_class_add_pad_template (element_class,
102 gst_static_pad_template_get (&sinktemplate));
104 gst_element_class_set_details_simple (element_class, "Valve element",
106 "This element drops all packets when drop is TRUE",
107 "Olivier Crete <olivier.crete@collabora.co.uk>");
111 gst_valve_class_init (GstValveClass * klass)
113 GObjectClass *gobject_class;
115 gobject_class = (GObjectClass *) klass;
117 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_valve_set_property);
118 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_valve_get_property);
120 g_object_class_install_property (gobject_class, ARG_DROP,
121 g_param_spec_boolean ("drop",
122 "Drops all buffers if TRUE",
123 "If this property if TRUE, the element will drop all buffers, if its FALSE, it will let them through",
124 FALSE, G_PARAM_READWRITE));
126 parent_class = g_type_class_peek_parent (klass);
130 gst_valve_init (GstValve * valve, GstValveClass * klass)
133 valve->discont = FALSE;
135 valve->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
136 gst_pad_set_getcaps_function (valve->srcpad,
137 GST_DEBUG_FUNCPTR (gst_valve_getcaps));
138 gst_element_add_pad (GST_ELEMENT (valve), valve->srcpad);
140 valve->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
141 gst_pad_set_chain_function (valve->sinkpad,
142 GST_DEBUG_FUNCPTR (gst_valve_chain));
143 gst_pad_set_event_function (valve->sinkpad,
144 GST_DEBUG_FUNCPTR (gst_valve_event));
145 gst_pad_set_bufferalloc_function (valve->sinkpad,
146 GST_DEBUG_FUNCPTR (gst_valve_buffer_alloc));
147 gst_pad_set_getcaps_function (valve->sinkpad,
148 GST_DEBUG_FUNCPTR (gst_valve_getcaps));
149 gst_element_add_pad (GST_ELEMENT (valve), valve->sinkpad);
154 gst_valve_set_property (GObject * object,
155 guint prop_id, const GValue * value, GParamSpec * pspec)
157 GstValve *valve = GST_VALVE (object);
161 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
164 GST_OBJECT_LOCK (object);
165 valve->drop = g_value_get_boolean (value);
166 GST_OBJECT_UNLOCK (object);
172 gst_valve_get_property (GObject * object,
173 guint prop_id, GValue * value, GParamSpec * pspec)
175 GstValve *valve = GST_VALVE (object);
179 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
182 GST_OBJECT_LOCK (object);
183 g_value_set_boolean (value, valve->drop);
184 GST_OBJECT_UNLOCK (object);
190 gst_valve_chain (GstPad * pad, GstBuffer * buffer)
192 GstValve *valve = GST_VALVE (gst_pad_get_parent_element (pad));
193 GstFlowReturn ret = GST_FLOW_OK;
196 GST_OBJECT_LOCK (GST_OBJECT (valve));
199 if (!drop && valve->discont) {
200 buffer = gst_buffer_make_metadata_writable (buffer);
201 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
202 valve->discont = FALSE;
204 GST_OBJECT_UNLOCK (GST_OBJECT (valve));
207 gst_buffer_unref (buffer);
209 ret = gst_pad_push (valve->srcpad, buffer);
212 GST_OBJECT_LOCK (GST_OBJECT (valve));
215 GST_OBJECT_UNLOCK (GST_OBJECT (valve));
217 gst_object_unref (valve);
224 gst_valve_event (GstPad * pad, GstEvent * event)
226 GstValve *valve = GST_VALVE (gst_pad_get_parent_element (pad));
230 GST_OBJECT_LOCK (GST_OBJECT (valve));
232 GST_OBJECT_UNLOCK (GST_OBJECT (valve));
235 gst_event_unref (event);
237 ret = gst_pad_push_event (valve->srcpad, event);
239 GST_OBJECT_LOCK (GST_OBJECT (valve));
242 GST_OBJECT_UNLOCK (GST_OBJECT (valve));
244 gst_object_unref (valve);
249 gst_valve_buffer_alloc (GstPad * pad, guint64 offset, guint size,
250 GstCaps * caps, GstBuffer ** buf)
252 GstValve *valve = GST_VALVE (gst_pad_get_parent_element (pad));
253 GstFlowReturn ret = GST_FLOW_OK;
256 GST_OBJECT_LOCK (GST_OBJECT (valve));
258 GST_OBJECT_UNLOCK (GST_OBJECT (valve));
263 ret = gst_pad_alloc_buffer (valve->srcpad, offset, size, caps, buf);
265 GST_OBJECT_LOCK (GST_OBJECT (valve));
268 GST_OBJECT_UNLOCK (GST_OBJECT (valve));
270 gst_object_unref (valve);
276 gst_valve_getcaps (GstPad * pad)
278 GstValve *valve = GST_VALVE (gst_pad_get_parent (pad));
281 if (pad == valve->sinkpad)
282 caps = gst_pad_peer_get_caps (valve->srcpad);
284 caps = gst_pad_peer_get_caps (valve->sinkpad);
287 caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
289 gst_object_unref (valve);
296 plugin_init (GstPlugin * plugin)
298 return gst_element_register (plugin, "valve",
299 GST_RANK_MARGINAL, GST_TYPE_VALVE);
302 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
306 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)