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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, 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
gst_test_class_init(GstTestClass * klass)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_static_pad_template (gstelement_class, &sinktemplate);
116
117 gst_element_class_set_static_metadata (gstelement_class, "Test plugin",
118 "Testing", "perform a number of tests", "Benjamin Otte <otte@gnome>");
119
120 basesink_class->render = GST_DEBUG_FUNCPTR (gst_test_render_buffer);
121 basesink_class->event = GST_DEBUG_FUNCPTR (gst_test_sink_event);
122 basesink_class->start = GST_DEBUG_FUNCPTR (gst_test_start);
123 basesink_class->stop = GST_DEBUG_FUNCPTR (gst_test_stop);
124 }
125
126 static void
gst_test_init(GstTest * test)127 gst_test_init (GstTest * test)
128 {
129 GstTestClass *klass;
130 guint i;
131
132 klass = GST_TEST_GET_CLASS (test);
133 for (i = 0; i < TESTS_COUNT; i++) {
134 GParamSpec *spec = g_object_class_find_property (G_OBJECT_CLASS (klass),
135 klass->param_names[2 * i + 1]);
136
137 g_value_init (&test->values[i], G_PARAM_SPEC_VALUE_TYPE (spec));
138 }
139 }
140
141 static void
gst_test_finalize(GstTest * test)142 gst_test_finalize (GstTest * test)
143 {
144 guint i;
145
146 for (i = 0; i < TESTS_COUNT; i++) {
147 g_value_unset (&test->values[i]);
148 }
149
150 G_OBJECT_CLASS (parent_class)->finalize ((GObject *) test);
151 }
152
153 static void
tests_unset(GstTest * test)154 tests_unset (GstTest * test)
155 {
156 guint i;
157
158 for (i = 0; i < TESTS_COUNT; i++) {
159 if (test->tests[i]) {
160 tests[i].free (test->tests[i]);
161 test->tests[i] = NULL;
162 }
163 }
164 }
165
166 static void
tests_set(GstTest * test)167 tests_set (GstTest * test)
168 {
169 guint i;
170
171 for (i = 0; i < TESTS_COUNT; i++) {
172 g_assert (test->tests[i] == NULL);
173 test->tests[i] = tests[i].new (&tests[i]);
174 }
175 }
176
177 static gboolean
gst_test_sink_event(GstBaseSink * basesink,GstEvent * event)178 gst_test_sink_event (GstBaseSink * basesink, GstEvent * event)
179 {
180 GstTestClass *klass = GST_TEST_GET_CLASS (basesink);
181 GstTest *test = GST_TEST (basesink);
182
183 switch (GST_EVENT_TYPE (event)) {
184 /*
185 case GST_EVENT_NEWSEGMENT:
186 if (GST_EVENT_DISCONT_NEW_MEDIA (event)) {
187 tests_unset (test);
188 tests_set (test);
189 }
190 break;
191 */
192 case GST_EVENT_EOS:{
193 gint i;
194
195 g_object_freeze_notify (G_OBJECT (test));
196 for (i = 0; i < TESTS_COUNT; i++) {
197 if (test->tests[i]) {
198 if (!tests[i].finish (test->tests[i], &test->values[i])) {
199 GValue v = { 0, };
200 gchar *real, *expected;
201
202 expected = gst_value_serialize (&test->values[i]);
203 g_value_init (&v, G_VALUE_TYPE (&test->values[i]));
204 g_object_get_property (G_OBJECT (test), klass->param_names[2 * i],
205 &v);
206 real = gst_value_serialize (&v);
207 g_value_unset (&v);
208 GST_ELEMENT_ERROR (test, STREAM, FORMAT, (NULL),
209 ("test %s returned value \"%s\" and not expected value \"%s\"",
210 klass->param_names[2 * i], real, expected));
211 g_free (real);
212 g_free (expected);
213 }
214 g_object_notify (G_OBJECT (test), klass->param_names[2 * i]);
215 }
216 }
217 g_object_thaw_notify (G_OBJECT (test));
218 break;
219 }
220 default:
221 break;
222 }
223
224 return GST_BASE_SINK_CLASS (parent_class)->event (basesink, event);
225 }
226
227 static GstFlowReturn
gst_test_render_buffer(GstBaseSink * basesink,GstBuffer * buf)228 gst_test_render_buffer (GstBaseSink * basesink, GstBuffer * buf)
229 {
230 GstTest *test = GST_TEST (basesink);
231 guint i;
232
233 for (i = 0; i < TESTS_COUNT; i++) {
234 if (test->tests[i]) {
235 tests[i].add (test->tests[i], buf);
236 }
237 }
238 return GST_FLOW_OK;
239 }
240
241 static gboolean
gst_test_start(GstBaseSink * sink)242 gst_test_start (GstBaseSink * sink)
243 {
244 GstTest *test = GST_TEST (sink);
245
246 tests_set (test);
247 return TRUE;
248 }
249
250 static gboolean
gst_test_stop(GstBaseSink * sink)251 gst_test_stop (GstBaseSink * sink)
252 {
253 GstTest *test = GST_TEST (sink);
254
255 tests_unset (test);
256 return TRUE;
257 }
258
259 static void
gst_test_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)260 gst_test_set_property (GObject * object, guint prop_id,
261 const GValue * value, GParamSpec * pspec)
262 {
263 GstTest *test = GST_TEST (object);
264
265 if (prop_id == 0 || prop_id > 2 * TESTS_COUNT) {
266 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
267 return;
268 }
269
270 if (prop_id % 2) {
271 /* real values can't be set */
272 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
273 } else {
274 /* expected values */
275 GST_OBJECT_LOCK (test);
276 g_value_copy (value, &test->values[prop_id / 2 - 1]);
277 GST_OBJECT_UNLOCK (test);
278 }
279 }
280
281 static void
gst_test_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)282 gst_test_get_property (GObject * object, guint prop_id, GValue * value,
283 GParamSpec * pspec)
284 {
285 GstTest *test = GST_TEST (object);
286 guint id = (prop_id - 1) / 2;
287
288 if (prop_id == 0 || prop_id > 2 * TESTS_COUNT) {
289 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
290 return;
291 }
292
293 GST_OBJECT_LOCK (test);
294
295 if (prop_id % 2) {
296 /* real values */
297 tests[id].get_value (test->tests[id], value);
298 } else {
299 /* expected values */
300 g_value_copy (&test->values[id], value);
301 }
302
303 GST_OBJECT_UNLOCK (test);
304 }
305