Merging gst-python
[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 #if !GLIB_CHECK_VERSION(2,58,0)
473 #define G_APPROX_VALUE(a, b, epsilon) \
474   (((a) > (b) ? (a) - (b) : (b) - (a)) < (epsilon))
475 #endif
476
477 GST_START_TEST (test_live_switch)
478 {
479   GstElement *audioresample;
480   GstEvent *newseg;
481   GstCaps *caps;
482   GstSegment segment;
483   GList *l;
484   guint i;
485
486   audioresample =
487       setup_audioresample (4, 0xf, 48000, 48000, GST_AUDIO_NE (S16));
488
489   caps = gst_pad_get_current_caps (mysrcpad);
490   fail_unless (gst_caps_is_fixed (caps));
491
492   fail_unless (gst_element_set_state (audioresample,
493           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
494       "could not set to playing");
495
496   gst_segment_init (&segment, GST_FORMAT_TIME);
497   newseg = gst_event_new_segment (&segment);
498   fail_unless (gst_pad_push_event (mysrcpad, newseg) != FALSE);
499
500   /* downstream can accept the requested rate */
501   live_switch_push (0, 48000, caps);
502
503   /* buffer is directly passed through */
504   fail_unless_equals_int (g_list_length (buffers), 1);
505
506   /* Downstream can never accept this rate */
507   live_switch_push (1, 40000, caps);
508
509   /* one additional buffer is provided with the new sample rate */
510   fail_unless_equals_int (g_list_length (buffers), 2);
511
512   /* Downstream can never accept this rate */
513   live_switch_push (2, 50000, caps);
514
515   /* two additional buffers are provided. One is the drained remainder of
516    * the previous sample rate, the second is the buffer with the new sample
517    * rate */
518   fail_unless_equals_int (g_list_length (buffers), 4);
519
520   /* Send EOS to drain the remaining samples */
521   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()));
522   fail_unless_equals_int (g_list_length (buffers), 5);
523
524   /* Now test that each buffer has the expected samples. We simply check this
525    * by checking whether the timestamps, durations and sizes are matching */
526   for (l = buffers, i = 0; l; l = l->next, i++) {
527     GstBuffer *buffer = GST_BUFFER (l->data);
528
529     switch (i) {
530       case 0:
531         fail_unless_equals_uint64 (GST_BUFFER_PTS (buffer), 0 * GST_SECOND);
532         fail_unless_equals_uint64 (GST_BUFFER_DURATION (buffer),
533             1 * GST_SECOND);
534         fail_unless_equals_int (gst_buffer_get_size (buffer), 48000 * 4 * 2);
535         break;
536       case 1:
537         fail_unless_equals_uint64 (GST_BUFFER_PTS (buffer), 1 * GST_SECOND);
538         fail_unless_equals_int (gst_buffer_get_size (buffer), 47961 * 4 * 2);
539         break;
540       case 2:
541         fail_unless (G_APPROX_VALUE (GST_BUFFER_PTS (buffer) +
542                 GST_BUFFER_DURATION (buffer), 2 * GST_SECOND,
543                 GST_SECOND / 48000 + 1));
544         fail_unless_equals_int (gst_buffer_get_size (buffer), 38 * 4 * 2);
545         break;
546       case 3:
547         fail_unless_equals_uint64 (GST_BUFFER_PTS (buffer), 2 * GST_SECOND);
548         fail_unless_equals_int (gst_buffer_get_size (buffer), 47969 * 4 * 2);
549         break;
550       case 4:
551         fail_unless (G_APPROX_VALUE (GST_BUFFER_PTS (buffer) +
552                 GST_BUFFER_DURATION (buffer), 3 * GST_SECOND,
553                 GST_SECOND / 48000 + 1));
554         fail_unless_equals_int (gst_buffer_get_size (buffer), 30 * 4 * 2);
555         break;
556       default:
557         g_assert_not_reached ();
558         break;
559     }
560
561     gst_buffer_unref (buffer);
562   }
563
564   g_list_free (buffers);
565   buffers = NULL;
566
567   cleanup_audioresample (audioresample);
568   gst_caps_unref (caps);
569 }
570
571 GST_END_TEST;
572
573 static gint current_rate = 0;
574
575 static gboolean
576 live_switch_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
577 {
578   switch (GST_QUERY_TYPE (query)) {
579     case GST_QUERY_ACCEPT_CAPS:{
580       GstCaps *acceptable_caps;
581       GstCaps *caps;
582
583       acceptable_caps = gst_pad_get_current_caps (mysrcpad);
584       acceptable_caps = gst_caps_make_writable (acceptable_caps);
585       gst_caps_set_simple (acceptable_caps, "rate", G_TYPE_INT, current_rate,
586           NULL);
587
588       gst_query_parse_accept_caps (query, &caps);
589
590       gst_query_set_accept_caps_result (query, gst_caps_can_intersect (caps,
591               acceptable_caps));
592
593       gst_caps_unref (acceptable_caps);
594
595       return TRUE;
596     }
597     case GST_QUERY_CAPS:{
598       GstCaps *acceptable_caps;
599       GstCaps *filter;
600       GstCaps *caps;
601
602       acceptable_caps = gst_pad_get_current_caps (mysrcpad);
603       acceptable_caps = gst_caps_make_writable (acceptable_caps);
604       gst_caps_set_simple (acceptable_caps, "rate", G_TYPE_INT, current_rate,
605           NULL);
606
607       gst_query_parse_caps (query, &filter);
608
609       if (filter)
610         caps =
611             gst_caps_intersect_full (filter, acceptable_caps,
612             GST_CAPS_INTERSECT_FIRST);
613       else
614         caps = gst_caps_ref (acceptable_caps);
615
616       gst_query_set_caps_result (query, caps);
617
618       gst_caps_unref (caps);
619       gst_caps_unref (acceptable_caps);
620
621       return TRUE;
622     }
623     default:
624       return gst_pad_query_default (pad, parent, query);
625   }
626 }
627
628 static void
629 live_switch_push_downstream (gint pts, gint rate)
630 {
631   GstBuffer *inbuffer;
632
633   current_rate = rate;
634   gst_pad_push_event (mysinkpad, gst_event_new_reconfigure ());
635
636   inbuffer = gst_buffer_new_and_alloc (48000 * 4 * 2);
637   gst_buffer_memset (inbuffer, 0, 0, 48000 * 4 * 2);
638
639   GST_BUFFER_DURATION (inbuffer) = GST_SECOND;
640   GST_BUFFER_TIMESTAMP (inbuffer) = pts * GST_SECOND;
641   GST_BUFFER_OFFSET (inbuffer) = 0;
642   GST_BUFFER_OFFSET_END (inbuffer) = 47999;
643
644   /* pushing gives away my reference ... */
645   fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK);
646
647   /* ... but it ends up being collected on the global buffer list */
648 }
649
650 GST_START_TEST (test_live_switch_downstream)
651 {
652   GstElement *audioresample;
653   GstEvent *newseg;
654   GstCaps *caps;
655   GstSegment segment;
656   GList *l;
657   guint i;
658
659   audioresample =
660       setup_audioresample (4, 0xf, 48000, 48000, GST_AUDIO_NE (S16));
661
662   gst_pad_set_query_function (mysinkpad, live_switch_sink_query);
663
664   caps = gst_pad_get_current_caps (mysrcpad);
665   fail_unless (gst_caps_is_fixed (caps));
666
667   fail_unless (gst_element_set_state (audioresample,
668           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
669       "could not set to playing");
670
671   gst_segment_init (&segment, GST_FORMAT_TIME);
672   newseg = gst_event_new_segment (&segment);
673   fail_unless (gst_pad_push_event (mysrcpad, newseg) != FALSE);
674
675   /* buffer is directly passed through */
676   live_switch_push_downstream (0, 48000);
677   fail_unless_equals_int (g_list_length (buffers), 1);
678
679   /* Reconfigure downstream to 40000 Hz */
680   live_switch_push_downstream (1, 40000);
681
682   /* one additional buffer is provided with the new sample rate */
683   fail_unless_equals_int (g_list_length (buffers), 2);
684
685   /* Reconfigure downstream to 50000 Hz */
686   live_switch_push_downstream (2, 50000);
687
688   /* two additional buffers are provided. One is the drained remainder of
689    * the previous sample rate, the second is the buffer with the new sample
690    * rate */
691   fail_unless_equals_int (g_list_length (buffers), 4);
692
693   /* Send EOS to drain the remaining samples */
694   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()));
695   fail_unless_equals_int (g_list_length (buffers), 5);
696
697   /* Now test that each buffer has the expected samples. We simply check this
698    * by checking whether the timestamps, durations and sizes are matching */
699   for (l = buffers, i = 0; l; l = l->next, i++) {
700     GstBuffer *buffer = GST_BUFFER (l->data);
701
702     switch (i) {
703       case 0:
704         fail_unless_equals_uint64 (GST_BUFFER_PTS (buffer), 0 * GST_SECOND);
705         fail_unless_equals_uint64 (GST_BUFFER_DURATION (buffer),
706             1 * GST_SECOND);
707         fail_unless_equals_int (gst_buffer_get_size (buffer), 48000 * 4 * 2);
708         break;
709       case 1:
710         fail_unless_equals_uint64 (GST_BUFFER_PTS (buffer), 1 * GST_SECOND);
711         fail_unless_equals_int (gst_buffer_get_size (buffer), 39966 * 4 * 2);
712         break;
713       case 2:
714         fail_unless (G_APPROX_VALUE (GST_BUFFER_PTS (buffer) +
715                 GST_BUFFER_DURATION (buffer), 2 * GST_SECOND,
716                 GST_SECOND / 40000 + 1));
717         fail_unless_equals_int (gst_buffer_get_size (buffer), 34 * 4 * 2);
718         break;
719       case 3:
720         fail_unless_equals_uint64 (GST_BUFFER_PTS (buffer), 2 * GST_SECOND);
721         fail_unless_equals_int (gst_buffer_get_size (buffer), 49966 * 4 * 2);
722         break;
723       case 4:
724         fail_unless (G_APPROX_VALUE (GST_BUFFER_PTS (buffer) +
725                 GST_BUFFER_DURATION (buffer), 3 * GST_SECOND,
726                 GST_SECOND / 50000 + 1));
727         fail_unless_equals_int (gst_buffer_get_size (buffer), 33 * 4 * 2);
728         break;
729       default:
730         g_assert_not_reached ();
731         break;
732     }
733
734     gst_buffer_unref (buffer);
735   }
736
737   g_list_free (buffers);
738   buffers = NULL;
739
740   cleanup_audioresample (audioresample);
741   gst_caps_unref (caps);
742 }
743
744 GST_END_TEST;
745
746 #ifndef GST_DISABLE_PARSE
747
748 static GMainLoop *loop;
749 static gint messages = 0;
750
751 static void
752 element_message_cb (GstBus * bus, GstMessage * message, gpointer user_data)
753 {
754   gchar *s;
755
756   s = gst_structure_to_string (gst_message_get_structure (message));
757   GST_DEBUG ("Received message: %s", s);
758   g_free (s);
759
760   messages++;
761 }
762
763 static void
764 eos_message_cb (GstBus * bus, GstMessage * message, gpointer user_data)
765 {
766   GST_DEBUG ("Received eos");
767   g_main_loop_quit (loop);
768 }
769
770 static void
771 test_pipeline (const gchar * format, gint inrate, gint outrate, gint quality)
772 {
773   GstElement *pipeline;
774   GstBus *bus;
775   GError *error = NULL;
776   gchar *pipe_str;
777
778   pipe_str =
779       g_strdup_printf
780       ("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",
781       format, inrate, quality, format, outrate);
782
783   pipeline = gst_parse_launch (pipe_str, &error);
784   fail_unless (pipeline != NULL, "Error parsing pipeline: %s",
785       error ? error->message : "(invalid error)");
786   g_free (pipe_str);
787
788   bus = gst_element_get_bus (pipeline);
789   fail_if (bus == NULL);
790   gst_bus_add_signal_watch (bus);
791   g_signal_connect (bus, "message::element", (GCallback) element_message_cb,
792       NULL);
793   g_signal_connect (bus, "message::eos", (GCallback) eos_message_cb, NULL);
794
795   gst_element_set_state (pipeline, GST_STATE_PLAYING);
796
797   /* run until we receive EOS */
798   loop = g_main_loop_new (NULL, FALSE);
799
800   g_main_loop_run (loop);
801
802   g_main_loop_unref (loop);
803   loop = NULL;
804
805   gst_element_set_state (pipeline, GST_STATE_NULL);
806
807   gst_bus_remove_signal_watch (bus);
808   gst_object_unref (bus);
809
810   fail_if (messages > 0, "Received imperfect timestamp messages");
811   gst_object_unref (pipeline);
812 }
813
814 GST_START_TEST (test_pipelines)
815 {
816   gint quality;
817
818   /* Test qualities 0, 5 and 10 */
819   for (quality = 0; quality < 11; quality += 5) {
820     GST_DEBUG ("Checking with quality %d", quality);
821
822     test_pipeline ("S8", 44100, 48000, quality);
823     test_pipeline ("S8", 48000, 44100, quality);
824
825     test_pipeline (GST_AUDIO_NE (S16), 44100, 48000, quality);
826     test_pipeline (GST_AUDIO_NE (S16), 48000, 44100, quality);
827
828     test_pipeline (GST_AUDIO_NE (S24), 44100, 48000, quality);
829     test_pipeline (GST_AUDIO_NE (S24), 48000, 44100, quality);
830
831     test_pipeline (GST_AUDIO_NE (S32), 44100, 48000, quality);
832     test_pipeline (GST_AUDIO_NE (S32), 48000, 44100, quality);
833
834     test_pipeline (GST_AUDIO_NE (F32), 44100, 48000, quality);
835     test_pipeline (GST_AUDIO_NE (F32), 48000, 44100, quality);
836
837     test_pipeline (GST_AUDIO_NE (F64), 44100, 48000, quality);
838     test_pipeline (GST_AUDIO_NE (F64), 48000, 44100, quality);
839   }
840 }
841
842 GST_END_TEST;
843
844 GST_START_TEST (test_preference_passthrough)
845 {
846   GstStateChangeReturn ret;
847   GstElement *pipeline, *src;
848   GstStructure *s;
849   GstMessage *msg;
850   GstCaps *caps;
851   GstPad *pad;
852   GstBus *bus;
853   GError *error = NULL;
854   gint rate = 0;
855
856   pipeline = gst_parse_launch ("audiotestsrc num-buffers=1 name=src ! "
857       "audioresample ! audio/x-raw,format=" GST_AUDIO_NE (S16) ",channels=1,"
858       "rate=8000 ! fakesink can-activate-pull=false", &error);
859   fail_unless (pipeline != NULL, "Error parsing pipeline: %s",
860       error ? error->message : "(invalid error)");
861
862   ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
863   fail_unless_equals_int (ret, GST_STATE_CHANGE_ASYNC);
864
865   /* run until we receive EOS */
866   bus = gst_element_get_bus (pipeline);
867   fail_if (bus == NULL);
868   msg = gst_bus_timed_pop_filtered (bus, -1, GST_MESSAGE_EOS);
869   gst_message_unref (msg);
870   gst_object_unref (bus);
871
872   src = gst_bin_get_by_name (GST_BIN (pipeline), "src");
873   fail_unless (src != NULL);
874   pad = gst_element_get_static_pad (src, "src");
875   fail_unless (pad != NULL);
876   caps = gst_pad_get_current_caps (pad);
877   GST_LOG ("current audiotestsrc caps: %" GST_PTR_FORMAT, caps);
878   fail_unless (caps != NULL);
879   s = gst_caps_get_structure (caps, 0);
880   fail_unless (gst_structure_get_int (s, "rate", &rate));
881   /* there's no need to resample, audiotestsrc supports any rate, so make
882    * sure audioresample provided upstream with the right caps to negotiate
883    * this correctly */
884   fail_unless_equals_int (rate, 8000);
885   gst_caps_unref (caps);
886   gst_object_unref (pad);
887   gst_object_unref (src);
888
889   gst_element_set_state (pipeline, GST_STATE_NULL);
890   gst_object_unref (pipeline);
891 }
892
893 GST_END_TEST;
894
895 #endif
896
897 static void
898 _message_cb (GstBus * bus, GstMessage * message, gpointer user_data)
899 {
900   GMainLoop *loop = user_data;
901
902   switch (GST_MESSAGE_TYPE (message)) {
903     case GST_MESSAGE_ERROR:
904     case GST_MESSAGE_WARNING:
905       g_assert_not_reached ();
906       break;
907     case GST_MESSAGE_EOS:
908       g_main_loop_quit (loop);
909       break;
910     default:
911       break;
912   }
913 }
914
915 typedef struct
916 {
917   guint64 latency;
918   GstClockTime in_ts;
919
920   GstClockTime next_out_ts;
921   guint64 next_out_off;
922
923   guint64 in_buffer_count, out_buffer_count;
924 } TimestampDriftCtx;
925
926 static void
927 fakesink_handoff_cb (GstElement * object, GstBuffer * buffer, GstPad * pad,
928     gpointer user_data)
929 {
930   TimestampDriftCtx *ctx = user_data;
931
932   ctx->out_buffer_count++;
933   if (ctx->latency == GST_CLOCK_TIME_NONE) {
934     ctx->latency = 1000 - gst_buffer_get_size (buffer) / 8;
935   }
936
937   /* Check if we have a perfectly timestamped stream */
938   if (ctx->next_out_ts != GST_CLOCK_TIME_NONE)
939     fail_unless (ctx->next_out_ts == GST_BUFFER_TIMESTAMP (buffer),
940         "expected timestamp %" GST_TIME_FORMAT " got timestamp %"
941         GST_TIME_FORMAT, GST_TIME_ARGS (ctx->next_out_ts),
942         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));
943
944   /* Check if we have a perfectly offsetted stream */
945   fail_unless (GST_BUFFER_OFFSET_END (buffer) ==
946       GST_BUFFER_OFFSET (buffer) + gst_buffer_get_size (buffer) / 8,
947       "expected offset end %" G_GUINT64_FORMAT " got offset end %"
948       G_GUINT64_FORMAT,
949       GST_BUFFER_OFFSET (buffer) + gst_buffer_get_size (buffer) / 8,
950       GST_BUFFER_OFFSET_END (buffer));
951   if (ctx->next_out_off != GST_BUFFER_OFFSET_NONE) {
952     fail_unless (GST_BUFFER_OFFSET (buffer) == ctx->next_out_off,
953         "expected offset %" G_GUINT64_FORMAT " got offset %" G_GUINT64_FORMAT,
954         ctx->next_out_off, GST_BUFFER_OFFSET (buffer));
955   }
956
957   if (ctx->in_buffer_count != ctx->out_buffer_count) {
958     GST_INFO ("timestamp %" GST_TIME_FORMAT,
959         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));
960   }
961
962   if (ctx->in_ts != GST_CLOCK_TIME_NONE && ctx->in_buffer_count > 1
963       && ctx->in_buffer_count == ctx->out_buffer_count) {
964     fail_unless (GST_BUFFER_TIMESTAMP (buffer) ==
965         ctx->in_ts - gst_util_uint64_scale_round (ctx->latency, GST_SECOND,
966             4096),
967         "expected output timestamp %" GST_TIME_FORMAT " (%" G_GUINT64_FORMAT
968         ") got output timestamp %" GST_TIME_FORMAT " (%" G_GUINT64_FORMAT ")",
969         GST_TIME_ARGS (ctx->in_ts - gst_util_uint64_scale_round (ctx->latency,
970                 GST_SECOND, 4096)),
971         ctx->in_ts - gst_util_uint64_scale_round (ctx->latency, GST_SECOND,
972             4096), GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
973         GST_BUFFER_TIMESTAMP (buffer));
974   }
975
976   ctx->next_out_ts =
977       GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer);
978   ctx->next_out_off = GST_BUFFER_OFFSET_END (buffer);
979 }
980
981 static void
982 identity_handoff_cb (GstElement * object, GstBuffer * buffer,
983     gpointer user_data)
984 {
985   TimestampDriftCtx *ctx = user_data;
986
987   ctx->in_ts = GST_BUFFER_TIMESTAMP (buffer);
988   ctx->in_buffer_count++;
989 }
990
991 GST_START_TEST (test_timestamp_drift)
992 {
993   TimestampDriftCtx ctx =
994       { GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE,
995     GST_BUFFER_OFFSET_NONE, 0, 0
996   };
997   GstElement *pipeline;
998   GstElement *audiotestsrc, *capsfilter1, *identity, *audioresample,
999       *capsfilter2, *fakesink;
1000   GstBus *bus;
1001   GMainLoop *loop;
1002   GstCaps *caps;
1003
1004   pipeline = gst_pipeline_new ("pipeline");
1005   fail_unless (pipeline != NULL);
1006
1007   audiotestsrc = gst_element_factory_make ("audiotestsrc", "src");
1008   fail_unless (audiotestsrc != NULL);
1009   g_object_set (G_OBJECT (audiotestsrc), "num-buffers", 10000,
1010       "samplesperbuffer", 4000, NULL);
1011
1012   capsfilter1 = gst_element_factory_make ("capsfilter", "capsfilter1");
1013   fail_unless (capsfilter1 != NULL);
1014   caps = gst_caps_from_string ("audio/x-raw, format=" GST_AUDIO_NE (F64)
1015       ", channels=1, rate=16384");
1016   g_object_set (G_OBJECT (capsfilter1), "caps", caps, NULL);
1017   gst_caps_unref (caps);
1018
1019   identity = gst_element_factory_make ("identity", "identity");
1020   fail_unless (identity != NULL);
1021   g_object_set (G_OBJECT (identity), "sync", FALSE, "signal-handoffs", TRUE,
1022       NULL);
1023   g_signal_connect (identity, "handoff", (GCallback) identity_handoff_cb, &ctx);
1024
1025   audioresample = gst_element_factory_make ("audioresample", "resample");
1026   fail_unless (audioresample != NULL);
1027   capsfilter2 = gst_element_factory_make ("capsfilter", "capsfilter2");
1028   fail_unless (capsfilter2 != NULL);
1029   caps = gst_caps_from_string ("audio/x-raw, format=" GST_AUDIO_NE (F64)
1030       ", channels=1, rate=4096");
1031   g_object_set (G_OBJECT (capsfilter2), "caps", caps, NULL);
1032   gst_caps_unref (caps);
1033
1034   fakesink = gst_element_factory_make ("fakesink", "sink");
1035   fail_unless (fakesink != NULL);
1036   g_object_set (G_OBJECT (fakesink), "sync", FALSE, "async", FALSE,
1037       "signal-handoffs", TRUE, NULL);
1038   g_signal_connect (fakesink, "handoff", (GCallback) fakesink_handoff_cb, &ctx);
1039
1040
1041   gst_bin_add_many (GST_BIN (pipeline), audiotestsrc, capsfilter1, identity,
1042       audioresample, capsfilter2, fakesink, NULL);
1043   fail_unless (gst_element_link_many (audiotestsrc, capsfilter1, identity,
1044           audioresample, capsfilter2, fakesink, NULL));
1045
1046   loop = g_main_loop_new (NULL, FALSE);
1047
1048   bus = gst_element_get_bus (pipeline);
1049   gst_bus_add_signal_watch (bus);
1050   g_signal_connect (bus, "message", (GCallback) _message_cb, loop);
1051
1052   fail_unless (gst_element_set_state (pipeline,
1053           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS);
1054   g_main_loop_run (loop);
1055
1056   fail_unless (gst_element_set_state (pipeline,
1057           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS);
1058   g_main_loop_unref (loop);
1059   gst_bus_remove_signal_watch (bus);
1060   gst_object_unref (bus);
1061
1062   gst_object_unref (pipeline);
1063
1064 } GST_END_TEST;
1065
1066 #define FFT_HELPERS(type,ffttag,ffttag2,scale);                                                 \
1067 static gdouble magnitude##ffttag (const GstFFT##ffttag##Complex *c)                             \
1068 {                                                                                               \
1069   gdouble mag = (gdouble) c->r * (gdouble) c->r;                                                \
1070   mag += (gdouble) c->i * (gdouble) c->i;                                                       \
1071   mag /= scale * scale;                                                                         \
1072   mag = 10.0 * log10 (mag);                                                                     \
1073   return mag;                                                                                   \
1074 }                                                                                               \
1075 static gdouble find_main_frequency_spot_##ffttag (const GstFFT##ffttag##Complex *v,             \
1076                                                   int elements)                                 \
1077 {                                                                                               \
1078   int i;                                                                                        \
1079   gdouble maxmag = -9999;                                                                       \
1080   int maxidx = 0;                                                                               \
1081   for (i=0; i<elements; ++i) {                                                                  \
1082     gdouble mag = magnitude##ffttag (v+i);                                                      \
1083     if (mag > maxmag) {                                                                         \
1084       maxmag = mag;                                                                             \
1085       maxidx = i;                                                                               \
1086     }                                                                                           \
1087   }                                                                                             \
1088   return maxidx / (gdouble) elements;                                                           \
1089 }                                                                                               \
1090 static gboolean is_zero_except_##ffttag (const GstFFT##ffttag##Complex *v, int elements,        \
1091                                 gdouble spot)                                                   \
1092 {                                                                                               \
1093   int i;                                                                                        \
1094   for (i=0; i<elements; ++i) {                                                                  \
1095     gdouble pos = i / (gdouble) elements;                                                       \
1096     gdouble mag = magnitude##ffttag (v+i);                                                      \
1097     if (fabs (pos - spot) > 0.01) {                                                             \
1098       if (mag > -55.0) {                                                                        \
1099         return FALSE;                                                                           \
1100       }                                                                                         \
1101     }                                                                                           \
1102   }                                                                                             \
1103   return TRUE;                                                                                  \
1104 }                                                                                               \
1105 static void compare_ffts_##ffttag (GstBuffer *inbuffer, GstBuffer *outbuffer)                   \
1106 {                                                                                               \
1107   GstMapInfo inmap, outmap;                                                                     \
1108   int insamples, outsamples;                                                                    \
1109   gdouble inspot, outspot;                                                                      \
1110   GstFFT##ffttag *inctx, *outctx;                                                               \
1111   GstFFT##ffttag##Complex *in, *out;                                                            \
1112                                                                                                 \
1113   gst_buffer_map (inbuffer, &inmap, GST_MAP_READ);                                              \
1114   gst_buffer_map (outbuffer, &outmap, GST_MAP_READWRITE);                                       \
1115                                                                                                 \
1116   insamples = inmap.size / sizeof(type) & ~1;                                                   \
1117   outsamples = outmap.size / sizeof(type) & ~1;                                                 \
1118   inctx = gst_fft_##ffttag2##_new (insamples, FALSE);                                           \
1119   outctx = gst_fft_##ffttag2##_new (outsamples, FALSE);                                         \
1120   in = g_new (GstFFT##ffttag##Complex, insamples / 2 + 1);                                      \
1121   out = g_new (GstFFT##ffttag##Complex, outsamples / 2 + 1);                                    \
1122                                                                                                 \
1123   gst_fft_##ffttag2##_window (inctx, (type*)inmap.data,                                         \
1124       GST_FFT_WINDOW_HAMMING);                                                                  \
1125   gst_fft_##ffttag2##_fft (inctx, (type*)inmap.data, in);                                       \
1126   gst_fft_##ffttag2##_window (outctx, (type*)outmap.data,                                       \
1127       GST_FFT_WINDOW_HAMMING);                                                                  \
1128   gst_fft_##ffttag2##_fft (outctx, (type*)outmap.data, out);                                    \
1129                                                                                                 \
1130   inspot = find_main_frequency_spot_##ffttag (in, insamples / 2 + 1);                           \
1131   outspot = find_main_frequency_spot_##ffttag (out, outsamples / 2 + 1);                        \
1132   GST_LOG ("Spots are %.3f and %.3f", inspot, outspot);                                         \
1133   fail_unless (fabs (outspot - inspot) < 0.05);                                                 \
1134   fail_unless (is_zero_except_##ffttag (in, insamples / 2 + 1, inspot));                        \
1135   fail_unless (is_zero_except_##ffttag (out, outsamples / 2 + 1, outspot));                     \
1136                                                                                                 \
1137   gst_buffer_unmap (inbuffer, &inmap);                                                          \
1138   gst_buffer_unmap (outbuffer, &outmap);                                                        \
1139                                                                                                 \
1140   gst_fft_##ffttag2##_free (inctx);                                                             \
1141   gst_fft_##ffttag2##_free (outctx);                                                            \
1142   g_free (in);                                                                                  \
1143   g_free (out);                                                                                 \
1144 }
1145 FFT_HELPERS (float, F32, f32, 2048.0f);
1146 FFT_HELPERS (double, F64, f64, 2048.0);
1147 FFT_HELPERS (gint16, S16, s16, 32767.0);
1148 FFT_HELPERS (gint32, S32, s32, 2147483647.0);
1149
1150 #define FILL_BUFFER(type, desc, value);                         \
1151   static void init_##type##_##desc (GstBuffer *buffer)          \
1152   {                                                             \
1153     GstMapInfo map;                                             \
1154     type *ptr;                                                  \
1155     int i, nsamples;                                            \
1156     gst_buffer_map (buffer, &map, GST_MAP_WRITE);               \
1157     ptr = (type *)map.data;                                     \
1158     nsamples = map.size / sizeof (type);                        \
1159     for (i = 0; i < nsamples; ++i) {                            \
1160       *ptr++ = value;                                           \
1161     }                                                           \
1162     gst_buffer_unmap (buffer, &map);                            \
1163   }
1164
1165 FILL_BUFFER (float, silence, 0.0f);
1166 FILL_BUFFER (double, silence, 0.0);
1167 FILL_BUFFER (gint16, silence, 0);
1168 FILL_BUFFER (gint32, silence, 0);
1169 FILL_BUFFER (float, sine, sinf (i * 0.01f));
1170 FILL_BUFFER (float, sine2, sinf (i * 1.8f));
1171 FILL_BUFFER (double, sine, sin (i * 0.01));
1172 FILL_BUFFER (double, sine2, sin (i * 1.8));
1173 FILL_BUFFER (gint16, sine, (gint16) (32767 * sinf (i * 0.01f)));
1174 FILL_BUFFER (gint16, sine2, (gint16) (32767 * sinf (i * 1.8f)));
1175 FILL_BUFFER (gint32, sine, (gint32) (2147483647.0 * sin (i * 0.01)));
1176 FILL_BUFFER (gint32, sine2, (gint32) (2147483647.0 * sin (i * 1.8)));
1177
1178 static void
1179 run_fft_pipeline (int inrate, int outrate, int quality, int width,
1180     const gchar * format, void (*init) (GstBuffer *),
1181     void (*compare_ffts) (GstBuffer *, GstBuffer *))
1182 {
1183   GstElement *audioresample;
1184   GstBuffer *inbuffer, *outbuffer;
1185   const int nsamples = 2048;
1186
1187   audioresample = setup_audioresample (1, 0, inrate, outrate, format);
1188   fail_unless (audioresample != NULL);
1189   g_object_set (audioresample, "quality", quality, NULL);
1190
1191   fail_unless (gst_element_set_state (audioresample,
1192           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
1193       "could not set to playing");
1194
1195   inbuffer = gst_buffer_new_and_alloc (nsamples * width / 8);
1196   GST_BUFFER_DURATION (inbuffer) = GST_FRAMES_TO_CLOCK_TIME (nsamples, inrate);
1197   GST_BUFFER_TIMESTAMP (inbuffer) = 0;
1198
1199   (*init) (inbuffer);
1200
1201   gst_buffer_ref (inbuffer);
1202   /* pushing gives away my reference ... */
1203   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
1204   /* ... but it ends up being collected on the global buffer list */
1205   fail_unless_equals_int (g_list_length (buffers), 1);
1206   /* retrieve out buffer */
1207   fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
1208
1209   fail_unless (gst_element_set_state (audioresample,
1210           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
1211
1212   if (inbuffer == outbuffer)
1213     gst_buffer_unref (inbuffer);
1214
1215   (*compare_ffts) (inbuffer, outbuffer);
1216
1217   /* cleanup */
1218   cleanup_audioresample (audioresample);
1219 }
1220
1221 GST_START_TEST (test_fft)
1222 {
1223   int quality;
1224   size_t f0, f1;
1225   static const int frequencies[] =
1226       { 8000, 16000, 44100, 48000, 128000, 12345, 54321 };
1227
1228   /* audioresample uses a mixed float/double code path for floats with quality>8, make sure we test it */
1229   for (quality = 0; quality <= 10; quality += 5) {
1230     for (f0 = 0; f0 < G_N_ELEMENTS (frequencies); ++f0) {
1231       for (f1 = 0; f1 < G_N_ELEMENTS (frequencies); ++f1) {
1232         run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 32,
1233             GST_AUDIO_NE (F32), &init_float_silence, &compare_ffts_F32);
1234         run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 32,
1235             GST_AUDIO_NE (F32), &init_float_sine, &compare_ffts_F32);
1236         run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 32,
1237             GST_AUDIO_NE (F32), &init_float_sine2, &compare_ffts_F32);
1238         run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 64,
1239             GST_AUDIO_NE (F64), &init_double_silence, &compare_ffts_F64);
1240         run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 64,
1241             GST_AUDIO_NE (F64), &init_double_sine, &compare_ffts_F64);
1242         run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 64,
1243             GST_AUDIO_NE (F64), &init_double_sine2, &compare_ffts_F64);
1244         run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 16,
1245             GST_AUDIO_NE (S16), &init_gint16_silence, &compare_ffts_S16);
1246         run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 16,
1247             GST_AUDIO_NE (S16), &init_gint16_sine, &compare_ffts_S16);
1248         run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 16,
1249             GST_AUDIO_NE (S16), &init_gint16_sine2, &compare_ffts_S16);
1250         run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 32,
1251             GST_AUDIO_NE (S32), &init_gint32_silence, &compare_ffts_S32);
1252         run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 32,
1253             GST_AUDIO_NE (S32), &init_gint32_sine, &compare_ffts_S32);
1254         run_fft_pipeline (frequencies[f0], frequencies[f0], quality, 32,
1255             GST_AUDIO_NE (S32), &init_gint32_sine2, &compare_ffts_S32);
1256       }
1257     }
1258   }
1259 }
1260
1261 GST_END_TEST;
1262
1263 static Suite *
1264 audioresample_suite (void)
1265 {
1266   Suite *s = suite_create ("audioresample");
1267   TCase *tc_chain = tcase_create ("general");
1268
1269   suite_add_tcase (s, tc_chain);
1270   tcase_add_test (tc_chain, test_perfect_stream);
1271   tcase_add_test (tc_chain, test_discont_stream);
1272   tcase_add_test (tc_chain, test_reuse);
1273   tcase_add_test (tc_chain, test_shutdown);
1274   tcase_add_test (tc_chain, test_live_switch);
1275   tcase_add_test (tc_chain, test_live_switch_downstream);
1276   tcase_add_test (tc_chain, test_timestamp_drift);
1277   tcase_add_test (tc_chain, test_fft);
1278
1279 #ifndef GST_DISABLE_PARSE
1280   tcase_set_timeout (tc_chain, 360);
1281   tcase_add_test (tc_chain, test_pipelines);
1282   tcase_add_test (tc_chain, test_preference_passthrough);
1283 #endif
1284
1285   return s;
1286 }
1287
1288 GST_CHECK_MAIN (audioresample);