1 <chapter id="chapter-components">
2 <title>Components</title>
5 &GStreamer; includes several higher-level components to simplify your
6 applications life. All of the components discussed here (for now) are
7 targetted at media playback. The idea of each of these components is
8 to integrate as closely as possible with a &GStreamer; pipeline, but
9 to hide the complexity of media type detection and several other
10 rather complex topics that have been discussed in <xref
11 linkend="part-advanced"/>.
15 We currently recommend people to use either playbin (see <xref
16 linkend="section-components-playbin"/>) or decodebin (see <xref
17 linkend="section-components-decodebin"/>), depending on their needs. The
18 other components discussed here are either outdated or deprecated. The
19 documentation is provided for legacy purposes. Use of those other
20 components is not recommended.
23 <sect1 id="section-components-playbin">
24 <title>Playbin</title>
27 Playbin is an element that can be created using the standard &GStreamer;
28 API (e.g. <function>gst_element_factory_make ()</function>). The factory
29 is conveniently called <quote>playbin</quote>. By being a
30 <classname>GstElement</classname>, playbin automatically supports all
31 of the features of this class, including error handling, tag support,
32 state handling, getting stream positions, seeking, and so on.
36 Setting up a playbin pipeline is as simple as creating an instance of
37 the playbin element, setting a file location (this has to be a valid
38 URI, so <quote><protocol>://<location></quote>, e.g.
39 file:///tmp/my.ogg or http://www.example.org/stream.ogg) using the
40 <quote>uri</quote> property on playbin, and then setting the element
41 to the <classname>GST_STATE_PLAYING</classname> state. Internally,
42 playbin uses threads, so there's no need to iterate the element or
43 anything. However, one thing to keep in mind is that signals fired
44 by playbin might come from another than the main thread, so be sure
45 to keep this in mind in your signal handles. Most application
46 programmers will want to use a function such as <function>g_idle_add
47 ()</function> to make sure that the signal is handled in the main
52 #include <gst/gst.h>
55 cb_eos (GstElement *play,
62 cb_error (GstElement *play,
68 g_print ("Error: %s\n", err->message);
78 gst_init (&argc, &argv);
80 /* make sure we have a URI */
82 g_print ("Usage: %s <URI>\n", argv[0]);
87 play = gst_element_factory_make ("playbin", "play);
88 g_object_set (G_OBJECT (play), "uri", argv[1], NULL);
89 g_signal_connect (play, "eos", G_CALLBACK (cb_eos), NULL);
90 g_signal_connect (play, "error", G_CALLBACK (cb_error), NULL);
91 if (gst_element_set_state (play, GST_STATE_PLAYING) != GST_STATE_SUCCESS) {
92 g_print ("Failed to play\n");
100 gst_element_set_state (play, GST_STATE_NULL);
101 gst_object_unref (GST_OBJECT (play));
108 Playbin has several features that have been discussed previously:
113 Settable video and audio output (using the <quote>video-sink</quote>
114 and <quote>audio-sink</quote> properties).
119 Mostly controllable and trackable as a
120 <classname>GstElement</classname>, including error handling, eos
121 handling, tag handling, state handling, media position handling and
127 Buffers network-sources.
132 Supports visualizations for audio-only media.
138 <sect1 id="section-components-decodebin">
139 <title>Decodebin</title>
142 Decodebin is the actual autoplugger backend of playbin, which was
143 discussed in the previous section. Decodebin will, in short, accept
144 input from a source that is linked to its sinkpad and will try to
145 detect the media type contained in the stream, and set up decoder
146 routines for each of those. It will automatically select decoders.
147 For each decoded stream, it will emit the <quote>new-decoded-pad</quote>
148 signal, to let the client know about the newly found decoded stream.
149 For unknown streams (which might be the whole stream), it will emit
150 the <quote>unknown-type</quote> signal. The application is then
151 responsible for reporting the error to the user.
155 The example code below will play back an audio stream of an input
156 file. For readability, it does not include any error handling of
161 #include <gst/gst.h>
163 GstElement *pipeline, *audio;
167 cb_newpad (GstElement *decodebin,
175 /* only link audio; only link once */
176 if (GST_PAD_IS_LINKED (audiopad))
178 caps = gst_pad_get_caps (pad);
179 str = gst_caps_get_structure (caps, 0);
180 if (!strstr (gst_structure_get_name (str), "audio"))
184 gst_pad_link (pad, audiopad);
185 gst_bin_add (GST_BIN (pipeline), audio);
186 gst_bin_sync_children_state (GST_BIN (pipeline));
193 GstElement *src, *dec, *conv, *scale, *sink;
196 gst_init (&argc, &argv);
198 /* make sure we have input */
200 g_print ("Usage: %s <filename>\n", argv[0]);
205 pipeline = gst_pipeline_new ("pipeline");
206 src = gst_element_factory_make ("filesrc", "source");
207 g_object_set (G_OBJECT (filesrc), "location", argv[1], NULL);
208 dec = gst_element_factory_make ("decodebin", "decoder");
209 g_signal_connect (dec, "new-decoded-pad", G_CALLBACK (cb_newpad), NULL);
210 audio = gst_bin_new ("audiobin");
211 conv = gst_element_factory_make ("audioconvert", "aconv");
212 audiopad = gst_element_get_pad (conv, "sink");
213 scale = gst_element_factory_make ("audioscale", "scale");
214 sink = gst_element_factory_make ("alsasink", "sink");
215 gst_bin_add_many (GST_BIN (audio), conv, scale, sink, NULL);
216 gst_element_link_many (conv, scale, sink);
217 gst_bin_add_many (GST_BIN (pipeline), src, dec, NULL);
218 gst_element_link (src, dec);
221 gst_element_set_state (audio, GST_STATE_PAUSED);
222 gst_element_set_state (pipeline, GST_STATE_PLAYING);
223 while (gst_bin_iterate (GST_BIN (pipeline))) ;
226 gst_element_set_state (pipeline, GST_STATE_NULL);
227 gst_object_unref (GST_OBJECT (pipeline));
234 Although decodebin is a good autoplugger, there's a whole lot of
235 things that it does not do and is not intended to do:
240 Taking care of input streams with a known media type (e.g. a DVD,
241 an audio-CD or such).
246 Selection of streams (e.g. which audio track to play in case of
247 multi-language media streams).
252 Overlaying subtitles over a decoded video stream.
258 <sect1 id="section-components-spider">
259 <title>Spider</title>
266 <sect1 id="section-components-gst-play">
267 <title>GstPlay</title>
269 GstPlay is a GtkWidget with a simple API to play, pause and stop a media file.
274 <sect1 id="section-components-gst-editor">
275 <title>GstEditor</title>
277 GstEditor is a set of widgets to display a graphical representation of a