2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
5 * gstbin.c: GstBin container object and support code
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.
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.
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.
23 //#define GST_DEBUG_ENABLED
24 #include "gst_private.h"
28 #include "gstscheduler.h"
31 GstElementDetails gst_bin_details = {
34 "Simple container object",
36 "Erik Walthinsen <omega@cse.ogi.edu>",
41 static void gst_bin_real_destroy (GtkObject *object);
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,
49 static void gst_bin_create_plan_func (GstBin *bin);
50 static void gst_bin_iterate_func (GstBin *bin);
52 static xmlNodePtr gst_bin_save_thyself (GstElement *element, xmlNodePtr parent);
53 static void gst_bin_restore_thyself (GstElement *element, xmlNodePtr parent,
54 GHashTable *elements);
56 /* Bin signals and args */
68 static void gst_bin_class_init (GstBinClass *klass);
69 static void gst_bin_init (GstBin *bin);
72 static GstElementClass *parent_class = NULL;
73 static guint gst_bin_signals[LAST_SIGNAL] = { 0 };
76 gst_bin_get_type (void)
78 static GtkType bin_type = 0;
81 static const GtkTypeInfo bin_info = {
85 (GtkClassInitFunc)gst_bin_class_init,
86 (GtkObjectInitFunc)gst_bin_init,
89 (GtkClassInitFunc)NULL,
91 bin_type = gtk_type_unique (GST_TYPE_ELEMENT, &bin_info);
97 gst_bin_class_init (GstBinClass *klass)
99 GtkObjectClass *gtkobject_class;
100 GstElementClass *gstelement_class;
102 gtkobject_class = (GtkObjectClass*)klass;
103 gstelement_class = (GstElementClass*)klass;
105 parent_class = gtk_type_class (GST_TYPE_ELEMENT);
107 gst_bin_signals[OBJECT_ADDED] =
108 gtk_signal_new ("object_added", GTK_RUN_FIRST, gtkobject_class->type,
109 GTK_SIGNAL_OFFSET (GstBinClass, object_added),
110 gtk_marshal_NONE__POINTER, GTK_TYPE_NONE, 1,
112 gtk_object_class_add_signals (gtkobject_class, gst_bin_signals, LAST_SIGNAL);
114 klass->change_state_type = gst_bin_change_state_type;
115 klass->create_plan = gst_bin_create_plan_func;
116 klass->schedule = gst_bin_schedule_func;
117 klass->iterate = gst_bin_iterate_func;
119 gstelement_class->change_state = gst_bin_change_state;
120 gstelement_class->save_thyself = gst_bin_save_thyself;
121 gstelement_class->restore_thyself = gst_bin_restore_thyself;
123 gtkobject_class->destroy = gst_bin_real_destroy;
127 gst_bin_init (GstBin *bin)
129 // in general, we prefer to use cothreads for most things
130 GST_FLAG_SET (bin, GST_BIN_FLAG_PREFER_COTHREADS);
132 bin->numchildren = 0;
133 bin->children = NULL;
135 // FIXME temporary testing measure
136 // bin->use_cothreads = TRUE;
141 * @name: name of new bin
143 * Create a new bin with given name.
148 gst_bin_new (gchar *name)
150 return gst_elementfactory_make ("bin", name);
155 * @bin: #GstBin to add element to
156 * @element: #GstElement to add to bin
158 * Add the given element to the bin. Set the elements parent, and thus
162 gst_bin_add (GstBin *bin,
165 g_return_if_fail (bin != NULL);
166 g_return_if_fail (GST_IS_BIN (bin));
167 g_return_if_fail (element != NULL);
168 g_return_if_fail (GST_IS_ELEMENT (element));
170 // must be NULL or PAUSED state in order to modify bin
171 g_return_if_fail ((GST_STATE (bin) == GST_STATE_NULL) ||
172 (GST_STATE (bin) == GST_STATE_PAUSED));
174 bin->children = g_list_append (bin->children, element);
176 gst_object_set_parent (GST_OBJECT (element), GST_OBJECT (bin));
178 /* we know we have at least one child, we just added one... */
179 // if (GST_STATE(element) < GST_STATE_READY)
180 // gst_bin_change_state_norecurse(bin,GST_STATE_READY);
182 gtk_signal_emit (GTK_OBJECT (bin), gst_bin_signals[OBJECT_ADDED], element);
187 * @bin: #GstBin to remove element from
188 * @element: #GstElement to remove
190 * Remove the element from its associated bin, unparenting as well.
193 gst_bin_remove (GstBin *bin,
196 g_return_if_fail (bin != NULL);
197 g_return_if_fail (GST_IS_BIN (bin));
198 g_return_if_fail (element != NULL);
199 g_return_if_fail (GST_IS_ELEMENT (element));
200 g_return_if_fail (bin->children != NULL);
202 // must be NULL or PAUSED state in order to modify bin
203 g_return_if_fail ((GST_STATE (bin) == GST_STATE_NULL) ||
204 (GST_STATE (bin) == GST_STATE_PAUSED));
206 gst_object_unparent (GST_OBJECT (element));
207 bin->children = g_list_remove (bin->children, element);
210 /* if we're down to zero children, force state to NULL */
211 if (bin->numchildren == 0)
212 gst_element_set_state (GST_ELEMENT (bin), GST_STATE_NULL);
216 static GstElementStateReturn
217 gst_bin_change_state (GstElement *element)
223 DEBUG_ENTER("(\"%s\")",gst_element_get_name (element));
225 g_return_val_if_fail (GST_IS_BIN (element), GST_STATE_FAILURE);
227 bin = GST_BIN (element);
229 DEBUG("currently %d(%s), %d(%s) pending\n", GST_STATE (element),
230 _gst_print_statename (GST_STATE (element)), GST_STATE_PENDING (element),
231 _gst_print_statename (GST_STATE_PENDING (element)));
233 // g_return_val_if_fail(bin->numchildren != 0, GST_STATE_FAILURE);
236 children = bin->children;
238 child = GST_ELEMENT (children->data);
239 DEBUG("setting state on '%s'\n",gst_element_get_name (child));
240 switch (gst_element_set_state (child, GST_STATE_PENDING (element))) {
241 case GST_STATE_FAILURE:
242 GST_STATE_PENDING (element) = GST_STATE_NONE_PENDING;
243 DEBUG("child '%s' failed to go to state %d(%s)\n", gst_element_get_name (child),
244 GST_STATE_PENDING (element), _gst_print_statename (GST_STATE_PENDING (element)));
245 return GST_STATE_FAILURE;
247 case GST_STATE_ASYNC:
248 DEBUG("child '%s' is changing state asynchronously\n", gst_element_get_name (child));
252 children = g_list_next (children);
254 // g_print("<-- \"%s\"\n",gst_object_get_name(GST_OBJECT(bin)));
256 switch (GST_STATE_TRANSITION (element)) {
257 case GST_STATE_NULL_TO_READY:
261 parent = gst_object_get_parent (GST_OBJECT (element));
263 if (!parent || !GST_IS_BIN (parent))
264 gst_bin_create_plan (bin);
272 return gst_bin_change_state_norecurse (bin);
276 static GstElementStateReturn
277 gst_bin_change_state_norecurse (GstBin *bin)
280 if (GST_ELEMENT_CLASS (parent_class)->change_state)
281 return GST_ELEMENT_CLASS (parent_class)->change_state (GST_ELEMENT (bin));
283 return GST_STATE_FAILURE;
287 gst_bin_change_state_type(GstBin *bin,
288 GstElementState state,
294 // g_print("gst_bin_change_state_type(\"%s\",%d,%d);\n",
295 // gst_object_get_name(GST_OBJECT(bin)),state,type);
297 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
298 g_return_val_if_fail (bin->numchildren != 0, FALSE);
301 children = bin->children;
303 child = GST_ELEMENT (children->data);
304 if (GST_IS_BIN (child)) {
305 if (!gst_bin_set_state_type (GST_BIN (child), state,type))
307 } else if (GTK_CHECK_TYPE (child,type)) {
308 if (!gst_element_set_state (child,state))
312 children = g_list_next (children);
314 if (type == GST_TYPE_BIN)
315 gst_element_set_state (GST_ELEMENT (bin),state);
321 * gst_bin_set_state_type:
322 * @bin: #GstBin to set the state
323 * @state: the new state to set the elements to
324 * @type: the type of elements to change
326 * Sets the state of only those objects of the given type.
328 * Returns: indication if the state change was successfull
331 gst_bin_set_state_type (GstBin *bin,
332 GstElementState state,
337 DEBUG("gst_bin_set_state_type(\"%s\",%d,%d)\n",
338 gst_element_get_name (GST_ELEMENT (bin)), state,type);
340 g_return_val_if_fail (bin != NULL, FALSE);
341 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
343 oclass = GST_BIN_CLASS (GTK_OBJECT (bin)->klass);
345 if (oclass->change_state_type)
346 (oclass->change_state_type) (bin,state,type);
351 gst_bin_real_destroy (GtkObject *object)
353 GstBin *bin = GST_BIN (object);
357 DEBUG("in gst_bin_real_destroy()\n");
359 children = bin->children;
361 child = GST_ELEMENT (children->data);
362 gst_element_destroy (child);
363 children = g_list_next (children);
366 g_list_free (bin->children);
370 * gst_bin_get_by_name:
371 * @bin: #Gstbin to search
372 * @name: the element name to search for
374 * get the element with the given name from this bin
376 * Returns: the element with the given name
379 gst_bin_get_by_name (GstBin *bin,
385 g_return_val_if_fail (bin != NULL, NULL);
386 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
387 g_return_val_if_fail (name != NULL, NULL);
389 g_print("gstbin: lookup element \"%s\" in \"%s\"\n", name,
390 gst_element_get_name (GST_ELEMENT (bin)));
392 children = bin->children;
394 child = GST_ELEMENT (children->data);
395 if (!strcmp (child->name,name))
397 if (GST_IS_BIN (child)) {
398 GstElement *res = gst_bin_get_by_name (GST_BIN (child), name);
402 children = g_list_next (children);
410 * @bin: #Gstbin to get the list from
412 * get the list of elements in this bin
414 * Returns: a GList of elements
417 gst_bin_get_list (GstBin *bin)
419 g_return_val_if_fail (bin != NULL, NULL);
420 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
422 return bin->children;
426 gst_bin_save_thyself (GstElement *element,
429 GstBin *bin = GST_BIN (element);
430 xmlNodePtr childlist;
434 if (GST_ELEMENT_CLASS (parent_class)->save_thyself)
435 GST_ELEMENT_CLASS (parent_class)->save_thyself (GST_ELEMENT (bin), parent);
437 childlist = xmlNewChild (parent,NULL,"children",NULL);
439 children = bin->children;
441 child = GST_ELEMENT (children->data);
442 gst_element_save_thyself (child, childlist);
443 children = g_list_next (children);
449 gst_bin_restore_thyself (GstElement *element,
451 GHashTable *elements)
453 GstBin *bin = GST_BIN (element);
454 xmlNodePtr field = parent->childs;
455 xmlNodePtr childlist;
457 g_print("gstbin: restore \"%s\"\n", gst_element_get_name (element));
460 if (!strcmp (field->name, "children")) {
461 childlist = field->childs;
463 if (!strcmp (childlist->name, "element")) {
464 GstElement *element = gst_element_load_thyself (childlist, elements);
466 gst_bin_add (bin, element);
468 childlist = childlist->next;
478 gst_bin_use_cothreads (GstBin *bin,
481 g_return_if_fail (GST_IS_BIN (bin));
483 bin->use_cothreads = enabled;
488 * @bin: #Gstbin to iterate
490 * iterates over the elements in this bin
493 gst_bin_iterate (GstBin *bin)
497 DEBUG_ENTER("(\"%s\")",gst_element_get_name(GST_ELEMENT(bin)));
499 oclass = GST_BIN_CLASS (GTK_OBJECT (bin)->klass);
502 (oclass->iterate) (bin);
504 DEBUG_LEAVE("(\"%s\")",gst_element_get_name(GST_ELEMENT(bin)));
508 * gst_bin_create_plan:
509 * @bin: #GstBin to create the plan for
511 * let the bin figure out how to handle the plugins in it.
514 gst_bin_create_plan (GstBin *bin)
518 oclass = GST_BIN_CLASS (GTK_OBJECT (bin)->klass);
520 if (oclass->create_plan)
521 (oclass->create_plan) (bin);
526 * @bin: #GstBin to schedule
528 * let the bin figure out how to handle the plugins in it.
531 gst_bin_schedule (GstBin *bin)
535 oclass = GST_BIN_CLASS (GTK_OBJECT (bin)->klass);
537 if (oclass->schedule)
538 (oclass->schedule) (bin);
548 gst_bin_create_plan_func (GstBin *bin)
553 const gchar *elementname;
554 GSList *pending = NULL;
557 DEBUG_SET_STRING("(\"%s\")",gst_element_get_name (GST_ELEMENT (bin)));
560 // first figure out which element is the manager of this and all child elements
561 // if we're a managing bin ourselves, that'd be us
562 if (GST_FLAG_IS_SET (bin, GST_BIN_FLAG_MANAGER)) {
563 manager = GST_ELEMENT (bin);
564 DEBUG("setting manager to self\n");
565 // otherwise, it's what our parent says it is
567 manager = gst_element_get_manager (GST_ELEMENT (bin));
569 DEBUG("manager not set for element \"%s\" assuming manager is self\n", gst_element_get_name (GST_ELEMENT (bin)));
570 manager = GST_ELEMENT (bin);
571 GST_FLAG_SET (bin, GST_BIN_FLAG_MANAGER);
573 DEBUG("setting manager to \"%s\"\n", gst_element_get_name (manager));
576 // perform the first recursive pass of plan generation
577 // we set the manager of every element but those who manage themselves
578 // the need for cothreads is also determined recursively
579 DEBUG("performing first-phase recursion\n");
580 bin->need_cothreads = bin->use_cothreads;
581 if (bin->need_cothreads)
582 DEBUG("requiring cothreads because we're forced to\n");
584 elements = bin->children;
586 element = GST_ELEMENT (elements->data);
587 elements = g_list_next (elements);
588 #ifdef GST_DEBUG_ENABLED
589 elementname = gst_element_get_name (element);
591 DEBUG("have element \"%s\"\n",elementname);
593 // first set their manager
594 DEBUG("setting manager of \"%s\" to \"%s\"\n",elementname,gst_element_get_name(manager));
595 gst_element_set_manager (element, manager);
597 // we do recursion and such for Bins
598 if (GST_IS_BIN (element)) {
599 // recurse into the child Bin
600 DEBUG("recursing into child Bin \"%s\"\n",elementname);
601 gst_bin_create_plan (GST_BIN (element));
602 // check to see if it needs cothreads and isn't self-managing
603 if (((GST_BIN (element))->need_cothreads) && !GST_FLAG_IS_SET(element,GST_BIN_FLAG_MANAGER)) {
604 DEBUG("requiring cothreads because child bin \"%s\" does\n",elementname);
605 bin->need_cothreads = TRUE;
608 // then we need to determine whether they need cothreads
609 // if it's a loop-based element, use cothreads
610 if (element->loopfunc != NULL) {
611 DEBUG("requiring cothreads because \"%s\" is a loop-based element\n",elementname);
612 GST_FLAG_SET (element, GST_ELEMENT_USE_COTHREAD);
613 // if it's a 'complex' element, use cothreads
614 } else if (GST_FLAG_IS_SET (element, GST_ELEMENT_COMPLEX)) {
615 DEBUG("requiring cothreads because \"%s\" is complex\n",elementname);
616 GST_FLAG_SET (element, GST_ELEMENT_USE_COTHREAD);
617 // if the element has more than one sink pad, use cothreads
618 } else if (element->numsinkpads > 1) {
619 DEBUG("requiring cothreads because \"%s\" has more than one sink pad\n",elementname);
620 GST_FLAG_SET (element, GST_ELEMENT_USE_COTHREAD);
622 if (GST_FLAG_IS_SET (element, GST_ELEMENT_USE_COTHREAD))
623 bin->need_cothreads = TRUE;
628 // if we're not a manager thread, we're done.
629 if (!GST_FLAG_IS_SET (bin, GST_BIN_FLAG_MANAGER)) {
630 DEBUG_LEAVE("(\"%s\")",gst_element_get_name(GST_ELEMENT(bin)));
634 // clear previous plan state
635 g_list_free (bin->managed_elements);
636 bin->managed_elements = NULL;
637 bin->num_managed_elements = 0;
639 // find all the managed children
640 // here we pull off the trick of walking an entire arbitrary tree without recursion
641 DEBUG("attempting to find all the elements to manage\n");
642 pending = g_slist_prepend (pending, bin);
644 // retrieve the top of the stack and pop it
645 pending_bin = GST_BIN (pending->data);
646 pending = g_slist_remove (pending, pending_bin);
648 // walk the list of elements, find bins, and do stuff
649 DEBUG("checking Bin \"%s\" for managed elements\n",
650 gst_element_get_name (GST_ELEMENT (pending_bin)));
651 elements = pending_bin->children;
653 element = GST_ELEMENT (elements->data);
654 elements = g_list_next (elements);
655 #ifdef GST_DEBUG_ENABLED
656 elementname = gst_element_get_name (element);
659 // if it's ours, add it to the list
660 if (element->manager == GST_ELEMENT(bin)) {
661 // if it's a Bin, add it to the list of Bins to check
662 if (GST_IS_BIN (element)) {
663 DEBUG("flattened recurse into \"%s\"\n",elementname);
664 pending = g_slist_prepend (pending, element);
665 // otherwise add it to the list of elements
667 DEBUG("found element \"%s\" that I manage\n",elementname);
668 bin->managed_elements = g_list_prepend (bin->managed_elements, element);
669 bin->num_managed_elements++;
675 DEBUG("have %d elements to manage, implementing plan\n",bin->num_managed_elements);
677 gst_bin_schedule(bin);
679 DEBUG_LEAVE("(\"%s\")",gst_element_get_name(GST_ELEMENT(bin)));
683 gst_bin_iterate_func (GstBin *bin)
693 DEBUG_SET_STRING("(\"%s\")", gst_element_get_name (GST_ELEMENT (bin)));
696 g_return_if_fail (bin != NULL);
697 g_return_if_fail (GST_IS_BIN (bin));
698 g_return_if_fail (GST_STATE (bin) == GST_STATE_PLAYING);
700 // step through all the chains
701 chains = bin->chains;
703 chain = (_GstBinChain *)(chains->data);
704 chains = g_list_next (chains);
706 if (chain->need_cothreads) {
707 // all we really have to do is switch to the first child
708 // FIXME this should be lots more intelligent about where to start
709 DEBUG("starting iteration via cothreads\n");
711 entry = GST_ELEMENT (chain->elements->data);
712 GST_FLAG_SET (entry, GST_ELEMENT_COTHREAD_STOPPING);
713 DEBUG("set COTHREAD_STOPPING flag on \"%s\"(@%p)\n",
714 gst_element_get_name(entry),entry);
715 cothread_switch (entry->threadstate);
718 DEBUG("starting iteration via chain-functions\n");
720 entries = chain->entries;
722 g_assert (entries != NULL);
725 entry = GST_ELEMENT (entries->data);
726 entries = g_list_next (entries);
728 DEBUG("have entry \"%s\"\n",gst_element_get_name(entry));
730 if (GST_IS_BIN (entry)) {
731 gst_bin_iterate (GST_BIN (entry));
735 pad = GST_PAD (pads->data);
736 if (pad->direction == GST_PAD_SRC) {
737 DEBUG("calling getfunc of %s:%s\n",GST_DEBUG_PAD_NAME(pad));
738 if (pad->getfunc == NULL)
739 fprintf(stderr, "error, no getfunc in \"%s\"\n", gst_element_get_name (entry));
741 buf = (pad->getfunc)(pad);
742 gst_pad_push(pad,buf);
744 pads = g_list_next (pads);
751 DEBUG_LEAVE("(%s)", gst_element_get_name (GST_ELEMENT (bin)));