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);
516 test_live_seeking_try_audiosrc (const gchar * factory_name)
519 GstStateChangeReturn state_res;
521 if (!(src = gst_element_factory_make (factory_name, NULL))) {
522 GST_INFO ("can't make '%s', skipping", factory_name);
526 /* Test that the audio source can get to ready, else skip */
527 state_res = gst_element_set_state (src, GST_STATE_READY);
528 gst_element_set_state (src, GST_STATE_NULL);
530 if (state_res == GST_STATE_CHANGE_FAILURE) {
531 GST_INFO_OBJECT (src, "can't go to ready, skipping");
532 gst_object_unref (src);
539 /* test failing seeks on live-sources */
540 GST_START_TEST (test_live_seeking)
542 GstElement *bin, *src1 = NULL, *src2, *ac1, *ac2, *adder, *sink;
547 GstStreamConsistency *consist;
548 /* don't use autoaudiosrc, as then we can't set anything here */
549 const gchar *audio_src_factories[] = {
554 GST_INFO ("preparing test");
555 play_seek_event = NULL;
558 bin = gst_pipeline_new ("pipeline");
559 bus = gst_element_get_bus (bin);
560 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
562 for (i = 0; (i < G_N_ELEMENTS (audio_src_factories) && src1 == NULL); i++) {
563 src1 = test_live_seeking_try_audiosrc (audio_src_factories[i]);
566 /* normal audiosources behave differently than audiotestsrc */
567 src1 = gst_element_factory_make ("audiotestsrc", "src1");
568 g_object_set (src1, "wave", 4, "is-live", TRUE, NULL); /* silence */
570 /* live sources ignore seeks, force eos after 2 sec (4 buffers half second
573 g_object_set (src1, "num-buffers", 4, "blocksize", 44100, NULL);
576 ac1 = gst_element_factory_make ("audioconvert", "ac1");
577 src2 = gst_element_factory_make ("audiotestsrc", "src2");
578 g_object_set (src2, "wave", 4, NULL); /* silence */
579 ac2 = gst_element_factory_make ("audioconvert", "ac2");
580 adder = gst_element_factory_make ("adder", "adder");
581 sink = gst_element_factory_make ("fakesink", "sink");
582 gst_bin_add_many (GST_BIN (bin), src1, ac1, src2, ac2, adder, sink, NULL);
584 res = gst_element_link_many (src1, ac1, adder, NULL);
585 fail_unless (res == TRUE, NULL);
586 res = gst_element_link_many (src2, ac2, adder, NULL);
587 fail_unless (res == TRUE, NULL);
588 res = gst_element_link (adder, sink);
589 fail_unless (res == TRUE, NULL);
591 play_seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
593 GST_SEEK_TYPE_SET, (GstClockTime) 0,
594 GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);
596 g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
597 g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
598 g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
600 srcpad = gst_element_get_static_pad (adder, "src");
601 consist = gst_consistency_checker_new (srcpad);
602 gst_object_unref (srcpad);
604 GST_INFO ("starting test");
607 for (i = 0; i < 2; i++) {
609 GST_INFO ("starting test-loop %d", i);
611 /* prepare playing */
612 set_state_and_wait (bin, GST_STATE_PAUSED);
614 res = gst_element_send_event (bin, gst_event_ref (play_seek_event));
615 fail_unless (res == TRUE, NULL);
622 gst_consistency_checker_reset (consist);
626 GST_INFO ("cleaning up");
627 gst_consistency_checker_free (consist);
629 gst_event_unref (play_seek_event);
630 gst_bus_remove_signal_watch (bus);
631 gst_object_unref (bus);
632 gst_object_unref (bin);
637 /* check if adding pads work as expected */
638 GST_START_TEST (test_add_pad)
640 GstElement *bin, *src1, *src2, *adder, *sink;
644 GstStateChangeReturn state_res;
646 GST_INFO ("preparing test");
649 bin = gst_pipeline_new ("pipeline");
650 bus = gst_element_get_bus (bin);
651 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
653 src1 = gst_element_factory_make ("audiotestsrc", "src1");
654 g_object_set (src1, "num-buffers", 4, "wave", /* silence */ 4, NULL);
655 src2 = gst_element_factory_make ("audiotestsrc", "src2");
656 /* one buffer less, we connect with 1 buffer of delay */
657 g_object_set (src2, "num-buffers", 3, "wave", /* silence */ 4, NULL);
658 adder = gst_element_factory_make ("adder", "adder");
659 sink = gst_element_factory_make ("fakesink", "sink");
660 gst_bin_add_many (GST_BIN (bin), src1, adder, sink, NULL);
662 res = gst_element_link (src1, adder);
663 fail_unless (res == TRUE, NULL);
664 res = gst_element_link (adder, sink);
665 fail_unless (res == TRUE, NULL);
667 srcpad = gst_element_get_static_pad (adder, "src");
668 gst_object_unref (srcpad);
670 g_signal_connect (bus, "message::segment-done", (GCallback) message_received,
672 g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
673 g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
674 g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
676 GST_INFO ("starting test");
678 /* prepare playing */
679 set_state_and_wait (bin, GST_STATE_PAUSED);
681 /* add other element */
682 gst_bin_add_many (GST_BIN (bin), src2, NULL);
684 /* now link the second element */
685 res = gst_element_link (src2, adder);
686 fail_unless (res == TRUE, NULL);
688 /* set to PAUSED as well */
689 state_res = gst_element_set_state (src2, GST_STATE_PAUSED);
690 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
696 gst_bus_remove_signal_watch (bus);
697 gst_object_unref (bus);
698 gst_object_unref (bin);
703 /* check if removing pads work as expected */
704 GST_START_TEST (test_remove_pad)
706 GstElement *bin, *src, *adder, *sink;
708 GstPad *pad, *srcpad;
710 GstStateChangeReturn state_res;
712 GST_INFO ("preparing test");
715 bin = gst_pipeline_new ("pipeline");
716 bus = gst_element_get_bus (bin);
717 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
719 src = gst_element_factory_make ("audiotestsrc", "src");
720 g_object_set (src, "num-buffers", 4, "wave", 4, NULL);
721 adder = gst_element_factory_make ("adder", "adder");
722 sink = gst_element_factory_make ("fakesink", "sink");
723 gst_bin_add_many (GST_BIN (bin), src, adder, sink, NULL);
725 res = gst_element_link (src, adder);
726 fail_unless (res == TRUE, NULL);
727 res = gst_element_link (adder, sink);
728 fail_unless (res == TRUE, NULL);
730 /* create an unconnected sinkpad in adder */
731 pad = gst_element_get_request_pad (adder, "sink_%u");
732 fail_if (pad == NULL, NULL);
734 srcpad = gst_element_get_static_pad (adder, "src");
735 gst_object_unref (srcpad);
737 g_signal_connect (bus, "message::segment-done", (GCallback) message_received,
739 g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
740 g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
741 g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
743 GST_INFO ("starting test");
745 /* prepare playing, this will not preroll as adder is waiting
746 * on the unconnected sinkpad. */
747 state_res = gst_element_set_state (bin, GST_STATE_PAUSED);
748 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
750 /* wait for completion for one second, will return ASYNC */
751 state_res = gst_element_get_state (GST_ELEMENT (bin), NULL, NULL, GST_SECOND);
752 ck_assert_int_eq (state_res, GST_STATE_CHANGE_ASYNC);
754 /* get rid of the pad now, adder should stop waiting on it and
755 * continue the preroll */
756 gst_element_release_request_pad (adder, pad);
757 gst_object_unref (pad);
759 /* wait for completion, should work now */
761 gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
762 GST_CLOCK_TIME_NONE);
763 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
769 gst_bus_remove_signal_watch (bus);
770 gst_object_unref (G_OBJECT (bus));
771 gst_object_unref (G_OBJECT (bin));
777 static GstBuffer *handoff_buffer = NULL;
779 handoff_buffer_cb (GstElement * fakesink, GstBuffer * buffer, GstPad * pad,
782 GST_DEBUG ("got buffer %p", buffer);
783 gst_buffer_replace (&handoff_buffer, buffer);
786 /* check if clipping works as expected */
787 GST_START_TEST (test_clip)
790 GstElement *bin, *adder, *sink;
794 GstStateChangeReturn state_res;
800 GST_INFO ("preparing test");
803 bin = gst_pipeline_new ("pipeline");
804 bus = gst_element_get_bus (bin);
805 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
807 g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
808 g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
809 g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
811 /* just an adder and a fakesink */
812 adder = gst_element_factory_make ("adder", "adder");
813 sink = gst_element_factory_make ("fakesink", "sink");
814 g_object_set (sink, "signal-handoffs", TRUE, NULL);
815 g_signal_connect (sink, "handoff", (GCallback) handoff_buffer_cb, NULL);
816 gst_bin_add_many (GST_BIN (bin), adder, sink, NULL);
818 res = gst_element_link (adder, sink);
819 fail_unless (res == TRUE, NULL);
822 state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
823 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
825 /* create an unconnected sinkpad in adder, should also automatically activate
827 sinkpad = gst_element_get_request_pad (adder, "sink_%u");
828 fail_if (sinkpad == NULL, NULL);
830 gst_pad_send_event (sinkpad, gst_event_new_stream_start ("test"));
832 caps = gst_caps_new_simple ("audio/x-raw",
833 #if G_BYTE_ORDER == G_BIG_ENDIAN
834 "format", G_TYPE_STRING, "S16BE",
836 "format", G_TYPE_STRING, "S16LE",
838 "layout", G_TYPE_STRING, "interleaved",
839 "rate", G_TYPE_INT, 44100, "channels", G_TYPE_INT, 2, NULL);
841 gst_pad_set_caps (sinkpad, caps);
842 gst_caps_unref (caps);
844 /* send segment to adder */
845 gst_segment_init (&segment, GST_FORMAT_TIME);
846 segment.start = GST_SECOND;
847 segment.stop = 2 * GST_SECOND;
849 event = gst_event_new_segment (&segment);
850 gst_pad_send_event (sinkpad, event);
852 /* should be clipped and ok */
853 buffer = new_buffer (44100, 0, 250 * GST_MSECOND);
854 ret = gst_pad_chain (sinkpad, buffer);
855 ck_assert_int_eq (ret, GST_FLOW_OK);
856 fail_unless (handoff_buffer == NULL);
858 /* should be partially clipped */
859 buffer = new_buffer (44100, 900 * GST_MSECOND, 250 * GST_MSECOND);
860 ret = gst_pad_chain (sinkpad, buffer);
861 ck_assert_int_eq (ret, GST_FLOW_OK);
862 fail_unless (handoff_buffer != NULL);
863 gst_buffer_replace (&handoff_buffer, NULL);
865 /* should not be clipped */
866 buffer = new_buffer (44100, 1 * GST_SECOND, 250 * GST_MSECOND);
867 ret = gst_pad_chain (sinkpad, buffer);
868 ck_assert_int_eq (ret, GST_FLOW_OK);
869 fail_unless (handoff_buffer != NULL);
870 gst_buffer_replace (&handoff_buffer, NULL);
872 /* should be clipped and ok */
873 buffer = new_buffer (44100, 2 * GST_SECOND, 250 * GST_MSECOND);
874 ret = gst_pad_chain (sinkpad, buffer);
875 ck_assert_int_eq (ret, GST_FLOW_OK);
876 fail_unless (handoff_buffer == NULL);
878 gst_element_release_request_pad (adder, sinkpad);
879 gst_object_unref (sinkpad);
880 gst_element_set_state (bin, GST_STATE_NULL);
881 gst_bus_remove_signal_watch (bus);
882 gst_object_unref (bus);
883 gst_object_unref (bin);
888 GST_START_TEST (test_duration_is_max)
890 GstElement *bin, *src[3], *adder, *sink;
891 GstStateChangeReturn state_res;
892 GstFormat format = GST_FORMAT_TIME;
896 GST_INFO ("preparing test");
899 bin = gst_pipeline_new ("pipeline");
901 /* 3 sources, an adder and a fakesink */
902 src[0] = gst_element_factory_make ("audiotestsrc", NULL);
903 src[1] = gst_element_factory_make ("audiotestsrc", NULL);
904 src[2] = gst_element_factory_make ("audiotestsrc", NULL);
905 adder = gst_element_factory_make ("adder", "adder");
906 sink = gst_element_factory_make ("fakesink", "sink");
907 gst_bin_add_many (GST_BIN (bin), src[0], src[1], src[2], adder, sink, NULL);
909 gst_element_link (src[0], adder);
910 gst_element_link (src[1], adder);
911 gst_element_link (src[2], adder);
912 gst_element_link (adder, sink);
914 /* irks, duration is reset on basesrc */
915 state_res = gst_element_set_state (bin, GST_STATE_PAUSED);
916 fail_unless (state_res != GST_STATE_CHANGE_FAILURE, NULL);
918 /* set durations on src */
919 GST_BASE_SRC (src[0])->segment.duration = 1000;
920 GST_BASE_SRC (src[1])->segment.duration = 3000;
921 GST_BASE_SRC (src[2])->segment.duration = 2000;
924 set_state_and_wait (bin, GST_STATE_PLAYING);
926 res = gst_element_query_duration (GST_ELEMENT (bin), format, &duration);
927 fail_unless (res, NULL);
929 ck_assert_int_eq (duration, 3000);
931 gst_element_set_state (bin, GST_STATE_NULL);
932 gst_object_unref (bin);
937 GST_START_TEST (test_duration_unknown_overrides)
939 GstElement *bin, *src[3], *adder, *sink;
940 GstStateChangeReturn state_res;
941 GstFormat format = GST_FORMAT_TIME;
945 GST_INFO ("preparing test");
948 bin = gst_pipeline_new ("pipeline");
950 /* 3 sources, an adder and a fakesink */
951 src[0] = gst_element_factory_make ("audiotestsrc", NULL);
952 src[1] = gst_element_factory_make ("audiotestsrc", NULL);
953 src[2] = gst_element_factory_make ("audiotestsrc", NULL);
954 adder = gst_element_factory_make ("adder", "adder");
955 sink = gst_element_factory_make ("fakesink", "sink");
956 gst_bin_add_many (GST_BIN (bin), src[0], src[1], src[2], adder, sink, NULL);
958 gst_element_link (src[0], adder);
959 gst_element_link (src[1], adder);
960 gst_element_link (src[2], adder);
961 gst_element_link (adder, sink);
963 /* irks, duration is reset on basesrc */
964 state_res = gst_element_set_state (bin, GST_STATE_PAUSED);
965 fail_unless (state_res != GST_STATE_CHANGE_FAILURE, NULL);
967 /* set durations on src */
968 GST_BASE_SRC (src[0])->segment.duration = GST_CLOCK_TIME_NONE;
969 GST_BASE_SRC (src[1])->segment.duration = 3000;
970 GST_BASE_SRC (src[2])->segment.duration = 2000;
973 set_state_and_wait (bin, GST_STATE_PLAYING);
975 res = gst_element_query_duration (GST_ELEMENT (bin), format, &duration);
976 fail_unless (res, NULL);
978 ck_assert_int_eq (duration, GST_CLOCK_TIME_NONE);
980 gst_element_set_state (bin, GST_STATE_NULL);
981 gst_object_unref (bin);
987 static gboolean looped = FALSE;
990 loop_segment_done (GstBus * bus, GstMessage * message, GstElement * bin)
992 GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
993 GST_MESSAGE_SRC (message), message);
996 g_main_loop_quit (main_loop);
998 GstEvent *seek_event;
1001 seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
1002 GST_SEEK_FLAG_SEGMENT,
1003 GST_SEEK_TYPE_SET, (GstClockTime) 0,
1004 GST_SEEK_TYPE_SET, (GstClockTime) 1 * GST_SECOND);
1006 res = gst_element_send_event (bin, seek_event);
1007 fail_unless (res == TRUE, NULL);
1012 GST_START_TEST (test_loop)
1016 GstEvent *seek_event;
1019 GST_INFO ("preparing test");
1021 /* build pipeline */
1022 bin = setup_pipeline (NULL, 2);
1023 bus = gst_element_get_bus (bin);
1024 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
1026 seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
1027 GST_SEEK_FLAG_SEGMENT | GST_SEEK_FLAG_FLUSH,
1028 GST_SEEK_TYPE_SET, (GstClockTime) 0,
1029 GST_SEEK_TYPE_SET, (GstClockTime) 1 * GST_SECOND);
1031 g_signal_connect (bus, "message::segment-done",
1032 (GCallback) loop_segment_done, bin);
1033 g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
1034 g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
1035 g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
1037 GST_INFO ("starting test");
1039 /* prepare playing */
1040 set_state_and_wait (bin, GST_STATE_PAUSED);
1042 res = gst_element_send_event (bin, seek_event);
1043 fail_unless (res == TRUE, NULL);
1046 play_and_wait (bin);
1048 fail_unless (looped);
1051 gst_bus_remove_signal_watch (bus);
1052 gst_object_unref (bus);
1053 gst_object_unref (bin);
1059 GST_START_TEST (test_flush_start_flush_stop)
1061 GstPadTemplate *sink_template;
1062 GstPad *tmppad, *sinkpad1, *sinkpad2, *adder_src;
1063 GstElement *pipeline, *src1, *src2, *adder, *sink;
1065 GST_INFO ("preparing test");
1067 /* build pipeline */
1068 pipeline = gst_pipeline_new ("pipeline");
1069 src1 = gst_element_factory_make ("audiotestsrc", "src1");
1070 g_object_set (src1, "wave", 4, NULL); /* silence */
1071 src2 = gst_element_factory_make ("audiotestsrc", "src2");
1072 g_object_set (src2, "wave", 4, NULL); /* silence */
1073 adder = gst_element_factory_make ("adder", "adder");
1074 sink = gst_element_factory_make ("fakesink", "sink");
1075 gst_bin_add_many (GST_BIN (pipeline), src1, src2, adder, sink, NULL);
1078 gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS (adder),
1080 fail_unless (GST_IS_PAD_TEMPLATE (sink_template));
1081 sinkpad1 = gst_element_request_pad (adder, sink_template, NULL, NULL);
1082 tmppad = gst_element_get_static_pad (src1, "src");
1083 gst_pad_link (tmppad, sinkpad1);
1084 gst_object_unref (tmppad);
1086 sinkpad2 = gst_element_request_pad (adder, sink_template, NULL, NULL);
1087 tmppad = gst_element_get_static_pad (src2, "src");
1088 gst_pad_link (tmppad, sinkpad2);
1089 gst_object_unref (tmppad);
1091 gst_element_link (adder, sink);
1093 /* prepare playing */
1094 set_state_and_wait (bin, GST_STATE_PLAYING);
1096 adder_src = gst_element_get_static_pad (adder, "src");
1097 fail_if (GST_PAD_IS_FLUSHING (adder_src));
1098 gst_pad_send_event (sinkpad1, gst_event_new_flush_start ());
1099 fail_unless (GST_PAD_IS_FLUSHING (adder_src));
1100 gst_pad_send_event (sinkpad1, gst_event_new_flush_stop (TRUE));
1101 fail_if (GST_PAD_IS_FLUSHING (adder_src));
1102 gst_object_unref (adder_src);
1104 gst_element_release_request_pad (adder, sinkpad1);
1105 gst_object_unref (sinkpad1);
1106 gst_element_release_request_pad (adder, sinkpad2);
1107 gst_object_unref (sinkpad2);
1110 gst_element_set_state (pipeline, GST_STATE_NULL);
1111 gst_object_unref (pipeline);
1120 Suite *s = suite_create ("adder");
1121 TCase *tc_chain = tcase_create ("general");
1123 suite_add_tcase (s, tc_chain);
1124 tcase_add_test (tc_chain, test_caps);
1125 tcase_add_test (tc_chain, test_filter_caps);
1126 tcase_add_test (tc_chain, test_event);
1127 tcase_add_test (tc_chain, test_play_twice);
1128 tcase_add_test (tc_chain, test_play_twice_then_add_and_play_again);
1129 tcase_add_test (tc_chain, test_live_seeking);
1130 tcase_add_test (tc_chain, test_add_pad);
1131 tcase_add_test (tc_chain, test_remove_pad);
1132 tcase_add_test (tc_chain, test_clip);
1133 tcase_add_test (tc_chain, test_duration_is_max);
1134 tcase_add_test (tc_chain, test_duration_unknown_overrides);
1135 tcase_add_test (tc_chain, test_loop);
1136 /* This test is racy and occasionally fails in interesting ways
1137 * https://bugzilla.gnome.org/show_bug.cgi?id=708891
1138 * It's unlikely that it will ever be fixed for adder, works with audiomixer */
1140 tcase_add_test (tc_chain, test_flush_start_flush_stop);
1142 tcase_add_checked_fixture (tc_chain, test_setup, test_teardown);
1144 /* Use a longer timeout */
1145 #ifdef HAVE_VALGRIND
1146 if (RUNNING_ON_VALGRIND) {
1147 tcase_set_timeout (tc_chain, 5 * 60);
1151 /* this is shorter than the default 60 seconds?! (tpm) */
1152 /* tcase_set_timeout (tc_chain, 6); */
1158 GST_CHECK_MAIN (adder);