#!/usr/bin/env python3
import gi
import sys

gi.require_version("Gst", "1.0")
from gi.repository import Gst, GObject


Gst.init(sys.argv)


class PlaybinAdderExample(object):
    def __init__(self, uri):
        self.pipeline = Gst.ElementFactory.make("playbin", None)
        self.pipeline.set_property("uri", uri)
        
        adder = Gst.ElementFactory.make("adder", None)
        self.pipeline.set_property("audio-stream-combiner", adder);
        
        # xvimagesink is broken on my VM
        videosink = Gst.ElementFactory.make("autovideosink", None)
        self.pipeline.set_property("video-sink", videosink)
        
        self.bus = self.pipeline.get_bus()
        self.bus.add_signal_watch()
        self.bus.connect("message::eos", self.on_eos)
        self.bus.connect("message::error", self.on_error)
        
        self.main_loop = GObject.MainLoop()
        
    def on_eos(self, bus, message):
        self.quit()
        
    def on_error(self, bus, message):
        print(message.parse_error())
        self.quit()
    
    def quit(self):
        self.pipeline.set_state(Gst.State.NULL)
        self.main_loop.quit()
    
    def run(self):
        self.pipeline.set_state(Gst.State.PLAYING)
        self.main_loop.run()

if len(sys.argv) < 2:
    print("Usage: %s [uri]" % (sys.argv[0]))
    sys.exit(1)
else:
    PlaybinAdderExample(sys.argv[1]).run()
