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"
30 GstElementDetails gst_bin_details = {
33 "Simple container object",
35 "Erik Walthinsen <omega@cse.ogi.edu>",
39 GType _gst_bin_type = 0;
41 static void gst_bin_dispose (GObject *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,
48 static void gst_bin_child_state_change (GstBin *bin, GstElementState old,
49 GstElementState new, GstElement *child);
51 static gboolean gst_bin_iterate_func (GstBin *bin);
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);
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)
81 static const GTypeInfo bin_info = {
85 (GClassInitFunc)gst_bin_class_init,
90 (GInstanceInitFunc)gst_bin_init,
93 _gst_bin_type = g_type_register_static (GST_TYPE_ELEMENT, "GstBin", &bin_info, 0);
99 gst_bin_class_init (GstBinClass *klass)
101 GObjectClass *gobject_class;
102 GstObjectClass *gstobject_class;
103 GstElementClass *gstelement_class;
105 gobject_class = (GObjectClass*)klass;
106 gstobject_class = (GstObjectClass*)klass;
107 gstelement_class = (GstElementClass*)klass;
109 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
111 gst_bin_signals[OBJECT_ADDED] =
112 g_signal_new ("object_added", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_FIRST,
113 G_STRUCT_OFFSET (GstBinClass, object_added), NULL, NULL,
114 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
117 klass->change_state_type = GST_DEBUG_FUNCPTR (gst_bin_change_state_type);
118 klass->iterate = GST_DEBUG_FUNCPTR (gst_bin_iterate_func);
120 #ifndef GST_DISABLE_LOADSAVE
121 gstobject_class->save_thyself = GST_DEBUG_FUNCPTR (gst_bin_save_thyself);
122 gstobject_class->restore_thyself = GST_DEBUG_FUNCPTR (gst_bin_restore_thyself);
125 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_bin_change_state);
127 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_bin_dispose);
131 gst_bin_init (GstBin *bin)
133 // in general, we prefer to use cothreads for most things
134 GST_FLAG_SET (bin, GST_BIN_FLAG_PREFER_COTHREADS);
136 bin->numchildren = 0;
137 bin->children = NULL;
138 bin->eos_providers = NULL;
139 bin->num_eos_providers = 0;
140 bin->eoscond = g_cond_new ();
145 * @name: name of new bin
147 * Create a new bin with given name.
152 gst_bin_new (const gchar *name)
154 return gst_elementfactory_make ("bin", name);
158 gst_bin_reset_element_sched (GstElement *element, GstScheduler *sched)
160 GST_INFO_ELEMENT (GST_CAT_PARENTAGE, element, "resetting element's scheduler");
162 gst_element_set_sched (element,sched);
166 gst_bin_set_element_sched (GstElement *element,GstScheduler *sched)
171 g_return_if_fail (element != NULL);
172 g_return_if_fail (GST_IS_ELEMENT(element));
173 g_return_if_fail (sched != NULL);
174 g_return_if_fail (GST_IS_SCHEDULER(sched));
176 GST_INFO (GST_CAT_SCHEDULING, "setting element \"%s\" sched to %p",GST_ELEMENT_NAME(element),
179 // if it's actually a Bin
180 if (GST_IS_BIN(element)) {
181 if (GST_FLAG_IS_SET (element, GST_BIN_FLAG_MANAGER)) {
182 GST_INFO_ELEMENT (GST_CAT_PARENTAGE, element, "child is already a manager, not resetting");
186 GST_INFO_ELEMENT (GST_CAT_PARENTAGE, element, "setting children's schedule to parent's");
187 gst_scheduler_add_element (sched, element);
189 // set the children's schedule
190 children = GST_BIN(element)->children;
192 child = GST_ELEMENT (children->data);
193 children = g_list_next(children);
195 gst_bin_set_element_sched (child, sched);
198 // otherwise, if it's just a regular old element
200 gst_scheduler_add_element (sched, element);
206 gst_bin_unset_element_sched (GstElement *element)
211 g_return_if_fail (element != NULL);
212 g_return_if_fail (GST_IS_ELEMENT(element));
214 GST_INFO (GST_CAT_SCHEDULING, "removing element \"%s\" from it sched %p",
215 GST_ELEMENT_NAME(element),GST_ELEMENT_SCHED(element));
217 // if it's actually a Bin
218 if (GST_IS_BIN(element)) {
220 if (GST_FLAG_IS_SET(element,GST_BIN_FLAG_MANAGER)) {
221 GST_INFO_ELEMENT (GST_CAT_PARENTAGE, element, "child is already a manager, not unsetting sched");
225 // FIXME this check should be irrelevant
226 if (GST_ELEMENT_SCHED (element))
227 gst_scheduler_remove_element (GST_ELEMENT_SCHED(element), element);
229 // for each child, remove them from their schedule
230 children = GST_BIN(element)->children;
232 child = GST_ELEMENT (children->data);
233 children = g_list_next(children);
235 gst_bin_unset_element_sched (child);
238 // otherwise, if it's just a regular old element
240 // FIXME this check should be irrelevant
241 if (GST_ELEMENT_SCHED (element))
242 gst_scheduler_remove_element (GST_ELEMENT_SCHED(element), element);
249 * @bin: #GstBin to add element to
250 * @element: #GstElement to add to bin
252 * Add the given element to the bin. Set the elements parent, and thus
256 gst_bin_add (GstBin *bin,
260 GstElementState state;
262 g_return_if_fail (bin != NULL);
263 g_return_if_fail (GST_IS_BIN (bin));
264 g_return_if_fail (element != NULL);
265 g_return_if_fail (GST_IS_ELEMENT (element));
267 GST_DEBUG (GST_CAT_PARENTAGE, "adding element \"%s\" to bin \"%s\"\n",
268 GST_ELEMENT_NAME (element), GST_ELEMENT_NAME (bin));
270 // must be not be in PLAYING state in order to modify bin
271 g_return_if_fail (GST_STATE (bin) != GST_STATE_PLAYING);
273 // the element must not already have a parent
274 g_return_if_fail (GST_ELEMENT_PARENT (element) == NULL);
276 // then check to see if the element's name is already taken in the bin
277 g_return_if_fail (gst_object_check_uniqueness (bin->children, GST_ELEMENT_NAME (element)) == TRUE);
279 // set the element's parent and add the element to the bin's list of children
280 gst_object_set_parent (GST_OBJECT (element), GST_OBJECT (bin));
281 g_signal_connect (G_OBJECT (element), "state_change", gst_bin_child_state_change, G_OBJECT (bin));
283 bin->children = g_list_append (bin->children, element);
286 // bump our internal state counter
287 state = GST_STATE (element);
288 while (state>>=1) state_idx++;
289 bin->child_states[state_idx]++;
291 ///// now we have to deal with manager stuff
292 // we can only do this if there's a scheduler:
293 // if we're not a manager, and aren't attached to anything, we have no sched (yet)
294 if (GST_IS_BIN(element) && GST_FLAG_IS_SET (element, GST_BIN_FLAG_MANAGER)) {
295 GST_INFO_ELEMENT (GST_CAT_PARENTAGE, element, "child is a manager");
297 else if (GST_ELEMENT_SCHED (bin) != NULL) {
298 gst_bin_set_element_sched (element, GST_ELEMENT_SCHED (bin));
301 GST_INFO_ELEMENT (GST_CAT_PARENTAGE, bin, "added child \"%s\"", GST_ELEMENT_NAME (element));
303 g_signal_emit (G_OBJECT (bin), gst_bin_signals[OBJECT_ADDED], 0, element);
308 * @bin: #GstBin to remove element from
309 * @element: #GstElement to remove
311 * Remove the element from its associated bin, unparenting as well.
314 gst_bin_remove (GstBin *bin,
318 GstElementState state;
320 g_return_if_fail (bin != NULL);
321 g_return_if_fail (GST_IS_BIN (bin));
322 g_return_if_fail (element != NULL);
323 g_return_if_fail (GST_IS_ELEMENT (element));
324 g_return_if_fail (bin->children != NULL);
326 // must not be in PLAYING state in order to modify bin
327 g_return_if_fail (GST_STATE (bin) != GST_STATE_PLAYING);
329 // the element must have its parent set to the current bin
330 g_return_if_fail (GST_ELEMENT_PARENT(element) == (GstObject *)bin);
332 // the element must be in the bin's list of children
333 if (g_list_find(bin->children, element) == NULL) {
334 // FIXME this should be a warning!!!
335 GST_ERROR_OBJECT(bin,element,"no such element in bin");
339 // remove this element from the list of managed elements
340 gst_bin_unset_element_sched (element);
342 // now remove the element from the list of elements
343 bin->children = g_list_remove (bin->children, element);
346 // bump our internal state counter
347 state = GST_STATE (element);
348 while (state>>=1) state_idx++;
349 bin->child_states[state_idx]--;
351 GST_INFO_ELEMENT (GST_CAT_PARENTAGE, bin, "removed child %s", GST_ELEMENT_NAME (element));
353 gst_object_unparent (GST_OBJECT (element));
355 /* if we're down to zero children, force state to NULL */
356 if (bin->numchildren == 0 && GST_ELEMENT_SCHED (bin) != NULL)
357 gst_element_set_state (GST_ELEMENT (bin), GST_STATE_NULL);
361 gst_bin_child_state_change (GstBin *bin, GstElementState old, GstElementState new, GstElement *child)
363 gint old_idx = 0, new_idx = 0, i;
365 GST_INFO (GST_CAT_STATES, "child %s changed state in bin %s from %s to %s",
366 GST_ELEMENT_NAME (child), GST_ELEMENT_NAME (bin),
367 gst_element_statename (old), gst_element_statename (new));
369 while (old>>=1) old_idx++;
370 while (new>>=1) new_idx++;
373 bin->child_states[old_idx]--;
374 bin->child_states[new_idx]++;
376 for (i = GST_NUM_STATES-1; i >= 0; i--) {
377 if (bin->child_states[i] != 0) {
378 if (GST_STATE (bin) != (1 << i)) {
379 GST_INFO (GST_CAT_STATES, "bin %s need state change to %s",
380 GST_ELEMENT_NAME (bin), gst_element_statename (1<<i));
381 GST_STATE_PENDING (bin) = (1<<i);
382 gst_bin_change_state_norecurse (bin);
387 // FIXME, need to setup this array at add/remove time
389 GST_STATE_PENDING (bin) = GST_STATE_NULL;
390 gst_bin_change_state_norecurse (bin);
396 static GstElementStateReturn
397 gst_bin_change_state (GstElement *element)
402 GstElementStateReturn ret;
403 GstElementState old_state, pending;
404 gboolean have_async = FALSE;
406 g_return_val_if_fail (GST_IS_BIN (element), GST_STATE_FAILURE);
408 bin = GST_BIN (element);
410 old_state = GST_STATE (element);
411 pending = GST_STATE_PENDING (element);
413 GST_INFO_ELEMENT (GST_CAT_STATES, element, "changing childrens' state from %s to %s",
414 gst_element_statename (old_state),
415 gst_element_statename (pending));
417 children = bin->children;
419 child = GST_ELEMENT (children->data);
420 children = g_list_next (children);
422 switch (gst_element_set_state (child, pending)) {
423 case GST_STATE_FAILURE:
424 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
425 GST_DEBUG (GST_CAT_STATES,"child '%s' failed to go to state %d(%s)\n", GST_ELEMENT_NAME (child),
426 pending, gst_element_statename (pending));
428 gst_element_set_state (child, old_state);
429 if (GST_ELEMENT_SCHED (child) == GST_ELEMENT_SCHED (element)) {
430 return GST_STATE_FAILURE;
433 case GST_STATE_ASYNC:
434 GST_DEBUG (GST_CAT_STATES,"child '%s' is changing state asynchronously\n", GST_ELEMENT_NAME (child));
440 GST_INFO_ELEMENT (GST_CAT_STATES, element, "done changing bin's state from %s to %s",
441 gst_element_statename (old_state),
442 gst_element_statename (pending));
445 ret = GST_STATE_ASYNC;
447 ret = GST_STATE_SUCCESS;
453 static GstElementStateReturn
454 gst_bin_change_state_norecurse (GstBin *bin)
456 if (GST_ELEMENT_CLASS (parent_class)->change_state) {
457 GST_DEBUG_ELEMENT (GST_CAT_STATES, bin, "setting bin's own state\n");
458 return GST_ELEMENT_CLASS (parent_class)->change_state (GST_ELEMENT (bin));
460 return GST_STATE_FAILURE;
464 gst_bin_change_state_type (GstBin *bin,
465 GstElementState state,
471 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
472 g_return_val_if_fail (bin->numchildren != 0, FALSE);
474 children = bin->children;
476 child = GST_ELEMENT (children->data);
477 if (GST_IS_BIN (child)) {
478 if (!gst_bin_set_state_type (GST_BIN (child), state,type))
480 } else if (G_TYPE_CHECK_INSTANCE_TYPE (child,type)) {
481 if (!gst_element_set_state (child,state))
484 children = g_list_next (children);
486 if (type == GST_TYPE_BIN)
487 gst_element_set_state (GST_ELEMENT (bin), state);
493 * gst_bin_set_state_type:
494 * @bin: #GstBin to set the state
495 * @state: the new state to set the elements to
496 * @type: the type of elements to change
498 * Sets the state of only those objects of the given type.
500 * Returns: indication if the state change was successfull
503 gst_bin_set_state_type (GstBin *bin,
504 GstElementState state,
509 GST_DEBUG (GST_CAT_STATES,"gst_bin_set_state_type(\"%s\",%d,%d)\n",
510 GST_ELEMENT_NAME (bin), state,type);
512 g_return_val_if_fail (bin != NULL, FALSE);
513 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
515 oclass = GST_BIN_CLASS (G_OBJECT_GET_CLASS(bin));
517 if (oclass->change_state_type)
518 (oclass->change_state_type) (bin,state,type);
523 gst_bin_dispose (GObject *object)
525 GstBin *bin = GST_BIN (object);
526 GList *children, *orig;
529 GST_DEBUG (GST_CAT_REFCOUNTING,"dispose\n");
532 orig = children = g_list_copy (bin->children);
534 child = GST_ELEMENT (children->data);
535 //gst_object_unref (GST_OBJECT (child));
536 //gst_object_unparent (GST_OBJECT (child));
537 gst_bin_remove (bin, child);
538 children = g_list_next (children);
541 g_list_free (bin->children);
543 bin->children = NULL;
544 bin->numchildren = 0;
546 g_cond_free (bin->eoscond);
548 G_OBJECT_CLASS (parent_class)->dispose (object);
552 * gst_bin_get_by_name:
553 * @bin: #Gstbin to search
554 * @name: the element name to search for
556 * Get the element with the given name from this bin.
558 * Returns: the element with the given name
561 gst_bin_get_by_name (GstBin *bin,
567 g_return_val_if_fail (bin != NULL, NULL);
568 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
569 g_return_val_if_fail (name != NULL, NULL);
571 GST_INFO_ELEMENT (GST_CAT_PARENTAGE, bin, "looking up child element %s", name);
573 children = bin->children;
575 child = GST_ELEMENT (children->data);
576 if (!strcmp (GST_OBJECT_NAME(child),name))
578 if (GST_IS_BIN (child)) {
579 GstElement *res = gst_bin_get_by_name (GST_BIN (child), name);
583 children = g_list_next (children);
590 * gst_bin_get_by_name_recurse_up:
591 * @bin: #Gstbin to search
592 * @name: the element name to search for
594 * Get the element with the given name from this bin. If the
595 * element is not found, a recursion is performed on the parent bin.
597 * Returns: the element with the given name
600 gst_bin_get_by_name_recurse_up (GstBin *bin,
603 GstElement *result = NULL;
606 g_return_val_if_fail (bin != NULL, NULL);
607 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
608 g_return_val_if_fail (name != NULL, NULL);
610 result = gst_bin_get_by_name (bin, name);
615 parent = gst_object_get_parent (GST_OBJECT (bin));
617 if (parent && GST_IS_BIN (parent)) {
618 result = gst_bin_get_by_name_recurse_up (GST_BIN (parent), name);
626 * @bin: #Gstbin to get the list from
628 * Get the list of elements in this bin.
630 * Returns: a GList of elements
633 gst_bin_get_list (GstBin *bin)
635 g_return_val_if_fail (bin != NULL, NULL);
636 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
638 return bin->children;
641 #ifndef GST_DISABLE_LOADSAVE
643 gst_bin_save_thyself (GstObject *object,
646 GstBin *bin = GST_BIN (object);
647 xmlNodePtr childlist, elementnode;
651 if (GST_OBJECT_CLASS (parent_class)->save_thyself)
652 GST_OBJECT_CLASS (parent_class)->save_thyself (GST_OBJECT (bin), parent);
654 childlist = xmlNewChild (parent, NULL, "children", NULL);
656 GST_INFO_ELEMENT (GST_CAT_XML, bin, "saving %d children", bin->numchildren);
658 children = bin->children;
660 child = GST_ELEMENT (children->data);
661 elementnode = xmlNewChild (childlist, NULL, "element", NULL);
662 gst_object_save_thyself (GST_OBJECT (child), elementnode);
663 children = g_list_next (children);
669 gst_bin_restore_thyself (GstObject *object,
672 GstBin *bin = GST_BIN (object);
673 xmlNodePtr field = self->xmlChildrenNode;
674 xmlNodePtr childlist;
677 if (!strcmp (field->name, "children")) {
678 GST_INFO_ELEMENT (GST_CAT_XML, GST_ELEMENT (object), "loading children");
679 childlist = field->xmlChildrenNode;
681 if (!strcmp (childlist->name, "element")) {
682 GstElement *element = gst_element_restore_thyself (childlist, GST_OBJECT (bin));
684 gst_bin_add (bin, element);
686 childlist = childlist->next;
693 #endif /* GST_DISABLE_LOADSAVE */
696 gst_bin_iterate_func (GstBin *bin)
698 // only iterate if this is the manager bin
699 if (GST_ELEMENT_SCHED(bin)->parent == GST_ELEMENT (bin)) {
700 return gst_scheduler_iterate (GST_ELEMENT_SCHED(bin));
702 g_warning ("bin \"%d\" can't be iterated on!\n", GST_ELEMENT_NAME (bin));
710 * @bin: #Gstbin to iterate
712 * Iterates over the elements in this bin.
714 * Returns: TRUE if the bin did something usefull. This value
715 * can be used to determine it the bin is in EOS.
718 gst_bin_iterate (GstBin *bin)
721 gboolean running = TRUE;
723 GST_DEBUG_ENTER("(\"%s\")",GST_ELEMENT_NAME (bin));
725 oclass = GST_BIN_CLASS (G_OBJECT_GET_CLASS(bin));
728 running = (oclass->iterate) (bin);
730 GST_DEBUG_LEAVE("(\"%s\") %d",GST_ELEMENT_NAME (bin), running);
733 if (GST_STATE (bin) == GST_STATE_PLAYING && GST_STATE_PENDING (bin) == GST_STATE_VOID_PENDING) {
734 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, bin, "waiting for child shutdown after useless iteration\n");
735 //gst_element_set_state (GST_ELEMENT (bin), GST_STATE_PAUSED);
736 gst_element_wait_state_change (GST_ELEMENT (bin));
737 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, bin, "child shutdown\n");
744 /* out internal element fired EOS, we decrement the number of pending EOS childs */
745 G_GNUC_UNUSED static void
746 gst_bin_received_eos (GstElement *element, GstBin *bin)
748 GST_INFO_ELEMENT (GST_CAT_PLANNING, bin, "child %s fired eos, pending %d", GST_ELEMENT_NAME (element),
749 bin->num_eos_providers);
752 if (bin->num_eos_providers) {
753 bin->num_eos_providers--;
754 g_cond_signal (bin->eoscond);