source: trunk/third/gstreamer/docs/manual/basics-elements.xml @ 21448

Revision 21448, 21.2 KB checked in by ghudson, 20 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r21447, which included commits to RCS files with non-trunk default branches.
Line 
1<chapter id="chapter-elements" xreflabel="Elements">
2  <title>Elements</title>
3  <para>
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>
11  </para>
12
13  <sect1 id="section-elements-design" xreflabel="What are elements?">
14    <title>What are elements?</title>
15    <para>
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.
23    </para>
24
25    <sect2 id="section-elements-src">
26      <title>Source elements</title>
27      <para>
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
32        the element.
33      </para>
34      <figure float="1" id="section-element-srcimg">
35        <title>Visualisation of a source element</title>
36          <mediaobject> 
37            <imageobject>
38              <imagedata fileref="images/src-element.&image;"
39              format="&IMAGE;"/>
40            </imageobject>
41          </mediaobject>
42      </figure>
43      <para>
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.
47      </para>
48    </sect2>
49
50    <sect2 id="section-elements-filter">
51      <title>Filters, convertors, demuxers, muxers and codecs</title>
52      <para>
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.
58      </para>
59      <para>
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
64        source and sink pads.
65      </para>
66      <figure float="1" id="section-element-filterimg">
67        <title>Visualisation of a filter element</title>
68          <mediaobject> 
69            <imageobject>
70              <imagedata fileref="images/filter-element.&image;"
71              format="&IMAGE;"/>
72            </imageobject>
73          </mediaobject>
74      </figure>
75      <para>
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.
80      </para>
81      <figure float="1" id="section-element-multifilterimg">
82        <title>Visualisation of a filter element with
83          more than one output pad</title>
84        <mediaobject> 
85          <imageobject>
86            <imagedata fileref="images/filter-element-multi.&image;"
87            format="&IMAGE;" />
88          </imageobject>
89        </mediaobject>
90      </figure>
91      <para>
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.
100      </para>
101    </sect2>
102 
103    <sect2 id="section-elements-sink">
104      <title>Sink elements</title>
105      <para>
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.
110      </para>
111      <figure float="1" id="section-element-sinkimg">
112        <title>Visualisation of a sink element</title>
113        <mediaobject> 
114          <imageobject>
115            <imagedata fileref="images/sink-element.&image;"
116            format="&IMAGE;" />
117          </imageobject>
118        </mediaobject>
119      </figure>
120    </sect2>
121  </sect1>
122
123  <sect1 id="section-elements-create">
124    <title>Creating a <classname>GstElement</classname></title>
125    <para>
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,
133      default name.
134    </para>
135    <para>
136      When you don't need the element anymore, you need to unref it using
137      <ulink type="http"
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
142      to 0.
143    </para>
144    <para>
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.
149    </para>
150    <programlisting><!-- example-begin elementmake.c --><![CDATA[
151#include <gst/gst.h>
152
153int
154main (int   argc,
155      char *argv[])
156{
157  GstElement *element;
158
159  /* init GStreamer */
160  gst_init (&argc, &argv);
161
162  /* create element */
163  element = gst_element_factory_make ("fakesrc", "source");
164  if (!element) {
165    g_print ("Failed to create element of type 'fakesrc'\n");
166    return -1;
167  }
168
169  gst_object_unref (GST_OBJECT (element));
170
171  return 0;
172}
173    ]]><!-- example-end elementmake.c --></programlisting>
174    <para>
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>.
184    </para>
185    <para>
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.
192    </para>
193    <programlisting><!-- example-begin elementcreate.c --><![CDATA[
194#include <gst/gst.h>
195
196int
197main (int   argc,
198      char *argv[])
199{
200  GstElementFactory *factory;
201  GstElement * element;
202
203  /* init GStreamer */
204  gst_init (&argc, &argv);
205
206  /* create element, method #2 */
207  factory = gst_element_factory_find ("fakesrc");
208  if (!factory) {
209    g_print ("Failed to find factory of type 'fakesrc'\n");
210    return -1;
211  }
212  element = gst_element_factory_create (factory, "source");
213  if (!element) {
214    g_print ("Failed to create element, even though its factory exists!\n");
215    return -1;
216  }
217
218  gst_object_unref (GST_OBJECT (element));
219
220  return 0;
221}
222    ]]><!-- example-end elementcreate.c --></programlisting>
223  </sect1>
224
225  <sect1 id="section-elements-properties">
226    <title>Using an element as a <classname>GObject</classname></title>
227    <para>
228      A <ulink type="http"
229      url="&URLAPI;GstElement.html"><classname>GstElement</classname></ulink>
230      can have several properties which are implemented using standard
231      <classname>GObject</classname> properties. The usual
232      <classname>GObject</classname> methods to query, set and get
233      property values and <classname>GParamSpecs</classname> are
234      therefore supported.
235    </para>
236    <para>
237      Every <classname>GstElement</classname> inherits at least one
238      property from its parent <classname>GstObject</classname>: the
239      "name" property. This is the name you provide to the functions
240      <function>gst_element_factory_make ()</function> or
241      <function>gst_element_factory_create ()</function>. You can get
242      and set this property using the functions
243      <function>gst_object_set_name</function> and
244      <function>gst_object_get_name</function> or use the
245      <classname>GObject</classname> property mechanism as shown below.
246    </para>
247    <programlisting><!-- example-begin elementget.c --><![CDATA[
248#include <gst/gst.h>
249
250int
251main (int   argc,
252      char *argv[])
253{
254  GstElement *element;
255  const gchar *name;
256
257  /* init GStreamer */
258  gst_init (&argc, &argv);
259
260  /* create element */
261  element = gst_element_factory_make ("fakesrc", "source");
262
263  /* get name */
264  g_object_get (G_OBJECT (element), "name", &name, NULL);
265  g_print ("The name of the element is '%s'.\n", name);
266
267  gst_object_unref (GST_OBJECT (element));
268
269  return 0;
270}
271    ]]><!-- example-end elementget.c --></programlisting>
272    <para>
273      Most plugins provide additional properties to provide more information
274      about their configuration or to configure the element.
275      <command>gst-inspect</command> is a useful tool to query the properties
276      of a particular element, it will also use property introspection to give
277      a short explanation about the function of the property and about the
278      parameter types and ranges it supports. See the appendix for details
279      about <command>gst-inspect</command>.
280    </para>
281    <para>
282      For more information about <classname>GObject</classname>
283      properties we recommend you read the <ulink
284      url="http://developer.gnome.org/doc/API/2.0/gobject/index.html"
285      type="http">GObject manual</ulink> and an introduction to <ulink
286      url="http://le-hacker.org/papers/gobject/index.html" type="http">The
287      Glib Object system</ulink>.
288    </para>
289    <para>
290      A <ulink type="http" url="&URLAPI;gstreamer/html/GstElementFactory.html">
291      <classname>GstElement</classname></ulink> also provides various
292      <classname>GObject</classname> signals that can be used as a flexible
293      callback mechanism. Here, too, you can use <command>gst-inspect</command>
294      to see which signals a specific elements supports. Together, signals
295      and properties are the most basic way in which elements and
296      applications interact.
297    </para>
298  </sect1>
299
300  <sect1 id="section-elements-factories">
301    <title>More about element factories</title>
302    <para>
303      In the previous section, we briefly introduced the <ulink type="http"
304      url="&URLAPI;GstElement.html"><classname>GstElementFactory</classname></ulink>
305      object already as a way to create instances of an element. Element
306      factories, however, are much more than just that. Element factories
307      are the basic types retrieved from the &GStreamer; registry, they
308      describe all plugins and elements that &GStreamer; can create. This
309      means that element factories are useful for automated element
310      instancing, such as what autopluggers do, and for creating lists
311      of available elements, such as what pipeline editing applications
312      (e.g. <ulink type="http"
313      url="http://gstreamer.freedesktop.org/modules/gst-editor.html">&GStreamer;
314      Editor</ulink>) do.
315    </para>
316   
317    <sect2 id="section-elements-factories-details">
318      <title>Getting information about an element using a factory</title>
319      <para>
320        Tools like <command>gst-inspect</command> will provide some generic
321        information about an element, such as the person that wrote the
322        plugin, a descriptive name (and a shortname), a rank and a category.
323        The category can be used to get the type of the element that can
324        be created using this element factory. Examples of categories include
325        <classname>Codec/Decoder/Video</classname> (video decoder),
326        <classname>Codec/Encoder/Video</classname> (video encoder),
327        <classname>Source/Video</classname> (a video generator),
328        <classname>Sink/Video</classname> (a video output), and all these
329        exist for audio as well, of course. Then, there's also
330        <classname>Codec/Demuxer</classname> and
331        <classname>Codec/Muxer</classname> and a whole lot more.
332        <command>gst-inspect</command> will give a list of all factories, and
333        <command>gst-inspect &lt;factory-name&gt;</command> will list all
334        of the above information, and a lot more.
335      </para>
336      <programlisting><!-- example-begin elementfactory.c --><![CDATA[
337#include <gst/gst.h>
338
339int
340main (int   argc,
341      char *argv[])
342{
343  GstElementFactory *factory;
344
345  /* init GStreamer */
346  gst_init (&argc, &argv);
347
348  /* get factory */
349  factory = gst_element_factory_find ("sinesrc");
350  if (!factory) {
351    g_print ("You don't have the 'sinesrc' element installed, go get it!\n");
352    return -1;
353  }
354
355  /* display information */
356  g_print ("The '%s' element is a member of the category %s.\n"
357           "Description: %s\n",
358           gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)),
359           gst_element_factory_get_klass (factory),
360           gst_element_factory_get_description (factory));
361
362  return 0;
363}
364      ]]><!-- example-end elementfactory.c --></programlisting>
365      <para>
366        You can use <function>gst_registry_pool_feature_list (GST_TYPE_ELEMENT_FACTORY)</function>
367        to get a list of all the element factories that &GStreamer; knows
368        about.
369      </para>
370    </sect2>
371   
372    <sect2 id="section-elements-factories-padtemplates">
373      <title>Finding out what pads an element can contain</title>
374      <para>
375        Perhaps the most powerful feature of element factories is that
376        they contain a full description of the pads that the element
377        can generate, and the capabilities of those pads (in layman words:
378        what types of media can stream over those pads), without actually
379        having to load those plugins into memory. This can be used
380        to provide a codec selection list for encoders, or it can be used
381        for autoplugging purposes for media players. All current
382        &GStreamer;-based media players and autopluggers work this way.
383        We'll look closer at these features as we learn about
384        <classname>GstPad</classname> and <classname>GstCaps</classname>
385        in the next chapter: <xref linkend="chapter-pads"/>
386      </para>
387    </sect2>
388  </sect1>
389
390  <sect1 id="section-elements-link" xreflabel="Linking elements">
391    <title>Linking elements</title>
392    <para>
393      By linking a source element with zero or more filter-like
394      elements and finally a sink element, you set up a media
395      pipeline. Data will flow through the elements. This is the
396      basic concept of media handling in &GStreamer;.
397    </para>
398    <figure float="1" id="section-link">
399      <title>Visualisation of three linked elements</title>
400        <mediaobject>
401          <imageobject>
402            <imagedata fileref="images/linked-elements.&image;"
403            format="&IMAGE;"/>
404          </imageobject>
405        </mediaobject>
406    </figure>
407    <para>
408      By linking these three elements, we have created a very simple
409      chain of elements. The effect of this will be that the output of
410      the source element (<quote>element1</quote>) will be used as input
411      for the filter-like element (<quote>element2</quote>). The
412      filter-like element will do something with the data and send the
413      result to the final sink element (<quote>element3</quote>).
414    </para>
415    <para>
416      Imagine the above graph as a simple Ogg/Vorbis audio decoder. The
417      source is a disk source which reads the file from disc. The second
418      element is a Ogg/Vorbis audio decoder. The sink element is your
419      soundcard, playing back the decoded audio data. We will use this
420      simple graph to construct an Ogg/Vorbis player later in this manual.
421    </para>
422    <para>
423      In code, the above graph is written like this:
424    </para>
425    <programlisting><!-- example-begin elementlink.c a -->
426#include &lt;gst/gst.h&gt;
427
428int
429main (int   argc,
430      char *argv[])
431{
432  GstElement *source, *filter, *sink;
433
434  /* init */
435  gst_init (&amp;argc, &amp;argv);
436
437  /* create elements */
438  source = gst_element_factory_make ("fakesrc", "source");
439  filter = gst_element_factory_make ("identity", "filter");
440  sink = gst_element_factory_make ("fakesink", "sink");
441
442  /* link */
443  gst_element_link_many (source, filter, sink, NULL);
444<!-- example-end elementlink.c a -->
445[..]<!-- example-begin elementlink.c b --><!--
446  return 0;
447--><!-- example-end elementlink.c b -->
448<!-- example-begin elementlink.c c -->
449}
450    <!-- example-end elementlink.c c --></programlisting>
451    <para>
452      For more specific behaviour, there are also the functions
453      <function>gst_element_link ()</function> and
454      <function>gst_element_link_pads ()</function>. You can also obtain
455      references to individual pads and link those using various
456      <function>gst_pad_link_* ()</function> functions. See the API
457      references for more details.
458    </para>
459  </sect1>
460
461  <sect1 id="section-elements-states">
462    <title>Element States</title>
463    <para>
464      After being created, an element will not actually perform any actions
465      yet. You need to change elements state to make it do something.
466      &GStreamer; knows four element states, each with a very specific
467      meaning. Those four states are:
468    </para>
469    <itemizedlist>
470      <listitem>
471        <para>
472          <classname>GST_STATE_NULL</classname>: this is the default state.
473          This state will deallocate all resources held by the element.
474        </para>
475      </listitem>
476      <listitem>
477        <para>
478          <classname>GST_STATE_READY</classname>: in the ready state, an
479          element has allocated all of its global resources, that is,
480          resources that can be kept within streams. You can think about
481          opening devices, allocating buffers and so on. However, the
482          stream is not opened in this state, so the stream positions is
483          automatically zero. If a stream was previously opened, it should
484          be closed in this state, and position, properties and such should
485          be reset.
486        </para>
487      </listitem>
488      <listitem>
489        <para>
490          <classname>GST_STATE_PAUSED</classname>: in this state, an
491          element has opened the stream, but is not actively processing
492          it. An element should not modify the stream's position, data or
493          anything else in this state. When set back to PLAYING, it should
494          continue processing at the point where it left off as soon as
495          possible.
496        </para>
497      </listitem>
498      <listitem>
499        <para>
500          <classname>GST_STATE_PLAYING</classname>: in the PLAYING state,
501          an element does exactly the same as in the PAUSED state, except
502          that it actually processes data.
503        </para>
504      </listitem>
505    </itemizedlist>
506    <para>
507      You can change the state of an element using the function
508      <function>gst_element_set_state ()</function>. If you set an element
509      to another state, &GStreamer; will internally traverse all intermediate
510      states. So if you set an element from NULL to PLAYING, &GStreamer;
511      will internally set the element to READY and PAUSED in between.
512    </para>
513    <para>
514      Even though an element in <classname>GST_STATE_PLAYING</classname>
515      is ready for data processing, it will not necessarily do that. If
516      the element is placed in a thread (see <xref
517      linkend="chapter-threads"/>), it will process data automatically.
518      In other cases, however, you will need to <emphasis>iterate</emphasis>
519      the element's container.
520    </para>
521  </sect1>
522</chapter>
Note: See TracBrowser for help on using the repository browser.