f118a5150a0413e0e7dfe52c4517f8d9b7d37def
[platform/upstream/gstreamer.git] / gst / debug / testplugin.c
1 /* GStreamer
2  * Copyright (C) 2004 Benjamin Otte <otte@gnome.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <gst/gst.h>
25 #include <gst/base/gstbasesink.h>
26 #include "tests.h"
27
28 GST_DEBUG_CATEGORY_STATIC (gst_test_debug);
29 #define GST_CAT_DEFAULT gst_test_debug
30
31 /* This plugin does all the tests registered in the tests.h file
32  */
33
34 #define GST_TYPE_TEST \
35   (gst_test_get_type())
36 #define GST_TEST(obj) \
37   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_TEST,GstTest))
38 #define GST_TEST_CLASS(klass) \
39   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_TEST,GstTestClass))
40 #define GST_TEST_GET_CLASS(obj) \
41   (G_TYPE_INSTANCE_GET_CLASS ((obj),GST_TYPE_TEST,GstTestClass))
42 #define GST_IS_TEST(obj) \
43   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_TEST))
44 #define GST_IS_TEST_CLASS(klass) \
45   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_TEST))
46
47 typedef struct _GstTest GstTest;
48 typedef struct _GstTestClass GstTestClass;
49
50 struct _GstTest
51 {
52   GstBaseSink basesink;
53
54   gpointer tests[TESTS_COUNT];
55   GValue values[TESTS_COUNT];
56 };
57
58 struct _GstTestClass
59 {
60   GstBaseSinkClass parent_class;
61
62   gchar *param_names[2 * TESTS_COUNT];
63 };
64
65 static void gst_test_finalize (GstTest * test);
66
67 static gboolean gst_test_start (GstBaseSink * trans);
68 static gboolean gst_test_stop (GstBaseSink * trans);
69 static gboolean gst_test_sink_event (GstBaseSink * basesink, GstEvent * event);
70 static GstFlowReturn gst_test_render_buffer (GstBaseSink * basesink,
71     GstBuffer * buf);
72
73 static void gst_test_set_property (GObject * object, guint prop_id,
74     const GValue * value, GParamSpec * pspec);
75 static void gst_test_get_property (GObject * object, guint prop_id,
76     GValue * value, GParamSpec * pspec);
77
78 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
79     GST_PAD_SINK,
80     GST_PAD_ALWAYS,
81     GST_STATIC_CAPS_ANY);
82
83
84 static const GstElementDetails details = GST_ELEMENT_DETAILS ("Test plugin",
85     "Testing",
86     "perform a number of tests",
87     "Benjamin Otte <otte@gnome>");
88
89 GST_BOILERPLATE (GstTest, gst_test, GstBaseSink, GST_TYPE_BASE_SINK);
90
91 static void
92 gst_test_base_init (gpointer g_class)
93 {
94   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
95
96   gst_element_class_add_pad_template (gstelement_class,
97       gst_static_pad_template_get (&sinktemplate));
98
99   gst_element_class_set_details (gstelement_class, &details);
100 }
101
102 static void
103 gst_test_class_init (GstTestClass * klass)
104 {
105   GstBaseSinkClass *basesink_class = GST_BASE_SINK_CLASS (klass);
106   GObjectClass *object_class = G_OBJECT_CLASS (klass);
107   guint i;
108
109   object_class->set_property = GST_DEBUG_FUNCPTR (gst_test_set_property);
110   object_class->get_property = GST_DEBUG_FUNCPTR (gst_test_get_property);
111
112   object_class->finalize = (GObjectFinalizeFunc) gst_test_finalize;
113
114   for (i = 0; i < TESTS_COUNT; i++) {
115     GParamSpec *spec;
116
117     spec = tests[i].get_spec (&tests[i], FALSE);
118     klass->param_names[2 * i] = g_strdup (g_param_spec_get_name (spec));
119     g_object_class_install_property (object_class, 2 * i + 1, spec);
120     spec = tests[i].get_spec (&tests[i], TRUE);
121     klass->param_names[2 * i + 1] = g_strdup (g_param_spec_get_name (spec));
122     g_object_class_install_property (object_class, 2 * i + 2, spec);
123   }
124
125   basesink_class->preroll = GST_DEBUG_FUNCPTR (gst_test_render_buffer);
126   basesink_class->render = GST_DEBUG_FUNCPTR (gst_test_render_buffer);
127   basesink_class->event = GST_DEBUG_FUNCPTR (gst_test_sink_event);
128   basesink_class->start = GST_DEBUG_FUNCPTR (gst_test_start);
129   basesink_class->stop = GST_DEBUG_FUNCPTR (gst_test_stop);
130 }
131
132 static void
133 gst_test_init (GstTest * test, GstTestClass * g_class)
134 {
135   GstTestClass *klass;
136   guint i;
137
138   klass = GST_TEST_GET_CLASS (test);
139   for (i = 0; i < TESTS_COUNT; i++) {
140     GParamSpec *spec = g_object_class_find_property (G_OBJECT_CLASS (klass),
141         klass->param_names[2 * i + 1]);
142
143     g_value_init (&test->values[i], G_PARAM_SPEC_VALUE_TYPE (spec));
144   }
145 }
146
147 static void
148 gst_test_finalize (GstTest * test)
149 {
150   guint i;
151
152   for (i = 0; i < TESTS_COUNT; i++) {
153     g_value_unset (&test->values[i]);
154   }
155
156   G_OBJECT_CLASS (parent_class)->finalize ((GObject *) test);
157 }
158
159 static void
160 tests_unset (GstTest * test)
161 {
162   guint i;
163
164   for (i = 0; i < TESTS_COUNT; i++) {
165     if (test->tests[i]) {
166       tests[i].free (test->tests[i]);
167       test->tests[i] = NULL;
168     }
169   }
170 }
171
172 static void
173 tests_set (GstTest * test)
174 {
175   guint i;
176
177   for (i = 0; i < TESTS_COUNT; i++) {
178     g_assert (test->tests[i] == NULL);
179     test->tests[i] = tests[i].new (&tests[i]);
180   }
181 }
182
183 static gboolean
184 gst_test_sink_event (GstBaseSink * basesink, GstEvent * event)
185 {
186   GstTestClass *klass = GST_TEST_GET_CLASS (basesink);
187   GstTest *test = GST_TEST (basesink);
188   gboolean ret = FALSE;
189
190   switch (GST_EVENT_TYPE (event)) {
191 /*
192     case GST_EVENT_NEWSEGMENT:
193       if (GST_EVENT_DISCONT_NEW_MEDIA (event)) {
194         tests_unset (test);
195         tests_set (test);
196       }
197       break;
198 */
199     case GST_EVENT_EOS:{
200       gint i;
201
202       g_object_freeze_notify (G_OBJECT (test));
203       for (i = 0; i < TESTS_COUNT; i++) {
204         if (test->tests[i]) {
205           if (!tests[i].finish (test->tests[i], &test->values[i])) {
206             GValue v = { 0, };
207             gchar *real, *expected;
208
209             expected = gst_value_serialize (&test->values[i]);
210             g_value_init (&v, G_VALUE_TYPE (&test->values[i]));
211             g_object_get_property (G_OBJECT (test), klass->param_names[2 * i],
212                 &v);
213             real = gst_value_serialize (&v);
214             g_value_unset (&v);
215             GST_ELEMENT_ERROR (test, STREAM, FORMAT, (NULL),
216                 ("test %s returned value \"%s\" and not expected value \"%s\"",
217                     klass->param_names[2 * i], real, expected));
218             g_free (real);
219             g_free (expected);
220           }
221           g_object_notify (G_OBJECT (test), klass->param_names[2 * i]);
222         }
223       }
224       g_object_thaw_notify (G_OBJECT (test));
225       ret = TRUE;
226       break;
227     }
228     default:
229       break;
230   }
231
232   return ret;
233 }
234
235 static GstFlowReturn
236 gst_test_render_buffer (GstBaseSink * basesink, GstBuffer * buf)
237 {
238   GstTest *test = GST_TEST (basesink);
239   guint i;
240
241   for (i = 0; i < TESTS_COUNT; i++) {
242     if (test->tests[i]) {
243       tests[i].add (test->tests[i], buf);
244     }
245   }
246   return GST_FLOW_OK;
247 }
248
249 static gboolean
250 gst_test_start (GstBaseSink * sink)
251 {
252   GstTest *test = GST_TEST (sink);
253
254   tests_set (test);
255   return TRUE;
256 }
257
258 static gboolean
259 gst_test_stop (GstBaseSink * sink)
260 {
261   GstTest *test = GST_TEST (sink);
262
263   tests_unset (test);
264   return TRUE;
265 }
266
267 static void
268 gst_test_set_property (GObject * object, guint prop_id,
269     const GValue * value, GParamSpec * pspec)
270 {
271   GstTest *test = GST_TEST (object);
272
273   if (prop_id == 0 || prop_id > 2 * TESTS_COUNT) {
274     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
275     return;
276   }
277
278   if (prop_id % 2) {
279     /* real values can't be set */
280     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
281   } else {
282     /* expected values */
283     GST_OBJECT_LOCK (test);
284     g_value_copy (value, &test->values[prop_id / 2 - 1]);
285     GST_OBJECT_UNLOCK (test);
286   }
287 }
288
289 static void
290 gst_test_get_property (GObject * object, guint prop_id, GValue * value,
291     GParamSpec * pspec)
292 {
293   GstTest *test = GST_TEST (object);
294   guint id = (prop_id - 1) / 2;
295
296   if (prop_id == 0 || prop_id > 2 * TESTS_COUNT) {
297     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
298     return;
299   }
300
301   GST_OBJECT_LOCK (test);
302
303   if (prop_id % 2) {
304     /* real values */
305     tests[id].get_value (test->tests[id], value);
306   } else {
307     /* expected values */
308     g_value_copy (&test->values[id], value);
309   }
310
311   GST_OBJECT_UNLOCK (test);
312 }
313
314 gboolean
315 gst_test_plugin_init (GstPlugin * plugin)
316 {
317   if (!gst_element_register (plugin, "testsink", GST_RANK_NONE, GST_TYPE_TEST))
318     return FALSE;
319
320   GST_DEBUG_CATEGORY_INIT (gst_test_debug, "testsink", 0,
321       "debugging category for testsink element");
322
323   return TRUE;
324 }