Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / 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 #include <gst/check/gstconsistencychecker.h>
35
36 static GMainLoop *main_loop;
37
38 static void
39 message_received (GstBus * bus, GstMessage * message, GstPipeline * bin)
40 {
41   GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
42       GST_MESSAGE_SRC (message), message);
43
44   switch (message->type) {
45     case GST_MESSAGE_EOS:
46       g_main_loop_quit (main_loop);
47       break;
48     case GST_MESSAGE_WARNING:{
49       GError *gerror;
50       gchar *debug;
51
52       gst_message_parse_warning (message, &gerror, &debug);
53       gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
54       g_error_free (gerror);
55       g_free (debug);
56       break;
57     }
58     case GST_MESSAGE_ERROR:{
59       GError *gerror;
60       gchar *debug;
61
62       gst_message_parse_error (message, &gerror, &debug);
63       gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
64       g_error_free (gerror);
65       g_free (debug);
66       g_main_loop_quit (main_loop);
67       break;
68     }
69     default:
70       break;
71   }
72 }
73
74
75 static GstFormat format = GST_FORMAT_UNDEFINED;
76 static gint64 position = -1;
77
78 static void
79 test_event_message_received (GstBus * bus, GstMessage * message,
80     GstPipeline * bin)
81 {
82   GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
83       GST_MESSAGE_SRC (message), message);
84
85   switch (message->type) {
86     case GST_MESSAGE_SEGMENT_DONE:
87       gst_message_parse_segment_done (message, &format, &position);
88       GST_INFO ("received segment_done : %" G_GINT64_FORMAT, position);
89       g_main_loop_quit (main_loop);
90       break;
91     default:
92       g_assert_not_reached ();
93       break;
94   }
95 }
96
97
98 GST_START_TEST (test_event)
99 {
100   GstElement *bin, *src1, *src2, *adder, *sink;
101   GstBus *bus;
102   GstEvent *seek_event;
103   gboolean res;
104   GstPad *srcpad;
105   GstStreamConsistency *consist;
106
107   GST_INFO ("preparing test");
108
109   /* build pipeline */
110   bin = gst_pipeline_new ("pipeline");
111   bus = gst_element_get_bus (bin);
112   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
113
114   /* FIXME, fakesrc with default setting will produce 0 sized
115    * buffers and incompatible caps for adder that will make
116    * adder EOS and error out */
117   src1 = gst_element_factory_make ("audiotestsrc", "src1");
118   g_object_set (src1, "wave", 4, NULL); /* silence */
119   src2 = gst_element_factory_make ("audiotestsrc", "src2");
120   g_object_set (src2, "wave", 4, NULL); /* silence */
121   adder = gst_element_factory_make ("adder", "adder");
122   sink = gst_element_factory_make ("fakesink", "sink");
123   gst_bin_add_many (GST_BIN (bin), src1, src2, adder, sink, NULL);
124
125   res = gst_element_link (src1, adder);
126   fail_unless (res == TRUE, NULL);
127   res = gst_element_link (src2, adder);
128   fail_unless (res == TRUE, NULL);
129   res = gst_element_link (adder, sink);
130   fail_unless (res == TRUE, NULL);
131
132   srcpad = gst_element_get_static_pad (adder, "src");
133   consist = gst_consistency_checker_new (srcpad);
134   gst_object_unref (srcpad);
135
136   seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
137       GST_SEEK_FLAG_SEGMENT | GST_SEEK_FLAG_FLUSH,
138       GST_SEEK_TYPE_SET, (GstClockTime) 0,
139       GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);
140
141   format = GST_FORMAT_UNDEFINED;
142   position = -1;
143
144   main_loop = g_main_loop_new (NULL, FALSE);
145   g_signal_connect (bus, "message::segment-done",
146       (GCallback) test_event_message_received, bin);
147   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
148   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
149   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
150
151   GST_INFO ("starting test");
152
153   /* prepare playing */
154   res = gst_element_set_state (bin, GST_STATE_PAUSED);
155   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
156
157   /* wait for completion */
158   res = gst_element_get_state (bin, NULL, NULL, GST_CLOCK_TIME_NONE);
159   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
160
161   res = gst_element_send_event (bin, seek_event);
162   fail_unless (res == TRUE, NULL);
163
164   /* run pipeline */
165   res = gst_element_set_state (bin, GST_STATE_PLAYING);
166   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
167
168   g_main_loop_run (main_loop);
169
170   res = gst_element_set_state (bin, GST_STATE_NULL);
171   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
172
173   fail_unless (position == 2 * GST_SECOND, NULL);
174
175   /* cleanup */
176   g_main_loop_unref (main_loop);
177   gst_consistency_checker_free (consist);
178   gst_object_unref (G_OBJECT (bus));
179   gst_object_unref (G_OBJECT (bin));
180 }
181
182 GST_END_TEST;
183
184 static guint play_count = 0;
185 static GstEvent *play_seek_event = NULL;
186
187 static void
188 test_play_twice_message_received (GstBus * bus, GstMessage * message,
189     GstPipeline * bin)
190 {
191   gboolean res;
192
193   GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
194       GST_MESSAGE_SRC (message), message);
195
196   switch (message->type) {
197     case GST_MESSAGE_SEGMENT_DONE:
198       play_count++;
199       if (play_count == 1) {
200         res = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_READY);
201         fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
202
203         /* prepare playing again */
204         res = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_PAUSED);
205         fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
206
207         /* wait for completion */
208         res =
209             gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
210             GST_CLOCK_TIME_NONE);
211         fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
212
213         res = gst_element_send_event (GST_ELEMENT (bin),
214             gst_event_ref (play_seek_event));
215         fail_unless (res == TRUE, NULL);
216
217         res = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_PLAYING);
218         fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
219       } else {
220         g_main_loop_quit (main_loop);
221       }
222       break;
223     default:
224       g_assert_not_reached ();
225       break;
226   }
227 }
228
229
230 GST_START_TEST (test_play_twice)
231 {
232   GstElement *bin, *src1, *src2, *adder, *sink;
233   GstBus *bus;
234   gboolean res;
235   GstPad *srcpad;
236   GstStreamConsistency *consist;
237
238   GST_INFO ("preparing test");
239
240   /* build pipeline */
241   bin = gst_pipeline_new ("pipeline");
242   bus = gst_element_get_bus (bin);
243   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
244
245   src1 = gst_element_factory_make ("audiotestsrc", "src1");
246   g_object_set (src1, "wave", 4, NULL); /* silence */
247   src2 = gst_element_factory_make ("audiotestsrc", "src2");
248   g_object_set (src2, "wave", 4, NULL); /* silence */
249   adder = gst_element_factory_make ("adder", "adder");
250   sink = gst_element_factory_make ("fakesink", "sink");
251   gst_bin_add_many (GST_BIN (bin), src1, src2, adder, sink, NULL);
252
253   res = gst_element_link (src1, adder);
254   fail_unless (res == TRUE, NULL);
255   res = gst_element_link (src2, adder);
256   fail_unless (res == TRUE, NULL);
257   res = gst_element_link (adder, sink);
258   fail_unless (res == TRUE, NULL);
259
260   srcpad = gst_element_get_static_pad (adder, "src");
261   consist = gst_consistency_checker_new (srcpad);
262   gst_object_unref (srcpad);
263
264   play_seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
265       GST_SEEK_FLAG_SEGMENT | GST_SEEK_FLAG_FLUSH,
266       GST_SEEK_TYPE_SET, (GstClockTime) 0,
267       GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);
268
269   play_count = 0;
270
271   main_loop = g_main_loop_new (NULL, FALSE);
272   g_signal_connect (bus, "message::segment-done",
273       (GCallback) test_play_twice_message_received, bin);
274   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
275   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
276   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
277
278   GST_INFO ("starting test");
279
280   /* prepare playing */
281   res = gst_element_set_state (bin, GST_STATE_PAUSED);
282   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
283
284   /* wait for completion */
285   res =
286       gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
287       GST_CLOCK_TIME_NONE);
288   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
289
290   res = gst_element_send_event (bin, gst_event_ref (play_seek_event));
291   fail_unless (res == TRUE, NULL);
292
293   GST_INFO ("seeked");
294
295   /* run pipeline */
296   res = gst_element_set_state (bin, GST_STATE_PLAYING);
297   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
298
299   g_main_loop_run (main_loop);
300
301   res = gst_element_set_state (bin, GST_STATE_NULL);
302   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
303
304   fail_unless (play_count == 2, NULL);
305
306   /* cleanup */
307   g_main_loop_unref (main_loop);
308   gst_consistency_checker_free (consist);
309   gst_event_ref (play_seek_event);
310   gst_object_unref (G_OBJECT (bus));
311   gst_object_unref (G_OBJECT (bin));
312 }
313
314 GST_END_TEST;
315
316 GST_START_TEST (test_play_twice_then_add_and_play_again)
317 {
318   GstElement *bin, *src1, *src2, *src3, *adder, *sink;
319   GstBus *bus;
320   gboolean res;
321   gint i;
322   GstPad *srcpad;
323   GstStreamConsistency *consist;
324
325   GST_INFO ("preparing test");
326
327   /* build pipeline */
328   bin = gst_pipeline_new ("pipeline");
329   bus = gst_element_get_bus (bin);
330   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
331
332   src1 = gst_element_factory_make ("audiotestsrc", "src1");
333   g_object_set (src1, "wave", 4, NULL); /* silence */
334   src2 = gst_element_factory_make ("audiotestsrc", "src2");
335   g_object_set (src2, "wave", 4, NULL); /* silence */
336   adder = gst_element_factory_make ("adder", "adder");
337   sink = gst_element_factory_make ("fakesink", "sink");
338   gst_bin_add_many (GST_BIN (bin), src1, src2, adder, sink, NULL);
339
340   srcpad = gst_element_get_static_pad (adder, "src");
341   consist = gst_consistency_checker_new (srcpad);
342   gst_object_unref (srcpad);
343
344   res = gst_element_link (src1, adder);
345   fail_unless (res == TRUE, NULL);
346   res = gst_element_link (src2, adder);
347   fail_unless (res == TRUE, NULL);
348   res = gst_element_link (adder, sink);
349   fail_unless (res == TRUE, NULL);
350
351   play_seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
352       GST_SEEK_FLAG_SEGMENT | GST_SEEK_FLAG_FLUSH,
353       GST_SEEK_TYPE_SET, (GstClockTime) 0,
354       GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);
355
356   main_loop = g_main_loop_new (NULL, FALSE);
357   g_signal_connect (bus, "message::segment-done",
358       (GCallback) test_play_twice_message_received, bin);
359   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
360   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
361   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
362
363   /* run it twice */
364   for (i = 0; i < 2; i++) {
365     play_count = 0;
366
367     GST_INFO ("starting test-loop %d", i);
368
369     /* prepare playing */
370     res = gst_element_set_state (bin, GST_STATE_PAUSED);
371     fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
372
373     /* wait for completion */
374     res =
375         gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
376         GST_CLOCK_TIME_NONE);
377     fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
378
379     res = gst_element_send_event (bin, gst_event_ref (play_seek_event));
380     fail_unless (res == TRUE, NULL);
381
382     GST_INFO ("seeked");
383
384     /* run pipeline */
385     res = gst_element_set_state (bin, GST_STATE_PLAYING);
386     fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
387
388     g_main_loop_run (main_loop);
389
390     res = gst_element_set_state (bin, GST_STATE_READY);
391     fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
392
393     fail_unless (play_count == 2, NULL);
394
395     /* plug another source */
396     if (i == 0) {
397       src3 = gst_element_factory_make ("audiotestsrc", "src3");
398       g_object_set (src3, "wave", 4, NULL);     /* silence */
399       gst_bin_add (GST_BIN (bin), src3);
400
401       res = gst_element_link (src3, adder);
402       fail_unless (res == TRUE, NULL);
403     }
404
405     gst_consistency_checker_reset (consist);
406   }
407
408   res = gst_element_set_state (bin, GST_STATE_NULL);
409   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
410
411   /* cleanup */
412   g_main_loop_unref (main_loop);
413   gst_event_ref (play_seek_event);
414   gst_consistency_checker_free (consist);
415   gst_object_unref (G_OBJECT (bus));
416   gst_object_unref (G_OBJECT (bin));
417 }
418
419 GST_END_TEST;
420
421
422 static void
423 test_live_seeking_eos_message_received (GstBus * bus, GstMessage * message,
424     GstPipeline * bin)
425 {
426   GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
427       GST_MESSAGE_SRC (message), message);
428
429   switch (message->type) {
430     case GST_MESSAGE_EOS:
431       g_main_loop_quit (main_loop);
432       break;
433     default:
434       g_assert_not_reached ();
435       break;
436   }
437 }
438
439
440 /* test failing seeks on live-sources */
441 GST_START_TEST (test_live_seeking)
442 {
443   GstElement *bin, *src1, *src2, *ac1, *ac2, *adder, *sink;
444   GstBus *bus;
445   gboolean res;
446   GstPad *srcpad;
447   gint i;
448   GstStreamConsistency *consist;
449
450   GST_INFO ("preparing test");
451   main_loop = NULL;
452   play_seek_event = NULL;
453
454   /* build pipeline */
455   bin = gst_pipeline_new ("pipeline");
456   bus = gst_element_get_bus (bin);
457   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
458
459   /* normal audiosources behave differently than audiotestsrc */
460 #if 0
461   src1 = gst_element_factory_make ("audiotestsrc", "src1");
462   g_object_set (src1, "wave", 4, "is-live", TRUE, NULL);        /* silence */
463 #else
464   src1 = gst_element_factory_make ("alsasrc", "src1");
465   if (!src1) {
466     GST_INFO ("no audiosrc, skipping");
467     goto cleanup;
468   }
469   /* Test that the audio source can get to paused, else skip */
470   res = gst_element_set_state (src1, GST_STATE_PAUSED);
471   (void) gst_element_set_state (src1, GST_STATE_NULL);
472   gst_object_unref (src1);
473
474   if (res == GST_STATE_CHANGE_FAILURE)
475     goto cleanup;
476   src1 = gst_element_factory_make ("alsasrc", "src1");
477
478   /* live sources ignore seeks, force eos after 2 sec (4 buffers half second
479    * each) - don't use autoaudiosrc, as then we can't set anything here */
480   g_object_set (src1, "num-buffers", 4, "blocksize", 44100, NULL);
481 #endif
482   ac1 = gst_element_factory_make ("audioconvert", "ac1");
483   src2 = gst_element_factory_make ("audiotestsrc", "src2");
484   g_object_set (src2, "wave", 4, NULL); /* silence */
485   ac2 = gst_element_factory_make ("audioconvert", "ac2");
486   adder = gst_element_factory_make ("adder", "adder");
487   sink = gst_element_factory_make ("fakesink", "sink");
488   gst_bin_add_many (GST_BIN (bin), src1, ac1, src2, ac2, adder, sink, NULL);
489
490   res = gst_element_link (src1, ac1);
491   fail_unless (res == TRUE, NULL);
492   res = gst_element_link (ac1, adder);
493   fail_unless (res == TRUE, NULL);
494   res = gst_element_link (src2, ac2);
495   fail_unless (res == TRUE, NULL);
496   res = gst_element_link (ac2, adder);
497   fail_unless (res == TRUE, NULL);
498   res = gst_element_link (adder, sink);
499   fail_unless (res == TRUE, NULL);
500
501   play_seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
502       GST_SEEK_FLAG_FLUSH,
503       GST_SEEK_TYPE_SET, (GstClockTime) 0,
504       GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);
505
506   main_loop = g_main_loop_new (NULL, FALSE);
507   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
508   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
509   g_signal_connect (bus, "message::eos",
510       (GCallback) test_live_seeking_eos_message_received, bin);
511
512   srcpad = gst_element_get_static_pad (adder, "src");
513   consist = gst_consistency_checker_new (srcpad);
514   gst_object_unref (srcpad);
515
516   GST_INFO ("starting test");
517
518   /* run it twice */
519   for (i = 0; i < 2; i++) {
520
521     GST_INFO ("starting test-loop %d", i);
522
523     /* prepare playing */
524     res = gst_element_set_state (bin, GST_STATE_PAUSED);
525     fail_unless (res != GST_STATE_CHANGE_FAILURE);
526
527     /* wait for completion */
528     res =
529         gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
530         GST_CLOCK_TIME_NONE);
531     fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
532
533     res = gst_element_send_event (bin, gst_event_ref (play_seek_event));
534 #if 1
535     fail_unless (res == TRUE, NULL);
536 #else
537     /* adder is picky, if a single seek fails it totaly fails */
538     fail_unless (res == FALSE, NULL);
539 #endif
540
541     GST_INFO ("seeked");
542
543     /* run pipeline */
544     res = gst_element_set_state (bin, GST_STATE_PLAYING);
545     fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
546
547     GST_INFO ("playing");
548
549     g_main_loop_run (main_loop);
550
551     res = gst_element_set_state (bin, GST_STATE_NULL);
552     fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
553
554     gst_consistency_checker_reset (consist);
555   }
556
557   /* cleanup */
558 cleanup:
559   GST_INFO ("cleaning up");
560   if (main_loop)
561     g_main_loop_unref (main_loop);
562   if (play_seek_event)
563     gst_event_unref (play_seek_event);
564   gst_object_unref (G_OBJECT (bus));
565   gst_object_unref (G_OBJECT (bin));
566 }
567
568 GST_END_TEST;
569
570 /* check if adding pads work as expected */
571 GST_START_TEST (test_add_pad)
572 {
573   GstElement *bin, *src1, *src2, *adder, *sink;
574   GstBus *bus;
575   GstPad *srcpad;
576   GstStreamConsistency *consist;
577   gboolean res;
578
579   GST_INFO ("preparing test");
580
581   /* build pipeline */
582   bin = gst_pipeline_new ("pipeline");
583   bus = gst_element_get_bus (bin);
584   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
585
586   src1 = gst_element_factory_make ("audiotestsrc", "src1");
587   g_object_set (src1, "num-buffers", 4, NULL);
588   g_object_set (src1, "wave", 4, NULL); /* silence */
589   src2 = gst_element_factory_make ("audiotestsrc", "src2");
590   /* one buffer less, we connect with 1 buffer of delay */
591   g_object_set (src2, "num-buffers", 3, NULL);
592   g_object_set (src2, "wave", 4, NULL); /* silence */
593   adder = gst_element_factory_make ("adder", "adder");
594   sink = gst_element_factory_make ("fakesink", "sink");
595   gst_bin_add_many (GST_BIN (bin), src1, adder, sink, NULL);
596
597   res = gst_element_link (src1, adder);
598   fail_unless (res == TRUE, NULL);
599   res = gst_element_link (adder, sink);
600   fail_unless (res == TRUE, NULL);
601
602   srcpad = gst_element_get_static_pad (adder, "src");
603   consist = gst_consistency_checker_new (srcpad);
604   gst_object_unref (srcpad);
605
606   main_loop = g_main_loop_new (NULL, FALSE);
607   g_signal_connect (bus, "message::segment-done", (GCallback) message_received,
608       bin);
609   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
610   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
611   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
612
613   GST_INFO ("starting test");
614
615   /* prepare playing */
616   res = gst_element_set_state (bin, GST_STATE_PAUSED);
617   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
618
619   /* wait for completion */
620   res =
621       gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
622       GST_CLOCK_TIME_NONE);
623   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
624
625   /* add other element */
626   gst_bin_add_many (GST_BIN (bin), src2, NULL);
627
628   /* now link the second element */
629   res = gst_element_link (src2, adder);
630   fail_unless (res == TRUE, NULL);
631
632   /* set to PAUSED as well */
633   res = gst_element_set_state (src2, GST_STATE_PAUSED);
634
635   /* now play all */
636   res = gst_element_set_state (bin, GST_STATE_PLAYING);
637   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
638
639   g_main_loop_run (main_loop);
640
641   res = gst_element_set_state (bin, GST_STATE_NULL);
642   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
643
644   /* cleanup */
645   g_main_loop_unref (main_loop);
646   gst_object_unref (G_OBJECT (bus));
647   gst_object_unref (G_OBJECT (bin));
648 }
649
650 GST_END_TEST;
651
652 /* check if removing pads work as expected */
653 GST_START_TEST (test_remove_pad)
654 {
655   GstElement *bin, *src, *adder, *sink;
656   GstBus *bus;
657   GstPad *pad, *srcpad;
658   gboolean res;
659   GstStreamConsistency *consist;
660
661   GST_INFO ("preparing test");
662
663   /* build pipeline */
664   bin = gst_pipeline_new ("pipeline");
665   bus = gst_element_get_bus (bin);
666   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
667
668   src = gst_element_factory_make ("audiotestsrc", "src");
669   g_object_set (src, "num-buffers", 4, NULL);
670   g_object_set (src, "wave", 4, NULL);
671   adder = gst_element_factory_make ("adder", "adder");
672   sink = gst_element_factory_make ("fakesink", "sink");
673   gst_bin_add_many (GST_BIN (bin), src, adder, sink, NULL);
674
675   res = gst_element_link (src, adder);
676   fail_unless (res == TRUE, NULL);
677   res = gst_element_link (adder, sink);
678   fail_unless (res == TRUE, NULL);
679
680   /* create an unconnected sinkpad in adder */
681   pad = gst_element_get_request_pad (adder, "sink%d");
682   fail_if (pad == NULL, NULL);
683
684   srcpad = gst_element_get_static_pad (adder, "src");
685   consist = gst_consistency_checker_new (srcpad);
686   gst_object_unref (srcpad);
687
688   main_loop = g_main_loop_new (NULL, FALSE);
689   g_signal_connect (bus, "message::segment-done", (GCallback) message_received,
690       bin);
691   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
692   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
693   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
694
695   GST_INFO ("starting test");
696
697   /* prepare playing, this will not preroll as adder is waiting
698    * on the unconnected sinkpad. */
699   res = gst_element_set_state (bin, GST_STATE_PAUSED);
700   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
701
702   /* wait for completion for one second, will return ASYNC */
703   res = gst_element_get_state (GST_ELEMENT (bin), NULL, NULL, GST_SECOND);
704   fail_unless (res == GST_STATE_CHANGE_ASYNC, NULL);
705
706   /* get rid of the pad now, adder should stop waiting on it and
707    * continue the preroll */
708   gst_element_release_request_pad (adder, pad);
709   gst_object_unref (pad);
710
711   /* wait for completion, should work now */
712   res =
713       gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
714       GST_CLOCK_TIME_NONE);
715   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
716
717   /* now play all */
718   res = gst_element_set_state (bin, GST_STATE_PLAYING);
719   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
720
721   g_main_loop_run (main_loop);
722
723   res = gst_element_set_state (bin, GST_STATE_NULL);
724   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
725
726   /* cleanup */
727   g_main_loop_unref (main_loop);
728   gst_object_unref (G_OBJECT (bus));
729   gst_object_unref (G_OBJECT (bin));
730 }
731
732 GST_END_TEST;
733
734
735 static GstBuffer *handoff_buffer = NULL;
736 static void
737 handoff_buffer_cb (GstElement * fakesink, GstBuffer * buffer, GstPad * pad,
738     gpointer user_data)
739 {
740   GST_DEBUG ("got buffer %p", buffer);
741   gst_buffer_replace (&handoff_buffer, buffer);
742 }
743
744 /* check if clipping works as expected */
745 GST_START_TEST (test_clip)
746 {
747   GstElement *bin, *adder, *sink;
748   GstBus *bus;
749   GstPad *sinkpad;
750   gboolean res;
751   GstFlowReturn ret;
752   GstEvent *event;
753   GstBuffer *buffer;
754   GstCaps *caps;
755
756   GST_INFO ("preparing test");
757
758   /* build pipeline */
759   bin = gst_pipeline_new ("pipeline");
760   bus = gst_element_get_bus (bin);
761   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
762
763   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
764   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
765   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
766
767   /* just an adder and a fakesink */
768   adder = gst_element_factory_make ("adder", "adder");
769   sink = gst_element_factory_make ("fakesink", "sink");
770   g_object_set (sink, "signal-handoffs", TRUE, NULL);
771   g_signal_connect (sink, "handoff", (GCallback) handoff_buffer_cb, NULL);
772   gst_bin_add_many (GST_BIN (bin), adder, sink, NULL);
773
774   res = gst_element_link (adder, sink);
775   fail_unless (res == TRUE, NULL);
776
777   /* set to playing */
778   res = gst_element_set_state (bin, GST_STATE_PLAYING);
779   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
780
781   /* create an unconnected sinkpad in adder, should also automatically activate
782    * the pad */
783   sinkpad = gst_element_get_request_pad (adder, "sink%d");
784   fail_if (sinkpad == NULL, NULL);
785
786   /* send segment to adder */
787   event = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME,
788       GST_SECOND, 2 * GST_SECOND, 0);
789   gst_pad_send_event (sinkpad, event);
790
791   caps = gst_caps_new_simple ("audio/x-raw-int",
792       "rate", G_TYPE_INT, 44100,
793       "channels", G_TYPE_INT, 2,
794       "endianness", G_TYPE_INT, G_BYTE_ORDER,
795       "width", G_TYPE_INT, 16,
796       "depth", G_TYPE_INT, 16, "signed", G_TYPE_BOOLEAN, TRUE, NULL);
797
798   /* should be clipped and ok */
799   buffer = gst_buffer_new_and_alloc (44100);
800   GST_BUFFER_TIMESTAMP (buffer) = 0;
801   GST_BUFFER_DURATION (buffer) = 250 * GST_MSECOND;
802   gst_buffer_set_caps (buffer, caps);
803   GST_DEBUG ("pushing buffer %p", buffer);
804   ret = gst_pad_chain (sinkpad, buffer);
805   fail_unless (ret == GST_FLOW_OK);
806   fail_unless (handoff_buffer == NULL);
807
808   /* should be partially clipped */
809   buffer = gst_buffer_new_and_alloc (44100);
810   GST_BUFFER_TIMESTAMP (buffer) = 900 * GST_MSECOND;
811   GST_BUFFER_DURATION (buffer) = 250 * GST_MSECOND;
812   gst_buffer_set_caps (buffer, caps);
813   GST_DEBUG ("pushing buffer %p", buffer);
814   ret = gst_pad_chain (sinkpad, buffer);
815   fail_unless (ret == GST_FLOW_OK);
816   fail_unless (handoff_buffer != NULL);
817   gst_buffer_replace (&handoff_buffer, NULL);
818
819   /* should not be clipped */
820   buffer = gst_buffer_new_and_alloc (44100);
821   GST_BUFFER_TIMESTAMP (buffer) = 1 * GST_SECOND;
822   GST_BUFFER_DURATION (buffer) = 250 * GST_MSECOND;
823   gst_buffer_set_caps (buffer, caps);
824   GST_DEBUG ("pushing buffer %p", buffer);
825   ret = gst_pad_chain (sinkpad, buffer);
826   fail_unless (ret == GST_FLOW_OK);
827   fail_unless (handoff_buffer != NULL);
828   gst_buffer_replace (&handoff_buffer, NULL);
829
830   /* should be clipped and ok */
831   buffer = gst_buffer_new_and_alloc (44100);
832   GST_BUFFER_TIMESTAMP (buffer) = 2 * GST_SECOND;
833   GST_BUFFER_DURATION (buffer) = 250 * GST_MSECOND;
834   gst_buffer_set_caps (buffer, caps);
835   GST_DEBUG ("pushing buffer %p", buffer);
836   ret = gst_pad_chain (sinkpad, buffer);
837   fail_unless (ret == GST_FLOW_OK);
838   fail_unless (handoff_buffer == NULL);
839
840
841 }
842
843 GST_END_TEST;
844
845 static Suite *
846 adder_suite (void)
847 {
848   Suite *s = suite_create ("adder");
849   TCase *tc_chain = tcase_create ("general");
850
851   suite_add_tcase (s, tc_chain);
852   tcase_add_test (tc_chain, test_event);
853   tcase_add_test (tc_chain, test_play_twice);
854   tcase_add_test (tc_chain, test_play_twice_then_add_and_play_again);
855   tcase_add_test (tc_chain, test_live_seeking);
856   tcase_add_test (tc_chain, test_add_pad);
857   tcase_add_test (tc_chain, test_remove_pad);
858   tcase_add_test (tc_chain, test_clip);
859
860   /* Use a longer timeout */
861 #ifdef HAVE_VALGRIND
862   if (RUNNING_ON_VALGRIND) {
863     tcase_set_timeout (tc_chain, 5 * 60);
864   } else
865 #endif
866   {
867     /* this is shorter than the default 60 seconds?! (tpm) */
868     /* tcase_set_timeout (tc_chain, 6); */
869   }
870
871   return s;
872 }
873
874 GST_CHECK_MAIN (adder);