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., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
26 #include <gst/base/gstbasetransform.h>
27 #include <gst/check/gstcheck.h>
28 #include <gst/audio/audio.h>
30 gboolean have_eos = FALSE;
32 /* For ease of programming we use globals to keep refs for our floating
33 * src and sink pads we create; otherwise we always have to do get_pad,
34 * get_peer, and then remove references in every test function */
35 GstPad *mysrcpad, *mysinkpad;
38 #define AMPLIFY_CAPS_STRING \
40 "channels = (int) 1, " \
41 "rate = (int) 44100, " \
42 "layout = (string) interleaved, " \
43 "format = (string) " GST_AUDIO_NE(S16)
45 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
48 GST_STATIC_CAPS ("audio/x-raw, "
49 "channels = (int) 1, "
50 "rate = (int) [ 1, MAX ], "
51 "layout = (string) interleaved, "
52 "format = (string) " GST_AUDIO_NE (S16)));
53 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
56 GST_STATIC_CAPS ("audio/x-raw, "
57 "channels = (int) 1, "
58 "rate = (int) [ 1, MAX ], "
59 "layout = (string) interleaved, "
60 "format = (string) " GST_AUDIO_NE (S16)));
67 GST_DEBUG ("setup_amplify");
68 amplify = gst_check_setup_element ("audioamplify");
69 mysrcpad = gst_check_setup_src_pad (amplify, &srctemplate);
70 mysinkpad = gst_check_setup_sink_pad (amplify, &sinktemplate);
71 gst_pad_set_active (mysrcpad, TRUE);
72 gst_pad_set_active (mysinkpad, TRUE);
78 cleanup_amplify (GstElement * amplify)
80 GST_DEBUG ("cleanup_amplify");
82 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
83 g_list_free (buffers);
86 gst_pad_set_active (mysrcpad, FALSE);
87 gst_pad_set_active (mysinkpad, FALSE);
88 gst_check_teardown_src_pad (amplify);
89 gst_check_teardown_sink_pad (amplify);
90 gst_check_teardown_element (amplify);
93 GST_START_TEST (test_passthrough)
96 GstBuffer *inbuffer, *outbuffer;
98 gint16 in[6] = { 24576, -16384, 256, -128, 0, -24576 };
101 amplify = setup_amplify ();
102 fail_unless (gst_element_set_state (amplify,
103 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
104 "could not set to playing");
106 caps = gst_caps_from_string (AMPLIFY_CAPS_STRING);
107 gst_check_setup_events (mysrcpad, amplify, caps, GST_FORMAT_TIME);
108 gst_caps_unref (caps);
111 gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, in, 12, 0, 12,
113 fail_unless (gst_buffer_memcmp (inbuffer, 0, in, 12) == 0);
114 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
116 /* pushing gives away my reference ... */
117 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
118 /* ... but it ends up being collected on the global buffer list */
119 fail_unless_equals_int (g_list_length (buffers), 1);
120 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
122 fail_unless (gst_buffer_extract (outbuffer, 0, res, 12) == 12);
124 ("expected %+5d %+5d %+5d %+5d %+5d %+5d real %+5d %+5d %+5d %+5d %+5d %+5d",
125 in[0], in[1], in[2], in[3], in[4], in[5], res[0], res[1], res[2], res[3],
127 fail_unless (gst_buffer_memcmp (outbuffer, 0, in, 12) == 0);
130 cleanup_amplify (amplify);
135 GST_START_TEST (test_zero)
138 GstBuffer *inbuffer, *outbuffer;
140 gint16 in[6] = { 24576, -16384, 256, -128, 0, -24576 };
141 gint16 out[6] = { 0, 0, 0, 0, 0, 0 };
144 amplify = setup_amplify ();
145 g_object_set (G_OBJECT (amplify), "amplification", 0.0, NULL);
146 fail_unless (gst_element_set_state (amplify,
147 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
148 "could not set to playing");
150 caps = gst_caps_from_string (AMPLIFY_CAPS_STRING);
151 gst_check_setup_events (mysrcpad, amplify, caps, GST_FORMAT_TIME);
152 gst_caps_unref (caps);
155 gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, in, 12, 0, 12,
157 fail_unless (gst_buffer_memcmp (inbuffer, 0, in, 12) == 0);
158 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
160 /* pushing gives away my reference ... */
161 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
162 /* ... and puts a new buffer on the global list */
163 fail_unless_equals_int (g_list_length (buffers), 1);
164 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
166 fail_unless (gst_buffer_extract (outbuffer, 0, res, 12) == 12);
168 ("expected %+5d %+5d %+5d %+5d %+5d %+5d real %+5d %+5d %+5d %+5d %+5d %+5d",
169 out[0], out[1], out[2], out[3], out[4], out[5], res[0], res[1], res[2],
170 res[3], res[4], res[5]);
171 fail_unless (gst_buffer_memcmp (outbuffer, 0, out, 12) == 0);
174 cleanup_amplify (amplify);
179 GST_START_TEST (test_050_clip)
182 GstBuffer *inbuffer, *outbuffer;
184 gint16 in[6] = { 24576, -16384, 256, -128, 0, -24576 };
185 gint16 out[6] = { 12288, -8192, 128, -64, 0, -12288 };
188 amplify = setup_amplify ();
189 g_object_set (G_OBJECT (amplify), "amplification", 0.5, NULL);
190 fail_unless (gst_element_set_state (amplify,
191 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
192 "could not set to playing");
194 caps = gst_caps_from_string (AMPLIFY_CAPS_STRING);
195 gst_check_setup_events (mysrcpad, amplify, caps, GST_FORMAT_TIME);
196 gst_caps_unref (caps);
199 gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, in, 12, 0, 12,
201 fail_unless (gst_buffer_memcmp (inbuffer, 0, in, 12) == 0);
202 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
204 /* pushing gives away my reference ... */
205 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
206 /* ... and puts a new buffer on the global list */
207 fail_unless_equals_int (g_list_length (buffers), 1);
208 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
210 fail_unless (gst_buffer_extract (outbuffer, 0, res, 12) == 12);
212 ("expected %+5d %+5d %+5d %+5d %+5d %+5d real %+5d %+5d %+5d %+5d %+5d %+5d",
213 out[0], out[1], out[2], out[3], out[4], out[5], res[0], res[1], res[2],
214 res[3], res[4], res[5]);
215 fail_unless (gst_buffer_memcmp (outbuffer, 0, out, 12) == 0);
218 cleanup_amplify (amplify);
223 GST_START_TEST (test_200_clip)
226 GstBuffer *inbuffer, *outbuffer;
228 gint16 in[6] = { 24576, -16384, 256, -128, 0, -24576 };
229 gint16 out[6] = { G_MAXINT16, -32768, 512, -256, 0, G_MININT16 };
232 amplify = setup_amplify ();
233 g_object_set (G_OBJECT (amplify), "amplification", 2.0, NULL);
234 fail_unless (gst_element_set_state (amplify,
235 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
236 "could not set to playing");
238 caps = gst_caps_from_string (AMPLIFY_CAPS_STRING);
239 gst_check_setup_events (mysrcpad, amplify, caps, GST_FORMAT_TIME);
240 gst_caps_unref (caps);
243 gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, in, 12, 0, 12,
245 fail_unless (gst_buffer_memcmp (inbuffer, 0, in, 12) == 0);
246 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
248 /* pushing gives away my reference ... */
249 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
250 /* ... and puts a new buffer on the global list */
251 fail_unless_equals_int (g_list_length (buffers), 1);
252 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
254 fail_unless (gst_buffer_extract (outbuffer, 0, res, 12) == 12);
256 ("expected %+5d %+5d %+5d %+5d %+5d %+5d real %+5d %+5d %+5d %+5d %+5d %+5d",
257 out[0], out[1], out[2], out[3], out[4], out[5], res[0], res[1], res[2],
258 res[3], res[4], res[5]);
259 fail_unless (gst_buffer_memcmp (outbuffer, 0, out, 12) == 0);
262 cleanup_amplify (amplify);
267 GST_START_TEST (test_050_wrap_negative)
270 GstBuffer *inbuffer, *outbuffer;
272 gint16 in[6] = { 24576, -16384, 256, -128, 0, -24576 };
273 gint16 out[6] = { 12288, -8192, 128, -64, 0, -12288 };
276 amplify = setup_amplify ();
277 g_object_set (G_OBJECT (amplify), "amplification", 0.5, NULL);
278 g_object_set (G_OBJECT (amplify), "clipping-method", 1, NULL);
279 fail_unless (gst_element_set_state (amplify,
280 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
281 "could not set to playing");
283 caps = gst_caps_from_string (AMPLIFY_CAPS_STRING);
284 gst_check_setup_events (mysrcpad, amplify, caps, GST_FORMAT_TIME);
285 gst_caps_unref (caps);
288 gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, in, 12, 0, 12,
290 fail_unless (gst_buffer_memcmp (inbuffer, 0, in, 12) == 0);
291 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
293 /* pushing gives away my reference ... */
294 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
295 /* ... and puts a new buffer on the global list */
296 fail_unless_equals_int (g_list_length (buffers), 1);
297 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
299 fail_unless (gst_buffer_extract (outbuffer, 0, res, 12) == 12);
301 ("expected %+5d %+5d %+5d %+5d %+5d %+5d real %+5d %+5d %+5d %+5d %+5d %+5d",
302 out[0], out[1], out[2], out[3], out[4], out[5], res[0], res[1], res[2],
303 res[3], res[4], res[5]);
304 fail_unless (gst_buffer_memcmp (outbuffer, 0, out, 12) == 0);
307 cleanup_amplify (amplify);
312 GST_START_TEST (test_200_wrap_negative)
315 GstBuffer *inbuffer, *outbuffer;
317 gint16 in[6] = { 24576, -16384, 256, -128, 0, -24576 };
318 gint16 out[6] = { -16384, -32768, 512, -256, 0, 16384 };
321 amplify = setup_amplify ();
322 g_object_set (G_OBJECT (amplify), "amplification", 2.0, NULL);
323 g_object_set (G_OBJECT (amplify), "clipping-method", 1, NULL);
324 fail_unless (gst_element_set_state (amplify,
325 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
326 "could not set to playing");
328 caps = gst_caps_from_string (AMPLIFY_CAPS_STRING);
329 gst_check_setup_events (mysrcpad, amplify, caps, GST_FORMAT_TIME);
330 gst_caps_unref (caps);
333 gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, in, 12, 0, 12,
335 fail_unless (gst_buffer_memcmp (inbuffer, 0, in, 12) == 0);
336 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
338 /* pushing gives away my reference ... */
339 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
340 /* ... and puts a new buffer on the global list */
341 fail_unless_equals_int (g_list_length (buffers), 1);
342 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
344 fail_unless (gst_buffer_extract (outbuffer, 0, res, 12) == 12);
346 ("expected %+5d %+5d %+5d %+5d %+5d %+5d real %+5d %+5d %+5d %+5d %+5d %+5d",
347 out[0], out[1], out[2], out[3], out[4], out[5], res[0], res[1], res[2],
348 res[3], res[4], res[5]);
349 fail_unless (gst_buffer_memcmp (outbuffer, 0, out, 12) == 0);
352 cleanup_amplify (amplify);
357 GST_START_TEST (test_050_wrap_positive)
360 GstBuffer *inbuffer, *outbuffer;
362 gint16 in[6] = { 24576, -16384, 256, -128, 0, -24576 };
363 gint16 out[6] = { 12288, -8192, 128, -64, 0, -12288 };
366 amplify = setup_amplify ();
367 g_object_set (G_OBJECT (amplify), "amplification", 0.5, NULL);
368 g_object_set (G_OBJECT (amplify), "clipping-method", 2, NULL);
369 fail_unless (gst_element_set_state (amplify,
370 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
371 "could not set to playing");
373 caps = gst_caps_from_string (AMPLIFY_CAPS_STRING);
374 gst_check_setup_events (mysrcpad, amplify, caps, GST_FORMAT_TIME);
375 gst_caps_unref (caps);
378 gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, in, 12, 0, 12,
380 fail_unless (gst_buffer_memcmp (inbuffer, 0, in, 12) == 0);
381 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
383 /* pushing gives away my reference ... */
384 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
385 /* ... and puts a new buffer on the global list */
386 fail_unless_equals_int (g_list_length (buffers), 1);
387 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
389 fail_unless (gst_buffer_extract (outbuffer, 0, res, 12) == 12);
391 ("expected %+5d %+5d %+5d %+5d %+5d %+5d real %+5d %+5d %+5d %+5d %+5d %+5d",
392 out[0], out[1], out[2], out[3], out[4], out[5], res[0], res[1], res[2],
393 res[3], res[4], res[5]);
394 fail_unless (gst_buffer_memcmp (outbuffer, 0, out, 12) == 0);
397 cleanup_amplify (amplify);
402 GST_START_TEST (test_200_wrap_positive)
405 GstBuffer *inbuffer, *outbuffer;
407 gint16 in[6] = { 24576, -16384, 256, -128, 0, -24576 };
408 gint16 out[6] = { 16382, -32768, 512, -256, 0, -16384 };
411 amplify = setup_amplify ();
412 g_object_set (G_OBJECT (amplify), "amplification", 2.0, NULL);
413 g_object_set (G_OBJECT (amplify), "clipping-method", 2, NULL);
414 fail_unless (gst_element_set_state (amplify,
415 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
416 "could not set to playing");
418 caps = gst_caps_from_string (AMPLIFY_CAPS_STRING);
419 gst_check_setup_events (mysrcpad, amplify, caps, GST_FORMAT_TIME);
420 gst_caps_unref (caps);
423 gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, in, 12, 0, 12,
425 fail_unless (gst_buffer_memcmp (inbuffer, 0, in, 12) == 0);
426 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
428 /* pushing gives away my reference ... */
429 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
430 /* ... and puts a new buffer on the global list */
431 fail_unless_equals_int (g_list_length (buffers), 1);
432 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
434 fail_unless (gst_buffer_extract (outbuffer, 0, res, 12) == 12);
436 ("expected %+5d %+5d %+5d %+5d %+5d %+5d real %+5d %+5d %+5d %+5d %+5d %+5d",
437 out[0], out[1], out[2], out[3], out[4], out[5], res[0], res[1], res[2],
438 res[3], res[4], res[5]);
439 fail_unless (gst_buffer_memcmp (outbuffer, 0, out, 12) == 0);
442 cleanup_amplify (amplify);
450 Suite *s = suite_create ("amplify");
451 TCase *tc_chain = tcase_create ("general");
453 suite_add_tcase (s, tc_chain);
454 tcase_add_test (tc_chain, test_passthrough);
455 tcase_add_test (tc_chain, test_zero);
456 tcase_add_test (tc_chain, test_050_clip);
457 tcase_add_test (tc_chain, test_200_clip);
458 tcase_add_test (tc_chain, test_050_wrap_negative);
459 tcase_add_test (tc_chain, test_200_wrap_negative);
460 tcase_add_test (tc_chain, test_050_wrap_positive);
461 tcase_add_test (tc_chain, test_200_wrap_positive);
465 GST_CHECK_MAIN (amplify);