pad: add parent to the query function
[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, GstBuffer * buffer);
76 static gboolean gst_valve_sink_event (GstPad * pad, 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_details_simple (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_query_function (valve->srcpad,
120       GST_DEBUG_FUNCPTR (gst_valve_query));
121   gst_element_add_pad (GST_ELEMENT (valve), valve->srcpad);
122
123   valve->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
124   gst_pad_set_chain_function (valve->sinkpad,
125       GST_DEBUG_FUNCPTR (gst_valve_chain));
126   gst_pad_set_event_function (valve->sinkpad,
127       GST_DEBUG_FUNCPTR (gst_valve_sink_event));
128   gst_pad_set_query_function (valve->sinkpad,
129       GST_DEBUG_FUNCPTR (gst_valve_query));
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       break;
144     default:
145       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
146       break;
147   }
148 }
149
150 static void
151 gst_valve_get_property (GObject * object,
152     guint prop_id, GValue * value, GParamSpec * pspec)
153 {
154   GstValve *valve = GST_VALVE (object);
155
156   switch (prop_id) {
157     case PROP_DROP:
158       g_value_set_boolean (value, g_atomic_int_get (&valve->drop));
159       break;
160     default:
161       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
162       break;
163   }
164 }
165
166 static GstFlowReturn
167 gst_valve_chain (GstPad * pad, GstBuffer * buffer)
168 {
169   GstValve *valve = GST_VALVE (GST_OBJECT_PARENT (pad));
170   GstFlowReturn ret = GST_FLOW_OK;
171
172   if (g_atomic_int_get (&valve->drop)) {
173     gst_buffer_unref (buffer);
174     valve->discont = TRUE;
175   } else {
176     if (valve->discont) {
177       buffer = gst_buffer_make_writable (buffer);
178       GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
179       valve->discont = FALSE;
180     }
181
182     ret = gst_pad_push (valve->srcpad, buffer);
183   }
184
185
186   /* Ignore errors if "drop" was changed while the thread was blocked
187    * downwards
188    */
189   if (g_atomic_int_get (&valve->drop))
190     ret = GST_FLOW_OK;
191
192   return ret;
193 }
194
195
196 static gboolean
197 gst_valve_sink_event (GstPad * pad, GstEvent * event)
198 {
199   GstValve *valve;
200   gboolean ret = TRUE;
201
202   valve = GST_VALVE (GST_PAD_PARENT (pad));
203
204   if (g_atomic_int_get (&valve->drop))
205     gst_event_unref (event);
206   else
207     ret = gst_pad_push_event (valve->srcpad, event);
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 = TRUE;
214
215   return ret;
216 }
217
218 static gboolean
219 gst_valve_query (GstPad * pad, GstObject * parent, GstQuery * query)
220 {
221   GstValve *valve;
222   gboolean res;
223   GstPad *otherpad;
224
225   valve = GST_VALVE (parent);
226
227   otherpad = (pad == valve->sinkpad ? valve->srcpad : valve->sinkpad);
228
229   switch (GST_QUERY_TYPE (query)) {
230     case GST_QUERY_CAPS:
231       if (!(res = gst_pad_peer_query (otherpad, query)))
232         res = gst_pad_query_default (pad, parent, query);
233       break;
234     default:
235       res = gst_pad_peer_query (otherpad, query);
236       break;
237   }
238
239   return res;
240 }