Remove 0.10-related documentation and "Since" markers
[platform/upstream/gstreamer.git] / plugins / elements / gstvalve.c
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
5  *
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.
10  *
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.
15  *
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.
20  *
21  */
22
23 /**
24  * SECTION:element-valve
25  *
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.
28  *
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.
32  *
33  * This element was previously part of gst-plugins-farsight, and then
34  * gst-plugins-bad.
35  *
36  * Documentation last reviewed on 2010-12-30 (0.10.31)
37  */
38
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42
43 #include "gstvalve.h"
44
45 #include <string.h>
46
47 GST_DEBUG_CATEGORY_STATIC (valve_debug);
48 #define GST_CAT_DEFAULT (valve_debug)
49
50 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
51     GST_PAD_SINK,
52     GST_PAD_ALWAYS,
53     GST_STATIC_CAPS_ANY);
54
55 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
56     GST_PAD_SRC,
57     GST_PAD_ALWAYS,
58     GST_STATIC_CAPS_ANY);
59
60 enum
61 {
62   PROP_0,
63   PROP_DROP
64 };
65
66 #define DEFAULT_DROP FALSE
67
68 static void gst_valve_set_property (GObject * object,
69     guint prop_id, const GValue * value, GParamSpec * pspec);
70 static void gst_valve_get_property (GObject * object,
71     guint prop_id, GValue * value, GParamSpec * pspec);
72
73 static GstFlowReturn gst_valve_chain (GstPad * pad, GstObject * parent,
74     GstBuffer * buffer);
75 static gboolean gst_valve_event (GstPad * pad, GstObject * parent,
76     GstEvent * event);
77 static gboolean gst_valve_query (GstPad * pad, GstObject * parent,
78     GstQuery * query);
79
80 #define _do_init \
81   GST_DEBUG_CATEGORY_INIT (valve_debug, "valve", 0, "Valve");
82 #define gst_valve_parent_class parent_class
83 G_DEFINE_TYPE_WITH_CODE (GstValve, gst_valve, GST_TYPE_ELEMENT, _do_init);
84
85 static void
86 gst_valve_class_init (GstValveClass * klass)
87 {
88   GObjectClass *gobject_class;
89   GstElementClass *gstelement_class;
90
91   gobject_class = (GObjectClass *) klass;
92   gstelement_class = (GstElementClass *) (klass);
93
94   gobject_class->set_property = gst_valve_set_property;
95   gobject_class->get_property = gst_valve_get_property;
96
97   g_object_class_install_property (gobject_class, PROP_DROP,
98       g_param_spec_boolean ("drop", "Drop buffers and events",
99           "Whether to drop buffers and events or let them through",
100           DEFAULT_DROP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
101
102   gst_element_class_add_pad_template (gstelement_class,
103       gst_static_pad_template_get (&srctemplate));
104   gst_element_class_add_pad_template (gstelement_class,
105       gst_static_pad_template_get (&sinktemplate));
106
107   gst_element_class_set_static_metadata (gstelement_class, "Valve element",
108       "Filter", "Drops buffers and events or lets them through",
109       "Olivier Crete <olivier.crete@collabora.co.uk>");
110 }
111
112 static void
113 gst_valve_init (GstValve * valve)
114 {
115   valve->drop = FALSE;
116   valve->discont = FALSE;
117
118   valve->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
119   gst_pad_set_event_function (valve->srcpad,
120       GST_DEBUG_FUNCPTR (gst_valve_event));
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);
124
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_event));
130   gst_pad_set_query_function (valve->sinkpad,
131       GST_DEBUG_FUNCPTR (gst_valve_query));
132   GST_PAD_SET_PROXY_CAPS (valve->sinkpad);
133   gst_element_add_pad (GST_ELEMENT (valve), valve->sinkpad);
134 }
135
136
137 static void
138 gst_valve_set_property (GObject * object,
139     guint prop_id, const GValue * value, GParamSpec * pspec)
140 {
141   GstValve *valve = GST_VALVE (object);
142
143   switch (prop_id) {
144     case PROP_DROP:
145       g_atomic_int_set (&valve->drop, g_value_get_boolean (value));
146       gst_pad_push_event (valve->sinkpad, gst_event_new_reconfigure ());
147       break;
148     default:
149       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
150       break;
151   }
152 }
153
154 static void
155 gst_valve_get_property (GObject * object,
156     guint prop_id, GValue * value, GParamSpec * pspec)
157 {
158   GstValve *valve = GST_VALVE (object);
159
160   switch (prop_id) {
161     case PROP_DROP:
162       g_value_set_boolean (value, g_atomic_int_get (&valve->drop));
163       break;
164     default:
165       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
166       break;
167   }
168 }
169
170
171 static gboolean
172 forward_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
173 {
174   GstValve *valve = user_data;
175
176   if (!gst_pad_push_event (valve->srcpad, gst_event_ref (*event)))
177     valve->need_repush_sticky = TRUE;
178
179   return TRUE;
180 }
181
182 static void
183 gst_valve_repush_sticky (GstValve * valve)
184 {
185   valve->need_repush_sticky = FALSE;
186   gst_pad_sticky_events_foreach (valve->sinkpad, forward_sticky_events, valve);
187 }
188
189 static GstFlowReturn
190 gst_valve_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
191 {
192   GstValve *valve = GST_VALVE (parent);
193   GstFlowReturn ret = GST_FLOW_OK;
194
195   if (g_atomic_int_get (&valve->drop)) {
196     gst_buffer_unref (buffer);
197     valve->discont = TRUE;
198   } else {
199     if (valve->discont) {
200       buffer = gst_buffer_make_writable (buffer);
201       GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
202       valve->discont = FALSE;
203     }
204
205     if (valve->need_repush_sticky)
206       gst_valve_repush_sticky (valve);
207
208     ret = gst_pad_push (valve->srcpad, buffer);
209   }
210
211
212   /* Ignore errors if "drop" was changed while the thread was blocked
213    * downwards
214    */
215   if (g_atomic_int_get (&valve->drop))
216     ret = GST_FLOW_OK;
217
218   return ret;
219 }
220
221
222 static gboolean
223 gst_valve_event (GstPad * pad, GstObject * parent, GstEvent * event)
224 {
225   GstValve *valve;
226   gboolean ret = TRUE;
227
228   valve = GST_VALVE (parent);
229
230   if (g_atomic_int_get (&valve->drop)) {
231     valve->need_repush_sticky |= GST_EVENT_IS_STICKY (event);
232     gst_event_unref (event);
233   } else {
234     if (valve->need_repush_sticky)
235       gst_valve_repush_sticky (valve);
236     ret = gst_pad_event_default (pad, parent, event);
237   }
238
239   /* Ignore errors if "drop" was changed while the thread was blocked
240    * downwards.
241    */
242   if (g_atomic_int_get (&valve->drop)) {
243     valve->need_repush_sticky |= GST_EVENT_IS_STICKY (event);
244     ret = TRUE;
245   }
246
247   return ret;
248 }
249
250
251
252 static gboolean
253 gst_valve_query (GstPad * pad, GstObject * parent, GstQuery * query)
254 {
255   GstValve *valve = GST_VALVE (parent);
256
257   if (g_atomic_int_get (&valve->drop))
258     return FALSE;
259
260   return gst_pad_query_default (pad, parent, query);
261 }