Vala GStreamer Audio Example
using Gst;
void main (string[] args) {
// Initializing GStreamer
Gst.init (ref args);
// Creating pipeline and elements
var pipeline = new Pipeline ("test");
var src = ElementFactory.make ("audiotestsrc", "my_src");
var 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 and starting a GLib main loop
new MainLoop ().run ();
}
Tip: You can also declare a GStreamer Element as dynamic and set its properties directly:
dynamic Element src = ElementFactory.make ("audiotestsrc", "my_src");
// ...
src.wave = 1;
Compile and Run
$ valac --pkg gstreamer-0.10 gst-squarebeep.vala $ ./gst-squarebeep
Vala GStreamer Audio Stream Example
using Gst;
public class StreamPlayer {
private MainLoop loop = new MainLoop ();
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 ((TagForeachFunc) 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://streamer-dtc-aa02.somafm.com:80/stream/1018";
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 gst-play-stream.vala $ ./gst-play-stream
Vala GStreamer Video Example
Requires Vala >= 0.11.0
using Gtk;
using Gst;
public class VideoSample : Window {
private DrawingArea drawing_area;
private Pipeline pipeline;
private Element src;
private Element sink;
private ulong xid;
public VideoSample () {
create_widgets ();
setup_gst_pipeline ();
}
private void create_widgets () {
var vbox = new Box (Orientation.VERTICAL, 0);
this.drawing_area = new DrawingArea ();
this.drawing_area.realize.connect(on_realize);
vbox.pack_start (this.drawing_area, true, true, 0);
var play_button = new Button.from_stock (Stock.MEDIA_PLAY);
play_button.clicked.connect (on_play);
var stop_button = new Button.from_stock (Stock.MEDIA_STOP);
stop_button.clicked.connect (on_stop);
var quit_button = new Button.from_stock (Stock.QUIT);
quit_button.clicked.connect (Gtk.main_quit);
var bb = new ButtonBox (Orientation.HORIZONTAL);
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 = 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_realize() {
this.xid = (ulong)Gdk.X11Window.get_xid(this.drawing_area.get_window());
}
private void on_play () {
var xoverlay = this.sink as XOverlay;
xoverlay.set_xwindow_id (this.xid);
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+-3.0 --pkg gdk-x11-3.0 --pkg gstreamer-0.10 --pkg gstreamer-interfaces-0.10 gst-videotest.vala $ ./gst-videotest