1 <chapter id="chapter-bins">
4 A bin is a container element. You can add elements to a bin. Since a
5 bin is an element itself, a bin can be handled in the same way as any
6 other element. Therefore, the whole previous chapter (<xref
7 linkend="chapter-elements"/>) applies to bins as well.
10 <sect1 id="section-bins">
11 <title>What are bins</title>
13 Bins allow you to combine a group of linked elements into one
14 logical element. You do not deal with the individual elements
15 anymore but with just one element, the bin. We will see that
16 this is extremely powerful when you are going to construct
17 complex pipelines since it allows you to break up the pipeline
21 The bin will also manage the elements contained in it. It will
22 figure out how the data will flow in the bin and generate an
23 optimal plan for that data flow. Plan generation is one of the
24 most complicated procedures in &GStreamer;. You will learn more
25 about this process, called scheduling, in <xref
26 linkend="chapter-scheduler"/>.
29 <figure float="1" id="section-bin-img">
30 <title>Visualisation of a bin with some elements in it</title>
33 <imagedata fileref="images/bin-element.ℑ" format="&IMAGE;"/>
39 There are two specialized types of bins available to the
40 &GStreamer; programmer:
45 A pipeline: a generic container that allows scheduling of the
46 containing elements. The toplevel bin has to be a pipeline.
47 Every application thus needs at least one of these. Applications
48 can iterate pipelines using <function>gst_bin_iterate
49 ()</function> to make it process data while in the playing state.
54 A thread: a bin that will be run in a separate execution thread.
55 You will have to use this bin if you have to carefully
56 synchronize audio and video, or for buffering. You will learn
57 more about threads in <xref linkend="chapter-threads"/>.
63 <sect1 id="section-bin-create">
64 <title>Creating a bin</title>
66 Bins are created in the same way that other elements are created,
67 i.e. using an element factory. There are also convenience functions
68 available (<function>gst_bin_new ()</function>,
69 <function>gst_thread_new ()</function> and <function>gst_pipeline_new
70 ()</function>). To add elements to a bin or remove elements from a
71 bin, you can use <function>gst_bin_add ()</function> and
72 <function>gst_bin_remove ()</function>. Note that the bin that you
73 add an element to will take ownership of that element. If you
74 destroy the bin, the element will be dereferenced with it. If you
75 remove an element from a bin, it will be dereferenced automatically.
82 GstElement *bin, *pipeline, *source, *sink;
85 gst_init (&argc, &argv);
88 pipeline = gst_pipeline_new ("my_pipeline");
89 bin = gst_pipeline_new ("my_bin");
90 source = gst_element_factory_make ("fakesrc", "source");
91 sink = gst_element_factory_make ("fakesink", "sink");
94 gst_bin_add_many (GST_BIN (bin), source, sink, NULL);
95 gst_bin_add (GST_BIN (pipeline), bin);
96 gst_element_link (source, sink);
102 There are various functions to lookup elements in a bin. You can
103 also get a list of all elements that a bin contains using the function
104 <function>gst_bin_get_list ()</function>. See the API references of
106 url="../../GStreamer/html/GstBin.html"><classname>GstBin</classname></ulink>
111 <sect1 id="section-bin-custom">
112 <title>Custom bins</title>
114 The application programmer can create custom bins packed with elements
115 to perform a specific task. This allows you, for example, to write
116 an Ogg/Vorbis decoder with just the following lines of code:
126 gst_init (&argc, &argv);
129 player = gst_element_factory_make ("oggvorbisplayer", "player");
131 /* set the source audio file */
132 g_object_set (G_OBJECT (player), "location", "helloworld.ogg", NULL);
135 gst_element_set_state (GST_ELEMENT (mp3player), GST_STATE_PLAYING);
140 Custom bins can be created with a plugin or an XML description. You
141 will find more information about creating custom bin in the <ulink
143 url="http://gstreamer.freedesktop.org/data/doc/gstreamer/head/pwg/html/index.html">Plugin
144 Writers Guide</ulink>.