#!/usr/bin/python3

from gi.repository import Gst, GES, GObject, Gtk
import signal

videoFile = "file:///home/bmonkey/workspace/ges/data/sd/raw.mov"

def busMessageCb(bus, message):
  if message.type == Gst.MessageType.EOS:
      print("eos")
      Gtk.main_quit()
  elif message.type == Gst.MessageType.ERROR:
      print (message.parse_error())
  else:
      pass

def duration_querier(pipeline):
  print(pipeline.query_position(Gst.Format.TIME)[1] / Gst.SECOND)
  return True

if  __name__ =='__main__':
  Gst.init(None)
  GES.init()

  timeline = GES.Timeline.new_audio_video ()
  layer = GES.Layer.new ()
  
  timeline.add_layer(layer)

  asset = GES.UriClipAsset.request_sync(videoFile)
  layer.add_asset(asset, 0, 0, 5 * Gst.SECOND, GES.TrackType.UNKNOWN)

  timeline.commit ()

  pipeline = GES.Pipeline()
  pipeline.add_timeline(timeline)
  pipeline.set_state(Gst.State.PLAYING)

  bus = pipeline.get_bus()
  bus.add_signal_watch()
  bus.connect("message", busMessageCb)
  GObject.timeout_add(1000, duration_querier, pipeline)

  signal.signal(signal.SIGINT, signal.SIG_DFL)
  Gtk.main()
