From 7da31ee8b419bda7b0b73a09123b5421840f9653 Mon Sep 17 00:00:00 2001 From: Thomas Vander Stichele Date: Sat, 20 Aug 2005 18:07:10 +0000 Subject: [PATCH] add tests to gst-plugins-base add a volume element test clean up volume a little more for basetransform Original commit message from CVS: add tests to gst-plugins-base add a volume element test clean up volume a little more for basetransform --- Makefile.am | 2 + check/.gitignore | 1 + check/Makefile.am | 31 ++++ check/elements/.gitignore | 2 + check/elements/volume.c | 319 ++++++++++++++++++++++++++++++++ common | 2 +- configure.ac | 7 + gst/volume/gstvolume.c | 244 ++++++++++++------------ tests/check/.gitignore | 1 + tests/check/Makefile.am | 31 ++++ tests/check/elements/.gitignore | 2 + tests/check/elements/volume.c | 319 ++++++++++++++++++++++++++++++++ 12 files changed, 846 insertions(+), 115 deletions(-) create mode 100644 check/.gitignore create mode 100644 check/Makefile.am create mode 100644 check/elements/.gitignore create mode 100644 check/elements/volume.c create mode 100644 tests/check/.gitignore create mode 100644 tests/check/Makefile.am create mode 100644 tests/check/elements/.gitignore create mode 100644 tests/check/elements/volume.c diff --git a/Makefile.am b/Makefile.am index a2e9378411..67b3ddf6bd 100644 --- a/Makefile.am +++ b/Makefile.am @@ -28,6 +28,7 @@ SUBDIRS = \ $(SUBDIRS_EXAMPLES) \ tools \ $(SUBDIRS_GCONF) \ + check \ docs \ po \ common \ @@ -35,6 +36,7 @@ SUBDIRS = \ pkgconfig DIST_SUBDIRS = \ + check \ docs \ gst-libs \ gst sys ext \ diff --git a/check/.gitignore b/check/.gitignore new file mode 100644 index 0000000000..5d45c6c3fd --- /dev/null +++ b/check/.gitignore @@ -0,0 +1 @@ +test-registry.xml diff --git a/check/Makefile.am b/check/Makefile.am new file mode 100644 index 0000000000..52c17ba3cc --- /dev/null +++ b/check/Makefile.am @@ -0,0 +1,31 @@ +include $(top_srcdir)/common/check.mak + +CHECK_REGISTRY=$(top_builddir)/check/test-registry.xml + +TESTS_ENVIRONMENT=\ + GST_REGISTRY=$(CHECK_REGISTRY) + +# ths core dumps of some machines have PIDs appended +CLEANFILES = core.* test-registry.xml + +clean-local: clean-local-check + +$(CHECK_REGISTRY): + $(TESTS_ENVIRONMENT) \ + GST_PLUGIN_PATH_ONLY=yes \ + GST_PLUGIN_PATH=$(top_builddir)/gst \ + $(GST_TOOLS_DIR)/gst-register-@GST_MAJORMINOR@ + +TESTS = $(GST_TOOLS_DIR)/gst-register-@GST_MAJORMINOR@ \ + $(check_PROGRAMS) + +check_PROGRAMS = elements/volume + +AM_CFLAGS = $(GST_OBJ_CFLAGS) $(GST_CHECK_CFLAGS) +LDADD = $(GST_OBJ_LIBS) $(GST_CHECK_LIBS) + +# valgrind testing +VALGRIND_TESTS_DISABLE = \ + $(GST_TOOLS_DIR)/gst-register-@GST_MAJORMINOR@ + +SUPPRESSIONS = $(top_srcdir)/common/gst.supp diff --git a/check/elements/.gitignore b/check/elements/.gitignore new file mode 100644 index 0000000000..b638b06eb0 --- /dev/null +++ b/check/elements/.gitignore @@ -0,0 +1,2 @@ +.dirstamp +volume diff --git a/check/elements/volume.c b/check/elements/volume.c new file mode 100644 index 0000000000..b2fcc2dac8 --- /dev/null +++ b/check/elements/volume.c @@ -0,0 +1,319 @@ +/* GStreamer + * + * unit test for volume + * + * 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 VOLUME_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" + +#define VOLUME_CAPS_STRING \ + "audio/x-raw-int, " \ + "channels = (int) 1, " \ + "rate = (int) 44100, " \ + "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 (VOLUME_CAPS_TEMPLATE_STRING) + ); +static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS (VOLUME_CAPS_TEMPLATE_STRING) + ); + +GstFlowReturn +chain_func (GstPad * pad, GstBuffer * buffer) +{ + GST_DEBUG ("chain_func: received buffer %p", buffer); + buffers = g_list_append (buffers, buffer); + + return GST_FLOW_OK; +} + +gboolean +event_func (GstPad * pad, GstEvent * event) +{ + if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) { + /* we take the lock here because it's good practice to so, even though + * no buffers will be pushed anymore anyway */ + GST_STREAM_LOCK (pad); + have_eos = TRUE; + GST_STREAM_UNLOCK (pad); + gst_event_unref (event); + return TRUE; + } + + gst_event_unref (event); + return FALSE; +} + +GstElement * +setup_volume () +{ + GstElement *volume; + GstPad *srcpad, *sinkpad; + + GST_DEBUG ("setup_volume"); + + volume = gst_element_factory_make ("volume", "volume"); + fail_if (volume == NULL, "Could not create a volume"); + + /* sending pad */ + mysrcpad = + gst_pad_new_from_template (gst_static_pad_template_get (&srctemplate), + "src"); + fail_if (mysrcpad == NULL, "Could not create a mysrcpad"); + ASSERT_OBJECT_REFCOUNT (mysrcpad, "mysrcpad", 1); + + sinkpad = gst_element_get_pad (volume, "sink"); + fail_if (sinkpad == NULL, "Could not get source pad from volume"); + ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 2); + gst_pad_set_caps (mysrcpad, NULL); + fail_unless (gst_pad_link (mysrcpad, sinkpad) == GST_PAD_LINK_OK, + "Could not link source and volume sink pads"); + gst_object_unref (sinkpad); /* because we got it higher up */ + ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 1); + + /* receiving pad */ + mysinkpad = + gst_pad_new_from_template (gst_static_pad_template_get (&sinktemplate), + "sink"); + fail_if (mysinkpad == NULL, "Could not create a mysinkpad"); + + srcpad = gst_element_get_pad (volume, "src"); + fail_if (srcpad == NULL, "Could not get source pad from volume"); + gst_pad_set_caps (mysinkpad, NULL); + gst_pad_set_chain_function (mysinkpad, chain_func); + gst_pad_set_event_function (mysinkpad, event_func); + + fail_unless (gst_pad_link (srcpad, mysinkpad) == GST_PAD_LINK_OK, + "Could not link volume source and mysink pads"); + gst_object_unref (srcpad); /* because we got it higher up */ + ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 1); + + return volume; +} + +void +cleanup_volume (GstElement * volume) +{ + GstPad *srcpad, *sinkpad; + + GST_DEBUG ("cleanup_volume"); + + fail_unless (gst_element_set_state (volume, GST_STATE_NULL) == + GST_STATE_SUCCESS, "could not set to null"); + ASSERT_OBJECT_REFCOUNT (volume, "volume", 1); + + /* clean up floating src pad */ + sinkpad = gst_element_get_pad (volume, "sink"); + ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 2); + + gst_pad_unlink (mysrcpad, sinkpad); + + /* pad refs held by both creator and this function (through _get) */ + ASSERT_OBJECT_REFCOUNT (mysrcpad, "srcpad", 1); + gst_object_unref (mysrcpad); + mysrcpad = NULL; + + ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 2); + gst_object_unref (sinkpad); + /* one more ref is held by volume itself */ + + /* clean up floating sink pad */ + srcpad = gst_element_get_pad (volume, "src"); + gst_pad_unlink (srcpad, mysinkpad); + + /* pad refs held by both creator and this function (through _get) */ + ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 2); + gst_object_unref (srcpad); + /* one more ref is held by volume itself */ + + ASSERT_OBJECT_REFCOUNT (mysinkpad, "mysinkpad", 1); + gst_object_unref (mysinkpad); + mysinkpad = NULL; + + ASSERT_OBJECT_REFCOUNT (volume, "volume", 1); + gst_object_unref (volume); +} + +GST_START_TEST (test_unity) +{ + GstElement *volume; + GstBuffer *inbuffer, *outbuffer; + gint16 in[2] = { 16384, -256 }; + + volume = setup_volume (); + fail_unless (gst_element_set_state (volume, + GST_STATE_PLAYING) == GST_STATE_SUCCESS, "could not set to playing"); + + inbuffer = gst_buffer_new_and_alloc (4); + memcpy (GST_BUFFER_DATA (inbuffer), in, 4); + gst_buffer_set_caps (inbuffer, gst_caps_from_string (VOLUME_CAPS_STRING)); + ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1); + + /* pushing gives away my reference ... */ + gst_pad_push (mysrcpad, inbuffer); + /* ... but it ends up being collected on the global buffer list */ + ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1); + fail_unless (g_list_length (buffers) == 1); + fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL); + fail_unless (inbuffer == outbuffer); + fail_unless (memcmp (GST_BUFFER_DATA (inbuffer), in, 4) == 0); + + /* cleanup */ + cleanup_volume (volume); +} + +GST_END_TEST; + +GST_START_TEST (test_half) +{ + GstElement *volume; + GstBuffer *inbuffer; + GstBuffer *outbuffer; + gint16 in[2] = { 16384, -256 }; + gint16 out[2] = { 8192, -128 }; + + volume = setup_volume (); + g_object_set (G_OBJECT (volume), "volume", 0.5, NULL); + fail_unless (gst_element_set_state (volume, + GST_STATE_PLAYING) == GST_STATE_SUCCESS, "could not set to playing"); + + inbuffer = gst_buffer_new_and_alloc (4); + memcpy (GST_BUFFER_DATA (inbuffer), in, 4); + fail_unless (memcmp (GST_BUFFER_DATA (inbuffer), in, 4) == 0); + gst_buffer_set_caps (inbuffer, gst_caps_from_string (VOLUME_CAPS_STRING)); + ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1); + /* FIXME: reffing the inbuffer should make the transformation not be + * inplace + gst_buffer_ref (inbuffer); + */ + + /* pushing gives away my reference ... */ + gst_pad_push (mysrcpad, inbuffer); + /* ... but it ends up being modified inplace and + * collected on the global buffer list */ + ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1); + fail_unless (g_list_length (buffers) == 1); + fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL); + fail_unless (inbuffer == outbuffer); + fail_unless (memcmp (GST_BUFFER_DATA (outbuffer), out, 4) == 0); + + /* cleanup */ + cleanup_volume (volume); +} + +GST_END_TEST; + +GST_START_TEST (test_double) +{ + GstElement *volume; + GstBuffer *inbuffer; + GstBuffer *outbuffer; + gint16 in[2] = { 16384, -256 }; + gint16 out[2] = { 32767, -512 }; /* notice the clamped sample */ + + volume = setup_volume (); + g_object_set (G_OBJECT (volume), "volume", 2.0, NULL); + fail_unless (gst_element_set_state (volume, + GST_STATE_PLAYING) == GST_STATE_SUCCESS, "could not set to playing"); + + inbuffer = gst_buffer_new_and_alloc (4); + memcpy (GST_BUFFER_DATA (inbuffer), in, 4); + fail_unless (memcmp (GST_BUFFER_DATA (inbuffer), in, 4) == 0); + gst_buffer_set_caps (inbuffer, gst_caps_from_string (VOLUME_CAPS_STRING)); + ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1); + /* FIXME: reffing the inbuffer should make the transformation not be + * inplace + gst_buffer_ref (inbuffer); + */ + + /* pushing gives away my reference ... */ + gst_pad_push (mysrcpad, inbuffer); + /* ... but it ends up being modified inplace and + * collected on the global buffer list */ + ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1); + fail_unless (g_list_length (buffers) == 1); + fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL); + fail_unless (inbuffer == outbuffer); + fail_unless (memcmp (GST_BUFFER_DATA (outbuffer), out, 4) == 0); + + /* cleanup */ + cleanup_volume (volume); +} + +GST_END_TEST; + + +Suite * +volume_suite (void) +{ + Suite *s = suite_create ("volume"); + TCase *tc_chain = tcase_create ("general"); + + suite_add_tcase (s, tc_chain); + tcase_add_test (tc_chain, test_unity); + tcase_add_test (tc_chain, test_half); + tcase_add_test (tc_chain, test_double); + + return s; +} + +int +main (int argc, char **argv) +{ + int nf; + + Suite *s = volume_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; +} diff --git a/common b/common index 8ff526a316..4cc6f46585 160000 --- a/common +++ b/common @@ -1 +1 @@ -Subproject commit 8ff526a316f9b576e727b8e32cba0a53cdec07a6 +Subproject commit 4cc6f465857331531a09aff0a23dc0b133e7f669 diff --git a/configure.ac b/configure.ac index eacaee216d..0cf63a2752 100644 --- a/configure.ac +++ b/configure.ac @@ -238,6 +238,12 @@ fi AC_SUBST(GST_BASE_LIBS) +PKG_CHECK_MODULES(GST_CHECK, gstreamer-check-$GST_MAJORMINOR >= $GST_REQ, + HAVE_GST_CHECK="yes", HAVE_GST_CHECK="no") + +AC_PATH_PROG(VALGRIND_PATH, valgrind, no) +AM_CONDITIONAL(HAVE_VALGRIND, test ! "x$VALGRIND_PATH" = "xno") + dnl Determine endianness AC_C_BIGENDIAN @@ -927,6 +933,7 @@ gconf/gstreamer.schemas pkgconfig/Makefile pkgconfig/gstreamer-plugins-base.pc pkgconfig/gstreamer-plugins-base-uninstalled.pc +check/Makefile docs/Makefile docs/libs/Makefile docs/plugins/Makefile diff --git a/gst/volume/gstvolume.c b/gst/volume/gstvolume.c index 8c70d43d3f..e41fe563f1 100644 --- a/gst/volume/gstvolume.c +++ b/gst/volume/gstvolume.c @@ -1,6 +1,7 @@ /* -*- c-basic-offset: 2 -*- * GStreamer * Copyright (C) 1999-2001 Erik Walthinsen + * Copyright (C) 2005 Andy Wingo * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -101,28 +102,27 @@ static GstStaticPadTemplate volume_src_factory = GST_STATIC_PAD_TEMPLATE ("src", static void gst_volume_interface_init (GstImplementsInterfaceClass * klass); static void gst_volume_mixer_init (GstMixerClass * iface); -#define _init_interfaces(type) \ - { \ - static const GInterfaceInfo voliface_info = { \ - (GInterfaceInitFunc) gst_volume_interface_init, \ - NULL, \ - NULL \ - }; \ - static const GInterfaceInfo volmixer_info = { \ - (GInterfaceInitFunc) gst_volume_mixer_init, \ - NULL, \ - NULL \ - }; \ - \ - g_type_add_interface_static (type, GST_TYPE_IMPLEMENTS_INTERFACE, \ - &voliface_info); \ - g_type_add_interface_static (type, GST_TYPE_MIXER, &volmixer_info); \ +#define _init_interfaces(type) \ + { \ + static const GInterfaceInfo voliface_info = { \ + (GInterfaceInitFunc) gst_volume_interface_init, \ + NULL, \ + NULL \ + }; \ + static const GInterfaceInfo volmixer_info = { \ + (GInterfaceInitFunc) gst_volume_mixer_init, \ + NULL, \ + NULL \ + }; \ + \ + g_type_add_interface_static (type, GST_TYPE_IMPLEMENTS_INTERFACE, \ + &voliface_info); \ + g_type_add_interface_static (type, GST_TYPE_MIXER, &volmixer_info); \ } GST_BOILERPLATE_FULL (GstVolume, gst_volume, GstBaseTransform, GST_TYPE_BASE_TRANSFORM, _init_interfaces); - static void volume_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec); static void volume_get_property (GObject * object, guint prop_id, @@ -132,9 +132,13 @@ static void volume_update_mute (const GValue * value, gpointer data); static GstFlowReturn volume_transform (GstBaseTransform * base, GstBuffer * inbuf, GstBuffer * outbuf); -static void volume_process_float (GstVolume * filter, GstClockTime tstamp, +gboolean volume_set_caps (GstBaseTransform * base, GstCaps * incaps, + GstCaps * outcaps); + + +static void volume_process_float (GstVolume * this, GstClockTime tstamp, gpointer bytes, gint n_bytes); -static void volume_process_int16 (GstVolume * filter, GstClockTime tstamp, +static void volume_process_int16 (GstVolume * this, GstClockTime tstamp, gpointer bytes, gint n_bytes); static gboolean @@ -153,61 +157,61 @@ gst_volume_interface_init (GstImplementsInterfaceClass * klass) static const GList * gst_volume_list_tracks (GstMixer * mixer) { - GstVolume *filter = GST_VOLUME (mixer); + GstVolume *this = GST_VOLUME (mixer); - g_return_val_if_fail (filter != NULL, NULL); - g_return_val_if_fail (GST_IS_VOLUME (filter), NULL); + g_return_val_if_fail (this != NULL, NULL); + g_return_val_if_fail (GST_IS_VOLUME (this), NULL); - return filter->tracklist; + return this->tracklist; } static void gst_volume_set_volume (GstMixer * mixer, GstMixerTrack * track, gint * volumes) { - GstVolume *filter = GST_VOLUME (mixer); + GstVolume *this = GST_VOLUME (mixer); - g_return_if_fail (filter != NULL); - g_return_if_fail (GST_IS_VOLUME (filter)); + g_return_if_fail (this != NULL); + g_return_if_fail (GST_IS_VOLUME (this)); - filter->volume_f = (gfloat) volumes[0] / VOLUME_STEPS; - filter->volume_i = filter->volume_f * VOLUME_UNITY_INT; + this->volume_f = (gfloat) volumes[0] / VOLUME_STEPS; + this->volume_i = this->volume_f * VOLUME_UNITY_INT; - if (filter->mute) { - filter->real_vol_f = 0.0; - filter->real_vol_i = 0; + if (this->mute) { + this->real_vol_f = 0.0; + this->real_vol_i = 0; } else { - filter->real_vol_f = filter->volume_f; - filter->real_vol_i = filter->volume_i; + this->real_vol_f = this->volume_f; + this->real_vol_i = this->volume_i; } } static void gst_volume_get_volume (GstMixer * mixer, GstMixerTrack * track, gint * volumes) { - GstVolume *filter = GST_VOLUME (mixer); + GstVolume *this = GST_VOLUME (mixer); - g_return_if_fail (filter != NULL); - g_return_if_fail (GST_IS_VOLUME (filter)); + g_return_if_fail (this != NULL); + g_return_if_fail (GST_IS_VOLUME (this)); - volumes[0] = (gint) filter->volume_f * VOLUME_STEPS; + volumes[0] = (gint) this->volume_f * VOLUME_STEPS; } static void gst_volume_set_mute (GstMixer * mixer, GstMixerTrack * track, gboolean mute) { - GstVolume *filter = GST_VOLUME (mixer); + GstVolume *this = GST_VOLUME (mixer); - g_return_if_fail (filter != NULL); - g_return_if_fail (GST_IS_VOLUME (filter)); + g_return_if_fail (this != NULL); + g_return_if_fail (GST_IS_VOLUME (this)); - filter->mute = mute; + this->mute = mute; - if (filter->mute) { - filter->real_vol_f = 0.0; - filter->real_vol_i = 0; + if (this->mute) { + this->real_vol_f = 0.0; + this->real_vol_i = 0; } else { - filter->real_vol_f = filter->volume_f; - filter->real_vol_i = filter->volume_i; + this->real_vol_f = this->volume_f; + this->real_vol_i = this->volume_i; } } @@ -269,19 +273,21 @@ gst_volume_class_init (GstVolumeClass * klass) GST_BASE_TRANSFORM_CLASS (klass)->transform = GST_DEBUG_FUNCPTR (volume_transform); + GST_BASE_TRANSFORM_CLASS (klass)->set_caps = + GST_DEBUG_FUNCPTR (volume_set_caps); } static void -gst_volume_init (GstVolume * filter) +gst_volume_init (GstVolume * this) { GstMixerTrack *track = NULL; - filter->mute = FALSE; - filter->volume_i = VOLUME_UNITY_INT; - filter->volume_f = 1.0; - filter->real_vol_i = VOLUME_UNITY_INT; - filter->real_vol_f = 1.0; - filter->tracklist = NULL; + this->mute = FALSE; + this->volume_i = VOLUME_UNITY_INT; + this->volume_f = 1.0; + this->real_vol_i = VOLUME_UNITY_INT; + this->real_vol_f = 1.0; + this->tracklist = NULL; track = g_object_new (GST_TYPE_MIXER_TRACK, NULL); @@ -291,50 +297,26 @@ gst_volume_init (GstVolume * filter) track->min_volume = 0; track->max_volume = VOLUME_STEPS; track->flags = GST_MIXER_TRACK_SOFTWARE; - filter->tracklist = g_list_append (filter->tracklist, track); + this->tracklist = g_list_append (this->tracklist, track); } } +/* based on the caps' structure, install the correct volume_process method */ static void -volume_typefind (GstVolume * filter, const GstStructure * structure) +volume_funcfind (GstVolume * this, const GstStructure * structure) { const gchar *mimetype; mimetype = gst_structure_get_name (structure); if (strcmp (mimetype, "audio/x-raw-int") == 0) - filter->process = volume_process_int16; + this->process = volume_process_int16; else if (strcmp (mimetype, "audio/x-raw-float") == 0) - filter->process = volume_process_float; -} - -static GstFlowReturn -volume_transform (GstBaseTransform * base, GstBuffer * inbuf, - GstBuffer * outbuf) -{ - GstVolume *filter = GST_VOLUME (base); - - if (G_UNLIKELY (!filter->process)) { - GstCaps *caps = GST_BUFFER_CAPS (inbuf); - - if (gst_caps_get_size (caps) == 1) - volume_typefind (filter, gst_caps_get_structure (caps, 0)); - - if (!filter->process) { - GST_ELEMENT_ERROR (filter, CORE, NEGOTIATION, - ("Invalid caps on first buffer"), NULL); - return GST_FLOW_UNEXPECTED; - } - } - - filter->process (filter, GST_BUFFER_TIMESTAMP (outbuf), - GST_BUFFER_DATA (outbuf), GST_BUFFER_SIZE (outbuf)); - - return GST_FLOW_OK; + this->process = volume_process_float; } static void -volume_process_float (GstVolume * filter, GstClockTime tstamp, +volume_process_float (GstVolume * this, GstClockTime tstamp, gpointer bytes, gint n_bytes) { gfloat *data; @@ -344,12 +326,12 @@ volume_process_float (GstVolume * filter, GstClockTime tstamp, num_samples = n_bytes / sizeof (gfloat); for (i = 0; i < num_samples; i++) { - *data++ *= filter->real_vol_f; + *data++ *= this->real_vol_f; } } static void -volume_process_int16 (GstVolume * filter, GstClockTime tstamp, +volume_process_int16 (GstVolume * this, GstClockTime tstamp, gpointer bytes, gint n_bytes) { gint16 *data; @@ -358,14 +340,14 @@ volume_process_int16 (GstVolume * filter, GstClockTime tstamp, data = (gint16 *) bytes; num_samples = n_bytes / sizeof (gint16); - /* need... liboil... */ + /* FIXME: need... liboil... */ /* only clamp if the gain is greater than 1.0 */ - if (filter->real_vol_i > VOLUME_UNITY_INT) { + if (this->real_vol_i > VOLUME_UNITY_INT) { for (i = 0; i < num_samples; i++) { /* we use bitshifting instead of dividing by UNITY_INT for speed */ val = (gint) * data; *data++ = - (gint16) CLAMP ((filter->real_vol_i * + (gint16) CLAMP ((this->real_vol_i * val) >> VOLUME_UNITY_BIT_SHIFT, VOLUME_MIN_INT16, VOLUME_MAX_INT16); } @@ -373,48 +355,82 @@ volume_process_int16 (GstVolume * filter, GstClockTime tstamp, for (i = 0; i < num_samples; i++) { /* we use bitshifting instead of dividing by UNITY_INT for speed */ val = (gint) * data; - *data++ = (gint16) ((filter->real_vol_i * val) >> VOLUME_UNITY_BIT_SHIFT); + *data++ = (gint16) ((this->real_vol_i * val) >> VOLUME_UNITY_BIT_SHIFT); } } } +/* GstBaseTransform vmethod implementations */ + +/* get notified of caps and plug in the correct process function */ +gboolean +volume_set_caps (GstBaseTransform * base, GstCaps * incaps, GstCaps * outcaps) +{ + GstVolume *this = GST_VOLUME (base); + + volume_funcfind (this, gst_caps_get_structure (incaps, 0)); + + if (!this->process) { + GST_ELEMENT_ERROR (this, CORE, NEGOTIATION, + ("Invalid incoming caps: %" GST_PTR_FORMAT, incaps), NULL); + return FALSE; + } + return TRUE; +} + +/* call the plugged-in process function for this instance + * needs to be done with this indirection since volume_transform is + * a class-global method + */ +static GstFlowReturn +volume_transform (GstBaseTransform * base, GstBuffer * inbuf, + GstBuffer * outbuf) +{ + GstVolume *this = GST_VOLUME (base); + + this->process (this, GST_BUFFER_TIMESTAMP (outbuf), + GST_BUFFER_DATA (outbuf), GST_BUFFER_SIZE (outbuf)); + + return GST_FLOW_OK; +} + static void volume_update_mute (const GValue * value, gpointer data) { - GstVolume *filter = (GstVolume *) data; + GstVolume *this = (GstVolume *) data; - g_return_if_fail (GST_IS_VOLUME (filter)); + g_return_if_fail (GST_IS_VOLUME (this)); if (G_VALUE_HOLDS_BOOLEAN (value)) { - filter->mute = g_value_get_boolean (value); + this->mute = g_value_get_boolean (value); } else if (G_VALUE_HOLDS_INT (value)) { - filter->mute = (g_value_get_int (value) == 1); + this->mute = (g_value_get_int (value) == 1); } - if (filter->mute) { - filter->real_vol_f = 0.0; - filter->real_vol_i = 0; + if (this->mute) { + this->real_vol_f = 0.0; + this->real_vol_i = 0; } else { - filter->real_vol_f = filter->volume_f; - filter->real_vol_i = filter->volume_i; + this->real_vol_f = this->volume_f; + this->real_vol_i = this->volume_i; } } static void volume_update_volume (const GValue * value, gpointer data) { - GstVolume *filter = (GstVolume *) data; + GstVolume *this = (GstVolume *) data; - g_return_if_fail (GST_IS_VOLUME (filter)); + g_return_if_fail (GST_IS_VOLUME (this)); - filter->volume_f = g_value_get_double (value); - filter->volume_i = filter->volume_f * VOLUME_UNITY_INT; - if (filter->mute) { - filter->real_vol_f = 0.0; - filter->real_vol_i = 0; + this->volume_f = g_value_get_double (value); + this->volume_i = this->volume_f * VOLUME_UNITY_INT; + if (this->mute) { + this->real_vol_f = 0.0; + this->real_vol_i = 0; } else { - filter->real_vol_f = filter->volume_f; - filter->real_vol_i = filter->volume_i; + this->real_vol_f = this->volume_f; + this->real_vol_i = this->volume_i; } } @@ -422,14 +438,14 @@ static void volume_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec) { - GstVolume *filter = GST_VOLUME (object); + GstVolume *this = GST_VOLUME (object); switch (prop_id) { case PROP_MUTE: - volume_update_mute (value, filter); + volume_update_mute (value, this); break; case PROP_VOLUME: - volume_update_volume (value, filter); + volume_update_volume (value, this); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -441,14 +457,14 @@ static void volume_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec) { - GstVolume *filter = GST_VOLUME (object); + GstVolume *this = GST_VOLUME (object); switch (prop_id) { case PROP_MUTE: - g_value_set_boolean (value, filter->mute); + g_value_set_boolean (value, this->mute); break; case PROP_VOLUME: - g_value_set_double (value, filter->volume_f); + g_value_set_double (value, this->volume_f); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -467,4 +483,4 @@ GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, GST_VERSION_MINOR, "volume", "element for controlling audio volume", - plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN) + plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN); diff --git a/tests/check/.gitignore b/tests/check/.gitignore new file mode 100644 index 0000000000..5d45c6c3fd --- /dev/null +++ b/tests/check/.gitignore @@ -0,0 +1 @@ +test-registry.xml diff --git a/tests/check/Makefile.am b/tests/check/Makefile.am new file mode 100644 index 0000000000..52c17ba3cc --- /dev/null +++ b/tests/check/Makefile.am @@ -0,0 +1,31 @@ +include $(top_srcdir)/common/check.mak + +CHECK_REGISTRY=$(top_builddir)/check/test-registry.xml + +TESTS_ENVIRONMENT=\ + GST_REGISTRY=$(CHECK_REGISTRY) + +# ths core dumps of some machines have PIDs appended +CLEANFILES = core.* test-registry.xml + +clean-local: clean-local-check + +$(CHECK_REGISTRY): + $(TESTS_ENVIRONMENT) \ + GST_PLUGIN_PATH_ONLY=yes \ + GST_PLUGIN_PATH=$(top_builddir)/gst \ + $(GST_TOOLS_DIR)/gst-register-@GST_MAJORMINOR@ + +TESTS = $(GST_TOOLS_DIR)/gst-register-@GST_MAJORMINOR@ \ + $(check_PROGRAMS) + +check_PROGRAMS = elements/volume + +AM_CFLAGS = $(GST_OBJ_CFLAGS) $(GST_CHECK_CFLAGS) +LDADD = $(GST_OBJ_LIBS) $(GST_CHECK_LIBS) + +# valgrind testing +VALGRIND_TESTS_DISABLE = \ + $(GST_TOOLS_DIR)/gst-register-@GST_MAJORMINOR@ + +SUPPRESSIONS = $(top_srcdir)/common/gst.supp diff --git a/tests/check/elements/.gitignore b/tests/check/elements/.gitignore new file mode 100644 index 0000000000..b638b06eb0 --- /dev/null +++ b/tests/check/elements/.gitignore @@ -0,0 +1,2 @@ +.dirstamp +volume diff --git a/tests/check/elements/volume.c b/tests/check/elements/volume.c new file mode 100644 index 0000000000..b2fcc2dac8 --- /dev/null +++ b/tests/check/elements/volume.c @@ -0,0 +1,319 @@ +/* GStreamer + * + * unit test for volume + * + * 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 VOLUME_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" + +#define VOLUME_CAPS_STRING \ + "audio/x-raw-int, " \ + "channels = (int) 1, " \ + "rate = (int) 44100, " \ + "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 (VOLUME_CAPS_TEMPLATE_STRING) + ); +static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS (VOLUME_CAPS_TEMPLATE_STRING) + ); + +GstFlowReturn +chain_func (GstPad * pad, GstBuffer * buffer) +{ + GST_DEBUG ("chain_func: received buffer %p", buffer); + buffers = g_list_append (buffers, buffer); + + return GST_FLOW_OK; +} + +gboolean +event_func (GstPad * pad, GstEvent * event) +{ + if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) { + /* we take the lock here because it's good practice to so, even though + * no buffers will be pushed anymore anyway */ + GST_STREAM_LOCK (pad); + have_eos = TRUE; + GST_STREAM_UNLOCK (pad); + gst_event_unref (event); + return TRUE; + } + + gst_event_unref (event); + return FALSE; +} + +GstElement * +setup_volume () +{ + GstElement *volume; + GstPad *srcpad, *sinkpad; + + GST_DEBUG ("setup_volume"); + + volume = gst_element_factory_make ("volume", "volume"); + fail_if (volume == NULL, "Could not create a volume"); + + /* sending pad */ + mysrcpad = + gst_pad_new_from_template (gst_static_pad_template_get (&srctemplate), + "src"); + fail_if (mysrcpad == NULL, "Could not create a mysrcpad"); + ASSERT_OBJECT_REFCOUNT (mysrcpad, "mysrcpad", 1); + + sinkpad = gst_element_get_pad (volume, "sink"); + fail_if (sinkpad == NULL, "Could not get source pad from volume"); + ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 2); + gst_pad_set_caps (mysrcpad, NULL); + fail_unless (gst_pad_link (mysrcpad, sinkpad) == GST_PAD_LINK_OK, + "Could not link source and volume sink pads"); + gst_object_unref (sinkpad); /* because we got it higher up */ + ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 1); + + /* receiving pad */ + mysinkpad = + gst_pad_new_from_template (gst_static_pad_template_get (&sinktemplate), + "sink"); + fail_if (mysinkpad == NULL, "Could not create a mysinkpad"); + + srcpad = gst_element_get_pad (volume, "src"); + fail_if (srcpad == NULL, "Could not get source pad from volume"); + gst_pad_set_caps (mysinkpad, NULL); + gst_pad_set_chain_function (mysinkpad, chain_func); + gst_pad_set_event_function (mysinkpad, event_func); + + fail_unless (gst_pad_link (srcpad, mysinkpad) == GST_PAD_LINK_OK, + "Could not link volume source and mysink pads"); + gst_object_unref (srcpad); /* because we got it higher up */ + ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 1); + + return volume; +} + +void +cleanup_volume (GstElement * volume) +{ + GstPad *srcpad, *sinkpad; + + GST_DEBUG ("cleanup_volume"); + + fail_unless (gst_element_set_state (volume, GST_STATE_NULL) == + GST_STATE_SUCCESS, "could not set to null"); + ASSERT_OBJECT_REFCOUNT (volume, "volume", 1); + + /* clean up floating src pad */ + sinkpad = gst_element_get_pad (volume, "sink"); + ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 2); + + gst_pad_unlink (mysrcpad, sinkpad); + + /* pad refs held by both creator and this function (through _get) */ + ASSERT_OBJECT_REFCOUNT (mysrcpad, "srcpad", 1); + gst_object_unref (mysrcpad); + mysrcpad = NULL; + + ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 2); + gst_object_unref (sinkpad); + /* one more ref is held by volume itself */ + + /* clean up floating sink pad */ + srcpad = gst_element_get_pad (volume, "src"); + gst_pad_unlink (srcpad, mysinkpad); + + /* pad refs held by both creator and this function (through _get) */ + ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 2); + gst_object_unref (srcpad); + /* one more ref is held by volume itself */ + + ASSERT_OBJECT_REFCOUNT (mysinkpad, "mysinkpad", 1); + gst_object_unref (mysinkpad); + mysinkpad = NULL; + + ASSERT_OBJECT_REFCOUNT (volume, "volume", 1); + gst_object_unref (volume); +} + +GST_START_TEST (test_unity) +{ + GstElement *volume; + GstBuffer *inbuffer, *outbuffer; + gint16 in[2] = { 16384, -256 }; + + volume = setup_volume (); + fail_unless (gst_element_set_state (volume, + GST_STATE_PLAYING) == GST_STATE_SUCCESS, "could not set to playing"); + + inbuffer = gst_buffer_new_and_alloc (4); + memcpy (GST_BUFFER_DATA (inbuffer), in, 4); + gst_buffer_set_caps (inbuffer, gst_caps_from_string (VOLUME_CAPS_STRING)); + ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1); + + /* pushing gives away my reference ... */ + gst_pad_push (mysrcpad, inbuffer); + /* ... but it ends up being collected on the global buffer list */ + ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1); + fail_unless (g_list_length (buffers) == 1); + fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL); + fail_unless (inbuffer == outbuffer); + fail_unless (memcmp (GST_BUFFER_DATA (inbuffer), in, 4) == 0); + + /* cleanup */ + cleanup_volume (volume); +} + +GST_END_TEST; + +GST_START_TEST (test_half) +{ + GstElement *volume; + GstBuffer *inbuffer; + GstBuffer *outbuffer; + gint16 in[2] = { 16384, -256 }; + gint16 out[2] = { 8192, -128 }; + + volume = setup_volume (); + g_object_set (G_OBJECT (volume), "volume", 0.5, NULL); + fail_unless (gst_element_set_state (volume, + GST_STATE_PLAYING) == GST_STATE_SUCCESS, "could not set to playing"); + + inbuffer = gst_buffer_new_and_alloc (4); + memcpy (GST_BUFFER_DATA (inbuffer), in, 4); + fail_unless (memcmp (GST_BUFFER_DATA (inbuffer), in, 4) == 0); + gst_buffer_set_caps (inbuffer, gst_caps_from_string (VOLUME_CAPS_STRING)); + ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1); + /* FIXME: reffing the inbuffer should make the transformation not be + * inplace + gst_buffer_ref (inbuffer); + */ + + /* pushing gives away my reference ... */ + gst_pad_push (mysrcpad, inbuffer); + /* ... but it ends up being modified inplace and + * collected on the global buffer list */ + ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1); + fail_unless (g_list_length (buffers) == 1); + fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL); + fail_unless (inbuffer == outbuffer); + fail_unless (memcmp (GST_BUFFER_DATA (outbuffer), out, 4) == 0); + + /* cleanup */ + cleanup_volume (volume); +} + +GST_END_TEST; + +GST_START_TEST (test_double) +{ + GstElement *volume; + GstBuffer *inbuffer; + GstBuffer *outbuffer; + gint16 in[2] = { 16384, -256 }; + gint16 out[2] = { 32767, -512 }; /* notice the clamped sample */ + + volume = setup_volume (); + g_object_set (G_OBJECT (volume), "volume", 2.0, NULL); + fail_unless (gst_element_set_state (volume, + GST_STATE_PLAYING) == GST_STATE_SUCCESS, "could not set to playing"); + + inbuffer = gst_buffer_new_and_alloc (4); + memcpy (GST_BUFFER_DATA (inbuffer), in, 4); + fail_unless (memcmp (GST_BUFFER_DATA (inbuffer), in, 4) == 0); + gst_buffer_set_caps (inbuffer, gst_caps_from_string (VOLUME_CAPS_STRING)); + ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1); + /* FIXME: reffing the inbuffer should make the transformation not be + * inplace + gst_buffer_ref (inbuffer); + */ + + /* pushing gives away my reference ... */ + gst_pad_push (mysrcpad, inbuffer); + /* ... but it ends up being modified inplace and + * collected on the global buffer list */ + ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1); + fail_unless (g_list_length (buffers) == 1); + fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL); + fail_unless (inbuffer == outbuffer); + fail_unless (memcmp (GST_BUFFER_DATA (outbuffer), out, 4) == 0); + + /* cleanup */ + cleanup_volume (volume); +} + +GST_END_TEST; + + +Suite * +volume_suite (void) +{ + Suite *s = suite_create ("volume"); + TCase *tc_chain = tcase_create ("general"); + + suite_add_tcase (s, tc_chain); + tcase_add_test (tc_chain, test_unity); + tcase_add_test (tc_chain, test_half); + tcase_add_test (tc_chain, test_double); + + return s; +} + +int +main (int argc, char **argv) +{ + int nf; + + Suite *s = volume_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; +}