2 * Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
3 * Copyright (C) 2005 Thomas Vander Stichele <thomas at apestaart dot org>
5 * gstbin.c: Unit test for GstBin
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 #include <gst/check/gstcheck.h>
26 pop_messages (GstBus * bus, int count)
32 GST_DEBUG ("popping %d messages", count);
33 for (i = 0; i < count; ++i) {
34 message = gst_bus_poll (bus, GST_MESSAGE_STATE_CHANGED, -1);
36 fail_unless (message && GST_MESSAGE_TYPE (message)
37 == GST_MESSAGE_STATE_CHANGED, "did not get GST_MESSAGE_STATE_CHANGED");
39 gst_message_unref (message);
41 GST_DEBUG ("popped %d messages", count);
44 GST_START_TEST (test_interface)
51 bin = GST_BIN (gst_bin_new (NULL));
52 fail_unless (bin != NULL, "Could not create bin");
54 filesrc = gst_element_factory_make ("filesrc", NULL);
55 fail_unless (filesrc != NULL, "Could not create filesrc");
56 fail_unless (GST_IS_URI_HANDLER (filesrc), "Filesrc not a URI handler");
57 gst_bin_add (bin, filesrc);
59 fail_unless (gst_bin_get_by_interface (bin, GST_TYPE_URI_HANDLER) == filesrc);
60 gst_object_unref (filesrc);
62 it = gst_bin_iterate_all_by_interface (bin, GST_TYPE_URI_HANDLER);
63 fail_unless (it != NULL);
64 fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_OK);
65 fail_unless (item == (gpointer) filesrc);
66 gst_object_unref (GST_OBJECT (item));
67 fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_DONE);
68 gst_iterator_free (it);
70 gst_bin_add_many (bin,
71 gst_element_factory_make ("identity", NULL),
72 gst_element_factory_make ("identity", NULL),
73 gst_element_factory_make ("identity", NULL), NULL);
74 it = gst_bin_iterate_all_by_interface (bin, GST_TYPE_URI_HANDLER);
75 fail_unless (it != NULL);
76 fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_OK);
77 fail_unless (item == (gpointer) filesrc);
78 gst_object_unref (GST_OBJECT (item));
79 fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_DONE);
80 gst_iterator_free (it);
83 bin = GST_BIN (gst_bin_new (NULL));
84 fail_unless (bin != NULL);
85 gst_bin_add_many (bin,
86 gst_element_factory_make ("identity", NULL),
87 gst_element_factory_make ("identity", NULL),
88 GST_ELEMENT (bin2), gst_element_factory_make ("identity", NULL), NULL);
89 it = gst_bin_iterate_all_by_interface (bin, GST_TYPE_URI_HANDLER);
90 fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_OK);
91 fail_unless (item == (gpointer) filesrc);
92 gst_object_unref (GST_OBJECT (item));
93 fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_DONE);
94 gst_iterator_free (it);
96 gst_bin_add (bin, gst_element_factory_make ("filesrc", NULL));
97 gst_bin_add (bin2, gst_element_factory_make ("filesrc", NULL));
98 it = gst_bin_iterate_all_by_interface (bin, GST_TYPE_URI_HANDLER);
99 fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_OK);
100 gst_object_unref (GST_OBJECT (item));
101 fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_OK);
102 gst_object_unref (GST_OBJECT (item));
103 fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_OK);
104 gst_object_unref (GST_OBJECT (item));
105 fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_DONE);
106 gst_iterator_free (it);
108 gst_object_unref (bin);
113 GST_START_TEST (test_message_state_changed)
118 GstStateChangeReturn ret;
120 bin = GST_BIN (gst_bin_new (NULL));
121 fail_unless (bin != NULL, "Could not create bin");
122 ASSERT_OBJECT_REFCOUNT (bin, "bin", 1);
124 bus = g_object_new (gst_bus_get_type (), NULL);
125 gst_element_set_bus (GST_ELEMENT_CAST (bin), bus);
127 /* change state, spawning a message, causing an incref on the bin */
128 ret = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_READY);
129 fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
131 ASSERT_OBJECT_REFCOUNT (bin, "bin", 2);
133 /* get and unref the message, causing a decref on the bin */
134 message = gst_bus_poll (bus, GST_MESSAGE_STATE_CHANGED, -1);
136 fail_unless (message && GST_MESSAGE_TYPE (message)
137 == GST_MESSAGE_STATE_CHANGED, "did not get GST_MESSAGE_STATE_CHANGED");
139 gst_message_unref (message);
141 ASSERT_OBJECT_REFCOUNT (bin, "bin", 1);
144 ret = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_NULL);
145 fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
147 gst_object_unref (bus);
148 gst_object_unref (bin);
153 GST_START_TEST (test_message_state_changed_child)
159 GstStateChangeReturn ret;
161 bin = GST_BIN (gst_bin_new (NULL));
162 fail_unless (bin != NULL, "Could not create bin");
163 ASSERT_OBJECT_REFCOUNT (bin, "bin", 1);
165 bus = g_object_new (gst_bus_get_type (), NULL);
166 gst_element_set_bus (GST_ELEMENT_CAST (bin), bus);
168 src = gst_element_factory_make ("fakesrc", NULL);
169 fail_if (src == NULL, "Could not create fakesrc");
170 gst_bin_add (bin, src);
171 ASSERT_OBJECT_REFCOUNT (bin, "bin", 1);
172 ASSERT_OBJECT_REFCOUNT (src, "src", 1);
174 /* change state, spawning two messages:
175 * - first for fakesrc, forwarded to bin's bus, causing incref on fakesrc
176 * - second for bin, causing an incref on the bin */
177 GST_DEBUG ("setting bin to READY");
178 ret = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_READY);
179 fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
181 ASSERT_OBJECT_REFCOUNT (src, "src", 2);
182 ASSERT_OBJECT_REFCOUNT (bin, "bin", 2);
184 /* get and unref the message, causing a decref on the src */
185 message = gst_bus_poll (bus, GST_MESSAGE_STATE_CHANGED, -1);
186 fail_unless (message && GST_MESSAGE_TYPE (message)
187 == GST_MESSAGE_STATE_CHANGED, "did not get GST_MESSAGE_STATE_CHANGED");
189 fail_unless (message->src == GST_OBJECT (src));
190 gst_message_unref (message);
192 ASSERT_OBJECT_REFCOUNT (src, "src", 1);
193 ASSERT_OBJECT_REFCOUNT (bin, "bin", 2);
195 /* get and unref message 2, causing a decref on the bin */
196 message = gst_bus_poll (bus, GST_MESSAGE_STATE_CHANGED, -1);
197 fail_unless (message && GST_MESSAGE_TYPE (message)
198 == GST_MESSAGE_STATE_CHANGED, "did not get GST_MESSAGE_STATE_CHANGED");
200 fail_unless (message->src == GST_OBJECT (bin));
201 gst_message_unref (message);
203 ASSERT_OBJECT_REFCOUNT (src, "src", 1);
204 ASSERT_OBJECT_REFCOUNT (bin, "bin", 1);
207 ret = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_NULL);
208 fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
209 gst_object_unref (bus);
210 gst_object_unref (bin);
215 GST_START_TEST (test_message_state_changed_children)
217 GstPipeline *pipeline;
218 GstElement *src, *sink;
220 GstStateChangeReturn ret;
221 GstState current, pending;
223 pipeline = GST_PIPELINE (gst_pipeline_new (NULL));
224 fail_unless (pipeline != NULL, "Could not create pipeline");
225 ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
227 src = gst_element_factory_make ("fakesrc", NULL);
228 fail_if (src == NULL, "Could not create fakesrc");
229 /* need to silence the element as the deep_notify refcounts the
230 * parents while running */
231 g_object_set (G_OBJECT (src), "silent", TRUE, NULL);
232 gst_bin_add (GST_BIN (pipeline), src);
234 sink = gst_element_factory_make ("fakesink", NULL);
235 /* need to silence the element as the deep_notify refcounts the
236 * parents while running */
237 g_object_set (G_OBJECT (sink), "silent", TRUE, NULL);
238 fail_if (sink == NULL, "Could not create fakesink");
239 gst_bin_add (GST_BIN (pipeline), sink);
241 fail_unless (gst_element_link (src, sink), "could not link src and sink");
243 ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
244 ASSERT_OBJECT_REFCOUNT (src, "src", 1);
245 ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
247 bus = gst_pipeline_get_bus (pipeline);
249 /* change state to READY, spawning three messages */
250 GST_DEBUG ("setting pipeline to READY");
251 ret = gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_READY);
252 fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
254 /* each object is referenced by a message */
255 ASSERT_OBJECT_REFCOUNT (bus, "bus", 2);
256 ASSERT_OBJECT_REFCOUNT (src, "src", 2);
257 ASSERT_OBJECT_REFCOUNT (sink, "sink", 2);
258 ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 2);
260 pop_messages (bus, 3);
261 fail_if (gst_bus_have_pending (bus), "unexpected pending messages");
263 ASSERT_OBJECT_REFCOUNT (bus, "bus", 2);
264 ASSERT_OBJECT_REFCOUNT (src, "src", 1);
265 ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
266 ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
268 /* change state to PAUSED, spawning three messages */
269 GST_DEBUG ("setting pipeline to PAUSED");
270 ret = gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PAUSED);
271 fail_unless (ret == GST_STATE_CHANGE_ASYNC);
273 gst_element_get_state (GST_ELEMENT (pipeline), ¤t, &pending,
274 GST_CLOCK_TIME_NONE);
275 fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
276 fail_unless (current == GST_STATE_PAUSED);
277 fail_unless (pending == GST_STATE_VOID_PENDING);
279 /* wait for async thread to settle down */
280 while (GST_OBJECT_REFCOUNT_VALUE (pipeline) > 2)
283 /* each object is referenced by a message;
284 * base_src is blocked in the push and has an extra refcount.
285 * base_sink_chain has taken a refcount on the sink, and is blocked on
287 ASSERT_OBJECT_REFCOUNT (src, "src", 3);
288 ASSERT_OBJECT_REFCOUNT (sink, "sink", 3);
289 ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 2);
291 pop_messages (bus, 3);
292 fail_if ((gst_bus_pop (bus)) != NULL);
294 ASSERT_OBJECT_REFCOUNT (bus, "bus", 2);
295 ASSERT_OBJECT_REFCOUNT (src, "src", 2);
296 ASSERT_OBJECT_REFCOUNT (sink, "sink", 2);
297 ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
299 /* change state to PLAYING, spawning three messages */
300 GST_DEBUG ("setting pipeline to PLAYING");
301 ret = gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
302 fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
304 gst_element_get_state (GST_ELEMENT (pipeline), ¤t, &pending,
305 GST_CLOCK_TIME_NONE);
306 fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
307 fail_unless (current == GST_STATE_PLAYING);
308 fail_unless (pending == GST_STATE_VOID_PENDING);
310 /* each object is referenced by one message
311 * src might have an extra reference if it's still pushing
312 * sink might have an extra reference if it's still blocked on preroll
313 * pipeline posted a new-clock message too. */
314 ASSERT_OBJECT_REFCOUNT_BETWEEN (src, "src", 2, 3);
315 ASSERT_OBJECT_REFCOUNT_BETWEEN (sink, "sink", 2, 3);
316 ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 3);
318 pop_messages (bus, 3);
319 fail_if ((gst_bus_pop (bus)) != NULL);
321 ASSERT_OBJECT_REFCOUNT (bus, "bus", 2);
322 /* src might have an extra reference if it's still pushing */
323 ASSERT_OBJECT_REFCOUNT_BETWEEN (src, "src", 1, 2);
324 /* sink might have an extra reference if it's still blocked on preroll */
325 ASSERT_OBJECT_REFCOUNT_BETWEEN (sink, "sink", 1, 2);
326 ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
328 /* go back to READY, spawning six messages */
329 GST_DEBUG ("setting pipeline to READY");
330 ret = gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_READY);
331 fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
333 /* each object is referenced by two messages */
334 ASSERT_OBJECT_REFCOUNT (src, "src", 3);
335 ASSERT_OBJECT_REFCOUNT (sink, "sink", 3);
336 ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 3);
338 pop_messages (bus, 6);
339 fail_if ((gst_bus_pop (bus)) != NULL);
341 ASSERT_OBJECT_REFCOUNT (src, "src", 1);
342 ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
343 ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
345 /* setting pipeline to NULL flushes the bus automatically */
346 ret = gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
347 fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
349 ASSERT_OBJECT_REFCOUNT (src, "src", 1);
350 ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
351 ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
354 gst_object_unref (bus);
355 gst_object_unref (pipeline);
360 GST_START_TEST (test_watch_for_state_change)
362 GstElement *src, *sink, *bin;
364 GstStateChangeReturn ret;
366 bin = gst_element_factory_make ("bin", NULL);
367 fail_unless (bin != NULL, "Could not create bin");
369 bus = g_object_new (gst_bus_get_type (), NULL);
370 gst_element_set_bus (GST_ELEMENT_CAST (bin), bus);
372 src = gst_element_factory_make ("fakesrc", NULL);
373 fail_if (src == NULL, "Could not create fakesrc");
374 sink = gst_element_factory_make ("fakesink", NULL);
375 fail_if (sink == NULL, "Could not create fakesink");
377 gst_bin_add (GST_BIN (bin), sink);
378 gst_bin_add (GST_BIN (bin), src);
380 fail_unless (gst_element_link (src, sink), "could not link src and sink");
382 /* change state, spawning two times three messages */
383 ret = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_PAUSED);
384 fail_unless (ret == GST_STATE_CHANGE_ASYNC);
386 gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
387 GST_CLOCK_TIME_NONE);
388 fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
390 pop_messages (bus, 6);
392 fail_unless (gst_bus_have_pending (bus) == FALSE,
393 "Unexpected messages on bus");
395 ret = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_PLAYING);
396 fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
398 pop_messages (bus, 3);
400 /* this one might return either SUCCESS or ASYNC, likely SUCCESS */
401 gst_element_set_state (GST_ELEMENT (bin), GST_STATE_PAUSED);
402 gst_element_get_state (GST_ELEMENT (bin), NULL, NULL, GST_CLOCK_TIME_NONE);
404 pop_messages (bus, 3);
406 fail_unless (gst_bus_have_pending (bus) == FALSE,
407 "Unexpected messages on bus");
409 /* setting bin to NULL flushes the bus automatically */
410 ret = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_NULL);
411 fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
414 gst_object_unref (bus);
415 gst_object_unref (bin);
420 /* adding an element with linked pads to a bin unlinks the
422 GST_START_TEST (test_add_linked)
424 GstElement *src, *sink;
425 GstPad *srcpad, *sinkpad;
426 GstElement *pipeline;
428 pipeline = gst_pipeline_new (NULL);
429 fail_unless (pipeline != NULL, "Could not create pipeline");
431 src = gst_element_factory_make ("fakesrc", NULL);
432 fail_if (src == NULL, "Could not create fakesrc");
433 sink = gst_element_factory_make ("fakesink", NULL);
434 fail_if (sink == NULL, "Could not create fakesink");
436 srcpad = gst_element_get_pad (src, "src");
437 fail_unless (srcpad != NULL);
438 sinkpad = gst_element_get_pad (sink, "sink");
439 fail_unless (sinkpad != NULL);
441 fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK);
443 /* pads are linked now */
444 fail_unless (gst_pad_is_linked (srcpad));
445 fail_unless (gst_pad_is_linked (sinkpad));
447 /* adding element to bin voids hierarchy so pads are unlinked */
448 gst_bin_add (GST_BIN (pipeline), src);
450 /* check if pads really are unlinked */
451 fail_unless (!gst_pad_is_linked (srcpad));
452 fail_unless (!gst_pad_is_linked (sinkpad));
454 /* cannot link pads in wrong hierarchy */
455 fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_WRONG_HIERARCHY);
457 /* adding other element to bin as well */
458 gst_bin_add (GST_BIN (pipeline), sink);
460 /* now we can link again */
461 fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK);
463 /* check if pads really are linked */
464 fail_unless (gst_pad_is_linked (srcpad));
465 fail_unless (gst_pad_is_linked (sinkpad));
467 gst_object_unref (srcpad);
468 gst_object_unref (sinkpad);
469 gst_object_unref (pipeline);
474 /* g_print ("%10s: %4d => %4d\n", GST_OBJECT_NAME (msg->src), old, new); */
476 #define ASSERT_STATE_CHANGE_MSG(bus,element,old_state,new_state,num) \
479 GstState old = 0, new = 0, pending = 0; \
480 msg = gst_bus_poll (bus, GST_MESSAGE_STATE_CHANGED, GST_SECOND); \
481 fail_if (msg == NULL, "No state change message within 1 second (#" \
482 G_STRINGIFY (num) ")"); \
483 gst_message_parse_state_changed (msg, &old, &new, &pending); \
484 fail_if (msg->src != GST_OBJECT (element), G_STRINGIFY(element) \
485 " should have changed state next (#" G_STRINGIFY (num) ")"); \
486 fail_if (old != old_state || new != new_state, "state change is not " \
487 G_STRINGIFY (old_state) " => " G_STRINGIFY (new_state)); \
488 gst_message_unref (msg); \
491 GST_START_TEST (test_children_state_change_order_flagged_sink)
493 GstElement *src, *identity, *sink, *pipeline;
494 GstStateChangeReturn ret;
495 GstState current, pending;
498 pipeline = gst_pipeline_new (NULL);
499 fail_unless (pipeline != NULL, "Could not create pipeline");
501 bus = gst_element_get_bus (pipeline);
502 fail_unless (bus != NULL, "Pipeline has no bus?!");
504 src = gst_element_factory_make ("fakesrc", NULL);
505 fail_if (src == NULL, "Could not create fakesrc");
507 identity = gst_element_factory_make ("identity", NULL);
508 fail_if (identity == NULL, "Could not create identity");
510 sink = gst_element_factory_make ("fakesink", NULL);
511 fail_if (sink == NULL, "Could not create fakesink");
513 gst_bin_add_many (GST_BIN (pipeline), src, identity, sink, NULL);
515 fail_unless (gst_element_link (src, identity) == TRUE);
516 fail_unless (gst_element_link (identity, sink) == TRUE);
518 /* (1) Test state change with fakesink being a regular sink */
519 ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
520 fail_if (ret != GST_STATE_CHANGE_ASYNC,
521 "State change to PLAYING did not return ASYNC");
523 gst_element_get_state (pipeline, ¤t, &pending, GST_CLOCK_TIME_NONE);
524 fail_if (ret != GST_STATE_CHANGE_SUCCESS, "State change to PLAYING failed");
525 fail_if (current != GST_STATE_PLAYING, "State change to PLAYING failed");
526 fail_if (pending != GST_STATE_VOID_PENDING, "State change to PLAYING failed");
529 ASSERT_STATE_CHANGE_MSG (bus, sink, GST_STATE_NULL, GST_STATE_READY, 101);
530 ASSERT_STATE_CHANGE_MSG (bus, identity, GST_STATE_NULL, GST_STATE_READY, 102);
531 ASSERT_STATE_CHANGE_MSG (bus, src, GST_STATE_NULL, GST_STATE_READY, 103);
532 ASSERT_STATE_CHANGE_MSG (bus, pipeline, GST_STATE_NULL, GST_STATE_READY, 104);
534 /* READY => PAUSED */
535 /* because of pre-rolling, sink will return ASYNC on state
536 * change and change state later when it has a buffer */
537 ASSERT_STATE_CHANGE_MSG (bus, identity, GST_STATE_READY, GST_STATE_PAUSED,
540 /* From here on, all bets are off. Usually the source changes state next,
541 * but it might just as well be that the first buffer produced by the
542 * source reaches the sink before the source has finished its state change,
543 * in which case the sink will commit its new state before the source ... */
544 ASSERT_STATE_CHANGE_MSG (bus, src, GST_STATE_READY, GST_STATE_PAUSED, 106);
545 ASSERT_STATE_CHANGE_MSG (bus, sink, GST_STATE_READY, GST_STATE_PAUSED, 107);
546 ASSERT_STATE_CHANGE_MSG (bus, pipeline, GST_STATE_READY, GST_STATE_PAUSED,
549 /* PAUSED => PLAYING */
550 ASSERT_STATE_CHANGE_MSG (bus, sink, GST_STATE_PAUSED, GST_STATE_PLAYING, 109);
551 ASSERT_STATE_CHANGE_MSG (bus, identity, GST_STATE_PAUSED, GST_STATE_PLAYING,
553 ASSERT_STATE_CHANGE_MSG (bus, src, GST_STATE_PAUSED, GST_STATE_PLAYING, 111);
554 ASSERT_STATE_CHANGE_MSG (bus, pipeline, GST_STATE_PAUSED, GST_STATE_PLAYING,
557 pop_messages (bus, 3); /* pop remaining ready => paused messages off the bus */
558 pop_messages (bus, 4); /* pop paused => playing messages off the bus */
561 /* don't set to NULL that will set the bus flushing and kill our messages */
562 ret = gst_element_set_state (pipeline, GST_STATE_READY);
563 fail_if (ret != GST_STATE_CHANGE_SUCCESS, "State change to READY failed");
564 ret = gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
565 fail_if (ret != GST_STATE_CHANGE_SUCCESS, "State change to READY failed");
567 /* TODO: do we need to check downwards state change order as well? */
568 pop_messages (bus, 4); /* pop playing => paused messages off the bus */
569 pop_messages (bus, 4); /* pop paused => ready messages off the bus */
571 while (GST_OBJECT_REFCOUNT_VALUE (pipeline) > 1)
574 ASSERT_OBJECT_REFCOUNT (src, "src", 1);
575 ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
576 ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
578 ret = gst_element_set_state (pipeline, GST_STATE_NULL);
579 fail_if (ret != GST_STATE_CHANGE_SUCCESS, "State change to NULL failed");
581 ASSERT_OBJECT_REFCOUNT (src, "src", 1);
582 ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
583 ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
585 gst_object_unref (bus);
586 gst_object_unref (pipeline);
592 GST_START_TEST (test_children_state_change_order_semi_sink)
594 GstElement *src, *identity, *sink, *pipeline;
595 GstStateChangeReturn ret;
596 GstState current, pending;
599 /* (2) Now again, but check other code path where we don't have
600 * a proper sink correctly flagged as such, but a 'semi-sink' */
601 pipeline = gst_pipeline_new (NULL);
602 fail_unless (pipeline != NULL, "Could not create pipeline");
604 bus = gst_element_get_bus (pipeline);
605 fail_unless (bus != NULL, "Pipeline has no bus?!");
607 src = gst_element_factory_make ("fakesrc", NULL);
608 fail_if (src == NULL, "Could not create fakesrc");
610 identity = gst_element_factory_make ("identity", NULL);
611 fail_if (identity == NULL, "Could not create identity");
613 sink = gst_element_factory_make ("fakesink", NULL);
614 fail_if (sink == NULL, "Could not create fakesink");
616 gst_bin_add_many (GST_BIN (pipeline), src, identity, sink, NULL);
618 fail_unless (gst_element_link (src, identity) == TRUE);
619 fail_unless (gst_element_link (identity, sink) == TRUE);
621 /* this is not very nice but should work just fine in this case. */
622 GST_OBJECT_FLAG_UNSET (sink, GST_ELEMENT_IS_SINK); /* <======== */
624 ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
625 fail_if (ret != GST_STATE_CHANGE_ASYNC, "State change to PLAYING not ASYNC");
627 gst_element_get_state (pipeline, ¤t, &pending, GST_CLOCK_TIME_NONE);
628 fail_if (ret != GST_STATE_CHANGE_SUCCESS, "State change to PLAYING failed");
629 fail_if (current != GST_STATE_PLAYING, "State change to PLAYING failed");
630 fail_if (pending != GST_STATE_VOID_PENDING, "State change to PLAYING failed");
633 ASSERT_STATE_CHANGE_MSG (bus, sink, GST_STATE_NULL, GST_STATE_READY, 201);
634 ASSERT_STATE_CHANGE_MSG (bus, identity, GST_STATE_NULL, GST_STATE_READY, 202);
635 ASSERT_STATE_CHANGE_MSG (bus, src, GST_STATE_NULL, GST_STATE_READY, 203);
636 ASSERT_STATE_CHANGE_MSG (bus, pipeline, GST_STATE_NULL, GST_STATE_READY, 204);
638 /* READY => PAUSED */
639 /* because of pre-rolling, sink will return ASYNC on state
640 * change and change state later when it has a buffer */
641 ASSERT_STATE_CHANGE_MSG (bus, identity, GST_STATE_READY, GST_STATE_PAUSED,
644 /* From here on, all bets are off. Usually the source changes state next,
645 * but it might just as well be that the first buffer produced by the
646 * source reaches the sink before the source has finished its state change,
647 * in which case the sink will commit its new state before the source ... */
648 ASSERT_STATE_CHANGE_MSG (bus, src, GST_STATE_READY, GST_STATE_PAUSED, 206);
649 ASSERT_STATE_CHANGE_MSG (bus, sink, GST_STATE_READY, GST_STATE_PAUSED, 207);
650 ASSERT_STATE_CHANGE_MSG (bus, pipeline, GST_STATE_READY, GST_STATE_PAUSED,
653 /* PAUSED => PLAYING */
654 ASSERT_STATE_CHANGE_MSG (bus, sink, GST_STATE_PAUSED, GST_STATE_PLAYING, 209);
655 ASSERT_STATE_CHANGE_MSG (bus, identity, GST_STATE_PAUSED, GST_STATE_PLAYING,
657 ASSERT_STATE_CHANGE_MSG (bus, src, GST_STATE_PAUSED, GST_STATE_PLAYING, 211);
658 ASSERT_STATE_CHANGE_MSG (bus, pipeline, GST_STATE_PAUSED, GST_STATE_PLAYING,
661 pop_messages (bus, 3); /* pop remaining ready => paused messages off the bus */
662 pop_messages (bus, 4); /* pop paused => playing messages off the bus */
665 /* don't set to NULL that will set the bus flushing and kill our messages */
666 ret = gst_element_set_state (pipeline, GST_STATE_READY);
667 fail_if (ret != GST_STATE_CHANGE_SUCCESS, "State change to READY failed");
669 /* TODO: do we need to check downwards state change order as well? */
670 pop_messages (bus, 4); /* pop playing => paused messages off the bus */
671 pop_messages (bus, 4); /* pop paused => ready messages off the bus */
673 while (GST_OBJECT_REFCOUNT_VALUE (pipeline) > 1)
676 ASSERT_OBJECT_REFCOUNT (src, "src", 1);
677 ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
678 ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
680 ret = gst_element_set_state (pipeline, GST_STATE_NULL);
681 fail_if (ret != GST_STATE_CHANGE_SUCCESS, "State change to NULL failed");
683 ASSERT_OBJECT_REFCOUNT (src, "src", 1);
684 ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
685 ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
687 gst_object_unref (bus);
688 gst_object_unref (pipeline);
693 GST_START_TEST (test_children_state_change_order_two_sink)
695 GstElement *src, *tee, *identity, *sink1, *sink2, *pipeline;
696 GstStateChangeReturn ret;
699 pipeline = gst_pipeline_new (NULL);
700 fail_unless (pipeline != NULL, "Could not create pipeline");
702 bus = gst_element_get_bus (pipeline);
703 fail_unless (bus != NULL, "Pipeline has no bus?!");
705 src = gst_element_factory_make ("fakesrc", NULL);
706 fail_if (src == NULL, "Could not create fakesrc");
708 tee = gst_element_factory_make ("tee", NULL);
709 fail_if (tee == NULL, "Could not create tee");
711 identity = gst_element_factory_make ("identity", NULL);
712 fail_if (identity == NULL, "Could not create identity");
714 sink1 = gst_element_factory_make ("fakesink", NULL);
715 fail_if (sink1 == NULL, "Could not create fakesink1");
717 sink2 = gst_element_factory_make ("fakesink", NULL);
718 fail_if (sink2 == NULL, "Could not create fakesink2");
720 gst_bin_add_many (GST_BIN (pipeline), src, tee, identity, sink1, sink2, NULL);
722 fail_unless (gst_element_link (src, tee) == TRUE);
723 fail_unless (gst_element_link (tee, identity) == TRUE);
724 fail_unless (gst_element_link (identity, sink1) == TRUE);
725 fail_unless (gst_element_link (tee, sink2) == TRUE);
727 ret = gst_element_set_state (pipeline, GST_STATE_READY);
728 fail_if (ret != GST_STATE_CHANGE_SUCCESS, "State change to READY failed");
733 GstState old = 0, new = 0, pending = 0;
734 GstObject *first, *second;
736 msg = gst_bus_poll (bus, GST_MESSAGE_STATE_CHANGED, GST_SECOND);
737 fail_if (msg == NULL, "No state change message within 1 second (#201)");
739 gst_message_parse_state_changed (msg, &old, &new, &pending);
740 first = gst_object_ref (msg->src);
742 fail_if (first != GST_OBJECT (sink1) && first != GST_OBJECT (sink2),
743 "sink1 or sink2 should have changed state next #(202)");
744 gst_message_unref (msg);
746 msg = gst_bus_poll (bus, GST_MESSAGE_STATE_CHANGED, GST_SECOND);
747 fail_if (msg == NULL, "No state change message within 1 second (#201)");
749 gst_message_parse_state_changed (msg, &old, &new, &pending);
750 second = gst_object_ref (msg->src);
752 fail_if (second != GST_OBJECT (sink1) && second != GST_OBJECT (sink2),
753 "sink1 or sink2 should have changed state next #(202)");
754 gst_message_unref (msg);
756 fail_if (second == first, "got state change from same object");
758 gst_object_unref (first);
759 gst_object_unref (second);
761 ASSERT_STATE_CHANGE_MSG (bus, identity, GST_STATE_NULL, GST_STATE_READY, 203);
762 ASSERT_STATE_CHANGE_MSG (bus, tee, GST_STATE_NULL, GST_STATE_READY, 204);
763 ASSERT_STATE_CHANGE_MSG (bus, src, GST_STATE_NULL, GST_STATE_READY, 205);
764 ASSERT_STATE_CHANGE_MSG (bus, pipeline, GST_STATE_NULL, GST_STATE_READY, 206);
766 ASSERT_OBJECT_REFCOUNT (src, "src", 1);
767 ASSERT_OBJECT_REFCOUNT (tee, "tee", 1);
768 ASSERT_OBJECT_REFCOUNT (identity, "identity", 1);
769 ASSERT_OBJECT_REFCOUNT (sink1, "sink1", 1);
770 ASSERT_OBJECT_REFCOUNT (sink2, "sink2", 1);
771 ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
773 ret = gst_element_set_state (pipeline, GST_STATE_NULL);
774 fail_if (ret != GST_STATE_CHANGE_SUCCESS, "State change to NULL failed");
776 ASSERT_OBJECT_REFCOUNT (src, "src", 1);
777 ASSERT_OBJECT_REFCOUNT (tee, "tee", 1);
778 ASSERT_OBJECT_REFCOUNT (identity, "identity", 1);
779 ASSERT_OBJECT_REFCOUNT (sink1, "sink1", 1);
780 ASSERT_OBJECT_REFCOUNT (sink2, "sink2", 1);
781 ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
783 gst_object_unref (bus);
784 gst_object_unref (pipeline);
792 Suite *s = suite_create ("GstBin");
793 TCase *tc_chain = tcase_create ("bin tests");
795 tcase_set_timeout (tc_chain, 0);
797 suite_add_tcase (s, tc_chain);
798 tcase_add_test (tc_chain, test_interface);
799 tcase_add_test (tc_chain, test_children_state_change_order_flagged_sink);
800 tcase_add_test (tc_chain, test_children_state_change_order_semi_sink);
801 tcase_add_test (tc_chain, test_children_state_change_order_two_sink);
802 tcase_add_test (tc_chain, test_message_state_changed);
803 tcase_add_test (tc_chain, test_message_state_changed_child);
804 tcase_add_test (tc_chain, test_message_state_changed_children);
805 tcase_add_test (tc_chain, test_watch_for_state_change);
806 tcase_add_test (tc_chain, test_add_linked);
811 GST_CHECK_MAIN (gst_bin);