]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/gstreamer0-10.git/blob - gst/gstbin.c
c57d5eca8427d82472040bc1ddf3a62f6fe6fa8b
[glsdk/gstreamer0-10.git] / gst / gstbin.c
1 /* GStreamer
2  *
3  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
4  *                    2004 Wim Taymans <wim@fluendo.com>
5  *
6  * gstbin.c: GstBin container object and support code
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  *
23  * MT safe.
24  */
25 /**
26  * SECTION:gstbin
27  * @short_description: Base class for elements that contain other elements
28  *
29  * GstBin is the simplest of the container elements, allowing elements to
30  * become children of itself.  Pads from the child elements can be ghosted to
31  * the bin, making the bin itself look transparently like any other element,
32  * allowing for deep nesting of predefined sub-pipelines.
33  *
34  * A new GstBin is created with gst_bin_new(). Use a #GstPipeline instead if you
35  * want to create a toplevel bin because a normal bin doesn't have a bus or
36  * handle clock distribution of its own.
37  * 
38  * After the bin has been created you will typically add elements to it with
39  * gst_bin_add(). You can remove elements with gst_bin_remove().
40  *
41  * An element can be retrieved from a bin with gst_bin_get_by_name(), using the
42  * elements name. gst_bin_get_by_name_recurse_up() is mainly used for internal
43  * purposes and will query the parent bins when the element is not found in the
44  * current bin.
45  * 
46  * An iterator of elements in a bin can be retrieved with 
47  * gst_bin_iterate_elements(). Various other iterators exist to retrieve the
48  * elements in a bin.
49  * 
50  * The "element_added" signal is fired whenever a new element is added to the
51  * bin. Likewise the "element_removed" signal is fired whenever an element is
52  * removed from the bin.
53  *
54  * gst_bin_unref() is used to destroy the bin. 
55  */
57 #include "gst_private.h"
59 #include "gstevent.h"
60 #include "gstbin.h"
61 #include "gstmarshal.h"
62 #include "gstxml.h"
63 #include "gstinfo.h"
64 #include "gsterror.h"
66 #include "gstindex.h"
67 #include "gstindexfactory.h"
68 #include "gstutils.h"
69 #include "gstchildproxy.h"
71 GST_DEBUG_CATEGORY_STATIC (bin_debug);
72 #define GST_CAT_DEFAULT bin_debug
73 #define GST_LOG_BIN_CONTENTS(bin, text) GST_LOG_OBJECT ((bin), \
74         text ": %d elements: %u PLAYING, %u PAUSED, %u READY, %u NULL, own state: %s", \
75         (bin)->numchildren, (guint) (bin)->child_states[3], \
76         (guint) (bin)->child_states[2], (bin)->child_states[1], \
77         (bin)->child_states[0], gst_element_state_get_name (GST_STATE (bin)))
80 static GstElementDetails gst_bin_details = GST_ELEMENT_DETAILS ("Generic bin",
81     "Generic/Bin",
82     "Simple container object",
83     "Erik Walthinsen <omega@cse.ogi.edu>," "Wim Taymans <wim@fluendo.com>");
85 GType _gst_bin_type = 0;
87 static void gst_bin_dispose (GObject * object);
89 static GstStateChangeReturn gst_bin_change_state (GstElement * element,
90     GstStateChange transition);
91 static GstStateChangeReturn gst_bin_get_state (GstElement * element,
92     GstState * state, GstState * pending, GTimeVal * timeout);
94 static gboolean gst_bin_add_func (GstBin * bin, GstElement * element);
95 static gboolean gst_bin_remove_func (GstBin * bin, GstElement * element);
97 #ifndef GST_DISABLE_INDEX
98 static void gst_bin_set_index_func (GstElement * element, GstIndex * index);
99 #endif
100 static GstClock *gst_bin_get_clock_func (GstElement * element);
101 static void gst_bin_set_clock_func (GstElement * element, GstClock * clock);
103 static gboolean gst_bin_send_event (GstElement * element, GstEvent * event);
104 static GstBusSyncReply bin_bus_handler (GstBus * bus,
105     GstMessage * message, GstBin * bin);
106 static gboolean gst_bin_query (GstElement * element, GstQuery * query);
108 #ifndef GST_DISABLE_LOADSAVE
109 static xmlNodePtr gst_bin_save_thyself (GstObject * object, xmlNodePtr parent);
110 static void gst_bin_restore_thyself (GstObject * object, xmlNodePtr self);
111 #endif
113 static gint bin_element_is_sink (GstElement * child, GstBin * bin);
115 /* Bin signals and args */
116 enum
118   ELEMENT_ADDED,
119   ELEMENT_REMOVED,
120   LAST_SIGNAL
121 };
123 enum
125   ARG_0
126       /* FILL ME */
127 };
129 static void gst_bin_base_init (gpointer g_class);
130 static void gst_bin_class_init (GstBinClass * klass);
131 static void gst_bin_init (GstBin * bin);
132 static void gst_bin_child_proxy_init (gpointer g_iface, gpointer iface_data);
134 static GstElementClass *parent_class = NULL;
135 static guint gst_bin_signals[LAST_SIGNAL] = { 0 };
137 /**
138  * gst_bin_get_type:
139  *
140  * Returns: the type of #GstBin
141  */
142 GType
143 gst_bin_get_type (void)
145   if (!_gst_bin_type) {
146     static const GTypeInfo bin_info = {
147       sizeof (GstBinClass),
148       gst_bin_base_init,
149       NULL,
150       (GClassInitFunc) gst_bin_class_init,
151       NULL,
152       NULL,
153       sizeof (GstBin),
154       0,
155       (GInstanceInitFunc) gst_bin_init,
156       NULL
157     };
158     static const GInterfaceInfo child_proxy_info = {
159       gst_bin_child_proxy_init,
160       NULL,
161       NULL
162     };
164     _gst_bin_type =
165         g_type_register_static (GST_TYPE_ELEMENT, "GstBin", &bin_info, 0);
167     g_type_add_interface_static (_gst_bin_type, GST_TYPE_CHILD_PROXY,
168         &child_proxy_info);
170     GST_DEBUG_CATEGORY_INIT (bin_debug, "bin", GST_DEBUG_BOLD,
171         "debugging info for the 'bin' container element");
172   }
173   return _gst_bin_type;
176 static void
177 gst_bin_base_init (gpointer g_class)
179   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
181   gst_element_class_set_details (gstelement_class, &gst_bin_details);
184 static GstObject *
185 gst_bin_child_proxy_get_child_by_index (GstChildProxy * child_proxy,
186     guint index)
188   return g_list_nth_data (GST_BIN (child_proxy)->children, index);
191 guint
192 gst_bin_child_proxy_get_children_count (GstChildProxy * child_proxy)
194   return GST_BIN (child_proxy)->numchildren;
197 static void
198 gst_bin_child_proxy_init (gpointer g_iface, gpointer iface_data)
200   GstChildProxyInterface *iface = g_iface;
202   iface->get_children_count = gst_bin_child_proxy_get_children_count;
203   iface->get_child_by_index = gst_bin_child_proxy_get_child_by_index;
206 static void
207 gst_bin_class_init (GstBinClass * klass)
209   GObjectClass *gobject_class;
210   GstObjectClass *gstobject_class;
211   GstElementClass *gstelement_class;
213   gobject_class = (GObjectClass *) klass;
214   gstobject_class = (GstObjectClass *) klass;
215   gstelement_class = (GstElementClass *) klass;
217   parent_class = g_type_class_peek_parent (klass);
219   /**
220    * GstBin::element-added:
221    * @bin: the object which emitted the signal.
222    * @element: the element that was added to the bin
223    *
224    * Will be emitted if a new element was removed/added to this bin.
225    */
226   gst_bin_signals[ELEMENT_ADDED] =
227       g_signal_new ("element-added", G_TYPE_FROM_CLASS (klass),
228       G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstBinClass, element_added), NULL,
229       NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
230   /**
231    * GstBin::element-removed:
232    * @bin: the object which emitted the signal.
233    * @element: the element that was removed from the bin
234    *
235    * Will be emitted if an element was removed from this bin.
236    */
237   gst_bin_signals[ELEMENT_REMOVED] =
238       g_signal_new ("element-removed", G_TYPE_FROM_CLASS (klass),
239       G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstBinClass, element_removed), NULL,
240       NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
242   gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_bin_dispose);
244 #ifndef GST_DISABLE_LOADSAVE
245   gstobject_class->save_thyself = GST_DEBUG_FUNCPTR (gst_bin_save_thyself);
246   gstobject_class->restore_thyself =
247       GST_DEBUG_FUNCPTR (gst_bin_restore_thyself);
248 #endif
250   gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_bin_change_state);
251   gstelement_class->get_state = GST_DEBUG_FUNCPTR (gst_bin_get_state);
252 #ifndef GST_DISABLE_INDEX
253   gstelement_class->set_index = GST_DEBUG_FUNCPTR (gst_bin_set_index_func);
254 #endif
255   gstelement_class->get_clock = GST_DEBUG_FUNCPTR (gst_bin_get_clock_func);
256   gstelement_class->set_clock = GST_DEBUG_FUNCPTR (gst_bin_set_clock_func);
258   gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_bin_send_event);
259   gstelement_class->query = GST_DEBUG_FUNCPTR (gst_bin_query);
261   klass->add_element = GST_DEBUG_FUNCPTR (gst_bin_add_func);
262   klass->remove_element = GST_DEBUG_FUNCPTR (gst_bin_remove_func);
265 static void
266 gst_bin_init (GstBin * bin)
268   GstBus *bus;
270   bin->numchildren = 0;
271   bin->children = NULL;
272   bin->children_cookie = 0;
273   bin->eosed = NULL;
275   /* Set up a bus for listening to child elements,
276    * and one for sending messages up the hierarchy */
277   bus = g_object_new (gst_bus_get_type (), NULL);
278   bin->child_bus = bus;
279   gst_bus_set_sync_handler (bus, (GstBusSyncHandler) bin_bus_handler, bin);
281   bus = g_object_new (gst_bus_get_type (), NULL);
282   gst_element_set_bus (GST_ELEMENT (bin), bus);
283   /* set_bus refs the bus via gst_object_replace, we drop our ref */
284   gst_object_unref (bus);
287 /**
288  * gst_bin_new:
289  * @name: name of new bin
290  *
291  * Create a new bin with given name.
292  *
293  * Returns: new bin
294  */
295 GstElement *
296 gst_bin_new (const gchar * name)
298   return gst_element_factory_make ("bin", name);
301 /* set the index on all elements in this bin
302  *
303  * MT safe
304  */
305 #ifndef GST_DISABLE_INDEX
306 static void
307 gst_bin_set_index_func (GstElement * element, GstIndex * index)
309   GstBin *bin;
310   GList *children;
312   bin = GST_BIN (element);
314   GST_LOCK (bin);
315   for (children = bin->children; children; children = g_list_next (children)) {
316     GstElement *child = GST_ELEMENT (children->data);
318     gst_element_set_index (child, index);
319   }
320   GST_UNLOCK (bin);
322 #endif
324 /* set the clock on all elements in this bin
325  *
326  * MT safe
327  */
328 static void
329 gst_bin_set_clock_func (GstElement * element, GstClock * clock)
331   GList *children;
332   GstBin *bin;
334   bin = GST_BIN (element);
336   GST_LOCK (bin);
337   for (children = bin->children; children; children = g_list_next (children)) {
338     GstElement *child = GST_ELEMENT (children->data);
340     gst_element_set_clock (child, clock);
341   }
342   GST_UNLOCK (bin);
345 /* get the clock for this bin by asking all of the children in this bin
346  *
347  * MT safe
348  */
349 static GstClock *
350 gst_bin_get_clock_func (GstElement * element)
352   GstClock *result = NULL;
353   GstBin *bin;
354   GList *children;
356   bin = GST_BIN (element);
358   GST_LOCK (bin);
359   for (children = bin->children; children; children = g_list_next (children)) {
360     GstElement *child = GST_ELEMENT (children->data);
362     result = gst_element_get_clock (child);
363     if (result)
364       break;
365   }
366   GST_UNLOCK (bin);
368   return result;
371 static gboolean
372 is_eos (GstBin * bin)
374   GstIterator *sinks;
375   gboolean result = TRUE;
376   gboolean done = FALSE;
378   sinks = gst_bin_iterate_sinks (bin);
379   while (!done) {
380     gpointer data;
382     switch (gst_iterator_next (sinks, &data)) {
383       case GST_ITERATOR_OK:
384       {
385         GstElement *element = GST_ELEMENT (data);
386         GList *eosed;
387         gchar *name;
389         name = gst_element_get_name (element);
390         eosed = g_list_find (bin->eosed, element);
391         if (!eosed) {
392           GST_DEBUG ("element %s did not post EOS yet", name);
393           result = FALSE;
394           done = TRUE;
395         } else {
396           GST_DEBUG ("element %s posted EOS", name);
397         }
398         g_free (name);
399         gst_object_unref (element);
400         break;
401       }
402       case GST_ITERATOR_RESYNC:
403         result = TRUE;
404         gst_iterator_resync (sinks);
405         break;
406       case GST_ITERATOR_DONE:
407         done = TRUE;
408         break;
409       default:
410         g_assert_not_reached ();
411         break;
412     }
413   }
414   gst_iterator_free (sinks);
415   return result;
418 static void
419 unlink_pads (GstPad * pad)
421   GstPad *peer;
423   if ((peer = gst_pad_get_peer (pad))) {
424     if (gst_pad_get_direction (pad) == GST_PAD_SRC)
425       gst_pad_unlink (pad, peer);
426     else
427       gst_pad_unlink (peer, pad);
428     gst_object_unref (peer);
429   }
430   gst_object_unref (pad);
433 /* add an element to this bin
434  *
435  * MT safe
436  */
437 static gboolean
438 gst_bin_add_func (GstBin * bin, GstElement * element)
440   gchar *elem_name;
441   GstIterator *it;
443   /* we obviously can't add ourself to ourself */
444   if (G_UNLIKELY (GST_ELEMENT_CAST (element) == GST_ELEMENT_CAST (bin)))
445     goto adding_itself;
447   /* get the element name to make sure it is unique in this bin. */
448   GST_LOCK (element);
449   elem_name = g_strdup (GST_ELEMENT_NAME (element));
450   GST_UNLOCK (element);
452   GST_LOCK (bin);
454   /* then check to see if the element's name is already taken in the bin,
455    * we can safely take the lock here. This check is probably bogus because
456    * you can safely change the element name after this check and before setting
457    * the object parent. The window is very small though... */
458   if (G_UNLIKELY (!gst_object_check_uniqueness (bin->children, elem_name)))
459     goto duplicate_name;
461   /* set the element's parent and add the element to the bin's list of children */
462   if (G_UNLIKELY (!gst_object_set_parent (GST_OBJECT_CAST (element),
463               GST_OBJECT_CAST (bin))))
464     goto had_parent;
466   /* if we add a sink we become a sink */
467   if (GST_FLAG_IS_SET (element, GST_ELEMENT_IS_SINK)) {
468     GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, bin, "element \"%s\" was sink",
469         elem_name);
470     GST_FLAG_SET (bin, GST_ELEMENT_IS_SINK);
471   }
473   bin->children = g_list_prepend (bin->children, element);
474   bin->numchildren++;
475   bin->children_cookie++;
477   /* distribute the bus */
478   gst_element_set_bus (element, bin->child_bus);
480   /* propagate the current base time and clock */
481   gst_element_set_base_time (element, GST_ELEMENT (bin)->base_time);
482   gst_element_set_clock (element, GST_ELEMENT_CLOCK (bin));
484   GST_UNLOCK (bin);
486   /* unlink all linked pads */
487   it = gst_element_iterate_pads (element);
488   gst_iterator_foreach (it, (GFunc) unlink_pads, element);
489   gst_iterator_free (it);
491   GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, bin, "added element \"%s\"",
492       elem_name);
493   g_free (elem_name);
495   g_signal_emit (G_OBJECT (bin), gst_bin_signals[ELEMENT_ADDED], 0, element);
497   return TRUE;
499   /* ERROR handling here */
500 adding_itself:
501   {
502     GST_LOCK (bin);
503     g_warning ("Cannot add bin %s to itself", GST_ELEMENT_NAME (bin));
504     GST_UNLOCK (bin);
505     return FALSE;
506   }
507 duplicate_name:
508   {
509     g_warning ("Name %s is not unique in bin %s, not adding",
510         elem_name, GST_ELEMENT_NAME (bin));
511     GST_UNLOCK (bin);
512     g_free (elem_name);
513     return FALSE;
514   }
515 had_parent:
516   {
517     g_warning ("Element %s already has parent", elem_name);
518     GST_UNLOCK (bin);
519     g_free (elem_name);
520     return FALSE;
521   }
525 /**
526  * gst_bin_add:
527  * @bin: #GstBin to add element to
528  * @element: #GstElement to add to bin
529  *
530  * Adds the given element to the bin.  Sets the element's parent, and thus
531  * takes ownership of the element. An element can only be added to one bin.
532  *
533  * If the element's pads are linked to other pads, the pads will be unlinked
534  * before the element is added to the bin.
535  *
536  * MT safe.
537  *
538  * Returns: TRUE if the element could be added, FALSE on wrong parameters or
539  * the bin does not want to accept the element.
540  */
541 gboolean
542 gst_bin_add (GstBin * bin, GstElement * element)
544   GstBinClass *bclass;
545   gboolean result;
547   g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
548   g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
550   bclass = GST_BIN_GET_CLASS (bin);
552   if (G_UNLIKELY (bclass->add_element == NULL))
553     goto no_function;
555   GST_CAT_DEBUG (GST_CAT_PARENTAGE, "adding element %s to bin %s",
556       GST_ELEMENT_NAME (element), GST_ELEMENT_NAME (bin));
558   result = bclass->add_element (bin, element);
560   return result;
562   /* ERROR handling */
563 no_function:
564   {
565     g_warning ("adding elements to bin %s is not supported",
566         GST_ELEMENT_NAME (bin));
567     return FALSE;
568   }
571 /* remove an element from the bin
572  *
573  * MT safe
574  */
575 static gboolean
576 gst_bin_remove_func (GstBin * bin, GstElement * element)
578   gchar *elem_name;
579   GstIterator *it;
581   GST_LOCK (element);
582   /* Check if the element is already being removed and immediately
583    * return */
584   if (G_UNLIKELY (GST_FLAG_IS_SET (element, GST_ELEMENT_UNPARENTING)))
585     goto already_removing;
587   GST_FLAG_SET (element, GST_ELEMENT_UNPARENTING);
588   /* grab element name so we can print it */
589   elem_name = g_strdup (GST_ELEMENT_NAME (element));
590   GST_UNLOCK (element);
592   /* unlink all linked pads */
593   it = gst_element_iterate_pads (element);
594   gst_iterator_foreach (it, (GFunc) unlink_pads, element);
595   gst_iterator_free (it);
597   GST_LOCK (bin);
598   /* the element must be in the bin's list of children */
599   if (G_UNLIKELY (g_list_find (bin->children, element) == NULL))
600     goto not_in_bin;
602   /* now remove the element from the list of elements */
603   bin->children = g_list_remove (bin->children, element);
604   bin->numchildren--;
605   bin->children_cookie++;
607   /* check if we removed a sink */
608   if (GST_FLAG_IS_SET (element, GST_ELEMENT_IS_SINK)) {
609     GList *other_sink;
611     /* check if we removed the last sink */
612     other_sink = g_list_find_custom (bin->children,
613         bin, (GCompareFunc) bin_element_is_sink);
614     if (!other_sink) {
615       /* yups, we're not a sink anymore */
616       GST_FLAG_UNSET (bin, GST_ELEMENT_IS_SINK);
617     }
618   }
619   GST_UNLOCK (bin);
621   GST_CAT_INFO_OBJECT (GST_CAT_PARENTAGE, bin, "removed child \"%s\"",
622       elem_name);
623   g_free (elem_name);
625   gst_element_set_bus (element, NULL);
627   /* unlock any waiters for the state change. It is possible that
628    * we are waiting for an ASYNC state change on this element. The
629    * element cannot be added to another bin yet as it is not yet
630    * unparented. */
631   GST_STATE_LOCK (element);
632   GST_STATE_BROADCAST (element);
633   GST_STATE_UNLOCK (element);
635   /* we ref here because after the _unparent() the element can be disposed
636    * and we still need it to reset the UNPARENTING flag and fire a signal. */
637   gst_object_ref (element);
638   gst_object_unparent (GST_OBJECT_CAST (element));
640   GST_LOCK (element);
641   GST_FLAG_UNSET (element, GST_ELEMENT_UNPARENTING);
642   GST_UNLOCK (element);
644   g_signal_emit (G_OBJECT (bin), gst_bin_signals[ELEMENT_REMOVED], 0, element);
646   /* element is really out of our control now */
647   gst_object_unref (element);
649   return TRUE;
651   /* ERROR handling */
652 not_in_bin:
653   {
654     g_warning ("Element %s is not in bin %s", elem_name,
655         GST_ELEMENT_NAME (bin));
656     GST_UNLOCK (bin);
657     g_free (elem_name);
658     return FALSE;
659   }
660 already_removing:
661   {
662     GST_UNLOCK (element);
663     return FALSE;
664   }
667 /**
668  * gst_bin_remove:
669  * @bin: #GstBin to remove element from
670  * @element: #GstElement to remove
671  *
672  * Remove the element from its associated bin, unparenting it as well.
673  * Unparenting the element means that the element will be dereferenced,
674  * so if the bin holds the only reference to the element, the element
675  * will be freed in the process of removing it from the bin.  If you
676  * want the element to still exist after removing, you need to call
677  * gst_object_ref() before removing it from the bin.
678  *
679  * If the element's pads are linked to other pads, the pads will be unlinked
680  * before the element is removed from the bin.
681  *
682  * MT safe.
683  *
684  * Returns: TRUE if the element could be removed, FALSE on wrong parameters or
685  * the bin does not want to remove the element.
686  */
687 gboolean
688 gst_bin_remove (GstBin * bin, GstElement * element)
690   GstBinClass *bclass;
691   gboolean result;
693   g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
694   g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
696   bclass = GST_BIN_GET_CLASS (bin);
698   if (G_UNLIKELY (bclass->remove_element == NULL))
699     goto no_function;
701   GST_CAT_DEBUG (GST_CAT_PARENTAGE, "removing element %s from bin %s",
702       GST_ELEMENT_NAME (element), GST_ELEMENT_NAME (bin));
704   result = bclass->remove_element (bin, element);
706   return result;
708   /* ERROR handling */
709 no_function:
710   {
711     g_warning ("removing elements from bin %s is not supported",
712         GST_ELEMENT_NAME (bin));
713     return FALSE;
714   }
717 static GstIteratorItem
718 iterate_child (GstIterator * it, GstElement * child)
720   gst_object_ref (child);
721   return GST_ITERATOR_ITEM_PASS;
724 /**
725  * gst_bin_iterate_elements:
726  * @bin: #Gstbin to iterate the elements of
727  *
728  * Get an iterator for the elements in this bin.
729  * Each element will have its refcount increased, so unref
730  * after use.
731  *
732  * MT safe.
733  *
734  * Returns: a #GstIterator of #GstElements. gst_iterator_free after
735  * use. returns NULL when passing bad parameters.
736  */
737 GstIterator *
738 gst_bin_iterate_elements (GstBin * bin)
740   GstIterator *result;
742   g_return_val_if_fail (GST_IS_BIN (bin), NULL);
744   GST_LOCK (bin);
745   /* add ref because the iterator refs the bin. When the iterator
746    * is freed it will unref the bin again using the provided dispose
747    * function. */
748   gst_object_ref (bin);
749   result = gst_iterator_new_list (GST_GET_LOCK (bin),
750       &bin->children_cookie,
751       &bin->children,
752       bin,
753       (GstIteratorItemFunction) iterate_child,
754       (GstIteratorDisposeFunction) gst_object_unref);
755   GST_UNLOCK (bin);
757   return result;
760 static GstIteratorItem
761 iterate_child_recurse (GstIterator * it, GstElement * child)
763   gst_object_ref (child);
764   if (GST_IS_BIN (child)) {
765     GstIterator *other = gst_bin_iterate_recurse (GST_BIN (child));
767     gst_iterator_push (it, other);
768   }
769   return GST_ITERATOR_ITEM_PASS;
772 /**
773  * gst_bin_iterate_recurse:
774  * @bin: #Gstbin to iterate the elements of
775  *
776  * Get an iterator for the elements in this bin.
777  * Each element will have its refcount increased, so unref
778  * after use. This iterator recurses into GstBin children.
779  *
780  * MT safe.
781  *
782  * Returns: a #GstIterator of #GstElements. gst_iterator_free after
783  * use. returns NULL when passing bad parameters.
784  */
785 GstIterator *
786 gst_bin_iterate_recurse (GstBin * bin)
788   GstIterator *result;
790   g_return_val_if_fail (GST_IS_BIN (bin), NULL);
792   GST_LOCK (bin);
793   /* add ref because the iterator refs the bin. When the iterator
794    * is freed it will unref the bin again using the provided dispose
795    * function. */
796   gst_object_ref (bin);
797   result = gst_iterator_new_list (GST_GET_LOCK (bin),
798       &bin->children_cookie,
799       &bin->children,
800       bin,
801       (GstIteratorItemFunction) iterate_child_recurse,
802       (GstIteratorDisposeFunction) gst_object_unref);
803   GST_UNLOCK (bin);
805   return result;
808 /* returns 0 when TRUE because this is a GCompareFunc */
809 /* MT safe */
810 static gint
811 bin_element_is_sink (GstElement * child, GstBin * bin)
813   gboolean is_sink;
815   /* we lock the child here for the remainder of the function to
816    * get its name safely. */
817   GST_LOCK (child);
818   is_sink = GST_FLAG_IS_SET (child, GST_ELEMENT_IS_SINK);
820   GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
821       "child %s %s sink", GST_OBJECT_NAME (child), is_sink ? "is" : "is not");
823   GST_UNLOCK (child);
824   return is_sink ? 0 : 1;
827 /* returns 0 when TRUE because this is a GCompareFunc.
828  * This function returns elements that have no connected srcpads and
829  * are therefore not reachable from a real sink. */
830 /* MT safe */
831 static gint
832 bin_element_is_semi_sink (GstElement * child, GstBin * bin)
834   int ret = 1;
836   /* we lock the child here for the remainder of the function to
837    * get its pads and name safely. */
838   GST_LOCK (child);
840   /* check if this is a sink element, these are the elements
841    * without (linked) source pads. */
842   if (child->numsrcpads == 0) {
843     /* shortcut */
844     GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
845         "adding child %s as sink", GST_OBJECT_NAME (child));
846     ret = 0;
847   } else {
848     /* loop over all pads, try to figure out if this element
849      * is a semi sink because it has no linked source pads */
850     GList *pads;
851     gboolean connected_src = FALSE;
853     for (pads = child->srcpads; pads; pads = g_list_next (pads)) {
854       GstPad *peer;
856       GST_DEBUG ("looking at pad %p", pads->data);
857       if ((peer = gst_pad_get_peer (GST_PAD_CAST (pads->data)))) {
858         connected_src =
859             gst_object_has_ancestor (GST_OBJECT_CAST (peer),
860             GST_OBJECT_CAST (bin));
861         gst_object_unref (peer);
862         if (connected_src) {
863           break;
864         }
865       }
866     }
867     if (connected_src) {
868       GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
869           "not adding child %s as sink: linked source pads",
870           GST_OBJECT_NAME (child));
871     } else {
872       GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
873           "adding child %s as sink since it has unlinked source pads in this bin",
874           GST_OBJECT_NAME (child));
875       ret = 0;
876     }
877   }
878   GST_UNLOCK (child);
880   return ret;
883 static gint
884 sink_iterator_filter (GstElement * child, GstBin * bin)
886   if (bin_element_is_sink (child, bin) == 0) {
887     /* returns 0 because this is a GCompareFunc */
888     return 0;
889   } else {
890     /* child carries a ref from gst_bin_iterate_elements -- drop if not passing
891        through */
892     gst_object_unref ((GstObject *) child);
893     return 1;
894   }
897 /**
898  * gst_bin_iterate_sinks:
899  * @bin: #Gstbin to iterate on
900  *
901  * Get an iterator for the sink elements in this bin.
902  * Each element will have its refcount increased, so unref
903  * after use.
904  *
905  * The sink elements are those without any linked srcpads.
906  *
907  * MT safe.
908  *
909  * Returns: a #GstIterator of #GstElements. gst_iterator_free after use.
910  */
911 GstIterator *
912 gst_bin_iterate_sinks (GstBin * bin)
914   GstIterator *children;
915   GstIterator *result;
917   g_return_val_if_fail (GST_IS_BIN (bin), NULL);
919   children = gst_bin_iterate_elements (bin);
920   result = gst_iterator_filter (children,
921       (GCompareFunc) sink_iterator_filter, bin);
923   return result;
926 /* 2 phases:
927  *  1) check state of all children with 0 timeout to find ERROR and
928  *     NO_PREROLL elements. return if found.
929  *  2) perform full blocking wait with requested timeout.
930  *
931  * 2) cannot be performed when 1) returns results as the sinks might
932  *    not be able to complete the state change making 2) block forever.
933  *
934  * MT safe
935  */
936 static GstStateChangeReturn
937 gst_bin_get_state (GstElement * element, GstState * state,
938     GstState * pending, GTimeVal * timeout)
940   GstBin *bin = GST_BIN (element);
941   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
942   GList *children;
943   guint32 children_cookie;
944   gboolean have_no_preroll;
946   GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "getting state");
948   /* lock bin, no element can be added or removed between going into
949    * the quick scan and the blocking wait. */
950   GST_LOCK (bin);
952 restart:
953   have_no_preroll = FALSE;
955   /* first we need to poll with a non zero timeout to make sure we don't block
956    * on the sinks when we have NO_PREROLL elements. This is why we do
957    * a quick check if there are still NO_PREROLL elements. We also
958    * catch the error elements this way. */
959   {
960     GTimeVal tv;
961     gboolean have_async = FALSE;
963     GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "checking for NO_PREROLL");
964     /* use 0 timeout so we don't block on the sinks */
965     GST_TIME_TO_TIMEVAL (0, tv);
966     children = bin->children;
967     children_cookie = bin->children_cookie;
968     while (children) {
969       GstElement *child = GST_ELEMENT_CAST (children->data);
971       gst_object_ref (child);
972       /* now we release the lock to enter a non blocking wait. We
973        * release the lock anyway since we can. */
974       GST_UNLOCK (bin);
976       ret = gst_element_get_state (child, NULL, NULL, &tv);
978       gst_object_unref (child);
980       /* now grab the lock to iterate to the next child */
981       GST_LOCK (bin);
982       if (G_UNLIKELY (children_cookie != bin->children_cookie)) {
983         /* child added/removed during state change, restart. We need
984          * to restart with the quick check as a no-preroll element could
985          * have been added here and we don't want to block on sinks then.*/
986         goto restart;
987       }
989       switch (ret) {
990           /* report FAILURE  immediatly */
991         case GST_STATE_CHANGE_FAILURE:
992           goto done;
993         case GST_STATE_CHANGE_NO_PREROLL:
994           /* we have to continue scanning as there might be
995            * ERRORS too */
996           have_no_preroll = TRUE;
997           break;
998         case GST_STATE_CHANGE_ASYNC:
999           have_async = TRUE;
1000           break;
1001         default:
1002           break;
1003       }
1004       children = g_list_next (children);
1005     }
1006     /* if we get here, we have no FAILURES, check for any NO_PREROLL
1007      * elements then. */
1008     if (have_no_preroll) {
1009       ret = GST_STATE_CHANGE_NO_PREROLL;
1010       goto done;
1011     }
1013     /* if we get here, no NO_PREROLL elements are in the pipeline */
1014     GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "no NO_PREROLL elements");
1016     /* if no ASYNC elements exist we don't even have to poll with a
1017      * timeout again */
1018     if (!have_async) {
1019       ret = GST_STATE_CHANGE_SUCCESS;
1020       goto done;
1021     }
1022   }
1024   /* next we poll all children for their state to see if one of them
1025    * is still busy with its state change. We did not release the bin lock
1026    * yet so the elements are the same as the ones from the quick scan. */
1027   children = bin->children;
1028   children_cookie = bin->children_cookie;
1029   while (children) {
1030     GstElement *child = GST_ELEMENT_CAST (children->data);
1032     gst_object_ref (child);
1033     /* now we release the lock to enter the potentialy blocking wait */
1034     GST_UNLOCK (bin);
1036     /* ret is ASYNC if some child is still performing the state change
1037      * ater the timeout. */
1038     ret = gst_element_get_state (child, NULL, NULL, timeout);
1040     gst_object_unref (child);
1042     /* now grab the lock to iterate to the next child */
1043     GST_LOCK (bin);
1044     if (G_UNLIKELY (children_cookie != bin->children_cookie)) {
1045       /* child added/removed during state change, restart. We need
1046        * to restart with the quick check as a no-preroll element could
1047        * have been added here and we don't want to block on sinks then.*/
1048       goto restart;
1049     }
1051     switch (ret) {
1052       case GST_STATE_CHANGE_SUCCESS:
1053         break;
1054       case GST_STATE_CHANGE_FAILURE:
1055       case GST_STATE_CHANGE_NO_PREROLL:
1056         /* report FAILURE and NO_PREROLL immediatly */
1057         goto done;
1058       case GST_STATE_CHANGE_ASYNC:
1059         goto done;
1060       default:
1061         g_assert_not_reached ();
1062     }
1063     children = g_list_next (children);
1064   }
1065   /* if we got here, all elements can do preroll */
1066   have_no_preroll = FALSE;
1068 done:
1069   GST_UNLOCK (bin);
1071   /* now we can take the state lock, it is possible that new elements
1072    * are added now and we still report the old state. No problem though as
1073    * the return is still consistent, the effect is as if the element was
1074    * added after this function completed. */
1075   GST_STATE_LOCK (bin);
1076   switch (ret) {
1077     case GST_STATE_CHANGE_SUCCESS:
1078       /* we can commit the state */
1079       gst_element_commit_state (element);
1080       break;
1081     case GST_STATE_CHANGE_FAILURE:
1082       /* some element failed, abort the state change */
1083       gst_element_abort_state (element);
1084       break;
1085     default:
1086       /* other cases are just passed along */
1087       break;
1088   }
1090   /* and report the state if needed */
1091   if (state)
1092     *state = GST_STATE (element);
1093   if (pending)
1094     *pending = GST_STATE_PENDING (element);
1096   GST_STATE_NO_PREROLL (element) = have_no_preroll;
1098   GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1099       "state current: %s, pending: %s, error: %d, no_preroll: %d, result: %d",
1100       gst_element_state_get_name (GST_STATE (element)),
1101       gst_element_state_get_name (GST_STATE_PENDING (element)),
1102       GST_STATE_ERROR (element), GST_STATE_NO_PREROLL (element), ret);
1104   GST_STATE_UNLOCK (bin);
1106   return ret;
1109 /**
1110  * gst_bin_iterate_state_order:
1111  * @bin: #Gstbin to iterate on
1112  *
1113  * Get an iterator for the elements in this bin in the order
1114  * in which a state change should be performed on them. This 
1115  * means that first the sinks and then the other elements will
1116  * be returned.
1117  * Each element will have its refcount increased, so unref
1118  * after use.
1119  *
1120  * Not implemented yet.
1121  *
1122  * MT safe.
1123  *
1124  * Returns: a #GstIterator of #GstElements. gst_iterator_free after use.
1125  */
1126 GstIterator *
1127 gst_bin_iterate_state_order (GstBin * bin)
1129   GstIterator *result;
1131   g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1133   result = NULL;
1135   return result;
1138 static void
1139 clear_queue (GQueue * queue, gboolean unref)
1141   gpointer p;
1143   while ((p = g_queue_pop_head (queue)))
1144     if (unref)
1145       gst_object_unref (p);
1148 static void
1149 remove_all_from_queue (GQueue * queue, gpointer elem, gboolean unref)
1151   gpointer p;
1153   while ((p = g_queue_find (queue, elem))) {
1154     if (unref)
1155       gst_object_unref (elem);
1156     g_queue_delete_link (queue, p);
1157   }
1160 /* this function is called with the STATE_LOCK held. It works
1161  * as follows:
1162  *
1163  * 1) put all sink elements on the queue.
1164  * 2) put all semisink elements on the queue.
1165  * 3) change state of elements in queue, put linked elements to queue.
1166  * 4) while queue not empty goto 3)
1167  *
1168  * This will effectively change the state of all elements in the bin
1169  * from the sinks to the sources. We have to change the states this
1170  * way so that when a source element pushes data, the downstream element
1171  * is in the right state to receive the data.
1172  *
1173  * MT safe.
1174  */
1175 /* FIXME,  make me more elegant, want to use a topological sort algorithm
1176  * based on indegrees (or outdegrees in our case) */
1177 static GstStateChangeReturn
1178 gst_bin_change_state (GstElement * element, GstStateChange transition)
1180   GstBin *bin;
1181   GstStateChangeReturn ret;
1182   GstState old_state, pending;
1183   gboolean have_async = FALSE;
1184   gboolean have_no_preroll = FALSE;
1185   GList *children;
1186   guint32 children_cookie;
1187   GQueue *elem_queue;           /* list of elements waiting for a state change */
1188   GQueue *semi_queue;           /* list of elements with no connected srcpads */
1189   GQueue *temp;                 /* queue of leftovers */
1190   GstClockTime base_time;
1192   bin = GST_BIN (element);
1194   /* we don't need to take the STATE_LOCK, it is already taken */
1195   old_state = GST_STATE (element);
1196   pending = GST_STATE_PENDING (element);
1198   GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
1199       "changing state of children from %s to %s",
1200       gst_element_state_get_name (old_state),
1201       gst_element_state_get_name (pending));
1203   if (pending == GST_STATE_VOID_PENDING)
1204     return GST_STATE_CHANGE_SUCCESS;
1206   /* Clear eosed element list on READY-> PAUSED */
1207   if (transition == GST_STATE_CHANGE_READY_TO_PAUSED) {
1208     g_list_free (bin->eosed);
1209     bin->eosed = NULL;
1210   }
1212   /* all elements added to these queues should have their refcount
1213    * incremented */
1214   elem_queue = g_queue_new ();
1215   semi_queue = g_queue_new ();
1216   temp = g_queue_new ();
1218   /* first step, find all sink elements, these are the elements
1219    * without (linked) source pads. */
1220   GST_LOCK (bin);
1222 restart:
1223   /* take base time */
1224   base_time = element->base_time;
1226   /* make sure queues are empty, they could be filled when
1227    * restarting. */
1228   clear_queue (elem_queue, TRUE);
1229   clear_queue (semi_queue, TRUE);
1230   clear_queue (temp, TRUE);
1232   children = bin->children;
1233   children_cookie = bin->children_cookie;
1234   GST_DEBUG_OBJECT (bin, "reffing and examining children");
1235   while (children) {
1236     GstElement *child = GST_ELEMENT_CAST (children->data);
1238     gst_object_ref (child);
1239     GST_UNLOCK (bin);
1241     if (bin_element_is_sink (child, bin) == 0) {
1242       g_queue_push_tail (elem_queue, child);
1243     } else if (bin_element_is_semi_sink (child, bin) == 0) {
1244       g_queue_push_tail (semi_queue, child);
1245     } else {
1246       g_queue_push_tail (temp, child);
1247     }
1249     GST_LOCK (bin);
1250     if (G_UNLIKELY (children_cookie != bin->children_cookie)) {
1251       GST_INFO_OBJECT (bin, "bin->children_cookie changed, restarting");
1252       /* restart will unref the children in the queues so that we don't
1253        * leak refcounts. */
1254       goto restart;
1255     }
1256     children = g_list_next (children);
1257   }
1258   GST_DEBUG_OBJECT (bin, "reffed and examined children");
1259   GST_UNLOCK (bin);
1261   /* after this point new elements can be added/removed from the
1262    * bin. We operate on the snapshot taken above. Applications
1263    * should serialize their add/remove and set_state. */
1265   /* if we don't have real sinks, we continue with the other elements, there
1266    * has to be at least one element in the semi queue. */
1267   if (g_queue_is_empty (elem_queue) && !g_queue_is_empty (semi_queue)) {
1268     GQueue *q = elem_queue;
1270     /* we swap the queues as oposed to copy them over */
1271     elem_queue = semi_queue;
1272     semi_queue = q;
1273   }
1275   /* second step, change state of elements in the queue */
1276   GST_DEBUG_OBJECT (bin, "change state of elements in the queue");
1277   while (!g_queue_is_empty (elem_queue)) {
1278     GstElement *qelement;
1279     GList *pads;
1280     gboolean locked;
1282     /* take element */
1283     qelement = g_queue_pop_head (elem_queue);
1284     /* we don't need any duplicates in the other queue anymore */
1285     remove_all_from_queue (semi_queue, qelement, TRUE);
1286     remove_all_from_queue (temp, qelement, TRUE);
1288     /* queue all elements connected to the sinkpads of this element */
1289     GST_LOCK (qelement);
1290     pads = qelement->sinkpads;
1291     while (pads) {
1292       GstPad *pad = GST_PAD_CAST (pads->data);
1293       GstPad *peer;
1295       GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
1296           "found sinkpad %s:%s", GST_DEBUG_PAD_NAME (pad));
1298       peer = gst_pad_get_peer (pad);
1299       if (peer) {
1300         GstObject *peer_parent;
1302         /*  get parent */
1303         peer_parent = gst_object_get_parent (GST_OBJECT (peer));
1305         /* if we have an element parent, follow it */
1306         if (peer_parent && GST_IS_ELEMENT (peer_parent)) {
1307           GstObject *parent;
1309           /* see if this element is in the bin we are currently handling */
1310           parent = gst_object_get_parent (peer_parent);
1311           if (parent) {
1312             if (parent == GST_OBJECT_CAST (bin)) {
1313               GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
1314                   "adding element %s to queue", GST_ELEMENT_NAME (peer_parent));
1316               /* make sure we don't have duplicates */
1317               remove_all_from_queue (semi_queue, peer_parent, TRUE);
1318               remove_all_from_queue (elem_queue, peer_parent, TRUE);
1319               remove_all_from_queue (temp, peer_parent, TRUE);
1321               /* was reffed before pushing on the queue by the
1322                * gst_object_get_parent() call we used to get the element. */
1323               g_queue_push_tail (elem_queue, peer_parent);
1324               /* so that we don't unref it */
1325               peer_parent = NULL;
1326             } else {
1327               GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
1328                   "not adding element %s to queue, it is in another bin",
1329                   GST_ELEMENT_NAME (peer_parent));
1330             }
1331             gst_object_unref (parent);
1332           }
1333         }
1334         if (peer_parent)
1335           gst_object_unref (peer_parent);
1337         gst_object_unref (peer);
1338       } else {
1339         GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
1340             "pad %s:%s does not have a peer", GST_DEBUG_PAD_NAME (pad));
1341       }
1342       pads = g_list_next (pads);
1343     }
1344     /* peel off the locked flag and release the element lock */
1345     locked = GST_FLAG_IS_SET (qelement, GST_ELEMENT_LOCKED_STATE);
1346     GST_UNLOCK (qelement);
1348     /* skip locked elements */
1349     if (G_UNLIKELY (locked))
1350       goto next_element;
1352     /* set base time on element */
1353     gst_element_set_base_time (qelement, base_time);
1355     /* then change state */
1356     ret = gst_element_set_state (qelement, pending);
1358     /* the set state could have cause elements to be added/removed,
1359      * we support that. */
1360     GST_LOCK (bin);
1361     if (G_UNLIKELY (children_cookie != bin->children_cookie)) {
1362       gst_object_unref (qelement);
1363       goto restart;
1364     }
1365     GST_UNLOCK (bin);
1367     switch (ret) {
1368       case GST_STATE_CHANGE_SUCCESS:
1369         GST_CAT_DEBUG (GST_CAT_STATES,
1370             "child '%s' changed state to %d(%s) successfully",
1371             GST_ELEMENT_NAME (qelement), pending,
1372             gst_element_state_get_name (pending));
1373         break;
1374       case GST_STATE_CHANGE_ASYNC:
1375         GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1376             "child '%s' is changing state asynchronously",
1377             GST_ELEMENT_NAME (qelement));
1378         have_async = TRUE;
1379         break;
1380       case GST_STATE_CHANGE_FAILURE:
1381         GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1382             "child '%s' failed to go to state %d(%s)",
1383             GST_ELEMENT_NAME (qelement),
1384             pending, gst_element_state_get_name (pending));
1385         ret = GST_STATE_CHANGE_FAILURE;
1386         /* release refcount of element we popped off the queue */
1387         gst_object_unref (qelement);
1388         goto exit;
1389       case GST_STATE_CHANGE_NO_PREROLL:
1390         GST_CAT_DEBUG (GST_CAT_STATES,
1391             "child '%s' changed state to %d(%s) successfully without preroll",
1392             GST_ELEMENT_NAME (qelement), pending,
1393             gst_element_state_get_name (pending));
1394         have_no_preroll = TRUE;
1395         break;
1396       default:
1397         g_assert_not_reached ();
1398         break;
1399     }
1400   next_element:
1401     gst_object_unref (qelement);
1403     /* if queue is empty now, continue with a non-sink */
1404     if (g_queue_is_empty (elem_queue)) {
1405       GstElement *non_sink;
1407       GST_DEBUG ("sinks and upstream elements exhausted");
1408       non_sink = g_queue_pop_head (semi_queue);
1409       if (non_sink) {
1410         GST_DEBUG ("found lefover semi-sink %s", GST_OBJECT_NAME (non_sink));
1411         g_queue_push_tail (elem_queue, non_sink);
1412       } else {
1413         non_sink = g_queue_pop_head (temp);
1414         if (non_sink) {
1415           GST_DEBUG ("found lefover non-sink %s", GST_OBJECT_NAME (non_sink));
1416           g_queue_push_tail (elem_queue, non_sink);
1417         }
1418       }
1419     }
1420   }
1422   if (have_no_preroll) {
1423     ret = GST_STATE_CHANGE_NO_PREROLL;
1424   } else if (have_async) {
1425     ret = GST_STATE_CHANGE_ASYNC;
1426   } else {
1427     ret = parent_class->change_state (element, transition);
1428   }
1430   GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
1431       "done changing bin's state from %s to %s, now in %s, ret %d",
1432       gst_element_state_get_name (old_state),
1433       gst_element_state_get_name (pending),
1434       gst_element_state_get_name (GST_STATE (element)), ret);
1436 exit:
1437   /* release refcounts in queue, should normally be empty unless we
1438    * had an error. */
1439   clear_queue (elem_queue, TRUE);
1440   clear_queue (semi_queue, TRUE);
1441   clear_queue (temp, TRUE);
1442   g_queue_free (elem_queue);
1443   g_queue_free (semi_queue);
1444   g_queue_free (temp);
1446   return ret;
1449 static void
1450 gst_bin_dispose (GObject * object)
1452   GstBin *bin = GST_BIN (object);
1454   GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, object, "dispose");
1456   /* ref to not hit 0 again */
1457   gst_object_ref (object);
1459   g_list_free (bin->eosed);
1460   bin->eosed = NULL;
1461   gst_object_unref (bin->child_bus);
1462   bin->child_bus = NULL;
1463   gst_element_set_bus (GST_ELEMENT (bin), NULL);
1465   while (bin->children) {
1466     gst_bin_remove (bin, GST_ELEMENT (bin->children->data));
1467   }
1468   GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, object, "dispose no children");
1469   g_assert (bin->children == NULL);
1470   g_assert (bin->numchildren == 0);
1472   G_OBJECT_CLASS (parent_class)->dispose (object);
1475 /*
1476  * This function is a utility event handler for seek events.
1477  * It will send the event to all sinks.
1478  * Applications are free to override this behaviour and
1479  * implement their own seek handler, but this will work for
1480  * pretty much all cases in practice.
1481  */
1482 static gboolean
1483 gst_bin_send_event (GstElement * element, GstEvent * event)
1485   GstBin *bin = GST_BIN (element);
1486   GstIterator *iter;
1487   gboolean res = TRUE;
1488   gboolean done = FALSE;
1490   iter = gst_bin_iterate_sinks (bin);
1491   GST_DEBUG_OBJECT (bin, "Sending event to sink children");
1493   while (!done) {
1494     gpointer data;
1496     switch (gst_iterator_next (iter, &data)) {
1497       case GST_ITERATOR_OK:
1498       {
1499         GstElement *sink;
1501         gst_event_ref (event);
1502         sink = GST_ELEMENT_CAST (data);
1503         res &= gst_element_send_event (sink, event);
1504         gst_object_unref (sink);
1505         break;
1506       }
1507       case GST_ITERATOR_RESYNC:
1508         gst_iterator_resync (iter);
1509         res = TRUE;
1510         break;
1511       default:
1512       case GST_ITERATOR_DONE:
1513         done = TRUE;
1514         break;
1515     }
1516   }
1517   gst_iterator_free (iter);
1518   gst_event_unref (event);
1520   return res;
1523 static GstBusSyncReply
1524 bin_bus_handler (GstBus * bus, GstMessage * message, GstBin * bin)
1526   GST_DEBUG_OBJECT (bin, "[msg %p] handling child message of type %d",
1527       message, GST_MESSAGE_TYPE (message));
1529   switch (GST_MESSAGE_TYPE (message)) {
1530     case GST_MESSAGE_EOS:{
1531       GstObject *src = GST_MESSAGE_SRC (message);
1533       if (src) {
1534         gchar *name;
1536         name = gst_object_get_name (src);
1537         GST_DEBUG_OBJECT (bin, "got EOS message from %s", name);
1538         g_free (name);
1540         /* collect all eos messages from the children */
1541         GST_LOCK (bin->child_bus);
1542         bin->eosed = g_list_prepend (bin->eosed, src);
1543         GST_UNLOCK (bin->child_bus);
1545         /* if we are completely EOS, we forward an EOS message */
1546         if (is_eos (bin)) {
1547           GST_DEBUG_OBJECT (bin, "all sinks posted EOS");
1548           gst_element_post_message (GST_ELEMENT (bin),
1549               gst_message_new_eos (GST_OBJECT (bin)));
1550         }
1551       } else {
1552         GST_DEBUG_OBJECT (bin, "got EOS message from (NULL), not processing");
1553       }
1555       /* we drop all EOS messages */
1556       gst_message_unref (message);
1557       break;
1558     }
1559     default:
1560       /* Send all other messages upward */
1561       GST_DEBUG_OBJECT (bin, "posting message upward");
1562       gst_element_post_message (GST_ELEMENT (bin), message);
1563       break;
1564   }
1566   return GST_BUS_DROP;
1569 static gboolean
1570 gst_bin_query (GstElement * element, GstQuery * query)
1572   GstBin *bin = GST_BIN (element);
1573   GstIterator *iter;
1574   gboolean res = FALSE, done = FALSE;
1576   iter = gst_bin_iterate_sinks (bin);
1577   GST_DEBUG_OBJECT (bin, "Sending event to sink children");
1579   while (!(res || done)) {
1580     gpointer data;
1582     switch (gst_iterator_next (iter, &data)) {
1583       case GST_ITERATOR_OK:
1584       {
1585         GstElement *sink;
1587         sink = GST_ELEMENT_CAST (data);
1588         res = gst_element_query (sink, query);
1589         gst_object_unref (sink);
1590         break;
1591       }
1592       case GST_ITERATOR_RESYNC:
1593         gst_iterator_resync (iter);
1594         break;
1595       default:
1596       case GST_ITERATOR_DONE:
1597         done = TRUE;
1598         break;
1599     }
1600   }
1601   gst_iterator_free (iter);
1603   return res;
1606 static gint
1607 compare_name (GstElement * element, const gchar * name)
1609   gint eq;
1611   GST_LOCK (element);
1612   eq = strcmp (GST_ELEMENT_NAME (element), name);
1613   GST_UNLOCK (element);
1615   if (eq != 0) {
1616     gst_object_unref (element);
1617   }
1618   return eq;
1621 /**
1622  * gst_bin_get_by_name:
1623  * @bin: #Gstbin to search
1624  * @name: the element name to search for
1625  *
1626  * Get the element with the given name from this bin. This
1627  * function recurses into subbins.
1628  *
1629  * MT safe.
1630  *
1631  * Returns: the element with the given name. Returns NULL if the
1632  * element is not found or when bad parameters were given. Unref after
1633  * use.
1634  */
1635 GstElement *
1636 gst_bin_get_by_name (GstBin * bin, const gchar * name)
1638   GstIterator *children;
1639   GstIterator *result;
1641   g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1643   GST_CAT_INFO (GST_CAT_PARENTAGE, "[%s]: looking up child element %s",
1644       GST_ELEMENT_NAME (bin), name);
1646   children = gst_bin_iterate_recurse (bin);
1647   result = gst_iterator_find_custom (children,
1648       (GCompareFunc) compare_name, (gpointer) name);
1649   gst_iterator_free (children);
1651   return GST_ELEMENT_CAST (result);
1654 /**
1655  * gst_bin_get_by_name_recurse_up:
1656  * @bin: #Gstbin to search
1657  * @name: the element name to search for
1658  *
1659  * MT safe.
1660  *
1661  * Get the element with the given name from this bin. If the
1662  * element is not found, a recursion is performed on the parent bin.
1663  *
1664  * Returns: the element with the given name or NULL when the element
1665  * was not found or bad parameters were given. Unref after use.
1666  */
1667 GstElement *
1668 gst_bin_get_by_name_recurse_up (GstBin * bin, const gchar * name)
1670   GstElement *result;
1672   g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1673   g_return_val_if_fail (name != NULL, NULL);
1675   result = gst_bin_get_by_name (bin, name);
1677   if (!result) {
1678     GstObject *parent;
1680     parent = gst_object_get_parent (GST_OBJECT_CAST (bin));
1681     if (parent) {
1682       if (GST_IS_BIN (parent)) {
1683         result = gst_bin_get_by_name_recurse_up (GST_BIN_CAST (parent), name);
1684       }
1685       gst_object_unref (parent);
1686     }
1687   }
1689   return result;
1692 static gint
1693 compare_interface (GstElement * element, gpointer interface)
1695   gint ret;
1697   if (G_TYPE_CHECK_INSTANCE_TYPE (element, GPOINTER_TO_INT (interface))) {
1698     ret = 0;
1699   } else {
1700     /* we did not find the element, need to release the ref
1701      * added by the iterator */
1702     gst_object_unref (element);
1703     ret = 1;
1704   }
1705   return ret;
1708 /**
1709  * gst_bin_get_by_interface:
1710  * @bin: bin to find element in
1711  * @interface: interface to be implemented by interface
1712  *
1713  * Looks for the first element inside the bin that implements the given
1714  * interface. If such an element is found, it returns the element. You can
1715  * cast this element to the given interface afterwards.
1716  * If you want all elements that implement the interface, use
1717  * gst_bin_iterate_all_by_interface(). The function recurses inside bins.
1718  *
1719  * MT safe.
1720  *
1721  * Returns: An #GstElement inside the bin implementing the interface.
1722  *          Unref after use.
1723  */
1724 GstElement *
1725 gst_bin_get_by_interface (GstBin * bin, GType interface)
1727   GstIterator *children;
1728   GstIterator *result;
1730   g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1732   children = gst_bin_iterate_recurse (bin);
1733   result = gst_iterator_find_custom (children, (GCompareFunc) compare_interface,
1734       GINT_TO_POINTER (interface));
1735   gst_iterator_free (children);
1737   return GST_ELEMENT_CAST (result);
1740 /**
1741  * gst_bin_iterate_all_by_interface:
1742  * @bin: bin to find elements in
1743  * @interface: interface to be implemented by interface
1744  *
1745  * Looks for all elements inside the bin that implements the given
1746  * interface. You can safely cast all returned elements to the given interface.
1747  * The function recurses bins inside bins. The iterator will return a series
1748  * of #GstElement that should be unreffed after use.
1749  *
1750  * MT safe.
1751  *
1752  * Returns: A #GstIterator for the elements inside the bin implementing the
1753  *          given interface.
1754  */
1755 GstIterator *
1756 gst_bin_iterate_all_by_interface (GstBin * bin, GType interface)
1758   GstIterator *children;
1759   GstIterator *result;
1761   g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1763   children = gst_bin_iterate_recurse (bin);
1764   result = gst_iterator_filter (children, (GCompareFunc) compare_interface,
1765       GINT_TO_POINTER (interface));
1767   return result;
1770 #ifndef GST_DISABLE_LOADSAVE
1771 static xmlNodePtr
1772 gst_bin_save_thyself (GstObject * object, xmlNodePtr parent)
1774   GstBin *bin = GST_BIN (object);
1775   xmlNodePtr childlist, elementnode;
1776   GList *children;
1777   GstElement *child;
1779   if (GST_OBJECT_CLASS (parent_class)->save_thyself)
1780     GST_OBJECT_CLASS (parent_class)->save_thyself (GST_OBJECT (bin), parent);
1782   childlist = xmlNewChild (parent, NULL, (xmlChar *) "children", NULL);
1784   GST_CAT_INFO (GST_CAT_XML, "[%s]: saving %d children",
1785       GST_ELEMENT_NAME (bin), bin->numchildren);
1787   children = bin->children;
1788   while (children) {
1789     child = GST_ELEMENT (children->data);
1790     elementnode = xmlNewChild (childlist, NULL, (xmlChar *) "element", NULL);
1791     gst_object_save_thyself (GST_OBJECT (child), elementnode);
1792     children = g_list_next (children);
1793   }
1794   return childlist;
1797 static void
1798 gst_bin_restore_thyself (GstObject * object, xmlNodePtr self)
1800   GstBin *bin = GST_BIN (object);
1801   xmlNodePtr field = self->xmlChildrenNode;
1802   xmlNodePtr childlist;
1804   while (field) {
1805     if (!strcmp ((char *) field->name, "children")) {
1806       GST_CAT_INFO (GST_CAT_XML, "[%s]: loading children",
1807           GST_ELEMENT_NAME (object));
1808       childlist = field->xmlChildrenNode;
1809       while (childlist) {
1810         if (!strcmp ((char *) childlist->name, "element")) {
1811           GstElement *element =
1812               gst_xml_make_element (childlist, GST_OBJECT (bin));
1814           /* it had to be parented to find the pads, now we ref and unparent so
1815            * we can add it to the bin */
1816           gst_object_ref (element);
1817           gst_object_unparent (GST_OBJECT (element));
1819           gst_bin_add (bin, element);
1820         }
1821         childlist = childlist->next;
1822       }
1823     }
1825     field = field->next;
1826   }
1827   if (GST_OBJECT_CLASS (parent_class)->restore_thyself)
1828     (GST_OBJECT_CLASS (parent_class)->restore_thyself) (object, self);
1830 #endif /* GST_DISABLE_LOADSAVE */