don't mix tabs and spaces
[platform/upstream/gstreamer.git] / testsuite / dynparams / dparamstest.c
1 /* GStreamer
2  * Copyright (C) 2001 Steve Baker <stevebaker_org@yahoo.co.uk>
3  *
4  * dparamstest.c: 
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 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <gst/gst.h>
30 #include <gst/control/control.h>
31
32 #define GST_TYPE_DPTEST                 (gst_dptest_get_type())
33 #define GST_DPTEST(obj)                 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_DPTEST,GstDpTest))
34 #define GST_DPTEST_CLASS(klass)         (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_DPTEST,GstDpTestClass))
35 #define GST_IS_DPTEST(obj)              (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_DPTEST))
36 #define GST_IS_DPTEST_CLASS(obj)        (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_DPTEST))
37
38 typedef struct _GstDpTest GstDpTest;
39 typedef struct _GstDpTestClass GstDpTestClass;
40
41 struct _GstDpTest
42 {
43   GstElement element;
44
45   GstPad *sinkpad;
46   GstPad *srcpad;
47   GstDParamManager *dpman;
48
49   gfloat float1;
50   gfloat float2;
51   gboolean bool1;
52   gdouble double1;
53 };
54
55 struct _GstDpTestClass
56 {
57   GstElementClass parent_class;
58 };
59
60 GType gst_dptest_get_type (void);
61
62 enum
63 {
64   ARG_0,
65 };
66
67
68 static void gst_dptest_base_init (gpointer g_class);
69 static void gst_dptest_class_init (GstDpTestClass * klass);
70 static void gst_dptest_init (GstDpTest * dptest);
71
72 static void gst_dptest_set_property (GObject * object, guint prop_id,
73     const GValue * value, GParamSpec * pspec);
74
75 static GstElementStateReturn gst_dptest_change_state (GstElement * element);
76 static void gst_dptest_chain (GstPad * pad, GstData * buf);
77
78 static GstElementClass *parent_class = NULL;
79
80 GType
81 gst_dptest_get_type (void)
82 {
83   static GType dptest_type = 0;
84
85   if (!dptest_type) {
86     static const GTypeInfo dptest_info = {
87       sizeof (GstDpTestClass),
88       gst_dptest_base_init,
89       NULL,
90       (GClassInitFunc) gst_dptest_class_init,
91       NULL,
92       NULL,
93       sizeof (GstDpTest),
94       0,
95       (GInstanceInitFunc) gst_dptest_init,
96     };
97
98     dptest_type =
99         g_type_register_static (GST_TYPE_ELEMENT, "GstDpTest", &dptest_info, 0);
100   }
101   return dptest_type;
102 }
103
104 static void
105 gst_dptest_base_init (gpointer g_class)
106 {
107   static GstElementDetails dptest_details = GST_ELEMENT_DETAILS ("DParamTest",
108       "Filter",
109       "Test for the GstDParam code",
110       "Steve Baker <stevebaker_org@yahoo.co.uk>");
111   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
112
113   gst_element_class_set_details (element_class, &dptest_details);
114
115   g_print ("got here %d\n", __LINE__);
116 }
117
118 static void
119 gst_dptest_class_init (GstDpTestClass * klass)
120 {
121   GObjectClass *gobject_class;
122   GstElementClass *gstelement_class;
123
124   gobject_class = (GObjectClass *) klass;
125   gstelement_class = (GstElementClass *) klass;
126
127   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
128
129   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_dptest_set_property);
130
131   gstelement_class->change_state = gst_dptest_change_state;
132
133 }
134
135 static void
136 gst_dptest_init (GstDpTest * dptest)
137 {
138
139   dptest->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
140   gst_element_add_pad (GST_ELEMENT (dptest), dptest->sinkpad);
141   gst_pad_set_chain_function (dptest->sinkpad, gst_dptest_chain);
142
143   dptest->srcpad = gst_pad_new ("src", GST_PAD_SRC);
144   gst_element_add_pad (GST_ELEMENT (dptest), dptest->srcpad);
145
146   dptest->dpman = gst_dpman_new ("dptest_dpman", GST_ELEMENT (dptest));
147
148   gst_dpman_add_required_dparam_direct (dptest->dpman,
149       g_param_spec_float ("float1", "float1", "float1",
150           0.0, 1.0, 0.5, G_PARAM_READWRITE), "float", &(dptest->float1)
151       );
152
153   dptest->float1 = 0.0;
154 }
155
156 static void
157 gst_dptest_set_property (GObject * object, guint prop_id, const GValue * value,
158     GParamSpec * pspec)
159 {
160   GstDpTest *dptest;
161
162   /* it's not null if we got it, but it might not be ours */
163   g_return_if_fail (GST_IS_DPTEST (object));
164
165   dptest = GST_DPTEST (object);
166
167   switch (prop_id) {
168     default:
169       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
170       break;
171   }
172 }
173
174 static GstElementStateReturn
175 gst_dptest_change_state (GstElement * element)
176 {
177   GstDpTest *dptest;
178
179   g_return_val_if_fail (GST_IS_DPTEST (element), GST_STATE_FAILURE);
180   g_print ("changing state\n");
181
182   dptest = GST_DPTEST (element);
183   if (GST_ELEMENT_CLASS (parent_class)->change_state)
184     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
185
186   return GST_STATE_SUCCESS;
187 }
188
189 static void
190 gst_dptest_chain (GstPad * pad, GstData * data)
191 {
192   GstDpTest *dptest;
193   gint frame_countdown;
194
195   dptest = GST_DPTEST (gst_pad_get_parent (pad));
196   g_assert (dptest);
197
198   /* we're using a made up buffer size of 64 and a timestamp of zero */
199   frame_countdown = GST_DPMAN_PREPROCESS (dptest->dpman, 64, 0LL);
200
201   while (GST_DPMAN_PROCESS (dptest->dpman, frame_countdown));
202
203   g_print ("dp chain\n");
204 }
205
206 gboolean
207 gst_dptest_register_elements (GstPlugin * plugin)
208 {
209   return gst_element_register (plugin, "dptest", GST_RANK_NONE,
210       GST_TYPE_DPTEST);
211 }
212
213 static GstPluginDesc plugin_desc = {
214   GST_VERSION_MAJOR,
215   GST_VERSION_MINOR,
216   "dptest_elements",
217   "test elements",
218   &gst_dptest_register_elements,
219   NULL,
220   VERSION,
221   GST_LICENSE,
222   GST_PACKAGE,
223   GST_ORIGIN
224 };
225
226 int
227 main (int argc, char *argv[])
228 {
229
230   GstElement *src;
231   GstElement *sink;
232   GstElement *testelement;
233   GstElement *pipeline;
234   GstDParamManager *dpman;
235   GstDParam *dp_float1;
236   GValue *dp_float1_value;
237
238   alarm (10);
239
240   gst_init (&argc, &argv);
241   gst_control_init (&argc, &argv);
242
243   _gst_plugin_register_static (&plugin_desc);
244
245   pipeline = gst_element_factory_make ("pipeline", "pipeline");
246   g_assert (pipeline);
247
248   src = gst_element_factory_make ("fakesrc", "src");
249   g_assert (src);
250
251   sink = gst_element_factory_make ("fakesink", "sink");
252   g_assert (sink);
253
254   testelement = gst_element_factory_make ("dptest", "testelement");
255   g_assert (testelement);
256
257   gst_element_link (src, testelement);
258   gst_element_link (testelement, sink);
259
260   gst_bin_add (GST_BIN (pipeline), src);
261   gst_bin_add (GST_BIN (pipeline), testelement);
262   gst_bin_add (GST_BIN (pipeline), sink);
263
264   g_print ("playing pipeline\n");
265
266   g_object_set (G_OBJECT (src), "num_buffers", 1, NULL);
267
268   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
269
270   /* test that dparam manager is accessable */
271   g_print ("getting dparam manager\n");
272   dpman = gst_dpman_get_manager (testelement);
273   gst_dpman_set_mode (dpman, "synchronous");
274
275   g_assert (dpman);
276   g_assert (GST_IS_DPMAN (dpman));
277
278   g_print ("creating dparam for float1\n");
279   dp_float1 = gst_dparam_new (G_TYPE_FLOAT);;
280   g_assert (dp_float1);
281   g_assert (GST_IS_DPARAM (dp_float1));
282
283   g_print ("attach dparam to float1\n");
284   g_assert (gst_dpman_attach_dparam (dpman, "float1", dp_float1));
285
286   dp_float1_value = g_new0 (GValue, 1);
287   g_value_init (dp_float1_value, G_TYPE_FLOAT);
288
289   g_value_set_float (dp_float1_value, 0.1);
290   g_object_set_property (G_OBJECT (dp_float1), "value_float", dp_float1_value);
291
292   g_print ("iterate once\n");
293   gst_bin_iterate (GST_BIN (pipeline));
294
295   g_print ("check that value changed\n");
296   g_assert (GST_DPTEST (testelement)->float1 == 0.1F);
297   g_assert (!GST_DPARAM_READY_FOR_UPDATE (dp_float1));
298
299   g_print ("nulling pipeline\n");
300   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
301
302   g_print ("playing pipeline\n");
303   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
304
305   g_print ("iterate twice\n");
306
307   g_object_set (G_OBJECT (src), "num_buffers", 2, NULL);
308   gst_bin_iterate (GST_BIN (pipeline));
309
310   return 0;
311 }