s/gst_element_install_std_props/gst_element_class_install_std_props/ -- it just makes...
[platform/upstream/gstreamer.git] / gst / gstbin.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstbin.c: GstBin container object and support code
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /* #define GST_DEBUG_ENABLED */
24 #include "gst_private.h"
25
26 #include "gstevent.h"
27 #include "gstbin.h"
28 #include "gstxml.h"
29 #include "gstsystemclock.h"
30
31 #include "gstscheduler.h"
32
33 GstElementDetails gst_bin_details = {
34   "Generic bin",
35   "Bin",
36   "Simple container object",
37   VERSION,
38   "Erik Walthinsen <omega@cse.ogi.edu>",
39   "(C) 1999",
40 };
41
42 GType _gst_bin_type = 0;
43
44 static void                     gst_bin_dispose                 (GObject * object);
45
46 static GstElementStateReturn    gst_bin_change_state            (GstElement *element);
47 static GstElementStateReturn    gst_bin_change_state_norecurse  (GstBin *bin);
48 static gboolean                 gst_bin_change_state_type       (GstBin *bin,
49                                                                  GstElementState state,
50                                                                  GType type);
51
52 static gboolean                 gst_bin_iterate_func            (GstBin * bin);
53
54 #ifndef GST_DISABLE_LOADSAVE
55 static xmlNodePtr               gst_bin_save_thyself            (GstObject * object, xmlNodePtr parent);
56 static void                     gst_bin_restore_thyself         (GstObject * object, xmlNodePtr self);
57 #endif
58
59 /* Bin signals and args */
60 enum
61 {
62   OBJECT_ADDED,
63   LAST_SIGNAL
64 };
65
66 enum
67 {
68   ARG_0,
69   /* FILL ME */
70 };
71
72 static void                     gst_bin_class_init              (GstBinClass * klass);
73 static void                     gst_bin_init                    (GstBin * bin);
74
75 static GstElementClass *parent_class = NULL;
76 static guint gst_bin_signals[LAST_SIGNAL] = { 0 };
77
78 GType
79 gst_bin_get_type (void)
80 {
81   if (!_gst_bin_type) {
82     static const GTypeInfo bin_info = {
83       sizeof (GstBinClass),
84       NULL,
85       NULL,
86       (GClassInitFunc) gst_bin_class_init,
87       NULL,
88       NULL,
89       sizeof (GstBin),
90       8,
91       (GInstanceInitFunc) gst_bin_init,
92       NULL
93     };
94
95     _gst_bin_type = g_type_register_static (GST_TYPE_ELEMENT, "GstBin", &bin_info, 0);
96   }
97   return _gst_bin_type;
98 }
99
100 static void
101 gst_bin_class_init (GstBinClass * klass)
102 {
103   GObjectClass *gobject_class;
104   GstObjectClass *gstobject_class;
105   GstElementClass *gstelement_class;
106
107   gobject_class = (GObjectClass *) klass;
108   gstobject_class = (GstObjectClass *) klass;
109   gstelement_class = (GstElementClass *) klass;
110
111   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
112
113   gst_bin_signals[OBJECT_ADDED] =
114     g_signal_new ("object_added", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
115                   G_STRUCT_OFFSET (GstBinClass, object_added), NULL, NULL,
116                   gst_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER);
117
118   gobject_class->dispose                = GST_DEBUG_FUNCPTR (gst_bin_dispose);
119
120 #ifndef GST_DISABLE_LOADSAVE
121   gstobject_class->save_thyself         = GST_DEBUG_FUNCPTR (gst_bin_save_thyself);
122   gstobject_class->restore_thyself      = GST_DEBUG_FUNCPTR (gst_bin_restore_thyself);
123 #endif
124
125   gstelement_class->change_state        = GST_DEBUG_FUNCPTR (gst_bin_change_state);
126
127   klass->change_state_type              = GST_DEBUG_FUNCPTR (gst_bin_change_state_type);
128   klass->iterate                        = GST_DEBUG_FUNCPTR (gst_bin_iterate_func);
129 }
130
131 static void
132 gst_bin_init (GstBin * bin)
133 {
134   /* in general, we prefer to use cothreads for most things */
135   GST_FLAG_SET (bin, GST_BIN_FLAG_PREFER_COTHREADS);
136
137   bin->numchildren = 0;
138   bin->children = NULL;
139 }
140
141 /**
142  * gst_bin_new:
143  * @name: name of new bin
144  *
145  * Create a new bin with given name.
146  *
147  * Returns: new bin
148  */
149 GstElement *
150 gst_bin_new (const gchar * name)
151 {
152   return gst_elementfactory_make ("bin", name);
153 }
154
155 static inline void
156 gst_bin_reset_element_sched (GstElement * element, GstScheduler * sched)
157 {
158   GST_INFO_ELEMENT (GST_CAT_PARENTAGE, element, "resetting element's scheduler");
159
160   gst_element_set_sched (element, sched);
161 }
162
163
164 static void
165 gst_bin_get_clock_elements (GstBin *bin, GList **needing, GList **providing) 
166 {
167   GList *children = gst_bin_get_list (bin);
168
169   while (children) {
170     GstElement *child = GST_ELEMENT (children->data);
171
172     if (GST_IS_BIN (child)) {
173       gst_bin_get_clock_elements (GST_BIN (child), needing, providing);
174     }
175     if (child->getclockfunc) {
176       *providing = g_list_prepend (*providing, child);
177     }
178     if (child->setclockfunc) {
179       *needing = g_list_prepend (*needing, child);
180     }
181         
182     children = g_list_next (children);
183   }
184 }
185
186 static void
187 gst_bin_distribute_clock (GstBin *bin, GList *needing, GstClock *clock)
188 {
189   while (needing) {
190     GST_DEBUG (GST_CAT_CLOCK, "setting clock on %s\n", GST_ELEMENT_NAME (needing->data));
191     gst_element_set_clock (GST_ELEMENT (needing->data), clock);
192
193     needing = g_list_next (needing);
194   }
195 }
196
197 static void
198 gst_bin_distribute_clocks (GstBin *bin)
199 {
200   GList *needing = NULL, *providing = NULL;
201   GstClock *clock;
202       
203   gst_bin_get_clock_elements (bin, &needing, &providing);
204
205   if (GST_FLAG_IS_SET (bin, GST_BIN_FLAG_FIXED_CLOCK)) {
206     clock = bin->clock;
207   }
208   else if (providing) {
209     clock = gst_element_get_clock (GST_ELEMENT (providing->data));      
210   }
211   else {
212     GST_DEBUG (GST_CAT_CLOCK, "no clock provided, using default clock\n");
213     clock = gst_system_clock_obtain ();
214   }
215
216   GST_BIN_CLOCK (bin) = clock;
217   gst_bin_distribute_clock (bin, needing, clock);
218 }
219
220 GstClock*
221 gst_bin_get_clock (GstBin *bin)
222 {
223   g_return_val_if_fail (bin != NULL, NULL);
224   g_return_val_if_fail (GST_IS_BIN (bin), NULL);
225
226   return GST_BIN_CLOCK (bin);
227 }
228
229 void
230 gst_bin_use_clock (GstBin *bin, GstClock *clock)
231 {
232   g_return_if_fail (bin != NULL);
233   g_return_if_fail (GST_IS_BIN (bin));
234
235   GST_FLAG_SET (bin, GST_BIN_FLAG_FIXED_CLOCK);
236   GST_BIN_CLOCK (bin) = clock;
237 }
238
239 void
240 gst_bin_auto_clock (GstBin *bin)
241 {
242   g_return_if_fail (bin != NULL);
243   g_return_if_fail (GST_IS_BIN (bin));
244
245   GST_FLAG_UNSET (bin, GST_BIN_FLAG_FIXED_CLOCK);
246   GST_BIN_CLOCK (bin) = NULL;
247 }
248
249 static void
250 gst_bin_set_element_sched (GstElement * element, GstScheduler * sched)
251 {
252   GList *children;
253   GstElement *child;
254
255   g_return_if_fail (element != NULL);
256   g_return_if_fail (GST_IS_ELEMENT (element));
257   g_return_if_fail (sched != NULL);
258   g_return_if_fail (GST_IS_SCHEDULER (sched));
259
260   GST_INFO (GST_CAT_SCHEDULING, "setting element \"%s\" sched to %p", GST_ELEMENT_NAME (element),
261             sched);
262
263   /* if it's actually a Bin */
264   if (GST_IS_BIN (element)) {
265     if (GST_FLAG_IS_SET (element, GST_BIN_FLAG_MANAGER)) {
266       GST_INFO_ELEMENT (GST_CAT_PARENTAGE, element, "child is already a manager, not resetting");
267       return;
268     }
269
270     GST_INFO_ELEMENT (GST_CAT_PARENTAGE, element, "setting children's schedule to parent's");
271     gst_scheduler_add_element (sched, element);
272
273     /* set the children's schedule */
274     children = GST_BIN (element)->children;
275     while (children) {
276       child = GST_ELEMENT (children->data);
277       children = g_list_next (children);
278
279       gst_bin_set_element_sched (child, sched);
280     }
281
282   }
283   /* otherwise, if it's just a regular old element */
284   else {
285     gst_scheduler_add_element (sched, element);
286   }
287 }
288
289
290 static void
291 gst_bin_unset_element_sched (GstElement * element)
292 {
293   GList *children;
294   GstElement *child;
295
296   g_return_if_fail (element != NULL);
297   g_return_if_fail (GST_IS_ELEMENT (element));
298
299   if (GST_ELEMENT_SCHED (element) == NULL) {
300     GST_INFO (GST_CAT_SCHEDULING, "element \"%s\" has no scheduler",
301               GST_ELEMENT_NAME (element));
302     return;
303   }
304   
305   GST_INFO (GST_CAT_SCHEDULING, "removing element \"%s\" from it sched %p",
306             GST_ELEMENT_NAME (element), GST_ELEMENT_SCHED (element));
307
308   /* if it's actually a Bin */
309   if (GST_IS_BIN (element)) {
310
311     if (GST_FLAG_IS_SET (element, GST_BIN_FLAG_MANAGER)) {
312       GST_INFO_ELEMENT (GST_CAT_PARENTAGE, element,
313                         "child is already a manager, not unsetting sched");
314       return;
315     }
316
317     gst_scheduler_remove_element (GST_ELEMENT_SCHED (element), element);
318
319     /* for each child, remove them from their schedule */
320     children = GST_BIN (element)->children;
321     while (children) {
322       child = GST_ELEMENT (children->data);
323       children = g_list_next (children);
324
325       gst_bin_unset_element_sched (child);
326     }
327
328   }
329   /* otherwise, if it's just a regular old element */
330   else {
331     gst_scheduler_remove_element (GST_ELEMENT_SCHED (element), element);
332   }
333 }
334
335
336 /**
337  * gst_element_connect_elements_many:
338  * @element_1: the first element to add to the bin
339  * @...: NULL-terminated list of elements to add to the bin
340  * 
341  * Add a list of elements to a bin. Uses #gst_bin_add.
342  **/
343 /* API FIXME: this should be called gst_element_connect_many, and connect_elements
344  * should just be connect */
345 void
346 gst_bin_add_many (GstBin *bin, GstElement *element_1, ...)
347 {
348   va_list args;
349
350   g_return_if_fail (bin != NULL);
351   g_return_if_fail (element_1 != NULL);
352   g_return_if_fail (GST_IS_BIN (bin));
353   g_return_if_fail (GST_IS_ELEMENT (element_1));
354
355   va_start (args, element_1);
356
357   while (element_1) {
358     gst_bin_add (bin, element_1);
359     
360     element_1 = va_arg (args, GstElement*);
361   }
362
363   va_end (args);
364 }
365
366 /**
367  * gst_bin_add:
368  * @bin: #GstBin to add element to
369  * @element: #GstElement to add to bin
370  *
371  * Add the given element to the bin.  Set the elements parent, and thus
372  * add a reference.
373  */
374 void
375 gst_bin_add (GstBin * bin, GstElement * element)
376 {
377   gint state_idx = 0;
378   GstElementState state;
379
380   g_return_if_fail (bin != NULL);
381   g_return_if_fail (GST_IS_BIN (bin));
382   g_return_if_fail (element != NULL);
383   g_return_if_fail (GST_IS_ELEMENT (element));
384
385   GST_DEBUG (GST_CAT_PARENTAGE, "adding element \"%s\" to bin \"%s\"\n",
386              GST_ELEMENT_NAME (element), GST_ELEMENT_NAME (bin));
387
388   /* must be not be in PLAYING state in order to modify bin */
389   g_return_if_fail (GST_STATE (bin) != GST_STATE_PLAYING);
390
391   /* the element must not already have a parent */
392   g_return_if_fail (GST_ELEMENT_PARENT (element) == NULL);
393
394   /* then check to see if the element's name is already taken in the bin */
395   g_return_if_fail (gst_object_check_uniqueness (bin->children, GST_ELEMENT_NAME (element)) ==
396                     TRUE);
397
398   /* set the element's parent and add the element to the bin's list of children */
399   gst_object_set_parent (GST_OBJECT (element), GST_OBJECT (bin));
400
401   bin->children = g_list_append (bin->children, element);
402   bin->numchildren++;
403
404   /* bump our internal state counter */
405   state = GST_STATE (element);
406   while (state >>= 1) state_idx++;
407   bin->child_states[state_idx]++;
408
409   /* now we have to deal with manager stuff 
410    * we can only do this if there's a scheduler: 
411    * if we're not a manager, and aren't attached to anything, we have no sched (yet) */
412   if (GST_IS_BIN (element) && GST_FLAG_IS_SET (element, GST_BIN_FLAG_MANAGER)) {
413     GST_INFO_ELEMENT (GST_CAT_PARENTAGE, element, "child is a manager");
414   }
415   else if (GST_ELEMENT_SCHED (bin) != NULL) {
416     gst_bin_set_element_sched (element, GST_ELEMENT_SCHED (bin));
417   }
418
419   GST_INFO_ELEMENT (GST_CAT_PARENTAGE, bin, "added child \"%s\"", GST_ELEMENT_NAME (element));
420
421   g_signal_emit (G_OBJECT (bin), gst_bin_signals[OBJECT_ADDED], 0, element);
422 }
423
424 /**
425  * gst_bin_remove:
426  * @bin: #GstBin to remove element from
427  * @element: #GstElement to remove
428  *
429  * Remove the element from its associated bin, unparenting as well.
430  */
431 void
432 gst_bin_remove (GstBin * bin, GstElement * element)
433 {
434   gint state_idx = 0;
435   GstElementState state;
436
437   GST_DEBUG_ELEMENT (GST_CAT_PARENTAGE, bin, "trying to remove child %s\n", GST_ELEMENT_NAME (element));
438
439   g_return_if_fail (bin != NULL);
440   g_return_if_fail (GST_IS_BIN (bin));
441   g_return_if_fail (element != NULL);
442   g_return_if_fail (GST_IS_ELEMENT (element));
443   g_return_if_fail (bin->children != NULL);
444
445   /* must not be in PLAYING state in order to modify bin */
446   g_return_if_fail (GST_STATE (bin) != GST_STATE_PLAYING);
447
448   /* the element must have its parent set to the current bin */
449   g_return_if_fail (GST_ELEMENT_PARENT (element) == (GstObject *) bin);
450
451   /* the element must be in the bin's list of children */
452   if (g_list_find (bin->children, element) == NULL) {
453     g_warning ("no element \"%s\" in bin \"%s\"\n", GST_ELEMENT_NAME (element),
454                GST_ELEMENT_NAME (bin));
455     return;
456   }
457
458   /* remove this element from the list of managed elements */
459   gst_bin_unset_element_sched (element);
460
461   /* now remove the element from the list of elements */
462   bin->children = g_list_remove (bin->children, element);
463   bin->numchildren--;
464
465   /* bump our internal state counter */
466   state = GST_STATE (element);
467   while (state >>= 1) state_idx++;
468   bin->child_states[state_idx]--;
469
470   GST_INFO_ELEMENT (GST_CAT_PARENTAGE, bin, "removed child %s", GST_ELEMENT_NAME (element));
471
472   gst_object_unparent (GST_OBJECT (element));
473
474   /* if we're down to zero children, force state to NULL */
475   if (bin->numchildren == 0 && GST_ELEMENT_SCHED (bin) != NULL) {
476     GST_STATE_PENDING (bin) = GST_STATE_NULL;
477     gst_bin_change_state_norecurse (bin);
478   }
479 }
480
481 /**
482  * gst_bin_child_state_change:
483  * @bin: #GstBin with the child
484  * @oldstate: The old child state
485  * @newstate: The new child state
486  * @child: #GstElement that signaled an changed state
487  *
488  * An internal function to inform the parent bin about a state change
489  * of a child.
490  */
491 void
492 gst_bin_child_state_change (GstBin *bin, GstElementState oldstate, GstElementState newstate,
493                             GstElement *child)
494 {
495   gint old_idx = 0, new_idx = 0, i;
496
497   GST_INFO (GST_CAT_STATES, "child %s changed state in bin %s from %s to %s",
498             GST_ELEMENT_NAME (child), GST_ELEMENT_NAME (bin),
499             gst_element_statename (oldstate), gst_element_statename (newstate));
500
501   while (oldstate >>= 1) old_idx++;
502   while (newstate >>= 1) new_idx++;
503
504   GST_LOCK (bin);
505   bin->child_states[old_idx]--;
506   bin->child_states[new_idx]++;
507
508   for (i = GST_NUM_STATES - 1; i >= 0; i--) {
509     if (bin->child_states[i] != 0) {
510       gint state = (1 << i);
511       if (GST_STATE (bin) != state) {
512         GST_INFO (GST_CAT_STATES, "bin %s need state change to %s",
513                   GST_ELEMENT_NAME (bin), gst_element_statename (state));
514         GST_STATE_PENDING (bin) = state;
515         gst_bin_change_state_norecurse (bin);
516       }
517       break;
518     }
519   }
520   GST_UNLOCK (bin);
521 }
522
523 static GstElementStateReturn
524 gst_bin_change_state (GstElement * element)
525 {
526   GstBin *bin;
527   GList *children;
528   GstElement *child;
529   GstElementStateReturn ret;
530   GstElementState old_state, pending;
531   gint transition;
532   gboolean have_async = FALSE;
533
534   g_return_val_if_fail (GST_IS_BIN (element), GST_STATE_FAILURE);
535
536   bin = GST_BIN (element);
537
538   old_state = GST_STATE (element);
539   pending = GST_STATE_PENDING (element);
540   transition = GST_STATE_TRANSITION (element);
541
542   GST_INFO_ELEMENT (GST_CAT_STATES, element, "changing childrens' state from %s to %s",
543                     gst_element_statename (old_state), gst_element_statename (pending));
544
545   children = bin->children;
546   while (children) {
547     child = GST_ELEMENT (children->data);
548     children = g_list_next (children);
549
550     switch (gst_element_set_state (child, pending)) {
551       case GST_STATE_FAILURE:
552         GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
553         GST_DEBUG (GST_CAT_STATES, "child '%s' failed to go to state %d(%s)\n",
554                    GST_ELEMENT_NAME (child), pending, gst_element_statename (pending));
555
556         gst_element_set_state (child, old_state);
557         if (GST_ELEMENT_SCHED (child) == GST_ELEMENT_SCHED (element)) {
558           /* reset to what is was */
559           GST_STATE_PENDING (element) = old_state;
560           gst_bin_change_state (element);
561           return GST_STATE_FAILURE;
562         }
563         break;
564       case GST_STATE_ASYNC:
565         GST_DEBUG (GST_CAT_STATES, "child '%s' is changing state asynchronously\n",
566                    GST_ELEMENT_NAME (child));
567         have_async = TRUE;
568         break;
569     }
570   }
571
572   if (GST_ELEMENT_SCHED (bin) != NULL && GST_ELEMENT_PARENT (bin) == NULL) {
573     switch (transition) {
574       case GST_STATE_NULL_TO_READY:
575         gst_bin_distribute_clocks (bin);
576         break;
577       case GST_STATE_READY_TO_PAUSED:
578         if (GST_BIN_CLOCK (bin))
579           gst_clock_reset (GST_BIN_CLOCK (bin));
580         break;
581       case GST_STATE_PAUSED_TO_PLAYING:
582         gst_bin_distribute_clocks (bin);
583         if (GST_BIN_CLOCK (bin))
584           gst_clock_activate (GST_BIN_CLOCK (bin), TRUE);
585         break;
586       case GST_STATE_PLAYING_TO_PAUSED:
587         if (GST_BIN_CLOCK (bin))
588           gst_clock_activate (GST_BIN_CLOCK (bin), FALSE);
589         break;
590     }
591   }
592
593
594   GST_INFO_ELEMENT (GST_CAT_STATES, element, "done changing bin's state from %s to %s, now in %s",
595                 gst_element_statename (old_state),
596                 gst_element_statename (pending),
597                 gst_element_statename (GST_STATE (element)));
598
599   if (have_async)
600     ret = GST_STATE_ASYNC;
601   else
602     ret = GST_STATE_SUCCESS;
603
604   return ret;
605 }
606
607
608 static GstElementStateReturn
609 gst_bin_change_state_norecurse (GstBin * bin)
610 {
611   GstElementStateReturn ret;
612
613   if (GST_ELEMENT_CLASS (parent_class)->change_state) {
614     GST_DEBUG_ELEMENT (GST_CAT_STATES, bin, "setting bin's own state\n");
615     ret = GST_ELEMENT_CLASS (parent_class)->change_state (GST_ELEMENT (bin));
616
617     return ret;
618   }
619   else
620     return GST_STATE_FAILURE;
621 }
622
623 static gboolean
624 gst_bin_change_state_type (GstBin * bin, GstElementState state, GType type)
625 {
626   GList *children;
627   GstElement *child;
628
629   g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
630   g_return_val_if_fail (bin->numchildren != 0, FALSE);
631
632   children = bin->children;
633   while (children) {
634     child = GST_ELEMENT (children->data);
635     if (GST_IS_BIN (child)) {
636       if (!gst_bin_set_state_type (GST_BIN (child), state, type))
637         return FALSE;
638     }
639     else if (G_TYPE_CHECK_INSTANCE_TYPE (child, type)) {
640       if (!gst_element_set_state (child, state))
641         return FALSE;
642     }
643     children = g_list_next (children);
644   }
645   if (type == GST_TYPE_BIN)
646     gst_element_set_state (GST_ELEMENT (bin), state);
647
648   return TRUE;
649 }
650
651 /**
652  * gst_bin_set_state_type:
653  * @bin: #GstBin to set the state
654  * @state: the new state to set the elements to
655  * @type: the type of elements to change
656  *
657  * Sets the state of only those objects of the given type.
658  *
659  * Returns: indication if the state change was successfull
660  */
661 gboolean
662 gst_bin_set_state_type (GstBin * bin, GstElementState state, GType type)
663 {
664   GstBinClass *oclass;
665
666   GST_DEBUG (GST_CAT_STATES, "gst_bin_set_state_type(\"%s\",%d,%s)\n",
667              GST_ELEMENT_NAME (bin), state, G_OBJECT_TYPE_NAME (type));
668
669   g_return_val_if_fail (bin != NULL, FALSE);
670   g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
671
672   oclass = GST_BIN_CLASS (G_OBJECT_GET_CLASS (bin));
673
674   if (oclass->change_state_type)
675     (oclass->change_state_type) (bin, state, type);
676   return TRUE;
677 }
678
679 static void
680 gst_bin_dispose (GObject * object)
681 {
682   GstBin *bin = GST_BIN (object);
683   GList *children, *orig;
684   GstElement *child;
685
686   GST_DEBUG (GST_CAT_REFCOUNTING, "dispose\n");
687
688   if (gst_element_get_state (GST_ELEMENT (object)) == GST_STATE_PLAYING)
689     gst_element_set_state (GST_ELEMENT (object), GST_STATE_PAUSED);
690
691   if (bin->children) {
692     orig = children = g_list_copy (bin->children);
693     while (children) {
694       child = GST_ELEMENT (children->data);
695       gst_bin_remove (bin, child);
696       children = g_list_next (children);
697     }
698     g_list_free (bin->children);
699     g_list_free (orig);
700   }
701   bin->children = NULL;
702   bin->numchildren = 0;
703
704   G_OBJECT_CLASS (parent_class)->dispose (object);
705 }
706
707 /**
708  * gst_bin_get_by_name:
709  * @bin: #Gstbin to search
710  * @name: the element name to search for
711  *
712  * Get the element with the given name from this bin.
713  *
714  * Returns: the element with the given name
715  */
716 GstElement *
717 gst_bin_get_by_name (GstBin * bin, const gchar * name)
718 {
719   GList *children;
720   GstElement *child;
721
722   g_return_val_if_fail (bin != NULL, NULL);
723   g_return_val_if_fail (GST_IS_BIN (bin), NULL);
724   g_return_val_if_fail (name != NULL, NULL);
725
726   GST_INFO_ELEMENT (GST_CAT_PARENTAGE, bin, "looking up child element %s", name);
727
728   children = bin->children;
729   while (children) {
730     child = GST_ELEMENT (children->data);
731     if (!strcmp (GST_OBJECT_NAME (child), name))
732       return child;
733     if (GST_IS_BIN (child)) {
734       GstElement *res = gst_bin_get_by_name (GST_BIN (child), name);
735
736       if (res)
737         return res;
738     }
739     children = g_list_next (children);
740   }
741
742   return NULL;
743 }
744
745 /**
746  * gst_bin_get_by_name_recurse_up:
747  * @bin: #Gstbin to search
748  * @name: the element name to search for
749  *
750  * Get the element with the given name from this bin. If the
751  * element is not found, a recursion is performed on the parent bin.
752  *
753  * Returns: the element with the given name
754  */
755 GstElement *
756 gst_bin_get_by_name_recurse_up (GstBin * bin, const gchar * name)
757 {
758   GstElement *result = NULL;
759   GstObject *parent;
760
761   g_return_val_if_fail (bin != NULL, NULL);
762   g_return_val_if_fail (GST_IS_BIN (bin), NULL);
763   g_return_val_if_fail (name != NULL, NULL);
764
765   result = gst_bin_get_by_name (bin, name);
766
767   if (!result) {
768     parent = gst_object_get_parent (GST_OBJECT (bin));
769
770     if (parent && GST_IS_BIN (parent)) {
771       result = gst_bin_get_by_name_recurse_up (GST_BIN (parent), name);
772     }
773   }
774
775   return result;
776 }
777
778 /**
779  * gst_bin_get_list:
780  * @bin: #Gstbin to get the list from
781  *
782  * Get the list of elements in this bin.
783  *
784  * Returns: a GList of elements
785  */
786 GList *
787 gst_bin_get_list (GstBin * bin)
788 {
789   g_return_val_if_fail (bin != NULL, NULL);
790   g_return_val_if_fail (GST_IS_BIN (bin), NULL);
791
792   return bin->children;
793 }
794
795 #ifndef GST_DISABLE_LOADSAVE
796 static xmlNodePtr
797 gst_bin_save_thyself (GstObject * object, xmlNodePtr parent)
798 {
799   GstBin *bin = GST_BIN (object);
800   xmlNodePtr childlist, elementnode;
801   GList *children;
802   GstElement *child;
803
804   if (GST_OBJECT_CLASS (parent_class)->save_thyself)
805     GST_OBJECT_CLASS (parent_class)->save_thyself (GST_OBJECT (bin), parent);
806
807   childlist = xmlNewChild (parent, NULL, "children", NULL);
808
809   GST_INFO_ELEMENT (GST_CAT_XML, bin, "saving %d children", bin->numchildren);
810
811   children = bin->children;
812   while (children) {
813     child = GST_ELEMENT (children->data);
814     elementnode = xmlNewChild (childlist, NULL, "element", NULL);
815     gst_object_save_thyself (GST_OBJECT (child), elementnode);
816     children = g_list_next (children);
817   }
818   return childlist;
819 }
820
821 static void
822 gst_bin_restore_thyself (GstObject * object, xmlNodePtr self)
823 {
824   GstBin *bin = GST_BIN (object);
825   xmlNodePtr field = self->xmlChildrenNode;
826   xmlNodePtr childlist;
827
828   while (field) {
829     if (!strcmp (field->name, "children")) {
830       GST_INFO_ELEMENT (GST_CAT_XML, GST_ELEMENT (object), "loading children");
831       childlist = field->xmlChildrenNode;
832       while (childlist) {
833         if (!strcmp (childlist->name, "element")) {
834           GstElement *element = gst_xml_make_element (childlist, GST_OBJECT (bin));
835           
836           /* it had to be parented to find the pads, now we ref and unparent so
837            * we can add it to the bin */
838           gst_object_ref (GST_OBJECT (element));
839           gst_object_unparent (GST_OBJECT (element));
840           
841           gst_bin_add (bin, element);
842         }
843         childlist = childlist->next;
844       }
845     }
846
847     field = field->next;
848   }
849 }
850 #endif /* GST_DISABLE_LOADSAVE */
851
852 static gboolean
853 gst_bin_iterate_func (GstBin * bin)
854 {
855   /* only iterate if this is the manager bin */
856   if (GST_ELEMENT_SCHED (bin) &&
857       GST_ELEMENT_SCHED (bin)->parent == GST_ELEMENT (bin)) {
858     GstSchedulerState state;
859
860     state = gst_scheduler_iterate (GST_ELEMENT_SCHED (bin));
861
862     if (state == GST_SCHEDULER_STATE_RUNNING) {
863       return TRUE;
864     }
865     else if (state == GST_SCHEDULER_STATE_ERROR) {
866       gst_element_set_state (GST_ELEMENT (bin), GST_STATE_PAUSED);
867     }
868   }
869   else {
870     g_warning ("bin \"%s\" can't be iterated on!\n", GST_ELEMENT_NAME (bin));
871   }
872
873   return FALSE;
874 }
875
876 /**
877  * gst_bin_iterate:
878  * @bin: #Gstbin to iterate
879  *
880  * Iterates over the elements in this bin.
881  *
882  * Returns: TRUE if the bin did something usefull. This value
883  *          can be used to determine it the bin is in EOS.
884  */
885 gboolean
886 gst_bin_iterate (GstBin * bin)
887 {
888   GstBinClass *oclass;
889   gboolean running = TRUE;
890
891   GST_DEBUG_ENTER ("(\"%s\")", GST_ELEMENT_NAME (bin));
892
893   g_return_val_if_fail (bin != NULL, FALSE);
894   g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
895
896   oclass = GST_BIN_CLASS (G_OBJECT_GET_CLASS (bin));
897
898   if (oclass->iterate)
899     running = (oclass->iterate) (bin);
900
901   GST_DEBUG_LEAVE ("(\"%s\") %d", GST_ELEMENT_NAME (bin), running);
902
903   if (!running) {
904     if (GST_STATE (bin) == GST_STATE_PLAYING && GST_STATE_PENDING (bin) == GST_STATE_VOID_PENDING) {
905       GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, bin,
906                          "waiting for child shutdown after useless iteration\n");
907       gst_element_wait_state_change (GST_ELEMENT (bin));
908       GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, bin, "child shutdown\n");
909     }
910   }
911
912   return running;
913 }