From 80c3258a6ac2c796df247dd372e94e3a765a93c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Wed, 29 Oct 2008 12:11:20 +0000 Subject: [PATCH] gst/speexresample/gstspeexresample.*: Rewrite timestamp tracking to make it more robust and guarantee a continous str... Original commit message from CVS: * gst/speexresample/gstspeexresample.c: (gst_speex_resample_start), (gst_speex_resample_get_unit_size), (gst_speex_resample_push_drain), (gst_speex_resample_event), (gst_speex_resample_check_discont), (gst_speex_resample_process), (gst_speex_resample_transform): * gst/speexresample/gstspeexresample.h: Rewrite timestamp tracking to make it more robust and guarantee a continous stream. * tests/check/Makefile.am: * tests/check/elements/speexresample.c: (setup_speexresample), (cleanup_speexresample), (fail_unless_perfect_stream), (test_perfect_stream_instance), (GST_START_TEST), (test_discont_stream_instance), (live_switch_alloc_only_48000), (live_switch_get_sink_caps), (live_switch_push), (speexresample_suite): Add unit tests for speexresample based on the audioresample unit tests. --- ChangeLog | 20 + gst/speexresample/gstspeexresample.c | 169 +++----- gst/speexresample/gstspeexresample.h | 5 +- tests/check/Makefile.am | 3 + tests/check/elements/speexresample.c | 579 +++++++++++++++++++++++++++ 5 files changed, 667 insertions(+), 109 deletions(-) create mode 100644 tests/check/elements/speexresample.c diff --git a/ChangeLog b/ChangeLog index 946fa6a379..241eb6451a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,23 @@ +2008-10-29 Sebastian Dröge + + * gst/speexresample/gstspeexresample.c: (gst_speex_resample_start), + (gst_speex_resample_get_unit_size), + (gst_speex_resample_push_drain), (gst_speex_resample_event), + (gst_speex_resample_check_discont), (gst_speex_resample_process), + (gst_speex_resample_transform): + * gst/speexresample/gstspeexresample.h: + Rewrite timestamp tracking to make it more robust and guarantee + a continous stream. + + * tests/check/Makefile.am: + * tests/check/elements/speexresample.c: (setup_speexresample), + (cleanup_speexresample), (fail_unless_perfect_stream), + (test_perfect_stream_instance), (GST_START_TEST), + (test_discont_stream_instance), (live_switch_alloc_only_48000), + (live_switch_get_sink_caps), (live_switch_push), + (speexresample_suite): + Add unit tests for speexresample based on the audioresample unit tests. + 2008-10-29 Jan Schmidt * ext/resindvd/resindvdsrc.c: diff --git a/gst/speexresample/gstspeexresample.c b/gst/speexresample/gstspeexresample.c index 240d8e5d9e..f9b5b0e365 100644 --- a/gst/speexresample/gstspeexresample.c +++ b/gst/speexresample/gstspeexresample.c @@ -183,9 +183,9 @@ gst_speex_resample_start (GstBaseTransform * base) { GstSpeexResample *resample = GST_SPEEX_RESAMPLE (base); - resample->ts_offset = -1; - resample->offset = -1; + resample->next_offset = -1; resample->next_ts = -1; + resample->next_upstream_ts = -1; return TRUE; } @@ -224,7 +224,7 @@ gst_speex_resample_get_unit_size (GstBaseTransform * base, GstCaps * caps, if (G_UNLIKELY (!ret)) return FALSE; - *size = width * channels / 8; + *size = gst_util_uint64_scale (width, channels, 8); return TRUE; } @@ -595,26 +595,18 @@ gst_speex_resample_push_drain (GstSpeexResample * resample) return; } - GST_BUFFER_OFFSET (buf) = resample->offset; - GST_BUFFER_TIMESTAMP (buf) = resample->next_ts; + GST_BUFFER_DURATION (buf) = + GST_FRAMES_TO_CLOCK_TIME (out_processed, resample->outrate); GST_BUFFER_SIZE (buf) = out_processed * resample->channels * ((resample->fp) ? 4 : 2); - if (resample->ts_offset != -1) { - resample->offset += out_processed; - resample->ts_offset += out_processed; - resample->next_ts = - GST_FRAMES_TO_CLOCK_TIME (resample->ts_offset, resample->outrate); - GST_BUFFER_OFFSET_END (buf) = resample->offset; + if (GST_CLOCK_TIME_IS_VALID (resample->next_ts)) { + GST_BUFFER_OFFSET (buf) = resample->next_offset; + GST_BUFFER_OFFSET_END (buf) = resample->next_offset + out_processed; + GST_BUFFER_TIMESTAMP (buf) = resample->next_ts; - /* we calculate DURATION as the difference between "next" timestamp - * and current timestamp so we ensure a contiguous stream, instead of - * having rounding errors. */ - GST_BUFFER_DURATION (buf) = resample->next_ts - GST_BUFFER_TIMESTAMP (buf); - } else { - /* no valid offset know, we can still sortof calculate the duration though */ - GST_BUFFER_DURATION (buf) = - GST_FRAMES_TO_CLOCK_TIME (out_processed, resample->outrate); + resample->next_ts += GST_BUFFER_DURATION (buf); + resample->next_offset += out_processed; } GST_LOG_OBJECT (resample, @@ -644,15 +636,15 @@ gst_speex_resample_event (GstBaseTransform * base, GstEvent * event) break; case GST_EVENT_FLUSH_STOP: gst_speex_resample_reset_state (resample); - resample->ts_offset = -1; + resample->next_offset = -1; resample->next_ts = -1; - resample->offset = -1; + resample->next_upstream_ts = -1; case GST_EVENT_NEWSEGMENT: gst_speex_resample_push_drain (resample); gst_speex_resample_reset_state (resample); - resample->ts_offset = -1; + resample->next_offset = -1; resample->next_ts = -1; - resample->offset = -1; + resample->next_upstream_ts = -1; break; case GST_EVENT_EOS:{ gst_speex_resample_push_drain (resample); @@ -671,19 +663,18 @@ gst_speex_resample_check_discont (GstSpeexResample * resample, GstClockTime timestamp) { if (timestamp != GST_CLOCK_TIME_NONE && - resample->prev_ts != GST_CLOCK_TIME_NONE && - resample->prev_duration != GST_CLOCK_TIME_NONE && - timestamp != resample->prev_ts + resample->prev_duration) { + resample->next_upstream_ts != GST_CLOCK_TIME_NONE && + timestamp != resample->next_upstream_ts) { /* Potentially a discontinuous buffer. However, it turns out that many * elements generate imperfect streams due to rounding errors, so we permit * a small error (up to one sample) without triggering a filter * flush/restart (if triggered incorrectly, this will be audible) */ - GstClockTimeDiff diff = timestamp - - (resample->prev_ts + resample->prev_duration); + GstClockTimeDiff diff = timestamp - resample->next_upstream_ts; - if (ABS (diff) > GST_SECOND / resample->inrate) { + if (ABS (diff) > (GST_SECOND + resample->inrate - 1) / resample->inrate) { GST_WARNING_OBJECT (resample, - "encountered timestamp discontinuity of %" G_GINT64_FORMAT, diff); + "encountered timestamp discontinuity of %s%" GST_TIME_FORMAT, + (diff < 0) ? "-" : "", ABS (diff)); return TRUE; } } @@ -691,27 +682,6 @@ gst_speex_resample_check_discont (GstSpeexResample * resample, return FALSE; } -static void -gst_speex_fix_output_buffer (GstSpeexResample * resample, GstBuffer * outbuf, - guint diff) -{ - GstClockTime timediff = GST_FRAMES_TO_CLOCK_TIME (diff, resample->outrate); - - GST_LOG_OBJECT (resample, "Adjusting buffer by %d samples", diff); - - GST_BUFFER_DURATION (outbuf) -= timediff; - GST_BUFFER_SIZE (outbuf) -= - diff * ((resample->fp) ? 4 : 2) * resample->channels; - - if (resample->ts_offset != -1) { - GST_BUFFER_OFFSET_END (outbuf) -= diff; - resample->offset -= diff; - resample->ts_offset -= diff; - resample->next_ts = - GST_FRAMES_TO_CLOCK_TIME (resample->ts_offset, resample->outrate); - } -} - static GstFlowReturn gst_speex_resample_process (GstSpeexResample * resample, GstBuffer * inbuf, GstBuffer * outbuf) @@ -753,14 +723,6 @@ gst_speex_resample_process (GstSpeexResample * resample, GstBuffer * inbuf, if (out_processed == 0) { GST_DEBUG_OBJECT (resample, "Converted to 0 samples, buffer dropped"); - if (resample->ts_offset != -1) { - GST_BUFFER_OFFSET_END (outbuf) -= out_len; - resample->offset -= out_len; - resample->ts_offset -= out_len; - resample->next_ts = - GST_FRAMES_TO_CLOCK_TIME (resample->ts_offset, resample->outrate); - } - return GST_BASE_TRANSFORM_FLOW_DROPPED; } else if (out_len - out_processed != 1) { GST_WARNING_OBJECT (resample, @@ -768,9 +730,7 @@ gst_speex_resample_process (GstSpeexResample * resample, GstBuffer * inbuf, out_len); } - if (G_LIKELY (out_len > out_processed)) { - gst_speex_fix_output_buffer (resample, outbuf, out_len - out_processed); - } else { + if (G_UNLIKELY (out_len < out_processed)) { GST_ERROR_OBJECT (resample, "Wrote more output than allocated!"); return GST_FLOW_ERROR; } @@ -781,6 +741,20 @@ gst_speex_resample_process (GstSpeexResample * resample, GstBuffer * inbuf, resample_resampler_strerror (err)); return GST_FLOW_ERROR; } else { + GST_BUFFER_DURATION (outbuf) = + GST_FRAMES_TO_CLOCK_TIME (out_processed, resample->outrate); + GST_BUFFER_SIZE (outbuf) = + out_processed * resample->channels * ((resample->fp) ? 4 : 2); + + if (GST_CLOCK_TIME_IS_VALID (resample->next_ts)) { + GST_BUFFER_TIMESTAMP (outbuf) = resample->next_ts; + GST_BUFFER_OFFSET (outbuf) = resample->next_offset; + GST_BUFFER_OFFSET_END (outbuf) = resample->next_offset + out_processed; + + resample->next_ts += GST_BUFFER_DURATION (outbuf); + resample->next_offset += out_processed; + } + GST_LOG_OBJECT (resample, "Converted to buffer of %u bytes with timestamp %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT ", offset %" G_GUINT64_FORMAT @@ -801,7 +775,8 @@ gst_speex_resample_transform (GstBaseTransform * base, GstBuffer * inbuf, guint8 *data; gulong size; GstClockTime timestamp; - gint outsamples; + guint outsamples, insamples; + GstFlowReturn ret; if (resample->state == NULL) if (G_UNLIKELY (!(resample->state = @@ -828,53 +803,23 @@ gst_speex_resample_transform (GstBaseTransform * base, GstBuffer * inbuf, gst_speex_resample_reset_state (resample); /* Inform downstream element about discontinuity */ resample->need_discont = TRUE; - /* We want to recalculate the offset */ - resample->ts_offset = -1; + /* We want to recalculate the timestamps */ + resample->next_ts = -1; + resample->next_upstream_ts = -1; + resample->next_offset = -1; } + insamples = GST_BUFFER_SIZE (inbuf) / resample->channels; + insamples /= (resample->fp) ? 4 : 2; + outsamples = GST_BUFFER_SIZE (outbuf) / resample->channels; outsamples /= (resample->fp) ? 4 : 2; - if (resample->ts_offset == -1) { - /* if we don't know the initial offset yet, calculate it based on the - * input timestamp. */ - if (GST_CLOCK_TIME_IS_VALID (timestamp)) { - GstClockTime stime; - - /* offset used to calculate the timestamps. We use the sample offset for - * this to make it more accurate. We want the first buffer to have the - * same timestamp as the incoming timestamp. */ - resample->next_ts = timestamp; - resample->ts_offset = - GST_CLOCK_TIME_TO_FRAMES (timestamp, resample->outrate); - /* offset used to set as the buffer offset, this offset is always - * relative to the stream time, note that timestamp is not... */ - stime = (timestamp - base->segment.start) + base->segment.time; - resample->offset = GST_CLOCK_TIME_TO_FRAMES (stime, resample->outrate); - } - } - resample->prev_ts = timestamp; - resample->prev_duration = GST_BUFFER_DURATION (inbuf); - - GST_BUFFER_OFFSET (outbuf) = resample->offset; - GST_BUFFER_TIMESTAMP (outbuf) = resample->next_ts; - - if (resample->ts_offset != -1) { - resample->offset += outsamples; - resample->ts_offset += outsamples; - resample->next_ts = - GST_FRAMES_TO_CLOCK_TIME (resample->ts_offset, resample->outrate); - GST_BUFFER_OFFSET_END (outbuf) = resample->offset; - - /* we calculate DURATION as the difference between "next" timestamp - * and current timestamp so we ensure a contiguous stream, instead of - * having rounding errors. */ - GST_BUFFER_DURATION (outbuf) = resample->next_ts - - GST_BUFFER_TIMESTAMP (outbuf); - } else { - /* no valid offset know, we can still sortof calculate the duration though */ - GST_BUFFER_DURATION (outbuf) = - GST_FRAMES_TO_CLOCK_TIME (outsamples, resample->outrate); + if (GST_CLOCK_TIME_IS_VALID (timestamp) + && !GST_CLOCK_TIME_IS_VALID (resample->next_ts)) { + resample->next_ts = timestamp; + resample->next_offset = + GST_CLOCK_TIME_TO_FRAMES (timestamp, resample->outrate); } if (G_UNLIKELY (resample->need_discont)) { @@ -883,7 +828,19 @@ gst_speex_resample_transform (GstBaseTransform * base, GstBuffer * inbuf, resample->need_discont = FALSE; } - return gst_speex_resample_process (resample, inbuf, outbuf); + ret = gst_speex_resample_process (resample, inbuf, outbuf); + if (G_UNLIKELY (ret != GST_FLOW_OK)) + return ret; + + if (GST_CLOCK_TIME_IS_VALID (timestamp) + && !GST_CLOCK_TIME_IS_VALID (resample->next_upstream_ts)) + resample->next_upstream_ts = timestamp; + + if (GST_CLOCK_TIME_IS_VALID (resample->next_upstream_ts)) + resample->next_upstream_ts += + GST_FRAMES_TO_CLOCK_TIME (insamples, resample->inrate); + + return GST_FLOW_OK; } static gboolean diff --git a/gst/speexresample/gstspeexresample.h b/gst/speexresample/gstspeexresample.h index 8d1413c96e..7e7a68f652 100644 --- a/gst/speexresample/gstspeexresample.h +++ b/gst/speexresample/gstspeexresample.h @@ -57,10 +57,9 @@ struct _GstSpeexResample { gboolean need_discont; - guint64 offset; - guint64 ts_offset; + guint64 next_offset; GstClockTime next_ts; - GstClockTime prev_ts, prev_duration; + GstClockTime next_upstream_ts; gboolean fp; gint channels; diff --git a/tests/check/Makefile.am b/tests/check/Makefile.am index 9e981dc984..9bac60947a 100644 --- a/tests/check/Makefile.am +++ b/tests/check/Makefile.am @@ -88,6 +88,7 @@ check_PROGRAMS = \ $(check_timidity) \ $(check_x264enc) \ elements/selector \ + elements/speexresample \ elements/y4menc \ $(check_metadata) @@ -101,3 +102,5 @@ LDADD = $(GST_OBJ_LIBS) $(GST_CHECK_LIBS) $(CHECK_LIBS) elements_timidity_CFLAGS = $(GST_BASE_CFLAGS) $(AM_CFLAGS) elements_timidity_LDADD = $(GST_BASE_LIBS) $(LDADD) +elements_speexresample_CFLAGS = $(GST_PLUGINS_BASE_CFLAGS) $(AM_CFLAGS) +elements_speexresample_LDADD = $(GST_PLUGINS_BASE_LIBS) $(LDADD) -lgstaudio-$(GST_MAJORMINOR) diff --git a/tests/check/elements/speexresample.c b/tests/check/elements/speexresample.c new file mode 100644 index 0000000000..a78ada6d1f --- /dev/null +++ b/tests/check/elements/speexresample.c @@ -0,0 +1,579 @@ +/* GStreamer + * + * unit test for speexresample, based on the audioresample unit test + * + * Copyright (C) <2005> Thomas Vander Stichele + * Copyright (C) <2006> Tim-Philipp Müller + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include + +#include + +#include + +/* For ease of programming we use globals to keep refs for our floating + * src and sink pads we create; otherwise we always have to do get_pad, + * get_peer, and then remove references in every test function */ +static GstPad *mysrcpad, *mysinkpad; + + +#define RESAMPLE_CAPS_TEMPLATE_STRING \ + "audio/x-raw-int, " \ + "channels = (int) [ 1, MAX ], " \ + "rate = (int) [ 1, MAX ], " \ + "endianness = (int) BYTE_ORDER, " \ + "width = (int) 16, " \ + "depth = (int) 16, " \ + "signed = (bool) TRUE" + +static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS (RESAMPLE_CAPS_TEMPLATE_STRING) + ); +static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS (RESAMPLE_CAPS_TEMPLATE_STRING) + ); + +static GstElement * +setup_speexresample (int channels, int inrate, int outrate) +{ + GstElement *speexresample; + GstCaps *caps; + GstStructure *structure; + + GST_DEBUG ("setup_speexresample"); + speexresample = gst_check_setup_element ("speexresample"); + + caps = gst_caps_from_string (RESAMPLE_CAPS_TEMPLATE_STRING); + structure = gst_caps_get_structure (caps, 0); + gst_structure_set (structure, "channels", G_TYPE_INT, channels, + "rate", G_TYPE_INT, inrate, NULL); + fail_unless (gst_caps_is_fixed (caps)); + + fail_unless (gst_element_set_state (speexresample, + GST_STATE_PAUSED) == GST_STATE_CHANGE_SUCCESS, + "could not set to paused"); + + mysrcpad = gst_check_setup_src_pad (speexresample, &srctemplate, caps); + gst_pad_set_caps (mysrcpad, caps); + gst_caps_unref (caps); + + caps = gst_caps_from_string (RESAMPLE_CAPS_TEMPLATE_STRING); + structure = gst_caps_get_structure (caps, 0); + gst_structure_set (structure, "channels", G_TYPE_INT, channels, + "rate", G_TYPE_INT, outrate, NULL); + fail_unless (gst_caps_is_fixed (caps)); + + mysinkpad = gst_check_setup_sink_pad (speexresample, &sinktemplate, caps); + /* this installs a getcaps func that will always return the caps we set + * later */ + gst_pad_set_caps (mysinkpad, caps); + gst_pad_use_fixed_caps (mysinkpad); + + gst_pad_set_active (mysinkpad, TRUE); + gst_pad_set_active (mysrcpad, TRUE); + + gst_caps_unref (caps); + + return speexresample; +} + +static void +cleanup_speexresample (GstElement * speexresample) +{ + GST_DEBUG ("cleanup_speexresample"); + + fail_unless (gst_element_set_state (speexresample, + GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to NULL"); + + gst_pad_set_active (mysrcpad, FALSE); + gst_pad_set_active (mysinkpad, FALSE); + gst_check_teardown_src_pad (speexresample); + gst_check_teardown_sink_pad (speexresample); + gst_check_teardown_element (speexresample); +} + +static void +fail_unless_perfect_stream (void) +{ + guint64 timestamp = 0L, duration = 0L; + guint64 offset = 0L, offset_end = 0L; + + GList *l; + GstBuffer *buffer; + + for (l = buffers; l; l = l->next) { + buffer = GST_BUFFER (l->data); + ASSERT_BUFFER_REFCOUNT (buffer, "buffer", 1); + GST_DEBUG ("buffer timestamp %" G_GUINT64_FORMAT ", duration %" + G_GUINT64_FORMAT " offset %" G_GUINT64_FORMAT " offset_end %" + G_GUINT64_FORMAT, + GST_BUFFER_TIMESTAMP (buffer), + GST_BUFFER_DURATION (buffer), + GST_BUFFER_OFFSET (buffer), GST_BUFFER_OFFSET_END (buffer)); + + fail_unless_equals_uint64 (timestamp, GST_BUFFER_TIMESTAMP (buffer)); + fail_unless_equals_uint64 (offset, GST_BUFFER_OFFSET (buffer)); + duration = GST_BUFFER_DURATION (buffer); + offset_end = GST_BUFFER_OFFSET_END (buffer); + + timestamp += duration; + offset = offset_end; + gst_buffer_unref (buffer); + } + g_list_free (buffers); + buffers = NULL; +} + +/* this tests that the output is a perfect stream if the input is */ +static void +test_perfect_stream_instance (int inrate, int outrate, int samples, + int numbuffers) +{ + GstElement *speexresample; + GstBuffer *inbuffer, *outbuffer; + GstCaps *caps; + guint64 offset = 0; + + int i, j; + gint16 *p; + + speexresample = setup_speexresample (2, inrate, outrate); + caps = gst_pad_get_negotiated_caps (mysrcpad); + fail_unless (gst_caps_is_fixed (caps)); + + fail_unless (gst_element_set_state (speexresample, + GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS, + "could not set to playing"); + + for (j = 1; j <= numbuffers; ++j) { + + inbuffer = gst_buffer_new_and_alloc (samples * 4); + GST_BUFFER_DURATION (inbuffer) = GST_FRAMES_TO_CLOCK_TIME (samples, inrate); + GST_BUFFER_TIMESTAMP (inbuffer) = GST_BUFFER_DURATION (inbuffer) * (j - 1); + GST_BUFFER_OFFSET (inbuffer) = offset; + offset += samples; + GST_BUFFER_OFFSET_END (inbuffer) = offset; + + gst_buffer_set_caps (inbuffer, caps); + + p = (gint16 *) GST_BUFFER_DATA (inbuffer); + + /* create a 16 bit signed ramp */ + for (i = 0; i < samples; ++i) { + *p = -32767 + i * (65535 / samples); + ++p; + *p = -32767 + i * (65535 / samples); + ++p; + } + + /* pushing gives away my reference ... */ + fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK); + /* ... but it ends up being collected on the global buffer list */ + fail_unless_equals_int (g_list_length (buffers), j); + } + + /* FIXME: we should make speexresample handle eos by flushing out the last + * samples, which will give us one more, small, buffer */ + fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL); + ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1); + + fail_unless_perfect_stream (); + + /* cleanup */ + gst_caps_unref (caps); + cleanup_speexresample (speexresample); +} + + +/* make sure that outgoing buffers are contiguous in timestamp/duration and + * offset/offsetend + */ +GST_START_TEST (test_perfect_stream) +{ + /* integral scalings */ + test_perfect_stream_instance (48000, 24000, 500, 20); +#if 0 + test_perfect_stream_instance (48000, 12000, 500, 20); + test_perfect_stream_instance (12000, 24000, 500, 20); + test_perfect_stream_instance (12000, 48000, 500, 20); + + /* non-integral scalings */ + test_perfect_stream_instance (44100, 8000, 500, 20); + test_perfect_stream_instance (8000, 44100, 500, 20); + + /* wacky scalings */ + test_perfect_stream_instance (12345, 54321, 500, 20); + test_perfect_stream_instance (101, 99, 500, 20); +#endif +} + +GST_END_TEST; + +/* this tests that the output is a correct discontinuous stream + * if the input is; ie input drops in time come out the same way */ +static void +test_discont_stream_instance (int inrate, int outrate, int samples, + int numbuffers) +{ + GstElement *speexresample; + GstBuffer *inbuffer, *outbuffer; + GstCaps *caps; + GstClockTime ints; + + int i, j; + gint16 *p; + + GST_DEBUG ("inrate:%d outrate:%d samples:%d numbuffers:%d", + inrate, outrate, samples, numbuffers); + + speexresample = setup_speexresample (2, inrate, outrate); + caps = gst_pad_get_negotiated_caps (mysrcpad); + fail_unless (gst_caps_is_fixed (caps)); + + fail_unless (gst_element_set_state (speexresample, + GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS, + "could not set to playing"); + + for (j = 1; j <= numbuffers; ++j) { + + inbuffer = gst_buffer_new_and_alloc (samples * 4); + GST_BUFFER_DURATION (inbuffer) = samples * GST_SECOND / inrate; + /* "drop" half the buffers */ + ints = GST_BUFFER_DURATION (inbuffer) * 2 * (j - 1); + GST_BUFFER_TIMESTAMP (inbuffer) = ints; + GST_BUFFER_OFFSET (inbuffer) = (j - 1) * 2 * samples; + GST_BUFFER_OFFSET_END (inbuffer) = j * 2 * samples + samples; + + gst_buffer_set_caps (inbuffer, caps); + + p = (gint16 *) GST_BUFFER_DATA (inbuffer); + + /* create a 16 bit signed ramp */ + for (i = 0; i < samples; ++i) { + *p = -32767 + i * (65535 / samples); + ++p; + *p = -32767 + i * (65535 / samples); + ++p; + } + + GST_DEBUG ("Sending Buffer time:%" G_GUINT64_FORMAT " duration:%" + G_GINT64_FORMAT " discont:%d offset:%" G_GUINT64_FORMAT " offset_end:%" + G_GUINT64_FORMAT, GST_BUFFER_TIMESTAMP (inbuffer), + GST_BUFFER_DURATION (inbuffer), GST_BUFFER_IS_DISCONT (inbuffer), + GST_BUFFER_OFFSET (inbuffer), GST_BUFFER_OFFSET_END (inbuffer)); + /* pushing gives away my reference ... */ + fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK); + + /* check if the timestamp of the pushed buffer matches the incoming one */ + outbuffer = g_list_nth_data (buffers, g_list_length (buffers) - 1); + fail_if (outbuffer == NULL); + fail_unless_equals_uint64 (ints, GST_BUFFER_TIMESTAMP (outbuffer)); + GST_DEBUG ("Got Buffer time:%" G_GUINT64_FORMAT " duration:%" + G_GINT64_FORMAT " discont:%d offset:%" G_GUINT64_FORMAT " offset_end:%" + G_GUINT64_FORMAT, GST_BUFFER_TIMESTAMP (outbuffer), + GST_BUFFER_DURATION (outbuffer), GST_BUFFER_IS_DISCONT (outbuffer), + GST_BUFFER_OFFSET (outbuffer), GST_BUFFER_OFFSET_END (outbuffer)); + if (j > 1) { + fail_unless (GST_BUFFER_IS_DISCONT (outbuffer), + "expected discont for buffer #%d", j); + } + } + + /* cleanup */ + gst_caps_unref (caps); + cleanup_speexresample (speexresample); +} + +GST_START_TEST (test_discont_stream) +{ + /* integral scalings */ + test_discont_stream_instance (48000, 24000, 500, 20); + test_discont_stream_instance (48000, 12000, 500, 20); + test_discont_stream_instance (12000, 24000, 500, 20); + test_discont_stream_instance (12000, 48000, 500, 20); + + /* non-integral scalings */ + test_discont_stream_instance (44100, 8000, 500, 20); + test_discont_stream_instance (8000, 44100, 500, 20); + + /* wacky scalings */ + test_discont_stream_instance (12345, 54321, 500, 20); + test_discont_stream_instance (101, 99, 500, 20); +} + +GST_END_TEST; + + + +GST_START_TEST (test_reuse) +{ + GstElement *speexresample; + GstEvent *newseg; + GstBuffer *inbuffer; + GstCaps *caps; + + speexresample = setup_speexresample (1, 9343, 48000); + caps = gst_pad_get_negotiated_caps (mysrcpad); + fail_unless (gst_caps_is_fixed (caps)); + + fail_unless (gst_element_set_state (speexresample, + GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS, + "could not set to playing"); + + newseg = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME, 0, -1, 0); + fail_unless (gst_pad_push_event (mysrcpad, newseg) != FALSE); + + inbuffer = gst_buffer_new_and_alloc (9343 * 4); + memset (GST_BUFFER_DATA (inbuffer), 0, GST_BUFFER_SIZE (inbuffer)); + GST_BUFFER_DURATION (inbuffer) = GST_SECOND; + GST_BUFFER_TIMESTAMP (inbuffer) = 0; + GST_BUFFER_OFFSET (inbuffer) = 0; + gst_buffer_set_caps (inbuffer, caps); + + /* pushing gives away my reference ... */ + fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK); + + /* ... but it ends up being collected on the global buffer list */ + fail_unless_equals_int (g_list_length (buffers), 1); + + /* now reset and try again ... */ + fail_unless (gst_element_set_state (speexresample, + GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to NULL"); + + fail_unless (gst_element_set_state (speexresample, + GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS, + "could not set to playing"); + + newseg = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME, 0, -1, 0); + fail_unless (gst_pad_push_event (mysrcpad, newseg) != FALSE); + + inbuffer = gst_buffer_new_and_alloc (9343 * 4); + memset (GST_BUFFER_DATA (inbuffer), 0, GST_BUFFER_SIZE (inbuffer)); + GST_BUFFER_DURATION (inbuffer) = GST_SECOND; + GST_BUFFER_TIMESTAMP (inbuffer) = 0; + GST_BUFFER_OFFSET (inbuffer) = 0; + gst_buffer_set_caps (inbuffer, caps); + + fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK); + + /* ... it also ends up being collected on the global buffer list. If we + * now have more than 2 buffers, then speexresample probably didn't clean + * up its internal buffer properly and tried to push the remaining samples + * when it got the second NEWSEGMENT event */ + fail_unless_equals_int (g_list_length (buffers), 2); + + cleanup_speexresample (speexresample); + gst_caps_unref (caps); +} + +GST_END_TEST; + +GST_START_TEST (test_shutdown) +{ + GstElement *pipeline, *src, *cf1, *ar, *cf2, *sink; + GstCaps *caps; + guint i; + + /* create pipeline, force speexresample to actually resample */ + pipeline = gst_pipeline_new (NULL); + + src = gst_check_setup_element ("audiotestsrc"); + cf1 = gst_check_setup_element ("capsfilter"); + ar = gst_check_setup_element ("speexresample"); + cf2 = gst_check_setup_element ("capsfilter"); + g_object_set (cf2, "name", "capsfilter2", NULL); + sink = gst_check_setup_element ("fakesink"); + + caps = + gst_caps_new_simple ("audio/x-raw-int", "rate", G_TYPE_INT, 11025, NULL); + g_object_set (cf1, "caps", caps, NULL); + gst_caps_unref (caps); + + caps = + gst_caps_new_simple ("audio/x-raw-int", "rate", G_TYPE_INT, 48000, NULL); + g_object_set (cf2, "caps", caps, NULL); + gst_caps_unref (caps); + + /* don't want to sync against the clock, the more throughput the better */ + g_object_set (src, "is-live", FALSE, NULL); + g_object_set (sink, "sync", FALSE, NULL); + + gst_bin_add_many (GST_BIN (pipeline), src, cf1, ar, cf2, sink, NULL); + fail_if (!gst_element_link_many (src, cf1, ar, cf2, sink, NULL)); + + /* now, wait until pipeline is running and then shut it down again; repeat */ + for (i = 0; i < 20; ++i) { + gst_element_set_state (pipeline, GST_STATE_PAUSED); + gst_element_get_state (pipeline, NULL, NULL, -1); + gst_element_set_state (pipeline, GST_STATE_PLAYING); + g_usleep (100); + gst_element_set_state (pipeline, GST_STATE_NULL); + } + + gst_object_unref (pipeline); +} + +GST_END_TEST; + +static GstFlowReturn +live_switch_alloc_only_48000 (GstPad * pad, guint64 offset, + guint size, GstCaps * caps, GstBuffer ** buf) +{ + GstStructure *structure; + gint rate; + gint channels; + GstCaps *desired; + + structure = gst_caps_get_structure (caps, 0); + fail_unless (gst_structure_get_int (structure, "rate", &rate)); + fail_unless (gst_structure_get_int (structure, "channels", &channels)); + + if (rate < 48000) + return GST_FLOW_NOT_NEGOTIATED; + + desired = gst_caps_copy (caps); + gst_caps_set_simple (desired, "rate", G_TYPE_INT, 48000, NULL); + + *buf = gst_buffer_new_and_alloc (channels * 48000); + gst_buffer_set_caps (*buf, desired); + gst_caps_unref (desired); + + return GST_FLOW_OK; +} + +static GstCaps * +live_switch_get_sink_caps (GstPad * pad) +{ + GstCaps *result; + + result = gst_caps_copy (GST_PAD_CAPS (pad)); + + gst_caps_set_simple (result, + "rate", GST_TYPE_INT_RANGE, 48000, G_MAXINT, NULL); + + return result; +} + +static void +live_switch_push (int rate, GstCaps * caps) +{ + GstBuffer *inbuffer; + GstCaps *desired; + GList *l; + + desired = gst_caps_copy (caps); + gst_caps_set_simple (desired, "rate", G_TYPE_INT, rate, NULL); + gst_pad_set_caps (mysrcpad, desired); + + fail_unless (gst_pad_alloc_buffer_and_set_caps (mysrcpad, + GST_BUFFER_OFFSET_NONE, rate * 4, desired, &inbuffer) == GST_FLOW_OK); + + /* When the basetransform hits the non-configured case it always + * returns a buffer with exactly the same caps as we requested so the actual + * renegotiation (if needed) will be done in the _chain*/ + fail_unless (inbuffer != NULL); + GST_DEBUG ("desired: %" GST_PTR_FORMAT ".... got: %" GST_PTR_FORMAT, + desired, GST_BUFFER_CAPS (inbuffer)); + fail_unless (gst_caps_is_equal (desired, GST_BUFFER_CAPS (inbuffer))); + + memset (GST_BUFFER_DATA (inbuffer), 0, GST_BUFFER_SIZE (inbuffer)); + GST_BUFFER_DURATION (inbuffer) = GST_SECOND; + GST_BUFFER_TIMESTAMP (inbuffer) = 0; + GST_BUFFER_OFFSET (inbuffer) = 0; + + /* pushing gives away my reference ... */ + fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK); + + /* ... but it ends up being collected on the global buffer list */ + fail_unless_equals_int (g_list_length (buffers), 1); + + for (l = buffers; l; l = l->next) { + GstBuffer *buffer = GST_BUFFER (l->data); + + gst_buffer_unref (buffer); + } + + g_list_free (buffers); + buffers = NULL; + + gst_caps_unref (desired); +} + +GST_START_TEST (test_live_switch) +{ + GstElement *speexresample; + GstEvent *newseg; + GstCaps *caps; + + speexresample = setup_speexresample (4, 48000, 48000); + + /* Let the sinkpad act like something that can only handle things of + * rate 48000- and can only allocate buffers for that rate, but if someone + * tries to get a buffer with a rate higher then 48000 tries to renegotiate + * */ + gst_pad_set_bufferalloc_function (mysinkpad, live_switch_alloc_only_48000); + gst_pad_set_getcaps_function (mysinkpad, live_switch_get_sink_caps); + + gst_pad_use_fixed_caps (mysrcpad); + + caps = gst_pad_get_negotiated_caps (mysrcpad); + fail_unless (gst_caps_is_fixed (caps)); + + fail_unless (gst_element_set_state (speexresample, + GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS, + "could not set to playing"); + + newseg = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME, 0, -1, 0); + fail_unless (gst_pad_push_event (mysrcpad, newseg) != FALSE); + + /* downstream can provide the requested rate, a buffer alloc will be passed + * on */ + live_switch_push (48000, caps); + + /* Downstream can never accept this rate, buffer alloc isn't passed on */ + live_switch_push (40000, caps); + + /* Downstream can provide the requested rate but will re-negotiate */ + live_switch_push (50000, caps); + + cleanup_speexresample (speexresample); + gst_caps_unref (caps); +} + +GST_END_TEST static Suite * +speexresample_suite (void) +{ + Suite *s = suite_create ("speexresample"); + TCase *tc_chain = tcase_create ("general"); + + suite_add_tcase (s, tc_chain); + tcase_add_test (tc_chain, test_perfect_stream); + tcase_add_test (tc_chain, test_discont_stream); + tcase_add_test (tc_chain, test_reuse); + tcase_add_test (tc_chain, test_shutdown); + tcase_add_test (tc_chain, test_live_switch); + + return s; +} + +GST_CHECK_MAIN (speexresample);