2 * aggregator.c - GstAggregator testsuite
3 * Copyright (C) 2006 Alessandro Decina <alessandro.d@gmail.com>
4 * Copyright (C) 2014 Mathieu Duponchelle <mathieu.duponchelle@oencreed.com>
5 * Copyright (C) 2014 Thibault Saunier <tsaunier@opencreed.com>
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.
27 #include <gst/check/gstcheck.h>
28 #include <gst/base/gstaggregator.h>
30 /* dummy aggregator based element */
32 #define GST_TYPE_TEST_AGGREGATOR (gst_test_aggregator_get_type ())
33 #define GST_TEST_AGGREGATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_TEST_AGGREGATOR, GstTestAggregator))
34 #define GST_TEST_AGGREGATOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_TEST_AGGREGATOR, GstTestAggregatorClass))
35 #define GST_TEST_AGGREGATOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_TEST_AGGREGATOR, GstTestAggregatorClass))
37 #define fail_error_message(msg) \
40 gst_message_parse_error(msg, &error, NULL); \
41 fail_unless(FALSE, "Error Message from %s : %s", \
42 GST_OBJECT_NAME (GST_MESSAGE_SRC(msg)), error->message); \
43 g_error_free (error); \
46 typedef struct _GstTestAggregator GstTestAggregator;
47 typedef struct _GstTestAggregatorClass GstTestAggregatorClass;
49 static GType gst_test_aggregator_get_type (void);
51 #define BUFFER_DURATION 100000000 /* 10 frames per second */
53 struct _GstTestAggregator
60 struct _GstTestAggregatorClass
62 GstAggregatorClass parent_class;
66 gst_test_aggregator_aggregate (GstAggregator * aggregator)
69 gboolean all_eos = TRUE;
70 GstTestAggregator *testagg;
73 gboolean done_iterating = FALSE;
75 testagg = GST_TEST_AGGREGATOR (aggregator);
77 iter = gst_element_iterate_sink_pads (GST_ELEMENT (testagg));
78 while (!done_iterating) {
80 GValue value = { 0, };
81 GstAggregatorPad *pad;
83 switch (gst_iterator_next (iter, &value)) {
85 pad = g_value_get_object (&value);
87 if (pad->eos == FALSE)
89 buffer = gst_aggregator_pad_steal_buffer (pad);
90 gst_buffer_replace (&buffer, NULL);
92 g_value_reset (&value);
94 case GST_ITERATOR_RESYNC:
95 gst_iterator_resync (iter);
97 case GST_ITERATOR_ERROR:
98 GST_WARNING_OBJECT (testagg, "Sinkpads iteration error");
99 done_iterating = TRUE;
101 case GST_ITERATOR_DONE:
102 done_iterating = TRUE;
106 gst_iterator_free (iter);
108 if (all_eos == TRUE) {
109 GST_INFO_OBJECT (testagg, "no data available, must be EOS");
110 gst_pad_push_event (aggregator->srcpad, gst_event_new_eos ());
114 buf = gst_buffer_new ();
115 GST_BUFFER_TIMESTAMP (buf) = testagg->timestamp;
116 GST_BUFFER_DURATION (buf) = BUFFER_DURATION;
117 testagg->timestamp += BUFFER_DURATION;
119 gst_aggregator_finish_buffer (aggregator, buf);
121 /* We just check finish_frame return FLOW_OK */
125 #define gst_test_aggregator_parent_class parent_class
126 G_DEFINE_TYPE (GstTestAggregator, gst_test_aggregator, GST_TYPE_AGGREGATOR);
129 gst_test_aggregator_class_init (GstTestAggregatorClass * klass)
131 GstElementClass *gstelement_class = (GstElementClass *) klass;
132 GstAggregatorClass *base_aggregator_class = (GstAggregatorClass *) klass;
134 static GstStaticPadTemplate _src_template =
135 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
136 GST_STATIC_CAPS_ANY);
138 static GstStaticPadTemplate _sink_template =
139 GST_STATIC_PAD_TEMPLATE ("sink_%u", GST_PAD_SINK, GST_PAD_REQUEST,
140 GST_STATIC_CAPS_ANY);
142 gst_element_class_add_pad_template (gstelement_class,
143 gst_static_pad_template_get (&_src_template));
145 gst_element_class_add_pad_template (gstelement_class,
146 gst_static_pad_template_get (&_sink_template));
148 gst_element_class_set_static_metadata (gstelement_class, "Aggregator",
149 "Testing", "Combine N buffers", "Stefan Sauer <ensonic@users.sf.net>");
151 base_aggregator_class->aggregate =
152 GST_DEBUG_FUNCPTR (gst_test_aggregator_aggregate);
156 gst_test_aggregator_init (GstTestAggregator * self)
158 GstAggregator *agg = GST_AGGREGATOR (self);
159 gst_segment_init (&agg->segment, GST_FORMAT_BYTES);
164 gst_test_aggregator_plugin_init (GstPlugin * plugin)
166 return gst_element_register (plugin, "testaggregator", GST_RANK_NONE,
167 GST_TYPE_TEST_AGGREGATOR);
171 gst_test_aggregator_plugin_register (void)
173 return gst_plugin_register_static (GST_VERSION_MAJOR,
177 gst_test_aggregator_plugin_init,
178 VERSION, GST_LICENSE, PACKAGE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
185 GstElement *aggregator;
186 GstPad *sinkpad, *srcpad;
187 GstFlowReturn expected_result;
189 /* ------------------
190 * ----------- --------|-- |
191 * | srcpad | -- | sinkpad | aggregator |
192 * ----------- --------|-- |
194 * This is for 1 Chain, we can have several
201 GstPad *srcpad, /* srcpad of the GstAggregator */
202 *sinkpad; /* fake sinkpad to which GstAggregator.srcpad is linked */
204 GstElement *aggregator;
206 /* -----------------|
207 * | ---------- -----------
208 * | aggregator | srcpad | -- | sinkpad |
209 * | ---------- -----------
213 gint flush_start_events, flush_stop_events;
216 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
219 GST_STATIC_CAPS_ANY);
221 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
224 GST_STATIC_CAPS_ANY);
227 push_buffer (gpointer user_data)
231 ChainData *chain_data = (ChainData *) user_data;
234 gst_pad_push_event (chain_data->srcpad, gst_event_new_stream_start ("test"));
236 caps = gst_caps_new_empty_simple ("foo/x-bar");
237 gst_pad_push_event (chain_data->srcpad, gst_event_new_caps (caps));
238 gst_caps_unref (caps);
240 gst_segment_init (&segment, GST_FORMAT_TIME);
241 gst_pad_push_event (chain_data->srcpad, gst_event_new_segment (&segment));
243 GST_DEBUG ("Pushing buffer on pad: %s:%s",
244 GST_DEBUG_PAD_NAME (chain_data->sinkpad));
245 flow = gst_pad_push (chain_data->srcpad, chain_data->buffer);
246 fail_unless (flow == chain_data->expected_result,
247 "got flow %s instead of %s on %s:%s", gst_flow_get_name (flow),
248 gst_flow_get_name (chain_data->expected_result),
249 GST_DEBUG_PAD_NAME (chain_data->sinkpad));
250 chain_data->buffer = NULL;
256 push_event (gpointer user_data)
258 ChainData *chain_data = (ChainData *) user_data;
260 GST_INFO_OBJECT (chain_data->srcpad, "Pushing event: %"
261 GST_PTR_FORMAT, chain_data->event);
262 fail_unless (gst_pad_push_event (chain_data->srcpad,
263 chain_data->event) == TRUE);
269 _aggregate_timeout (GMainLoop * ml)
271 g_main_loop_quit (ml);
273 fail_unless ("No buffer found on aggregator.srcpad -> TIMEOUT" == NULL);
279 _quit (GMainLoop * ml)
281 GST_DEBUG ("QUITING ML");
282 g_main_loop_quit (ml);
284 return G_SOURCE_REMOVE;
287 static GstPadProbeReturn
288 _aggregated_cb (GstPad * pad, GstPadProbeInfo * info, GMainLoop * ml)
290 GST_DEBUG ("SHould quit ML");
291 g_idle_add ((GSourceFunc) _quit, ml);
293 return GST_PAD_PROBE_REMOVE;
296 static GstPadProbeReturn
297 downstream_probe_cb (GstPad * pad, GstPadProbeInfo * info, TestData * test)
299 GST_DEBUG ("PROBING ");
300 if (info->type & GST_PAD_PROBE_TYPE_EVENT_FLUSH) {
301 if (GST_EVENT_TYPE (GST_PAD_PROBE_INFO_EVENT (info)) ==
302 GST_EVENT_FLUSH_START) {
304 g_atomic_int_inc (&test->flush_start_events);
305 GST_DEBUG ("==========> FLUSH: %i", test->flush_start_events);
306 } else if (GST_EVENT_TYPE (GST_PAD_PROBE_INFO_EVENT (info)) ==
307 GST_EVENT_FLUSH_STOP)
308 g_atomic_int_inc (&test->flush_stop_events);
311 return GST_PAD_PROBE_DROP;
315 * Not thread safe, will create a new ChainData which contains
316 * an activated src pad linked to a requested sink pad of @agg, and
317 * a newly allocated buffer ready to be pushed. Caller needs to
318 * clear with _chain_data_clear after.
321 _chain_data_init (ChainData * data, GstElement * agg)
323 static gint num_src_pads = 0;
324 gchar *pad_name = g_strdup_printf ("src%d", num_src_pads);
328 data->srcpad = gst_pad_new_from_static_template (&srctemplate, pad_name);
330 gst_pad_set_active (data->srcpad, TRUE);
331 data->aggregator = agg;
332 data->buffer = gst_buffer_new ();
333 data->sinkpad = gst_element_get_request_pad (agg, "sink_%u");
334 fail_unless (GST_IS_PAD (data->sinkpad));
335 fail_unless (gst_pad_link (data->srcpad, data->sinkpad) == GST_PAD_LINK_OK);
339 _chain_data_clear (ChainData * data)
342 gst_buffer_unref (data->buffer);
344 gst_object_unref (data->srcpad);
346 gst_object_unref (data->sinkpad);
350 _test_data_init (TestData * test, gboolean needs_flushing)
352 test->aggregator = gst_element_factory_make ("testaggregator", NULL);
353 gst_element_set_state (test->aggregator, GST_STATE_PLAYING);
354 test->ml = g_main_loop_new (NULL, TRUE);
355 test->srcpad = GST_AGGREGATOR (test->aggregator)->srcpad;
357 GST_DEBUG ("Srcpad: %p", test->srcpad);
359 if (needs_flushing) {
360 static gint num_sink_pads = 0;
361 gchar *pad_name = g_strdup_printf ("sink%d", num_sink_pads);
364 test->sinkpad = gst_pad_new_from_static_template (&sinktemplate, pad_name);
366 fail_unless (gst_pad_link (test->srcpad, test->sinkpad) == GST_PAD_LINK_OK);
367 gst_pad_add_probe (test->srcpad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM |
368 GST_PAD_PROBE_TYPE_DATA_DOWNSTREAM |
369 GST_PAD_PROBE_TYPE_EVENT_FLUSH,
370 (GstPadProbeCallback) downstream_probe_cb, test, NULL);
372 gst_pad_add_probe (test->srcpad, GST_PAD_PROBE_TYPE_BUFFER,
373 (GstPadProbeCallback) _aggregated_cb, test->ml, NULL);
378 g_timeout_add (1000, (GSourceFunc) _aggregate_timeout, test->ml);
382 _test_data_clear (TestData * test)
384 gst_element_set_state (test->aggregator, GST_STATE_NULL);
385 gst_object_unref (test->aggregator);
388 gst_object_unref (test->sinkpad);
390 g_main_loop_unref (test->ml);
393 GST_START_TEST (test_aggregate)
395 GThread *thread1, *thread2;
397 ChainData data1 = { 0, };
398 ChainData data2 = { 0, };
399 TestData test = { 0, };
401 _test_data_init (&test, FALSE);
402 _chain_data_init (&data1, test.aggregator);
403 _chain_data_init (&data2, test.aggregator);
405 thread1 = g_thread_try_new ("gst-check", push_buffer, &data1, NULL);
406 thread2 = g_thread_try_new ("gst-check", push_buffer, &data2, NULL);
408 g_main_loop_run (test.ml);
409 g_source_remove (test.timeout_id);
412 /* these will return immediately as when the data is popped the threads are
413 * unlocked and will terminate */
414 g_thread_join (thread1);
415 g_thread_join (thread2);
417 _chain_data_clear (&data1);
418 _chain_data_clear (&data2);
419 _test_data_clear (&test);
424 GST_START_TEST (test_aggregate_eos)
426 GThread *thread1, *thread2;
428 ChainData data1 = { 0, };
429 ChainData data2 = { 0, };
430 TestData test = { 0, };
432 _test_data_init (&test, FALSE);
433 _chain_data_init (&data1, test.aggregator);
434 _chain_data_init (&data2, test.aggregator);
436 data2.event = gst_event_new_eos ();
438 thread1 = g_thread_try_new ("gst-check", push_buffer, &data1, NULL);
439 thread2 = g_thread_try_new ("gst-check", push_event, &data2, NULL);
441 g_main_loop_run (test.ml);
442 g_source_remove (test.timeout_id);
444 /* these will return immediately as when the data is popped the threads are
445 * unlocked and will terminate */
446 g_thread_join (thread1);
447 g_thread_join (thread2);
449 _chain_data_clear (&data1);
450 _chain_data_clear (&data2);
451 _test_data_clear (&test);
456 #define NUM_BUFFERS 3
458 handoff (GstElement * fakesink, GstBuffer * buf, GstPad * pad, guint * count)
461 GST_DEBUG ("HANDOFF: %i", *count);
464 /* Test a linear pipeline using aggregator */
465 GST_START_TEST (test_linear_pipeline)
469 GstElement *pipeline, *src, *agg, *sink;
473 pipeline = gst_pipeline_new ("pipeline");
474 src = gst_check_setup_element ("fakesrc");
475 g_object_set (src, "num-buffers", NUM_BUFFERS, "sizetype", 2, "sizemax", 4,
477 agg = gst_check_setup_element ("testaggregator");
478 sink = gst_check_setup_element ("fakesink");
479 g_object_set (sink, "signal-handoffs", TRUE, NULL);
480 g_signal_connect (sink, "handoff", (GCallback) handoff, &count);
482 fail_unless (gst_bin_add (GST_BIN (pipeline), src));
483 fail_unless (gst_bin_add (GST_BIN (pipeline), agg));
484 fail_unless (gst_bin_add (GST_BIN (pipeline), sink));
485 fail_unless (gst_element_link (src, agg));
486 fail_unless (gst_element_link (agg, sink));
488 bus = gst_element_get_bus (pipeline);
489 fail_if (bus == NULL);
490 gst_element_set_state (pipeline, GST_STATE_PLAYING);
492 msg = gst_bus_poll (bus, GST_MESSAGE_EOS | GST_MESSAGE_ERROR, -1);
493 fail_if (GST_MESSAGE_TYPE (msg) != GST_MESSAGE_EOS);
494 gst_message_unref (msg);
496 fail_unless_equals_int (count, NUM_BUFFERS);
498 gst_element_set_state (pipeline, GST_STATE_NULL);
499 gst_object_unref (bus);
500 gst_object_unref (pipeline);
505 GST_START_TEST (test_two_src_pipeline)
509 GstElement *pipeline, *src, *src1, *agg, *sink;
513 pipeline = gst_pipeline_new ("pipeline");
514 src = gst_element_factory_make ("fakesrc", NULL);
515 g_object_set (src, "num-buffers", NUM_BUFFERS, "sizetype", 2, "sizemax", 4,
518 src1 = gst_element_factory_make ("fakesrc", NULL);
519 g_object_set (src1, "num-buffers", NUM_BUFFERS + 1, "sizetype", 2, "sizemax",
522 agg = gst_check_setup_element ("testaggregator");
523 sink = gst_check_setup_element ("fakesink");
524 g_object_set (sink, "signal-handoffs", TRUE, NULL);
525 g_signal_connect (sink, "handoff", (GCallback) handoff, &count);
527 fail_unless (gst_bin_add (GST_BIN (pipeline), src));
528 fail_unless (gst_bin_add (GST_BIN (pipeline), src1));
529 fail_unless (gst_bin_add (GST_BIN (pipeline), agg));
530 fail_unless (gst_bin_add (GST_BIN (pipeline), sink));
531 fail_unless (gst_element_link (src, agg));
532 fail_unless (gst_element_link (src1, agg));
533 fail_unless (gst_element_link (agg, sink));
535 bus = gst_element_get_bus (pipeline);
536 fail_if (bus == NULL);
537 gst_element_set_state (pipeline, GST_STATE_PLAYING);
539 msg = gst_bus_poll (bus, GST_MESSAGE_EOS | GST_MESSAGE_ERROR, -1);
540 fail_if (GST_MESSAGE_TYPE (msg) != GST_MESSAGE_EOS);
541 gst_message_unref (msg);
543 fail_unless_equals_int (count, NUM_BUFFERS + 1);
545 gst_element_set_state (pipeline, GST_STATE_NULL);
546 gst_object_unref (bus);
547 gst_object_unref (pipeline);
552 GST_START_TEST (test_flushing_seek)
555 GThread *thread1, *thread2;
557 ChainData data1 = { 0, };
558 ChainData data2 = { 0, };
559 TestData test = { 0, };
561 _test_data_init (&test, TRUE);
563 /* Queue a buffer in agg:sink_1. Then do a flushing seek and check that the
564 * new flushing seek logic is triggered. On the first FLUSH_START call the
565 * buffers queued in collectpads should get flushed. Only one FLUSH_START and
566 * one FLUSH_STOP should be forwarded downstream.
568 _chain_data_init (&data1, test.aggregator);
569 _chain_data_init (&data2, test.aggregator);
570 GST_BUFFER_TIMESTAMP (data2.buffer) = 0;
572 gst_segment_init (&GST_AGGREGATOR (test.aggregator)->segment,
574 /* now do a successful flushing seek */
575 event = gst_event_new_seek (1, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
576 GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_SET, 10 * GST_SECOND);
577 fail_unless (gst_pad_send_event (test.srcpad, event));
579 /* flushing starts once one of the upstream elements sends the first
581 fail_unless_equals_int (test.flush_start_events, 0);
582 fail_unless_equals_int (test.flush_stop_events, 0);
584 /* flush ogg:sink_0. This flushs collectpads, calls ::flush() and sends
585 * FLUSH_START downstream */
586 GST_DEBUG ("Flushing: %s:%s", GST_DEBUG_PAD_NAME (data2.sinkpad));
587 fail_unless (gst_pad_push_event (data2.srcpad, gst_event_new_flush_start ()));
589 /* expect this buffer to be flushed */
590 data2.expected_result = GST_FLOW_FLUSHING;
591 thread2 = g_thread_try_new ("gst-check", push_buffer, &data2, NULL);
593 fail_unless (gst_pad_push_event (data1.srcpad, gst_event_new_flush_start ()));
594 fail_unless_equals_int (test.flush_start_events, 1);
595 fail_unless_equals_int (test.flush_stop_events, 0);
597 /* the first FLUSH_STOP is not forwarded downstream */
598 fail_unless (gst_pad_push_event (data1.srcpad,
599 gst_event_new_flush_stop (TRUE)));
600 fail_unless_equals_int (test.flush_start_events, 1);
601 fail_unless_equals_int (test.flush_stop_events, 0);
603 /* at this point even the other pad agg:sink_1 should be flushing so thread2
604 * should have stopped */
605 g_thread_join (thread2);
607 /* push a buffer on agg:sink_0 to trigger one collect after flushing to verify
608 * that flushing completes once all the pads have been flushed */
609 thread1 = g_thread_try_new ("gst-check", push_buffer, &data1, NULL);
611 /* flush agg:sink_1 as well. This completes the flushing seek so a FLUSH_STOP is
613 gst_pad_push_event (data2.srcpad, gst_event_new_flush_stop (TRUE));
615 /* and the last FLUSH_STOP is forwarded downstream */
616 fail_unless_equals_int (test.flush_start_events, 1);
618 /* Check collected */
619 gst_pad_add_probe (test.srcpad, GST_PAD_PROBE_TYPE_BUFFER,
620 (GstPadProbeCallback) _aggregated_cb, test.ml, NULL);
622 data2.event = gst_event_new_eos ();
623 thread2 = g_thread_try_new ("gst-check", push_event, &data2, NULL);
625 g_main_loop_run (test.ml);
626 g_source_remove (test.timeout_id);
628 fail_unless_equals_int (test.flush_stop_events, 1);
630 /* these will return immediately as at this point the threads have been
631 * unlocked and are finished */
632 g_thread_join (thread1);
633 g_thread_join (thread2);
635 _chain_data_clear (&data1);
636 _chain_data_clear (&data2);
637 _test_data_clear (&test);
644 infinite_seek (guint num_srcs, guint num_seeks)
648 GstElement *pipeline, *src, *agg, *sink;
651 gboolean seek_res, carry_on = TRUE;
653 gst_init (NULL, NULL);
655 pipeline = gst_pipeline_new ("pipeline");
657 agg = gst_check_setup_element ("testaggregator");
658 sink = gst_check_setup_element ("fakesink");
660 fail_unless (gst_bin_add (GST_BIN (pipeline), agg));
661 fail_unless (gst_bin_add (GST_BIN (pipeline), sink));
662 fail_unless (gst_element_link (agg, sink));
664 for (i = 0; i < num_srcs; i++) {
665 src = gst_element_factory_make ("fakesrc", NULL);
666 g_object_set (src, "sizetype", 2, "sizemax", 4, NULL);
667 fail_unless (gst_bin_add (GST_BIN (pipeline), src));
668 fail_unless (gst_element_link (src, agg));
671 bus = gst_element_get_bus (pipeline);
672 fail_if (bus == NULL);
673 gst_element_set_state (pipeline, GST_STATE_PLAYING);
674 while (count < num_seeks && carry_on) {
675 message = gst_bus_poll (bus, GST_MESSAGE_ANY, GST_SECOND / 10);
677 switch (GST_MESSAGE_TYPE (message)) {
678 case GST_MESSAGE_EOS:
680 /* we should check if we really finished here */
681 GST_WARNING ("Got an EOS");
685 case GST_MESSAGE_STATE_CHANGED:
689 if (GST_MESSAGE_SRC (message) == GST_OBJECT (pipeline)) {
690 gst_message_parse_state_changed (message, NULL, &new, NULL);
692 if (new != GST_STATE_PLAYING)
695 GST_INFO ("Seeking (num: %i)", count);
697 gst_element_seek_simple (sink, GST_FORMAT_BYTES,
698 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, 0);
699 GST_INFO ("seek result is : %d", seek_res);
700 fail_unless (seek_res != 0);
706 case GST_MESSAGE_ERROR:
707 GST_ERROR ("Error on the bus: %" GST_PTR_FORMAT, message);
709 fail_error_message (message);
714 gst_message_unref (message);
718 gst_element_set_state (pipeline, GST_STATE_NULL);
719 gst_object_unref (bus);
720 gst_object_unref (pipeline);
723 GST_START_TEST (test_infinite_seek)
725 infinite_seek (2, 500);
730 GST_START_TEST (test_infinite_seek_50_src)
732 infinite_seek (50, 100);
739 GstElement *agg, *src, *pipeline;
744 static GstPadProbeReturn
745 pad_probe_cb (GstPad * pad, GstPadProbeInfo * info, RemoveElementData * data)
749 GST_INFO_OBJECT (pad, "Removing pad");
751 peer = gst_pad_get_peer (pad);
752 gst_pad_unlink (pad, peer);
753 gst_element_release_request_pad (data->agg, peer);
754 fail_unless (gst_bin_remove (GST_BIN (data->pipeline), data->src));
755 gst_object_unref (peer);
757 g_mutex_lock (data->lock);
758 g_cond_broadcast (data->cond);
759 g_mutex_unlock (data->lock);
761 return GST_PAD_PROBE_OK;
764 GST_START_TEST (test_add_remove)
766 /* Used to notify that we removed the pad from */
773 gboolean carry_on = TRUE;
774 guint num_iterations = 100;
777 GstElement *pipeline, *src, *src1 = NULL, *agg, *sink;
781 gst_init (NULL, NULL);
782 g_mutex_init (&lock);
785 pipeline = gst_pipeline_new ("pipeline");
787 agg = gst_check_setup_element ("testaggregator");
788 sink = gst_check_setup_element ("fakesink");
790 fail_unless (gst_bin_add (GST_BIN (pipeline), agg));
791 fail_unless (gst_bin_add (GST_BIN (pipeline), sink));
792 fail_unless (gst_element_link (agg, sink));
794 bus = gst_element_get_bus (pipeline);
795 while (count < num_iterations) {
797 src = gst_element_factory_make ("fakesrc", NULL);
798 g_object_set (src, "num-buffers", 100000, "sizetype", 2, "sizemax", 4,
800 gst_element_set_locked_state (src, TRUE);
801 fail_unless (gst_bin_add (GST_BIN (pipeline), src));
802 fail_unless (gst_element_link (src, agg));
803 gst_element_set_locked_state (src, FALSE);
804 fail_unless (gst_element_sync_state_with_parent (src));
807 gst_element_set_state (pipeline, GST_STATE_PLAYING);
809 /* Now make sure the seek happend */
812 message = gst_bus_timed_pop (bus, -1);
813 switch (GST_MESSAGE_TYPE (message)) {
814 case GST_MESSAGE_EOS:
816 /* we should check if we really finished here */
817 GST_WARNING ("Got an EOS");
821 case GST_MESSAGE_STATE_CHANGED:
823 if (GST_MESSAGE_SRC (message) == GST_OBJECT (pipeline)) {
824 gst_message_parse_state_changed (message, NULL, &state, NULL);
826 if (state == GST_STATE_PLAYING) {
827 RemoveElementData data;
831 GST_DEBUG ("First run, not removing any element yet");
836 data.src = gst_object_ref (src1);
840 data.pipeline = pipeline;
841 pad = gst_element_get_static_pad (data.src, "src");
843 g_mutex_lock (&lock);
844 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
845 (GstPadProbeCallback) pad_probe_cb, &data, NULL);
846 GST_INFO ("Waiting for %" GST_PTR_FORMAT " %s", pad,
847 gst_element_state_get_name (GST_STATE (data.src)));
848 g_cond_wait (&cond, &lock);
849 g_mutex_unlock (&lock);
850 gst_object_unref (pad);
852 /* We can not set state from the streaming thread so we
853 * need to make sure that the source has been removed
854 * before setting its state to NULL */
855 gst_element_set_state (data.src, GST_STATE_NULL);
857 gst_object_unref (data.src);
863 case GST_MESSAGE_ERROR:
865 GST_ERROR ("Error on the bus: %" GST_PTR_FORMAT, message);
867 fail_error_message (message);
874 gst_message_unref (message);
877 GST_INFO ("Seeking");
878 fail_unless (gst_element_seek_simple (pipeline, GST_FORMAT_BYTES,
879 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, 0));
884 gst_element_set_state (pipeline, GST_STATE_NULL);
885 gst_object_unref (bus);
886 gst_object_unref (pipeline);
887 g_mutex_clear (&lock);
888 g_cond_clear (&cond);
893 GST_START_TEST (test_change_state_intensive)
897 GstElement *pipeline, *src, *agg, *sink;
899 gint i, state_i = 0, num_srcs = 3;
900 gboolean carry_on = TRUE, ready = FALSE;
901 GstStateChangeReturn state_return;
902 GstState wanted_state, wanted_states[] = {
903 GST_STATE_PLAYING, GST_STATE_NULL, GST_STATE_PAUSED, GST_STATE_READY,
904 GST_STATE_PLAYING, GST_STATE_NULL, GST_STATE_PAUSED, GST_STATE_READY,
905 GST_STATE_PLAYING, GST_STATE_NULL, GST_STATE_PAUSED, GST_STATE_READY,
906 GST_STATE_PAUSED, GST_STATE_READY, GST_STATE_PAUSED, GST_STATE_READY,
907 GST_STATE_PAUSED, GST_STATE_READY, GST_STATE_PAUSED, GST_STATE_NULL,
908 GST_STATE_PAUSED, GST_STATE_NULL, GST_STATE_PAUSED, GST_STATE_NULL,
909 GST_STATE_PAUSED, GST_STATE_NULL, GST_STATE_PAUSED, GST_STATE_NULL,
910 GST_STATE_PAUSED, GST_STATE_NULL, GST_STATE_PLAYING, GST_STATE_NULL,
911 GST_STATE_PLAYING, GST_STATE_NULL, GST_STATE_PLAYING, GST_STATE_NULL,
912 GST_STATE_PLAYING, GST_STATE_NULL, GST_STATE_PLAYING, GST_STATE_NULL,
913 GST_STATE_PLAYING, GST_STATE_NULL, GST_STATE_PLAYING, GST_STATE_NULL,
916 gst_init (NULL, NULL);
918 pipeline = gst_pipeline_new ("pipeline");
920 agg = gst_check_setup_element ("testaggregator");
921 sink = gst_check_setup_element ("fakesink");
923 fail_unless (gst_bin_add (GST_BIN (pipeline), agg));
924 fail_unless (gst_bin_add (GST_BIN (pipeline), sink));
925 fail_unless (gst_element_link (agg, sink));
927 for (i = 0; i < num_srcs; i++) {
928 src = gst_element_factory_make ("fakesrc", NULL);
929 g_object_set (src, "sizetype", 2, "sizemax", 4, NULL);
930 fail_unless (gst_bin_add (GST_BIN (pipeline), src));
931 fail_unless (gst_element_link (src, agg));
934 bus = gst_element_get_bus (pipeline);
935 fail_if (bus == NULL);
937 wanted_state = wanted_states[state_i++];
938 state_return = gst_element_set_state (pipeline, wanted_state);
940 while (state_i < G_N_ELEMENTS (wanted_states) && carry_on) {
941 if (state_return == GST_STATE_CHANGE_SUCCESS && ready) {
942 wanted_state = wanted_states[state_i++];
943 fail_unless (gst_element_set_state (pipeline, wanted_state),
944 GST_STATE_CHANGE_SUCCESS);
945 GST_INFO ("Wanted state: %s", gst_element_state_get_name (wanted_state));
948 message = gst_bus_poll (bus, GST_MESSAGE_ANY, GST_SECOND / 10);
950 switch (GST_MESSAGE_TYPE (message)) {
951 case GST_MESSAGE_EOS:
953 /* we should check if we really finished here */
954 GST_WARNING ("Got an EOS");
958 case GST_MESSAGE_STATE_CHANGED:
962 if (GST_MESSAGE_SRC (message) == GST_OBJECT (pipeline)) {
963 gst_message_parse_state_changed (message, NULL, &new, NULL);
965 if (new != wanted_state) {
970 GST_DEBUG ("State %s reached",
971 gst_element_state_get_name (wanted_state));
972 wanted_state = wanted_states[state_i++];
973 GST_DEBUG ("Wanted state: %s",
974 gst_element_state_get_name (wanted_state));
975 state_return = gst_element_set_state (pipeline, wanted_state);
976 fail_unless (state_return == GST_STATE_CHANGE_SUCCESS ||
977 state_return == GST_STATE_CHANGE_ASYNC);
983 case GST_MESSAGE_ERROR:
984 GST_ERROR ("Error on the bus: %" GST_PTR_FORMAT, message);
990 gst_message_unref (message);
994 gst_element_set_state (pipeline, GST_STATE_NULL);
995 gst_object_unref (bus);
996 gst_object_unref (pipeline);
1002 gst_aggregator_suite (void)
1007 gst_test_aggregator_plugin_register ();
1009 suite = suite_create ("GstAggregator");
1011 general = tcase_create ("general");
1012 suite_add_tcase (suite, general);
1013 tcase_add_test (general, test_aggregate);
1014 tcase_add_test (general, test_aggregate_eos);
1015 tcase_add_test (general, test_flushing_seek);
1016 tcase_add_test (general, test_infinite_seek);
1017 tcase_add_test (general, test_infinite_seek_50_src);
1018 tcase_add_test (general, test_linear_pipeline);
1019 tcase_add_test (general, test_two_src_pipeline);
1020 tcase_add_test (general, test_add_remove);
1021 tcase_add_test (general, test_change_state_intensive);
1026 GST_CHECK_MAIN (gst_aggregator);