From 3dbc3b7ad1caac2e7fe319a68e3a0864d661681e Mon Sep 17 00:00:00 2001 From: Stefan Kost Date: Wed, 20 Jun 2007 08:56:17 +0000 Subject: [PATCH] gst/spectrum/gstspectrum.c: Fix leaking buffers. Original commit message from CVS: * gst/spectrum/gstspectrum.c: (gst_spectrum_event), (gst_spectrum_transform_ip): Fix leaking buffers. * tests/check/Makefile.am: * tests/check/elements/spectrum.c: (setup_spectrum), (cleanup_spectrum), (GST_START_TEST), (spectrum_suite), (main): Add simple test for spectrum element. --- ChangeLog | 11 ++ gst/spectrum/gstspectrum.c | 5 +- tests/check/Makefile.am | 1 + tests/check/elements/spectrum.c | 216 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 231 insertions(+), 2 deletions(-) create mode 100644 tests/check/elements/spectrum.c diff --git a/ChangeLog b/ChangeLog index f6ae6b0..3c3a08f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,16 @@ 2007-06-20 Stefan Kost + * gst/spectrum/gstspectrum.c: (gst_spectrum_event), + (gst_spectrum_transform_ip): + Fix leaking buffers. + + * tests/check/Makefile.am: + * tests/check/elements/spectrum.c: (setup_spectrum), + (cleanup_spectrum), (GST_START_TEST), (spectrum_suite), (main): + Add simple test for spectrum element. + +2007-06-20 Stefan Kost + * ext/amrwb/gstamrwbdec.c: (gst_amrwbdec_base_init), (gst_amrwbdec_class_init), (gst_amrwbdec_finalize), (gst_amrwbdec_event), (gst_amrwbdec_chain), diff --git a/gst/spectrum/gstspectrum.c b/gst/spectrum/gstspectrum.c index 3ca5a90..75d3e2d 100644 --- a/gst/spectrum/gstspectrum.c +++ b/gst/spectrum/gstspectrum.c @@ -396,7 +396,8 @@ gst_spectrum_transform_ip (GstBaseTransform * trans, GstBuffer * in) GST_LOG_OBJECT (spectrum, "input size: %d bytes", GST_BUFFER_SIZE (in)); - gst_adapter_push (spectrum->adapter, gst_buffer_ref (in)); + /* can we do this nicer? */ + gst_adapter_push (spectrum->adapter, gst_buffer_copy (in)); /* required number of bytes */ wanted = spectrum->channels * SPECTRUM_WINDOW_LEN * sizeof (gint16); /* FIXME: 4.0 was 2.0 before, but that include the mirrored spectrum */ @@ -422,7 +423,7 @@ gst_spectrum_transform_ip (GstBaseTransform * trans, GstBuffer * in) gst_spectrum_fix_loud (spectrum->loud, spectrum->re, spectrum->im, SPECTRUM_WINDOW_LEN, 0); - /* resample to requested number of bands */ + /* resample to requested number of bands and offset by threshold */ for (i = 0, pos = 0.0; i < spectrum->bands; i++, pos += step) { if (spectrum->loud[(gint) pos] > spectrum->threshold) { spect[i] = spectrum->loud[(gint) pos] - spectrum->threshold; diff --git a/tests/check/Makefile.am b/tests/check/Makefile.am index 3db8536..9856fda 100644 --- a/tests/check/Makefile.am +++ b/tests/check/Makefile.am @@ -51,6 +51,7 @@ check_PROGRAMS = \ elements/rganalysis \ elements/rglimiter \ elements/rgvolume \ + elements/spectrum \ elements/y4menc TESTS = $(check_PROGRAMS) diff --git a/tests/check/elements/spectrum.c b/tests/check/elements/spectrum.c new file mode 100644 index 0000000..7e3d1e1 --- /dev/null +++ b/tests/check/elements/spectrum.c @@ -0,0 +1,216 @@ +/* GStreamer + * + * unit test for spectrum + * + * Copyright (C) <2007> Stefan Kost + * + * 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 SPECT_CAPS_TEMPLATE_STRING \ + "audio/x-raw-int, " \ + "rate = (int) [ 1, MAX ], " \ + "channels = (int) [ 1, 8 ], " \ + "endianness = (int) BYTE_ORDER, " \ + "width = (int) {8, 16}, " \ + "depth = (int) {8, 16}, " \ + "signed = (boolean) true" + +#define SPECT_CAPS_STRING \ + "audio/x-raw-int, " \ + "rate = (int) 44100, " \ + "channels = (int) 1, " \ + "endianness = (int) BYTE_ORDER, " \ + "width = (int) 16, " \ + "depth = (int) 16, " \ + "signed = (boolean) true" + +#define SPECT_BANDS 64 + +static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS (SPECT_CAPS_TEMPLATE_STRING) + ); +static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS (SPECT_CAPS_TEMPLATE_STRING) + ); + +/* takes over reference for outcaps */ +GstElement * +setup_spectrum () +{ + GstElement *spectrum; + + GST_DEBUG ("setup_spectrum"); + spectrum = gst_check_setup_element ("spectrum"); + mysrcpad = gst_check_setup_src_pad (spectrum, &srctemplate, NULL); + mysinkpad = gst_check_setup_sink_pad (spectrum, &sinktemplate, NULL); + gst_pad_set_active (mysrcpad, TRUE); + gst_pad_set_active (mysinkpad, TRUE); + + return spectrum; +} + +void +cleanup_spectrum (GstElement * spectrum) +{ + GST_DEBUG ("cleanup_spectrum"); + + gst_pad_set_active (mysrcpad, FALSE); + gst_pad_set_active (mysinkpad, FALSE); + gst_check_teardown_src_pad (spectrum); + gst_check_teardown_sink_pad (spectrum); + gst_check_teardown_element (spectrum); +} + + +GST_START_TEST (test_int16) +{ + GstElement *spectrum; + GstBuffer *inbuffer, *outbuffer; + GstBus *bus; + GstCaps *caps; + GstMessage *message; + const GstStructure *structure; + int i, j; + gint16 *data; + const GValue *list, *value; + GstClockTime endtime; + guchar level; + + spectrum = setup_spectrum (); + g_object_set (spectrum, "message", TRUE, "interval", GST_SECOND / 10, + "bands", SPECT_BANDS, "threshold", -80, NULL); + + fail_unless (gst_element_set_state (spectrum, + GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS, + "could not set to playing"); + + /* create a fake 1 sec buffer with a half-amplitude block signal */ + inbuffer = gst_buffer_new_and_alloc (44100 * sizeof (gint16)); + data = (gint16 *) GST_BUFFER_DATA (inbuffer); + for (j = 0; j < 44100; ++j) { + *data = 16536; + ++data; + } + caps = gst_caps_from_string (SPECT_CAPS_STRING); + gst_buffer_set_caps (inbuffer, caps); + gst_caps_unref (caps); + ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1); + + /* create a bus to get the spectrum message on */ + bus = gst_bus_new (); + ASSERT_OBJECT_REFCOUNT (bus, "bus", 1); + gst_element_set_bus (spectrum, bus); + ASSERT_OBJECT_REFCOUNT (bus, "bus", 2); + + /* 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 */ + ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1); + fail_unless_equals_int (g_list_length (buffers), 1); + fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL); + fail_unless (inbuffer == outbuffer); + + message = gst_bus_poll (bus, GST_MESSAGE_ELEMENT, -1); + ASSERT_OBJECT_REFCOUNT (message, "message", 1); + + fail_unless (message != NULL); + fail_unless (GST_MESSAGE_SRC (message) == GST_OBJECT (spectrum)); + fail_unless (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ELEMENT); + structure = gst_message_get_structure (message); + fail_if (structure == NULL); + fail_unless_equals_string ((char *) gst_structure_get_name (structure), + "spectrum"); + fail_unless (gst_structure_get_clock_time (structure, "endtime", &endtime)); + + /* block wave of half amplitude has -5.94 dB for rms, peak and decay */ + list = gst_structure_get_value (structure, "spectrum"); + for (i = 0; i < SPECT_BANDS; ++i) { + value = gst_value_list_get_value (list, i); + level = g_value_get_uchar (value); + GST_DEBUG ("band[%3d] is %3d", i, level); + fail_if (level == 0); + } + fail_unless_equals_int (g_list_length (buffers), 1); + fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL); + fail_unless (inbuffer == outbuffer); + + /* clean up */ + /* flush current messages,and future state change messages */ + gst_bus_set_flushing (bus, TRUE); + + /* message has a ref to the element */ + ASSERT_OBJECT_REFCOUNT (spectrum, "spectrum", 2); + gst_message_unref (message); + ASSERT_OBJECT_REFCOUNT (spectrum, "spectrum", 1); + + gst_element_set_bus (spectrum, NULL); + ASSERT_OBJECT_REFCOUNT (bus, "bus", 1); + gst_object_unref (bus); + gst_buffer_unref (outbuffer); + fail_unless (gst_element_set_state (spectrum, + GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null"); + ASSERT_OBJECT_REFCOUNT (spectrum, "spectrum", 1); + cleanup_spectrum (spectrum); +} + +GST_END_TEST; + + +Suite * +spectrum_suite (void) +{ + Suite *s = suite_create ("spectrum"); + TCase *tc_chain = tcase_create ("general"); + + suite_add_tcase (s, tc_chain); + tcase_add_test (tc_chain, test_int16); + + return s; +} + +int +main (int argc, char **argv) +{ + int nf; + + Suite *s = spectrum_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; +} -- 2.7.4