3 * unit test for audioresample, based on the audioresample unit test
5 * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
6 * Copyright (C) <2006> Tim-Philipp Müller <tim at centricular net>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
26 #include <gst/check/gstcheck.h>
28 #include <gst/audio/audio.h>
30 #include <gst/fft/gstfft.h>
31 #include <gst/fft/gstffts16.h>
32 #include <gst/fft/gstffts32.h>
33 #include <gst/fft/gstfftf32.h>
34 #include <gst/fft/gstfftf64.h>
36 /* For ease of programming we use globals to keep refs for our floating
37 * src and sink pads we create; otherwise we always have to do get_pad,
38 * get_peer, and then remove references in every test function */
39 static GstPad *mysrcpad, *mysinkpad;
41 #define RESAMPLE_CAPS_FLOAT \
42 "audio/x-raw-float, " \
43 "channels = (int) [ 1, MAX ], " \
44 "rate = (int) [ 1, MAX ], " \
45 "endianness = (int) BYTE_ORDER, " \
46 "width = (int) { 32, 64 }"
48 #define RESAMPLE_CAPS_INT \
50 "channels = (int) [ 1, MAX ], " \
51 "rate = (int) [ 1, MAX ], " \
52 "endianness = (int) BYTE_ORDER, " \
53 "width = (int) { 16, 32 }, " \
54 "depth = (int) { 16, 32 }, " \
55 "signed = (bool) TRUE"
57 #define RESAMPLE_CAPS_TEMPLATE_STRING \
58 RESAMPLE_CAPS_FLOAT " ; " \
61 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
64 GST_STATIC_CAPS (RESAMPLE_CAPS_TEMPLATE_STRING)
66 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
69 GST_STATIC_CAPS (RESAMPLE_CAPS_TEMPLATE_STRING)
73 setup_audioresample (int channels, int inrate, int outrate, int width,
76 GstElement *audioresample;
78 GstStructure *structure;
80 GST_DEBUG ("setup_audioresample");
81 audioresample = gst_check_setup_element ("audioresample");
84 caps = gst_caps_from_string (RESAMPLE_CAPS_FLOAT);
86 caps = gst_caps_from_string (RESAMPLE_CAPS_INT);
87 structure = gst_caps_get_structure (caps, 0);
88 gst_structure_set (structure, "channels", G_TYPE_INT, channels,
89 "rate", G_TYPE_INT, inrate, "width", G_TYPE_INT, width, NULL);
91 gst_structure_set (structure, "depth", G_TYPE_INT, width, NULL);
92 fail_unless (gst_caps_is_fixed (caps));
94 fail_unless (gst_element_set_state (audioresample,
95 GST_STATE_PAUSED) == GST_STATE_CHANGE_SUCCESS,
96 "could not set to paused");
98 mysrcpad = gst_check_setup_src_pad (audioresample, &srctemplate, caps);
99 gst_pad_set_caps (mysrcpad, caps);
100 gst_caps_unref (caps);
103 caps = gst_caps_from_string (RESAMPLE_CAPS_FLOAT);
105 caps = gst_caps_from_string (RESAMPLE_CAPS_INT);
106 structure = gst_caps_get_structure (caps, 0);
107 gst_structure_set (structure, "channels", G_TYPE_INT, channels,
108 "rate", G_TYPE_INT, outrate, "width", G_TYPE_INT, width, NULL);
110 gst_structure_set (structure, "depth", G_TYPE_INT, width, NULL);
111 fail_unless (gst_caps_is_fixed (caps));
113 mysinkpad = gst_check_setup_sink_pad (audioresample, &sinktemplate, caps);
114 /* this installs a getcaps func that will always return the caps we set
116 gst_pad_set_caps (mysinkpad, caps);
117 gst_pad_use_fixed_caps (mysinkpad);
119 gst_pad_set_active (mysinkpad, TRUE);
120 gst_pad_set_active (mysrcpad, TRUE);
122 gst_caps_unref (caps);
124 return audioresample;
128 cleanup_audioresample (GstElement * audioresample)
130 GST_DEBUG ("cleanup_audioresample");
132 fail_unless (gst_element_set_state (audioresample,
133 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to NULL");
135 gst_pad_set_active (mysrcpad, FALSE);
136 gst_pad_set_active (mysinkpad, FALSE);
137 gst_check_teardown_src_pad (audioresample);
138 gst_check_teardown_sink_pad (audioresample);
139 gst_check_teardown_element (audioresample);
140 gst_check_drop_buffers ();
144 fail_unless_perfect_stream (void)
146 guint64 timestamp = 0L, duration = 0L;
147 guint64 offset = 0L, offset_end = 0L;
152 for (l = buffers; l; l = l->next) {
153 buffer = GST_BUFFER (l->data);
154 ASSERT_BUFFER_REFCOUNT (buffer, "buffer", 1);
155 GST_DEBUG ("buffer timestamp %" G_GUINT64_FORMAT ", duration %"
156 G_GUINT64_FORMAT " offset %" G_GUINT64_FORMAT " offset_end %"
158 GST_BUFFER_TIMESTAMP (buffer),
159 GST_BUFFER_DURATION (buffer),
160 GST_BUFFER_OFFSET (buffer), GST_BUFFER_OFFSET_END (buffer));
162 fail_unless_equals_uint64 (timestamp, GST_BUFFER_TIMESTAMP (buffer));
163 fail_unless_equals_uint64 (offset, GST_BUFFER_OFFSET (buffer));
164 duration = GST_BUFFER_DURATION (buffer);
165 offset_end = GST_BUFFER_OFFSET_END (buffer);
167 timestamp += duration;
169 gst_buffer_unref (buffer);
171 g_list_free (buffers);
175 /* this tests that the output is a perfect stream if the input is */
177 test_perfect_stream_instance (int inrate, int outrate, int samples,
180 GstElement *audioresample;
181 GstBuffer *inbuffer, *outbuffer;
188 audioresample = setup_audioresample (2, inrate, outrate, 16, FALSE);
189 caps = gst_pad_get_negotiated_caps (mysrcpad);
190 fail_unless (gst_caps_is_fixed (caps));
192 fail_unless (gst_element_set_state (audioresample,
193 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
194 "could not set to playing");
196 for (j = 1; j <= numbuffers; ++j) {
198 inbuffer = gst_buffer_new_and_alloc (samples * 4);
199 GST_BUFFER_DURATION (inbuffer) = GST_FRAMES_TO_CLOCK_TIME (samples, inrate);
200 GST_BUFFER_TIMESTAMP (inbuffer) = GST_BUFFER_DURATION (inbuffer) * (j - 1);
201 GST_BUFFER_OFFSET (inbuffer) = offset;
203 GST_BUFFER_OFFSET_END (inbuffer) = offset;
205 gst_buffer_set_caps (inbuffer, caps);
207 p = (gint16 *) GST_BUFFER_DATA (inbuffer);
209 /* create a 16 bit signed ramp */
210 for (i = 0; i < samples; ++i) {
211 *p = -32767 + i * (65535 / samples);
213 *p = -32767 + i * (65535 / samples);
217 /* pushing gives away my reference ... */
218 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
219 /* ... but it ends up being collected on the global buffer list */
220 fail_unless_equals_int (g_list_length (buffers), j);
223 /* FIXME: we should make audioresample handle eos by flushing out the last
224 * samples, which will give us one more, small, buffer */
225 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
226 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
228 fail_unless_perfect_stream ();
231 gst_caps_unref (caps);
232 cleanup_audioresample (audioresample);
236 /* make sure that outgoing buffers are contiguous in timestamp/duration and
239 GST_START_TEST (test_perfect_stream)
241 /* integral scalings */
242 test_perfect_stream_instance (48000, 24000, 500, 20);
243 test_perfect_stream_instance (48000, 12000, 500, 20);
244 test_perfect_stream_instance (12000, 24000, 500, 20);
245 test_perfect_stream_instance (12000, 48000, 500, 20);
247 /* non-integral scalings */
248 test_perfect_stream_instance (44100, 8000, 500, 20);
249 test_perfect_stream_instance (8000, 44100, 500, 20);
252 test_perfect_stream_instance (12345, 54321, 500, 20);
253 test_perfect_stream_instance (101, 99, 500, 20);
258 /* this tests that the output is a correct discontinuous stream
259 * if the input is; ie input drops in time come out the same way */
261 test_discont_stream_instance (int inrate, int outrate, int samples,
264 GstElement *audioresample;
265 GstBuffer *inbuffer, *outbuffer;
272 GST_DEBUG ("inrate:%d outrate:%d samples:%d numbuffers:%d",
273 inrate, outrate, samples, numbuffers);
275 audioresample = setup_audioresample (2, inrate, outrate, 16, FALSE);
276 caps = gst_pad_get_negotiated_caps (mysrcpad);
277 fail_unless (gst_caps_is_fixed (caps));
279 fail_unless (gst_element_set_state (audioresample,
280 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
281 "could not set to playing");
283 for (j = 1; j <= numbuffers; ++j) {
285 inbuffer = gst_buffer_new_and_alloc (samples * 4);
286 GST_BUFFER_DURATION (inbuffer) = samples * GST_SECOND / inrate;
287 /* "drop" half the buffers */
288 ints = GST_BUFFER_DURATION (inbuffer) * 2 * (j - 1);
289 GST_BUFFER_TIMESTAMP (inbuffer) = ints;
290 GST_BUFFER_OFFSET (inbuffer) = (j - 1) * 2 * samples;
291 GST_BUFFER_OFFSET_END (inbuffer) = j * 2 * samples + samples;
293 gst_buffer_set_caps (inbuffer, caps);
295 p = (gint16 *) GST_BUFFER_DATA (inbuffer);
297 /* create a 16 bit signed ramp */
298 for (i = 0; i < samples; ++i) {
299 *p = -32767 + i * (65535 / samples);
301 *p = -32767 + i * (65535 / samples);
305 GST_DEBUG ("Sending Buffer time:%" G_GUINT64_FORMAT " duration:%"
306 G_GINT64_FORMAT " discont:%d offset:%" G_GUINT64_FORMAT " offset_end:%"
307 G_GUINT64_FORMAT, GST_BUFFER_TIMESTAMP (inbuffer),
308 GST_BUFFER_DURATION (inbuffer), GST_BUFFER_IS_DISCONT (inbuffer),
309 GST_BUFFER_OFFSET (inbuffer), GST_BUFFER_OFFSET_END (inbuffer));
310 /* pushing gives away my reference ... */
311 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
313 /* check if the timestamp of the pushed buffer matches the incoming one */
314 outbuffer = g_list_nth_data (buffers, g_list_length (buffers) - 1);
315 fail_if (outbuffer == NULL);
316 fail_unless_equals_uint64 (ints, GST_BUFFER_TIMESTAMP (outbuffer));
317 GST_DEBUG ("Got Buffer time:%" G_GUINT64_FORMAT " duration:%"
318 G_GINT64_FORMAT " discont:%d offset:%" G_GUINT64_FORMAT " offset_end:%"
319 G_GUINT64_FORMAT, GST_BUFFER_TIMESTAMP (outbuffer),
320 GST_BUFFER_DURATION (outbuffer), GST_BUFFER_IS_DISCONT (outbuffer),
321 GST_BUFFER_OFFSET (outbuffer), GST_BUFFER_OFFSET_END (outbuffer));
323 fail_unless (GST_BUFFER_IS_DISCONT (outbuffer),
324 "expected discont for buffer #%d", j);
329 gst_caps_unref (caps);
330 cleanup_audioresample (audioresample);
333 GST_START_TEST (test_discont_stream)
335 /* integral scalings */
336 test_discont_stream_instance (48000, 24000, 5000, 20);
337 test_discont_stream_instance (48000, 12000, 5000, 20);
338 test_discont_stream_instance (12000, 24000, 5000, 20);
339 test_discont_stream_instance (12000, 48000, 5000, 20);
341 /* non-integral scalings */
342 test_discont_stream_instance (44100, 8000, 5000, 20);
343 test_discont_stream_instance (8000, 44100, 5000, 20);
346 test_discont_stream_instance (12345, 54321, 5000, 20);
347 test_discont_stream_instance (101, 99, 5000, 20);
354 GST_START_TEST (test_reuse)
356 GstElement *audioresample;
361 audioresample = setup_audioresample (1, 9343, 48000, 16, FALSE);
362 caps = gst_pad_get_negotiated_caps (mysrcpad);
363 fail_unless (gst_caps_is_fixed (caps));
365 fail_unless (gst_element_set_state (audioresample,
366 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
367 "could not set to playing");
369 newseg = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME, 0, -1, 0);
370 fail_unless (gst_pad_push_event (mysrcpad, newseg) != FALSE);
372 inbuffer = gst_buffer_new_and_alloc (9343 * 4);
373 memset (GST_BUFFER_DATA (inbuffer), 0, GST_BUFFER_SIZE (inbuffer));
374 GST_BUFFER_DURATION (inbuffer) = GST_SECOND;
375 GST_BUFFER_TIMESTAMP (inbuffer) = 0;
376 GST_BUFFER_OFFSET (inbuffer) = 0;
377 gst_buffer_set_caps (inbuffer, caps);
379 /* pushing gives away my reference ... */
380 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
382 /* ... but it ends up being collected on the global buffer list */
383 fail_unless_equals_int (g_list_length (buffers), 1);
385 /* now reset and try again ... */
386 fail_unless (gst_element_set_state (audioresample,
387 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to NULL");
389 fail_unless (gst_element_set_state (audioresample,
390 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
391 "could not set to playing");
393 newseg = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME, 0, -1, 0);
394 fail_unless (gst_pad_push_event (mysrcpad, newseg) != FALSE);
396 inbuffer = gst_buffer_new_and_alloc (9343 * 4);
397 memset (GST_BUFFER_DATA (inbuffer), 0, GST_BUFFER_SIZE (inbuffer));
398 GST_BUFFER_DURATION (inbuffer) = GST_SECOND;
399 GST_BUFFER_TIMESTAMP (inbuffer) = 0;
400 GST_BUFFER_OFFSET (inbuffer) = 0;
401 gst_buffer_set_caps (inbuffer, caps);
403 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
405 /* ... it also ends up being collected on the global buffer list. If we
406 * now have more than 2 buffers, then audioresample probably didn't clean
407 * up its internal buffer properly and tried to push the remaining samples
408 * when it got the second NEWSEGMENT event */
409 fail_unless_equals_int (g_list_length (buffers), 2);
411 cleanup_audioresample (audioresample);
412 gst_caps_unref (caps);
417 GST_START_TEST (test_shutdown)
419 GstElement *pipeline, *src, *cf1, *ar, *cf2, *sink;
423 /* create pipeline, force audioresample to actually resample */
424 pipeline = gst_pipeline_new (NULL);
426 src = gst_check_setup_element ("audiotestsrc");
427 cf1 = gst_check_setup_element ("capsfilter");
428 ar = gst_check_setup_element ("audioresample");
429 cf2 = gst_check_setup_element ("capsfilter");
430 g_object_set (cf2, "name", "capsfilter2", NULL);
431 sink = gst_check_setup_element ("fakesink");
434 gst_caps_new_simple ("audio/x-raw-int", "rate", G_TYPE_INT, 11025, NULL);
435 g_object_set (cf1, "caps", caps, NULL);
436 gst_caps_unref (caps);
439 gst_caps_new_simple ("audio/x-raw-int", "rate", G_TYPE_INT, 48000, NULL);
440 g_object_set (cf2, "caps", caps, NULL);
441 gst_caps_unref (caps);
443 /* don't want to sync against the clock, the more throughput the better */
444 g_object_set (src, "is-live", FALSE, NULL);
445 g_object_set (sink, "sync", FALSE, NULL);
447 gst_bin_add_many (GST_BIN (pipeline), src, cf1, ar, cf2, sink, NULL);
448 fail_if (!gst_element_link_many (src, cf1, ar, cf2, sink, NULL));
450 /* now, wait until pipeline is running and then shut it down again; repeat */
451 for (i = 0; i < 20; ++i) {
452 gst_element_set_state (pipeline, GST_STATE_PAUSED);
453 gst_element_get_state (pipeline, NULL, NULL, -1);
454 gst_element_set_state (pipeline, GST_STATE_PLAYING);
456 gst_element_set_state (pipeline, GST_STATE_NULL);
459 gst_object_unref (pipeline);
465 live_switch_alloc_only_48000 (GstPad * pad, guint64 offset,
466 guint size, GstCaps * caps, GstBuffer ** buf)
468 GstStructure *structure;
473 structure = gst_caps_get_structure (caps, 0);
474 fail_unless (gst_structure_get_int (structure, "rate", &rate));
475 fail_unless (gst_structure_get_int (structure, "channels", &channels));
478 return GST_FLOW_NOT_NEGOTIATED;
480 desired = gst_caps_copy (caps);
481 gst_caps_set_simple (desired, "rate", G_TYPE_INT, 48000, NULL);
483 *buf = gst_buffer_new_and_alloc (channels * 48000);
484 gst_buffer_set_caps (*buf, desired);
485 gst_caps_unref (desired);
491 live_switch_get_sink_caps (GstPad * pad)
495 result = gst_caps_copy (GST_PAD_CAPS (pad));
497 gst_caps_set_simple (result,
498 "rate", GST_TYPE_INT_RANGE, 48000, G_MAXINT, NULL);
504 live_switch_push (int rate, GstCaps * caps)
510 desired = gst_caps_copy (caps);
511 gst_caps_set_simple (desired, "rate", G_TYPE_INT, rate, NULL);
512 gst_pad_set_caps (mysrcpad, desired);
514 fail_unless (gst_pad_alloc_buffer_and_set_caps (mysrcpad,
515 GST_BUFFER_OFFSET_NONE, rate * 4, desired, &inbuffer) == GST_FLOW_OK);
517 /* When the basetransform hits the non-configured case it always
518 * returns a buffer with exactly the same caps as we requested so the actual
519 * renegotiation (if needed) will be done in the _chain*/
520 fail_unless (inbuffer != NULL);
521 GST_DEBUG ("desired: %" GST_PTR_FORMAT ".... got: %" GST_PTR_FORMAT,
522 desired, GST_BUFFER_CAPS (inbuffer));
523 fail_unless (gst_caps_is_equal (desired, GST_BUFFER_CAPS (inbuffer)));
525 memset (GST_BUFFER_DATA (inbuffer), 0, GST_BUFFER_SIZE (inbuffer));
526 GST_BUFFER_DURATION (inbuffer) = GST_SECOND;
527 GST_BUFFER_TIMESTAMP (inbuffer) = 0;
528 GST_BUFFER_OFFSET (inbuffer) = 0;
530 /* pushing gives away my reference ... */
531 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
533 /* ... but it ends up being collected on the global buffer list */
534 fail_unless_equals_int (g_list_length (buffers), 1);
536 for (l = buffers; l; l = l->next) {
537 GstBuffer *buffer = GST_BUFFER (l->data);
539 gst_buffer_unref (buffer);
542 g_list_free (buffers);
545 gst_caps_unref (desired);
548 GST_START_TEST (test_live_switch)
550 GstElement *audioresample;
554 audioresample = setup_audioresample (4, 48000, 48000, 16, FALSE);
556 /* Let the sinkpad act like something that can only handle things of
557 * rate 48000- and can only allocate buffers for that rate, but if someone
558 * tries to get a buffer with a rate higher then 48000 tries to renegotiate
560 gst_pad_set_bufferalloc_function (mysinkpad, live_switch_alloc_only_48000);
561 gst_pad_set_getcaps_function (mysinkpad, live_switch_get_sink_caps);
563 gst_pad_use_fixed_caps (mysrcpad);
565 caps = gst_pad_get_negotiated_caps (mysrcpad);
566 fail_unless (gst_caps_is_fixed (caps));
568 fail_unless (gst_element_set_state (audioresample,
569 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
570 "could not set to playing");
572 newseg = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME, 0, -1, 0);
573 fail_unless (gst_pad_push_event (mysrcpad, newseg) != FALSE);
575 /* downstream can provide the requested rate, a buffer alloc will be passed
577 live_switch_push (48000, caps);
579 /* Downstream can never accept this rate, buffer alloc isn't passed on */
580 live_switch_push (40000, caps);
582 /* Downstream can provide the requested rate but will re-negotiate */
583 live_switch_push (50000, caps);
585 cleanup_audioresample (audioresample);
586 gst_caps_unref (caps);
591 #ifndef GST_DISABLE_PARSE
593 static GMainLoop *loop;
594 static gint messages = 0;
597 element_message_cb (GstBus * bus, GstMessage * message, gpointer user_data)
601 s = gst_structure_to_string (gst_message_get_structure (message));
602 GST_DEBUG ("Received message: %s", s);
609 eos_message_cb (GstBus * bus, GstMessage * message, gpointer user_data)
611 GST_DEBUG ("Received eos");
612 g_main_loop_quit (loop);
616 test_pipeline (gint width, gboolean fp, gint inrate, gint outrate, gint quality)
618 GstElement *pipeline;
620 GError *error = NULL;
625 ("audiotestsrc num-buffers=10 ! audioconvert ! audio/x-raw-%s,rate=%d,width=%d,channels=2 ! audioresample quality=%d ! audio/x-raw-%s,rate=%d,width=%d ! identity check-imperfect-timestamp=TRUE ! fakesink",
626 (fp) ? "float" : "int", inrate, width, quality, (fp) ? "float" : "int",
629 pipeline = gst_parse_launch (pipe_str, &error);
630 fail_unless (pipeline != NULL, "Error parsing pipeline: %s",
631 error ? error->message : "(invalid error)");
634 bus = gst_element_get_bus (pipeline);
635 fail_if (bus == NULL);
636 gst_bus_add_signal_watch (bus);
637 g_signal_connect (bus, "message::element", (GCallback) element_message_cb,
639 g_signal_connect (bus, "message::eos", (GCallback) eos_message_cb, NULL);
641 gst_element_set_state (pipeline, GST_STATE_PLAYING);
643 /* run until we receive EOS */
644 loop = g_main_loop_new (NULL, FALSE);
646 g_main_loop_run (loop);
648 g_main_loop_unref (loop);
651 gst_element_set_state (pipeline, GST_STATE_NULL);
653 fail_if (messages > 0, "Received imperfect timestamp messages");
654 gst_object_unref (pipeline);
657 GST_START_TEST (test_pipelines)
661 /* Test qualities 0, 5 and 10 */
662 for (quality = 0; quality < 11; quality += 5) {
663 GST_DEBUG ("Checking with quality %d", quality);
665 test_pipeline (8, FALSE, 44100, 48000, quality);
666 test_pipeline (8, FALSE, 48000, 44100, quality);
668 test_pipeline (16, FALSE, 44100, 48000, quality);
669 test_pipeline (16, FALSE, 48000, 44100, quality);
671 test_pipeline (24, FALSE, 44100, 48000, quality);
672 test_pipeline (24, FALSE, 48000, 44100, quality);
674 test_pipeline (32, FALSE, 44100, 48000, quality);
675 test_pipeline (32, FALSE, 48000, 44100, quality);
677 test_pipeline (32, TRUE, 44100, 48000, quality);
678 test_pipeline (32, TRUE, 48000, 44100, quality);
680 test_pipeline (64, TRUE, 44100, 48000, quality);
681 test_pipeline (64, TRUE, 48000, 44100, quality);
687 GST_START_TEST (test_preference_passthrough)
689 GstStateChangeReturn ret;
690 GstElement *pipeline, *src;
696 GError *error = NULL;
699 pipeline = gst_parse_launch ("audiotestsrc num-buffers=1 name=src ! "
700 "audioresample ! audio/x-raw-int,channels=1,width=16,depth=16,"
701 "endianness=BYTE_ORDER,signed=true,rate=8000 ! "
702 "fakesink can-activate-pull=false", &error);
703 fail_unless (pipeline != NULL, "Error parsing pipeline: %s",
704 error ? error->message : "(invalid error)");
706 ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
707 fail_unless_equals_int (ret, GST_STATE_CHANGE_ASYNC);
709 /* run until we receive EOS */
710 bus = gst_element_get_bus (pipeline);
711 fail_if (bus == NULL);
712 msg = gst_bus_timed_pop_filtered (bus, -1, GST_MESSAGE_EOS);
713 gst_message_unref (msg);
714 gst_object_unref (bus);
716 src = gst_bin_get_by_name (GST_BIN (pipeline), "src");
717 fail_unless (src != NULL);
718 pad = gst_element_get_static_pad (src, "src");
719 fail_unless (pad != NULL);
720 caps = gst_pad_get_negotiated_caps (pad);
721 GST_LOG ("negotiated audiotestsrc caps: %" GST_PTR_FORMAT, caps);
722 fail_unless (caps != NULL);
723 s = gst_caps_get_structure (caps, 0);
724 fail_unless (gst_structure_get_int (s, "rate", &rate));
725 /* there's no need to resample, audiotestsrc supports any rate, so make
726 * sure audioresample provided upstream with the right caps to negotiate
728 fail_unless_equals_int (rate, 8000);
729 gst_caps_unref (caps);
730 gst_object_unref (pad);
731 gst_object_unref (src);
733 gst_element_set_state (pipeline, GST_STATE_NULL);
734 gst_object_unref (pipeline);
742 _message_cb (GstBus * bus, GstMessage * message, gpointer user_data)
744 GMainLoop *loop = user_data;
746 switch (GST_MESSAGE_TYPE (message)) {
747 case GST_MESSAGE_ERROR:
748 case GST_MESSAGE_WARNING:
749 g_assert_not_reached ();
751 case GST_MESSAGE_EOS:
752 g_main_loop_quit (loop);
764 GstClockTime next_out_ts;
765 guint64 next_out_off;
767 guint64 in_buffer_count, out_buffer_count;
771 fakesink_handoff_cb (GstElement * object, GstBuffer * buffer, GstPad * pad,
774 TimestampDriftCtx *ctx = user_data;
776 ctx->out_buffer_count++;
777 if (ctx->latency == GST_CLOCK_TIME_NONE) {
778 ctx->latency = 1000 - GST_BUFFER_SIZE (buffer) / 8;
781 /* Check if we have a perfectly timestamped stream */
782 if (ctx->next_out_ts != GST_CLOCK_TIME_NONE)
783 fail_unless (ctx->next_out_ts == GST_BUFFER_TIMESTAMP (buffer),
784 "expected timestamp %" GST_TIME_FORMAT " got timestamp %"
785 GST_TIME_FORMAT, GST_TIME_ARGS (ctx->next_out_ts),
786 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));
788 /* Check if we have a perfectly offsetted stream */
789 fail_unless (GST_BUFFER_OFFSET_END (buffer) ==
790 GST_BUFFER_OFFSET (buffer) + GST_BUFFER_SIZE (buffer) / 8,
791 "expected offset end %" G_GUINT64_FORMAT " got offset end %"
793 GST_BUFFER_OFFSET (buffer) + GST_BUFFER_SIZE (buffer) / 8,
794 GST_BUFFER_OFFSET_END (buffer));
795 if (ctx->next_out_off != GST_BUFFER_OFFSET_NONE) {
796 fail_unless (GST_BUFFER_OFFSET (buffer) == ctx->next_out_off,
797 "expected offset %" G_GUINT64_FORMAT " got offset %" G_GUINT64_FORMAT,
798 ctx->next_out_off, GST_BUFFER_OFFSET (buffer));
801 if (ctx->in_buffer_count != ctx->out_buffer_count) {
802 GST_INFO ("timestamp %" GST_TIME_FORMAT,
803 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));
806 if (ctx->in_ts != GST_CLOCK_TIME_NONE && ctx->in_buffer_count > 1
807 && ctx->in_buffer_count == ctx->out_buffer_count) {
808 fail_unless (GST_BUFFER_TIMESTAMP (buffer) ==
809 ctx->in_ts - gst_util_uint64_scale_round (ctx->latency, GST_SECOND,
811 "expected output timestamp %" GST_TIME_FORMAT " (%" G_GUINT64_FORMAT
812 ") got output timestamp %" GST_TIME_FORMAT " (%" G_GUINT64_FORMAT ")",
813 GST_TIME_ARGS (ctx->in_ts - gst_util_uint64_scale_round (ctx->latency,
815 ctx->in_ts - gst_util_uint64_scale_round (ctx->latency, GST_SECOND,
816 4096), GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
817 GST_BUFFER_TIMESTAMP (buffer));
821 GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer);
822 ctx->next_out_off = GST_BUFFER_OFFSET_END (buffer);
826 identity_handoff_cb (GstElement * object, GstBuffer * buffer,
829 TimestampDriftCtx *ctx = user_data;
831 ctx->in_ts = GST_BUFFER_TIMESTAMP (buffer);
832 ctx->in_buffer_count++;
835 GST_START_TEST (test_timestamp_drift)
837 TimestampDriftCtx ctx =
838 { GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE,
839 GST_BUFFER_OFFSET_NONE, 0, 0
841 GstElement *pipeline;
842 GstElement *audiotestsrc, *capsfilter1, *identity, *audioresample,
843 *capsfilter2, *fakesink;
848 pipeline = gst_pipeline_new ("pipeline");
849 fail_unless (pipeline != NULL);
851 audiotestsrc = gst_element_factory_make ("audiotestsrc", "src");
852 fail_unless (audiotestsrc != NULL);
853 g_object_set (G_OBJECT (audiotestsrc), "num-buffers", 10000,
854 "samplesperbuffer", 4000, NULL);
856 capsfilter1 = gst_element_factory_make ("capsfilter", "capsfilter1");
857 fail_unless (capsfilter1 != NULL);
860 ("audio/x-raw-float, channels=1, width=64, rate=16384");
861 g_object_set (G_OBJECT (capsfilter1), "caps", caps, NULL);
862 gst_caps_unref (caps);
864 identity = gst_element_factory_make ("identity", "identity");
865 fail_unless (identity != NULL);
866 g_object_set (G_OBJECT (identity), "sync", FALSE, "signal-handoffs", TRUE,
868 g_signal_connect (identity, "handoff", (GCallback) identity_handoff_cb, &ctx);
870 audioresample = gst_element_factory_make ("audioresample", "resample");
871 fail_unless (audioresample != NULL);
872 capsfilter2 = gst_element_factory_make ("capsfilter", "capsfilter2");
873 fail_unless (capsfilter2 != NULL);
876 ("audio/x-raw-float, channels=1, width=64, rate=4096");
877 g_object_set (G_OBJECT (capsfilter2), "caps", caps, NULL);
878 gst_caps_unref (caps);
880 fakesink = gst_element_factory_make ("fakesink", "sink");
881 fail_unless (fakesink != NULL);
882 g_object_set (G_OBJECT (fakesink), "sync", FALSE, "async", FALSE,
883 "signal-handoffs", TRUE, NULL);
884 g_signal_connect (fakesink, "handoff", (GCallback) fakesink_handoff_cb, &ctx);
887 gst_bin_add_many (GST_BIN (pipeline), audiotestsrc, capsfilter1, identity,
888 audioresample, capsfilter2, fakesink, NULL);
889 fail_unless (gst_element_link_many (audiotestsrc, capsfilter1, identity,
890 audioresample, capsfilter2, fakesink, NULL));
892 loop = g_main_loop_new (NULL, FALSE);
894 bus = gst_element_get_bus (pipeline);
895 gst_bus_add_signal_watch (bus);
896 g_signal_connect (bus, "message", (GCallback) _message_cb, loop);
898 fail_unless (gst_element_set_state (pipeline,
899 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS);
900 g_main_loop_run (loop);
902 fail_unless (gst_element_set_state (pipeline,
903 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS);
904 g_main_loop_unref (loop);
905 gst_object_unref (pipeline);
909 #define FFT_HELPERS(type,ffttag,ffttag2,scale); \
910 static gdouble magnitude##ffttag (const GstFFT##ffttag##Complex *c) \
912 gdouble mag = (gdouble) c->r * (gdouble) c->r; \
913 mag += (gdouble) c->i * (gdouble) c->i; \
914 mag /= scale * scale; \
915 mag = 10.0 * log10 (mag); \
918 static gdouble find_main_frequency_spot_##ffttag (const GstFFT##ffttag##Complex *v, \
922 gdouble maxmag = -9999; \
924 for (i=0; i<elements; ++i) { \
925 gdouble mag = magnitude##ffttag (v+i); \
926 if (mag > maxmag) { \
931 return maxidx / (gdouble) elements; \
933 static gboolean is_zero_except_##ffttag (const GstFFT##ffttag##Complex *v, int elements, \
937 for (i=0; i<elements; ++i) { \
938 gdouble pos = i / (gdouble) elements; \
939 gdouble mag = magnitude##ffttag (v+i); \
940 if (fabs (pos - spot) > 0.01) { \
948 static void compare_ffts_##ffttag (const GstBuffer *inbuffer, const GstBuffer *outbuffer) \
950 int insamples = GST_BUFFER_SIZE (inbuffer) / sizeof(type) & ~1; \
951 int outsamples = GST_BUFFER_SIZE (outbuffer) / sizeof(type) & ~1; \
952 gdouble inspot, outspot; \
954 GstFFT##ffttag *inctx = gst_fft_##ffttag2##_new (insamples, FALSE); \
955 GstFFT##ffttag##Complex *in = g_new (GstFFT##ffttag##Complex, insamples / 2 + 1); \
956 GstFFT##ffttag *outctx = gst_fft_##ffttag2##_new (outsamples, FALSE); \
957 GstFFT##ffttag##Complex *out = g_new (GstFFT##ffttag##Complex, outsamples / 2 + 1); \
959 gst_fft_##ffttag2##_window (inctx, (type*)GST_BUFFER_DATA (inbuffer), \
960 GST_FFT_WINDOW_HAMMING); \
961 gst_fft_##ffttag2##_fft (inctx, (type*)GST_BUFFER_DATA (inbuffer), in); \
962 gst_fft_##ffttag2##_window (outctx, (type*)GST_BUFFER_DATA (outbuffer), \
963 GST_FFT_WINDOW_HAMMING); \
964 gst_fft_##ffttag2##_fft (outctx, (type*)GST_BUFFER_DATA (outbuffer), out); \
966 inspot = find_main_frequency_spot_##ffttag (in, insamples / 2 + 1); \
967 outspot = find_main_frequency_spot_##ffttag (out, outsamples / 2 + 1); \
968 GST_LOG ("Spots are %.3f and %.3f", inspot, outspot); \
969 fail_unless (fabs (outspot - inspot) < 0.05); \
970 fail_unless (is_zero_except_##ffttag (in, insamples / 2 + 1, inspot)); \
971 fail_unless (is_zero_except_##ffttag (out, outsamples / 2 + 1, outspot)); \
973 gst_fft_##ffttag2##_free (inctx); \
974 gst_fft_##ffttag2##_free (outctx); \
978 FFT_HELPERS (float, F32, f32, 2048.0f);
979 FFT_HELPERS (double, F64, f64, 2048.0);
980 FFT_HELPERS (gint16, S16, s16, 32767.0);
981 FFT_HELPERS (gint32, S32, s32, 2147483647.0);
983 #define FILL_BUFFER(type, desc, value); \
984 static void init_##type##_##desc (GstBuffer *buffer) \
986 type *ptr = (type *) GST_BUFFER_DATA (buffer); \
987 int i, nsamples = GST_BUFFER_SIZE (buffer) / sizeof (type); \
988 for (i = 0; i < nsamples; ++i) { \
993 FILL_BUFFER (float, silence, 0.0f);
994 FILL_BUFFER (double, silence, 0.0);
995 FILL_BUFFER (gint16, silence, 0);
996 FILL_BUFFER (gint32, silence, 0);
997 FILL_BUFFER (float, sine, sinf (i * 0.01f));
998 FILL_BUFFER (float, sine2, sinf (i * 1.8f));
999 FILL_BUFFER (double, sine, sin (i * 0.01));
1000 FILL_BUFFER (double, sine2, sin (i * 1.8));
1001 FILL_BUFFER (gint16, sine, (gint16) (32767 * sinf (i * 0.01f)));
1002 FILL_BUFFER (gint16, sine2, (gint16) (32767 * sinf (i * 1.8f)));
1003 FILL_BUFFER (gint32, sine, (gint32) (2147483647 * sinf (i * 0.01f)));
1004 FILL_BUFFER (gint32, sine2, (gint32) (2147483647 * sinf (i * 1.8f)));
1007 run_fft_pipeline (int inrate, int outrate, int quality, int width, gboolean fp,
1008 void (*init) (GstBuffer *),
1009 void (*compare_ffts) (const GstBuffer *, const GstBuffer *))
1011 GstElement *audioresample;
1012 GstBuffer *inbuffer, *outbuffer;
1014 const int nsamples = 2048;
1016 audioresample = setup_audioresample (1, inrate, outrate, width, fp);
1017 fail_unless (audioresample != NULL);
1018 g_object_set (audioresample, "quality", quality, NULL);
1019 caps = gst_pad_get_negotiated_caps (mysrcpad);
1020 fail_unless (gst_caps_is_fixed (caps));
1022 fail_unless (gst_element_set_state (audioresample,
1023 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
1024 "could not set to playing");
1026 inbuffer = gst_buffer_new_and_alloc (nsamples * width / 8);
1027 GST_BUFFER_DURATION (inbuffer) = GST_FRAMES_TO_CLOCK_TIME (nsamples, inrate);
1028 GST_BUFFER_TIMESTAMP (inbuffer) = 0;
1029 gst_buffer_set_caps (inbuffer, caps);
1030 gst_buffer_ref (inbuffer);
1034 /* pushing gives away my reference ... */
1035 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
1036 /* ... but it ends up being collected on the global buffer list */
1037 fail_unless_equals_int (g_list_length (buffers), 1);
1038 /* retrieve out buffer */
1039 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
1041 fail_unless (gst_element_set_state (audioresample,
1042 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
1044 (*compare_ffts) (inbuffer, outbuffer);
1047 gst_buffer_unref (inbuffer);
1048 gst_caps_unref (caps);
1049 cleanup_audioresample (audioresample);
1052 GST_START_TEST (test_fft)
1056 static const int frequencies[] =
1057 { 8000, 16000, 44100, 48000, 128000, 12345, 54321 };
1059 /* audioresample uses a mixed float/double code path for floats with quality>8, make sure we test it */
1060 for (quality = 0; quality <= 10; quality += 5) {
1061 for (f0 = 0; f0 < G_N_ELEMENTS (frequencies); ++f0) {
1062 for (f1 = 0; f1 < G_N_ELEMENTS (frequencies); ++f1) {
1063 run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 32, TRUE,
1064 &init_float_silence, &compare_ffts_F32);
1065 run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 32, TRUE,
1066 &init_float_sine, &compare_ffts_F32);
1067 run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 32, TRUE,
1068 &init_float_sine2, &compare_ffts_F32);
1069 run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 64, TRUE,
1070 &init_double_silence, &compare_ffts_F64);
1071 run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 64, TRUE,
1072 &init_double_sine, &compare_ffts_F64);
1073 run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 64, TRUE,
1074 &init_double_sine2, &compare_ffts_F64);
1075 run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 16, FALSE,
1076 &init_gint16_silence, &compare_ffts_S16);
1077 run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 16, FALSE,
1078 &init_gint16_sine, &compare_ffts_S16);
1079 run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 16, FALSE,
1080 &init_gint16_sine2, &compare_ffts_S16);
1081 run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 32, FALSE,
1082 &init_gint32_silence, &compare_ffts_S32);
1083 run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 32, FALSE,
1084 &init_gint32_sine, &compare_ffts_S32);
1085 run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 32, FALSE,
1086 &init_gint32_sine2, &compare_ffts_S32);
1095 audioresample_suite (void)
1097 Suite *s = suite_create ("audioresample");
1098 TCase *tc_chain = tcase_create ("general");
1100 suite_add_tcase (s, tc_chain);
1101 tcase_add_test (tc_chain, test_perfect_stream);
1102 tcase_add_test (tc_chain, test_discont_stream);
1103 tcase_add_test (tc_chain, test_reuse);
1104 tcase_add_test (tc_chain, test_shutdown);
1105 tcase_add_test (tc_chain, test_live_switch);
1106 tcase_add_test (tc_chain, test_timestamp_drift);
1107 tcase_add_test (tc_chain, test_fft);
1109 #ifndef GST_DISABLE_PARSE
1110 tcase_set_timeout (tc_chain, 360);
1111 tcase_add_test (tc_chain, test_pipelines);
1112 tcase_add_test (tc_chain, test_preference_passthrough);
1118 GST_CHECK_MAIN (audioresample);