|
Packit |
1470ea |
/*
|
|
Packit |
1470ea |
* Compile using:
|
|
Packit |
1470ea |
* valac --pkg gtk+-2.0 --pkg gdk-x11-2.0 --pkg gstreamer-0.10 --pkg gstreamer-interfaces-0.10 webcam.vala
|
|
Packit |
1470ea |
*
|
|
Packit |
1470ea |
*/
|
|
Packit |
1470ea |
using Gtk;
|
|
Packit |
1470ea |
using Gst;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
public class Webcam : Gtk.Window
|
|
Packit |
1470ea |
{
|
|
Packit |
1470ea |
private Gtk.DrawingArea drawing_area;
|
|
Packit |
1470ea |
private X.ID xid;
|
|
Packit |
1470ea |
private Gst.Element camerabin;
|
|
Packit |
1470ea |
private int counter = 1;
|
|
Packit |
1470ea |
private bool playing;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
public Webcam ()
|
|
Packit |
1470ea |
{
|
|
Packit |
1470ea |
this.set_title ("Press play to start");
|
|
Packit |
1470ea |
this.destroy.connect (Gtk.main_quit);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
var vbox = new Gtk.VBox (false, 0);
|
|
Packit |
1470ea |
this.drawing_area = new Gtk.DrawingArea ();
|
|
Packit |
1470ea |
this.drawing_area.set_size_request (640, 480);
|
|
Packit |
1470ea |
this.drawing_area.realize.connect (on_realize);
|
|
Packit |
1470ea |
vbox.pack_start (this.drawing_area, true, true, 0);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
var play_button = new Button.from_stock (Gtk.STOCK_MEDIA_PLAY);
|
|
Packit |
1470ea |
play_button.clicked.connect (on_play);
|
|
Packit |
1470ea |
var pause_button = new Button.from_stock (Gtk.STOCK_MEDIA_PAUSE);
|
|
Packit |
1470ea |
pause_button.clicked.connect (on_pause);
|
|
Packit |
1470ea |
var photo_button = new Button.with_label ("Take a picture");
|
|
Packit |
1470ea |
photo_button.clicked.connect (on_take_picture);
|
|
Packit |
1470ea |
var stop_button = new Button.from_stock (Gtk.STOCK_MEDIA_STOP);
|
|
Packit |
1470ea |
stop_button.clicked.connect (on_stop);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
var button_box = new Gtk.HButtonBox ();
|
|
Packit |
1470ea |
button_box.add (play_button);
|
|
Packit |
1470ea |
button_box.add (pause_button);
|
|
Packit |
1470ea |
button_box.add (photo_button);
|
|
Packit |
1470ea |
button_box.add (stop_button);
|
|
Packit |
1470ea |
vbox.pack_start (button_box, false, true, 5);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
this.add (vbox);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
this.camerabin = Gst.ElementFactory.make ("camerabin", "camera");
|
|
Packit |
1470ea |
var bus = this.camerabin.get_bus ();
|
|
Packit |
1470ea |
bus.set_sync_handler (on_bus_callback);
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
private Gst.BusSyncReply on_bus_callback (Gst.Bus bus, Gst.Message message)
|
|
Packit |
1470ea |
{
|
|
Packit |
1470ea |
if (message.get_structure () != null && message.get_structure().has_name("prepare-xwindow-id")) {
|
|
Packit |
1470ea |
var xoverlay = message.src as Gst.XOverlay;
|
|
Packit |
1470ea |
xoverlay.set_xwindow_id (this.xid);
|
|
Packit |
1470ea |
return Gst.BusSyncReply.DROP;
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
return Gst.BusSyncReply.PASS;
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
private void on_realize ()
|
|
Packit |
1470ea |
{
|
|
Packit |
1470ea |
this.xid = Gdk.x11_drawable_get_xid (this.drawing_area.window);
|
|
Packit |
1470ea |
on_play ();
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
private void on_play ()
|
|
Packit |
1470ea |
{
|
|
Packit |
1470ea |
this.camerabin.set_state (Gst.State.PLAYING);
|
|
Packit |
1470ea |
this.playing = true;
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
private void on_pause ()
|
|
Packit |
1470ea |
{
|
|
Packit |
1470ea |
this.camerabin.set_state (Gst.State.PAUSED);
|
|
Packit |
1470ea |
this.playing = false;
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
private void on_stop ()
|
|
Packit |
1470ea |
{
|
|
Packit |
1470ea |
this.camerabin.set_state (Gst.State.NULL);
|
|
Packit |
1470ea |
this.playing = false;
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
private void on_take_picture ()
|
|
Packit |
1470ea |
{
|
|
Packit |
1470ea |
if (this.playing)
|
|
Packit |
1470ea |
{
|
|
Packit |
1470ea |
var filename = "photo" + "%d".printf (this.counter) + ".jpg";
|
|
Packit |
1470ea |
this.set_title ("%d".printf (this.counter) + " photos taken");
|
|
Packit |
1470ea |
this.counter++;
|
|
Packit |
1470ea |
this.camerabin.set ("filename", filename);
|
|
Packit |
1470ea |
GLib.Signal.emit_by_name (this.camerabin, "capture-start");
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
public static int main (string[] args)
|
|
Packit |
1470ea |
{
|
|
Packit |
1470ea |
Gst.init (ref args);
|
|
Packit |
1470ea |
Gtk.init (ref args);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
var webcam = new Webcam ();
|
|
Packit |
1470ea |
webcam.show_all ();
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Gtk.main ();
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
return 0;
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
}
|