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 linked 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 powerful 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 <classname>GstBin</classname> 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). The plan for the GstThread will be run in a separate thread. You will have to use this bin if you have to carefully synchronize audio and video, for example. You will learn more about threads in . Creating a bin Bins are created in the same way that other elements are created. ie. using an element factory, or any of the associated convenience functions: GstElement *bin, *thread, *pipeline; /* create a new bin called 'mybin'. this bin will be only for organizational purposes; a normal GstBin doesn't affect plan generation */ bin = gst_element_factory_make ("bin", "mybin"); /* create a new thread, and give it a unique name */ thread = gst_element_factory_make ("thread", NULL); /* the core bins (GstBin, GstThread, GstPipeline) also have convenience APIs, gst_<bintype>_new (). these are equivalent to the gst_element_factory_make () syntax. */ pipeline = gst_pipeline_new ("pipeline_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_element_factory_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. Note that it doesn't make very much sense to add a GstPipeline to anything, as it's a toplevel bin that needs to be explicitly iterated. 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 a bin by using the element's 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: %s\n", GST_OBJECT_NAME (GST_OBJECT (element))); elements = g_list_next (elements); } ... To remove an element from a bin, use: GstElement *element; gst_bin_remove (GST_BIN (bin), element); ... To add many elements to a bin at the same time, use the gst_bin_add_many () function. Remember to pass NULL as the last argument. GstElement *filesrc, *decoder, *audiosink; GstBin *bin; /* instantiate the elements and the bins... */ gst_bin_add_many (bin, filesrc, decoder, audiosink, NULL); Custom bins The application programmer can create custom bins packed with elements to perform a specific task. This allows you to write an MPEG audio decoder with just the following lines of code: /* create the mp3player element */ GstElement *mp3player = gst_element_factory_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); Note that the above code assumes that the mp3player bin derives itself from a GstThread, which begins to play as soon as its state is set to PLAYING. Other bin types may need explicit iteration. For more information, see . Custom bins can be created with a plugin or an XML description. You will find more information about creating custom bin in the Plugin Writers Guide (FIXME ref). Ghost pads You can see from figure how a bin has no pads of its own. This is where "ghost pads" come into play.
Visualisation of a <classname>GstBin</classname> element without ghost pads
A ghost pad 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 <classname>GstBin</classname> element with a ghost pad
Above is a representation of a ghost pad. The sink pad of element one is now also a pad of the bin. Ghost pads can actually be added to all GstElements and not just GstBins. Use the following code example to add a ghost pad to a bin: GstElement *bin; GstElement *element; element = gst_element_factory_create ("mad", "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, link the source pad of a filesrc element to the bin with: GstElement *filesrc; filesrc = gst_element_factory_create ("filesrc", "disk_reader"); gst_element_link_pads (filesrc, "src", bin, "sink"); ...