1 /* GStreamer ReplayGain volume adjustment
3 * Copyright (C) 2007 Rene Stadler <mail@renestadler.de>
5 * rgvolume.c: Unit test for the rgvolume element
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * as published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 #include <gst/check/gstcheck.h>
24 #include <gst/audio/audio.h>
30 /* For ease of programming we use globals to keep refs for our floating src and
31 * sink pads we create; otherwise we always have to do get_pad, get_peer, and
32 * then remove references in every test function */
33 static GstPad *mysrcpad, *mysinkpad;
35 #define RG_VOLUME_CAPS_TEMPLATE_STRING \
37 "format = (string) "GST_AUDIO_NE (F32) ", " \
38 "channels = (int) [ 1, MAX ], " \
39 "rate = (int) [ 1, MAX ]"
41 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
44 GST_STATIC_CAPS (RG_VOLUME_CAPS_TEMPLATE_STRING)
46 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
49 GST_STATIC_CAPS (RG_VOLUME_CAPS_TEMPLATE_STRING)
52 static GstBuffer *test_buffer_new (gfloat value);
54 /* gstcheck sets up a chain function that appends buffers to a global list.
55 * This is our equivalent of that for event handling. */
57 event_func (GstPad * pad, GstObject * parent, GstEvent * event)
59 events = g_list_append (events, event);
69 GST_DEBUG ("setup_rgvolume");
70 element = gst_check_setup_element ("rgvolume");
71 mysrcpad = gst_check_setup_src_pad (element, &srctemplate);
72 mysinkpad = gst_check_setup_sink_pad (element, &sinktemplate);
74 /* Capture events, to test tag filtering behavior: */
75 gst_pad_set_event_function (mysinkpad, event_func);
77 gst_pad_set_active (mysrcpad, TRUE);
78 gst_pad_set_active (mysinkpad, TRUE);
84 send_newsegment_and_empty_buffer (void)
90 fail_unless (g_list_length (events) == 0);
92 gst_segment_init (&segment, GST_FORMAT_TIME);
93 ev = gst_event_new_segment (&segment);
94 fail_unless (gst_pad_push_event (mysrcpad, ev),
95 "Pushing newsegment event failed");
97 buf = test_buffer_new (0.0);
98 GST_BUFFER_SIZE (buf) = 0;
99 GST_BUFFER_DURATION (buf) = 0;
100 GST_BUFFER_OFFSET_END (buf) = GST_BUFFER_OFFSET (buf);
101 fail_unless (gst_pad_push (mysrcpad, buf) == GST_FLOW_OK);
103 fail_unless (g_list_length (events) == 1);
104 fail_unless (events->data == ev);
105 gst_mini_object_unref ((GstMiniObject *) events->data);
106 events = g_list_remove (events, ev);
108 fail_unless (g_list_length (buffers) == 1);
109 fail_unless (buffers->data == buf);
110 gst_mini_object_unref ((GstMiniObject *) buffers->data);
111 buffers = g_list_remove (buffers, buf);
115 cleanup_rgvolume (GstElement * element)
117 GST_DEBUG ("cleanup_rgvolume");
119 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
120 g_list_free (buffers);
123 g_list_foreach (events, (GFunc) gst_mini_object_unref, NULL);
124 g_list_free (events);
127 gst_pad_set_active (mysrcpad, FALSE);
128 gst_pad_set_active (mysinkpad, FALSE);
129 gst_check_teardown_src_pad (element);
130 gst_check_teardown_sink_pad (element);
131 gst_check_teardown_element (element);
135 set_playing_state (GstElement * element)
137 fail_unless (gst_element_set_state (element,
138 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
139 "Could not set state to PLAYING");
143 set_null_state (GstElement * element)
145 fail_unless (gst_element_set_state (element,
146 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS,
147 "Could not set state to NULL");
151 send_eos_event (GstElement * element)
153 GstEvent *event = gst_event_new_eos ();
155 fail_unless (g_list_length (events) == 0);
156 fail_unless (gst_pad_push_event (mysrcpad, event),
157 "Pushing EOS event failed");
158 fail_unless (g_list_length (events) == 1);
159 fail_unless (events->data == event);
160 gst_mini_object_unref ((GstMiniObject *) events->data);
161 events = g_list_remove (events, event);
165 send_tag_event (GstElement * element, GstEvent * event)
167 g_return_val_if_fail (event->type == GST_EVENT_TAG, NULL);
169 fail_unless (g_list_length (events) == 0);
170 fail_unless (gst_pad_push_event (mysrcpad, event),
171 "Pushing tag event failed");
173 if (g_list_length (events) == 0) {
174 /* Event got filtered out. */
177 GstTagList *tag_list;
180 event = events->data;
181 events = g_list_remove (events, event);
183 fail_unless (event->type == GST_EVENT_TAG);
184 gst_event_parse_tag (event, &tag_list);
186 /* The element is supposed to filter out ReplayGain related tags. */
187 fail_if (gst_tag_list_get_double (tag_list, GST_TAG_TRACK_GAIN, &dummy),
188 "tag event still contains track gain tag");
189 fail_if (gst_tag_list_get_double (tag_list, GST_TAG_TRACK_PEAK, &dummy),
190 "tag event still contains track peak tag");
191 fail_if (gst_tag_list_get_double (tag_list, GST_TAG_ALBUM_GAIN, &dummy),
192 "tag event still contains album gain tag");
193 fail_if (gst_tag_list_get_double (tag_list, GST_TAG_ALBUM_PEAK, &dummy),
194 "tag event still contains album peak tag");
201 test_buffer_new (gfloat value)
208 buf = gst_buffer_new_and_alloc (8 * sizeof (gfloat));
209 data = gst_buffer_map (buf, NULL, NULL, GST_MAP_WRITE);
210 for (i = 0; i < 8; i++)
212 gst_buffer_unmap (buf, data, -1);
214 caps = gst_caps_from_string ("audio/x-raw-float, "
215 "rate = 8000, channels = 1, endianness = BYTE_ORDER, width = 32");
216 gst_pad_set_caps (mysrcpad, caps);
217 gst_caps_unref (caps);
219 ASSERT_BUFFER_REFCOUNT (buf, "buf", 1);
224 #define MATCH_GAIN(g1, g2) ((g1 < g2 + 1e-6) && (g2 < g1 + 1e-6))
227 fail_unless_target_gain (GstElement * element, gdouble expected_gain)
231 g_object_get (element, "target-gain", &prop_gain, NULL);
233 fail_unless (MATCH_GAIN (prop_gain, expected_gain),
234 "Target gain is %.2f dB, expected %.2f dB", prop_gain, expected_gain);
238 fail_unless_result_gain (GstElement * element, gdouble expected_gain)
240 GstBuffer *input_buf, *output_buf;
242 gfloat input_sample, output_sample;
243 gdouble gain, prop_gain;
244 gboolean is_passthrough, expect_passthrough;
247 fail_unless (g_list_length (buffers) == 0);
250 input_buf = test_buffer_new (input_sample);
252 /* We keep an extra reference to detect passthrough mode. */
253 gst_buffer_ref (input_buf);
254 /* Pushing steals a reference. */
255 fail_unless (gst_pad_push (mysrcpad, input_buf) == GST_FLOW_OK);
256 gst_buffer_unref (input_buf);
258 /* The output buffer ends up on the global buffer list. */
259 fail_unless (g_list_length (buffers) == 1);
260 output_buf = buffers->data;
261 fail_if (output_buf == NULL);
263 buffers = g_list_remove (buffers, output_buf);
264 ASSERT_BUFFER_REFCOUNT (output_buf, "output_buf", 1);
266 fail_unless_equals_int (gst_buffer_get_size (output_buf),
267 8 * sizeof (gfloat));
269 data = gst_buffer_map (output_buf, NULL, NULL, GST_MAP_READ);
271 output_sample = *data;
272 fail_if (output_sample == 0.0, "First output sample is zero");
273 for (i = 1; i < 8; i++) {
274 fail_unless (output_sample == data[i], "Output samples not uniform");
276 gst_buffer_unmap (output_buf, data, -1);
278 gain = 20. * log10 (output_sample / input_sample);
279 fail_unless (MATCH_GAIN (gain, expected_gain),
280 "Applied gain is %.2f dB, expected %.2f dB", gain, expected_gain);
281 g_object_get (element, "result-gain", &prop_gain, NULL);
282 fail_unless (MATCH_GAIN (prop_gain, expected_gain),
283 "Result gain is %.2f dB, expected %.2f dB", prop_gain, expected_gain);
285 is_passthrough = (output_buf == input_buf);
286 expect_passthrough = MATCH_GAIN (expected_gain, +0.00);
287 fail_unless (is_passthrough == expect_passthrough,
289 ? "Expected operation in passthrough mode"
290 : "Incorrect passthrough behaviour");
292 gst_buffer_unref (output_buf);
296 fail_unless_gain (GstElement * element, gdouble expected_gain)
298 fail_unless_target_gain (element, expected_gain);
299 fail_unless_result_gain (element, expected_gain);
302 /* Start of tests. */
304 GST_START_TEST (test_no_buffer)
306 GstElement *element = setup_rgvolume ();
308 set_playing_state (element);
309 set_null_state (element);
310 set_playing_state (element);
311 send_eos_event (element);
313 cleanup_rgvolume (element);
318 GST_START_TEST (test_events)
320 GstElement *element = setup_rgvolume ();
323 GstTagList *tag_list;
326 set_playing_state (element);
328 send_newsegment_and_empty_buffer ();
330 tag_list = gst_tag_list_new_empty ();
331 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
332 GST_TAG_TRACK_GAIN, +4.95, GST_TAG_TRACK_PEAK, 0.59463,
333 GST_TAG_ALBUM_GAIN, -1.54, GST_TAG_ALBUM_PEAK, 0.693415,
334 GST_TAG_ARTIST, "Foobar", NULL);
335 event = gst_event_new_tag (tag_list);
336 new_event = send_tag_event (element, event);
337 /* Expect the element to modify the writable event. */
338 fail_unless (event == new_event, "Writable tag event not reused");
339 gst_event_parse_tag (new_event, &tag_list);
340 fail_unless (gst_tag_list_get_string (tag_list, GST_TAG_ARTIST, &artist));
341 fail_unless (g_str_equal (artist, "Foobar"));
343 gst_event_unref (new_event);
345 /* Same as above, but with a non-writable event. */
347 tag_list = gst_tag_list_new_empty ();
348 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
349 GST_TAG_TRACK_GAIN, +4.95, GST_TAG_TRACK_PEAK, 0.59463,
350 GST_TAG_ALBUM_GAIN, -1.54, GST_TAG_ALBUM_PEAK, 0.693415,
351 GST_TAG_ARTIST, "Foobar", NULL);
352 event = gst_event_new_tag (tag_list);
353 /* Holding an extra ref makes the event unwritable: */
354 gst_event_ref (event);
355 new_event = send_tag_event (element, event);
356 fail_unless (event != new_event, "Unwritable tag event reused");
357 gst_event_parse_tag (new_event, &tag_list);
358 fail_unless (gst_tag_list_get_string (tag_list, GST_TAG_ARTIST, &artist));
359 fail_unless (g_str_equal (artist, "Foobar"));
361 gst_event_unref (event);
362 gst_event_unref (new_event);
364 cleanup_rgvolume (element);
369 GST_START_TEST (test_simple)
371 GstElement *element = setup_rgvolume ();
372 GstTagList *tag_list;
374 g_object_set (element, "album-mode", FALSE, "headroom", +0.00,
375 "pre-amp", -6.00, "fallback-gain", +1.23, NULL);
376 set_playing_state (element);
378 send_newsegment_and_empty_buffer ();
380 tag_list = gst_tag_list_new_empty ();
381 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
382 GST_TAG_TRACK_GAIN, -3.45, GST_TAG_TRACK_PEAK, 1.0,
383 GST_TAG_ALBUM_GAIN, +2.09, GST_TAG_ALBUM_PEAK, 1.0, NULL);
384 fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
385 fail_unless_gain (element, -9.45); /* pre-amp + track gain */
386 send_eos_event (element);
388 g_object_set (element, "album-mode", TRUE, NULL);
390 tag_list = gst_tag_list_new_empty ();
391 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
392 GST_TAG_TRACK_GAIN, -3.45, GST_TAG_TRACK_PEAK, 1.0,
393 GST_TAG_ALBUM_GAIN, +2.09, GST_TAG_ALBUM_PEAK, 1.0, NULL);
394 fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
395 fail_unless_gain (element, -3.91); /* pre-amp + album gain */
397 /* Switching back to track mode in the middle of a stream: */
398 g_object_set (element, "album-mode", FALSE, NULL);
399 fail_unless_gain (element, -9.45); /* pre-amp + track gain */
400 send_eos_event (element);
402 cleanup_rgvolume (element);
407 /* If there are no gain tags at all, the fallback gain is used. */
409 GST_START_TEST (test_fallback_gain)
411 GstElement *element = setup_rgvolume ();
412 GstTagList *tag_list;
414 /* First some track where fallback does _not_ apply. */
416 g_object_set (element, "album-mode", FALSE, "headroom", 10.00,
417 "pre-amp", -6.00, "fallback-gain", -3.00, NULL);
418 set_playing_state (element);
420 send_newsegment_and_empty_buffer ();
422 tag_list = gst_tag_list_new_empty ();
423 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
424 GST_TAG_TRACK_GAIN, +3.5, GST_TAG_TRACK_PEAK, 1.0,
425 GST_TAG_ALBUM_GAIN, -0.5, GST_TAG_ALBUM_PEAK, 1.0, NULL);
426 fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
427 fail_unless_gain (element, -2.50); /* pre-amp + track gain */
428 send_eos_event (element);
430 /* Now a track completely missing tags. */
432 fail_unless_gain (element, -9.00); /* pre-amp + fallback-gain */
434 /* Changing the fallback gain in the middle of a stream, going to pass-through
436 g_object_set (element, "fallback-gain", +6.00, NULL);
437 fail_unless_gain (element, +0.00); /* pre-amp + fallback-gain */
438 send_eos_event (element);
440 /* Verify that result gain is set to +0.00 with pre-amp + fallback-gain >
441 * +0.00 and no headroom. */
443 g_object_set (element, "fallback-gain", +12.00, "headroom", +0.00, NULL);
444 fail_unless_target_gain (element, +6.00); /* pre-amp + fallback-gain */
445 fail_unless_result_gain (element, +0.00);
446 send_eos_event (element);
448 cleanup_rgvolume (element);
453 /* If album gain is to be preferred but not available, the track gain is to be
456 GST_START_TEST (test_fallback_track)
458 GstElement *element = setup_rgvolume ();
459 GstTagList *tag_list;
461 g_object_set (element, "album-mode", TRUE, "headroom", +0.00,
462 "pre-amp", -6.00, "fallback-gain", +1.23, NULL);
463 set_playing_state (element);
465 send_newsegment_and_empty_buffer ();
467 tag_list = gst_tag_list_new_empty ();
468 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
469 GST_TAG_TRACK_GAIN, +2.11, GST_TAG_TRACK_PEAK, 1.0, NULL);
470 fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
471 fail_unless_gain (element, -3.89); /* pre-amp + track gain */
473 send_eos_event (element);
475 cleanup_rgvolume (element);
480 /* If track gain is to be preferred but not available, the album gain is to be
483 GST_START_TEST (test_fallback_album)
485 GstElement *element = setup_rgvolume ();
486 GstTagList *tag_list;
488 g_object_set (element, "album-mode", FALSE, "headroom", +0.00,
489 "pre-amp", -6.00, "fallback-gain", +1.23, NULL);
490 set_playing_state (element);
492 send_newsegment_and_empty_buffer ();
494 tag_list = gst_tag_list_new_empty ();
495 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
496 GST_TAG_ALBUM_GAIN, +3.73, GST_TAG_ALBUM_PEAK, 1.0, NULL);
497 fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
498 fail_unless_gain (element, -2.27); /* pre-amp + album gain */
500 send_eos_event (element);
502 cleanup_rgvolume (element);
507 GST_START_TEST (test_headroom)
509 GstElement *element = setup_rgvolume ();
510 GstTagList *tag_list;
512 g_object_set (element, "album-mode", FALSE, "headroom", +0.00,
513 "pre-amp", +0.00, "fallback-gain", +1.23, NULL);
514 set_playing_state (element);
516 send_newsegment_and_empty_buffer ();
518 tag_list = gst_tag_list_new_empty ();
519 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
520 GST_TAG_TRACK_GAIN, +3.50, GST_TAG_TRACK_PEAK, 1.0, NULL);
521 fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
522 fail_unless_target_gain (element, +3.50); /* pre-amp + track gain */
523 fail_unless_result_gain (element, +0.00);
524 send_eos_event (element);
526 g_object_set (element, "headroom", +2.00, NULL);
527 tag_list = gst_tag_list_new_empty ();
528 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
529 GST_TAG_TRACK_GAIN, +9.18, GST_TAG_TRACK_PEAK, 0.687149, NULL);
530 fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
531 fail_unless_target_gain (element, +9.18); /* pre-amp + track gain */
532 /* Result is 20. * log10 (1. / peak) + headroom. */
533 fail_unless_result_gain (element, 5.2589816238303335);
534 send_eos_event (element);
536 g_object_set (element, "album-mode", TRUE, NULL);
537 tag_list = gst_tag_list_new_empty ();
538 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
539 GST_TAG_ALBUM_GAIN, +5.50, GST_TAG_ALBUM_PEAK, 1.0, NULL);
540 fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
541 fail_unless_target_gain (element, +5.50); /* pre-amp + album gain */
542 fail_unless_result_gain (element, +2.00); /* headroom */
543 send_eos_event (element);
545 cleanup_rgvolume (element);
550 GST_START_TEST (test_reference_level)
552 GstElement *element = setup_rgvolume ();
553 GstTagList *tag_list;
555 g_object_set (element,
557 "headroom", +0.00, "pre-amp", +0.00, "fallback-gain", +1.23, NULL);
558 set_playing_state (element);
560 send_newsegment_and_empty_buffer ();
562 tag_list = gst_tag_list_new_empty ();
563 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
564 GST_TAG_TRACK_GAIN, 0.00, GST_TAG_TRACK_PEAK, 0.2,
565 GST_TAG_REFERENCE_LEVEL, 83., NULL);
566 fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
567 /* Because our authorative reference is 89 dB, we bump it up by +6 dB. */
568 fail_unless_gain (element, +6.00); /* pre-amp + track gain */
569 send_eos_event (element);
571 g_object_set (element, "album-mode", TRUE, NULL);
573 /* Same as above, but with album gain. */
575 tag_list = gst_tag_list_new_empty ();
576 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
577 GST_TAG_TRACK_GAIN, 1.23, GST_TAG_TRACK_PEAK, 0.1,
578 GST_TAG_ALBUM_GAIN, 0.00, GST_TAG_ALBUM_PEAK, 0.2,
579 GST_TAG_REFERENCE_LEVEL, 83., NULL);
580 fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
581 fail_unless_gain (element, +6.00); /* pre-amp + album gain */
583 cleanup_rgvolume (element);
589 rgvolume_suite (void)
591 Suite *s = suite_create ("rgvolume");
592 TCase *tc_chain = tcase_create ("general");
594 suite_add_tcase (s, tc_chain);
596 tcase_add_test (tc_chain, test_no_buffer);
597 tcase_add_test (tc_chain, test_events);
598 tcase_add_test (tc_chain, test_simple);
599 tcase_add_test (tc_chain, test_fallback_gain);
600 tcase_add_test (tc_chain, test_fallback_track);
601 tcase_add_test (tc_chain, test_fallback_album);
602 tcase_add_test (tc_chain, test_headroom);
603 tcase_add_test (tc_chain, test_reference_level);
609 main (int argc, char **argv)
613 Suite *s = rgvolume_suite ();
614 SRunner *sr = srunner_create (s);
616 gst_check_init (&argc, &argv);
618 srunner_run_all (sr, CK_ENV);
619 nf = srunner_ntests_failed (sr);