bin: Make sure we don't add/remove a bin to/from itself
[platform/upstream/gstreamer.git] / tests / check / gst / gstbin.c
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
23 #include <gst/check/gstcheck.h>
24 #include <gst/base/gstbasesrc.h>
25
26 static void
27 pop_async_done (GstBus * bus)
28 {
29   GstMessage *message;
30
31   GST_DEBUG ("popping async-done message");
32   message = gst_bus_poll (bus, GST_MESSAGE_ASYNC_DONE, -1);
33
34   fail_unless (message && GST_MESSAGE_TYPE (message)
35       == GST_MESSAGE_ASYNC_DONE, "did not get GST_MESSAGE_ASYNC_DONE");
36
37   gst_message_unref (message);
38   GST_DEBUG ("popped message");
39 }
40
41 static void
42 pop_messages (GstBus * bus, int count)
43 {
44   GstMessage *message;
45
46   int i;
47
48   GST_DEBUG ("popping %d messages", count);
49   for (i = 0; i < count; ++i) {
50     message = gst_bus_poll (bus, GST_MESSAGE_STATE_CHANGED, -1);
51
52     fail_unless (message && GST_MESSAGE_TYPE (message)
53         == GST_MESSAGE_STATE_CHANGED, "did not get GST_MESSAGE_STATE_CHANGED");
54
55     gst_message_unref (message);
56   }
57   GST_DEBUG ("popped %d messages", count);
58 }
59
60 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
61     GST_PAD_SRC,
62     GST_PAD_ALWAYS,
63     GST_STATIC_CAPS_ANY);
64
65 static gpointer
66 push_one_eos (GstPad * pad)
67 {
68   GST_DEBUG_OBJECT (pad, "Pushing EOS event");
69   gst_pad_push_event (pad, gst_event_new_eos ());
70
71   return NULL;
72 }
73
74 static gpointer
75 push_one_stream_start (GstPad * pad)
76 {
77   GST_DEBUG_OBJECT (pad, "Pushing STREAM_START event");
78   gst_pad_push_event (pad, gst_event_new_stream_start ("test"));
79
80   return NULL;
81 }
82
83 GST_START_TEST (test_interface)
84 {
85   GstBin *bin, *bin2;
86   GstElement *filesrc;
87   GstIterator *it;
88   GValue item = { 0, };
89
90   bin = GST_BIN (gst_bin_new (NULL));
91   fail_unless (bin != NULL, "Could not create bin");
92
93   filesrc = gst_element_factory_make ("filesrc", NULL);
94   fail_unless (filesrc != NULL, "Could not create filesrc");
95   fail_unless (GST_IS_URI_HANDLER (filesrc), "Filesrc not a URI handler");
96   gst_bin_add (bin, filesrc);
97
98   fail_unless (gst_bin_get_by_interface (bin, GST_TYPE_URI_HANDLER) == filesrc);
99   gst_object_unref (filesrc);
100
101   it = gst_bin_iterate_all_by_interface (bin, GST_TYPE_URI_HANDLER);
102   fail_unless (it != NULL);
103   fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_OK);
104   fail_unless (g_value_get_object (&item) == (gpointer) filesrc);
105   g_value_reset (&item);
106   fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_DONE);
107   gst_iterator_free (it);
108
109   gst_bin_add_many (bin,
110       gst_element_factory_make ("identity", NULL),
111       gst_element_factory_make ("identity", NULL),
112       gst_element_factory_make ("identity", NULL), NULL);
113   it = gst_bin_iterate_all_by_interface (bin, GST_TYPE_URI_HANDLER);
114   fail_unless (it != NULL);
115   fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_OK);
116   fail_unless (g_value_get_object (&item) == (gpointer) filesrc);
117   g_value_reset (&item);
118   fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_DONE);
119   gst_iterator_free (it);
120
121   bin2 = bin;
122   bin = GST_BIN (gst_bin_new (NULL));
123   fail_unless (bin != NULL);
124   gst_bin_add_many (bin,
125       gst_element_factory_make ("identity", NULL),
126       gst_element_factory_make ("identity", NULL),
127       GST_ELEMENT (bin2), gst_element_factory_make ("identity", NULL), NULL);
128   it = gst_bin_iterate_all_by_interface (bin, GST_TYPE_URI_HANDLER);
129   fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_OK);
130   fail_unless (g_value_get_object (&item) == (gpointer) filesrc);
131   g_value_reset (&item);
132   fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_DONE);
133   gst_iterator_free (it);
134
135   gst_bin_add (bin, gst_element_factory_make ("filesrc", NULL));
136   gst_bin_add (bin2, gst_element_factory_make ("filesrc", NULL));
137   it = gst_bin_iterate_all_by_interface (bin, GST_TYPE_URI_HANDLER);
138   fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_OK);
139   g_value_reset (&item);
140   fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_OK);
141   g_value_reset (&item);
142   fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_OK);
143   g_value_reset (&item);
144   fail_unless (gst_iterator_next (it, &item) == GST_ITERATOR_DONE);
145   g_value_unset (&item);
146   gst_iterator_free (it);
147
148   gst_object_unref (bin);
149 }
150
151 GST_END_TEST;
152
153 GST_START_TEST (test_eos)
154 {
155   GstBus *bus;
156   GstElement *pipeline, *sink1, *sink2;
157   GstMessage *message;
158   GstPad *pad1, *pad2;
159   GThread *thread1, *thread2;
160
161   pipeline = gst_pipeline_new ("test_eos");
162   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
163
164   sink1 = gst_element_factory_make ("fakesink", "sink1");
165   sink2 = gst_element_factory_make ("fakesink", "sink2");
166
167   gst_bin_add_many (GST_BIN (pipeline), sink1, sink2, NULL);
168
169   pad1 = gst_check_setup_src_pad_by_name (sink1, &srctemplate, "sink");
170   pad2 = gst_check_setup_src_pad_by_name (sink2, &srctemplate, "sink");
171
172   gst_pad_set_active (pad1, TRUE);
173   gst_pad_set_active (pad2, TRUE);
174
175   fail_if (gst_element_set_state (GST_ELEMENT (pipeline),
176           GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE);
177
178   /* Send one EOS to sink1 */
179   thread1 = g_thread_new ("thread1", (GThreadFunc) push_one_eos, pad1);
180
181   /* Make sure the EOS message is not sent */
182   message =
183       gst_bus_poll (bus, GST_MESSAGE_ERROR | GST_MESSAGE_EOS, 2 * GST_SECOND);
184   fail_if (message != NULL);
185
186   /* Send one EOS to sink2 */
187   thread2 = g_thread_new ("thread2", (GThreadFunc) push_one_eos, pad2);
188
189   /* Make sure the EOS message is sent then */
190   message = gst_bus_poll (bus, GST_MESSAGE_ERROR | GST_MESSAGE_EOS, -1);
191   fail_if (message == NULL);
192   fail_unless (GST_MESSAGE_TYPE (message) == GST_MESSAGE_EOS);
193   gst_message_unref (message);
194
195   /* Cleanup */
196   g_thread_join (thread1);
197   g_thread_join (thread2);
198
199   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
200   gst_pad_set_active (pad1, FALSE);
201   gst_pad_set_active (pad2, FALSE);
202   gst_check_teardown_src_pad (sink1);
203   gst_check_teardown_src_pad (sink2);
204   gst_object_unref (bus);
205   gst_object_unref (pipeline);
206 }
207
208 GST_END_TEST;
209
210 GST_START_TEST (test_stream_start)
211 {
212   GstBus *bus;
213   GstElement *pipeline, *sink1, *sink2;
214   GstMessage *message;
215   GstPad *pad1, *pad2;
216   GThread *thread1, *thread2;
217
218   pipeline = gst_pipeline_new ("test_stream_start");
219   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
220
221   sink1 = gst_element_factory_make ("fakesink", "sink1");
222   sink2 = gst_element_factory_make ("fakesink", "sink2");
223
224   gst_bin_add_many (GST_BIN (pipeline), sink1, sink2, NULL);
225
226   pad1 = gst_check_setup_src_pad_by_name (sink1, &srctemplate, "sink");
227   pad2 = gst_check_setup_src_pad_by_name (sink2, &srctemplate, "sink");
228
229   gst_pad_set_active (pad1, TRUE);
230   gst_pad_set_active (pad2, TRUE);
231
232   fail_if (gst_element_set_state (GST_ELEMENT (pipeline),
233           GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE);
234
235   /* Send one STREAM_START to sink1 */
236   thread1 = g_thread_new ("thread1", (GThreadFunc) push_one_stream_start, pad1);
237
238   /* Make sure the STREAM_START message is not sent */
239   message =
240       gst_bus_poll (bus, GST_MESSAGE_ERROR | GST_MESSAGE_STREAM_START,
241       2 * GST_SECOND);
242   fail_if (message != NULL);
243
244   /* Send one STREAM_START to sink2 */
245   thread2 = g_thread_new ("thread2", (GThreadFunc) push_one_stream_start, pad2);
246
247   /* Make sure the STREAM_START message is sent then */
248   message =
249       gst_bus_poll (bus, GST_MESSAGE_ERROR | GST_MESSAGE_STREAM_START, -1);
250   fail_if (message == NULL);
251   fail_unless (GST_MESSAGE_TYPE (message) == GST_MESSAGE_STREAM_START);
252   gst_message_unref (message);
253
254   /* Cleanup */
255   g_thread_join (thread1);
256   g_thread_join (thread2);
257
258   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
259   gst_pad_set_active (pad1, FALSE);
260   gst_pad_set_active (pad2, FALSE);
261   gst_check_teardown_src_pad (sink1);
262   gst_check_teardown_src_pad (sink2);
263   gst_object_unref (bus);
264   gst_object_unref (pipeline);
265 }
266
267 GST_END_TEST;
268
269 GST_START_TEST (test_message_state_changed)
270 {
271   GstBin *bin;
272   GstBus *bus;
273   GstMessage *message;
274   GstStateChangeReturn ret;
275
276   bin = GST_BIN (gst_bin_new (NULL));
277   fail_unless (bin != NULL, "Could not create bin");
278   ASSERT_OBJECT_REFCOUNT (bin, "bin", 1);
279
280   bus = g_object_new (gst_bus_get_type (), NULL);
281   gst_element_set_bus (GST_ELEMENT_CAST (bin), bus);
282
283   /* change state, spawning a message, causing an incref on the bin */
284   ret = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_READY);
285   fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
286
287   ASSERT_OBJECT_REFCOUNT (bin, "bin", 2);
288
289   /* get and unref the message, causing a decref on the bin */
290   message = gst_bus_poll (bus, GST_MESSAGE_STATE_CHANGED, -1);
291
292   fail_unless (message && GST_MESSAGE_TYPE (message)
293       == GST_MESSAGE_STATE_CHANGED, "did not get GST_MESSAGE_STATE_CHANGED");
294
295   gst_message_unref (message);
296
297   ASSERT_OBJECT_REFCOUNT (bin, "bin", 1);
298
299   /* clean up */
300   ret = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_NULL);
301   fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
302
303   gst_object_unref (bus);
304   gst_object_unref (bin);
305 }
306
307 GST_END_TEST;
308
309 GST_START_TEST (test_message_state_changed_child)
310 {
311   GstBin *bin;
312   GstElement *src;
313   GstBus *bus;
314   GstMessage *message;
315   GstStateChangeReturn ret;
316
317   bin = GST_BIN (gst_bin_new (NULL));
318   fail_unless (bin != NULL, "Could not create bin");
319   ASSERT_OBJECT_REFCOUNT (bin, "bin", 1);
320
321   bus = g_object_new (gst_bus_get_type (), NULL);
322   gst_element_set_bus (GST_ELEMENT_CAST (bin), bus);
323
324   src = gst_element_factory_make ("fakesrc", NULL);
325   fail_if (src == NULL, "Could not create fakesrc");
326   gst_bin_add (bin, src);
327   ASSERT_OBJECT_REFCOUNT (bin, "bin", 1);
328   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
329
330   /* change state, spawning two messages:
331    * - first for fakesrc, forwarded to bin's bus, causing incref on fakesrc
332    * - second for bin, causing an incref on the bin */
333   GST_DEBUG ("setting bin to READY");
334   ret = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_READY);
335   fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
336
337   ASSERT_OBJECT_REFCOUNT (src, "src", 2);
338   ASSERT_OBJECT_REFCOUNT (bin, "bin", 2);
339
340   /* get and unref the message, causing a decref on the src */
341   message = gst_bus_poll (bus, GST_MESSAGE_STATE_CHANGED, -1);
342   fail_unless (message && GST_MESSAGE_TYPE (message)
343       == GST_MESSAGE_STATE_CHANGED, "did not get GST_MESSAGE_STATE_CHANGED");
344
345   fail_unless (message->src == GST_OBJECT (src));
346   gst_message_unref (message);
347
348   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
349   ASSERT_OBJECT_REFCOUNT (bin, "bin", 2);
350
351   /* get and unref message 2, causing a decref on the bin */
352   message = gst_bus_poll (bus, GST_MESSAGE_STATE_CHANGED, -1);
353   fail_unless (message && GST_MESSAGE_TYPE (message)
354       == GST_MESSAGE_STATE_CHANGED, "did not get GST_MESSAGE_STATE_CHANGED");
355
356   fail_unless (message->src == GST_OBJECT (bin));
357   gst_message_unref (message);
358
359   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
360   ASSERT_OBJECT_REFCOUNT (bin, "bin", 1);
361
362   /* clean up */
363   ret = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_NULL);
364   fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
365   gst_object_unref (bus);
366   gst_object_unref (bin);
367 }
368
369 GST_END_TEST;
370
371 GST_START_TEST (test_message_state_changed_children)
372 {
373   GstPipeline *pipeline;
374   GstElement *src, *sink;
375   GstBus *bus;
376   GstStateChangeReturn ret;
377   GstState current, pending;
378
379   pipeline = GST_PIPELINE (gst_pipeline_new (NULL));
380   fail_unless (pipeline != NULL, "Could not create pipeline");
381   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
382
383   src = gst_element_factory_make ("fakesrc", NULL);
384   fail_if (src == NULL, "Could not create fakesrc");
385   /* need to silence the element as the deep_notify refcounts the
386    * parents while running */
387   g_object_set (G_OBJECT (src), "silent", TRUE, NULL);
388   gst_bin_add (GST_BIN (pipeline), src);
389
390   sink = gst_element_factory_make ("fakesink", NULL);
391   /* need to silence the element as the deep_notify refcounts the
392    * parents while running */
393   g_object_set (G_OBJECT (sink), "silent", TRUE, NULL);
394   fail_if (sink == NULL, "Could not create fakesink");
395   gst_bin_add (GST_BIN (pipeline), sink);
396
397   fail_unless (gst_element_link (src, sink), "could not link src and sink");
398
399   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
400   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
401   ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
402
403   bus = gst_pipeline_get_bus (pipeline);
404
405   /* change state to READY, spawning three messages */
406   GST_DEBUG ("setting pipeline to READY");
407   ret = gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_READY);
408   fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
409
410   /* each object is referenced by a message */
411   ASSERT_OBJECT_REFCOUNT (bus, "bus", 2);
412   ASSERT_OBJECT_REFCOUNT (src, "src", 2);
413   ASSERT_OBJECT_REFCOUNT (sink, "sink", 2);
414   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 2);
415
416   pop_messages (bus, 3);
417   fail_if (gst_bus_have_pending (bus), "unexpected pending messages");
418
419   ASSERT_OBJECT_REFCOUNT (bus, "bus", 2);
420   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
421   ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
422   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
423
424   /* change state to PAUSED, spawning four messages */
425   /* STATE_CHANGED (NULL => READY)
426    * STREAM_START
427    * ASYNC_DONE
428    * STATE_CHANGED (READY => PAUSED)
429    */
430   GST_DEBUG ("setting pipeline to PAUSED");
431   ret = gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PAUSED);
432   fail_unless (ret == GST_STATE_CHANGE_ASYNC);
433   ret =
434       gst_element_get_state (GST_ELEMENT (pipeline), &current, &pending,
435       GST_CLOCK_TIME_NONE);
436   fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
437   fail_unless (current == GST_STATE_PAUSED);
438   fail_unless (pending == GST_STATE_VOID_PENDING);
439
440   /* wait for async thread to settle down */
441   GST_DEBUG ("waiting for refcount");
442   while (GST_OBJECT_REFCOUNT_VALUE (pipeline) > 4)
443     THREAD_SWITCH ();
444   GST_DEBUG ("refcount <= 4 now");
445
446   /* each object is referenced by a message;
447    * base_src is blocked in the push and has an extra refcount.
448    * base_sink_chain has taken a refcount on the sink, and is blocked on
449    * preroll
450    * The stream-status messages holds 2 more refs to the element */
451   ASSERT_OBJECT_REFCOUNT (src, "src", 4);
452   /* refcount can be 4 if the bin is still processing the async_done message of
453    * the sink. */
454   ASSERT_OBJECT_REFCOUNT_BETWEEN (sink, "sink", 2, 3);
455   /* 3 or 4 is valid, because the pipeline might still be posting 
456    * its state_change message */
457   ASSERT_OBJECT_REFCOUNT_BETWEEN (pipeline, "pipeline", 3, 4);
458
459   pop_messages (bus, 3);
460   pop_async_done (bus);
461   fail_if ((gst_bus_pop (bus)) != NULL);
462
463   ASSERT_OBJECT_REFCOUNT (bus, "bus", 2);
464   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
465   ASSERT_OBJECT_REFCOUNT (sink, "sink", 2);
466   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
467
468   /* change state to PLAYING, spawning three messages */
469   GST_DEBUG ("setting pipeline to PLAYING");
470   ret = gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
471   fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
472   ret =
473       gst_element_get_state (GST_ELEMENT (pipeline), &current, &pending,
474       GST_CLOCK_TIME_NONE);
475   fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
476   fail_unless (current == GST_STATE_PLAYING);
477   fail_unless (pending == GST_STATE_VOID_PENDING);
478
479   /* each object is referenced by one message
480    * src might have an extra reference if it's still pushing
481    * sink might have an extra reference if it's still blocked on preroll
482    * pipeline posted a new-clock message too. */
483   ASSERT_OBJECT_REFCOUNT_BETWEEN (src, "src", 2, 3);
484   ASSERT_OBJECT_REFCOUNT_BETWEEN (sink, "sink", 2, 4);
485   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 3);
486
487   pop_messages (bus, 3);
488   fail_if ((gst_bus_pop (bus)) != NULL);
489
490   ASSERT_OBJECT_REFCOUNT (bus, "bus", 2);
491   /* src might have an extra reference if it's still pushing */
492   ASSERT_OBJECT_REFCOUNT_BETWEEN (src, "src", 1, 2);
493   /* sink might have an extra reference if it's still blocked on preroll */
494   ASSERT_OBJECT_REFCOUNT_BETWEEN (sink, "sink", 1, 3);
495   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
496
497   /* go back to READY, spawning six messages */
498   GST_DEBUG ("setting pipeline to READY");
499   ret = gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_READY);
500   fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
501
502   /* each object is referenced by two messages, the source also has the
503    * stream-status message referencing it */
504   ASSERT_OBJECT_REFCOUNT (src, "src", 4);
505   ASSERT_OBJECT_REFCOUNT (sink, "sink", 3);
506   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 3);
507
508   pop_messages (bus, 6);
509   fail_if ((gst_bus_pop (bus)) != NULL);
510
511   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
512   ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
513   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
514
515   /* setting pipeline to NULL flushes the bus automatically */
516   ret = gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
517   fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
518
519   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
520   ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
521   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
522
523   /* clean up */
524   gst_object_unref (bus);
525   gst_object_unref (pipeline);
526 }
527
528 GST_END_TEST;
529
530 GST_START_TEST (test_watch_for_state_change)
531 {
532   GstElement *src, *sink, *bin;
533   GstBus *bus;
534   GstStateChangeReturn ret;
535
536   bin = gst_element_factory_make ("bin", NULL);
537   fail_unless (bin != NULL, "Could not create bin");
538
539   bus = g_object_new (gst_bus_get_type (), NULL);
540   gst_element_set_bus (GST_ELEMENT_CAST (bin), bus);
541
542   src = gst_element_factory_make ("fakesrc", NULL);
543   fail_if (src == NULL, "Could not create fakesrc");
544   sink = gst_element_factory_make ("fakesink", NULL);
545   fail_if (sink == NULL, "Could not create fakesink");
546
547   gst_bin_add (GST_BIN (bin), sink);
548   gst_bin_add (GST_BIN (bin), src);
549
550   fail_unless (gst_element_link (src, sink), "could not link src and sink");
551
552   /* change state, spawning two times three messages */
553   ret = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_PAUSED);
554   fail_unless (ret == GST_STATE_CHANGE_ASYNC);
555   ret =
556       gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
557       GST_CLOCK_TIME_NONE);
558   fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
559
560   pop_messages (bus, 6);
561   pop_async_done (bus);
562
563   fail_unless (gst_bus_have_pending (bus) == FALSE,
564       "Unexpected messages on bus");
565
566   ret = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_PLAYING);
567   fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
568
569   pop_messages (bus, 3);
570
571   /* this one might return either SUCCESS or ASYNC, likely SUCCESS */
572   ret = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_PAUSED);
573   gst_element_get_state (GST_ELEMENT (bin), NULL, NULL, GST_CLOCK_TIME_NONE);
574
575   pop_messages (bus, 3);
576   if (ret == GST_STATE_CHANGE_ASYNC)
577     pop_async_done (bus);
578
579   fail_unless (gst_bus_have_pending (bus) == FALSE,
580       "Unexpected messages on bus");
581
582   /* setting bin to NULL flushes the bus automatically */
583   ret = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_NULL);
584   fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
585
586   /* clean up */
587   gst_object_unref (bus);
588   gst_object_unref (bin);
589 }
590
591 GST_END_TEST;
592
593 GST_START_TEST (test_state_change_error_message)
594 {
595   GstElement *src, *sink, *bin;
596   GstBus *bus;
597   GstStateChangeReturn ret;
598
599   bin = gst_element_factory_make ("bin", NULL);
600   fail_unless (bin != NULL, "Could not create bin");
601
602   bus = g_object_new (gst_bus_get_type (), NULL);
603   gst_element_set_bus (GST_ELEMENT_CAST (bin), bus);
604
605   src = gst_element_factory_make ("fakesrc", NULL);
606   fail_if (src == NULL, "Could not create fakesrc");
607   sink = gst_element_factory_make ("fakesink", NULL);
608   fail_if (sink == NULL, "Could not create fakesink");
609
610   /* add but don't link elements */
611   gst_bin_add (GST_BIN (bin), sink);
612   gst_bin_add (GST_BIN (bin), src);
613
614   /* change state, this should succeed */
615   ret = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_PAUSED);
616   fail_unless (ret == GST_STATE_CHANGE_ASYNC);
617
618   /* now wait, the streaming thread will error because the source is not
619    * linked. */
620   ret = gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
621       GST_CLOCK_TIME_NONE);
622   fail_unless (ret == GST_STATE_CHANGE_FAILURE);
623
624   gst_bus_set_flushing (bus, TRUE);
625
626   /* setting bin to NULL flushes the bus automatically */
627   ret = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_NULL);
628   fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
629
630   /* clean up */
631   gst_object_unref (bus);
632   gst_object_unref (bin);
633 }
634
635 GST_END_TEST;
636
637
638 /* adding an element with linked pads to a bin unlinks the
639  * pads */
640 GST_START_TEST (test_add_linked)
641 {
642   GstElement *src, *sink;
643   GstPad *srcpad, *sinkpad;
644   GstElement *pipeline;
645
646   pipeline = gst_pipeline_new (NULL);
647   fail_unless (pipeline != NULL, "Could not create pipeline");
648
649   src = gst_element_factory_make ("fakesrc", NULL);
650   fail_if (src == NULL, "Could not create fakesrc");
651   sink = gst_element_factory_make ("fakesink", NULL);
652   fail_if (sink == NULL, "Could not create fakesink");
653
654   srcpad = gst_element_get_static_pad (src, "src");
655   fail_unless (srcpad != NULL);
656   sinkpad = gst_element_get_static_pad (sink, "sink");
657   fail_unless (sinkpad != NULL);
658
659   fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK);
660
661   /* pads are linked now */
662   fail_unless (gst_pad_is_linked (srcpad));
663   fail_unless (gst_pad_is_linked (sinkpad));
664
665   /* adding element to bin voids hierarchy so pads are unlinked */
666   gst_bin_add (GST_BIN (pipeline), src);
667
668   /* check if pads really are unlinked */
669   fail_unless (!gst_pad_is_linked (srcpad));
670   fail_unless (!gst_pad_is_linked (sinkpad));
671
672   /* cannot link pads in wrong hierarchy */
673   fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_WRONG_HIERARCHY);
674
675   /* adding other element to bin as well */
676   gst_bin_add (GST_BIN (pipeline), sink);
677
678   /* now we can link again */
679   fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK);
680
681   /* check if pads really are linked */
682   fail_unless (gst_pad_is_linked (srcpad));
683   fail_unless (gst_pad_is_linked (sinkpad));
684
685   gst_object_unref (srcpad);
686   gst_object_unref (sinkpad);
687   gst_object_unref (pipeline);
688 }
689
690 GST_END_TEST;
691
692 /* adding ourself should fail */
693 GST_START_TEST (test_add_self)
694 {
695   GstElement *bin;
696
697   bin = gst_bin_new (NULL);
698   fail_unless (bin != NULL, "Could not create bin");
699
700   ASSERT_CRITICAL (gst_bin_add (GST_BIN (bin), bin));
701
702   gst_object_unref (bin);
703 }
704
705 GST_END_TEST;
706
707
708 /* g_print ("%10s: %4d => %4d\n", GST_OBJECT_NAME (msg->src), old, new); */
709
710 #define ASSERT_STATE_CHANGE_MSG(bus,element,old_state,new_state,num)          \
711   {                                                                           \
712     GstMessage *msg;                                                          \
713     GstState old = 0, new = 0, pending = 0;                                   \
714     msg = gst_bus_poll (bus, GST_MESSAGE_STATE_CHANGED, GST_SECOND);          \
715     fail_if (msg == NULL, "No state change message within 1 second (#"        \
716         G_STRINGIFY (num) ")");                                               \
717     gst_message_parse_state_changed (msg, &old, &new, &pending);              \
718     fail_if (msg->src != GST_OBJECT (element), G_STRINGIFY(element)           \
719         " should have changed state next (#" G_STRINGIFY (num) ")");          \
720     fail_if (old != old_state || new != new_state, "state change is not "     \
721         G_STRINGIFY (old_state) " => " G_STRINGIFY (new_state));              \
722     gst_message_unref (msg);                                                  \
723   }
724
725 GST_START_TEST (test_children_state_change_order_flagged_sink)
726 {
727   GstElement *src, *identity, *sink, *pipeline;
728   GstStateChangeReturn ret;
729   GstState current, pending;
730   GstBus *bus;
731
732   pipeline = gst_pipeline_new (NULL);
733   fail_unless (pipeline != NULL, "Could not create pipeline");
734
735   bus = gst_element_get_bus (pipeline);
736   fail_unless (bus != NULL, "Pipeline has no bus?!");
737
738   src = gst_element_factory_make ("fakesrc", NULL);
739   fail_if (src == NULL, "Could not create fakesrc");
740   g_object_set (src, "num-buffers", 5, NULL);
741
742   identity = gst_element_factory_make ("identity", NULL);
743   fail_if (identity == NULL, "Could not create identity");
744
745   sink = gst_element_factory_make ("fakesink", NULL);
746   fail_if (sink == NULL, "Could not create fakesink");
747
748   gst_bin_add_many (GST_BIN (pipeline), src, identity, sink, NULL);
749
750   fail_unless (gst_element_link (src, identity) == TRUE);
751   fail_unless (gst_element_link (identity, sink) == TRUE);
752
753   /* (1) Test state change with fakesink being a regular sink */
754   ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
755   fail_if (ret != GST_STATE_CHANGE_ASYNC,
756       "State change to PLAYING did not return ASYNC");
757   ret =
758       gst_element_get_state (pipeline, &current, &pending, GST_CLOCK_TIME_NONE);
759   fail_if (ret != GST_STATE_CHANGE_SUCCESS, "State change to PLAYING failed");
760   fail_if (current != GST_STATE_PLAYING, "State change to PLAYING failed");
761   fail_if (pending != GST_STATE_VOID_PENDING, "State change to PLAYING failed");
762
763   /* NULL => READY */
764   ASSERT_STATE_CHANGE_MSG (bus, sink, GST_STATE_NULL, GST_STATE_READY, 101);
765   ASSERT_STATE_CHANGE_MSG (bus, identity, GST_STATE_NULL, GST_STATE_READY, 102);
766   ASSERT_STATE_CHANGE_MSG (bus, src, GST_STATE_NULL, GST_STATE_READY, 103);
767   ASSERT_STATE_CHANGE_MSG (bus, pipeline, GST_STATE_NULL, GST_STATE_READY, 104);
768
769   /* READY => PAUSED */
770   /* because of pre-rolling, sink will return ASYNC on state
771    * change and change state later when it has a buffer */
772   GST_DEBUG ("popping READY -> PAUSED messages");
773   ASSERT_STATE_CHANGE_MSG (bus, identity, GST_STATE_READY, GST_STATE_PAUSED,
774       105);
775 #if 0
776   /* From here on, all bets are off. Usually the source changes state next,
777    * but it might just as well be that the first buffer produced by the
778    * source reaches the sink before the source has finished its state change,
779    * in which case the sink will commit its new state before the source ...  */
780   ASSERT_STATE_CHANGE_MSG (bus, src, GST_STATE_READY, GST_STATE_PAUSED, 106);
781   ASSERT_STATE_CHANGE_MSG (bus, sink, GST_STATE_READY, GST_STATE_PAUSED, 107);
782 #else
783
784   pop_messages (bus, 2);        /* pop remaining ready => paused messages off the bus */
785   ASSERT_STATE_CHANGE_MSG (bus, pipeline, GST_STATE_READY, GST_STATE_PAUSED,
786       108);
787   pop_async_done (bus);
788 #endif
789   /* PAUSED => PLAYING */
790   GST_DEBUG ("popping PAUSED -> PLAYING messages");
791   ASSERT_STATE_CHANGE_MSG (bus, sink, GST_STATE_PAUSED, GST_STATE_PLAYING, 109);
792   ASSERT_STATE_CHANGE_MSG (bus, identity, GST_STATE_PAUSED, GST_STATE_PLAYING,
793       110);
794   ASSERT_STATE_CHANGE_MSG (bus, src, GST_STATE_PAUSED, GST_STATE_PLAYING, 111);
795   ASSERT_STATE_CHANGE_MSG (bus, pipeline, GST_STATE_PAUSED, GST_STATE_PLAYING,
796       112);
797
798   /* don't set to NULL that will set the bus flushing and kill our messages */
799   ret = gst_element_set_state (pipeline, GST_STATE_READY);
800   fail_if (ret != GST_STATE_CHANGE_SUCCESS, "State change to READY failed");
801   ret = gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
802   fail_if (ret != GST_STATE_CHANGE_SUCCESS, "State change to READY failed");
803
804   /* TODO: do we need to check downwards state change order as well? */
805   pop_messages (bus, 4);        /* pop playing => paused messages off the bus */
806   pop_messages (bus, 4);        /* pop paused => ready messages off the bus */
807
808   while (GST_OBJECT_REFCOUNT_VALUE (pipeline) > 1)
809     THREAD_SWITCH ();
810
811   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
812   ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
813   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
814
815   ret = gst_element_set_state (pipeline, GST_STATE_NULL);
816   fail_if (ret != GST_STATE_CHANGE_SUCCESS, "State change to NULL failed");
817
818   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
819   ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
820   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
821
822   gst_object_unref (bus);
823   gst_object_unref (pipeline);
824 }
825
826 GST_END_TEST;
827
828
829 GST_START_TEST (test_children_state_change_order_semi_sink)
830 {
831   GstElement *src, *identity, *sink, *pipeline;
832   GstStateChangeReturn ret;
833   GstState current, pending;
834   GstBus *bus;
835
836   /* (2) Now again, but check other code path where we don't have
837    *     a proper sink correctly flagged as such, but a 'semi-sink' */
838   pipeline = gst_pipeline_new (NULL);
839   fail_unless (pipeline != NULL, "Could not create pipeline");
840
841   bus = gst_element_get_bus (pipeline);
842   fail_unless (bus != NULL, "Pipeline has no bus?!");
843
844   src = gst_element_factory_make ("fakesrc", NULL);
845   fail_if (src == NULL, "Could not create fakesrc");
846
847   identity = gst_element_factory_make ("identity", NULL);
848   fail_if (identity == NULL, "Could not create identity");
849
850   sink = gst_element_factory_make ("fakesink", NULL);
851   fail_if (sink == NULL, "Could not create fakesink");
852
853   gst_bin_add_many (GST_BIN (pipeline), src, identity, sink, NULL);
854
855   fail_unless (gst_element_link (src, identity) == TRUE);
856   fail_unless (gst_element_link (identity, sink) == TRUE);
857
858   /* this is not very nice but should work just fine in this case. */
859   GST_OBJECT_FLAG_UNSET (sink, GST_ELEMENT_FLAG_SINK);  /* <======== */
860
861   ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
862   fail_if (ret != GST_STATE_CHANGE_ASYNC, "State change to PLAYING not ASYNC");
863   ret =
864       gst_element_get_state (pipeline, &current, &pending, GST_CLOCK_TIME_NONE);
865   fail_if (ret != GST_STATE_CHANGE_SUCCESS, "State change to PLAYING failed");
866   fail_if (current != GST_STATE_PLAYING, "State change to PLAYING failed");
867   fail_if (pending != GST_STATE_VOID_PENDING, "State change to PLAYING failed");
868
869   /* NULL => READY */
870   ASSERT_STATE_CHANGE_MSG (bus, sink, GST_STATE_NULL, GST_STATE_READY, 201);
871   ASSERT_STATE_CHANGE_MSG (bus, identity, GST_STATE_NULL, GST_STATE_READY, 202);
872   ASSERT_STATE_CHANGE_MSG (bus, src, GST_STATE_NULL, GST_STATE_READY, 203);
873   ASSERT_STATE_CHANGE_MSG (bus, pipeline, GST_STATE_NULL, GST_STATE_READY, 204);
874
875   /* READY => PAUSED */
876   /* because of pre-rolling, sink will return ASYNC on state
877    * change and change state later when it has a buffer */
878   GST_DEBUG ("popping READY -> PAUSED messages");
879   ASSERT_STATE_CHANGE_MSG (bus, identity, GST_STATE_READY, GST_STATE_PAUSED,
880       205);
881 #if 0
882   /* From here on, all bets are off. Usually the source changes state next,
883    * but it might just as well be that the first buffer produced by the
884    * source reaches the sink before the source has finished its state change,
885    * in which case the sink will commit its new state before the source ...  */
886   ASSERT_STATE_CHANGE_MSG (bus, src, GST_STATE_READY, GST_STATE_PAUSED, 206);
887   ASSERT_STATE_CHANGE_MSG (bus, sink, GST_STATE_READY, GST_STATE_PAUSED, 207);
888 #else
889   pop_messages (bus, 2);        /* pop remaining ready => paused messages off the bus */
890   ASSERT_STATE_CHANGE_MSG (bus, pipeline, GST_STATE_READY, GST_STATE_PAUSED,
891       208);
892   pop_async_done (bus);
893
894   /* PAUSED => PLAYING */
895   GST_DEBUG ("popping PAUSED -> PLAYING messages");
896   ASSERT_STATE_CHANGE_MSG (bus, sink, GST_STATE_PAUSED, GST_STATE_PLAYING, 209);
897   ASSERT_STATE_CHANGE_MSG (bus, identity, GST_STATE_PAUSED, GST_STATE_PLAYING,
898       210);
899   ASSERT_STATE_CHANGE_MSG (bus, src, GST_STATE_PAUSED, GST_STATE_PLAYING, 211);
900   ASSERT_STATE_CHANGE_MSG (bus, pipeline, GST_STATE_PAUSED, GST_STATE_PLAYING,
901       212);
902 #endif
903
904   /* don't set to NULL that will set the bus flushing and kill our messages */
905   ret = gst_element_set_state (pipeline, GST_STATE_READY);
906   fail_if (ret != GST_STATE_CHANGE_SUCCESS, "State change to READY failed");
907
908   /* TODO: do we need to check downwards state change order as well? */
909   pop_messages (bus, 4);        /* pop playing => paused messages off the bus */
910   pop_messages (bus, 4);        /* pop paused => ready messages off the bus */
911
912   GST_DEBUG ("waiting for pipeline to reach refcount 1");
913   while (GST_OBJECT_REFCOUNT_VALUE (pipeline) > 1)
914     THREAD_SWITCH ();
915
916   GST_DEBUG ("checking refcount");
917   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
918   ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
919   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
920
921   ret = gst_element_set_state (pipeline, GST_STATE_NULL);
922   fail_if (ret != GST_STATE_CHANGE_SUCCESS, "State change to NULL failed");
923
924   GST_DEBUG ("checking refcount");
925   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
926   ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
927   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
928
929   GST_DEBUG ("cleanup");
930   gst_object_unref (bus);
931   gst_object_unref (pipeline);
932 }
933
934 GST_END_TEST;
935
936 GST_START_TEST (test_children_state_change_order_two_sink)
937 {
938   GstElement *src, *tee, *identity, *sink1, *sink2, *pipeline;
939   GstStateChangeReturn ret;
940   GstBus *bus;
941
942   pipeline = gst_pipeline_new (NULL);
943   fail_unless (pipeline != NULL, "Could not create pipeline");
944
945   bus = gst_element_get_bus (pipeline);
946   fail_unless (bus != NULL, "Pipeline has no bus?!");
947
948   src = gst_element_factory_make ("fakesrc", NULL);
949   fail_if (src == NULL, "Could not create fakesrc");
950
951   tee = gst_element_factory_make ("tee", NULL);
952   fail_if (tee == NULL, "Could not create tee");
953
954   identity = gst_element_factory_make ("identity", NULL);
955   fail_if (identity == NULL, "Could not create identity");
956
957   sink1 = gst_element_factory_make ("fakesink", NULL);
958   fail_if (sink1 == NULL, "Could not create fakesink1");
959
960   sink2 = gst_element_factory_make ("fakesink", NULL);
961   fail_if (sink2 == NULL, "Could not create fakesink2");
962
963   gst_bin_add_many (GST_BIN (pipeline), src, tee, identity, sink1, sink2, NULL);
964
965   fail_unless (gst_element_link (src, tee) == TRUE);
966   fail_unless (gst_element_link (tee, identity) == TRUE);
967   fail_unless (gst_element_link (identity, sink1) == TRUE);
968   fail_unless (gst_element_link (tee, sink2) == TRUE);
969
970   ret = gst_element_set_state (pipeline, GST_STATE_READY);
971   fail_if (ret != GST_STATE_CHANGE_SUCCESS, "State change to READY failed");
972
973   /* NULL => READY */
974   {
975     GstMessage *msg;
976     GstState old = 0, new = 0, pending = 0;
977     GstObject *first, *second;
978
979     msg = gst_bus_poll (bus, GST_MESSAGE_STATE_CHANGED, GST_SECOND);
980     fail_if (msg == NULL, "No state change message within 1 second (#201)");
981
982     gst_message_parse_state_changed (msg, &old, &new, &pending);
983     first = gst_object_ref (msg->src);
984
985     fail_if (first != GST_OBJECT (sink1) && first != GST_OBJECT (sink2),
986         "sink1 or sink2 should have changed state next #(202)");
987     gst_message_unref (msg);
988
989     msg = gst_bus_poll (bus, GST_MESSAGE_STATE_CHANGED, GST_SECOND);
990     fail_if (msg == NULL, "No state change message within 1 second (#201)");
991
992     gst_message_parse_state_changed (msg, &old, &new, &pending);
993     second = gst_object_ref (msg->src);
994
995     fail_if (second != GST_OBJECT (sink1) && second != GST_OBJECT (sink2),
996         "sink1 or sink2 should have changed state next #(202)");
997     gst_message_unref (msg);
998
999     fail_if (second == first, "got state change from same object");
1000
1001     gst_object_unref (first);
1002     gst_object_unref (second);
1003   }
1004   ASSERT_STATE_CHANGE_MSG (bus, identity, GST_STATE_NULL, GST_STATE_READY, 203);
1005   ASSERT_STATE_CHANGE_MSG (bus, tee, GST_STATE_NULL, GST_STATE_READY, 204);
1006   ASSERT_STATE_CHANGE_MSG (bus, src, GST_STATE_NULL, GST_STATE_READY, 205);
1007   ASSERT_STATE_CHANGE_MSG (bus, pipeline, GST_STATE_NULL, GST_STATE_READY, 206);
1008
1009   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
1010   ASSERT_OBJECT_REFCOUNT (tee, "tee", 1);
1011   ASSERT_OBJECT_REFCOUNT (identity, "identity", 1);
1012   ASSERT_OBJECT_REFCOUNT (sink1, "sink1", 1);
1013   ASSERT_OBJECT_REFCOUNT (sink2, "sink2", 1);
1014   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
1015
1016   ret = gst_element_set_state (pipeline, GST_STATE_NULL);
1017   fail_if (ret != GST_STATE_CHANGE_SUCCESS, "State change to NULL failed");
1018
1019   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
1020   ASSERT_OBJECT_REFCOUNT (tee, "tee", 1);
1021   ASSERT_OBJECT_REFCOUNT (identity, "identity", 1);
1022   ASSERT_OBJECT_REFCOUNT (sink1, "sink1", 1);
1023   ASSERT_OBJECT_REFCOUNT (sink2, "sink2", 1);
1024   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
1025
1026   gst_object_unref (bus);
1027   gst_object_unref (pipeline);
1028 }
1029
1030 GST_END_TEST;
1031
1032 GST_START_TEST (test_iterate_sorted)
1033 {
1034   GstElement *src, *tee, *identity, *sink1, *sink2, *pipeline, *bin;
1035   GstIterator *it;
1036   GValue elem = { 0, };
1037
1038   pipeline = gst_pipeline_new (NULL);
1039   fail_unless (pipeline != NULL, "Could not create pipeline");
1040
1041   bin = gst_bin_new (NULL);
1042   fail_unless (bin != NULL, "Could not create bin");
1043
1044   src = gst_element_factory_make ("fakesrc", NULL);
1045   fail_if (src == NULL, "Could not create fakesrc");
1046
1047   tee = gst_element_factory_make ("tee", NULL);
1048   fail_if (tee == NULL, "Could not create tee");
1049
1050   sink1 = gst_element_factory_make ("fakesink", NULL);
1051   fail_if (sink1 == NULL, "Could not create fakesink1");
1052
1053   gst_bin_add_many (GST_BIN (bin), src, tee, sink1, NULL);
1054
1055   fail_unless (gst_element_link (src, tee) == TRUE);
1056   fail_unless (gst_element_link (tee, sink1) == TRUE);
1057
1058   identity = gst_element_factory_make ("identity", NULL);
1059   fail_if (identity == NULL, "Could not create identity");
1060
1061   sink2 = gst_element_factory_make ("fakesink", NULL);
1062   fail_if (sink2 == NULL, "Could not create fakesink2");
1063
1064   gst_bin_add_many (GST_BIN (pipeline), bin, identity, sink2, NULL);
1065
1066   fail_unless (gst_element_link (tee, identity) == TRUE);
1067   fail_unless (gst_element_link (identity, sink2) == TRUE);
1068
1069   it = gst_bin_iterate_sorted (GST_BIN (pipeline));
1070   fail_unless (gst_iterator_next (it, &elem) == GST_ITERATOR_OK);
1071   fail_unless (g_value_get_object (&elem) == (gpointer) sink2);
1072   g_value_reset (&elem);
1073
1074   fail_unless (gst_iterator_next (it, &elem) == GST_ITERATOR_OK);
1075   fail_unless (g_value_get_object (&elem) == (gpointer) identity);
1076   g_value_reset (&elem);
1077
1078   fail_unless (gst_iterator_next (it, &elem) == GST_ITERATOR_OK);
1079   fail_unless (g_value_get_object (&elem) == (gpointer) bin);
1080   g_value_reset (&elem);
1081
1082   g_value_unset (&elem);
1083   gst_iterator_free (it);
1084
1085   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
1086   gst_object_unref (pipeline);
1087 }
1088
1089 GST_END_TEST;
1090
1091 static void
1092 test_link_structure_change_state_changed_sync_cb (GstBus * bus,
1093     GstMessage * message, gpointer data)
1094 {
1095   GstPipeline *pipeline = GST_PIPELINE (data);
1096   GstElement *src, *identity, *sink;
1097   GstState old, snew, pending;
1098
1099   sink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
1100   fail_unless (sink != NULL, "Could not get sink");
1101
1102   gst_message_parse_state_changed (message, &old, &snew, &pending);
1103   if (message->src != GST_OBJECT (sink) || snew != GST_STATE_READY) {
1104     gst_object_unref (sink);
1105     return;
1106   }
1107
1108   src = gst_bin_get_by_name (GST_BIN (pipeline), "src");
1109   fail_unless (src != NULL, "Could not get src");
1110
1111   identity = gst_bin_get_by_name (GST_BIN (pipeline), "identity");
1112   fail_unless (identity != NULL, "Could not get identity");
1113
1114   /* link src to identity, the pipeline should detect the new link and
1115    * resync the state change */
1116   fail_unless (gst_element_link (src, identity) == TRUE);
1117
1118   gst_object_unref (src);
1119   gst_object_unref (identity);
1120   gst_object_unref (sink);
1121 }
1122
1123 GST_START_TEST (test_link_structure_change)
1124 {
1125   GstElement *src, *identity, *sink, *pipeline;
1126   GstBus *bus;
1127   GstState state;
1128
1129   pipeline = gst_pipeline_new (NULL);
1130   fail_unless (pipeline != NULL, "Could not create pipeline");
1131
1132   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
1133   fail_unless (bus != NULL, "Could not get bus");
1134
1135   /* use the sync signal handler to link elements while the pipeline is still
1136    * doing the state change */
1137   gst_bus_set_sync_handler (bus, gst_bus_sync_signal_handler, pipeline, NULL);
1138   g_object_connect (bus, "signal::sync-message::state-changed",
1139       G_CALLBACK (test_link_structure_change_state_changed_sync_cb), pipeline,
1140       NULL);
1141
1142   src = gst_element_factory_make ("fakesrc", "src");
1143   fail_if (src == NULL, "Could not create fakesrc");
1144
1145   identity = gst_element_factory_make ("identity", "identity");
1146   fail_if (identity == NULL, "Could not create identity");
1147
1148   sink = gst_element_factory_make ("fakesink", "sink");
1149   fail_if (sink == NULL, "Could not create fakesink1");
1150
1151   gst_bin_add_many (GST_BIN (pipeline), src, identity, sink, NULL);
1152
1153   gst_element_set_state (pipeline, GST_STATE_READY);
1154   gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
1155
1156   /* the state change will be done on src only if the pipeline correctly resyncs
1157    * after that fakesrc has been linked to identity */
1158   gst_element_get_state (src, &state, NULL, 0);
1159   fail_unless_equals_int (state, GST_STATE_READY);
1160
1161   /* clean up */
1162   gst_element_set_state (pipeline, GST_STATE_NULL);
1163   gst_object_unref (bus);
1164   gst_object_unref (pipeline);
1165 }
1166
1167 GST_END_TEST;
1168
1169 static GstBusSyncReply
1170 sync_handler_remove_sink (GstBus * bus, GstMessage * message, gpointer data)
1171 {
1172   if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ERROR) {
1173     GstElement *child;
1174
1175     child = gst_bin_get_by_name (GST_BIN (data), "fakesink");
1176     fail_unless (child != NULL, "Could not find fakesink");
1177
1178     gst_bin_remove (GST_BIN (data), child);
1179     gst_object_unref (child);
1180   }
1181   return GST_BUS_PASS;
1182 }
1183
1184 GST_START_TEST (test_state_failure_remove)
1185 {
1186   GstElement *src, *sink, *pipeline;
1187   GstBus *bus;
1188   GstStateChangeReturn ret;
1189
1190   pipeline = gst_pipeline_new (NULL);
1191   fail_unless (pipeline != NULL, "Could not create pipeline");
1192
1193   src = gst_element_factory_make ("fakesrc", "fakesrc");
1194   fail_unless (src != NULL, "Could not create fakesrc");
1195
1196   sink = gst_element_factory_make ("fakesink", "fakesink");
1197   fail_unless (sink != NULL, "Could not create fakesink");
1198
1199   g_object_set (sink, "state-error", 1, NULL);
1200
1201   gst_bin_add (GST_BIN (pipeline), src);
1202   gst_bin_add (GST_BIN (pipeline), sink);
1203
1204   gst_element_link (src, sink);
1205
1206   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
1207   fail_unless (bus != NULL, "Could not get bus");
1208
1209   gst_bus_set_sync_handler (bus, sync_handler_remove_sink, pipeline, NULL);
1210
1211   ret = gst_element_set_state (pipeline, GST_STATE_READY);
1212   fail_unless (ret == GST_STATE_CHANGE_SUCCESS,
1213       "did not get state change success");
1214
1215   gst_element_set_state (pipeline, GST_STATE_NULL);
1216
1217   gst_object_unref (bus);
1218   gst_object_unref (pipeline);
1219 }
1220
1221 GST_END_TEST;
1222
1223 GST_START_TEST (test_many_bins)
1224 {
1225   GstStateChangeReturn ret;
1226   GstElement *src, *sink, *pipeline, *last_bin = NULL;
1227   gint i;
1228
1229 #define NUM_BINS 2000
1230
1231   pipeline = gst_pipeline_new (NULL);
1232   fail_unless (pipeline != NULL, "Could not create pipeline");
1233
1234   src = gst_element_factory_make ("fakesrc", "fakesrc");
1235   fail_unless (src != NULL, "Could not create fakesrc");
1236   g_object_set (src, "num-buffers", 3, NULL);
1237
1238   sink = gst_element_factory_make ("fakesink", "fakesink");
1239   fail_unless (sink != NULL, "Could not create fakesink");
1240
1241   gst_bin_add (GST_BIN (pipeline), src);
1242   gst_bin_add (GST_BIN (pipeline), sink);
1243
1244   for (i = 0; i < NUM_BINS; ++i) {
1245     GstElement *bin, *identity;
1246     GstPad *srcpad, *sinkpad;
1247
1248     bin = gst_bin_new (NULL);
1249     fail_unless (bin != NULL, "Could not create bin %d", i);
1250     identity = gst_element_factory_make ("identity", "identity");
1251     fail_unless (identity != NULL, "Could not create identity %d", i);
1252     g_object_set (identity, "silent", TRUE, NULL);
1253     gst_bin_add (GST_BIN (bin), identity);
1254     sinkpad = gst_element_get_static_pad (identity, "sink");
1255     srcpad = gst_element_get_static_pad (identity, "src");
1256     gst_element_add_pad (bin, gst_ghost_pad_new ("sink", sinkpad));
1257     gst_element_add_pad (bin, gst_ghost_pad_new ("src", srcpad));
1258     gst_object_unref (sinkpad);
1259     gst_object_unref (srcpad);
1260
1261     gst_bin_add (GST_BIN (pipeline), bin);
1262
1263     if (last_bin == NULL) {
1264       srcpad = gst_element_get_static_pad (src, "src");
1265     } else {
1266       srcpad = gst_element_get_static_pad (last_bin, "src");
1267     }
1268     sinkpad = gst_element_get_static_pad (bin, "sink");
1269     gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_NOTHING);
1270     gst_object_unref (sinkpad);
1271     gst_object_unref (srcpad);
1272
1273
1274     last_bin = bin;
1275
1276     /* insert some queues to limit the number of function calls in a row */
1277     if ((i % 100) == 0) {
1278       GstElement *q = gst_element_factory_make ("queue", NULL);
1279
1280       GST_LOG ("bin #%d, inserting queue", i);
1281       gst_bin_add (GST_BIN (pipeline), q);
1282       fail_unless (gst_element_link (last_bin, q));
1283       last_bin = q;
1284     }
1285   }
1286
1287   fail_unless (gst_element_link (last_bin, sink));
1288
1289   ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
1290   fail_unless_equals_int (ret, GST_STATE_CHANGE_ASYNC);
1291
1292   for (i = 0; i < 15; ++i) {
1293     GST_INFO ("waiting for preroll ...");
1294     ret = gst_element_get_state (pipeline, NULL, NULL, GST_SECOND);
1295     if (ret != GST_STATE_CHANGE_ASYNC)
1296       break;
1297   }
1298   fail_unless_equals_int (ret, GST_STATE_CHANGE_SUCCESS);
1299
1300   gst_element_set_state (pipeline, GST_STATE_NULL);
1301   gst_object_unref (pipeline);
1302 }
1303
1304 GST_END_TEST;
1305
1306 static GstPadProbeReturn
1307 fakesrc_pad_blocked_cb (GstPad * pad, GstPadProbeInfo * info, void *arg)
1308 {
1309   GstPipeline *pipeline = (GstPipeline *) arg;
1310   GstElement *src, *sink;
1311
1312   src = gst_bin_get_by_name (GST_BIN (pipeline), "fakesrc");
1313   fail_unless (src != NULL, "Could not get fakesrc");
1314
1315   sink = gst_element_factory_make ("fakesink", "fakesink");
1316   fail_unless (sink != NULL, "Could not create fakesink");
1317
1318   g_object_set (sink, "state-error", 1, NULL);
1319   gst_bin_add (GST_BIN (pipeline), sink);
1320
1321   gst_element_link (src, sink);
1322   gst_element_sync_state_with_parent (sink);
1323   gst_object_unref (src);
1324
1325   return GST_PAD_PROBE_REMOVE;
1326 }
1327
1328 GST_START_TEST (test_state_failure_unref)
1329 {
1330   GstElement *src, *pipeline;
1331   GstPad *srcpad;
1332   GstBus *bus;
1333   GstStateChangeReturn ret;
1334   GstMessage *msg;
1335
1336   pipeline = gst_pipeline_new (NULL);
1337   fail_unless (pipeline != NULL, "Could not create pipeline");
1338
1339   src = gst_element_factory_make ("fakesrc", "fakesrc");
1340   fail_unless (src != NULL, "Could not create fakesrc");
1341
1342   srcpad = gst_element_get_static_pad (src, "src");
1343   fail_unless (srcpad != NULL, "Could not get fakesrc srcpad");
1344
1345   gst_pad_add_probe (srcpad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
1346       fakesrc_pad_blocked_cb, pipeline, NULL);
1347   gst_object_unref (srcpad);
1348
1349   gst_bin_add (GST_BIN (pipeline), src);
1350
1351   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
1352   fail_unless (bus != NULL, "Could not get bus");
1353
1354   gst_element_set_state (pipeline, GST_STATE_PLAYING);
1355
1356   /* Wait for an error message from our fakesink (added from the
1357      pad block callback). */
1358   msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, GST_SECOND);
1359   fail_if (msg == NULL, "No error message within 1 second");
1360   gst_message_unref (msg);
1361
1362   /* Check that after this failure, we can still stop, and then unref, the
1363      pipeline. This should always be possible. */
1364   ret = gst_element_set_state (pipeline, GST_STATE_NULL);
1365   fail_unless (ret == GST_STATE_CHANGE_SUCCESS, "downward state change failed");
1366
1367   gst_object_unref (bus);
1368   gst_object_unref (pipeline);
1369 }
1370
1371 GST_END_TEST;
1372
1373 static void
1374 on_sync_bus_error (GstBus * bus, GstMessage * msg)
1375 {
1376   fail_if (msg != NULL);
1377 }
1378
1379 GST_START_TEST (test_state_change_skip)
1380 {
1381   GstElement *sink, *pipeline;
1382   GstStateChangeReturn ret;
1383   GstBus *bus;
1384
1385   pipeline = gst_pipeline_new (NULL);
1386   fail_unless (pipeline != NULL, "Could not create pipeline");
1387
1388   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
1389   fail_unless (bus != NULL, "Could not get bus");
1390
1391   /* no errors */
1392   gst_bus_enable_sync_message_emission (bus);
1393   g_signal_connect (bus, "sync-message::error", (GCallback) on_sync_bus_error,
1394       NULL);
1395
1396   sink = gst_element_factory_make ("fakesink", "fakesink");
1397   fail_unless (sink != NULL, "Could not create fakesink");
1398   gst_element_set_state (sink, GST_STATE_PAUSED);
1399
1400   g_object_set (sink, "state-error", 5, NULL);
1401
1402   gst_bin_add (GST_BIN (pipeline), sink);
1403   gst_element_set_state (pipeline, GST_STATE_PLAYING);
1404
1405   g_object_set (sink, "state-error", 0, NULL);
1406
1407   /* Check that after this failure, we can still stop, and then unref, the
1408      pipeline. This should always be possible. */
1409   ret = gst_element_set_state (pipeline, GST_STATE_NULL);
1410   fail_unless (ret == GST_STATE_CHANGE_SUCCESS, "downward state change failed");
1411
1412   gst_object_unref (pipeline);
1413   gst_object_unref (bus);
1414 }
1415
1416 GST_END_TEST;
1417
1418 GST_START_TEST (test_duration_is_max)
1419 {
1420   GstElement *bin, *src[3], *sink[3];
1421   GstStateChangeReturn state_res;
1422   GstFormat format = GST_FORMAT_BYTES;
1423   gboolean res;
1424   gint64 duration;
1425
1426   GST_INFO ("preparing test");
1427
1428   /* build pipeline */
1429   bin = gst_pipeline_new ("pipeline");
1430
1431   /* 3 sources, an adder and a fakesink */
1432   src[0] = gst_element_factory_make ("fakesrc", NULL);
1433   src[1] = gst_element_factory_make ("fakesrc", NULL);
1434   src[2] = gst_element_factory_make ("fakesrc", NULL);
1435   sink[0] = gst_element_factory_make ("fakesink", NULL);
1436   sink[1] = gst_element_factory_make ("fakesink", NULL);
1437   sink[2] = gst_element_factory_make ("fakesink", NULL);
1438   gst_bin_add_many (GST_BIN (bin), src[0], src[1], src[2], sink[0], sink[1],
1439       sink[2], NULL);
1440
1441   gst_element_link (src[0], sink[0]);
1442   gst_element_link (src[1], sink[1]);
1443   gst_element_link (src[2], sink[2]);
1444
1445   /* irks, duration is reset on basesrc */
1446   state_res = gst_element_set_state (bin, GST_STATE_PAUSED);
1447   fail_unless (state_res != GST_STATE_CHANGE_FAILURE, NULL);
1448
1449   /* set durations on src */
1450   GST_BASE_SRC (src[0])->segment.duration = 1000;
1451   GST_BASE_SRC (src[1])->segment.duration = 3000;
1452   GST_BASE_SRC (src[2])->segment.duration = 2000;
1453
1454   /* set to playing */
1455   state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
1456   fail_unless (state_res != GST_STATE_CHANGE_FAILURE, NULL);
1457
1458   /* wait for completion */
1459   state_res =
1460       gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
1461       GST_CLOCK_TIME_NONE);
1462   fail_unless (state_res != GST_STATE_CHANGE_FAILURE, NULL);
1463
1464   res = gst_element_query_duration (GST_ELEMENT (bin), format, &duration);
1465   fail_unless (res, NULL);
1466
1467   ck_assert_int_eq (duration, 3000);
1468
1469   gst_element_set_state (bin, GST_STATE_NULL);
1470   gst_object_unref (bin);
1471 }
1472
1473 GST_END_TEST;
1474
1475 GST_START_TEST (test_duration_unknown_overrides)
1476 {
1477   GstElement *bin, *src[3], *sink[3];
1478   GstStateChangeReturn state_res;
1479   GstFormat format = GST_FORMAT_BYTES;
1480   gboolean res;
1481   gint64 duration;
1482
1483   GST_INFO ("preparing test");
1484
1485   /* build pipeline */
1486   bin = gst_pipeline_new ("pipeline");
1487
1488   /* 3 sources, an adder and a fakesink */
1489   src[0] = gst_element_factory_make ("fakesrc", NULL);
1490   src[1] = gst_element_factory_make ("fakesrc", NULL);
1491   src[2] = gst_element_factory_make ("fakesrc", NULL);
1492   sink[0] = gst_element_factory_make ("fakesink", NULL);
1493   sink[1] = gst_element_factory_make ("fakesink", NULL);
1494   sink[2] = gst_element_factory_make ("fakesink", NULL);
1495   gst_bin_add_many (GST_BIN (bin), src[0], src[1], src[2], sink[0], sink[1],
1496       sink[2], NULL);
1497
1498   gst_element_link (src[0], sink[0]);
1499   gst_element_link (src[1], sink[1]);
1500   gst_element_link (src[2], sink[2]);
1501
1502   /* irks, duration is reset on basesrc */
1503   state_res = gst_element_set_state (bin, GST_STATE_PAUSED);
1504   fail_unless (state_res != GST_STATE_CHANGE_FAILURE, NULL);
1505
1506   /* set durations on src */
1507   GST_BASE_SRC (src[0])->segment.duration = GST_CLOCK_TIME_NONE;
1508   GST_BASE_SRC (src[1])->segment.duration = 3000;
1509   GST_BASE_SRC (src[2])->segment.duration = 2000;
1510
1511   /* set to playing */
1512   state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
1513   fail_unless (state_res != GST_STATE_CHANGE_FAILURE, NULL);
1514
1515   /* wait for completion */
1516   state_res =
1517       gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
1518       GST_CLOCK_TIME_NONE);
1519   fail_unless (state_res != GST_STATE_CHANGE_FAILURE, NULL);
1520
1521   res = gst_element_query_duration (GST_ELEMENT (bin), format, &duration);
1522   fail_unless (res, NULL);
1523
1524   ck_assert_int_eq (duration, GST_CLOCK_TIME_NONE);
1525
1526   gst_element_set_state (bin, GST_STATE_NULL);
1527   gst_object_unref (bin);
1528 }
1529
1530 GST_END_TEST;
1531
1532
1533
1534 static Suite *
1535 gst_bin_suite (void)
1536 {
1537   Suite *s = suite_create ("GstBin");
1538   TCase *tc_chain = tcase_create ("bin tests");
1539
1540   tcase_set_timeout (tc_chain, 0);
1541
1542   suite_add_tcase (s, tc_chain);
1543   tcase_add_test (tc_chain, test_interface);
1544   tcase_add_test (tc_chain, test_eos);
1545   tcase_add_test (tc_chain, test_stream_start);
1546   tcase_add_test (tc_chain, test_children_state_change_order_flagged_sink);
1547   tcase_add_test (tc_chain, test_children_state_change_order_semi_sink);
1548   tcase_add_test (tc_chain, test_children_state_change_order_two_sink);
1549   tcase_add_test (tc_chain, test_message_state_changed);
1550   tcase_add_test (tc_chain, test_message_state_changed_child);
1551   tcase_add_test (tc_chain, test_message_state_changed_children);
1552   tcase_add_test (tc_chain, test_watch_for_state_change);
1553   tcase_add_test (tc_chain, test_state_change_error_message);
1554   tcase_add_test (tc_chain, test_add_linked);
1555   tcase_add_test (tc_chain, test_add_self);
1556   tcase_add_test (tc_chain, test_iterate_sorted);
1557   tcase_add_test (tc_chain, test_link_structure_change);
1558   tcase_add_test (tc_chain, test_state_failure_remove);
1559   tcase_add_test (tc_chain, test_state_failure_unref);
1560   tcase_add_test (tc_chain, test_state_change_skip);
1561   tcase_add_test (tc_chain, test_duration_is_max);
1562   tcase_add_test (tc_chain, test_duration_unknown_overrides);
1563
1564   /* fails on OSX build bot for some reason, and is a bit silly anyway */
1565   if (0)
1566     tcase_add_test (tc_chain, test_many_bins);
1567
1568   return s;
1569 }
1570
1571 GST_CHECK_MAIN (gst_bin);