From 3d95afd88998b3aa8d26fbf5252ff7cbb46900da Mon Sep 17 00:00:00 2001 From: Thomas Vander Stichele Date: Thu, 25 Aug 2005 15:44:58 +0000 Subject: [PATCH] add a check for audioresample Original commit message from CVS: add a check for audioresample --- gst/audioresample/gstaudioresample.c | 12 +- gst/audioresample/resample_ref.c | 3 + tests/check/elements/audioresample.c | 227 +++++++++++++++++++++++++++ 3 files changed, 240 insertions(+), 2 deletions(-) create mode 100644 tests/check/elements/audioresample.c diff --git a/gst/audioresample/gstaudioresample.c b/gst/audioresample/gstaudioresample.c index 2aba0928c6..cbbfe81a3a 100644 --- a/gst/audioresample/gstaudioresample.c +++ b/gst/audioresample/gstaudioresample.c @@ -353,6 +353,7 @@ static GstFlowReturn guchar *data; gulong size; int outsize; + int outsamples; /* FIXME: move to _inplace */ #if 0 @@ -390,10 +391,17 @@ static GstFlowReturn } outsize = resample_get_output_data (r, GST_BUFFER_DATA (outbuf), outsize); + outsamples = outsize / r->sample_size; + GST_LOG_OBJECT (audioresample, "resample gave me %d bytes or %d samples", + outsize, outsamples); + GST_BUFFER_TIMESTAMP (outbuf) = audioresample->offset * GST_SECOND / audioresample->o_rate; - audioresample->offset += outsize / sizeof (gint16) / audioresample->channels; - GST_BUFFER_DURATION (outbuf) = outsize * GST_SECOND / audioresample->o_rate; + GST_BUFFER_DURATION (outbuf) = + outsamples * GST_SECOND / audioresample->o_rate; + GST_BUFFER_OFFSET (outbuf) = audioresample->offset; + audioresample->offset += outsamples; + GST_BUFFER_OFFSET_END (outbuf) = audioresample->offset; /* check for possible mem corruption */ if (outsize > GST_BUFFER_SIZE (outbuf)) { diff --git a/gst/audioresample/resample_ref.c b/gst/audioresample/resample_ref.c index 187f72c4e5..4cb3d2502d 100644 --- a/gst/audioresample/resample_ref.c +++ b/gst/audioresample/resample_ref.c @@ -111,6 +111,9 @@ resample_scale_ref (ResampleState * r) -0.5 * r->i_inc, r->i_inc); buffer = audioresample_buffer_queue_pull (r->queue, r->sample_size); if (buffer == NULL) { + /* FIXME: for the first buffer, this isn't necessarily an error, + * since because of the filter length we'll output less buffers. + * deal with that so we don't print to console */ RESAMPLE_ERROR ("buffer_queue_pull returned NULL"); return; } diff --git a/tests/check/elements/audioresample.c b/tests/check/elements/audioresample.c new file mode 100644 index 0000000000..4b3368d4f7 --- /dev/null +++ b/tests/check/elements/audioresample.c @@ -0,0 +1,227 @@ +/* GStreamer + * + * unit test for audioresample + * + * Copyright (C) <2005> Thomas Vander Stichele + * + * 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 + +GList *buffers = NULL; +gboolean have_eos = FALSE; + +/* 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 */ +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) + ); + +GstElement * +setup_audioresample (int inchannels, int inrate, int outchannels, int outrate) +{ + GstElement *audioresample; + GstCaps *caps; + GstStructure *structure; + GstPad *pad; + + GST_DEBUG ("setup_audioresample"); + audioresample = gst_check_setup_element ("audioresample"); + + caps = gst_caps_from_string (RESAMPLE_CAPS_TEMPLATE_STRING); + structure = gst_caps_get_structure (caps, 0); + gst_structure_set (structure, "channels", G_TYPE_INT, inchannels, + "rate", G_TYPE_INT, inrate, NULL); + fail_unless (gst_caps_is_fixed (caps)); + + mysrcpad = gst_check_setup_src_pad (audioresample, &srctemplate, caps); + pad = gst_pad_get_peer (mysrcpad); + gst_pad_set_caps (pad, caps); + gst_object_unref (GST_OBJECT (pad)); + 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, outchannels, + "rate", G_TYPE_INT, outrate, NULL); + fail_unless (gst_caps_is_fixed (caps)); + + mysinkpad = gst_check_setup_sink_pad (audioresample, &sinktemplate, caps); + /* this installs a getcaps func that will always return the caps we set + * later */ + gst_pad_use_fixed_caps (mysinkpad); + pad = gst_pad_get_peer (mysinkpad); + gst_pad_set_caps (pad, caps); + gst_object_unref (GST_OBJECT (pad)); + gst_caps_unref (caps); + + return audioresample; +} + +void +cleanup_audioresample (GstElement * audioresample) +{ + GST_DEBUG ("cleanup_audioresample"); + + gst_check_teardown_src_pad (audioresample); + gst_check_teardown_sink_pad (audioresample); + gst_check_teardown_element (audioresample); +} + +static void +fail_unless_perfect_stream () +{ + 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, GST_BUFFER_TIMESTAMP (buffer), + GST_BUFFER_DURATION (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; + } +} + +static void +test_perfect_stream_instance (int inrate, int outrate, int samples, + int numbuffers) +{ + GstElement *audioresample; + GstBuffer *inbuffer, *outbuffer; + GstCaps *caps; + + int i, j; + gint16 *p; + + audioresample = setup_audioresample (2, inrate, 2, outrate); + caps = gst_pad_get_negotiated_caps (mysrcpad); + fail_unless (gst_caps_is_fixed (caps)); + + fail_unless (gst_element_set_state (audioresample, + GST_STATE_PLAYING) == GST_STATE_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; + GST_BUFFER_TIMESTAMP (inbuffer) = GST_BUFFER_DURATION (inbuffer) * (j - 1); + GST_BUFFER_OFFSET (inbuffer) = 0; + GST_BUFFER_OFFSET_END (inbuffer) = 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; + } + + /* 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 (g_list_length (buffers) == j); + } + + /* FIXME: we should make audioresample 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_audioresample (audioresample); +} + + +/* make sure that outgoing buffers are contiguous in timestamp/duration and + * offset/offsetend + */ +GST_START_TEST (test_perfect_stream) +{ + test_perfect_stream_instance (4000, 2000, 1000, 20); +} + +GST_END_TEST; + +Suite * +audioresample_suite (void) +{ + Suite *s = suite_create ("audioresample"); + TCase *tc_chain = tcase_create ("general"); + + suite_add_tcase (s, tc_chain); + tcase_add_test (tc_chain, test_perfect_stream); + + return s; +} + +int +main (int argc, char **argv) +{ + int nf; + + Suite *s = audioresample_suite (); + SRunner *sr = srunner_create (s); + + gst_check_init (&argc, &argv); + + srunner_run_all (sr, CK_NORMAL); + nf = srunner_ntests_failed (sr); + srunner_free (sr); + + return nf; +}