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