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>",
40 static void gst_bin_dispose (GObject *object);
42 static GstElementStateReturn gst_bin_change_state (GstElement *element);
43 static GstElementStateReturn gst_bin_change_state_norecurse (GstBin *bin);
44 static gboolean gst_bin_change_state_type (GstBin *bin,
45 GstElementState state,
48 static gboolean gst_bin_iterate_func (GstBin *bin);
50 #ifndef GST_DISABLE_LOADSAVE
51 static xmlNodePtr gst_bin_save_thyself (GstObject *object, xmlNodePtr parent);
52 static void gst_bin_restore_thyself (GstObject *object, xmlNodePtr self);
55 /* Bin signals and args */
67 static void gst_bin_class_init (GstBinClass *klass);
68 static void gst_bin_init (GstBin *bin);
71 static GstElementClass *parent_class = NULL;
72 static guint gst_bin_signals[LAST_SIGNAL] = { 0 };
75 gst_bin_get_type (void)
77 static GType bin_type = 0;
80 static const GTypeInfo bin_info = {
84 (GClassInitFunc)gst_bin_class_init,
89 (GInstanceInitFunc)gst_bin_init,
92 bin_type = g_type_register_static (GST_TYPE_ELEMENT, "GstBin", &bin_info, 0);
98 gst_bin_class_init (GstBinClass *klass)
100 GObjectClass *gobject_class;
101 GstObjectClass *gstobject_class;
102 GstElementClass *gstelement_class;
104 gobject_class = (GObjectClass*)klass;
105 gstobject_class = (GstObjectClass*)klass;
106 gstelement_class = (GstElementClass*)klass;
108 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
110 gst_bin_signals[OBJECT_ADDED] =
111 g_signal_new ("object_added", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_FIRST,
112 G_STRUCT_OFFSET (GstBinClass, object_added), NULL, NULL,
113 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
116 klass->change_state_type = GST_DEBUG_FUNCPTR (gst_bin_change_state_type);
117 klass->iterate = GST_DEBUG_FUNCPTR (gst_bin_iterate_func);
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);
124 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_bin_change_state);
126 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_bin_dispose);
130 gst_bin_init (GstBin *bin)
132 // in general, we prefer to use cothreads for most things
133 GST_FLAG_SET (bin, GST_BIN_FLAG_PREFER_COTHREADS);
135 bin->numchildren = 0;
136 bin->children = NULL;
137 bin->eos_providers = NULL;
138 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, GstSchedule *sched)
160 GST_INFO_ELEMENT (GST_CAT_PARENTAGE, element, "resetting element's scheduler");
162 // first remove the element from its current schedule, if any
163 // if (GST_ELEMENT_SCHED(element))
164 // GST_SCHEDULE_REMOVE_ELEMENT (GST_ELEMENT_SCHED(element), element);
165 // then set the new manager
166 gst_element_set_sched (element,sched);
168 // and add it to the new scheduler
170 // GST_SCHEDULE_ADD_ELEMENT (sched, element);
174 gst_bin_set_element_sched (GstElement *element,GstSchedule *sched)
179 g_return_if_fail (element != NULL);
180 g_return_if_fail (GST_IS_ELEMENT(element));
181 g_return_if_fail (sched != NULL);
182 g_return_if_fail (GST_IS_SCHEDULE(sched));
184 GST_INFO (GST_CAT_SCHEDULING, "setting element \"%s\" sched to %p",GST_ELEMENT_NAME(element),
187 // if it's actually a Bin
188 if (GST_IS_BIN(element)) {
190 if (GST_FLAG_IS_SET(element,GST_BIN_FLAG_MANAGER)) {
191 GST_INFO_ELEMENT (GST_CAT_PARENTAGE, element, "child is already a manager, not resetting");
195 GST_INFO_ELEMENT (GST_CAT_PARENTAGE, element, "setting children's schedule to parent's");
196 GST_SCHEDULE_ADD_ELEMENT (sched, element);
198 // set the children's schedule
199 children = GST_BIN(element)->children;
201 child = GST_ELEMENT (children->data);
202 children = g_list_next(children);
204 gst_bin_set_element_sched (child, sched);
207 // otherwise, if it's just a regular old element
209 GST_SCHEDULE_ADD_ELEMENT (sched, element);
215 gst_bin_unset_element_sched (GstElement *element)
220 g_return_if_fail (element != NULL);
221 g_return_if_fail (GST_IS_ELEMENT(element));
223 GST_INFO (GST_CAT_SCHEDULING, "removing element \"%s\" from it sched %p",
224 GST_ELEMENT_NAME(element),GST_ELEMENT_SCHED(element));
226 // if it's actually a Bin
227 if (GST_IS_BIN(element)) {
229 if (GST_FLAG_IS_SET(element,GST_BIN_FLAG_MANAGER)) {
230 GST_INFO_ELEMENT (GST_CAT_PARENTAGE, element, "child is already a manager, not unsetting sched");
234 // FIXME this check should be irrelevant
235 if (GST_ELEMENT_SCHED (element))
236 GST_SCHEDULE_REMOVE_ELEMENT (GST_ELEMENT_SCHED(element), element);
238 // for each child, remove them from their schedule
239 children = GST_BIN(element)->children;
241 child = GST_ELEMENT (children->data);
242 children = g_list_next(children);
244 gst_bin_unset_element_sched (child);
247 // otherwise, if it's just a regular old element
249 // FIXME this check should be irrelevant
250 if (GST_ELEMENT_SCHED (element))
251 GST_SCHEDULE_REMOVE_ELEMENT (GST_ELEMENT_SCHED(element), element);
258 * @bin: #GstBin to add element to
259 * @element: #GstElement to add to bin
261 * Add the given element to the bin. Set the elements parent, and thus
265 gst_bin_add (GstBin *bin,
268 g_return_if_fail (bin != NULL);
269 g_return_if_fail (GST_IS_BIN (bin));
270 g_return_if_fail (element != NULL);
271 g_return_if_fail (GST_IS_ELEMENT (element));
273 GST_DEBUG (GST_CAT_PARENTAGE, "adding element \"%s\" to bin \"%s\"\n",
274 GST_ELEMENT_NAME(element),GST_ELEMENT_NAME(bin));
276 // must be not be in PLAYING state in order to modify bin
277 // g_return_if_fail (GST_STATE (bin) != GST_STATE_PLAYING);
279 // the element must not already have a parent
280 g_return_if_fail (GST_ELEMENT_PARENT(element) == NULL);
282 // then check to see if the element's name is already taken in the bin
283 g_return_if_fail (gst_object_check_uniqueness (bin->children, GST_ELEMENT_NAME(element)) == TRUE);
285 // set the element's parent and add the element to the bin's list of children
286 gst_object_set_parent (GST_OBJECT (element), GST_OBJECT (bin));
287 bin->children = g_list_append (bin->children, element);
290 ///// now we have to deal with manager stuff
291 // we can only do this if there's a scheduler:
292 // if we're not a manager, and aren't attached to anything, we have no sched (yet)
293 if (GST_ELEMENT_SCHED(bin) != NULL)
294 gst_bin_set_element_sched (element, GST_ELEMENT_SCHED(bin));
296 GST_INFO_ELEMENT (GST_CAT_PARENTAGE, bin, "added child \"%s\"", GST_ELEMENT_NAME (element));
298 g_signal_emit (G_OBJECT (bin), gst_bin_signals[OBJECT_ADDED], 0, element);
303 * @bin: #GstBin to remove element from
304 * @element: #GstElement to remove
306 * Remove the element from its associated bin, unparenting as well.
309 gst_bin_remove (GstBin *bin,
312 g_return_if_fail (bin != NULL);
313 g_return_if_fail (GST_IS_BIN (bin));
314 g_return_if_fail (element != NULL);
315 g_return_if_fail (GST_IS_ELEMENT (element));
316 g_return_if_fail (bin->children != NULL);
318 // must not be in PLAYING state in order to modify bin
319 g_return_if_fail (GST_STATE (bin) != GST_STATE_PLAYING);
321 // the element must have its parent set to the current bin
322 g_return_if_fail (GST_ELEMENT_PARENT(element) == (GstObject *)bin);
324 // the element must be in the bin's list of children
325 if (g_list_find(bin->children, element) == NULL) {
326 // FIXME this should be a warning!!!
327 GST_ERROR_OBJECT(bin,element,"no such element in bin");
331 // remove this element from the list of managed elements
332 gst_bin_unset_element_sched (element);
334 // now remove the element from the list of elements
335 bin->children = g_list_remove (bin->children, element);
338 GST_INFO_ELEMENT (GST_CAT_PARENTAGE, bin, "removed child %s", GST_ELEMENT_NAME (element));
340 gst_object_unparent (GST_OBJECT (element));
342 /* if we're down to zero children, force state to NULL */
343 if (bin->numchildren == 0 && GST_ELEMENT_SCHED (bin) != NULL)
344 gst_element_set_state (GST_ELEMENT (bin), GST_STATE_NULL);
348 static GstElementStateReturn
349 gst_bin_change_state (GstElement *element)
354 GstElementStateReturn ret;
356 // GST_DEBUG_ENTER("(\"%s\")",GST_ELEMENT_NAME (element));
358 g_return_val_if_fail (GST_IS_BIN (element), GST_STATE_FAILURE);
360 bin = GST_BIN (element);
362 // GST_DEBUG (GST_CAT_STATES,"currently %d(%s), %d(%s) pending\n",GST_STATE (element),
363 // gst_element_statename (GST_STATE (element)), GST_STATE_PENDING (element),
364 // gst_element_statename (GST_STATE_PENDING (element)));
366 GST_INFO_ELEMENT (GST_CAT_STATES, element, "changing childrens' state from %s to %s",
367 gst_element_statename (GST_STATE (element)),
368 gst_element_statename (GST_STATE_PENDING (element)));
370 // g_return_val_if_fail(bin->numchildren != 0, GST_STATE_FAILURE);
374 children = bin->children;
376 child = GST_ELEMENT (children->data);
377 // GST_DEBUG (GST_CAT_STATES,"setting state on '%s'\n",GST_ELEMENT_NAME (child));
378 switch (gst_element_set_state (child, GST_STATE_PENDING (element))) {
379 case GST_STATE_FAILURE:
380 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
381 GST_DEBUG (GST_CAT_STATES,"child '%s' failed to go to state %d(%s)\n", GST_ELEMENT_NAME (child),
382 GST_STATE_PENDING (element), gst_element_statename (GST_STATE_PENDING (element)));
383 return GST_STATE_FAILURE;
385 case GST_STATE_ASYNC:
386 GST_DEBUG (GST_CAT_STATES,"child '%s' is changing state asynchronously\n", GST_ELEMENT_NAME (child));
390 children = g_list_next (children);
392 // g_print("<-- \"%s\"\n",GST_OBJECT_NAME(bin));
394 GST_INFO_ELEMENT (GST_CAT_STATES, element, "done changing bin's state from %s to %s",
395 gst_element_statename (GST_STATE (element)),
396 gst_element_statename (GST_STATE_PENDING (element)));
397 ret = gst_bin_change_state_norecurse (bin);
403 static GstElementStateReturn
404 gst_bin_change_state_norecurse (GstBin *bin)
406 if (GST_ELEMENT_CLASS (parent_class)->change_state) {
407 GST_DEBUG_ELEMENT (GST_CAT_STATES, bin, "setting bin's own state\n");
408 return GST_ELEMENT_CLASS (parent_class)->change_state (GST_ELEMENT (bin));
410 return GST_STATE_FAILURE;
414 gst_bin_change_state_type(GstBin *bin,
415 GstElementState state,
421 // g_print("gst_bin_change_state_type(\"%s\",%d,%d);\n",
422 // GST_OBJECT_NAME(bin))),state,type);
424 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
425 g_return_val_if_fail (bin->numchildren != 0, FALSE);
428 children = bin->children;
430 child = GST_ELEMENT (children->data);
431 if (GST_IS_BIN (child)) {
432 if (!gst_bin_set_state_type (GST_BIN (child), state,type))
434 } else if (G_TYPE_CHECK_INSTANCE_TYPE (child,type)) {
435 if (!gst_element_set_state (child,state))
439 children = g_list_next (children);
441 if (type == GST_TYPE_BIN)
442 gst_element_set_state (GST_ELEMENT (bin),state);
448 * gst_bin_set_state_type:
449 * @bin: #GstBin to set the state
450 * @state: the new state to set the elements to
451 * @type: the type of elements to change
453 * Sets the state of only those objects of the given type.
455 * Returns: indication if the state change was successfull
458 gst_bin_set_state_type (GstBin *bin,
459 GstElementState state,
464 GST_DEBUG (GST_CAT_STATES,"gst_bin_set_state_type(\"%s\",%d,%d)\n",
465 GST_ELEMENT_NAME (bin), state,type);
467 g_return_val_if_fail (bin != NULL, FALSE);
468 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
470 oclass = GST_BIN_CLASS (G_OBJECT_GET_CLASS(bin));
472 if (oclass->change_state_type)
473 (oclass->change_state_type) (bin,state,type);
478 gst_bin_dispose (GObject *object)
480 GstBin *bin = GST_BIN (object);
481 GList *children, *orig;
484 GST_DEBUG (GST_CAT_REFCOUNTING,"dispose\n");
487 orig = children = g_list_copy (bin->children);
489 child = GST_ELEMENT (children->data);
490 //gst_object_unref (GST_OBJECT (child));
491 //gst_object_unparent (GST_OBJECT (child));
492 gst_bin_remove (bin, child);
493 children = g_list_next (children);
496 g_list_free (bin->children);
498 bin->children = NULL;
499 bin->numchildren = 0;
501 g_cond_free (bin->eoscond);
503 G_OBJECT_CLASS (parent_class)->dispose (object);
507 * gst_bin_get_by_name:
508 * @bin: #Gstbin to search
509 * @name: the element name to search for
511 * Get the element with the given name from this bin.
513 * Returns: the element with the given name
516 gst_bin_get_by_name (GstBin *bin,
522 g_return_val_if_fail (bin != NULL, NULL);
523 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
524 g_return_val_if_fail (name != NULL, NULL);
526 GST_INFO_ELEMENT (GST_CAT_PARENTAGE, bin, "looking up child element %s", name);
528 children = bin->children;
530 child = GST_ELEMENT (children->data);
531 if (!strcmp (GST_OBJECT_NAME(child),name))
533 if (GST_IS_BIN (child)) {
534 GstElement *res = gst_bin_get_by_name (GST_BIN (child), name);
538 children = g_list_next (children);
545 * gst_bin_get_by_name_recurse_up:
546 * @bin: #Gstbin to search
547 * @name: the element name to search for
549 * Get the element with the given name from this bin. If the
550 * element is not found, a recursion is performed on the parent bin.
552 * Returns: the element with the given name
555 gst_bin_get_by_name_recurse_up (GstBin *bin,
558 GstElement *result = NULL;
561 g_return_val_if_fail (bin != NULL, NULL);
562 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
563 g_return_val_if_fail (name != NULL, NULL);
565 result = gst_bin_get_by_name (bin, name);
570 parent = gst_object_get_parent (GST_OBJECT (bin));
572 if (parent && GST_IS_BIN (parent)) {
573 result = gst_bin_get_by_name_recurse_up (GST_BIN (parent), name);
581 * @bin: #Gstbin to get the list from
583 * Get the list of elements in this bin.
585 * Returns: a GList of elements
588 gst_bin_get_list (GstBin *bin)
590 g_return_val_if_fail (bin != NULL, NULL);
591 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
593 return bin->children;
596 #ifndef GST_DISABLE_LOADSAVE
598 gst_bin_save_thyself (GstObject *object,
601 GstBin *bin = GST_BIN (object);
602 xmlNodePtr childlist, elementnode;
606 if (GST_OBJECT_CLASS (parent_class)->save_thyself)
607 GST_OBJECT_CLASS (parent_class)->save_thyself (GST_OBJECT (bin), parent);
609 childlist = xmlNewChild (parent, NULL, "children", NULL);
611 GST_INFO_ELEMENT (GST_CAT_XML, bin, "saving %d children", bin->numchildren);
613 children = bin->children;
615 child = GST_ELEMENT (children->data);
616 elementnode = xmlNewChild (childlist, NULL, "element", NULL);
617 gst_object_save_thyself (GST_OBJECT (child), elementnode);
618 children = g_list_next (children);
624 gst_bin_restore_thyself (GstObject *object,
627 GstBin *bin = GST_BIN (object);
628 xmlNodePtr field = self->xmlChildrenNode;
629 xmlNodePtr childlist;
632 if (!strcmp (field->name, "children")) {
633 GST_INFO_ELEMENT (GST_CAT_XML, GST_ELEMENT (object), "loading children");
634 childlist = field->xmlChildrenNode;
636 if (!strcmp (childlist->name, "element")) {
637 GstElement *element = gst_element_restore_thyself (childlist, GST_OBJECT (bin));
639 gst_bin_add (bin, element);
641 childlist = childlist->next;
648 #endif /* GST_DISABLE_LOADSAVE */
653 * @bin: #Gstbin to iterate
655 * Iterates over the elements in this bin.
657 * Returns: TRUE if the bin did something usefull. This value
658 * can be used to determine it the bin is in EOS.
661 gst_bin_iterate (GstBin *bin)
666 GST_DEBUG_ENTER("(\"%s\")",GST_ELEMENT_NAME (bin));
668 oclass = GST_BIN_CLASS (G_OBJECT_GET_CLASS(bin));
671 eos = (oclass->iterate) (bin);
673 GST_DEBUG_LEAVE("(\"%s\")",GST_ELEMENT_NAME (bin));
678 /* out internal element fired EOS, we decrement the number of pending EOS childs */
679 G_GNUC_UNUSED static void
680 gst_bin_received_eos (GstElement *element, GstBin *bin)
682 GST_INFO_ELEMENT (GST_CAT_PLANNING, bin, "child %s fired eos, pending %d", GST_ELEMENT_NAME (element),
683 bin->num_eos_providers);
686 if (bin->num_eos_providers) {
687 bin->num_eos_providers--;
688 g_cond_signal (bin->eoscond);
700 gst_bin_iterate_func (GstBin *bin)
702 // only iterate if this is the manager bin
703 if (GST_ELEMENT_SCHED(bin)->parent == GST_ELEMENT (bin)) {
704 return GST_SCHEDULE_ITERATE(GST_ELEMENT_SCHED(bin));
706 GST_DEBUG (GST_CAT_SCHEDULING, "this bin can't be iterated on!\n");