]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/gstreamer0-10.git/blob - gst/gstbin.c
4b0024894832ff8fb551c0d606f0e8b085bfaccc
[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 static gint
828 sink_iterator_filter (GstElement * child, GstBin * bin)
830   if (bin_element_is_sink (child, bin) == 0) {
831     /* returns 0 because this is a GCompareFunc */
832     return 0;
833   } else {
834     /* child carries a ref from gst_bin_iterate_elements -- drop if not passing
835        through */
836     gst_object_unref ((GstObject *) child);
837     return 1;
838   }
841 /**
842  * gst_bin_iterate_sinks:
843  * @bin: #Gstbin to iterate on
844  *
845  * Get an iterator for the sink elements in this bin.
846  * Each element will have its refcount increased, so unref
847  * after use.
848  *
849  * The sink elements are those without any linked srcpads.
850  *
851  * MT safe.
852  *
853  * Returns: a #GstIterator of #GstElements. gst_iterator_free after use.
854  */
855 GstIterator *
856 gst_bin_iterate_sinks (GstBin * bin)
858   GstIterator *children;
859   GstIterator *result;
861   g_return_val_if_fail (GST_IS_BIN (bin), NULL);
863   children = gst_bin_iterate_elements (bin);
864   result = gst_iterator_filter (children,
865       (GCompareFunc) sink_iterator_filter, bin);
867   return result;
870 /* 2 phases:
871  *  1) check state of all children with 0 timeout to find ERROR and
872  *     NO_PREROLL elements. return if found.
873  *  2) perform full blocking wait with requested timeout.
874  *
875  * 2) cannot be performed when 1) returns results as the sinks might
876  *    not be able to complete the state change making 2) block forever.
877  *
878  * MT safe
879  */
880 static GstStateChangeReturn
881 gst_bin_get_state (GstElement * element, GstState * state,
882     GstState * pending, GTimeVal * timeout)
884   GstBin *bin = GST_BIN (element);
885   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
886   GList *children;
887   guint32 children_cookie;
888   gboolean have_no_preroll;
890   GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "getting state");
892   /* lock bin, no element can be added or removed between going into
893    * the quick scan and the blocking wait. */
894   GST_LOCK (bin);
896 restart:
897   have_no_preroll = FALSE;
899   /* first we need to poll with a non zero timeout to make sure we don't block
900    * on the sinks when we have NO_PREROLL elements. This is why we do
901    * a quick check if there are still NO_PREROLL elements. We also
902    * catch the error elements this way. */
903   {
904     GTimeVal tv;
905     gboolean have_async = FALSE;
907     GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "checking for NO_PREROLL");
908     /* use 0 timeout so we don't block on the sinks */
909     GST_TIME_TO_TIMEVAL (0, tv);
910     children = bin->children;
911     children_cookie = bin->children_cookie;
912     while (children) {
913       GstElement *child = GST_ELEMENT_CAST (children->data);
915       gst_object_ref (child);
916       /* now we release the lock to enter a non blocking wait. We
917        * release the lock anyway since we can. */
918       GST_UNLOCK (bin);
920       ret = gst_element_get_state (child, NULL, NULL, &tv);
922       gst_object_unref (child);
924       /* now grab the lock to iterate to the next child */
925       GST_LOCK (bin);
926       if (G_UNLIKELY (children_cookie != bin->children_cookie)) {
927         /* child added/removed during state change, restart. We need
928          * to restart with the quick check as a no-preroll element could
929          * have been added here and we don't want to block on sinks then.*/
930         goto restart;
931       }
933       switch (ret) {
934           /* report FAILURE  immediatly */
935         case GST_STATE_CHANGE_FAILURE:
936           goto done;
937         case GST_STATE_CHANGE_NO_PREROLL:
938           /* we have to continue scanning as there might be
939            * ERRORS too */
940           have_no_preroll = TRUE;
941           break;
942         case GST_STATE_CHANGE_ASYNC:
943           have_async = TRUE;
944           break;
945         default:
946           break;
947       }
948       children = g_list_next (children);
949     }
950     /* if we get here, we have no FAILURES, check for any NO_PREROLL
951      * elements then. */
952     if (have_no_preroll) {
953       ret = GST_STATE_CHANGE_NO_PREROLL;
954       goto done;
955     }
957     /* if we get here, no NO_PREROLL elements are in the pipeline */
958     GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "no NO_PREROLL elements");
960     /* if no ASYNC elements exist we don't even have to poll with a
961      * timeout again */
962     if (!have_async) {
963       ret = GST_STATE_CHANGE_SUCCESS;
964       goto done;
965     }
966   }
968   /* next we poll all children for their state to see if one of them
969    * is still busy with its state change. We did not release the bin lock
970    * yet so the elements are the same as the ones from the quick scan. */
971   children = bin->children;
972   children_cookie = bin->children_cookie;
973   while (children) {
974     GstElement *child = GST_ELEMENT_CAST (children->data);
976     gst_object_ref (child);
977     /* now we release the lock to enter the potentialy blocking wait */
978     GST_UNLOCK (bin);
980     /* ret is ASYNC if some child is still performing the state change
981      * ater the timeout. */
982     ret = gst_element_get_state (child, NULL, NULL, timeout);
984     gst_object_unref (child);
986     /* now grab the lock to iterate to the next child */
987     GST_LOCK (bin);
988     if (G_UNLIKELY (children_cookie != bin->children_cookie)) {
989       /* child added/removed during state change, restart. We need
990        * to restart with the quick check as a no-preroll element could
991        * have been added here and we don't want to block on sinks then.*/
992       goto restart;
993     }
995     switch (ret) {
996       case GST_STATE_CHANGE_SUCCESS:
997         break;
998       case GST_STATE_CHANGE_FAILURE:
999       case GST_STATE_CHANGE_NO_PREROLL:
1000         /* report FAILURE and NO_PREROLL immediatly */
1001         goto done;
1002       case GST_STATE_CHANGE_ASYNC:
1003         goto done;
1004       default:
1005         g_assert_not_reached ();
1006     }
1007     children = g_list_next (children);
1008   }
1009   /* if we got here, all elements can do preroll */
1010   have_no_preroll = FALSE;
1012 done:
1013   GST_UNLOCK (bin);
1015   /* now we can take the state lock, it is possible that new elements
1016    * are added now and we still report the old state. No problem though as
1017    * the return is still consistent, the effect is as if the element was
1018    * added after this function completed. */
1019   GST_STATE_LOCK (bin);
1020   switch (ret) {
1021     case GST_STATE_CHANGE_SUCCESS:
1022       /* we can commit the state */
1023       gst_element_commit_state (element);
1024       break;
1025     case GST_STATE_CHANGE_FAILURE:
1026       /* some element failed, abort the state change */
1027       gst_element_abort_state (element);
1028       break;
1029     default:
1030       /* other cases are just passed along */
1031       break;
1032   }
1034   /* and report the state if needed */
1035   if (state)
1036     *state = GST_STATE (element);
1037   if (pending)
1038     *pending = GST_STATE_PENDING (element);
1040   GST_STATE_NO_PREROLL (element) = have_no_preroll;
1042   GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1043       "state current: %s, pending: %s, error: %d, no_preroll: %d, result: %d",
1044       gst_element_state_get_name (GST_STATE (element)),
1045       gst_element_state_get_name (GST_STATE_PENDING (element)),
1046       GST_STATE_ERROR (element), GST_STATE_NO_PREROLL (element), ret);
1048   GST_STATE_UNLOCK (bin);
1050   return ret;
1053 /***********************************************
1054  * Topologically sorted iterator 
1055  * see http://en.wikipedia.org/wiki/Topological_sorting
1056  */
1057 typedef struct _GstBinSortIterator
1059   GstIterator it;
1060   GQueue *queue;                /* elements queued for state change */
1061   GstBin *bin;                  /* bin we iterate */
1062   gint mode;                    /* adding or removing dependency */
1063   GstElement *best;             /* next element with least dependencies */
1064   gint best_deg;                /* best degree */
1065   GHashTable *hash;             /* has table with element dependencies */
1066 } GstBinSortIterator;
1068 /* we add and subtract 1 to make sure we don't confuse NULL and 0 */
1069 #define HASH_SET_DEGREE(bit, elem, deg) \
1070     g_hash_table_replace (bit->hash, elem, GINT_TO_POINTER(deg+1))
1071 #define HASH_GET_DEGREE(bit, elem) \
1072     (GPOINTER_TO_INT(g_hash_table_lookup (bit->hash, elem))-1)
1074 /* add element to queue of next elements in the iterator.
1075  * We push at the tail to give higher priority elements a
1076  * chance first */
1077 static void
1078 add_to_queue (GstBinSortIterator * bit, GstElement * element)
1080   GST_DEBUG ("%s add to queue", GST_ELEMENT_NAME (element));
1081   gst_object_ref (element);
1082   g_queue_push_tail (bit->queue, element);
1083   HASH_SET_DEGREE (bit, element, -1);
1086 /* clear the queue, unref all objects as we took a ref when
1087  * we added them to the queue */
1088 static void
1089 clear_queue (GQueue * queue)
1091   gpointer p;
1093   while ((p = g_queue_pop_head (queue)))
1094     gst_object_unref (p);
1097 /* set all outdegrees to 0. Elements marked as a sink are
1098  * added to the queue immediatly. */
1099 static void
1100 reset_outdegree (GstElement * element, GstBinSortIterator * bit)
1102   /* sinks are added right away */
1103   if (GST_FLAG_IS_SET (element, GST_ELEMENT_IS_SINK)) {
1104     add_to_queue (bit, element);
1105   } else {
1106     /* others are marked with 0 and handled when sinks are done */
1107     HASH_SET_DEGREE (bit, element, 0);
1108   }
1111 /* adjust the outdegree of all elements connected to the given
1112  * element. If an outdegree of an element drops to 0, it is
1113  * added to the queue of elements to schedule next.
1114  *
1115  * We have to make sure not to cross the bin boundary this element
1116  * belongs to.
1117  */
1118 static void
1119 update_outdegree (GstElement * element, GstBinSortIterator * bit)
1121   gboolean linked = FALSE;
1123   GST_LOCK (element);
1124   /* don't touch outdegree is element has no sourcepads */
1125   if (element->numsinkpads != 0) {
1126     /* loop over all sinkpads, decrement outdegree for all connected
1127      * elements in this bin */
1128     GList *pads;
1130     for (pads = element->sinkpads; pads; pads = g_list_next (pads)) {
1131       GstPad *peer;
1133       if ((peer = gst_pad_get_peer (GST_PAD_CAST (pads->data)))) {
1134         GstElement *peer_element;
1136         if ((peer_element = gst_pad_get_parent_element (peer))) {
1137           GST_LOCK (peer_element);
1138           if (GST_OBJECT_CAST (peer_element)->parent ==
1139               GST_OBJECT_CAST (bit->bin)) {
1140             gint old_deg, new_deg;
1142             old_deg = HASH_GET_DEGREE (bit, peer_element);
1143             new_deg = old_deg + bit->mode;
1145             GST_DEBUG ("change element %s, degree %d->%d, linked to %s",
1146                 GST_ELEMENT_NAME (peer_element),
1147                 old_deg, new_deg, GST_ELEMENT_NAME (element));
1149             /* update outdegree */
1150             if (new_deg == 0) {
1151               /* outdegree hit 0, add to queue */
1152               add_to_queue (bit, peer_element);
1153             } else {
1154               HASH_SET_DEGREE (bit, peer_element, new_deg);
1155             }
1156             linked = TRUE;
1157           }
1158           GST_UNLOCK (peer_element);
1159           gst_object_unref (peer_element);
1160         }
1161         gst_object_unref (peer);
1162       }
1163     }
1164   }
1165   if (!linked) {
1166     GST_DEBUG ("element %s not linked to anything", GST_ELEMENT_NAME (element));
1167   }
1168   GST_UNLOCK (element);
1171 /* find the next best element not handled yet. This is the one
1172  * with the lowest non-negative outdegree */
1173 static void
1174 find_element (GstElement * element, GstBinSortIterator * bit)
1176   gint outdegree;
1178   /* element is already handled */
1179   if ((outdegree = HASH_GET_DEGREE (bit, element)) < 0)
1180     return;
1182   /* first element or element with smaller outdegree */
1183   if (bit->best == NULL || bit->best_deg > outdegree) {
1184     bit->best = element;
1185     bit->best_deg = outdegree;
1186   }
1189 /* get next element in iterator. the returned element has the
1190  * refcount increased */
1191 static GstIteratorResult
1192 gst_bin_sort_iterator_next (GstBinSortIterator * bit, gpointer * result)
1194   /* empty queue, we have to find a next best element */
1195   if (g_queue_is_empty (bit->queue)) {
1196     bit->best = NULL;
1197     bit->best_deg = G_MAXINT;
1198     g_list_foreach (bit->bin->children, (GFunc) find_element, bit);
1199     if (bit->best) {
1200       if (bit->best_deg != 0) {
1201         /* we don't fail on this one yet */
1202         g_warning ("loop detected in the graph !!");
1203       }
1204       /* best unhandled elements, scheduler as next element */
1205       GST_DEBUG ("queue empty, next best: %s", GST_ELEMENT_NAME (bit->best));
1206       gst_object_ref (bit->best);
1207       HASH_SET_DEGREE (bit, bit->best, -1);
1208       *result = bit->best;
1209     } else {
1210       GST_DEBUG ("queue empty, elements exhausted");
1211       /* no more unhandled elements, we are done */
1212       return GST_ITERATOR_DONE;
1213     }
1214   } else {
1215     /* everything added to the queue got reffed */
1216     *result = g_queue_pop_head (bit->queue);
1217   }
1219   GST_DEBUG ("queue head gives %s", GST_ELEMENT_NAME (*result));
1220   /* update outdegrees of linked elements */
1221   update_outdegree (GST_ELEMENT_CAST (*result), bit);
1223   return GST_ITERATOR_OK;
1226 /* clear queues, recalculate the outdegrees and restart. */
1227 static void
1228 gst_bin_sort_iterator_resync (GstBinSortIterator * bit)
1230   clear_queue (bit->queue);
1231   /* reset outdegrees */
1232   g_list_foreach (bit->bin->children, (GFunc) reset_outdegree, bit);
1233   /* calc outdegrees, incrementing */
1234   bit->mode = 1;
1235   g_list_foreach (bit->bin->children, (GFunc) update_outdegree, bit);
1236   /* for the rest of the function we decrement the outdegrees */
1237   bit->mode = -1;
1240 /* clear queues, unref bin and free iterator. */
1241 static void
1242 gst_bin_sort_iterator_free (GstBinSortIterator * bit)
1244   clear_queue (bit->queue);
1245   g_queue_free (bit->queue);
1246   g_hash_table_destroy (bit->hash);
1247   gst_object_unref (bit->bin);
1248   g_free (bit);
1251 /**
1252  * gst_bin_iterate_sorted:
1253  * @bin: #Gstbin to iterate on
1254  *
1255  * Get an iterator for the elements in this bin in topologically
1256  * sorted order. This means that the elements are returned from
1257  * the most downstream elements (sinks) to the sources.
1258  *
1259  * This function is used internally to perform the state changes 
1260  * of the bin elements.
1261  *
1262  * Each element will have its refcount increased, so unref
1263  * after use.
1264  *
1265  * MT safe. 
1266  *
1267  * Returns: a #GstIterator of #GstElements. gst_iterator_free after use.
1268  */
1269 GstIterator *
1270 gst_bin_iterate_sorted (GstBin * bin)
1272   GstBinSortIterator *result;
1274   g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1276   GST_LOCK (bin);
1277   gst_object_ref (bin);
1278   /* we don't need a NextFunction because we ref the items in the _next
1279    * method already */
1280   result = (GstBinSortIterator *)
1281       gst_iterator_new (sizeof (GstBinSortIterator),
1282       GST_GET_LOCK (bin),
1283       &bin->children_cookie,
1284       (GstIteratorNextFunction) gst_bin_sort_iterator_next,
1285       (GstIteratorItemFunction) NULL,
1286       (GstIteratorResyncFunction) gst_bin_sort_iterator_resync,
1287       (GstIteratorFreeFunction) gst_bin_sort_iterator_free);
1288   result->queue = g_queue_new ();
1289   result->hash = g_hash_table_new (NULL, NULL);
1290   result->bin = bin;
1291   gst_bin_sort_iterator_resync (result);
1292   GST_UNLOCK (bin);
1294   return (GstIterator *) result;
1297 static GstStateChangeReturn
1298 gst_bin_element_set_state (GstBin * bin, GstElement * element, GstState pending)
1300   GstStateChangeReturn ret;
1301   gboolean locked;
1303   /* peel off the locked flag */
1304   GST_LOCK (element);
1305   locked = GST_FLAG_IS_SET (element, GST_ELEMENT_LOCKED_STATE);
1306   GST_UNLOCK (element);
1308   /* skip locked elements */
1309   if (G_UNLIKELY (locked)) {
1310     ret = GST_STATE_CHANGE_SUCCESS;
1311     goto done;
1312   }
1314   /* change state */
1315   ret = gst_element_set_state (element, pending);
1317 done:
1318   return ret;
1321 static GstStateChangeReturn
1322 gst_bin_change_state (GstElement * element, GstStateChange transition)
1324   GstBin *bin;
1325   GstStateChangeReturn ret;
1326   GstState old_state, pending;
1327   gboolean have_async;
1328   gboolean have_no_preroll;
1329   GstClockTime base_time;
1330   GstIterator *it;
1331   gboolean done;
1333   /* we don't need to take the STATE_LOCK, it is already taken */
1334   old_state = GST_STATE (element);
1335   pending = GST_STATE_PENDING (element);
1337   GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
1338       "changing state of children from %s to %s",
1339       gst_element_state_get_name (old_state),
1340       gst_element_state_get_name (pending));
1342   if (pending == GST_STATE_VOID_PENDING)
1343     return GST_STATE_CHANGE_SUCCESS;
1345   bin = GST_BIN_CAST (element);
1347   /* Clear eosed element list on READY-> PAUSED */
1348   if (transition == GST_STATE_CHANGE_READY_TO_PAUSED) {
1349     g_list_free (bin->eosed);
1350     bin->eosed = NULL;
1351   }
1353   /* iterate in state change order */
1354   it = gst_bin_iterate_sorted (bin);
1356 restart:
1357   /* take base time */
1358   base_time = element->base_time;
1360   have_async = FALSE;
1361   have_no_preroll = FALSE;
1363   done = FALSE;
1364   while (!done) {
1365     gpointer data;
1367     switch (gst_iterator_next (it, &data)) {
1368       case GST_ITERATOR_OK:
1369       {
1370         GstElement *element;
1372         element = GST_ELEMENT_CAST (data);
1374         /* set base time on element */
1375         gst_element_set_base_time (element, base_time);
1377         /* set state now */
1378         ret = gst_bin_element_set_state (bin, element, pending);
1380         switch (ret) {
1381           case GST_STATE_CHANGE_SUCCESS:
1382             GST_CAT_DEBUG (GST_CAT_STATES,
1383                 "child '%s' changed state to %d(%s) successfully",
1384                 GST_ELEMENT_NAME (element), pending,
1385                 gst_element_state_get_name (pending));
1386             break;
1387           case GST_STATE_CHANGE_ASYNC:
1388             GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1389                 "child '%s' is changing state asynchronously",
1390                 GST_ELEMENT_NAME (element));
1391             have_async = TRUE;
1392             break;
1393           case GST_STATE_CHANGE_FAILURE:
1394             GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1395                 "child '%s' failed to go to state %d(%s)",
1396                 GST_ELEMENT_NAME (element),
1397                 pending, gst_element_state_get_name (pending));
1398             gst_object_unref (element);
1399             goto done;
1400           case GST_STATE_CHANGE_NO_PREROLL:
1401             GST_CAT_DEBUG (GST_CAT_STATES,
1402                 "child '%s' changed state to %d(%s) successfully without preroll",
1403                 GST_ELEMENT_NAME (element), pending,
1404                 gst_element_state_get_name (pending));
1405             have_no_preroll = TRUE;
1406             break;
1407           default:
1408             g_assert_not_reached ();
1409             break;
1410         }
1411         gst_object_unref (element);
1412         break;
1413       }
1414       case GST_ITERATOR_RESYNC:
1415         gst_iterator_resync (it);
1416         goto restart;
1417         break;
1418       default:
1419       case GST_ITERATOR_DONE:
1420         done = TRUE;
1421         break;
1422     }
1423   }
1425   if (have_no_preroll) {
1426     ret = GST_STATE_CHANGE_NO_PREROLL;
1427   } else if (have_async) {
1428     ret = GST_STATE_CHANGE_ASYNC;
1429   } else {
1430     ret = parent_class->change_state (element, transition);
1431   }
1433 done:
1434   GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
1435       "done changing bin's state from %s to %s, now in %s, ret %d",
1436       gst_element_state_get_name (old_state),
1437       gst_element_state_get_name (pending),
1438       gst_element_state_get_name (GST_STATE (element)), ret);
1440   gst_iterator_free (it);
1442   return ret;
1445 static void
1446 gst_bin_dispose (GObject * object)
1448   GstBin *bin = GST_BIN (object);
1450   GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, object, "dispose");
1452   g_list_free (bin->eosed);
1453   bin->eosed = NULL;
1454   gst_object_unref (bin->child_bus);
1455   bin->child_bus = NULL;
1456   gst_element_set_bus (GST_ELEMENT (bin), NULL);
1458   while (bin->children) {
1459     gst_bin_remove (bin, GST_ELEMENT (bin->children->data));
1460   }
1461   GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, object, "dispose no children");
1462   g_assert (bin->children == NULL);
1463   g_assert (bin->numchildren == 0);
1465   G_OBJECT_CLASS (parent_class)->dispose (object);
1468 /*
1469  * This function is a utility event handler for seek events.
1470  * It will send the event to all sinks.
1471  * Applications are free to override this behaviour and
1472  * implement their own seek handler, but this will work for
1473  * pretty much all cases in practice.
1474  */
1475 static gboolean
1476 gst_bin_send_event (GstElement * element, GstEvent * event)
1478   GstBin *bin = GST_BIN (element);
1479   GstIterator *iter;
1480   gboolean res = TRUE;
1481   gboolean done = FALSE;
1483   iter = gst_bin_iterate_sinks (bin);
1484   GST_DEBUG_OBJECT (bin, "Sending event to sink children");
1486   while (!done) {
1487     gpointer data;
1489     switch (gst_iterator_next (iter, &data)) {
1490       case GST_ITERATOR_OK:
1491       {
1492         GstElement *sink;
1494         gst_event_ref (event);
1495         sink = GST_ELEMENT_CAST (data);
1496         res &= gst_element_send_event (sink, event);
1497         gst_object_unref (sink);
1498         break;
1499       }
1500       case GST_ITERATOR_RESYNC:
1501         gst_iterator_resync (iter);
1502         res = TRUE;
1503         break;
1504       default:
1505       case GST_ITERATOR_DONE:
1506         done = TRUE;
1507         break;
1508     }
1509   }
1510   gst_iterator_free (iter);
1511   gst_event_unref (event);
1513   return res;
1516 static GstBusSyncReply
1517 bin_bus_handler (GstBus * bus, GstMessage * message, GstBin * bin)
1519   GST_DEBUG_OBJECT (bin, "[msg %p] handling child message of type %d",
1520       message, GST_MESSAGE_TYPE (message));
1522   switch (GST_MESSAGE_TYPE (message)) {
1523     case GST_MESSAGE_EOS:{
1524       GstObject *src = GST_MESSAGE_SRC (message);
1526       if (src) {
1527         gchar *name;
1529         name = gst_object_get_name (src);
1530         GST_DEBUG_OBJECT (bin, "got EOS message from %s", name);
1531         g_free (name);
1533         /* collect all eos messages from the children */
1534         GST_LOCK (bin->child_bus);
1535         bin->eosed = g_list_prepend (bin->eosed, src);
1536         GST_UNLOCK (bin->child_bus);
1538         /* if we are completely EOS, we forward an EOS message */
1539         if (is_eos (bin)) {
1540           GST_DEBUG_OBJECT (bin, "all sinks posted EOS");
1541           gst_element_post_message (GST_ELEMENT (bin),
1542               gst_message_new_eos (GST_OBJECT (bin)));
1543         }
1544       } else {
1545         GST_DEBUG_OBJECT (bin, "got EOS message from (NULL), not processing");
1546       }
1548       /* we drop all EOS messages */
1549       gst_message_unref (message);
1550       break;
1551     }
1552     default:
1553       /* Send all other messages upward */
1554       GST_DEBUG_OBJECT (bin, "posting message upward");
1555       gst_element_post_message (GST_ELEMENT (bin), message);
1556       break;
1557   }
1559   return GST_BUS_DROP;
1562 static gboolean
1563 gst_bin_query (GstElement * element, GstQuery * query)
1565   GstBin *bin = GST_BIN (element);
1566   GstIterator *iter;
1567   gboolean res = FALSE, done = FALSE;
1569   iter = gst_bin_iterate_sinks (bin);
1570   GST_DEBUG_OBJECT (bin, "Sending event to sink children");
1572   while (!(res || done)) {
1573     gpointer data;
1575     switch (gst_iterator_next (iter, &data)) {
1576       case GST_ITERATOR_OK:
1577       {
1578         GstElement *sink;
1580         sink = GST_ELEMENT_CAST (data);
1581         res = gst_element_query (sink, query);
1582         gst_object_unref (sink);
1583         break;
1584       }
1585       case GST_ITERATOR_RESYNC:
1586         gst_iterator_resync (iter);
1587         break;
1588       default:
1589       case GST_ITERATOR_DONE:
1590         done = TRUE;
1591         break;
1592     }
1593   }
1594   gst_iterator_free (iter);
1596   return res;
1599 static gint
1600 compare_name (GstElement * element, const gchar * name)
1602   gint eq;
1604   GST_LOCK (element);
1605   eq = strcmp (GST_ELEMENT_NAME (element), name);
1606   GST_UNLOCK (element);
1608   if (eq != 0) {
1609     gst_object_unref (element);
1610   }
1611   return eq;
1614 /**
1615  * gst_bin_get_by_name:
1616  * @bin: #Gstbin to search
1617  * @name: the element name to search for
1618  *
1619  * Get the element with the given name from this bin. This
1620  * function recurses into subbins.
1621  *
1622  * MT safe.
1623  *
1624  * Returns: the element with the given name. Returns NULL if the
1625  * element is not found or when bad parameters were given. Unref after
1626  * use.
1627  */
1628 GstElement *
1629 gst_bin_get_by_name (GstBin * bin, const gchar * name)
1631   GstIterator *children;
1632   GstIterator *result;
1634   g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1636   GST_CAT_INFO (GST_CAT_PARENTAGE, "[%s]: looking up child element %s",
1637       GST_ELEMENT_NAME (bin), name);
1639   children = gst_bin_iterate_recurse (bin);
1640   result = gst_iterator_find_custom (children,
1641       (GCompareFunc) compare_name, (gpointer) name);
1642   gst_iterator_free (children);
1644   return GST_ELEMENT_CAST (result);
1647 /**
1648  * gst_bin_get_by_name_recurse_up:
1649  * @bin: #Gstbin to search
1650  * @name: the element name to search for
1651  *
1652  * MT safe.
1653  *
1654  * Get the element with the given name from this bin. If the
1655  * element is not found, a recursion is performed on the parent bin.
1656  *
1657  * Returns: the element with the given name or NULL when the element
1658  * was not found or bad parameters were given. Unref after use.
1659  */
1660 GstElement *
1661 gst_bin_get_by_name_recurse_up (GstBin * bin, const gchar * name)
1663   GstElement *result;
1665   g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1666   g_return_val_if_fail (name != NULL, NULL);
1668   result = gst_bin_get_by_name (bin, name);
1670   if (!result) {
1671     GstObject *parent;
1673     parent = gst_object_get_parent (GST_OBJECT_CAST (bin));
1674     if (parent) {
1675       if (GST_IS_BIN (parent)) {
1676         result = gst_bin_get_by_name_recurse_up (GST_BIN_CAST (parent), name);
1677       }
1678       gst_object_unref (parent);
1679     }
1680   }
1682   return result;
1685 static gint
1686 compare_interface (GstElement * element, gpointer interface)
1688   gint ret;
1690   if (G_TYPE_CHECK_INSTANCE_TYPE (element, GPOINTER_TO_INT (interface))) {
1691     ret = 0;
1692   } else {
1693     /* we did not find the element, need to release the ref
1694      * added by the iterator */
1695     gst_object_unref (element);
1696     ret = 1;
1697   }
1698   return ret;
1701 /**
1702  * gst_bin_get_by_interface:
1703  * @bin: bin to find element in
1704  * @interface: interface to be implemented by interface
1705  *
1706  * Looks for the first element inside the bin that implements the given
1707  * interface. If such an element is found, it returns the element. You can
1708  * cast this element to the given interface afterwards.
1709  * If you want all elements that implement the interface, use
1710  * gst_bin_iterate_all_by_interface(). The function recurses inside bins.
1711  *
1712  * MT safe.
1713  *
1714  * Returns: An #GstElement inside the bin implementing the interface.
1715  *          Unref after use.
1716  */
1717 GstElement *
1718 gst_bin_get_by_interface (GstBin * bin, GType interface)
1720   GstIterator *children;
1721   GstIterator *result;
1723   g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1725   children = gst_bin_iterate_recurse (bin);
1726   result = gst_iterator_find_custom (children, (GCompareFunc) compare_interface,
1727       GINT_TO_POINTER (interface));
1728   gst_iterator_free (children);
1730   return GST_ELEMENT_CAST (result);
1733 /**
1734  * gst_bin_iterate_all_by_interface:
1735  * @bin: bin to find elements in
1736  * @interface: interface to be implemented by interface
1737  *
1738  * Looks for all elements inside the bin that implements the given
1739  * interface. You can safely cast all returned elements to the given interface.
1740  * The function recurses bins inside bins. The iterator will return a series
1741  * of #GstElement that should be unreffed after use.
1742  *
1743  * MT safe.
1744  *
1745  * Returns: A #GstIterator for the elements inside the bin implementing the
1746  *          given interface.
1747  */
1748 GstIterator *
1749 gst_bin_iterate_all_by_interface (GstBin * bin, GType interface)
1751   GstIterator *children;
1752   GstIterator *result;
1754   g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1756   children = gst_bin_iterate_recurse (bin);
1757   result = gst_iterator_filter (children, (GCompareFunc) compare_interface,
1758       GINT_TO_POINTER (interface));
1760   return result;
1763 #ifndef GST_DISABLE_LOADSAVE
1764 static xmlNodePtr
1765 gst_bin_save_thyself (GstObject * object, xmlNodePtr parent)
1767   GstBin *bin = GST_BIN (object);
1768   xmlNodePtr childlist, elementnode;
1769   GList *children;
1770   GstElement *child;
1772   if (GST_OBJECT_CLASS (parent_class)->save_thyself)
1773     GST_OBJECT_CLASS (parent_class)->save_thyself (GST_OBJECT (bin), parent);
1775   childlist = xmlNewChild (parent, NULL, (xmlChar *) "children", NULL);
1777   GST_CAT_INFO (GST_CAT_XML, "[%s]: saving %d children",
1778       GST_ELEMENT_NAME (bin), bin->numchildren);
1780   children = bin->children;
1781   while (children) {
1782     child = GST_ELEMENT (children->data);
1783     elementnode = xmlNewChild (childlist, NULL, (xmlChar *) "element", NULL);
1784     gst_object_save_thyself (GST_OBJECT (child), elementnode);
1785     children = g_list_next (children);
1786   }
1787   return childlist;
1790 static void
1791 gst_bin_restore_thyself (GstObject * object, xmlNodePtr self)
1793   GstBin *bin = GST_BIN (object);
1794   xmlNodePtr field = self->xmlChildrenNode;
1795   xmlNodePtr childlist;
1797   while (field) {
1798     if (!strcmp ((char *) field->name, "children")) {
1799       GST_CAT_INFO (GST_CAT_XML, "[%s]: loading children",
1800           GST_ELEMENT_NAME (object));
1801       childlist = field->xmlChildrenNode;
1802       while (childlist) {
1803         if (!strcmp ((char *) childlist->name, "element")) {
1804           GstElement *element =
1805               gst_xml_make_element (childlist, GST_OBJECT (bin));
1807           /* it had to be parented to find the pads, now we ref and unparent so
1808            * we can add it to the bin */
1809           gst_object_ref (element);
1810           gst_object_unparent (GST_OBJECT (element));
1812           gst_bin_add (bin, element);
1813         }
1814         childlist = childlist->next;
1815       }
1816     }
1818     field = field->next;
1819   }
1820   if (GST_OBJECT_CLASS (parent_class)->restore_thyself)
1821     (GST_OBJECT_CLASS (parent_class)->restore_thyself) (object, self);
1823 #endif /* GST_DISABLE_LOADSAVE */