Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 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> |
Tim-Philipp Müller | 2fa12d1 | 2005-10-05 11:38:29 +0000 | [diff] [blame] | 25 | #include <gst/base/gstbasesink.h> |
Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 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)) |
Stefan Kost | eb1b723 | 2006-09-16 21:57:29 +0000 | [diff] [blame] | 44 | #define GST_IS_TEST_CLASS(klass) \ |
Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 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 | { |
Tim-Philipp Müller | 2fa12d1 | 2005-10-05 11:38:29 +0000 | [diff] [blame] | 52 | GstBaseSink basesink; |
Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 53 | |
| 54 | gpointer tests[TESTS_COUNT]; |
| 55 | GValue values[TESTS_COUNT]; |
| 56 | }; |
| 57 | |
| 58 | struct _GstTestClass |
| 59 | { |
Tim-Philipp Müller | 2fa12d1 | 2005-10-05 11:38:29 +0000 | [diff] [blame] | 60 | GstBaseSinkClass parent_class; |
Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 61 | |
| 62 | gchar *param_names[2 * TESTS_COUNT]; |
| 63 | }; |
| 64 | |
Jan Schmidt | de1357a | 2007-03-04 13:52:03 +0000 | [diff] [blame] | 65 | static void gst_test_finalize (GstTest * test); |
| 66 | |
Tim-Philipp Müller | 2fa12d1 | 2005-10-05 11:38:29 +0000 | [diff] [blame] | 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); |
Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 72 | |
Tim-Philipp Müller | 2fa12d1 | 2005-10-05 11:38:29 +0000 | [diff] [blame] | 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); |
Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 77 | |
Tim-Philipp Müller | 2fa12d1 | 2005-10-05 11:38:29 +0000 | [diff] [blame] | 78 | static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink", |
| 79 | GST_PAD_SINK, |
| 80 | GST_PAD_ALWAYS, |
| 81 | GST_STATIC_CAPS_ANY); |
| 82 | |
Stefan Kost | a3a8b70 | 2008-06-11 14:11:16 +0000 | [diff] [blame] | 83 | #define DEBUG_INIT(bla) \ |
| 84 | GST_DEBUG_CATEGORY_INIT (gst_test_debug, "testsink", 0, \ |
| 85 | "debugging category for testsink element"); |
| 86 | |
Benjamin Otte | 3342b16 | 2010-03-17 18:23:00 +0100 | [diff] [blame] | 87 | GType gst_test_get_type (void); |
Stefan Kost | a3a8b70 | 2008-06-11 14:11:16 +0000 | [diff] [blame] | 88 | GST_BOILERPLATE_FULL (GstTest, gst_test, GstBaseSink, GST_TYPE_BASE_SINK, |
| 89 | DEBUG_INIT); |
| 90 | |
Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 91 | |
Maciej Katafiasz | be7d42b | 2006-05-06 00:14:09 +0000 | [diff] [blame] | 92 | static void |
| 93 | gst_test_base_init (gpointer g_class) |
Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 94 | { |
Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 95 | GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class); |
| 96 | |
Tim-Philipp Müller | 2fa12d1 | 2005-10-05 11:38:29 +0000 | [diff] [blame] | 97 | gst_element_class_add_pad_template (gstelement_class, |
| 98 | gst_static_pad_template_get (&sinktemplate)); |
| 99 | |
Benjamin Otte | cccfeaa | 2010-03-18 14:31:35 +0100 | [diff] [blame] | 100 | gst_element_class_set_details_simple (gstelement_class, "Test plugin", |
| 101 | "Testing", "perform a number of tests", "Benjamin Otte <otte@gnome>"); |
Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static void |
| 105 | gst_test_class_init (GstTestClass * klass) |
| 106 | { |
Tim-Philipp Müller | 2fa12d1 | 2005-10-05 11:38:29 +0000 | [diff] [blame] | 107 | GstBaseSinkClass *basesink_class = GST_BASE_SINK_CLASS (klass); |
| 108 | GObjectClass *object_class = G_OBJECT_CLASS (klass); |
Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 109 | guint i; |
| 110 | |
Sebastian Dröge | f16ed4a | 2010-06-06 17:52:40 +0200 | [diff] [blame] | 111 | object_class->set_property = gst_test_set_property; |
| 112 | object_class->get_property = gst_test_get_property; |
Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 113 | |
Jan Schmidt | de1357a | 2007-03-04 13:52:03 +0000 | [diff] [blame] | 114 | object_class->finalize = (GObjectFinalizeFunc) gst_test_finalize; |
| 115 | |
Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 116 | for (i = 0; i < TESTS_COUNT; i++) { |
| 117 | GParamSpec *spec; |
| 118 | |
| 119 | spec = tests[i].get_spec (&tests[i], FALSE); |
| 120 | klass->param_names[2 * i] = g_strdup (g_param_spec_get_name (spec)); |
Tim-Philipp Müller | 2fa12d1 | 2005-10-05 11:38:29 +0000 | [diff] [blame] | 121 | g_object_class_install_property (object_class, 2 * i + 1, spec); |
Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 122 | spec = tests[i].get_spec (&tests[i], TRUE); |
| 123 | klass->param_names[2 * i + 1] = g_strdup (g_param_spec_get_name (spec)); |
Tim-Philipp Müller | 2fa12d1 | 2005-10-05 11:38:29 +0000 | [diff] [blame] | 124 | g_object_class_install_property (object_class, 2 * i + 2, spec); |
Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 125 | } |
Tim-Philipp Müller | 2fa12d1 | 2005-10-05 11:38:29 +0000 | [diff] [blame] | 126 | |
Tim-Philipp Müller | 2fa12d1 | 2005-10-05 11:38:29 +0000 | [diff] [blame] | 127 | basesink_class->render = GST_DEBUG_FUNCPTR (gst_test_render_buffer); |
| 128 | basesink_class->event = GST_DEBUG_FUNCPTR (gst_test_sink_event); |
| 129 | basesink_class->start = GST_DEBUG_FUNCPTR (gst_test_start); |
| 130 | basesink_class->stop = GST_DEBUG_FUNCPTR (gst_test_stop); |
Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | static void |
Andy Wingo | 99fc329 | 2005-08-28 17:59:20 +0000 | [diff] [blame] | 134 | gst_test_init (GstTest * test, GstTestClass * g_class) |
Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 135 | { |
| 136 | GstTestClass *klass; |
| 137 | guint i; |
| 138 | |
Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 139 | klass = GST_TEST_GET_CLASS (test); |
| 140 | for (i = 0; i < TESTS_COUNT; i++) { |
| 141 | GParamSpec *spec = g_object_class_find_property (G_OBJECT_CLASS (klass), |
| 142 | klass->param_names[2 * i + 1]); |
| 143 | |
| 144 | g_value_init (&test->values[i], G_PARAM_SPEC_VALUE_TYPE (spec)); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | static void |
Jan Schmidt | de1357a | 2007-03-04 13:52:03 +0000 | [diff] [blame] | 149 | gst_test_finalize (GstTest * test) |
| 150 | { |
| 151 | guint i; |
| 152 | |
| 153 | for (i = 0; i < TESTS_COUNT; i++) { |
| 154 | g_value_unset (&test->values[i]); |
| 155 | } |
| 156 | |
| 157 | G_OBJECT_CLASS (parent_class)->finalize ((GObject *) test); |
| 158 | } |
| 159 | |
| 160 | static void |
Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 161 | tests_unset (GstTest * test) |
| 162 | { |
| 163 | guint i; |
| 164 | |
| 165 | for (i = 0; i < TESTS_COUNT; i++) { |
| 166 | if (test->tests[i]) { |
| 167 | tests[i].free (test->tests[i]); |
| 168 | test->tests[i] = NULL; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | static void |
| 174 | tests_set (GstTest * test) |
| 175 | { |
| 176 | guint i; |
| 177 | |
| 178 | for (i = 0; i < TESTS_COUNT; i++) { |
| 179 | g_assert (test->tests[i] == NULL); |
| 180 | test->tests[i] = tests[i].new (&tests[i]); |
| 181 | } |
| 182 | } |
| 183 | |
Tim-Philipp Müller | 2fa12d1 | 2005-10-05 11:38:29 +0000 | [diff] [blame] | 184 | static gboolean |
| 185 | gst_test_sink_event (GstBaseSink * basesink, GstEvent * event) |
Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 186 | { |
Tim-Philipp Müller | 2fa12d1 | 2005-10-05 11:38:29 +0000 | [diff] [blame] | 187 | GstTestClass *klass = GST_TEST_GET_CLASS (basesink); |
| 188 | GstTest *test = GST_TEST (basesink); |
| 189 | gboolean ret = FALSE; |
Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 190 | |
Tim-Philipp Müller | 2fa12d1 | 2005-10-05 11:38:29 +0000 | [diff] [blame] | 191 | switch (GST_EVENT_TYPE (event)) { |
| 192 | /* |
| 193 | case GST_EVENT_NEWSEGMENT: |
| 194 | if (GST_EVENT_DISCONT_NEW_MEDIA (event)) { |
| 195 | tests_unset (test); |
| 196 | tests_set (test); |
| 197 | } |
| 198 | break; |
| 199 | */ |
| 200 | case GST_EVENT_EOS:{ |
| 201 | gint i; |
Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 202 | |
Tim-Philipp Müller | 2fa12d1 | 2005-10-05 11:38:29 +0000 | [diff] [blame] | 203 | g_object_freeze_notify (G_OBJECT (test)); |
| 204 | for (i = 0; i < TESTS_COUNT; i++) { |
| 205 | if (test->tests[i]) { |
| 206 | if (!tests[i].finish (test->tests[i], &test->values[i])) { |
| 207 | GValue v = { 0, }; |
| 208 | gchar *real, *expected; |
Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 209 | |
Tim-Philipp Müller | 2fa12d1 | 2005-10-05 11:38:29 +0000 | [diff] [blame] | 210 | expected = gst_value_serialize (&test->values[i]); |
| 211 | g_value_init (&v, G_VALUE_TYPE (&test->values[i])); |
| 212 | g_object_get_property (G_OBJECT (test), klass->param_names[2 * i], |
| 213 | &v); |
| 214 | real = gst_value_serialize (&v); |
| 215 | g_value_unset (&v); |
| 216 | GST_ELEMENT_ERROR (test, STREAM, FORMAT, (NULL), |
| 217 | ("test %s returned value \"%s\" and not expected value \"%s\"", |
| 218 | klass->param_names[2 * i], real, expected)); |
| 219 | g_free (real); |
| 220 | g_free (expected); |
Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 221 | } |
Tim-Philipp Müller | 2fa12d1 | 2005-10-05 11:38:29 +0000 | [diff] [blame] | 222 | g_object_notify (G_OBJECT (test), klass->param_names[2 * i]); |
Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 223 | } |
Tim-Philipp Müller | 2fa12d1 | 2005-10-05 11:38:29 +0000 | [diff] [blame] | 224 | } |
| 225 | g_object_thaw_notify (G_OBJECT (test)); |
| 226 | ret = TRUE; |
| 227 | break; |
Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 228 | } |
Tim-Philipp Müller | 2fa12d1 | 2005-10-05 11:38:29 +0000 | [diff] [blame] | 229 | default: |
| 230 | break; |
Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Tim-Philipp Müller | 2fa12d1 | 2005-10-05 11:38:29 +0000 | [diff] [blame] | 233 | return ret; |
| 234 | } |
| 235 | |
| 236 | static GstFlowReturn |
| 237 | gst_test_render_buffer (GstBaseSink * basesink, GstBuffer * buf) |
| 238 | { |
| 239 | GstTest *test = GST_TEST (basesink); |
| 240 | guint i; |
| 241 | |
Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 242 | for (i = 0; i < TESTS_COUNT; i++) { |
| 243 | if (test->tests[i]) { |
Tim-Philipp Müller | 2fa12d1 | 2005-10-05 11:38:29 +0000 | [diff] [blame] | 244 | tests[i].add (test->tests[i], buf); |
Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 245 | } |
| 246 | } |
Tim-Philipp Müller | 2fa12d1 | 2005-10-05 11:38:29 +0000 | [diff] [blame] | 247 | return GST_FLOW_OK; |
| 248 | } |
| 249 | |
| 250 | static gboolean |
| 251 | gst_test_start (GstBaseSink * sink) |
| 252 | { |
| 253 | GstTest *test = GST_TEST (sink); |
| 254 | |
| 255 | tests_set (test); |
| 256 | return TRUE; |
| 257 | } |
| 258 | |
| 259 | static gboolean |
| 260 | gst_test_stop (GstBaseSink * sink) |
| 261 | { |
| 262 | GstTest *test = GST_TEST (sink); |
| 263 | |
| 264 | tests_unset (test); |
| 265 | return TRUE; |
Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | static void |
| 269 | gst_test_set_property (GObject * object, guint prop_id, |
| 270 | const GValue * value, GParamSpec * pspec) |
| 271 | { |
| 272 | GstTest *test = GST_TEST (object); |
| 273 | |
| 274 | if (prop_id == 0 || prop_id > 2 * TESTS_COUNT) { |
| 275 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | if (prop_id % 2) { |
| 280 | /* real values can't be set */ |
| 281 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 282 | } else { |
| 283 | /* expected values */ |
Andy Wingo | 0d470f5 | 2005-11-21 16:36:05 +0000 | [diff] [blame] | 284 | GST_OBJECT_LOCK (test); |
Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 285 | g_value_copy (value, &test->values[prop_id / 2 - 1]); |
Andy Wingo | 0d470f5 | 2005-11-21 16:36:05 +0000 | [diff] [blame] | 286 | GST_OBJECT_UNLOCK (test); |
Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
| 290 | static void |
| 291 | gst_test_get_property (GObject * object, guint prop_id, GValue * value, |
| 292 | GParamSpec * pspec) |
| 293 | { |
| 294 | GstTest *test = GST_TEST (object); |
| 295 | guint id = (prop_id - 1) / 2; |
| 296 | |
| 297 | if (prop_id == 0 || prop_id > 2 * TESTS_COUNT) { |
| 298 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 299 | return; |
| 300 | } |
| 301 | |
Andy Wingo | 0d470f5 | 2005-11-21 16:36:05 +0000 | [diff] [blame] | 302 | GST_OBJECT_LOCK (test); |
Tim-Philipp Müller | 2fa12d1 | 2005-10-05 11:38:29 +0000 | [diff] [blame] | 303 | |
Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 304 | if (prop_id % 2) { |
| 305 | /* real values */ |
| 306 | tests[id].get_value (test->tests[id], value); |
| 307 | } else { |
| 308 | /* expected values */ |
| 309 | g_value_copy (&test->values[id], value); |
| 310 | } |
Tim-Philipp Müller | 2fa12d1 | 2005-10-05 11:38:29 +0000 | [diff] [blame] | 311 | |
Andy Wingo | 0d470f5 | 2005-11-21 16:36:05 +0000 | [diff] [blame] | 312 | GST_OBJECT_UNLOCK (test); |
Benjamin Otte | c7a62ce | 2004-05-19 14:15:41 +0000 | [diff] [blame] | 313 | } |