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