Wednesday, October 27, 2010

Looping playback with GStreamer

Recently on the GStreamer mailing list, someone asked how to loop playback using the playbin element. Since this seemed pretty straightforward, I thought I'd post it here. To clarify, playbin is a higher-level element that greatly simplifies typical playback scenarios. The code posted here is derived from this playbin example. The important addition is the bus callback, which simply listens for an end of stream (EOS) message, and upon receiving it, seeks to the beginning of the stream. This restarts playback.

#include <gst/gst.h>

gboolean bus_callback(GstBus *bus, GstMessage *msg, gpointer data)
    GstElement *play = GST_ELEMENT(data);
    switch (GST_MESSAGE_TYPE(msg))
        case GST_MESSAGE_EOS:
            /* restart playback if at end */
            if (!gst_element_seek(play, 
                        1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
                        GST_SEEK_TYPE_SET, 0,
                        GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE)) {
                g_print("Seek failed!\n");
    return TRUE;

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, bus_callback, play);
  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;

Thursday, October 7, 2010

New Hey Predator! EP (apologies to the technical readers)

Since I do have the word music in the subtitle of this blog, I thought I should share some of ours for a change. My band Hey Predator! just finished a 5 song EP.

Friday, May 21, 2010

Building the GStreamer VP8 plugins

By now it's old news that Google has launched the WebM project. Despite its shortcomings, this is very exciting as at the very least, we are that much closer to a high quality open and free video format for the web...well, maybe.
In any case, I just wanted to quickly document the steps required to use the new VP8 plugins in GStreamer (also thanks to David Schleef for his feedback). VP8's build system leaves a bit to be desired so bear with me:

-Get the latest tarball of vp8 here.
-Configure with your target architecture, in my case:
./configure --target=x86-linux-gcc
-Build with the make command.
-Do an install (not as root):
make install
The resulting install directory will be called something like vpx-vp8-nodocs-x86-linux-v0.9.0
It will contain the following:
bin build include lib md5sums.txt src
-Go into this new directory
cd vpx-vp8-nodocs-x86-linux-v0.9.0
-Copy include to /usr/local/include/vpx (notice the renaming):
sudo cp -rf include /usr/local/include/vpx
-Copy the library to /usr/local/lib
sudo cp lib/libvpx.a /usr/local/lib
-Build gst-plugins-bad (git version or patch the latest releases).

Note that I use an uninstalled GStreamer source tree from git, which I highly recommend. Refer to for a brief overview on how to get that going.
Make sure that after running or configure, the vp8 plugins are listed under those which will be built. If all has gone well, you should be able to get a plugin description from gst-inspect vp8.

Now to test with your favourite GStreamer pipeline. Run something like:
gst-launch -v v4l2src ! video/x-raw-yuv, width=320, height=240 \
! ffmpegcolorspace ! vp8enc max-latency=1 ! vp8dec \
! xvimagesink sync=false

(hopefully) Success! My thanks go out the GStreamer developers for yet another great contribution.