python: update python examples to modern python, Gst, and Glib versions
[experiments/gstreamer.git] / python / gst-trick-mode-looping-2.py
index 7b6b07c..a398331 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 #
 # A simple "trick-mode" looping player.
 # Version 2, based on "about-to-finish" handling.
@@ -15,8 +15,7 @@ gi.require_version('Gst', '1.0')
 from gi.repository import Gst
 Gst.init(None)
 
-from gi.repository import GObject
-GObject.threads_init()
+from gi.repository import GLib
 
 
 class Player:
@@ -24,7 +23,7 @@ class Player:
         self._uri = uri
         self._rate = rate
 
-        self._player = Gst.ElementFactory.make("playbin", "player")
+        self._player = Gst.ElementFactory.make("playbin3", "player")
         self._player.set_property("uri", uri)
         self._player.connect("about-to-finish", self.on_about_to_finish)
 
@@ -35,7 +34,7 @@ class Player:
 
     def run(self):
         self._player.set_state(Gst.State.PLAYING)
-        self.loop = GObject.MainLoop()
+        self.loop = GLib.MainLoop()
         self.loop.run()
 
     def quit(self):
@@ -47,51 +46,55 @@ class Player:
         self._player.set_property("uri", self._uri)
 
         # XXX After the first iteration of the loop the playback rate is
-        # reset to normal. Do I need to re-set the rate here?
+        # reset to 1. Do I need to re-set the rate here?
 
         # If I uncomment the following line, nothing changes:
-        #self.set_rate(self._rate)
-
-        # And nothing changes either if I set the seek_event manually using
-        # position=0:
-        #seek_event = Gst.Event.new_seek(self._rate,
-        #                                Gst.Format.TIME,
-        #                                Gst.SeekFlags.FLUSH | Gst.SeekFlags.ACCURATE,
-        #                                Gst.SeekType.SET, 0,
-        #                                Gst.SeekType.NONE, 0)
-        #self._player.send_event(seek_event)
+        #self.set_seek(0)
 
     def on_error(self, bus, msg):
         (err, debug) = msg.parse_error()
-        print "Error: %s" % err, debug
+        print("Error: %s" % err, debug)
         self.quit()
 
     def on_state_changed(self, bus, msg):
         if msg.src != self._player:
             return
 
-        print 'on_state_changed'
+        print('on_state_changed')
         old_state, new_state, pending = msg.parse_state_changed()
-        print "%s -> %s" % (old_state, new_state)
+        print("%s -> %s" % (old_state, new_state))
         if old_state == Gst.State.READY and new_state == Gst.State.PAUSED:
             self.set_rate(self._rate)
 
     def set_rate(self, rate):
         self._rate = rate
         position = self._player.query_position(Gst.Format.TIME)[1]
+        self.set_seek(position, True)
+
+    def set_seek(self, position, flush=False):
+        flags = Gst.SeekFlags.SKIP | Gst.SeekFlags.ACCURATE
 
-        # Create the seek event
-        seek_event = Gst.Event.new_seek(rate,
-                                        Gst.Format.TIME,
-                                        Gst.SeekFlags.FLUSH | Gst.SeekFlags.ACCURATE,
-                                        Gst.SeekType.SET, position,
-                                        Gst.SeekType.SET, -1)
+        if flush:
+            flags |= Gst.SeekFlags.FLUSH
+
+        if self._rate >= 0:
+            seek_event = Gst.Event.new_seek(self._rate,
+                                            Gst.Format.TIME,
+                                            flags,
+                                            Gst.SeekType.SET, position,
+                                            Gst.SeekType.NONE, 0)
+        else:
+            seek_event = Gst.Event.new_seek(self._rate,
+                                            Gst.Format.TIME,
+                                            flags,
+                                            Gst.SeekType.NONE, 0,
+                                            Gst.SeekType.END, position)
 
         if seek_event:
             self._player.send_event(seek_event)
-            Gst.info("rate set to %s" % rate)
+            Gst.info("rate set to %s" % self._rate)
         else:
-             Gst.warining("change rate failed")
+            Gst.warining("change rate failed")
 
 
 def main(args):