Bump GLib requirement to >= 2.62
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-base / tests / check / elements / audioresample.c
1 /* GStreamer
2  *
3  * unit test for audioresample, based on the audioresample unit test
4  *
5  * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
6  * Copyright (C) <2006> Tim-Philipp Müller <tim at centricular net>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <gst/check/gstcheck.h>
28
29 #include <gst/audio/audio.h>
30
31 #include <gst/fft/gstfft.h>
32 #include <gst/fft/gstffts16.h>
33 #include <gst/fft/gstffts32.h>
34 #include <gst/fft/gstfftf32.h>
35 #include <gst/fft/gstfftf64.h>
36
37 /* For ease of programming we use globals to keep refs for our floating
38  * src and sink pads we create; otherwise we always have to do get_pad,
39  * get_peer, and then remove references in every test function */
40 static GstPad *mysrcpad, *mysinkpad;
41
42 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
43 #define FORMATS  "{ F32LE, F64LE, S16LE, S32LE }"
44 #else
45 #define FORMATS  "{ F32BE, F64BE, S16BE, S32BE }"
46 #endif
47
48 #define RESAMPLE_CAPS                   \
49     "audio/x-raw, "                     \
50     "format = (string) "FORMATS", "     \
51     "channels = (int) [ 1, MAX ], "     \
52     "rate = (int) [ 1,  MAX ], "        \
53     "layout = (string) interleaved"
54
55 static GstElement *
56 setup_audioresample (int channels, guint64 mask, int inrate, int outrate,
57     const gchar * format)
58 {
59   GstPadTemplate *sinktemplate;
60   static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
61       GST_PAD_SRC,
62       GST_PAD_ALWAYS,
63       GST_STATIC_CAPS (RESAMPLE_CAPS)
64       );
65   GstElement *audioresample;
66   GstCaps *caps;
67   GstStructure *structure;
68
69   GST_DEBUG ("setup_audioresample");
70   audioresample = gst_check_setup_element ("audioresample");
71
72   caps = gst_caps_from_string (RESAMPLE_CAPS);
73   structure = gst_caps_get_structure (caps, 0);
74   gst_structure_set (structure, "channels", G_TYPE_INT, channels,
75       "rate", G_TYPE_INT, inrate, "format", G_TYPE_STRING, format,
76       "channel-mask", GST_TYPE_BITMASK, mask, NULL);
77   fail_unless (gst_caps_is_fixed (caps));
78
79   fail_unless (gst_element_set_state (audioresample,
80           GST_STATE_PAUSED) == GST_STATE_CHANGE_SUCCESS,
81       "could not set to paused");
82
83   mysrcpad = gst_check_setup_src_pad (audioresample, &srctemplate);
84   gst_pad_set_active (mysrcpad, TRUE);
85   gst_check_setup_events (mysrcpad, audioresample, caps, GST_FORMAT_TIME);
86   gst_caps_unref (caps);
87
88   caps = gst_caps_from_string (RESAMPLE_CAPS);
89   structure = gst_caps_get_structure (caps, 0);
90   gst_structure_set (structure, "channels", G_TYPE_INT, channels,
91       "rate", G_TYPE_INT, outrate, "format", G_TYPE_STRING, format, NULL);
92   fail_unless (gst_caps_is_fixed (caps));
93   sinktemplate =
94       gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, caps);
95
96   mysinkpad =
97       gst_check_setup_sink_pad_from_template (audioresample, sinktemplate);
98   gst_pad_set_active (mysinkpad, TRUE);
99   /* this installs a getcaps func that will always return the caps we set
100    * later */
101   gst_pad_use_fixed_caps (mysinkpad);
102
103   gst_caps_unref (caps);
104   gst_object_unref (sinktemplate);
105
106   return audioresample;
107 }
108
109 static void
110 cleanup_audioresample (GstElement * audioresample)
111 {
112   GST_DEBUG ("cleanup_audioresample");
113
114   fail_unless (gst_element_set_state (audioresample,
115           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to NULL");
116
117   gst_pad_set_active (mysrcpad, FALSE);
118   gst_pad_set_active (mysinkpad, FALSE);
119   gst_check_teardown_src_pad (audioresample);
120   gst_check_teardown_sink_pad (audioresample);
121   gst_check_teardown_element (audioresample);
122   gst_check_drop_buffers ();
123 }
124
125 static void
126 fail_unless_perfect_stream (void)
127 {
128   guint64 timestamp = 0L, duration = 0L;
129   guint64 offset = 0L, offset_end = 0L;
130
131   GList *l;
132   GstBuffer *buffer;
133
134   for (l = buffers; l; l = l->next) {
135     buffer = GST_BUFFER (l->data);
136     ASSERT_BUFFER_REFCOUNT (buffer, "buffer", 1);
137     GST_DEBUG ("buffer timestamp %" G_GUINT64_FORMAT ", duration %"
138         G_GUINT64_FORMAT " offset %" G_GUINT64_FORMAT " offset_end %"
139         G_GUINT64_FORMAT,
140         GST_BUFFER_TIMESTAMP (buffer),
141         GST_BUFFER_DURATION (buffer),
142         GST_BUFFER_OFFSET (buffer), GST_BUFFER_OFFSET_END (buffer));
143
144     fail_unless_equals_uint64 (timestamp, GST_BUFFER_TIMESTAMP (buffer));
145     fail_unless_equals_uint64 (offset, GST_BUFFER_OFFSET (buffer));
146     duration = GST_BUFFER_DURATION (buffer);
147     offset_end = GST_BUFFER_OFFSET_END (buffer);
148
149     timestamp += duration;
150     offset = offset_end;
151     gst_buffer_unref (buffer);
152   }
153   g_list_free (buffers);
154   buffers = NULL;
155 }
156
157 /* this tests that the output is a perfect stream if the input is */
158 static void
159 test_perfect_stream_instance (int inrate, int outrate, int samples,
160     int numbuffers)
161 {
162   GstElement *audioresample;
163   GstBuffer *inbuffer, *outbuffer;
164   GstCaps *caps;
165   guint64 offset = 0;
166   int i, j;
167   GstMapInfo map;
168   gint16 *p;
169
170   audioresample =
171       setup_audioresample (2, 0x3, inrate, outrate, GST_AUDIO_NE (S16));
172   caps = gst_pad_get_current_caps (mysrcpad);
173   fail_unless (gst_caps_is_fixed (caps));
174
175   fail_unless (gst_element_set_state (audioresample,
176           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
177       "could not set to playing");
178
179   for (j = 1; j <= numbuffers; ++j) {
180
181     inbuffer = gst_buffer_new_and_alloc (samples * 4);
182     GST_BUFFER_DURATION (inbuffer) = GST_FRAMES_TO_CLOCK_TIME (samples, inrate);
183     GST_BUFFER_TIMESTAMP (inbuffer) = GST_BUFFER_DURATION (inbuffer) * (j - 1);
184     GST_BUFFER_OFFSET (inbuffer) = offset;
185     offset += samples;
186     GST_BUFFER_OFFSET_END (inbuffer) = offset;
187
188     gst_buffer_map (inbuffer, &map, GST_MAP_WRITE);
189     p = (gint16 *) map.data;
190
191     /* create a 16 bit signed ramp */
192     for (i = 0; i < samples; ++i) {
193       *p = -32767 + i * (65535 / samples);
194       ++p;
195       *p = -32767 + i * (65535 / samples);
196       ++p;
197     }
198     gst_buffer_unmap (inbuffer, &map);
199
200     /* pushing gives away my reference ... */
201     fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
202     /* ... but it ends up being collected on the global buffer list */
203     fail_unless_equals_int (g_list_length (buffers), j);
204   }
205
206   /* FIXME: we should make audioresample handle eos by flushing out the last
207    * samples, which will give us one more, small, buffer */
208   fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
209   ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
210
211   fail_unless_perfect_stream ();
212
213   /* cleanup */
214   gst_caps_unref (caps);
215   cleanup_audioresample (audioresample);
216 }
217
218
219 /* make sure that outgoing buffers are contiguous in timestamp/duration and
220  * offset/offsetend
221  */
222 GST_START_TEST (test_perfect_stream)
223 {
224   /* integral scalings */
225   test_perfect_stream_instance (48000, 24000, 500, 20);
226   test_perfect_stream_instance (48000, 12000, 500, 20);
227   test_perfect_stream_instance (12000, 24000, 500, 20);
228   test_perfect_stream_instance (12000, 48000, 500, 20);
229
230   /* non-integral scalings */
231   test_perfect_stream_instance (44100, 8000, 500, 20);
232   test_perfect_stream_instance (8000, 44100, 500, 20);
233
234   /* wacky scalings */
235   test_perfect_stream_instance (12345, 54321, 500, 20);
236   test_perfect_stream_instance (101, 99, 500, 20);
237 }
238
239 GST_END_TEST;
240
241 /* this tests that the output is a correct discontinuous stream
242  * if the input is; ie input drops in time come out the same way */
243 static void
244 test_discont_stream_instance (int inrate, int outrate, int samples,
245     int numbuffers)
246 {
247   GstElement *audioresample;
248   GstBuffer *inbuffer, *outbuffer;
249   GstCaps *caps;
250   GstClockTime ints;
251
252   int i, j;
253   GstMapInfo map;
254   gint16 *p;
255
256   GST_DEBUG ("inrate:%d outrate:%d samples:%d numbuffers:%d",
257       inrate, outrate, samples, numbuffers);
258
259   audioresample =
260       setup_audioresample (2, 3, inrate, outrate, GST_AUDIO_NE (S16));
261   caps = gst_pad_get_current_caps (mysrcpad);
262   fail_unless (gst_caps_is_fixed (caps));
263
264   fail_unless (gst_element_set_state (audioresample,
265           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
266       "could not set to playing");
267
268   for (j = 1; j <= numbuffers; ++j) {
269
270     inbuffer = gst_buffer_new_and_alloc (samples * 4);
271     GST_BUFFER_DURATION (inbuffer) = samples * GST_SECOND / inrate;
272     /* "drop" half the buffers */
273     ints = GST_BUFFER_DURATION (inbuffer) * 2 * (j - 1);
274     GST_BUFFER_TIMESTAMP (inbuffer) = ints;
275     GST_BUFFER_OFFSET (inbuffer) = (j - 1) * 2 * samples;
276     GST_BUFFER_OFFSET_END (inbuffer) = j * 2 * samples + samples;
277
278     gst_buffer_map (inbuffer, &map, GST_MAP_WRITE);
279     p = (gint16 *) map.data;
280     /* create a 16 bit signed ramp */
281     for (i = 0; i < samples; ++i) {
282       *p = -32767 + i * (65535 / samples);
283       ++p;
284       *p = -32767 + i * (65535 / samples);
285       ++p;
286     }
287     gst_buffer_unmap (inbuffer, &map);
288
289     GST_DEBUG ("Sending Buffer time:%" G_GUINT64_FORMAT " duration:%"
290         G_GINT64_FORMAT " discont:%d offset:%" G_GUINT64_FORMAT " offset_end:%"
291         G_GUINT64_FORMAT, GST_BUFFER_TIMESTAMP (inbuffer),
292         GST_BUFFER_DURATION (inbuffer), GST_BUFFER_IS_DISCONT (inbuffer),
293         GST_BUFFER_OFFSET (inbuffer), GST_BUFFER_OFFSET_END (inbuffer));
294     /* pushing gives away my reference ... */
295     fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
296
297     /* check if the timestamp of the pushed buffer matches the incoming one */
298     outbuffer = g_list_nth_data (buffers, g_list_length (buffers) - 1);
299     fail_if (outbuffer == NULL);
300     fail_unless_equals_uint64 (ints, GST_BUFFER_TIMESTAMP (outbuffer));
301     GST_DEBUG ("Got Buffer time:%" G_GUINT64_FORMAT " duration:%"
302         G_GINT64_FORMAT " discont:%d offset:%" G_GUINT64_FORMAT " offset_end:%"
303         G_GUINT64_FORMAT, GST_BUFFER_TIMESTAMP (outbuffer),
304         GST_BUFFER_DURATION (outbuffer), GST_BUFFER_IS_DISCONT (outbuffer),
305         GST_BUFFER_OFFSET (outbuffer), GST_BUFFER_OFFSET_END (outbuffer));
306     if (j > 1) {
307       fail_unless (GST_BUFFER_IS_DISCONT (outbuffer),
308           "expected discont for buffer #%d", j);
309     }
310   }
311
312   /* cleanup */
313   gst_caps_unref (caps);
314   cleanup_audioresample (audioresample);
315 }
316
317 GST_START_TEST (test_discont_stream)
318 {
319   /* integral scalings */
320   test_discont_stream_instance (48000, 24000, 5000, 20);
321   test_discont_stream_instance (48000, 12000, 5000, 20);
322   test_discont_stream_instance (12000, 24000, 5000, 20);
323   test_discont_stream_instance (12000, 48000, 5000, 20);
324
325   /* non-integral scalings */
326   test_discont_stream_instance (44100, 8000, 5000, 20);
327   test_discont_stream_instance (8000, 44100, 5000, 20);
328
329   /* wacky scalings */
330   test_discont_stream_instance (12345, 54321, 5000, 20);
331   test_discont_stream_instance (101, 99, 5000, 20);
332 }
333
334 GST_END_TEST;
335
336
337
338 GST_START_TEST (test_reuse)
339 {
340   GstElement *audioresample;
341   GstEvent *newseg;
342   GstBuffer *inbuffer;
343   GstCaps *caps;
344   GstSegment segment;
345
346   audioresample = setup_audioresample (1, 0, 9343, 48000, GST_AUDIO_NE (S16));
347   caps = gst_pad_get_current_caps (mysrcpad);
348   fail_unless (gst_caps_is_fixed (caps));
349
350   fail_unless (gst_element_set_state (audioresample,
351           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
352       "could not set to playing");
353
354   gst_segment_init (&segment, GST_FORMAT_TIME);
355   newseg = gst_event_new_segment (&segment);
356   fail_unless (gst_pad_push_event (mysrcpad, newseg) != FALSE);
357
358   inbuffer = gst_buffer_new_and_alloc (9343 * 4);
359   gst_buffer_memset (inbuffer, 0, 0, 9343 * 4);
360   GST_BUFFER_DURATION (inbuffer) = GST_SECOND;
361   GST_BUFFER_TIMESTAMP (inbuffer) = 0;
362   GST_BUFFER_OFFSET (inbuffer) = 0;
363
364   /* pushing gives away my reference ... */
365   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
366
367   /* ... but it ends up being collected on the global buffer list */
368   fail_unless_equals_int (g_list_length (buffers), 1);
369
370   /* now reset and try again ... */
371   fail_unless (gst_element_set_state (audioresample,
372           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to NULL");
373
374   fail_unless (gst_element_set_state (audioresample,
375           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
376       "could not set to playing");
377
378   newseg = gst_event_new_segment (&segment);
379   fail_unless (gst_pad_push_event (mysrcpad, newseg) != FALSE);
380
381   inbuffer = gst_buffer_new_and_alloc (9343 * 4);
382   gst_buffer_memset (inbuffer, 0, 0, 9343 * 4);
383   GST_BUFFER_DURATION (inbuffer) = GST_SECOND;
384   GST_BUFFER_TIMESTAMP (inbuffer) = 0;
385   GST_BUFFER_OFFSET (inbuffer) = 0;
386
387   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
388
389   /* ... it also ends up being collected on the global buffer list. If we
390    * now have more than 2 buffers, then audioresample probably didn't clean
391    * up its internal buffer properly and tried to push the remaining samples
392    * when it got the second NEWSEGMENT event */
393   fail_unless_equals_int (g_list_length (buffers), 2);
394
395   cleanup_audioresample (audioresample);
396   gst_caps_unref (caps);
397 }
398
399 GST_END_TEST;
400
401 GST_START_TEST (test_shutdown)
402 {
403   GstElement *pipeline, *src, *cf1, *ar, *cf2, *sink;
404   GstCaps *caps;
405   guint i;
406
407   /* create pipeline, force audioresample to actually resample */
408   pipeline = gst_pipeline_new (NULL);
409
410   src = gst_check_setup_element ("audiotestsrc");
411   cf1 = gst_check_setup_element ("capsfilter");
412   ar = gst_check_setup_element ("audioresample");
413   cf2 = gst_check_setup_element ("capsfilter");
414   g_object_set (cf2, "name", "capsfilter2", NULL);
415   sink = gst_check_setup_element ("fakesink");
416
417   caps = gst_caps_new_simple ("audio/x-raw", "rate", G_TYPE_INT, 11025, NULL);
418   g_object_set (cf1, "caps", caps, NULL);
419   gst_caps_unref (caps);
420
421   caps = gst_caps_new_simple ("audio/x-raw", "rate", G_TYPE_INT, 48000, NULL);
422   g_object_set (cf2, "caps", caps, NULL);
423   gst_caps_unref (caps);
424
425   /* don't want to sync against the clock, the more throughput the better */
426   g_object_set (src, "is-live", FALSE, NULL);
427   g_object_set (sink, "sync", FALSE, NULL);
428
429   gst_bin_add_many (GST_BIN (pipeline), src, cf1, ar, cf2, sink, NULL);
430   fail_if (!gst_element_link_many (src, cf1, ar, cf2, sink, NULL));
431
432   /* now, wait until pipeline is running and then shut it down again; repeat */
433   for (i = 0; i < 20; ++i) {
434     gst_element_set_state (pipeline, GST_STATE_PAUSED);
435     gst_element_get_state (pipeline, NULL, NULL, -1);
436     gst_element_set_state (pipeline, GST_STATE_PLAYING);
437     g_usleep (100);
438     gst_element_set_state (pipeline, GST_STATE_NULL);
439   }
440
441   gst_object_unref (pipeline);
442 }
443
444 GST_END_TEST;
445
446 static void
447 live_switch_push (gint pts, gint rate, GstCaps * caps)
448 {
449   GstBuffer *inbuffer;
450   GstCaps *desired;
451
452   desired = gst_caps_copy (caps);
453   gst_caps_set_simple (desired, "rate", G_TYPE_INT, rate, NULL);
454   gst_pad_set_caps (mysrcpad, desired);
455
456   inbuffer = gst_buffer_new_and_alloc (rate * 4 * 2);
457   gst_buffer_memset (inbuffer, 0, 0, rate * 4 * 2);
458
459   GST_BUFFER_DURATION (inbuffer) = GST_SECOND;
460   GST_BUFFER_TIMESTAMP (inbuffer) = pts * GST_SECOND;
461   GST_BUFFER_OFFSET (inbuffer) = 0;
462   GST_BUFFER_OFFSET_END (inbuffer) = rate - 1;
463
464   /* pushing gives away my reference ... */
465   fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
466
467   /* ... but it ends up being collected on the global buffer list */
468
469   gst_caps_unref (desired);
470 }
471
472 GST_START_TEST (test_live_switch)
473 {
474   GstElement *audioresample;
475   GstEvent *newseg;
476   GstCaps *caps;
477   GstSegment segment;
478   GList *l;
479   guint i;
480
481   audioresample =
482       setup_audioresample (4, 0xf, 48000, 48000, GST_AUDIO_NE (S16));
483
484   caps = gst_pad_get_current_caps (mysrcpad);
485   fail_unless (gst_caps_is_fixed (caps));
486
487   fail_unless (gst_element_set_state (audioresample,
488           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
489       "could not set to playing");
490
491   gst_segment_init (&segment, GST_FORMAT_TIME);
492   newseg = gst_event_new_segment (&segment);
493   fail_unless (gst_pad_push_event (mysrcpad, newseg) != FALSE);
494
495   /* downstream can accept the requested rate */
496   live_switch_push (0, 48000, caps);
497
498   /* buffer is directly passed through */
499   fail_unless_equals_int (g_list_length (buffers), 1);
500
501   /* Downstream can never accept this rate */
502   live_switch_push (1, 40000, caps);
503
504   /* one additional buffer is provided with the new sample rate */
505   fail_unless_equals_int (g_list_length (buffers), 2);
506
507   /* Downstream can never accept this rate */
508   live_switch_push (2, 50000, caps);
509
510   /* two additional buffers are provided. One is the drained remainder of
511    * the previous sample rate, the second is the buffer with the new sample
512    * rate */
513   fail_unless_equals_int (g_list_length (buffers), 4);
514
515   /* Send EOS to drain the remaining samples */
516   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()));
517   fail_unless_equals_int (g_list_length (buffers), 5);
518
519   /* Now test that each buffer has the expected samples. We simply check this
520    * by checking whether the timestamps, durations and sizes are matching */
521   for (l = buffers, i = 0; l; l = l->next, i++) {
522     GstBuffer *buffer = GST_BUFFER (l->data);
523
524     switch (i) {
525       case 0:
526         fail_unless_equals_uint64 (GST_BUFFER_PTS (buffer), 0 * GST_SECOND);
527         fail_unless_equals_uint64 (GST_BUFFER_DURATION (buffer),
528             1 * GST_SECOND);
529         fail_unless_equals_int (gst_buffer_get_size (buffer), 48000 * 4 * 2);
530         break;
531       case 1:
532         fail_unless_equals_uint64 (GST_BUFFER_PTS (buffer), 1 * GST_SECOND);
533         fail_unless_equals_int (gst_buffer_get_size (buffer), 47961 * 4 * 2);
534         break;
535       case 2:
536         fail_unless (G_APPROX_VALUE (GST_BUFFER_PTS (buffer) +
537                 GST_BUFFER_DURATION (buffer), 2 * GST_SECOND,
538                 GST_SECOND / 48000 + 1));
539         fail_unless_equals_int (gst_buffer_get_size (buffer), 38 * 4 * 2);
540         break;
541       case 3:
542         fail_unless_equals_uint64 (GST_BUFFER_PTS (buffer), 2 * GST_SECOND);
543         fail_unless_equals_int (gst_buffer_get_size (buffer), 47969 * 4 * 2);
544         break;
545       case 4:
546         fail_unless (G_APPROX_VALUE (GST_BUFFER_PTS (buffer) +
547                 GST_BUFFER_DURATION (buffer), 3 * GST_SECOND,
548                 GST_SECOND / 48000 + 1));
549         fail_unless_equals_int (gst_buffer_get_size (buffer), 30 * 4 * 2);
550         break;
551       default:
552         g_assert_not_reached ();
553         break;
554     }
555
556     gst_buffer_unref (buffer);
557   }
558
559   g_list_free (buffers);
560   buffers = NULL;
561
562   cleanup_audioresample (audioresample);
563   gst_caps_unref (caps);
564 }
565
566 GST_END_TEST;
567
568 static gint current_rate = 0;
569
570 static gboolean
571 live_switch_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
572 {
573   switch (GST_QUERY_TYPE (query)) {
574     case GST_QUERY_ACCEPT_CAPS:{
575       GstCaps *acceptable_caps;
576       GstCaps *caps;
577
578       acceptable_caps = gst_pad_get_current_caps (mysrcpad);
579       acceptable_caps = gst_caps_make_writable (acceptable_caps);
580       gst_caps_set_simple (acceptable_caps, "rate", G_TYPE_INT, current_rate,
581           NULL);
582
583       gst_query_parse_accept_caps (query, &caps);
584
585       gst_query_set_accept_caps_result (query, gst_caps_can_intersect (caps,
586               acceptable_caps));
587
588       gst_caps_unref (acceptable_caps);
589
590       return TRUE;
591     }
592     case GST_QUERY_CAPS:{
593       GstCaps *acceptable_caps;
594       GstCaps *filter;
595       GstCaps *caps;
596
597       acceptable_caps = gst_pad_get_current_caps (mysrcpad);
598       acceptable_caps = gst_caps_make_writable (acceptable_caps);
599       gst_caps_set_simple (acceptable_caps, "rate", G_TYPE_INT, current_rate,
600           NULL);
601
602       gst_query_parse_caps (query, &filter);
603
604       if (filter)
605         caps =
606             gst_caps_intersect_full (filter, acceptable_caps,
607             GST_CAPS_INTERSECT_FIRST);
608       else
609         caps = gst_caps_ref (acceptable_caps);
610
611       gst_query_set_caps_result (query, caps);
612
613       gst_caps_unref (caps);
614       gst_caps_unref (acceptable_caps);
615
616       return TRUE;
617     }
618     default:
619       return gst_pad_query_default (pad, parent, query);
620   }
621 }
622
623 static void
624 live_switch_push_downstream (gint pts, gint rate)
625 {
626   GstBuffer *inbuffer;
627
628   current_rate = rate;
629   gst_pad_push_event (mysinkpad, gst_event_new_reconfigure ());
630
631   inbuffer = gst_buffer_new_and_alloc (48000 * 4 * 2);
632   gst_buffer_memset (inbuffer, 0, 0, 48000 * 4 * 2);
633
634   GST_BUFFER_DURATION (inbuffer) = GST_SECOND;
635   GST_BUFFER_TIMESTAMP (inbuffer) = pts * GST_SECOND;
636   GST_BUFFER_OFFSET (inbuffer) = 0;
637   GST_BUFFER_OFFSET_END (inbuffer) = 47999;
638
639   /* pushing gives away my reference ... */
640   fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
641
642   /* ... but it ends up being collected on the global buffer list */
643 }
644
645 GST_START_TEST (test_live_switch_downstream)
646 {
647   GstElement *audioresample;
648   GstEvent *newseg;
649   GstCaps *caps;
650   GstSegment segment;
651   GList *l;
652   guint i;
653
654   audioresample =
655       setup_audioresample (4, 0xf, 48000, 48000, GST_AUDIO_NE (S16));
656
657   gst_pad_set_query_function (mysinkpad, live_switch_sink_query);
658
659   caps = gst_pad_get_current_caps (mysrcpad);
660   fail_unless (gst_caps_is_fixed (caps));
661
662   fail_unless (gst_element_set_state (audioresample,
663           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
664       "could not set to playing");
665
666   gst_segment_init (&segment, GST_FORMAT_TIME);
667   newseg = gst_event_new_segment (&segment);
668   fail_unless (gst_pad_push_event (mysrcpad, newseg) != FALSE);
669
670   /* buffer is directly passed through */
671   live_switch_push_downstream (0, 48000);
672   fail_unless_equals_int (g_list_length (buffers), 1);
673
674   /* Reconfigure downstream to 40000 Hz */
675   live_switch_push_downstream (1, 40000);
676
677   /* one additional buffer is provided with the new sample rate */
678   fail_unless_equals_int (g_list_length (buffers), 2);
679
680   /* Reconfigure downstream to 50000 Hz */
681   live_switch_push_downstream (2, 50000);
682
683   /* two additional buffers are provided. One is the drained remainder of
684    * the previous sample rate, the second is the buffer with the new sample
685    * rate */
686   fail_unless_equals_int (g_list_length (buffers), 4);
687
688   /* Send EOS to drain the remaining samples */
689   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()));
690   fail_unless_equals_int (g_list_length (buffers), 5);
691
692   /* Now test that each buffer has the expected samples. We simply check this
693    * by checking whether the timestamps, durations and sizes are matching */
694   for (l = buffers, i = 0; l; l = l->next, i++) {
695     GstBuffer *buffer = GST_BUFFER (l->data);
696
697     switch (i) {
698       case 0:
699         fail_unless_equals_uint64 (GST_BUFFER_PTS (buffer), 0 * GST_SECOND);
700         fail_unless_equals_uint64 (GST_BUFFER_DURATION (buffer),
701             1 * GST_SECOND);
702         fail_unless_equals_int (gst_buffer_get_size (buffer), 48000 * 4 * 2);
703         break;
704       case 1:
705         fail_unless_equals_uint64 (GST_BUFFER_PTS (buffer), 1 * GST_SECOND);
706         fail_unless_equals_int (gst_buffer_get_size (buffer), 39966 * 4 * 2);
707         break;
708       case 2:
709         fail_unless (G_APPROX_VALUE (GST_BUFFER_PTS (buffer) +
710                 GST_BUFFER_DURATION (buffer), 2 * GST_SECOND,
711                 GST_SECOND / 40000 + 1));
712         fail_unless_equals_int (gst_buffer_get_size (buffer), 34 * 4 * 2);
713         break;
714       case 3:
715         fail_unless_equals_uint64 (GST_BUFFER_PTS (buffer), 2 * GST_SECOND);
716         fail_unless_equals_int (gst_buffer_get_size (buffer), 49966 * 4 * 2);
717         break;
718       case 4:
719         fail_unless (G_APPROX_VALUE (GST_BUFFER_PTS (buffer) +
720                 GST_BUFFER_DURATION (buffer), 3 * GST_SECOND,
721                 GST_SECOND / 50000 + 1));
722         fail_unless_equals_int (gst_buffer_get_size (buffer), 33 * 4 * 2);
723         break;
724       default:
725         g_assert_not_reached ();
726         break;
727     }
728
729     gst_buffer_unref (buffer);
730   }
731
732   g_list_free (buffers);
733   buffers = NULL;
734
735   cleanup_audioresample (audioresample);
736   gst_caps_unref (caps);
737 }
738
739 GST_END_TEST;
740
741 #ifndef GST_DISABLE_PARSE
742
743 static GMainLoop *loop;
744 static gint messages = 0;
745
746 static void
747 element_message_cb (GstBus * bus, GstMessage * message, gpointer user_data)
748 {
749   gchar *s;
750
751   s = gst_structure_to_string (gst_message_get_structure (message));
752   GST_DEBUG ("Received message: %s", s);
753   g_free (s);
754
755   messages++;
756 }
757
758 static void
759 eos_message_cb (GstBus * bus, GstMessage * message, gpointer user_data)
760 {
761   GST_DEBUG ("Received eos");
762   g_main_loop_quit (loop);
763 }
764
765 static void
766 test_pipeline (const gchar * format, gint inrate, gint outrate, gint quality)
767 {
768   GstElement *pipeline;
769   GstBus *bus;
770   GError *error = NULL;
771   gchar *pipe_str;
772
773   pipe_str =
774       g_strdup_printf
775       ("audiotestsrc num-buffers=10 ! audioconvert ! audio/x-raw,format=%s,rate=%d,channels=2 ! audioresample quality=%d ! audio/x-raw,format=%s,rate=%d ! identity check-imperfect-timestamp=TRUE ! fakesink",
776       format, inrate, quality, format, outrate);
777
778   pipeline = gst_parse_launch (pipe_str, &error);
779   fail_unless (pipeline != NULL, "Error parsing pipeline: %s",
780       error ? error->message : "(invalid error)");
781   g_free (pipe_str);
782
783   bus = gst_element_get_bus (pipeline);
784   fail_if (bus == NULL);
785   gst_bus_add_signal_watch (bus);
786   g_signal_connect (bus, "message::element", (GCallback) element_message_cb,
787       NULL);
788   g_signal_connect (bus, "message::eos", (GCallback) eos_message_cb, NULL);
789
790   gst_element_set_state (pipeline, GST_STATE_PLAYING);
791
792   /* run until we receive EOS */
793   loop = g_main_loop_new (NULL, FALSE);
794
795   g_main_loop_run (loop);
796
797   g_main_loop_unref (loop);
798   loop = NULL;
799
800   gst_element_set_state (pipeline, GST_STATE_NULL);
801
802   gst_bus_remove_signal_watch (bus);
803   gst_object_unref (bus);
804
805   fail_if (messages > 0, "Received imperfect timestamp messages");
806   gst_object_unref (pipeline);
807 }
808
809 GST_START_TEST (test_pipelines)
810 {
811   gint quality;
812
813   /* Test qualities 0, 5 and 10 */
814   for (quality = 0; quality < 11; quality += 5) {
815     GST_DEBUG ("Checking with quality %d", quality);
816
817     test_pipeline ("S8", 44100, 48000, quality);
818     test_pipeline ("S8", 48000, 44100, quality);
819
820     test_pipeline (GST_AUDIO_NE (S16), 44100, 48000, quality);
821     test_pipeline (GST_AUDIO_NE (S16), 48000, 44100, quality);
822
823     test_pipeline (GST_AUDIO_NE (S24), 44100, 48000, quality);
824     test_pipeline (GST_AUDIO_NE (S24), 48000, 44100, quality);
825
826     test_pipeline (GST_AUDIO_NE (S32), 44100, 48000, quality);
827     test_pipeline (GST_AUDIO_NE (S32), 48000, 44100, quality);
828
829     test_pipeline (GST_AUDIO_NE (F32), 44100, 48000, quality);
830     test_pipeline (GST_AUDIO_NE (F32), 48000, 44100, quality);
831
832     test_pipeline (GST_AUDIO_NE (F64), 44100, 48000, quality);
833     test_pipeline (GST_AUDIO_NE (F64), 48000, 44100, quality);
834   }
835 }
836
837 GST_END_TEST;
838
839 GST_START_TEST (test_preference_passthrough)
840 {
841   GstStateChangeReturn ret;
842   GstElement *pipeline, *src;
843   GstStructure *s;
844   GstMessage *msg;
845   GstCaps *caps;
846   GstPad *pad;
847   GstBus *bus;
848   GError *error = NULL;
849   gint rate = 0;
850
851   pipeline = gst_parse_launch ("audiotestsrc num-buffers=1 name=src ! "
852       "audioresample ! audio/x-raw,format=" GST_AUDIO_NE (S16) ",channels=1,"
853       "rate=8000 ! fakesink can-activate-pull=false", &error);
854   fail_unless (pipeline != NULL, "Error parsing pipeline: %s",
855       error ? error->message : "(invalid error)");
856
857   ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
858   fail_unless_equals_int (ret, GST_STATE_CHANGE_ASYNC);
859
860   /* run until we receive EOS */
861   bus = gst_element_get_bus (pipeline);
862   fail_if (bus == NULL);
863   msg = gst_bus_timed_pop_filtered (bus, -1, GST_MESSAGE_EOS);
864   gst_message_unref (msg);
865   gst_object_unref (bus);
866
867   src = gst_bin_get_by_name (GST_BIN (pipeline), "src");
868   fail_unless (src != NULL);
869   pad = gst_element_get_static_pad (src, "src");
870   fail_unless (pad != NULL);
871   caps = gst_pad_get_current_caps (pad);
872   GST_LOG ("current audiotestsrc caps: %" GST_PTR_FORMAT, caps);
873   fail_unless (caps != NULL);
874   s = gst_caps_get_structure (caps, 0);
875   fail_unless (gst_structure_get_int (s, "rate", &rate));
876   /* there's no need to resample, audiotestsrc supports any rate, so make
877    * sure audioresample provided upstream with the right caps to negotiate
878    * this correctly */
879   fail_unless_equals_int (rate, 8000);
880   gst_caps_unref (caps);
881   gst_object_unref (pad);
882   gst_object_unref (src);
883
884   gst_element_set_state (pipeline, GST_STATE_NULL);
885   gst_object_unref (pipeline);
886 }
887
888 GST_END_TEST;
889
890 #endif
891
892 static void
893 _message_cb (GstBus * bus, GstMessage * message, gpointer user_data)
894 {
895   GMainLoop *loop = user_data;
896
897   switch (GST_MESSAGE_TYPE (message)) {
898     case GST_MESSAGE_ERROR:
899     case GST_MESSAGE_WARNING:
900       g_assert_not_reached ();
901       break;
902     case GST_MESSAGE_EOS:
903       g_main_loop_quit (loop);
904       break;
905     default:
906       break;
907   }
908 }
909
910 typedef struct
911 {
912   guint64 latency;
913   GstClockTime in_ts;
914
915   GstClockTime next_out_ts;
916   guint64 next_out_off;
917
918   guint64 in_buffer_count, out_buffer_count;
919 } TimestampDriftCtx;
920
921 static void
922 fakesink_handoff_cb (GstElement * object, GstBuffer * buffer, GstPad * pad,
923     gpointer user_data)
924 {
925   TimestampDriftCtx *ctx = user_data;
926
927   ctx->out_buffer_count++;
928   if (ctx->latency == GST_CLOCK_TIME_NONE) {
929     ctx->latency = 1000 - gst_buffer_get_size (buffer) / 8;
930   }
931
932   /* Check if we have a perfectly timestamped stream */
933   if (ctx->next_out_ts != GST_CLOCK_TIME_NONE)
934     fail_unless (ctx->next_out_ts == GST_BUFFER_TIMESTAMP (buffer),
935         "expected timestamp %" GST_TIME_FORMAT " got timestamp %"
936         GST_TIME_FORMAT, GST_TIME_ARGS (ctx->next_out_ts),
937         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));
938
939   /* Check if we have a perfectly offsetted stream */
940   fail_unless (GST_BUFFER_OFFSET_END (buffer) ==
941       GST_BUFFER_OFFSET (buffer) + gst_buffer_get_size (buffer) / 8,
942       "expected offset end %" G_GUINT64_FORMAT " got offset end %"
943       G_GUINT64_FORMAT,
944       GST_BUFFER_OFFSET (buffer) + gst_buffer_get_size (buffer) / 8,
945       GST_BUFFER_OFFSET_END (buffer));
946   if (ctx->next_out_off != GST_BUFFER_OFFSET_NONE) {
947     fail_unless (GST_BUFFER_OFFSET (buffer) == ctx->next_out_off,
948         "expected offset %" G_GUINT64_FORMAT " got offset %" G_GUINT64_FORMAT,
949         ctx->next_out_off, GST_BUFFER_OFFSET (buffer));
950   }
951
952   if (ctx->in_buffer_count != ctx->out_buffer_count) {
953     GST_INFO ("timestamp %" GST_TIME_FORMAT,
954         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));
955   }
956
957   if (ctx->in_ts != GST_CLOCK_TIME_NONE && ctx->in_buffer_count > 1
958       && ctx->in_buffer_count == ctx->out_buffer_count) {
959     fail_unless (GST_BUFFER_TIMESTAMP (buffer) ==
960         ctx->in_ts - gst_util_uint64_scale_round (ctx->latency, GST_SECOND,
961             4096),
962         "expected output timestamp %" GST_TIME_FORMAT " (%" G_GUINT64_FORMAT
963         ") got output timestamp %" GST_TIME_FORMAT " (%" G_GUINT64_FORMAT ")",
964         GST_TIME_ARGS (ctx->in_ts - gst_util_uint64_scale_round (ctx->latency,
965                 GST_SECOND, 4096)),
966         ctx->in_ts - gst_util_uint64_scale_round (ctx->latency, GST_SECOND,
967             4096), GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
968         GST_BUFFER_TIMESTAMP (buffer));
969   }
970
971   ctx->next_out_ts =
972       GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer);
973   ctx->next_out_off = GST_BUFFER_OFFSET_END (buffer);
974 }
975
976 static void
977 identity_handoff_cb (GstElement * object, GstBuffer * buffer,
978     gpointer user_data)
979 {
980   TimestampDriftCtx *ctx = user_data;
981
982   ctx->in_ts = GST_BUFFER_TIMESTAMP (buffer);
983   ctx->in_buffer_count++;
984 }
985
986 GST_START_TEST (test_timestamp_drift)
987 {
988   TimestampDriftCtx ctx =
989       { GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE,
990     GST_BUFFER_OFFSET_NONE, 0, 0
991   };
992   GstElement *pipeline;
993   GstElement *audiotestsrc, *capsfilter1, *identity, *audioresample,
994       *capsfilter2, *fakesink;
995   GstBus *bus;
996   GMainLoop *loop;
997   GstCaps *caps;
998
999   pipeline = gst_pipeline_new ("pipeline");
1000   fail_unless (pipeline != NULL);
1001
1002   audiotestsrc = gst_element_factory_make ("audiotestsrc", "src");
1003   fail_unless (audiotestsrc != NULL);
1004   g_object_set (G_OBJECT (audiotestsrc), "num-buffers", 10000,
1005       "samplesperbuffer", 4000, NULL);
1006
1007   capsfilter1 = gst_element_factory_make ("capsfilter", "capsfilter1");
1008   fail_unless (capsfilter1 != NULL);
1009   caps = gst_caps_from_string ("audio/x-raw, format=" GST_AUDIO_NE (F64)
1010       ", channels=1, rate=16384");
1011   g_object_set (G_OBJECT (capsfilter1), "caps", caps, NULL);
1012   gst_caps_unref (caps);
1013
1014   identity = gst_element_factory_make ("identity", "identity");
1015   fail_unless (identity != NULL);
1016   g_object_set (G_OBJECT (identity), "sync", FALSE, "signal-handoffs", TRUE,
1017       NULL);
1018   g_signal_connect (identity, "handoff", (GCallback) identity_handoff_cb, &ctx);
1019
1020   audioresample = gst_element_factory_make ("audioresample", "resample");
1021   fail_unless (audioresample != NULL);
1022   capsfilter2 = gst_element_factory_make ("capsfilter", "capsfilter2");
1023   fail_unless (capsfilter2 != NULL);
1024   caps = gst_caps_from_string ("audio/x-raw, format=" GST_AUDIO_NE (F64)
1025       ", channels=1, rate=4096");
1026   g_object_set (G_OBJECT (capsfilter2), "caps", caps, NULL);
1027   gst_caps_unref (caps);
1028
1029   fakesink = gst_element_factory_make ("fakesink", "sink");
1030   fail_unless (fakesink != NULL);
1031   g_object_set (G_OBJECT (fakesink), "sync", FALSE, "async", FALSE,
1032       "signal-handoffs", TRUE, NULL);
1033   g_signal_connect (fakesink, "handoff", (GCallback) fakesink_handoff_cb, &ctx);
1034
1035
1036   gst_bin_add_many (GST_BIN (pipeline), audiotestsrc, capsfilter1, identity,
1037       audioresample, capsfilter2, fakesink, NULL);
1038   fail_unless (gst_element_link_many (audiotestsrc, capsfilter1, identity,
1039           audioresample, capsfilter2, fakesink, NULL));
1040
1041   loop = g_main_loop_new (NULL, FALSE);
1042
1043   bus = gst_element_get_bus (pipeline);
1044   gst_bus_add_signal_watch (bus);
1045   g_signal_connect (bus, "message", (GCallback) _message_cb, loop);
1046
1047   fail_unless (gst_element_set_state (pipeline,
1048           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS);
1049   g_main_loop_run (loop);
1050
1051   fail_unless (gst_element_set_state (pipeline,
1052           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS);
1053   g_main_loop_unref (loop);
1054   gst_bus_remove_signal_watch (bus);
1055   gst_object_unref (bus);
1056
1057   gst_object_unref (pipeline);
1058
1059 } GST_END_TEST;
1060
1061 #define FFT_HELPERS(type,ffttag,ffttag2,scale);                                                 \
1062 static gdouble magnitude##ffttag (const GstFFT##ffttag##Complex *c)                             \
1063 {                                                                                               \
1064   gdouble mag = (gdouble) c->r * (gdouble) c->r;                                                \
1065   mag += (gdouble) c->i * (gdouble) c->i;                                                       \
1066   mag /= scale * scale;                                                                         \
1067   mag = 10.0 * log10 (mag);                                                                     \
1068   return mag;                                                                                   \
1069 }                                                                                               \
1070 static gdouble find_main_frequency_spot_##ffttag (const GstFFT##ffttag##Complex *v,             \
1071                                                   int elements)                                 \
1072 {                                                                                               \
1073   int i;                                                                                        \
1074   gdouble maxmag = -9999;                                                                       \
1075   int maxidx = 0;                                                                               \
1076   for (i=0; i<elements; ++i) {                                                                  \
1077     gdouble mag = magnitude##ffttag (v+i);                                                      \
1078     if (mag > maxmag) {                                                                         \
1079       maxmag = mag;                                                                             \
1080       maxidx = i;                                                                               \
1081     }                                                                                           \
1082   }                                                                                             \
1083   return maxidx / (gdouble) elements;                                                           \
1084 }                                                                                               \
1085 static gboolean is_zero_except_##ffttag (const GstFFT##ffttag##Complex *v, int elements,        \
1086                                 gdouble spot)                                                   \
1087 {                                                                                               \
1088   int i;                                                                                        \
1089   for (i=0; i<elements; ++i) {                                                                  \
1090     gdouble pos = i / (gdouble) elements;                                                       \
1091     gdouble mag = magnitude##ffttag (v+i);                                                      \
1092     if (fabs (pos - spot) > 0.01) {                                                             \
1093       if (mag > -55.0) {                                                                        \
1094         return FALSE;                                                                           \
1095       }                                                                                         \
1096     }                                                                                           \
1097   }                                                                                             \
1098   return TRUE;                                                                                  \
1099 }                                                                                               \
1100 static void compare_ffts_##ffttag (GstBuffer *inbuffer, GstBuffer *outbuffer)                   \
1101 {                                                                                               \
1102   GstMapInfo inmap, outmap;                                                                     \
1103   int insamples, outsamples;                                                                    \
1104   gdouble inspot, outspot;                                                                      \
1105   GstFFT##ffttag *inctx, *outctx;                                                               \
1106   GstFFT##ffttag##Complex *in, *out;                                                            \
1107                                                                                                 \
1108   gst_buffer_map (inbuffer, &inmap, GST_MAP_READ);                                              \
1109   gst_buffer_map (outbuffer, &outmap, GST_MAP_READWRITE);                                       \
1110                                                                                                 \
1111   insamples = inmap.size / sizeof(type) & ~1;                                                   \
1112   outsamples = outmap.size / sizeof(type) & ~1;                                                 \
1113   inctx = gst_fft_##ffttag2##_new (insamples, FALSE);                                           \
1114   outctx = gst_fft_##ffttag2##_new (outsamples, FALSE);                                         \
1115   in = g_new (GstFFT##ffttag##Complex, insamples / 2 + 1);                                      \
1116   out = g_new (GstFFT##ffttag##Complex, outsamples / 2 + 1);                                    \
1117                                                                                                 \
1118   gst_fft_##ffttag2##_window (inctx, (type*)inmap.data,                                         \
1119       GST_FFT_WINDOW_HAMMING);                                                                  \
1120   gst_fft_##ffttag2##_fft (inctx, (type*)inmap.data, in);                                       \
1121   gst_fft_##ffttag2##_window (outctx, (type*)outmap.data,                                       \
1122       GST_FFT_WINDOW_HAMMING);                                                                  \
1123   gst_fft_##ffttag2##_fft (outctx, (type*)outmap.data, out);                                    \
1124                                                                                                 \
1125   inspot = find_main_frequency_spot_##ffttag (in, insamples / 2 + 1);                           \
1126   outspot = find_main_frequency_spot_##ffttag (out, outsamples / 2 + 1);                        \
1127   GST_LOG ("Spots are %.3f and %.3f", inspot, outspot);                                         \
1128   fail_unless (fabs (outspot - inspot) < 0.05);                                                 \
1129   fail_unless (is_zero_except_##ffttag (in, insamples / 2 + 1, inspot));                        \
1130   fail_unless (is_zero_except_##ffttag (out, outsamples / 2 + 1, outspot));                     \
1131                                                                                                 \
1132   gst_buffer_unmap (inbuffer, &inmap);                                                          \
1133   gst_buffer_unmap (outbuffer, &outmap);                                                        \
1134                                                                                                 \
1135   gst_fft_##ffttag2##_free (inctx);                                                             \
1136   gst_fft_##ffttag2##_free (outctx);                                                            \
1137   g_free (in);                                                                                  \
1138   g_free (out);                                                                                 \
1139 }
1140 FFT_HELPERS (float, F32, f32, 2048.0f);
1141 FFT_HELPERS (double, F64, f64, 2048.0);
1142 FFT_HELPERS (gint16, S16, s16, 32767.0);
1143 FFT_HELPERS (gint32, S32, s32, 2147483647.0);
1144
1145 #define FILL_BUFFER(type, desc, value);                         \
1146   static void init_##type##_##desc (GstBuffer *buffer)          \
1147   {                                                             \
1148     GstMapInfo map;                                             \
1149     type *ptr;                                                  \
1150     int i, nsamples;                                            \
1151     gst_buffer_map (buffer, &map, GST_MAP_WRITE);               \
1152     ptr = (type *)map.data;                                     \
1153     nsamples = map.size / sizeof (type);                        \
1154     for (i = 0; i < nsamples; ++i) {                            \
1155       *ptr++ = value;                                           \
1156     }                                                           \
1157     gst_buffer_unmap (buffer, &map);                            \
1158   }
1159
1160 FILL_BUFFER (float, silence, 0.0f);
1161 FILL_BUFFER (double, silence, 0.0);
1162 FILL_BUFFER (gint16, silence, 0);
1163 FILL_BUFFER (gint32, silence, 0);
1164 FILL_BUFFER (float, sine, sinf (i * 0.01f));
1165 FILL_BUFFER (float, sine2, sinf (i * 1.8f));
1166 FILL_BUFFER (double, sine, sin (i * 0.01));
1167 FILL_BUFFER (double, sine2, sin (i * 1.8));
1168 FILL_BUFFER (gint16, sine, (gint16) (32767 * sinf (i * 0.01f)));
1169 FILL_BUFFER (gint16, sine2, (gint16) (32767 * sinf (i * 1.8f)));
1170 FILL_BUFFER (gint32, sine, (gint32) (2147483647.0 * sin (i * 0.01)));
1171 FILL_BUFFER (gint32, sine2, (gint32) (2147483647.0 * sin (i * 1.8)));
1172
1173 static void
1174 run_fft_pipeline (int inrate, int outrate, int quality, int width,
1175     const gchar * format, void (*init) (GstBuffer *),
1176     void (*compare_ffts) (GstBuffer *, GstBuffer *))
1177 {
1178   GstElement *audioresample;
1179   GstBuffer *inbuffer, *outbuffer;
1180   const int nsamples = 2048;
1181
1182   audioresample = setup_audioresample (1, 0, inrate, outrate, format);
1183   fail_unless (audioresample != NULL);
1184   g_object_set (audioresample, "quality", quality, NULL);
1185
1186   fail_unless (gst_element_set_state (audioresample,
1187           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
1188       "could not set to playing");
1189
1190   inbuffer = gst_buffer_new_and_alloc (nsamples * width / 8);
1191   GST_BUFFER_DURATION (inbuffer) = GST_FRAMES_TO_CLOCK_TIME (nsamples, inrate);
1192   GST_BUFFER_TIMESTAMP (inbuffer) = 0;
1193
1194   (*init) (inbuffer);
1195
1196   gst_buffer_ref (inbuffer);
1197   /* pushing gives away my reference ... */
1198   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
1199   /* ... but it ends up being collected on the global buffer list */
1200   fail_unless_equals_int (g_list_length (buffers), 1);
1201   /* retrieve out buffer */
1202   fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
1203
1204   fail_unless (gst_element_set_state (audioresample,
1205           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
1206
1207   if (inbuffer == outbuffer)
1208     gst_buffer_unref (inbuffer);
1209
1210   (*compare_ffts) (inbuffer, outbuffer);
1211
1212   /* cleanup */
1213   cleanup_audioresample (audioresample);
1214 }
1215
1216 GST_START_TEST (test_fft)
1217 {
1218   int quality;
1219   size_t f0, f1;
1220   static const int frequencies[] =
1221       { 8000, 16000, 44100, 48000, 128000, 12345, 54321 };
1222
1223   /* audioresample uses a mixed float/double code path for floats with quality>8, make sure we test it */
1224   for (quality = 0; quality <= 10; quality += 5) {
1225     for (f0 = 0; f0 < G_N_ELEMENTS (frequencies); ++f0) {
1226       for (f1 = 0; f1 < G_N_ELEMENTS (frequencies); ++f1) {
1227         run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 32,
1228             GST_AUDIO_NE (F32), &init_float_silence, &compare_ffts_F32);
1229         run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 32,
1230             GST_AUDIO_NE (F32), &init_float_sine, &compare_ffts_F32);
1231         run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 32,
1232             GST_AUDIO_NE (F32), &init_float_sine2, &compare_ffts_F32);
1233         run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 64,
1234             GST_AUDIO_NE (F64), &init_double_silence, &compare_ffts_F64);
1235         run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 64,
1236             GST_AUDIO_NE (F64), &init_double_sine, &compare_ffts_F64);
1237         run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 64,
1238             GST_AUDIO_NE (F64), &init_double_sine2, &compare_ffts_F64);
1239         run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 16,
1240             GST_AUDIO_NE (S16), &init_gint16_silence, &compare_ffts_S16);
1241         run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 16,
1242             GST_AUDIO_NE (S16), &init_gint16_sine, &compare_ffts_S16);
1243         run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 16,
1244             GST_AUDIO_NE (S16), &init_gint16_sine2, &compare_ffts_S16);
1245         run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 32,
1246             GST_AUDIO_NE (S32), &init_gint32_silence, &compare_ffts_S32);
1247         run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 32,
1248             GST_AUDIO_NE (S32), &init_gint32_sine, &compare_ffts_S32);
1249         run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 32,
1250             GST_AUDIO_NE (S32), &init_gint32_sine2, &compare_ffts_S32);
1251       }
1252     }
1253   }
1254 }
1255
1256 GST_END_TEST;
1257
1258 static Suite *
1259 audioresample_suite (void)
1260 {
1261   Suite *s = suite_create ("audioresample");
1262   TCase *tc_chain = tcase_create ("general");
1263
1264   suite_add_tcase (s, tc_chain);
1265   tcase_add_test (tc_chain, test_perfect_stream);
1266   tcase_add_test (tc_chain, test_discont_stream);
1267   tcase_add_test (tc_chain, test_reuse);
1268   tcase_add_test (tc_chain, test_shutdown);
1269   tcase_add_test (tc_chain, test_live_switch);
1270   tcase_add_test (tc_chain, test_live_switch_downstream);
1271   tcase_add_test (tc_chain, test_timestamp_drift);
1272   tcase_add_test (tc_chain, test_fft);
1273
1274 #ifndef GST_DISABLE_PARSE
1275   tcase_set_timeout (tc_chain, 360);
1276   tcase_add_test (tc_chain, test_pipelines);
1277   tcase_add_test (tc_chain, test_preference_passthrough);
1278 #endif
1279
1280   return s;
1281 }
1282
1283 GST_CHECK_MAIN (audioresample);