This is a megapatch with the following changes:
[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 "config.h"
25 #include "gst_private.h"
26
27 #include "gstbin.h"
28
29 #include "gstscheduler.h"
30
31 GstElementDetails gst_bin_details = {
32   "Generic bin",
33   "Bin",
34   "Simple container object",
35   VERSION,
36   "Erik Walthinsen <omega@cse.ogi.edu>",
37   "(C) 1999",
38 };
39
40
41 static void                     gst_bin_real_destroy            (GtkObject *object);
42
43 static GstElementStateReturn    gst_bin_change_state            (GstElement *element);
44 static GstElementStateReturn    gst_bin_change_state_norecurse  (GstBin *bin);
45 static gboolean                 gst_bin_change_state_type       (GstBin *bin,
46                                                                  GstElementState state,
47                                                                  GtkType type);
48
49 static void                     gst_bin_create_plan_func        (GstBin *bin);
50 static gboolean                 gst_bin_iterate_func            (GstBin *bin);
51
52 static xmlNodePtr               gst_bin_save_thyself            (GstObject *object, xmlNodePtr parent);
53 static void                     gst_bin_restore_thyself         (GstObject *object, xmlNodePtr self);
54
55 /* Bin signals and args */
56 enum {
57   OBJECT_ADDED,
58   LAST_SIGNAL
59 };
60
61 enum {
62   ARG_0,
63   /* FILL ME */
64 };
65
66
67 static void gst_bin_class_init  (GstBinClass *klass);
68 static void gst_bin_init        (GstBin *bin);
69
70
71 static GstElementClass *parent_class = NULL;
72 static guint gst_bin_signals[LAST_SIGNAL] = { 0 };
73
74 GtkType
75 gst_bin_get_type (void)
76 {
77   static GtkType bin_type = 0;
78
79   if (!bin_type) {
80     static const GtkTypeInfo bin_info = {
81       "GstBin",
82       sizeof(GstBin),
83       sizeof(GstBinClass),
84       (GtkClassInitFunc)gst_bin_class_init,
85       (GtkObjectInitFunc)gst_bin_init,
86       (GtkArgSetFunc)NULL,
87       (GtkArgGetFunc)NULL,
88       (GtkClassInitFunc)NULL,
89     };
90     bin_type = gtk_type_unique (GST_TYPE_ELEMENT, &bin_info);
91   }
92   return bin_type;
93 }
94
95 static void
96 gst_bin_class_init (GstBinClass *klass)
97 {
98   GtkObjectClass *gtkobject_class;
99   GstObjectClass *gstobject_class;
100   GstElementClass *gstelement_class;
101
102   gtkobject_class = (GtkObjectClass*)klass;
103   gstobject_class = (GstObjectClass*)klass;
104   gstelement_class = (GstElementClass*)klass;
105
106   parent_class = gtk_type_class (GST_TYPE_ELEMENT);
107
108   gst_bin_signals[OBJECT_ADDED] =
109     gtk_signal_new ("object_added", GTK_RUN_FIRST, gtkobject_class->type,
110                     GTK_SIGNAL_OFFSET (GstBinClass, object_added),
111                     gtk_marshal_NONE__POINTER, GTK_TYPE_NONE, 1,
112                     GST_TYPE_ELEMENT);
113   gtk_object_class_add_signals (gtkobject_class, gst_bin_signals, LAST_SIGNAL);
114
115   klass->change_state_type =            gst_bin_change_state_type;
116   klass->create_plan =                  gst_bin_create_plan_func;
117   klass->schedule =                     gst_bin_schedule_func;
118   klass->iterate =                      gst_bin_iterate_func;
119
120   gstobject_class->save_thyself =       gst_bin_save_thyself;
121   gstobject_class->restore_thyself =    gst_bin_restore_thyself;
122
123   gstelement_class->change_state =      gst_bin_change_state;
124
125   gtkobject_class->destroy =            gst_bin_real_destroy;
126 }
127
128 static void
129 gst_bin_init (GstBin *bin)
130 {
131   // in general, we prefer to use cothreads for most things
132   GST_FLAG_SET (bin, GST_BIN_FLAG_PREFER_COTHREADS);
133
134   bin->numchildren = 0;
135   bin->children = NULL;
136   bin->eos_providers = NULL;
137   bin->num_eos_providers = 0;
138   bin->chains = NULL;
139   bin->eoscond = g_cond_new ();
140 // FIXME temporary testing measure
141 //  bin->use_cothreads = TRUE;
142 }
143
144 /**
145  * gst_bin_new:
146  * @name: name of new bin
147  *
148  * Create a new bin with given name.
149  *
150  * Returns: new bin
151  */
152 GstElement*
153 gst_bin_new (const gchar *name)
154 {
155   return gst_elementfactory_make ("bin", name);
156 }
157
158 /**
159  * gst_bin_add:
160  * @bin: #GstBin to add element to
161  * @element: #GstElement to add to bin
162  *
163  * Add the given element to the bin.  Set the elements parent, and thus
164  * add a reference.
165  */
166 void
167 gst_bin_add (GstBin *bin,
168              GstElement *element)
169 {
170   g_return_if_fail (bin != NULL);
171   g_return_if_fail (GST_IS_BIN (bin));
172   g_return_if_fail (element != NULL);
173   g_return_if_fail (GST_IS_ELEMENT (element));
174
175   // must be NULL or PAUSED state in order to modify bin
176   g_return_if_fail ((GST_STATE (bin) == GST_STATE_NULL) ||
177                     (GST_STATE (bin) == GST_STATE_PAUSED));
178
179   bin->children = g_list_append (bin->children, element);
180   bin->numchildren++;
181   gst_object_set_parent (GST_OBJECT (element), GST_OBJECT (bin));
182
183   GST_INFO_ELEMENT (GST_CAT_PARENTAGE, bin, "added child %s", GST_ELEMENT_NAME (element));
184
185   /* we know we have at least one child, we just added one... */
186 //  if (GST_STATE(element) < GST_STATE_READY)
187 //    gst_bin_change_state_norecurse(bin,GST_STATE_READY);
188
189   gtk_signal_emit (GTK_OBJECT (bin), gst_bin_signals[OBJECT_ADDED], element);
190 }
191
192 /**
193  * gst_bin_remove:
194  * @bin: #GstBin to remove element from
195  * @element: #GstElement to remove
196  *
197  * Remove the element from its associated bin, unparenting as well.
198  */
199 void
200 gst_bin_remove (GstBin *bin,
201                 GstElement *element)
202 {
203   g_return_if_fail (bin != NULL);
204   g_return_if_fail (GST_IS_BIN (bin));
205   g_return_if_fail (element != NULL);
206   g_return_if_fail (GST_IS_ELEMENT (element));
207   g_return_if_fail (bin->children != NULL);
208
209   // must be NULL or PAUSED state in order to modify bin
210   g_return_if_fail ((GST_STATE (bin) == GST_STATE_NULL) ||
211                     (GST_STATE (bin) == GST_STATE_PAUSED));
212
213   if (g_list_find(bin->children, element) == NULL) {
214     // FIXME this should be a warning!!!
215     GST_ERROR_OBJECT(bin,element,"no such element in bin");
216     return;
217   }
218
219   gst_object_unparent (GST_OBJECT (element));
220   bin->children = g_list_remove (bin->children, element);
221   bin->numchildren--;
222
223   GST_INFO_ELEMENT (GST_CAT_PARENTAGE, bin, "removed child %s", GST_ELEMENT_NAME (element));
224
225   /* if we're down to zero children, force state to NULL */
226   if (bin->numchildren == 0)
227     gst_element_set_state (GST_ELEMENT (bin), GST_STATE_NULL);
228 }
229
230
231 static GstElementStateReturn
232 gst_bin_change_state (GstElement *element)
233 {
234   GstBin *bin;
235   GList *children;
236   GstElement *child;
237
238   GST_DEBUG_ENTER("(\"%s\")",GST_ELEMENT_NAME  (element));
239
240   g_return_val_if_fail (GST_IS_BIN (element), GST_STATE_FAILURE);
241
242   bin = GST_BIN (element);
243
244 //  GST_DEBUG (0,"currently %d(%s), %d(%s) pending\n",GST_STATE (element),
245 //          _gst_print_statename (GST_STATE (element)), GST_STATE_PENDING (element),
246 //          _gst_print_statename (GST_STATE_PENDING (element)));
247
248   GST_INFO_ELEMENT (GST_CAT_STATES, element, "changing bin's state from %s to %s",
249                 _gst_print_statename (GST_STATE (element)),
250                 _gst_print_statename (GST_STATE_PENDING (element)));
251
252 //  g_return_val_if_fail(bin->numchildren != 0, GST_STATE_FAILURE);
253
254   switch (GST_STATE_TRANSITION (element)) {
255     case GST_STATE_NULL_TO_READY:
256     {
257       GstObject *parent;
258
259       parent = gst_object_get_parent (GST_OBJECT (element));
260
261       if (!parent || !GST_IS_BIN (parent))
262         gst_bin_create_plan (bin);
263
264       break;
265     }
266     default:
267       break;
268   }
269
270 //  g_print("-->\n");
271   children = bin->children;
272   while (children) {
273     child = GST_ELEMENT (children->data);
274     GST_DEBUG (0,"setting state on '%s'\n",GST_ELEMENT_NAME  (child));
275     switch (gst_element_set_state (child, GST_STATE_PENDING (element))) {
276       case GST_STATE_FAILURE:
277         GST_STATE_PENDING (element) = GST_STATE_NONE_PENDING;
278         GST_DEBUG (0,"child '%s' failed to go to state %d(%s)\n", GST_ELEMENT_NAME  (child),
279               GST_STATE_PENDING (element), _gst_print_statename (GST_STATE_PENDING (element)));
280         return GST_STATE_FAILURE;
281         break;
282       case GST_STATE_ASYNC:
283         GST_DEBUG (0,"child '%s' is changing state asynchronously\n", GST_ELEMENT_NAME  (child));
284         break;
285     }
286 //    g_print("\n");
287     children = g_list_next (children);
288   }
289 //  g_print("<-- \"%s\"\n",gst_object_get_name(GST_OBJECT(bin)));
290
291
292   return gst_bin_change_state_norecurse (bin);
293 }
294
295
296 static GstElementStateReturn
297 gst_bin_change_state_norecurse (GstBin *bin)
298 {
299
300   if (GST_ELEMENT_CLASS (parent_class)->change_state)
301     return GST_ELEMENT_CLASS (parent_class)->change_state (GST_ELEMENT (bin));
302   else
303     return GST_STATE_FAILURE;
304 }
305
306 static gboolean
307 gst_bin_change_state_type(GstBin *bin,
308                           GstElementState state,
309                           GtkType type)
310 {
311   GList *children;
312   GstElement *child;
313
314 //  g_print("gst_bin_change_state_type(\"%s\",%d,%d);\n",
315 //          gst_object_get_name(GST_OBJECT(bin)),state,type);
316
317   g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
318   g_return_val_if_fail (bin->numchildren != 0, FALSE);
319
320 //  g_print("-->\n");
321   children = bin->children;
322   while (children) {
323     child = GST_ELEMENT (children->data);
324     if (GST_IS_BIN (child)) {
325       if (!gst_bin_set_state_type (GST_BIN (child), state,type))
326         return FALSE;
327     } else if (GTK_CHECK_TYPE (child,type)) {
328       if (!gst_element_set_state (child,state))
329         return FALSE;
330     }
331 //    g_print("\n");
332     children = g_list_next (children);
333   }
334   if (type == GST_TYPE_BIN)
335     gst_element_set_state (GST_ELEMENT (bin),state);
336
337   return TRUE;
338 }
339
340 /**
341  * gst_bin_set_state_type:
342  * @bin: #GstBin to set the state
343  * @state: the new state to set the elements to
344  * @type: the type of elements to change
345  *
346  * Sets the state of only those objects of the given type.
347  *
348  * Returns: indication if the state change was successfull
349  */
350 gboolean
351 gst_bin_set_state_type (GstBin *bin,
352                         GstElementState state,
353                         GtkType type)
354 {
355   GstBinClass *oclass;
356
357   GST_DEBUG (0,"gst_bin_set_state_type(\"%s\",%d,%d)\n",
358           GST_ELEMENT_NAME (bin), state,type);
359
360   g_return_val_if_fail (bin != NULL, FALSE);
361   g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
362
363   oclass = GST_BIN_CLASS (GTK_OBJECT (bin)->klass);
364
365   if (oclass->change_state_type)
366     (oclass->change_state_type) (bin,state,type);
367   return TRUE;
368 }
369
370 static void
371 gst_bin_real_destroy (GtkObject *object)
372 {
373   GstBin *bin = GST_BIN (object);
374   GList *children;
375   GstElement *child;
376
377   GST_DEBUG (0,"in gst_bin_real_destroy()\n");
378
379   children = bin->children;
380   while (children) {
381     child = GST_ELEMENT (children->data);
382     gst_element_destroy (child);
383     children = g_list_next (children);
384   }
385
386   g_list_free (bin->children);
387 }
388
389 /**
390  * gst_bin_get_by_name:
391  * @bin: #Gstbin to search
392  * @name: the element name to search for
393  *
394  * Get the element with the given name from this bin.
395  *
396  * Returns: the element with the given name
397  */
398 GstElement*
399 gst_bin_get_by_name (GstBin *bin,
400                      const gchar *name)
401 {
402   GList *children;
403   GstElement *child;
404
405   g_return_val_if_fail (bin != NULL, NULL);
406   g_return_val_if_fail (GST_IS_BIN (bin), NULL);
407   g_return_val_if_fail (name != NULL, NULL);
408
409   GST_INFO_ELEMENT (GST_CAT_PARENTAGE, bin, "looking up child element %s", name);
410
411   children = bin->children;
412   while (children) {
413     child = GST_ELEMENT (children->data);
414     if (!strcmp (gst_object_get_name (GST_OBJECT (child)),name))
415       return child;
416     if (GST_IS_BIN (child)) {
417       GstElement *res = gst_bin_get_by_name (GST_BIN (child), name);
418       if (res)
419         return res;
420     }
421     children = g_list_next (children);
422   }
423
424   return NULL;
425 }
426
427 /**
428  * gst_bin_get_by_name_recurse_up:
429  * @bin: #Gstbin to search
430  * @name: the element name to search for
431  *
432  * Get the element with the given name from this bin. If the
433  * element is not found, a recursion is performed on the parent bin.
434  *
435  * Returns: the element with the given name
436  */
437 GstElement*
438 gst_bin_get_by_name_recurse_up (GstBin *bin,
439                                 const gchar *name)
440 {
441   GstElement *result = NULL;
442   GstObject *parent;
443
444   g_return_val_if_fail (bin != NULL, NULL);
445   g_return_val_if_fail (GST_IS_BIN (bin), NULL);
446   g_return_val_if_fail (name != NULL, NULL);
447
448   result = gst_bin_get_by_name (bin, name);
449
450   if (result)
451     return result;
452
453   parent = gst_object_get_parent (GST_OBJECT (bin));
454
455   if (parent && GST_IS_BIN (parent)) {
456     result = gst_bin_get_by_name_recurse_up (GST_BIN (parent), name);
457   }
458
459   return result;
460 }
461
462 /**
463  * gst_bin_get_list:
464  * @bin: #Gstbin to get the list from
465  *
466  * Get the list of elements in this bin.
467  *
468  * Returns: a GList of elements
469  */
470 GList*
471 gst_bin_get_list (GstBin *bin)
472 {
473   g_return_val_if_fail (bin != NULL, NULL);
474   g_return_val_if_fail (GST_IS_BIN (bin), NULL);
475
476   return bin->children;
477 }
478
479 static xmlNodePtr
480 gst_bin_save_thyself (GstObject *object,
481                       xmlNodePtr parent)
482 {
483   GstBin *bin = GST_BIN (object);
484   xmlNodePtr childlist, elementnode;
485   GList *children;
486   GstElement *child;
487
488   if (GST_OBJECT_CLASS (parent_class)->save_thyself)
489     GST_OBJECT_CLASS (parent_class)->save_thyself (GST_OBJECT (bin), parent);
490
491   childlist = xmlNewChild (parent, NULL, "children", NULL);
492
493   GST_INFO_ELEMENT (GST_CAT_XML, bin, "saving %d children", bin->numchildren);
494
495   children = bin->children;
496   while (children) {
497     child = GST_ELEMENT (children->data);
498     elementnode = xmlNewChild (childlist, NULL, "element", NULL);
499     gst_object_save_thyself (GST_OBJECT (child), elementnode);
500     children = g_list_next (children);
501   }
502   return childlist;
503 }
504
505 static void
506 gst_bin_restore_thyself (GstObject *object,
507                          xmlNodePtr self)
508 {
509   GstBin *bin = GST_BIN (object);
510   xmlNodePtr field = self->xmlChildrenNode;
511   xmlNodePtr childlist;
512
513   while (field) {
514     if (!strcmp (field->name, "children")) {
515       GST_INFO_ELEMENT (GST_CAT_XML, GST_ELEMENT (object), "loading children");
516       childlist = field->xmlChildrenNode;
517       while (childlist) {
518         if (!strcmp (childlist->name, "element")) {
519           GstElement *element = gst_element_load_thyself (childlist, GST_OBJECT (bin));
520
521           gst_bin_add (bin, element);
522         }
523         childlist = childlist->next;
524       }
525     }
526
527     field = field->next;
528   }
529 }
530
531 void
532 gst_bin_use_cothreads (GstBin *bin,
533                        gboolean enabled)
534 {
535   g_return_if_fail (GST_IS_BIN (bin));
536
537   bin->use_cothreads = enabled;
538 }
539
540 /**
541  * gst_bin_iterate:
542  * @bin: #Gstbin to iterate
543  *
544  * Iterates over the elements in this bin.
545  *
546  * Returns: TRUE if the bin did something usefull. This value
547  *          can be used to determine it the bin is in EOS.
548  */
549 gboolean
550 gst_bin_iterate (GstBin *bin)
551 {
552   GstBinClass *oclass;
553   gboolean eos = TRUE;
554
555   GST_DEBUG_ENTER("(\"%s\")",GST_ELEMENT_NAME (bin));
556
557   oclass = GST_BIN_CLASS (GTK_OBJECT (bin)->klass);
558
559   if (oclass->iterate)
560     eos = (oclass->iterate) (bin);
561
562   GST_DEBUG_LEAVE("(\"%s\")",GST_ELEMENT_NAME (bin));
563
564   return eos;
565 }
566
567 /**
568  * gst_bin_create_plan:
569  * @bin: #GstBin to create the plan for
570  *
571  * Let the bin figure out how to handle its children.
572  */
573 void
574 gst_bin_create_plan (GstBin *bin)
575 {
576   GstBinClass *oclass;
577
578   oclass = GST_BIN_CLASS (GTK_OBJECT (bin)->klass);
579
580   if (oclass->create_plan)
581     (oclass->create_plan) (bin);
582 }
583
584 /* out internal element fired EOS, we decrement the number of pending EOS childs */
585 static void
586 gst_bin_received_eos (GstElement *element, GstBin *bin)
587 {
588   GST_INFO_ELEMENT (GST_CAT_PLANNING, bin, "child %s fired eos, pending %d\n", GST_ELEMENT_NAME (element),
589                   bin->num_eos_providers);
590
591   GST_LOCK (bin);
592   if (bin->num_eos_providers) {
593     bin->num_eos_providers--;
594     g_cond_signal (bin->eoscond);
595   }
596   GST_UNLOCK (bin);
597 }
598
599 /**
600  * gst_bin_schedule:
601  * @bin: #GstBin to schedule
602  *
603  * Let the bin figure out how to handle its children.
604  */
605 void
606 gst_bin_schedule (GstBin *bin)
607 {
608   GstBinClass *oclass;
609
610   oclass = GST_BIN_CLASS (GTK_OBJECT (bin)->klass);
611
612   if (oclass->schedule)
613     (oclass->schedule) (bin);
614 }
615
616 typedef struct {
617   gulong offset;
618   gulong size;
619 } region_struct;
620
621
622 static void
623 gst_bin_create_plan_func (GstBin *bin)
624 {
625   GstElement *manager;
626   GList *elements;
627   GstElement *element;
628 #ifdef GST_DEBUG_ENABLED
629   const gchar *elementname;
630 #endif
631   GSList *pending = NULL;
632   GstBin *pending_bin;
633
634   GST_DEBUG_ENTER("(\"%s\")",GST_ELEMENT_NAME  (bin));
635
636   GST_INFO_ELEMENT (GST_CAT_PLANNING, bin, "creating plan");
637
638   // first figure out which element is the manager of this and all child elements
639   // if we're a managing bin ourselves, that'd be us
640   if (GST_FLAG_IS_SET (bin, GST_BIN_FLAG_MANAGER)) {
641     manager = GST_ELEMENT (bin);
642     GST_DEBUG (0,"setting manager to self\n");
643   // otherwise, it's what our parent says it is
644   } else {
645     manager = gst_element_get_manager (GST_ELEMENT (bin));
646     if (!manager) {
647       GST_DEBUG (0,"manager not set for element \"%s\" assuming manager is self\n", GST_ELEMENT_NAME (bin));
648       manager = GST_ELEMENT (bin);
649       GST_FLAG_SET (bin, GST_BIN_FLAG_MANAGER);
650     }
651     GST_DEBUG (0,"setting manager to \"%s\"\n", GST_ELEMENT_NAME (manager));
652   }
653   gst_element_set_manager (GST_ELEMENT (bin), manager);
654
655   // perform the first recursive pass of plan generation
656   // we set the manager of every element but those who manage themselves
657   // the need for cothreads is also determined recursively
658   GST_DEBUG (0,"performing first-phase recursion\n");
659   bin->need_cothreads = bin->use_cothreads;
660   if (bin->need_cothreads)
661     GST_DEBUG (0,"requiring cothreads because we're forced to\n");
662
663   elements = bin->children;
664   while (elements) {
665     element = GST_ELEMENT (elements->data);
666     elements = g_list_next (elements);
667 #ifdef GST_DEBUG_ENABLED
668     elementname = GST_ELEMENT_NAME  (element);
669 #endif
670     GST_DEBUG (0,"have element \"%s\"\n",elementname);
671
672     // first set their manager
673     GST_DEBUG (0,"setting manager of \"%s\" to \"%s\"\n",elementname,GST_ELEMENT_NAME (manager));
674     gst_element_set_manager (element, manager);
675
676     // we do recursion and such for Bins
677     if (GST_IS_BIN (element)) {
678       // recurse into the child Bin
679       GST_DEBUG (0,"recursing into child Bin \"%s\" with manager \"%s\"\n",elementname,
680                       GST_ELEMENT_NAME (element->manager));
681       gst_bin_create_plan (GST_BIN (element));
682       GST_DEBUG (0,"after recurse got manager \"%s\"\n",
683                       GST_ELEMENT_NAME (element->manager));
684       // check to see if it needs cothreads and isn't self-managing
685       if (((GST_BIN (element))->need_cothreads) && !GST_FLAG_IS_SET(element,GST_BIN_FLAG_MANAGER)) {
686         GST_DEBUG (0,"requiring cothreads because child bin \"%s\" does\n",elementname);
687         bin->need_cothreads = TRUE;
688       }
689     } else {
690       // then we need to determine whether they need cothreads
691       // if it's a loop-based element, use cothreads
692       if (element->loopfunc != NULL) {
693         GST_DEBUG (0,"requiring cothreads because \"%s\" is a loop-based element\n",elementname);
694         GST_FLAG_SET (element, GST_ELEMENT_USE_COTHREAD);
695       // if it's a 'complex' element, use cothreads
696       } else if (GST_FLAG_IS_SET (element, GST_ELEMENT_COMPLEX)) {
697         GST_DEBUG (0,"requiring cothreads because \"%s\" is complex\n",elementname);
698         GST_FLAG_SET (element, GST_ELEMENT_USE_COTHREAD);
699       // if the element has more than one sink pad, use cothreads
700       } else if (element->numsinkpads > 1) {
701         GST_DEBUG (0,"requiring cothreads because \"%s\" has more than one sink pad\n",elementname);
702         GST_FLAG_SET (element, GST_ELEMENT_USE_COTHREAD);
703       }
704       if (GST_FLAG_IS_SET (element, GST_ELEMENT_USE_COTHREAD))
705         bin->need_cothreads = TRUE;
706     }
707   }
708
709
710   // if we're not a manager thread, we're done.
711   if (!GST_FLAG_IS_SET (bin, GST_BIN_FLAG_MANAGER)) {
712     GST_DEBUG_LEAVE("(\"%s\")",GST_ELEMENT_NAME (bin));
713     return;
714   }
715
716   // clear previous plan state
717   g_list_free (bin->managed_elements);
718   bin->managed_elements = NULL;
719   bin->num_managed_elements = 0;
720
721   // find all the managed children
722   // here we pull off the trick of walking an entire arbitrary tree without recursion
723   GST_DEBUG (0,"attempting to find all the elements to manage\n");
724   pending = g_slist_prepend (pending, bin);
725   do {
726     // retrieve the top of the stack and pop it
727     pending_bin = GST_BIN (pending->data);
728     pending = g_slist_remove (pending, pending_bin);
729
730     // walk the list of elements, find bins, and do stuff
731     GST_DEBUG (0,"checking Bin \"%s\" for managed elements\n",
732           GST_ELEMENT_NAME  (pending_bin));
733     elements = pending_bin->children;
734     while (elements) {
735       element = GST_ELEMENT (elements->data);
736       elements = g_list_next (elements);
737 #ifdef GST_DEBUG_ENABLED
738       elementname = GST_ELEMENT_NAME  (element);
739 #endif
740
741       // if it's ours, add it to the list
742       if (element->manager == GST_ELEMENT(bin)) {
743         // if it's a Bin, add it to the list of Bins to check
744         if (GST_IS_BIN (element)) {
745           GST_DEBUG (0,"flattened recurse into \"%s\"\n",elementname);
746           pending = g_slist_prepend (pending, element);
747
748         // otherwise add it to the list of elements
749         } else {
750           GST_DEBUG (0,"found element \"%s\" that I manage\n",elementname);
751           bin->managed_elements = g_list_prepend (bin->managed_elements, element);
752           bin->num_managed_elements++;
753         }
754       }
755       // else it's not ours and we need to wait for EOS notifications
756       else {
757         gtk_signal_connect (GTK_OBJECT (element), "eos", gst_bin_received_eos, bin);
758         bin->eos_providers = g_list_prepend (bin->eos_providers, element);
759         bin->num_eos_providers++;
760       }
761     }
762   } while (pending);
763
764   GST_DEBUG (0,"have %d elements to manage, implementing plan\n",bin->num_managed_elements);
765
766   gst_bin_schedule(bin);
767
768   g_print ("gstbin \"%s\", eos providers:%d\n",
769                   GST_ELEMENT_NAME (bin),
770                   bin->num_eos_providers);
771
772   GST_DEBUG_LEAVE("(\"%s\")",GST_ELEMENT_NAME (bin));
773 }
774
775 static gboolean
776 gst_bin_iterate_func (GstBin *bin)
777 {
778   GList *chains;
779   _GstBinChain *chain;
780   GList *entries;
781   GstElement *entry;
782   GList *pads;
783   GstPad *pad;
784   GstBuffer *buf = NULL;
785   gint num_scheduled = 0;
786   gboolean eos = FALSE;
787
788   GST_DEBUG_ENTER("(\"%s\")", GST_ELEMENT_NAME (bin));
789
790   g_return_val_if_fail (bin != NULL, TRUE);
791   g_return_val_if_fail (GST_IS_BIN (bin), TRUE);
792   g_return_val_if_fail (GST_STATE (bin) == GST_STATE_PLAYING, TRUE);
793
794   // step through all the chains
795   chains = bin->chains;
796   while (chains) {
797     chain = (_GstBinChain *)(chains->data);
798     chains = g_list_next (chains);
799
800     if (!chain->need_scheduling) continue;
801
802     if (chain->need_cothreads) {
803       // all we really have to do is switch to the first child
804       // FIXME this should be lots more intelligent about where to start
805       GST_DEBUG (0,"starting iteration via cothreads\n");
806
807       entry = GST_ELEMENT (chain->elements->data);
808       GST_FLAG_SET (entry, GST_ELEMENT_COTHREAD_STOPPING);
809       GST_DEBUG (0,"set COTHREAD_STOPPING flag on \"%s\"(@%p)\n",
810             GST_ELEMENT_NAME (entry),entry);
811       cothread_switch (entry->threadstate);
812
813     } else {
814       GST_DEBUG (0,"starting iteration via chain-functions\n");
815
816       entries = chain->entries;
817
818       g_assert (entries != NULL);
819
820       while (entries) {
821         entry = GST_ELEMENT (entries->data);
822         entries = g_list_next (entries);
823
824         GST_DEBUG (0,"have entry \"%s\"\n",GST_ELEMENT_NAME (entry));
825
826         if (GST_IS_BIN (entry)) {
827           gst_bin_iterate (GST_BIN (entry));
828         } else {
829           pads = entry->pads;
830           while (pads) {
831             pad = GST_PAD (pads->data);
832             if (GST_RPAD_DIRECTION(pad) == GST_PAD_SRC) {
833               GST_DEBUG (0,"calling getfunc of %s:%s\n",GST_DEBUG_PAD_NAME(pad));
834               if (GST_REAL_PAD(pad)->getfunc == NULL)
835                 fprintf(stderr, "error, no getfunc in \"%s\"\n", GST_ELEMENT_NAME  (entry));
836               else
837                 buf = (GST_REAL_PAD(pad)->getfunc)(pad);
838               if (buf) gst_pad_push(pad,buf);
839             }
840             pads = g_list_next (pads);
841           }
842         }
843       }
844     }
845     num_scheduled++;
846   }
847
848   // check if nothing was scheduled that was ours..
849   if (!num_scheduled) {
850     // are there any other elements that are still busy?
851     if (bin->num_eos_providers) {
852       GST_LOCK (bin);
853       GST_DEBUG (0,"waiting for eos providers\n");
854       g_cond_wait (bin->eoscond, GST_OBJECT(bin)->lock);
855       GST_DEBUG (0,"num eos providers %d\n", bin->num_eos_providers);
856       GST_UNLOCK (bin);
857     }
858     else {
859       gst_element_signal_eos (GST_ELEMENT (bin));
860       eos = TRUE;
861     }
862   }
863
864   GST_DEBUG_LEAVE("(%s)", GST_ELEMENT_NAME (bin));
865   return !eos;
866 }
867