3 * unit test for audiomixer
5 * Copyright (C) 2005 Thomas Vander Stichele <thomas at apestaart dot org>
6 * Copyright (C) 2013 Sebastian Dröge <sebastian@centricular.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
29 # include <valgrind/valgrind.h>
32 #include <gst/check/gstharness.h>
34 #include <gst/check/gstcheck.h>
35 #include <gst/check/gstconsistencychecker.h>
36 #include <gst/audio/audio.h>
37 #include <gst/base/gstbasesrc.h>
38 #include <gst/controller/gstdirectcontrolbinding.h>
39 #include <gst/controller/gstinterpolationcontrolsource.h>
41 static GMainLoop *main_loop;
48 main_loop = g_main_loop_new (NULL, FALSE);
54 g_main_loop_unref (main_loop);
59 /* some test helpers */
62 setup_pipeline (GstElement * audiomixer, gint num_srcs, GstElement * capsfilter)
64 GstElement *pipeline, *src, *sink;
67 pipeline = gst_pipeline_new ("pipeline");
69 audiomixer = gst_element_factory_make ("audiomixer", "audiomixer");
72 sink = gst_element_factory_make ("fakesink", "sink");
73 gst_bin_add_many (GST_BIN (pipeline), audiomixer, sink, NULL);
76 gst_bin_add (GST_BIN (pipeline), capsfilter);
77 gst_element_link_many (audiomixer, capsfilter, sink, NULL);
79 gst_element_link (audiomixer, sink);
82 for (i = 0; i < num_srcs; i++) {
83 src = gst_element_factory_make ("audiotestsrc", NULL);
84 g_object_set (src, "wave", 4, NULL); /* silence */
85 gst_bin_add (GST_BIN (pipeline), src);
86 gst_element_link (src, audiomixer);
92 get_element_sink_pad_caps (GstElement * pipeline, const gchar * element_name)
98 sink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
99 pad = gst_element_get_static_pad (sink, "sink");
100 caps = gst_pad_get_current_caps (pad);
101 gst_object_unref (pad);
102 gst_object_unref (sink);
108 set_state_and_wait (GstElement * pipeline, GstState state)
110 GstStateChangeReturn state_res;
112 /* prepare paused/playing */
113 state_res = gst_element_set_state (pipeline, state);
114 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
116 /* wait for preroll */
117 state_res = gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
118 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
122 set_playing (GstElement * element)
124 GstStateChangeReturn state_res;
126 state_res = gst_element_set_state (element, GST_STATE_PLAYING);
127 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
133 play_and_wait (GstElement * pipeline)
135 GstStateChangeReturn state_res;
137 g_idle_add ((GSourceFunc) set_playing, pipeline);
139 GST_INFO ("running main loop");
140 g_main_loop_run (main_loop);
142 state_res = gst_element_set_state (pipeline, GST_STATE_NULL);
143 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
147 message_received (GstBus * bus, GstMessage * message, GstPipeline * bin)
149 GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
150 GST_MESSAGE_SRC (message), message);
152 switch (message->type) {
153 case GST_MESSAGE_EOS:
154 g_main_loop_quit (main_loop);
156 case GST_MESSAGE_WARNING:{
160 gst_message_parse_warning (message, &gerror, &debug);
161 gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
162 g_error_free (gerror);
166 case GST_MESSAGE_ERROR:{
170 gst_message_parse_error (message, &gerror, &debug);
171 gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
172 g_error_free (gerror);
174 g_main_loop_quit (main_loop);
183 new_buffer (gsize num_bytes, gint data, GstClockTime ts, GstClockTime dur,
184 GstBufferFlags flags)
187 GstBuffer *buffer = gst_buffer_new_and_alloc (num_bytes);
189 gst_buffer_map (buffer, &map, GST_MAP_WRITE);
190 memset (map.data, data, map.size);
191 gst_buffer_unmap (buffer, &map);
192 GST_BUFFER_TIMESTAMP (buffer) = ts;
193 GST_BUFFER_DURATION (buffer) = dur;
195 GST_BUFFER_FLAG_SET (buffer, flags);
196 GST_DEBUG ("created buffer %p", buffer);
200 /* make sure downstream gets a CAPS event before buffers are sent */
201 GST_START_TEST (test_caps)
203 GstElement *pipeline;
207 pipeline = setup_pipeline (NULL, 1, NULL);
209 /* prepare playing */
210 set_state_and_wait (pipeline, GST_STATE_PAUSED);
212 /* check caps on fakesink */
213 caps = get_element_sink_pad_caps (pipeline, "sink");
214 fail_unless (caps != NULL);
215 gst_caps_unref (caps);
217 gst_element_set_state (pipeline, GST_STATE_NULL);
218 gst_object_unref (pipeline);
223 /* check that caps set on the property are honoured */
224 GST_START_TEST (test_filter_caps)
226 GstElement *pipeline, *audiomixer, *capsfilter;
227 GstCaps *filter_caps, *caps;
229 filter_caps = gst_caps_new_simple ("audio/x-raw",
230 "format", G_TYPE_STRING, GST_AUDIO_NE (F32),
231 "layout", G_TYPE_STRING, "interleaved",
232 "rate", G_TYPE_INT, 44100, "channels", G_TYPE_INT, 1,
233 "channel-mask", GST_TYPE_BITMASK, (guint64) 0x04, NULL);
235 capsfilter = gst_element_factory_make ("capsfilter", NULL);
238 audiomixer = gst_element_factory_make ("audiomixer", NULL);
239 g_object_set (capsfilter, "caps", filter_caps, NULL);
240 pipeline = setup_pipeline (audiomixer, 1, capsfilter);
242 /* prepare playing */
243 set_state_and_wait (pipeline, GST_STATE_PAUSED);
245 /* check caps on fakesink */
246 caps = get_element_sink_pad_caps (pipeline, "sink");
247 fail_unless (caps != NULL);
248 GST_INFO_OBJECT (pipeline, "received caps: %" GST_PTR_FORMAT, caps);
249 fail_unless (gst_caps_is_equal_fixed (caps, filter_caps));
250 gst_caps_unref (caps);
252 gst_element_set_state (pipeline, GST_STATE_NULL);
253 gst_object_unref (pipeline);
255 gst_caps_unref (filter_caps);
260 static GstFormat format = GST_FORMAT_UNDEFINED;
261 static gint64 position = -1;
264 test_event_message_received (GstBus * bus, GstMessage * message,
267 GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
268 GST_MESSAGE_SRC (message), message);
270 switch (message->type) {
271 case GST_MESSAGE_SEGMENT_DONE:
272 gst_message_parse_segment_done (message, &format, &position);
273 GST_INFO ("received segment_done : %" G_GINT64_FORMAT, position);
274 g_main_loop_quit (main_loop);
277 g_assert_not_reached ();
283 GST_START_TEST (test_event)
285 GstElement *bin, *src1, *src2, *audiomixer, *sink;
287 GstEvent *seek_event;
289 GstPad *srcpad, *sinkpad;
290 GstStreamConsistency *chk_1, *chk_2, *chk_3;
292 GST_INFO ("preparing test");
295 bin = gst_pipeline_new ("pipeline");
296 bus = gst_element_get_bus (bin);
297 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
299 src1 = gst_element_factory_make ("audiotestsrc", "src1");
300 g_object_set (src1, "wave", 4, NULL); /* silence */
301 src2 = gst_element_factory_make ("audiotestsrc", "src2");
302 g_object_set (src2, "wave", 4, NULL); /* silence */
303 audiomixer = gst_element_factory_make ("audiomixer", "audiomixer");
304 sink = gst_element_factory_make ("fakesink", "sink");
305 gst_bin_add_many (GST_BIN (bin), src1, src2, audiomixer, sink, NULL);
307 res = gst_element_link (src1, audiomixer);
308 fail_unless (res == TRUE, NULL);
309 res = gst_element_link (src2, audiomixer);
310 fail_unless (res == TRUE, NULL);
311 res = gst_element_link (audiomixer, sink);
312 fail_unless (res == TRUE, NULL);
314 srcpad = gst_element_get_static_pad (audiomixer, "src");
315 chk_3 = gst_consistency_checker_new (srcpad);
316 gst_object_unref (srcpad);
318 /* create consistency checkers for the pads */
319 srcpad = gst_element_get_static_pad (src1, "src");
320 chk_1 = gst_consistency_checker_new (srcpad);
321 sinkpad = gst_pad_get_peer (srcpad);
322 gst_consistency_checker_add_pad (chk_3, sinkpad);
323 gst_object_unref (sinkpad);
324 gst_object_unref (srcpad);
326 srcpad = gst_element_get_static_pad (src2, "src");
327 chk_2 = gst_consistency_checker_new (srcpad);
328 sinkpad = gst_pad_get_peer (srcpad);
329 gst_consistency_checker_add_pad (chk_3, sinkpad);
330 gst_object_unref (sinkpad);
331 gst_object_unref (srcpad);
333 seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
334 GST_SEEK_FLAG_SEGMENT | GST_SEEK_FLAG_FLUSH,
335 GST_SEEK_TYPE_SET, (GstClockTime) 0,
336 GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);
338 format = GST_FORMAT_UNDEFINED;
341 g_signal_connect (bus, "message::segment-done",
342 (GCallback) test_event_message_received, bin);
343 g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
344 g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
345 g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
347 GST_INFO ("starting test");
349 /* prepare playing */
350 set_state_and_wait (bin, GST_STATE_PAUSED);
352 res = gst_element_send_event (bin, seek_event);
353 fail_unless (res == TRUE, NULL);
358 ck_assert_int_eq (position, 2 * GST_SECOND);
361 gst_consistency_checker_free (chk_1);
362 gst_consistency_checker_free (chk_2);
363 gst_consistency_checker_free (chk_3);
364 gst_bus_remove_signal_watch (bus);
365 gst_object_unref (bus);
366 gst_object_unref (bin);
371 static guint play_count = 0;
372 static GstEvent *play_seek_event = NULL;
375 test_play_twice_message_received (GstBus * bus, GstMessage * message,
379 GstStateChangeReturn state_res;
381 GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
382 GST_MESSAGE_SRC (message), message);
384 switch (message->type) {
385 case GST_MESSAGE_SEGMENT_DONE:
387 if (play_count == 1) {
388 state_res = gst_element_set_state (bin, GST_STATE_READY);
389 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
391 /* prepare playing again */
392 set_state_and_wait (bin, GST_STATE_PAUSED);
394 gst_event_set_seqnum (play_seek_event, gst_util_seqnum_next ());
395 res = gst_element_send_event (bin, gst_event_ref (play_seek_event));
396 fail_unless (res == TRUE, NULL);
398 state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
399 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
401 g_main_loop_quit (main_loop);
405 g_assert_not_reached ();
411 GST_START_TEST (test_play_twice)
413 GstElement *bin, *audiomixer;
417 GstStreamConsistency *consist;
419 GST_INFO ("preparing test");
422 audiomixer = gst_element_factory_make ("audiomixer", "audiomixer");
423 bin = setup_pipeline (audiomixer, 2, NULL);
424 bus = gst_element_get_bus (bin);
425 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
427 srcpad = gst_element_get_static_pad (audiomixer, "src");
428 consist = gst_consistency_checker_new (srcpad);
429 gst_object_unref (srcpad);
431 play_seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
432 GST_SEEK_FLAG_SEGMENT | GST_SEEK_FLAG_FLUSH,
433 GST_SEEK_TYPE_SET, (GstClockTime) 0,
434 GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);
438 g_signal_connect (bus, "message::segment-done",
439 (GCallback) test_play_twice_message_received, bin);
440 g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
441 g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
442 g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
444 GST_INFO ("starting test");
446 /* prepare playing */
447 set_state_and_wait (bin, GST_STATE_PAUSED);
449 gst_event_set_seqnum (play_seek_event, gst_util_seqnum_next ());
450 res = gst_element_send_event (bin, gst_event_ref (play_seek_event));
451 fail_unless (res == TRUE, NULL);
458 ck_assert_int_eq (play_count, 2);
461 gst_consistency_checker_free (consist);
462 gst_event_unref (play_seek_event);
463 gst_bus_remove_signal_watch (bus);
464 gst_object_unref (bus);
465 gst_object_unref (bin);
470 GST_START_TEST (test_play_twice_then_add_and_play_again)
472 GstElement *bin, *src, *audiomixer;
475 GstStateChangeReturn state_res;
478 GstStreamConsistency *consist;
480 GST_INFO ("preparing test");
483 audiomixer = gst_element_factory_make ("audiomixer", "audiomixer");
484 bin = setup_pipeline (audiomixer, 2, NULL);
485 bus = gst_element_get_bus (bin);
486 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
488 srcpad = gst_element_get_static_pad (audiomixer, "src");
489 consist = gst_consistency_checker_new (srcpad);
490 gst_object_unref (srcpad);
492 play_seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
493 GST_SEEK_FLAG_SEGMENT | GST_SEEK_FLAG_FLUSH,
494 GST_SEEK_TYPE_SET, (GstClockTime) 0,
495 GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);
497 g_signal_connect (bus, "message::segment-done",
498 (GCallback) test_play_twice_message_received, bin);
499 g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
500 g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
501 g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
504 for (i = 0; i < 2; i++) {
507 GST_INFO ("starting test-loop %d", i);
509 /* prepare playing */
510 set_state_and_wait (bin, GST_STATE_PAUSED);
512 gst_event_set_seqnum (play_seek_event, gst_util_seqnum_next ());
513 res = gst_element_send_event (bin, gst_event_ref (play_seek_event));
514 fail_unless (res == TRUE, NULL);
521 ck_assert_int_eq (play_count, 2);
523 /* plug another source */
525 src = gst_element_factory_make ("audiotestsrc", NULL);
526 g_object_set (src, "wave", 4, NULL); /* silence */
527 gst_bin_add (GST_BIN (bin), src);
529 res = gst_element_link (src, audiomixer);
530 fail_unless (res == TRUE, NULL);
533 gst_consistency_checker_reset (consist);
536 state_res = gst_element_set_state (bin, GST_STATE_NULL);
537 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
540 gst_event_unref (play_seek_event);
541 gst_consistency_checker_free (consist);
542 gst_bus_remove_signal_watch (bus);
543 gst_object_unref (bus);
544 gst_object_unref (bin);
549 /* test failing seeks on live-sources */
550 GST_START_TEST (test_live_seeking)
552 GstElement *bin, *src1 = NULL, *cf, *src2, *audiomixer, *sink;
559 GstStreamConsistency *consist;
561 GST_INFO ("preparing test");
562 play_seek_event = NULL;
565 bin = gst_pipeline_new ("pipeline");
566 bus = gst_element_get_bus (bin);
567 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
569 src1 = gst_element_factory_make ("audiotestsrc", "src1");
570 g_object_set (src1, "wave", 4, "is-live", TRUE, NULL); /* silence */
572 audiomixer = gst_element_factory_make ("audiomixer", "audiomixer");
573 cf = gst_element_factory_make ("capsfilter", "capsfilter");
574 sink = gst_element_factory_make ("fakesink", "sink");
576 gst_bin_add_many (GST_BIN (bin), src1, cf, audiomixer, sink, NULL);
577 res = gst_element_link_many (src1, cf, audiomixer, sink, NULL);
578 fail_unless (res == TRUE, NULL);
580 /* get the caps for the livesrc, we'll reuse this for the non-live source */
581 set_state_and_wait (bin, GST_STATE_PLAYING);
583 sinkpad = gst_element_get_static_pad (sink, "sink");
584 fail_unless (sinkpad != NULL);
585 caps = gst_pad_get_current_caps (sinkpad);
586 fail_unless (caps != NULL);
587 gst_object_unref (sinkpad);
589 gst_element_set_state (bin, GST_STATE_NULL);
591 g_object_set (cf, "caps", caps, NULL);
593 src2 = gst_element_factory_make ("audiotestsrc", "src2");
594 g_object_set (src2, "wave", 4, NULL); /* silence */
595 gst_bin_add (GST_BIN (bin), src2);
597 res = gst_element_link_filtered (src2, audiomixer, caps);
598 fail_unless (res == TRUE, NULL);
600 gst_caps_unref (caps);
602 play_seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
604 GST_SEEK_TYPE_SET, (GstClockTime) 0,
605 GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);
607 g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
608 g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
609 g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
611 srcpad = gst_element_get_static_pad (audiomixer, "src");
612 consist = gst_consistency_checker_new (srcpad);
613 gst_object_unref (srcpad);
615 GST_INFO ("starting test");
618 for (i = 0; i < 2; i++) {
620 GST_INFO ("starting test-loop %d", i);
622 /* prepare playing */
623 set_state_and_wait (bin, GST_STATE_PAUSED);
625 gst_event_set_seqnum (play_seek_event, gst_util_seqnum_next ());
626 res = gst_element_send_event (bin, gst_event_ref (play_seek_event));
627 fail_unless (res == TRUE, NULL);
634 gst_consistency_checker_reset (consist);
638 GST_INFO ("cleaning up");
639 gst_consistency_checker_free (consist);
641 gst_event_unref (play_seek_event);
642 gst_bus_remove_signal_watch (bus);
643 gst_object_unref (bus);
644 gst_object_unref (bin);
649 /* check if adding pads work as expected */
650 GST_START_TEST (test_add_pad)
652 GstElement *bin, *src1, *src2, *audiomixer, *sink;
656 GstStateChangeReturn state_res;
658 GST_INFO ("preparing test");
661 bin = gst_pipeline_new ("pipeline");
662 bus = gst_element_get_bus (bin);
663 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
665 src1 = gst_element_factory_make ("audiotestsrc", "src1");
666 g_object_set (src1, "num-buffers", 4, "wave", /* silence */ 4, NULL);
667 src2 = gst_element_factory_make ("audiotestsrc", "src2");
668 /* one buffer less, we connect with 1 buffer of delay */
669 g_object_set (src2, "num-buffers", 3, "wave", /* silence */ 4, NULL);
670 audiomixer = gst_element_factory_make ("audiomixer", "audiomixer");
671 sink = gst_element_factory_make ("fakesink", "sink");
672 gst_bin_add_many (GST_BIN (bin), src1, audiomixer, sink, NULL);
674 res = gst_element_link (src1, audiomixer);
675 fail_unless (res == TRUE, NULL);
676 res = gst_element_link (audiomixer, sink);
677 fail_unless (res == TRUE, NULL);
679 srcpad = gst_element_get_static_pad (audiomixer, "src");
680 gst_object_unref (srcpad);
682 g_signal_connect (bus, "message::segment-done", (GCallback) message_received,
684 g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
685 g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
686 g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
688 GST_INFO ("starting test");
690 /* prepare playing */
691 set_state_and_wait (bin, GST_STATE_PAUSED);
693 /* add other element */
694 gst_bin_add_many (GST_BIN (bin), src2, NULL);
696 /* now link the second element */
697 res = gst_element_link (src2, audiomixer);
698 fail_unless (res == TRUE, NULL);
700 /* set to PAUSED as well */
701 state_res = gst_element_set_state (src2, GST_STATE_PAUSED);
702 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
708 gst_bus_remove_signal_watch (bus);
709 gst_object_unref (bus);
710 gst_object_unref (bin);
715 /* check if removing pads work as expected */
716 GST_START_TEST (test_remove_pad)
718 GstElement *bin, *src, *audiomixer, *sink;
720 GstPad *pad, *srcpad;
722 GstStateChangeReturn state_res;
724 GST_INFO ("preparing test");
727 bin = gst_pipeline_new ("pipeline");
728 bus = gst_element_get_bus (bin);
729 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
731 src = gst_element_factory_make ("audiotestsrc", "src");
732 g_object_set (src, "num-buffers", 4, "wave", 4, NULL);
733 audiomixer = gst_element_factory_make ("audiomixer", "audiomixer");
734 sink = gst_element_factory_make ("fakesink", "sink");
735 gst_bin_add_many (GST_BIN (bin), src, audiomixer, sink, NULL);
737 res = gst_element_link (src, audiomixer);
738 fail_unless (res == TRUE, NULL);
739 res = gst_element_link (audiomixer, sink);
740 fail_unless (res == TRUE, NULL);
742 /* create an unconnected sinkpad in audiomixer */
743 pad = gst_element_request_pad_simple (audiomixer, "sink_%u");
744 fail_if (pad == NULL, NULL);
746 srcpad = gst_element_get_static_pad (audiomixer, "src");
747 gst_object_unref (srcpad);
749 g_signal_connect (bus, "message::segment-done", (GCallback) message_received,
751 g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
752 g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
753 g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
755 GST_INFO ("starting test");
757 /* prepare playing, this will not preroll as audiomixer is waiting
758 * on the unconnected sinkpad. */
759 state_res = gst_element_set_state (bin, GST_STATE_PAUSED);
760 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
762 /* wait for completion for one second, will return ASYNC */
763 state_res = gst_element_get_state (GST_ELEMENT (bin), NULL, NULL, GST_SECOND);
764 ck_assert_int_eq (state_res, GST_STATE_CHANGE_ASYNC);
766 /* get rid of the pad now, audiomixer should stop waiting on it and
767 * continue the preroll */
768 gst_element_release_request_pad (audiomixer, pad);
769 gst_object_unref (pad);
771 /* wait for completion, should work now */
773 gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
774 GST_CLOCK_TIME_NONE);
775 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
781 gst_bus_remove_signal_watch (bus);
782 gst_object_unref (G_OBJECT (bus));
783 gst_object_unref (G_OBJECT (bin));
789 static GstBuffer *handoff_buffer = NULL;
792 handoff_buffer_cb (GstElement * fakesink, GstBuffer * buffer, GstPad * pad,
795 GST_DEBUG ("got buffer -- SIZE: %" G_GSIZE_FORMAT
796 " -- %p PTS is %" GST_TIME_FORMAT " END is %" GST_TIME_FORMAT,
797 gst_buffer_get_size (buffer), buffer,
798 GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
799 GST_TIME_ARGS (GST_BUFFER_PTS (buffer) + GST_BUFFER_DURATION (buffer)));
801 gst_buffer_replace (&handoff_buffer, buffer);
804 /* check if clipping works as expected */
805 GST_START_TEST (test_clip)
808 GstElement *bin, *audiomixer, *sink;
812 GstStateChangeReturn state_res;
817 GstQuery *drain = gst_query_new_drain ();
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 audiomixer and a fakesink */
831 audiomixer = gst_element_factory_make ("audiomixer", "audiomixer");
832 g_object_set (audiomixer, "output-buffer-duration", 50 * GST_MSECOND, NULL);
833 sink = gst_element_factory_make ("fakesink", "sink");
834 g_object_set (sink, "signal-handoffs", TRUE, NULL);
835 g_signal_connect (sink, "handoff", (GCallback) handoff_buffer_cb, NULL);
836 gst_bin_add_many (GST_BIN (bin), audiomixer, sink, NULL);
838 res = gst_element_link (audiomixer, sink);
839 fail_unless (res == TRUE, NULL);
842 state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
843 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
845 /* create an unconnected sinkpad in audiomixer, should also automatically activate
847 sinkpad = gst_element_request_pad_simple (audiomixer, "sink_%u");
848 fail_if (sinkpad == NULL, NULL);
850 gst_pad_send_event (sinkpad, gst_event_new_stream_start ("test"));
852 caps = gst_caps_new_simple ("audio/x-raw",
853 "format", G_TYPE_STRING, GST_AUDIO_NE (S16),
854 "layout", G_TYPE_STRING, "interleaved",
855 "rate", G_TYPE_INT, 44100, "channels", G_TYPE_INT, 2, NULL);
857 gst_pad_set_caps (sinkpad, caps);
858 gst_caps_unref (caps);
860 /* send segment to audiomixer */
861 gst_segment_init (&segment, GST_FORMAT_TIME);
862 segment.start = GST_SECOND;
863 segment.stop = 2 * GST_SECOND;
865 event = gst_event_new_segment (&segment);
866 gst_pad_send_event (sinkpad, event);
868 /* should be clipped and ok */
869 buffer = new_buffer (44100, 0, 0, 250 * GST_MSECOND, 0);
870 GST_DEBUG ("pushing buffer %p END is %" GST_TIME_FORMAT,
872 GST_TIME_ARGS (GST_BUFFER_PTS (buffer) + GST_BUFFER_DURATION (buffer)));
873 ret = gst_pad_chain (sinkpad, buffer);
874 ck_assert_int_eq (ret, GST_FLOW_OK);
875 /* The aggregation is done in a dedicated thread, so we can't
876 * know when it is actually going to happen, so we use a DRAIN query
877 * to wait for it to complete.
879 gst_pad_query (sinkpad, drain);
880 fail_unless (handoff_buffer == NULL);
882 /* should be partially clipped */
883 buffer = new_buffer (44100, 0, 900 * GST_MSECOND, 250 * GST_MSECOND,
884 GST_BUFFER_FLAG_DISCONT);
885 GST_DEBUG ("pushing buffer %p START %" GST_TIME_FORMAT " -- DURATION is %"
886 GST_TIME_FORMAT, buffer, GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
887 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
888 ret = gst_pad_chain (sinkpad, buffer);
889 ck_assert_int_eq (ret, GST_FLOW_OK);
890 gst_pad_query (sinkpad, drain);
892 fail_unless (handoff_buffer != NULL);
893 ck_assert_int_eq (GST_BUFFER_PTS (handoff_buffer) +
894 GST_BUFFER_DURATION (handoff_buffer), 150 * GST_MSECOND);
895 gst_buffer_replace (&handoff_buffer, NULL);
897 /* should not be clipped */
898 buffer = new_buffer (44100, 0, 1150 * GST_MSECOND, 250 * GST_MSECOND, 0);
899 GST_DEBUG ("pushing buffer %p END is %" GST_TIME_FORMAT,
901 GST_TIME_ARGS (GST_BUFFER_PTS (buffer) + GST_BUFFER_DURATION (buffer)));
902 ret = gst_pad_chain (sinkpad, buffer);
903 ck_assert_int_eq (ret, GST_FLOW_OK);
904 gst_pad_query (sinkpad, drain);
905 fail_unless (handoff_buffer != NULL);
906 ck_assert_int_eq (GST_BUFFER_PTS (handoff_buffer) +
907 GST_BUFFER_DURATION (handoff_buffer), 400 * GST_MSECOND);
908 gst_buffer_replace (&handoff_buffer, NULL);
909 fail_unless (handoff_buffer == NULL);
911 /* should be clipped and ok */
912 buffer = new_buffer (44100, 0, 2 * GST_SECOND, 250 * GST_MSECOND,
913 GST_BUFFER_FLAG_DISCONT);
914 GST_DEBUG ("pushing buffer %p PTS is %" GST_TIME_FORMAT
915 " END is %" GST_TIME_FORMAT,
917 GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
918 GST_TIME_ARGS (GST_BUFFER_PTS (buffer) + GST_BUFFER_DURATION (buffer)));
919 ret = gst_pad_chain (sinkpad, buffer);
920 ck_assert_int_eq (ret, GST_FLOW_OK);
921 gst_pad_query (sinkpad, drain);
922 fail_unless (handoff_buffer == NULL);
924 gst_element_release_request_pad (audiomixer, sinkpad);
925 gst_object_unref (sinkpad);
926 gst_element_set_state (bin, GST_STATE_NULL);
927 gst_bus_remove_signal_watch (bus);
928 gst_object_unref (bus);
929 gst_object_unref (bin);
930 gst_query_unref (drain);
935 GST_START_TEST (test_duration_is_max)
937 GstElement *bin, *src[3], *audiomixer, *sink;
938 GstStateChangeReturn state_res;
939 GstFormat format = GST_FORMAT_TIME;
943 GST_INFO ("preparing test");
946 bin = gst_pipeline_new ("pipeline");
948 /* 3 sources, an audiomixer and a fakesink */
949 src[0] = gst_element_factory_make ("audiotestsrc", NULL);
950 src[1] = gst_element_factory_make ("audiotestsrc", NULL);
951 src[2] = gst_element_factory_make ("audiotestsrc", NULL);
952 audiomixer = gst_element_factory_make ("audiomixer", "audiomixer");
953 sink = gst_element_factory_make ("fakesink", "sink");
954 gst_bin_add_many (GST_BIN (bin), src[0], src[1], src[2], audiomixer, sink,
957 gst_element_link (src[0], audiomixer);
958 gst_element_link (src[1], audiomixer);
959 gst_element_link (src[2], audiomixer);
960 gst_element_link (audiomixer, sink);
962 /* irks, duration is reset on basesrc */
963 state_res = gst_element_set_state (bin, GST_STATE_PAUSED);
964 fail_unless (state_res != GST_STATE_CHANGE_FAILURE, NULL);
966 /* set durations on src */
967 GST_BASE_SRC (src[0])->segment.duration = 1000;
968 GST_BASE_SRC (src[1])->segment.duration = 3000;
969 GST_BASE_SRC (src[2])->segment.duration = 2000;
972 set_state_and_wait (bin, GST_STATE_PLAYING);
974 res = gst_element_query_duration (GST_ELEMENT (bin), format, &duration);
975 fail_unless (res, NULL);
977 ck_assert_int_eq (duration, 3000);
979 gst_element_set_state (bin, GST_STATE_NULL);
980 gst_object_unref (bin);
985 GST_START_TEST (test_duration_unknown_overrides)
987 GstElement *bin, *src[3], *audiomixer, *sink;
988 GstStateChangeReturn state_res;
989 GstFormat format = GST_FORMAT_TIME;
993 GST_INFO ("preparing test");
996 bin = gst_pipeline_new ("pipeline");
998 /* 3 sources, an audiomixer and a fakesink */
999 src[0] = gst_element_factory_make ("audiotestsrc", NULL);
1000 src[1] = gst_element_factory_make ("audiotestsrc", NULL);
1001 src[2] = gst_element_factory_make ("audiotestsrc", NULL);
1002 audiomixer = gst_element_factory_make ("audiomixer", "audiomixer");
1003 sink = gst_element_factory_make ("fakesink", "sink");
1004 gst_bin_add_many (GST_BIN (bin), src[0], src[1], src[2], audiomixer, sink,
1007 gst_element_link (src[0], audiomixer);
1008 gst_element_link (src[1], audiomixer);
1009 gst_element_link (src[2], audiomixer);
1010 gst_element_link (audiomixer, sink);
1012 /* irks, duration is reset on basesrc */
1013 state_res = gst_element_set_state (bin, GST_STATE_PAUSED);
1014 fail_unless (state_res != GST_STATE_CHANGE_FAILURE, NULL);
1016 /* set durations on src */
1017 GST_BASE_SRC (src[0])->segment.duration = GST_CLOCK_TIME_NONE;
1018 GST_BASE_SRC (src[1])->segment.duration = 3000;
1019 GST_BASE_SRC (src[2])->segment.duration = 2000;
1021 /* set to playing */
1022 set_state_and_wait (bin, GST_STATE_PLAYING);
1024 res = gst_element_query_duration (GST_ELEMENT (bin), format, &duration);
1025 fail_unless (res, NULL);
1027 ck_assert_int_eq (duration, GST_CLOCK_TIME_NONE);
1029 gst_element_set_state (bin, GST_STATE_NULL);
1030 gst_object_unref (bin);
1036 static gboolean looped = FALSE;
1039 loop_segment_done (GstBus * bus, GstMessage * message, GstElement * bin)
1041 GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
1042 GST_MESSAGE_SRC (message), message);
1045 g_main_loop_quit (main_loop);
1047 GstEvent *seek_event;
1050 seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
1051 GST_SEEK_FLAG_SEGMENT,
1052 GST_SEEK_TYPE_SET, (GstClockTime) 0,
1053 GST_SEEK_TYPE_SET, (GstClockTime) 1 * GST_SECOND);
1055 res = gst_element_send_event (bin, seek_event);
1056 fail_unless (res == TRUE, NULL);
1061 GST_START_TEST (test_loop)
1065 GstEvent *seek_event;
1068 GST_INFO ("preparing test");
1070 /* build pipeline */
1071 bin = setup_pipeline (NULL, 2, NULL);
1072 bus = gst_element_get_bus (bin);
1073 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
1075 seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
1076 GST_SEEK_FLAG_SEGMENT | GST_SEEK_FLAG_FLUSH,
1077 GST_SEEK_TYPE_SET, (GstClockTime) 0,
1078 GST_SEEK_TYPE_SET, (GstClockTime) 1 * GST_SECOND);
1080 g_signal_connect (bus, "message::segment-done",
1081 (GCallback) loop_segment_done, bin);
1082 g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
1083 g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
1084 g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
1086 GST_INFO ("starting test");
1088 /* prepare playing */
1089 set_state_and_wait (bin, GST_STATE_PAUSED);
1091 res = gst_element_send_event (bin, seek_event);
1092 fail_unless (res == TRUE, NULL);
1095 play_and_wait (bin);
1097 fail_unless (looped);
1100 gst_bus_remove_signal_watch (bus);
1101 gst_object_unref (bus);
1102 gst_object_unref (bin);
1107 GST_START_TEST (test_flush_start_flush_stop)
1109 GstPadTemplate *sink_template;
1110 GstPad *tmppad, *srcpad1, *sinkpad1, *sinkpad2, *audiomixer_src;
1111 GstElement *pipeline, *src1, *src2, *audiomixer, *sink;
1113 GST_INFO ("preparing test");
1115 /* build pipeline */
1116 pipeline = gst_pipeline_new ("pipeline");
1117 src1 = gst_element_factory_make ("audiotestsrc", "src1");
1118 g_object_set (src1, "wave", 4, NULL); /* silence */
1119 src2 = gst_element_factory_make ("audiotestsrc", "src2");
1120 g_object_set (src2, "wave", 4, NULL); /* silence */
1121 audiomixer = gst_element_factory_make ("audiomixer", "audiomixer");
1122 sink = gst_element_factory_make ("fakesink", "sink");
1123 gst_bin_add_many (GST_BIN (pipeline), src1, src2, audiomixer, sink, NULL);
1126 gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS (audiomixer),
1128 fail_unless (GST_IS_PAD_TEMPLATE (sink_template));
1129 sinkpad1 = gst_element_request_pad (audiomixer, sink_template, NULL, NULL);
1130 srcpad1 = gst_element_get_static_pad (src1, "src");
1131 gst_pad_link (srcpad1, sinkpad1);
1133 sinkpad2 = gst_element_request_pad (audiomixer, sink_template, NULL, NULL);
1134 tmppad = gst_element_get_static_pad (src2, "src");
1135 gst_pad_link (tmppad, sinkpad2);
1136 gst_object_unref (tmppad);
1138 gst_element_link (audiomixer, sink);
1140 /* prepare playing */
1141 set_state_and_wait (pipeline, GST_STATE_PLAYING);
1143 audiomixer_src = gst_element_get_static_pad (audiomixer, "src");
1144 fail_if (GST_PAD_IS_FLUSHING (audiomixer_src));
1145 gst_pad_send_event (sinkpad1, gst_event_new_flush_start ());
1146 fail_if (GST_PAD_IS_FLUSHING (audiomixer_src));
1147 fail_unless (GST_PAD_IS_FLUSHING (sinkpad1));
1148 /* Hold the streamlock to make sure the flush stop is not between
1149 the attempted push of a segment event and of the following buffer. */
1150 GST_PAD_STREAM_LOCK (srcpad1);
1151 gst_pad_send_event (sinkpad1, gst_event_new_flush_stop (TRUE));
1152 GST_PAD_STREAM_UNLOCK (srcpad1);
1153 fail_if (GST_PAD_IS_FLUSHING (audiomixer_src));
1154 fail_if (GST_PAD_IS_FLUSHING (sinkpad1));
1155 gst_object_unref (audiomixer_src);
1157 gst_element_release_request_pad (audiomixer, sinkpad1);
1158 gst_object_unref (sinkpad1);
1159 gst_element_release_request_pad (audiomixer, sinkpad2);
1160 gst_object_unref (sinkpad2);
1161 gst_object_unref (srcpad1);
1164 gst_element_set_state (pipeline, GST_STATE_NULL);
1165 gst_object_unref (pipeline);
1171 handoff_buffer_collect_cb (GstElement * fakesink, GstBuffer * buffer,
1172 GstPad * pad, gpointer user_data)
1174 GList **received_buffers = user_data;
1176 GST_DEBUG ("got buffer %p", buffer);
1178 g_list_append (*received_buffers, gst_buffer_ref (buffer));
1181 typedef void (*SendBuffersFunction) (GstPad * pad1, GstPad * pad2);
1182 typedef void (*CheckBuffersFunction) (GList * buffers);
1185 run_sync_test (SendBuffersFunction send_buffers,
1186 CheckBuffersFunction check_buffers)
1189 GstElement *bin, *audiomixer, *queue1, *queue2, *sink;
1191 GstPad *sinkpad1, *sinkpad2;
1192 GstPad *queue1_sinkpad, *queue2_sinkpad;
1195 GstStateChangeReturn state_res;
1198 GList *received_buffers = NULL;
1200 GST_INFO ("preparing test");
1202 /* build pipeline */
1203 bin = gst_pipeline_new ("pipeline");
1204 bus = gst_element_get_bus (bin);
1205 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
1207 g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
1208 g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
1209 g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
1211 /* just an audiomixer and a fakesink */
1212 queue1 = gst_element_factory_make ("queue", "queue1");
1213 queue2 = gst_element_factory_make ("queue", "queue2");
1214 audiomixer = gst_element_factory_make ("audiomixer", "audiomixer");
1215 g_object_set (audiomixer, "output-buffer-duration", 500 * GST_MSECOND, NULL);
1216 sink = gst_element_factory_make ("fakesink", "sink");
1217 g_object_set (sink, "signal-handoffs", TRUE, NULL);
1218 g_signal_connect (sink, "handoff", (GCallback) handoff_buffer_collect_cb,
1220 gst_bin_add_many (GST_BIN (bin), queue1, queue2, audiomixer, sink, NULL);
1222 res = gst_element_link (audiomixer, sink);
1223 fail_unless (res == TRUE, NULL);
1226 state_res = gst_element_set_state (bin, GST_STATE_PAUSED);
1227 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
1229 /* create an unconnected sinkpad in audiomixer, should also automatically activate
1231 sinkpad1 = gst_element_request_pad_simple (audiomixer, "sink_%u");
1232 fail_if (sinkpad1 == NULL, NULL);
1234 queue1_sinkpad = gst_element_get_static_pad (queue1, "sink");
1235 pad = gst_element_get_static_pad (queue1, "src");
1236 fail_unless (gst_pad_link (pad, sinkpad1) == GST_PAD_LINK_OK);
1237 gst_object_unref (pad);
1239 sinkpad2 = gst_element_request_pad_simple (audiomixer, "sink_%u");
1240 fail_if (sinkpad2 == NULL, NULL);
1242 queue2_sinkpad = gst_element_get_static_pad (queue2, "sink");
1243 pad = gst_element_get_static_pad (queue2, "src");
1244 fail_unless (gst_pad_link (pad, sinkpad2) == GST_PAD_LINK_OK);
1245 gst_object_unref (pad);
1247 gst_pad_send_event (queue1_sinkpad, gst_event_new_stream_start ("test"));
1248 gst_pad_send_event (queue2_sinkpad, gst_event_new_stream_start ("test"));
1250 caps = gst_caps_new_simple ("audio/x-raw",
1251 "format", G_TYPE_STRING, GST_AUDIO_NE (S16),
1252 "layout", G_TYPE_STRING, "interleaved",
1253 "rate", G_TYPE_INT, 1000, "channels", G_TYPE_INT, 1, NULL);
1255 gst_pad_set_caps (queue1_sinkpad, caps);
1256 gst_pad_set_caps (queue2_sinkpad, caps);
1257 gst_caps_unref (caps);
1259 /* send segment to audiomixer */
1260 gst_segment_init (&segment, GST_FORMAT_TIME);
1261 event = gst_event_new_segment (&segment);
1262 gst_pad_send_event (queue1_sinkpad, gst_event_ref (event));
1263 gst_pad_send_event (queue2_sinkpad, event);
1266 send_buffers (queue1_sinkpad, queue2_sinkpad);
1269 g_idle_add ((GSourceFunc) set_playing, bin);
1271 /* Collect buffers and messages */
1272 g_main_loop_run (main_loop);
1274 /* Here we get once we got EOS, for errors we failed */
1276 check_buffers (received_buffers);
1278 g_list_free_full (received_buffers, (GDestroyNotify) gst_buffer_unref);
1280 gst_element_release_request_pad (audiomixer, sinkpad1);
1281 gst_object_unref (sinkpad1);
1282 gst_object_unref (queue1_sinkpad);
1283 gst_element_release_request_pad (audiomixer, sinkpad2);
1284 gst_object_unref (sinkpad2);
1285 gst_object_unref (queue2_sinkpad);
1286 gst_element_set_state (bin, GST_STATE_NULL);
1287 gst_bus_remove_signal_watch (bus);
1288 gst_object_unref (bus);
1289 gst_object_unref (bin);
1293 send_buffers_sync (GstPad * pad1, GstPad * pad2)
1298 buffer = new_buffer (2000, 1, 1 * GST_SECOND, 1 * GST_SECOND, 0);
1299 ret = gst_pad_chain (pad1, buffer);
1300 ck_assert_int_eq (ret, GST_FLOW_OK);
1302 buffer = new_buffer (2000, 1, 2 * GST_SECOND, 1 * GST_SECOND, 0);
1303 ret = gst_pad_chain (pad1, buffer);
1304 ck_assert_int_eq (ret, GST_FLOW_OK);
1306 gst_pad_send_event (pad1, gst_event_new_eos ());
1308 buffer = new_buffer (2000, 2, 2 * GST_SECOND, 1 * GST_SECOND, 0);
1309 ret = gst_pad_chain (pad2, buffer);
1310 ck_assert_int_eq (ret, GST_FLOW_OK);
1312 buffer = new_buffer (2000, 2, 3 * GST_SECOND, 1 * GST_SECOND, 0);
1313 ret = gst_pad_chain (pad2, buffer);
1314 ck_assert_int_eq (ret, GST_FLOW_OK);
1316 gst_pad_send_event (pad2, gst_event_new_eos ());
1320 check_buffers_sync (GList * received_buffers)
1327 /* Should have 8 * 0.5s buffers */
1328 fail_unless_equals_int (g_list_length (received_buffers), 8);
1329 for (i = 0, l = received_buffers; l; l = l->next, i++) {
1332 gst_buffer_map (buffer, &map, GST_MAP_READ);
1334 if (i == 0 && GST_BUFFER_TIMESTAMP (buffer) == 0) {
1335 fail_unless (map.data[0] == 0);
1336 fail_unless (map.data[map.size - 1] == 0);
1337 } else if (i == 1 && GST_BUFFER_TIMESTAMP (buffer) == 500 * GST_MSECOND) {
1338 fail_unless (map.data[0] == 0);
1339 fail_unless (map.data[map.size - 1] == 0);
1340 } else if (i == 2 && GST_BUFFER_TIMESTAMP (buffer) == 1000 * GST_MSECOND) {
1341 fail_unless (map.data[0] == 1);
1342 fail_unless (map.data[map.size - 1] == 1);
1343 } else if (i == 3 && GST_BUFFER_TIMESTAMP (buffer) == 1500 * GST_MSECOND) {
1344 fail_unless (map.data[0] == 1);
1345 fail_unless (map.data[map.size - 1] == 1);
1346 } else if (i == 4 && GST_BUFFER_TIMESTAMP (buffer) == 2000 * GST_MSECOND) {
1347 fail_unless (map.data[0] == 3);
1348 fail_unless (map.data[map.size - 1] == 3);
1349 } else if (i == 5 && GST_BUFFER_TIMESTAMP (buffer) == 2500 * GST_MSECOND) {
1350 fail_unless (map.data[0] == 3);
1351 fail_unless (map.data[map.size - 1] == 3);
1352 } else if (i == 6 && GST_BUFFER_TIMESTAMP (buffer) == 3000 * GST_MSECOND) {
1353 fail_unless (map.data[0] == 2);
1354 fail_unless (map.data[map.size - 1] == 2);
1355 } else if (i == 7 && GST_BUFFER_TIMESTAMP (buffer) == 3500 * GST_MSECOND) {
1356 fail_unless (map.data[0] == 2);
1357 fail_unless (map.data[map.size - 1] == 2);
1359 g_assert_not_reached ();
1362 gst_buffer_unmap (buffer, &map);
1367 GST_START_TEST (test_sync)
1369 run_sync_test (send_buffers_sync, check_buffers_sync);
1375 send_buffers_sync_discont (GstPad * pad1, GstPad * pad2)
1380 buffer = new_buffer (2000, 1, 1 * GST_SECOND, 1 * GST_SECOND, 0);
1381 ret = gst_pad_chain (pad1, buffer);
1382 ck_assert_int_eq (ret, GST_FLOW_OK);
1384 buffer = new_buffer (2000, 1, 3 * GST_SECOND, 1 * GST_SECOND,
1385 GST_BUFFER_FLAG_DISCONT);
1386 ret = gst_pad_chain (pad1, buffer);
1387 ck_assert_int_eq (ret, GST_FLOW_OK);
1389 gst_pad_send_event (pad1, gst_event_new_eos ());
1391 buffer = new_buffer (2000, 2, 2 * GST_SECOND, 1 * GST_SECOND, 0);
1392 ret = gst_pad_chain (pad2, buffer);
1393 ck_assert_int_eq (ret, GST_FLOW_OK);
1395 buffer = new_buffer (2000, 2, 3 * GST_SECOND, 1 * GST_SECOND, 0);
1396 ret = gst_pad_chain (pad2, buffer);
1397 ck_assert_int_eq (ret, GST_FLOW_OK);
1399 gst_pad_send_event (pad2, gst_event_new_eos ());
1403 check_buffers_sync_discont (GList * received_buffers)
1410 /* Should have 8 * 0.5s buffers */
1411 fail_unless_equals_int (g_list_length (received_buffers), 8);
1412 for (i = 0, l = received_buffers; l; l = l->next, i++) {
1415 gst_buffer_map (buffer, &map, GST_MAP_READ);
1417 if (i == 0 && GST_BUFFER_TIMESTAMP (buffer) == 0) {
1418 fail_unless (map.data[0] == 0);
1419 fail_unless (map.data[map.size - 1] == 0);
1420 } else if (i == 1 && GST_BUFFER_TIMESTAMP (buffer) == 500 * GST_MSECOND) {
1421 fail_unless (map.data[0] == 0);
1422 fail_unless (map.data[map.size - 1] == 0);
1423 } else if (i == 2 && GST_BUFFER_TIMESTAMP (buffer) == 1000 * GST_MSECOND) {
1424 fail_unless (map.data[0] == 1);
1425 fail_unless (map.data[map.size - 1] == 1);
1426 } else if (i == 3 && GST_BUFFER_TIMESTAMP (buffer) == 1500 * GST_MSECOND) {
1427 fail_unless (map.data[0] == 1);
1428 fail_unless (map.data[map.size - 1] == 1);
1429 } else if (i == 4 && GST_BUFFER_TIMESTAMP (buffer) == 2000 * GST_MSECOND) {
1430 fail_unless (map.data[0] == 2);
1431 fail_unless (map.data[map.size - 1] == 2);
1432 } else if (i == 5 && GST_BUFFER_TIMESTAMP (buffer) == 2500 * GST_MSECOND) {
1433 fail_unless (map.data[0] == 2);
1434 fail_unless (map.data[map.size - 1] == 2);
1435 } else if (i == 6 && GST_BUFFER_TIMESTAMP (buffer) == 3000 * GST_MSECOND) {
1436 fail_unless (map.data[0] == 3);
1437 fail_unless (map.data[map.size - 1] == 3);
1438 } else if (i == 7 && GST_BUFFER_TIMESTAMP (buffer) == 3500 * GST_MSECOND) {
1439 fail_unless (map.data[0] == 3);
1440 fail_unless (map.data[map.size - 1] == 3);
1442 g_assert_not_reached ();
1445 gst_buffer_unmap (buffer, &map);
1450 GST_START_TEST (test_sync_discont)
1452 run_sync_test (send_buffers_sync_discont, check_buffers_sync_discont);
1459 send_buffers_sync_discont_backwards (GstPad * pad1, GstPad * pad2)
1464 buffer = new_buffer (2300, 1, 1 * GST_SECOND, 1.25 * GST_SECOND, 0);
1465 ret = gst_pad_chain (pad1, buffer);
1466 ck_assert_int_eq (ret, GST_FLOW_OK);
1468 buffer = new_buffer (2000, 1, 2 * GST_SECOND, 1 * GST_SECOND,
1469 GST_BUFFER_FLAG_DISCONT);
1470 ret = gst_pad_chain (pad1, buffer);
1471 ck_assert_int_eq (ret, GST_FLOW_OK);
1473 gst_pad_send_event (pad1, gst_event_new_eos ());
1475 buffer = new_buffer (2000, 1, 2 * GST_SECOND, 1 * GST_SECOND, 0);
1476 ret = gst_pad_chain (pad2, buffer);
1477 ck_assert_int_eq (ret, GST_FLOW_OK);
1480 gst_pad_send_event (pad2, gst_event_new_eos ());
1484 check_buffers_sync_discont_backwards (GList * received_buffers)
1491 /* Should have 6 * 0.5s buffers */
1492 fail_unless_equals_int (g_list_length (received_buffers), 6);
1493 for (i = 0, l = received_buffers; l; l = l->next, i++) {
1496 gst_buffer_map (buffer, &map, GST_MAP_READ);
1498 if (i == 0 && GST_BUFFER_TIMESTAMP (buffer) == 0) {
1499 fail_unless (map.data[0] == 0);
1500 fail_unless (map.data[map.size - 1] == 0);
1501 } else if (i == 1 && GST_BUFFER_TIMESTAMP (buffer) == 500 * GST_MSECOND) {
1502 fail_unless (map.data[0] == 0);
1503 fail_unless (map.data[map.size - 1] == 0);
1504 } else if (i == 2 && GST_BUFFER_TIMESTAMP (buffer) == 1000 * GST_MSECOND) {
1505 fail_unless (map.data[0] == 1);
1506 fail_unless (map.data[map.size - 1] == 1);
1507 } else if (i == 3 && GST_BUFFER_TIMESTAMP (buffer) == 1500 * GST_MSECOND) {
1508 fail_unless (map.data[0] == 1);
1509 fail_unless (map.data[map.size - 1] == 1);
1510 } else if (i == 4 && GST_BUFFER_TIMESTAMP (buffer) == 2000 * GST_MSECOND) {
1511 fail_unless (map.data[0] == 2);
1512 fail_unless (map.data[map.size - 1] == 2);
1513 } else if (i == 5 && GST_BUFFER_TIMESTAMP (buffer) == 2500 * GST_MSECOND) {
1514 fail_unless (map.data[0] == 2);
1515 fail_unless (map.data[map.size - 1] == 2);
1517 g_assert_not_reached ();
1520 gst_buffer_unmap (buffer, &map);
1525 GST_START_TEST (test_sync_discont_backwards)
1527 run_sync_test (send_buffers_sync_discont_backwards,
1528 check_buffers_sync_discont_backwards);
1535 send_buffers_sync_unaligned (GstPad * pad1, GstPad * pad2)
1540 buffer = new_buffer (2000, 1, 750 * GST_MSECOND, 1 * GST_SECOND, 0);
1541 ret = gst_pad_chain (pad1, buffer);
1542 ck_assert_int_eq (ret, GST_FLOW_OK);
1544 buffer = new_buffer (2000, 1, 1750 * GST_MSECOND, 1 * GST_SECOND, 0);
1545 ret = gst_pad_chain (pad1, buffer);
1546 ck_assert_int_eq (ret, GST_FLOW_OK);
1548 gst_pad_send_event (pad1, gst_event_new_eos ());
1550 buffer = new_buffer (2000, 2, 1750 * GST_MSECOND, 1 * GST_SECOND, 0);
1551 ret = gst_pad_chain (pad2, buffer);
1552 ck_assert_int_eq (ret, GST_FLOW_OK);
1554 buffer = new_buffer (2000, 2, 2750 * GST_MSECOND, 1 * GST_SECOND, 0);
1555 ret = gst_pad_chain (pad2, buffer);
1556 ck_assert_int_eq (ret, GST_FLOW_OK);
1558 gst_pad_send_event (pad2, gst_event_new_eos ());
1562 check_buffers_sync_unaligned (GList * received_buffers)
1569 /* Should have 8 * 0.5s buffers */
1570 fail_unless_equals_int (g_list_length (received_buffers), 8);
1571 for (i = 0, l = received_buffers; l; l = l->next, i++) {
1574 gst_buffer_map (buffer, &map, GST_MAP_READ);
1576 if (i == 0 && GST_BUFFER_TIMESTAMP (buffer) == 0) {
1577 fail_unless (map.data[0] == 0);
1578 fail_unless (map.data[map.size - 1] == 0);
1579 } else if (i == 1 && GST_BUFFER_TIMESTAMP (buffer) == 500 * GST_MSECOND) {
1580 fail_unless (map.data[0] == 0);
1581 fail_unless (map.data[499] == 0);
1582 fail_unless (map.data[500] == 1);
1583 fail_unless (map.data[map.size - 1] == 1);
1584 } else if (i == 2 && GST_BUFFER_TIMESTAMP (buffer) == 1000 * GST_MSECOND) {
1585 fail_unless (map.data[0] == 1);
1586 fail_unless (map.data[map.size - 1] == 1);
1587 } else if (i == 3 && GST_BUFFER_TIMESTAMP (buffer) == 1500 * GST_MSECOND) {
1588 fail_unless (map.data[0] == 1);
1589 fail_unless (map.data[499] == 1);
1590 fail_unless (map.data[500] == 3);
1591 fail_unless (map.data[map.size - 1] == 3);
1592 } else if (i == 4 && GST_BUFFER_TIMESTAMP (buffer) == 2000 * GST_MSECOND) {
1593 fail_unless (map.data[0] == 3);
1594 fail_unless (map.data[499] == 3);
1595 fail_unless (map.data[500] == 3);
1596 fail_unless (map.data[map.size - 1] == 3);
1597 } else if (i == 5 && GST_BUFFER_TIMESTAMP (buffer) == 2500 * GST_MSECOND) {
1598 fail_unless (map.data[0] == 3);
1599 fail_unless (map.data[499] == 3);
1600 fail_unless (map.data[500] == 2);
1601 fail_unless (map.data[map.size - 1] == 2);
1602 } else if (i == 6 && GST_BUFFER_TIMESTAMP (buffer) == 3000 * GST_MSECOND) {
1603 fail_unless (map.data[0] == 2);
1604 fail_unless (map.data[499] == 2);
1605 fail_unless (map.data[500] == 2);
1606 fail_unless (map.data[map.size - 1] == 2);
1607 } else if (i == 7 && GST_BUFFER_TIMESTAMP (buffer) == 3500 * GST_MSECOND) {
1608 fail_unless (map.size == 500);
1609 fail_unless (GST_BUFFER_DURATION (buffer) == 250 * GST_MSECOND);
1610 fail_unless (map.data[0] == 2);
1611 fail_unless (map.data[499] == 2);
1613 g_assert_not_reached ();
1616 gst_buffer_unmap (buffer, &map);
1621 GST_START_TEST (test_sync_unaligned)
1623 run_sync_test (send_buffers_sync_unaligned, check_buffers_sync_unaligned);
1628 GST_START_TEST (test_segment_base_handling)
1630 GstElement *pipeline, *sink, *mix, *src1, *src2;
1631 GstPad *srcpad, *sinkpad;
1632 GstClockTime end_time;
1633 GstSample *last_sample = NULL;
1638 caps = gst_caps_new_simple ("audio/x-raw", "rate", G_TYPE_INT, 44100,
1639 "channels", G_TYPE_INT, 2, NULL);
1641 pipeline = gst_pipeline_new ("pipeline");
1642 mix = gst_element_factory_make ("audiomixer", "audiomixer");
1643 sink = gst_element_factory_make ("appsink", "sink");
1644 g_object_set (sink, "caps", caps, "sync", FALSE, NULL);
1645 gst_caps_unref (caps);
1646 /* 50 buffers of 1/10 sec = 5 sec */
1647 src1 = gst_element_factory_make ("audiotestsrc", "src1");
1648 g_object_set (src1, "samplesperbuffer", 4410, "num-buffers", 50, NULL);
1649 src2 = gst_element_factory_make ("audiotestsrc", "src2");
1650 g_object_set (src2, "samplesperbuffer", 4410, "num-buffers", 50, NULL);
1651 gst_bin_add_many (GST_BIN (pipeline), src1, src2, mix, sink, NULL);
1652 fail_unless (gst_element_link (mix, sink));
1654 srcpad = gst_element_get_static_pad (src1, "src");
1655 sinkpad = gst_element_request_pad_simple (mix, "sink_1");
1656 fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK);
1657 gst_object_unref (sinkpad);
1658 gst_object_unref (srcpad);
1660 srcpad = gst_element_get_static_pad (src2, "src");
1661 sinkpad = gst_element_request_pad_simple (mix, "sink_2");
1662 fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK);
1663 /* set a pad offset of another 5 seconds */
1664 gst_pad_set_offset (sinkpad, 5 * GST_SECOND);
1665 gst_object_unref (sinkpad);
1666 gst_object_unref (srcpad);
1668 gst_element_set_state (pipeline, GST_STATE_PLAYING);
1671 g_signal_emit_by_name (sink, "pull-sample", &sample);
1675 gst_sample_unref (last_sample);
1676 last_sample = sample;
1679 buf = gst_sample_get_buffer (last_sample);
1680 end_time = GST_BUFFER_TIMESTAMP (buf) + GST_BUFFER_DURATION (buf);
1681 fail_unless_equals_int64 (end_time, 10 * GST_SECOND);
1682 gst_sample_unref (last_sample);
1684 gst_element_set_state (pipeline, GST_STATE_NULL);
1685 gst_object_unref (pipeline);
1691 set_pad_volume_fade (GstPad * pad, GstClockTime start, gdouble start_value,
1692 GstClockTime end, gdouble end_value)
1694 GstControlSource *cs;
1695 GstTimedValueControlSource *tvcs;
1697 cs = gst_interpolation_control_source_new ();
1698 fail_unless (gst_object_add_control_binding (GST_OBJECT_CAST (pad),
1699 gst_direct_control_binding_new_absolute (GST_OBJECT_CAST (pad),
1702 /* set volume interpolation mode */
1703 g_object_set (cs, "mode", GST_INTERPOLATION_MODE_LINEAR, NULL);
1705 tvcs = (GstTimedValueControlSource *) cs;
1706 fail_unless (gst_timed_value_control_source_set (tvcs, start, start_value));
1707 fail_unless (gst_timed_value_control_source_set (tvcs, end, end_value));
1708 gst_object_unref (cs);
1711 GST_START_TEST (test_sinkpad_property_controller)
1715 GstElement *pipeline, *sink, *mix, *src1;
1716 GstPad *srcpad, *sinkpad;
1717 GError *error = NULL;
1720 pipeline = gst_pipeline_new ("pipeline");
1721 mix = gst_element_factory_make ("audiomixer", "audiomixer");
1722 sink = gst_element_factory_make ("fakesink", "sink");
1723 src1 = gst_element_factory_make ("audiotestsrc", "src1");
1724 g_object_set (src1, "num-buffers", 100, NULL);
1725 gst_bin_add_many (GST_BIN (pipeline), src1, mix, sink, NULL);
1726 fail_unless (gst_element_link (mix, sink));
1728 srcpad = gst_element_get_static_pad (src1, "src");
1729 sinkpad = gst_element_request_pad_simple (mix, "sink_0");
1730 fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK);
1731 set_pad_volume_fade (sinkpad, 0, 0, 1.0, 2.0);
1732 gst_object_unref (sinkpad);
1733 gst_object_unref (srcpad);
1735 gst_element_set_state (pipeline, GST_STATE_PLAYING);
1737 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
1738 msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE,
1739 GST_MESSAGE_EOS | GST_MESSAGE_ERROR);
1740 switch (GST_MESSAGE_TYPE (msg)) {
1741 case GST_MESSAGE_ERROR:
1742 gst_message_parse_error (msg, &error, &debug);
1743 g_printerr ("ERROR from element %s: %s\n",
1744 GST_OBJECT_NAME (msg->src), error->message);
1745 g_printerr ("Debug info: %s\n", debug);
1746 g_error_free (error);
1749 case GST_MESSAGE_EOS:
1752 g_assert_not_reached ();
1754 gst_message_unref (msg);
1755 g_object_unref (bus);
1757 gst_element_set_state (pipeline, GST_STATE_NULL);
1758 gst_object_unref (pipeline);
1764 change_src_caps (GstElement * fakesink, GstBuffer * buffer, GstPad * pad,
1765 GstElement * capsfilter)
1767 GstCaps *caps = gst_caps_new_simple ("audio/x-raw",
1768 "format", G_TYPE_STRING, GST_AUDIO_NE (S32),
1769 "layout", G_TYPE_STRING, "interleaved",
1770 "rate", G_TYPE_INT, 10, "channels", G_TYPE_INT, 1, NULL);
1772 g_object_set (capsfilter, "caps", caps, NULL);
1773 gst_caps_unref (caps);
1774 g_signal_connect (fakesink, "handoff", (GCallback) handoff_buffer_cb, NULL);
1775 g_signal_handlers_disconnect_by_func (fakesink, change_src_caps, capsfilter);
1778 /* In this test, we create an input buffer with a duration of 2 seconds,
1779 * and require the audiomixer to output 1 second long buffers.
1780 * The input buffer will thus be mixed twice, and the audiomixer will
1781 * output two buffers.
1783 * After audiomixer has output a first buffer, we change its output format
1784 * from S8 to S32. As our sample rate stays the same at 10 fps, and we use
1785 * mono, the first buffer should be 10 bytes long, and the second 40.
1787 * The input buffer is made up of 15 0-valued bytes, and 5 1-valued bytes.
1788 * We verify that the second buffer contains 5 0-valued integers, and
1789 * 5 1 << 24 valued integers.
1791 GST_START_TEST (test_change_output_caps)
1794 GstElement *bin, *audiomixer, *capsfilter, *sink;
1798 GstStateChangeReturn state_res;
1803 GstQuery *drain = gst_query_new_drain ();
1808 bin = gst_pipeline_new ("pipeline");
1809 bus = gst_element_get_bus (bin);
1810 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
1812 g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
1813 g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
1814 g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
1816 audiomixer = gst_element_factory_make ("audiomixer", "audiomixer");
1817 g_object_set (audiomixer, "output-buffer-duration", GST_SECOND, NULL);
1818 capsfilter = gst_element_factory_make ("capsfilter", NULL);
1819 sink = gst_element_factory_make ("fakesink", "sink");
1820 g_object_set (sink, "signal-handoffs", TRUE, NULL);
1821 g_signal_connect (sink, "handoff", (GCallback) change_src_caps, capsfilter);
1822 gst_bin_add_many (GST_BIN (bin), audiomixer, capsfilter, sink, NULL);
1824 res = gst_element_link_many (audiomixer, capsfilter, sink, NULL);
1825 fail_unless (res == TRUE, NULL);
1827 state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
1828 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
1830 sinkpad = gst_element_request_pad_simple (audiomixer, "sink_%u");
1831 fail_if (sinkpad == NULL, NULL);
1833 gst_pad_send_event (sinkpad, gst_event_new_stream_start ("test"));
1835 caps = gst_caps_new_simple ("audio/x-raw",
1836 "format", G_TYPE_STRING, "S8",
1837 "layout", G_TYPE_STRING, "interleaved",
1838 "rate", G_TYPE_INT, 10, "channels", G_TYPE_INT, 1, NULL);
1840 gst_pad_set_caps (sinkpad, caps);
1841 g_object_set (capsfilter, "caps", caps, NULL);
1842 gst_caps_unref (caps);
1844 gst_segment_init (&segment, GST_FORMAT_TIME);
1846 segment.stop = 2 * GST_SECOND;
1848 event = gst_event_new_segment (&segment);
1849 gst_pad_send_event (sinkpad, event);
1851 gst_buffer_replace (&handoff_buffer, NULL);
1853 buffer = new_buffer (20, 0, 0, 2 * GST_SECOND, 0);
1854 gst_buffer_map (buffer, &inmap, GST_MAP_WRITE);
1855 memset (inmap.data + 15, 1, 5);
1856 gst_buffer_unmap (buffer, &inmap);
1857 ret = gst_pad_chain (sinkpad, buffer);
1858 ck_assert_int_eq (ret, GST_FLOW_OK);
1859 gst_pad_query (sinkpad, drain);
1860 fail_unless (handoff_buffer != NULL);
1861 fail_unless_equals_int (gst_buffer_get_size (handoff_buffer), 40);
1863 gst_buffer_map (handoff_buffer, &outmap, GST_MAP_READ);
1864 for (i = 0; i < 10; i++) {
1867 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1868 sample = GUINT32_FROM_LE (((guint32 *) outmap.data)[i]);
1870 sample = GUINT32_FROM_BE (((guint32 *) outmap.data)[i]);
1874 fail_unless_equals_int (sample, 0);
1876 fail_unless_equals_int (sample, 1 << 24);
1879 gst_buffer_unmap (handoff_buffer, &outmap);
1880 gst_clear_buffer (&handoff_buffer);
1882 gst_element_release_request_pad (audiomixer, sinkpad);
1883 gst_object_unref (sinkpad);
1884 gst_element_set_state (bin, GST_STATE_NULL);
1885 gst_bus_remove_signal_watch (bus);
1886 gst_object_unref (bus);
1887 gst_object_unref (bin);
1888 gst_query_unref (drain);
1893 /* In this test, we create two input buffers with a duration of 1 second,
1894 * and require the audiomixer to output 1.5 second long buffers.
1896 * After we have input two buffers, we change the output format
1897 * from S8 to S32, then push a last buffer.
1899 * This makes audioaggregator convert its "half-mixed" current_buffer,
1900 * we can then ensure that the second output buffer is as expected.
1902 GST_START_TEST (test_change_output_caps_mid_output_buffer)
1905 GstElement *bin, *audiomixer, *capsfilter, *sink;
1909 GstStateChangeReturn state_res;
1919 bin = gst_pipeline_new ("pipeline");
1920 bus = gst_element_get_bus (bin);
1921 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
1923 g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
1924 g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
1925 g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
1927 audiomixer = gst_element_factory_make ("audiomixer", "audiomixer");
1928 g_object_set (audiomixer, "output-buffer-duration", 1500 * GST_MSECOND, NULL);
1929 capsfilter = gst_element_factory_make ("capsfilter", NULL);
1930 sink = gst_element_factory_make ("fakesink", "sink");
1931 gst_bin_add_many (GST_BIN (bin), audiomixer, capsfilter, sink, NULL);
1933 res = gst_element_link_many (audiomixer, capsfilter, sink, NULL);
1934 fail_unless (res == TRUE, NULL);
1936 state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
1937 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
1939 sinkpad = gst_element_request_pad_simple (audiomixer, "sink_%u");
1940 fail_if (sinkpad == NULL, NULL);
1942 gst_pad_send_event (sinkpad, gst_event_new_stream_start ("test"));
1944 caps = gst_caps_new_simple ("audio/x-raw",
1945 "format", G_TYPE_STRING, "S8",
1946 "layout", G_TYPE_STRING, "interleaved",
1947 "rate", G_TYPE_INT, 10, "channels", G_TYPE_INT, 1, NULL);
1949 gst_pad_set_caps (sinkpad, caps);
1950 g_object_set (capsfilter, "caps", caps, NULL);
1951 gst_caps_unref (caps);
1953 gst_segment_init (&segment, GST_FORMAT_TIME);
1955 segment.stop = 3 * GST_SECOND;
1957 event = gst_event_new_segment (&segment);
1958 gst_pad_send_event (sinkpad, event);
1960 buffer = new_buffer (10, 0, 0, 1 * GST_SECOND, 0);
1961 ret = gst_pad_chain (sinkpad, buffer);
1962 ck_assert_int_eq (ret, GST_FLOW_OK);
1964 buffer = new_buffer (10, 0, 1 * GST_SECOND, 1 * GST_SECOND, 0);
1965 gst_buffer_map (buffer, &inmap, GST_MAP_WRITE);
1966 memset (inmap.data, 1, 10);
1967 gst_buffer_unmap (buffer, &inmap);
1968 ret = gst_pad_chain (sinkpad, buffer);
1969 ck_assert_int_eq (ret, GST_FLOW_OK);
1971 drain = gst_query_new_drain ();
1972 gst_pad_query (sinkpad, drain);
1973 gst_query_unref (drain);
1975 caps = gst_caps_new_simple ("audio/x-raw",
1976 "format", G_TYPE_STRING, GST_AUDIO_NE (S32),
1977 "layout", G_TYPE_STRING, "interleaved",
1978 "rate", G_TYPE_INT, 10, "channels", G_TYPE_INT, 1, NULL);
1979 g_object_set (capsfilter, "caps", caps, NULL);
1980 gst_caps_unref (caps);
1982 gst_buffer_replace (&handoff_buffer, NULL);
1983 g_object_set (sink, "signal-handoffs", TRUE, NULL);
1984 g_signal_connect (sink, "handoff", (GCallback) handoff_buffer_cb, NULL);
1986 buffer = new_buffer (10, 0, 2 * GST_SECOND, 1 * GST_SECOND, 0);
1987 gst_buffer_map (buffer, &inmap, GST_MAP_WRITE);
1988 memset (inmap.data, 0, 10);
1989 gst_buffer_unmap (buffer, &inmap);
1990 ret = gst_pad_chain (sinkpad, buffer);
1991 ck_assert_int_eq (ret, GST_FLOW_OK);
1993 drain = gst_query_new_drain ();
1994 gst_pad_query (sinkpad, drain);
1995 gst_query_unref (drain);
1997 fail_unless (handoff_buffer);
1998 fail_unless_equals_int (gst_buffer_get_size (handoff_buffer), 60);
2000 gst_buffer_map (handoff_buffer, &outmap, GST_MAP_READ);
2001 for (i = 0; i < 15; i++) {
2004 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
2005 sample = GUINT32_FROM_LE (((guint32 *) outmap.data)[i]);
2007 sample = GUINT32_FROM_BE (((guint32 *) outmap.data)[i]);
2011 fail_unless_equals_int (sample, 1 << 24);
2013 fail_unless_equals_int (sample, 0);
2017 gst_buffer_unmap (handoff_buffer, &outmap);
2018 gst_clear_buffer (&handoff_buffer);
2020 gst_element_release_request_pad (audiomixer, sinkpad);
2021 gst_object_unref (sinkpad);
2022 gst_element_set_state (bin, GST_STATE_NULL);
2023 gst_bus_remove_signal_watch (bus);
2024 gst_object_unref (bus);
2025 gst_object_unref (bin);
2031 check_qos_message (GstMessage * msg, GstClockTime expected_timestamp,
2032 GstClockTime expected_duration, guint64 expected_processed,
2033 guint64 expected_dropped)
2036 guint64 running_time, stream_time, timestamp, duration;
2038 guint64 processed, dropped;
2040 gst_message_parse_qos (msg, &live, &running_time, &stream_time,
2041 ×tamp, &duration);
2042 gst_message_parse_qos_stats (msg, &format, &processed, &dropped);
2044 fail_unless_equals_uint64 (running_time, expected_timestamp);
2045 fail_unless_equals_uint64 (stream_time, expected_timestamp);
2046 fail_unless_equals_uint64 (timestamp, expected_timestamp);
2047 fail_unless_equals_uint64 (duration, expected_duration);
2049 fail_unless_equals_int64 (format, GST_FORMAT_DEFAULT);
2050 fail_unless_equals_uint64 (processed, expected_processed);
2051 fail_unless_equals_uint64 (dropped, expected_dropped);
2053 gst_message_unref (msg);
2056 GST_START_TEST (test_qos_message_live)
2058 GstBus *bus = gst_bus_new ();
2061 static const char *caps_str = "audio/x-raw, format=(string)S16LE, "
2062 "rate=(int)1000, channels=(int)1, layout=(string)interleaved";
2066 h = gst_harness_new_with_padnames ("audiomixer", "sink_0", "src");
2067 g_object_set (h->element, "output-buffer-duration", GST_SECOND, NULL);
2069 pad = gst_element_get_static_pad (h->element, "sink_0");
2070 g_object_set (pad, "qos-messages", TRUE, NULL);
2071 gst_object_unref (pad);
2073 h2 = gst_harness_new_with_element (h->element, "sink_1", NULL);
2074 pad = gst_element_get_static_pad (h->element, "sink_1");
2075 g_object_set (pad, "qos-messages", TRUE, NULL);
2076 gst_object_unref (pad);
2078 gst_element_set_bus (h->element, bus);
2079 gst_harness_play (h);
2080 gst_harness_play (h2);
2081 gst_harness_set_caps_str (h, caps_str, caps_str);
2082 gst_harness_set_src_caps_str (h2, caps_str);
2084 /* Push in 1.5s of data on sink_0 and 4s on sink_1 */
2085 gst_harness_push (h, new_buffer (3000, 0, 0, 1.5 * GST_SECOND, 0));
2086 gst_harness_push (h2, new_buffer (10000, 0, 0, 5 * GST_SECOND, 0));
2088 /* Pull a normal buffer at time 0 */
2089 b = gst_harness_pull (h);
2090 fail_unless_equals_int64 (GST_BUFFER_PTS (b), 0);
2091 fail_unless_equals_int64 (GST_BUFFER_DURATION (b), GST_SECOND);
2092 gst_buffer_unref (b);
2093 msg = gst_bus_pop_filtered (bus, GST_MESSAGE_QOS);
2094 fail_unless (msg == NULL);
2096 gst_harness_crank_single_clock_wait (h);
2098 /* Pull a buffer a time 1, the second half is faked data */
2099 b = gst_harness_pull (h);
2100 fail_unless_equals_int64 (GST_BUFFER_PTS (b), GST_SECOND);
2101 fail_unless_equals_int64 (GST_BUFFER_DURATION (b), GST_SECOND);
2102 gst_buffer_unref (b);
2103 msg = gst_bus_pop_filtered (bus, GST_MESSAGE_QOS);
2104 fail_unless (msg == NULL);
2106 /* Push a buffer thar partially overlaps, expect a QoS message */
2107 b = gst_harness_push_and_pull (h, new_buffer (3000, 0, 1.5 * GST_SECOND,
2108 1.5 * GST_SECOND, GST_BUFFER_FLAG_DISCONT));
2109 fail_unless_equals_int64 (GST_BUFFER_PTS (b), 2 * GST_SECOND);
2110 fail_unless_equals_int64 (GST_BUFFER_DURATION (b), GST_SECOND);
2111 gst_buffer_unref (b);
2113 msg = gst_bus_pop_filtered (bus, GST_MESSAGE_QOS);
2114 check_qos_message (msg, 1500 * GST_MSECOND, 500 * GST_MSECOND, 1500, 500);
2116 /* Pull one buffer to get out the mixed data */
2117 gst_harness_crank_single_clock_wait (h);
2118 b = gst_harness_pull (h);
2119 fail_unless_equals_int64 (GST_BUFFER_PTS (b), 3 * GST_SECOND);
2120 fail_unless_equals_int64 (GST_BUFFER_DURATION (b), GST_SECOND);
2121 gst_buffer_unref (b);
2122 msg = gst_bus_pop_filtered (bus, GST_MESSAGE_QOS);
2123 fail_unless (msg == NULL);
2125 /* Pull another buffer to move the time to 4s */
2126 gst_harness_crank_single_clock_wait (h);
2127 b = gst_harness_pull (h);
2128 fail_unless_equals_int64 (GST_BUFFER_PTS (b), 4 * GST_SECOND);
2129 fail_unless_equals_int64 (GST_BUFFER_DURATION (b), GST_SECOND);
2130 gst_buffer_unref (b);
2131 msg = gst_bus_pop_filtered (bus, GST_MESSAGE_QOS);
2132 fail_unless (msg == NULL);
2134 /* Push a buffer that totally overlaps, it should get dropped */
2135 gst_harness_push (h, new_buffer (1000, 0, 3 * GST_SECOND,
2136 500 * GST_MSECOND, 0));
2138 /* Crank it to get the next one, and expect message from the dropped buffer */
2139 gst_harness_crank_single_clock_wait (h);
2140 msg = gst_bus_timed_pop_filtered (bus, GST_SECOND, GST_MESSAGE_QOS);
2141 check_qos_message (msg, 3 * GST_SECOND, 500 * GST_MSECOND, 2500, 1000);
2143 gst_element_set_bus (h->element, NULL);
2144 gst_harness_teardown (h2);
2145 gst_harness_teardown (h);
2146 gst_object_unref (bus);
2152 audiomixer_suite (void)
2154 Suite *s = suite_create ("audiomixer");
2155 TCase *tc_chain = tcase_create ("general");
2157 suite_add_tcase (s, tc_chain);
2158 tcase_add_test (tc_chain, test_caps);
2159 tcase_add_test (tc_chain, test_filter_caps);
2160 tcase_add_test (tc_chain, test_event);
2161 tcase_add_test (tc_chain, test_play_twice);
2162 tcase_add_test (tc_chain, test_play_twice_then_add_and_play_again);
2163 tcase_add_test (tc_chain, test_live_seeking);
2164 tcase_add_test (tc_chain, test_add_pad);
2165 tcase_add_test (tc_chain, test_remove_pad);
2166 tcase_add_test (tc_chain, test_clip);
2167 tcase_add_test (tc_chain, test_duration_is_max);
2168 tcase_add_test (tc_chain, test_duration_unknown_overrides);
2169 tcase_add_test (tc_chain, test_loop);
2170 tcase_add_test (tc_chain, test_flush_start_flush_stop);
2171 tcase_add_test (tc_chain, test_sync);
2172 tcase_add_test (tc_chain, test_sync_discont);
2173 tcase_add_test (tc_chain, test_sync_discont_backwards);
2174 tcase_add_test (tc_chain, test_sync_unaligned);
2175 tcase_add_test (tc_chain, test_segment_base_handling);
2176 tcase_add_test (tc_chain, test_sinkpad_property_controller);
2177 tcase_add_test (tc_chain, test_qos_message_live);
2178 tcase_add_checked_fixture (tc_chain, test_setup, test_teardown);
2179 tcase_add_test (tc_chain, test_change_output_caps);
2180 tcase_add_test (tc_chain, test_change_output_caps_mid_output_buffer);
2182 /* Use a longer timeout */
2183 #ifdef HAVE_VALGRIND
2184 if (RUNNING_ON_VALGRIND) {
2185 tcase_set_timeout (tc_chain, 5 * 60);
2189 /* this is shorter than the default 60 seconds?! (tpm) */
2190 /* tcase_set_timeout (tc_chain, 6); */
2196 GST_CHECK_MAIN (audiomixer);