## GstBin GstBin — Base class and element that can contain other elements |

## Synopsis

    #include <gst/gst.h>

    struct              GstBin;
    struct              GstBinClass;
    GstElement *        gst_bin_new                         (const gchar *name);
    gboolean            gst_bin_add                         (GstBin *bin,
                                                             GstElement *element);
    gboolean            gst_bin_remove                      (GstBin *bin,
                                                             GstElement *element);
    GstElement *        gst_bin_get_by_name                 (GstBin *bin,
                                                             const gchar *name);
    GstElement *        gst_bin_get_by_name_recurse_up      (GstBin *bin,
                                                             const gchar *name);
    GstElement *        gst_bin_get_by_interface            (GstBin *bin,
                                                             GType iface);
    GstIterator *       gst_bin_iterate_elements            (GstBin *bin);
    GstIterator *       gst_bin_iterate_recurse             (GstBin *bin);
    GstIterator *       gst_bin_iterate_sinks               (GstBin *bin);
    GstIterator *       gst_bin_iterate_sorted              (GstBin *bin);
    GstIterator *       gst_bin_iterate_sources             (GstBin *bin);
    GstIterator *       gst_bin_iterate_all_by_interface    (GstBin *bin,
                                                             GType iface);
    gboolean            gst_bin_recalculate_latency         (GstBin *bin);

    void                gst_bin_add_many                    (GstBin *bin,
                                                             GstElement *element_1,
                                                             ...);
    void                gst_bin_remove_many                 (GstBin *bin,
                                                             GstElement *element_1,
                                                             ...);
    GstPad *            gst_bin_find_unlinked_pad           (GstBin *bin,
                                                             GstPadDirection direction);
    GstPad *            gst_bin_find_unconnected_pad        (GstBin *bin,
                                                             GstPadDirection direction);

    enum                GstBinFlags;
    #define             GST_BIN_CHILDREN                    (bin)
    #define             GST_BIN_CHILDREN_COOKIE             (bin)
    #define             GST_BIN_NUMCHILDREN                 (bin)

## Object Hierarchy

      GObject
       +----GstObject
             +----GstElement
                   +----GstBin
                         +----GstPipeline

## Implemented Interfaces

GstBin implements GstChildProxy.

## Properties

      "async-handling"           gboolean              : Read / Write
      "message-forward"          gboolean              : Read / Write

## Signals

      "do-latency"                                     : Run Last
      "element-added"                                  : Run First
      "element-removed"                                : Run First

## Description

GstBin is an element that can contain other GstElement, allowing them to be managed as a group. Pads from the child elements can be ghosted to the bin, see GstGhostPad. This makes the bin look like any other elements and enables creation of higher-level abstraction elements.

A new GstBin is created with `gst_bin_new()`. Use a GstPipeline instead if you want to create a toplevel bin because a normal bin doesn't have a bus or handle clock distribution of its own.

After the bin has been created you will typically add elements to it with `gst_bin_add()`. You can remove elements with `gst_bin_remove()`.

An element can be retrieved from a bin with `gst_bin_get_by_name()`, using the elements name. `gst_bin_get_by_name_recurse_up()` is mainly used for internal purposes and will query the parent bins when the element is not found in the current bin.

An iterator of elements in a bin can be retrieved with `gst_bin_iterate_elements()`. Various other iterators exist to retrieve the elements in a bin.

`gst_object_unref()` is used to drop your reference to the bin.

The "element-added" signal is fired whenever a new element is added to the bin. Likewise the "element-removed" signal is fired whenever an element is removed from the bin.

### Notes

A GstBin internally intercepts every GstMessage posted by its children and implements the following default behaviour for each of them:

