vidi/Player.py: use per message callbacks
[vidi-player.git] / vidi / Player.py
index 26a9832..d2eacef 100755 (executable)
@@ -30,6 +30,12 @@ GObject.threads_init()
 class Player(object):
     def __init__(self, pipeline):
         self.pipeline = pipeline
 class Player(object):
     def __init__(self, pipeline):
         self.pipeline = pipeline
+
+        bus = self.pipeline.get_bus()
+        bus.add_signal_watch()
+        bus.connect("message::eos", self.on_eos)
+        bus.connect('message::error', self.on_error)
+
         self.mainloop = GObject.MainLoop()
 
     @staticmethod
         self.mainloop = GObject.MainLoop()
 
     @staticmethod
@@ -37,25 +43,18 @@ class Player(object):
         pipeline = Gst.parse_launch(pipeline_string)
         return Player(pipeline)
 
         pipeline = Gst.parse_launch(pipeline_string)
         return Player(pipeline)
 
-    def quit(self):
-        self.mainloop.quit()
-        self.pipeline.set_state(Gst.State.NULL)
+    def on_eos(self, bus, message):
+        self.stop()
 
 
-    def bus_message_cb(self, unused_bus, message):
-        if message.type == Gst.MessageType.EOS:
-            self.quit()
+    def on_error(self, bus, msg):
+        (err, debug) = msg.parse_error()
+        print("Error: %s" % err)
+        self.stop()
 
     def play(self):
 
     def play(self):
-        bus = self.pipeline.get_bus()
-        bus.add_signal_watch()
-        bus.connect("message", self.bus_message_cb)
-
         self.pipeline.set_state(Gst.State.PLAYING)
         self.pipeline.set_state(Gst.State.PLAYING)
+        self.mainloop.run()
 
 
-        try:
-            self.mainloop.run()
-        except KeyboardInterrupt:
-            self.quit()
-            return 1
-
-        return 0
+    def stop(self):
+        self.mainloop.quit()
+        self.pipeline.set_state(Gst.State.NULL)