7 The most important object in GStreamer for the application programmer is
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`
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.
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
33 ![Visualisation of a source element](images/src-element.png "fig:")
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.
39 ### Filters, convertors, demuxers, muxers and codecs
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.
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.
52 ![Visualisation of a filter element](images/filter-element.png "fig:")
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
60 ![Visualisation of a filter element with more than one output
61 pad](images/filter-element-multi.png "fig:")
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.
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.
80 ![Visualisation of a sink element](images/sink-element.png "fig:")
82 ## Creating a `GstElement`
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
93 When you don't need the element anymore, you need to unref it using
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.
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.
114 gst_init (&argc, &argv);
117 element = gst_element_factory_make ("fakesrc", "source");
119 g_print ("Failed to create element of type 'fakesrc'\n");
123 gst_object_unref (GST_OBJECT (element));
130 `gst_element_factory_make` is actually a shorthand for a combination of
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
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).
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.
153 GstElementFactory *factory;
154 GstElement * element;
157 gst_init (&argc, &argv);
159 /* create element, method #2 */
160 factory = gst_element_factory_find ("fakesrc");
162 g_print ("Failed to find factory of type 'fakesrc'\n");
165 element = gst_element_factory_create (factory, "source");
167 g_print ("Failed to create element, even though its factory exists!\n");
171 gst_object_unref (GST_OBJECT (element));
178 ## Using an element as a `GObject`
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.
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.
204 gst_init (&argc, &argv);
207 element = gst_element_factory_make ("fakesrc", "source");
210 g_object_get (G_OBJECT (element), "name", &name, NULL);
211 g_print ("The name of the element is '%s'.\n", name);
214 gst_object_unref (GST_OBJECT (element));
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`.
229 For more information about `GObject` properties we recommend you read
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).
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.
242 ## More about element factories
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.
253 ### Getting information about an element using a factory
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,
274 GstElementFactory *factory;
277 gst_init (&argc, &argv);
280 factory = gst_element_factory_find ("fakesrc");
282 g_print ("You don't have the 'fakesrc' element installed!\n");
286 /* display information */
287 g_print ("The '%s' element is a member of the category %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));
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.
301 ### Finding out what pads an element can contain
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)
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
321 ![Visualisation of three linked elements](images/linked-elements.png
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”).
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.
336 In code, the above graph is written like this:
345 GstElement *pipeline;
346 GstElement *source, *filter, *sink;
349 gst_init (&argc, &argv);
351 /* create pipeline */
352 pipeline = gst_pipeline_new ("my-pipeline");
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");
359 /* must add elements to pipeline before linking them */
360 gst_bin_add_many (GST_BIN (pipeline), source, filter, sink, NULL);
363 if (!gst_element_link_many (source, filter, sink, NULL)) {
364 g_warning ("Failed to link elements!");
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.
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)).
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:
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.
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.
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.
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,
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.
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.
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.
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 ()`.
448 1. The code for this example is automatically extracted from the
449 documentation and built under `tests/examples/manual` in the