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
25 #include "gst_private.h"
29 #include "gstscheduler.h"
33 GstElementDetails gst_bin_details = {
36 "Simple container object",
38 "Erik Walthinsen <omega@cse.ogi.edu>",
43 static void gst_bin_real_destroy (GtkObject *object);
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,
51 static void gst_bin_create_plan_func (GstBin *bin);
52 static gboolean gst_bin_iterate_func (GstBin *bin);
54 static xmlNodePtr gst_bin_save_thyself (GstElement *element, xmlNodePtr parent);
55 static void gst_bin_restore_thyself (GstElement *element, xmlNodePtr parent,
56 GHashTable *elements);
58 /* Bin signals and args */
70 static void gst_bin_class_init (GstBinClass *klass);
71 static void gst_bin_init (GstBin *bin);
74 static GstElementClass *parent_class = NULL;
75 static guint gst_bin_signals[LAST_SIGNAL] = { 0 };
78 gst_bin_get_type (void)
80 static GtkType bin_type = 0;
83 static const GtkTypeInfo bin_info = {
87 (GtkClassInitFunc)gst_bin_class_init,
88 (GtkObjectInitFunc)gst_bin_init,
91 (GtkClassInitFunc)NULL,
93 bin_type = gtk_type_unique (GST_TYPE_ELEMENT, &bin_info);
99 gst_bin_class_init (GstBinClass *klass)
101 GtkObjectClass *gtkobject_class;
102 GstElementClass *gstelement_class;
104 gtkobject_class = (GtkObjectClass*)klass;
105 gstelement_class = (GstElementClass*)klass;
107 parent_class = gtk_type_class (GST_TYPE_ELEMENT);
109 gst_bin_signals[OBJECT_ADDED] =
110 gtk_signal_new ("object_added", GTK_RUN_FIRST, gtkobject_class->type,
111 GTK_SIGNAL_OFFSET (GstBinClass, object_added),
112 gtk_marshal_NONE__POINTER, GTK_TYPE_NONE, 1,
114 gtk_object_class_add_signals (gtkobject_class, gst_bin_signals, LAST_SIGNAL);
116 klass->change_state_type = gst_bin_change_state_type;
117 klass->create_plan = gst_bin_create_plan_func;
118 klass->schedule = gst_bin_schedule_func;
119 klass->iterate = gst_bin_iterate_func;
121 gstelement_class->change_state = gst_bin_change_state;
122 gstelement_class->save_thyself = gst_bin_save_thyself;
123 gstelement_class->restore_thyself = gst_bin_restore_thyself;
125 gtkobject_class->destroy = gst_bin_real_destroy;
129 gst_bin_init (GstBin *bin)
131 // in general, we prefer to use cothreads for most things
132 GST_FLAG_SET (bin, GST_BIN_FLAG_PREFER_COTHREADS);
134 bin->numchildren = 0;
135 bin->children = NULL;
136 bin->eos_providers = NULL;
137 bin->num_eos_providers = 0;
139 bin->eoscond = g_cond_new ();
140 // FIXME temporary testing measure
141 // bin->use_cothreads = TRUE;
146 * @name: name of new bin
148 * Create a new bin with given name.
153 gst_bin_new (const gchar *name)
155 return gst_elementfactory_make ("bin", name);
160 * @bin: #GstBin to add element to
161 * @element: #GstElement to add to bin
163 * Add the given element to the bin. Set the elements parent, and thus
167 gst_bin_add (GstBin *bin,
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));
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));
179 bin->children = g_list_append (bin->children, element);
181 gst_object_set_parent (GST_OBJECT (element), GST_OBJECT (bin));
183 GST_INFO_ELEMENT (GST_CAT_PARENTAGE, bin, "added child %s", gst_element_get_name(element));
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);
189 gtk_signal_emit (GTK_OBJECT (bin), gst_bin_signals[OBJECT_ADDED], element);
194 * @bin: #GstBin to remove element from
195 * @element: #GstElement to remove
197 * Remove the element from its associated bin, unparenting as well.
200 gst_bin_remove (GstBin *bin,
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);
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));
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");
219 gst_object_unparent (GST_OBJECT (element));
220 bin->children = g_list_remove (bin->children, element);
223 GST_INFO_ELEMENT (GST_CAT_PARENTAGE, bin, "removed child %s", gst_element_get_name(element));
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);
231 static GstElementStateReturn
232 gst_bin_change_state (GstElement *element)
238 GST_DEBUG_ENTER("(\"%s\")",gst_element_get_name (element));
240 g_return_val_if_fail (GST_IS_BIN (element), GST_STATE_FAILURE);
242 bin = GST_BIN (element);
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)));
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)));
252 // g_return_val_if_fail(bin->numchildren != 0, GST_STATE_FAILURE);
254 switch (GST_STATE_TRANSITION (element)) {
255 case GST_STATE_NULL_TO_READY:
259 parent = gst_object_get_parent (GST_OBJECT (element));
261 if (!parent || !GST_IS_BIN (parent))
262 gst_bin_create_plan (bin);
271 children = bin->children;
273 child = GST_ELEMENT (children->data);
274 GST_DEBUG (0,"setting state on '%s'\n",gst_element_get_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_get_name (child),
279 GST_STATE_PENDING (element), _gst_print_statename (GST_STATE_PENDING (element)));
280 return GST_STATE_FAILURE;
282 case GST_STATE_ASYNC:
283 GST_DEBUG (0,"child '%s' is changing state asynchronously\n", gst_element_get_name (child));
287 children = g_list_next (children);
289 // g_print("<-- \"%s\"\n",gst_object_get_name(GST_OBJECT(bin)));
292 return gst_bin_change_state_norecurse (bin);
296 static GstElementStateReturn
297 gst_bin_change_state_norecurse (GstBin *bin)
300 if (GST_ELEMENT_CLASS (parent_class)->change_state)
301 return GST_ELEMENT_CLASS (parent_class)->change_state (GST_ELEMENT (bin));
303 return GST_STATE_FAILURE;
307 gst_bin_change_state_type(GstBin *bin,
308 GstElementState state,
314 // g_print("gst_bin_change_state_type(\"%s\",%d,%d);\n",
315 // gst_object_get_name(GST_OBJECT(bin)),state,type);
317 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
318 g_return_val_if_fail (bin->numchildren != 0, FALSE);
321 children = bin->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))
327 } else if (GTK_CHECK_TYPE (child,type)) {
328 if (!gst_element_set_state (child,state))
332 children = g_list_next (children);
334 if (type == GST_TYPE_BIN)
335 gst_element_set_state (GST_ELEMENT (bin),state);
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
346 * Sets the state of only those objects of the given type.
348 * Returns: indication if the state change was successfull
351 gst_bin_set_state_type (GstBin *bin,
352 GstElementState state,
357 GST_DEBUG (0,"gst_bin_set_state_type(\"%s\",%d,%d)\n",
358 gst_element_get_name (GST_ELEMENT (bin)), state,type);
360 g_return_val_if_fail (bin != NULL, FALSE);
361 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
363 oclass = GST_BIN_CLASS (GTK_OBJECT (bin)->klass);
365 if (oclass->change_state_type)
366 (oclass->change_state_type) (bin,state,type);
371 gst_bin_real_destroy (GtkObject *object)
373 GstBin *bin = GST_BIN (object);
377 GST_DEBUG (0,"in gst_bin_real_destroy()\n");
379 children = bin->children;
381 child = GST_ELEMENT (children->data);
382 gst_element_destroy (child);
383 children = g_list_next (children);
386 g_list_free (bin->children);
390 * gst_bin_get_by_name:
391 * @bin: #Gstbin to search
392 * @name: the element name to search for
394 * Get the element with the given name from this bin.
396 * Returns: the element with the given name
399 gst_bin_get_by_name (GstBin *bin,
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);
409 GST_INFO_ELEMENT (GST_CAT_PARENTAGE, bin, "looking up child element %s", name);
411 children = bin->children;
413 child = GST_ELEMENT (children->data);
414 if (!strcmp (child->name,name))
416 if (GST_IS_BIN (child)) {
417 GstElement *res = gst_bin_get_by_name (GST_BIN (child), name);
421 children = g_list_next (children);
429 * @bin: #Gstbin to get the list from
431 * Get the list of elements in this bin.
433 * Returns: a GList of elements
436 gst_bin_get_list (GstBin *bin)
438 g_return_val_if_fail (bin != NULL, NULL);
439 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
441 return bin->children;
445 gst_bin_save_thyself (GstElement *element,
448 GstBin *bin = GST_BIN (element);
449 xmlNodePtr childlist;
453 if (GST_ELEMENT_CLASS (parent_class)->save_thyself)
454 GST_ELEMENT_CLASS (parent_class)->save_thyself (GST_ELEMENT (bin), parent);
456 childlist = xmlNewChild (parent,NULL,"children",NULL);
458 GST_INFO_ELEMENT (GST_CAT_XML, bin, "saving %d children", bin->numchildren);
460 children = bin->children;
462 child = GST_ELEMENT (children->data);
463 gst_element_save_thyself (child, childlist);
464 children = g_list_next (children);
470 gst_bin_restore_thyself (GstElement *element,
472 GHashTable *elements)
474 GstBin *bin = GST_BIN (element);
475 xmlNodePtr field = parent->xmlChildrenNode;
476 xmlNodePtr childlist;
478 // g_print("gstbin: restore \"%s\"\n", gst_element_get_name (element));
481 if (!strcmp (field->name, "children")) {
482 GST_INFO_ELEMENT (GST_CAT_XML, element, "loading children");
483 childlist = field->xmlChildrenNode;
485 if (!strcmp (childlist->name, "element")) {
486 GstElement *element = gst_element_load_thyself (childlist, elements);
488 gst_bin_add (bin, element);
490 childlist = childlist->next;
500 gst_bin_use_cothreads (GstBin *bin,
503 g_return_if_fail (GST_IS_BIN (bin));
505 bin->use_cothreads = enabled;
510 * @bin: #Gstbin to iterate
512 * Iterates over the elements in this bin.
514 * Returns: TRUE if the bin did something usefull. This value
515 * can be used to determine it the bin is in EOS.
518 gst_bin_iterate (GstBin *bin)
523 GST_DEBUG_ENTER("(\"%s\")",gst_element_get_name(GST_ELEMENT(bin)));
525 oclass = GST_BIN_CLASS (GTK_OBJECT (bin)->klass);
528 eos = (oclass->iterate) (bin);
530 GST_DEBUG_LEAVE("(\"%s\")",gst_element_get_name(GST_ELEMENT(bin)));
536 * gst_bin_create_plan:
537 * @bin: #GstBin to create the plan for
539 * Let the bin figure out how to handle its children.
542 gst_bin_create_plan (GstBin *bin)
546 oclass = GST_BIN_CLASS (GTK_OBJECT (bin)->klass);
548 if (oclass->create_plan)
549 (oclass->create_plan) (bin);
552 /* out internal element fired EOS, we decrement the number of pending EOS childs */
554 gst_bin_received_eos (GstElement *element, GstBin *bin)
556 GST_INFO_ELEMENT (GST_CAT_PLANNING, bin, "child %s fired eos, pending %d\n", gst_element_get_name (element),
557 bin->num_eos_providers);
560 if (bin->num_eos_providers) {
561 bin->num_eos_providers--;
562 g_cond_signal (bin->eoscond);
569 * @bin: #GstBin to schedule
571 * Let the bin figure out how to handle its children.
574 gst_bin_schedule (GstBin *bin)
578 oclass = GST_BIN_CLASS (GTK_OBJECT (bin)->klass);
580 if (oclass->schedule)
581 (oclass->schedule) (bin);
591 gst_bin_create_plan_func (GstBin *bin)
596 #ifdef GST_DEBUG_ENABLED
597 const gchar *elementname;
599 GSList *pending = NULL;
602 GST_DEBUG_ENTER("(\"%s\")",gst_element_get_name (GST_ELEMENT (bin)));
604 GST_INFO_ELEMENT (GST_CAT_PLANNING, bin, "creating plan");
606 // first figure out which element is the manager of this and all child elements
607 // if we're a managing bin ourselves, that'd be us
608 if (GST_FLAG_IS_SET (bin, GST_BIN_FLAG_MANAGER)) {
609 manager = GST_ELEMENT (bin);
610 GST_DEBUG (0,"setting manager to self\n");
611 // otherwise, it's what our parent says it is
613 manager = gst_element_get_manager (GST_ELEMENT (bin));
615 GST_DEBUG (0,"manager not set for element \"%s\" assuming manager is self\n", gst_element_get_name (GST_ELEMENT (bin)));
616 manager = GST_ELEMENT (bin);
617 GST_FLAG_SET (bin, GST_BIN_FLAG_MANAGER);
619 GST_DEBUG (0,"setting manager to \"%s\"\n", gst_element_get_name (manager));
621 gst_element_set_manager (GST_ELEMENT (bin), manager);
623 // perform the first recursive pass of plan generation
624 // we set the manager of every element but those who manage themselves
625 // the need for cothreads is also determined recursively
626 GST_DEBUG (0,"performing first-phase recursion\n");
627 bin->need_cothreads = bin->use_cothreads;
628 if (bin->need_cothreads)
629 GST_DEBUG (0,"requiring cothreads because we're forced to\n");
631 elements = bin->children;
633 element = GST_ELEMENT (elements->data);
634 elements = g_list_next (elements);
635 #ifdef GST_DEBUG_ENABLED
636 elementname = gst_element_get_name (element);
638 GST_DEBUG (0,"have element \"%s\"\n",elementname);
640 // first set their manager
641 GST_DEBUG (0,"setting manager of \"%s\" to \"%s\"\n",elementname,gst_element_get_name(manager));
642 gst_element_set_manager (element, manager);
644 // we do recursion and such for Bins
645 if (GST_IS_BIN (element)) {
646 // recurse into the child Bin
647 GST_DEBUG (0,"recursing into child Bin \"%s\" with manager \"%s\"\n",elementname,
648 gst_element_get_name(element->manager));
649 gst_bin_create_plan (GST_BIN (element));
650 GST_DEBUG (0,"after recurse got manager \"%s\"\n",
651 gst_element_get_name(element->manager));
652 // check to see if it needs cothreads and isn't self-managing
653 if (((GST_BIN (element))->need_cothreads) && !GST_FLAG_IS_SET(element,GST_BIN_FLAG_MANAGER)) {
654 GST_DEBUG (0,"requiring cothreads because child bin \"%s\" does\n",elementname);
655 bin->need_cothreads = TRUE;
658 // then we need to determine whether they need cothreads
659 // if it's a loop-based element, use cothreads
660 if (element->loopfunc != NULL) {
661 GST_DEBUG (0,"requiring cothreads because \"%s\" is a loop-based element\n",elementname);
662 GST_FLAG_SET (element, GST_ELEMENT_USE_COTHREAD);
663 // if it's a 'complex' element, use cothreads
664 } else if (GST_FLAG_IS_SET (element, GST_ELEMENT_COMPLEX)) {
665 GST_DEBUG (0,"requiring cothreads because \"%s\" is complex\n",elementname);
666 GST_FLAG_SET (element, GST_ELEMENT_USE_COTHREAD);
667 // if the element has more than one sink pad, use cothreads
668 } else if (element->numsinkpads > 1) {
669 GST_DEBUG (0,"requiring cothreads because \"%s\" has more than one sink pad\n",elementname);
670 GST_FLAG_SET (element, GST_ELEMENT_USE_COTHREAD);
672 if (GST_FLAG_IS_SET (element, GST_ELEMENT_USE_COTHREAD))
673 bin->need_cothreads = TRUE;
678 // if we're not a manager thread, we're done.
679 if (!GST_FLAG_IS_SET (bin, GST_BIN_FLAG_MANAGER)) {
680 GST_DEBUG_LEAVE("(\"%s\")",gst_element_get_name(GST_ELEMENT(bin)));
684 // clear previous plan state
685 g_list_free (bin->managed_elements);
686 bin->managed_elements = NULL;
687 bin->num_managed_elements = 0;
689 // find all the managed children
690 // here we pull off the trick of walking an entire arbitrary tree without recursion
691 GST_DEBUG (0,"attempting to find all the elements to manage\n");
692 pending = g_slist_prepend (pending, bin);
694 // retrieve the top of the stack and pop it
695 pending_bin = GST_BIN (pending->data);
696 pending = g_slist_remove (pending, pending_bin);
698 // walk the list of elements, find bins, and do stuff
699 GST_DEBUG (0,"checking Bin \"%s\" for managed elements\n",
700 gst_element_get_name (GST_ELEMENT (pending_bin)));
701 elements = pending_bin->children;
703 element = GST_ELEMENT (elements->data);
704 elements = g_list_next (elements);
705 #ifdef GST_DEBUG_ENABLED
706 elementname = gst_element_get_name (element);
709 // if it's ours, add it to the list
710 if (element->manager == GST_ELEMENT(bin)) {
711 // if it's a Bin, add it to the list of Bins to check
712 if (GST_IS_BIN (element)) {
713 GST_DEBUG (0,"flattened recurse into \"%s\"\n",elementname);
714 pending = g_slist_prepend (pending, element);
716 // otherwise add it to the list of elements
718 GST_DEBUG (0,"found element \"%s\" that I manage\n",elementname);
719 bin->managed_elements = g_list_prepend (bin->managed_elements, element);
720 bin->num_managed_elements++;
723 // else it's not ours and we need to wait for EOS notifications
725 gtk_signal_connect (GTK_OBJECT (element), "eos", gst_bin_received_eos, bin);
726 bin->eos_providers = g_list_prepend (bin->eos_providers, element);
727 bin->num_eos_providers++;
732 GST_DEBUG (0,"have %d elements to manage, implementing plan\n",bin->num_managed_elements);
734 gst_bin_schedule(bin);
736 g_print ("gstbin \"%s\", eos providers:%d\n",
737 gst_element_get_name (GST_ELEMENT (bin)),
738 bin->num_eos_providers);
740 GST_DEBUG_LEAVE("(\"%s\")",gst_element_get_name(GST_ELEMENT(bin)));
744 gst_bin_iterate_func (GstBin *bin)
752 GstBuffer *buf = NULL;
753 gint num_scheduled = 0;
754 gboolean eos = FALSE;
756 GST_DEBUG_ENTER("(\"%s\")", gst_element_get_name (GST_ELEMENT (bin)));
758 g_return_val_if_fail (bin != NULL, TRUE);
759 g_return_val_if_fail (GST_IS_BIN (bin), TRUE);
760 g_return_val_if_fail (GST_STATE (bin) == GST_STATE_PLAYING, TRUE);
762 // step through all the chains
763 chains = bin->chains;
765 chain = (_GstBinChain *)(chains->data);
766 chains = g_list_next (chains);
768 if (!chain->need_scheduling) continue;
770 if (chain->need_cothreads) {
771 // all we really have to do is switch to the first child
772 // FIXME this should be lots more intelligent about where to start
773 GST_DEBUG (0,"starting iteration via cothreads\n");
775 entry = GST_ELEMENT (chain->elements->data);
776 GST_FLAG_SET (entry, GST_ELEMENT_COTHREAD_STOPPING);
777 GST_DEBUG (0,"set COTHREAD_STOPPING flag on \"%s\"(@%p)\n",
778 gst_element_get_name(entry),entry);
779 cothread_switch (entry->threadstate);
782 GST_DEBUG (0,"starting iteration via chain-functions\n");
784 entries = chain->entries;
786 g_assert (entries != NULL);
789 entry = GST_ELEMENT (entries->data);
790 entries = g_list_next (entries);
792 GST_DEBUG (0,"have entry \"%s\"\n",gst_element_get_name(entry));
794 if (GST_IS_BIN (entry)) {
795 gst_bin_iterate (GST_BIN (entry));
799 pad = GST_PAD (pads->data);
800 if (GST_RPAD_DIRECTION(pad) == GST_PAD_SRC) {
801 GST_DEBUG (0,"calling getfunc of %s:%s\n",GST_DEBUG_PAD_NAME(pad));
802 if (GST_REAL_PAD(pad)->getfunc == NULL)
803 fprintf(stderr, "error, no getfunc in \"%s\"\n", gst_element_get_name (entry));
805 buf = (GST_REAL_PAD(pad)->getfunc)(pad);
806 if (buf) gst_pad_push(pad,buf);
808 pads = g_list_next (pads);
816 // check if nothing was scheduled that was ours..
817 if (!num_scheduled) {
818 // are there any other elements that are still busy?
819 if (bin->num_eos_providers) {
821 GST_DEBUG (0,"waiting for eos providers\n");
822 g_cond_wait (bin->eoscond, GST_OBJECT(bin)->lock);
823 GST_DEBUG (0,"num eos providers %d\n", bin->num_eos_providers);
827 gst_element_signal_eos (GST_ELEMENT (bin));
832 GST_DEBUG_LEAVE("(%s)", gst_element_get_name (GST_ELEMENT (bin)));