GST_MESSAGE_EOS | This message is only posted by sinks in the PLAYING state. If all sinks posted the EOS message, this bin will post and EOS message upwards.
GST_MESSAGE_SEGMENT_START | just collected and never forwarded upwards. The messages are used to decide when all elements have completed playback of their segment.
GST_MESSAGE_SEGMENT_DONE | Is posted by GstBin when all elements that posted a SEGMENT_START have posted a SEGMENT_DONE.
GST_MESSAGE_DURATION | Is posted by an element that detected a change in the stream duration. The default bin behaviour is to clear any cached duration values so that the next duration query will perform a full duration recalculation. The duration change is posted to the application so that it can refetch the new duration with a duration query. Note that these messages can be posted before the bin is prerolled, in which case the duration query might fail.
GST_MESSAGE_CLOCK_LOST | This message is posted by an element when it can no longer provide a clock. The default bin behaviour is to check if the lost clock was the one provided by the bin. If so and the bin is currently in the PLAYING state, the message is forwarded to the bin parent. This message is also generated when a clock provider is removed from the bin. If this message is received by the application, it should PAUSE the pipeline and set it back to PLAYING to force a new clock distribution.
GST_MESSAGE_CLOCK_PROVIDE | This message is generated when an element can provide a clock. This mostly happens when a new clock provider is added to the bin. The default behaviour of the bin is to mark the currently selected clock as dirty, which will perform a clock recalculation the next time the bin is asked to provide a clock. This message is never sent tot the application but is forwarded to the parent of the bin.
OTHERS | posted upwards.

A GstBin implements the following default behaviour for answering to a GstQuery:

GST_QUERY_DURATION | If the query has been asked before with the same format and the bin is a toplevel bin (ie. has no parent), use the cached previous value. If no previous value was cached, the query is sent to all sink elements in the bin and the MAXIMUM of all values is returned. If the bin is a toplevel bin the value is cached. If no sinks are available in the bin, the query fails.
GST_QUERY_POSITION | The query is sent to all sink elements in the bin and the MAXIMUM of all values is returned. If no sinks are available in the bin, the query fails.
OTHERS | the query is forwarded to all sink elements, the result of the first sink that answers the query successfully is returned. If no sink is in the bin, the query fails.

A GstBin will by default forward any event sent to it to all sink elements. If all the sinks return TRUE, the bin will also return TRUE, else FALSE is returned. If no sinks are in the bin, the event handler will return TRUE.

Last reviewed on 2006-04-28 (0.10.6)

## Details

### struct GstBin

    struct GstBin {
      /* our children, subclass are supposed to update these
       * fields to reflect their state with _iterate_*() */
      gint		 numchildren;
      GList		*children;
      guint32	 children_cookie;

      GstBus        *child_bus;
      GList         *messages;

      gboolean	 polling;
      gboolean       state_dirty;

      gboolean       clock_dirty;
      GstClock *provided_clock;
      GstElement    *clock_provider;
    };

The GstBin base class. Subclasses can access these fields provided the LOCK is taken.

gint `numchildren`; | the number of children in this bin
GList *`children`; | the list of children in this bin
guint32 `children_cookie`; | updated whenever `children` changes
GstBus *`child_bus`; | internal bus for handling child messages
GList *`messages`; | queued and cached messages
gboolean `polling`; | the bin is currently calculating its state
gboolean `state_dirty`; | the bin needs to recalculate its state (deprecated)
gboolean `clock_dirty`; | the bin needs to select a new clock
GstClock *`provided_clock`; | the last clock selected
GstElement *`clock_provider`; | the element that provided `provided_clock`

### struct GstBinClass

    struct GstBinClass {
      GstElementClass parent_class;

      /* virtual methods for subclasses */
      gboolean (*add_element)		(GstBin *bin, GstElement *element);
      gboolean (*remove_element) (GstBin *bin, GstElement *element);

      void		(*handle_message) (GstBin *bin, GstMessage *message);
    };

Subclasses can override the `add_element` and `remove_element` to update the list of children in the bin.

The `handle_message` method can be overridden to implement custom message handling. `handle_message` takes ownership of the message, just like gst_element_post_message.

GstElementClass `parent_class`; | bin parent class
`add_element` () | method to add an element to a bin
`remove_element` () | method to remove an element from a bin
`handle_message` () | method to handle a message from the children

