3a229d0c87bf9b8720955b055417426406aa7052
[platform/upstream/gst-plugins-base.git] / tests / check / elements / adder.c
1 /* GStreamer
2  *
3  * unit test for adder
4  *
5  * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 #ifdef HAVE_VALGRIND
28 # include <valgrind/valgrind.h>
29 #endif
30
31 #include <gst/check/gstcheck.h>
32 #include <gst/check/gstconsistencychecker.h>
33 #include <gst/base/gstbasesrc.h>
34 #include <gst/audio/audio.h>
35
36 static GMainLoop *main_loop;
37
38 /* fixtures */
39
40 static void
41 test_setup (void)
42 {
43   main_loop = g_main_loop_new (NULL, FALSE);
44 }
45
46 static void
47 test_teardown (void)
48 {
49   g_main_loop_unref (main_loop);
50   main_loop = NULL;
51 }
52
53
54 /* some test helpers */
55
56 static GstElement *
57 setup_pipeline (GstElement * adder, gint num_srcs)
58 {
59   GstElement *pipeline, *src, *sink;
60   gint i;
61
62   pipeline = gst_pipeline_new ("pipeline");
63   if (!adder) {
64     adder = gst_element_factory_make ("adder", "adder");
65   }
66
67   sink = gst_element_factory_make ("fakesink", "sink");
68   gst_bin_add_many (GST_BIN (pipeline), adder, sink, NULL);
69   gst_element_link (adder, sink);
70
71   for (i = 0; i < num_srcs; i++) {
72     src = gst_element_factory_make ("audiotestsrc", NULL);
73     g_object_set (src, "wave", 4, NULL);        /* silence */
74     gst_bin_add (GST_BIN (pipeline), src);
75     gst_element_link (src, adder);
76   }
77   return pipeline;
78 }
79
80 static GstCaps *
81 get_element_sink_pad_caps (GstElement * pipeline, const gchar * element_name)
82 {
83   GstElement *sink;
84   GstCaps *caps;
85   GstPad *pad;
86
87   sink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
88   pad = gst_element_get_static_pad (sink, "sink");
89   caps = gst_pad_get_current_caps (pad);
90   gst_object_unref (pad);
91   gst_object_unref (sink);
92
93   return caps;
94 }
95
96 static void
97 set_state_and_wait (GstElement * pipeline, GstState state)
98 {
99   GstStateChangeReturn state_res;
100
101   /* prepare paused/playing */
102   state_res = gst_element_set_state (pipeline, state);
103   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
104
105   /* wait for preroll */
106   state_res = gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
107   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
108 }
109
110 static void
111 play_and_wait (GstElement * pipeline)
112 {
113   GstStateChangeReturn state_res;
114
115   state_res = gst_element_set_state (pipeline, GST_STATE_PLAYING);
116   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
117
118   GST_INFO ("running main loop");
119   g_main_loop_run (main_loop);
120
121   state_res = gst_element_set_state (pipeline, GST_STATE_NULL);
122   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
123 }
124
125 static void
126 message_received (GstBus * bus, GstMessage * message, GstPipeline * bin)
127 {
128   GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
129       GST_MESSAGE_SRC (message), message);
130
131   switch (message->type) {
132     case GST_MESSAGE_EOS:
133       g_main_loop_quit (main_loop);
134       break;
135     case GST_MESSAGE_WARNING:{
136       GError *gerror;
137       gchar *debug;
138
139       gst_message_parse_warning (message, &gerror, &debug);
140       gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
141       g_error_free (gerror);
142       g_free (debug);
143       break;
144     }
145     case GST_MESSAGE_ERROR:{
146       GError *gerror;
147       gchar *debug;
148
149       gst_message_parse_error (message, &gerror, &debug);
150       gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
151       g_error_free (gerror);
152       g_free (debug);
153       g_main_loop_quit (main_loop);
154       break;
155     }
156     default:
157       break;
158   }
159 }
160
161 static GstBuffer *
162 new_buffer (gsize num_bytes, GstClockTime ts, GstClockTime dur)
163 {
164   GstBuffer *buffer = gst_buffer_new_and_alloc (num_bytes);
165
166   GST_BUFFER_TIMESTAMP (buffer) = ts;
167   GST_BUFFER_DURATION (buffer) = dur;
168   GST_DEBUG ("created buffer %p", buffer);
169   return buffer;
170 }
171
172 /* make sure downstream gets a CAPS event before buffers are sent */
173 GST_START_TEST (test_caps)
174 {
175   GstElement *pipeline;
176   GstCaps *caps;
177
178   /* build pipeline */
179   pipeline = setup_pipeline (NULL, 1);
180
181   /* prepare playing */
182   set_state_and_wait (pipeline, GST_STATE_PAUSED);
183
184   /* check caps on fakesink */
185   caps = get_element_sink_pad_caps (pipeline, "sink");
186   fail_unless (caps != NULL);
187   gst_caps_unref (caps);
188
189   gst_element_set_state (pipeline, GST_STATE_NULL);
190   gst_object_unref (pipeline);
191 }
192
193 GST_END_TEST;
194
195 /* check that caps set on the property are honoured */
196 GST_START_TEST (test_filter_caps)
197 {
198   GstElement *pipeline, *adder;
199   GstCaps *filter_caps, *caps;
200
201   filter_caps = gst_caps_new_simple ("audio/x-raw",
202       "format", G_TYPE_STRING, GST_AUDIO_NE (F32),
203       "layout", G_TYPE_STRING, "interleaved",
204       "rate", G_TYPE_INT, 44100, "channels", G_TYPE_INT, 1, NULL);
205
206   /* build pipeline */
207   adder = gst_element_factory_make ("adder", "adder");
208   g_object_set (adder, "caps", filter_caps, NULL);
209   pipeline = setup_pipeline (adder, 1);
210
211   /* prepare playing */
212   set_state_and_wait (pipeline, GST_STATE_PAUSED);
213
214   /* check caps on fakesink */
215   caps = get_element_sink_pad_caps (pipeline, "sink");
216   fail_unless (caps != NULL);
217   GST_INFO_OBJECT (pipeline, "received caps: %" GST_PTR_FORMAT, caps);
218   fail_unless (gst_caps_is_equal_fixed (caps, filter_caps));
219   gst_caps_unref (caps);
220
221   gst_element_set_state (pipeline, GST_STATE_NULL);
222   gst_object_unref (pipeline);
223
224   gst_caps_unref (filter_caps);
225 }
226
227 GST_END_TEST;
228
229 static GstFormat format = GST_FORMAT_UNDEFINED;
230 static gint64 position = -1;
231
232 static void
233 test_event_message_received (GstBus * bus, GstMessage * message,
234     GstPipeline * bin)
235 {
236   GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
237       GST_MESSAGE_SRC (message), message);
238
239   switch (message->type) {
240     case GST_MESSAGE_SEGMENT_DONE:
241       gst_message_parse_segment_done (message, &format, &position);
242       GST_INFO ("received segment_done : %" G_GINT64_FORMAT, position);
243       g_main_loop_quit (main_loop);
244       break;
245     default:
246       g_assert_not_reached ();
247       break;
248   }
249 }
250
251 GST_START_TEST (test_event)
252 {
253   GstElement *bin, *src1, *src2, *adder, *sink;
254   GstBus *bus;
255   GstEvent *seek_event;
256   gboolean res;
257   GstPad *srcpad, *sinkpad;
258   GstStreamConsistency *chk_1, *chk_2, *chk_3;
259
260   GST_INFO ("preparing test");
261
262   /* build pipeline */
263   bin = gst_pipeline_new ("pipeline");
264   bus = gst_element_get_bus (bin);
265   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
266
267   src1 = gst_element_factory_make ("audiotestsrc", "src1");
268   g_object_set (src1, "wave", 4, NULL); /* silence */
269   src2 = gst_element_factory_make ("audiotestsrc", "src2");
270   g_object_set (src2, "wave", 4, NULL); /* silence */
271   adder = gst_element_factory_make ("adder", "adder");
272   sink = gst_element_factory_make ("fakesink", "sink");
273   gst_bin_add_many (GST_BIN (bin), src1, src2, adder, sink, NULL);
274
275   res = gst_element_link (src1, adder);
276   fail_unless (res == TRUE, NULL);
277   res = gst_element_link (src2, adder);
278   fail_unless (res == TRUE, NULL);
279   res = gst_element_link (adder, sink);
280   fail_unless (res == TRUE, NULL);
281
282   srcpad = gst_element_get_static_pad (adder, "src");
283   chk_3 = gst_consistency_checker_new (srcpad);
284   gst_object_unref (srcpad);
285
286   /* create consistency checkers for the pads */
287   srcpad = gst_element_get_static_pad (src1, "src");
288   chk_1 = gst_consistency_checker_new (srcpad);
289   sinkpad = gst_pad_get_peer (srcpad);
290   gst_consistency_checker_add_pad (chk_3, sinkpad);
291   gst_object_unref (sinkpad);
292   gst_object_unref (srcpad);
293
294   srcpad = gst_element_get_static_pad (src2, "src");
295   chk_2 = gst_consistency_checker_new (srcpad);
296   sinkpad = gst_pad_get_peer (srcpad);
297   gst_consistency_checker_add_pad (chk_3, sinkpad);
298   gst_object_unref (sinkpad);
299   gst_object_unref (srcpad);
300
301   seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
302       GST_SEEK_FLAG_SEGMENT | GST_SEEK_FLAG_FLUSH,
303       GST_SEEK_TYPE_SET, (GstClockTime) 0,
304       GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);
305
306   format = GST_FORMAT_UNDEFINED;
307   position = -1;
308
309   g_signal_connect (bus, "message::segment-done",
310       (GCallback) test_event_message_received, bin);
311   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
312   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
313   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
314
315   GST_INFO ("starting test");
316
317   /* prepare playing */
318   set_state_and_wait (bin, GST_STATE_PAUSED);
319
320   res = gst_element_send_event (bin, seek_event);
321   fail_unless (res == TRUE, NULL);
322
323   /* run pipeline */
324   play_and_wait (bin);
325
326   ck_assert_int_eq (position, 2 * GST_SECOND);
327
328   /* cleanup */
329   gst_consistency_checker_free (chk_1);
330   gst_consistency_checker_free (chk_2);
331   gst_consistency_checker_free (chk_3);
332   gst_bus_remove_signal_watch (bus);
333   gst_object_unref (bus);
334   gst_object_unref (bin);
335 }
336
337 GST_END_TEST;
338
339 static guint play_count = 0;
340 static GstEvent *play_seek_event = NULL;
341
342 static void
343 test_play_twice_message_received (GstBus * bus, GstMessage * message,
344     GstElement * bin)
345 {
346   gboolean res;
347   GstStateChangeReturn state_res;
348
349   GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
350       GST_MESSAGE_SRC (message), message);
351
352   switch (message->type) {
353     case GST_MESSAGE_SEGMENT_DONE:
354       play_count++;
355       if (play_count == 1) {
356         state_res = gst_element_set_state (bin, GST_STATE_READY);
357         ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
358
359         /* prepare playing again */
360         set_state_and_wait (bin, GST_STATE_PAUSED);
361
362         res = gst_element_send_event (bin, gst_event_ref (play_seek_event));
363         fail_unless (res == TRUE, NULL);
364
365         state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
366         ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
367       } else {
368         g_main_loop_quit (main_loop);
369       }
370       break;
371     default:
372       g_assert_not_reached ();
373       break;
374   }
375 }
376
377
378 GST_START_TEST (test_play_twice)
379 {
380   GstElement *bin, *adder;
381   GstBus *bus;
382   gboolean res;
383   GstPad *srcpad;
384   GstStreamConsistency *consist;
385
386   GST_INFO ("preparing test");
387
388   /* build pipeline */
389   adder = gst_element_factory_make ("adder", "adder");
390   bin = setup_pipeline (adder, 2);
391   bus = gst_element_get_bus (bin);
392   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
393
394   srcpad = gst_element_get_static_pad (adder, "src");
395   consist = gst_consistency_checker_new (srcpad);
396   gst_object_unref (srcpad);
397
398   play_seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
399       GST_SEEK_FLAG_SEGMENT | GST_SEEK_FLAG_FLUSH,
400       GST_SEEK_TYPE_SET, (GstClockTime) 0,
401       GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);
402
403   play_count = 0;
404
405   g_signal_connect (bus, "message::segment-done",
406       (GCallback) test_play_twice_message_received, bin);
407   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
408   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
409   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
410
411   GST_INFO ("starting test");
412
413   /* prepare playing */
414   set_state_and_wait (bin, GST_STATE_PAUSED);
415
416   res = gst_element_send_event (bin, gst_event_ref (play_seek_event));
417   fail_unless (res == TRUE, NULL);
418
419   GST_INFO ("seeked");
420
421   /* run pipeline */
422   play_and_wait (bin);
423
424   ck_assert_int_eq (play_count, 2);
425
426   /* cleanup */
427   gst_consistency_checker_free (consist);
428   gst_event_unref (play_seek_event);
429   gst_bus_remove_signal_watch (bus);
430   gst_object_unref (bus);
431   gst_object_unref (bin);
432 }
433
434 GST_END_TEST;
435
436 GST_START_TEST (test_play_twice_then_add_and_play_again)
437 {
438   GstElement *bin, *src, *adder;
439   GstBus *bus;
440   gboolean res;
441   GstStateChangeReturn state_res;
442   gint i;
443   GstPad *srcpad;
444   GstStreamConsistency *consist;
445
446   GST_INFO ("preparing test");
447
448   /* build pipeline */
449   adder = gst_element_factory_make ("adder", "adder");
450   bin = setup_pipeline (adder, 2);
451   bus = gst_element_get_bus (bin);
452   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
453
454   srcpad = gst_element_get_static_pad (adder, "src");
455   consist = gst_consistency_checker_new (srcpad);
456   gst_object_unref (srcpad);
457
458   play_seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
459       GST_SEEK_FLAG_SEGMENT | GST_SEEK_FLAG_FLUSH,
460       GST_SEEK_TYPE_SET, (GstClockTime) 0,
461       GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);
462
463   g_signal_connect (bus, "message::segment-done",
464       (GCallback) test_play_twice_message_received, bin);
465   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
466   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
467   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
468
469   /* run it twice */
470   for (i = 0; i < 2; i++) {
471     play_count = 0;
472
473     GST_INFO ("starting test-loop %d", i);
474
475     /* prepare playing */
476     set_state_and_wait (bin, GST_STATE_PAUSED);
477
478     res = gst_element_send_event (bin, gst_event_ref (play_seek_event));
479     fail_unless (res == TRUE, NULL);
480
481     GST_INFO ("seeked");
482
483     /* run pipeline */
484     play_and_wait (bin);
485
486     ck_assert_int_eq (play_count, 2);
487
488     /* plug another source */
489     if (i == 0) {
490       src = gst_element_factory_make ("audiotestsrc", NULL);
491       g_object_set (src, "wave", 4, NULL);      /* silence */
492       gst_bin_add (GST_BIN (bin), src);
493
494       res = gst_element_link (src, adder);
495       fail_unless (res == TRUE, NULL);
496     }
497
498     gst_consistency_checker_reset (consist);
499   }
500
501   state_res = gst_element_set_state (bin, GST_STATE_NULL);
502   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
503
504   /* cleanup */
505   gst_event_unref (play_seek_event);
506   gst_consistency_checker_free (consist);
507   gst_bus_remove_signal_watch (bus);
508   gst_object_unref (bus);
509   gst_object_unref (bin);
510 }
511
512 GST_END_TEST;
513
514
515 static GstElement *
516 test_live_seeking_try_audiosrc (const gchar * factory_name)
517 {
518   GstElement *src;
519   GstStateChangeReturn state_res;
520
521   if (!(src = gst_element_factory_make (factory_name, NULL))) {
522     GST_INFO ("can't make '%s', skipping", factory_name);
523     return NULL;
524   }
525
526   /* Test that the audio source can get to ready, else skip */
527   state_res = gst_element_set_state (src, GST_STATE_READY);
528   gst_element_set_state (src, GST_STATE_NULL);
529
530   if (state_res == GST_STATE_CHANGE_FAILURE) {
531     GST_INFO_OBJECT (src, "can't go to ready, skipping");
532     gst_object_unref (src);
533     return NULL;
534   }
535
536   return src;
537 }
538
539 /* test failing seeks on live-sources */
540 GST_START_TEST (test_live_seeking)
541 {
542   GstElement *bin, *src1 = NULL, *src2, *ac1, *ac2, *adder, *sink;
543   GstBus *bus;
544   gboolean res;
545   GstPad *srcpad;
546   gint i;
547   GstStreamConsistency *consist;
548   /* don't use autoaudiosrc, as then we can't set anything here */
549   const gchar *audio_src_factories[] = {
550     "alsasrc",
551     "pulseaudiosrc"
552   };
553
554   GST_INFO ("preparing test");
555   play_seek_event = NULL;
556
557   /* build pipeline */
558   bin = gst_pipeline_new ("pipeline");
559   bus = gst_element_get_bus (bin);
560   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
561
562   for (i = 0; (i < G_N_ELEMENTS (audio_src_factories) && src1 == NULL); i++) {
563     src1 = test_live_seeking_try_audiosrc (audio_src_factories[i]);
564   }
565   if (!src1) {
566     /* normal audiosources behave differently than audiotestsrc */
567     src1 = gst_element_factory_make ("audiotestsrc", "src1");
568     g_object_set (src1, "wave", 4, "is-live", TRUE, NULL);      /* silence */
569   } else {
570     /* live sources ignore seeks, force eos after 2 sec (4 buffers half second
571      * each)
572      */
573     g_object_set (src1, "num-buffers", 4, "blocksize", 44100, NULL);
574   }
575
576   ac1 = gst_element_factory_make ("audioconvert", "ac1");
577   src2 = gst_element_factory_make ("audiotestsrc", "src2");
578   g_object_set (src2, "wave", 4, NULL); /* silence */
579   ac2 = gst_element_factory_make ("audioconvert", "ac2");
580   adder = gst_element_factory_make ("adder", "adder");
581   sink = gst_element_factory_make ("fakesink", "sink");
582   gst_bin_add_many (GST_BIN (bin), src1, ac1, src2, ac2, adder, sink, NULL);
583
584   res = gst_element_link_many (src1, ac1, adder, NULL);
585   fail_unless (res == TRUE, NULL);
586   res = gst_element_link_many (src2, ac2, adder, NULL);
587   fail_unless (res == TRUE, NULL);
588   res = gst_element_link (adder, sink);
589   fail_unless (res == TRUE, NULL);
590
591   play_seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
592       GST_SEEK_FLAG_FLUSH,
593       GST_SEEK_TYPE_SET, (GstClockTime) 0,
594       GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);
595
596   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
597   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
598   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
599
600   srcpad = gst_element_get_static_pad (adder, "src");
601   consist = gst_consistency_checker_new (srcpad);
602   gst_object_unref (srcpad);
603
604   GST_INFO ("starting test");
605
606   /* run it twice */
607   for (i = 0; i < 2; i++) {
608
609     GST_INFO ("starting test-loop %d", i);
610
611     /* prepare playing */
612     set_state_and_wait (bin, GST_STATE_PAUSED);
613
614     res = gst_element_send_event (bin, gst_event_ref (play_seek_event));
615     fail_unless (res == TRUE, NULL);
616
617     GST_INFO ("seeked");
618
619     /* run pipeline */
620     play_and_wait (bin);
621
622     gst_consistency_checker_reset (consist);
623   }
624
625   /* cleanup */
626   GST_INFO ("cleaning up");
627   gst_consistency_checker_free (consist);
628   if (play_seek_event)
629     gst_event_unref (play_seek_event);
630   gst_bus_remove_signal_watch (bus);
631   gst_object_unref (bus);
632   gst_object_unref (bin);
633 }
634
635 GST_END_TEST;
636
637 /* check if adding pads work as expected */
638 GST_START_TEST (test_add_pad)
639 {
640   GstElement *bin, *src1, *src2, *adder, *sink;
641   GstBus *bus;
642   GstPad *srcpad;
643   gboolean res;
644   GstStateChangeReturn state_res;
645
646   GST_INFO ("preparing test");
647
648   /* build pipeline */
649   bin = gst_pipeline_new ("pipeline");
650   bus = gst_element_get_bus (bin);
651   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
652
653   src1 = gst_element_factory_make ("audiotestsrc", "src1");
654   g_object_set (src1, "num-buffers", 4, "wave", /* silence */ 4, NULL);
655   src2 = gst_element_factory_make ("audiotestsrc", "src2");
656   /* one buffer less, we connect with 1 buffer of delay */
657   g_object_set (src2, "num-buffers", 3, "wave", /* silence */ 4, NULL);
658   adder = gst_element_factory_make ("adder", "adder");
659   sink = gst_element_factory_make ("fakesink", "sink");
660   gst_bin_add_many (GST_BIN (bin), src1, adder, sink, NULL);
661
662   res = gst_element_link (src1, adder);
663   fail_unless (res == TRUE, NULL);
664   res = gst_element_link (adder, sink);
665   fail_unless (res == TRUE, NULL);
666
667   srcpad = gst_element_get_static_pad (adder, "src");
668   gst_object_unref (srcpad);
669
670   g_signal_connect (bus, "message::segment-done", (GCallback) message_received,
671       bin);
672   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
673   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
674   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
675
676   GST_INFO ("starting test");
677
678   /* prepare playing */
679   set_state_and_wait (bin, GST_STATE_PAUSED);
680
681   /* add other element */
682   gst_bin_add_many (GST_BIN (bin), src2, NULL);
683
684   /* now link the second element */
685   res = gst_element_link (src2, adder);
686   fail_unless (res == TRUE, NULL);
687
688   /* set to PAUSED as well */
689   state_res = gst_element_set_state (src2, GST_STATE_PAUSED);
690   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
691
692   /* now play all */
693   play_and_wait (bin);
694
695   /* cleanup */
696   gst_bus_remove_signal_watch (bus);
697   gst_object_unref (bus);
698   gst_object_unref (bin);
699 }
700
701 GST_END_TEST;
702
703 /* check if removing pads work as expected */
704 GST_START_TEST (test_remove_pad)
705 {
706   GstElement *bin, *src, *adder, *sink;
707   GstBus *bus;
708   GstPad *pad, *srcpad;
709   gboolean res;
710   GstStateChangeReturn state_res;
711
712   GST_INFO ("preparing test");
713
714   /* build pipeline */
715   bin = gst_pipeline_new ("pipeline");
716   bus = gst_element_get_bus (bin);
717   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
718
719   src = gst_element_factory_make ("audiotestsrc", "src");
720   g_object_set (src, "num-buffers", 4, "wave", 4, NULL);
721   adder = gst_element_factory_make ("adder", "adder");
722   sink = gst_element_factory_make ("fakesink", "sink");
723   gst_bin_add_many (GST_BIN (bin), src, adder, sink, NULL);
724
725   res = gst_element_link (src, adder);
726   fail_unless (res == TRUE, NULL);
727   res = gst_element_link (adder, sink);
728   fail_unless (res == TRUE, NULL);
729
730   /* create an unconnected sinkpad in adder */
731   pad = gst_element_get_request_pad (adder, "sink_%u");
732   fail_if (pad == NULL, NULL);
733
734   srcpad = gst_element_get_static_pad (adder, "src");
735   gst_object_unref (srcpad);
736
737   g_signal_connect (bus, "message::segment-done", (GCallback) message_received,
738       bin);
739   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
740   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
741   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
742
743   GST_INFO ("starting test");
744
745   /* prepare playing, this will not preroll as adder is waiting
746    * on the unconnected sinkpad. */
747   state_res = gst_element_set_state (bin, GST_STATE_PAUSED);
748   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
749
750   /* wait for completion for one second, will return ASYNC */
751   state_res = gst_element_get_state (GST_ELEMENT (bin), NULL, NULL, GST_SECOND);
752   ck_assert_int_eq (state_res, GST_STATE_CHANGE_ASYNC);
753
754   /* get rid of the pad now, adder should stop waiting on it and
755    * continue the preroll */
756   gst_element_release_request_pad (adder, pad);
757   gst_object_unref (pad);
758
759   /* wait for completion, should work now */
760   state_res =
761       gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
762       GST_CLOCK_TIME_NONE);
763   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
764
765   /* now play all */
766   play_and_wait (bin);
767
768   /* cleanup */
769   gst_bus_remove_signal_watch (bus);
770   gst_object_unref (G_OBJECT (bus));
771   gst_object_unref (G_OBJECT (bin));
772 }
773
774 GST_END_TEST;
775
776
777 static GstBuffer *handoff_buffer = NULL;
778 static void
779 handoff_buffer_cb (GstElement * fakesink, GstBuffer * buffer, GstPad * pad,
780     gpointer user_data)
781 {
782   GST_DEBUG ("got buffer %p", buffer);
783   gst_buffer_replace (&handoff_buffer, buffer);
784 }
785
786 /* check if clipping works as expected */
787 GST_START_TEST (test_clip)
788 {
789   GstSegment segment;
790   GstElement *bin, *adder, *sink;
791   GstBus *bus;
792   GstPad *sinkpad;
793   gboolean res;
794   GstStateChangeReturn state_res;
795   GstFlowReturn ret;
796   GstEvent *event;
797   GstBuffer *buffer;
798   GstCaps *caps;
799
800   GST_INFO ("preparing test");
801
802   /* build pipeline */
803   bin = gst_pipeline_new ("pipeline");
804   bus = gst_element_get_bus (bin);
805   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
806
807   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
808   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
809   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
810
811   /* just an adder and a fakesink */
812   adder = gst_element_factory_make ("adder", "adder");
813   sink = gst_element_factory_make ("fakesink", "sink");
814   g_object_set (sink, "signal-handoffs", TRUE, NULL);
815   g_signal_connect (sink, "handoff", (GCallback) handoff_buffer_cb, NULL);
816   gst_bin_add_many (GST_BIN (bin), adder, sink, NULL);
817
818   res = gst_element_link (adder, sink);
819   fail_unless (res == TRUE, NULL);
820
821   /* set to playing */
822   state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
823   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
824
825   /* create an unconnected sinkpad in adder, should also automatically activate
826    * the pad */
827   sinkpad = gst_element_get_request_pad (adder, "sink_%u");
828   fail_if (sinkpad == NULL, NULL);
829
830   gst_pad_send_event (sinkpad, gst_event_new_stream_start ("test"));
831
832   caps = gst_caps_new_simple ("audio/x-raw",
833 #if G_BYTE_ORDER == G_BIG_ENDIAN
834       "format", G_TYPE_STRING, "S16BE",
835 #else
836       "format", G_TYPE_STRING, "S16LE",
837 #endif
838       "layout", G_TYPE_STRING, "interleaved",
839       "rate", G_TYPE_INT, 44100, "channels", G_TYPE_INT, 2, NULL);
840
841   gst_pad_set_caps (sinkpad, caps);
842   gst_caps_unref (caps);
843
844   /* send segment to adder */
845   gst_segment_init (&segment, GST_FORMAT_TIME);
846   segment.start = GST_SECOND;
847   segment.stop = 2 * GST_SECOND;
848   segment.time = 0;
849   event = gst_event_new_segment (&segment);
850   gst_pad_send_event (sinkpad, event);
851
852   /* should be clipped and ok */
853   buffer = new_buffer (44100, 0, 250 * GST_MSECOND);
854   ret = gst_pad_chain (sinkpad, buffer);
855   ck_assert_int_eq (ret, GST_FLOW_OK);
856   fail_unless (handoff_buffer == NULL);
857
858   /* should be partially clipped */
859   buffer = new_buffer (44100, 900 * GST_MSECOND, 250 * GST_MSECOND);
860   ret = gst_pad_chain (sinkpad, buffer);
861   ck_assert_int_eq (ret, GST_FLOW_OK);
862   fail_unless (handoff_buffer != NULL);
863   gst_buffer_replace (&handoff_buffer, NULL);
864
865   /* should not be clipped */
866   buffer = new_buffer (44100, 1 * GST_SECOND, 250 * GST_MSECOND);
867   ret = gst_pad_chain (sinkpad, buffer);
868   ck_assert_int_eq (ret, GST_FLOW_OK);
869   fail_unless (handoff_buffer != NULL);
870   gst_buffer_replace (&handoff_buffer, NULL);
871
872   /* should be clipped and ok */
873   buffer = new_buffer (44100, 2 * GST_SECOND, 250 * GST_MSECOND);
874   ret = gst_pad_chain (sinkpad, buffer);
875   ck_assert_int_eq (ret, GST_FLOW_OK);
876   fail_unless (handoff_buffer == NULL);
877
878   gst_element_release_request_pad (adder, sinkpad);
879   gst_object_unref (sinkpad);
880   gst_element_set_state (bin, GST_STATE_NULL);
881   gst_bus_remove_signal_watch (bus);
882   gst_object_unref (bus);
883   gst_object_unref (bin);
884 }
885
886 GST_END_TEST;
887
888 GST_START_TEST (test_duration_is_max)
889 {
890   GstElement *bin, *src[3], *adder, *sink;
891   GstStateChangeReturn state_res;
892   GstFormat format = GST_FORMAT_TIME;
893   gboolean res;
894   gint64 duration;
895
896   GST_INFO ("preparing test");
897
898   /* build pipeline */
899   bin = gst_pipeline_new ("pipeline");
900
901   /* 3 sources, an adder and a fakesink */
902   src[0] = gst_element_factory_make ("audiotestsrc", NULL);
903   src[1] = gst_element_factory_make ("audiotestsrc", NULL);
904   src[2] = gst_element_factory_make ("audiotestsrc", NULL);
905   adder = gst_element_factory_make ("adder", "adder");
906   sink = gst_element_factory_make ("fakesink", "sink");
907   gst_bin_add_many (GST_BIN (bin), src[0], src[1], src[2], adder, sink, NULL);
908
909   gst_element_link (src[0], adder);
910   gst_element_link (src[1], adder);
911   gst_element_link (src[2], adder);
912   gst_element_link (adder, sink);
913
914   /* irks, duration is reset on basesrc */
915   state_res = gst_element_set_state (bin, GST_STATE_PAUSED);
916   fail_unless (state_res != GST_STATE_CHANGE_FAILURE, NULL);
917
918   /* set durations on src */
919   GST_BASE_SRC (src[0])->segment.duration = 1000;
920   GST_BASE_SRC (src[1])->segment.duration = 3000;
921   GST_BASE_SRC (src[2])->segment.duration = 2000;
922
923   /* set to playing */
924   set_state_and_wait (bin, GST_STATE_PLAYING);
925
926   res = gst_element_query_duration (GST_ELEMENT (bin), format, &duration);
927   fail_unless (res, NULL);
928
929   ck_assert_int_eq (duration, 3000);
930
931   gst_element_set_state (bin, GST_STATE_NULL);
932   gst_object_unref (bin);
933 }
934
935 GST_END_TEST;
936
937 GST_START_TEST (test_duration_unknown_overrides)
938 {
939   GstElement *bin, *src[3], *adder, *sink;
940   GstStateChangeReturn state_res;
941   GstFormat format = GST_FORMAT_TIME;
942   gboolean res;
943   gint64 duration;
944
945   GST_INFO ("preparing test");
946
947   /* build pipeline */
948   bin = gst_pipeline_new ("pipeline");
949
950   /* 3 sources, an adder and a fakesink */
951   src[0] = gst_element_factory_make ("audiotestsrc", NULL);
952   src[1] = gst_element_factory_make ("audiotestsrc", NULL);
953   src[2] = gst_element_factory_make ("audiotestsrc", NULL);
954   adder = gst_element_factory_make ("adder", "adder");
955   sink = gst_element_factory_make ("fakesink", "sink");
956   gst_bin_add_many (GST_BIN (bin), src[0], src[1], src[2], adder, sink, NULL);
957
958   gst_element_link (src[0], adder);
959   gst_element_link (src[1], adder);
960   gst_element_link (src[2], adder);
961   gst_element_link (adder, sink);
962
963   /* irks, duration is reset on basesrc */
964   state_res = gst_element_set_state (bin, GST_STATE_PAUSED);
965   fail_unless (state_res != GST_STATE_CHANGE_FAILURE, NULL);
966
967   /* set durations on src */
968   GST_BASE_SRC (src[0])->segment.duration = GST_CLOCK_TIME_NONE;
969   GST_BASE_SRC (src[1])->segment.duration = 3000;
970   GST_BASE_SRC (src[2])->segment.duration = 2000;
971
972   /* set to playing */
973   set_state_and_wait (bin, GST_STATE_PLAYING);
974
975   res = gst_element_query_duration (GST_ELEMENT (bin), format, &duration);
976   fail_unless (res, NULL);
977
978   ck_assert_int_eq (duration, GST_CLOCK_TIME_NONE);
979
980   gst_element_set_state (bin, GST_STATE_NULL);
981   gst_object_unref (bin);
982 }
983
984 GST_END_TEST;
985
986
987 static gboolean looped = FALSE;
988
989 static void
990 loop_segment_done (GstBus * bus, GstMessage * message, GstElement * bin)
991 {
992   GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
993       GST_MESSAGE_SRC (message), message);
994
995   if (looped) {
996     g_main_loop_quit (main_loop);
997   } else {
998     GstEvent *seek_event;
999     gboolean res;
1000
1001     seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
1002         GST_SEEK_FLAG_SEGMENT,
1003         GST_SEEK_TYPE_SET, (GstClockTime) 0,
1004         GST_SEEK_TYPE_SET, (GstClockTime) 1 * GST_SECOND);
1005
1006     res = gst_element_send_event (bin, seek_event);
1007     fail_unless (res == TRUE, NULL);
1008     looped = TRUE;
1009   }
1010 }
1011
1012 GST_START_TEST (test_loop)
1013 {
1014   GstElement *bin;
1015   GstBus *bus;
1016   GstEvent *seek_event;
1017   gboolean res;
1018
1019   GST_INFO ("preparing test");
1020
1021   /* build pipeline */
1022   bin = setup_pipeline (NULL, 2);
1023   bus = gst_element_get_bus (bin);
1024   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
1025
1026   seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
1027       GST_SEEK_FLAG_SEGMENT | GST_SEEK_FLAG_FLUSH,
1028       GST_SEEK_TYPE_SET, (GstClockTime) 0,
1029       GST_SEEK_TYPE_SET, (GstClockTime) 1 * GST_SECOND);
1030
1031   g_signal_connect (bus, "message::segment-done",
1032       (GCallback) loop_segment_done, bin);
1033   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
1034   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
1035   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
1036
1037   GST_INFO ("starting test");
1038
1039   /* prepare playing */
1040   set_state_and_wait (bin, GST_STATE_PAUSED);
1041
1042   res = gst_element_send_event (bin, seek_event);
1043   fail_unless (res == TRUE, NULL);
1044
1045   /* run pipeline */
1046   play_and_wait (bin);
1047
1048   fail_unless (looped);
1049
1050   /* cleanup */
1051   gst_bus_remove_signal_watch (bus);
1052   gst_object_unref (bus);
1053   gst_object_unref (bin);
1054 }
1055
1056 GST_END_TEST;
1057
1058 #if 0
1059 GST_START_TEST (test_flush_start_flush_stop)
1060 {
1061   GstPadTemplate *sink_template;
1062   GstPad *tmppad, *sinkpad1, *sinkpad2, *adder_src;
1063   GstElement *pipeline, *src1, *src2, *adder, *sink;
1064
1065   GST_INFO ("preparing test");
1066
1067   /* build pipeline */
1068   pipeline = gst_pipeline_new ("pipeline");
1069   src1 = gst_element_factory_make ("audiotestsrc", "src1");
1070   g_object_set (src1, "wave", 4, NULL); /* silence */
1071   src2 = gst_element_factory_make ("audiotestsrc", "src2");
1072   g_object_set (src2, "wave", 4, NULL); /* silence */
1073   adder = gst_element_factory_make ("adder", "adder");
1074   sink = gst_element_factory_make ("fakesink", "sink");
1075   gst_bin_add_many (GST_BIN (pipeline), src1, src2, adder, sink, NULL);
1076
1077   sink_template =
1078       gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS (adder),
1079       "sink_%u");
1080   fail_unless (GST_IS_PAD_TEMPLATE (sink_template));
1081   sinkpad1 = gst_element_request_pad (adder, sink_template, NULL, NULL);
1082   tmppad = gst_element_get_static_pad (src1, "src");
1083   gst_pad_link (tmppad, sinkpad1);
1084   gst_object_unref (tmppad);
1085
1086   sinkpad2 = gst_element_request_pad (adder, sink_template, NULL, NULL);
1087   tmppad = gst_element_get_static_pad (src2, "src");
1088   gst_pad_link (tmppad, sinkpad2);
1089   gst_object_unref (tmppad);
1090
1091   gst_element_link (adder, sink);
1092
1093   /* prepare playing */
1094   set_state_and_wait (bin, GST_STATE_PLAYING);
1095
1096   adder_src = gst_element_get_static_pad (adder, "src");
1097   fail_if (GST_PAD_IS_FLUSHING (adder_src));
1098   gst_pad_send_event (sinkpad1, gst_event_new_flush_start ());
1099   fail_unless (GST_PAD_IS_FLUSHING (adder_src));
1100   gst_pad_send_event (sinkpad1, gst_event_new_flush_stop (TRUE));
1101   fail_if (GST_PAD_IS_FLUSHING (adder_src));
1102   gst_object_unref (adder_src);
1103
1104   gst_element_release_request_pad (adder, sinkpad1);
1105   gst_object_unref (sinkpad1);
1106   gst_element_release_request_pad (adder, sinkpad2);
1107   gst_object_unref (sinkpad2);
1108
1109   /* cleanup */
1110   gst_element_set_state (pipeline, GST_STATE_NULL);
1111   gst_object_unref (pipeline);
1112 }
1113
1114 GST_END_TEST;
1115 #endif
1116
1117 static Suite *
1118 adder_suite (void)
1119 {
1120   Suite *s = suite_create ("adder");
1121   TCase *tc_chain = tcase_create ("general");
1122
1123   suite_add_tcase (s, tc_chain);
1124   tcase_add_test (tc_chain, test_caps);
1125   tcase_add_test (tc_chain, test_filter_caps);
1126   tcase_add_test (tc_chain, test_event);
1127   tcase_add_test (tc_chain, test_play_twice);
1128   tcase_add_test (tc_chain, test_play_twice_then_add_and_play_again);
1129   tcase_add_test (tc_chain, test_live_seeking);
1130   tcase_add_test (tc_chain, test_add_pad);
1131   tcase_add_test (tc_chain, test_remove_pad);
1132   tcase_add_test (tc_chain, test_clip);
1133   tcase_add_test (tc_chain, test_duration_is_max);
1134   tcase_add_test (tc_chain, test_duration_unknown_overrides);
1135   tcase_add_test (tc_chain, test_loop);
1136   /* This test is racy and occasionally fails in interesting ways
1137    * https://bugzilla.gnome.org/show_bug.cgi?id=708891
1138    * It's unlikely that it will ever be fixed for adder, works with audiomixer */
1139 #if 0
1140   tcase_add_test (tc_chain, test_flush_start_flush_stop);
1141 #endif
1142   tcase_add_checked_fixture (tc_chain, test_setup, test_teardown);
1143
1144   /* Use a longer timeout */
1145 #ifdef HAVE_VALGRIND
1146   if (RUNNING_ON_VALGRIND) {
1147     tcase_set_timeout (tc_chain, 5 * 60);
1148   } else
1149 #endif
1150   {
1151     /* this is shorter than the default 60 seconds?! (tpm) */
1152     /* tcase_set_timeout (tc_chain, 6); */
1153   }
1154
1155   return s;
1156 }
1157
1158 GST_CHECK_MAIN (adder);