#!/usr/bin/perl -w

# Sreview, a web-based video review and transcoding system
# Copyright (c) 2016-2017, Wouter Verhelst <w@uter.be>
#
# Sreview is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with this program.  If not, see
# <http://www.gnu.org/licenses/>.

use strict;
use warnings;

use DBI;
use Glib qw/G_PRIORITY_DEFAULT/;
use Glib::Object::Introspection;
use File::Path qw/make_path/;

Glib::Object::Introspection->setup(
		basename => "Gst",
		version => "1.0",
		package => "Gst",
	);

my $mainloop = Glib::MainLoop->new();

Gst::init(\@ARGV);

our $config;
require "./config.pl";

my $talkid = $ARGV[0];

my $dbh = DBI->connect($config->{dbistring}, '', '') or die "Cannot connect to database!";

$dbh->prepare("UPDATE talks SET progress='running' WHERE id=? AND state='generating_previews' AND progress < 'running'")->execute($talkid);

$dbh->begin_work;

my $talk = $dbh->prepare("SELECT * FROM talks WHERE id = ?");

$talk->execute($talkid);

my $row = $talk->fetchrow_hashref();
my $slug = $row->{slug};

sub buswatch {
	my $bus = shift;
	my $msg = shift;
	my $data = shift;

	if($msg->type >= "eos") {
		$mainloop->quit();
	}

	return 1;
}

my $data = $dbh->prepare("SELECT events.id AS eventid, events.name AS event, rooms.name AS room, talks.starttime::date, talks.slug FROM talks JOIN events ON talks.event = events.id JOIN rooms ON rooms.id = talks.room WHERE talks.id = ?");
$data->execute($talkid);
my $drow = $data->fetchrow_hashref();
my $pubdir = $config->{pubdir};
my $eid = $drow->{eventid};
make_path("$pubdir/video/$eid");
my $outputdir = "$pubdir/video/$eid/" . substr($drow->{room}, 0, 1);
my @pipelines = (
	"webmmux name=mux ! filesink location=$outputdir/$slug.webm uridecodebin uri=file://$outputdir/$slug.ts name=demux demux. ! deinterlace ! videoconvert ! videoscale ! video/x-raw,width=160,height=90 ! vp8enc cq-level=63 ! queue ! mux.video_0 demux. ! progressreport ! audioconvert ! audiorate ! vorbisenc ! queue ! mux.audio_0",
	"webmmux name=mux ! filesink location=$outputdir/$slug-pre.webm uridecodebin uri=file://$outputdir/$slug-pre.ts name=demux demux. ! deinterlace ! videoconvert ! videoscale ! video/x-raw,width=160,height=90 ! vp8enc cq-level=63 ! queue ! mux.video_0 demux. ! progressreport ! audioconvert ! audiorate ! vorbisenc ! queue ! mux.audio_0",
	"webmmux name=mux ! filesink location=$outputdir/$slug-post.webm uridecodebin uri=file://$outputdir/$slug-post.ts name=demux demux. ! deinterlace ! videoconvert ! videoscale ! video/x-raw,width=160,height=90 ! vp8enc cq-level=63 ! queue ! mux.video_0 demux. ! progressreport ! audioconvert ! audiorate ! vorbisenc ! queue ! mux.audio_0",
);

foreach my $pipestring(@pipelines) {
	my $pipeline = Gst::parse_launch($pipestring);
	my $bus = $pipeline->get_bus();
	$bus->add_watch(G_PRIORITY_DEFAULT, \&buswatch, $mainloop);

	$pipeline->set_state("playing");

	$mainloop->run();
}

my $update = $dbh->prepare("UPDATE talks SET progress = 'done' WHERE id = ? AND state = 'generating_previews' AND progress < 'done'");
$update->execute($talkid);
$dbh->commit;