### gst_bin_new ()

    GstElement *        gst_bin_new                         (const gchar *name);

Creates a new bin with the given name.

`name` : | the name of the new bin
Returns : | a new GstBin. [transfer full]

### gst_bin_add ()

    gboolean            gst_bin_add                         (GstBin *bin,
                                                             GstElement *element);

Adds the given element to the bin. Sets the element's parent, and thus takes ownership of the element. An element can only be added to one bin.

If the element's pads are linked to other pads, the pads will be unlinked before the element is added to the bin.

### Note

When you add an element to an already-running pipeline, you will have to take care to set the state of the newly-added element to the desired state (usually PLAYING or PAUSED, same you set the pipeline to originally) with `gst_element_set_state()`, or use `gst_element_sync_state_with_parent()`. The bin or pipeline will not take care of this for you.

MT safe.

`bin` : | a GstBin
`element` : | the GstElement to add. [transfer full]
Returns : | TRUE if the element could be added, FALSE if the bin does not want to accept the element.

### gst_bin_remove ()

    gboolean            gst_bin_remove                      (GstBin *bin,
                                                             GstElement *element);

Removes the element from the bin, unparenting it as well. Unparenting the element means that the element will be dereferenced, so if the bin holds the only reference to the element, the element will be freed in the process of removing it from the bin. If you want the element to still exist after removing, you need to call `gst_object_ref()` before removing it from the bin.

If the element's pads are linked to other pads, the pads will be unlinked before the element is removed from the bin.

MT safe.

`bin` : | a GstBin
`element` : | the GstElement to remove. [transfer none]
Returns : | TRUE if the element could be removed, FALSE if the bin does not want to remove the element.

### gst_bin_get_by_name ()

    GstElement *        gst_bin_get_by_name                 (GstBin *bin,
                                                             const gchar *name);

Gets the element with the given name from a bin. This function recurses into child bins.

Returns NULL if no element with the given name is found in the bin.

MT safe. Caller owns returned reference.

`bin` : | a GstBin
`name` : | the element name to search for
Returns : | the GstElement with the given name, or NULL. [transfer full]

### gst_bin_get_by_name_recurse_up ()

    GstElement *        gst_bin_get_by_name_recurse_up      (GstBin *bin,
                                                             const gchar *name);

Gets the element with the given name from this bin. If the element is not found, a recursion is performed on the parent bin.

Returns NULL if:

- no element with the given name is found in the bin

MT safe. Caller owns returned reference.

`bin` : | a GstBin
`name` : | the element name to search for
Returns : | the GstElement with the given name, or NULL. [transfer full]

### gst_bin_get_by_interface ()

    GstElement *        gst_bin_get_by_interface            (GstBin *bin,
                                                             GType iface);

Looks for an element inside the bin that implements the given interface. If such an element is found, it returns the element. You can cast this element to the given interface afterwards. If you want all elements that implement the interface, use `gst_bin_iterate_all_by_interface()`. This function recurses into child bins.

MT safe. Caller owns returned reference.

`bin` : | a GstBin
`iface` : | the GType of an interface
Returns : | A GstElement inside the bin implementing the interface. [transfer full]

### gst_bin_iterate_elements ()

    GstIterator *       gst_bin_iterate_elements            (GstBin *bin);

Gets an iterator for the elements in this bin.

Each element yielded by the iterator will have its refcount increased, so unref after use.

MT safe. Caller owns returned value.

`bin` : | a GstBin
Returns : | a GstIterator of GstElement, or NULL. [transfer full]

### gst_bin_iterate_recurse ()

    GstIterator *       gst_bin_iterate_recurse             (GstBin *bin);

Gets an iterator for the elements in this bin. This iterator recurses into GstBin children.

Each element yielded by the iterator will have its refcount increased, so unref after use.

MT safe. Caller owns returned value.

`bin` : | a GstBin
Returns : | a GstIterator of GstElement, or NULL. [transfer full]

### gst_bin_iterate_sinks ()

    GstIterator *       gst_bin_iterate_sinks               (GstBin *bin);

