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,
49 static gboolean gst_bin_iterate_func (GstBin *bin);
51 #ifndef GST_DISABLE_LOADSAVE
52 static xmlNodePtr gst_bin_save_thyself (GstObject *object, xmlNodePtr parent);
53 static void gst_bin_restore_thyself (GstObject *object, xmlNodePtr self);
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)
79 static const GTypeInfo bin_info = {
83 (GClassInitFunc)gst_bin_class_init,
88 (GInstanceInitFunc)gst_bin_init,
91 _gst_bin_type = g_type_register_static (GST_TYPE_ELEMENT, "GstBin", &bin_info, 0);
97 gst_bin_class_init (GstBinClass *klass)
99 GObjectClass *gobject_class;
100 GstObjectClass *gstobject_class;
101 GstElementClass *gstelement_class;
103 gobject_class = (GObjectClass*)klass;
104 gstobject_class = (GstObjectClass*)klass;
105 gstelement_class = (GstElementClass*)klass;
107 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
109 gst_bin_signals[OBJECT_ADDED] =
110 g_signal_new ("object_added", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_FIRST,
111 G_STRUCT_OFFSET (GstBinClass, object_added), NULL, NULL,
112 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
115 klass->change_state_type = GST_DEBUG_FUNCPTR (gst_bin_change_state_type);
116 klass->iterate = GST_DEBUG_FUNCPTR (gst_bin_iterate_func);
118 #ifndef GST_DISABLE_LOADSAVE
119 gstobject_class->save_thyself = GST_DEBUG_FUNCPTR (gst_bin_save_thyself);
120 gstobject_class->restore_thyself = GST_DEBUG_FUNCPTR (gst_bin_restore_thyself);
123 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_bin_change_state);
125 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_bin_dispose);
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 ();
144 * @name: name of new bin
146 * Create a new bin with given name.
151 gst_bin_new (const gchar *name)
153 return gst_elementfactory_make ("bin", name);
157 gst_bin_reset_element_sched (GstElement *element, GstSchedule *sched)
159 GST_INFO_ELEMENT (GST_CAT_PARENTAGE, element, "resetting element's scheduler");
161 // first remove the element from its current schedule, if any
162 // if (GST_ELEMENT_SCHED(element))
163 // GST_SCHEDULE_REMOVE_ELEMENT (GST_ELEMENT_SCHED(element), element);
164 // then set the new manager
165 gst_element_set_sched (element,sched);
167 // and add it to the new scheduler
169 // GST_SCHEDULE_ADD_ELEMENT (sched, element);
173 gst_bin_set_element_sched (GstElement *element,GstSchedule *sched)
178 g_return_if_fail (element != NULL);
179 g_return_if_fail (GST_IS_ELEMENT(element));
180 g_return_if_fail (sched != NULL);
181 g_return_if_fail (GST_IS_SCHEDULE(sched));
183 GST_INFO (GST_CAT_SCHEDULING, "setting element \"%s\" sched to %p",GST_ELEMENT_NAME(element),
186 // if it's actually a Bin
187 if (GST_IS_BIN(element)) {
189 if (GST_FLAG_IS_SET(element,GST_BIN_FLAG_MANAGER)) {
190 GST_INFO_ELEMENT (GST_CAT_PARENTAGE, element, "child is already a manager, not resetting");
194 GST_INFO_ELEMENT (GST_CAT_PARENTAGE, element, "setting children's schedule to parent's");
195 GST_SCHEDULE_ADD_ELEMENT (sched, element);
197 // set the children's schedule
198 children = GST_BIN(element)->children;
200 child = GST_ELEMENT (children->data);
201 children = g_list_next(children);
203 gst_bin_set_element_sched (child, sched);
206 // otherwise, if it's just a regular old element
208 GST_SCHEDULE_ADD_ELEMENT (sched, element);
214 gst_bin_unset_element_sched (GstElement *element)
219 g_return_if_fail (element != NULL);
220 g_return_if_fail (GST_IS_ELEMENT(element));
222 GST_INFO (GST_CAT_SCHEDULING, "removing element \"%s\" from it sched %p",
223 GST_ELEMENT_NAME(element),GST_ELEMENT_SCHED(element));
225 // if it's actually a Bin
226 if (GST_IS_BIN(element)) {
228 if (GST_FLAG_IS_SET(element,GST_BIN_FLAG_MANAGER)) {
229 GST_INFO_ELEMENT (GST_CAT_PARENTAGE, element, "child is already a manager, not unsetting sched");
233 // FIXME this check should be irrelevant
234 if (GST_ELEMENT_SCHED (element))
235 GST_SCHEDULE_REMOVE_ELEMENT (GST_ELEMENT_SCHED(element), element);
237 // for each child, remove them from their schedule
238 children = GST_BIN(element)->children;
240 child = GST_ELEMENT (children->data);
241 children = g_list_next(children);
243 gst_bin_unset_element_sched (child);
246 // otherwise, if it's just a regular old element
248 // FIXME this check should be irrelevant
249 if (GST_ELEMENT_SCHED (element))
250 GST_SCHEDULE_REMOVE_ELEMENT (GST_ELEMENT_SCHED(element), element);
257 * @bin: #GstBin to add element to
258 * @element: #GstElement to add to bin
260 * Add the given element to the bin. Set the elements parent, and thus
264 gst_bin_add (GstBin *bin,
267 g_return_if_fail (bin != NULL);
268 g_return_if_fail (GST_IS_BIN (bin));
269 g_return_if_fail (element != NULL);
270 g_return_if_fail (GST_IS_ELEMENT (element));
272 GST_DEBUG (GST_CAT_PARENTAGE, "adding element \"%s\" to bin \"%s\"\n",
273 GST_ELEMENT_NAME(element),GST_ELEMENT_NAME(bin));
275 // must be not be in PLAYING state in order to modify bin
276 // g_return_if_fail (GST_STATE (bin) != GST_STATE_PLAYING);
278 // the element must not already have a parent
279 g_return_if_fail (GST_ELEMENT_PARENT(element) == NULL);
281 // then check to see if the element's name is already taken in the bin
282 g_return_if_fail (gst_object_check_uniqueness (bin->children, GST_ELEMENT_NAME(element)) == TRUE);
284 // set the element's parent and add the element to the bin's list of children
285 gst_object_set_parent (GST_OBJECT (element), GST_OBJECT (bin));
286 bin->children = g_list_append (bin->children, element);
289 ///// now we have to deal with manager stuff
290 // we can only do this if there's a scheduler:
291 // if we're not a manager, and aren't attached to anything, we have no sched (yet)
292 if (GST_ELEMENT_SCHED(bin) != NULL)
293 gst_bin_set_element_sched (element, GST_ELEMENT_SCHED(bin));
295 GST_INFO_ELEMENT (GST_CAT_PARENTAGE, bin, "added child \"%s\"", GST_ELEMENT_NAME (element));
297 g_signal_emit (G_OBJECT (bin), gst_bin_signals[OBJECT_ADDED], 0, element);
302 * @bin: #GstBin to remove element from
303 * @element: #GstElement to remove
305 * Remove the element from its associated bin, unparenting as well.
308 gst_bin_remove (GstBin *bin,
311 g_return_if_fail (bin != NULL);
312 g_return_if_fail (GST_IS_BIN (bin));
313 g_return_if_fail (element != NULL);
314 g_return_if_fail (GST_IS_ELEMENT (element));
315 g_return_if_fail (bin->children != NULL);
317 // must not be in PLAYING state in order to modify bin
318 g_return_if_fail (GST_STATE (bin) != GST_STATE_PLAYING);
320 // the element must have its parent set to the current bin
321 g_return_if_fail (GST_ELEMENT_PARENT(element) == (GstObject *)bin);
323 // the element must be in the bin's list of children
324 if (g_list_find(bin->children, element) == NULL) {
325 // FIXME this should be a warning!!!
326 GST_ERROR_OBJECT(bin,element,"no such element in bin");
330 // remove this element from the list of managed elements
331 gst_bin_unset_element_sched (element);
333 // now remove the element from the list of elements
334 bin->children = g_list_remove (bin->children, element);
337 GST_INFO_ELEMENT (GST_CAT_PARENTAGE, bin, "removed child %s", GST_ELEMENT_NAME (element));
339 gst_object_unparent (GST_OBJECT (element));
341 /* if we're down to zero children, force state to NULL */
342 if (bin->numchildren == 0 && GST_ELEMENT_SCHED (bin) != NULL)
343 gst_element_set_state (GST_ELEMENT (bin), GST_STATE_NULL);
347 static GstElementStateReturn
348 gst_bin_change_state (GstElement *element)
353 GstElementStateReturn ret;
355 // GST_DEBUG_ENTER("(\"%s\")",GST_ELEMENT_NAME (element));
357 g_return_val_if_fail (GST_IS_BIN (element), GST_STATE_FAILURE);
359 bin = GST_BIN (element);
361 // GST_DEBUG (GST_CAT_STATES,"currently %d(%s), %d(%s) pending\n",GST_STATE (element),
362 // gst_element_statename (GST_STATE (element)), GST_STATE_PENDING (element),
363 // gst_element_statename (GST_STATE_PENDING (element)));
365 GST_INFO_ELEMENT (GST_CAT_STATES, element, "changing childrens' state from %s to %s",
366 gst_element_statename (GST_STATE (element)),
367 gst_element_statename (GST_STATE_PENDING (element)));
369 // g_return_val_if_fail(bin->numchildren != 0, GST_STATE_FAILURE);
373 children = bin->children;
375 child = GST_ELEMENT (children->data);
376 // GST_DEBUG (GST_CAT_STATES,"setting state on '%s'\n",GST_ELEMENT_NAME (child));
377 switch (gst_element_set_state (child, GST_STATE_PENDING (element))) {
378 case GST_STATE_FAILURE:
379 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
380 GST_DEBUG (GST_CAT_STATES,"child '%s' failed to go to state %d(%s)\n", GST_ELEMENT_NAME (child),
381 GST_STATE_PENDING (element), gst_element_statename (GST_STATE_PENDING (element)));
382 return GST_STATE_FAILURE;
384 case GST_STATE_ASYNC:
385 GST_DEBUG (GST_CAT_STATES,"child '%s' is changing state asynchronously\n", GST_ELEMENT_NAME (child));
389 children = g_list_next (children);
391 // g_print("<-- \"%s\"\n",GST_OBJECT_NAME(bin));
393 GST_INFO_ELEMENT (GST_CAT_STATES, element, "done changing bin's state from %s to %s",
394 gst_element_statename (GST_STATE (element)),
395 gst_element_statename (GST_STATE_PENDING (element)));
396 ret = gst_bin_change_state_norecurse (bin);
402 static GstElementStateReturn
403 gst_bin_change_state_norecurse (GstBin *bin)
405 if (GST_ELEMENT_CLASS (parent_class)->change_state) {
406 GST_DEBUG_ELEMENT (GST_CAT_STATES, bin, "setting bin's own state\n");
407 return GST_ELEMENT_CLASS (parent_class)->change_state (GST_ELEMENT (bin));
409 return GST_STATE_FAILURE;
413 gst_bin_change_state_type(GstBin *bin,
414 GstElementState state,
420 // g_print("gst_bin_change_state_type(\"%s\",%d,%d);\n",
421 // GST_OBJECT_NAME(bin))),state,type);
423 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
424 g_return_val_if_fail (bin->numchildren != 0, FALSE);
427 children = bin->children;
429 child = GST_ELEMENT (children->data);
430 if (GST_IS_BIN (child)) {
431 if (!gst_bin_set_state_type (GST_BIN (child), state,type))
433 } else if (G_TYPE_CHECK_INSTANCE_TYPE (child,type)) {
434 if (!gst_element_set_state (child,state))
438 children = g_list_next (children);
440 if (type == GST_TYPE_BIN)
441 gst_element_set_state (GST_ELEMENT (bin),state);
447 * gst_bin_set_state_type:
448 * @bin: #GstBin to set the state
449 * @state: the new state to set the elements to
450 * @type: the type of elements to change
452 * Sets the state of only those objects of the given type.
454 * Returns: indication if the state change was successfull
457 gst_bin_set_state_type (GstBin *bin,
458 GstElementState state,
463 GST_DEBUG (GST_CAT_STATES,"gst_bin_set_state_type(\"%s\",%d,%d)\n",
464 GST_ELEMENT_NAME (bin), state,type);
466 g_return_val_if_fail (bin != NULL, FALSE);
467 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
469 oclass = GST_BIN_CLASS (G_OBJECT_GET_CLASS(bin));
471 if (oclass->change_state_type)
472 (oclass->change_state_type) (bin,state,type);
477 gst_bin_dispose (GObject *object)
479 GstBin *bin = GST_BIN (object);
480 GList *children, *orig;
483 GST_DEBUG (GST_CAT_REFCOUNTING,"dispose\n");
486 orig = children = g_list_copy (bin->children);
488 child = GST_ELEMENT (children->data);
489 //gst_object_unref (GST_OBJECT (child));
490 //gst_object_unparent (GST_OBJECT (child));
491 gst_bin_remove (bin, child);
492 children = g_list_next (children);
495 g_list_free (bin->children);
497 bin->children = NULL;
498 bin->numchildren = 0;
500 g_cond_free (bin->eoscond);
502 G_OBJECT_CLASS (parent_class)->dispose (object);
506 * gst_bin_get_by_name:
507 * @bin: #Gstbin to search
508 * @name: the element name to search for
510 * Get the element with the given name from this bin.
512 * Returns: the element with the given name
515 gst_bin_get_by_name (GstBin *bin,
521 g_return_val_if_fail (bin != NULL, NULL);
522 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
523 g_return_val_if_fail (name != NULL, NULL);
525 GST_INFO_ELEMENT (GST_CAT_PARENTAGE, bin, "looking up child element %s", name);
527 children = bin->children;
529 child = GST_ELEMENT (children->data);
530 if (!strcmp (GST_OBJECT_NAME(child),name))
532 if (GST_IS_BIN (child)) {
533 GstElement *res = gst_bin_get_by_name (GST_BIN (child), name);
537 children = g_list_next (children);
544 * gst_bin_get_by_name_recurse_up:
545 * @bin: #Gstbin to search
546 * @name: the element name to search for
548 * Get the element with the given name from this bin. If the
549 * element is not found, a recursion is performed on the parent bin.
551 * Returns: the element with the given name
554 gst_bin_get_by_name_recurse_up (GstBin *bin,
557 GstElement *result = NULL;
560 g_return_val_if_fail (bin != NULL, NULL);
561 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
562 g_return_val_if_fail (name != NULL, NULL);
564 result = gst_bin_get_by_name (bin, name);
569 parent = gst_object_get_parent (GST_OBJECT (bin));
571 if (parent && GST_IS_BIN (parent)) {
572 result = gst_bin_get_by_name_recurse_up (GST_BIN (parent), name);
580 * @bin: #Gstbin to get the list from
582 * Get the list of elements in this bin.
584 * Returns: a GList of elements
587 gst_bin_get_list (GstBin *bin)
589 g_return_val_if_fail (bin != NULL, NULL);
590 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
592 return bin->children;
595 #ifndef GST_DISABLE_LOADSAVE
597 gst_bin_save_thyself (GstObject *object,
600 GstBin *bin = GST_BIN (object);
601 xmlNodePtr childlist, elementnode;
605 if (GST_OBJECT_CLASS (parent_class)->save_thyself)
606 GST_OBJECT_CLASS (parent_class)->save_thyself (GST_OBJECT (bin), parent);
608 childlist = xmlNewChild (parent, NULL, "children", NULL);
610 GST_INFO_ELEMENT (GST_CAT_XML, bin, "saving %d children", bin->numchildren);
612 children = bin->children;
614 child = GST_ELEMENT (children->data);
615 elementnode = xmlNewChild (childlist, NULL, "element", NULL);
616 gst_object_save_thyself (GST_OBJECT (child), elementnode);
617 children = g_list_next (children);
623 gst_bin_restore_thyself (GstObject *object,
626 GstBin *bin = GST_BIN (object);
627 xmlNodePtr field = self->xmlChildrenNode;
628 xmlNodePtr childlist;
631 if (!strcmp (field->name, "children")) {
632 GST_INFO_ELEMENT (GST_CAT_XML, GST_ELEMENT (object), "loading children");
633 childlist = field->xmlChildrenNode;
635 if (!strcmp (childlist->name, "element")) {
636 GstElement *element = gst_element_restore_thyself (childlist, GST_OBJECT (bin));
638 gst_bin_add (bin, element);
640 childlist = childlist->next;
647 #endif /* GST_DISABLE_LOADSAVE */
652 * @bin: #Gstbin to iterate
654 * Iterates over the elements in this bin.
656 * Returns: TRUE if the bin did something usefull. This value
657 * can be used to determine it the bin is in EOS.
660 gst_bin_iterate (GstBin *bin)
665 GST_DEBUG_ENTER("(\"%s\")",GST_ELEMENT_NAME (bin));
667 oclass = GST_BIN_CLASS (G_OBJECT_GET_CLASS(bin));
670 eos = (oclass->iterate) (bin);
672 GST_DEBUG_LEAVE("(\"%s\")",GST_ELEMENT_NAME (bin));
677 /* out internal element fired EOS, we decrement the number of pending EOS childs */
678 G_GNUC_UNUSED static void
679 gst_bin_received_eos (GstElement *element, GstBin *bin)
681 GST_INFO_ELEMENT (GST_CAT_PLANNING, bin, "child %s fired eos, pending %d", GST_ELEMENT_NAME (element),
682 bin->num_eos_providers);
685 if (bin->num_eos_providers) {
686 bin->num_eos_providers--;
687 g_cond_signal (bin->eoscond);
699 gst_bin_iterate_func (GstBin *bin)
701 // only iterate if this is the manager bin
702 if (GST_ELEMENT_SCHED(bin)->parent == GST_ELEMENT (bin)) {
703 return GST_SCHEDULE_ITERATE(GST_ELEMENT_SCHED(bin));
705 GST_DEBUG (GST_CAT_SCHEDULING, "this bin can't be iterated on!\n");