08040b525ac1d9f53d0b70b2781784fbc85f877e
[platform/upstream/gstreamer.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., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, 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 <unistd.h>
32
33 #include <gst/check/gstcheck.h>
34
35 static GMainLoop *main_loop;
36
37 static void
38 message_received (GstBus * bus, GstMessage * message, GstPipeline * bin)
39 {
40   GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
41       GST_MESSAGE_SRC (message), message);
42
43   switch (message->type) {
44     case GST_MESSAGE_EOS:
45       g_main_loop_quit (main_loop);
46       break;
47     case GST_MESSAGE_WARNING:{
48       GError *gerror;
49       gchar *debug;
50
51       gst_message_parse_warning (message, &gerror, &debug);
52       gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
53       g_error_free (gerror);
54       g_free (debug);
55       break;
56     }
57     case GST_MESSAGE_ERROR:{
58       GError *gerror;
59       gchar *debug;
60
61       gst_message_parse_error (message, &gerror, &debug);
62       gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
63       g_error_free (gerror);
64       g_free (debug);
65       g_main_loop_quit (main_loop);
66       break;
67     }
68     default:
69       break;
70   }
71 }
72
73
74 static GstFormat format = GST_FORMAT_UNDEFINED;
75 static gint64 position = -1;
76
77 static void
78 test_event_message_received (GstBus * bus, GstMessage * message,
79     GstPipeline * bin)
80 {
81   GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
82       GST_MESSAGE_SRC (message), message);
83
84   switch (message->type) {
85     case GST_MESSAGE_SEGMENT_DONE:
86       gst_message_parse_segment_done (message, &format, &position);
87       GST_INFO ("received segment_done : %" G_GINT64_FORMAT, position);
88       g_main_loop_quit (main_loop);
89       break;
90     default:
91       g_assert_not_reached ();
92       break;
93   }
94 }
95
96
97 GST_START_TEST (test_event)
98 {
99   GstElement *bin, *src1, *src2, *adder, *sink;
100   GstBus *bus;
101   GstEvent *seek_event;
102   gboolean res;
103
104   GST_INFO ("preparing test");
105
106   /* build pipeline */
107   bin = gst_pipeline_new ("pipeline");
108   bus = gst_element_get_bus (bin);
109   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
110
111   /* FIXME, fakesrc with default setting will produce 0 sized
112    * buffers and incompatible caps for adder that will make
113    * adder EOS and error out */
114   src1 = gst_element_factory_make ("audiotestsrc", "src1");
115   g_object_set (src1, "wave", 4, NULL); /* silence */
116   src2 = gst_element_factory_make ("audiotestsrc", "src2");
117   g_object_set (src2, "wave", 4, NULL); /* silence */
118   adder = gst_element_factory_make ("adder", "adder");
119   sink = gst_element_factory_make ("fakesink", "sink");
120   gst_bin_add_many (GST_BIN (bin), src1, src2, adder, sink, NULL);
121
122   res = gst_element_link (src1, adder);
123   fail_unless (res == TRUE, NULL);
124   res = gst_element_link (src2, adder);
125   fail_unless (res == TRUE, NULL);
126   res = gst_element_link (adder, sink);
127   fail_unless (res == TRUE, NULL);
128
129   seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
130       GST_SEEK_FLAG_SEGMENT | GST_SEEK_FLAG_FLUSH,
131       GST_SEEK_TYPE_SET, (GstClockTime) 0,
132       GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);
133
134   format = GST_FORMAT_UNDEFINED;
135   position = -1;
136
137   main_loop = g_main_loop_new (NULL, FALSE);
138   g_signal_connect (bus, "message::segment-done",
139       (GCallback) test_event_message_received, bin);
140   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
141   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
142   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
143
144   GST_INFO ("starting test");
145
146   /* prepare playing */
147   res = gst_element_set_state (bin, GST_STATE_PAUSED);
148   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
149
150   /* wait for completion */
151   res = gst_element_get_state (bin, NULL, NULL, GST_CLOCK_TIME_NONE);
152   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
153
154   res = gst_element_send_event (bin, seek_event);
155   fail_unless (res == TRUE, NULL);
156
157   /* run pipeline */
158   res = gst_element_set_state (bin, GST_STATE_PLAYING);
159   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
160
161   g_main_loop_run (main_loop);
162
163   res = gst_element_set_state (bin, GST_STATE_NULL);
164   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
165
166   fail_unless (position == 2 * GST_SECOND, NULL);
167
168   /* cleanup */
169   g_main_loop_unref (main_loop);
170   gst_object_unref (G_OBJECT (bus));
171   gst_object_unref (G_OBJECT (bin));
172 }
173
174 GST_END_TEST;
175
176 static guint play_count = 0;
177 static GstEvent *play_seek_event = NULL;
178
179 static void
180 test_play_twice_message_received (GstBus * bus, GstMessage * message,
181     GstPipeline * bin)
182 {
183   gboolean res;
184
185   GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
186       GST_MESSAGE_SRC (message), message);
187
188   switch (message->type) {
189     case GST_MESSAGE_SEGMENT_DONE:
190       play_count++;
191       if (play_count == 1) {
192         res = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_READY);
193         fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
194
195         /* prepare playing again */
196         res = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_PAUSED);
197         fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
198
199         /* wait for completion */
200         res =
201             gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
202             GST_CLOCK_TIME_NONE);
203         fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
204
205         res = gst_element_send_event (GST_ELEMENT (bin),
206             gst_event_ref (play_seek_event));
207         fail_unless (res == TRUE, NULL);
208
209         res = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_PLAYING);
210         fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
211       } else {
212         g_main_loop_quit (main_loop);
213       }
214       break;
215     default:
216       g_assert_not_reached ();
217       break;
218   }
219 }
220
221
222 GST_START_TEST (test_play_twice)
223 {
224   GstElement *bin, *src1, *src2, *adder, *sink;
225   GstBus *bus;
226   gboolean res;
227
228   GST_INFO ("preparing test");
229
230   /* build pipeline */
231   bin = gst_pipeline_new ("pipeline");
232   bus = gst_element_get_bus (bin);
233   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
234
235   src1 = gst_element_factory_make ("audiotestsrc", "src1");
236   g_object_set (src1, "wave", 4, NULL); /* silence */
237   src2 = gst_element_factory_make ("audiotestsrc", "src2");
238   g_object_set (src2, "wave", 4, NULL); /* silence */
239   adder = gst_element_factory_make ("adder", "adder");
240   sink = gst_element_factory_make ("fakesink", "sink");
241   gst_bin_add_many (GST_BIN (bin), src1, src2, adder, sink, NULL);
242
243   res = gst_element_link (src1, adder);
244   fail_unless (res == TRUE, NULL);
245   res = gst_element_link (src2, adder);
246   fail_unless (res == TRUE, NULL);
247   res = gst_element_link (adder, sink);
248   fail_unless (res == TRUE, NULL);
249
250   play_seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
251       GST_SEEK_FLAG_SEGMENT | GST_SEEK_FLAG_FLUSH,
252       GST_SEEK_TYPE_SET, (GstClockTime) 0,
253       GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);
254
255   play_count = 0;
256
257   main_loop = g_main_loop_new (NULL, FALSE);
258   g_signal_connect (bus, "message::segment-done",
259       (GCallback) test_play_twice_message_received, bin);
260   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
261   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
262   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
263
264   GST_INFO ("starting test");
265
266   /* prepare playing */
267   res = gst_element_set_state (bin, GST_STATE_PAUSED);
268   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
269
270   /* wait for completion */
271   res =
272       gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
273       GST_CLOCK_TIME_NONE);
274   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
275
276   res = gst_element_send_event (bin, gst_event_ref (play_seek_event));
277   fail_unless (res == TRUE, NULL);
278
279   GST_INFO ("seeked");
280
281   /* run pipeline */
282   res = gst_element_set_state (bin, GST_STATE_PLAYING);
283   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
284
285   g_main_loop_run (main_loop);
286
287   res = gst_element_set_state (bin, GST_STATE_NULL);
288   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
289
290   fail_unless (play_count == 2, NULL);
291
292   /* cleanup */
293   g_main_loop_unref (main_loop);
294   gst_event_ref (play_seek_event);
295   gst_object_unref (G_OBJECT (bus));
296   gst_object_unref (G_OBJECT (bin));
297 }
298
299 GST_END_TEST;
300
301 GST_START_TEST (test_play_twice_then_add_and_play_again)
302 {
303   GstElement *bin, *src1, *src2, *src3, *adder, *sink;
304   GstBus *bus;
305   gboolean res;
306   gint i;
307
308   GST_INFO ("preparing test");
309
310   /* build pipeline */
311   bin = gst_pipeline_new ("pipeline");
312   bus = gst_element_get_bus (bin);
313   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
314
315   src1 = gst_element_factory_make ("audiotestsrc", "src1");
316   g_object_set (src1, "wave", 4, NULL); /* silence */
317   src2 = gst_element_factory_make ("audiotestsrc", "src2");
318   g_object_set (src2, "wave", 4, NULL); /* silence */
319   adder = gst_element_factory_make ("adder", "adder");
320   sink = gst_element_factory_make ("fakesink", "sink");
321   gst_bin_add_many (GST_BIN (bin), src1, src2, adder, sink, NULL);
322
323   res = gst_element_link (src1, adder);
324   fail_unless (res == TRUE, NULL);
325   res = gst_element_link (src2, adder);
326   fail_unless (res == TRUE, NULL);
327   res = gst_element_link (adder, sink);
328   fail_unless (res == TRUE, NULL);
329
330   play_seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
331       GST_SEEK_FLAG_SEGMENT | GST_SEEK_FLAG_FLUSH,
332       GST_SEEK_TYPE_SET, (GstClockTime) 0,
333       GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);
334
335   main_loop = g_main_loop_new (NULL, FALSE);
336   g_signal_connect (bus, "message::segment-done",
337       (GCallback) test_play_twice_message_received, bin);
338   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
339   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
340   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
341
342   /* run it twice */
343   for (i = 0; i < 2; i++) {
344     play_count = 0;
345
346     GST_INFO ("starting test-loop %d", i);
347
348     /* prepare playing */
349     res = gst_element_set_state (bin, GST_STATE_PAUSED);
350     fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
351
352     /* wait for completion */
353     res =
354         gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
355         GST_CLOCK_TIME_NONE);
356     fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
357
358     res = gst_element_send_event (bin, gst_event_ref (play_seek_event));
359     fail_unless (res == TRUE, NULL);
360
361     GST_INFO ("seeked");
362
363     /* run pipeline */
364     res = gst_element_set_state (bin, GST_STATE_PLAYING);
365     fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
366
367     g_main_loop_run (main_loop);
368
369     res = gst_element_set_state (bin, GST_STATE_READY);
370     fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
371
372     fail_unless (play_count == 2, NULL);
373
374     /* plug another source */
375     if (i == 0) {
376       src3 = gst_element_factory_make ("audiotestsrc", "src3");
377       g_object_set (src3, "wave", 4, NULL);     /* silence */
378       gst_bin_add (GST_BIN (bin), src3);
379
380       res = gst_element_link (src3, adder);
381       fail_unless (res == TRUE, NULL);
382     }
383   }
384
385   res = gst_element_set_state (bin, GST_STATE_NULL);
386   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
387
388   /* cleanup */
389   g_main_loop_unref (main_loop);
390   gst_event_ref (play_seek_event);
391   gst_object_unref (G_OBJECT (bus));
392   gst_object_unref (G_OBJECT (bin));
393 }
394
395 GST_END_TEST;
396
397
398 static void
399 test_live_seeking_eos_message_received (GstBus * bus, GstMessage * message,
400     GstPipeline * bin)
401 {
402   GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
403       GST_MESSAGE_SRC (message), message);
404
405   switch (message->type) {
406     case GST_MESSAGE_EOS:
407       g_main_loop_quit (main_loop);
408       break;
409     default:
410       g_assert_not_reached ();
411       break;
412   }
413 }
414
415
416 /* test failing seeks on live-sources */
417 GST_START_TEST (test_live_seeking)
418 {
419   GstElement *bin, *src1, *src2, *ac1, *ac2, *adder, *sink;
420   GstBus *bus;
421   gboolean res;
422   gint i;
423
424   GST_INFO ("preparing test");
425
426   /* build pipeline */
427   bin = gst_pipeline_new ("pipeline");
428   bus = gst_element_get_bus (bin);
429   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
430
431   /* normal audiosources behave differently than audiotestsrc */
432 #if 0
433   src1 = gst_element_factory_make ("audiotestsrc", "src1");
434   g_object_set (src1, "wave", 4, "is-live", TRUE, NULL);        /* silence */
435 #else
436   src1 = gst_element_factory_make ("alsasrc", "src1");
437   if (!src1) {
438     GST_INFO ("no audiosrc, skipping");
439   }
440   /* live sources ignore seeks, force eos after 2 sec (4 buffers half second
441    * each) - don't use autoaudiosrc, as then we can't set anything here */
442   g_object_set (src1, "num-buffers", 4, "blocksize", 44100, NULL);
443 #endif
444   ac1 = gst_element_factory_make ("audioconvert", "ac1");
445   src2 = gst_element_factory_make ("audiotestsrc", "src2");
446   g_object_set (src2, "wave", 4, NULL); /* silence */
447   ac2 = gst_element_factory_make ("audioconvert", "ac2");
448   adder = gst_element_factory_make ("adder", "adder");
449   sink = gst_element_factory_make ("fakesink", "sink");
450   gst_bin_add_many (GST_BIN (bin), src1, ac1, src2, ac2, adder, sink, NULL);
451
452   res = gst_element_link (src1, ac1);
453   fail_unless (res == TRUE, NULL);
454   res = gst_element_link (ac1, adder);
455   fail_unless (res == TRUE, NULL);
456   res = gst_element_link (src2, ac2);
457   fail_unless (res == TRUE, NULL);
458   res = gst_element_link (ac2, adder);
459   fail_unless (res == TRUE, NULL);
460   res = gst_element_link (adder, sink);
461   fail_unless (res == TRUE, NULL);
462
463   play_seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
464       GST_SEEK_FLAG_FLUSH,
465       GST_SEEK_TYPE_SET, (GstClockTime) 0,
466       GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);
467
468   main_loop = g_main_loop_new (NULL, FALSE);
469   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
470   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
471   g_signal_connect (bus, "message::eos",
472       (GCallback) test_live_seeking_eos_message_received, bin);
473
474   /* run it twice */
475   for (i = 0; i < 2; i++) {
476
477     GST_INFO ("starting test-loop %d", i);
478
479     /* prepare playing */
480     res = gst_element_set_state (bin, GST_STATE_PAUSED);
481     fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
482
483     /* wait for completion */
484     res =
485         gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
486         GST_CLOCK_TIME_NONE);
487     fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
488
489     res = gst_element_send_event (bin, gst_event_ref (play_seek_event));
490 #if 0
491     fail_unless (res == TRUE, NULL);
492 #else
493     /* adder is picky, if a single seek fails it totaly fails */
494     fail_unless (res == FALSE, NULL);
495 #endif
496
497     GST_INFO ("seeked");
498
499     /* run pipeline */
500     res = gst_element_set_state (bin, GST_STATE_PLAYING);
501     fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
502
503     GST_INFO ("playing");
504
505     g_main_loop_run (main_loop);
506
507     res = gst_element_set_state (bin, GST_STATE_NULL);
508     fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
509   }
510
511   /* cleanup */
512   g_main_loop_unref (main_loop);
513   gst_event_unref (play_seek_event);
514   gst_object_unref (G_OBJECT (bus));
515   gst_object_unref (G_OBJECT (bin));
516 }
517
518 GST_END_TEST;
519
520 /* check if adding pads work as expected */
521 GST_START_TEST (test_add_pad)
522 {
523   GstElement *bin, *src1, *src2, *adder, *sink;
524   GstBus *bus;
525   gboolean res;
526
527   GST_INFO ("preparing test");
528
529   /* build pipeline */
530   bin = gst_pipeline_new ("pipeline");
531   bus = gst_element_get_bus (bin);
532   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
533
534   src1 = gst_element_factory_make ("audiotestsrc", "src1");
535   g_object_set (src1, "num-buffers", 4, NULL);
536   g_object_set (src1, "wave", 4, NULL); /* silence */
537   src2 = gst_element_factory_make ("audiotestsrc", "src2");
538   /* one buffer less, we connect with 1 buffer of delay */
539   g_object_set (src2, "num-buffers", 3, NULL);
540   g_object_set (src2, "wave", 4, NULL); /* silence */
541   adder = gst_element_factory_make ("adder", "adder");
542   sink = gst_element_factory_make ("fakesink", "sink");
543   gst_bin_add_many (GST_BIN (bin), src1, adder, sink, NULL);
544
545   res = gst_element_link (src1, adder);
546   fail_unless (res == TRUE, NULL);
547   res = gst_element_link (adder, sink);
548   fail_unless (res == TRUE, NULL);
549
550   main_loop = g_main_loop_new (NULL, FALSE);
551   g_signal_connect (bus, "message::segment-done", (GCallback) message_received,
552       bin);
553   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
554   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
555   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
556
557   GST_INFO ("starting test");
558
559   /* prepare playing */
560   res = gst_element_set_state (bin, GST_STATE_PAUSED);
561   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
562
563   /* wait for completion */
564   res =
565       gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
566       GST_CLOCK_TIME_NONE);
567   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
568
569   /* add other element */
570   gst_bin_add_many (GST_BIN (bin), src2, NULL);
571
572   /* now link the second element */
573   res = gst_element_link (src2, adder);
574   fail_unless (res == TRUE, NULL);
575
576   /* set to PAUSED as well */
577   res = gst_element_set_state (src2, GST_STATE_PAUSED);
578
579   /* now play all */
580   res = gst_element_set_state (bin, GST_STATE_PLAYING);
581   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
582
583   g_main_loop_run (main_loop);
584
585   res = gst_element_set_state (bin, GST_STATE_NULL);
586   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
587
588   /* cleanup */
589   g_main_loop_unref (main_loop);
590   gst_object_unref (G_OBJECT (bus));
591   gst_object_unref (G_OBJECT (bin));
592 }
593
594 GST_END_TEST;
595
596 /* check if removing pads work as expected */
597 GST_START_TEST (test_remove_pad)
598 {
599   GstElement *bin, *src, *adder, *sink;
600   GstBus *bus;
601   GstPad *pad;
602   gboolean res;
603
604   GST_INFO ("preparing test");
605
606   /* build pipeline */
607   bin = gst_pipeline_new ("pipeline");
608   bus = gst_element_get_bus (bin);
609   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
610
611   src = gst_element_factory_make ("audiotestsrc", "src");
612   g_object_set (src, "num-buffers", 4, NULL);
613   g_object_set (src, "wave", 4, NULL);
614   adder = gst_element_factory_make ("adder", "adder");
615   sink = gst_element_factory_make ("fakesink", "sink");
616   gst_bin_add_many (GST_BIN (bin), src, adder, sink, NULL);
617
618   res = gst_element_link (src, adder);
619   fail_unless (res == TRUE, NULL);
620   res = gst_element_link (adder, sink);
621   fail_unless (res == TRUE, NULL);
622
623   /* create an unconnected sinkpad in adder */
624   pad = gst_element_get_request_pad (adder, "sink%d");
625   fail_if (pad == NULL, NULL);
626
627   main_loop = g_main_loop_new (NULL, FALSE);
628   g_signal_connect (bus, "message::segment-done", (GCallback) message_received,
629       bin);
630   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
631   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
632   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
633
634   GST_INFO ("starting test");
635
636   /* prepare playing, this will not preroll as adder is waiting
637    * on the unconnected sinkpad. */
638   res = gst_element_set_state (bin, GST_STATE_PAUSED);
639   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
640
641   /* wait for completion for one second, will return ASYNC */
642   res = gst_element_get_state (GST_ELEMENT (bin), NULL, NULL, GST_SECOND);
643   fail_unless (res == GST_STATE_CHANGE_ASYNC, NULL);
644
645   /* get rid of the pad now, adder should stop waiting on it and
646    * continue the preroll */
647   gst_element_release_request_pad (adder, pad);
648   gst_object_unref (pad);
649
650   /* wait for completion, should work now */
651   res =
652       gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
653       GST_CLOCK_TIME_NONE);
654   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
655
656   /* now play all */
657   res = gst_element_set_state (bin, GST_STATE_PLAYING);
658   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
659
660   g_main_loop_run (main_loop);
661
662   res = gst_element_set_state (bin, GST_STATE_NULL);
663   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
664
665   /* cleanup */
666   g_main_loop_unref (main_loop);
667   gst_object_unref (G_OBJECT (bus));
668   gst_object_unref (G_OBJECT (bin));
669 }
670
671 GST_END_TEST;
672
673
674 static Suite *
675 adder_suite (void)
676 {
677   Suite *s = suite_create ("adder");
678   TCase *tc_chain = tcase_create ("general");
679
680   suite_add_tcase (s, tc_chain);
681   tcase_add_test (tc_chain, test_event);
682   tcase_add_test (tc_chain, test_play_twice);
683   tcase_add_test (tc_chain, test_play_twice_then_add_and_play_again);
684   tcase_add_test (tc_chain, test_live_seeking);
685   tcase_add_test (tc_chain, test_add_pad);
686   tcase_add_test (tc_chain, test_remove_pad);
687
688   /* Use a longer timeout */
689 #ifdef HAVE_VALGRIND
690   if (RUNNING_ON_VALGRIND) {
691     tcase_set_timeout (tc_chain, 5 * 60);
692   } else
693 #endif
694   {
695     /* this is shorter than the default 60 seconds?! (tpm) */
696     /* tcase_set_timeout (tc_chain, 6); */
697   }
698
699   return s;
700 }
701
702 int
703 main (int argc, char **argv)
704 {
705   int nf;
706
707   Suite *s = adder_suite ();
708   SRunner *sr = srunner_create (s);
709
710   gst_check_init (&argc, &argv);
711
712   srunner_run_all (sr, CK_NORMAL);
713   nf = srunner_ntests_failed (sr);
714   srunner_free (sr);
715
716   return nf;
717 }