1 /* GStreamer unit tests for audiorate
3 * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
25 #include <gst/check/gstcheck.h>
27 /* helper element to insert additional buffers overlapping with previous ones */
28 static gdouble injector_inject_probability = 0.0;
30 typedef GstElement TestInjector;
31 typedef GstElementClass TestInjectorClass;
33 GType test_injector_get_type (void);
34 GST_BOILERPLATE (TestInjector, test_injector, GstElement, GST_TYPE_ELEMENT);
36 #define INJECTOR_CAPS \
37 "audio/x-raw-float, " \
38 "rate = (int) [ 1, MAX ], " \
39 "channels = (int) [ 1, 8 ], " \
40 "endianness = (int) BYTE_ORDER, " \
43 "rate = (int) [ 1, MAX ], " \
44 "channels = (int) [ 1, 8 ], " \
45 "endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }, " \
46 "width = (int) { 8, 16, 32 }, " \
47 "depth = (int) [ 1, 32 ], " \
48 "signed = (boolean) { true, false }"
50 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
53 GST_STATIC_CAPS (INJECTOR_CAPS));
55 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
58 GST_STATIC_CAPS (INJECTOR_CAPS));
61 test_injector_base_init (gpointer g_class)
63 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
65 gst_element_class_add_static_pad_template (element_class, &src_template);
66 gst_element_class_add_static_pad_template (element_class,
71 test_injector_class_init (TestInjectorClass * klass)
73 /* nothing to do here */
77 test_injector_chain (GstPad * pad, GstBuffer * buf)
83 gst_element_get_static_pad (GST_ELEMENT (GST_PAD_PARENT (pad)), "src");
85 /* since we're increasing timestamp/offsets, push this one first */
86 GST_LOG (" passing buffer [t=%" GST_TIME_FORMAT "-%" GST_TIME_FORMAT
87 "], offset=%" G_GINT64_FORMAT ", offset_end=%" G_GINT64_FORMAT,
88 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
89 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf) + GST_BUFFER_DURATION (buf)),
90 GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf));
94 ret = gst_pad_push (srcpad, buf);
96 if (g_random_double () < injector_inject_probability) {
99 ibuf = gst_buffer_copy (buf);
101 if (GST_BUFFER_OFFSET_IS_VALID (buf) &&
102 GST_BUFFER_OFFSET_END_IS_VALID (buf)) {
105 delta = GST_BUFFER_OFFSET_END (buf) - GST_BUFFER_OFFSET (buf);
106 GST_BUFFER_OFFSET (ibuf) += delta / 4;
107 GST_BUFFER_OFFSET_END (ibuf) += delta / 4;
109 GST_BUFFER_OFFSET (ibuf) = GST_BUFFER_OFFSET_NONE;
110 GST_BUFFER_OFFSET_END (ibuf) = GST_BUFFER_OFFSET_NONE;
113 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf) &&
114 GST_BUFFER_DURATION_IS_VALID (buf)) {
117 delta = GST_BUFFER_DURATION (buf);
118 GST_BUFFER_TIMESTAMP (ibuf) += delta / 4;
120 GST_BUFFER_TIMESTAMP (ibuf) = GST_CLOCK_TIME_NONE;
121 GST_BUFFER_DURATION (ibuf) = GST_CLOCK_TIME_NONE;
124 if (GST_BUFFER_TIMESTAMP_IS_VALID (ibuf) ||
125 GST_BUFFER_OFFSET_IS_VALID (ibuf)) {
126 GST_LOG ("injecting buffer [t=%" GST_TIME_FORMAT "-%" GST_TIME_FORMAT
127 "], offset=%" G_GINT64_FORMAT ", offset_end=%" G_GINT64_FORMAT,
128 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (ibuf)),
129 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (ibuf) +
130 GST_BUFFER_DURATION (ibuf)), GST_BUFFER_OFFSET (ibuf),
131 GST_BUFFER_OFFSET_END (ibuf));
133 if (gst_pad_push (srcpad, ibuf) != GST_FLOW_OK) {
134 /* ignore return value */
137 GST_WARNING ("couldn't inject buffer, no incoming timestamps or offsets");
138 gst_buffer_unref (ibuf);
142 gst_buffer_unref (buf);
148 test_injector_init (TestInjector * injector, TestInjectorClass * klass)
152 pad = gst_pad_new_from_static_template (&sink_template, "sink");
153 gst_pad_set_chain_function (pad, test_injector_chain);
154 gst_pad_set_getcaps_function (pad, gst_pad_proxy_getcaps);
155 gst_pad_set_setcaps_function (pad, gst_pad_proxy_setcaps);
156 gst_element_add_pad (GST_ELEMENT (injector), pad);
158 pad = gst_pad_new_from_static_template (&src_template, "src");
159 gst_pad_set_getcaps_function (pad, gst_pad_proxy_getcaps);
160 gst_pad_set_setcaps_function (pad, gst_pad_proxy_setcaps);
161 gst_element_add_pad (GST_ELEMENT (injector), pad);
165 probe_cb (GstPad * pad, GstBuffer * buf, gdouble * drop_probability)
167 if (g_random_double () < *drop_probability) {
168 GST_LOG ("dropping buffer [t=%" GST_TIME_FORMAT "-%" GST_TIME_FORMAT "], "
169 "offset=%" G_GINT64_FORMAT ", offset_end=%" G_GINT64_FORMAT,
170 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
171 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf) + GST_BUFFER_DURATION (buf)),
172 GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf));
173 return FALSE; /* drop buffer */
176 return TRUE; /* don't drop buffer */
180 got_buf (GstElement * fakesink, GstBuffer * buf, GstPad * pad, GList ** p_bufs)
182 *p_bufs = g_list_append (*p_bufs, gst_buffer_ref (buf));
186 do_perfect_stream_test (guint rate, guint width, gdouble drop_probability,
187 gdouble inject_probability)
189 GstElement *pipe, *src, *conv, *filter, *injector, *audiorate, *sink;
193 GList *l, *bufs = NULL;
194 GstClockTime next_time = GST_CLOCK_TIME_NONE;
195 guint64 next_offset = GST_BUFFER_OFFSET_NONE;
197 caps = gst_caps_new_simple ("audio/x-raw-int", "rate", G_TYPE_INT,
198 rate, "width", G_TYPE_INT, width, NULL);
200 GST_INFO ("-------- drop=%.0f%% caps = %" GST_PTR_FORMAT " ---------- ",
201 drop_probability * 100.0, caps);
203 g_assert (drop_probability >= 0.0 && drop_probability <= 1.0);
204 g_assert (inject_probability >= 0.0 && inject_probability <= 1.0);
205 g_assert (width > 0 && (width % 8) == 0);
207 pipe = gst_pipeline_new ("pipeline");
208 fail_unless (pipe != NULL);
210 src = gst_element_factory_make ("audiotestsrc", "audiotestsrc");
211 fail_unless (src != NULL);
213 g_object_set (src, "num-buffers", 100, NULL);
215 conv = gst_element_factory_make ("audioconvert", "audioconvert");
216 fail_unless (conv != NULL);
218 filter = gst_element_factory_make ("capsfilter", "capsfilter");
219 fail_unless (filter != NULL);
221 g_object_set (filter, "caps", caps, NULL);
223 injector_inject_probability = inject_probability;
225 injector = GST_ELEMENT (g_object_new (test_injector_get_type (), NULL));
227 srcpad = gst_element_get_static_pad (injector, "src");
228 fail_unless (srcpad != NULL);
229 gst_pad_add_buffer_probe (srcpad, G_CALLBACK (probe_cb), &drop_probability);
230 gst_object_unref (srcpad);
232 audiorate = gst_element_factory_make ("audiorate", "audiorate");
233 fail_unless (audiorate != NULL);
235 sink = gst_element_factory_make ("fakesink", "fakesink");
236 fail_unless (sink != NULL);
238 g_object_set (sink, "signal-handoffs", TRUE, NULL);
240 g_signal_connect (sink, "handoff", G_CALLBACK (got_buf), &bufs);
242 gst_bin_add_many (GST_BIN (pipe), src, conv, filter, injector, audiorate,
244 gst_element_link_many (src, conv, filter, injector, audiorate, sink, NULL);
246 fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_PLAYING),
247 GST_STATE_CHANGE_ASYNC);
249 fail_unless_equals_int (gst_element_get_state (pipe, NULL, NULL, -1),
250 GST_STATE_CHANGE_SUCCESS);
252 msg = gst_bus_poll (GST_ELEMENT_BUS (pipe),
253 GST_MESSAGE_EOS | GST_MESSAGE_ERROR, -1);
254 fail_unless_equals_string (GST_MESSAGE_TYPE_NAME (msg), "eos");
256 for (l = bufs; l != NULL; l = l->next) {
257 GstBuffer *buf = GST_BUFFER (l->data);
260 fail_unless (GST_BUFFER_TIMESTAMP_IS_VALID (buf));
261 fail_unless (GST_BUFFER_DURATION_IS_VALID (buf));
262 fail_unless (GST_BUFFER_OFFSET_IS_VALID (buf));
263 fail_unless (GST_BUFFER_OFFSET_END_IS_VALID (buf));
265 GST_LOG ("buffer: ts=%" GST_TIME_FORMAT ", end_ts=%" GST_TIME_FORMAT
266 " off=%" G_GINT64_FORMAT ", end_off=%" G_GINT64_FORMAT,
267 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
268 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf) + GST_BUFFER_DURATION (buf)),
269 GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf));
271 if (GST_CLOCK_TIME_IS_VALID (next_time)) {
272 fail_unless_equals_uint64 (next_time, GST_BUFFER_TIMESTAMP (buf));
274 if (next_offset != GST_BUFFER_OFFSET_NONE) {
275 fail_unless_equals_uint64 (next_offset, GST_BUFFER_OFFSET (buf));
278 /* check buffer size for sanity */
279 fail_unless_equals_int (GST_BUFFER_SIZE (buf) % (width / 8), 0);
281 /* check there is actually as much data as there should be */
282 num_samples = GST_BUFFER_OFFSET_END (buf) - GST_BUFFER_OFFSET (buf);
283 fail_unless_equals_int (GST_BUFFER_SIZE (buf), num_samples * (width / 8));
285 next_time = GST_BUFFER_TIMESTAMP (buf) + GST_BUFFER_DURATION (buf);
286 next_offset = GST_BUFFER_OFFSET_END (buf);
289 gst_message_unref (msg);
290 gst_element_set_state (pipe, GST_STATE_NULL);
291 gst_object_unref (pipe);
293 g_list_foreach (bufs, (GFunc) gst_mini_object_unref, NULL);
296 gst_caps_unref (caps);
299 static const guint rates[] = { 8000, 11025, 16000, 22050, 32000, 44100,
300 48000, 3333, 33333, 66666, 9999
303 GST_START_TEST (test_perfect_stream_drop0)
307 for (i = 0; i < G_N_ELEMENTS (rates); ++i) {
308 do_perfect_stream_test (rates[i], 8, 0.0, 0.0);
309 do_perfect_stream_test (rates[i], 16, 0.0, 0.0);
315 GST_START_TEST (test_perfect_stream_drop10)
319 for (i = 0; i < G_N_ELEMENTS (rates); ++i) {
320 do_perfect_stream_test (rates[i], 8, 0.10, 0.0);
321 do_perfect_stream_test (rates[i], 16, 0.10, 0.0);
327 GST_START_TEST (test_perfect_stream_drop50)
331 for (i = 0; i < G_N_ELEMENTS (rates); ++i) {
332 do_perfect_stream_test (rates[i], 8, 0.50, 0.0);
333 do_perfect_stream_test (rates[i], 16, 0.50, 0.0);
339 GST_START_TEST (test_perfect_stream_drop90)
343 for (i = 0; i < G_N_ELEMENTS (rates); ++i) {
344 do_perfect_stream_test (rates[i], 8, 0.90, 0.0);
345 do_perfect_stream_test (rates[i], 16, 0.90, 0.0);
351 GST_START_TEST (test_perfect_stream_inject10)
355 for (i = 0; i < G_N_ELEMENTS (rates); ++i) {
356 do_perfect_stream_test (rates[i], 8, 0.0, 0.10);
357 do_perfect_stream_test (rates[i], 16, 0.0, 0.10);
363 GST_START_TEST (test_perfect_stream_inject90)
367 for (i = 0; i < G_N_ELEMENTS (rates); ++i) {
368 do_perfect_stream_test (rates[i], 8, 0.0, 0.90);
369 do_perfect_stream_test (rates[i], 16, 0.0, 0.90);
375 GST_START_TEST (test_perfect_stream_drop45_inject25)
379 for (i = 0; i < G_N_ELEMENTS (rates); ++i) {
380 do_perfect_stream_test (rates[i], 8, 0.45, 0.25);
381 do_perfect_stream_test (rates[i], 16, 0.45, 0.25);
387 /* TODO: also do all tests with channels=1 and channels=2 */
389 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
392 GST_STATIC_CAPS ("audio/x-raw-float,channels=1,rate=44100,width=32")
395 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
398 GST_STATIC_CAPS ("audio/x-raw-float,channels=1,rate=44100,width=32")
401 GST_START_TEST (test_large_discont)
403 GstElement *audiorate;
405 GstPad *srcpad, *sinkpad;
408 audiorate = gst_check_setup_element ("audiorate");
409 caps = gst_caps_new_simple ("audio/x-raw-float",
410 "channels", G_TYPE_INT, 1,
411 "rate", G_TYPE_INT, 44100, "width", G_TYPE_INT, 32,
412 "endianness", G_TYPE_INT, G_BYTE_ORDER, NULL);
414 srcpad = gst_check_setup_src_pad (audiorate, &srctemplate, caps);
415 sinkpad = gst_check_setup_sink_pad (audiorate, &sinktemplate, caps);
417 gst_pad_set_active (srcpad, TRUE);
418 gst_pad_set_active (sinkpad, TRUE);
420 fail_unless (gst_element_set_state (audiorate,
421 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
422 "failed to set audiorate playing");
424 buf = gst_buffer_new_and_alloc (4);
425 gst_buffer_set_caps (buf, caps);
426 GST_BUFFER_TIMESTAMP (buf) = 0;
427 gst_pad_push (srcpad, buf);
429 fail_unless_equals_int (g_list_length (buffers), 1);
431 buf = gst_buffer_new_and_alloc (4);
432 gst_buffer_set_caps (buf, caps);
433 GST_BUFFER_TIMESTAMP (buf) = 2 * GST_SECOND;
434 gst_pad_push (srcpad, buf);
435 /* Now we should have 3 more buffers: the one we injected, plus _two_ filler
436 * buffers, because the gap is > 1 second (but less than 2 seconds) */
437 fail_unless_equals_int (g_list_length (buffers), 4);
439 gst_element_set_state (audiorate, GST_STATE_NULL);
440 gst_caps_unref (caps);
442 gst_check_teardown_sink_pad (audiorate);
443 gst_check_teardown_src_pad (audiorate);
445 gst_object_unref (audiorate);
451 audiorate_suite (void)
453 Suite *s = suite_create ("audiorate");
454 TCase *tc_chain = tcase_create ("general");
456 suite_add_tcase (s, tc_chain);
458 tcase_add_test (tc_chain, test_perfect_stream_drop0);
459 tcase_add_test (tc_chain, test_perfect_stream_drop10);
460 tcase_add_test (tc_chain, test_perfect_stream_drop50);
461 tcase_add_test (tc_chain, test_perfect_stream_drop90);
462 tcase_add_test (tc_chain, test_perfect_stream_inject10);
463 tcase_add_test (tc_chain, test_perfect_stream_inject90);
464 tcase_add_test (tc_chain, test_perfect_stream_drop45_inject25);
465 tcase_add_test (tc_chain, test_large_discont);
470 GST_CHECK_MAIN (audiorate);