#!/usr/bin/perl

use strict;
use warnings;

use Glib qw(TRUE FALSE);
use GStreamer '-init';
use Gtk2 '-init';

exit main();


sub main {
	my $loop = Glib::MainLoop->new();
	
	my $pipeline = GStreamer::Pipeline->new('pipeline');
	
	# The pipeline's elements
	my ($src, $sink) = GStreamer::ElementFactory->make(
		videotestsrc => 'source',
		xvimagesink  => 'sink',
	);
	
	$pipeline->add($src, $sink);
	$src->link($sink);
	
	# Create the widgets
	my $window = Gtk2::Window->new();
	$window->set_title("Gst video test");
	my $canvas = Gtk2::DrawingArea->new();
	$canvas->set_size_request(300, 150);
	
	my $vbox = Gtk2::VBox->new(FALSE, 0);
	$vbox->pack_start($canvas, TRUE, TRUE, 0);
	
	my $buttons = Gtk2::HButtonBox->new();
	add_button($buttons, 'gtk-media-play', sub {
		$pipeline->set_state('playing');
	});
	$vbox->pack_start($buttons, FALSE, TRUE, 0);
	
	$window->add($vbox);
	$window->show_all();
	
	$window->signal_connect(destroy => sub {Gtk2->main_quit()});

	# Run the program
	$sink->set_xwindow_id($canvas->window->get_xid);
	Gtk2->main();
	
	# Cleanup
	$pipeline->set_state('null');
	return 0;
}


sub add_button {
	my ($box, $stock, $callback) = @_;
	my $button = Gtk2::Button->new_from_stock($stock);
	$button->signal_connect(clicked => $callback);
	$box->add($button);
}
