#!/usr/bin/env python

import sys

import pygst
pygst.require('0.10')
import gst

def main(args):
    _, uri, port = args
    port = int(port)

    # make the pipeline
    pipeline = gst.parse_launch('gnomevfssrc name=src ! oggdemux ! '
                                'theoradec ! ffmpegcolorspace ! '
                                'videoscale ! ximagesink')
    pipeline.get_by_name('src').set_uri(uri) # uri interface

    # make sure some other clock isn't autoselected
    clock = pipeline.get_clock()
    print 'Using clock: ', clock
    pipeline.use_clock(clock)

    # this will start a server listening on a UDP port
    clock_provider = gst.NetTimeProvider(clock, None, port)

    # we explicitly manage our base time
    base_time = clock.get_time()
    print ('Start slave as: python ./play-slave.py %s [IP] %d %d'
           % (uri, port, base_time))

    # disable the pipeline's management of base_time -- we're going
    # to set it ourselves.
    pipeline.set_new_stream_time(gst.CLOCK_TIME_NONE)
    pipeline.set_base_time(base_time)

    # now we go :)
    pipeline.set_state(gst.STATE_PLAYING)

    # wait until things stop
    pipeline.get_bus().poll(gst.MESSAGE_EOS | gst.MESSAGE_ERROR, -1)
    pipeline.set_state(gst.STATE_NULL)

if __name__ == '__main__':
    main(sys.argv)
