3 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
4 * 2000 Wim Taymans <wtay@chello.be>
6 * gstbin.c: GstBin container object and support code
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
24 #include "gst_private.h"
28 #include "gstmarshal.h"
33 #include "gstscheduler.h"
36 static GstElementDetails gst_bin_details = GST_ELEMENT_DETAILS ("Generic bin",
38 "Simple container object",
39 "Erik Walthinsen <omega@cse.ogi.edu>");
41 GType _gst_bin_type = 0;
43 static gboolean _gst_boolean_did_something_accumulator (GSignalInvocationHint *
44 ihint, GValue * return_accu, const GValue * handler_return, gpointer dummy);
46 static void gst_bin_dispose (GObject * object);
48 static GstElementStateReturn gst_bin_change_state (GstElement * element);
49 static GstElementStateReturn gst_bin_change_state_norecurse (GstBin * bin);
51 #ifndef GST_DISABLE_INDEX
52 static void gst_bin_set_index (GstElement * element, GstIndex * index);
55 static void gst_bin_add_func (GstBin * bin, GstElement * element);
56 static void gst_bin_remove_func (GstBin * bin, GstElement * element);
57 static void gst_bin_child_state_change_func (GstBin * bin,
58 GstElementState oldstate, GstElementState newstate, GstElement * child);
60 static GstClock *gst_bin_get_clock_func (GstElement * element);
61 static void gst_bin_set_clock_func (GstElement * element, GstClock * clock);
63 static gboolean gst_bin_iterate_func (GstBin * bin);
65 #ifndef GST_DISABLE_LOADSAVE
66 static xmlNodePtr gst_bin_save_thyself (GstObject * object, xmlNodePtr parent);
67 static void gst_bin_restore_thyself (GstObject * object, xmlNodePtr self);
70 /* Bin signals and args */
85 static void gst_bin_base_init (gpointer g_class);
86 static void gst_bin_class_init (GstBinClass * klass);
87 static void gst_bin_init (GstBin * bin);
89 static GstElementClass *parent_class = NULL;
90 static guint gst_bin_signals[LAST_SIGNAL] = { 0 };
95 * Returns: the type of #GstBin
98 gst_bin_get_type (void)
100 if (!_gst_bin_type) {
101 static const GTypeInfo bin_info = {
102 sizeof (GstBinClass),
105 (GClassInitFunc) gst_bin_class_init,
110 (GInstanceInitFunc) gst_bin_init,
115 g_type_register_static (GST_TYPE_ELEMENT, "GstBin", &bin_info, 0);
117 return _gst_bin_type;
121 gst_bin_base_init (gpointer g_class)
123 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
125 gst_element_class_set_details (gstelement_class, &gst_bin_details);
129 gst_bin_class_init (GstBinClass * klass)
131 GObjectClass *gobject_class;
132 GstObjectClass *gstobject_class;
133 GstElementClass *gstelement_class;
135 gobject_class = (GObjectClass *) klass;
136 gstobject_class = (GstObjectClass *) klass;
137 gstelement_class = (GstElementClass *) klass;
139 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
141 gst_bin_signals[ELEMENT_ADDED] =
142 g_signal_new ("element-added", G_TYPE_FROM_CLASS (klass),
143 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstBinClass, element_added), NULL,
144 NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
145 gst_bin_signals[ELEMENT_REMOVED] =
146 g_signal_new ("element-removed", G_TYPE_FROM_CLASS (klass),
147 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstBinClass, element_removed), NULL,
148 NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
149 gst_bin_signals[ITERATE] =
150 g_signal_new ("iterate", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
151 G_STRUCT_OFFSET (GstBinClass, iterate),
152 _gst_boolean_did_something_accumulator, NULL, gst_marshal_BOOLEAN__VOID,
155 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_bin_dispose);
157 #ifndef GST_DISABLE_LOADSAVE
158 gstobject_class->save_thyself = GST_DEBUG_FUNCPTR (gst_bin_save_thyself);
159 gstobject_class->restore_thyself =
160 GST_DEBUG_FUNCPTR (gst_bin_restore_thyself);
163 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_bin_change_state);
164 #ifndef GST_DISABLE_INDEX
165 gstelement_class->set_index = GST_DEBUG_FUNCPTR (gst_bin_set_index);
168 klass->add_element = GST_DEBUG_FUNCPTR (gst_bin_add_func);
169 klass->remove_element = GST_DEBUG_FUNCPTR (gst_bin_remove_func);
170 klass->child_state_change =
171 GST_DEBUG_FUNCPTR (gst_bin_child_state_change_func);
172 klass->iterate = GST_DEBUG_FUNCPTR (gst_bin_iterate_func);
176 _gst_boolean_did_something_accumulator (GSignalInvocationHint * ihint,
177 GValue * return_accu, const GValue * handler_return, gpointer dummy)
179 gboolean did_something;
181 did_something = g_value_get_boolean (handler_return);
183 g_value_set_boolean (return_accu, TRUE);
186 /* always continue emission */
191 gst_bin_init (GstBin * bin)
193 /* in general, we prefer to use cothreads for most things */
194 GST_FLAG_SET (bin, GST_BIN_FLAG_PREFER_COTHREADS);
196 bin->numchildren = 0;
197 bin->children = NULL;
202 * @name: name of new bin
204 * Create a new bin with given name.
209 gst_bin_new (const gchar * name)
211 return gst_element_factory_make ("bin", name);
215 gst_bin_get_clock_func (GstElement * element)
217 if (GST_ELEMENT_SCHED (element))
218 return gst_scheduler_get_clock (GST_ELEMENT_SCHED (element));
224 gst_bin_set_clock_func (GstElement * element, GstClock * clock)
226 if (GST_ELEMENT_SCHED (element))
227 gst_scheduler_use_clock (GST_ELEMENT_SCHED (element), clock);
232 * @bin: a #GstBin to get the clock of
234 * Gets the current clock of the (scheduler of the) bin.
236 * Returns: the #GstClock of the bin
239 gst_bin_get_clock (GstBin * bin)
241 g_return_val_if_fail (bin != NULL, NULL);
242 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
244 return gst_bin_get_clock_func (GST_ELEMENT (bin));
249 * @bin: the bin to set the clock for
250 * @clock: the clock to use.
252 * Force the bin to use the given clock. Use NULL to
253 * force it to use no clock at all.
256 gst_bin_use_clock (GstBin * bin, GstClock * clock)
258 g_return_if_fail (GST_IS_BIN (bin));
260 gst_bin_set_clock_func (GST_ELEMENT (bin), clock);
264 * gst_bin_auto_clock:
265 * @bin: the bin to autoclock
267 * Let the bin select a clock automatically.
270 gst_bin_auto_clock (GstBin * bin)
272 g_return_if_fail (GST_IS_BIN (bin));
274 if (GST_ELEMENT_SCHED (bin))
275 gst_scheduler_auto_clock (GST_ELEMENT_SCHED (bin));
278 #ifndef GST_DISABLE_INDEX
280 gst_bin_set_index (GstElement * element, GstIndex * index)
282 GstBin *bin = GST_BIN (element);
284 g_return_if_fail (GST_IS_BIN (bin));
286 g_list_foreach (bin->children, (GFunc) gst_element_set_index, index);
291 gst_bin_set_element_sched (GstElement * element, GstScheduler * sched)
293 GST_CAT_LOG (GST_CAT_SCHEDULING, "setting element \"%s\" sched to %p",
294 GST_ELEMENT_NAME (element), sched);
296 /* if it's actually a Bin */
297 if (GST_IS_BIN (element)) {
298 if (GST_FLAG_IS_SET (element, GST_BIN_FLAG_MANAGER)) {
299 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, element,
300 "child is already a manager, not resetting sched");
301 if (GST_ELEMENT_SCHED (element))
302 gst_scheduler_add_scheduler (sched, GST_ELEMENT_SCHED (element));
306 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, element,
307 "setting child bin's scheduler to be the same as the parent's");
308 gst_scheduler_add_element (sched, element);
310 /* set the children's schedule */
311 g_list_foreach (GST_BIN (element)->children,
312 (GFunc) gst_bin_set_element_sched, sched);
314 /* otherwise, if it's just a regular old element */
315 else if (!GST_FLAG_IS_SET (element, GST_ELEMENT_DECOUPLED)) {
318 gst_scheduler_add_element (sched, element);
320 /* set the sched pointer in all the pads */
321 pads = element->pads;
325 pad = GST_PAD (pads->data);
326 pads = g_list_next (pads);
328 /* we only operate on real pads */
329 if (!GST_IS_REAL_PAD (pad))
332 /* if the peer element exists and is a candidate */
333 if (GST_PAD_PEER (pad)) {
334 if (gst_pad_get_scheduler (GST_PAD_PEER (pad)) == sched) {
335 GST_CAT_LOG (GST_CAT_SCHEDULING,
336 "peer is in same scheduler, telling scheduler");
338 if (GST_PAD_IS_SRC (pad))
339 gst_scheduler_pad_link (sched, pad, GST_PAD_PEER (pad));
341 gst_scheduler_pad_link (sched, GST_PAD_PEER (pad), pad);
350 gst_bin_unset_element_sched (GstElement * element, GstScheduler * sched)
352 if (GST_ELEMENT_SCHED (element) == NULL) {
353 GST_CAT_DEBUG (GST_CAT_SCHEDULING, "element \"%s\" has no scheduler",
354 GST_ELEMENT_NAME (element));
358 GST_CAT_DEBUG (GST_CAT_SCHEDULING,
359 "removing element \"%s\" from its sched %p", GST_ELEMENT_NAME (element),
360 GST_ELEMENT_SCHED (element));
362 /* if it's actually a Bin */
363 if (GST_IS_BIN (element)) {
365 if (GST_FLAG_IS_SET (element, GST_BIN_FLAG_MANAGER)) {
366 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, element,
367 "child is already a manager, not unsetting sched");
369 gst_scheduler_remove_scheduler (sched, GST_ELEMENT_SCHED (element));
373 /* for each child, remove them from their schedule */
374 g_list_foreach (GST_BIN (element)->children,
375 (GFunc) gst_bin_unset_element_sched, sched);
377 gst_scheduler_remove_element (GST_ELEMENT_SCHED (element), element);
378 } else if (!GST_FLAG_IS_SET (element, GST_ELEMENT_DECOUPLED)) {
379 /* otherwise, if it's just a regular old element */
382 /* set the sched pointer in all the pads */
383 pads = element->pads;
387 pad = GST_PAD (pads->data);
388 pads = g_list_next (pads);
390 /* we only operate on real pads */
391 if (!GST_IS_REAL_PAD (pad))
394 /* if the peer element exists and is a candidate */
395 if (GST_PAD_PEER (pad)) {
396 if (gst_pad_get_scheduler (GST_PAD_PEER (pad)) == sched) {
397 GST_CAT_LOG (GST_CAT_SCHEDULING,
398 "peer is in same scheduler, telling scheduler");
400 if (GST_PAD_IS_SRC (pad))
401 gst_scheduler_pad_unlink (sched, pad, GST_PAD_PEER (pad));
403 gst_scheduler_pad_unlink (sched, GST_PAD_PEER (pad), pad);
407 gst_scheduler_remove_element (GST_ELEMENT_SCHED (element), element);
414 * @bin: the bin to add the elements to
415 * @element_1: the first element to add to the bin
416 * @...: additional elements to add to the bin
418 * Adds a NULL-terminated list of elements to a bin. This function is
419 * equivalent to calling #gst_bin_add() for each member of the list.
422 gst_bin_add_many (GstBin * bin, GstElement * element_1, ...)
426 g_return_if_fail (GST_IS_BIN (bin));
427 g_return_if_fail (GST_IS_ELEMENT (element_1));
429 va_start (args, element_1);
432 gst_bin_add (bin, element_1);
434 element_1 = va_arg (args, GstElement *);
441 gst_bin_add_func (GstBin * bin, GstElement * element)
444 GstElementState state;
447 /* the element must not already have a parent */
448 g_return_if_fail (GST_ELEMENT_PARENT (element) == NULL);
450 /* then check to see if the element's name is already taken in the bin */
451 if (gst_object_check_uniqueness (bin->children,
452 GST_ELEMENT_NAME (element)) == FALSE) {
453 g_warning ("Name %s is not unique in bin %s, not adding\n",
454 GST_ELEMENT_NAME (element), GST_ELEMENT_NAME (bin));
458 /* set the element's parent and add the element to the bin's list of children */
459 gst_object_set_parent (GST_OBJECT (element), GST_OBJECT (bin));
461 bin->children = g_list_append (bin->children, element);
464 /* bump our internal state counter */
465 state = GST_STATE (element);
468 bin->child_states[state_idx]++;
470 /* now we have to deal with manager stuff
471 * we can only do this if there's a scheduler:
472 * if we're not a manager, and aren't attached to anything, we have no sched (yet) */
473 sched = GST_ELEMENT_SCHED (bin);
475 gst_bin_set_element_sched (element, sched);
478 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, bin, "added element \"%s\"",
479 GST_OBJECT_NAME (element));
481 g_signal_emit (G_OBJECT (bin), gst_bin_signals[ELEMENT_ADDED], 0, element);
486 * @bin: #GstBin to add element to
487 * @element: #GstElement to add to bin
489 * Adds the given element to the bin. Sets the element's parent, and thus
493 gst_bin_add (GstBin * bin, GstElement * element)
497 g_return_if_fail (GST_IS_BIN (bin));
498 g_return_if_fail (GST_IS_ELEMENT (element));
500 GST_CAT_INFO_OBJECT (GST_CAT_PARENTAGE, bin, "adding element \"%s\"",
501 GST_OBJECT_NAME (element));
503 bclass = GST_BIN_GET_CLASS (bin);
505 if (bclass->add_element) {
506 bclass->add_element (bin, element);
508 GST_ELEMENT_ERROR (bin, CORE, FAILED, (NULL),
509 ("cannot add element %s to bin %s",
510 GST_ELEMENT_NAME (element), GST_ELEMENT_NAME (bin)));
515 gst_bin_remove_func (GstBin * bin, GstElement * element)
518 GstElementState state;
520 /* the element must have its parent set to the current bin */
521 g_return_if_fail (GST_ELEMENT_PARENT (element) == (GstObject *) bin);
523 /* the element must be in the bin's list of children */
524 if (g_list_find (bin->children, element) == NULL) {
525 g_warning ("no element \"%s\" in bin \"%s\"\n", GST_ELEMENT_NAME (element),
526 GST_ELEMENT_NAME (bin));
530 /* remove this element from the list of managed elements */
531 gst_bin_unset_element_sched (element, GST_ELEMENT_SCHED (bin));
533 /* now remove the element from the list of elements */
534 bin->children = g_list_remove (bin->children, element);
537 /* bump our internal state counter */
538 state = GST_STATE (element);
541 bin->child_states[state_idx]--;
543 GST_CAT_INFO_OBJECT (GST_CAT_PARENTAGE, bin, "removed child \"%s\"",
544 GST_OBJECT_NAME (element));
546 /* ref as we're going to emit a signal */
547 gst_object_ref (GST_OBJECT (element));
548 gst_object_unparent (GST_OBJECT (element));
550 /* if we're down to zero children, force state to NULL */
551 while (bin->numchildren == 0 && GST_ELEMENT_SCHED (bin) != NULL &&
552 GST_STATE (bin) > GST_STATE_NULL) {
553 GstElementState next = GST_STATE (bin) >> 1;
555 GST_STATE_PENDING (bin) = next;
556 gst_bin_change_state_norecurse (bin);
557 if (!GST_STATE (bin) == next) {
558 g_warning ("bin %s failed state change to %d", GST_ELEMENT_NAME (bin),
563 g_signal_emit (G_OBJECT (bin), gst_bin_signals[ELEMENT_REMOVED], 0, element);
565 /* element is really out of our control now */
566 gst_object_unref (GST_OBJECT (element));
571 * @bin: #GstBin to remove element from
572 * @element: #GstElement to remove
574 * Remove the element from its associated bin, unparenting it as well.
575 * Unparenting the element means that the element will be dereferenced,
576 * so if the bin holds the only reference to the element, the element
577 * will be freed in the process of removing it from the bin. If you
578 * want the element to still exist after removing, you need to call
579 * #gst_object_ref before removing it from the bin.
582 gst_bin_remove (GstBin * bin, GstElement * element)
586 GST_CAT_DEBUG (GST_CAT_PARENTAGE, "[%s]: trying to remove child %s",
587 GST_ELEMENT_NAME (bin), GST_ELEMENT_NAME (element));
589 g_return_if_fail (GST_IS_BIN (bin));
590 g_return_if_fail (GST_IS_ELEMENT (element));
591 g_return_if_fail (bin->children != NULL);
593 bclass = GST_BIN_GET_CLASS (bin);
595 if (bclass->remove_element) {
596 bclass->remove_element (bin, element);
598 g_warning ("cannot remove elements from bin %s\n", GST_ELEMENT_NAME (bin));
603 * gst_bin_remove_many:
604 * @bin: the bin to remove the elements from
605 * @element_1: the first element to remove from the bin
606 * @...: NULL-terminated list of elements to remove from the bin
608 * Remove a list of elements from a bin. This function is equivalent
609 * to calling #gst_bin_remove with each member of the list.
612 gst_bin_remove_many (GstBin * bin, GstElement * element_1, ...)
616 g_return_if_fail (GST_IS_BIN (bin));
617 g_return_if_fail (GST_IS_ELEMENT (element_1));
619 va_start (args, element_1);
622 gst_bin_remove (bin, element_1);
624 element_1 = va_arg (args, GstElement *);
631 * gst_bin_child_state_change:
632 * @bin: #GstBin with the child
633 * @oldstate: The old child state
634 * @newstate: The new child state
635 * @child: #GstElement that signaled an changed state
637 * An internal function to inform the parent bin about a state change
641 gst_bin_child_state_change (GstBin * bin, GstElementState oldstate,
642 GstElementState newstate, GstElement * child)
646 g_return_if_fail (GST_IS_BIN (bin));
647 g_return_if_fail (GST_IS_ELEMENT (child));
649 GST_CAT_LOG (GST_CAT_STATES, "child %s changed state in bin %s from %s to %s",
650 GST_ELEMENT_NAME (child), GST_ELEMENT_NAME (bin),
651 gst_element_state_get_name (oldstate),
652 gst_element_state_get_name (newstate));
654 bclass = GST_BIN_GET_CLASS (bin);
656 if (bclass->child_state_change) {
657 bclass->child_state_change (bin, oldstate, newstate, child);
659 g_warning ("cannot signal state change of child %s to bin %s\n",
660 GST_ELEMENT_NAME (child), GST_ELEMENT_NAME (bin));
665 gst_bin_child_state_change_func (GstBin * bin, GstElementState oldstate,
666 GstElementState newstate, GstElement * child)
668 gint old_idx = 0, new_idx = 0, i;
670 while (oldstate >>= 1)
672 while (newstate >>= 1)
676 bin->child_states[old_idx]--;
677 bin->child_states[new_idx]++;
679 for (i = GST_NUM_STATES - 1; i >= 0; i--) {
680 if (bin->child_states[i] != 0) {
681 gint state = (1 << i);
683 if (GST_STATE (bin) != state) {
684 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
685 "highest child state is %s, changing bin state accordingly",
686 gst_element_state_get_name (state));
687 GST_STATE_PENDING (bin) = state;
689 gst_bin_change_state_norecurse (bin);
690 if (state != GST_STATE (bin)) {
691 g_warning ("%s: state change in callback %d %d",
692 GST_ELEMENT_NAME (bin), state, GST_STATE (bin));
702 static GstElementStateReturn
703 gst_bin_change_state (GstElement * element)
708 GstElementStateReturn ret;
709 GstElementState old_state, pending;
711 gboolean have_async = FALSE;
713 g_return_val_if_fail (GST_IS_BIN (element), GST_STATE_FAILURE);
715 bin = GST_BIN (element);
717 old_state = GST_STATE (element);
718 pending = GST_STATE_PENDING (element);
719 transition = GST_STATE_TRANSITION (element);
721 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
722 "changing state of children from %s to %s",
723 gst_element_state_get_name (old_state),
724 gst_element_state_get_name (pending));
726 if (pending == GST_STATE_VOID_PENDING)
727 return GST_STATE_SUCCESS;
729 /* we want to recurse into children anyway, regardless of our old
732 if (old_state == pending) {
733 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element,
734 "old and pending state are both %s, returning",
735 gst_element_state_get_name (pending));
736 return GST_STATE_SUCCESS;
740 children = bin->children;
743 GstElementState old_child_state;
745 child = GST_ELEMENT (children->data);
747 if (GST_FLAG_IS_SET (child, GST_ELEMENT_LOCKED_STATE)) {
748 children = g_list_next (children);
752 old_child_state = GST_STATE (child);
754 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
755 "changing state of child %s from current %s to pending %s",
756 GST_ELEMENT_NAME (child), gst_element_state_get_name (old_child_state),
757 gst_element_state_get_name (pending));
759 switch (gst_element_set_state (child, pending)) {
760 case GST_STATE_FAILURE:
761 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
762 "child '%s' failed to go to state %d(%s)",
763 GST_ELEMENT_NAME (child),
764 pending, gst_element_state_get_name (pending));
766 gst_element_set_state (child, old_child_state);
767 /* There was a check for elements being in the same scheduling group
768 here. Removed by dolphy <julien@moutte.net>. No matter the
769 scheduling group we should always return a failure. This change
770 seems to work on my machine and fixes tons of issues. If anyone
771 want to revert please tell me what it breaks first, Thanks. */
772 GST_STATE_PENDING (element) = old_state;
773 return GST_STATE_FAILURE;
775 case GST_STATE_ASYNC:
776 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
777 "child '%s' is changing state asynchronously",
778 GST_ELEMENT_NAME (child));
781 case GST_STATE_SUCCESS:
782 GST_CAT_DEBUG (GST_CAT_STATES,
783 "child '%s' changed state to %d(%s) successfully",
784 GST_ELEMENT_NAME (child), pending,
785 gst_element_state_get_name (pending));
788 /* we need to do this down here, because there might be elements removed
789 * from this bin during state changes, so g_list_next (children) might
791 children = g_list_next (children);
794 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
795 "done changing bin's state from %s to %s, now in %s",
796 gst_element_state_get_name (old_state),
797 gst_element_state_get_name (pending),
798 gst_element_state_get_name (GST_STATE (element)));
801 ret = GST_STATE_ASYNC;
803 if (parent_class->change_state) {
804 ret = parent_class->change_state (element);
806 ret = GST_STATE_SUCCESS;
812 static GstElementStateReturn
813 gst_bin_change_state_norecurse (GstBin * bin)
815 GstElementStateReturn ret;
817 if (parent_class->change_state) {
818 GST_CAT_LOG_OBJECT (GST_CAT_STATES, bin, "setting bin's own state");
819 ret = parent_class->change_state (GST_ELEMENT (bin));
823 return GST_STATE_FAILURE;
827 gst_bin_dispose (GObject * object)
829 GstBin *bin = GST_BIN (object);
831 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, object, "dispose");
833 if (gst_element_get_state (GST_ELEMENT (object)) == GST_STATE_PLAYING)
834 gst_element_set_state (GST_ELEMENT (object), GST_STATE_PAUSED);
836 while (bin->children) {
837 gst_bin_remove (bin, GST_ELEMENT (bin->children->data));
839 g_assert (bin->children == NULL);
840 g_assert (bin->numchildren == 0);
842 G_OBJECT_CLASS (parent_class)->dispose (object);
846 * gst_bin_get_by_name:
847 * @bin: #Gstbin to search
848 * @name: the element name to search for
850 * Get the element with the given name from this bin.
852 * Returns: the element with the given name
855 gst_bin_get_by_name (GstBin * bin, const gchar * name)
860 g_return_val_if_fail (bin != NULL, NULL);
861 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
862 g_return_val_if_fail (name != NULL, NULL);
864 GST_CAT_INFO (GST_CAT_PARENTAGE, "[%s]: looking up child element %s",
865 GST_ELEMENT_NAME (bin), name);
867 children = bin->children;
869 child = GST_ELEMENT (children->data);
870 if (!strcmp (GST_OBJECT_NAME (child), name))
872 if (GST_IS_BIN (child)) {
873 GstElement *res = gst_bin_get_by_name (GST_BIN (child), name);
878 children = g_list_next (children);
885 * gst_bin_get_by_name_recurse_up:
886 * @bin: #Gstbin to search
887 * @name: the element name to search for
889 * Get the element with the given name from this bin. If the
890 * element is not found, a recursion is performed on the parent bin.
892 * Returns: the element with the given name
895 gst_bin_get_by_name_recurse_up (GstBin * bin, const gchar * name)
897 GstElement *result = NULL;
900 g_return_val_if_fail (bin != NULL, NULL);
901 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
902 g_return_val_if_fail (name != NULL, NULL);
904 result = gst_bin_get_by_name (bin, name);
907 parent = gst_object_get_parent (GST_OBJECT (bin));
909 if (parent && GST_IS_BIN (parent)) {
910 result = gst_bin_get_by_name_recurse_up (GST_BIN (parent), name);
919 * @bin: #Gstbin to get the list from
921 * Get the list of elements in this bin.
923 * Returns: a GList of elements
926 gst_bin_get_list (GstBin * bin)
928 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
930 return bin->children;
934 * gst_bin_get_by_interface:
935 * @bin: bin to find element in
936 * @interface: interface to be implemented by interface
938 * Looks for the first element inside the bin that implements the given
939 * interface. If such an element is found, it returns the element. You can
940 * cast this element to the given interface afterwards.
941 * If you want all elements that implement the interface, use
942 * gst_bin_get_all_by_interface(). The function recurses bins inside bins.
944 * Returns: An element inside the bin implementing the interface.
947 gst_bin_get_by_interface (GstBin * bin, GType interface)
951 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
952 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface), NULL);
954 walk = bin->children;
956 if (G_TYPE_CHECK_INSTANCE_TYPE (walk->data, interface))
957 return GST_ELEMENT (walk->data);
958 if (GST_IS_BIN (walk->data)) {
961 ret = gst_bin_get_by_interface (GST_BIN (walk->data), interface);
965 walk = g_list_next (walk);
972 * gst_bin_get_all_by_interface:
973 * @bin: bin to find elements in
974 * @interface: interface to be implemented by interface
976 * Looks for all elements inside the bin that implements the given
977 * interface. You can safely cast all returned elements to the given interface.
978 * The function recurses bins inside bins. You need to free the list using
979 * g_list_free() after use.
981 * Returns: An element inside the bin implementing the interface.
984 gst_bin_get_all_by_interface (GstBin * bin, GType interface)
986 GList *walk, *ret = NULL;
988 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
989 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface), NULL);
991 walk = bin->children;
993 if (G_TYPE_CHECK_INSTANCE_TYPE (walk->data, interface)) {
994 GST_DEBUG_OBJECT (bin, "element %s implements requested interface",
995 GST_ELEMENT_NAME (GST_ELEMENT (walk->data)));
996 ret = g_list_prepend (ret, walk->data);
998 if (GST_IS_BIN (walk->data)) {
999 ret = g_list_concat (ret,
1000 gst_bin_get_all_by_interface (GST_BIN (walk->data), interface));
1002 walk = g_list_next (walk);
1009 * gst_bin_sync_children_state:
1010 * @bin: #Gstbin to sync state
1012 * Tries to set the state of the children of this bin to the same state of the
1013 * bin by calling gst_element_set_state for each child not already having a
1014 * synchronized state.
1016 * Returns: The worst return value of any gst_element_set_state. So if one child
1017 * returns #GST_STATE_FAILURE while all others return #GST_STATE_SUCCESS
1018 * this function returns #GST_STATE_FAILURE.
1020 GstElementStateReturn
1021 gst_bin_sync_children_state (GstBin * bin)
1024 GstElement *element;
1025 GstElementState state;
1026 GstElementStateReturn ret = GST_STATE_SUCCESS;
1028 g_return_val_if_fail (GST_IS_BIN (bin), GST_STATE_FAILURE);
1030 state = GST_STATE (bin);
1031 children = bin->children;
1032 GST_CAT_INFO (GST_CAT_STATES,
1033 "syncing state of children with bin \"%s\"'s state %s",
1034 GST_ELEMENT_NAME (bin), gst_element_state_get_name (state));
1037 element = GST_ELEMENT (children->data);
1038 children = children->next;
1039 if (GST_STATE (element) != state) {
1040 switch (gst_element_set_state (element, state)) {
1041 case GST_STATE_SUCCESS:
1043 case GST_STATE_ASYNC:
1044 if (ret == GST_STATE_SUCCESS)
1045 ret = GST_STATE_ASYNC;
1047 case GST_STATE_FAILURE:
1048 ret = GST_STATE_FAILURE;
1050 /* make sure gst_element_set_state never returns this */
1051 g_assert_not_reached ();
1059 #ifndef GST_DISABLE_LOADSAVE
1061 gst_bin_save_thyself (GstObject * object, xmlNodePtr parent)
1063 GstBin *bin = GST_BIN (object);
1064 xmlNodePtr childlist, elementnode;
1068 if (GST_OBJECT_CLASS (parent_class)->save_thyself)
1069 GST_OBJECT_CLASS (parent_class)->save_thyself (GST_OBJECT (bin), parent);
1071 childlist = xmlNewChild (parent, NULL, "children", NULL);
1073 GST_CAT_INFO (GST_CAT_XML, "[%s]: saving %d children",
1074 GST_ELEMENT_NAME (bin), bin->numchildren);
1076 children = bin->children;
1078 child = GST_ELEMENT (children->data);
1079 elementnode = xmlNewChild (childlist, NULL, "element", NULL);
1080 gst_object_save_thyself (GST_OBJECT (child), elementnode);
1081 children = g_list_next (children);
1087 gst_bin_restore_thyself (GstObject * object, xmlNodePtr self)
1089 GstBin *bin = GST_BIN (object);
1090 xmlNodePtr field = self->xmlChildrenNode;
1091 xmlNodePtr childlist;
1094 if (!strcmp (field->name, "children")) {
1095 GST_CAT_INFO (GST_CAT_XML, "[%s]: loading children",
1096 GST_ELEMENT_NAME (object));
1097 childlist = field->xmlChildrenNode;
1099 if (!strcmp (childlist->name, "element")) {
1100 GstElement *element =
1101 gst_xml_make_element (childlist, GST_OBJECT (bin));
1103 /* it had to be parented to find the pads, now we ref and unparent so
1104 * we can add it to the bin */
1105 gst_object_ref (GST_OBJECT (element));
1106 gst_object_unparent (GST_OBJECT (element));
1108 gst_bin_add (bin, element);
1110 childlist = childlist->next;
1114 field = field->next;
1116 if (GST_OBJECT_CLASS (parent_class)->restore_thyself)
1117 (GST_OBJECT_CLASS (parent_class)->restore_thyself) (object, self);
1119 #endif /* GST_DISABLE_LOADSAVE */
1122 gst_bin_iterate_func (GstBin * bin)
1124 GstScheduler *sched = GST_ELEMENT_SCHED (bin);
1126 /* only iterate if this is the manager bin */
1127 if (sched && sched->parent == GST_ELEMENT (bin)) {
1128 GstSchedulerState state;
1130 state = gst_scheduler_iterate (sched);
1132 if (state == GST_SCHEDULER_STATE_RUNNING) {
1134 } else if (state == GST_SCHEDULER_STATE_ERROR) {
1135 gst_element_set_state (GST_ELEMENT (bin), GST_STATE_PAUSED);
1136 } else if (state == GST_SCHEDULER_STATE_STOPPED) {
1137 /* check if we have children scheds that are still running */
1138 /* FIXME: remove in 0.9? autouseless because iterations gone? */
1141 for (walk = sched->schedulers; walk; walk = g_list_next (walk)) {
1142 GstScheduler *test = walk->data;
1144 g_return_val_if_fail (test->parent, FALSE);
1145 if (GST_STATE (test->parent) == GST_STATE_PLAYING) {
1146 GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, bin,
1147 "current bin is not iterating, but children are, "
1148 "so returning TRUE anyway...");
1155 g_warning ("bin \"%s\" is not the managing bin, can't be iterated on!\n",
1156 GST_ELEMENT_NAME (bin));
1164 * @bin: a#GstBin to iterate.
1166 * Iterates over the elements in this bin.
1168 * Returns: TRUE if the bin did something useful. This value
1169 * can be used to determine it the bin is in EOS.
1172 gst_bin_iterate (GstBin * bin)
1176 g_return_val_if_fail (bin != NULL, FALSE);
1177 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1179 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, bin, "starting iteration");
1180 gst_object_ref (GST_OBJECT (bin));
1183 g_signal_emit (G_OBJECT (bin), gst_bin_signals[ITERATE], 0, &running);
1185 gst_object_unref (GST_OBJECT (bin));
1186 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, bin, "finished iteration");