4c89d610cda3d78c9d9528a76a8ce91eb9a98e13
[platform/upstream/gstreamer.git] / tests / check / elements / audiomixer.c
1 /* GStreamer
2  *
3  * unit test for audiomixer
4  *
5  * Copyright (C) 2005 Thomas Vander Stichele <thomas at apestaart dot org>
6  * Copyright (C) 2013 Sebastian Dröge <sebastian@centricular.com>
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
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27
28 #ifdef HAVE_VALGRIND
29 # include <valgrind/valgrind.h>
30 #endif
31
32 #include <gst/check/gstcheck.h>
33 #include <gst/check/gstconsistencychecker.h>
34 #include <gst/audio/audio.h>
35 #include <gst/base/gstbasesrc.h>
36 #include <gst/controller/gstdirectcontrolbinding.h>
37 #include <gst/controller/gstinterpolationcontrolsource.h>
38
39 static GMainLoop *main_loop;
40
41 /* fixtures */
42
43 static void
44 test_setup (void)
45 {
46   main_loop = g_main_loop_new (NULL, FALSE);
47 }
48
49 static void
50 test_teardown (void)
51 {
52   g_main_loop_unref (main_loop);
53   main_loop = NULL;
54 }
55
56
57 /* some test helpers */
58
59 static GstElement *
60 setup_pipeline (GstElement * audiomixer, gint num_srcs, GstElement * capsfilter)
61 {
62   GstElement *pipeline, *src, *sink;
63   gint i;
64
65   pipeline = gst_pipeline_new ("pipeline");
66   if (!audiomixer) {
67     audiomixer = gst_element_factory_make ("audiomixer", "audiomixer");
68   }
69
70   sink = gst_element_factory_make ("fakesink", "sink");
71   gst_bin_add_many (GST_BIN (pipeline), audiomixer, sink, NULL);
72
73   if (capsfilter) {
74     gst_bin_add (GST_BIN (pipeline), capsfilter);
75     gst_element_link_many (audiomixer, capsfilter, sink, NULL);
76   } else {
77     gst_element_link (audiomixer, sink);
78   }
79
80   for (i = 0; i < num_srcs; i++) {
81     src = gst_element_factory_make ("audiotestsrc", NULL);
82     g_object_set (src, "wave", 4, NULL);        /* silence */
83     gst_bin_add (GST_BIN (pipeline), src);
84     gst_element_link (src, audiomixer);
85   }
86   return pipeline;
87 }
88
89 static GstCaps *
90 get_element_sink_pad_caps (GstElement * pipeline, const gchar * element_name)
91 {
92   GstElement *sink;
93   GstCaps *caps;
94   GstPad *pad;
95
96   sink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
97   pad = gst_element_get_static_pad (sink, "sink");
98   caps = gst_pad_get_current_caps (pad);
99   gst_object_unref (pad);
100   gst_object_unref (sink);
101
102   return caps;
103 }
104
105 static void
106 set_state_and_wait (GstElement * pipeline, GstState state)
107 {
108   GstStateChangeReturn state_res;
109
110   /* prepare paused/playing */
111   state_res = gst_element_set_state (pipeline, state);
112   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
113
114   /* wait for preroll */
115   state_res = gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
116   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
117 }
118
119 static gboolean
120 set_playing (GstElement * element)
121 {
122   GstStateChangeReturn state_res;
123
124   state_res = gst_element_set_state (element, GST_STATE_PLAYING);
125   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
126
127   return FALSE;
128 }
129
130 static void
131 play_and_wait (GstElement * pipeline)
132 {
133   GstStateChangeReturn state_res;
134
135   g_idle_add ((GSourceFunc) set_playing, pipeline);
136
137   GST_INFO ("running main loop");
138   g_main_loop_run (main_loop);
139
140   state_res = gst_element_set_state (pipeline, GST_STATE_NULL);
141   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
142 }
143
144 static void
145 message_received (GstBus * bus, GstMessage * message, GstPipeline * bin)
146 {
147   GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
148       GST_MESSAGE_SRC (message), message);
149
150   switch (message->type) {
151     case GST_MESSAGE_EOS:
152       g_main_loop_quit (main_loop);
153       break;
154     case GST_MESSAGE_WARNING:{
155       GError *gerror;
156       gchar *debug;
157
158       gst_message_parse_warning (message, &gerror, &debug);
159       gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
160       g_error_free (gerror);
161       g_free (debug);
162       break;
163     }
164     case GST_MESSAGE_ERROR:{
165       GError *gerror;
166       gchar *debug;
167
168       gst_message_parse_error (message, &gerror, &debug);
169       gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
170       g_error_free (gerror);
171       g_free (debug);
172       g_main_loop_quit (main_loop);
173       break;
174     }
175     default:
176       break;
177   }
178 }
179
180 static GstBuffer *
181 new_buffer (gsize num_bytes, gint data, GstClockTime ts, GstClockTime dur,
182     GstBufferFlags flags)
183 {
184   GstMapInfo map;
185   GstBuffer *buffer = gst_buffer_new_and_alloc (num_bytes);
186
187   gst_buffer_map (buffer, &map, GST_MAP_WRITE);
188   memset (map.data, data, map.size);
189   gst_buffer_unmap (buffer, &map);
190   GST_BUFFER_TIMESTAMP (buffer) = ts;
191   GST_BUFFER_DURATION (buffer) = dur;
192   if (flags)
193     GST_BUFFER_FLAG_SET (buffer, flags);
194   GST_DEBUG ("created buffer %p", buffer);
195   return buffer;
196 }
197
198 /* make sure downstream gets a CAPS event before buffers are sent */
199 GST_START_TEST (test_caps)
200 {
201   GstElement *pipeline;
202   GstCaps *caps;
203
204   /* build pipeline */
205   pipeline = setup_pipeline (NULL, 1, NULL);
206
207   /* prepare playing */
208   set_state_and_wait (pipeline, GST_STATE_PAUSED);
209
210   /* check caps on fakesink */
211   caps = get_element_sink_pad_caps (pipeline, "sink");
212   fail_unless (caps != NULL);
213   gst_caps_unref (caps);
214
215   gst_element_set_state (pipeline, GST_STATE_NULL);
216   gst_object_unref (pipeline);
217 }
218
219 GST_END_TEST;
220
221 /* check that caps set on the property are honoured */
222 GST_START_TEST (test_filter_caps)
223 {
224   GstElement *pipeline, *audiomixer, *capsfilter;
225   GstCaps *filter_caps, *caps;
226
227   filter_caps = gst_caps_new_simple ("audio/x-raw",
228       "format", G_TYPE_STRING, GST_AUDIO_NE (F32),
229       "layout", G_TYPE_STRING, "interleaved",
230       "rate", G_TYPE_INT, 44100, "channels", G_TYPE_INT, 1,
231       "channel-mask", GST_TYPE_BITMASK, (guint64) 0x04, NULL);
232
233   capsfilter = gst_element_factory_make ("capsfilter", NULL);
234
235   /* build pipeline */
236   audiomixer = gst_element_factory_make ("audiomixer", NULL);
237   g_object_set (capsfilter, "caps", filter_caps, NULL);
238   pipeline = setup_pipeline (audiomixer, 1, capsfilter);
239
240   /* prepare playing */
241   set_state_and_wait (pipeline, GST_STATE_PAUSED);
242
243   /* check caps on fakesink */
244   caps = get_element_sink_pad_caps (pipeline, "sink");
245   fail_unless (caps != NULL);
246   GST_INFO_OBJECT (pipeline, "received caps: %" GST_PTR_FORMAT, caps);
247   fail_unless (gst_caps_is_equal_fixed (caps, filter_caps));
248   gst_caps_unref (caps);
249
250   gst_element_set_state (pipeline, GST_STATE_NULL);
251   gst_object_unref (pipeline);
252
253   gst_caps_unref (filter_caps);
254 }
255
256 GST_END_TEST;
257
258 static GstFormat format = GST_FORMAT_UNDEFINED;
259 static gint64 position = -1;
260
261 static void
262 test_event_message_received (GstBus * bus, GstMessage * message,
263     GstPipeline * bin)
264 {
265   GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
266       GST_MESSAGE_SRC (message), message);
267
268   switch (message->type) {
269     case GST_MESSAGE_SEGMENT_DONE:
270       gst_message_parse_segment_done (message, &format, &position);
271       GST_INFO ("received segment_done : %" G_GINT64_FORMAT, position);
272       g_main_loop_quit (main_loop);
273       break;
274     default:
275       g_assert_not_reached ();
276       break;
277   }
278 }
279
280
281 GST_START_TEST (test_event)
282 {
283   GstElement *bin, *src1, *src2, *audiomixer, *sink;
284   GstBus *bus;
285   GstEvent *seek_event;
286   gboolean res;
287   GstPad *srcpad, *sinkpad;
288   GstStreamConsistency *chk_1, *chk_2, *chk_3;
289
290   GST_INFO ("preparing test");
291
292   /* build pipeline */
293   bin = gst_pipeline_new ("pipeline");
294   bus = gst_element_get_bus (bin);
295   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
296
297   src1 = gst_element_factory_make ("audiotestsrc", "src1");
298   g_object_set (src1, "wave", 4, NULL); /* silence */
299   src2 = gst_element_factory_make ("audiotestsrc", "src2");
300   g_object_set (src2, "wave", 4, NULL); /* silence */
301   audiomixer = gst_element_factory_make ("audiomixer", "audiomixer");
302   sink = gst_element_factory_make ("fakesink", "sink");
303   gst_bin_add_many (GST_BIN (bin), src1, src2, audiomixer, sink, NULL);
304
305   res = gst_element_link (src1, audiomixer);
306   fail_unless (res == TRUE, NULL);
307   res = gst_element_link (src2, audiomixer);
308   fail_unless (res == TRUE, NULL);
309   res = gst_element_link (audiomixer, sink);
310   fail_unless (res == TRUE, NULL);
311
312   srcpad = gst_element_get_static_pad (audiomixer, "src");
313   chk_3 = gst_consistency_checker_new (srcpad);
314   gst_object_unref (srcpad);
315
316   /* create consistency checkers for the pads */
317   srcpad = gst_element_get_static_pad (src1, "src");
318   chk_1 = gst_consistency_checker_new (srcpad);
319   sinkpad = gst_pad_get_peer (srcpad);
320   gst_consistency_checker_add_pad (chk_3, sinkpad);
321   gst_object_unref (sinkpad);
322   gst_object_unref (srcpad);
323
324   srcpad = gst_element_get_static_pad (src2, "src");
325   chk_2 = gst_consistency_checker_new (srcpad);
326   sinkpad = gst_pad_get_peer (srcpad);
327   gst_consistency_checker_add_pad (chk_3, sinkpad);
328   gst_object_unref (sinkpad);
329   gst_object_unref (srcpad);
330
331   seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
332       GST_SEEK_FLAG_SEGMENT | GST_SEEK_FLAG_FLUSH,
333       GST_SEEK_TYPE_SET, (GstClockTime) 0,
334       GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);
335
336   format = GST_FORMAT_UNDEFINED;
337   position = -1;
338
339   g_signal_connect (bus, "message::segment-done",
340       (GCallback) test_event_message_received, bin);
341   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
342   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
343   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
344
345   GST_INFO ("starting test");
346
347   /* prepare playing */
348   set_state_and_wait (bin, GST_STATE_PAUSED);
349
350   res = gst_element_send_event (bin, seek_event);
351   fail_unless (res == TRUE, NULL);
352
353   /* run pipeline */
354   play_and_wait (bin);
355
356   ck_assert_int_eq (position, 2 * GST_SECOND);
357
358   /* cleanup */
359   gst_consistency_checker_free (chk_1);
360   gst_consistency_checker_free (chk_2);
361   gst_consistency_checker_free (chk_3);
362   gst_bus_remove_signal_watch (bus);
363   gst_object_unref (bus);
364   gst_object_unref (bin);
365 }
366
367 GST_END_TEST;
368
369 static guint play_count = 0;
370 static GstEvent *play_seek_event = NULL;
371
372 static void
373 test_play_twice_message_received (GstBus * bus, GstMessage * message,
374     GstElement * bin)
375 {
376   gboolean res;
377   GstStateChangeReturn state_res;
378
379   GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
380       GST_MESSAGE_SRC (message), message);
381
382   switch (message->type) {
383     case GST_MESSAGE_SEGMENT_DONE:
384       play_count++;
385       if (play_count == 1) {
386         state_res = gst_element_set_state (bin, GST_STATE_READY);
387         ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
388
389         /* prepare playing again */
390         set_state_and_wait (bin, GST_STATE_PAUSED);
391
392         gst_event_set_seqnum (play_seek_event, gst_util_seqnum_next ());
393         res = gst_element_send_event (bin, gst_event_ref (play_seek_event));
394         fail_unless (res == TRUE, NULL);
395
396         state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
397         ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
398       } else {
399         g_main_loop_quit (main_loop);
400       }
401       break;
402     default:
403       g_assert_not_reached ();
404       break;
405   }
406 }
407
408
409 GST_START_TEST (test_play_twice)
410 {
411   GstElement *bin, *audiomixer;
412   GstBus *bus;
413   gboolean res;
414   GstPad *srcpad;
415   GstStreamConsistency *consist;
416
417   GST_INFO ("preparing test");
418
419   /* build pipeline */
420   audiomixer = gst_element_factory_make ("audiomixer", "audiomixer");
421   bin = setup_pipeline (audiomixer, 2, NULL);
422   bus = gst_element_get_bus (bin);
423   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
424
425   srcpad = gst_element_get_static_pad (audiomixer, "src");
426   consist = gst_consistency_checker_new (srcpad);
427   gst_object_unref (srcpad);
428
429   play_seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
430       GST_SEEK_FLAG_SEGMENT | GST_SEEK_FLAG_FLUSH,
431       GST_SEEK_TYPE_SET, (GstClockTime) 0,
432       GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);
433
434   play_count = 0;
435
436   g_signal_connect (bus, "message::segment-done",
437       (GCallback) test_play_twice_message_received, bin);
438   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
439   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
440   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
441
442   GST_INFO ("starting test");
443
444   /* prepare playing */
445   set_state_and_wait (bin, GST_STATE_PAUSED);
446
447   gst_event_set_seqnum (play_seek_event, gst_util_seqnum_next ());
448   res = gst_element_send_event (bin, gst_event_ref (play_seek_event));
449   fail_unless (res == TRUE, NULL);
450
451   GST_INFO ("seeked");
452
453   /* run pipeline */
454   play_and_wait (bin);
455
456   ck_assert_int_eq (play_count, 2);
457
458   /* cleanup */
459   gst_consistency_checker_free (consist);
460   gst_event_unref (play_seek_event);
461   gst_bus_remove_signal_watch (bus);
462   gst_object_unref (bus);
463   gst_object_unref (bin);
464 }
465
466 GST_END_TEST;
467
468 GST_START_TEST (test_play_twice_then_add_and_play_again)
469 {
470   GstElement *bin, *src, *audiomixer;
471   GstBus *bus;
472   gboolean res;
473   GstStateChangeReturn state_res;
474   gint i;
475   GstPad *srcpad;
476   GstStreamConsistency *consist;
477
478   GST_INFO ("preparing test");
479
480   /* build pipeline */
481   audiomixer = gst_element_factory_make ("audiomixer", "audiomixer");
482   bin = setup_pipeline (audiomixer, 2, NULL);
483   bus = gst_element_get_bus (bin);
484   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
485
486   srcpad = gst_element_get_static_pad (audiomixer, "src");
487   consist = gst_consistency_checker_new (srcpad);
488   gst_object_unref (srcpad);
489
490   play_seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
491       GST_SEEK_FLAG_SEGMENT | GST_SEEK_FLAG_FLUSH,
492       GST_SEEK_TYPE_SET, (GstClockTime) 0,
493       GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);
494
495   g_signal_connect (bus, "message::segment-done",
496       (GCallback) test_play_twice_message_received, bin);
497   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
498   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
499   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
500
501   /* run it twice */
502   for (i = 0; i < 2; i++) {
503     play_count = 0;
504
505     GST_INFO ("starting test-loop %d", i);
506
507     /* prepare playing */
508     set_state_and_wait (bin, GST_STATE_PAUSED);
509
510     gst_event_set_seqnum (play_seek_event, gst_util_seqnum_next ());
511     res = gst_element_send_event (bin, gst_event_ref (play_seek_event));
512     fail_unless (res == TRUE, NULL);
513
514     GST_INFO ("seeked");
515
516     /* run pipeline */
517     play_and_wait (bin);
518
519     ck_assert_int_eq (play_count, 2);
520
521     /* plug another source */
522     if (i == 0) {
523       src = gst_element_factory_make ("audiotestsrc", NULL);
524       g_object_set (src, "wave", 4, NULL);      /* silence */
525       gst_bin_add (GST_BIN (bin), src);
526
527       res = gst_element_link (src, audiomixer);
528       fail_unless (res == TRUE, NULL);
529     }
530
531     gst_consistency_checker_reset (consist);
532   }
533
534   state_res = gst_element_set_state (bin, GST_STATE_NULL);
535   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
536
537   /* cleanup */
538   gst_event_unref (play_seek_event);
539   gst_consistency_checker_free (consist);
540   gst_bus_remove_signal_watch (bus);
541   gst_object_unref (bus);
542   gst_object_unref (bin);
543 }
544
545 GST_END_TEST;
546
547 /* test failing seeks on live-sources */
548 GST_START_TEST (test_live_seeking)
549 {
550   GstElement *bin, *src1 = NULL, *cf, *src2, *audiomixer, *sink;
551   GstCaps *caps;
552   GstBus *bus;
553   gboolean res;
554   GstPad *srcpad;
555   GstPad *sinkpad;
556   gint i;
557   GstStreamConsistency *consist;
558
559   GST_INFO ("preparing test");
560   play_seek_event = NULL;
561
562   /* build pipeline */
563   bin = gst_pipeline_new ("pipeline");
564   bus = gst_element_get_bus (bin);
565   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
566
567   src1 = gst_element_factory_make ("audiotestsrc", "src1");
568   g_object_set (src1, "wave", 4, "is-live", TRUE, NULL);        /* silence */
569
570   audiomixer = gst_element_factory_make ("audiomixer", "audiomixer");
571   cf = gst_element_factory_make ("capsfilter", "capsfilter");
572   sink = gst_element_factory_make ("fakesink", "sink");
573
574   gst_bin_add_many (GST_BIN (bin), src1, cf, audiomixer, sink, NULL);
575   res = gst_element_link_many (src1, cf, audiomixer, sink, NULL);
576   fail_unless (res == TRUE, NULL);
577
578   /* get the caps for the livesrc, we'll reuse this for the non-live source */
579   set_state_and_wait (bin, GST_STATE_PLAYING);
580
581   sinkpad = gst_element_get_static_pad (sink, "sink");
582   fail_unless (sinkpad != NULL);
583   caps = gst_pad_get_current_caps (sinkpad);
584   fail_unless (caps != NULL);
585   gst_object_unref (sinkpad);
586
587   gst_element_set_state (bin, GST_STATE_NULL);
588
589   g_object_set (cf, "caps", caps, NULL);
590
591   src2 = gst_element_factory_make ("audiotestsrc", "src2");
592   g_object_set (src2, "wave", 4, NULL); /* silence */
593   gst_bin_add (GST_BIN (bin), src2);
594
595   res = gst_element_link_filtered (src2, audiomixer, caps);
596   fail_unless (res == TRUE, NULL);
597
598   gst_caps_unref (caps);
599
600   play_seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
601       GST_SEEK_FLAG_FLUSH,
602       GST_SEEK_TYPE_SET, (GstClockTime) 0,
603       GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);
604
605   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
606   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
607   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
608
609   srcpad = gst_element_get_static_pad (audiomixer, "src");
610   consist = gst_consistency_checker_new (srcpad);
611   gst_object_unref (srcpad);
612
613   GST_INFO ("starting test");
614
615   /* run it twice */
616   for (i = 0; i < 2; i++) {
617
618     GST_INFO ("starting test-loop %d", i);
619
620     /* prepare playing */
621     set_state_and_wait (bin, GST_STATE_PAUSED);
622
623     gst_event_set_seqnum (play_seek_event, gst_util_seqnum_next ());
624     res = gst_element_send_event (bin, gst_event_ref (play_seek_event));
625     fail_unless (res == TRUE, NULL);
626
627     GST_INFO ("seeked");
628
629     /* run pipeline */
630     play_and_wait (bin);
631
632     gst_consistency_checker_reset (consist);
633   }
634
635   /* cleanup */
636   GST_INFO ("cleaning up");
637   gst_consistency_checker_free (consist);
638   if (play_seek_event)
639     gst_event_unref (play_seek_event);
640   gst_bus_remove_signal_watch (bus);
641   gst_object_unref (bus);
642   gst_object_unref (bin);
643 }
644
645 GST_END_TEST;
646
647 /* check if adding pads work as expected */
648 GST_START_TEST (test_add_pad)
649 {
650   GstElement *bin, *src1, *src2, *audiomixer, *sink;
651   GstBus *bus;
652   GstPad *srcpad;
653   gboolean res;
654   GstStateChangeReturn state_res;
655
656   GST_INFO ("preparing test");
657
658   /* build pipeline */
659   bin = gst_pipeline_new ("pipeline");
660   bus = gst_element_get_bus (bin);
661   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
662
663   src1 = gst_element_factory_make ("audiotestsrc", "src1");
664   g_object_set (src1, "num-buffers", 4, "wave", /* silence */ 4, NULL);
665   src2 = gst_element_factory_make ("audiotestsrc", "src2");
666   /* one buffer less, we connect with 1 buffer of delay */
667   g_object_set (src2, "num-buffers", 3, "wave", /* silence */ 4, NULL);
668   audiomixer = gst_element_factory_make ("audiomixer", "audiomixer");
669   sink = gst_element_factory_make ("fakesink", "sink");
670   gst_bin_add_many (GST_BIN (bin), src1, audiomixer, sink, NULL);
671
672   res = gst_element_link (src1, audiomixer);
673   fail_unless (res == TRUE, NULL);
674   res = gst_element_link (audiomixer, sink);
675   fail_unless (res == TRUE, NULL);
676
677   srcpad = gst_element_get_static_pad (audiomixer, "src");
678   gst_object_unref (srcpad);
679
680   g_signal_connect (bus, "message::segment-done", (GCallback) message_received,
681       bin);
682   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
683   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
684   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
685
686   GST_INFO ("starting test");
687
688   /* prepare playing */
689   set_state_and_wait (bin, GST_STATE_PAUSED);
690
691   /* add other element */
692   gst_bin_add_many (GST_BIN (bin), src2, NULL);
693
694   /* now link the second element */
695   res = gst_element_link (src2, audiomixer);
696   fail_unless (res == TRUE, NULL);
697
698   /* set to PAUSED as well */
699   state_res = gst_element_set_state (src2, GST_STATE_PAUSED);
700   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
701
702   /* now play all */
703   play_and_wait (bin);
704
705   /* cleanup */
706   gst_bus_remove_signal_watch (bus);
707   gst_object_unref (bus);
708   gst_object_unref (bin);
709 }
710
711 GST_END_TEST;
712
713 /* check if removing pads work as expected */
714 GST_START_TEST (test_remove_pad)
715 {
716   GstElement *bin, *src, *audiomixer, *sink;
717   GstBus *bus;
718   GstPad *pad, *srcpad;
719   gboolean res;
720   GstStateChangeReturn state_res;
721
722   GST_INFO ("preparing test");
723
724   /* build pipeline */
725   bin = gst_pipeline_new ("pipeline");
726   bus = gst_element_get_bus (bin);
727   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
728
729   src = gst_element_factory_make ("audiotestsrc", "src");
730   g_object_set (src, "num-buffers", 4, "wave", 4, NULL);
731   audiomixer = gst_element_factory_make ("audiomixer", "audiomixer");
732   sink = gst_element_factory_make ("fakesink", "sink");
733   gst_bin_add_many (GST_BIN (bin), src, audiomixer, sink, NULL);
734
735   res = gst_element_link (src, audiomixer);
736   fail_unless (res == TRUE, NULL);
737   res = gst_element_link (audiomixer, sink);
738   fail_unless (res == TRUE, NULL);
739
740   /* create an unconnected sinkpad in audiomixer */
741   pad = gst_element_request_pad_simple (audiomixer, "sink_%u");
742   fail_if (pad == NULL, NULL);
743
744   srcpad = gst_element_get_static_pad (audiomixer, "src");
745   gst_object_unref (srcpad);
746
747   g_signal_connect (bus, "message::segment-done", (GCallback) message_received,
748       bin);
749   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
750   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
751   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
752
753   GST_INFO ("starting test");
754
755   /* prepare playing, this will not preroll as audiomixer is waiting
756    * on the unconnected sinkpad. */
757   state_res = gst_element_set_state (bin, GST_STATE_PAUSED);
758   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
759
760   /* wait for completion for one second, will return ASYNC */
761   state_res = gst_element_get_state (GST_ELEMENT (bin), NULL, NULL, GST_SECOND);
762   ck_assert_int_eq (state_res, GST_STATE_CHANGE_ASYNC);
763
764   /* get rid of the pad now, audiomixer should stop waiting on it and
765    * continue the preroll */
766   gst_element_release_request_pad (audiomixer, pad);
767   gst_object_unref (pad);
768
769   /* wait for completion, should work now */
770   state_res =
771       gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
772       GST_CLOCK_TIME_NONE);
773   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
774
775   /* now play all */
776   play_and_wait (bin);
777
778   /* cleanup */
779   gst_bus_remove_signal_watch (bus);
780   gst_object_unref (G_OBJECT (bus));
781   gst_object_unref (G_OBJECT (bin));
782 }
783
784 GST_END_TEST;
785
786
787 static GstBuffer *handoff_buffer = NULL;
788
789 static void
790 handoff_buffer_cb (GstElement * fakesink, GstBuffer * buffer, GstPad * pad,
791     gpointer user_data)
792 {
793   GST_DEBUG ("got buffer -- SIZE: %" G_GSIZE_FORMAT
794       " -- %p PTS is %" GST_TIME_FORMAT " END is %" GST_TIME_FORMAT,
795       gst_buffer_get_size (buffer), buffer,
796       GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
797       GST_TIME_ARGS (GST_BUFFER_PTS (buffer) + GST_BUFFER_DURATION (buffer)));
798
799   gst_buffer_replace (&handoff_buffer, buffer);
800 }
801
802 /* check if clipping works as expected */
803 GST_START_TEST (test_clip)
804 {
805   GstSegment segment;
806   GstElement *bin, *audiomixer, *sink;
807   GstBus *bus;
808   GstPad *sinkpad;
809   gboolean res;
810   GstStateChangeReturn state_res;
811   GstFlowReturn ret;
812   GstEvent *event;
813   GstBuffer *buffer;
814   GstCaps *caps;
815   GstQuery *drain = gst_query_new_drain ();
816
817   GST_INFO ("preparing test");
818
819   /* build pipeline */
820   bin = gst_pipeline_new ("pipeline");
821   bus = gst_element_get_bus (bin);
822   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
823
824   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
825   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
826   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
827
828   /* just an audiomixer and a fakesink */
829   audiomixer = gst_element_factory_make ("audiomixer", "audiomixer");
830   g_object_set (audiomixer, "output-buffer-duration", 50 * GST_MSECOND, NULL);
831   sink = gst_element_factory_make ("fakesink", "sink");
832   g_object_set (sink, "signal-handoffs", TRUE, NULL);
833   g_signal_connect (sink, "handoff", (GCallback) handoff_buffer_cb, NULL);
834   gst_bin_add_many (GST_BIN (bin), audiomixer, sink, NULL);
835
836   res = gst_element_link (audiomixer, sink);
837   fail_unless (res == TRUE, NULL);
838
839   /* set to playing */
840   state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
841   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
842
843   /* create an unconnected sinkpad in audiomixer, should also automatically activate
844    * the pad */
845   sinkpad = gst_element_request_pad_simple (audiomixer, "sink_%u");
846   fail_if (sinkpad == NULL, NULL);
847
848   gst_pad_send_event (sinkpad, gst_event_new_stream_start ("test"));
849
850   caps = gst_caps_new_simple ("audio/x-raw",
851       "format", G_TYPE_STRING, GST_AUDIO_NE (S16),
852       "layout", G_TYPE_STRING, "interleaved",
853       "rate", G_TYPE_INT, 44100, "channels", G_TYPE_INT, 2, NULL);
854
855   gst_pad_set_caps (sinkpad, caps);
856   gst_caps_unref (caps);
857
858   /* send segment to audiomixer */
859   gst_segment_init (&segment, GST_FORMAT_TIME);
860   segment.start = GST_SECOND;
861   segment.stop = 2 * GST_SECOND;
862   segment.time = 0;
863   event = gst_event_new_segment (&segment);
864   gst_pad_send_event (sinkpad, event);
865
866   /* should be clipped and ok */
867   buffer = new_buffer (44100, 0, 0, 250 * GST_MSECOND, 0);
868   GST_DEBUG ("pushing buffer %p END is %" GST_TIME_FORMAT,
869       buffer,
870       GST_TIME_ARGS (GST_BUFFER_PTS (buffer) + GST_BUFFER_DURATION (buffer)));
871   ret = gst_pad_chain (sinkpad, buffer);
872   ck_assert_int_eq (ret, GST_FLOW_OK);
873   /* The aggregation is done in a dedicated thread, so we can't
874    * know when it is actually going to happen, so we use a DRAIN query
875    * to wait for it to complete.
876    */
877   gst_pad_query (sinkpad, drain);
878   fail_unless (handoff_buffer == NULL);
879
880   /* should be partially clipped */
881   buffer = new_buffer (44100, 0, 900 * GST_MSECOND, 250 * GST_MSECOND,
882       GST_BUFFER_FLAG_DISCONT);
883   GST_DEBUG ("pushing buffer %p START %" GST_TIME_FORMAT " -- DURATION is %"
884       GST_TIME_FORMAT, buffer, GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
885       GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
886   ret = gst_pad_chain (sinkpad, buffer);
887   ck_assert_int_eq (ret, GST_FLOW_OK);
888   gst_pad_query (sinkpad, drain);
889
890   fail_unless (handoff_buffer != NULL);
891   ck_assert_int_eq (GST_BUFFER_PTS (handoff_buffer) +
892       GST_BUFFER_DURATION (handoff_buffer), 150 * GST_MSECOND);
893   gst_buffer_replace (&handoff_buffer, NULL);
894
895   /* should not be clipped */
896   buffer = new_buffer (44100, 0, 1150 * GST_MSECOND, 250 * GST_MSECOND, 0);
897   GST_DEBUG ("pushing buffer %p END is %" GST_TIME_FORMAT,
898       buffer,
899       GST_TIME_ARGS (GST_BUFFER_PTS (buffer) + GST_BUFFER_DURATION (buffer)));
900   ret = gst_pad_chain (sinkpad, buffer);
901   ck_assert_int_eq (ret, GST_FLOW_OK);
902   gst_pad_query (sinkpad, drain);
903   fail_unless (handoff_buffer != NULL);
904   ck_assert_int_eq (GST_BUFFER_PTS (handoff_buffer) +
905       GST_BUFFER_DURATION (handoff_buffer), 400 * GST_MSECOND);
906   gst_buffer_replace (&handoff_buffer, NULL);
907   fail_unless (handoff_buffer == NULL);
908
909   /* should be clipped and ok */
910   buffer = new_buffer (44100, 0, 2 * GST_SECOND, 250 * GST_MSECOND,
911       GST_BUFFER_FLAG_DISCONT);
912   GST_DEBUG ("pushing buffer %p PTS is %" GST_TIME_FORMAT
913       " END is %" GST_TIME_FORMAT,
914       buffer,
915       GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
916       GST_TIME_ARGS (GST_BUFFER_PTS (buffer) + GST_BUFFER_DURATION (buffer)));
917   ret = gst_pad_chain (sinkpad, buffer);
918   ck_assert_int_eq (ret, GST_FLOW_OK);
919   gst_pad_query (sinkpad, drain);
920   fail_unless (handoff_buffer == NULL);
921
922   gst_element_release_request_pad (audiomixer, sinkpad);
923   gst_object_unref (sinkpad);
924   gst_element_set_state (bin, GST_STATE_NULL);
925   gst_bus_remove_signal_watch (bus);
926   gst_object_unref (bus);
927   gst_object_unref (bin);
928   gst_query_unref (drain);
929 }
930
931 GST_END_TEST;
932
933 GST_START_TEST (test_duration_is_max)
934 {
935   GstElement *bin, *src[3], *audiomixer, *sink;
936   GstStateChangeReturn state_res;
937   GstFormat format = GST_FORMAT_TIME;
938   gboolean res;
939   gint64 duration;
940
941   GST_INFO ("preparing test");
942
943   /* build pipeline */
944   bin = gst_pipeline_new ("pipeline");
945
946   /* 3 sources, an audiomixer and a fakesink */
947   src[0] = gst_element_factory_make ("audiotestsrc", NULL);
948   src[1] = gst_element_factory_make ("audiotestsrc", NULL);
949   src[2] = gst_element_factory_make ("audiotestsrc", NULL);
950   audiomixer = gst_element_factory_make ("audiomixer", "audiomixer");
951   sink = gst_element_factory_make ("fakesink", "sink");
952   gst_bin_add_many (GST_BIN (bin), src[0], src[1], src[2], audiomixer, sink,
953       NULL);
954
955   gst_element_link (src[0], audiomixer);
956   gst_element_link (src[1], audiomixer);
957   gst_element_link (src[2], audiomixer);
958   gst_element_link (audiomixer, sink);
959
960   /* irks, duration is reset on basesrc */
961   state_res = gst_element_set_state (bin, GST_STATE_PAUSED);
962   fail_unless (state_res != GST_STATE_CHANGE_FAILURE, NULL);
963
964   /* set durations on src */
965   GST_BASE_SRC (src[0])->segment.duration = 1000;
966   GST_BASE_SRC (src[1])->segment.duration = 3000;
967   GST_BASE_SRC (src[2])->segment.duration = 2000;
968
969   /* set to playing */
970   set_state_and_wait (bin, GST_STATE_PLAYING);
971
972   res = gst_element_query_duration (GST_ELEMENT (bin), format, &duration);
973   fail_unless (res, NULL);
974
975   ck_assert_int_eq (duration, 3000);
976
977   gst_element_set_state (bin, GST_STATE_NULL);
978   gst_object_unref (bin);
979 }
980
981 GST_END_TEST;
982
983 GST_START_TEST (test_duration_unknown_overrides)
984 {
985   GstElement *bin, *src[3], *audiomixer, *sink;
986   GstStateChangeReturn state_res;
987   GstFormat format = GST_FORMAT_TIME;
988   gboolean res;
989   gint64 duration;
990
991   GST_INFO ("preparing test");
992
993   /* build pipeline */
994   bin = gst_pipeline_new ("pipeline");
995
996   /* 3 sources, an audiomixer and a fakesink */
997   src[0] = gst_element_factory_make ("audiotestsrc", NULL);
998   src[1] = gst_element_factory_make ("audiotestsrc", NULL);
999   src[2] = gst_element_factory_make ("audiotestsrc", NULL);
1000   audiomixer = gst_element_factory_make ("audiomixer", "audiomixer");
1001   sink = gst_element_factory_make ("fakesink", "sink");
1002   gst_bin_add_many (GST_BIN (bin), src[0], src[1], src[2], audiomixer, sink,
1003       NULL);
1004
1005   gst_element_link (src[0], audiomixer);
1006   gst_element_link (src[1], audiomixer);
1007   gst_element_link (src[2], audiomixer);
1008   gst_element_link (audiomixer, sink);
1009
1010   /* irks, duration is reset on basesrc */
1011   state_res = gst_element_set_state (bin, GST_STATE_PAUSED);
1012   fail_unless (state_res != GST_STATE_CHANGE_FAILURE, NULL);
1013
1014   /* set durations on src */
1015   GST_BASE_SRC (src[0])->segment.duration = GST_CLOCK_TIME_NONE;
1016   GST_BASE_SRC (src[1])->segment.duration = 3000;
1017   GST_BASE_SRC (src[2])->segment.duration = 2000;
1018
1019   /* set to playing */
1020   set_state_and_wait (bin, GST_STATE_PLAYING);
1021
1022   res = gst_element_query_duration (GST_ELEMENT (bin), format, &duration);
1023   fail_unless (res, NULL);
1024
1025   ck_assert_int_eq (duration, GST_CLOCK_TIME_NONE);
1026
1027   gst_element_set_state (bin, GST_STATE_NULL);
1028   gst_object_unref (bin);
1029 }
1030
1031 GST_END_TEST;
1032
1033
1034 static gboolean looped = FALSE;
1035
1036 static void
1037 loop_segment_done (GstBus * bus, GstMessage * message, GstElement * bin)
1038 {
1039   GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
1040       GST_MESSAGE_SRC (message), message);
1041
1042   if (looped) {
1043     g_main_loop_quit (main_loop);
1044   } else {
1045     GstEvent *seek_event;
1046     gboolean res;
1047
1048     seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
1049         GST_SEEK_FLAG_SEGMENT,
1050         GST_SEEK_TYPE_SET, (GstClockTime) 0,
1051         GST_SEEK_TYPE_SET, (GstClockTime) 1 * GST_SECOND);
1052
1053     res = gst_element_send_event (bin, seek_event);
1054     fail_unless (res == TRUE, NULL);
1055     looped = TRUE;
1056   }
1057 }
1058
1059 GST_START_TEST (test_loop)
1060 {
1061   GstElement *bin;
1062   GstBus *bus;
1063   GstEvent *seek_event;
1064   gboolean res;
1065
1066   GST_INFO ("preparing test");
1067
1068   /* build pipeline */
1069   bin = setup_pipeline (NULL, 2, NULL);
1070   bus = gst_element_get_bus (bin);
1071   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
1072
1073   seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
1074       GST_SEEK_FLAG_SEGMENT | GST_SEEK_FLAG_FLUSH,
1075       GST_SEEK_TYPE_SET, (GstClockTime) 0,
1076       GST_SEEK_TYPE_SET, (GstClockTime) 1 * GST_SECOND);
1077
1078   g_signal_connect (bus, "message::segment-done",
1079       (GCallback) loop_segment_done, bin);
1080   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
1081   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
1082   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
1083
1084   GST_INFO ("starting test");
1085
1086   /* prepare playing */
1087   set_state_and_wait (bin, GST_STATE_PAUSED);
1088
1089   res = gst_element_send_event (bin, seek_event);
1090   fail_unless (res == TRUE, NULL);
1091
1092   /* run pipeline */
1093   play_and_wait (bin);
1094
1095   fail_unless (looped);
1096
1097   /* cleanup */
1098   gst_bus_remove_signal_watch (bus);
1099   gst_object_unref (bus);
1100   gst_object_unref (bin);
1101 }
1102
1103 GST_END_TEST;
1104
1105 GST_START_TEST (test_flush_start_flush_stop)
1106 {
1107   GstPadTemplate *sink_template;
1108   GstPad *tmppad, *srcpad1, *sinkpad1, *sinkpad2, *audiomixer_src;
1109   GstElement *pipeline, *src1, *src2, *audiomixer, *sink;
1110
1111   GST_INFO ("preparing test");
1112
1113   /* build pipeline */
1114   pipeline = gst_pipeline_new ("pipeline");
1115   src1 = gst_element_factory_make ("audiotestsrc", "src1");
1116   g_object_set (src1, "wave", 4, NULL); /* silence */
1117   src2 = gst_element_factory_make ("audiotestsrc", "src2");
1118   g_object_set (src2, "wave", 4, NULL); /* silence */
1119   audiomixer = gst_element_factory_make ("audiomixer", "audiomixer");
1120   sink = gst_element_factory_make ("fakesink", "sink");
1121   gst_bin_add_many (GST_BIN (pipeline), src1, src2, audiomixer, sink, NULL);
1122
1123   sink_template =
1124       gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS (audiomixer),
1125       "sink_%u");
1126   fail_unless (GST_IS_PAD_TEMPLATE (sink_template));
1127   sinkpad1 = gst_element_request_pad (audiomixer, sink_template, NULL, NULL);
1128   srcpad1 = gst_element_get_static_pad (src1, "src");
1129   gst_pad_link (srcpad1, sinkpad1);
1130
1131   sinkpad2 = gst_element_request_pad (audiomixer, sink_template, NULL, NULL);
1132   tmppad = gst_element_get_static_pad (src2, "src");
1133   gst_pad_link (tmppad, sinkpad2);
1134   gst_object_unref (tmppad);
1135
1136   gst_element_link (audiomixer, sink);
1137
1138   /* prepare playing */
1139   set_state_and_wait (pipeline, GST_STATE_PLAYING);
1140
1141   audiomixer_src = gst_element_get_static_pad (audiomixer, "src");
1142   fail_if (GST_PAD_IS_FLUSHING (audiomixer_src));
1143   gst_pad_send_event (sinkpad1, gst_event_new_flush_start ());
1144   fail_if (GST_PAD_IS_FLUSHING (audiomixer_src));
1145   fail_unless (GST_PAD_IS_FLUSHING (sinkpad1));
1146   /* Hold the streamlock to make sure the flush stop is not between
1147      the attempted push of a segment event and of the following buffer. */
1148   GST_PAD_STREAM_LOCK (srcpad1);
1149   gst_pad_send_event (sinkpad1, gst_event_new_flush_stop (TRUE));
1150   GST_PAD_STREAM_UNLOCK (srcpad1);
1151   fail_if (GST_PAD_IS_FLUSHING (audiomixer_src));
1152   fail_if (GST_PAD_IS_FLUSHING (sinkpad1));
1153   gst_object_unref (audiomixer_src);
1154
1155   gst_element_release_request_pad (audiomixer, sinkpad1);
1156   gst_object_unref (sinkpad1);
1157   gst_element_release_request_pad (audiomixer, sinkpad2);
1158   gst_object_unref (sinkpad2);
1159   gst_object_unref (srcpad1);
1160
1161   /* cleanup */
1162   gst_element_set_state (pipeline, GST_STATE_NULL);
1163   gst_object_unref (pipeline);
1164 }
1165
1166 GST_END_TEST;
1167
1168 static void
1169 handoff_buffer_collect_cb (GstElement * fakesink, GstBuffer * buffer,
1170     GstPad * pad, gpointer user_data)
1171 {
1172   GList **received_buffers = user_data;
1173
1174   GST_DEBUG ("got buffer %p", buffer);
1175   *received_buffers =
1176       g_list_append (*received_buffers, gst_buffer_ref (buffer));
1177 }
1178
1179 typedef void (*SendBuffersFunction) (GstPad * pad1, GstPad * pad2);
1180 typedef void (*CheckBuffersFunction) (GList * buffers);
1181
1182 static void
1183 run_sync_test (SendBuffersFunction send_buffers,
1184     CheckBuffersFunction check_buffers)
1185 {
1186   GstSegment segment;
1187   GstElement *bin, *audiomixer, *queue1, *queue2, *sink;
1188   GstBus *bus;
1189   GstPad *sinkpad1, *sinkpad2;
1190   GstPad *queue1_sinkpad, *queue2_sinkpad;
1191   GstPad *pad;
1192   gboolean res;
1193   GstStateChangeReturn state_res;
1194   GstEvent *event;
1195   GstCaps *caps;
1196   GList *received_buffers = NULL;
1197
1198   GST_INFO ("preparing test");
1199
1200   /* build pipeline */
1201   bin = gst_pipeline_new ("pipeline");
1202   bus = gst_element_get_bus (bin);
1203   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
1204
1205   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
1206   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
1207   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
1208
1209   /* just an audiomixer and a fakesink */
1210   queue1 = gst_element_factory_make ("queue", "queue1");
1211   queue2 = gst_element_factory_make ("queue", "queue2");
1212   audiomixer = gst_element_factory_make ("audiomixer", "audiomixer");
1213   g_object_set (audiomixer, "output-buffer-duration", 500 * GST_MSECOND, NULL);
1214   sink = gst_element_factory_make ("fakesink", "sink");
1215   g_object_set (sink, "signal-handoffs", TRUE, NULL);
1216   g_signal_connect (sink, "handoff", (GCallback) handoff_buffer_collect_cb,
1217       &received_buffers);
1218   gst_bin_add_many (GST_BIN (bin), queue1, queue2, audiomixer, sink, NULL);
1219
1220   res = gst_element_link (audiomixer, sink);
1221   fail_unless (res == TRUE, NULL);
1222
1223   /* set to paused */
1224   state_res = gst_element_set_state (bin, GST_STATE_PAUSED);
1225   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
1226
1227   /* create an unconnected sinkpad in audiomixer, should also automatically activate
1228    * the pad */
1229   sinkpad1 = gst_element_request_pad_simple (audiomixer, "sink_%u");
1230   fail_if (sinkpad1 == NULL, NULL);
1231
1232   queue1_sinkpad = gst_element_get_static_pad (queue1, "sink");
1233   pad = gst_element_get_static_pad (queue1, "src");
1234   fail_unless (gst_pad_link (pad, sinkpad1) == GST_PAD_LINK_OK);
1235   gst_object_unref (pad);
1236
1237   sinkpad2 = gst_element_request_pad_simple (audiomixer, "sink_%u");
1238   fail_if (sinkpad2 == NULL, NULL);
1239
1240   queue2_sinkpad = gst_element_get_static_pad (queue2, "sink");
1241   pad = gst_element_get_static_pad (queue2, "src");
1242   fail_unless (gst_pad_link (pad, sinkpad2) == GST_PAD_LINK_OK);
1243   gst_object_unref (pad);
1244
1245   gst_pad_send_event (queue1_sinkpad, gst_event_new_stream_start ("test"));
1246   gst_pad_send_event (queue2_sinkpad, gst_event_new_stream_start ("test"));
1247
1248   caps = gst_caps_new_simple ("audio/x-raw",
1249       "format", G_TYPE_STRING, GST_AUDIO_NE (S16),
1250       "layout", G_TYPE_STRING, "interleaved",
1251       "rate", G_TYPE_INT, 1000, "channels", G_TYPE_INT, 1, NULL);
1252
1253   gst_pad_set_caps (queue1_sinkpad, caps);
1254   gst_pad_set_caps (queue2_sinkpad, caps);
1255   gst_caps_unref (caps);
1256
1257   /* send segment to audiomixer */
1258   gst_segment_init (&segment, GST_FORMAT_TIME);
1259   event = gst_event_new_segment (&segment);
1260   gst_pad_send_event (queue1_sinkpad, gst_event_ref (event));
1261   gst_pad_send_event (queue2_sinkpad, event);
1262
1263   /* Push buffers */
1264   send_buffers (queue1_sinkpad, queue2_sinkpad);
1265
1266   /* Set PLAYING */
1267   g_idle_add ((GSourceFunc) set_playing, bin);
1268
1269   /* Collect buffers and messages */
1270   g_main_loop_run (main_loop);
1271
1272   /* Here we get once we got EOS, for errors we failed */
1273
1274   check_buffers (received_buffers);
1275
1276   g_list_free_full (received_buffers, (GDestroyNotify) gst_buffer_unref);
1277
1278   gst_element_release_request_pad (audiomixer, sinkpad1);
1279   gst_object_unref (sinkpad1);
1280   gst_object_unref (queue1_sinkpad);
1281   gst_element_release_request_pad (audiomixer, sinkpad2);
1282   gst_object_unref (sinkpad2);
1283   gst_object_unref (queue2_sinkpad);
1284   gst_element_set_state (bin, GST_STATE_NULL);
1285   gst_bus_remove_signal_watch (bus);
1286   gst_object_unref (bus);
1287   gst_object_unref (bin);
1288 }
1289
1290 static void
1291 send_buffers_sync (GstPad * pad1, GstPad * pad2)
1292 {
1293   GstBuffer *buffer;
1294   GstFlowReturn ret;
1295
1296   buffer = new_buffer (2000, 1, 1 * GST_SECOND, 1 * GST_SECOND, 0);
1297   ret = gst_pad_chain (pad1, buffer);
1298   ck_assert_int_eq (ret, GST_FLOW_OK);
1299
1300   buffer = new_buffer (2000, 1, 2 * GST_SECOND, 1 * GST_SECOND, 0);
1301   ret = gst_pad_chain (pad1, buffer);
1302   ck_assert_int_eq (ret, GST_FLOW_OK);
1303
1304   gst_pad_send_event (pad1, gst_event_new_eos ());
1305
1306   buffer = new_buffer (2000, 2, 2 * GST_SECOND, 1 * GST_SECOND, 0);
1307   ret = gst_pad_chain (pad2, buffer);
1308   ck_assert_int_eq (ret, GST_FLOW_OK);
1309
1310   buffer = new_buffer (2000, 2, 3 * GST_SECOND, 1 * GST_SECOND, 0);
1311   ret = gst_pad_chain (pad2, buffer);
1312   ck_assert_int_eq (ret, GST_FLOW_OK);
1313
1314   gst_pad_send_event (pad2, gst_event_new_eos ());
1315 }
1316
1317 static void
1318 check_buffers_sync (GList * received_buffers)
1319 {
1320   GstBuffer *buffer;
1321   GList *l;
1322   gint i;
1323   GstMapInfo map;
1324
1325   /* Should have 8 * 0.5s buffers */
1326   fail_unless_equals_int (g_list_length (received_buffers), 8);
1327   for (i = 0, l = received_buffers; l; l = l->next, i++) {
1328     buffer = l->data;
1329
1330     gst_buffer_map (buffer, &map, GST_MAP_READ);
1331
1332     if (i == 0 && GST_BUFFER_TIMESTAMP (buffer) == 0) {
1333       fail_unless (map.data[0] == 0);
1334       fail_unless (map.data[map.size - 1] == 0);
1335     } else if (i == 1 && GST_BUFFER_TIMESTAMP (buffer) == 500 * GST_MSECOND) {
1336       fail_unless (map.data[0] == 0);
1337       fail_unless (map.data[map.size - 1] == 0);
1338     } else if (i == 2 && GST_BUFFER_TIMESTAMP (buffer) == 1000 * GST_MSECOND) {
1339       fail_unless (map.data[0] == 1);
1340       fail_unless (map.data[map.size - 1] == 1);
1341     } else if (i == 3 && GST_BUFFER_TIMESTAMP (buffer) == 1500 * GST_MSECOND) {
1342       fail_unless (map.data[0] == 1);
1343       fail_unless (map.data[map.size - 1] == 1);
1344     } else if (i == 4 && GST_BUFFER_TIMESTAMP (buffer) == 2000 * GST_MSECOND) {
1345       fail_unless (map.data[0] == 3);
1346       fail_unless (map.data[map.size - 1] == 3);
1347     } else if (i == 5 && GST_BUFFER_TIMESTAMP (buffer) == 2500 * GST_MSECOND) {
1348       fail_unless (map.data[0] == 3);
1349       fail_unless (map.data[map.size - 1] == 3);
1350     } else if (i == 6 && GST_BUFFER_TIMESTAMP (buffer) == 3000 * GST_MSECOND) {
1351       fail_unless (map.data[0] == 2);
1352       fail_unless (map.data[map.size - 1] == 2);
1353     } else if (i == 7 && GST_BUFFER_TIMESTAMP (buffer) == 3500 * GST_MSECOND) {
1354       fail_unless (map.data[0] == 2);
1355       fail_unless (map.data[map.size - 1] == 2);
1356     } else {
1357       g_assert_not_reached ();
1358     }
1359
1360     gst_buffer_unmap (buffer, &map);
1361
1362   }
1363 }
1364
1365 GST_START_TEST (test_sync)
1366 {
1367   run_sync_test (send_buffers_sync, check_buffers_sync);
1368 }
1369
1370 GST_END_TEST;
1371
1372 static void
1373 send_buffers_sync_discont (GstPad * pad1, GstPad * pad2)
1374 {
1375   GstBuffer *buffer;
1376   GstFlowReturn ret;
1377
1378   buffer = new_buffer (2000, 1, 1 * GST_SECOND, 1 * GST_SECOND, 0);
1379   ret = gst_pad_chain (pad1, buffer);
1380   ck_assert_int_eq (ret, GST_FLOW_OK);
1381
1382   buffer = new_buffer (2000, 1, 3 * GST_SECOND, 1 * GST_SECOND,
1383       GST_BUFFER_FLAG_DISCONT);
1384   ret = gst_pad_chain (pad1, buffer);
1385   ck_assert_int_eq (ret, GST_FLOW_OK);
1386
1387   gst_pad_send_event (pad1, gst_event_new_eos ());
1388
1389   buffer = new_buffer (2000, 2, 2 * GST_SECOND, 1 * GST_SECOND, 0);
1390   ret = gst_pad_chain (pad2, buffer);
1391   ck_assert_int_eq (ret, GST_FLOW_OK);
1392
1393   buffer = new_buffer (2000, 2, 3 * GST_SECOND, 1 * GST_SECOND, 0);
1394   ret = gst_pad_chain (pad2, buffer);
1395   ck_assert_int_eq (ret, GST_FLOW_OK);
1396
1397   gst_pad_send_event (pad2, gst_event_new_eos ());
1398 }
1399
1400 static void
1401 check_buffers_sync_discont (GList * received_buffers)
1402 {
1403   GstBuffer *buffer;
1404   GList *l;
1405   gint i;
1406   GstMapInfo map;
1407
1408   /* Should have 8 * 0.5s buffers */
1409   fail_unless_equals_int (g_list_length (received_buffers), 8);
1410   for (i = 0, l = received_buffers; l; l = l->next, i++) {
1411     buffer = l->data;
1412
1413     gst_buffer_map (buffer, &map, GST_MAP_READ);
1414
1415     if (i == 0 && GST_BUFFER_TIMESTAMP (buffer) == 0) {
1416       fail_unless (map.data[0] == 0);
1417       fail_unless (map.data[map.size - 1] == 0);
1418     } else if (i == 1 && GST_BUFFER_TIMESTAMP (buffer) == 500 * GST_MSECOND) {
1419       fail_unless (map.data[0] == 0);
1420       fail_unless (map.data[map.size - 1] == 0);
1421     } else if (i == 2 && GST_BUFFER_TIMESTAMP (buffer) == 1000 * GST_MSECOND) {
1422       fail_unless (map.data[0] == 1);
1423       fail_unless (map.data[map.size - 1] == 1);
1424     } else if (i == 3 && GST_BUFFER_TIMESTAMP (buffer) == 1500 * GST_MSECOND) {
1425       fail_unless (map.data[0] == 1);
1426       fail_unless (map.data[map.size - 1] == 1);
1427     } else if (i == 4 && GST_BUFFER_TIMESTAMP (buffer) == 2000 * GST_MSECOND) {
1428       fail_unless (map.data[0] == 2);
1429       fail_unless (map.data[map.size - 1] == 2);
1430     } else if (i == 5 && GST_BUFFER_TIMESTAMP (buffer) == 2500 * GST_MSECOND) {
1431       fail_unless (map.data[0] == 2);
1432       fail_unless (map.data[map.size - 1] == 2);
1433     } else if (i == 6 && GST_BUFFER_TIMESTAMP (buffer) == 3000 * GST_MSECOND) {
1434       fail_unless (map.data[0] == 3);
1435       fail_unless (map.data[map.size - 1] == 3);
1436     } else if (i == 7 && GST_BUFFER_TIMESTAMP (buffer) == 3500 * GST_MSECOND) {
1437       fail_unless (map.data[0] == 3);
1438       fail_unless (map.data[map.size - 1] == 3);
1439     } else {
1440       g_assert_not_reached ();
1441     }
1442
1443     gst_buffer_unmap (buffer, &map);
1444
1445   }
1446 }
1447
1448 GST_START_TEST (test_sync_discont)
1449 {
1450   run_sync_test (send_buffers_sync_discont, check_buffers_sync_discont);
1451 }
1452
1453 GST_END_TEST;
1454
1455
1456 static void
1457 send_buffers_sync_discont_backwards (GstPad * pad1, GstPad * pad2)
1458 {
1459   GstBuffer *buffer;
1460   GstFlowReturn ret;
1461
1462   buffer = new_buffer (2300, 1, 1 * GST_SECOND, 1.25 * GST_SECOND, 0);
1463   ret = gst_pad_chain (pad1, buffer);
1464   ck_assert_int_eq (ret, GST_FLOW_OK);
1465
1466   buffer = new_buffer (2000, 1, 2 * GST_SECOND, 1 * GST_SECOND,
1467       GST_BUFFER_FLAG_DISCONT);
1468   ret = gst_pad_chain (pad1, buffer);
1469   ck_assert_int_eq (ret, GST_FLOW_OK);
1470
1471   gst_pad_send_event (pad1, gst_event_new_eos ());
1472
1473   buffer = new_buffer (2000, 1, 2 * GST_SECOND, 1 * GST_SECOND, 0);
1474   ret = gst_pad_chain (pad2, buffer);
1475   ck_assert_int_eq (ret, GST_FLOW_OK);
1476
1477
1478   gst_pad_send_event (pad2, gst_event_new_eos ());
1479 }
1480
1481 static void
1482 check_buffers_sync_discont_backwards (GList * received_buffers)
1483 {
1484   GstBuffer *buffer;
1485   GList *l;
1486   gint i;
1487   GstMapInfo map;
1488
1489   /* Should have 6 * 0.5s buffers */
1490   fail_unless_equals_int (g_list_length (received_buffers), 6);
1491   for (i = 0, l = received_buffers; l; l = l->next, i++) {
1492     buffer = l->data;
1493
1494     gst_buffer_map (buffer, &map, GST_MAP_READ);
1495
1496     if (i == 0 && GST_BUFFER_TIMESTAMP (buffer) == 0) {
1497       fail_unless (map.data[0] == 0);
1498       fail_unless (map.data[map.size - 1] == 0);
1499     } else if (i == 1 && GST_BUFFER_TIMESTAMP (buffer) == 500 * GST_MSECOND) {
1500       fail_unless (map.data[0] == 0);
1501       fail_unless (map.data[map.size - 1] == 0);
1502     } else if (i == 2 && GST_BUFFER_TIMESTAMP (buffer) == 1000 * GST_MSECOND) {
1503       fail_unless (map.data[0] == 1);
1504       fail_unless (map.data[map.size - 1] == 1);
1505     } else if (i == 3 && GST_BUFFER_TIMESTAMP (buffer) == 1500 * GST_MSECOND) {
1506       fail_unless (map.data[0] == 1);
1507       fail_unless (map.data[map.size - 1] == 1);
1508     } else if (i == 4 && GST_BUFFER_TIMESTAMP (buffer) == 2000 * GST_MSECOND) {
1509       fail_unless (map.data[0] == 2);
1510       fail_unless (map.data[map.size - 1] == 2);
1511     } else if (i == 5 && GST_BUFFER_TIMESTAMP (buffer) == 2500 * GST_MSECOND) {
1512       fail_unless (map.data[0] == 2);
1513       fail_unless (map.data[map.size - 1] == 2);
1514     } else {
1515       g_assert_not_reached ();
1516     }
1517
1518     gst_buffer_unmap (buffer, &map);
1519
1520   }
1521 }
1522
1523 GST_START_TEST (test_sync_discont_backwards)
1524 {
1525   run_sync_test (send_buffers_sync_discont_backwards,
1526       check_buffers_sync_discont_backwards);
1527 }
1528
1529 GST_END_TEST;
1530
1531
1532 static void
1533 send_buffers_sync_unaligned (GstPad * pad1, GstPad * pad2)
1534 {
1535   GstBuffer *buffer;
1536   GstFlowReturn ret;
1537
1538   buffer = new_buffer (2000, 1, 750 * GST_MSECOND, 1 * GST_SECOND, 0);
1539   ret = gst_pad_chain (pad1, buffer);
1540   ck_assert_int_eq (ret, GST_FLOW_OK);
1541
1542   buffer = new_buffer (2000, 1, 1750 * GST_MSECOND, 1 * GST_SECOND, 0);
1543   ret = gst_pad_chain (pad1, buffer);
1544   ck_assert_int_eq (ret, GST_FLOW_OK);
1545
1546   gst_pad_send_event (pad1, gst_event_new_eos ());
1547
1548   buffer = new_buffer (2000, 2, 1750 * GST_MSECOND, 1 * GST_SECOND, 0);
1549   ret = gst_pad_chain (pad2, buffer);
1550   ck_assert_int_eq (ret, GST_FLOW_OK);
1551
1552   buffer = new_buffer (2000, 2, 2750 * GST_MSECOND, 1 * GST_SECOND, 0);
1553   ret = gst_pad_chain (pad2, buffer);
1554   ck_assert_int_eq (ret, GST_FLOW_OK);
1555
1556   gst_pad_send_event (pad2, gst_event_new_eos ());
1557 }
1558
1559 static void
1560 check_buffers_sync_unaligned (GList * received_buffers)
1561 {
1562   GstBuffer *buffer;
1563   GList *l;
1564   gint i;
1565   GstMapInfo map;
1566
1567   /* Should have 8 * 0.5s buffers */
1568   fail_unless_equals_int (g_list_length (received_buffers), 8);
1569   for (i = 0, l = received_buffers; l; l = l->next, i++) {
1570     buffer = l->data;
1571
1572     gst_buffer_map (buffer, &map, GST_MAP_READ);
1573
1574     if (i == 0 && GST_BUFFER_TIMESTAMP (buffer) == 0) {
1575       fail_unless (map.data[0] == 0);
1576       fail_unless (map.data[map.size - 1] == 0);
1577     } else if (i == 1 && GST_BUFFER_TIMESTAMP (buffer) == 500 * GST_MSECOND) {
1578       fail_unless (map.data[0] == 0);
1579       fail_unless (map.data[499] == 0);
1580       fail_unless (map.data[500] == 1);
1581       fail_unless (map.data[map.size - 1] == 1);
1582     } else if (i == 2 && GST_BUFFER_TIMESTAMP (buffer) == 1000 * GST_MSECOND) {
1583       fail_unless (map.data[0] == 1);
1584       fail_unless (map.data[map.size - 1] == 1);
1585     } else if (i == 3 && GST_BUFFER_TIMESTAMP (buffer) == 1500 * GST_MSECOND) {
1586       fail_unless (map.data[0] == 1);
1587       fail_unless (map.data[499] == 1);
1588       fail_unless (map.data[500] == 3);
1589       fail_unless (map.data[map.size - 1] == 3);
1590     } else if (i == 4 && GST_BUFFER_TIMESTAMP (buffer) == 2000 * GST_MSECOND) {
1591       fail_unless (map.data[0] == 3);
1592       fail_unless (map.data[499] == 3);
1593       fail_unless (map.data[500] == 3);
1594       fail_unless (map.data[map.size - 1] == 3);
1595     } else if (i == 5 && GST_BUFFER_TIMESTAMP (buffer) == 2500 * GST_MSECOND) {
1596       fail_unless (map.data[0] == 3);
1597       fail_unless (map.data[499] == 3);
1598       fail_unless (map.data[500] == 2);
1599       fail_unless (map.data[map.size - 1] == 2);
1600     } else if (i == 6 && GST_BUFFER_TIMESTAMP (buffer) == 3000 * GST_MSECOND) {
1601       fail_unless (map.data[0] == 2);
1602       fail_unless (map.data[499] == 2);
1603       fail_unless (map.data[500] == 2);
1604       fail_unless (map.data[map.size - 1] == 2);
1605     } else if (i == 7 && GST_BUFFER_TIMESTAMP (buffer) == 3500 * GST_MSECOND) {
1606       fail_unless (map.size == 500);
1607       fail_unless (GST_BUFFER_DURATION (buffer) == 250 * GST_MSECOND);
1608       fail_unless (map.data[0] == 2);
1609       fail_unless (map.data[499] == 2);
1610     } else {
1611       g_assert_not_reached ();
1612     }
1613
1614     gst_buffer_unmap (buffer, &map);
1615
1616   }
1617 }
1618
1619 GST_START_TEST (test_sync_unaligned)
1620 {
1621   run_sync_test (send_buffers_sync_unaligned, check_buffers_sync_unaligned);
1622 }
1623
1624 GST_END_TEST;
1625
1626 GST_START_TEST (test_segment_base_handling)
1627 {
1628   GstElement *pipeline, *sink, *mix, *src1, *src2;
1629   GstPad *srcpad, *sinkpad;
1630   GstClockTime end_time;
1631   GstSample *last_sample = NULL;
1632   GstSample *sample;
1633   GstBuffer *buf;
1634   GstCaps *caps;
1635
1636   caps = gst_caps_new_simple ("audio/x-raw", "rate", G_TYPE_INT, 44100,
1637       "channels", G_TYPE_INT, 2, NULL);
1638
1639   pipeline = gst_pipeline_new ("pipeline");
1640   mix = gst_element_factory_make ("audiomixer", "audiomixer");
1641   sink = gst_element_factory_make ("appsink", "sink");
1642   g_object_set (sink, "caps", caps, "sync", FALSE, NULL);
1643   gst_caps_unref (caps);
1644   /* 50 buffers of 1/10 sec = 5 sec */
1645   src1 = gst_element_factory_make ("audiotestsrc", "src1");
1646   g_object_set (src1, "samplesperbuffer", 4410, "num-buffers", 50, NULL);
1647   src2 = gst_element_factory_make ("audiotestsrc", "src2");
1648   g_object_set (src2, "samplesperbuffer", 4410, "num-buffers", 50, NULL);
1649   gst_bin_add_many (GST_BIN (pipeline), src1, src2, mix, sink, NULL);
1650   fail_unless (gst_element_link (mix, sink));
1651
1652   srcpad = gst_element_get_static_pad (src1, "src");
1653   sinkpad = gst_element_request_pad_simple (mix, "sink_1");
1654   fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK);
1655   gst_object_unref (sinkpad);
1656   gst_object_unref (srcpad);
1657
1658   srcpad = gst_element_get_static_pad (src2, "src");
1659   sinkpad = gst_element_request_pad_simple (mix, "sink_2");
1660   fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK);
1661   /* set a pad offset of another 5 seconds */
1662   gst_pad_set_offset (sinkpad, 5 * GST_SECOND);
1663   gst_object_unref (sinkpad);
1664   gst_object_unref (srcpad);
1665
1666   gst_element_set_state (pipeline, GST_STATE_PLAYING);
1667
1668   do {
1669     g_signal_emit_by_name (sink, "pull-sample", &sample);
1670     if (sample == NULL)
1671       break;
1672     if (last_sample)
1673       gst_sample_unref (last_sample);
1674     last_sample = sample;
1675   } while (TRUE);
1676
1677   buf = gst_sample_get_buffer (last_sample);
1678   end_time = GST_BUFFER_TIMESTAMP (buf) + GST_BUFFER_DURATION (buf);
1679   fail_unless_equals_int64 (end_time, 10 * GST_SECOND);
1680   gst_sample_unref (last_sample);
1681
1682   gst_element_set_state (pipeline, GST_STATE_NULL);
1683   gst_object_unref (pipeline);
1684 }
1685
1686 GST_END_TEST;
1687
1688 static void
1689 set_pad_volume_fade (GstPad * pad, GstClockTime start, gdouble start_value,
1690     GstClockTime end, gdouble end_value)
1691 {
1692   GstControlSource *cs;
1693   GstTimedValueControlSource *tvcs;
1694
1695   cs = gst_interpolation_control_source_new ();
1696   fail_unless (gst_object_add_control_binding (GST_OBJECT_CAST (pad),
1697           gst_direct_control_binding_new_absolute (GST_OBJECT_CAST (pad),
1698               "volume", cs)));
1699
1700   /* set volume interpolation mode */
1701   g_object_set (cs, "mode", GST_INTERPOLATION_MODE_LINEAR, NULL);
1702
1703   tvcs = (GstTimedValueControlSource *) cs;
1704   fail_unless (gst_timed_value_control_source_set (tvcs, start, start_value));
1705   fail_unless (gst_timed_value_control_source_set (tvcs, end, end_value));
1706   gst_object_unref (cs);
1707 }
1708
1709 GST_START_TEST (test_sinkpad_property_controller)
1710 {
1711   GstBus *bus;
1712   GstMessage *msg;
1713   GstElement *pipeline, *sink, *mix, *src1;
1714   GstPad *srcpad, *sinkpad;
1715   GError *error = NULL;
1716   gchar *debug;
1717
1718   pipeline = gst_pipeline_new ("pipeline");
1719   mix = gst_element_factory_make ("audiomixer", "audiomixer");
1720   sink = gst_element_factory_make ("fakesink", "sink");
1721   src1 = gst_element_factory_make ("audiotestsrc", "src1");
1722   g_object_set (src1, "num-buffers", 100, NULL);
1723   gst_bin_add_many (GST_BIN (pipeline), src1, mix, sink, NULL);
1724   fail_unless (gst_element_link (mix, sink));
1725
1726   srcpad = gst_element_get_static_pad (src1, "src");
1727   sinkpad = gst_element_request_pad_simple (mix, "sink_0");
1728   fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK);
1729   set_pad_volume_fade (sinkpad, 0, 0, 1.0, 2.0);
1730   gst_object_unref (sinkpad);
1731   gst_object_unref (srcpad);
1732
1733   gst_element_set_state (pipeline, GST_STATE_PLAYING);
1734
1735   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
1736   msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE,
1737       GST_MESSAGE_EOS | GST_MESSAGE_ERROR);
1738   switch (GST_MESSAGE_TYPE (msg)) {
1739     case GST_MESSAGE_ERROR:
1740       gst_message_parse_error (msg, &error, &debug);
1741       g_printerr ("ERROR from element %s: %s\n",
1742           GST_OBJECT_NAME (msg->src), error->message);
1743       g_printerr ("Debug info: %s\n", debug);
1744       g_error_free (error);
1745       g_free (debug);
1746       break;
1747     case GST_MESSAGE_EOS:
1748       break;
1749     default:
1750       g_assert_not_reached ();
1751   }
1752   gst_message_unref (msg);
1753   g_object_unref (bus);
1754
1755   gst_element_set_state (pipeline, GST_STATE_NULL);
1756   gst_object_unref (pipeline);
1757 }
1758
1759 GST_END_TEST;
1760
1761 static void
1762 change_src_caps (GstElement * fakesink, GstBuffer * buffer, GstPad * pad,
1763     GstElement * capsfilter)
1764 {
1765   GstCaps *caps = gst_caps_new_simple ("audio/x-raw",
1766       "format", G_TYPE_STRING, GST_AUDIO_NE (S32),
1767       "layout", G_TYPE_STRING, "interleaved",
1768       "rate", G_TYPE_INT, 10, "channels", G_TYPE_INT, 1, NULL);
1769
1770   g_object_set (capsfilter, "caps", caps, NULL);
1771   gst_caps_unref (caps);
1772   g_signal_connect (fakesink, "handoff", (GCallback) handoff_buffer_cb, NULL);
1773   g_signal_handlers_disconnect_by_func (fakesink, change_src_caps, capsfilter);
1774 }
1775
1776 /* In this test, we create an input buffer with a duration of 2 seconds,
1777  * and require the audiomixer to output 1 second long buffers.
1778  * The input buffer will thus be mixed twice, and the audiomixer will
1779  * output two buffers.
1780  *
1781  * After audiomixer has output a first buffer, we change its output format
1782  * from S8 to S32. As our sample rate stays the same at 10 fps, and we use
1783  * mono, the first buffer should be 10 bytes long, and the second 40.
1784  *
1785  * The input buffer is made up of 15 0-valued bytes, and 5 1-valued bytes.
1786  * We verify that the second buffer contains 5 0-valued integers, and
1787  * 5 1 << 24 valued integers.
1788  */
1789 GST_START_TEST (test_change_output_caps)
1790 {
1791   GstSegment segment;
1792   GstElement *bin, *audiomixer, *capsfilter, *sink;
1793   GstBus *bus;
1794   GstPad *sinkpad;
1795   gboolean res;
1796   GstStateChangeReturn state_res;
1797   GstFlowReturn ret;
1798   GstEvent *event;
1799   GstBuffer *buffer;
1800   GstCaps *caps;
1801   GstQuery *drain = gst_query_new_drain ();
1802   GstMapInfo inmap;
1803   GstMapInfo outmap;
1804   gsize i;
1805
1806   bin = gst_pipeline_new ("pipeline");
1807   bus = gst_element_get_bus (bin);
1808   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
1809
1810   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
1811   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
1812   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
1813
1814   audiomixer = gst_element_factory_make ("audiomixer", "audiomixer");
1815   g_object_set (audiomixer, "output-buffer-duration", GST_SECOND, NULL);
1816   capsfilter = gst_element_factory_make ("capsfilter", NULL);
1817   sink = gst_element_factory_make ("fakesink", "sink");
1818   g_object_set (sink, "signal-handoffs", TRUE, NULL);
1819   g_signal_connect (sink, "handoff", (GCallback) change_src_caps, capsfilter);
1820   gst_bin_add_many (GST_BIN (bin), audiomixer, capsfilter, sink, NULL);
1821
1822   res = gst_element_link_many (audiomixer, capsfilter, sink, NULL);
1823   fail_unless (res == TRUE, NULL);
1824
1825   state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
1826   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
1827
1828   sinkpad = gst_element_request_pad_simple (audiomixer, "sink_%u");
1829   fail_if (sinkpad == NULL, NULL);
1830
1831   gst_pad_send_event (sinkpad, gst_event_new_stream_start ("test"));
1832
1833   caps = gst_caps_new_simple ("audio/x-raw",
1834       "format", G_TYPE_STRING, "S8",
1835       "layout", G_TYPE_STRING, "interleaved",
1836       "rate", G_TYPE_INT, 10, "channels", G_TYPE_INT, 1, NULL);
1837
1838   gst_pad_set_caps (sinkpad, caps);
1839   g_object_set (capsfilter, "caps", caps, NULL);
1840   gst_caps_unref (caps);
1841
1842   gst_segment_init (&segment, GST_FORMAT_TIME);
1843   segment.start = 0;
1844   segment.stop = 2 * GST_SECOND;
1845   segment.time = 0;
1846   event = gst_event_new_segment (&segment);
1847   gst_pad_send_event (sinkpad, event);
1848
1849   gst_buffer_replace (&handoff_buffer, NULL);
1850
1851   buffer = new_buffer (20, 0, 0, 2 * GST_SECOND, 0);
1852   gst_buffer_map (buffer, &inmap, GST_MAP_WRITE);
1853   memset (inmap.data + 15, 1, 5);
1854   gst_buffer_unmap (buffer, &inmap);
1855   ret = gst_pad_chain (sinkpad, buffer);
1856   ck_assert_int_eq (ret, GST_FLOW_OK);
1857   gst_pad_query (sinkpad, drain);
1858   fail_unless (handoff_buffer != NULL);
1859   fail_unless_equals_int (gst_buffer_get_size (handoff_buffer), 40);
1860
1861   gst_buffer_map (handoff_buffer, &outmap, GST_MAP_READ);
1862   for (i = 0; i < 10; i++) {
1863     guint32 sample;
1864
1865 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1866     sample = GUINT32_FROM_LE (((guint32 *) outmap.data)[i]);
1867 #else
1868     sample = GUINT32_FROM_BE (((guint32 *) outmap.data)[i]);
1869 #endif
1870
1871     if (i < 5) {
1872       fail_unless_equals_int (sample, 0);
1873     } else {
1874       fail_unless_equals_int (sample, 1 << 24);
1875     }
1876   }
1877   gst_buffer_unmap (handoff_buffer, &outmap);
1878   gst_clear_buffer (&handoff_buffer);
1879
1880   gst_element_release_request_pad (audiomixer, sinkpad);
1881   gst_object_unref (sinkpad);
1882   gst_element_set_state (bin, GST_STATE_NULL);
1883   gst_bus_remove_signal_watch (bus);
1884   gst_object_unref (bus);
1885   gst_object_unref (bin);
1886   gst_query_unref (drain);
1887 }
1888
1889 GST_END_TEST;
1890
1891 /* In this test, we create two input buffers with a duration of 1 second,
1892  * and require the audiomixer to output 1.5 second long buffers.
1893  *
1894  * After we have input two buffers, we change the output format
1895  * from S8 to S32, then push a last buffer.
1896  *
1897  * This makes audioaggregator convert its "half-mixed" current_buffer,
1898  * we can then ensure that the second output buffer is as expected.
1899  */
1900 GST_START_TEST (test_change_output_caps_mid_output_buffer)
1901 {
1902   GstSegment segment;
1903   GstElement *bin, *audiomixer, *capsfilter, *sink;
1904   GstBus *bus;
1905   GstPad *sinkpad;
1906   gboolean res;
1907   GstStateChangeReturn state_res;
1908   GstFlowReturn ret;
1909   GstEvent *event;
1910   GstBuffer *buffer;
1911   GstCaps *caps;
1912   GstQuery *drain;
1913   GstMapInfo inmap;
1914   GstMapInfo outmap;
1915   guint i;
1916
1917   bin = gst_pipeline_new ("pipeline");
1918   bus = gst_element_get_bus (bin);
1919   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
1920
1921   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
1922   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
1923   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
1924
1925   audiomixer = gst_element_factory_make ("audiomixer", "audiomixer");
1926   g_object_set (audiomixer, "output-buffer-duration", 1500 * GST_MSECOND, NULL);
1927   capsfilter = gst_element_factory_make ("capsfilter", NULL);
1928   sink = gst_element_factory_make ("fakesink", "sink");
1929   gst_bin_add_many (GST_BIN (bin), audiomixer, capsfilter, sink, NULL);
1930
1931   res = gst_element_link_many (audiomixer, capsfilter, sink, NULL);
1932   fail_unless (res == TRUE, NULL);
1933
1934   state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
1935   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
1936
1937   sinkpad = gst_element_request_pad_simple (audiomixer, "sink_%u");
1938   fail_if (sinkpad == NULL, NULL);
1939
1940   gst_pad_send_event (sinkpad, gst_event_new_stream_start ("test"));
1941
1942   caps = gst_caps_new_simple ("audio/x-raw",
1943       "format", G_TYPE_STRING, "S8",
1944       "layout", G_TYPE_STRING, "interleaved",
1945       "rate", G_TYPE_INT, 10, "channels", G_TYPE_INT, 1, NULL);
1946
1947   gst_pad_set_caps (sinkpad, caps);
1948   g_object_set (capsfilter, "caps", caps, NULL);
1949   gst_caps_unref (caps);
1950
1951   gst_segment_init (&segment, GST_FORMAT_TIME);
1952   segment.start = 0;
1953   segment.stop = 3 * GST_SECOND;
1954   segment.time = 0;
1955   event = gst_event_new_segment (&segment);
1956   gst_pad_send_event (sinkpad, event);
1957
1958   buffer = new_buffer (10, 0, 0, 1 * GST_SECOND, 0);
1959   ret = gst_pad_chain (sinkpad, buffer);
1960   ck_assert_int_eq (ret, GST_FLOW_OK);
1961
1962   buffer = new_buffer (10, 0, 1 * GST_SECOND, 1 * GST_SECOND, 0);
1963   gst_buffer_map (buffer, &inmap, GST_MAP_WRITE);
1964   memset (inmap.data, 1, 10);
1965   gst_buffer_unmap (buffer, &inmap);
1966   ret = gst_pad_chain (sinkpad, buffer);
1967   ck_assert_int_eq (ret, GST_FLOW_OK);
1968
1969   drain = gst_query_new_drain ();
1970   gst_pad_query (sinkpad, drain);
1971   gst_query_unref (drain);
1972
1973   caps = gst_caps_new_simple ("audio/x-raw",
1974       "format", G_TYPE_STRING, GST_AUDIO_NE (S32),
1975       "layout", G_TYPE_STRING, "interleaved",
1976       "rate", G_TYPE_INT, 10, "channels", G_TYPE_INT, 1, NULL);
1977   g_object_set (capsfilter, "caps", caps, NULL);
1978   gst_caps_unref (caps);
1979
1980   gst_buffer_replace (&handoff_buffer, NULL);
1981   g_object_set (sink, "signal-handoffs", TRUE, NULL);
1982   g_signal_connect (sink, "handoff", (GCallback) handoff_buffer_cb, NULL);
1983
1984   buffer = new_buffer (10, 0, 2 * GST_SECOND, 1 * GST_SECOND, 0);
1985   gst_buffer_map (buffer, &inmap, GST_MAP_WRITE);
1986   memset (inmap.data, 0, 10);
1987   gst_buffer_unmap (buffer, &inmap);
1988   ret = gst_pad_chain (sinkpad, buffer);
1989   ck_assert_int_eq (ret, GST_FLOW_OK);
1990
1991   drain = gst_query_new_drain ();
1992   gst_pad_query (sinkpad, drain);
1993   gst_query_unref (drain);
1994
1995   fail_unless (handoff_buffer);
1996   fail_unless_equals_int (gst_buffer_get_size (handoff_buffer), 60);
1997
1998   gst_buffer_map (handoff_buffer, &outmap, GST_MAP_READ);
1999   for (i = 0; i < 15; i++) {
2000     guint32 sample;
2001
2002 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
2003     sample = GUINT32_FROM_LE (((guint32 *) outmap.data)[i]);
2004 #else
2005     sample = GUINT32_FROM_BE (((guint32 *) outmap.data)[i]);
2006 #endif
2007
2008     if (i < 5) {
2009       fail_unless_equals_int (sample, 1 << 24);
2010     } else {
2011       fail_unless_equals_int (sample, 0);
2012     }
2013   }
2014
2015   gst_buffer_unmap (handoff_buffer, &outmap);
2016   gst_clear_buffer (&handoff_buffer);
2017
2018   gst_element_release_request_pad (audiomixer, sinkpad);
2019   gst_object_unref (sinkpad);
2020   gst_element_set_state (bin, GST_STATE_NULL);
2021   gst_bus_remove_signal_watch (bus);
2022   gst_object_unref (bus);
2023   gst_object_unref (bin);
2024 }
2025
2026 GST_END_TEST;
2027 static Suite *
2028 audiomixer_suite (void)
2029 {
2030   Suite *s = suite_create ("audiomixer");
2031   TCase *tc_chain = tcase_create ("general");
2032
2033   suite_add_tcase (s, tc_chain);
2034   tcase_add_test (tc_chain, test_caps);
2035   tcase_add_test (tc_chain, test_filter_caps);
2036   tcase_add_test (tc_chain, test_event);
2037   tcase_add_test (tc_chain, test_play_twice);
2038   tcase_add_test (tc_chain, test_play_twice_then_add_and_play_again);
2039   tcase_add_test (tc_chain, test_live_seeking);
2040   tcase_add_test (tc_chain, test_add_pad);
2041   tcase_add_test (tc_chain, test_remove_pad);
2042   tcase_add_test (tc_chain, test_clip);
2043   tcase_add_test (tc_chain, test_duration_is_max);
2044   tcase_add_test (tc_chain, test_duration_unknown_overrides);
2045   tcase_add_test (tc_chain, test_loop);
2046   tcase_add_test (tc_chain, test_flush_start_flush_stop);
2047   tcase_add_test (tc_chain, test_sync);
2048   tcase_add_test (tc_chain, test_sync_discont);
2049   tcase_add_test (tc_chain, test_sync_discont_backwards);
2050   tcase_add_test (tc_chain, test_sync_unaligned);
2051   tcase_add_test (tc_chain, test_segment_base_handling);
2052   tcase_add_test (tc_chain, test_sinkpad_property_controller);
2053   tcase_add_checked_fixture (tc_chain, test_setup, test_teardown);
2054   tcase_add_test (tc_chain, test_change_output_caps);
2055   tcase_add_test (tc_chain, test_change_output_caps_mid_output_buffer);
2056
2057   /* Use a longer timeout */
2058 #ifdef HAVE_VALGRIND
2059   if (RUNNING_ON_VALGRIND) {
2060     tcase_set_timeout (tc_chain, 5 * 60);
2061   } else
2062 #endif
2063   {
2064     /* this is shorter than the default 60 seconds?! (tpm) */
2065     /* tcase_set_timeout (tc_chain, 6); */
2066   }
2067
2068   return s;
2069 }
2070
2071 GST_CHECK_MAIN (audiomixer);