Gets an iterator for all elements in the bin that have the GST_ELEMENT_IS_SINK flag set.

Each element yielded by the iterator will have its refcount increased, so unref after use.

MT safe. Caller owns returned value.

`bin` : | a GstBin
Returns : | a GstIterator of GstElement, or NULL. [transfer full]

### gst_bin_iterate_sorted ()

    GstIterator *       gst_bin_iterate_sorted              (GstBin *bin);

Gets an iterator for the elements in this bin in topologically sorted order. This means that the elements are returned from the most downstream elements (sinks) to the sources.

This function is used internally to perform the state changes of the bin elements and for clock selection.

Each element yielded by the iterator will have its refcount increased, so unref after use.

MT safe. Caller owns returned value.

`bin` : | a GstBin
Returns : | a GstIterator of GstElement, or NULL. [transfer full]

### gst_bin_iterate_sources ()

    GstIterator *       gst_bin_iterate_sources             (GstBin *bin);

Gets an iterator for all elements in the bin that have the GST_ELEMENT_IS_SOURCE flag set.

Each element yielded by the iterator will have its refcount increased, so unref after use.

MT safe. Caller owns returned value.

`bin` : | a GstBin
Returns : | a GstIterator of GstElement, or NULL. [transfer full]

### gst_bin_iterate_all_by_interface ()

    GstIterator *       gst_bin_iterate_all_by_interface    (GstBin *bin,
                                                             GType iface);

Looks for all elements inside the bin that implements the given interface. You can safely cast all returned elements to the given interface. The function recurses inside child bins. The iterator will yield a series of GstElement that should be unreffed after use.

Each element yielded by the iterator will have its refcount increased, so unref after use.

MT safe. Caller owns returned value.

`bin` : | a GstBin
`iface` : | the GType of an interface
Returns : | a GstIterator of GstElement for all elements in the bin implementing the given interface, or NULL. [transfer full]

### gst_bin_recalculate_latency ()

    gboolean            gst_bin_recalculate_latency         (GstBin *bin);

Query `bin` for the current latency using and reconfigures this latency to all the elements with a LATENCY event.

This method is typically called on the pipeline when a GST_MESSAGE_LATENCY is posted on the bus.

This function simply emits the 'do-latency' signal so any custom latency calculations will be performed.

`bin` : | a GstBin
Returns : | `TRUE` if the latency could be queried and reconfigured.

Since 0.10.22.

### gst_bin_add_many ()

    void                gst_bin_add_many                    (GstBin *bin,
                                                             GstElement *element_1,
                                                             ...);

Adds a NULL-terminated list of elements to a bin. This function is equivalent to calling `gst_bin_add()` for each member of the list. The return value of each `gst_bin_add()` is ignored.

`bin` : | a GstBin
`element_1` : | the GstElement element to add to the bin. [transfer full]
`...` : | additional elements to add to the bin. [transfer full]

### gst_bin_remove_many ()

    void                gst_bin_remove_many                 (GstBin *bin,
                                                             GstElement *element_1,
                                                             ...);

Remove a list of elements from a bin. This function is equivalent to calling `gst_bin_remove()` with each member of the list.

`bin` : | a GstBin
`element_1` : | the first GstElement to remove from the bin. [transfer none]
`...` : | NULL-terminated list of elements to remove from the bin. [transfer none]

### gst_bin_find_unlinked_pad ()

    GstPad *            gst_bin_find_unlinked_pad           (GstBin *bin,
                                                             GstPadDirection direction);

Recursively looks for elements with an unlinked pad of the given direction within the specified bin and returns an unlinked pad if one is found, or NULL otherwise. If a pad is found, the caller owns a reference to it and should use `gst_object_unref()` on the pad when it is not needed any longer.

`bin` : | bin in which to look for elements with unlinked pads
`direction` : | whether to look for an unlinked source or sink pad
Returns : | unlinked pad of the given direction, or NULL. [transfer full]

Since 0.10.20

