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