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