highlevel-components.xml 11.6 KB
Newer Older
1 2 3 4
<chapter id="chapter-components">
  <title>Components</title>

  <para>
5 6
    &GStreamer; includes several higher-level components to simplify an
    application developer's life. All of the components discussed here (for now) are
7 8 9 10 11
    targetted at media playback. The idea of each of these components is
    to integrate as closely as possible with a &GStreamer; pipeline, but
    to hide the complexity of media type detection and several other
    rather complex topics that have been discussed in <xref
    linkend="part-advanced"/>.
12 13
  </para>

14 15 16
  <para>
    We currently recommend people to use either playbin (see <xref
    linkend="section-components-playbin"/>) or decodebin (see <xref
17 18 19
    linkend="section-components-decodebin"/>), depending on their needs.
    Playbin is the recommended solution for everything related to simple
    playback of media that should just work. Decodebin is a more flexible
20
    autoplugger that could be used to add more advanced features, such
21 22
    as playlist support, crossfading of audio tracks and so on. Its
    programming interface is more low-level than that of playbin, though.
23 24
  </para>

25 26 27 28 29 30 31
  <sect1 id="section-components-playbin">
    <title>Playbin</title>

    <para>
      Playbin is an element that can be created using the standard &GStreamer;
      API (e.g. <function>gst_element_factory_make ()</function>). The factory
      is conveniently called <quote>playbin</quote>. By being a
32 33
      <classname>GstPipeline</classname> (and thus a
      <classname>GstElement</classname>), playbin automatically supports all
34 35 36 37 38 39
      of the features of this class, including error handling, tag support,
      state handling, getting stream positions, seeking, and so on.
    </para>

    <para>
      Setting up a playbin pipeline is as simple as creating an instance of
40
      the playbin element, setting a file location using the
41
      <quote>uri</quote> property on playbin, and then setting the element
