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