3 * unit test for audioamplify
5 * Copyright (C) 2007 Sebastian Dröge <slomo@circular-chaos.org>
7 * Greatly based on the audiopanorama unit test
8 * Copyright (C) 2006 Stefan Kost <ensonic@users.sf.net>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
20 * You should have received a copy of the GNU Library General Public
21 * License along with this library; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 02111-1307, USA.
28 #include <gst/base/gstbasetransform.h>
29 #include <gst/check/gstcheck.h>
30 #include <gst/audio/audio.h>
32 gboolean have_eos = FALSE;
34 /* For ease of programming we use globals to keep refs for our floating
35 * src and sink pads we create; otherwise we always have to do get_pad,
36 * get_peer, and then remove references in every test function */
37 GstPad *mysrcpad, *mysinkpad;
40 #define AMPLIFY_CAPS_STRING \
42 "channels = (int) 1, " \
43 "rate = (int) 44100, " \
44 "layout = (string) interleaved, " \
45 "format = (string) " GST_AUDIO_NE(S16)
47 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
50 GST_STATIC_CAPS ("audio/x-raw, "
51 "channels = (int) 1, "
52 "rate = (int) [ 1, MAX ], "
53 "layout = (string) interleaved, "
54 "format = (string) " GST_AUDIO_NE (S16)));
55 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
58 GST_STATIC_CAPS ("audio/x-raw, "
59 "channels = (int) 1, "
60 "rate = (int) [ 1, MAX ], "
61 "layout = (string) interleaved, "
62 "format = (string) " GST_AUDIO_NE (S16)));
69 GST_DEBUG ("setup_amplify");
70 amplify = gst_check_setup_element ("audioamplify");
71 mysrcpad = gst_check_setup_src_pad (amplify, &srctemplate);
72 mysinkpad = gst_check_setup_sink_pad (amplify, &sinktemplate);
73 gst_pad_set_active (mysrcpad, TRUE);
74 gst_pad_set_active (mysinkpad, TRUE);
80 cleanup_amplify (GstElement * amplify)
82 GST_DEBUG ("cleanup_amplify");
84 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
85 g_list_free (buffers);
88 gst_pad_set_active (mysrcpad, FALSE);
89 gst_pad_set_active (mysinkpad, FALSE);
90 gst_check_teardown_src_pad (amplify);
91 gst_check_teardown_sink_pad (amplify);
92 gst_check_teardown_element (amplify);
95 GST_START_TEST (test_passthrough)
98 GstBuffer *inbuffer, *outbuffer;
100 gint16 in[6] = { 24576, -16384, 256, -128, 0, -24576 };
103 amplify = setup_amplify ();
104 fail_unless (gst_element_set_state (amplify,
105 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
106 "could not set to playing");
108 caps = gst_caps_from_string (AMPLIFY_CAPS_STRING);
109 fail_unless (gst_pad_set_caps (mysrcpad, caps));
110 gst_caps_unref (caps);
113 gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, in, 12, 0, 12,
115 fail_unless (gst_buffer_memcmp (inbuffer, 0, in, 12) == 0);
116 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
118 /* pushing gives away my reference ... */
119 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
120 /* ... but it ends up being collected on the global buffer list */
121 fail_unless_equals_int (g_list_length (buffers), 1);
122 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
124 fail_unless (gst_buffer_extract (outbuffer, 0, res, 12) == 12);
126 ("expected %+5d %+5d %+5d %+5d %+5d %+5d real %+5d %+5d %+5d %+5d %+5d %+5d",
127 in[0], in[1], in[2], in[3], in[4], in[5], res[0], res[1], res[2], res[3],
129 fail_unless (gst_buffer_memcmp (outbuffer, 0, in, 12) == 0);
132 cleanup_amplify (amplify);
137 GST_START_TEST (test_zero)
140 GstBuffer *inbuffer, *outbuffer;
142 gint16 in[6] = { 24576, -16384, 256, -128, 0, -24576 };
143 gint16 out[6] = { 0, 0, 0, 0, 0, 0 };
146 amplify = setup_amplify ();
147 g_object_set (G_OBJECT (amplify), "amplification", 0.0, NULL);
148 fail_unless (gst_element_set_state (amplify,
149 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
150 "could not set to playing");
152 caps = gst_caps_from_string (AMPLIFY_CAPS_STRING);
153 fail_unless (gst_pad_set_caps (mysrcpad, caps));
154 gst_caps_unref (caps);
157 gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, in, 12, 0, 12,
159 fail_unless (gst_buffer_memcmp (inbuffer, 0, in, 12) == 0);
160 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
162 /* pushing gives away my reference ... */
163 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
164 /* ... and puts a new buffer on the global list */
165 fail_unless_equals_int (g_list_length (buffers), 1);
166 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
168 fail_unless (gst_buffer_extract (outbuffer, 0, res, 12) == 12);
170 ("expected %+5d %+5d %+5d %+5d %+5d %+5d real %+5d %+5d %+5d %+5d %+5d %+5d",
171 out[0], out[1], out[2], out[3], out[4], out[5], res[0], res[1], res[2],
172 res[3], res[4], res[5]);
173 fail_unless (gst_buffer_memcmp (outbuffer, 0, out, 12) == 0);
176 cleanup_amplify (amplify);
181 GST_START_TEST (test_050_clip)
184 GstBuffer *inbuffer, *outbuffer;
186 gint16 in[6] = { 24576, -16384, 256, -128, 0, -24576 };
187 gint16 out[6] = { 12288, -8192, 128, -64, 0, -12288 };
190 amplify = setup_amplify ();
191 g_object_set (G_OBJECT (amplify), "amplification", 0.5, NULL);
192 fail_unless (gst_element_set_state (amplify,
193 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
194 "could not set to playing");
196 caps = gst_caps_from_string (AMPLIFY_CAPS_STRING);
197 fail_unless (gst_pad_set_caps (mysrcpad, caps));
198 gst_caps_unref (caps);
201 gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, in, 12, 0, 12,
203 fail_unless (gst_buffer_memcmp (inbuffer, 0, in, 12) == 0);
204 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
206 /* pushing gives away my reference ... */
207 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
208 /* ... and puts a new buffer on the global list */
209 fail_unless_equals_int (g_list_length (buffers), 1);
210 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
212 fail_unless (gst_buffer_extract (outbuffer, 0, res, 12) == 12);
214 ("expected %+5d %+5d %+5d %+5d %+5d %+5d real %+5d %+5d %+5d %+5d %+5d %+5d",
215 out[0], out[1], out[2], out[3], out[4], out[5], res[0], res[1], res[2],
216 res[3], res[4], res[5]);
217 fail_unless (gst_buffer_memcmp (outbuffer, 0, out, 12) == 0);
220 cleanup_amplify (amplify);
225 GST_START_TEST (test_200_clip)
228 GstBuffer *inbuffer, *outbuffer;
230 gint16 in[6] = { 24576, -16384, 256, -128, 0, -24576 };
231 gint16 out[6] = { G_MAXINT16, -32768, 512, -256, 0, G_MININT16 };
234 amplify = setup_amplify ();
235 g_object_set (G_OBJECT (amplify), "amplification", 2.0, NULL);
236 fail_unless (gst_element_set_state (amplify,
237 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
238 "could not set to playing");
240 caps = gst_caps_from_string (AMPLIFY_CAPS_STRING);
241 fail_unless (gst_pad_set_caps (mysrcpad, caps));
242 gst_caps_unref (caps);
245 gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, in, 12, 0, 12,
247 fail_unless (gst_buffer_memcmp (inbuffer, 0, in, 12) == 0);
248 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
250 /* pushing gives away my reference ... */
251 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
252 /* ... and puts a new buffer on the global list */
253 fail_unless_equals_int (g_list_length (buffers), 1);
254 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
256 fail_unless (gst_buffer_extract (outbuffer, 0, res, 12) == 12);
258 ("expected %+5d %+5d %+5d %+5d %+5d %+5d real %+5d %+5d %+5d %+5d %+5d %+5d",
259 out[0], out[1], out[2], out[3], out[4], out[5], res[0], res[1], res[2],
260 res[3], res[4], res[5]);
261 fail_unless (gst_buffer_memcmp (outbuffer, 0, out, 12) == 0);
264 cleanup_amplify (amplify);
269 GST_START_TEST (test_050_wrap_negative)
272 GstBuffer *inbuffer, *outbuffer;
274 gint16 in[6] = { 24576, -16384, 256, -128, 0, -24576 };
275 gint16 out[6] = { 12288, -8192, 128, -64, 0, -12288 };
278 amplify = setup_amplify ();
279 g_object_set (G_OBJECT (amplify), "amplification", 0.5, NULL);
280 g_object_set (G_OBJECT (amplify), "clipping-method", 1, NULL);
281 fail_unless (gst_element_set_state (amplify,
282 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
283 "could not set to playing");
285 caps = gst_caps_from_string (AMPLIFY_CAPS_STRING);
286 fail_unless (gst_pad_set_caps (mysrcpad, caps));
287 gst_caps_unref (caps);
290 gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, in, 12, 0, 12,
292 fail_unless (gst_buffer_memcmp (inbuffer, 0, in, 12) == 0);
293 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
295 /* pushing gives away my reference ... */
296 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
297 /* ... and puts a new buffer on the global list */
298 fail_unless_equals_int (g_list_length (buffers), 1);
299 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
301 fail_unless (gst_buffer_extract (outbuffer, 0, res, 12) == 12);
303 ("expected %+5d %+5d %+5d %+5d %+5d %+5d real %+5d %+5d %+5d %+5d %+5d %+5d",
304 out[0], out[1], out[2], out[3], out[4], out[5], res[0], res[1], res[2],
305 res[3], res[4], res[5]);
306 fail_unless (gst_buffer_memcmp (outbuffer, 0, out, 12) == 0);
309 cleanup_amplify (amplify);
314 GST_START_TEST (test_200_wrap_negative)
317 GstBuffer *inbuffer, *outbuffer;
319 gint16 in[6] = { 24576, -16384, 256, -128, 0, -24576 };
320 gint16 out[6] = { -16384, -32768, 512, -256, 0, 16384 };
323 amplify = setup_amplify ();
324 g_object_set (G_OBJECT (amplify), "amplification", 2.0, NULL);
325 g_object_set (G_OBJECT (amplify), "clipping-method", 1, NULL);
326 fail_unless (gst_element_set_state (amplify,
327 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
328 "could not set to playing");
330 caps = gst_caps_from_string (AMPLIFY_CAPS_STRING);
331 fail_unless (gst_pad_set_caps (mysrcpad, caps));
332 gst_caps_unref (caps);
335 gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, in, 12, 0, 12,
337 fail_unless (gst_buffer_memcmp (inbuffer, 0, in, 12) == 0);
338 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
340 /* pushing gives away my reference ... */
341 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
342 /* ... and puts a new buffer on the global list */
343 fail_unless_equals_int (g_list_length (buffers), 1);
344 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
346 fail_unless (gst_buffer_extract (outbuffer, 0, res, 12) == 12);
348 ("expected %+5d %+5d %+5d %+5d %+5d %+5d real %+5d %+5d %+5d %+5d %+5d %+5d",
349 out[0], out[1], out[2], out[3], out[4], out[5], res[0], res[1], res[2],
350 res[3], res[4], res[5]);
351 fail_unless (gst_buffer_memcmp (outbuffer, 0, out, 12) == 0);
354 cleanup_amplify (amplify);
359 GST_START_TEST (test_050_wrap_positive)
362 GstBuffer *inbuffer, *outbuffer;
364 gint16 in[6] = { 24576, -16384, 256, -128, 0, -24576 };
365 gint16 out[6] = { 12288, -8192, 128, -64, 0, -12288 };
368 amplify = setup_amplify ();
369 g_object_set (G_OBJECT (amplify), "amplification", 0.5, NULL);
370 g_object_set (G_OBJECT (amplify), "clipping-method", 2, NULL);
371 fail_unless (gst_element_set_state (amplify,
372 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
373 "could not set to playing");
375 caps = gst_caps_from_string (AMPLIFY_CAPS_STRING);
376 fail_unless (gst_pad_set_caps (mysrcpad, caps));
377 gst_caps_unref (caps);
380 gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, in, 12, 0, 12,
382 fail_unless (gst_buffer_memcmp (inbuffer, 0, in, 12) == 0);
383 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
385 /* pushing gives away my reference ... */
386 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
387 /* ... and puts a new buffer on the global list */
388 fail_unless_equals_int (g_list_length (buffers), 1);
389 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
391 fail_unless (gst_buffer_extract (outbuffer, 0, res, 12) == 12);
393 ("expected %+5d %+5d %+5d %+5d %+5d %+5d real %+5d %+5d %+5d %+5d %+5d %+5d",
394 out[0], out[1], out[2], out[3], out[4], out[5], res[0], res[1], res[2],
395 res[3], res[4], res[5]);
396 fail_unless (gst_buffer_memcmp (outbuffer, 0, out, 12) == 0);
399 cleanup_amplify (amplify);
404 GST_START_TEST (test_200_wrap_positive)
407 GstBuffer *inbuffer, *outbuffer;
409 gint16 in[6] = { 24576, -16384, 256, -128, 0, -24576 };
410 gint16 out[6] = { 16382, -32768, 512, -256, 0, -16384 };
413 amplify = setup_amplify ();
414 g_object_set (G_OBJECT (amplify), "amplification", 2.0, NULL);
415 g_object_set (G_OBJECT (amplify), "clipping-method", 2, NULL);
416 fail_unless (gst_element_set_state (amplify,
417 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
418 "could not set to playing");
420 caps = gst_caps_from_string (AMPLIFY_CAPS_STRING);
421 fail_unless (gst_pad_set_caps (mysrcpad, caps));
422 gst_caps_unref (caps);
425 gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, in, 12, 0, 12,
427 fail_unless (gst_buffer_memcmp (inbuffer, 0, in, 12) == 0);
428 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
430 /* pushing gives away my reference ... */
431 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
432 /* ... and puts a new buffer on the global list */
433 fail_unless_equals_int (g_list_length (buffers), 1);
434 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
436 fail_unless (gst_buffer_extract (outbuffer, 0, res, 12) == 12);
438 ("expected %+5d %+5d %+5d %+5d %+5d %+5d real %+5d %+5d %+5d %+5d %+5d %+5d",
439 out[0], out[1], out[2], out[3], out[4], out[5], res[0], res[1], res[2],
440 res[3], res[4], res[5]);
441 fail_unless (gst_buffer_memcmp (outbuffer, 0, out, 12) == 0);
444 cleanup_amplify (amplify);
452 Suite *s = suite_create ("amplify");
453 TCase *tc_chain = tcase_create ("general");
455 suite_add_tcase (s, tc_chain);
456 tcase_add_test (tc_chain, test_passthrough);
457 tcase_add_test (tc_chain, test_zero);
458 tcase_add_test (tc_chain, test_050_clip);
459 tcase_add_test (tc_chain, test_200_clip);
460 tcase_add_test (tc_chain, test_050_wrap_negative);
461 tcase_add_test (tc_chain, test_200_wrap_negative);
462 tcase_add_test (tc_chain, test_050_wrap_positive);
463 tcase_add_test (tc_chain, test_200_wrap_positive);
468 main (int argc, char **argv)
472 Suite *s = amplify_suite ();
473 SRunner *sr = srunner_create (s);
475 gst_check_init (&argc, &argv);
477 srunner_run_all (sr, CK_NORMAL);
478 nf = srunner_ntests_failed (sr);