Chapter 19. Components

GStreamer includes several higher-level components to simplify your applications 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 in GStreamer Application Development Manual (0.8.12).

We currently recommend people to use either playbin (see Section 19.1) or decodebin (see Section 19.2), depending on their needs. The other components discussed here are either outdated or deprecated. The documentation is provided for legacy purposes. Use of those other components is not recommended.

19.1. 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 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 (this has to be a valid URI, so "<protocol>://<location>", e.g. file:///tmp/my.ogg or http://www.example.org/stream.ogg) using the "uri" property on playbin, and then setting the element to the GST_STATE_PLAYING state. Internally, playbin uses threads, so there's no need to iterate the element or anything. However, one thing to keep in mind is that signals fired by playbin might come from another than the main thread, so be sure to keep this in mind in your signal handles. Most application programmers will want to use a function such as g_idle_add () to make sure that the signal is handled in the main thread.


#include <gst/gst.h>

static void
cb_eos (GstElement *play,
	gpointer    data)
{
  gst_main_quit ();
}

static void
cb_error (GstElement *play,
	  GstElement *src,
	  GError     *err,
	  gchar      *debug,
	  gpointer    data)
{
  g_print ("Error: %s\n", err->message);
}

gint
main (gint   argc,
      gchar *argv[])
{
  GstElement *play;

  /* init GStreamer */
  gst_init (&argc, &argv);

  /* 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);
  g_signal_connect (play, "eos", G_CALLBACK (cb_eos), NULL);
  g_signal_connect (play, "error", G_CALLBACK (cb_error), NULL);
  if (gst_element_set_state (play, GST_STATE_PLAYING) != GST_STATE_SUCCESS) {
    g_print ("Failed to play\n");
    return -1;
  }

  /* now run */
  gst_main ();

  /* 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: