• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
3  * Copyright (C) 2005 Thomas Vander Stichele <thomas at apestaart dot org>
4  *
5  * gstbin.c: Unit test for GstBin
6  *
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.
11  *
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.
16  *
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.
21  */
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 
26 #include <gst/check/gstcheck.h>
27 #include <gst/base/gstbasesrc.h>
28 
29 static void
pop_async_done(GstBus * bus)30 pop_async_done (GstBus * bus)
31 {
32   GstMessage *message;
33 
34   GST_DEBUG ("popping async-done message");
35   message = gst_bus_poll (bus, GST_MESSAGE_ASYNC_DONE, -1);
36 
37   fail_unless (message && GST_MESSAGE_TYPE (message)
38       == GST_MESSAGE_ASYNC_DONE, "did not get GST_MESSAGE_ASYNC_DONE");
39 
40   gst_message_unref (message);
41   GST_DEBUG ("popped message");
42 }
43 
44 static void
pop_latency(GstBus * bus)45 pop_latency (GstBus * bus)
46 {
47   GstMessage *message;
48 
49   GST_DEBUG ("popping async-done message");
50   message = gst_bus_poll (bus, GST_MESSAGE_LATENCY, -1);
51 
52   fail_unless (message && GST_MESSAGE_TYPE (message)
53       == GST_MESSAGE_LATENCY, "did not get GST_MESSAGE_LATENCY");
54 
55   gst_message_unref (message);
56   GST_DEBUG ("popped message");
57 }
58 
59 static void
pop_state_changed(GstBus * bus,int count)60 pop_state_changed (GstBus * bus, int count)
61 {
62   GstMessage *message;
63 
64   int i;
65 
66   GST_DEBUG ("popping %d messages", count);
67   for (i = 0; i < count; ++i) {
68     message = gst_bus_poll (bus, GST_MESSAGE_STATE_CHANGED, -1);
69 
70     fail_unless (message && GST_MESSAGE_TYPE (message)
71         == GST_MESSAGE_STATE_CHANGED, "did not get GST_MESSAGE_STATE_CHANGED");
72 
73     gst_message_unref (message);
74   }
75   GST_DEBUG ("popped %d messages", count);
76 }
77 
78 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
79     GST_PAD_SRC,
80     GST_PAD_ALWAYS,
81     GST_STATIC_CAPS_ANY);
82 
83 static gpointer
push_one_eos(GstPad * pad)84 push_one_eos (GstPad * pad)
85 {
86   GST_DEBUG_OBJECT (pad, "Pushing EOS event");
87   gst_pad_push_event (pad, gst_event_new_eos ());
88 
89   return NULL;
90 }
91 
92 static gpointer
push_one_stream_start(GstPad * pad)93 push_one_stream_start (GstPad * pad)
94 {
95   GST_DEBUG_OBJECT (pad, "Pushing STREAM_START event");
96   gst_pad_push_event (pad, gst_event_new_stream_start ("test"));
97 
98   return NULL;
99 }
100 
GST_START_TEST(test_interface)101 GST_START_TEST (test_interface)
102 {
103   GstBin *bin, *bin2;
104   GstElement *filesrc;
105   GstIterator *it;
106   GValue item = { 0, };
107 
108   bin = GST_BIN (gst_bin_new (NULL));
109   fail_unless (bin != NULL, "Could not create bin");
110 
111   filesrc = gst_element_factory_make ("filesrc", NULL);
112   fail_unless (filesrc != NULL, "Could not create filesrc");
113   fail_unless (GST_IS_URI_HANDLER (filesrc), "Filesrc not a URI handler");
114   gst_bin_add (bin, filesrc);
115 
116   fail_unless (gst_bin_get_by_interface (bin, GST_TYPE_URI_HANDLER) == filesrc);
117   gst_object_unref (filesrc);
118 
119   it = gst_bin_iterate_all_by_interface (bin, GST_TYPE_URI_HANDLER);
120   fail_unless (it != NULL);
121   fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_OK);
122   fail_unless (g_value_get_object (&item) == (gpointer) filesrc);
123   g_value_reset (&item);
124   fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_DONE);
125   gst_iterator_free (it);
126 
127   gst_bin_add_many (bin,
128       gst_element_factory_make ("identity", NULL),
129       gst_element_factory_make ("identity", NULL),
130       gst_element_factory_make ("identity", NULL), NULL);
131   it = gst_bin_iterate_all_by_interface (bin, GST_TYPE_URI_HANDLER);
132   fail_unless (it != NULL);
133   fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_OK);
134   fail_unless (g_value_get_object (&item) == (gpointer) filesrc);
135   g_value_reset (&item);
136   fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_DONE);
137   gst_iterator_free (it);
138 
139   bin2 = bin;
140   bin = GST_BIN (gst_bin_new (NULL));
141   fail_unless (bin != NULL);
142   gst_bin_add_many (bin,
143       gst_element_factory_make ("identity", NULL),
144       gst_element_factory_make ("identity", NULL),
145       GST_ELEMENT (bin2), gst_element_factory_make ("identity", NULL), NULL);
146   it = gst_bin_iterate_all_by_interface (bin, GST_TYPE_URI_HANDLER);
147   fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_OK);
148   fail_unless (g_value_get_object (&item) == (gpointer) filesrc);
149   g_value_reset (&item);
150   fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_DONE);
151   gst_iterator_free (it);
152 
153   gst_bin_add (bin, gst_element_factory_make ("filesrc", NULL));
154   gst_bin_add (bin2, gst_element_factory_make ("filesrc", NULL));
155   it = gst_bin_iterate_all_by_interface (bin, GST_TYPE_URI_HANDLER);
156   fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_OK);
157   g_value_reset (&item);
158   fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_OK);
159   g_value_reset (&item);
160   fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_OK);
161   g_value_reset (&item);
162   fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_DONE);
163   g_value_unset (&item);
164   gst_iterator_free (it);
165 
166   gst_object_unref (bin);
167 }
168 
169 GST_END_TEST;
170 
GST_START_TEST(test_iterate_all_by_element_factory_name)171 GST_START_TEST (test_iterate_all_by_element_factory_name)
172 {
173   GstBin *bin, *bin2;
174   GstElement *filesrc;
175   GstIterator *it;
176   GValue item = { 0, };
177 
178   bin = GST_BIN (gst_bin_new (NULL));
179   fail_unless (bin != NULL, "Could not create bin");
180 
181   filesrc = gst_element_factory_make ("filesrc", NULL);
182   fail_unless (filesrc != NULL, "Could not create filesrc");
183   gst_bin_add (bin, filesrc);
184 
185   /* Test bin with single element */
186   it = gst_bin_iterate_all_by_element_factory_name (bin, "filesrc");
187   fail_unless (it != NULL);
188   fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_OK);
189   fail_unless (g_value_get_object (&item) == (gpointer) filesrc);
190   g_value_reset (&item);
191   fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_DONE);
192   gst_iterator_free (it);
193 
194   /* Negative test bin with single element */
195   it = gst_bin_iterate_all_by_element_factory_name (bin, "filesink");
196   fail_unless (it != NULL);
197   fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_DONE);
198   gst_iterator_free (it);
199 
200   /* Test bin with multiple other elements, 1 layer */
201   gst_bin_add_many (bin,
202       gst_element_factory_make ("identity", NULL),
203       gst_element_factory_make ("identity", NULL),
204       gst_element_factory_make ("identity", NULL), NULL);
205   it = gst_bin_iterate_all_by_element_factory_name (bin, "filesrc");
206   fail_unless (it != NULL);
207   fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_OK);
208   fail_unless (g_value_get_object (&item) == (gpointer) filesrc);
209   g_value_reset (&item);
210   fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_DONE);
211   gst_iterator_free (it);
212 
213   /* Test bin with multiple other elements in subbins */
214   bin2 = bin;
215   bin = GST_BIN (gst_bin_new (NULL));
216   fail_unless (bin != NULL);
217   gst_bin_add_many (bin,
218       gst_element_factory_make ("identity", NULL),
219       gst_element_factory_make ("identity", NULL),
220       GST_ELEMENT (bin2), gst_element_factory_make ("identity", NULL), NULL);
221   it = gst_bin_iterate_all_by_element_factory_name (bin, "filesrc");
222   fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_OK);
223   fail_unless (g_value_get_object (&item) == (gpointer) filesrc);
224   g_value_reset (&item);
225   fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_DONE);
226   gst_iterator_free (it);
227 
228   /* Test bin with multiple other elements, multiple occurrences in subbins */
229   gst_bin_add (bin, gst_element_factory_make ("filesrc", NULL));
230   gst_bin_add (bin2, gst_element_factory_make ("filesrc", NULL));
231   it = gst_bin_iterate_all_by_element_factory_name (bin, "filesrc");
232   fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_OK);
233   g_value_reset (&item);
234   fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_OK);
235   g_value_reset (&item);
236   fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_OK);
237   g_value_reset (&item);
238   fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_DONE);
239   g_value_unset (&item);
240   gst_iterator_free (it);
241 
242   gst_object_unref (bin);
243 }
244 
245 GST_END_TEST;
246 
GST_START_TEST(test_eos)247 GST_START_TEST (test_eos)
248 {
249   GstBus *bus;
250   GstElement *pipeline, *sink1, *sink2;
251   GstMessage *message;
252   GstPad *pad1, *pad2;
253   GThread *thread1, *thread2;
254 
255   pipeline = gst_pipeline_new ("test_eos");
256   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
257 
258   sink1 = gst_element_factory_make ("fakesink", "sink1");
259   sink2 = gst_element_factory_make ("fakesink", "sink2");
260 
261   gst_bin_add_many (GST_BIN (pipeline), sink1, sink2, NULL);
262 
263   pad1 = gst_check_setup_src_pad_by_name (sink1, &srctemplate, "sink");
264   pad2 = gst_check_setup_src_pad_by_name (sink2, &srctemplate, "sink");
265 
266   gst_pad_set_active (pad1, TRUE);
267   gst_pad_set_active (pad2, TRUE);
268 
269   fail_if (gst_element_set_state (GST_ELEMENT (pipeline),
270           GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE);
271 
272   /* Send one EOS to sink1 */
273   thread1 = g_thread_new ("thread1", (GThreadFunc) push_one_eos, pad1);
274 
275   /* Make sure the EOS message is not sent */
276   message =
277       gst_bus_poll (bus, GST_MESSAGE_ERROR | GST_MESSAGE_EOS, 2 * GST_SECOND);
278   fail_if (message != NULL);
279 
280   /* Send one EOS to sink2 */
281   thread2 = g_thread_new ("thread2", (GThreadFunc) push_one_eos, pad2);
282 
283   /* Make sure the EOS message is sent then */
284   message = gst_bus_poll (bus, GST_MESSAGE_ERROR | GST_MESSAGE_EOS, -1);
285   fail_if (message == NULL);
286   fail_unless (GST_MESSAGE_TYPE (message) == GST_MESSAGE_EOS);
287   gst_message_unref (message);
288 
289   /* Cleanup */
290   g_thread_join (thread1);
291   g_thread_join (thread2);
292 
293   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
294   gst_pad_set_active (pad1, FALSE);
295   gst_pad_set_active (pad2, FALSE);
296   gst_check_teardown_src_pad (sink1);
297   gst_check_teardown_src_pad (sink2);
298   gst_object_unref (bus);
299   gst_object_unref (pipeline);
300 }
301 
302 GST_END_TEST;
303 
GST_START_TEST(test_eos_recheck)304 GST_START_TEST (test_eos_recheck)
305 {
306   GstBus *bus;
307   GstElement *pipeline, *sink1, *sink2;
308   GstMessage *message;
309   GstPad *pad1;
310   GThread *thread1;
311 
312   pipeline = gst_pipeline_new ("test_eos_recheck");
313   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
314 
315   sink1 = gst_element_factory_make ("fakesink", "sink1");
316   sink2 = gst_element_factory_make ("fakesink", "sink2");
317 
318   gst_bin_add_many (GST_BIN (pipeline), sink1, sink2, NULL);
319 
320   /* Set async=FALSE so we don't wait for preroll */
321   g_object_set (sink1, "async", FALSE, NULL);
322   g_object_set (sink2, "async", FALSE, NULL);
323 
324   pad1 = gst_check_setup_src_pad_by_name (sink1, &srctemplate, "sink");
325 
326   gst_pad_set_active (pad1, TRUE);
327 
328   fail_if (gst_element_set_state (GST_ELEMENT (pipeline),
329           GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE);
330   fail_unless (gst_element_get_state (GST_ELEMENT (pipeline), NULL, NULL,
331           GST_CLOCK_TIME_NONE) == GST_STATE_CHANGE_SUCCESS);
332 
333   /* Send one EOS to sink1 */
334   thread1 = g_thread_new ("thread1", (GThreadFunc) push_one_eos, pad1);
335 
336   /* Make sure the EOS message is not sent */
337   message =
338       gst_bus_poll (bus, GST_MESSAGE_ERROR | GST_MESSAGE_EOS, 2 * GST_SECOND);
339   fail_if (message != NULL);
340 
341   /* Remove sink2 without it EOSing, which should trigger an EOS re-check */
342   gst_object_ref (sink2);
343   gst_bin_remove (GST_BIN (pipeline), sink2);
344   gst_element_set_state (GST_ELEMENT (sink2), GST_STATE_NULL);
345 
346   /* Make sure the EOS message is sent then */
347   message =
348       gst_bus_poll (bus, GST_MESSAGE_ERROR | GST_MESSAGE_EOS, 20 * GST_SECOND);
349   fail_if (message == NULL);
350   fail_unless (GST_MESSAGE_TYPE (message) == GST_MESSAGE_EOS);
351   gst_message_unref (message);
352 
353   /* Cleanup */
354   g_thread_join (thread1);
355 
356   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
357   gst_pad_set_active (pad1, FALSE);
358   gst_check_teardown_src_pad (sink1);
359   gst_object_unref (sink2);
360   gst_object_unref (bus);
361   gst_object_unref (pipeline);
362 }
363 
364 GST_END_TEST;
365 
GST_START_TEST(test_stream_start)366 GST_START_TEST (test_stream_start)
367 {
368   GstBus *bus;
369   GstElement *pipeline, *sink1, *sink2;
370   GstMessage *message;
371   GstPad *pad1, *pad2;
372   GThread *thread1, *thread2;
373 
374   pipeline = gst_pipeline_new ("test_stream_start");
375   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
376 
377   sink1 = gst_element_factory_make ("fakesink", "sink1");
378   sink2 = gst_element_factory_make ("fakesink", "sink2");
379 
380   gst_bin_add_many (GST_BIN (pipeline), sink1, sink2, NULL);
381 
382   pad1 = gst_check_setup_src_pad_by_name (sink1, &srctemplate, "sink");
383   pad2 = gst_check_setup_src_pad_by_name (sink2, &srctemplate, "sink");
384 
385   gst_pad_set_active (pad1, TRUE);
386   gst_pad_set_active (pad2, TRUE);
387 
388   fail_if (gst_element_set_state (GST_ELEMENT (pipeline),
389           GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE);
390 
391   /* Send one STREAM_START to sink1 */
392   thread1 = g_thread_new ("thread1", (GThreadFunc) push_one_stream_start, pad1);
393 
394   /* Make sure the STREAM_START message is not sent */
395   message =
396       gst_bus_poll (bus, GST_MESSAGE_ERROR | GST_MESSAGE_STREAM_START,
397       2 * GST_SECOND);
398   fail_if (message != NULL);
399 
400   /* Send one STREAM_START to sink2 */
401   thread2 = g_thread_new ("thread2", (GThreadFunc) push_one_stream_start, pad2);
402 
403   /* Make sure the STREAM_START message is sent then */
404   message =
405       gst_bus_poll (bus, GST_MESSAGE_ERROR | GST_MESSAGE_STREAM_START, -1);
406   fail_if (message == NULL);
407   fail_unless (GST_MESSAGE_TYPE (message) == GST_MESSAGE_STREAM_START);
408   gst_message_unref (message);
409 
410   /* Cleanup */
411   g_thread_join (thread1);
412   g_thread_join (thread2);
413 
414   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
415   gst_pad_set_active (pad1, FALSE);
416   gst_pad_set_active (pad2, FALSE);
417   gst_check_teardown_src_pad (sink1);
418   gst_check_teardown_src_pad (sink2);
419   gst_object_unref (bus);
420   gst_object_unref (pipeline);
421 }
422 
423 GST_END_TEST;
424 
GST_START_TEST(test_message_state_changed)425 GST_START_TEST (test_message_state_changed)
426 {
427   GstBin *bin;
428   GstBus *bus;
429   GstMessage *message;
430   GstStateChangeReturn ret;
431 
432   bin = GST_BIN (gst_bin_new (NULL));
433   fail_unless (bin != NULL, "Could not create bin");
434   ASSERT_OBJECT_REFCOUNT (bin, "bin", 1);
435 
436   bus = g_object_new (gst_bus_get_type (), NULL);
437   gst_object_ref_sink (bus);
438   gst_element_set_bus (GST_ELEMENT_CAST (bin), bus);
439 
440   /* change state, spawning a message, causing an incref on the bin */
441   ret = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_READY);
442   fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
443 
444   ASSERT_OBJECT_REFCOUNT (bin, "bin", 2);
445 
446   /* get and unref the message, causing a decref on the bin */
447   message = gst_bus_poll (bus, GST_MESSAGE_STATE_CHANGED, -1);
448 
449   fail_unless (message && GST_MESSAGE_TYPE (message)
450       == GST_MESSAGE_STATE_CHANGED, "did not get GST_MESSAGE_STATE_CHANGED");
451 
452   gst_message_unref (message);
453 
454   ASSERT_OBJECT_REFCOUNT (bin, "bin", 1);
455 
456   gst_bus_set_flushing (bus, TRUE);
457 
458   /* clean up */
459   ret = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_NULL);
460   fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
461 
462   gst_object_unref (bus);
463   gst_object_unref (bin);
464 }
465 
466 GST_END_TEST;
467 
GST_START_TEST(test_message_state_changed_child)468 GST_START_TEST (test_message_state_changed_child)
469 {
470   GstBin *bin;
471   GstElement *src;
472   GstBus *bus;
473   GstMessage *message;
474   GstStateChangeReturn ret;
475 
476   bin = GST_BIN (gst_bin_new (NULL));
477   fail_unless (bin != NULL, "Could not create bin");
478   ASSERT_OBJECT_REFCOUNT (bin, "bin", 1);
479 
480   bus = g_object_new (gst_bus_get_type (), NULL);
481   gst_object_ref_sink (bus);
482   gst_element_set_bus (GST_ELEMENT_CAST (bin), bus);
483 
484   src = gst_element_factory_make ("fakesrc", NULL);
485   fail_if (src == NULL, "Could not create fakesrc");
486   gst_bin_add (bin, src);
487   ASSERT_OBJECT_REFCOUNT (bin, "bin", 1);
488   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
489 
490   /* change state, spawning two messages:
491    * - first for fakesrc, forwarded to bin's bus, causing incref on fakesrc
492    * - second for bin, causing an incref on the bin */
493   GST_DEBUG ("setting bin to READY");
494   ret = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_READY);
495   fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
496 
497   ASSERT_OBJECT_REFCOUNT (src, "src", 2);
498   ASSERT_OBJECT_REFCOUNT (bin, "bin", 2);
499 
500   /* get and unref the message, causing a decref on the src */
501   message = gst_bus_poll (bus, GST_MESSAGE_STATE_CHANGED, -1);
502   fail_unless (message && GST_MESSAGE_TYPE (message)
503       == GST_MESSAGE_STATE_CHANGED, "did not get GST_MESSAGE_STATE_CHANGED");
504 
505   fail_unless (message->src == GST_OBJECT (src));
506   gst_message_unref (message);
507 
508   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
509   ASSERT_OBJECT_REFCOUNT (bin, "bin", 2);
510 
511   /* get and unref message 2, causing a decref on the bin */
512   message = gst_bus_poll (bus, GST_MESSAGE_STATE_CHANGED, -1);
513   fail_unless (message && GST_MESSAGE_TYPE (message)
514       == GST_MESSAGE_STATE_CHANGED, "did not get GST_MESSAGE_STATE_CHANGED");
515 
516   fail_unless (message->src == GST_OBJECT (bin));
517   gst_message_unref (message);
518 
519   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
520   ASSERT_OBJECT_REFCOUNT (bin, "bin", 1);
521 
522   gst_bus_set_flushing (bus, TRUE);
523 
524   /* clean up */
525   ret = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_NULL);
526   fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
527 
528   gst_object_unref (bus);
529   gst_object_unref (bin);
530 }
531 
532 GST_END_TEST;
533 
GST_START_TEST(test_message_state_changed_children)534 GST_START_TEST (test_message_state_changed_children)
535 {
536   GstPipeline *pipeline;
537   GstElement *src, *sink;
538   GstBus *bus;
539   GstStateChangeReturn ret;
540   GstState current, pending;
541 
542   pipeline = GST_PIPELINE (gst_pipeline_new (NULL));
543   fail_unless (pipeline != NULL, "Could not create pipeline");
544   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
545 
546   src = gst_element_factory_make ("fakesrc", NULL);
547   fail_if (src == NULL, "Could not create fakesrc");
548   /* need to silence the element as the deep_notify refcounts the
549    * parents while running */
550   g_object_set (G_OBJECT (src), "silent", TRUE, NULL);
551   gst_bin_add (GST_BIN (pipeline), src);
552 
553   sink = gst_element_factory_make ("fakesink", NULL);
554   /* need to silence the element as the deep_notify refcounts the
555    * parents while running */
556   g_object_set (G_OBJECT (sink), "silent", TRUE, NULL);
557   fail_if (sink == NULL, "Could not create fakesink");
558   gst_bin_add (GST_BIN (pipeline), sink);
559 
560   fail_unless (gst_element_link (src, sink), "could not link src and sink");
561 
562   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
563   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
564   ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
565 
566   bus = gst_pipeline_get_bus (pipeline);
567 
568   /* change state to READY, spawning three messages */
569   GST_DEBUG ("setting pipeline to READY");
570   ret = gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_READY);
571   fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
572 
573   /* each object is referenced by a message */
574   ASSERT_OBJECT_REFCOUNT (bus, "bus", 2);
575   ASSERT_OBJECT_REFCOUNT (src, "src", 2);
576   ASSERT_OBJECT_REFCOUNT (sink, "sink", 2);
577   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 2);
578 
579   pop_state_changed (bus, 3);
580   fail_if (gst_bus_have_pending (bus), "unexpected pending messages");
581 
582   ASSERT_OBJECT_REFCOUNT (bus, "bus", 2);
583   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
584   ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
585   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
586 
587   /* change state to PAUSED, spawning four messages */
588   /* STATE_CHANGED (NULL => READY)
589    * STREAM_START
590    * ASYNC_DONE
591    * STATE_CHANGED (READY => PAUSED)
592    */
593   GST_DEBUG ("setting pipeline to PAUSED");
594   ret = gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PAUSED);
595   fail_unless (ret == GST_STATE_CHANGE_ASYNC);
596   ret =
597       gst_element_get_state (GST_ELEMENT (pipeline), &current, &pending,
598       GST_CLOCK_TIME_NONE);
599   fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
600   fail_unless (current == GST_STATE_PAUSED);
601   fail_unless (pending == GST_STATE_VOID_PENDING);
602 
603   /* wait for async thread to settle down */
604   GST_DEBUG ("waiting for refcount");
605   while (GST_OBJECT_REFCOUNT_VALUE (pipeline) > 4)
606     THREAD_SWITCH ();
607   GST_DEBUG ("refcount <= 4 now");
608 
609   /* each object is referenced by a message;
610    * base_src is blocked in the push and has an extra refcount.
611    * base_sink_chain has taken a refcount on the sink, and is blocked on
612    * preroll
613    * The stream-status messages holds 2 more refs to the element */
614   ASSERT_OBJECT_REFCOUNT (src, "src", 4);
615   /* refcount can be 4 if the bin is still processing the async_done message of
616    * the sink. */
617   ASSERT_OBJECT_REFCOUNT_BETWEEN (sink, "sink", 2, 4);
618   /* 3 or 4 is valid, because the pipeline might still be posting
619    * its state_change message */
620   ASSERT_OBJECT_REFCOUNT_BETWEEN (pipeline, "pipeline", 3, 4);
621 
622   pop_state_changed (bus, 3);
623   pop_async_done (bus);
624   pop_latency (bus);
625   fail_if ((gst_bus_pop (bus)) != NULL);
626 
627   ASSERT_OBJECT_REFCOUNT_BETWEEN (bus, "bus", 2, 3);
628   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
629   ASSERT_OBJECT_REFCOUNT_BETWEEN (sink, "sink", 2, 3);
630   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
631 
632   /* change state to PLAYING, spawning three messages */
633   GST_DEBUG ("setting pipeline to PLAYING");
634   ret = gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
635   fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
636   ret =
637       gst_element_get_state (GST_ELEMENT (pipeline), &current, &pending,
638       GST_CLOCK_TIME_NONE);
639   fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
640   fail_unless (current == GST_STATE_PLAYING);
641   fail_unless (pending == GST_STATE_VOID_PENDING);
642 
643   /* each object is referenced by one message
644    * src might have an extra reference if it's still pushing
645    * sink might have an extra reference if it's still blocked on preroll
646    * pipeline posted a new-clock message too. */
647   ASSERT_OBJECT_REFCOUNT_BETWEEN (src, "src", 2, 3);
648   ASSERT_OBJECT_REFCOUNT_BETWEEN (sink, "sink", 2, 4);
649   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 3);
650 
651   pop_state_changed (bus, 3);
652   fail_if ((gst_bus_pop (bus)) != NULL);
653 
654   ASSERT_OBJECT_REFCOUNT (bus, "bus", 2);
655   /* src might have an extra reference if it's still pushing */
656   ASSERT_OBJECT_REFCOUNT_BETWEEN (src, "src", 1, 2);
657   /* sink might have an extra reference if it's still blocked on preroll */
658   ASSERT_OBJECT_REFCOUNT_BETWEEN (sink, "sink", 1, 3);
659   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
660 
661   /* go back to READY, spawning six messages */
662   GST_DEBUG ("setting pipeline to READY");
663   ret = gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_READY);
664   fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
665 
666   /* each object is referenced by two messages, the source also has the
667    * stream-status message referencing it */
668   ASSERT_OBJECT_REFCOUNT (src, "src", 4);
669   ASSERT_OBJECT_REFCOUNT_BETWEEN (sink, "sink", 3, 4);
670   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 3);
671 
672   pop_state_changed (bus, 6);
673   fail_if ((gst_bus_pop (bus)) != NULL);
674 
675   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
676   ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
677   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
678 
679   /* setting pipeline to NULL flushes the bus automatically */
680   ret = gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
681   fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
682 
683   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
684   ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
685   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
686 
687   /* clean up */
688   gst_object_unref (bus);
689   gst_object_unref (pipeline);
690 }
691 
692 GST_END_TEST;
693 
GST_START_TEST(test_watch_for_state_change)694 GST_START_TEST (test_watch_for_state_change)
695 {
696   GstElement *src, *sink, *bin;
697   GstBus *bus;
698   GstStateChangeReturn ret;
699 
700   bin = gst_element_factory_make ("bin", NULL);
701   fail_unless (bin != NULL, "Could not create bin");
702 
703   bus = g_object_new (gst_bus_get_type (), NULL);
704   gst_object_ref_sink (bus);
705   gst_element_set_bus (GST_ELEMENT_CAST (bin), bus);
706 
707   src = gst_element_factory_make ("fakesrc", NULL);
708   fail_if (src == NULL, "Could not create fakesrc");
709   sink = gst_element_factory_make ("fakesink", NULL);
710   fail_if (sink == NULL, "Could not create fakesink");
711 
712   gst_bin_add (GST_BIN (bin), sink);
713   gst_bin_add (GST_BIN (bin), src);
714 
715   fail_unless (gst_element_link (src, sink), "could not link src and sink");
716 
717   /* change state, spawning two times three messages */
718   ret = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_PAUSED);
719   fail_unless (ret == GST_STATE_CHANGE_ASYNC);
720   ret =
721       gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
722       GST_CLOCK_TIME_NONE);
723   fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
724 
725   pop_state_changed (bus, 6);
726   pop_async_done (bus);
727   pop_latency (bus);
728 
729   fail_unless (gst_bus_have_pending (bus) == FALSE,
730       "Unexpected messages on bus");
731 
732   ret = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_PLAYING);
733   fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
734 
735   pop_state_changed (bus, 3);
736 
737   /* this one might return either SUCCESS or ASYNC, likely SUCCESS */
738   ret = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_PAUSED);
739   gst_element_get_state (GST_ELEMENT (bin), NULL, NULL, GST_CLOCK_TIME_NONE);
740 
741   pop_state_changed (bus, 3);
742   if (ret == GST_STATE_CHANGE_ASYNC) {
743     pop_async_done (bus);
744     pop_latency (bus);
745   }
746 
747   fail_unless (gst_bus_have_pending (bus) == FALSE,
748       "Unexpected messages on bus");
749 
750   gst_bus_set_flushing (bus, TRUE);
751 
752   ret = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_NULL);
753   fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
754 
755   /* clean up */
756   gst_object_unref (bus);
757   gst_object_unref (bin);
758 }
759 
760 GST_END_TEST;
761 
GST_START_TEST(test_state_change_error_message)762 GST_START_TEST (test_state_change_error_message)
763 {
764   GstElement *src, *sink, *bin;
765   GstBus *bus;
766   GstStateChangeReturn ret;
767 
768   bin = gst_element_factory_make ("bin", NULL);
769   fail_unless (bin != NULL, "Could not create bin");
770 
771   bus = g_object_new (gst_bus_get_type (), NULL);
772   gst_object_ref_sink (bus);
773   gst_element_set_bus (GST_ELEMENT_CAST (bin), bus);
774 
775   src = gst_element_factory_make ("fakesrc", NULL);
776   fail_if (src == NULL, "Could not create fakesrc");
777   sink = gst_element_factory_make ("fakesink", NULL);
778   fail_if (sink == NULL, "Could not create fakesink");
779 
780   /* add but don't link elements */
781   gst_bin_add (GST_BIN (bin), sink);
782   gst_bin_add (GST_BIN (bin), src);
783 
784   /* change state, this should succeed */
785   ret = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_PAUSED);
786   fail_unless (ret == GST_STATE_CHANGE_ASYNC);
787 
788   /* now wait, the streaming thread will error because the source is not
789    * linked. */
790   ret = gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
791       GST_CLOCK_TIME_NONE);
792   fail_unless (ret == GST_STATE_CHANGE_FAILURE);
793 
794   gst_bus_set_flushing (bus, TRUE);
795 
796   /* setting bin to NULL flushes the bus automatically */
797   ret = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_NULL);
798   fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
799 
800   /* clean up */
801   gst_object_unref (bus);
802   gst_object_unref (bin);
803 }
804 
805 GST_END_TEST;
806 
807 
808 /* adding an element with linked pads to a bin unlinks the
809  * pads */
GST_START_TEST(test_add_linked)810 GST_START_TEST (test_add_linked)
811 {
812   GstElement *src, *sink;
813   GstPad *srcpad, *sinkpad;
814   GstElement *pipeline;
815 
816   pipeline = gst_pipeline_new (NULL);
817   fail_unless (pipeline != NULL, "Could not create pipeline");
818 
819   src = gst_element_factory_make ("fakesrc", NULL);
820   fail_if (src == NULL, "Could not create fakesrc");
821   sink = gst_element_factory_make ("fakesink", NULL);
822   fail_if (sink == NULL, "Could not create fakesink");
823 
824   srcpad = gst_element_get_static_pad (src, "src");
825   fail_unless (srcpad != NULL);
826   sinkpad = gst_element_get_static_pad (sink, "sink");
827   fail_unless (sinkpad != NULL);
828 
829   fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK);
830 
831   /* pads are linked now */
832   fail_unless (gst_pad_is_linked (srcpad));
833   fail_unless (gst_pad_is_linked (sinkpad));
834 
835   /* adding element to bin voids hierarchy so pads are unlinked */
836   gst_bin_add (GST_BIN (pipeline), src);
837 
838   /* check if pads really are unlinked */
839   fail_unless (!gst_pad_is_linked (srcpad));
840   fail_unless (!gst_pad_is_linked (sinkpad));
841 
842   /* cannot link pads in wrong hierarchy */
843   fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_WRONG_HIERARCHY);
844 
845   /* adding other element to bin as well */
846   gst_bin_add (GST_BIN (pipeline), sink);
847 
848   /* now we can link again */
849   fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK);
850 
851   /* check if pads really are linked */
852   fail_unless (gst_pad_is_linked (srcpad));
853   fail_unless (gst_pad_is_linked (sinkpad));
854 
855   gst_object_unref (srcpad);
856   gst_object_unref (sinkpad);
857   gst_object_unref (pipeline);
858 }
859 
860 GST_END_TEST;
861 
862 /* adding ourself should fail */
GST_START_TEST(test_add_self)863 GST_START_TEST (test_add_self)
864 {
865   GstElement *bin;
866 
867   bin = gst_bin_new (NULL);
868   fail_unless (bin != NULL, "Could not create bin");
869 
870   ASSERT_CRITICAL (gst_bin_add (GST_BIN (bin), bin));
871 
872   gst_object_unref (bin);
873 }
874 
875 GST_END_TEST;
876 
877 
878 /* g_print ("%10s: %4d => %4d\n", GST_OBJECT_NAME (msg->src), old, new); */
879 
880 #define ASSERT_STATE_CHANGE_MSG(bus,element,old_state,new_state,num)          \
881   {                                                                           \
882     GstMessage *msg;                                                          \
883     GstState old = 0, new = 0, pending = 0;                                   \
884     msg = gst_bus_poll (bus, GST_MESSAGE_STATE_CHANGED, GST_SECOND);          \
885     fail_if (msg == NULL, "No state change message within 1 second (#"        \
886         G_STRINGIFY (num) ")");                                               \
887     gst_message_parse_state_changed (msg, &old, &new, &pending);              \
888     fail_if (msg->src != GST_OBJECT (element), G_STRINGIFY(element)           \
889         " should have changed state next (#" G_STRINGIFY (num) ")");          \
890     fail_if (old != old_state || new != new_state, "state change is not "     \
891         G_STRINGIFY (old_state) " => " G_STRINGIFY (new_state));              \
892     gst_message_unref (msg);                                                  \
893   }
894 
GST_START_TEST(test_children_state_change_order_flagged_sink)895 GST_START_TEST (test_children_state_change_order_flagged_sink)
896 {
897   GstElement *src, *identity, *sink, *pipeline;
898   GstStateChangeReturn ret;
899   GstState current, pending;
900   GstBus *bus;
901 
902   pipeline = gst_pipeline_new (NULL);
903   fail_unless (pipeline != NULL, "Could not create pipeline");
904 
905   bus = gst_element_get_bus (pipeline);
906   fail_unless (bus != NULL, "Pipeline has no bus?!");
907 
908   src = gst_element_factory_make ("fakesrc", NULL);
909   fail_if (src == NULL, "Could not create fakesrc");
910   g_object_set (src, "num-buffers", 5, NULL);
911 
912   identity = gst_element_factory_make ("identity", NULL);
913   fail_if (identity == NULL, "Could not create identity");
914 
915   sink = gst_element_factory_make ("fakesink", NULL);
916   fail_if (sink == NULL, "Could not create fakesink");
917 
918   gst_bin_add_many (GST_BIN (pipeline), src, identity, sink, NULL);
919 
920   fail_unless (gst_element_link (src, identity) == TRUE);
921   fail_unless (gst_element_link (identity, sink) == TRUE);
922 
923   /* (1) Test state change with fakesink being a regular sink */
924   ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
925   fail_if (ret != GST_STATE_CHANGE_ASYNC,
926       "State change to PLAYING did not return ASYNC");
927   ret =
928       gst_element_get_state (pipeline, &current, &pending, GST_CLOCK_TIME_NONE);
929   fail_if (ret != GST_STATE_CHANGE_SUCCESS, "State change to PLAYING failed");
930   fail_if (current != GST_STATE_PLAYING, "State change to PLAYING failed");
931   fail_if (pending != GST_STATE_VOID_PENDING, "State change to PLAYING failed");
932 
933   /* NULL => READY */
934   ASSERT_STATE_CHANGE_MSG (bus, sink, GST_STATE_NULL, GST_STATE_READY, 101);
935   ASSERT_STATE_CHANGE_MSG (bus, identity, GST_STATE_NULL, GST_STATE_READY, 102);
936   ASSERT_STATE_CHANGE_MSG (bus, src, GST_STATE_NULL, GST_STATE_READY, 103);
937   ASSERT_STATE_CHANGE_MSG (bus, pipeline, GST_STATE_NULL, GST_STATE_READY, 104);
938 
939   /* READY => PAUSED */
940   /* because of pre-rolling, sink will return ASYNC on state
941    * change and change state later when it has a buffer */
942   GST_DEBUG ("popping READY -> PAUSED messages");
943   ASSERT_STATE_CHANGE_MSG (bus, identity, GST_STATE_READY, GST_STATE_PAUSED,
944       105);
945 #if 0
946   /* From here on, all bets are off. Usually the source changes state next,
947    * but it might just as well be that the first buffer produced by the
948    * source reaches the sink before the source has finished its state change,
949    * in which case the sink will commit its new state before the source ...  */
950   ASSERT_STATE_CHANGE_MSG (bus, src, GST_STATE_READY, GST_STATE_PAUSED, 106);
951   ASSERT_STATE_CHANGE_MSG (bus, sink, GST_STATE_READY, GST_STATE_PAUSED, 107);
952 #else
953 
954   pop_state_changed (bus, 2);   /* pop remaining ready => paused messages off the bus */
955   ASSERT_STATE_CHANGE_MSG (bus, pipeline, GST_STATE_READY, GST_STATE_PAUSED,
956       108);
957   pop_async_done (bus);
958 #endif
959   /* PAUSED => PLAYING */
960   GST_DEBUG ("popping PAUSED -> PLAYING messages");
961   ASSERT_STATE_CHANGE_MSG (bus, sink, GST_STATE_PAUSED, GST_STATE_PLAYING, 109);
962   ASSERT_STATE_CHANGE_MSG (bus, identity, GST_STATE_PAUSED, GST_STATE_PLAYING,
963       110);
964   ASSERT_STATE_CHANGE_MSG (bus, src, GST_STATE_PAUSED, GST_STATE_PLAYING, 111);
965   ASSERT_STATE_CHANGE_MSG (bus, pipeline, GST_STATE_PAUSED, GST_STATE_PLAYING,
966       112);
967 
968   /* don't set to NULL that will set the bus flushing and kill our messages */
969   ret = gst_element_set_state (pipeline, GST_STATE_READY);
970   fail_if (ret != GST_STATE_CHANGE_SUCCESS, "State change to READY failed");
971   ret = gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
972   fail_if (ret != GST_STATE_CHANGE_SUCCESS, "State change to READY failed");
973 
974   /* TODO: do we need to check downwards state change order as well? */
975   pop_state_changed (bus, 4);   /* pop playing => paused messages off the bus */
976   pop_state_changed (bus, 4);   /* pop paused => ready messages off the bus */
977 
978   while (GST_OBJECT_REFCOUNT_VALUE (pipeline) > 1)
979     THREAD_SWITCH ();
980 
981   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
982   ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
983   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
984 
985   ret = gst_element_set_state (pipeline, GST_STATE_NULL);
986   fail_if (ret != GST_STATE_CHANGE_SUCCESS, "State change to NULL failed");
987 
988   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
989   ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
990   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
991 
992   gst_object_unref (bus);
993   gst_object_unref (pipeline);
994 }
995 
996 GST_END_TEST;
997 
998 
GST_START_TEST(test_children_state_change_order_semi_sink)999 GST_START_TEST (test_children_state_change_order_semi_sink)
1000 {
1001   GstElement *src, *identity, *sink, *pipeline;
1002   GstStateChangeReturn ret;
1003   GstState current, pending;
1004   GstBus *bus;
1005 
1006   /* (2) Now again, but check other code path where we don't have
1007    *     a proper sink correctly flagged as such, but a 'semi-sink' */
1008   pipeline = gst_pipeline_new (NULL);
1009   fail_unless (pipeline != NULL, "Could not create pipeline");
1010 
1011   bus = gst_element_get_bus (pipeline);
1012   fail_unless (bus != NULL, "Pipeline has no bus?!");
1013 
1014   src = gst_element_factory_make ("fakesrc", NULL);
1015   fail_if (src == NULL, "Could not create fakesrc");
1016 
1017   identity = gst_element_factory_make ("identity", NULL);
1018   fail_if (identity == NULL, "Could not create identity");
1019 
1020   sink = gst_element_factory_make ("fakesink", NULL);
1021   fail_if (sink == NULL, "Could not create fakesink");
1022 
1023   gst_bin_add_many (GST_BIN (pipeline), src, identity, sink, NULL);
1024 
1025   fail_unless (gst_element_link (src, identity) == TRUE);
1026   fail_unless (gst_element_link (identity, sink) == TRUE);
1027 
1028   /* this is not very nice but should work just fine in this case. */
1029   GST_OBJECT_FLAG_UNSET (sink, GST_ELEMENT_FLAG_SINK);  /* <======== */
1030 
1031   ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
1032   fail_if (ret != GST_STATE_CHANGE_ASYNC, "State change to PLAYING not ASYNC");
1033   ret =
1034       gst_element_get_state (pipeline, &current, &pending, GST_CLOCK_TIME_NONE);
1035   fail_if (ret != GST_STATE_CHANGE_SUCCESS, "State change to PLAYING failed");
1036   fail_if (current != GST_STATE_PLAYING, "State change to PLAYING failed");
1037   fail_if (pending != GST_STATE_VOID_PENDING, "State change to PLAYING failed");
1038 
1039   /* NULL => READY */
1040   ASSERT_STATE_CHANGE_MSG (bus, sink, GST_STATE_NULL, GST_STATE_READY, 201);
1041   ASSERT_STATE_CHANGE_MSG (bus, identity, GST_STATE_NULL, GST_STATE_READY, 202);
1042   ASSERT_STATE_CHANGE_MSG (bus, src, GST_STATE_NULL, GST_STATE_READY, 203);
1043   ASSERT_STATE_CHANGE_MSG (bus, pipeline, GST_STATE_NULL, GST_STATE_READY, 204);
1044 
1045   /* READY => PAUSED */
1046   /* because of pre-rolling, sink will return ASYNC on state
1047    * change and change state later when it has a buffer */
1048   GST_DEBUG ("popping READY -> PAUSED messages");
1049   ASSERT_STATE_CHANGE_MSG (bus, identity, GST_STATE_READY, GST_STATE_PAUSED,
1050       205);
1051 #if 0
1052   /* From here on, all bets are off. Usually the source changes state next,
1053    * but it might just as well be that the first buffer produced by the
1054    * source reaches the sink before the source has finished its state change,
1055    * in which case the sink will commit its new state before the source ...  */
1056   ASSERT_STATE_CHANGE_MSG (bus, src, GST_STATE_READY, GST_STATE_PAUSED, 206);
1057   ASSERT_STATE_CHANGE_MSG (bus, sink, GST_STATE_READY, GST_STATE_PAUSED, 207);
1058 #else
1059   pop_state_changed (bus, 2);   /* pop remaining ready => paused messages off the bus */
1060   ASSERT_STATE_CHANGE_MSG (bus, pipeline, GST_STATE_READY, GST_STATE_PAUSED,
1061       208);
1062   pop_async_done (bus);
1063 
1064   /* PAUSED => PLAYING */
1065   GST_DEBUG ("popping PAUSED -> PLAYING messages");
1066   ASSERT_STATE_CHANGE_MSG (bus, sink, GST_STATE_PAUSED, GST_STATE_PLAYING, 209);
1067   ASSERT_STATE_CHANGE_MSG (bus, identity, GST_STATE_PAUSED, GST_STATE_PLAYING,
1068       210);
1069   ASSERT_STATE_CHANGE_MSG (bus, src, GST_STATE_PAUSED, GST_STATE_PLAYING, 211);
1070   ASSERT_STATE_CHANGE_MSG (bus, pipeline, GST_STATE_PAUSED, GST_STATE_PLAYING,
1071       212);
1072 #endif
1073 
1074   /* don't set to NULL that will set the bus flushing and kill our messages */
1075   ret = gst_element_set_state (pipeline, GST_STATE_READY);
1076   fail_if (ret != GST_STATE_CHANGE_SUCCESS, "State change to READY failed");
1077 
1078   /* TODO: do we need to check downwards state change order as well? */
1079   pop_state_changed (bus, 4);   /* pop playing => paused messages off the bus */
1080   pop_state_changed (bus, 4);   /* pop paused => ready messages off the bus */
1081 
1082   GST_DEBUG ("waiting for pipeline to reach refcount 1");
1083   while (GST_OBJECT_REFCOUNT_VALUE (pipeline) > 1)
1084     THREAD_SWITCH ();
1085 
1086   GST_DEBUG ("checking refcount");
1087   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
1088   ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
1089   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
1090 
1091   ret = gst_element_set_state (pipeline, GST_STATE_NULL);
1092   fail_if (ret != GST_STATE_CHANGE_SUCCESS, "State change to NULL failed");
1093 
1094   GST_DEBUG ("checking refcount");
1095   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
1096   ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
1097   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
1098 
1099   GST_DEBUG ("cleanup");
1100   gst_object_unref (bus);
1101   gst_object_unref (pipeline);
1102 }
1103 
1104 GST_END_TEST;
1105 
GST_START_TEST(test_children_state_change_order_two_sink)1106 GST_START_TEST (test_children_state_change_order_two_sink)
1107 {
1108   GstElement *src, *tee, *identity, *sink1, *sink2, *pipeline;
1109   GstStateChangeReturn ret;
1110   GstBus *bus;
1111 
1112   pipeline = gst_pipeline_new (NULL);
1113   fail_unless (pipeline != NULL, "Could not create pipeline");
1114 
1115   bus = gst_element_get_bus (pipeline);
1116   fail_unless (bus != NULL, "Pipeline has no bus?!");
1117 
1118   src = gst_element_factory_make ("fakesrc", NULL);
1119   fail_if (src == NULL, "Could not create fakesrc");
1120 
1121   tee = gst_element_factory_make ("tee", NULL);
1122   fail_if (tee == NULL, "Could not create tee");
1123 
1124   identity = gst_element_factory_make ("identity", NULL);
1125   fail_if (identity == NULL, "Could not create identity");
1126 
1127   sink1 = gst_element_factory_make ("fakesink", NULL);
1128   fail_if (sink1 == NULL, "Could not create fakesink1");
1129 
1130   sink2 = gst_element_factory_make ("fakesink", NULL);
1131   fail_if (sink2 == NULL, "Could not create fakesink2");
1132 
1133   gst_bin_add_many (GST_BIN (pipeline), src, tee, identity, sink1, sink2, NULL);
1134 
1135   fail_unless (gst_element_link (src, tee) == TRUE);
1136   fail_unless (gst_element_link (tee, identity) == TRUE);
1137   fail_unless (gst_element_link (identity, sink1) == TRUE);
1138   fail_unless (gst_element_link (tee, sink2) == TRUE);
1139 
1140   ret = gst_element_set_state (pipeline, GST_STATE_READY);
1141   fail_if (ret != GST_STATE_CHANGE_SUCCESS, "State change to READY failed");
1142 
1143   /* NULL => READY */
1144   {
1145     GstMessage *msg;
1146     GstState old = 0, new = 0, pending = 0;
1147     GstObject *first, *second;
1148 
1149     msg = gst_bus_poll (bus, GST_MESSAGE_STATE_CHANGED, GST_SECOND);
1150     fail_if (msg == NULL, "No state change message within 1 second (#201)");
1151 
1152     gst_message_parse_state_changed (msg, &old, &new, &pending);
1153     first = gst_object_ref (msg->src);
1154 
1155     fail_if (first != GST_OBJECT (sink1) && first != GST_OBJECT (sink2),
1156         "sink1 or sink2 should have changed state next #(202)");
1157     gst_message_unref (msg);
1158 
1159     msg = gst_bus_poll (bus, GST_MESSAGE_STATE_CHANGED, GST_SECOND);
1160     fail_if (msg == NULL, "No state change message within 1 second (#201)");
1161 
1162     gst_message_parse_state_changed (msg, &old, &new, &pending);
1163     second = gst_object_ref (msg->src);
1164 
1165     fail_if (second != GST_OBJECT (sink1) && second != GST_OBJECT (sink2),
1166         "sink1 or sink2 should have changed state next #(202)");
1167     gst_message_unref (msg);
1168 
1169     fail_if (second == first, "got state change from same object");
1170 
1171     gst_object_unref (first);
1172     gst_object_unref (second);
1173   }
1174   ASSERT_STATE_CHANGE_MSG (bus, identity, GST_STATE_NULL, GST_STATE_READY, 203);
1175   ASSERT_STATE_CHANGE_MSG (bus, tee, GST_STATE_NULL, GST_STATE_READY, 204);
1176   ASSERT_STATE_CHANGE_MSG (bus, src, GST_STATE_NULL, GST_STATE_READY, 205);
1177   ASSERT_STATE_CHANGE_MSG (bus, pipeline, GST_STATE_NULL, GST_STATE_READY, 206);
1178 
1179   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
1180   ASSERT_OBJECT_REFCOUNT (tee, "tee", 1);
1181   ASSERT_OBJECT_REFCOUNT (identity, "identity", 1);
1182   ASSERT_OBJECT_REFCOUNT (sink1, "sink1", 1);
1183   ASSERT_OBJECT_REFCOUNT (sink2, "sink2", 1);
1184   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
1185 
1186   ret = gst_element_set_state (pipeline, GST_STATE_NULL);
1187   fail_if (ret != GST_STATE_CHANGE_SUCCESS, "State change to NULL failed");
1188 
1189   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
1190   ASSERT_OBJECT_REFCOUNT (tee, "tee", 1);
1191   ASSERT_OBJECT_REFCOUNT (identity, "identity", 1);
1192   ASSERT_OBJECT_REFCOUNT (sink1, "sink1", 1);
1193   ASSERT_OBJECT_REFCOUNT (sink2, "sink2", 1);
1194   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
1195 
1196   gst_object_unref (bus);
1197   gst_object_unref (pipeline);
1198 }
1199 
1200 GST_END_TEST;
1201 
GST_START_TEST(test_iterate_sorted)1202 GST_START_TEST (test_iterate_sorted)
1203 {
1204   GstElement *src, *tee, *identity, *sink1, *sink2, *pipeline, *bin;
1205   GstIterator *it;
1206   GValue elem = { 0, };
1207 
1208   pipeline = gst_pipeline_new (NULL);
1209   fail_unless (pipeline != NULL, "Could not create pipeline");
1210 
1211   bin = gst_bin_new (NULL);
1212   fail_unless (bin != NULL, "Could not create bin");
1213 
1214   src = gst_element_factory_make ("fakesrc", NULL);
1215   fail_if (src == NULL, "Could not create fakesrc");
1216 
1217   tee = gst_element_factory_make ("tee", NULL);
1218   fail_if (tee == NULL, "Could not create tee");
1219 
1220   sink1 = gst_element_factory_make ("fakesink", NULL);
1221   fail_if (sink1 == NULL, "Could not create fakesink1");
1222 
1223   gst_bin_add_many (GST_BIN (bin), src, tee, sink1, NULL);
1224 
1225   fail_unless (gst_element_link (src, tee) == TRUE);
1226   fail_unless (gst_element_link (tee, sink1) == TRUE);
1227 
1228   identity = gst_element_factory_make ("identity", NULL);
1229   fail_if (identity == NULL, "Could not create identity");
1230 
1231   sink2 = gst_element_factory_make ("fakesink", NULL);
1232   fail_if (sink2 == NULL, "Could not create fakesink2");
1233 
1234   gst_bin_add_many (GST_BIN (pipeline), bin, identity, sink2, NULL);
1235 
1236   fail_unless (gst_element_link (tee, identity) == TRUE);
1237   fail_unless (gst_element_link (identity, sink2) == TRUE);
1238 
1239   it = gst_bin_iterate_sorted (GST_BIN (pipeline));
1240   fail_unless (gst_iterator_next (it, &elem) == GST_ITERATOR_OK);
1241   fail_unless (g_value_get_object (&elem) == (gpointer) sink2);
1242   g_value_reset (&elem);
1243 
1244   fail_unless (gst_iterator_next (it, &elem) == GST_ITERATOR_OK);
1245   fail_unless (g_value_get_object (&elem) == (gpointer) identity);
1246   g_value_reset (&elem);
1247 
1248   fail_unless (gst_iterator_next (it, &elem) == GST_ITERATOR_OK);
1249   fail_unless (g_value_get_object (&elem) == (gpointer) bin);
1250   g_value_reset (&elem);
1251 
1252   g_value_unset (&elem);
1253   gst_iterator_free (it);
1254 
1255   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
1256   gst_object_unref (pipeline);
1257 }
1258 
1259 GST_END_TEST;
1260 
GST_START_TEST(test_iterate_sorted_unlinked)1261 GST_START_TEST (test_iterate_sorted_unlinked)
1262 {
1263   GstElement *pipeline, *src, *sink, *identity;
1264   GstIterator *it;
1265   GValue elem = { 0, };
1266 
1267   pipeline = gst_pipeline_new (NULL);
1268   fail_unless (pipeline != NULL, "Could not create pipeline");
1269 
1270   src = gst_element_factory_make ("fakesrc", NULL);
1271   fail_if (src == NULL, "Could not create fakesrc");
1272 
1273   sink = gst_element_factory_make ("fakesink", NULL);
1274   fail_if (sink == NULL, "Could not create fakesink1");
1275 
1276   identity = gst_element_factory_make ("identity", NULL);
1277   fail_if (identity == NULL, "Could not create identity");
1278 
1279   gst_bin_add_many (GST_BIN (pipeline), sink, identity, src, NULL);
1280 
1281   /* If elements aren't linked, we should end up with:
1282    * * sink elements always first
1283    * * source elements always last
1284    * * any other elements in between
1285    */
1286 
1287   it = gst_bin_iterate_sorted (GST_BIN (pipeline));
1288   fail_unless (gst_iterator_next (it, &elem) == GST_ITERATOR_OK);
1289   fail_unless (g_value_get_object (&elem) == (gpointer) sink);
1290   g_value_reset (&elem);
1291 
1292   fail_unless (gst_iterator_next (it, &elem) == GST_ITERATOR_OK);
1293   fail_unless (g_value_get_object (&elem) == (gpointer) identity);
1294   g_value_reset (&elem);
1295 
1296   fail_unless (gst_iterator_next (it, &elem) == GST_ITERATOR_OK);
1297   fail_unless (g_value_get_object (&elem) == (gpointer) src);
1298   g_value_reset (&elem);
1299 
1300   g_value_unset (&elem);
1301   gst_iterator_free (it);
1302 
1303   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
1304   gst_object_unref (pipeline);
1305 }
1306 
1307 GST_END_TEST;
1308 
1309 static void
test_link_structure_change_state_changed_sync_cb(GstBus * bus,GstMessage * message,gpointer data)1310 test_link_structure_change_state_changed_sync_cb (GstBus * bus,
1311     GstMessage * message, gpointer data)
1312 {
1313   GstPipeline *pipeline = GST_PIPELINE (data);
1314   GstElement *src, *identity, *sink;
1315   GstState old, snew, pending;
1316 
1317   sink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
1318   fail_unless (sink != NULL, "Could not get sink");
1319 
1320   gst_message_parse_state_changed (message, &old, &snew, &pending);
1321   if (message->src != GST_OBJECT (sink) || snew != GST_STATE_READY) {
1322     gst_object_unref (sink);
1323     return;
1324   }
1325 
1326   src = gst_bin_get_by_name (GST_BIN (pipeline), "src");
1327   fail_unless (src != NULL, "Could not get src");
1328 
1329   identity = gst_bin_get_by_name (GST_BIN (pipeline), "identity");
1330   fail_unless (identity != NULL, "Could not get identity");
1331 
1332   /* link src to identity, the pipeline should detect the new link and
1333    * resync the state change */
1334   fail_unless (gst_element_link (src, identity) == TRUE);
1335 
1336   gst_object_unref (src);
1337   gst_object_unref (identity);
1338   gst_object_unref (sink);
1339 }
1340 
GST_START_TEST(test_link_structure_change)1341 GST_START_TEST (test_link_structure_change)
1342 {
1343   GstElement *src, *identity, *sink, *pipeline;
1344   GstBus *bus;
1345   GstState state;
1346 
1347   pipeline = gst_pipeline_new (NULL);
1348   fail_unless (pipeline != NULL, "Could not create pipeline");
1349 
1350   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
1351   fail_unless (bus != NULL, "Could not get bus");
1352 
1353   /* use the sync signal handler to link elements while the pipeline is still
1354    * doing the state change */
1355   gst_bus_set_sync_handler (bus, gst_bus_sync_signal_handler, pipeline, NULL);
1356   g_object_connect (bus, "signal::sync-message::state-changed",
1357       G_CALLBACK (test_link_structure_change_state_changed_sync_cb), pipeline,
1358       NULL);
1359 
1360   src = gst_element_factory_make ("fakesrc", "src");
1361   fail_if (src == NULL, "Could not create fakesrc");
1362 
1363   identity = gst_element_factory_make ("identity", "identity");
1364   fail_if (identity == NULL, "Could not create identity");
1365 
1366   sink = gst_element_factory_make ("fakesink", "sink");
1367   fail_if (sink == NULL, "Could not create fakesink1");
1368 
1369   gst_bin_add_many (GST_BIN (pipeline), src, identity, sink, NULL);
1370 
1371   gst_element_set_state (pipeline, GST_STATE_READY);
1372   gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
1373 
1374   /* the state change will be done on src only if the pipeline correctly resyncs
1375    * after that fakesrc has been linked to identity */
1376   gst_element_get_state (src, &state, NULL, 0);
1377   fail_unless_equals_int (state, GST_STATE_READY);
1378 
1379   /* clean up */
1380   gst_element_set_state (pipeline, GST_STATE_NULL);
1381   gst_object_unref (bus);
1382   gst_object_unref (pipeline);
1383 }
1384 
1385 GST_END_TEST;
1386 
1387 static GstBusSyncReply
sync_handler_remove_sink(GstBus * bus,GstMessage * message,gpointer data)1388 sync_handler_remove_sink (GstBus * bus, GstMessage * message, gpointer data)
1389 {
1390   if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ERROR) {
1391     GstElement *child;
1392 
1393     child = gst_bin_get_by_name (GST_BIN (data), "fakesink");
1394     fail_unless (child != NULL, "Could not find fakesink");
1395 
1396     gst_bin_remove (GST_BIN (data), child);
1397     gst_object_unref (child);
1398   }
1399   return GST_BUS_PASS;
1400 }
1401 
GST_START_TEST(test_state_failure_remove)1402 GST_START_TEST (test_state_failure_remove)
1403 {
1404   GstElement *src, *sink, *pipeline;
1405   GstBus *bus;
1406   GstStateChangeReturn ret;
1407 
1408   pipeline = gst_pipeline_new (NULL);
1409   fail_unless (pipeline != NULL, "Could not create pipeline");
1410 
1411   src = gst_element_factory_make ("fakesrc", "fakesrc");
1412   fail_unless (src != NULL, "Could not create fakesrc");
1413 
1414   sink = gst_element_factory_make ("fakesink", "fakesink");
1415   fail_unless (sink != NULL, "Could not create fakesink");
1416 
1417   g_object_set (sink, "state-error", 1, NULL);
1418 
1419   gst_bin_add (GST_BIN (pipeline), src);
1420   gst_bin_add (GST_BIN (pipeline), sink);
1421 
1422   gst_element_link (src, sink);
1423 
1424   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
1425   fail_unless (bus != NULL, "Could not get bus");
1426 
1427   gst_bus_set_sync_handler (bus, sync_handler_remove_sink, pipeline, NULL);
1428 
1429   ret = gst_element_set_state (pipeline, GST_STATE_READY);
1430   fail_unless (ret == GST_STATE_CHANGE_SUCCESS,
1431       "did not get state change success");
1432 
1433   gst_element_set_state (pipeline, GST_STATE_NULL);
1434 
1435   gst_object_unref (bus);
1436   gst_object_unref (pipeline);
1437 }
1438 
1439 GST_END_TEST;
1440 
GST_START_TEST(test_many_bins)1441 GST_START_TEST (test_many_bins)
1442 {
1443   GstStateChangeReturn ret;
1444   GstElement *src, *sink, *pipeline, *last_bin = NULL;
1445   gint i;
1446 
1447 #define NUM_BINS 2000
1448 
1449   pipeline = gst_pipeline_new (NULL);
1450   fail_unless (pipeline != NULL, "Could not create pipeline");
1451 
1452   src = gst_element_factory_make ("fakesrc", "fakesrc");
1453   fail_unless (src != NULL, "Could not create fakesrc");
1454   g_object_set (src, "num-buffers", 3, NULL);
1455 
1456   sink = gst_element_factory_make ("fakesink", "fakesink");
1457   fail_unless (sink != NULL, "Could not create fakesink");
1458 
1459   gst_bin_add (GST_BIN (pipeline), src);
1460   gst_bin_add (GST_BIN (pipeline), sink);
1461 
1462   for (i = 0; i < NUM_BINS; ++i) {
1463     GstElement *bin, *identity;
1464     GstPad *srcpad, *sinkpad;
1465 
1466     bin = gst_bin_new (NULL);
1467     fail_unless (bin != NULL, "Could not create bin %d", i);
1468     identity = gst_element_factory_make ("identity", "identity");
1469     fail_unless (identity != NULL, "Could not create identity %d", i);
1470     g_object_set (identity, "silent", TRUE, NULL);
1471     gst_bin_add (GST_BIN (bin), identity);
1472     sinkpad = gst_element_get_static_pad (identity, "sink");
1473     srcpad = gst_element_get_static_pad (identity, "src");
1474     gst_element_add_pad (bin, gst_ghost_pad_new ("sink", sinkpad));
1475     gst_element_add_pad (bin, gst_ghost_pad_new ("src", srcpad));
1476     gst_object_unref (sinkpad);
1477     gst_object_unref (srcpad);
1478 
1479     gst_bin_add (GST_BIN (pipeline), bin);
1480 
1481     if (last_bin == NULL) {
1482       srcpad = gst_element_get_static_pad (src, "src");
1483     } else {
1484       srcpad = gst_element_get_static_pad (last_bin, "src");
1485     }
1486     sinkpad = gst_element_get_static_pad (bin, "sink");
1487     gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_NOTHING);
1488     gst_object_unref (sinkpad);
1489     gst_object_unref (srcpad);
1490 
1491 
1492     last_bin = bin;
1493 
1494     /* insert some queues to limit the number of function calls in a row */
1495     if ((i % 100) == 0) {
1496       GstElement *q = gst_element_factory_make ("queue", NULL);
1497 
1498       GST_LOG ("bin #%d, inserting queue", i);
1499       gst_bin_add (GST_BIN (pipeline), q);
1500       fail_unless (gst_element_link (last_bin, q));
1501       last_bin = q;
1502     }
1503   }
1504 
1505   fail_unless (gst_element_link (last_bin, sink));
1506 
1507   ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
1508   fail_unless_equals_int (ret, GST_STATE_CHANGE_ASYNC);
1509 
1510   for (i = 0; i < 15; ++i) {
1511     GST_INFO ("waiting for preroll ...");
1512     ret = gst_element_get_state (pipeline, NULL, NULL, GST_SECOND);
1513     if (ret != GST_STATE_CHANGE_ASYNC)
1514       break;
1515   }
1516   fail_unless_equals_int (ret, GST_STATE_CHANGE_SUCCESS);
1517 
1518   gst_element_set_state (pipeline, GST_STATE_NULL);
1519   gst_object_unref (pipeline);
1520 }
1521 
1522 GST_END_TEST;
1523 
1524 static GstPadProbeReturn
fakesrc_pad_blocked_cb(GstPad * pad,GstPadProbeInfo * info,void * arg)1525 fakesrc_pad_blocked_cb (GstPad * pad, GstPadProbeInfo * info, void *arg)
1526 {
1527   GstPipeline *pipeline = (GstPipeline *) arg;
1528   GstElement *src, *sink;
1529 
1530   src = gst_bin_get_by_name (GST_BIN (pipeline), "fakesrc");
1531   fail_unless (src != NULL, "Could not get fakesrc");
1532 
1533   sink = gst_element_factory_make ("fakesink", "fakesink");
1534   fail_unless (sink != NULL, "Could not create fakesink");
1535 
1536   g_object_set (sink, "state-error", 1, NULL);
1537   gst_bin_add (GST_BIN (pipeline), sink);
1538 
1539   gst_element_link (src, sink);
1540   gst_element_sync_state_with_parent (sink);
1541   gst_object_unref (src);
1542 
1543   return GST_PAD_PROBE_REMOVE;
1544 }
1545 
GST_START_TEST(test_state_failure_unref)1546 GST_START_TEST (test_state_failure_unref)
1547 {
1548   GstElement *src, *pipeline;
1549   GstPad *srcpad;
1550   GstBus *bus;
1551   GstStateChangeReturn ret;
1552   GstMessage *msg;
1553 
1554   pipeline = gst_pipeline_new (NULL);
1555   fail_unless (pipeline != NULL, "Could not create pipeline");
1556 
1557   src = gst_element_factory_make ("fakesrc", "fakesrc");
1558   fail_unless (src != NULL, "Could not create fakesrc");
1559 
1560   srcpad = gst_element_get_static_pad (src, "src");
1561   fail_unless (srcpad != NULL, "Could not get fakesrc srcpad");
1562 
1563   gst_pad_add_probe (srcpad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
1564       fakesrc_pad_blocked_cb, pipeline, NULL);
1565   gst_object_unref (srcpad);
1566 
1567   gst_bin_add (GST_BIN (pipeline), src);
1568 
1569   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
1570   fail_unless (bus != NULL, "Could not get bus");
1571 
1572   gst_element_set_state (pipeline, GST_STATE_PLAYING);
1573 
1574   /* Wait for an error message from our fakesink (added from the
1575      pad block callback). */
1576   msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, GST_SECOND);
1577   fail_if (msg == NULL, "No error message within 1 second");
1578   gst_message_unref (msg);
1579 
1580   /* Check that after this failure, we can still stop, and then unref, the
1581      pipeline. This should always be possible. */
1582   ret = gst_element_set_state (pipeline, GST_STATE_NULL);
1583   fail_unless (ret == GST_STATE_CHANGE_SUCCESS, "downward state change failed");
1584 
1585   gst_object_unref (bus);
1586   gst_object_unref (pipeline);
1587 }
1588 
1589 GST_END_TEST;
1590 
1591 static void
on_sync_bus_error(GstBus * bus,GstMessage * msg)1592 on_sync_bus_error (GstBus * bus, GstMessage * msg)
1593 {
1594   fail_if (msg != NULL);
1595 }
1596 
GST_START_TEST(test_state_change_skip)1597 GST_START_TEST (test_state_change_skip)
1598 {
1599   GstElement *sink, *pipeline;
1600   GstStateChangeReturn ret;
1601   GstBus *bus;
1602 
1603   pipeline = gst_pipeline_new (NULL);
1604   fail_unless (pipeline != NULL, "Could not create pipeline");
1605 
1606   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
1607   fail_unless (bus != NULL, "Could not get bus");
1608 
1609   /* no errors */
1610   gst_bus_enable_sync_message_emission (bus);
1611   g_signal_connect (bus, "sync-message::error", (GCallback) on_sync_bus_error,
1612       NULL);
1613 
1614   sink = gst_element_factory_make ("fakesink", "fakesink");
1615   fail_unless (sink != NULL, "Could not create fakesink");
1616   gst_element_set_state (sink, GST_STATE_PAUSED);
1617 
1618   g_object_set (sink, "state-error", 5, NULL);
1619 
1620   gst_bin_add (GST_BIN (pipeline), sink);
1621   gst_element_set_state (pipeline, GST_STATE_PLAYING);
1622 
1623   g_object_set (sink, "state-error", 0, NULL);
1624 
1625   /* Check that after this failure, we can still stop, and then unref, the
1626      pipeline. This should always be possible. */
1627   ret = gst_element_set_state (pipeline, GST_STATE_NULL);
1628   fail_unless (ret == GST_STATE_CHANGE_SUCCESS, "downward state change failed");
1629 
1630   gst_object_unref (pipeline);
1631   gst_object_unref (bus);
1632 }
1633 
1634 GST_END_TEST;
1635 
GST_START_TEST(test_duration_is_max)1636 GST_START_TEST (test_duration_is_max)
1637 {
1638   GstElement *bin, *src[3], *sink[3];
1639   GstStateChangeReturn state_res;
1640   GstFormat format = GST_FORMAT_BYTES;
1641   gboolean res;
1642   gint64 duration;
1643   GstBus *bus;
1644 
1645   GST_INFO ("preparing test");
1646 
1647   /* build pipeline */
1648   bin = gst_pipeline_new ("pipeline");
1649 
1650   /* 3 sources, an adder and a fakesink */
1651   src[0] = gst_element_factory_make ("fakesrc", NULL);
1652   src[1] = gst_element_factory_make ("fakesrc", NULL);
1653   src[2] = gst_element_factory_make ("fakesrc", NULL);
1654   sink[0] = gst_element_factory_make ("fakesink", NULL);
1655   sink[1] = gst_element_factory_make ("fakesink", NULL);
1656   sink[2] = gst_element_factory_make ("fakesink", NULL);
1657   gst_bin_add_many (GST_BIN (bin), src[0], src[1], src[2], sink[0], sink[1],
1658       sink[2], NULL);
1659 
1660   gst_element_link (src[0], sink[0]);
1661   gst_element_link (src[1], sink[1]);
1662   gst_element_link (src[2], sink[2]);
1663 
1664   /* irks, duration is reset on basesrc */
1665   state_res = gst_element_set_state (bin, GST_STATE_PAUSED);
1666   fail_unless (state_res != GST_STATE_CHANGE_FAILURE, NULL);
1667 
1668   /* set durations on src */
1669   GST_BASE_SRC (src[0])->segment.duration = 1000;
1670   GST_BASE_SRC (src[1])->segment.duration = 3000;
1671   GST_BASE_SRC (src[2])->segment.duration = 2000;
1672 
1673   /* set to playing */
1674   state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
1675   fail_unless (state_res != GST_STATE_CHANGE_FAILURE, NULL);
1676 
1677   /* wait for completion */
1678   state_res =
1679       gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
1680       GST_CLOCK_TIME_NONE);
1681   fail_unless (state_res != GST_STATE_CHANGE_FAILURE, NULL);
1682 
1683   res = gst_element_query_duration (GST_ELEMENT (bin), format, &duration);
1684   fail_unless (res, NULL);
1685 
1686   ck_assert_int_eq (duration, 3000);
1687 
1688   bus = gst_element_get_bus (bin);
1689   gst_bus_set_flushing (bus, TRUE);
1690   gst_object_unref (bus);
1691 
1692   gst_element_set_state (bin, GST_STATE_NULL);
1693   gst_object_unref (bin);
1694 }
1695 
1696 GST_END_TEST;
1697 
GST_START_TEST(test_duration_unknown_overrides)1698 GST_START_TEST (test_duration_unknown_overrides)
1699 {
1700   GstElement *bin, *src[3], *sink[3];
1701   GstStateChangeReturn state_res;
1702   GstFormat format = GST_FORMAT_BYTES;
1703   gboolean res;
1704   gint64 duration;
1705   GstBus *bus;
1706 
1707   GST_INFO ("preparing test");
1708 
1709   /* build pipeline */
1710   bin = gst_pipeline_new ("pipeline");
1711 
1712   /* 3 sources, an adder and a fakesink */
1713   src[0] = gst_element_factory_make ("fakesrc", NULL);
1714   src[1] = gst_element_factory_make ("fakesrc", NULL);
1715   src[2] = gst_element_factory_make ("fakesrc", NULL);
1716   sink[0] = gst_element_factory_make ("fakesink", NULL);
1717   sink[1] = gst_element_factory_make ("fakesink", NULL);
1718   sink[2] = gst_element_factory_make ("fakesink", NULL);
1719   gst_bin_add_many (GST_BIN (bin), src[0], src[1], src[2], sink[0], sink[1],
1720       sink[2], NULL);
1721 
1722   gst_element_link (src[0], sink[0]);
1723   gst_element_link (src[1], sink[1]);
1724   gst_element_link (src[2], sink[2]);
1725 
1726   /* irks, duration is reset on basesrc */
1727   state_res = gst_element_set_state (bin, GST_STATE_PAUSED);
1728   fail_unless (state_res != GST_STATE_CHANGE_FAILURE, NULL);
1729 
1730   /* set durations on src */
1731   GST_BASE_SRC (src[0])->segment.duration = GST_CLOCK_TIME_NONE;
1732   GST_BASE_SRC (src[1])->segment.duration = 3000;
1733   GST_BASE_SRC (src[2])->segment.duration = 2000;
1734 
1735   /* set to playing */
1736   state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
1737   fail_unless (state_res != GST_STATE_CHANGE_FAILURE, NULL);
1738 
1739   /* wait for completion */
1740   state_res =
1741       gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
1742       GST_CLOCK_TIME_NONE);
1743   fail_unless (state_res != GST_STATE_CHANGE_FAILURE, NULL);
1744 
1745   res = gst_element_query_duration (GST_ELEMENT (bin), format, &duration);
1746   fail_unless (res, NULL);
1747 
1748   ck_assert_int_eq (duration, GST_CLOCK_TIME_NONE);
1749 
1750   bus = gst_element_get_bus (bin);
1751   gst_bus_set_flushing (bus, TRUE);
1752   gst_object_unref (bus);
1753 
1754   gst_element_set_state (bin, GST_STATE_NULL);
1755   gst_object_unref (bin);
1756 }
1757 
1758 GST_END_TEST;
1759 
1760 static gboolean
element_in_list(GList ** list,GstElement * element)1761 element_in_list (GList ** list, GstElement * element)
1762 {
1763   GList *l = g_list_find (*list, element);
1764 
1765   if (l == NULL)
1766     return FALSE;
1767 
1768   *list = g_list_delete_link (*list, l);
1769   return TRUE;
1770 }
1771 
1772 #define element_was_added(e) element_in_list(&added,e)
1773 #define element_was_removed(e) element_in_list(&removed,e)
1774 
1775 static void
add_cb(GstBin * pipeline,GstBin * bin,GstElement * element,GList ** list)1776 add_cb (GstBin * pipeline, GstBin * bin, GstElement * element, GList ** list)
1777 {
1778   fail_unless (GST_OBJECT_PARENT (element) == GST_OBJECT_CAST (bin));
1779 
1780   *list = g_list_prepend (*list, element);
1781 }
1782 
1783 static void
remove_cb(GstBin * pipeline,GstBin * bin,GstElement * element,GList ** list)1784 remove_cb (GstBin * pipeline, GstBin * bin, GstElement * element, GList ** list)
1785 {
1786   *list = g_list_prepend (*list, element);
1787 }
1788 
GST_START_TEST(test_deep_added_removed)1789 GST_START_TEST (test_deep_added_removed)
1790 {
1791   GstElement *pipe, *e, *bin0, *bin1;
1792   gulong id_removed, id_added;
1793   GList *removed = NULL;
1794   GList *added = NULL;
1795 
1796   pipe = gst_pipeline_new (NULL);
1797 
1798   id_added = g_signal_connect (pipe, "deep-element-added",
1799       G_CALLBACK (add_cb), &added);
1800   id_removed = g_signal_connect (pipe, "deep-element-removed",
1801       G_CALLBACK (remove_cb), &removed);
1802 
1803   /* simple add/remove */
1804   e = gst_element_factory_make ("identity", NULL);
1805   gst_bin_add (GST_BIN (pipe), e);
1806   fail_unless (element_was_added (e));
1807   gst_bin_remove (GST_BIN (pipe), e);
1808   fail_unless (element_was_removed (e));
1809 
1810   /* let's try with a deeper hierarchy, construct it from top-level down */
1811   bin0 = gst_bin_new (NULL);
1812   gst_bin_add (GST_BIN (pipe), bin0);
1813   bin1 = gst_bin_new (NULL);
1814   gst_bin_add (GST_BIN (bin0), bin1);
1815   e = gst_element_factory_make ("identity", NULL);
1816   gst_bin_add (GST_BIN (bin1), e);
1817   fail_unless (element_was_added (bin0));
1818   fail_unless (element_was_added (bin1));
1819   fail_unless (element_was_added (e));
1820   fail_unless (added == NULL);
1821   fail_unless (removed == NULL);
1822 
1823   gst_object_ref (e);           /* keep e alive */
1824   gst_bin_remove (GST_BIN (bin1), e);
1825   fail_unless (element_was_removed (e));
1826   fail_unless (added == NULL);
1827   fail_unless (removed == NULL);
1828 
1829   /* now add existing bin hierarchy to pipeline (first remove it so we can re-add it) */
1830   gst_object_ref (bin0);        /* keep bin0 alive */
1831   gst_bin_remove (GST_BIN (pipe), bin0);
1832   fail_unless (element_was_removed (bin0));
1833   fail_unless (element_was_removed (bin1));
1834   fail_unless (added == NULL);
1835   fail_unless (removed == NULL);
1836 
1837   /* re-adding element to removed bin should not trigger our callbacks */
1838   gst_bin_add (GST_BIN (bin1), e);
1839   fail_unless (added == NULL);
1840   fail_unless (removed == NULL);
1841 
1842   gst_bin_add (GST_BIN (pipe), bin0);
1843   fail_unless (element_was_added (bin0));
1844   fail_unless (element_was_added (bin1));
1845   fail_unless (element_was_added (e));
1846   fail_unless (added == NULL);
1847   fail_unless (removed == NULL);
1848   gst_object_unref (bin0);
1849   gst_object_unref (e);
1850 
1851   /* disconnect signals, unref will trigger remove callbacks otherwise */
1852   g_signal_handler_disconnect (pipe, id_added);
1853   g_signal_handler_disconnect (pipe, id_removed);
1854 
1855   gst_object_unref (pipe);
1856 }
1857 
1858 GST_END_TEST;
1859 
1860 #define _GST_CHECK_BIN_SUPPRESSED_FLAGS(element_flags, suppressed_flags, \
1861     expected_flags) \
1862 G_STMT_START { \
1863   GstBin *bin = GST_BIN (gst_bin_new ("test-bin")); \
1864   GstElement *element = gst_element_factory_make ("queue", "test-q"); \
1865   GstElementFlags natural_flags = GST_OBJECT_FLAGS (bin); \
1866   GST_OBJECT_FLAG_SET (element, element_flags); \
1867   gst_bin_set_suppressed_flags (bin, suppressed_flags); \
1868   gst_bin_add (bin, element); \
1869   fail_unless ((natural_flags | GST_OBJECT_FLAGS (bin)) \
1870       == expected_flags); \
1871   gst_object_unref (bin); \
1872 } G_STMT_END
1873 
GST_START_TEST(test_suppressed_flags)1874 GST_START_TEST (test_suppressed_flags)
1875 {
1876   _GST_CHECK_BIN_SUPPRESSED_FLAGS (GST_ELEMENT_FLAG_SOURCE,
1877       0, GST_ELEMENT_FLAG_SOURCE);
1878   _GST_CHECK_BIN_SUPPRESSED_FLAGS (GST_ELEMENT_FLAG_SOURCE,
1879       GST_ELEMENT_FLAG_SOURCE, 0);
1880   _GST_CHECK_BIN_SUPPRESSED_FLAGS (GST_ELEMENT_FLAG_SOURCE,
1881       GST_ELEMENT_FLAG_SINK, GST_ELEMENT_FLAG_SOURCE);
1882   _GST_CHECK_BIN_SUPPRESSED_FLAGS (GST_ELEMENT_FLAG_SOURCE |
1883       GST_ELEMENT_FLAG_PROVIDE_CLOCK,
1884       GST_ELEMENT_FLAG_PROVIDE_CLOCK, GST_ELEMENT_FLAG_SOURCE);
1885 
1886   _GST_CHECK_BIN_SUPPRESSED_FLAGS (GST_ELEMENT_FLAG_SINK,
1887       0, GST_ELEMENT_FLAG_SINK);
1888   _GST_CHECK_BIN_SUPPRESSED_FLAGS (GST_ELEMENT_FLAG_SINK,
1889       GST_ELEMENT_FLAG_SINK, 0);
1890   _GST_CHECK_BIN_SUPPRESSED_FLAGS (GST_ELEMENT_FLAG_SINK,
1891       GST_ELEMENT_FLAG_SOURCE, GST_ELEMENT_FLAG_SINK);
1892 
1893   _GST_CHECK_BIN_SUPPRESSED_FLAGS (GST_ELEMENT_FLAG_PROVIDE_CLOCK,
1894       0, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1895   _GST_CHECK_BIN_SUPPRESSED_FLAGS (GST_ELEMENT_FLAG_PROVIDE_CLOCK,
1896       GST_ELEMENT_FLAG_PROVIDE_CLOCK, 0);
1897   _GST_CHECK_BIN_SUPPRESSED_FLAGS (GST_ELEMENT_FLAG_PROVIDE_CLOCK,
1898       GST_ELEMENT_FLAG_REQUIRE_CLOCK, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1899 
1900   _GST_CHECK_BIN_SUPPRESSED_FLAGS (GST_ELEMENT_FLAG_REQUIRE_CLOCK,
1901       0, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1902   _GST_CHECK_BIN_SUPPRESSED_FLAGS (GST_ELEMENT_FLAG_REQUIRE_CLOCK,
1903       GST_ELEMENT_FLAG_REQUIRE_CLOCK, 0);
1904   _GST_CHECK_BIN_SUPPRESSED_FLAGS (GST_ELEMENT_FLAG_REQUIRE_CLOCK,
1905       GST_ELEMENT_FLAG_PROVIDE_CLOCK, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1906 }
1907 
1908 GST_END_TEST;
1909 
1910 
1911 #define _GST_CHECK_BIN_SUPPRESSED_FLAGS_REMOVAL(suppressed_flags) \
1912 G_STMT_START { \
1913   GstBin *bin = GST_BIN (gst_bin_new ("test-bin")); \
1914   GstElement *element = gst_element_factory_make ("identity", "test-i"); \
1915   GST_OBJECT_FLAG_SET (bin, suppressed_flags); \
1916   gst_bin_set_suppressed_flags (bin, suppressed_flags); \
1917   GST_OBJECT_FLAG_SET (element, suppressed_flags); \
1918   fail_unless ((suppressed_flags & GST_OBJECT_FLAGS (bin)) \
1919       == suppressed_flags); \
1920   gst_bin_add (bin, element); \
1921   fail_unless ((suppressed_flags & GST_OBJECT_FLAGS (bin)) \
1922       == suppressed_flags); \
1923   gst_bin_remove (bin, element); \
1924   fail_unless ((suppressed_flags & GST_OBJECT_FLAGS (bin)) \
1925       == suppressed_flags); \
1926   gst_object_unref (bin); \
1927 } G_STMT_END
1928 
GST_START_TEST(test_suppressed_flags_when_removing)1929 GST_START_TEST (test_suppressed_flags_when_removing)
1930 {
1931   _GST_CHECK_BIN_SUPPRESSED_FLAGS_REMOVAL (GST_ELEMENT_FLAG_SOURCE);
1932   _GST_CHECK_BIN_SUPPRESSED_FLAGS_REMOVAL (GST_ELEMENT_FLAG_SINK);
1933   _GST_CHECK_BIN_SUPPRESSED_FLAGS_REMOVAL (GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1934   _GST_CHECK_BIN_SUPPRESSED_FLAGS_REMOVAL (GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1935 }
1936 
1937 GST_END_TEST;
1938 
1939 static Suite *
gst_bin_suite(void)1940 gst_bin_suite (void)
1941 {
1942   Suite *s = suite_create ("GstBin");
1943   TCase *tc_chain = tcase_create ("bin tests");
1944 
1945   tcase_set_timeout (tc_chain, 0);
1946 
1947   suite_add_tcase (s, tc_chain);
1948   tcase_add_test (tc_chain, test_interface);
1949   tcase_add_test (tc_chain, test_iterate_all_by_element_factory_name);
1950   tcase_add_test (tc_chain, test_eos);
1951   tcase_add_test (tc_chain, test_eos_recheck);
1952   tcase_add_test (tc_chain, test_stream_start);
1953   tcase_add_test (tc_chain, test_children_state_change_order_flagged_sink);
1954   tcase_add_test (tc_chain, test_children_state_change_order_semi_sink);
1955   tcase_add_test (tc_chain, test_children_state_change_order_two_sink);
1956   tcase_add_test (tc_chain, test_message_state_changed);
1957   tcase_add_test (tc_chain, test_message_state_changed_child);
1958   tcase_add_test (tc_chain, test_message_state_changed_children);
1959   tcase_add_test (tc_chain, test_watch_for_state_change);
1960   tcase_add_test (tc_chain, test_state_change_error_message);
1961   tcase_add_test (tc_chain, test_add_linked);
1962   tcase_add_test (tc_chain, test_add_self);
1963   tcase_add_test (tc_chain, test_iterate_sorted);
1964   tcase_add_test (tc_chain, test_iterate_sorted_unlinked);
1965   tcase_add_test (tc_chain, test_link_structure_change);
1966   tcase_add_test (tc_chain, test_state_failure_remove);
1967   tcase_add_test (tc_chain, test_state_failure_unref);
1968   tcase_add_test (tc_chain, test_state_change_skip);
1969   tcase_add_test (tc_chain, test_duration_is_max);
1970   tcase_add_test (tc_chain, test_duration_unknown_overrides);
1971   tcase_add_test (tc_chain, test_deep_added_removed);
1972   tcase_add_test (tc_chain, test_suppressed_flags);
1973   tcase_add_test (tc_chain, test_suppressed_flags_when_removing);
1974 
1975   /* fails on OSX build bot for some reason, and is a bit silly anyway */
1976   if (0)
1977     tcase_add_test (tc_chain, test_many_bins);
1978 
1979   return s;
1980 }
1981 
1982 GST_CHECK_MAIN (gst_bin);
1983