# -*- Mode: Python -*-
# vi:si:et:sw=4:sts=4:ts=4

import sys

import gobject
gobject.threads_init()

import gst

path = sys.argv[1]

loop = gobject.MainLoop()

i = 0
while True:
    print "loop", i
    i += 1

    pipeline = gst.parse_launch('''filesrc location="%s" 
        ! flacdec
        ! queue
        ! appsink name=sink sync=False''' % path)
    pipeline.set_state(gst.STATE_PAUSED)
# if we don't wait the query might fail
    pipeline.get_state()

    sink = pipeline.get_by_name('sink')
    length, format = sink.query_duration(gst.FORMAT_DEFAULT)

    event = gst.event_new_seek(1.0, gst.FORMAT_DEFAULT,
        gst.SEEK_FLAG_FLUSH,
        gst.SEEK_TYPE_SET, 0,
        gst.SEEK_TYPE_SET, length)

    result = sink.send_event(event)

    sink.connect('new-buffer', lambda s: s.emit('pull-buffer'))
    sink.connect('eos', lambda r: loop.quit())
    pipeline.set_state(gst.STATE_PLAYING)
    #gobject.timeout_add(1L, pipeline.set_state, gst.STATE_PLAYING)

    loop.run()

    pipeline.set_state(gst.STATE_PAUSED)
    del pipeline
