Merge branch 'master' into 0.11
[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 gboolean gst_valve_event (GstPad * pad, GstEvent * event);
76 static GstFlowReturn gst_valve_chain (GstPad * pad, GstBuffer * buffer);
77 static GstCaps *gst_valve_getcaps (GstPad * pad, GstCaps * filter);
78
79 #define _do_init \
80   GST_DEBUG_CATEGORY_INIT (valve_debug, "valve", 0, "Valve");
81 #define gst_valve_parent_class parent_class
82 G_DEFINE_TYPE_WITH_CODE (GstValve, gst_valve, GST_TYPE_ELEMENT, _do_init);
83
84 static void
85 gst_valve_class_init (GstValveClass * klass)
86 {
87   GObjectClass *gobject_class;
88   GstElementClass *gstelement_class;
89
90   gobject_class = (GObjectClass *) klass;
91   gstelement_class = (GstElementClass *) (klass);
92
93   gobject_class->set_property = gst_valve_set_property;
94   gobject_class->get_property = gst_valve_get_property;
95
96   g_object_class_install_property (gobject_class, PROP_DROP,
97       g_param_spec_boolean ("drop", "Drop buffers and events",
98           "Whether to drop buffers and events or let them through",
99           DEFAULT_DROP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
100
101   gst_element_class_add_pad_template (gstelement_class,
102       gst_static_pad_template_get (&srctemplate));
103   gst_element_class_add_pad_template (gstelement_class,
104       gst_static_pad_template_get (&sinktemplate));
105
106   gst_element_class_set_details_simple (gstelement_class, "Valve element",
107       "Filter", "Drops buffers and events or lets them through",
108       "Olivier Crete <olivier.crete@collabora.co.uk>");
109 }
110
111 static void
112 gst_valve_init (GstValve * valve)
113 {
114   valve->drop = FALSE;
115   valve->discont = FALSE;
116
117   valve->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
118   gst_pad_set_getcaps_function (valve->srcpad,
119       GST_DEBUG_FUNCPTR (gst_valve_getcaps));
120   gst_element_add_pad (GST_ELEMENT (valve), valve->srcpad);
121
122   valve->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
123   gst_pad_set_chain_function (valve->sinkpad,
124       GST_DEBUG_FUNCPTR (gst_valve_chain));
125   gst_pad_set_event_function (valve->sinkpad,
126       GST_DEBUG_FUNCPTR (gst_valve_event));
127   gst_pad_set_getcaps_function (valve->sinkpad,
128       GST_DEBUG_FUNCPTR (gst_valve_getcaps));
129   gst_element_add_pad (GST_ELEMENT (valve), valve->sinkpad);
130 }
131
132
133 static void
134 gst_valve_set_property (GObject * object,
135     guint prop_id, const GValue * value, GParamSpec * pspec)
136 {
137   GstValve *valve = GST_VALVE (object);
138
139   switch (prop_id) {
140     case PROP_DROP:
141       g_atomic_int_set (&valve->drop, g_value_get_boolean (value));
142       break;
143     default:
144       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
145       break;
146   }
147 }
148
149 static void
150 gst_valve_get_property (GObject * object,
151     guint prop_id, GValue * value, GParamSpec * pspec)
152 {
153   GstValve *valve = GST_VALVE (object);
154
155   switch (prop_id) {
156     case PROP_DROP:
157       g_value_set_boolean (value, g_atomic_int_get (&valve->drop));
158       break;
159     default:
160       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
161       break;
162   }
163 }
164
165 static GstFlowReturn
166 gst_valve_chain (GstPad * pad, GstBuffer * buffer)
167 {
168   GstValve *valve = GST_VALVE (GST_OBJECT_PARENT (pad));
169   GstFlowReturn ret = GST_FLOW_OK;
170
171   if (g_atomic_int_get (&valve->drop)) {
172     gst_buffer_unref (buffer);
173     valve->discont = TRUE;
174   } else {
175     if (valve->discont) {
176       buffer = gst_buffer_make_writable (buffer);
177       GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
178       valve->discont = FALSE;
179     }
180
181     ret = gst_pad_push (valve->srcpad, buffer);
182   }
183
184
185   /* Ignore errors if "drop" was changed while the thread was blocked
186    * downwards
187    */
188   if (g_atomic_int_get (&valve->drop))
189     ret = GST_FLOW_OK;
190
191   return ret;
192 }
193
194
195 static gboolean
196 gst_valve_event (GstPad * pad, GstEvent * event)
197 {
198   GstValve *valve = GST_VALVE (gst_pad_get_parent_element (pad));
199   gboolean ret = TRUE;
200
201   if (g_atomic_int_get (&valve->drop))
202     gst_event_unref (event);
203   else
204     ret = gst_pad_push_event (valve->srcpad, event);
205
206   /* Ignore errors if "drop" was changed while the thread was blocked
207    * downwards.
208    */
209   if (g_atomic_int_get (&valve->drop))
210     ret = TRUE;
211
212   gst_object_unref (valve);
213   return ret;
214 }
215
216 static GstCaps *
217 gst_valve_getcaps (GstPad * pad, GstCaps * filter)
218 {
219   GstValve *valve = GST_VALVE (gst_pad_get_parent (pad));
220   GstCaps *caps;
221
222   if (pad == valve->sinkpad)
223     caps = gst_pad_peer_get_caps (valve->srcpad, filter);
224   else
225     caps = gst_pad_peer_get_caps (valve->sinkpad, filter);
226
227   if (caps == NULL)
228     caps = (filter ? gst_caps_ref (filter) : gst_caps_new_any ());
229
230   gst_object_unref (valve);
231
232   return caps;
233 }