test: make more unit tests compile
[platform/upstream/gstreamer.git] / tests / check / elements / rgvolume.c
1 /* GStreamer ReplayGain volume adjustment
2  *
3  * Copyright (C) 2007 Rene Stadler <mail@renestadler.de>
4  *
5  * rgvolume.c: Unit test for the rgvolume element
6  *
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.
11  * 
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.
16  * 
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
20  * 02110-1301 USA
21  */
22
23 #include <gst/check/gstcheck.h>
24 #include <gst/audio/audio.h>
25
26 #include <math.h>
27
28 GList *events = NULL;
29
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;
34
35 #define RG_VOLUME_CAPS_TEMPLATE_STRING        \
36   "audio/x-raw, "                             \
37   "format = (string) "GST_AUDIO_NE (F32) ", " \
38   "channels = (int) [ 1, MAX ], "             \
39   "rate = (int) [ 1, MAX ]"
40
41 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
42     GST_PAD_SINK,
43     GST_PAD_ALWAYS,
44     GST_STATIC_CAPS (RG_VOLUME_CAPS_TEMPLATE_STRING)
45     );
46 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
47     GST_PAD_SRC,
48     GST_PAD_ALWAYS,
49     GST_STATIC_CAPS (RG_VOLUME_CAPS_TEMPLATE_STRING)
50     );
51
52 /* gstcheck sets up a chain function that appends buffers to a global list.
53  * This is our equivalent of that for event handling. */
54 static gboolean
55 event_func (GstPad * pad, GstObject * parent, GstEvent * event)
56 {
57   events = g_list_append (events, event);
58
59   return TRUE;
60 }
61
62 static GstElement *
63 setup_rgvolume (void)
64 {
65   GstElement *element;
66
67   GST_DEBUG ("setup_rgvolume");
68   element = gst_check_setup_element ("rgvolume");
69   mysrcpad = gst_check_setup_src_pad (element, &srctemplate);
70   mysinkpad = gst_check_setup_sink_pad (element, &sinktemplate);
71
72   /* Capture events, to test tag filtering behavior: */
73   gst_pad_set_event_function (mysinkpad, event_func);
74
75   gst_pad_set_active (mysrcpad, TRUE);
76   gst_pad_set_active (mysinkpad, TRUE);
77
78   return element;
79 }
80
81 static void
82 cleanup_rgvolume (GstElement * element)
83 {
84   GST_DEBUG ("cleanup_rgvolume");
85
86   g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
87   g_list_free (buffers);
88   buffers = NULL;
89
90   g_list_foreach (events, (GFunc) gst_mini_object_unref, NULL);
91   g_list_free (events);
92   events = NULL;
93
94   gst_pad_set_active (mysrcpad, FALSE);
95   gst_pad_set_active (mysinkpad, FALSE);
96   gst_check_teardown_src_pad (element);
97   gst_check_teardown_sink_pad (element);
98   gst_check_teardown_element (element);
99 }
100
101 static void
102 set_playing_state (GstElement * element)
103 {
104   fail_unless (gst_element_set_state (element,
105           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
106       "Could not set state to PLAYING");
107 }
108
109 static void
110 set_null_state (GstElement * element)
111 {
112   fail_unless (gst_element_set_state (element,
113           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS,
114       "Could not set state to NULL");
115 }
116
117 static void
118 send_eos_event (GstElement * element)
119 {
120   GstEvent *event = gst_event_new_eos ();
121
122   fail_unless (g_list_length (events) == 0);
123   fail_unless (gst_pad_push_event (mysrcpad, event),
124       "Pushing EOS event failed");
125   fail_unless (g_list_length (events) == 1);
126   fail_unless (events->data == event);
127   gst_mini_object_unref ((GstMiniObject *) events->data);
128   events = g_list_remove (events, event);
129 }
130
131 static GstEvent *
132 send_tag_event (GstElement * element, GstEvent * event)
133 {
134   g_return_val_if_fail (event->type == GST_EVENT_TAG, NULL);
135
136   fail_unless (g_list_length (events) == 0);
137   fail_unless (gst_pad_push_event (mysrcpad, event),
138       "Pushing tag event failed");
139
140   if (g_list_length (events) == 0) {
141     /* Event got filtered out. */
142     event = NULL;
143   } else {
144     GstTagList *tag_list;
145     gdouble dummy;
146
147     event = events->data;
148     events = g_list_remove (events, event);
149
150     fail_unless (event->type == GST_EVENT_TAG);
151     gst_event_parse_tag (event, &tag_list);
152
153     /* The element is supposed to filter out ReplayGain related tags. */
154     fail_if (gst_tag_list_get_double (tag_list, GST_TAG_TRACK_GAIN, &dummy),
155         "tag event still contains track gain tag");
156     fail_if (gst_tag_list_get_double (tag_list, GST_TAG_TRACK_PEAK, &dummy),
157         "tag event still contains track peak tag");
158     fail_if (gst_tag_list_get_double (tag_list, GST_TAG_ALBUM_GAIN, &dummy),
159         "tag event still contains album gain tag");
160     fail_if (gst_tag_list_get_double (tag_list, GST_TAG_ALBUM_PEAK, &dummy),
161         "tag event still contains album peak tag");
162   }
163
164   return event;
165 }
166
167 static GstBuffer *
168 test_buffer_new (gfloat value)
169 {
170   GstBuffer *buf;
171   GstCaps *caps;
172   gfloat *data;
173   gint i;
174
175   buf = gst_buffer_new_and_alloc (8 * sizeof (gfloat));
176   data = gst_buffer_map (buf, NULL, NULL, GST_MAP_WRITE);
177   for (i = 0; i < 8; i++)
178     data[i] = value;
179   gst_buffer_unmap (buf, data, -1);
180
181   caps = gst_caps_from_string ("audio/x-raw-float, "
182       "rate = 8000, channels = 1, endianness = BYTE_ORDER, width = 32");
183   gst_pad_set_caps (mysrcpad, caps);
184   gst_caps_unref (caps);
185
186   ASSERT_BUFFER_REFCOUNT (buf, "buf", 1);
187
188   return buf;
189 }
190
191 #define MATCH_GAIN(g1, g2) ((g1 < g2 + 1e-6) && (g2 < g1 + 1e-6))
192
193 static void
194 fail_unless_target_gain (GstElement * element, gdouble expected_gain)
195 {
196   gdouble prop_gain;
197
198   g_object_get (element, "target-gain", &prop_gain, NULL);
199
200   fail_unless (MATCH_GAIN (prop_gain, expected_gain),
201       "Target gain is %.2f dB, expected %.2f dB", prop_gain, expected_gain);
202 }
203
204 static void
205 fail_unless_result_gain (GstElement * element, gdouble expected_gain)
206 {
207   GstBuffer *input_buf, *output_buf;
208   gfloat *data;
209   gfloat input_sample, output_sample;
210   gdouble gain, prop_gain;
211   gboolean is_passthrough, expect_passthrough;
212   gint i;
213
214   fail_unless (g_list_length (buffers) == 0);
215
216   input_sample = 1.0;
217   input_buf = test_buffer_new (input_sample);
218
219   /* We keep an extra reference to detect passthrough mode. */
220   gst_buffer_ref (input_buf);
221   /* Pushing steals a reference. */
222   fail_unless (gst_pad_push (mysrcpad, input_buf) == GST_FLOW_OK);
223   gst_buffer_unref (input_buf);
224
225   /* The output buffer ends up on the global buffer list. */
226   fail_unless (g_list_length (buffers) == 1);
227   output_buf = buffers->data;
228   fail_if (output_buf == NULL);
229
230   buffers = g_list_remove (buffers, output_buf);
231   ASSERT_BUFFER_REFCOUNT (output_buf, "output_buf", 1);
232
233   fail_unless_equals_int (gst_buffer_get_size (output_buf),
234       8 * sizeof (gfloat));
235
236   data = gst_buffer_map (output_buf, NULL, NULL, GST_MAP_READ);
237
238   output_sample = *data;
239   fail_if (output_sample == 0.0, "First output sample is zero");
240   for (i = 1; i < 8; i++) {
241     fail_unless (output_sample == data[i], "Output samples not uniform");
242   };
243   gst_buffer_unmap (output_buf, data, -1);
244
245   gain = 20. * log10 (output_sample / input_sample);
246   fail_unless (MATCH_GAIN (gain, expected_gain),
247       "Applied gain is %.2f dB, expected %.2f dB", gain, expected_gain);
248   g_object_get (element, "result-gain", &prop_gain, NULL);
249   fail_unless (MATCH_GAIN (prop_gain, expected_gain),
250       "Result gain is %.2f dB, expected %.2f dB", prop_gain, expected_gain);
251
252   is_passthrough = (output_buf == input_buf);
253   expect_passthrough = MATCH_GAIN (expected_gain, +0.00);
254   fail_unless (is_passthrough == expect_passthrough,
255       expect_passthrough
256       ? "Expected operation in passthrough mode"
257       : "Incorrect passthrough behaviour");
258
259   gst_buffer_unref (output_buf);
260 }
261
262 static void
263 fail_unless_gain (GstElement * element, gdouble expected_gain)
264 {
265   fail_unless_target_gain (element, expected_gain);
266   fail_unless_result_gain (element, expected_gain);
267 }
268
269 /* Start of tests. */
270
271 GST_START_TEST (test_no_buffer)
272 {
273   GstElement *element = setup_rgvolume ();
274
275   set_playing_state (element);
276   set_null_state (element);
277   set_playing_state (element);
278   send_eos_event (element);
279
280   cleanup_rgvolume (element);
281 }
282
283 GST_END_TEST;
284
285 GST_START_TEST (test_events)
286 {
287   GstElement *element = setup_rgvolume ();
288   GstEvent *event;
289   GstEvent *new_event;
290   GstTagList *tag_list;
291   gchar *artist;
292
293   set_playing_state (element);
294
295   tag_list = gst_tag_list_new_empty ();
296   gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
297       GST_TAG_TRACK_GAIN, +4.95, GST_TAG_TRACK_PEAK, 0.59463,
298       GST_TAG_ALBUM_GAIN, -1.54, GST_TAG_ALBUM_PEAK, 0.693415,
299       GST_TAG_ARTIST, "Foobar", NULL);
300   event = gst_event_new_tag (tag_list);
301   new_event = send_tag_event (element, event);
302   /* Expect the element to modify the writable event. */
303   fail_unless (event == new_event, "Writable tag event not reused");
304   gst_event_parse_tag (new_event, &tag_list);
305   fail_unless (gst_tag_list_get_string (tag_list, GST_TAG_ARTIST, &artist));
306   fail_unless (g_str_equal (artist, "Foobar"));
307   g_free (artist);
308   gst_event_unref (new_event);
309
310   /* Same as above, but with a non-writable event. */
311
312   tag_list = gst_tag_list_new_empty ();
313   gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
314       GST_TAG_TRACK_GAIN, +4.95, GST_TAG_TRACK_PEAK, 0.59463,
315       GST_TAG_ALBUM_GAIN, -1.54, GST_TAG_ALBUM_PEAK, 0.693415,
316       GST_TAG_ARTIST, "Foobar", NULL);
317   event = gst_event_new_tag (tag_list);
318   /* Holding an extra ref makes the event unwritable: */
319   gst_event_ref (event);
320   new_event = send_tag_event (element, event);
321   fail_unless (event != new_event, "Unwritable tag event reused");
322   gst_event_parse_tag (new_event, &tag_list);
323   fail_unless (gst_tag_list_get_string (tag_list, GST_TAG_ARTIST, &artist));
324   fail_unless (g_str_equal (artist, "Foobar"));
325   g_free (artist);
326   gst_event_unref (event);
327   gst_event_unref (new_event);
328
329   cleanup_rgvolume (element);
330 }
331
332 GST_END_TEST;
333
334 GST_START_TEST (test_simple)
335 {
336   GstElement *element = setup_rgvolume ();
337   GstTagList *tag_list;
338
339   g_object_set (element, "album-mode", FALSE, "headroom", +0.00,
340       "pre-amp", -6.00, "fallback-gain", +1.23, NULL);
341   set_playing_state (element);
342
343   tag_list = gst_tag_list_new_empty ();
344   gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
345       GST_TAG_TRACK_GAIN, -3.45, GST_TAG_TRACK_PEAK, 1.0,
346       GST_TAG_ALBUM_GAIN, +2.09, GST_TAG_ALBUM_PEAK, 1.0, NULL);
347   fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
348   fail_unless_gain (element, -9.45);    /* pre-amp + track gain */
349   send_eos_event (element);
350
351   g_object_set (element, "album-mode", TRUE, NULL);
352
353   tag_list = gst_tag_list_new_empty ();
354   gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
355       GST_TAG_TRACK_GAIN, -3.45, GST_TAG_TRACK_PEAK, 1.0,
356       GST_TAG_ALBUM_GAIN, +2.09, GST_TAG_ALBUM_PEAK, 1.0, NULL);
357   fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
358   fail_unless_gain (element, -3.91);    /* pre-amp + album gain */
359
360   /* Switching back to track mode in the middle of a stream: */
361   g_object_set (element, "album-mode", FALSE, NULL);
362   fail_unless_gain (element, -9.45);    /* pre-amp + track gain */
363   send_eos_event (element);
364
365   cleanup_rgvolume (element);
366 }
367
368 GST_END_TEST;
369
370 /* If there are no gain tags at all, the fallback gain is used. */
371
372 GST_START_TEST (test_fallback_gain)
373 {
374   GstElement *element = setup_rgvolume ();
375   GstTagList *tag_list;
376
377   /* First some track where fallback does _not_ apply. */
378
379   g_object_set (element, "album-mode", FALSE, "headroom", 10.00,
380       "pre-amp", -6.00, "fallback-gain", -3.00, NULL);
381   set_playing_state (element);
382
383   tag_list = gst_tag_list_new_empty ();
384   gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
385       GST_TAG_TRACK_GAIN, +3.5, GST_TAG_TRACK_PEAK, 1.0,
386       GST_TAG_ALBUM_GAIN, -0.5, GST_TAG_ALBUM_PEAK, 1.0, NULL);
387   fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
388   fail_unless_gain (element, -2.50);    /* pre-amp + track gain */
389   send_eos_event (element);
390
391   /* Now a track completely missing tags. */
392
393   fail_unless_gain (element, -9.00);    /* pre-amp + fallback-gain */
394
395   /* Changing the fallback gain in the middle of a stream, going to pass-through
396    * mode: */
397   g_object_set (element, "fallback-gain", +6.00, NULL);
398   fail_unless_gain (element, +0.00);    /* pre-amp + fallback-gain */
399   send_eos_event (element);
400
401   /* Verify that result gain is set to +0.00 with pre-amp + fallback-gain >
402    * +0.00 and no headroom. */
403
404   g_object_set (element, "fallback-gain", +12.00, "headroom", +0.00, NULL);
405   fail_unless_target_gain (element, +6.00);     /* pre-amp + fallback-gain */
406   fail_unless_result_gain (element, +0.00);
407   send_eos_event (element);
408
409   cleanup_rgvolume (element);
410 }
411
412 GST_END_TEST;
413
414 /* If album gain is to be preferred but not available, the track gain is to be
415  * taken instead. */
416
417 GST_START_TEST (test_fallback_track)
418 {
419   GstElement *element = setup_rgvolume ();
420   GstTagList *tag_list;
421
422   g_object_set (element, "album-mode", TRUE, "headroom", +0.00,
423       "pre-amp", -6.00, "fallback-gain", +1.23, NULL);
424   set_playing_state (element);
425
426   tag_list = gst_tag_list_new_empty ();
427   gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
428       GST_TAG_TRACK_GAIN, +2.11, GST_TAG_TRACK_PEAK, 1.0, NULL);
429   fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
430   fail_unless_gain (element, -3.89);    /* pre-amp + track gain */
431
432   send_eos_event (element);
433
434   cleanup_rgvolume (element);
435 }
436
437 GST_END_TEST;
438
439 /* If track gain is to be preferred but not available, the album gain is to be
440  * taken instead. */
441
442 GST_START_TEST (test_fallback_album)
443 {
444   GstElement *element = setup_rgvolume ();
445   GstTagList *tag_list;
446
447   g_object_set (element, "album-mode", FALSE, "headroom", +0.00,
448       "pre-amp", -6.00, "fallback-gain", +1.23, NULL);
449   set_playing_state (element);
450
451   tag_list = gst_tag_list_new_empty ();
452   gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
453       GST_TAG_ALBUM_GAIN, +3.73, GST_TAG_ALBUM_PEAK, 1.0, NULL);
454   fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
455   fail_unless_gain (element, -2.27);    /* pre-amp + album gain */
456
457   send_eos_event (element);
458
459   cleanup_rgvolume (element);
460 }
461
462 GST_END_TEST;
463
464 GST_START_TEST (test_headroom)
465 {
466   GstElement *element = setup_rgvolume ();
467   GstTagList *tag_list;
468
469   g_object_set (element, "album-mode", FALSE, "headroom", +0.00,
470       "pre-amp", +0.00, "fallback-gain", +1.23, NULL);
471   set_playing_state (element);
472
473   tag_list = gst_tag_list_new_empty ();
474   gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
475       GST_TAG_TRACK_GAIN, +3.50, GST_TAG_TRACK_PEAK, 1.0, NULL);
476   fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
477   fail_unless_target_gain (element, +3.50);     /* pre-amp + track gain */
478   fail_unless_result_gain (element, +0.00);
479   send_eos_event (element);
480
481   g_object_set (element, "headroom", +2.00, NULL);
482   tag_list = gst_tag_list_new_empty ();
483   gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
484       GST_TAG_TRACK_GAIN, +9.18, GST_TAG_TRACK_PEAK, 0.687149, NULL);
485   fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
486   fail_unless_target_gain (element, +9.18);     /* pre-amp + track gain */
487   /* Result is 20. * log10 (1. / peak) + headroom. */
488   fail_unless_result_gain (element, 5.2589816238303335);
489   send_eos_event (element);
490
491   g_object_set (element, "album-mode", TRUE, NULL);
492   tag_list = gst_tag_list_new_empty ();
493   gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
494       GST_TAG_ALBUM_GAIN, +5.50, GST_TAG_ALBUM_PEAK, 1.0, NULL);
495   fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
496   fail_unless_target_gain (element, +5.50);     /* pre-amp + album gain */
497   fail_unless_result_gain (element, +2.00);     /* headroom */
498   send_eos_event (element);
499
500   cleanup_rgvolume (element);
501 }
502
503 GST_END_TEST;
504
505 GST_START_TEST (test_reference_level)
506 {
507   GstElement *element = setup_rgvolume ();
508   GstTagList *tag_list;
509
510   g_object_set (element,
511       "album-mode", FALSE,
512       "headroom", +0.00, "pre-amp", +0.00, "fallback-gain", +1.23, NULL);
513   set_playing_state (element);
514
515   tag_list = gst_tag_list_new_empty ();
516   gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
517       GST_TAG_TRACK_GAIN, 0.00, GST_TAG_TRACK_PEAK, 0.2,
518       GST_TAG_REFERENCE_LEVEL, 83., NULL);
519   fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
520   /* Because our authorative reference is 89 dB, we bump it up by +6 dB. */
521   fail_unless_gain (element, +6.00);    /* pre-amp + track gain */
522   send_eos_event (element);
523
524   g_object_set (element, "album-mode", TRUE, NULL);
525
526   /* Same as above, but with album gain. */
527
528   tag_list = gst_tag_list_new_empty ();
529   gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
530       GST_TAG_TRACK_GAIN, 1.23, GST_TAG_TRACK_PEAK, 0.1,
531       GST_TAG_ALBUM_GAIN, 0.00, GST_TAG_ALBUM_PEAK, 0.2,
532       GST_TAG_REFERENCE_LEVEL, 83., NULL);
533   fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
534   fail_unless_gain (element, +6.00);    /* pre-amp + album gain */
535
536   cleanup_rgvolume (element);
537 }
538
539 GST_END_TEST;
540
541 static Suite *
542 rgvolume_suite (void)
543 {
544   Suite *s = suite_create ("rgvolume");
545   TCase *tc_chain = tcase_create ("general");
546
547   suite_add_tcase (s, tc_chain);
548
549   tcase_add_test (tc_chain, test_no_buffer);
550   tcase_add_test (tc_chain, test_events);
551   tcase_add_test (tc_chain, test_simple);
552   tcase_add_test (tc_chain, test_fallback_gain);
553   tcase_add_test (tc_chain, test_fallback_track);
554   tcase_add_test (tc_chain, test_fallback_album);
555   tcase_add_test (tc_chain, test_headroom);
556   tcase_add_test (tc_chain, test_reference_level);
557
558   return s;
559 }
560
561 int
562 main (int argc, char **argv)
563 {
564   gint nf;
565
566   Suite *s = rgvolume_suite ();
567   SRunner *sr = srunner_create (s);
568
569   gst_check_init (&argc, &argv);
570
571   srunner_run_all (sr, CK_ENV);
572   nf = srunner_ntests_failed (sr);
573   srunner_free (sr);
574
575   return nf;
576 }