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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
28 # include <valgrind/valgrind.h>
31 #include <gst/check/gstcheck.h>
32 #include <gst/check/gstconsistencychecker.h>
33 #include <gst/base/gstbasesrc.h>
34 #include <gst/audio/audio.h>
36 static GMainLoop *main_loop;
43 main_loop = g_main_loop_new (NULL, FALSE);
49 g_main_loop_unref (main_loop);
54 /* some test helpers */
57 setup_pipeline (GstElement * adder, gint num_srcs)
59 GstElement *pipeline, *src, *sink;
62 pipeline = gst_pipeline_new ("pipeline");
64 adder = gst_element_factory_make ("adder", "adder");
67 sink = gst_element_factory_make ("fakesink", "sink");
68 gst_bin_add_many (GST_BIN (pipeline), adder, sink, NULL);
69 gst_element_link (adder, sink);
71 for (i = 0; i < num_srcs; i++) {
72 src = gst_element_factory_make ("audiotestsrc", NULL);
73 g_object_set (src, "wave", 4, NULL); /* silence */
74 gst_bin_add (GST_BIN (pipeline), src);
75 gst_element_link (src, adder);
81 get_element_sink_pad_caps (GstElement * pipeline, const gchar * element_name)
87 sink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
88 pad = gst_element_get_static_pad (sink, "sink");
89 caps = gst_pad_get_current_caps (pad);
90 gst_object_unref (pad);
91 gst_object_unref (sink);
97 set_state_and_wait (GstElement * pipeline, GstState state)
99 GstStateChangeReturn state_res;
101 /* prepare paused/playing */
102 state_res = gst_element_set_state (pipeline, state);
103 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
105 /* wait for preroll */
106 state_res = gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
107 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
111 play_and_wait (GstElement * pipeline)
113 GstStateChangeReturn state_res;
115 state_res = gst_element_set_state (pipeline, GST_STATE_PLAYING);
116 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
118 GST_INFO ("running main loop");
119 g_main_loop_run (main_loop);
121 state_res = gst_element_set_state (pipeline, GST_STATE_NULL);
122 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
126 message_received (GstBus * bus, GstMessage * message, GstPipeline * bin)
128 GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
129 GST_MESSAGE_SRC (message), message);
131 switch (message->type) {
132 case GST_MESSAGE_EOS:
133 g_main_loop_quit (main_loop);
135 case GST_MESSAGE_WARNING:{
139 gst_message_parse_warning (message, &gerror, &debug);
140 gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
141 g_error_free (gerror);
145 case GST_MESSAGE_ERROR:{
149 gst_message_parse_error (message, &gerror, &debug);
150 gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
151 g_error_free (gerror);
153 g_main_loop_quit (main_loop);
162 new_buffer (gsize num_bytes, GstClockTime ts, GstClockTime dur)
164 GstBuffer *buffer = gst_buffer_new_and_alloc (num_bytes);
166 GST_BUFFER_TIMESTAMP (buffer) = ts;
167 GST_BUFFER_DURATION (buffer) = dur;
168 GST_DEBUG ("created buffer %p", buffer);
172 /* make sure downstream gets a CAPS event before buffers are sent */
173 GST_START_TEST (test_caps)
175 GstElement *pipeline;
179 pipeline = setup_pipeline (NULL, 1);
181 /* prepare playing */
182 set_state_and_wait (pipeline, GST_STATE_PAUSED);
184 /* check caps on fakesink */
185 caps = get_element_sink_pad_caps (pipeline, "sink");
186 fail_unless (caps != NULL);
187 gst_caps_unref (caps);
189 gst_element_set_state (pipeline, GST_STATE_NULL);
190 gst_object_unref (pipeline);
195 /* check that caps set on the property are honoured */
196 GST_START_TEST (test_filter_caps)
198 GstElement *pipeline, *adder;
199 GstCaps *filter_caps, *caps;
201 filter_caps = gst_caps_new_simple ("audio/x-raw",
202 "format", G_TYPE_STRING, GST_AUDIO_NE (F32),
203 "layout", G_TYPE_STRING, "interleaved",
204 "rate", G_TYPE_INT, 44100, "channels", G_TYPE_INT, 1, NULL);
207 adder = gst_element_factory_make ("adder", "adder");
208 g_object_set (adder, "caps", filter_caps, NULL);
209 pipeline = setup_pipeline (adder, 1);
211 /* prepare playing */
212 set_state_and_wait (pipeline, GST_STATE_PAUSED);
214 /* check caps on fakesink */
215 caps = get_element_sink_pad_caps (pipeline, "sink");
216 fail_unless (caps != NULL);
217 GST_INFO_OBJECT (pipeline, "received caps: %" GST_PTR_FORMAT, caps);
218 fail_unless (gst_caps_is_equal_fixed (caps, filter_caps));
219 gst_caps_unref (caps);
221 gst_element_set_state (pipeline, GST_STATE_NULL);
222 gst_object_unref (pipeline);
224 gst_caps_unref (filter_caps);
229 static GstFormat format = GST_FORMAT_UNDEFINED;
230 static gint64 position = -1;
233 test_event_message_received (GstBus * bus, GstMessage * message,
236 GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
237 GST_MESSAGE_SRC (message), message);
239 switch (message->type) {
240 case GST_MESSAGE_SEGMENT_DONE:
241 gst_message_parse_segment_done (message, &format, &position);
242 GST_INFO ("received segment_done : %" G_GINT64_FORMAT, position);
243 g_main_loop_quit (main_loop);
246 g_assert_not_reached ();
251 GST_START_TEST (test_event)
253 GstElement *bin, *src1, *src2, *adder, *sink;
255 GstEvent *seek_event;
257 GstPad *srcpad, *sinkpad;
258 GstStreamConsistency *chk_1, *chk_2, *chk_3;
260 GST_INFO ("preparing test");
263 bin = gst_pipeline_new ("pipeline");
264 bus = gst_element_get_bus (bin);
265 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
267 src1 = gst_element_factory_make ("audiotestsrc", "src1");
268 g_object_set (src1, "wave", 4, NULL); /* silence */
269 src2 = gst_element_factory_make ("audiotestsrc", "src2");
270 g_object_set (src2, "wave", 4, NULL); /* silence */
271 adder = gst_element_factory_make ("adder", "adder");
272 sink = gst_element_factory_make ("fakesink", "sink");
273 gst_bin_add_many (GST_BIN (bin), src1, src2, adder, sink, NULL);
275 res = gst_element_link (src1, adder);
276 fail_unless (res == TRUE, NULL);
277 res = gst_element_link (src2, adder);
278 fail_unless (res == TRUE, NULL);
279 res = gst_element_link (adder, sink);
280 fail_unless (res == TRUE, NULL);
282 srcpad = gst_element_get_static_pad (adder, "src");
283 chk_3 = gst_consistency_checker_new (srcpad);
284 gst_object_unref (srcpad);
286 /* create consistency checkers for the pads */
287 srcpad = gst_element_get_static_pad (src1, "src");
288 chk_1 = gst_consistency_checker_new (srcpad);
289 sinkpad = gst_pad_get_peer (srcpad);
290 gst_consistency_checker_add_pad (chk_3, sinkpad);
291 gst_object_unref (sinkpad);
292 gst_object_unref (srcpad);
294 srcpad = gst_element_get_static_pad (src2, "src");
295 chk_2 = gst_consistency_checker_new (srcpad);
296 sinkpad = gst_pad_get_peer (srcpad);
297 gst_consistency_checker_add_pad (chk_3, sinkpad);
298 gst_object_unref (sinkpad);
299 gst_object_unref (srcpad);
301 seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
302 GST_SEEK_FLAG_SEGMENT | GST_SEEK_FLAG_FLUSH,
303 GST_SEEK_TYPE_SET, (GstClockTime) 0,
304 GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);
306 format = GST_FORMAT_UNDEFINED;
309 g_signal_connect (bus, "message::segment-done",
310 (GCallback) test_event_message_received, bin);
311 g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
312 g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
313 g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
315 GST_INFO ("starting test");
317 /* prepare playing */
318 set_state_and_wait (bin, GST_STATE_PAUSED);
320 res = gst_element_send_event (bin, seek_event);
321 fail_unless (res == TRUE, NULL);
326 ck_assert_int_eq (position, 2 * GST_SECOND);
329 gst_consistency_checker_free (chk_1);
330 gst_consistency_checker_free (chk_2);
331 gst_consistency_checker_free (chk_3);
332 gst_bus_remove_signal_watch (bus);
333 gst_object_unref (bus);
334 gst_object_unref (bin);
339 static guint play_count = 0;
340 static GstEvent *play_seek_event = NULL;
343 test_play_twice_message_received (GstBus * bus, GstMessage * message,
347 GstStateChangeReturn state_res;
349 GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
350 GST_MESSAGE_SRC (message), message);
352 switch (message->type) {
353 case GST_MESSAGE_SEGMENT_DONE:
355 if (play_count == 1) {
356 state_res = gst_element_set_state (bin, GST_STATE_READY);
357 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
359 /* prepare playing again */
360 set_state_and_wait (bin, GST_STATE_PAUSED);
362 res = gst_element_send_event (bin, gst_event_ref (play_seek_event));
363 fail_unless (res == TRUE, NULL);
365 state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
366 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
368 g_main_loop_quit (main_loop);
372 g_assert_not_reached ();
378 GST_START_TEST (test_play_twice)
380 GstElement *bin, *adder;
384 GstStreamConsistency *consist;
386 GST_INFO ("preparing test");
389 adder = gst_element_factory_make ("adder", "adder");
390 bin = setup_pipeline (adder, 2);
391 bus = gst_element_get_bus (bin);
392 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
394 srcpad = gst_element_get_static_pad (adder, "src");
395 consist = gst_consistency_checker_new (srcpad);
396 gst_object_unref (srcpad);
398 play_seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
399 GST_SEEK_FLAG_SEGMENT | GST_SEEK_FLAG_FLUSH,
400 GST_SEEK_TYPE_SET, (GstClockTime) 0,
401 GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);
405 g_signal_connect (bus, "message::segment-done",
406 (GCallback) test_play_twice_message_received, bin);
407 g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
408 g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
409 g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
411 GST_INFO ("starting test");
413 /* prepare playing */
414 set_state_and_wait (bin, GST_STATE_PAUSED);
416 res = gst_element_send_event (bin, gst_event_ref (play_seek_event));
417 fail_unless (res == TRUE, NULL);
424 ck_assert_int_eq (play_count, 2);
427 gst_consistency_checker_free (consist);
428 gst_event_unref (play_seek_event);
429 gst_bus_remove_signal_watch (bus);
430 gst_object_unref (bus);
431 gst_object_unref (bin);
436 GST_START_TEST (test_play_twice_then_add_and_play_again)
438 GstElement *bin, *src, *adder;
441 GstStateChangeReturn state_res;
444 GstStreamConsistency *consist;
446 GST_INFO ("preparing test");
449 adder = gst_element_factory_make ("adder", "adder");
450 bin = setup_pipeline (adder, 2);
451 bus = gst_element_get_bus (bin);
452 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
454 srcpad = gst_element_get_static_pad (adder, "src");
455 consist = gst_consistency_checker_new (srcpad);
456 gst_object_unref (srcpad);
458 play_seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
459 GST_SEEK_FLAG_SEGMENT | GST_SEEK_FLAG_FLUSH,
460 GST_SEEK_TYPE_SET, (GstClockTime) 0,
461 GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);
463 g_signal_connect (bus, "message::segment-done",
464 (GCallback) test_play_twice_message_received, bin);
465 g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
466 g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
467 g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
470 for (i = 0; i < 2; i++) {
473 GST_INFO ("starting test-loop %d", i);
475 /* prepare playing */
476 set_state_and_wait (bin, GST_STATE_PAUSED);
478 res = gst_element_send_event (bin, gst_event_ref (play_seek_event));
479 fail_unless (res == TRUE, NULL);
486 ck_assert_int_eq (play_count, 2);
488 /* plug another source */
490 src = gst_element_factory_make ("audiotestsrc", NULL);
491 g_object_set (src, "wave", 4, NULL); /* silence */
492 gst_bin_add (GST_BIN (bin), src);
494 res = gst_element_link (src, adder);
495 fail_unless (res == TRUE, NULL);
498 gst_consistency_checker_reset (consist);
501 state_res = gst_element_set_state (bin, GST_STATE_NULL);
502 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
505 gst_event_unref (play_seek_event);
506 gst_consistency_checker_free (consist);
507 gst_bus_remove_signal_watch (bus);
508 gst_object_unref (bus);
509 gst_object_unref (bin);
514 /* test failing seeks on live-sources */
515 GST_START_TEST (test_live_seeking)
517 GstElement *bin, *src1 = NULL, *src2, *ac1, *ac2, *adder, *sink;
522 GstStreamConsistency *consist;
524 GST_INFO ("preparing test");
525 play_seek_event = NULL;
528 bin = gst_pipeline_new ("pipeline");
529 bus = gst_element_get_bus (bin);
530 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
532 src1 = gst_element_factory_make ("audiotestsrc", "src1");
533 g_object_set (src1, "wave", 4, "is-live", TRUE, NULL); /* silence */
535 ac1 = gst_element_factory_make ("audioconvert", "ac1");
536 src2 = gst_element_factory_make ("audiotestsrc", "src2");
537 g_object_set (src2, "wave", 4, NULL); /* silence */
538 ac2 = gst_element_factory_make ("audioconvert", "ac2");
539 adder = gst_element_factory_make ("adder", "adder");
540 sink = gst_element_factory_make ("fakesink", "sink");
541 gst_bin_add_many (GST_BIN (bin), src1, ac1, src2, ac2, adder, sink, NULL);
543 res = gst_element_link_many (src1, ac1, adder, NULL);
544 fail_unless (res == TRUE, NULL);
545 res = gst_element_link_many (src2, ac2, adder, NULL);
546 fail_unless (res == TRUE, NULL);
547 res = gst_element_link (adder, sink);
548 fail_unless (res == TRUE, NULL);
550 play_seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
552 GST_SEEK_TYPE_SET, (GstClockTime) 0,
553 GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);
555 g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
556 g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
557 g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
559 srcpad = gst_element_get_static_pad (adder, "src");
560 consist = gst_consistency_checker_new (srcpad);
561 gst_object_unref (srcpad);
563 GST_INFO ("starting test");
566 for (i = 0; i < 2; i++) {
568 GST_INFO ("starting test-loop %d", i);
570 /* prepare playing */
571 set_state_and_wait (bin, GST_STATE_PAUSED);
573 res = gst_element_send_event (bin, gst_event_ref (play_seek_event));
574 fail_unless (res == TRUE, NULL);
581 gst_consistency_checker_reset (consist);
585 GST_INFO ("cleaning up");
586 gst_consistency_checker_free (consist);
588 gst_event_unref (play_seek_event);
589 gst_bus_remove_signal_watch (bus);
590 gst_object_unref (bus);
591 gst_object_unref (bin);
596 /* check if adding pads work as expected */
597 GST_START_TEST (test_add_pad)
599 GstElement *bin, *src1, *src2, *adder, *sink;
603 GstStateChangeReturn state_res;
605 GST_INFO ("preparing test");
608 bin = gst_pipeline_new ("pipeline");
609 bus = gst_element_get_bus (bin);
610 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
612 src1 = gst_element_factory_make ("audiotestsrc", "src1");
613 g_object_set (src1, "num-buffers", 4, "wave", /* silence */ 4, NULL);
614 src2 = gst_element_factory_make ("audiotestsrc", "src2");
615 /* one buffer less, we connect with 1 buffer of delay */
616 g_object_set (src2, "num-buffers", 3, "wave", /* silence */ 4, NULL);
617 adder = gst_element_factory_make ("adder", "adder");
618 sink = gst_element_factory_make ("fakesink", "sink");
619 gst_bin_add_many (GST_BIN (bin), src1, adder, sink, NULL);
621 res = gst_element_link (src1, adder);
622 fail_unless (res == TRUE, NULL);
623 res = gst_element_link (adder, sink);
624 fail_unless (res == TRUE, NULL);
626 srcpad = gst_element_get_static_pad (adder, "src");
627 gst_object_unref (srcpad);
629 g_signal_connect (bus, "message::segment-done", (GCallback) message_received,
631 g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
632 g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
633 g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
635 GST_INFO ("starting test");
637 /* prepare playing */
638 set_state_and_wait (bin, GST_STATE_PAUSED);
640 /* add other element */
641 gst_bin_add_many (GST_BIN (bin), src2, NULL);
643 /* now link the second element */
644 res = gst_element_link (src2, adder);
645 fail_unless (res == TRUE, NULL);
647 /* set to PAUSED as well */
648 state_res = gst_element_set_state (src2, GST_STATE_PAUSED);
649 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
655 gst_bus_remove_signal_watch (bus);
656 gst_object_unref (bus);
657 gst_object_unref (bin);
662 /* check if removing pads work as expected */
663 GST_START_TEST (test_remove_pad)
665 GstElement *bin, *src, *adder, *sink;
667 GstPad *pad, *srcpad;
669 GstStateChangeReturn state_res;
671 GST_INFO ("preparing test");
674 bin = gst_pipeline_new ("pipeline");
675 bus = gst_element_get_bus (bin);
676 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
678 src = gst_element_factory_make ("audiotestsrc", "src");
679 g_object_set (src, "num-buffers", 4, "wave", 4, NULL);
680 adder = gst_element_factory_make ("adder", "adder");
681 sink = gst_element_factory_make ("fakesink", "sink");
682 gst_bin_add_many (GST_BIN (bin), src, adder, sink, NULL);
684 res = gst_element_link (src, adder);
685 fail_unless (res == TRUE, NULL);
686 res = gst_element_link (adder, sink);
687 fail_unless (res == TRUE, NULL);
689 /* create an unconnected sinkpad in adder */
690 pad = gst_element_get_request_pad (adder, "sink_%u");
691 fail_if (pad == NULL, NULL);
693 srcpad = gst_element_get_static_pad (adder, "src");
694 gst_object_unref (srcpad);
696 g_signal_connect (bus, "message::segment-done", (GCallback) message_received,
698 g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
699 g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
700 g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
702 GST_INFO ("starting test");
704 /* prepare playing, this will not preroll as adder is waiting
705 * on the unconnected sinkpad. */
706 state_res = gst_element_set_state (bin, GST_STATE_PAUSED);
707 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
709 /* wait for completion for one second, will return ASYNC */
710 state_res = gst_element_get_state (GST_ELEMENT (bin), NULL, NULL, GST_SECOND);
711 ck_assert_int_eq (state_res, GST_STATE_CHANGE_ASYNC);
713 /* get rid of the pad now, adder should stop waiting on it and
714 * continue the preroll */
715 gst_element_release_request_pad (adder, pad);
716 gst_object_unref (pad);
718 /* wait for completion, should work now */
720 gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
721 GST_CLOCK_TIME_NONE);
722 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
728 gst_bus_remove_signal_watch (bus);
729 gst_object_unref (G_OBJECT (bus));
730 gst_object_unref (G_OBJECT (bin));
736 static GstBuffer *handoff_buffer = NULL;
738 handoff_buffer_cb (GstElement * fakesink, GstBuffer * buffer, GstPad * pad,
741 GST_DEBUG ("got buffer %p", buffer);
742 gst_buffer_replace (&handoff_buffer, buffer);
745 /* check if clipping works as expected */
746 GST_START_TEST (test_clip)
749 GstElement *bin, *adder, *sink;
753 GstStateChangeReturn state_res;
759 GST_INFO ("preparing test");
762 bin = gst_pipeline_new ("pipeline");
763 bus = gst_element_get_bus (bin);
764 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
766 g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
767 g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
768 g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
770 /* just an adder and a fakesink */
771 adder = gst_element_factory_make ("adder", "adder");
772 sink = gst_element_factory_make ("fakesink", "sink");
773 g_object_set (sink, "signal-handoffs", TRUE, NULL);
774 g_signal_connect (sink, "handoff", (GCallback) handoff_buffer_cb, NULL);
775 gst_bin_add_many (GST_BIN (bin), adder, sink, NULL);
777 res = gst_element_link (adder, sink);
778 fail_unless (res == TRUE, NULL);
781 state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
782 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
784 /* create an unconnected sinkpad in adder, should also automatically activate
786 sinkpad = gst_element_get_request_pad (adder, "sink_%u");
787 fail_if (sinkpad == NULL, NULL);
789 gst_pad_send_event (sinkpad, gst_event_new_stream_start ("test"));
791 caps = gst_caps_new_simple ("audio/x-raw",
792 #if G_BYTE_ORDER == G_BIG_ENDIAN
793 "format", G_TYPE_STRING, "S16BE",
795 "format", G_TYPE_STRING, "S16LE",
797 "layout", G_TYPE_STRING, "interleaved",
798 "rate", G_TYPE_INT, 44100, "channels", G_TYPE_INT, 2, NULL);
800 gst_pad_set_caps (sinkpad, caps);
801 gst_caps_unref (caps);
803 /* send segment to adder */
804 gst_segment_init (&segment, GST_FORMAT_TIME);
805 segment.start = GST_SECOND;
806 segment.stop = 2 * GST_SECOND;
808 event = gst_event_new_segment (&segment);
809 gst_pad_send_event (sinkpad, event);
811 /* should be clipped and ok */
812 buffer = new_buffer (44100, 0, 250 * GST_MSECOND);
813 ret = gst_pad_chain (sinkpad, buffer);
814 ck_assert_int_eq (ret, GST_FLOW_OK);
815 fail_unless (handoff_buffer == NULL);
817 /* should be partially clipped */
818 buffer = new_buffer (44100, 900 * GST_MSECOND, 250 * GST_MSECOND);
819 ret = gst_pad_chain (sinkpad, buffer);
820 ck_assert_int_eq (ret, GST_FLOW_OK);
821 fail_unless (handoff_buffer != NULL);
822 gst_buffer_replace (&handoff_buffer, NULL);
824 /* should not be clipped */
825 buffer = new_buffer (44100, 1 * GST_SECOND, 250 * GST_MSECOND);
826 ret = gst_pad_chain (sinkpad, buffer);
827 ck_assert_int_eq (ret, GST_FLOW_OK);
828 fail_unless (handoff_buffer != NULL);
829 gst_buffer_replace (&handoff_buffer, NULL);
831 /* should be clipped and ok */
832 buffer = new_buffer (44100, 2 * GST_SECOND, 250 * GST_MSECOND);
833 ret = gst_pad_chain (sinkpad, buffer);
834 ck_assert_int_eq (ret, GST_FLOW_OK);
835 fail_unless (handoff_buffer == NULL);
837 gst_element_release_request_pad (adder, sinkpad);
838 gst_object_unref (sinkpad);
839 gst_element_set_state (bin, GST_STATE_NULL);
840 gst_bus_remove_signal_watch (bus);
841 gst_object_unref (bus);
842 gst_object_unref (bin);
847 GST_START_TEST (test_duration_is_max)
849 GstElement *bin, *src[3], *adder, *sink;
850 GstStateChangeReturn state_res;
851 GstFormat format = GST_FORMAT_TIME;
855 GST_INFO ("preparing test");
858 bin = gst_pipeline_new ("pipeline");
860 /* 3 sources, an adder and a fakesink */
861 src[0] = gst_element_factory_make ("audiotestsrc", NULL);
862 src[1] = gst_element_factory_make ("audiotestsrc", NULL);
863 src[2] = gst_element_factory_make ("audiotestsrc", NULL);
864 adder = gst_element_factory_make ("adder", "adder");
865 sink = gst_element_factory_make ("fakesink", "sink");
866 gst_bin_add_many (GST_BIN (bin), src[0], src[1], src[2], adder, sink, NULL);
868 gst_element_link (src[0], adder);
869 gst_element_link (src[1], adder);
870 gst_element_link (src[2], adder);
871 gst_element_link (adder, sink);
873 /* irks, duration is reset on basesrc */
874 state_res = gst_element_set_state (bin, GST_STATE_PAUSED);
875 fail_unless (state_res != GST_STATE_CHANGE_FAILURE, NULL);
877 /* set durations on src */
878 GST_BASE_SRC (src[0])->segment.duration = 1000;
879 GST_BASE_SRC (src[1])->segment.duration = 3000;
880 GST_BASE_SRC (src[2])->segment.duration = 2000;
883 set_state_and_wait (bin, GST_STATE_PLAYING);
885 res = gst_element_query_duration (GST_ELEMENT (bin), format, &duration);
886 fail_unless (res, NULL);
888 ck_assert_int_eq (duration, 3000);
890 gst_element_set_state (bin, GST_STATE_NULL);
891 gst_object_unref (bin);
896 GST_START_TEST (test_duration_unknown_overrides)
898 GstElement *bin, *src[3], *adder, *sink;
899 GstStateChangeReturn state_res;
900 GstFormat format = GST_FORMAT_TIME;
904 GST_INFO ("preparing test");
907 bin = gst_pipeline_new ("pipeline");
909 /* 3 sources, an adder and a fakesink */
910 src[0] = gst_element_factory_make ("audiotestsrc", NULL);
911 src[1] = gst_element_factory_make ("audiotestsrc", NULL);
912 src[2] = gst_element_factory_make ("audiotestsrc", NULL);
913 adder = gst_element_factory_make ("adder", "adder");
914 sink = gst_element_factory_make ("fakesink", "sink");
915 gst_bin_add_many (GST_BIN (bin), src[0], src[1], src[2], adder, sink, NULL);
917 gst_element_link (src[0], adder);
918 gst_element_link (src[1], adder);
919 gst_element_link (src[2], adder);
920 gst_element_link (adder, sink);
922 /* irks, duration is reset on basesrc */
923 state_res = gst_element_set_state (bin, GST_STATE_PAUSED);
924 fail_unless (state_res != GST_STATE_CHANGE_FAILURE, NULL);
926 /* set durations on src */
927 GST_BASE_SRC (src[0])->segment.duration = GST_CLOCK_TIME_NONE;
928 GST_BASE_SRC (src[1])->segment.duration = 3000;
929 GST_BASE_SRC (src[2])->segment.duration = 2000;
932 set_state_and_wait (bin, GST_STATE_PLAYING);
934 res = gst_element_query_duration (GST_ELEMENT (bin), format, &duration);
935 fail_unless (res, NULL);
937 ck_assert_int_eq (duration, GST_CLOCK_TIME_NONE);
939 gst_element_set_state (bin, GST_STATE_NULL);
940 gst_object_unref (bin);
946 static gboolean looped = FALSE;
949 loop_segment_done (GstBus * bus, GstMessage * message, GstElement * bin)
951 GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
952 GST_MESSAGE_SRC (message), message);
955 g_main_loop_quit (main_loop);
957 GstEvent *seek_event;
960 seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
961 GST_SEEK_FLAG_SEGMENT,
962 GST_SEEK_TYPE_SET, (GstClockTime) 0,
963 GST_SEEK_TYPE_SET, (GstClockTime) 1 * GST_SECOND);
965 res = gst_element_send_event (bin, seek_event);
966 fail_unless (res == TRUE, NULL);
971 GST_START_TEST (test_loop)
975 GstEvent *seek_event;
978 GST_INFO ("preparing test");
981 bin = setup_pipeline (NULL, 2);
982 bus = gst_element_get_bus (bin);
983 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
985 seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
986 GST_SEEK_FLAG_SEGMENT | GST_SEEK_FLAG_FLUSH,
987 GST_SEEK_TYPE_SET, (GstClockTime) 0,
988 GST_SEEK_TYPE_SET, (GstClockTime) 1 * GST_SECOND);
990 g_signal_connect (bus, "message::segment-done",
991 (GCallback) loop_segment_done, bin);
992 g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
993 g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
994 g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
996 GST_INFO ("starting test");
998 /* prepare playing */
999 set_state_and_wait (bin, GST_STATE_PAUSED);
1001 res = gst_element_send_event (bin, seek_event);
1002 fail_unless (res == TRUE, NULL);
1005 play_and_wait (bin);
1007 fail_unless (looped);
1010 gst_bus_remove_signal_watch (bus);
1011 gst_object_unref (bus);
1012 gst_object_unref (bin);
1018 GST_START_TEST (test_flush_start_flush_stop)
1020 GstPadTemplate *sink_template;
1021 GstPad *tmppad, *sinkpad1, *sinkpad2, *adder_src;
1022 GstElement *pipeline, *src1, *src2, *adder, *sink;
1024 GST_INFO ("preparing test");
1026 /* build pipeline */
1027 pipeline = gst_pipeline_new ("pipeline");
1028 src1 = gst_element_factory_make ("audiotestsrc", "src1");
1029 g_object_set (src1, "wave", 4, NULL); /* silence */
1030 src2 = gst_element_factory_make ("audiotestsrc", "src2");
1031 g_object_set (src2, "wave", 4, NULL); /* silence */
1032 adder = gst_element_factory_make ("adder", "adder");
1033 sink = gst_element_factory_make ("fakesink", "sink");
1034 gst_bin_add_many (GST_BIN (pipeline), src1, src2, adder, sink, NULL);
1037 gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS (adder),
1039 fail_unless (GST_IS_PAD_TEMPLATE (sink_template));
1040 sinkpad1 = gst_element_request_pad (adder, sink_template, NULL, NULL);
1041 tmppad = gst_element_get_static_pad (src1, "src");
1042 gst_pad_link (tmppad, sinkpad1);
1043 gst_object_unref (tmppad);
1045 sinkpad2 = gst_element_request_pad (adder, sink_template, NULL, NULL);
1046 tmppad = gst_element_get_static_pad (src2, "src");
1047 gst_pad_link (tmppad, sinkpad2);
1048 gst_object_unref (tmppad);
1050 gst_element_link (adder, sink);
1052 /* prepare playing */
1053 set_state_and_wait (bin, GST_STATE_PLAYING);
1055 adder_src = gst_element_get_static_pad (adder, "src");
1056 fail_if (GST_PAD_IS_FLUSHING (adder_src));
1057 gst_pad_send_event (sinkpad1, gst_event_new_flush_start ());
1058 fail_unless (GST_PAD_IS_FLUSHING (adder_src));
1059 gst_pad_send_event (sinkpad1, gst_event_new_flush_stop (TRUE));
1060 fail_if (GST_PAD_IS_FLUSHING (adder_src));
1061 gst_object_unref (adder_src);
1063 gst_element_release_request_pad (adder, sinkpad1);
1064 gst_object_unref (sinkpad1);
1065 gst_element_release_request_pad (adder, sinkpad2);
1066 gst_object_unref (sinkpad2);
1069 gst_element_set_state (pipeline, GST_STATE_NULL);
1070 gst_object_unref (pipeline);
1079 Suite *s = suite_create ("adder");
1080 TCase *tc_chain = tcase_create ("general");
1082 suite_add_tcase (s, tc_chain);
1083 tcase_add_test (tc_chain, test_caps);
1084 tcase_add_test (tc_chain, test_filter_caps);
1085 tcase_add_test (tc_chain, test_event);
1086 tcase_add_test (tc_chain, test_play_twice);
1087 tcase_add_test (tc_chain, test_play_twice_then_add_and_play_again);
1088 tcase_add_test (tc_chain, test_live_seeking);
1089 tcase_add_test (tc_chain, test_add_pad);
1090 tcase_add_test (tc_chain, test_remove_pad);
1091 tcase_add_test (tc_chain, test_clip);
1092 tcase_add_test (tc_chain, test_duration_is_max);
1093 tcase_add_test (tc_chain, test_duration_unknown_overrides);
1094 tcase_add_test (tc_chain, test_loop);
1095 /* This test is racy and occasionally fails in interesting ways
1096 * https://bugzilla.gnome.org/show_bug.cgi?id=708891
1097 * It's unlikely that it will ever be fixed for adder, works with audiomixer */
1099 tcase_add_test (tc_chain, test_flush_start_flush_stop);
1101 tcase_add_checked_fixture (tc_chain, test_setup, test_teardown);
1103 /* Use a longer timeout */
1104 #ifdef HAVE_VALGRIND
1105 if (RUNNING_ON_VALGRIND) {
1106 tcase_set_timeout (tc_chain, 5 * 60);
1110 /* this is shorter than the default 60 seconds?! (tpm) */
1111 /* tcase_set_timeout (tc_chain, 6); */
1117 GST_CHECK_MAIN (adder);