42 43 44
      to the <classname>GST_STATE_PLAYING</classname> state (the location has to be a valid
      URI, so <quote>&lt;protocol&gt;://&lt;location&gt;</quote>, e.g.
      file:///tmp/my.ogg or http://www.example.org/stream.ogg). Internally,
45
      playbin will set up a pipeline to playback the media location.
46 47
    </para>

48
    <programlisting><!-- example-begin playbin.c a -->
49
#include &lt;gst/gst.h&gt;
50 51 52 53 54 55
<!-- example-end playbin.c a -->
[.. my_bus_callback goes here ..]<!-- example-begin playbin.c b --><!--
static gboolean
my_bus_callback (GstBus     *bus,
		 GstMessage *message,
		 gpointer    data)
56
{
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
  GMainLoop *loop = data;

  switch (GST_MESSAGE_TYPE (message)) {
    case GST_MESSAGE_ERROR: {
      GError *err;
      gchar *debug;

      gst_message_parse_error (message, &amp;err, &amp;debug);
      g_print ("Error: %s\n", err-&gt;message);
      g_error_free (err);
      g_free (debug);

      g_main_loop_quit (loop);
      break;
    }
    case GST_MESSAGE_EOS:
      /* end-of-stream */
      g_main_loop_quit (loop);
      break;
    default:
      /* unhandled message */
      break;
  }
80

81 82
  /* remove message from the queue */
  return TRUE;
83
}
84 85
--><!-- example-end playbin.c b -->
<!-- example-begin playbin.c c -->
86 87 88 89
gint
main (gint   argc,
      gchar *argv[])
{
90
  GMainLoop *loop;
91
  GstElement *play;
92
  GstBus *bus;
93 94 95

  /* init GStreamer */
  gst_init (&amp;argc, &amp;argv);
96
  loop = g_main_loop_new (NULL, FALSE);
97 98 99 100 101 102 103 104 105 106

  /* make sure we have a URI */
  if (argc != 2) {
    g_print ("Usage: %s &lt;URI&gt;\n", argv[0]);
    return -1;
  }

  /* set up */
  play = gst_element_factory_make ("playbin", "play");
  g_object_set (G_OBJECT (play), "uri", argv[1], NULL);
107 108 109 110 111

  bus = gst_pipeline_get_bus (GST_PIPELINE (play));
  gst_bus_add_watch (bus, my_bus_callback, loop);
  gst_object_unref (bus);

112
  gst_element_set_state (play, GST_STATE_PLAYING);
113 114

  /* now run */
115
  g_main_loop_run (loop);
116 117 118 119 120 121 122

  /* also clean up */
  gst_element_set_state (play, GST_STATE_NULL);
  gst_object_unref (GST_OBJECT (play));

  return 0;
}
123
    <!-- example-end playbin.c c --></programlisting>
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

    <para>
      Playbin has several features that have been discussed previously:
    </para>
    <itemizedlist>
      <listitem>
        <para>
          Settable video and audio output (using the <quote>video-sink</quote>
          and <quote>audio-sink</quote> properties).
        </para>
      </listitem>
      <listitem>
        <para>
          Mostly controllable and trackable as a
          <classname>GstElement</classname>, including error handling, eos
139 140
          handling, tag handling, state handling (through the
          <classname>GstBus</classname>), media position handling and
141 142 143 144 145
          seeking.
        </para>
      </listitem>
      <listitem>
        <para>
146 147
          Buffers network-sources, with buffer fullness notifications being
          passed through the <classname>GstBus</classname>.
148 149 150 151 152 153 154
        </para>
      </listitem>
      <listitem>
        <para>
          Supports visualizations for audio-only media.
        </para>
      </listitem>
155 156 157
      <listitem>
        <para>
          Supports subtitles, both in the media as well as from separate
158 159
          files. For separate subtitle files, use the <quote>suburi</quote>
          property.
160 161 162 163 164 165
        </para>
      </listitem>
      <listitem>
        <para>
          Supports stream selection and disabling. If your media has
          multiple audio or subtitle tracks, you can dynamically choose
Piotr Fusik's avatar
Piotr Fusik committed
166
          which one to play back, or decide to turn it off altogether
167 168 169
          (which is especially useful to turn off subtitles). For each
          of those, use the <quote>current-text</quote> and other related
          properties.
170 171
        </para>
      </listitem>
172
    </itemizedlist>
173 174
    <para>
      For convenience, it is possible to test <quote>playbin</quote> on
175
      the commandline, using the command <quote>gst-launch-0.10 playbin
176 177
      uri=file:///path/to/file</quote>.
    </para>
178 179 180
    <para>
      New applications should use playbin2 instead of the old playbin.
    </para>
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
  </sect1>

  <sect1 id="section-components-decodebin">
    <title>Decodebin</title>

    <para>
      Decodebin is the actual autoplugger backend of playbin, which was
      discussed in the previous section. Decodebin will, in short, accept
      input from a source that is linked to its sinkpad and will try to
      detect the media type contained in the stream, and set up decoder
      routines for each of those. It will automatically select decoders.
      For each decoded stream, it will emit the <quote>new-decoded-pad</quote>
      signal, to let the client know about the newly found decoded stream.
      For unknown streams (which might be the whole stream), it will emit
      the <quote>unknown-type</quote> signal. The application is then
      responsible for reporting the error to the user.
    </para>
198
    <programlisting><!-- example-begin decodebin.c a -->
199
#include &lt;gst/gst.h&gt;
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
<!-- example-end decodebin.c a -->
[.. my_bus_callback goes here ..]<!-- example-begin decodebin.c b --><!--
static gboolean
my_bus_callback (GstBus     *bus,
		 GstMessage *message,
		 gpointer    data)
{
  GMainLoop *loop = data;

  switch (GST_MESSAGE_TYPE (message)) {
    case GST_MESSAGE_ERROR: {
      GError *err;
      gchar *debug;

      gst_message_parse_error (message, &amp;err, &amp;debug);
      g_print ("Error: %s\n", err-&gt;message);
      g_error_free (err);
      g_free (debug);

      g_main_loop_quit (loop);
      break;
    }
    case GST_MESSAGE_EOS:
      /* end-of-stream */
      g_main_loop_quit (loop);
      break;
    default:
      /* unhandled message */
      break;
  }
230

231 232 233 234 235
  /* remove message from the queue */
  return TRUE;
}
--><!-- example-end decodebin.c b -->
<!-- example-begin decodebin.c c -->
236 237 238 239 240 241 242 243 244 245
GstElement *pipeline, *audio;

static void
cb_newpad (GstElement *decodebin,
	   GstPad     *pad,
	   gboolean    last,
	   gpointer    data)
{
  GstCaps *caps;
  GstStructure *str;
246
  GstPad *audiopad;
247

248
  /* only link once */
249
  audiopad = gst_element_get_static_pad (audio, "sink");
250 251
  if (GST_PAD_IS_LINKED (audiopad)) {
    g_object_unref (audiopad);
252
    return;
253 254 255
  }

  /* check media type */
256 257
  caps = gst_pad_get_caps (pad);
  str = gst_caps_get_structure (caps, 0);
258 259 260
  if (!g_strrstr (gst_structure_get_name (str), "audio")) {
    gst_caps_unref (caps);
    gst_object_unref (audiopad);
261
    return;
262 263
  }
  gst_caps_unref (caps);
264 265 266

  /* link'n'play */
  gst_pad_link (pad, audiopad);
267 268

  g_object_unref (audiopad);
269 270 271 272 273 274
}

gint
main (gint   argc,
      gchar *argv[])
{
275 276 277
  GMainLoop *loop;
  GstElement *src, *dec, *conv, *sink;
  GstPad *audiopad;
278
  GstBus *bus;
279 280 281

  /* init GStreamer */
  gst_init (&amp;argc, &amp;argv);
282
  loop = g_main_loop_new (NULL, FALSE);
283 284 285 286 287 288 289 290 291

  /* make sure we have input */
  if (argc != 2) {
    g_print ("Usage: %s &lt;filename&gt;\n", argv[0]);
    return -1;
  }

  /* setup */
  pipeline = gst_pipeline_new ("pipeline");
292 293 294 295 296

  bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
  gst_bus_add_watch (bus, my_bus_callback, loop);
  gst_object_unref (bus);

297 298 299 300
  src = gst_element_factory_make ("filesrc", "source");
  g_object_set (G_OBJECT (src), "location", argv[1], NULL);
  dec = gst_element_factory_make ("decodebin", "decoder");
  g_signal_connect (dec, "new-decoded-pad", G_CALLBACK (cb_newpad), NULL);
301 302 303 304
  gst_bin_add_many (GST_BIN (pipeline), src, dec, NULL);
  gst_element_link (src, dec);

  /* create audio output */
305 306
  audio = gst_bin_new ("audiobin");
  conv = gst_element_factory_make ("audioconvert", "aconv");
307
  audiopad = gst_element_get_static_pad (conv, "sink");
308
  sink = gst_element_factory_make ("alsasink", "sink");
309 310 311 312 313 314
  gst_bin_add_many (GST_BIN (audio), conv, sink, NULL);
  gst_element_link (conv, sink);
  gst_element_add_pad (audio,
      gst_ghost_pad_new ("sink", audiopad));
  gst_object_unref (audiopad);
  gst_bin_add (GST_BIN (pipeline), audio);
315 316 317

  /* run */
  gst_element_set_state (pipeline, GST_STATE_PLAYING);
318
  g_main_loop_run (loop);
319 320 321 322 323 324 325

  /* cleanup */
  gst_element_set_state (pipeline, GST_STATE_NULL);
  gst_object_unref (GST_OBJECT (pipeline));

  return 0;
}
326
    <!-- example-end decodebin.c c --></programlisting>
327

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
    <para>
      Decodebin, similar to playbin, supports the following features:
    </para>
    <itemizedlist>
      <listitem>
        <para>
          Can decode an unlimited number of contained streams to decoded
          output pads.
        </para>
      </listitem>
      <listitem>
        <para>
          Is handled as a <classname>GstElement</classname> in all ways,
          including tag or error forwarding and state handling.
        </para>
      </listitem>
    </itemizedlist>
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
    <para>
      Although decodebin is a good autoplugger, there's a whole lot of
      things that it does not do and is not intended to do:
    </para>
    <itemizedlist>
      <listitem>
        <para>
          Taking care of input streams with a known media type (e.g. a DVD,
          an audio-CD or such).
        </para>
      </listitem>
      <listitem>
        <para>
          Selection of streams (e.g. which audio track to play in case of
          multi-language media streams).
        </para>
      </listitem>
      <listitem>
        <para>
          Overlaying subtitles over a decoded video stream.
        </para>
      </listitem>
    </itemizedlist>
368 369
    <para>
      Decodebin can be easily tested on the commandline, e.g. by using the
370 371 372 373 374 375 376 377 378 379
      command <command>gst-launch-0.10 filesrc location=file.ogg ! decodebin
      ! audioconvert ! audioresample ! autoaudiosink</command>.
    </para>
    <para>
      New applications should use decodebin2 instead of the old decodebin.
    </para>
    <para>
      The uridecodebin element is very similar to decodebin2, only that it
      automatically plugs a source plugin based on the protocol of the URI
      given.
380
    </para>
381 382
  </sect1>

383
</chapter>