gst-trick-mode-looping-1.py: fix looping with negative rates
[experiments/gstreamer.git] / python / gst-trick-mode-looping-2.py
1 #!/usr/bin/env python
2 #
3 # A simple "trick-mode" looping player.
4 # Version 2, based on "about-to-finish" handling.
5 #
6 # Not working with speed rates other that 1.
7 #
8 # Get a test sample with:
9 # youtube-dl -t http://www.youtube.com/watch?v=yWa-YXiSk2Y
10
11 import sys
12
13 import gi
14 gi.require_version('Gst', '1.0')
15 from gi.repository import Gst
16 Gst.init(None)
17
18 from gi.repository import GObject
19 GObject.threads_init()
20
21
22 class Player:
23     def __init__(self, uri, rate):
24         self._uri = uri
25         self._rate = rate
26
27         self._player = Gst.ElementFactory.make("playbin", "player")
28         self._player.set_property("uri", uri)
29         self._player.connect("about-to-finish", self.on_about_to_finish)
30
31         bus = self._player.get_bus()
32         bus.add_signal_watch()
33         bus.connect('message::error', self.on_error)
34         bus.connect('message::state-changed', self.on_state_changed)
35
36     def run(self):
37         self._player.set_state(Gst.State.PLAYING)
38         self.loop = GObject.MainLoop()
39         self.loop.run()
40
41     def quit(self):
42         self._player.set_state(Gst.State.NULL)
43         self.loop.quit()
44
45     def on_about_to_finish(self, player):
46         sys.stderr.write(".")
47         self._player.set_property("uri", self._uri)
48
49         # XXX After the first iteration of the loop the playback rate is
50         # reset to normal. Do I need to re-set the rate here?
51
52         # If I uncomment the following line, nothing changes:
53         #self.set_rate(self._rate)
54
55         # And nothing changes either if I set the seek_event manually using
56         # position=0:
57         #seek_event = Gst.Event.new_seek(self._rate,
58         #                                Gst.Format.TIME,
59         #                                Gst.SeekFlags.FLUSH | Gst.SeekFlags.ACCURATE,
60         #                                Gst.SeekType.SET, 0,
61         #                                Gst.SeekType.NONE, 0)
62         #self._player.send_event(seek_event)
63
64     def on_error(self, bus, msg):
65         (err, debug) = msg.parse_error()
66         print "Error: %s" % err, debug
67         self.quit()
68
69     def on_state_changed(self, bus, msg):
70         if msg.src != self._player:
71             return
72
73         print 'on_state_changed'
74         old_state, new_state, pending = msg.parse_state_changed()
75         print "%s -> %s" % (old_state, new_state)
76         if old_state == Gst.State.READY and new_state == Gst.State.PAUSED:
77             self.set_rate(self._rate)
78
79     def set_rate(self, rate):
80         self._rate = rate
81         position = self._player.query_position(Gst.Format.TIME)[1]
82
83         # Create the seek event
84         seek_event = Gst.Event.new_seek(rate,
85                                         Gst.Format.TIME,
86                                         Gst.SeekFlags.FLUSH | Gst.SeekFlags.ACCURATE,
87                                         Gst.SeekType.SET, position,
88                                         Gst.SeekType.SET, -1)
89
90         if seek_event:
91             self._player.send_event(seek_event)
92             Gst.info("rate set to %s" % rate)
93         else:
94              Gst.warining("change rate failed")
95
96
97 def main(args):
98     def usage():
99         sys.stdout.write("usage: %s <filename> <speedrate>\n" % args[0])
100
101     if len(args) != 3:
102         usage()
103         sys.exit(1)
104
105     uri = Gst.filename_to_uri(args[1])
106     rate = float(args[2])
107
108     player = Player(uri, rate)
109     player.run()
110
111 if __name__ == '__main__':
112     sys.exit(main(sys.argv))