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 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
42 #define FORMATS_F "{ F32LE, F64LE }"
43 #define FORMATS_I "{ S16LE, S32LE }"
45 #define FORMATS_F "{ F32BE, F64BE }"
46 #define FORMATS_I "{ S16BE, S32BE }"
49 #define RESAMPLE_CAPS_FLOAT \
51 "formats = (string) "FORMATS_F", " \
52 "channels = (int) [ 1, MAX ], " \
53 "rate = (int) [ 1, MAX ]"
55 #define RESAMPLE_CAPS_INT \
57 "formats = (string) "FORMATS_I", " \
58 "channels = (int) [ 1, MAX ], " \
59 "rate = (int) [ 1, MAX ]"
61 #define RESAMPLE_CAPS_TEMPLATE_STRING \
62 RESAMPLE_CAPS_FLOAT " ; " \
65 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
68 GST_STATIC_CAPS (RESAMPLE_CAPS_TEMPLATE_STRING)
70 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
73 GST_STATIC_CAPS (RESAMPLE_CAPS_TEMPLATE_STRING)
77 setup_audioresample (int channels, int inrate, int outrate, int width,
80 GstElement *audioresample;
82 GstStructure *structure;
84 GST_DEBUG ("setup_audioresample");
85 audioresample = gst_check_setup_element ("audioresample");
88 caps = gst_caps_from_string (RESAMPLE_CAPS_FLOAT);
90 caps = gst_caps_from_string (RESAMPLE_CAPS_INT);
91 structure = gst_caps_get_structure (caps, 0);
92 gst_structure_set (structure, "channels", G_TYPE_INT, channels,
93 "rate", G_TYPE_INT, inrate, "width", G_TYPE_INT, width, NULL);
95 gst_structure_set (structure, "depth", G_TYPE_INT, width, NULL);
96 fail_unless (gst_caps_is_fixed (caps));
98 fail_unless (gst_element_set_state (audioresample,
99 GST_STATE_PAUSED) == GST_STATE_CHANGE_SUCCESS,
100 "could not set to paused");
102 mysrcpad = gst_check_setup_src_pad (audioresample, &srctemplate);
103 gst_pad_set_caps (mysrcpad, caps);
104 gst_caps_unref (caps);
107 caps = gst_caps_from_string (RESAMPLE_CAPS_FLOAT);
109 caps = gst_caps_from_string (RESAMPLE_CAPS_INT);
110 structure = gst_caps_get_structure (caps, 0);
111 gst_structure_set (structure, "channels", G_TYPE_INT, channels,
112 "rate", G_TYPE_INT, outrate, "width", G_TYPE_INT, width, NULL);
114 gst_structure_set (structure, "depth", G_TYPE_INT, width, NULL);
115 fail_unless (gst_caps_is_fixed (caps));
117 mysinkpad = gst_check_setup_sink_pad (audioresample, &sinktemplate);
118 /* this installs a getcaps func that will always return the caps we set
120 gst_pad_set_caps (mysinkpad, caps);
121 gst_pad_use_fixed_caps (mysinkpad);
123 gst_pad_set_active (mysinkpad, TRUE);
124 gst_pad_set_active (mysrcpad, TRUE);
126 gst_caps_unref (caps);
128 return audioresample;
132 cleanup_audioresample (GstElement * audioresample)
134 GST_DEBUG ("cleanup_audioresample");
136 fail_unless (gst_element_set_state (audioresample,
137 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to NULL");
139 gst_pad_set_active (mysrcpad, FALSE);
140 gst_pad_set_active (mysinkpad, FALSE);
141 gst_check_teardown_src_pad (audioresample);
142 gst_check_teardown_sink_pad (audioresample);
143 gst_check_teardown_element (audioresample);
144 gst_check_drop_buffers ();
148 fail_unless_perfect_stream (void)
150 guint64 timestamp = 0L, duration = 0L;
151 guint64 offset = 0L, offset_end = 0L;
156 for (l = buffers; l; l = l->next) {
157 buffer = GST_BUFFER (l->data);
158 ASSERT_BUFFER_REFCOUNT (buffer, "buffer", 1);
159 GST_DEBUG ("buffer timestamp %" G_GUINT64_FORMAT ", duration %"
160 G_GUINT64_FORMAT " offset %" G_GUINT64_FORMAT " offset_end %"
162 GST_BUFFER_TIMESTAMP (buffer),
163 GST_BUFFER_DURATION (buffer),
164 GST_BUFFER_OFFSET (buffer), GST_BUFFER_OFFSET_END (buffer));
166 fail_unless_equals_uint64 (timestamp, GST_BUFFER_TIMESTAMP (buffer));
167 fail_unless_equals_uint64 (offset, GST_BUFFER_OFFSET (buffer));
168 duration = GST_BUFFER_DURATION (buffer);
169 offset_end = GST_BUFFER_OFFSET_END (buffer);
171 timestamp += duration;
173 gst_buffer_unref (buffer);
175 g_list_free (buffers);
179 /* this tests that the output is a perfect stream if the input is */
181 test_perfect_stream_instance (int inrate, int outrate, int samples,
184 GstElement *audioresample;
185 GstBuffer *inbuffer, *outbuffer;
192 audioresample = setup_audioresample (2, inrate, outrate, 16, FALSE);
193 caps = gst_pad_get_current_caps (mysrcpad);
194 fail_unless (gst_caps_is_fixed (caps));
196 fail_unless (gst_element_set_state (audioresample,
197 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
198 "could not set to playing");
200 for (j = 1; j <= numbuffers; ++j) {
202 inbuffer = gst_buffer_new_and_alloc (samples * 4);
203 GST_BUFFER_DURATION (inbuffer) = GST_FRAMES_TO_CLOCK_TIME (samples, inrate);
204 GST_BUFFER_TIMESTAMP (inbuffer) = GST_BUFFER_DURATION (inbuffer) * (j - 1);
205 GST_BUFFER_OFFSET (inbuffer) = offset;
207 GST_BUFFER_OFFSET_END (inbuffer) = offset;
209 gst_buffer_map (inbuffer, &map, GST_MAP_WRITE);
210 p = (gint16 *) map.data;
212 /* create a 16 bit signed ramp */
213 for (i = 0; i < samples; ++i) {
214 *p = -32767 + i * (65535 / samples);
216 *p = -32767 + i * (65535 / samples);
219 gst_buffer_unmap (inbuffer, &map);
221 /* pushing gives away my reference ... */
222 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
223 /* ... but it ends up being collected on the global buffer list */
224 fail_unless_equals_int (g_list_length (buffers), j);
227 /* FIXME: we should make audioresample handle eos by flushing out the last
228 * samples, which will give us one more, small, buffer */
229 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
230 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
232 fail_unless_perfect_stream ();
235 gst_caps_unref (caps);
236 cleanup_audioresample (audioresample);
240 /* make sure that outgoing buffers are contiguous in timestamp/duration and
243 GST_START_TEST (test_perfect_stream)
245 /* integral scalings */
246 test_perfect_stream_instance (48000, 24000, 500, 20);
247 test_perfect_stream_instance (48000, 12000, 500, 20);
248 test_perfect_stream_instance (12000, 24000, 500, 20);
249 test_perfect_stream_instance (12000, 48000, 500, 20);
251 /* non-integral scalings */
252 test_perfect_stream_instance (44100, 8000, 500, 20);
253 test_perfect_stream_instance (8000, 44100, 500, 20);
256 test_perfect_stream_instance (12345, 54321, 500, 20);
257 test_perfect_stream_instance (101, 99, 500, 20);
262 /* this tests that the output is a correct discontinuous stream
263 * if the input is; ie input drops in time come out the same way */
265 test_discont_stream_instance (int inrate, int outrate, int samples,
268 GstElement *audioresample;
269 GstBuffer *inbuffer, *outbuffer;
277 GST_DEBUG ("inrate:%d outrate:%d samples:%d numbuffers:%d",
278 inrate, outrate, samples, numbuffers);
280 audioresample = setup_audioresample (2, inrate, outrate, 16, FALSE);
281 caps = gst_pad_get_current_caps (mysrcpad);
282 fail_unless (gst_caps_is_fixed (caps));
284 fail_unless (gst_element_set_state (audioresample,
285 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
286 "could not set to playing");
288 for (j = 1; j <= numbuffers; ++j) {
290 inbuffer = gst_buffer_new_and_alloc (samples * 4);
291 GST_BUFFER_DURATION (inbuffer) = samples * GST_SECOND / inrate;
292 /* "drop" half the buffers */
293 ints = GST_BUFFER_DURATION (inbuffer) * 2 * (j - 1);
294 GST_BUFFER_TIMESTAMP (inbuffer) = ints;
295 GST_BUFFER_OFFSET (inbuffer) = (j - 1) * 2 * samples;
296 GST_BUFFER_OFFSET_END (inbuffer) = j * 2 * samples + samples;
298 gst_buffer_map (inbuffer, &map, GST_MAP_WRITE);
299 p = (gint16 *) map.data;
300 /* create a 16 bit signed ramp */
301 for (i = 0; i < samples; ++i) {
302 *p = -32767 + i * (65535 / samples);
304 *p = -32767 + i * (65535 / samples);
307 gst_buffer_unmap (inbuffer, &map);
309 GST_DEBUG ("Sending Buffer time:%" G_GUINT64_FORMAT " duration:%"
310 G_GINT64_FORMAT " discont:%d offset:%" G_GUINT64_FORMAT " offset_end:%"
311 G_GUINT64_FORMAT, GST_BUFFER_TIMESTAMP (inbuffer),
312 GST_BUFFER_DURATION (inbuffer), GST_BUFFER_IS_DISCONT (inbuffer),
313 GST_BUFFER_OFFSET (inbuffer), GST_BUFFER_OFFSET_END (inbuffer));
314 /* pushing gives away my reference ... */
315 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
317 /* check if the timestamp of the pushed buffer matches the incoming one */
318 outbuffer = g_list_nth_data (buffers, g_list_length (buffers) - 1);
319 fail_if (outbuffer == NULL);
320 fail_unless_equals_uint64 (ints, GST_BUFFER_TIMESTAMP (outbuffer));
321 GST_DEBUG ("Got Buffer time:%" G_GUINT64_FORMAT " duration:%"
322 G_GINT64_FORMAT " discont:%d offset:%" G_GUINT64_FORMAT " offset_end:%"
323 G_GUINT64_FORMAT, GST_BUFFER_TIMESTAMP (outbuffer),
324 GST_BUFFER_DURATION (outbuffer), GST_BUFFER_IS_DISCONT (outbuffer),
325 GST_BUFFER_OFFSET (outbuffer), GST_BUFFER_OFFSET_END (outbuffer));
327 fail_unless (GST_BUFFER_IS_DISCONT (outbuffer),
328 "expected discont for buffer #%d", j);
333 gst_caps_unref (caps);
334 cleanup_audioresample (audioresample);
337 GST_START_TEST (test_discont_stream)
339 /* integral scalings */
340 test_discont_stream_instance (48000, 24000, 5000, 20);
341 test_discont_stream_instance (48000, 12000, 5000, 20);
342 test_discont_stream_instance (12000, 24000, 5000, 20);
343 test_discont_stream_instance (12000, 48000, 5000, 20);
345 /* non-integral scalings */
346 test_discont_stream_instance (44100, 8000, 5000, 20);
347 test_discont_stream_instance (8000, 44100, 5000, 20);
350 test_discont_stream_instance (12345, 54321, 5000, 20);
351 test_discont_stream_instance (101, 99, 5000, 20);
358 GST_START_TEST (test_reuse)
360 GstElement *audioresample;
366 audioresample = setup_audioresample (1, 9343, 48000, 16, FALSE);
367 caps = gst_pad_get_current_caps (mysrcpad);
368 fail_unless (gst_caps_is_fixed (caps));
370 fail_unless (gst_element_set_state (audioresample,
371 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
372 "could not set to playing");
374 gst_segment_init (&segment, GST_FORMAT_TIME);
375 newseg = gst_event_new_segment (&segment);
376 fail_unless (gst_pad_push_event (mysrcpad, newseg) != FALSE);
378 inbuffer = gst_buffer_new_and_alloc (9343 * 4);
379 gst_buffer_memset (inbuffer, 0, 0, 9343 * 4);
380 GST_BUFFER_DURATION (inbuffer) = GST_SECOND;
381 GST_BUFFER_TIMESTAMP (inbuffer) = 0;
382 GST_BUFFER_OFFSET (inbuffer) = 0;
384 /* pushing gives away my reference ... */
385 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
387 /* ... but it ends up being collected on the global buffer list */
388 fail_unless_equals_int (g_list_length (buffers), 1);
390 /* now reset and try again ... */
391 fail_unless (gst_element_set_state (audioresample,
392 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to NULL");
394 fail_unless (gst_element_set_state (audioresample,
395 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
396 "could not set to playing");
398 newseg = gst_event_new_segment (&segment);
399 fail_unless (gst_pad_push_event (mysrcpad, newseg) != FALSE);
401 inbuffer = gst_buffer_new_and_alloc (9343 * 4);
402 gst_buffer_memset (inbuffer, 0, 0, 9343 * 4);
403 GST_BUFFER_DURATION (inbuffer) = GST_SECOND;
404 GST_BUFFER_TIMESTAMP (inbuffer) = 0;
405 GST_BUFFER_OFFSET (inbuffer) = 0;
407 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
409 /* ... it also ends up being collected on the global buffer list. If we
410 * now have more than 2 buffers, then audioresample probably didn't clean
411 * up its internal buffer properly and tried to push the remaining samples
412 * when it got the second NEWSEGMENT event */
413 fail_unless_equals_int (g_list_length (buffers), 2);
415 cleanup_audioresample (audioresample);
416 gst_caps_unref (caps);
421 GST_START_TEST (test_shutdown)
423 GstElement *pipeline, *src, *cf1, *ar, *cf2, *sink;
427 /* create pipeline, force audioresample to actually resample */
428 pipeline = gst_pipeline_new (NULL);
430 src = gst_check_setup_element ("audiotestsrc");
431 cf1 = gst_check_setup_element ("capsfilter");
432 ar = gst_check_setup_element ("audioresample");
433 cf2 = gst_check_setup_element ("capsfilter");
434 g_object_set (cf2, "name", "capsfilter2", NULL);
435 sink = gst_check_setup_element ("fakesink");
437 caps = gst_caps_new_simple ("audio/x-raw", "rate", G_TYPE_INT, 11025, NULL);
438 g_object_set (cf1, "caps", caps, NULL);
439 gst_caps_unref (caps);
441 caps = gst_caps_new_simple ("audio/x-raw", "rate", G_TYPE_INT, 48000, NULL);
442 g_object_set (cf2, "caps", caps, NULL);
443 gst_caps_unref (caps);
445 /* don't want to sync against the clock, the more throughput the better */
446 g_object_set (src, "is-live", FALSE, NULL);
447 g_object_set (sink, "sync", FALSE, NULL);
449 gst_bin_add_many (GST_BIN (pipeline), src, cf1, ar, cf2, sink, NULL);
450 fail_if (!gst_element_link_many (src, cf1, ar, cf2, sink, NULL));
452 /* now, wait until pipeline is running and then shut it down again; repeat */
453 for (i = 0; i < 20; ++i) {
454 gst_element_set_state (pipeline, GST_STATE_PAUSED);
455 gst_element_get_state (pipeline, NULL, NULL, -1);
456 gst_element_set_state (pipeline, GST_STATE_PLAYING);
458 gst_element_set_state (pipeline, GST_STATE_NULL);
461 gst_object_unref (pipeline);
468 live_switch_alloc_only_48000 (GstPad * pad, guint64 offset,
469 guint size, GstCaps * caps, GstBuffer ** buf)
471 GstStructure *structure;
476 structure = gst_caps_get_structure (caps, 0);
477 fail_unless (gst_structure_get_int (structure, "rate", &rate));
478 fail_unless (gst_structure_get_int (structure, "channels", &channels));
481 return GST_FLOW_NOT_NEGOTIATED;
483 desired = gst_caps_copy (caps);
484 gst_caps_set_simple (desired, "rate", G_TYPE_INT, 48000, NULL);
486 *buf = gst_buffer_new_and_alloc (channels * 48000);
487 gst_buffer_set_caps (*buf, desired);
488 gst_caps_unref (desired);
494 live_switch_get_sink_caps (GstPad * pad)
498 result = gst_caps_make_writable (gst_pad_get_current_caps (pad));
500 gst_caps_set_simple (result,
501 "rate", GST_TYPE_INT_RANGE, 48000, G_MAXINT, NULL);
508 live_switch_push (int rate, GstCaps * caps)
514 desired = gst_caps_copy (caps);
515 gst_caps_set_simple (desired, "rate", G_TYPE_INT, rate, NULL);
516 gst_pad_set_caps (mysrcpad, desired);
519 fail_unless (gst_pad_alloc_buffer_and_set_caps (mysrcpad,
520 GST_BUFFER_OFFSET_NONE, rate * 4, desired, &inbuffer) == GST_FLOW_OK);
522 inbuffer = gst_buffer_new_and_alloc (rate * 4);
523 gst_buffer_memset (inbuffer, 0, 0, rate * 4);
525 GST_BUFFER_DURATION (inbuffer) = GST_SECOND;
526 GST_BUFFER_TIMESTAMP (inbuffer) = 0;
527 GST_BUFFER_OFFSET (inbuffer) = 0;
529 /* pushing gives away my reference ... */
530 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
532 /* ... but it ends up being collected on the global buffer list */
533 fail_unless_equals_int (g_list_length (buffers), 1);
535 for (l = buffers; l; l = l->next) {
536 GstBuffer *buffer = GST_BUFFER (l->data);
538 gst_buffer_unref (buffer);
541 g_list_free (buffers);
544 gst_caps_unref (desired);
547 GST_START_TEST (test_live_switch)
549 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_current_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 gst_segment_init (&segment, GST_FORMAT_TIME);
573 newseg = gst_event_new_segment (&segment);
574 fail_unless (gst_pad_push_event (mysrcpad, newseg) != FALSE);
576 /* downstream can provide the requested rate, a buffer alloc will be passed
578 live_switch_push (48000, caps);
580 /* Downstream can never accept this rate, buffer alloc isn't passed on */
581 live_switch_push (40000, caps);
583 /* Downstream can provide the requested rate but will re-negotiate */
584 live_switch_push (50000, caps);
586 cleanup_audioresample (audioresample);
587 gst_caps_unref (caps);
592 #ifndef GST_DISABLE_PARSE
594 static GMainLoop *loop;
595 static gint messages = 0;
598 element_message_cb (GstBus * bus, GstMessage * message, gpointer user_data)
602 s = gst_structure_to_string (gst_message_get_structure (message));
603 GST_DEBUG ("Received message: %s", s);
610 eos_message_cb (GstBus * bus, GstMessage * message, gpointer user_data)
612 GST_DEBUG ("Received eos");
613 g_main_loop_quit (loop);
617 test_pipeline (const gchar * format, gint inrate, gint outrate, gint quality)
619 GstElement *pipeline;
621 GError *error = NULL;
626 ("audiotestsrc num-buffers=10 ! audioconvert ! audio/x-raw,format=%s,rate=%d,channels=2 ! audioresample quality=%d ! audio/x-raw,format=%s,rate=%d ! identity check-imperfect-timestamp=TRUE ! fakesink",
627 format, inrate, quality, format, outrate);
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 ("S8", 44100, 48000, quality);
666 test_pipeline ("S8", 48000, 44100, quality);
668 test_pipeline (GST_AUDIO_NE (S16), 44100, 48000, quality);
669 test_pipeline (GST_AUDIO_NE (S16), 48000, 44100, quality);
671 test_pipeline (GST_AUDIO_NE (S24), 44100, 48000, quality);
672 test_pipeline (GST_AUDIO_NE (S24), 48000, 44100, quality);
674 test_pipeline (GST_AUDIO_NE (S32), 44100, 48000, quality);
675 test_pipeline (GST_AUDIO_NE (S32), 48000, 44100, quality);
677 test_pipeline (GST_AUDIO_NE (F32), 44100, 48000, quality);
678 test_pipeline (GST_AUDIO_NE (F32), 48000, 44100, quality);
680 test_pipeline (GST_AUDIO_NE (F64), 44100, 48000, quality);
681 test_pipeline (GST_AUDIO_NE (F64), 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,format=" GST_AUDIO_NE (S16) ",channels=1,"
701 "rate=8000 ! fakesink can-activate-pull=false", &error);
702 fail_unless (pipeline != NULL, "Error parsing pipeline: %s",
703 error ? error->message : "(invalid error)");
705 ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
706 fail_unless_equals_int (ret, GST_STATE_CHANGE_ASYNC);
708 /* run until we receive EOS */
709 bus = gst_element_get_bus (pipeline);
710 fail_if (bus == NULL);
711 msg = gst_bus_timed_pop_filtered (bus, -1, GST_MESSAGE_EOS);
712 gst_message_unref (msg);
713 gst_object_unref (bus);
715 src = gst_bin_get_by_name (GST_BIN (pipeline), "src");
716 fail_unless (src != NULL);
717 pad = gst_element_get_static_pad (src, "src");
718 fail_unless (pad != NULL);
719 caps = gst_pad_get_current_caps (pad);
720 GST_LOG ("current audiotestsrc caps: %" GST_PTR_FORMAT, caps);
721 fail_unless (caps != NULL);
722 s = gst_caps_get_structure (caps, 0);
723 fail_unless (gst_structure_get_int (s, "rate", &rate));
724 /* there's no need to resample, audiotestsrc supports any rate, so make
725 * sure audioresample provided upstream with the right caps to negotiate
727 fail_unless_equals_int (rate, 8000);
728 gst_caps_unref (caps);
729 gst_object_unref (pad);
730 gst_object_unref (src);
732 gst_element_set_state (pipeline, GST_STATE_NULL);
733 gst_object_unref (pipeline);
741 _message_cb (GstBus * bus, GstMessage * message, gpointer user_data)
743 GMainLoop *loop = user_data;
745 switch (GST_MESSAGE_TYPE (message)) {
746 case GST_MESSAGE_ERROR:
747 case GST_MESSAGE_WARNING:
748 g_assert_not_reached ();
750 case GST_MESSAGE_EOS:
751 g_main_loop_quit (loop);
763 GstClockTime next_out_ts;
764 guint64 next_out_off;
766 guint64 in_buffer_count, out_buffer_count;
770 fakesink_handoff_cb (GstElement * object, GstBuffer * buffer, GstPad * pad,
773 TimestampDriftCtx *ctx = user_data;
775 ctx->out_buffer_count++;
776 if (ctx->latency == GST_CLOCK_TIME_NONE) {
777 ctx->latency = 1000 - gst_buffer_get_size (buffer) / 8;
780 /* Check if we have a perfectly timestamped stream */
781 if (ctx->next_out_ts != GST_CLOCK_TIME_NONE)
782 fail_unless (ctx->next_out_ts == GST_BUFFER_TIMESTAMP (buffer),
783 "expected timestamp %" GST_TIME_FORMAT " got timestamp %"
784 GST_TIME_FORMAT, GST_TIME_ARGS (ctx->next_out_ts),
785 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));
787 /* Check if we have a perfectly offsetted stream */
788 fail_unless (GST_BUFFER_OFFSET_END (buffer) ==
789 GST_BUFFER_OFFSET (buffer) + gst_buffer_get_size (buffer) / 8,
790 "expected offset end %" G_GUINT64_FORMAT " got offset end %"
792 GST_BUFFER_OFFSET (buffer) + gst_buffer_get_size (buffer) / 8,
793 GST_BUFFER_OFFSET_END (buffer));
794 if (ctx->next_out_off != GST_BUFFER_OFFSET_NONE) {
795 fail_unless (GST_BUFFER_OFFSET (buffer) == ctx->next_out_off,
796 "expected offset %" G_GUINT64_FORMAT " got offset %" G_GUINT64_FORMAT,
797 ctx->next_out_off, GST_BUFFER_OFFSET (buffer));
800 if (ctx->in_buffer_count != ctx->out_buffer_count) {
801 GST_INFO ("timestamp %" GST_TIME_FORMAT,
802 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));
805 if (ctx->in_ts != GST_CLOCK_TIME_NONE && ctx->in_buffer_count > 1
806 && ctx->in_buffer_count == ctx->out_buffer_count) {
807 fail_unless (GST_BUFFER_TIMESTAMP (buffer) ==
808 ctx->in_ts - gst_util_uint64_scale_round (ctx->latency, GST_SECOND,
810 "expected output timestamp %" GST_TIME_FORMAT " (%" G_GUINT64_FORMAT
811 ") got output timestamp %" GST_TIME_FORMAT " (%" G_GUINT64_FORMAT ")",
812 GST_TIME_ARGS (ctx->in_ts - gst_util_uint64_scale_round (ctx->latency,
814 ctx->in_ts - gst_util_uint64_scale_round (ctx->latency, GST_SECOND,
815 4096), GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
816 GST_BUFFER_TIMESTAMP (buffer));
820 GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer);
821 ctx->next_out_off = GST_BUFFER_OFFSET_END (buffer);
825 identity_handoff_cb (GstElement * object, GstBuffer * buffer,
828 TimestampDriftCtx *ctx = user_data;
830 ctx->in_ts = GST_BUFFER_TIMESTAMP (buffer);
831 ctx->in_buffer_count++;
834 GST_START_TEST (test_timestamp_drift)
836 TimestampDriftCtx ctx =
837 { GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE,
838 GST_BUFFER_OFFSET_NONE, 0, 0
840 GstElement *pipeline;
841 GstElement *audiotestsrc, *capsfilter1, *identity, *audioresample,
842 *capsfilter2, *fakesink;
847 pipeline = gst_pipeline_new ("pipeline");
848 fail_unless (pipeline != NULL);
850 audiotestsrc = gst_element_factory_make ("audiotestsrc", "src");
851 fail_unless (audiotestsrc != NULL);
852 g_object_set (G_OBJECT (audiotestsrc), "num-buffers", 10000,
853 "samplesperbuffer", 4000, NULL);
855 capsfilter1 = gst_element_factory_make ("capsfilter", "capsfilter1");
856 fail_unless (capsfilter1 != NULL);
859 ("audio/x-raw, format=F64LE, channels=1, rate=16384");
860 g_object_set (G_OBJECT (capsfilter1), "caps", caps, NULL);
861 gst_caps_unref (caps);
863 identity = gst_element_factory_make ("identity", "identity");
864 fail_unless (identity != NULL);
865 g_object_set (G_OBJECT (identity), "sync", FALSE, "signal-handoffs", TRUE,
867 g_signal_connect (identity, "handoff", (GCallback) identity_handoff_cb, &ctx);
869 audioresample = gst_element_factory_make ("audioresample", "resample");
870 fail_unless (audioresample != NULL);
871 capsfilter2 = gst_element_factory_make ("capsfilter", "capsfilter2");
872 fail_unless (capsfilter2 != NULL);
874 gst_caps_from_string ("audio/x-raw, format=F64LE, channels=1, rate=4096");
875 g_object_set (G_OBJECT (capsfilter2), "caps", caps, NULL);
876 gst_caps_unref (caps);
878 fakesink = gst_element_factory_make ("fakesink", "sink");
879 fail_unless (fakesink != NULL);
880 g_object_set (G_OBJECT (fakesink), "sync", FALSE, "async", FALSE,
881 "signal-handoffs", TRUE, NULL);
882 g_signal_connect (fakesink, "handoff", (GCallback) fakesink_handoff_cb, &ctx);
885 gst_bin_add_many (GST_BIN (pipeline), audiotestsrc, capsfilter1, identity,
886 audioresample, capsfilter2, fakesink, NULL);
887 fail_unless (gst_element_link_many (audiotestsrc, capsfilter1, identity,
888 audioresample, capsfilter2, fakesink, NULL));
890 loop = g_main_loop_new (NULL, FALSE);
892 bus = gst_element_get_bus (pipeline);
893 gst_bus_add_signal_watch (bus);
894 g_signal_connect (bus, "message", (GCallback) _message_cb, loop);
896 fail_unless (gst_element_set_state (pipeline,
897 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS);
898 g_main_loop_run (loop);
900 fail_unless (gst_element_set_state (pipeline,
901 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS);
902 g_main_loop_unref (loop);
903 gst_object_unref (pipeline);
907 #define FFT_HELPERS(type,ffttag,ffttag2,scale); \
908 static gdouble magnitude##ffttag (const GstFFT##ffttag##Complex *c) \
910 gdouble mag = (gdouble) c->r * (gdouble) c->r; \
911 mag += (gdouble) c->i * (gdouble) c->i; \
912 mag /= scale * scale; \
913 mag = 10.0 * log10 (mag); \
916 static gdouble find_main_frequency_spot_##ffttag (const GstFFT##ffttag##Complex *v, \
920 gdouble maxmag = -9999; \
922 for (i=0; i<elements; ++i) { \
923 gdouble mag = magnitude##ffttag (v+i); \
924 if (mag > maxmag) { \
929 return maxidx / (gdouble) elements; \
931 static gboolean is_zero_except_##ffttag (const GstFFT##ffttag##Complex *v, int elements, \
935 for (i=0; i<elements; ++i) { \
936 gdouble pos = i / (gdouble) elements; \
937 gdouble mag = magnitude##ffttag (v+i); \
938 if (fabs (pos - spot) > 0.01) { \
946 static void compare_ffts_##ffttag (GstBuffer *inbuffer, GstBuffer *outbuffer) \
948 GstMapInfo inmap, outmap; \
949 int insamples, outsamples; \
950 gdouble inspot, outspot; \
951 GstFFT##ffttag *inctx, *outctx; \
952 GstFFT##ffttag##Complex *in, *out; \
954 gst_buffer_map (inbuffer, &inmap, GST_MAP_READ); \
955 gst_buffer_map (outbuffer, &outmap, GST_MAP_READWRITE); \
957 insamples = inmap.size / sizeof(type) & ~1; \
958 outsamples = outmap.size / sizeof(type) & ~1; \
959 inctx = gst_fft_##ffttag2##_new (insamples, FALSE); \
960 outctx = gst_fft_##ffttag2##_new (outsamples, FALSE); \
961 in = g_new (GstFFT##ffttag##Complex, insamples / 2 + 1); \
962 out = g_new (GstFFT##ffttag##Complex, outsamples / 2 + 1); \
964 gst_fft_##ffttag2##_window (inctx, (type*)inmap.data, \
965 GST_FFT_WINDOW_HAMMING); \
966 gst_fft_##ffttag2##_fft (inctx, (type*)inmap.data, in); \
967 gst_fft_##ffttag2##_window (outctx, (type*)outmap.data, \
968 GST_FFT_WINDOW_HAMMING); \
969 gst_fft_##ffttag2##_fft (outctx, (type*)outmap.data, out); \
971 inspot = find_main_frequency_spot_##ffttag (in, insamples / 2 + 1); \
972 outspot = find_main_frequency_spot_##ffttag (out, outsamples / 2 + 1); \
973 GST_LOG ("Spots are %.3f and %.3f", inspot, outspot); \
974 fail_unless (fabs (outspot - inspot) < 0.05); \
975 fail_unless (is_zero_except_##ffttag (in, insamples / 2 + 1, inspot)); \
976 fail_unless (is_zero_except_##ffttag (out, outsamples / 2 + 1, outspot)); \
978 gst_buffer_unmap (inbuffer, &inmap); \
979 gst_buffer_unmap (outbuffer, &outmap); \
981 gst_fft_##ffttag2##_free (inctx); \
982 gst_fft_##ffttag2##_free (outctx); \
986 FFT_HELPERS (float, F32, f32, 2048.0f);
987 FFT_HELPERS (double, F64, f64, 2048.0);
988 FFT_HELPERS (gint16, S16, s16, 32767.0);
989 FFT_HELPERS (gint32, S32, s32, 2147483647.0);
991 #define FILL_BUFFER(type, desc, value); \
992 static void init_##type##_##desc (GstBuffer *buffer) \
997 gst_buffer_map (buffer, &map, GST_MAP_WRITE); \
998 ptr = (type *)map.data; \
999 nsamples = map.size / sizeof (type); \
1000 for (i = 0; i < nsamples; ++i) { \
1005 FILL_BUFFER (float, silence, 0.0f);
1006 FILL_BUFFER (double, silence, 0.0);
1007 FILL_BUFFER (gint16, silence, 0);
1008 FILL_BUFFER (gint32, silence, 0);
1009 FILL_BUFFER (float, sine, sinf (i * 0.01f));
1010 FILL_BUFFER (float, sine2, sinf (i * 1.8f));
1011 FILL_BUFFER (double, sine, sin (i * 0.01));
1012 FILL_BUFFER (double, sine2, sin (i * 1.8));
1013 FILL_BUFFER (gint16, sine, (gint16) (32767 * sinf (i * 0.01f)));
1014 FILL_BUFFER (gint16, sine2, (gint16) (32767 * sinf (i * 1.8f)));
1015 FILL_BUFFER (gint32, sine, (gint32) (2147483647 * sinf (i * 0.01f)));
1016 FILL_BUFFER (gint32, sine2, (gint32) (2147483647 * sinf (i * 1.8f)));
1019 run_fft_pipeline (int inrate, int outrate, int quality, int width, gboolean fp,
1020 void (*init) (GstBuffer *), void (*compare_ffts) (GstBuffer *, GstBuffer *))
1022 GstElement *audioresample;
1023 GstBuffer *inbuffer, *outbuffer;
1025 const int nsamples = 2048;
1027 audioresample = setup_audioresample (1, inrate, outrate, width, fp);
1028 fail_unless (audioresample != NULL);
1029 g_object_set (audioresample, "quality", quality, NULL);
1030 caps = gst_pad_get_current_caps (mysrcpad);
1031 fail_unless (gst_caps_is_fixed (caps));
1033 fail_unless (gst_element_set_state (audioresample,
1034 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
1035 "could not set to playing");
1037 inbuffer = gst_buffer_new_and_alloc (nsamples * width / 8);
1038 GST_BUFFER_DURATION (inbuffer) = GST_FRAMES_TO_CLOCK_TIME (nsamples, inrate);
1039 GST_BUFFER_TIMESTAMP (inbuffer) = 0;
1040 gst_pad_set_caps (mysrcpad, caps);
1041 gst_buffer_ref (inbuffer);
1045 /* pushing gives away my reference ... */
1046 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
1047 /* ... but it ends up being collected on the global buffer list */
1048 fail_unless_equals_int (g_list_length (buffers), 1);
1049 /* retrieve out buffer */
1050 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
1052 fail_unless (gst_element_set_state (audioresample,
1053 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
1055 (*compare_ffts) (inbuffer, outbuffer);
1058 gst_buffer_unref (inbuffer);
1059 gst_caps_unref (caps);
1060 cleanup_audioresample (audioresample);
1063 GST_START_TEST (test_fft)
1067 static const int frequencies[] =
1068 { 8000, 16000, 44100, 48000, 128000, 12345, 54321 };
1070 /* audioresample uses a mixed float/double code path for floats with quality>8, make sure we test it */
1071 for (quality = 0; quality <= 10; quality += 5) {
1072 for (f0 = 0; f0 < G_N_ELEMENTS (frequencies); ++f0) {
1073 for (f1 = 0; f1 < G_N_ELEMENTS (frequencies); ++f1) {
1074 run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 32, TRUE,
1075 &init_float_silence, &compare_ffts_F32);
1076 run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 32, TRUE,
1077 &init_float_sine, &compare_ffts_F32);
1078 run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 32, TRUE,
1079 &init_float_sine2, &compare_ffts_F32);
1080 run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 64, TRUE,
1081 &init_double_silence, &compare_ffts_F64);
1082 run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 64, TRUE,
1083 &init_double_sine, &compare_ffts_F64);
1084 run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 64, TRUE,
1085 &init_double_sine2, &compare_ffts_F64);
1086 run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 16, FALSE,
1087 &init_gint16_silence, &compare_ffts_S16);
1088 run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 16, FALSE,
1089 &init_gint16_sine, &compare_ffts_S16);
1090 run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 16, FALSE,
1091 &init_gint16_sine2, &compare_ffts_S16);
1092 run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 32, FALSE,
1093 &init_gint32_silence, &compare_ffts_S32);
1094 run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 32, FALSE,
1095 &init_gint32_sine, &compare_ffts_S32);
1096 run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 32, FALSE,
1097 &init_gint32_sine2, &compare_ffts_S32);
1106 audioresample_suite (void)
1108 Suite *s = suite_create ("audioresample");
1109 TCase *tc_chain = tcase_create ("general");
1111 suite_add_tcase (s, tc_chain);
1112 tcase_add_test (tc_chain, test_perfect_stream);
1113 tcase_add_test (tc_chain, test_discont_stream);
1114 tcase_add_test (tc_chain, test_reuse);
1115 tcase_add_test (tc_chain, test_shutdown);
1116 tcase_add_test (tc_chain, test_live_switch);
1117 tcase_add_test (tc_chain, test_timestamp_drift);
1118 tcase_add_test (tc_chain, test_fft);
1120 #ifndef GST_DISABLE_PARSE
1121 tcase_set_timeout (tc_chain, 360);
1122 tcase_add_test (tc_chain, test_pipelines);
1123 tcase_add_test (tc_chain, test_preference_passthrough);
1129 GST_CHECK_MAIN (audioresample);