#!/usr/bin/perl -T -w
use strict;
use Glib qw(TRUE FALSE);
use GStreamer -init;
use threads;

my $loop;
my $l_SPipeline;
my $l_SSource;
my $l_SOggDemuxer;
my $l_SVorbisDecoder;
my $l_SAudioConverter;
my $l_SSink;
my $pipe_in;

$ENV{PATH}='';
&init();
&main();

exit 0;



sub init()
{
  $loop = Glib::MainLoop -> new();

  # create a new pipeline to hold the elements
  $l_SPipeline = GStreamer::Pipeline -> new("pipeline");

  $l_SSource = GStreamer::ElementFactory->make(fdsrc  =>  "fd-source");
  $l_SOggDemuxer = GStreamer::ElementFactory->make(oggdemux => "ogg-demuxer");
  $l_SVorbisDecoder = GStreamer::ElementFactory->make(vorbisdec => "vorbis-decoder");
  $l_SAudioConverter = GStreamer::ElementFactory->make(audioconvert => "converter");
  $l_SSink = GStreamer::ElementFactory->make(autoaudiosink  =>  "sink");

  my $pipe_out;
  pipe $pipe_out, $pipe_in;  
  $l_SSource->set(fd => fileno($pipe_out));

  $l_SPipeline -> add($l_SSource, $l_SOggDemuxer, $l_SVorbisDecoder, $l_SAudioConverter, $l_SSink);

  $l_SSource->link($l_SOggDemuxer) or die "Can't link source to OGG-muxer";
  $l_SVorbisDecoder->link($l_SAudioConverter, $l_SSink)  or die "Can't link rest of stuff";
  $l_SOggDemuxer->link_pads("src", $l_SVorbisDecoder, "sink");
  $l_SOggDemuxer->signal_connect("pad_added", \&pgotm_addPads) or die "Can't link Pads to vorbis";;

  $l_SPipeline -> get_bus() -> add_watch(\&my_bus_callback, $loop);

  print "setting pipeline to paused\n";
  $l_SPipeline -> set_state("paused");
  print "setting pipeline to playing\n";
  $l_SPipeline -> set_state("playing");
}

# Add pads
sub pgotm_addPads {
  my ($element, $pad, $data) = @_;

  my $l_SCaps = $pad->get_caps();
  my $l_SMime = $l_SCaps->get_structure(0) -> { name };

  printf "Mime that is added is: %sn", $l_SMime;
  printf "A new pad %s was created %sn", $pad->get_name(), $pad->get_parent()->get_name();

  # If it's vorbis audio
  if( $l_SMime eq "audio/x-vorbis" ) {
    # Can it link to the audiopad?
    my $l_SSinkPad = $l_SVorbisDecoder->get_pad("sink");

    # Check if vorbis not linked..
    if(  !$l_SSinkPad->is_linked() ) {
      if( $pad->link( $l_SSinkPad ) ) {
        die "Can't link pads!\n";
      }
    }
  }
}

sub my_bus_callback {
    my ($bus, $message, $loop) = @_;

    if ($message -> type & "error") {
        warn $message -> error;
        $loop -> quit();
    }

    elsif ($message -> type & "eos") {
        print "EOS received!\n";
        $loop -> quit();
    }

    return TRUE;
}

sub play
{
  my ($file)=@_;
  print "trying to play ${file}.ogg\n";

  my $data;
  open(my $fh, '<', "${file}.ogg") or die $!;
  while(my $n = read($fh, $data, 1024)) 
  { 
    print "${n} bytes read\n";
    print $pipe_in $data;
  } 
  print "finished read";
  close($fh);
}


sub main
{
  Glib::Timeout->add (5000, \&play, "double");

  $loop->run();


#  while (my $schuss = <STDIN>)
#  {
#		print $schuss;
#    chomp $schuss;
		
#    my ($mult,$zahl)=split /\s+/,$schuss;
#		if ($mult==2)
#		{
#		  play("double")
#		} elsif ($mult==3) {
#		  play("triple")
#    }
#		($zahl) = $zahl =~ m/(\d+)/;
#		if ($zahl >0 && $zahl < 21)
#    {
#			play($zahl)
#		} else {
#			play("bull")
#		}
#  }
}

