2000-08-16 21:38:57 +00:00
|
|
|
<chapter id="cha-bins">
|
|
|
|
<title>Bins</title>
|
|
|
|
<para>
|
|
|
|
A Bin is a container element. You can add elements to a bin. Since a bin is
|
2001-01-04 23:35:50 +00:00
|
|
|
an <classname>GstElement</classname> itself, it can also be added to another bin.
|
2000-08-16 21:38:57 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
2003-01-09 14:15:37 +00:00
|
|
|
Bins allow you to combine linked elements into one logical element. You do
|
2000-08-16 21:38:57 +00:00
|
|
|
not deal with the individual elements anymore but with just one element, the bin.
|
2002-04-07 23:32:16 +00:00
|
|
|
We will see that this is extremely powerful when you are going to construct
|
2000-08-16 21:38:57 +00:00
|
|
|
complex pipelines since it allows you to break up the pipeline in smaller chunks.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
The bin will also manage the elements contained in it. It will figure out how
|
2001-01-04 23:35:50 +00:00
|
|
|
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.
|
2000-08-16 21:38:57 +00:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<figure float="1" id="sec-bin-img">
|
|
|
|
<title>Visualisation of a <classname>GstBin</classname> element with some elements in it</title>
|
2001-12-13 15:24:00 +00:00
|
|
|
<mediaobject>
|
|
|
|
<imageobject>
|
2002-12-18 14:44:26 +00:00
|
|
|
<imagedata fileref="images/bin-element.&magic;" format="&MAGIC;" />
|
2001-12-13 15:24:00 +00:00
|
|
|
</imageobject>
|
|
|
|
</mediaobject>
|
2000-08-16 21:38:57 +00:00
|
|
|
</figure>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
There are two standard bins available to the GStreamer programmer:
|
|
|
|
|
|
|
|
<itemizedlist>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
A pipeline (<classname>GstPipeline</classname>). Which is a generic container you will
|
2001-06-15 21:46:57 +00:00
|
|
|
use most of the time. The toplevel bin has to be a pipeline.
|
2000-08-16 21:38:57 +00:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2002-04-07 23:32:16 +00:00
|
|
|
A thread (<classname>GstThread</classname>). The plan for the
|
|
|
|
<classname>GstThread</classname> 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 <xref linkend="cha-threads"/>.
|
2000-08-16 21:38:57 +00:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</itemizedlist>
|
|
|
|
</para>
|
|
|
|
|
2001-01-06 02:35:17 +00:00
|
|
|
<sect1 id="sec-bin-create">
|
|
|
|
<title>Creating a bin</title>
|
|
|
|
<para>
|
2002-09-14 14:13:34 +00:00
|
|
|
Bins are created in the same way that other elements are created. ie.
|
|
|
|
using an element factory, or any of the associated convenience functions:
|
2001-01-06 02:35:17 +00:00
|
|
|
</para>
|
|
|
|
<programlisting>
|
2002-04-07 23:32:16 +00:00
|
|
|
GstElement *bin, *thread, *pipeline;
|
2001-01-06 02:35:17 +00:00
|
|
|
|
2002-04-07 23:32:16 +00:00
|
|
|
/* create a new bin called 'mybin'. this bin will be only for organizational purposes; a normal
|
|
|
|
GstBin doesn't affect plan generation */
|
2002-04-11 20:35:18 +00:00
|
|
|
bin = gst_element_factory_make ("bin", "mybin");
|
2002-04-07 23:32:16 +00:00
|
|
|
|
|
|
|
/* create a new thread, and give it a unique name */
|
2002-04-11 20:35:18 +00:00
|
|
|
thread = gst_element_factory_make ("thread", NULL);
|
2002-04-07 23:32:16 +00:00
|
|
|
|
|
|
|
/* the core bins (GstBin, GstThread, GstPipeline) also have convenience APIs,
|
2002-04-11 20:35:18 +00:00
|
|
|
gst_<bintype>_new (). these are equivalent to the gst_element_factory_make () syntax. */
|
2002-04-07 23:32:16 +00:00
|
|
|
pipeline = gst_pipeline_new ("pipeline_name");
|
2001-01-06 02:35:17 +00:00
|
|
|
</programlisting>
|
|
|
|
</sect1>
|
2000-08-16 21:38:57 +00:00
|
|
|
|
2001-01-06 02:35:17 +00:00
|
|
|
<sect1 id="sec-bin-adding">
|
|
|
|
<title>Adding elements to a bin</title>
|
|
|
|
<para>
|
|
|
|
Elements are added to a bin with the following code sample:
|
|
|
|
</para>
|
|
|
|
<programlisting>
|
|
|
|
GstElement *element;
|
|
|
|
GstElement *bin;
|
|
|
|
|
|
|
|
bin = gst_bin_new ("mybin");
|
|
|
|
|
2002-04-11 20:35:18 +00:00
|
|
|
element = gst_element_factory_make ("mpg123", "decoder");
|
2001-01-06 02:35:17 +00:00
|
|
|
gst_bin_add (GST_BIN (bin), element);
|
|
|
|
...
|
|
|
|
</programlisting>
|
|
|
|
<para>
|
2002-04-07 23:32:16 +00:00
|
|
|
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 <classname>GstPipeline</classname> to anything,
|
|
|
|
as it's a toplevel bin that needs to be explicitly iterated.
|
2001-01-06 02:35:17 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
To get an element from the bin you can use:
|
|
|
|
</para>
|
|
|
|
<programlisting>
|
|
|
|
GstElement *element;
|
|
|
|
|
|
|
|
element = gst_bin_get_by_name (GST_BIN (bin), "decoder");
|
|
|
|
...
|
|
|
|
</programlisting>
|
|
|
|
<para>
|
2002-09-08 21:17:16 +00:00
|
|
|
You can see that the name of the element becomes very handy
|
2002-09-14 14:13:34 +00:00
|
|
|
for retrieving the element from a bin by using the element's
|
2002-09-08 21:17:16 +00:00
|
|
|
name. gst_bin_get_by_name () will recursively search nested bins.
|
2001-01-06 02:35:17 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
To get a list of elements in a bin, use:
|
|
|
|
</para>
|
2000-08-16 21:38:57 +00:00
|
|
|
<programlisting>
|
2001-01-06 02:35:17 +00:00
|
|
|
GList *elements;
|
|
|
|
|
|
|
|
elements = gst_bin_get_list (GST_BIN (bin));
|
2000-08-16 21:38:57 +00:00
|
|
|
|
2001-01-06 02:35:17 +00:00
|
|
|
while (elements) {
|
|
|
|
GstElement *element = GST_ELEMENT (elements->data);
|
|
|
|
|
2002-07-09 18:43:13 +00:00
|
|
|
g_print ("element in bin: %s\n", GST_OBJECT_NAME (GST_OBJECT (element)));
|
2001-01-06 02:35:17 +00:00
|
|
|
|
|
|
|
elements = g_list_next (elements);
|
|
|
|
}
|
|
|
|
...
|
2000-08-16 21:38:57 +00:00
|
|
|
</programlisting>
|
2001-01-06 02:35:17 +00:00
|
|
|
<para>
|
2002-09-14 14:13:34 +00:00
|
|
|
To remove an element from a bin, use:
|
2001-01-06 02:35:17 +00:00
|
|
|
</para>
|
|
|
|
<programlisting>
|
|
|
|
GstElement *element;
|
|
|
|
|
|
|
|
gst_bin_remove (GST_BIN (bin), element);
|
|
|
|
...
|
|
|
|
</programlisting>
|
2002-04-07 23:32:16 +00:00
|
|
|
<para>
|
2002-09-14 14:13:34 +00:00
|
|
|
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.
|
2002-04-07 23:32:16 +00:00
|
|
|
</para>
|
|
|
|
<programlisting>
|
|
|
|
GstElement *filesrc, *decoder, *audiosink;
|
|
|
|
GstBin *bin;
|
|
|
|
|
|
|
|
/* instantiate the elements and the bins... */
|
|
|
|
|
|
|
|
gst_bin_add_many (bin, filesrc, decoder, audiosink, NULL);
|
|
|
|
</programlisting>
|
2001-01-06 02:35:17 +00:00
|
|
|
</sect1>
|
2001-01-04 23:35:50 +00:00
|
|
|
|
2001-01-06 02:35:17 +00:00
|
|
|
<sect1 id="sec-bin-custom">
|
|
|
|
<title>Custom bins</title>
|
|
|
|
<para>
|
2002-09-08 21:17:16 +00:00
|
|
|
The application programmer can create custom bins packed with elements
|
2002-09-14 14:13:34 +00:00
|
|
|
to perform a specific task. This allows you to write an MPEG audio
|
|
|
|
decoder with just the following lines of code:
|
2002-04-07 23:32:16 +00:00
|
|
|
</para>
|
|
|
|
<programlisting>
|
2001-01-06 02:35:17 +00:00
|
|
|
|
2002-04-07 23:32:16 +00:00
|
|
|
/* create the mp3player element */
|
2002-04-11 20:35:18 +00:00
|
|
|
GstElement *mp3player = gst_element_factory_make ("mp3player", "mp3player");
|
2002-04-07 23:32:16 +00:00
|
|
|
/* set the source mp3 audio file */
|
2001-07-07 11:34:54 +00:00
|
|
|
g_object_set (G_OBJECT (mp3player), "location", "helloworld.mp3", NULL);
|
2002-04-07 23:32:16 +00:00
|
|
|
/* start playback */
|
2001-07-07 11:34:54 +00:00
|
|
|
gst_element_set_state (GST_ELEMENT (mp3player), GST_STATE_PLAYING);
|
2001-01-06 02:35:17 +00:00
|
|
|
...
|
2002-04-07 23:32:16 +00:00
|
|
|
/* pause playback */
|
2001-07-07 11:34:54 +00:00
|
|
|
gst_element_set_state (GST_ELEMENT (mp3player), GST_STATE_PAUSED);
|
2001-01-06 02:35:17 +00:00
|
|
|
...
|
2002-04-07 23:32:16 +00:00
|
|
|
/* stop */
|
2001-07-07 11:34:54 +00:00
|
|
|
gst_element_set_state (GST_ELEMENT (mp3player), GST_STATE_NULL);
|
2002-04-07 23:32:16 +00:00
|
|
|
</programlisting>
|
|
|
|
<para>
|
2002-09-08 21:17:16 +00:00
|
|
|
Note that the above code assumes that the mp3player bin derives itself
|
|
|
|
from a <classname>GstThread</classname>, which begins to play as soon
|
|
|
|
as its state is set to PLAYING. Other bin types may need explicit
|
|
|
|
iteration. For more information, see <xref linkend="cha-threads"/>.
|
2002-09-14 14:13:34 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
2002-09-08 21:17:16 +00:00
|
|
|
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).
|
2001-01-06 02:35:17 +00:00
|
|
|
</para>
|
|
|
|
</sect1>
|
2001-01-04 23:35:50 +00:00
|
|
|
|
|
|
|
<sect1 id="sec-bin-ghostpads">
|
2002-04-07 23:32:16 +00:00
|
|
|
<title>Ghost pads</title>
|
2001-01-04 23:35:50 +00:00
|
|
|
<para>
|
2002-04-07 23:32:16 +00:00
|
|
|
You can see from figure <xref linkend="sec-bin-noghost-img"/> how a bin has no pads of its own.
|
|
|
|
This is where "ghost pads" come into play.
|
2001-01-04 23:35:50 +00:00
|
|
|
</para>
|
2002-04-07 23:32:16 +00:00
|
|
|
<figure float="1" id="sec-bin-noghost-img">
|
|
|
|
<title>Visualisation of a <classname>GstBin</classname> element without ghost pads</title>
|
|
|
|
<mediaobject>
|
|
|
|
<imageobject>
|
2002-12-18 14:44:26 +00:00
|
|
|
<imagedata fileref="images/bin-element-noghost.&magic;" format="&MAGIC;" />
|
2002-04-07 23:32:16 +00:00
|
|
|
</imageobject>
|
|
|
|
</mediaobject>
|
|
|
|
</figure>
|
2001-01-04 23:35:50 +00:00
|
|
|
<para>
|
2002-04-07 23:32:16 +00:00
|
|
|
A ghost pad is a pad from some element in the bin that has been promoted to the bin.
|
2001-01-04 23:35:50 +00:00
|
|
|
This way, the bin also has a pad. The bin becomes just another element with a pad and
|
2001-01-06 02:35:17 +00:00
|
|
|
you can then use the bin just like any other element. This is a very important feature
|
|
|
|
for creating custom bins.
|
2001-01-04 23:35:50 +00:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<figure float="1" id="sec-bin-ghost-img">
|
2002-04-07 23:32:16 +00:00
|
|
|
<title>Visualisation of a <classname>GstBin</classname> element with a ghost pad</title>
|
2001-12-13 15:24:00 +00:00
|
|
|
<mediaobject>
|
|
|
|
<imageobject>
|
2002-12-18 14:44:26 +00:00
|
|
|
<imagedata fileref="images/bin-element-ghost.&magic;" format="&MAGIC;" />
|
2001-12-13 15:24:00 +00:00
|
|
|
</imageobject>
|
|
|
|
</mediaobject>
|
2001-01-04 23:35:50 +00:00
|
|
|
</figure>
|
|
|
|
<para>
|
2002-04-07 23:32:16 +00:00
|
|
|
Above is a representation of a ghost pad. The sink pad of element one is now also a pad
|
2001-01-04 23:35:50 +00:00
|
|
|
of the bin.
|
|
|
|
</para>
|
2001-01-06 02:35:17 +00:00
|
|
|
<para>
|
2002-04-07 23:32:16 +00:00
|
|
|
Ghost pads can actually be added to all <classname>GstElement</classname>s and not just
|
|
|
|
<classname>GstBin</classname>s. Use the following code example to add a ghost pad to a bin:
|
2001-01-06 02:35:17 +00:00
|
|
|
</para>
|
|
|
|
<programlisting>
|
|
|
|
GstElement *bin;
|
|
|
|
GstElement *element;
|
|
|
|
|
2002-04-11 20:35:18 +00:00
|
|
|
element = gst_element_factory_create ("mad", "decoder");
|
2001-01-06 02:35:17 +00:00
|
|
|
bin = gst_bin_new ("mybin");
|
|
|
|
|
|
|
|
gst_bin_add (GST_BIN (bin), element);
|
|
|
|
|
2001-06-15 21:46:57 +00:00
|
|
|
gst_element_add_ghost_pad (bin, gst_element_get_pad (element, "sink"), "sink");
|
2001-01-06 02:35:17 +00:00
|
|
|
|
|
|
|
</programlisting>
|
|
|
|
<para>
|
2002-09-08 21:17:16 +00:00
|
|
|
In the above example, the bin now also has a pad: the pad called 'sink'
|
2002-09-14 14:13:34 +00:00
|
|
|
of the given element.
|
|
|
|
</para>
|
|
|
|
<para>
|
2003-01-09 14:15:37 +00:00
|
|
|
We can now, for example, link the source pad of a filesrc element
|
2002-09-14 14:13:34 +00:00
|
|
|
to the bin with:
|
2001-01-06 02:35:17 +00:00
|
|
|
</para>
|
|
|
|
<programlisting>
|
2002-01-06 04:26:37 +00:00
|
|
|
GstElement *filesrc;
|
2001-01-06 02:35:17 +00:00
|
|
|
|
2002-04-11 20:35:18 +00:00
|
|
|
filesrc = gst_element_factory_create ("filesrc", "disk_reader");
|
2001-01-06 02:35:17 +00:00
|
|
|
|
2003-01-09 14:15:37 +00:00
|
|
|
gst_element_link_pads (filesrc, "src", bin, "sink");
|
2001-01-06 02:35:17 +00:00
|
|
|
...
|
|
|
|
</programlisting>
|
2001-01-04 23:35:50 +00:00
|
|
|
</sect1>
|
|
|
|
|
2000-08-16 21:38:57 +00:00
|
|
|
</chapter>
|