gst-input-selector-switch.py: use a better pattern for the second source
[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 1. Do I need to re-set the rate here?
51
52         # If I uncomment the following line, nothing changes:
53         #self.set_seek(0)
54
55     def on_error(self, bus, msg):
56         (err, debug) = msg.parse_error()
57         print "Error: %s" % err, debug
58         self.quit()
59
60     def on_state_changed(self, bus, msg):
61         if msg.src != self._player:
62             return
63
64         print 'on_state_changed'
65         old_state, new_state, pending = msg.parse_state_changed()
66         print "%s -> %s" % (old_state, new_state)
67         if old_state == Gst.State.READY and new_state == Gst.State.PAUSED:
68             self.set_rate(self._rate)
69
70     def set_rate(self, rate):
71         self._rate = rate
72         position = self._player.query_position(Gst.Format.TIME)[1]
73         self.set_seek(position, True)
74
75     def set_seek(self, position, flush=False):
76         flags = Gst.SeekFlags.SKIP | Gst.SeekFlags.ACCURATE
77
78         if flush:
79             flags |= Gst.SeekFlags.FLUSH
80
81         if self._rate >= 0:
82             seek_event = Gst.Event.new_seek(self._rate,
83                                             Gst.Format.TIME,
84                                             flags,
85                                             Gst.SeekType.SET, position,
86                                             Gst.SeekType.NONE, 0)
87         else:
88             seek_event = Gst.Event.new_seek(self._rate,
89                                             Gst.Format.TIME,
90                                             flags,
91                                             Gst.SeekType.NONE, 0,
92                                             Gst.SeekType.END, position)
93
94         if seek_event:
95             self._player.send_event(seek_event)
96             Gst.info("rate set to %s" % self._rate)
97         else:
98             Gst.warining("change rate failed")
99
100
101 def main(args):
102     def usage():
103         sys.stdout.write("usage: %s <filename> <speedrate>\n" % args[0])
104
105     if len(args) != 3:
106         usage()
107         sys.exit(1)
108
109     uri = Gst.filename_to_uri(args[1])
110     rate = float(args[2])
111
112     player = Player(uri, rate)
113     player.run()
114
115 if __name__ == '__main__':
116     sys.exit(main(sys.argv))