Update for GstURIHandler get_protocols() changes
[platform/upstream/gst-plugins-good.git] / gst / debugutils / 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 GType gst_test_get_type (void);
84 #define gst_test_parent_class parent_class
85 G_DEFINE_TYPE (GstTest, gst_test, GST_TYPE_BASE_SINK);
86
87
88 static void
89 gst_test_class_init (GstTestClass * klass)
90 {
91   GstBaseSinkClass *basesink_class = GST_BASE_SINK_CLASS (klass);
92   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
93   GObjectClass *object_class = G_OBJECT_CLASS (klass);
94   guint i;
95
96   GST_DEBUG_CATEGORY_INIT (gst_test_debug, "testsink", 0,
97       "debugging category for testsink element");
98
99   object_class->set_property = gst_test_set_property;
100   object_class->get_property = gst_test_get_property;
101
102   object_class->finalize = (GObjectFinalizeFunc) gst_test_finalize;
103
104   for (i = 0; i < TESTS_COUNT; i++) {
105     GParamSpec *spec;
106
107     spec = tests[i].get_spec (&tests[i], FALSE);
108     klass->param_names[2 * i] = g_strdup (g_param_spec_get_name (spec));
109     g_object_class_install_property (object_class, 2 * i + 1, spec);
110     spec = tests[i].get_spec (&tests[i], TRUE);
111     klass->param_names[2 * i + 1] = g_strdup (g_param_spec_get_name (spec));
112     g_object_class_install_property (object_class, 2 * i + 2, spec);
113   }
114
115   gst_element_class_add_pad_template (gstelement_class,
116       gst_static_pad_template_get (&sinktemplate));
117
118   gst_element_class_set_details_simple (gstelement_class, "Test plugin",
119       "Testing", "perform a number of tests", "Benjamin Otte <otte@gnome>");
120
121   basesink_class->render = GST_DEBUG_FUNCPTR (gst_test_render_buffer);
122   basesink_class->event = GST_DEBUG_FUNCPTR (gst_test_sink_event);
123   basesink_class->start = GST_DEBUG_FUNCPTR (gst_test_start);
124   basesink_class->stop = GST_DEBUG_FUNCPTR (gst_test_stop);
125 }
126
127 static void
128 gst_test_init (GstTest * test)
129 {
130   GstTestClass *klass;
131   guint i;
132
133   klass = GST_TEST_GET_CLASS (test);
134   for (i = 0; i < TESTS_COUNT; i++) {
135     GParamSpec *spec = g_object_class_find_property (G_OBJECT_CLASS (klass),
136         klass->param_names[2 * i + 1]);
137
138     g_value_init (&test->values[i], G_PARAM_SPEC_VALUE_TYPE (spec));
139   }
140 }
141
142 static void
143 gst_test_finalize (GstTest * test)
144 {
145   guint i;
146
147   for (i = 0; i < TESTS_COUNT; i++) {
148     g_value_unset (&test->values[i]);
149   }
150
151   G_OBJECT_CLASS (parent_class)->finalize ((GObject *) test);
152 }
153
154 static void
155 tests_unset (GstTest * test)
156 {
157   guint i;
158
159   for (i = 0; i < TESTS_COUNT; i++) {
160     if (test->tests[i]) {
161       tests[i].free (test->tests[i]);
162       test->tests[i] = NULL;
163     }
164   }
165 }
166
167 static void
168 tests_set (GstTest * test)
169 {
170   guint i;
171
172   for (i = 0; i < TESTS_COUNT; i++) {
173     g_assert (test->tests[i] == NULL);
174     test->tests[i] = tests[i].new (&tests[i]);
175   }
176 }
177
178 static gboolean
179 gst_test_sink_event (GstBaseSink * basesink, GstEvent * event)
180 {
181   GstTestClass *klass = GST_TEST_GET_CLASS (basesink);
182   GstTest *test = GST_TEST (basesink);
183   gboolean ret = FALSE;
184
185   switch (GST_EVENT_TYPE (event)) {
186 /*
187     case GST_EVENT_NEWSEGMENT:
188       if (GST_EVENT_DISCONT_NEW_MEDIA (event)) {
189         tests_unset (test);
190         tests_set (test);
191       }
192       break;
193 */
194     case GST_EVENT_EOS:{
195       gint i;
196
197       g_object_freeze_notify (G_OBJECT (test));
198       for (i = 0; i < TESTS_COUNT; i++) {
199         if (test->tests[i]) {
200           if (!tests[i].finish (test->tests[i], &test->values[i])) {
201             GValue v = { 0, };
202             gchar *real, *expected;
203
204             expected = gst_value_serialize (&test->values[i]);
205             g_value_init (&v, G_VALUE_TYPE (&test->values[i]));
206             g_object_get_property (G_OBJECT (test), klass->param_names[2 * i],
207                 &v);
208             real = gst_value_serialize (&v);
209             g_value_unset (&v);
210             GST_ELEMENT_ERROR (test, STREAM, FORMAT, (NULL),
211                 ("test %s returned value \"%s\" and not expected value \"%s\"",
212                     klass->param_names[2 * i], real, expected));
213             g_free (real);
214             g_free (expected);
215           }
216           g_object_notify (G_OBJECT (test), klass->param_names[2 * i]);
217         }
218       }
219       g_object_thaw_notify (G_OBJECT (test));
220       ret = TRUE;
221       break;
222     }
223     default:
224       break;
225   }
226
227   return ret;
228 }
229
230 static GstFlowReturn
231 gst_test_render_buffer (GstBaseSink * basesink, GstBuffer * buf)
232 {
233   GstTest *test = GST_TEST (basesink);
234   guint i;
235
236   for (i = 0; i < TESTS_COUNT; i++) {
237     if (test->tests[i]) {
238       tests[i].add (test->tests[i], buf);
239     }
240   }
241   return GST_FLOW_OK;
242 }
243
244 static gboolean
245 gst_test_start (GstBaseSink * sink)
246 {
247   GstTest *test = GST_TEST (sink);
248
249   tests_set (test);
250   return TRUE;
251 }
252
253 static gboolean
254 gst_test_stop (GstBaseSink * sink)
255 {
256   GstTest *test = GST_TEST (sink);
257
258   tests_unset (test);
259   return TRUE;
260 }
261
262 static void
263 gst_test_set_property (GObject * object, guint prop_id,
264     const GValue * value, GParamSpec * pspec)
265 {
266   GstTest *test = GST_TEST (object);
267
268   if (prop_id == 0 || prop_id > 2 * TESTS_COUNT) {
269     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
270     return;
271   }
272
273   if (prop_id % 2) {
274     /* real values can't be set */
275     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
276   } else {
277     /* expected values */
278     GST_OBJECT_LOCK (test);
279     g_value_copy (value, &test->values[prop_id / 2 - 1]);
280     GST_OBJECT_UNLOCK (test);
281   }
282 }
283
284 static void
285 gst_test_get_property (GObject * object, guint prop_id, GValue * value,
286     GParamSpec * pspec)
287 {
288   GstTest *test = GST_TEST (object);
289   guint id = (prop_id - 1) / 2;
290
291   if (prop_id == 0 || prop_id > 2 * TESTS_COUNT) {
292     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
293     return;
294   }
295
296   GST_OBJECT_LOCK (test);
297
298   if (prop_id % 2) {
299     /* real values */
300     tests[id].get_value (test->tests[id], value);
301   } else {
302     /* expected values */
303     g_value_copy (&test->values[id], value);
304   }
305
306   GST_OBJECT_UNLOCK (test);
307 }