1 | <chapter id="chapter-bins"> |
---|
2 | <title>Bins</title> |
---|
3 | <para> |
---|
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. |
---|
8 | </para> |
---|
9 | |
---|
10 | <sect1 id="section-bins"> |
---|
11 | <title>What are bins</title> |
---|
12 | <para> |
---|
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 |
---|
18 | in smaller chunks. |
---|
19 | </para> |
---|
20 | <para> |
---|
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"/>. |
---|
27 | </para> |
---|
28 | |
---|
29 | <figure float="1" id="section-bin-img"> |
---|
30 | <title>Visualisation of a bin with some elements in it</title> |
---|
31 | <mediaobject> |
---|
32 | <imageobject> |
---|
33 | <imagedata fileref="images/bin-element.ℑ" format="&IMAGE;"/> |
---|
34 | </imageobject> |
---|
35 | </mediaobject> |
---|
36 | </figure> |
---|
37 | |
---|
38 | <para> |
---|
39 | There are two specialized types of bins available to the |
---|
40 | &GStreamer; programmer: |
---|
41 | </para> |
---|
42 | <itemizedlist> |
---|
43 | <listitem> |
---|
44 | <para> |
---|
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. |
---|
50 | </para> |
---|
51 | </listitem> |
---|
52 | <listitem> |
---|
53 | <para> |
---|
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"/>. |
---|
58 | </para> |
---|
59 | </listitem> |
---|
60 | </itemizedlist> |
---|
61 | </sect1> |
---|
62 | |
---|
63 | <sect1 id="section-bin-create"> |
---|
64 | <title>Creating a bin</title> |
---|
65 | <para> |
---|
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. |
---|
76 | </para> |
---|
77 | <programlisting><!-- example-begin bin.c a --> |
---|
78 | #include <gst/gst.h> |
---|
79 | |
---|
80 | int |
---|
81 | main (int argc, |
---|
82 | char *argv[]) |
---|
83 | { |
---|
84 | GstElement *bin, *pipeline, *source, *sink; |
---|
85 | |
---|
86 | /* init */ |
---|
87 | gst_init (&argc, &argv); |
---|
88 | |
---|
89 | /* create */ |
---|
90 | pipeline = gst_pipeline_new ("my_pipeline"); |
---|
91 | bin = gst_pipeline_new ("my_bin"); |
---|
92 | source = gst_element_factory_make ("fakesrc", "source"); |
---|
93 | sink = gst_element_factory_make ("fakesink", "sink"); |
---|
94 | |
---|
95 | /* set up pipeline */ |
---|
96 | gst_bin_add_many (GST_BIN (bin), source, sink, NULL); |
---|
97 | gst_bin_add (GST_BIN (pipeline), bin); |
---|
98 | gst_element_link (source, sink); |
---|
99 | <!-- example-end bin.c a --> |
---|
100 | [..]<!-- example-begin bin.c b --><!-- |
---|
101 | return 0; |
---|
102 | --><!-- example-end bin.c b --> |
---|
103 | <!-- example-begin bin.c c --> |
---|
104 | } |
---|
105 | <!-- example-end bin.c c --></programlisting> |
---|
106 | <para> |
---|
107 | There are various functions to lookup elements in a bin. You can |
---|
108 | also get a list of all elements that a bin contains using the function |
---|
109 | <function>gst_bin_get_list ()</function>. See the API references of |
---|
110 | <ulink type="http" |
---|
111 | url="&URLAPI;GstBin.html"><classname>GstBin</classname></ulink> |
---|
112 | for details. |
---|
113 | </para> |
---|
114 | </sect1> |
---|
115 | |
---|
116 | <sect1 id="section-bin-custom"> |
---|
117 | <title>Custom bins</title> |
---|
118 | <para> |
---|
119 | The application programmer can create custom bins packed with elements |
---|
120 | to perform a specific task. This allows you, for example, to write |
---|
121 | an Ogg/Vorbis decoder with just the following lines of code: |
---|
122 | </para> |
---|
123 | <programlisting> |
---|
124 | int |
---|
125 | main (int argc |
---|
126 | char *argv[]) |
---|
127 | { |
---|
128 | GstElement *player; |
---|
129 | |
---|
130 | /* init */ |
---|
131 | gst_init (&argc, &argv); |
---|
132 | |
---|
133 | /* create player */ |
---|
134 | player = gst_element_factory_make ("oggvorbisplayer", "player"); |
---|
135 | |
---|
136 | /* set the source audio file */ |
---|
137 | g_object_set (G_OBJECT (player), "location", "helloworld.ogg", NULL); |
---|
138 | |
---|
139 | /* start playback */ |
---|
140 | gst_element_set_state (GST_ELEMENT (mp3player), GST_STATE_PLAYING); |
---|
141 | [..] |
---|
142 | } |
---|
143 | </programlisting> |
---|
144 | <para> |
---|
145 | Custom bins can be created with a plugin or an XML description. You |
---|
146 | will find more information about creating custom bin in the <ulink |
---|
147 | type="http" |
---|
148 | url="http://gstreamer.freedesktop.org/data/doc/gstreamer/head/pwg/html/index.html">Plugin |
---|
149 | Writers Guide</ulink>. |
---|
150 | </para> |
---|
151 | </sect1> |
---|
152 | </chapter> |
---|