Implement our own theme, yay!
[platform/upstream/gstreamer.git] / manual-elements.md
1 ---
2 title: Elements
3 ...
4
5 # Elements
6
7 The most important object in GStreamer for the application programmer is
8 the
9 [`GstElement`](http://gstreamer.freedesktop.org/data/doc/gstreamer/stable/gstreamer/html/GstElement.html)
10 object. An element is the basic building block for a media pipeline. All
11 the different high-level components you will use are derived from
12 `GstElement`. Every decoder, encoder, demuxer, video or audio output is
13 in fact a `GstElement`
14
15 ## What are elements?
16
17 For the application programmer, elements are best visualized as black
18 boxes. On the one end, you might put something in, the element does
19 something with it and something else comes out at the other side. For a
20 decoder element, for example, you'd put in encoded data, and the element
21 would output decoded data. In the next chapter (see [Pads and
22 capabilities](manual-pads.md)), you will learn more about data input
23 and output in elements, and how you can set that up in your application.
24
25 ### Source elements
26
27 Source elements generate data for use by a pipeline, for example reading
28 from disk or from a sound card. [Visualisation of a source
29 element](#visualisation-of-a-source-element) shows how we will visualise
30 a source element. We always draw a source pad to the right of the
31 element.
32
33 ![Visualisation of a source element](images/src-element.png "fig:")
34
35 Source elements do not accept data, they only generate data. You can see
36 this in the figure because it only has a source pad (on the right). A
37 source pad can only generate data.
38
39 ### Filters, convertors, demuxers, muxers and codecs
40
41 Filters and filter-like elements have both input and outputs pads. They
42 operate on data that they receive on their input (sink) pads, and will
43 provide data on their output (source) pads. Examples of such elements
44 are a volume element (filter), a video scaler (convertor), an Ogg
45 demuxer or a Vorbis decoder.
46
47 Filter-like elements can have any number of source or sink pads. A video
48 demuxer, for example, would have one sink pad and several (1-N) source
49 pads, one for each elementary stream contained in the container format.
50 Decoders, on the other hand, will only have one source and sink pads.
51
52 ![Visualisation of a filter element](images/filter-element.png "fig:")
53
54 [Visualisation of a filter element](#visualisation-of-a-filter-element)
55 shows how we will visualise a filter-like element. This specific element
56 has one source and one sink element. Sink pads, receiving input data,
57 are depicted at the left of the element; source pads are still on the
58 right.
59
60 ![Visualisation of a filter element with more than one output
61 pad](images/filter-element-multi.png "fig:")
62
63 [Visualisation of a filter element with more than one output
64 pad](#visualisation-of-a-filter-element-with----more-than-one-output-pad)
65 shows another filter-like element, this one having more than one output
66 (source) pad. An example of one such element could, for example, be an
67 Ogg demuxer for an Ogg stream containing both audio and video. One
68 source pad will contain the elementary video stream, another will
69 contain the elementary audio stream. Demuxers will generally fire
70 signals when a new pad is created. The application programmer can then
71 handle the new elementary stream in the signal handler.
72
73 ### Sink elements
74
75 Sink elements are end points in a media pipeline. They accept data but
76 do not produce anything. Disk writing, soundcard playback, and video
77 output would all be implemented by sink elements. [Visualisation of a
78 sink element](#visualisation-of-a-sink-element) shows a sink element.
79
80 ![Visualisation of a sink element](images/sink-element.png "fig:")
81
82 ## Creating a `GstElement`
83
84 The simplest way to create an element is to use
85 [`gst_element_factory_make
86 ()`](http://gstreamer.freedesktop.org/data/doc/gstreamer/stable/gstreamer/html/GstElementFactory.html#gst-element-factory-make).
87 This function takes a factory name and an element name for the newly
88 created element. The name of the element is something you can use later
89 on to look up the element in a bin, for example. The name will also be
90 used in debug output. You can pass `NULL` as the name argument to get a
91 unique, default name.
92
93 When you don't need the element anymore, you need to unref it using
94 [`gst_object_unref 
95 ()`](http://gstreamer.freedesktop.org/data/doc/gstreamer/stable/gstreamer/html/GstObject.html#gst-object-unref).
96 This decreases the reference count for the element by 1. An element has
97 a refcount of 1 when it gets created. An element gets destroyed
98 completely when the refcount is decreased to 0.
99
100 The following example \[1\] shows how to create an element named
101 *source* from the element factory named *fakesrc*. It checks if the
102 creation succeeded. After checking, it unrefs the element.
103
104 ``` c
105 #include <gst/gst.h>
106
107 int
108 main (int   argc,
109       char *argv[])
110 {
111   GstElement *element;
112
113   /* init GStreamer */
114   gst_init (&argc, &argv);
115
116   /* create element */
117   element = gst_element_factory_make ("fakesrc", "source");
118   if (!element) {
119     g_print ("Failed to create element of type 'fakesrc'\n");
120     return -1;
121   }
122
123   gst_object_unref (GST_OBJECT (element));
124
125   return 0;
126 }
127     
128 ```
129
130 `gst_element_factory_make` is actually a shorthand for a combination of
131 two functions. A
132 [`GstElement`](http://gstreamer.freedesktop.org/data/doc/gstreamer/stable/gstreamer/html/GstElement.html)
133 object is created from a factory. To create the element, you have to get
134 access to a
135 [`GstElementFactory`](http://gstreamer.freedesktop.org/data/doc/gstreamer/stable/gstreamer/html/GstElementFactory.html)
136 object using a unique factory name. This is done with
137 [`gst_element_factory_find
138 ()`](http://gstreamer.freedesktop.org/data/doc/gstreamer/stable/gstreamer/html/GstElementFactory.html#gst-element-factory-find).
139
140 The following code fragment is used to get a factory that can be used to
141 create the *fakesrc* element, a fake data source. The function
142 [`gst_element_factory_create
143 ()`](http://gstreamer.freedesktop.org/data/doc/gstreamer/stable/gstreamer/html/GstElementFactory.html#gst-element-factory-create)
144 will use the element factory to create an element with the given name.
145
146 ``` c
147 #include <gst/gst.h>
148
149 int
150 main (int   argc,
151       char *argv[])
152 {
153   GstElementFactory *factory;
154   GstElement * element;
155
156   /* init GStreamer */
157   gst_init (&argc, &argv);
158
159   /* create element, method #2 */
160   factory = gst_element_factory_find ("fakesrc");
161   if (!factory) {
162     g_print ("Failed to find factory of type 'fakesrc'\n");
163     return -1;
164   }
165   element = gst_element_factory_create (factory, "source");
166   if (!element) {
167     g_print ("Failed to create element, even though its factory exists!\n");
168     return -1;
169   }
170
171   gst_object_unref (GST_OBJECT (element));
172
173   return 0;
174 }
175     
176 ```
177
178 ## Using an element as a `GObject`
179
180 A
181 [`GstElement`](http://gstreamer.freedesktop.org/data/doc/gstreamer/stable/gstreamer/html/GstElement.html)
182 can have several properties which are implemented using standard
183 `GObject` properties. The usual `GObject` methods to query, set and get
184 property values and `GParamSpecs` are therefore supported.
185
186 Every `GstElement` inherits at least one property from its parent
187 `GstObject`: the "name" property. This is the name you provide to the
188 functions `gst_element_factory_make ()` or `gst_element_factory_create
189 ()`. You can get and set this property using the functions
190 `gst_object_set_name` and `gst_object_get_name` or use the `GObject`
191 property mechanism as shown below.
192
193 ``` c
194 #include <gst/gst.h>
195
196 int
197 main (int   argc,
198       char *argv[])
199 {
200   GstElement *element;
201   gchar *name;
202
203   /* init GStreamer */
204   gst_init (&argc, &argv);
205
206   /* create element */
207   element = gst_element_factory_make ("fakesrc", "source");
208
209   /* get name */
210   g_object_get (G_OBJECT (element), "name", &name, NULL);
211   g_print ("The name of the element is '%s'.\n", name);
212   g_free (name);
213
214   gst_object_unref (GST_OBJECT (element));
215
216   return 0;
217 }
218     
219 ```
220
221 Most plugins provide additional properties to provide more information
222 about their configuration or to configure the element. `gst-inspect` is
223 a useful tool to query the properties of a particular element, it will
224 also use property introspection to give a short explanation about the
225 function of the property and about the parameter types and ranges it
226 supports. See [gst-inspect](manual-checklist-element.md#gst-inspect) in
227 the appendix for details about `gst-inspect`.
228
229 For more information about `GObject` properties we recommend you read
230 the [GObject
231 manual](http://developer.gnome.org/gobject/stable/rn01.html) and an
232 introduction to [The Glib Object
233 system](http://developer.gnome.org/gobject/stable/pt01.html).
234
235 A
236 [`GstElement`](http://gstreamer.freedesktop.org/data/doc/gstreamer/stable/gstreamer/html/GstElement.html)
237 also provides various `GObject` signals that can be used as a flexible
238 callback mechanism. Here, too, you can use `gst-inspect` to see which
239 signals a specific element supports. Together, signals and properties
240 are the most basic way in which elements and applications interact.
241
242 ## More about element factories
243
244 In the previous section, we briefly introduced the
245 [`GstElementFactory`](http://gstreamer.freedesktop.org/data/doc/gstreamer/stable/gstreamer/html/GstElementFactory.html)
246 object already as a way to create instances of an element. Element
247 factories, however, are much more than just that. Element factories are
248 the basic types retrieved from the GStreamer registry, they describe all
249 plugins and elements that GStreamer can create. This means that element
250 factories are useful for automated element instancing, such as what
251 autopluggers do, and for creating lists of available elements.
252
253 ### Getting information about an element using a factory
254
255 Tools like `gst-inspect` will provide some generic information about an
256 element, such as the person that wrote the plugin, a descriptive name
257 (and a shortname), a rank and a category. The category can be used to
258 get the type of the element that can be created using this element
259 factory. Examples of categories include `Codec/Decoder/Video` (video
260 decoder), `Codec/Encoder/Video` (video encoder), `Source/Video` (a video
261 generator), `Sink/Video` (a video output), and all these exist for audio
262 as well, of course. Then, there's also `Codec/Demuxer` and `Codec/Muxer`
263 and a whole lot more. `gst-inspect` will give a list of all factories,
264 and `gst-inspect <factory-name>` will list all of the above information,
265 and a lot more.
266
267 ``` c
268 #include <gst/gst.h>
269
270 int
271 main (int   argc,
272       char *argv[])
273 {
274   GstElementFactory *factory;
275
276   /* init GStreamer */
277   gst_init (&argc, &argv);
278
279   /* get factory */
280   factory = gst_element_factory_find ("fakesrc");
281   if (!factory) {
282     g_print ("You don't have the 'fakesrc' element installed!\n");
283     return -1;
284   }
285
286   /* display information */
287   g_print ("The '%s' element is a member of the category %s.\n"
288            "Description: %s\n",
289            gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)),
290            gst_element_factory_get_metadata (factory, GST_ELEMENT_METADATA_KLASS),
291            gst_element_factory_get_metadata (factory, GST_ELEMENT_METADATA_DESCRIPTION));
292
293   return 0;
294 }
295       
296 ```
297
298 You can use `gst_registry_pool_feature_list (GST_TYPE_ELEMENT_FACTORY)`
299 to get a list of all the element factories that GStreamer knows about.
300
301 ### Finding out what pads an element can contain
302
303 Perhaps the most powerful feature of element factories is that they
304 contain a full description of the pads that the element can generate,
305 and the capabilities of those pads (in layman words: what types of media
306 can stream over those pads), without actually having to load those
307 plugins into memory. This can be used to provide a codec selection list
308 for encoders, or it can be used for autoplugging purposes for media
309 players. All current GStreamer-based media players and autopluggers work
310 this way. We'll look closer at these features as we learn about `GstPad`
311 and `GstCaps` in the next chapter: [Pads and
312 capabilities](manual-pads.md)
313
314 ## Linking elements
315
316 By linking a source element with zero or more filter-like elements and
317 finally a sink element, you set up a media pipeline. Data will flow
318 through the elements. This is the basic concept of media handling in
319 GStreamer.
320
321 ![Visualisation of three linked elements](images/linked-elements.png
322 "fig:")
323
324 By linking these three elements, we have created a very simple chain of
325 elements. The effect of this will be that the output of the source
326 element (“element1”) will be used as input for the filter-like element
327 (“element2”). The filter-like element will do something with the data
328 and send the result to the final sink element (“element3”).
329
330 Imagine the above graph as a simple Ogg/Vorbis audio decoder. The source
331 is a disk source which reads the file from disc. The second element is a
332 Ogg/Vorbis audio decoder. The sink element is your soundcard, playing
333 back the decoded audio data. We will use this simple graph to construct
334 an Ogg/Vorbis player later in this manual.
335
336 In code, the above graph is written like this:
337
338 ``` c
339 #include <gst/gst.h>
340
341 int
342 main (int   argc,
343       char *argv[])
344 {
345   GstElement *pipeline;
346   GstElement *source, *filter, *sink;
347
348   /* init */
349   gst_init (&argc, &argv);
350
351   /* create pipeline */
352   pipeline = gst_pipeline_new ("my-pipeline");
353
354   /* create elements */
355   source = gst_element_factory_make ("fakesrc", "source");
356   filter = gst_element_factory_make ("identity", "filter");
357   sink = gst_element_factory_make ("fakesink", "sink");
358
359   /* must add elements to pipeline before linking them */
360   gst_bin_add_many (GST_BIN (pipeline), source, filter, sink, NULL);
361
362   /* link */
363   if (!gst_element_link_many (source, filter, sink, NULL)) {
364     g_warning ("Failed to link elements!");
365   }
366
367 [..]
368
369 }
370     
371 ```
372
373 For more specific behaviour, there are also the functions
374 `gst_element_link ()` and `gst_element_link_pads ()`. You can also
375 obtain references to individual pads and link those using various
376 `gst_pad_link_* ()` functions. See the API references for more details.
377
378 Important: you must add elements to a bin or pipeline *before* linking
379 them, since adding an element to a bin will disconnect any already
380 existing links. Also, you cannot directly link elements that are not in
381 the same bin or pipeline; if you want to link elements or pads at
382 different hierarchy levels, you will need to use ghost pads (more about
383 ghost pads later, see [Ghost pads](manual-pads.md#ghost-pads)).
384
385 ## Element States
386
387 After being created, an element will not actually perform any actions
388 yet. You need to change elements state to make it do something.
389 GStreamer knows four element states, each with a very specific meaning.
390 Those four states are:
391
392   - `GST_STATE_NULL`: this is the default state. No resources are
393     allocated in this state, so, transitioning to it will free all
394     resources. The element must be in this state when its refcount
395     reaches 0 and it is freed.
396
397   - `GST_STATE_READY`: in the ready state, an element has allocated all
398     of its global resources, that is, resources that can be kept within
399     streams. You can think about opening devices, allocating buffers and
400     so on. However, the stream is not opened in this state, so the
401     stream positions is automatically zero. If a stream was previously
402     opened, it should be closed in this state, and position, properties
403     and such should be reset.
404
405   - `GST_STATE_PAUSED`: in this state, an element has opened the stream,
406     but is not actively processing it. An element is allowed to modify a
407     stream's position, read and process data and such to prepare for
408     playback as soon as state is changed to PLAYING, but it is *not*
409     allowed to play the data which would make the clock run. In summary,
410     PAUSED is the same as PLAYING but without a running clock.
411     
412     Elements going into the PAUSED state should prepare themselves for
413     moving over to the PLAYING state as soon as possible. Video or audio
414     outputs would, for example, wait for data to arrive and queue it so
415     they can play it right after the state change. Also, video sinks can
416     already play the first frame (since this does not affect the clock
417     yet). Autopluggers could use this same state transition to already
418     plug together a pipeline. Most other elements, such as codecs or
419     filters, do not need to explicitly do anything in this state,
420     however.
421
422   - `GST_STATE_PLAYING`: in the PLAYING state, an element does exactly
423     the same as in the PAUSED state, except that the clock now runs.
424
425 You can change the state of an element using the function
426 `gst_element_set_state ()`. If you set an element to another state,
427 GStreamer will internally traverse all intermediate states. So if you
428 set an element from NULL to PLAYING, GStreamer will internally set the
429 element to READY and PAUSED in between.
430
431 When moved to `GST_STATE_PLAYING`, pipelines will process data
432 automatically. They do not need to be iterated in any form. Internally,
433 GStreamer will start threads that take this task on to them. GStreamer
434 will also take care of switching messages from the pipeline's thread
435 into the application's own thread, by using a
436 [`GstBus`](http://gstreamer.freedesktop.org/data/doc/gstreamer/stable/gstreamer/html/GstBus.html).
437 See [Bus](manual-bus.md) for details.
438
439 When you set a bin or pipeline to a certain target state, it will
440 usually propagate the state change to all elements within the bin or
441 pipeline automatically, so it's usually only necessary to set the state
442 of the top-level pipeline to start up the pipeline or shut it down.
443 However, when adding elements dynamically to an already-running
444 pipeline, e.g. from within a "pad-added" signal callback, you need to
445 set it to the desired target state yourself using `gst_element_set_state
446 ()` or `gst_element_sync_state_with_parent ()`.
447
448 1.  The code for this example is automatically extracted from the
449     documentation and built under `tests/examples/manual` in the
450     GStreamer tarball.
451