Chapter 20. Playback Components

Table of Contents

Playbin
Decodebin
URIDecodebin
Playsink

GStreamer includes several higher-level components to simplify an application developer's life. All of the components discussed here (for now) are 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 Part III, “Advanced GStreamer concepts”.

We currently recommend people to use either playbin (see the section called “Playbin”) or decodebin (see the section called “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 autoplugger that could be used to add more advanced features, such as playlist support, crossfading of audio tracks and so on. Its programming interface is more low-level than that of playbin, though.

Playbin

Playbin is an element that can be created using the standard GStreamer API (e.g. gst_element_factory_make ()). The factory is conveniently called playbin. By being a GstPipeline (and thus a GstElement), playbin automatically supports all of the features of this class, including error handling, tag support, state handling, getting stream positions, seeking, and so on.

Setting up a playbin pipeline is as simple as creating an instance of the playbin element, setting a file location using the uri property on playbin, and then setting the element to the GST_STATE_PLAYING state (the location has to be a valid URI, so <protocol>://<location>, e.g. file:///tmp/my.ogg or http://www.example.org/stream.ogg). Internally, playbin will set up a pipeline to playback the media location.

#include <gst/gst.h>

[.. my_bus_callback goes here ..]

gint
main (gint   argc,
      gchar *argv[])
{
  GMainLoop *loop;
  GstElement *play;
  GstBus *bus;

  /* init GStreamer */
  gst_init (&argc, &argv);
  loop = g_main_loop_new (NULL, FALSE);

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

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

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

  gst_element_set_state (play, GST_STATE_PLAYING);

  /* now run */
  g_main_loop_run (loop);

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

  return 0;
}
    

Playbin has several features that have been discussed previously:

  • Settable video and audio output (using the video-sink and audio-sink properties).

  • Mostly controllable and trackable as a GstElement, including error handling, eos handling, tag handling, state handling (through the GstBus), media position handling and seeking.

  • Buffers network-sources, with buffer fullness notifications being passed through the GstBus.

  • Supports visualizations for audio-only media.

  • Supports subtitles, both in the media as well as from separate files. For separate subtitle files, use the suburi property.

  • Supports stream selection and disabling. If your media has multiple audio or subtitle tracks, you can dynamically choose which one to play back, or decide to turn it off altogether (which is especially useful to turn off subtitles). For each of those, use the current-text and other related properties.

For convenience, it is possible to test playbin on the commandline, using the command gst-launch-1.0 playbin uri=file:///path/to/file.