### gst_bin_find_unconnected_pad ()

    GstPad *            gst_bin_find_unconnected_pad        (GstBin *bin,
                                                             GstPadDirection direction);

### Warning

`gst_bin_find_unconnected_pad` is deprecated and should not be used in newly-written code. use `gst_bin_find_unlinked_pad()` instead.

Recursively looks for elements with an unlinked pad of the given direction within the specified bin and returns an unlinked pad if one is found, or NULL otherwise. If a pad is found, the caller owns a reference to it and should use `gst_object_unref()` on the pad when it is not needed any longer.

`bin` : | bin in which to look for elements with unlinked pads
`direction` : | whether to look for an unlinked source or sink pad
Returns : | unlinked pad of the given direction, or NULL. [transfer full]

Since 0.10.3

### enum GstBinFlags

    typedef enum {
      /* padding */
      GST_BIN_FLAG_LAST		= (GST_ELEMENT_FLAG_LAST << 5)
    } GstBinFlags;

GstBinFlags are a set of flags specific to bins. Most are set/used internally. They can be checked using the `GST_OBJECT_FLAG_IS_SET()` macro, and (un)set using `GST_OBJECT_FLAG_SET()` and `GST_OBJECT_FLAG_UNSET()`.

`GST_BIN_FLAG_LAST` | the last enum in the series of flags for bins. Derived classes can use this as first value in a list of flags.

### GST_BIN_CHILDREN()

    #define GST_BIN_CHILDREN(bin)		(GST_BIN_CAST(bin)->children)

Gets the list with children in a bin.

`bin` : | a GstBin

### GST_BIN_CHILDREN_COOKIE()

    #define GST_BIN_CHILDREN_COOKIE(bin) (GST_BIN_CAST(bin)->children_cookie)

Gets the children cookie that watches the children list.

`bin` : | a GstBin

### GST_BIN_NUMCHILDREN()

    #define GST_BIN_NUMCHILDREN(bin) (GST_BIN_CAST(bin)->numchildren)

Gets the number of children in a bin.

`bin` : | a GstBin

## Property Details

### The `"async-handling"` property

      "async-handling"           gboolean              : Read / Write

If set to TRUE, the bin will handle asynchronous state changes. This should be used only if the bin subclass is modifying the state of its children on its own.

Default value: FALSE

Since 0.10.13

### The `"message-forward"` property

      "message-forward"          gboolean              : Read / Write

Forward all children messages, even those that would normally be filtered by the bin. This can be interesting when one wants to be notified of the EOS state of individual elements, for example.

The messages are converted to an ELEMENT message with the bin as the source. The structure of the message is named 'GstBinForwarded' and contains a field named 'message' of type GST_TYPE_MESSAGE that contains the original forwarded message.

Default value: FALSE

Since 0.10.31

## Signal Details

### The `"do-latency"` signal

    gboolean            user_function                      (GstBin  *bin,
                                                            gpointer user_data)      : Run Last

Will be emitted when the bin needs to perform latency calculations. This signal is only emited for toplevel bins or when async-handling is enabled.

Only one signal handler is invoked. If no signals are connected, the default handler is invoked, which will query and distribute the lowest possible latency to all sinks.

Connect to this signal if the default latency calculations are not sufficient, like when you need different latencies for different sinks in the same pipeline.

`bin` : | the GstBin
`user_data` : | user data set when the signal handler was connected.

Since 0.10.22

### The `"element-added"` signal

    void                user_function                      (GstBin     *bin,
                                                            GstElement *element,
                                                            gpointer    user_data)      : Run First

Will be emitted after the element was added to the bin.

`bin` : | the GstBin
`element` : | the GstElement that was added to the bin
`user_data` : | user data set when the signal handler was connected.

### The `"element-removed"` signal

    void                user_function                      (GstBin     *bin,
                                                            GstElement *element,
                                                            gpointer    user_data)      : Run First

Will be emitted after the element was removed from the bin.

`bin` : | the GstBin
`element` : | the GstElement that was removed from the bin
`user_data` : | user data set when the signal handler was connected.