5 * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
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.
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.
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.
28 # include <valgrind/valgrind.h>
33 #include <gst/check/gstcheck.h>
34 #include <gst/check/gstconsistencychecker.h>
35 #include <gst/base/gstbasesrc.h>
37 static GMainLoop *main_loop;
39 /* make sure downstream gets a CAPS event before buffers are sent */
40 GST_START_TEST (test_caps)
42 GstElement *pipeline, *src, *adder, *sink;
43 GstStateChangeReturn state_res;
48 pipeline = gst_pipeline_new ("pipeline");
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);
56 fail_unless (gst_element_link_many (src, adder, sink, NULL));
59 state_res = gst_element_set_state (pipeline, GST_STATE_PAUSED);
60 fail_unless_equals_int (state_res, GST_STATE_CHANGE_ASYNC);
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);
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);
73 gst_element_set_state (pipeline, GST_STATE_NULL);
74 gst_object_unref (pipeline);
80 message_received (GstBus * bus, GstMessage * message, GstPipeline * bin)
82 GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
83 GST_MESSAGE_SRC (message), message);
85 switch (message->type) {
87 g_main_loop_quit (main_loop);
89 case GST_MESSAGE_WARNING:{
93 gst_message_parse_warning (message, &gerror, &debug);
94 gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
95 g_error_free (gerror);
99 case GST_MESSAGE_ERROR:{
103 gst_message_parse_error (message, &gerror, &debug);
104 gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
105 g_error_free (gerror);
107 g_main_loop_quit (main_loop);
116 static GstFormat format = GST_FORMAT_UNDEFINED;
117 static gint64 position = -1;
120 test_event_message_received (GstBus * bus, GstMessage * message,
123 GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
124 GST_MESSAGE_SRC (message), message);
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);
133 g_assert_not_reached ();
139 GST_START_TEST (test_event)
141 GstElement *bin, *src1, *src2, *adder, *sink;
143 GstEvent *seek_event;
144 GstStateChangeReturn state_res;
146 GstPad *srcpad, *sinkpad;
147 GstStreamConsistency *chk_1, *chk_2, *chk_3;
149 GST_INFO ("preparing test");
152 bin = gst_pipeline_new ("pipeline");
153 bus = gst_element_get_bus (bin);
154 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
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);
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);
171 srcpad = gst_element_get_static_pad (adder, "src");
172 chk_3 = gst_consistency_checker_new (srcpad);
173 gst_object_unref (srcpad);
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);
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);
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);
195 format = GST_FORMAT_UNDEFINED;
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);
205 GST_INFO ("starting test");
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);
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);
215 res = gst_element_send_event (bin, seek_event);
216 fail_unless (res == TRUE, NULL);
219 state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
220 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
222 GST_INFO ("running main loop");
223 g_main_loop_run (main_loop);
225 state_res = gst_element_set_state (bin, GST_STATE_NULL);
226 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
228 ck_assert_int_eq (position, 2 * GST_SECOND);
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);
241 static guint play_count = 0;
242 static GstEvent *play_seek_event = NULL;
245 test_play_twice_message_received (GstBus * bus, GstMessage * message,
249 GstStateChangeReturn state_res;
251 GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
252 GST_MESSAGE_SRC (message), message);
254 switch (message->type) {
255 case GST_MESSAGE_SEGMENT_DONE:
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);
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);
265 /* wait for completion */
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);
271 res = gst_element_send_event (GST_ELEMENT (bin),
272 gst_event_ref (play_seek_event));
273 fail_unless (res == TRUE, NULL);
276 gst_element_set_state (GST_ELEMENT (bin), GST_STATE_PLAYING);
277 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
279 g_main_loop_quit (main_loop);
283 g_assert_not_reached ();
289 GST_START_TEST (test_play_twice)
291 GstElement *bin, *src1, *src2, *adder, *sink;
294 GstStateChangeReturn state_res;
296 GstStreamConsistency *consist;
298 GST_INFO ("preparing test");
301 bin = gst_pipeline_new ("pipeline");
302 bus = gst_element_get_bus (bin);
303 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
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);
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);
320 srcpad = gst_element_get_static_pad (adder, "src");
321 consist = gst_consistency_checker_new (srcpad);
322 gst_object_unref (srcpad);
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);
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);
338 GST_INFO ("starting test");
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);
344 /* wait for completion */
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);
350 res = gst_element_send_event (bin, gst_event_ref (play_seek_event));
351 fail_unless (res == TRUE, NULL);
356 state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
357 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
359 g_main_loop_run (main_loop);
361 state_res = gst_element_set_state (bin, GST_STATE_NULL);
362 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
364 ck_assert_int_eq (play_count, 2);
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);
376 GST_START_TEST (test_play_twice_then_add_and_play_again)
378 GstElement *bin, *src1, *src2, *src3, *adder, *sink;
381 GstStateChangeReturn state_res;
384 GstStreamConsistency *consist;
386 GST_INFO ("preparing test");
389 bin = gst_pipeline_new ("pipeline");
390 bus = gst_element_get_bus (bin);
391 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
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);
401 srcpad = gst_element_get_static_pad (adder, "src");
402 consist = gst_consistency_checker_new (srcpad);
403 gst_object_unref (srcpad);
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);
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);
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);
425 for (i = 0; i < 2; i++) {
428 GST_INFO ("starting test-loop %d", i);
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);
434 /* wait for completion */
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);
440 res = gst_element_send_event (bin, gst_event_ref (play_seek_event));
441 fail_unless (res == TRUE, NULL);
446 state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
447 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
449 g_main_loop_run (main_loop);
451 state_res = gst_element_set_state (bin, GST_STATE_READY);
452 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
454 ck_assert_int_eq (play_count, 2);
456 /* plug another source */
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);
462 res = gst_element_link (src3, adder);
463 fail_unless (res == TRUE, NULL);
466 gst_consistency_checker_reset (consist);
469 state_res = gst_element_set_state (bin, GST_STATE_NULL);
470 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
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);
484 test_live_seeking_eos_message_received (GstBus * bus, GstMessage * message,
487 GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
488 GST_MESSAGE_SRC (message), message);
490 switch (message->type) {
491 case GST_MESSAGE_EOS:
492 g_main_loop_quit (main_loop);
495 g_assert_not_reached ();
501 /* test failing seeks on live-sources */
502 GST_START_TEST (test_live_seeking)
504 GstElement *bin, *src1, *src2, *ac1, *ac2, *adder, *sink;
507 GstStateChangeReturn state_res;
510 GstStreamConsistency *consist;
512 GST_INFO ("preparing test");
514 play_seek_event = NULL;
517 bin = gst_pipeline_new ("pipeline");
518 bus = gst_element_get_bus (bin);
519 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
521 /* normal audiosources behave differently than audiotestsrc */
523 src1 = gst_element_factory_make ("audiotestsrc", "src1");
524 g_object_set (src1, "wave", 4, "is-live", TRUE, NULL); /* silence */
526 src1 = gst_element_factory_make ("alsasrc", "src1");
528 GST_INFO ("no audiosrc, skipping");
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);
536 if (state_res == GST_STATE_CHANGE_FAILURE)
538 src1 = gst_element_factory_make ("alsasrc", "src1");
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);
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);
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);
563 play_seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
565 GST_SEEK_TYPE_SET, (GstClockTime) 0,
566 GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);
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);
574 srcpad = gst_element_get_static_pad (adder, "src");
575 consist = gst_consistency_checker_new (srcpad);
576 gst_object_unref (srcpad);
578 GST_INFO ("starting test");
581 for (i = 0; i < 2; i++) {
583 GST_INFO ("starting test-loop %d", i);
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);
589 /* wait for completion */
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);
595 res = gst_element_send_event (bin, gst_event_ref (play_seek_event));
597 fail_unless (res == TRUE, NULL);
599 /* adder is picky, if a single seek fails it totally fails */
600 fail_unless (res == FALSE, NULL);
606 state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
607 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
609 GST_INFO ("playing");
611 g_main_loop_run (main_loop);
613 state_res = gst_element_set_state (bin, GST_STATE_NULL);
614 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
616 gst_consistency_checker_reset (consist);
621 GST_INFO ("cleaning up");
623 g_main_loop_unref (main_loop);
625 gst_event_unref (play_seek_event);
626 gst_object_unref (bus);
627 gst_object_unref (bin);
632 /* check if adding pads work as expected */
633 GST_START_TEST (test_add_pad)
635 GstElement *bin, *src1, *src2, *adder, *sink;
639 GstStateChangeReturn state_res;
641 GST_INFO ("preparing test");
644 bin = gst_pipeline_new ("pipeline");
645 bus = gst_element_get_bus (bin);
646 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
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);
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);
664 srcpad = gst_element_get_static_pad (adder, "src");
665 gst_object_unref (srcpad);
667 main_loop = g_main_loop_new (NULL, FALSE);
668 g_signal_connect (bus, "message::segment-done", (GCallback) message_received,
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);
674 GST_INFO ("starting test");
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);
680 /* wait for completion */
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);
686 /* add other element */
687 gst_bin_add_many (GST_BIN (bin), src2, NULL);
689 /* now link the second element */
690 res = gst_element_link (src2, adder);
691 fail_unless (res == TRUE, NULL);
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);
698 state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
699 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
701 g_main_loop_run (main_loop);
703 state_res = gst_element_set_state (bin, GST_STATE_NULL);
704 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
707 g_main_loop_unref (main_loop);
708 gst_object_unref (bus);
709 gst_object_unref (bin);
714 /* check if removing pads work as expected */
715 GST_START_TEST (test_remove_pad)
717 GstElement *bin, *src, *adder, *sink;
719 GstPad *pad, *srcpad;
721 GstStateChangeReturn state_res;
723 GST_INFO ("preparing test");
726 bin = gst_pipeline_new ("pipeline");
727 bus = gst_element_get_bus (bin);
728 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
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);
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);
742 /* create an unconnected sinkpad in adder */
743 pad = gst_element_get_request_pad (adder, "sink_%u");
744 fail_if (pad == NULL, NULL);
746 srcpad = gst_element_get_static_pad (adder, "src");
747 gst_object_unref (srcpad);
749 main_loop = g_main_loop_new (NULL, FALSE);
750 g_signal_connect (bus, "message::segment-done", (GCallback) message_received,
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);
756 GST_INFO ("starting test");
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);
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);
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);
772 /* wait for completion, should work now */
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);
779 state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
780 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
782 g_main_loop_run (main_loop);
784 state_res = gst_element_set_state (bin, GST_STATE_NULL);
785 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
788 g_main_loop_unref (main_loop);
789 gst_object_unref (G_OBJECT (bus));
790 gst_object_unref (G_OBJECT (bin));
796 static GstBuffer *handoff_buffer = NULL;
798 handoff_buffer_cb (GstElement * fakesink, GstBuffer * buffer, GstPad * pad,
801 GST_DEBUG ("got buffer %p", buffer);
802 gst_buffer_replace (&handoff_buffer, buffer);
805 /* check if clipping works as expected */
806 GST_START_TEST (test_clip)
809 GstElement *bin, *adder, *sink;
813 GstStateChangeReturn state_res;
819 GST_INFO ("preparing test");
822 bin = gst_pipeline_new ("pipeline");
823 bus = gst_element_get_bus (bin);
824 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
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);
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);
837 res = gst_element_link (adder, sink);
838 fail_unless (res == TRUE, NULL);
841 state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
842 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
844 /* create an unconnected sinkpad in adder, should also automatically activate
846 sinkpad = gst_element_get_request_pad (adder, "sink_%u");
847 fail_if (sinkpad == NULL, NULL);
849 /* send segment to adder */
850 gst_segment_init (&segment, GST_FORMAT_TIME);
851 segment.start = GST_SECOND;
852 segment.stop = 2 * GST_SECOND;
854 event = gst_event_new_segment (&segment);
855 gst_pad_send_event (sinkpad, event);
857 caps = gst_caps_new_simple ("audio/x-raw",
858 #if G_BYTE_ORDER == G_BIG_ENDIAN
859 "format", G_TYPE_STRING, "S16BE",
861 "format", G_TYPE_STRING, "S16LE",
863 "layout", G_TYPE_STRING, "interleaved",
864 "rate", G_TYPE_INT, 44100, "channels", G_TYPE_INT, 2, NULL);
866 gst_pad_set_caps (sinkpad, caps);
867 gst_caps_unref (caps);
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);
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);
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);
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);
910 GST_START_TEST (test_duration_is_max)
912 GstElement *bin, *src[3], *adder, *sink;
913 GstStateChangeReturn state_res;
914 GstFormat format = GST_FORMAT_TIME;
918 GST_INFO ("preparing test");
921 bin = gst_pipeline_new ("pipeline");
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);
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);
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);
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;
946 state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
947 fail_unless (state_res != GST_STATE_CHANGE_FAILURE, NULL);
949 /* wait for completion */
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);
955 res = gst_element_query_duration (GST_ELEMENT (bin), format, &duration);
956 fail_unless (res, NULL);
958 ck_assert_int_eq (duration, 3000);
960 gst_element_set_state (bin, GST_STATE_NULL);
961 gst_object_unref (bin);
966 GST_START_TEST (test_duration_unknown_overrides)
968 GstElement *bin, *src[3], *adder, *sink;
969 GstStateChangeReturn state_res;
970 GstFormat format = GST_FORMAT_TIME;
974 GST_INFO ("preparing test");
977 bin = gst_pipeline_new ("pipeline");
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);
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);
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);
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;
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);
1005 /* wait for completion */
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);
1011 res = gst_element_query_duration (GST_ELEMENT (bin), format, &duration);
1012 fail_unless (res, NULL);
1014 ck_assert_int_eq (duration, GST_CLOCK_TIME_NONE);
1016 gst_element_set_state (bin, GST_STATE_NULL);
1017 gst_object_unref (bin);
1026 Suite *s = suite_create ("adder");
1027 TCase *tc_chain = tcase_create ("general");
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);
1041 /* Use a longer timeout */
1042 #ifdef HAVE_VALGRIND
1043 if (RUNNING_ON_VALGRIND) {
1044 tcase_set_timeout (tc_chain, 5 * 60);
1048 /* this is shorter than the default 60 seconds?! (tpm) */
1049 /* tcase_set_timeout (tc_chain, 6); */
1055 GST_CHECK_MAIN (adder);