Vala GStreamer Audio Example

using Gst;

public void main (string[] args) {
    Element src;
    Element sink;
    Pipeline pipeline;

    // Initializing GStreamer
    Gst.init (ref args);

    // Creating pipeline and elements
    // NOTE: The return type of the pipeline construction method is Element,
    // not Pipeline, so we have to cast
    pipeline = (Pipeline) new Pipeline ("test");
    src = ElementFactory.make ("audiotestsrc", "my_src");
    sink = ElementFactory.make ("autoaudiosink", "my_sink");

    // Adding elements to pipeline
    pipeline.add_many (src, sink);

    // Linking source to sink
    src.link (sink);

    // Setting waveform to square
    src.set ("wave", 1);

    // Set pipeline state to PLAYING
    pipeline.set_state (State.PLAYING);

    // Creating a GLib main loop with a default context
    var loop = new MainLoop (null, false);

    // Start GLib mainloop
    loop.run ();
}

Info: You can also declare a GStreamer Element as dynamic and set its properties directly:

    dynamic Element src;
    // ...
    src.wave = 1;

Compile and Run

$ valac --pkg gstreamer-0.10 -o gst-squarebeep gst-squarebeep.vala
$ ./gst-squarebeep

Vala GStreamer Audio Stream Example

using Gst;

public class StreamPlayer {

    private MainLoop loop = new MainLoop (null, false);

    private void foreach_tag (Gst.TagList list, string tag) {
        switch (tag) {
        case "title":
            string tag_string;
            list.get_string (tag, out tag_string);
            stdout.printf ("tag: %s = %s\n", tag, tag_string);
            break;
        default:
            break;
        }
    }

    private bool bus_callback (Gst.Bus bus, Gst.Message message) {
        switch (message.type) {
        case MessageType.ERROR:
            GLib.Error err;
            string debug;
            message.parse_error (out err, out debug);
            stdout.printf ("Error: %s\n", err.message);
            loop.quit ();
            break;
        case MessageType.EOS:
            stdout.printf ("end of stream\n");
            break;
        case MessageType.STATE_CHANGED:
            Gst.State oldstate;
            Gst.State newstate;
            Gst.State pending;
            message.parse_state_changed (out oldstate, out newstate,
                                         out pending);
            stdout.printf ("state changed: %s->%s:%s\n",
                           oldstate.to_string (), newstate.to_string (),
                           pending.to_string ());
            break;
        case MessageType.TAG:
            Gst.TagList tag_list;
            stdout.printf ("taglist found\n");
            message.parse_tag (out tag_list);
            tag_list.foreach (foreach_tag);
            break;
        default:
            break;
        }

        return true;
    }

    public void play (string stream) {
        dynamic Element play = ElementFactory.make ("playbin", "play");
        play.uri = stream;

        Bus bus = play.get_bus ();
        bus.add_watch (bus_callback);

        play.set_state (State.PLAYING);

        loop.run ();
    }
}

const string DEFAULT_STREAM = "http://scfire-dtc-aa06.stream.aol.com:80/stream/1018";

static int main (string[] args) {

    Gst.init (ref args);

    var player = new StreamPlayer ();
    player.play (args.length > 1 ? args[1] : DEFAULT_STREAM);

    return 0;
}

Compile and Run

$ valac --pkg gstreamer-0.10 play-stream.vala
$ ./play-stream

Vala GStreamer Video Example

using GLib;
using Gtk;
using Gst;

public class VideoSample : Window {

    private DrawingArea drawing_area;
    private Pipeline pipeline;
    private Element src;
    private Element sink;

    construct {
        create_widgets ();
        setup_gst_pipeline ();
    }

    private void create_widgets () {
        var vbox = new VBox (false, 0);
        this.drawing_area = new DrawingArea ();
        this.drawing_area.set_size_request (300, 150);
        vbox.pack_start (this.drawing_area, true, true, 0);

        var play_button = new Button.from_stock (STOCK_MEDIA_PLAY);
        play_button.clicked += on_play;
        var stop_button = new Button.from_stock (STOCK_MEDIA_STOP);
        stop_button.clicked += on_stop;
        var quit_button = new Button.from_stock (STOCK_QUIT);
        quit_button.clicked += Gtk.main_quit;

        var bb = new HButtonBox ();
        bb.add (play_button);
        bb.add (stop_button);
        bb.add (quit_button);
        vbox.pack_start (bb, false, true, 0);

        add (vbox);
    }

    private void setup_gst_pipeline () {
        this.pipeline = (Pipeline) new Pipeline ("mypipeline");
        this.src = ElementFactory.make ("videotestsrc", "video");
        this.sink = ElementFactory.make ("xvimagesink", "sink");
        this.pipeline.add_many (this.src, this.sink);
        this.src.link (this.sink);
    }

    private void on_play () {
        ((XOverlay) this.sink).set_xwindow_id (
                Gdk.x11_drawable_get_xid (this.drawing_area.window));
        this.pipeline.set_state (State.PLAYING);
    }

    private void on_stop () {
        this.pipeline.set_state (State.READY);
    }

    public static int main (string[] args) {
        Gst.init (ref args);
        Gtk.init (ref args);

        var sample = new VideoSample ();
        sample.show_all ();

        Gtk.main ();

        return 0;
    }
}

Compile and Run

$ valac --pkg gtk+-2.0 --pkg gdk-x11-2.0 --pkg gstreamer-0.10 --pkg gstreamer-interfaces-0.10 -o videosample VideoSample.vala
$ ./videosample

Vala/GStreamerSample (last edited 2009-05-21 15:05:41 by Frederik)