#!/usr/bin/perl
use strict;
use warnings;
use Test::More tests => 20;

# $Id: GstClock.t,v 1.4 2006/01/29 20:45:40 kaffeetisch Exp $

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

my $element = GStreamer::ElementFactory -> make("alsasink", "sink");
my $clock = $element -> provide_clock();

my $master_element = GStreamer::ElementFactory -> make("alsasink", "sink");
my $master = $element -> provide_clock();

is($clock -> set_resolution(1000), 0);
is($clock -> get_resolution(), 1000);

ok($clock -> get_time() >= 0);

$clock -> set_calibration(0, 2, 3, 4);
is_deeply([$clock -> get_calibration()], [0, 2, 3, 4]);

ok($clock -> set_master($master));
is($clock -> get_master(), $master);

my ($result, $r) = $clock -> add_observation(23, 42);
ok(!$result);
ok($r >= 0);

ok($clock -> get_internal_time() >= 0);
ok($clock -> adjust_unlocked(23) >= 0);

my $id = $clock -> new_single_shot_id($clock -> get_time() + 100);
isa_ok($id, "GStreamer::ClockID");

$id = $clock -> new_periodic_id($clock -> get_time(), 100);
isa_ok($id, "GStreamer::ClockID");

ok($id -> get_time() > 0);

my ($return, $jitter) = $id -> wait();
is($return, "ok");
ok($jitter >= 0);

SKIP: {
  skip "async waits cause threading related segfaults", 5;

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

  is($id -> wait_async(sub {
    my ($clock, $time, $id, $data) = @_;

    my $been_here = 0 if 0;
    return TRUE if $been_here++;

    isa_ok($clock, "GStreamer::Clock");
    ok($time > 0);
    isa_ok($id, "GStreamer::ClockID");
    is($data, "bla");

    $loop -> quit();

    return TRUE;
  }, "bla"), "ok");

  $loop -> run();
}

$id -> unschedule();
