Using Gstreamer with Python2.6 in Windows XP – Pt. 2 A Minimal Example

(Original post from 30. Oct. 2011)

Right folks, so first things first. Let’s try and get something easy going. You know, nothing fancy. Just a simple output. No gui nothing. For that, as a starting point, I chose the tutorial from the following website:

http://www.jonobacon.org/2006/08/28/getting-started-with-gstreamer-with-python/

In this tutorial (feel free to browse, it’s good), the author writes a very good introduction to what GStreamer is all about and explains the mechanics (Sinks, Pipelines etc.)… Unfortunately for me, for Linux. He gives a script that plays an audible tone, just to test if everything is working fine. Of course, taking it over to Windows is an issue.
Here is the code as quoted on JonoBacon’s website (many thanks for it):

#!/usr/bin/python

import pygst
pygst.require("0.10")
import gst
import pygtk
import gtk

class Main:
    def __init__(self):
        self.pipeline = gst.Pipeline("mypipeline")

        self.audiotestsrc = gst.element_factory_make("audiotestsrc", "audio")
        self.pipeline.add(self.audiotestsrc)

        self.sink = gst.element_factory_make("alsasink", "sink")
        self.pipeline.add(self.sink)

        self.audiotestsrc.link(self.sink)

        self.pipeline.set_state(gst.STATE_PLAYING)

start=Main()
gtk.main()

If you try the same code as stated on the website, you will receive a

gst.ElementNotFoundError: alsasink

in windows. After some browsing the web and learning about the combination of cygwin and the gst-inspect and gst-launch commands (google it, if you want to know more), I finally found the answer. First though, here are the website, I had that were useful to me in this instance:

http://www.jonobacon.org/2006/08/28/getting-started-with-gstreamer-with-python/

http://gstreamer.freedesktop.org/data/doc/gstreamer/head/faq/html/chapter-using.html#using-sound

The problem lies with using “alsasink”. ALSA is Linux’ way of playing sound. The equivalent of DirectSound for Windows if you want. Therefore, you need to find the right sink, to play audio on Windows. Alsasink won’t cut it, as it quite clearly is made for Linux. After trying several sinks via trial and error (osssink, esdsink, artsdsink, etc.) and no joy, I tried cygwin and gst-inspect to figure out which one I had, but this was also not successful. Finally, after a while, I came across a totally unrelated site, that mentions “autoaudiosink” as the auto-detect for your sound. Replacing “alsasink” with “autoaudiosink” and voilá, the sound was there when the script was run. Just careful and don’t turn the volume up too high. I did after a million and two failed tries and it almost blew me off my chair :D.

One last thing that I wanted to address was the pseudo-dependency on gtk. I say, pseudo, because the grand majority of examples were given with pygtk and thus most people have used gtk with it. Again a little bit of searching around, I found that the need for a gtk.main() call, was simply because GStreamer uses the Gobject module and its threads (see among others, this forum post: http://ubuntuforums.org/showthread.php?t=1168455 ). Once I found that, I was able to remove the gtk and pygtk import and replace it with the gobject import and the code still ran.
Thus, to some things up, the minimal example that works for Windows is as follows:

#!/usr/bin/python

import pygst
pygst.require("0.10")
import gst

# replaces the call to gtk and pygtk
import gobject
gobject.threads_init()

class Main:
def __init__(self):
self.pipeline = gst.Pipeline("mypipeline")

self.audiotestsrc = gst.element_factory_make("audiotestsrc", "audio")
self.pipeline.add(self.audiotestsrc)

self.sink = gst.element_factory_make("autoaudiosink", "sink")
self.pipeline.add(self.sink)

self.audiotestsrc.link(self.sink)

self.pipeline.set_state(gst.STATE_PLAYING)

start=Main()
gobject.MainLoop().run() # instead of gtk.main()

Having this in hand, I will next look at playing a simple audio file and testing the scope, i.e. which common formats work and if I can play several at the same time. Since I was able to remove gtk and replace it with gobject, I can now use wxpython to serve as my GUI of choice.

Advertisements

2 thoughts on “Using Gstreamer with Python2.6 in Windows XP – Pt. 2 A Minimal Example

    • Hey mosesesan,
      no sweat. I was learning at the time, too. I have abandoned this project by now, but one well-meant tip: If you plan on combining it with a GUI it is best to learn pygtk or one of its variations as it just works better (although the documentation tends to be a bit poo). I tried it with wxpython and landed in the land of too many endless loops.

      Always glad when my ramblings are useful to someone 🙂 Take care and success,

      Nebelhom

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s