python: update python examples to modern python, Gst, and Glib versions
[experiments/gstreamer.git] / python / gst-trick-mode.py
index 43cd8f4..5ae62b6 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 #
 # A simple "trick-mode" player to play a file at a given speed rate.
 #
@@ -12,8 +12,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:
@@ -31,7 +30,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):
@@ -43,39 +42,29 @@ class Player:
 
     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
-        try:
-            position, fmt = self._player.query_position(Gst.Format.TIME)
-        except:
-            position = 0
+        position = self._player.query_position(Gst.Format.TIME)[1]
 
         # Create the seek event
-        if rate > 0:
-            seek_event = Gst.Event.new_seek(rate,
-                                            Gst.Format.TIME,
-                                            Gst.SeekFlags.FLUSH | Gst.SeekFlags.ACCURATE,
-                                            Gst.SeekType.SET, position,
-                                            Gst.SeekType.NONE, 0)
-        else:
-            seek_event = Gst.Event.new_seek(rate,
-                                            Gst.Format.TIME,
-                                            Gst.SeekFlags.FLUSH | Gst.SeekFlags.ACCURATE,
-                                            Gst.SeekType.SET, 0,
-                                            Gst.SeekType.SET, position)
+        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 seek_event:
             self._player.send_event(seek_event)