fsvalve: Make the valve element work with gst < 0.10.13
[platform/upstream/gstreamer.git] / plugins / elements / gstvalve.c
1 /*
2  * Farsight Voice+Video library
3  *
4  *  Copyright 2007 Collabora Ltd, 
5  *  Copyright 2007 Nokia Corporation
6  *   @author: Olivier Crete <olivier.crete@collabora.co.uk>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  *
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include "gstvalve.h"
30
31 #include <string.h>
32
33 GST_DEBUG_CATEGORY (valve_debug);
34 #define GST_CAT_DEFAULT (valve_debug)
35
36 /* elementfactory information */
37 static const GstElementDetails gst_valve_details =
38 GST_ELEMENT_DETAILS (
39   "Valve element",
40   "Filter",
41   "This element drops all packets when drop is TRUE",
42   "Olivier Crete <olivier.crete@collabora.co.uk>");
43
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 /* Valve signals and args */
56 enum
57 {
58   /* FILL ME */
59   LAST_SIGNAL
60 };
61
62 enum
63 {
64   ARG_0,
65   ARG_DROP,
66 };
67
68
69
70
71 static void gst_valve_set_property (GObject *object,
72     guint prop_id, const GValue * value, GParamSpec * pspec);
73 static void gst_valve_get_property (GObject *object,
74     guint prop_id, GValue *value, GParamSpec *pspec);
75 static void gst_valve_dispose (GObject *object);
76
77 static GstFlowReturn
78 gst_valve_transform_ip (GstBaseTransform *trans, GstBuffer *buf);
79 static gboolean
80 gst_valve_event (GstBaseTransform *trans, GstEvent *event);
81
82
83 static void
84 _do_init (GType type)
85 {
86   GST_DEBUG_CATEGORY_INIT
87     (valve_debug, "valve", 0, "Valve");
88 }
89
90 GST_BOILERPLATE_FULL (GstValve, gst_valve, GstBaseTransform,
91     GST_TYPE_BASE_TRANSFORM, _do_init);
92
93 static void
94 gst_valve_base_init (gpointer klass)
95 {
96   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
97
98   gst_element_class_add_pad_template (element_class,
99       gst_static_pad_template_get (&srctemplate));
100   gst_element_class_add_pad_template (element_class,
101       gst_static_pad_template_get (&sinktemplate));
102
103   gst_element_class_set_details (element_class, &gst_valve_details);
104 }
105
106 static void
107 gst_valve_class_init (GstValveClass *klass)
108 {
109   GObjectClass *gobject_class;
110   GstBaseTransformClass *gstbasetransform_class;
111
112   gobject_class = (GObjectClass *) klass;
113   gstbasetransform_class = (GstBaseTransformClass *) klass;
114
115   gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_valve_dispose);
116
117   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_valve_set_property);
118   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_valve_get_property);
119
120   gstbasetransform_class->transform_ip =
121       GST_DEBUG_FUNCPTR (gst_valve_transform_ip);
122   gstbasetransform_class->event =
123       GST_DEBUG_FUNCPTR (gst_valve_event);
124   gstbasetransform_class->src_event =
125       GST_DEBUG_FUNCPTR (gst_valve_event);
126
127   g_object_class_install_property (gobject_class, ARG_DROP,
128       g_param_spec_boolean ("drop",
129         "Drops all buffers if TRUE",
130         "If this property if TRUE, the element will drop all buffers, if its FALSE, it will let them through",
131           FALSE, G_PARAM_READWRITE));
132
133   parent_class = g_type_class_peek_parent (klass);
134 }
135
136 static void
137 gst_valve_init (GstValve *valve,
138     GstValveClass *klass)
139 {
140
141   valve->drop = 0;
142
143   gst_base_transform_set_passthrough ((GstBaseTransform *)valve, TRUE);
144
145 }
146
147 static void
148 gst_valve_dispose (GObject *object)
149 {
150   GstValve *valve = NULL;
151   valve = GST_VALVE (valve);
152
153   G_OBJECT_CLASS (parent_class)->dispose (object);
154 }
155
156
157 static void
158 gst_valve_set_property (GObject *object,
159     guint prop_id, const GValue *value, GParamSpec *pspec)
160 {
161   GstValve *valve = GST_VALVE (object);
162
163   switch (prop_id) {
164     default:
165       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
166       break;
167     case ARG_DROP:
168       GST_OBJECT_LOCK (object);
169       valve->drop = g_value_get_boolean (value);
170       GST_OBJECT_UNLOCK (object);
171       break;
172   }
173 }
174
175 static void
176 gst_valve_get_property (GObject *object,
177     guint prop_id, GValue *value, GParamSpec *pspec)
178 {
179   GstValve *valve = GST_VALVE (object);
180
181   switch (prop_id) {
182     default:
183       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
184       break;
185     case ARG_DROP:
186       GST_OBJECT_LOCK (object);
187       g_value_set_boolean (value, valve->drop);
188       GST_OBJECT_UNLOCK (object);
189       break;
190   }
191 }
192
193 static GstFlowReturn
194 gst_valve_transform_ip (GstBaseTransform *trans, GstBuffer *buf)
195 {
196   GstValve *valve = GST_VALVE (trans);
197   GstFlowReturn ret = GST_FLOW_OK;
198
199   GST_OBJECT_LOCK (GST_OBJECT (trans));
200   if (valve->drop) {
201 #if GST_VERSION_MAJOR >= 10 &&  GST_VERSION_MICRO >= 13
202     ret = GST_BASE_TRANSFORM_FLOW_DROPPED;
203 #endif
204     buf = NULL;
205   }
206   GST_OBJECT_UNLOCK (GST_OBJECT (trans));
207
208   return ret;
209 }
210
211 static gboolean
212 gst_valve_event (GstBaseTransform *trans, GstEvent *event)
213 {
214   GstValve *valve = GST_VALVE (trans);
215   gboolean ret = TRUE;
216
217   GST_OBJECT_LOCK (GST_OBJECT (trans));
218   if (valve->drop)
219     ret = FALSE;
220   GST_OBJECT_UNLOCK (GST_OBJECT (trans));
221
222   return ret;
223 }
224
225 gboolean
226 gst_valve_plugin_init (GstPlugin *plugin)
227 {
228   return gst_element_register (plugin, "fsvalve",
229       GST_RANK_MARGINAL, GST_TYPE_VALVE);
230 }
231
232 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
233     GST_VERSION_MINOR,
234     "fsvalve",
235     "Valve",
236     gst_valve_plugin_init, VERSION, "LGPL", "Farsight", "http://farsight.sf.net")