1 <chapter id="chapter-elements" xreflabel="Elements">
2 <title>Elements</title>
4 The most important object in &GStreamer; for the application programmer
5 is the <ulink type="http"
6 url="../../gstreamer/html/GstElement.html"><classname>GstElement</classname></ulink>
7 object. An element is the basic building block for a media pipeline. All
8 the different high-level components you will use are derived from
9 <classname>GstElement</classname>. Every decoder, encoder, demuxer, video
10 or audio output is in fact a <classname>GstElement</classname>
13 <sect1 id="section-elements-design" xreflabel="What are elements?">
14 <title>What are elements?</title>
16 For the application programmer, elements are best visualized as black
17 boxes. On the one end, you might put something in, the element does
18 something with it and something else comes out at the other side. For
19 a decoder element, ifor example, you'd put in encoded data, and the
20 element would output decoded data. In the next chapter (see <xref
21 linkend="chapter-pads"/>), you will learn more about data input and
22 output in elements, and how you can set that up in your application.
25 <sect2 id="section-elements-src">
26 <title>Source elements</title>
28 Source elements generate data for use by a pipeline, for example
29 reading from disk or from a sound card. <xref
30 linkend="section-element-srcimg"/> shows how we will visualise
31 a source element. We always draw a source pad to the right of
34 <figure float="1" id="section-element-srcimg">
35 <title>Visualisation of a source element</title>
38 <imagedata fileref="images/src-element.ℑ"
44 Source elements do not accept data, they only generate data. You can
45 see this in the figure because it only has a source pad (on the
46 right). A source pad can only generate data.
50 <sect2 id="section-elements-filter">
51 <title>Filters, convertors, demuxers, muxers and codecs</title>
53 Filters and filter-like elements have both input and outputs pads.
54 They operate on data that they receive on their input (sink) pads,
55 and will provide data on their output (source) pads. Examples of
56 such elements are a volume element (filter), a video scaler
57 (convertor), an Ogg demuxer or a Vorbis decoder.
60 Filter-like elements can have any number of source or sink pads. A
61 video demuxer, for example, would have one sink pad and several
62 (1-N) source pads, one for each elementary stream contained in the
63 container format. Decoders, on the other hand, will only have one
66 <figure float="1" id="section-element-filterimg">
67 <title>Visualisation of a filter element</title>
70 <imagedata fileref="images/filter-element.ℑ"
76 <xref linkend="section-element-filterimg"/> shows how we will
77 visualise a filter-like element. This specific element has one source
78 and one sink element. Sink pads, receiving input data, are depicted
79 at the left of the element; source pads are still on the right.
81 <figure float="1" id="section-element-multifilterimg">
82 <title>Visualisation of a filter element with
83 more than one output pad</title>
86 <imagedata fileref="images/filter-element-multi.ℑ"
92 <xref linkend="section-element-multifilterimg"/> shows another
93 filter-like element, this one having more than one output (source)
94 pad. An example of one such element could, for example, be an Ogg
95 demuxer for an Ogg stream containing both audio and video. One
96 source pad will contain the elementary video stream, another will
97 contain the elementary audio stream. Demuxers will generally fire
98 signals when a new pad is created. The application programmer can
99 then handle the new elementary stream in the signal handler.
103 <sect2 id="section-elements-sink">
104 <title>Sink elements</title>
106 Sink elements are end points in a media pipeline. They accept
107 data but do not produce anything. Disk writing, soundcard playback,
108 and video output would all be implemented by sink elements.
109 <xref linkend="section-element-sinkimg"/> shows a sink element.
111 <figure float="1" id="section-element-sinkimg">
112 <title>Visualisation of a sink element</title>
115 <imagedata fileref="images/sink-element.ℑ"
123 <sect1 id="section-elements-create">
124 <title>Creating a <classname>GstElement</classname></title>
126 The simplest way to create an element is to use <ulink type="http"
127 url="&URLAPI;GstElementFactory.html#gst-element-factory-make"><function>gst_element_factory_make
128 ()</function></ulink>. This function takes a factory name and an
129 element name for the newly created element. The name of the element
130 is something you can use later on to look up the element in a bin,
131 for example. The name will also be used in debug output. You can
132 pass <symbol>NULL</symbol> as the name argument to get a unique,
136 When you don't need the element anymore, you need to unref it using
138 url="&URLAPI;GstObject.html#gst-object-unref"><function>gst_object_unref
139 ()</function></ulink>. This decreases the reference count for the
140 element by 1. An element has a refcount of 1 when it gets created.
141 An element gets destroyed completely when the refcount is decreased
145 The following example &EXAFOOT; shows how to create an element named
146 <emphasis>source</emphasis> from the element factory named
147 <emphasis>fakesrc</emphasis>. It checks if the creation succeeded.
148 After checking, it unrefs the element.
150 <programlisting><![CDATA[
160 gst_init (&argc, &argv);
163 element = gst_element_factory_make ("fakesrc", "source");
165 g_print ("Failed to create element of type 'fakesrc'\n");
169 gst_object_unref (GST_OBJECT (element));
175 <function>gst_element_factory_make</function> is actually a shorthand
176 for a combination of two functions. A <ulink type="http"
177 url="&URLAPI;GstElement.html"><classname>GstElement</classname></ulink>
178 object is created from a factory. To create the element, you have to
179 get access to a <ulink type="http"
180 url="&URLAPI;GstElementFactory.html"><classname>GstElementFactory</classname></ulink>
181 object using a unique factory name. This is done with <ulink type="http"
182 url="&URLAPI;GstElementFactory.html#gst-element-factory-find"><function>gst_element_factory_find
183 ()</function></ulink>.
186 The following code fragment is used to get a factory that can be used
187 to create the <emphasis>fakesrc</emphasis> element, a fake data source.
188 The function <ulink type="http"
189 url="&URLAPI;GstElementFactory.html#gst-element-factory-create"><function>gst_element_factory_create
190 ()</function></ulink> will use the element factory to create an
191 element with the given name.
193 <programlisting><![CDATA[
198 GstElementFactory *factory;
199 GstElement * element;
202 gst_init (&argc, &argv);
204 /* create element, method #2 */
205 factory = gst_element_factory_find ("fakesrc");
207 g_print ("Failed to find fctory of type 'fakesrc'\n");
210 element = gst_element_factory_create (factory, "source");
212 gst_object_unref (GST_OBJECT (element));
219 <sect1 id="section-elements-properties">
220 <title>Using an element as a <classname>GObject</classname></title>
223 url="&URLAPI;GstElement.html"><classname>GstElement</classname></ulink>
224 can have several properties which are implemented using standard
225 <classname>GObject</classname> properties. The usual
226 <classname>GObject</classname> methods to query, set and get
227 property values and <classname>GParamSpecs</classname> are
231 Every <classname>GstElement</classname> inherits at least one
232 property from its parent <classname>GstObject</classname>: the
233 "name" property. This is the name you provide to the functions
234 <function>gst_element_factory_make ()</function> or
235 <function>gst_element_factory_create ()</function>. You can get
236 and set this property using the functions
237 <function>gst_object_set_name</function> and
238 <function>gst_object_get_name</function> or use the
239 <classname>GObject</classname> property mechanism as shown below.
241 <programlisting><![CDATA[
252 gst_init (&argc, &argv);
255 element = gst_element_factory_make ("fakesrc", "source");
258 g_object_get (G_OBJECT (element), "name", &name, NULL);
259 g_print ("The name of the element is '%s'.\n", name);
261 gst_object_unref (GST_OBJECT (element));
267 Most plugins provide additional properties to provide more information
268 about their configuration or to configure the element.
269 <command>gst-inspect</command> is a useful tool to query the properties
270 of a particular element, it will also use property introspection to give
271 a short explanation about the function of the property and about the
272 parameter types and ranges it supports. See the appendix for details
273 about <command>gst-inspect</command>.
276 For more information about <classname>GObject</classname>
277 properties we recommend you read the <ulink
278 url="http://developer.gnome.org/doc/API/2.0/gobject/index.html"
279 type="http">GObject manual</ulink> and an introduction to <ulink
280 url="http://le-hacker.org/papers/gobject/index.html" type="http">The
281 Glib Object system</ulink>.
284 A <ulink type="http" url="&URLAPI;gstreamer/html/GstElementFactory.html">
285 <classname>GstElement</classname></ulink> also provides various
286 <classname>GObject</classname> signals that can be used as a flexible
287 callback mechanism. Here, too, you can use <command>gst-inspect</command>
288 to see which signals a specific elements supports. Together, signals
289 and properties are the most basic way in which elements and
290 applications interact.
294 <sect1 id="section-elements-factories">
295 <title>More about element factories</title>
297 In the previous section, we briefly introduced the <ulink type="http"
298 url="&URLAPI;GstElement.html"><classname>GstElementFactory</classname></ulink>
299 object already as a way to create instances of an element. Element
300 factories, however, are much more than just that. Element factories
301 are the basic types retrieved from the &GStreamer; registry, they
302 describe all plugins and elements that &GStreamer; can create. This
303 means that element factories are useful for automated element
304 instancing, such as what autopluggers do, and for creating lists
305 of available elements, such as what pipeline editing applications
306 (e.g. <ulink type="http"
307 url="http://gstreamer.freedesktop.org/modules/gst-editor.html">&GStreamer;
311 <sect2 id="section-elements-factories-details">
312 <title>Getting information about an element using a factory</title>
314 Tools like <command>gst-inspect</command> will provide some generic
315 information about an element, such as the person that wrote the
316 plugin, a descriptive name (and a shortname), a rank and a category.
317 The category can be used to get the type of the element that can
318 be created using this element factory. Examples of categories include
319 <classname>Codec/Decoder/Video</classname> (video decoder),
320 <classname>Codec/Encoder/Video</classname> (video encoder),
321 <classname>Source/Video</classname> (a video generator),
322 <classname>Sink/Video</classname> (a video output), and all these
323 exist for audio as well, of course. Then, there's also
324 <classname>Codec/Demuxer</classname> and
325 <classname>Codec/Muxer</classname> and a whole lot more.
326 <command>gst-inspect</command> will give a list of all factories, and
327 <command>gst-inspect <factory-name></command> will list all
328 of the above information, and a lot more.
330 <programlisting><![CDATA[
337 GstElementFactory *factory;
340 gst_init (&argc, &argv);
343 factory = gst_element_factory_find ("sinesrc");
345 g_print ("You don't have the 'sinesrc' element installed, go get it!\n");
349 /* display information */
350 g_print ("The '%s' element is a member of the category %s.\n"
352 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)),
353 gst_element_factory_get_klass (factory),
354 gst_element_factory_get_description (factory));
360 You can use <function>gst_registry_pool_feature_list (GST_TYPE_ELEMENT_FACTORY)</function>
361 to get a list of all the element factories that &GStreamer; knows
366 <sect2 id="section-elements-factories-padtemplates">
367 <title>Finding out what pads an element can contain</title>
369 Perhaps the most powerful feature of element factories is that
370 they contain a full description of the pads that the element
371 can generate, and the capabilities of those pads (in layman words:
372 what types of media can stream over those pads), without actually
373 having to load those plugins into memory. This can be used
374 to provide a codec selection list for encoders, or it can be used
375 for autoplugging purposes for media players. All current
376 &GStreamer;-based media players and autopluggers work this way.
377 We'll look closer at these features as we learn about
378 <classname>GstPad</classname> and <classname>GstCaps</classname>
379 in the next chapter: <xref linkend="chapter-pads"/>
384 <sect1 id="section-elements-link" xreflabel="Linking elements">
385 <title>Linking elements</title>
387 By linking a source element with zero or more filter-like
388 elements and finally a sink element, you set up a media
389 pipeline. Data will flow through the elements. This is the
390 basic concept of media handling in &GStreamer;.
392 <figure float="1" id="section-link">
393 <title>Visualisation of three linked elements</title>
396 <imagedata fileref="images/linked-elements.ℑ"
402 By linking these three elements, we have created a very simple
403 chain of elements. The effect of this will be that the output of
404 the source element (<quote>element1</quote>) will be used as input
405 for the filter-like element (<quote>element2</quote>). The
406 filter-like element will do something with the data and send the
407 result to the final sink element (<quote>element3</quote>).
410 Imagine the above graph as a simple Ogg/Vorbis audio decoder. The
411 source is a disk source which reads the file from disc. The second
412 element is a Ogg/Vorbis audio decoder. The sink element is your
413 soundcard, playing back the decoded audio data. We will use this
414 simple graph to construct an Ogg/Vorbis player later in this manual.
417 In code, the above graph is written like this:
424 GstElement *source, *filter, *sink;
427 gst_init (&argc, &argv);
429 /* create elements */
430 source = gst_element_factory_make ("fakesrc", "source");
431 filter = gst_element_factory_make ("identity", "filter");
432 sink = gst_element_factory_make ("fakesink", "sink");
435 gst_element_link_many (source, filter, sink, NULL);
441 For more specific behaviour, there are also the functions
442 <function>gst_element_link ()</function> and
443 <function>gst_element_link_pads ()</function>. You can also obtain
444 references to individual pads and link those using various
445 <function>gst_pad_link_* ()</function> functions. See the API
446 references for more details.
450 <sect1 id="section-elements-states">
451 <title>Element States</title>
453 After being created, an element will not actually perform any actions
454 yet. You need to change elements state to make it do something.
455 &GStreamer; knows four element states, each with a very specific
456 meaning. Those four states are:
461 <classname>GST_STATE_NULL</classname>: this is the default state.
462 This state will deallocate all resources held by the element.
467 <classname>GST_STATE_READY</classname>: in the ready state, an
468 element has allocated all of its global resources, that is,
469 resources that can be kept within streams. You can think about
470 opening devices, allocating buffers and so on. However, the
471 stream is not opened in this state, so the stream positions is
472 automatically zero. If a stream was previously opened, it should
473 be closed in this state, and position, properties and such should
479 <classname>GST_STATE_PAUSED</classname>: in this state, an
480 element has opened the stream, but is not actively processing
481 it. An element should not modify the stream's position, data or
482 anything else in this state. When set back to PLAYING, it should
483 continue processing at the point where it left off as soon as
489 <classname>GST_STATE_PLAYING</classname>: in the PLAYING state,
490 an element does exactly the same as in the PAUSED state, except
491 that it actually processes data.
496 You can change the state of an element using the function
497 <function>gst_element_set_state ()</function>. If you set an element
498 to another state, &GStreamer; will internally traverse all intermediate
499 states. So if you set an element from NULL to PLAYING, &GStreamer;
500 will internally set the element to READY and PAUSED in between.
503 Even though an element in <classname>GST_STATE_PLAYING</classname>
504 is ready for data processing, it will not necessarily do that. If
505 the element is placed in a thread (see <xref
506 linkend="chapter-threads"/>), it will process data automatically.
507 In other cases, however, you will need to <emphasis>iterate</emphasis>
508 the element's container.