docs: remove outdated and pointless 'Last reviewed' lines from docs
[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., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, 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
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37
38 #include "gstvalve.h"
39
40 #include <string.h>
41
42 GST_DEBUG_CATEGORY_STATIC (valve_debug);
43 #define GST_CAT_DEFAULT (valve_debug)
44
45 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
46     GST_PAD_SINK,
47     GST_PAD_ALWAYS,
48     GST_STATIC_CAPS_ANY);
49
50 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
51     GST_PAD_SRC,
52     GST_PAD_ALWAYS,
53     GST_STATIC_CAPS_ANY);
54
55 enum
56 {
57   PROP_0,
58   PROP_DROP
59 };
60
61 #define DEFAULT_DROP FALSE
62
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);
67
68 static GstFlowReturn gst_valve_chain (GstPad * pad, GstObject * parent,
69     GstBuffer * buffer);
70 static gboolean gst_valve_event (GstPad * pad, GstObject * parent,
71     GstEvent * event);
72 static gboolean gst_valve_query (GstPad * pad, GstObject * parent,
73     GstQuery * query);
74
75 #define _do_init \
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);
79
80 static void
81 gst_valve_class_init (GstValveClass * klass)
82 {
83   GObjectClass *gobject_class;
84   GstElementClass *gstelement_class;
85
86   gobject_class = (GObjectClass *) klass;
87   gstelement_class = (GstElementClass *) (klass);
88
89   gobject_class->set_property = gst_valve_set_property;
90   gobject_class->get_property = gst_valve_get_property;
91
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 | G_PARAM_STATIC_STRINGS));
96
97   gst_element_class_add_pad_template (gstelement_class,
98       gst_static_pad_template_get (&srctemplate));
99   gst_element_class_add_pad_template (gstelement_class,
100       gst_static_pad_template_get (&sinktemplate));
101
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>");
105 }
106
107 static void
108 gst_valve_init (GstValve * valve)
109 {
110   valve->drop = FALSE;
111   valve->discont = FALSE;
112
113   valve->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
114   gst_pad_set_event_function (valve->srcpad,
115       GST_DEBUG_FUNCPTR (gst_valve_event));
116   gst_pad_set_query_function (valve->srcpad,
117       GST_DEBUG_FUNCPTR (gst_valve_query));
118   GST_PAD_SET_PROXY_CAPS (valve->srcpad);
119   gst_element_add_pad (GST_ELEMENT (valve), valve->srcpad);
120
121   valve->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
122   gst_pad_set_chain_function (valve->sinkpad,
123       GST_DEBUG_FUNCPTR (gst_valve_chain));
124   gst_pad_set_event_function (valve->sinkpad,
125       GST_DEBUG_FUNCPTR (gst_valve_event));
126   gst_pad_set_query_function (valve->sinkpad,
127       GST_DEBUG_FUNCPTR (gst_valve_query));
128   GST_PAD_SET_PROXY_CAPS (valve->sinkpad);
129   GST_PAD_SET_PROXY_ALLOCATION (valve->sinkpad);
130   gst_element_add_pad (GST_ELEMENT (valve), valve->sinkpad);
131 }
132
133
134 static void
135 gst_valve_set_property (GObject * object,
136     guint prop_id, const GValue * value, GParamSpec * pspec)
137 {
138   GstValve *valve = GST_VALVE (object);
139
140   switch (prop_id) {
141     case PROP_DROP:
142       g_atomic_int_set (&valve->drop, g_value_get_boolean (value));
143       gst_pad_push_event (valve->sinkpad, gst_event_new_reconfigure ());
144       break;
145     default:
146       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
147       break;
148   }
149 }
150
151 static void
152 gst_valve_get_property (GObject * object,
153     guint prop_id, GValue * value, GParamSpec * pspec)
154 {
155   GstValve *valve = GST_VALVE (object);
156
157   switch (prop_id) {
158     case PROP_DROP:
159       g_value_set_boolean (value, g_atomic_int_get (&valve->drop));
160       break;
161     default:
162       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
163       break;
164   }
165 }
166
167
168 static gboolean
169 forward_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
170 {
171   GstValve *valve = user_data;
172
173   if (!gst_pad_push_event (valve->srcpad, gst_event_ref (*event)))
174     valve->need_repush_sticky = TRUE;
175
176   return TRUE;
177 }
178
179 static void
180 gst_valve_repush_sticky (GstValve * valve)
181 {
182   valve->need_repush_sticky = FALSE;
183   gst_pad_sticky_events_foreach (valve->sinkpad, forward_sticky_events, valve);
184 }
185
186 static GstFlowReturn
187 gst_valve_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
188 {
189   GstValve *valve = GST_VALVE (parent);
190   GstFlowReturn ret = GST_FLOW_OK;
191
192   if (g_atomic_int_get (&valve->drop)) {
193     gst_buffer_unref (buffer);
194     valve->discont = TRUE;
195   } else {
196     if (valve->discont) {
197       buffer = gst_buffer_make_writable (buffer);
198       GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
199       valve->discont = FALSE;
200     }
201
202     if (valve->need_repush_sticky)
203       gst_valve_repush_sticky (valve);
204
205     ret = gst_pad_push (valve->srcpad, buffer);
206   }
207
208
209   /* Ignore errors if "drop" was changed while the thread was blocked
210    * downwards
211    */
212   if (g_atomic_int_get (&valve->drop))
213     ret = GST_FLOW_OK;
214
215   return ret;
216 }
217
218
219 static gboolean
220 gst_valve_event (GstPad * pad, GstObject * parent, GstEvent * event)
221 {
222   GstValve *valve;
223   gboolean is_sticky = GST_EVENT_IS_STICKY (event);
224   gboolean ret = TRUE;
225
226   valve = GST_VALVE (parent);
227
228   if (g_atomic_int_get (&valve->drop)) {
229     valve->need_repush_sticky |= is_sticky;
230     gst_event_unref (event);
231   } else {
232     if (valve->need_repush_sticky)
233       gst_valve_repush_sticky (valve);
234     ret = gst_pad_event_default (pad, parent, event);
235   }
236
237   /* Ignore errors if "drop" was changed while the thread was blocked
238    * downwards.
239    */
240   if (g_atomic_int_get (&valve->drop)) {
241     valve->need_repush_sticky |= is_sticky;
242     ret = TRUE;
243   }
244
245   return ret;
246 }
247
248
249
250 static gboolean
251 gst_valve_query (GstPad * pad, GstObject * parent, GstQuery * query)
252 {
253   GstValve *valve = GST_VALVE (parent);
254
255   if (g_atomic_int_get (&valve->drop))
256     return FALSE;
257
258   return gst_pad_query_default (pad, parent, query);
259 }