# Bins

A Bin is a container element. You can add elements to a bin. Since a bin is an `GstElement` itself, it can also be added to another bin.

Bins allow you to combine connected elements into one logical element. You do not deal with the individual elements anymore but with just one element, the bin. We will see that this is extremely powerfull when you are going to construct complex pipelines since it allows you to break up the pipeline in smaller chunks.

The bin will also manage the elements contained in it. It will figure out how the data will flow in the bin and generate an optimal plan for that data flow. Plan generation is one of the most complicated procedures in GStreamer.

## Visualisation of a `GstBin` element with some elements in it

There are two standard bins available to the GStreamer programmer:

- A pipeline (`GstPipeline`). Which is a generic container you will use most of the time. The toplevel bin has to be a pipeline.

- A thread (`GstThread`). All the elements in the thread bin will run in a separate thread. You will have to use this bin if you carfully have to synchronize audio and video for example. You will learn more about threads in..

## Creating a bin

You create a bin with a specified name 'mybin' with:

      GstElement *bin;

      gst_bin_new ("mybin");
       ...

A thread can be created with:

      GstElement *thread;

      gst_thread_new ("mythread");
       ...

Pipelines are created with gst_pipeline_new ("name");

## Adding elements to a bin

Elements are added to a bin with the following code sample:

      GstElement *element;
      GstElement *bin;

      bin = gst_bin_new ("mybin");

      element = gst_elementfactory_make ("mpg123", "decoder");
      gst_bin_add (GST_BIN (bin), element);
       ...

Bins and threads can be added to other bins too. This allows you to create nested bins.

To get an element from the bin you can use:

      GstElement *element;

      element = gst_bin_get_by_name (GST_BIN (bin), "decoder");
       ...

You can see that the name of the element becomes very handy for retrieving the element from an bin by using the elements name. gst_bin_get_by_name () will recursively search nested bins.

To get a list of elements in a bin, use:

      GList *elements;

      elements = gst_bin_get_list (GST_BIN (bin));

      while (elements) {
        GstElement *element = GST_ELEMENT (elements->data);

        g_print ("element in bin: &percnt;s\n", gst_element_get_name (element));

        elements = g_list_next (elements);
      }
       ...

To remove an element from a bin use:

      GstElement *element;

      gst_bin_remove (GST_BIN (bin), element);
       ...

## Custom bins

The application programmer can create custom bins packed with elements to perform a specific task. This allow you to write an MPEG audio decoder with just the follwing lines of code:

      // create the mp3player element
      GstElement *mp3player = gst_elementfactory_make ("mp3player", "mp3player");
      // set the source mp3 audio file
      g_object_set (G_OBJECT (mp3player), "location", "helloworld.mp3", NULL);
      // start playback
      gst_element_set_state (GST_ELEMENT (mp3player), GST_STATE_PLAYING);
       ...
      // pause playback
      gst_element_set_state (GST_ELEMENT (mp3player), GST_STATE_PAUSED);
       ...
      // stop
      gst_element_set_state (GST_ELEMENT (mp3player), GST_STATE_NULL);

Custom bins can be created with a plugin or an XML description. You will find more information about creating custom bin in the Filter-Writers-Guide.

## Ghostpads

You can see from figure ... how a bin has no pads of its own. This is where Ghostpads come into play.

A ghostpad is a pad from some element in the bin that has been promoted to the bin. This way, the bin also has a pad. The bin becomes just another element with a pad and you can then use the bin just like any other element. This is a very important feature for creating custom bins.

### Visualisation of a `GstBin` element with a ghostpad

Above is a representation of a ghostpad. the sinkpad of element one is now also a pad of the bin.

Ghostpads can actually be added to all `GstElement`s and not just `GstBin`s. Use the following code example to add a ghostpad to a bin:

      GstElement *bin;
      GstElement *element;

      element = gst_elementfactory_create ("mpg123", "decoder");
      bin = gst_bin_new ("mybin");

      gst_bin_add (GST_BIN (bin), element);

      gst_element_add_ghost_pad (bin, gst_element_get_pad (element, "sink"), "sink");

In the above example, the bin now also has a pad: the pad called 'sink' of the given element. We can now, for example, connect the srcpad of a filesrc to the bin with:

      GstElement *filesrc;

      filesrc = gst_elementfactory_create ("filesrc", "disk_reader");

      gst_element_connect (filesrc, "src", bin, "sink");
        ...