1 <chapter id="cha-bins">
4 A Bin is a container element. You can add elements to a bin. Since a bin is
5 an <classname>GstElement</classname> itself, it can also be added to another bin.
8 Bins allow you to combine connected elements into one logical element. You do
9 not deal with the individual elements anymore but with just one element, the bin.
10 We will see that this is extremely powerfull when you are going to construct
11 complex pipelines since it allows you to break up the pipeline in smaller chunks.
14 The bin will also manage the elements contained in it. It will figure out how
15 the data will flow in the bin and generate an optimal plan for that data flow. Plan
16 generation is one of the most complicated procedures in GStreamer.
19 <figure float="1" id="sec-bin-img">
20 <title>Visualisation of a <classname>GstBin</classname> element with some elements in it</title>
21 <graphic fileref="images/bin-element" format="png"></graphic>
25 There are two standard bins available to the GStreamer programmer:
30 A pipeline (<classname>GstPipeline</classname>). Which is a generic container you will
36 A thread (<classname>GstThread</classname>). All the elements in the thread bin will
37 run in a separate thread. You will haver to use this bin if you carfully have to
38 synchronize audio and video for example. You will learn more about threads in.. <!-- FIXME -->
44 <sect1 id="sec-bin-create">
45 <title>Creating a bin</title>
47 You create a bin with a specified name 'mybin' with:
52 gst_bin_new ("mybin");
56 A thread can be created with:
61 gst_thread_new ("mythread");
65 Pipelines are created with gst_pipeline_new ("name");
69 <sect1 id="sec-bin-adding">
70 <title>Adding elements to a bin</title>
72 Elements are added to a bin with the following code sample:
78 bin = gst_bin_new ("mybin");
80 element = gst_elementfactory_make ("mpg123", "decoder");
81 gst_bin_add (GST_BIN (bin), element);
85 Bins and threads can be added to other bins too. This allows you to create nested
89 To get an element from the bin you can use:
94 element = gst_bin_get_by_name (GST_BIN (bin), "decoder");
98 You can see that the name of the element becomes very handy for retrieving the
99 element from an bin by using the elements name. gst_bin_get_by_name () will
100 recursively search nested bins.
103 To get a list of elements in a bin, use:
108 elements = gst_bin_get_list (GST_BIN (bin));
111 GstElement *element = GST_ELEMENT (elements->data);
113 g_print ("element in bin: %s\n", gst_element_get_name (element));
115 elements = g_list_next (elements);
120 To remove an element from a bin use:
125 gst_bin_remove (GST_BIN (bin), element);
130 <sect1 id="sec-bin-custom">
131 <title>Custom bins</title>
133 The application programmer can create custom bins packed with elements to perform a
134 specific task. This allow you to write an MPEG audio decoder with just the follwing lines
139 // create the mp3player element
140 GstElement *mp3player = gst_elementfactory_make("mp3player","mp3player");
141 // set the source mp3 audio file
142 gtk_object_set(GTK_OBJECT(mp3player), "location", "helloworld.mp3", NULL);
144 gst_element_set_state(GST_ELEMENT(mp3player),GST_STATE_PLAYING);
147 gst_element_set_state(GST_ELEMENT(mp3player),GST_STATE_PAUSED);
150 gst_element_set_state(GST_ELEMENT(mp3player),GST_STATE_NULL);
153 Custom bins can be created with a plugin or an XML description. You will find more
154 information about creating custom bin in the Filter-Writers-Guide.
158 <sect1 id="sec-bin-ghostpads">
159 <title>Ghostpads</title>
161 You can see from figure ... how a bin has no pads of its own. This is where Ghostpads
165 A ghostpad is a pad from some element in the bin that has been promoted to the bin.
166 This way, the bin also has a pad. The bin becomes just another element with a pad and
167 you can then use the bin just like any other element. This is a very important feature
168 for creating custom bins.
171 <figure float="1" id="sec-bin-ghost-img">
172 <title>Visualisation of a <classname>GstBin</classname> element with a ghostpad</title>
173 <graphic fileref="images/bin-element-ghost" format="png"></graphic>
176 Above is a representation of a ghostpad. the sinkpad of element one is now also a pad
180 Ghostpads can actually be added to all <classname>GstElement</classname>s and not just
181 <classname>GstBin</classname>s. Use the following code example to add a ghostpad to a bin:
187 element = gst_elementfactory_create ("mpg123", "decoder");
188 bin = gst_bin_new ("mybin");
190 gst_bin_add (GST_BIN (bin), element);
192 gst_element_add_ghost_pad (bin, gst_element_get_pad (element, "sink"));
196 In the above example, the bin now also has a pad: the pad called 'sink' of the
197 given element. We can now, for example, connect the srcpad of a disksrc to the
203 disksrc = gst_elementfactory_create ("disksrc", "disk_reader");
205 gst_element_connect (disksrc, "src", bin, "sink");