#!/usr/bin/perl
use strict;
use warnings;
use GStreamer qw(-init GST_SECOND GST_TIME_FORMAT GST_TIME_ARGS);

# This is based on the gst123 example from gst-python.

foreach my $file (@ARGV) {
  my $thread = GStreamer::Thread -> new("player");

  $thread -> signal_connect(eos => sub { GStreamer -> main_quit(); });
  $thread -> signal_connect(error => sub {
    my ($thread, $element, $error) = @_;
    printf "An error occured: %s\n", $error -> message();
    exit(1);
  });

  my ($source, $spider, $sink) =
    GStreamer::ElementFactory -> make(filesrc => "src",
                                      spider => "spider",
                                      osssink => "sink");

  $source -> set(location => $file);
  $spider -> signal_connect(found_tag => sub {
    my ($spider, $source, $tags) = @_;

    foreach (qw(artist title album track-number)) {
      if (exists $tags -> { $_ }) {
        printf "  %s: %s\n", ucfirst $_, $tags -> { $_ } -> [0];
      }
    }

    my ($format, $total) = $spider -> query("total", "time");
    if ($total) {
      printf "Length: %". GST_TIME_FORMAT . "\n", GST_TIME_ARGS($total);
    }
  });

  $thread -> add($source, $spider, $sink);
  $source -> link($spider, $sink) or die "Could not link";

  print "Playing: $file\n";
  $thread -> set_state("playing") or die "Could not start playing";

  Glib::Timeout -> add(5000, sub {
    my ($format, $pos) = $spider -> query("position", "time");

    warn "$pos, ", $pos + 5 * GST_SECOND;
    $sink -> seek([qw/method-set flag-flush time/], $pos + 5 * GST_SECOND);

    return 1;
  });

  GStreamer -> main();
  $thread -> set_state("null");
}
