Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / 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., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #include <unistd.h>
25
26 #include <gst/check/gstcheck.h>
27
28 #include <gst/audio/audio.h>
29
30 /* For ease of programming we use globals to keep refs for our floating
31  * src and sink pads we create; otherwise we always have to do get_pad,
32  * get_peer, and then remove references in every test function */
33 static GstPad *mysrcpad, *mysinkpad;
34
35 #define RESAMPLE_CAPS_FLOAT             \
36     "audio/x-raw-float, "               \
37     "channels = (int) [ 1, MAX ], "     \
38     "rate = (int) [ 1,  MAX ], "        \
39     "endianness = (int) BYTE_ORDER, "   \
40     "width = (int) { 32, 64 }"
41
42 #define RESAMPLE_CAPS_INT               \
43     "audio/x-raw-int, "                 \
44     "channels = (int) [ 1, MAX ], "     \
45     "rate = (int) [ 1,  MAX ], "        \
46     "endianness = (int) BYTE_ORDER, "   \
47     "width = (int) 16, "                \
48     "depth = (int) 16, "                \
49     "signed = (bool) TRUE"
50
51 #define RESAMPLE_CAPS_TEMPLATE_STRING   \
52     RESAMPLE_CAPS_FLOAT " ; " \
53     RESAMPLE_CAPS_INT
54
55 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
56     GST_PAD_SINK,
57     GST_PAD_ALWAYS,
58     GST_STATIC_CAPS (RESAMPLE_CAPS_TEMPLATE_STRING)
59     );
60 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
61     GST_PAD_SRC,
62     GST_PAD_ALWAYS,
63     GST_STATIC_CAPS (RESAMPLE_CAPS_TEMPLATE_STRING)
64     );
65
66 static GstElement *
67 setup_audioresample (int channels, int inrate, int outrate, int width,
68     gboolean fp)
69 {
70   GstElement *audioresample;
71   GstCaps *caps;
72   GstStructure *structure;
73
74   GST_DEBUG ("setup_audioresample");
75   audioresample = gst_check_setup_element ("audioresample");
76
77   if (fp)
78     caps = gst_caps_from_string (RESAMPLE_CAPS_FLOAT);
79   else
80     caps = gst_caps_from_string (RESAMPLE_CAPS_INT);
81   structure = gst_caps_get_structure (caps, 0);
82   gst_structure_set (structure, "channels", G_TYPE_INT, channels,
83       "rate", G_TYPE_INT, inrate, "width", G_TYPE_INT, width, NULL);
84   if (!fp)
85     gst_structure_set (structure, "depth", G_TYPE_INT, width, NULL);
86   fail_unless (gst_caps_is_fixed (caps));
87
88   fail_unless (gst_element_set_state (audioresample,
89           GST_STATE_PAUSED) == GST_STATE_CHANGE_SUCCESS,
90       "could not set to paused");
91
92   mysrcpad = gst_check_setup_src_pad (audioresample, &srctemplate, caps);
93   gst_pad_set_caps (mysrcpad, caps);
94   gst_caps_unref (caps);
95
96   if (fp)
97     caps = gst_caps_from_string (RESAMPLE_CAPS_FLOAT);
98   else
99     caps = gst_caps_from_string (RESAMPLE_CAPS_INT);
100   structure = gst_caps_get_structure (caps, 0);
101   gst_structure_set (structure, "channels", G_TYPE_INT, channels,
102       "rate", G_TYPE_INT, outrate, "width", G_TYPE_INT, width, NULL);
103   if (!fp)
104     gst_structure_set (structure, "depth", G_TYPE_INT, width, NULL);
105   fail_unless (gst_caps_is_fixed (caps));
106
107   mysinkpad = gst_check_setup_sink_pad (audioresample, &sinktemplate, caps);
108   /* this installs a getcaps func that will always return the caps we set
109    * later */
110   gst_pad_set_caps (mysinkpad, caps);
111   gst_pad_use_fixed_caps (mysinkpad);
112
113   gst_pad_set_active (mysinkpad, TRUE);
114   gst_pad_set_active (mysrcpad, TRUE);
115
116   gst_caps_unref (caps);
117
118   return audioresample;
119 }
120
121 static void
122 cleanup_audioresample (GstElement * audioresample)
123 {
124   GST_DEBUG ("cleanup_audioresample");
125
126   fail_unless (gst_element_set_state (audioresample,
127           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to NULL");
128
129   gst_pad_set_active (mysrcpad, FALSE);
130   gst_pad_set_active (mysinkpad, FALSE);
131   gst_check_teardown_src_pad (audioresample);
132   gst_check_teardown_sink_pad (audioresample);
133   gst_check_teardown_element (audioresample);
134   gst_check_drop_buffers ();
135 }
136
137 static void
138 fail_unless_perfect_stream (void)
139 {
140   guint64 timestamp = 0L, duration = 0L;
141   guint64 offset = 0L, offset_end = 0L;
142
143   GList *l;
144   GstBuffer *buffer;
145
146   for (l = buffers; l; l = l->next) {
147     buffer = GST_BUFFER (l->data);
148     ASSERT_BUFFER_REFCOUNT (buffer, "buffer", 1);
149     GST_DEBUG ("buffer timestamp %" G_GUINT64_FORMAT ", duration %"
150         G_GUINT64_FORMAT " offset %" G_GUINT64_FORMAT " offset_end %"
151         G_GUINT64_FORMAT,
152         GST_BUFFER_TIMESTAMP (buffer),
153         GST_BUFFER_DURATION (buffer),
154         GST_BUFFER_OFFSET (buffer), GST_BUFFER_OFFSET_END (buffer));
155
156     fail_unless_equals_uint64 (timestamp, GST_BUFFER_TIMESTAMP (buffer));
157     fail_unless_equals_uint64 (offset, GST_BUFFER_OFFSET (buffer));
158     duration = GST_BUFFER_DURATION (buffer);
159     offset_end = GST_BUFFER_OFFSET_END (buffer);
160
161     timestamp += duration;
162     offset = offset_end;
163     gst_buffer_unref (buffer);
164   }
165   g_list_free (buffers);
166   buffers = NULL;
167 }
168
169 /* this tests that the output is a perfect stream if the input is */
170 static void
171 test_perfect_stream_instance (int inrate, int outrate, int samples,
172     int numbuffers)
173 {
174   GstElement *audioresample;
175   GstBuffer *inbuffer, *outbuffer;
176   GstCaps *caps;
177   guint64 offset = 0;
178
179   int i, j;
180   gint16 *p;
181
182   audioresample = setup_audioresample (2, inrate, outrate, 16, FALSE);
183   caps = gst_pad_get_negotiated_caps (mysrcpad);
184   fail_unless (gst_caps_is_fixed (caps));
185
186   fail_unless (gst_element_set_state (audioresample,
187           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
188       "could not set to playing");
189
190   for (j = 1; j <= numbuffers; ++j) {
191
192     inbuffer = gst_buffer_new_and_alloc (samples * 4);
193     GST_BUFFER_DURATION (inbuffer) = GST_FRAMES_TO_CLOCK_TIME (samples, inrate);
194     GST_BUFFER_TIMESTAMP (inbuffer) = GST_BUFFER_DURATION (inbuffer) * (j - 1);
195     GST_BUFFER_OFFSET (inbuffer) = offset;
196     offset += samples;
197     GST_BUFFER_OFFSET_END (inbuffer) = offset;
198
199     gst_buffer_set_caps (inbuffer, caps);
200
201     p = (gint16 *) GST_BUFFER_DATA (inbuffer);
202
203     /* create a 16 bit signed ramp */
204     for (i = 0; i < samples; ++i) {
205       *p = -32767 + i * (65535 / samples);
206       ++p;
207       *p = -32767 + i * (65535 / samples);
208       ++p;
209     }
210
211     /* pushing gives away my reference ... */
212     fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
213     /* ... but it ends up being collected on the global buffer list */
214     fail_unless_equals_int (g_list_length (buffers), j);
215   }
216
217   /* FIXME: we should make audioresample handle eos by flushing out the last
218    * samples, which will give us one more, small, buffer */
219   fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
220   ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
221
222   fail_unless_perfect_stream ();
223
224   /* cleanup */
225   gst_caps_unref (caps);
226   cleanup_audioresample (audioresample);
227 }
228
229
230 /* make sure that outgoing buffers are contiguous in timestamp/duration and
231  * offset/offsetend
232  */
233 GST_START_TEST (test_perfect_stream)
234 {
235   /* integral scalings */
236   test_perfect_stream_instance (48000, 24000, 500, 20);
237   test_perfect_stream_instance (48000, 12000, 500, 20);
238   test_perfect_stream_instance (12000, 24000, 500, 20);
239   test_perfect_stream_instance (12000, 48000, 500, 20);
240
241   /* non-integral scalings */
242   test_perfect_stream_instance (44100, 8000, 500, 20);
243   test_perfect_stream_instance (8000, 44100, 500, 20);
244
245   /* wacky scalings */
246   test_perfect_stream_instance (12345, 54321, 500, 20);
247   test_perfect_stream_instance (101, 99, 500, 20);
248 }
249
250 GST_END_TEST;
251
252 /* this tests that the output is a correct discontinuous stream
253  * if the input is; ie input drops in time come out the same way */
254 static void
255 test_discont_stream_instance (int inrate, int outrate, int samples,
256     int numbuffers)
257 {
258   GstElement *audioresample;
259   GstBuffer *inbuffer, *outbuffer;
260   GstCaps *caps;
261   GstClockTime ints;
262
263   int i, j;
264   gint16 *p;
265
266   GST_DEBUG ("inrate:%d outrate:%d samples:%d numbuffers:%d",
267       inrate, outrate, samples, numbuffers);
268
269   audioresample = setup_audioresample (2, inrate, outrate, 16, FALSE);
270   caps = gst_pad_get_negotiated_caps (mysrcpad);
271   fail_unless (gst_caps_is_fixed (caps));
272
273   fail_unless (gst_element_set_state (audioresample,
274           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
275       "could not set to playing");
276
277   for (j = 1; j <= numbuffers; ++j) {
278
279     inbuffer = gst_buffer_new_and_alloc (samples * 4);
280     GST_BUFFER_DURATION (inbuffer) = samples * GST_SECOND / inrate;
281     /* "drop" half the buffers */
282     ints = GST_BUFFER_DURATION (inbuffer) * 2 * (j - 1);
283     GST_BUFFER_TIMESTAMP (inbuffer) = ints;
284     GST_BUFFER_OFFSET (inbuffer) = (j - 1) * 2 * samples;
285     GST_BUFFER_OFFSET_END (inbuffer) = j * 2 * samples + samples;
286
287     gst_buffer_set_caps (inbuffer, caps);
288
289     p = (gint16 *) GST_BUFFER_DATA (inbuffer);
290
291     /* create a 16 bit signed ramp */
292     for (i = 0; i < samples; ++i) {
293       *p = -32767 + i * (65535 / samples);
294       ++p;
295       *p = -32767 + i * (65535 / samples);
296       ++p;
297     }
298
299     GST_DEBUG ("Sending Buffer time:%" G_GUINT64_FORMAT " duration:%"
300         G_GINT64_FORMAT " discont:%d offset:%" G_GUINT64_FORMAT " offset_end:%"
301         G_GUINT64_FORMAT, GST_BUFFER_TIMESTAMP (inbuffer),
302         GST_BUFFER_DURATION (inbuffer), GST_BUFFER_IS_DISCONT (inbuffer),
303         GST_BUFFER_OFFSET (inbuffer), GST_BUFFER_OFFSET_END (inbuffer));
304     /* pushing gives away my reference ... */
305     fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
306
307     /* check if the timestamp of the pushed buffer matches the incoming one */
308     outbuffer = g_list_nth_data (buffers, g_list_length (buffers) - 1);
309     fail_if (outbuffer == NULL);
310     fail_unless_equals_uint64 (ints, GST_BUFFER_TIMESTAMP (outbuffer));
311     GST_DEBUG ("Got Buffer time:%" G_GUINT64_FORMAT " duration:%"
312         G_GINT64_FORMAT " discont:%d offset:%" G_GUINT64_FORMAT " offset_end:%"
313         G_GUINT64_FORMAT, GST_BUFFER_TIMESTAMP (outbuffer),
314         GST_BUFFER_DURATION (outbuffer), GST_BUFFER_IS_DISCONT (outbuffer),
315         GST_BUFFER_OFFSET (outbuffer), GST_BUFFER_OFFSET_END (outbuffer));
316     if (j > 1) {
317       fail_unless (GST_BUFFER_IS_DISCONT (outbuffer),
318           "expected discont for buffer #%d", j);
319     }
320   }
321
322   /* cleanup */
323   gst_caps_unref (caps);
324   cleanup_audioresample (audioresample);
325 }
326
327 GST_START_TEST (test_discont_stream)
328 {
329   /* integral scalings */
330   test_discont_stream_instance (48000, 24000, 5000, 20);
331   test_discont_stream_instance (48000, 12000, 5000, 20);
332   test_discont_stream_instance (12000, 24000, 5000, 20);
333   test_discont_stream_instance (12000, 48000, 5000, 20);
334
335   /* non-integral scalings */
336   test_discont_stream_instance (44100, 8000, 5000, 20);
337   test_discont_stream_instance (8000, 44100, 5000, 20);
338
339   /* wacky scalings */
340   test_discont_stream_instance (12345, 54321, 5000, 20);
341   test_discont_stream_instance (101, 99, 5000, 20);
342 }
343
344 GST_END_TEST;
345
346
347
348 GST_START_TEST (test_reuse)
349 {
350   GstElement *audioresample;
351   GstEvent *newseg;
352   GstBuffer *inbuffer;
353   GstCaps *caps;
354
355   audioresample = setup_audioresample (1, 9343, 48000, 16, FALSE);
356   caps = gst_pad_get_negotiated_caps (mysrcpad);
357   fail_unless (gst_caps_is_fixed (caps));
358
359   fail_unless (gst_element_set_state (audioresample,
360           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
361       "could not set to playing");
362
363   newseg = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME, 0, -1, 0);
364   fail_unless (gst_pad_push_event (mysrcpad, newseg) != FALSE);
365
366   inbuffer = gst_buffer_new_and_alloc (9343 * 4);
367   memset (GST_BUFFER_DATA (inbuffer), 0, GST_BUFFER_SIZE (inbuffer));
368   GST_BUFFER_DURATION (inbuffer) = GST_SECOND;
369   GST_BUFFER_TIMESTAMP (inbuffer) = 0;
370   GST_BUFFER_OFFSET (inbuffer) = 0;
371   gst_buffer_set_caps (inbuffer, caps);
372
373   /* pushing gives away my reference ... */
374   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
375
376   /* ... but it ends up being collected on the global buffer list */
377   fail_unless_equals_int (g_list_length (buffers), 1);
378
379   /* now reset and try again ... */
380   fail_unless (gst_element_set_state (audioresample,
381           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to NULL");
382
383   fail_unless (gst_element_set_state (audioresample,
384           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
385       "could not set to playing");
386
387   newseg = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME, 0, -1, 0);
388   fail_unless (gst_pad_push_event (mysrcpad, newseg) != FALSE);
389
390   inbuffer = gst_buffer_new_and_alloc (9343 * 4);
391   memset (GST_BUFFER_DATA (inbuffer), 0, GST_BUFFER_SIZE (inbuffer));
392   GST_BUFFER_DURATION (inbuffer) = GST_SECOND;
393   GST_BUFFER_TIMESTAMP (inbuffer) = 0;
394   GST_BUFFER_OFFSET (inbuffer) = 0;
395   gst_buffer_set_caps (inbuffer, caps);
396
397   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
398
399   /* ... it also ends up being collected on the global buffer list. If we
400    * now have more than 2 buffers, then audioresample probably didn't clean
401    * up its internal buffer properly and tried to push the remaining samples
402    * when it got the second NEWSEGMENT event */
403   fail_unless_equals_int (g_list_length (buffers), 2);
404
405   cleanup_audioresample (audioresample);
406   gst_caps_unref (caps);
407 }
408
409 GST_END_TEST;
410
411 GST_START_TEST (test_shutdown)
412 {
413   GstElement *pipeline, *src, *cf1, *ar, *cf2, *sink;
414   GstCaps *caps;
415   guint i;
416
417   /* create pipeline, force audioresample to actually resample */
418   pipeline = gst_pipeline_new (NULL);
419
420   src = gst_check_setup_element ("audiotestsrc");
421   cf1 = gst_check_setup_element ("capsfilter");
422   ar = gst_check_setup_element ("audioresample");
423   cf2 = gst_check_setup_element ("capsfilter");
424   g_object_set (cf2, "name", "capsfilter2", NULL);
425   sink = gst_check_setup_element ("fakesink");
426
427   caps =
428       gst_caps_new_simple ("audio/x-raw-int", "rate", G_TYPE_INT, 11025, NULL);
429   g_object_set (cf1, "caps", caps, NULL);
430   gst_caps_unref (caps);
431
432   caps =
433       gst_caps_new_simple ("audio/x-raw-int", "rate", G_TYPE_INT, 48000, NULL);
434   g_object_set (cf2, "caps", caps, NULL);
435   gst_caps_unref (caps);
436
437   /* don't want to sync against the clock, the more throughput the better */
438   g_object_set (src, "is-live", FALSE, NULL);
439   g_object_set (sink, "sync", FALSE, NULL);
440
441   gst_bin_add_many (GST_BIN (pipeline), src, cf1, ar, cf2, sink, NULL);
442   fail_if (!gst_element_link_many (src, cf1, ar, cf2, sink, NULL));
443
444   /* now, wait until pipeline is running and then shut it down again; repeat */
445   for (i = 0; i < 20; ++i) {
446     gst_element_set_state (pipeline, GST_STATE_PAUSED);
447     gst_element_get_state (pipeline, NULL, NULL, -1);
448     gst_element_set_state (pipeline, GST_STATE_PLAYING);
449     g_usleep (100);
450     gst_element_set_state (pipeline, GST_STATE_NULL);
451   }
452
453   gst_object_unref (pipeline);
454 }
455
456 GST_END_TEST;
457
458 static GstFlowReturn
459 live_switch_alloc_only_48000 (GstPad * pad, guint64 offset,
460     guint size, GstCaps * caps, GstBuffer ** buf)
461 {
462   GstStructure *structure;
463   gint rate;
464   gint channels;
465   GstCaps *desired;
466
467   structure = gst_caps_get_structure (caps, 0);
468   fail_unless (gst_structure_get_int (structure, "rate", &rate));
469   fail_unless (gst_structure_get_int (structure, "channels", &channels));
470
471   if (rate < 48000)
472     return GST_FLOW_NOT_NEGOTIATED;
473
474   desired = gst_caps_copy (caps);
475   gst_caps_set_simple (desired, "rate", G_TYPE_INT, 48000, NULL);
476
477   *buf = gst_buffer_new_and_alloc (channels * 48000);
478   gst_buffer_set_caps (*buf, desired);
479   gst_caps_unref (desired);
480
481   return GST_FLOW_OK;
482 }
483
484 static GstCaps *
485 live_switch_get_sink_caps (GstPad * pad)
486 {
487   GstCaps *result;
488
489   result = gst_caps_copy (GST_PAD_CAPS (pad));
490
491   gst_caps_set_simple (result,
492       "rate", GST_TYPE_INT_RANGE, 48000, G_MAXINT, NULL);
493
494   return result;
495 }
496
497 static void
498 live_switch_push (int rate, GstCaps * caps)
499 {
500   GstBuffer *inbuffer;
501   GstCaps *desired;
502   GList *l;
503
504   desired = gst_caps_copy (caps);
505   gst_caps_set_simple (desired, "rate", G_TYPE_INT, rate, NULL);
506   gst_pad_set_caps (mysrcpad, desired);
507
508   fail_unless (gst_pad_alloc_buffer_and_set_caps (mysrcpad,
509           GST_BUFFER_OFFSET_NONE, rate * 4, desired, &inbuffer) == GST_FLOW_OK);
510
511   /* When the basetransform hits the non-configured case it always
512    * returns a buffer with exactly the same caps as we requested so the actual
513    * renegotiation (if needed) will be done in the _chain*/
514   fail_unless (inbuffer != NULL);
515   GST_DEBUG ("desired: %" GST_PTR_FORMAT ".... got: %" GST_PTR_FORMAT,
516       desired, GST_BUFFER_CAPS (inbuffer));
517   fail_unless (gst_caps_is_equal (desired, GST_BUFFER_CAPS (inbuffer)));
518
519   memset (GST_BUFFER_DATA (inbuffer), 0, GST_BUFFER_SIZE (inbuffer));
520   GST_BUFFER_DURATION (inbuffer) = GST_SECOND;
521   GST_BUFFER_TIMESTAMP (inbuffer) = 0;
522   GST_BUFFER_OFFSET (inbuffer) = 0;
523
524   /* pushing gives away my reference ... */
525   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
526
527   /* ... but it ends up being collected on the global buffer list */
528   fail_unless_equals_int (g_list_length (buffers), 1);
529
530   for (l = buffers; l; l = l->next) {
531     GstBuffer *buffer = GST_BUFFER (l->data);
532
533     gst_buffer_unref (buffer);
534   }
535
536   g_list_free (buffers);
537   buffers = NULL;
538
539   gst_caps_unref (desired);
540 }
541
542 GST_START_TEST (test_live_switch)
543 {
544   GstElement *audioresample;
545   GstEvent *newseg;
546   GstCaps *caps;
547
548   audioresample = setup_audioresample (4, 48000, 48000, 16, FALSE);
549
550   /* Let the sinkpad act like something that can only handle things of
551    * rate 48000- and can only allocate buffers for that rate, but if someone
552    * tries to get a buffer with a rate higher then 48000 tries to renegotiate
553    * */
554   gst_pad_set_bufferalloc_function (mysinkpad, live_switch_alloc_only_48000);
555   gst_pad_set_getcaps_function (mysinkpad, live_switch_get_sink_caps);
556
557   gst_pad_use_fixed_caps (mysrcpad);
558
559   caps = gst_pad_get_negotiated_caps (mysrcpad);
560   fail_unless (gst_caps_is_fixed (caps));
561
562   fail_unless (gst_element_set_state (audioresample,
563           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
564       "could not set to playing");
565
566   newseg = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME, 0, -1, 0);
567   fail_unless (gst_pad_push_event (mysrcpad, newseg) != FALSE);
568
569   /* downstream can provide the requested rate, a buffer alloc will be passed
570    * on */
571   live_switch_push (48000, caps);
572
573   /* Downstream can never accept this rate, buffer alloc isn't passed on */
574   live_switch_push (40000, caps);
575
576   /* Downstream can provide the requested rate but will re-negotiate */
577   live_switch_push (50000, caps);
578
579   cleanup_audioresample (audioresample);
580   gst_caps_unref (caps);
581 }
582
583 GST_END_TEST;
584
585 #ifndef GST_DISABLE_PARSE
586
587 static GMainLoop *loop;
588 static gint messages = 0;
589
590 static void
591 element_message_cb (GstBus * bus, GstMessage * message, gpointer user_data)
592 {
593   gchar *s;
594
595   s = gst_structure_to_string (gst_message_get_structure (message));
596   GST_DEBUG ("Received message: %s", s);
597   g_free (s);
598
599   messages++;
600 }
601
602 static void
603 eos_message_cb (GstBus * bus, GstMessage * message, gpointer user_data)
604 {
605   GST_DEBUG ("Received eos");
606   g_main_loop_quit (loop);
607 }
608
609 static void
610 test_pipeline (gint width, gboolean fp, gint inrate, gint outrate, gint quality)
611 {
612   GstElement *pipeline;
613   GstBus *bus;
614   GError *error = NULL;
615   gchar *pipe_str;
616
617   pipe_str =
618       g_strdup_printf
619       ("audiotestsrc num-buffers=10 ! audioconvert ! audio/x-raw-%s,rate=%d,width=%d,channels=2 ! audioresample quality=%d ! audio/x-raw-%s,rate=%d,width=%d ! identity check-imperfect-timestamp=TRUE ! fakesink",
620       (fp) ? "float" : "int", inrate, width, quality, (fp) ? "float" : "int",
621       outrate, width);
622
623   pipeline = gst_parse_launch (pipe_str, &error);
624   fail_unless (pipeline != NULL, "Error parsing pipeline: %s",
625       error ? error->message : "(invalid error)");
626   g_free (pipe_str);
627
628   bus = gst_element_get_bus (pipeline);
629   fail_if (bus == NULL);
630   gst_bus_add_signal_watch (bus);
631   g_signal_connect (bus, "message::element", (GCallback) element_message_cb,
632       NULL);
633   g_signal_connect (bus, "message::eos", (GCallback) eos_message_cb, NULL);
634
635   gst_element_set_state (pipeline, GST_STATE_PLAYING);
636
637   /* run until we receive EOS */
638   loop = g_main_loop_new (NULL, FALSE);
639
640   g_main_loop_run (loop);
641
642   g_main_loop_unref (loop);
643   loop = NULL;
644
645   gst_element_set_state (pipeline, GST_STATE_NULL);
646
647   fail_if (messages > 0, "Received imperfect timestamp messages");
648   gst_object_unref (pipeline);
649 }
650
651 GST_START_TEST (test_pipelines)
652 {
653   gint quality;
654
655   /* Test qualities 0, 5 and 10 */
656   for (quality = 0; quality < 11; quality += 5) {
657     GST_DEBUG ("Checking with quality %d", quality);
658
659     test_pipeline (8, FALSE, 44100, 48000, quality);
660     test_pipeline (8, FALSE, 48000, 44100, quality);
661
662     test_pipeline (16, FALSE, 44100, 48000, quality);
663     test_pipeline (16, FALSE, 48000, 44100, quality);
664
665     test_pipeline (24, FALSE, 44100, 48000, quality);
666     test_pipeline (24, FALSE, 48000, 44100, quality);
667
668     test_pipeline (32, FALSE, 44100, 48000, quality);
669     test_pipeline (32, FALSE, 48000, 44100, quality);
670
671     test_pipeline (32, TRUE, 44100, 48000, quality);
672     test_pipeline (32, TRUE, 48000, 44100, quality);
673
674     test_pipeline (64, TRUE, 44100, 48000, quality);
675     test_pipeline (64, TRUE, 48000, 44100, quality);
676   }
677 }
678
679 GST_END_TEST;
680
681 GST_START_TEST (test_preference_passthrough)
682 {
683   GstStateChangeReturn ret;
684   GstElement *pipeline, *src;
685   GstStructure *s;
686   GstMessage *msg;
687   GstCaps *caps;
688   GstPad *pad;
689   GstBus *bus;
690   GError *error = NULL;
691   gint rate = 0;
692
693   pipeline = gst_parse_launch ("audiotestsrc num-buffers=1 name=src ! "
694       "audioresample ! audio/x-raw-int,channels=1,width=16,depth=16,"
695       "endianness=BYTE_ORDER,signed=true,rate=8000 ! "
696       "fakesink can-activate-pull=false", &error);
697   fail_unless (pipeline != NULL, "Error parsing pipeline: %s",
698       error ? error->message : "(invalid error)");
699
700   ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
701   fail_unless_equals_int (ret, GST_STATE_CHANGE_ASYNC);
702
703   /* run until we receive EOS */
704   bus = gst_element_get_bus (pipeline);
705   fail_if (bus == NULL);
706   msg = gst_bus_timed_pop_filtered (bus, -1, GST_MESSAGE_EOS);
707   gst_message_unref (msg);
708   gst_object_unref (bus);
709
710   src = gst_bin_get_by_name (GST_BIN (pipeline), "src");
711   fail_unless (src != NULL);
712   pad = gst_element_get_static_pad (src, "src");
713   fail_unless (pad != NULL);
714   caps = gst_pad_get_negotiated_caps (pad);
715   GST_LOG ("negotiated audiotestsrc caps: %" GST_PTR_FORMAT, caps);
716   fail_unless (caps != NULL);
717   s = gst_caps_get_structure (caps, 0);
718   fail_unless (gst_structure_get_int (s, "rate", &rate));
719   /* there's no need to resample, audiotestsrc supports any rate, so make
720    * sure audioresample provided upstream with the right caps to negotiate
721    * this correctly */
722   fail_unless_equals_int (rate, 8000);
723   gst_caps_unref (caps);
724   gst_object_unref (pad);
725   gst_object_unref (src);
726
727   gst_element_set_state (pipeline, GST_STATE_NULL);
728   gst_object_unref (pipeline);
729 }
730
731 GST_END_TEST;
732
733 #endif
734
735 static void
736 _message_cb (GstBus * bus, GstMessage * message, gpointer user_data)
737 {
738   GMainLoop *loop = user_data;
739
740   switch (GST_MESSAGE_TYPE (message)) {
741     case GST_MESSAGE_ERROR:
742     case GST_MESSAGE_WARNING:
743       g_assert_not_reached ();
744       break;
745     case GST_MESSAGE_EOS:
746       g_main_loop_quit (loop);
747       break;
748     default:
749       break;
750   }
751 }
752
753 typedef struct
754 {
755   guint64 latency;
756   GstClockTime in_ts;
757
758   GstClockTime next_out_ts;
759   guint64 next_out_off;
760
761   guint64 in_buffer_count, out_buffer_count;
762 } TimestampDriftCtx;
763
764 static void
765 fakesink_handoff_cb (GstElement * object, GstBuffer * buffer, GstPad * pad,
766     gpointer user_data)
767 {
768   TimestampDriftCtx *ctx = user_data;
769
770   ctx->out_buffer_count++;
771   if (ctx->latency == GST_CLOCK_TIME_NONE) {
772     ctx->latency = 1000 - GST_BUFFER_SIZE (buffer) / 8;
773   }
774
775   /* Check if we have a perfectly timestampped stream */
776   if (ctx->next_out_ts != GST_CLOCK_TIME_NONE)
777     fail_unless (ctx->next_out_ts == GST_BUFFER_TIMESTAMP (buffer),
778         "expected timestamp %" GST_TIME_FORMAT " got timestamp %"
779         GST_TIME_FORMAT, GST_TIME_ARGS (ctx->next_out_ts),
780         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));
781
782   /* Check if we have a perfectly offsetted stream */
783   fail_unless (GST_BUFFER_OFFSET_END (buffer) ==
784       GST_BUFFER_OFFSET (buffer) + GST_BUFFER_SIZE (buffer) / 8,
785       "expected offset end %" G_GUINT64_FORMAT " got offset end %"
786       G_GUINT64_FORMAT,
787       GST_BUFFER_OFFSET (buffer) + GST_BUFFER_SIZE (buffer) / 8,
788       GST_BUFFER_OFFSET_END (buffer));
789   if (ctx->next_out_off != GST_BUFFER_OFFSET_NONE) {
790     fail_unless (GST_BUFFER_OFFSET (buffer) == ctx->next_out_off,
791         "expected offset %" G_GUINT64_FORMAT " got offset %" G_GUINT64_FORMAT,
792         ctx->next_out_off, GST_BUFFER_OFFSET (buffer));
793   }
794
795   if (ctx->in_buffer_count != ctx->out_buffer_count) {
796     GST_INFO ("timestamp %" GST_TIME_FORMAT,
797         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));
798   }
799
800   if (ctx->in_ts != GST_CLOCK_TIME_NONE && ctx->in_buffer_count > 1
801       && ctx->in_buffer_count == ctx->out_buffer_count) {
802     fail_unless (GST_BUFFER_TIMESTAMP (buffer) ==
803         ctx->in_ts - gst_util_uint64_scale_round (ctx->latency, GST_SECOND,
804             4096),
805         "expected output timestamp %" GST_TIME_FORMAT " (%" G_GUINT64_FORMAT
806         ") got output timestamp %" GST_TIME_FORMAT " (%" G_GUINT64_FORMAT ")",
807         GST_TIME_ARGS (ctx->in_ts - gst_util_uint64_scale_round (ctx->latency,
808                 GST_SECOND, 4096)),
809         ctx->in_ts - gst_util_uint64_scale_round (ctx->latency, GST_SECOND,
810             4096), GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
811         GST_BUFFER_TIMESTAMP (buffer));
812   }
813
814   ctx->next_out_ts =
815       GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer);
816   ctx->next_out_off = GST_BUFFER_OFFSET_END (buffer);
817 }
818
819 static void
820 identity_handoff_cb (GstElement * object, GstBuffer * buffer,
821     gpointer user_data)
822 {
823   TimestampDriftCtx *ctx = user_data;
824
825   ctx->in_ts = GST_BUFFER_TIMESTAMP (buffer);
826   ctx->in_buffer_count++;
827 }
828
829 GST_START_TEST (test_timestamp_drift)
830 {
831   TimestampDriftCtx ctx =
832       { GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE,
833     GST_BUFFER_OFFSET_NONE, 0, 0
834   };
835   GstElement *pipeline;
836   GstElement *audiotestsrc, *capsfilter1, *identity, *audioresample,
837       *capsfilter2, *fakesink;
838   GstBus *bus;
839   GMainLoop *loop;
840   GstCaps *caps;
841
842   pipeline = gst_pipeline_new ("pipeline");
843   fail_unless (pipeline != NULL);
844
845   audiotestsrc = gst_element_factory_make ("audiotestsrc", "src");
846   fail_unless (audiotestsrc != NULL);
847   g_object_set (G_OBJECT (audiotestsrc), "num-buffers", 10000,
848       "samplesperbuffer", 4000, NULL);
849
850   capsfilter1 = gst_element_factory_make ("capsfilter", "capsfilter1");
851   fail_unless (capsfilter1 != NULL);
852   caps =
853       gst_caps_from_string
854       ("audio/x-raw-float, channels=1, width=64, rate=16384");
855   g_object_set (G_OBJECT (capsfilter1), "caps", caps, NULL);
856   gst_caps_unref (caps);
857
858   identity = gst_element_factory_make ("identity", "identity");
859   fail_unless (identity != NULL);
860   g_object_set (G_OBJECT (identity), "sync", FALSE, "signal-handoffs", TRUE,
861       NULL);
862   g_signal_connect (identity, "handoff", (GCallback) identity_handoff_cb, &ctx);
863
864   audioresample = gst_element_factory_make ("audioresample", "resample");
865   fail_unless (audioresample != NULL);
866   capsfilter2 = gst_element_factory_make ("capsfilter", "capsfilter2");
867   fail_unless (capsfilter2 != NULL);
868   caps =
869       gst_caps_from_string
870       ("audio/x-raw-float, channels=1, width=64, rate=4096");
871   g_object_set (G_OBJECT (capsfilter2), "caps", caps, NULL);
872   gst_caps_unref (caps);
873
874   fakesink = gst_element_factory_make ("fakesink", "sink");
875   fail_unless (fakesink != NULL);
876   g_object_set (G_OBJECT (fakesink), "sync", FALSE, "async", FALSE,
877       "signal-handoffs", TRUE, NULL);
878   g_signal_connect (fakesink, "handoff", (GCallback) fakesink_handoff_cb, &ctx);
879
880
881   gst_bin_add_many (GST_BIN (pipeline), audiotestsrc, capsfilter1, identity,
882       audioresample, capsfilter2, fakesink, NULL);
883   fail_unless (gst_element_link_many (audiotestsrc, capsfilter1, identity,
884           audioresample, capsfilter2, fakesink, NULL));
885
886   loop = g_main_loop_new (NULL, FALSE);
887
888   bus = gst_element_get_bus (pipeline);
889   gst_bus_add_signal_watch (bus);
890   g_signal_connect (bus, "message", (GCallback) _message_cb, loop);
891
892   fail_unless (gst_element_set_state (pipeline,
893           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS);
894   g_main_loop_run (loop);
895
896   fail_unless (gst_element_set_state (pipeline,
897           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS);
898   g_main_loop_unref (loop);
899   gst_object_unref (pipeline);
900
901 } GST_END_TEST;
902
903 static Suite *
904 audioresample_suite (void)
905 {
906   Suite *s = suite_create ("audioresample");
907   TCase *tc_chain = tcase_create ("general");
908
909   suite_add_tcase (s, tc_chain);
910   tcase_add_test (tc_chain, test_perfect_stream);
911   tcase_add_test (tc_chain, test_discont_stream);
912   tcase_add_test (tc_chain, test_reuse);
913   tcase_add_test (tc_chain, test_shutdown);
914   tcase_add_test (tc_chain, test_live_switch);
915   tcase_add_test (tc_chain, test_timestamp_drift);
916
917 #ifndef GST_DISABLE_PARSE
918   tcase_set_timeout (tc_chain, 360);
919   tcase_add_test (tc_chain, test_pipelines);
920   tcase_add_test (tc_chain, test_preference_passthrough);
921 #endif
922
923   return s;
924 }
925
926 GST_CHECK_MAIN (audioresample);