[21447] | 1 | <chapter id="chapter-components"> |
---|
| 2 | <title>Components</title> |
---|
| 3 | |
---|
| 4 | <para> |
---|
| 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"/>. |
---|
| 12 | </para> |
---|
| 13 | |
---|
| 14 | <para> |
---|
| 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. |
---|
| 21 | </para> |
---|
| 22 | |
---|
| 23 | <sect1 id="section-components-playbin"> |
---|
| 24 | <title>Playbin</title> |
---|
| 25 | |
---|
| 26 | <para> |
---|
| 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. |
---|
| 33 | </para> |
---|
| 34 | |
---|
| 35 | <para> |
---|
| 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 |
---|
| 48 | thread. |
---|
| 49 | </para> |
---|
| 50 | |
---|
| 51 | <programlisting><!-- example-begin playbin.c --> |
---|
| 52 | #include <gst/gst.h> |
---|
| 53 | |
---|
| 54 | static void |
---|
| 55 | cb_eos (GstElement *play, |
---|
| 56 | gpointer data) |
---|
| 57 | { |
---|
| 58 | gst_main_quit (); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | static void |
---|
| 62 | cb_error (GstElement *play, |
---|
| 63 | GstElement *src, |
---|
| 64 | GError *err, |
---|
| 65 | gchar *debug, |
---|
| 66 | gpointer data) |
---|
| 67 | { |
---|
| 68 | g_print ("Error: %s\n", err->message); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | gint |
---|
| 72 | main (gint argc, |
---|
| 73 | gchar *argv[]) |
---|
| 74 | { |
---|
| 75 | GstElement *play; |
---|
| 76 | |
---|
| 77 | /* init GStreamer */ |
---|
| 78 | gst_init (&argc, &argv); |
---|
| 79 | |
---|
| 80 | /* make sure we have a URI */ |
---|
| 81 | if (argc != 2) { |
---|
| 82 | g_print ("Usage: %s <URI>\n", argv[0]); |
---|
| 83 | return -1; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | /* set up */ |
---|
| 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"); |
---|
| 93 | return -1; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | /* now run */ |
---|
| 97 | gst_main (); |
---|
| 98 | |
---|
| 99 | /* also clean up */ |
---|
| 100 | gst_element_set_state (play, GST_STATE_NULL); |
---|
| 101 | gst_object_unref (GST_OBJECT (play)); |
---|
| 102 | |
---|
| 103 | return 0; |
---|
| 104 | } |
---|
| 105 | <!-- example-end playbin.c --></programlisting> |
---|
| 106 | |
---|
| 107 | <para> |
---|
| 108 | Playbin has several features that have been discussed previously: |
---|
| 109 | </para> |
---|
| 110 | <itemizedlist> |
---|
| 111 | <listitem> |
---|
| 112 | <para> |
---|
| 113 | Settable video and audio output (using the <quote>video-sink</quote> |
---|
| 114 | and <quote>audio-sink</quote> properties). |
---|
| 115 | </para> |
---|
| 116 | </listitem> |
---|
| 117 | <listitem> |
---|
| 118 | <para> |
---|
| 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 |
---|
| 122 | seeking. |
---|
| 123 | </para> |
---|
| 124 | </listitem> |
---|
| 125 | <listitem> |
---|
| 126 | <para> |
---|
| 127 | Buffers network-sources. |
---|
| 128 | </para> |
---|
| 129 | </listitem> |
---|
| 130 | <listitem> |
---|
| 131 | <para> |
---|
| 132 | Supports visualizations for audio-only media. |
---|
| 133 | </para> |
---|
| 134 | </listitem> |
---|
| 135 | <listitem> |
---|
| 136 | <para> |
---|
| 137 | Supports subtitles, both in the media as well as from separate |
---|
| 138 | files. |
---|
| 139 | </para> |
---|
| 140 | </listitem> |
---|
| 141 | <listitem> |
---|
| 142 | <para> |
---|
| 143 | Supports stream selection and disabling. If your media has |
---|
| 144 | multiple audio or subtitle tracks, you can dynamically choose |
---|
| 145 | which one to play back, or decide to turn it off alltogther |
---|
| 146 | (which is especially useful to turn off subtitles). |
---|
| 147 | </para> |
---|
| 148 | </listitem> |
---|
| 149 | </itemizedlist> |
---|
| 150 | </sect1> |
---|
| 151 | |
---|
| 152 | <sect1 id="section-components-decodebin"> |
---|
| 153 | <title>Decodebin</title> |
---|
| 154 | |
---|
| 155 | <para> |
---|
| 156 | Decodebin is the actual autoplugger backend of playbin, which was |
---|
| 157 | discussed in the previous section. Decodebin will, in short, accept |
---|
| 158 | input from a source that is linked to its sinkpad and will try to |
---|
| 159 | detect the media type contained in the stream, and set up decoder |
---|
| 160 | routines for each of those. It will automatically select decoders. |
---|
| 161 | For each decoded stream, it will emit the <quote>new-decoded-pad</quote> |
---|
| 162 | signal, to let the client know about the newly found decoded stream. |
---|
| 163 | For unknown streams (which might be the whole stream), it will emit |
---|
| 164 | the <quote>unknown-type</quote> signal. The application is then |
---|
| 165 | responsible for reporting the error to the user. |
---|
| 166 | </para> |
---|
| 167 | |
---|
| 168 | <para> |
---|
| 169 | The example code below will play back an audio stream of an input |
---|
| 170 | file. For readability, it does not include any error handling of |
---|
| 171 | any sort. |
---|
| 172 | </para> |
---|
| 173 | |
---|
| 174 | <programlisting><!-- example-begin decodebin.c --> |
---|
| 175 | #include <gst/gst.h> |
---|
| 176 | |
---|
| 177 | GstElement *pipeline, *audio; |
---|
| 178 | GstPad *audiopad; |
---|
| 179 | |
---|
| 180 | static void |
---|
| 181 | cb_newpad (GstElement *decodebin, |
---|
| 182 | GstPad *pad, |
---|
| 183 | gboolean last, |
---|
| 184 | gpointer data) |
---|
| 185 | { |
---|
| 186 | GstCaps *caps; |
---|
| 187 | GstStructure *str; |
---|
| 188 | |
---|
| 189 | /* only link audio; only link once */ |
---|
| 190 | if (GST_PAD_IS_LINKED (audiopad)) |
---|
| 191 | return; |
---|
| 192 | caps = gst_pad_get_caps (pad); |
---|
| 193 | str = gst_caps_get_structure (caps, 0); |
---|
| 194 | if (!g_strrstr (gst_structure_get_name (str), "audio")) |
---|
| 195 | return; |
---|
| 196 | |
---|
| 197 | /* link'n'play */ |
---|
| 198 | gst_pad_link (pad, audiopad); |
---|
| 199 | gst_bin_add (GST_BIN (pipeline), audio); |
---|
| 200 | gst_bin_sync_children_state (GST_BIN (pipeline)); |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | gint |
---|
| 204 | main (gint argc, |
---|
| 205 | gchar *argv[]) |
---|
| 206 | { |
---|
| 207 | GstElement *src, *dec, *conv, *scale, *sink; |
---|
| 208 | |
---|
| 209 | /* init GStreamer */ |
---|
| 210 | gst_init (&argc, &argv); |
---|
| 211 | |
---|
| 212 | /* make sure we have input */ |
---|
| 213 | if (argc != 2) { |
---|
| 214 | g_print ("Usage: %s <filename>\n", argv[0]); |
---|
| 215 | return -1; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | /* setup */ |
---|
| 219 | pipeline = gst_pipeline_new ("pipeline"); |
---|
| 220 | src = gst_element_factory_make ("filesrc", "source"); |
---|
| 221 | g_object_set (G_OBJECT (src), "location", argv[1], NULL); |
---|
| 222 | dec = gst_element_factory_make ("decodebin", "decoder"); |
---|
| 223 | g_signal_connect (dec, "new-decoded-pad", G_CALLBACK (cb_newpad), NULL); |
---|
| 224 | audio = gst_bin_new ("audiobin"); |
---|
| 225 | conv = gst_element_factory_make ("audioconvert", "aconv"); |
---|
| 226 | audiopad = gst_element_get_pad (conv, "sink"); |
---|
| 227 | scale = gst_element_factory_make ("audioscale", "scale"); |
---|
| 228 | sink = gst_element_factory_make ("alsasink", "sink"); |
---|
| 229 | gst_bin_add_many (GST_BIN (audio), conv, scale, sink, NULL); |
---|
| 230 | gst_element_link_many (conv, scale, sink, NULL); |
---|
| 231 | gst_bin_add_many (GST_BIN (pipeline), src, dec, NULL); |
---|
| 232 | gst_element_link (src, dec); |
---|
| 233 | |
---|
| 234 | /* run */ |
---|
| 235 | gst_element_set_state (audio, GST_STATE_PAUSED); |
---|
| 236 | gst_element_set_state (pipeline, GST_STATE_PLAYING); |
---|
| 237 | while (gst_bin_iterate (GST_BIN (pipeline))) ; |
---|
| 238 | |
---|
| 239 | /* cleanup */ |
---|
| 240 | gst_element_set_state (pipeline, GST_STATE_NULL); |
---|
| 241 | gst_object_unref (GST_OBJECT (pipeline)); |
---|
| 242 | |
---|
| 243 | return 0; |
---|
| 244 | } |
---|
| 245 | <!-- example-end decodebin.c --></programlisting> |
---|
| 246 | |
---|
| 247 | <para> |
---|
| 248 | Decodebin, similar to playbin, supports the following features: |
---|
| 249 | </para> |
---|
| 250 | <itemizedlist> |
---|
| 251 | <listitem> |
---|
| 252 | <para> |
---|
| 253 | Can decode an unlimited number of contained streams to decoded |
---|
| 254 | output pads. |
---|
| 255 | </para> |
---|
| 256 | </listitem> |
---|
| 257 | <listitem> |
---|
| 258 | <para> |
---|
| 259 | Is handled as a <classname>GstElement</classname> in all ways, |
---|
| 260 | including tag or error forwarding and state handling. |
---|
| 261 | </para> |
---|
| 262 | </listitem> |
---|
| 263 | </itemizedlist> |
---|
| 264 | <para> |
---|
| 265 | Although decodebin is a good autoplugger, there's a whole lot of |
---|
| 266 | things that it does not do and is not intended to do: |
---|
| 267 | </para> |
---|
| 268 | <itemizedlist> |
---|
| 269 | <listitem> |
---|
| 270 | <para> |
---|
| 271 | Taking care of input streams with a known media type (e.g. a DVD, |
---|
| 272 | an audio-CD or such). |
---|
| 273 | </para> |
---|
| 274 | </listitem> |
---|
| 275 | <listitem> |
---|
| 276 | <para> |
---|
| 277 | Selection of streams (e.g. which audio track to play in case of |
---|
| 278 | multi-language media streams). |
---|
| 279 | </para> |
---|
| 280 | </listitem> |
---|
| 281 | <listitem> |
---|
| 282 | <para> |
---|
| 283 | Overlaying subtitles over a decoded video stream. |
---|
| 284 | </para> |
---|
| 285 | </listitem> |
---|
| 286 | </itemizedlist> |
---|
| 287 | <para> |
---|
| 288 | Decodebin can be easily tested on the commandline, e.g. by using the |
---|
| 289 | command <command>gst-launch-0.8 filesrc location=file.ogg ! decodebin |
---|
| 290 | ! audioconvert ! audioscale ! alsasink</command>. |
---|
| 291 | </para> |
---|
| 292 | </sect1> |
---|
| 293 | |
---|
| 294 | <sect1 id="section-components-spider"> |
---|
| 295 | <title>Spider</title> |
---|
| 296 | |
---|
| 297 | <para> |
---|
| 298 | Spider is an autoplugger that looks and feels very much like decodebin. |
---|
| 299 | On the commandline, you can literally switch between spider and |
---|
| 300 | decodebin and it'll mostly just work. Try, for example, |
---|
| 301 | <command>gst-launch-0.8 filesrc location=file.ogg ! spider ! |
---|
| 302 | audioconvert ! audioscale ! alsasink</command>. Although the two may |
---|
| 303 | seem very much alike from the outside, they are very different from |
---|
| 304 | the inside. Those internal differences are the main reason why spider |
---|
| 305 | is currently considered deprecated (along with the fact that it was |
---|
| 306 | hard to maintain). |
---|
| 307 | </para> |
---|
| 308 | <para> |
---|
| 309 | As opposed to decodebin, spider does not decode pads and emit signals |
---|
| 310 | for each detected stream. Instead, you have to add output sinks to |
---|
| 311 | spider by create source request pads and connecting those to sink |
---|
| 312 | elements. This means that streams decoded by spider cannot be dynamic. |
---|
| 313 | Also, spider uses many loop-based elements internally, which is rather |
---|
| 314 | heavy scheduler-wise. |
---|
| 315 | </para> |
---|
| 316 | <para> |
---|
| 317 | Code for using spider would look almost identical to the code of |
---|
| 318 | decodebin, and is therefore omitted. Also, featureset and limitations |
---|
| 319 | are very much alike, except for the above-mentioned extra limitations |
---|
| 320 | for spider with respect to decodebin. |
---|
| 321 | </para> |
---|
| 322 | </sect1> |
---|
| 323 | |
---|
| 324 | <sect1 id="section-components-gst-play"> |
---|
| 325 | <title>GstPlay</title> |
---|
| 326 | <para> |
---|
| 327 | GstPlay is a GtkWidget with a simple API to play, pause and stop a media file. |
---|
| 328 | </para> |
---|
| 329 | |
---|
| 330 | </sect1> |
---|
| 331 | |
---|
| 332 | <sect1 id="section-components-gst-editor"> |
---|
| 333 | <title>GstEditor</title> |
---|
| 334 | <para> |
---|
| 335 | GstEditor is a set of widgets to display a graphical representation of a |
---|
| 336 | pipeline. |
---|
| 337 | </para> |
---|
| 338 | </sect1> |
---|
| 339 | |
---|
| 340 | </chapter> |
---|