python: update python examples to modern python, Gst, and Glib versions
[experiments/gstreamer.git] / python / gst-playbin-switch.py
1 #!/usr/bin/env python3
2 #
3 # gst-playbin-switch - an example of switching file inputs with playbin
4 #
5 # Copyright (C) 2016  Antonio Ospite <ao2@ao2.it>
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 import sys
21
22 import gi
23 gi.require_version('Gst', '1.0')
24 from gi.repository import Gst
25 Gst.init(None)
26
27 from gi.repository import GLib
28
29
30 class Player:
31     def __init__(self):
32         self.loop = GLib.MainLoop()
33         self.pipeline = Gst.ElementFactory.make("playbin3", "player")
34
35         files = ["sample_440hz.webm", "sample_880hz.webm"]
36         self.uris = [Gst.filename_to_uri(f) for f in files]
37         self.uri_index = 0
38         self.pipeline.set_property("uri", self.uris[self.uri_index])
39         self.pipeline.connect("about-to-finish", self.on_about_to_finish)
40
41         bus = self.pipeline.get_bus()
42         bus.add_signal_watch()
43         bus.connect('message::eos', self.on_eos)
44         bus.connect('message::error', self.on_error)
45         bus.connect('message::state-changed', self.on_state_changed)
46
47     def on_about_to_finish(self, playbin):
48         sys.stderr.write(".")
49         sys.stderr.flush()
50         playbin.set_property("uri", self.uris[self.uri_index])
51
52     def run(self):
53         self.pipeline.set_state(Gst.State.PLAYING)
54         self.loop.run()
55
56     def quit(self):
57         self.pipeline.set_state(Gst.State.NULL)
58         self.loop.quit()
59
60     def on_switch(self):
61         self.uri_index ^= 1
62         print("Next: %s" % self.uris[self.uri_index])
63
64         # Seek to the end of the stream, this will trigger the about-to-finish
65         # signal
66         seek_event = Gst.Event.new_seek(1.0,
67                                         Gst.Format.TIME,
68                                         Gst.SeekFlags.FLUSH,
69                                         Gst.SeekType.END, -1,
70                                         Gst.SeekType.NONE, 0)
71
72         self.pipeline.send_event(seek_event)
73
74     def on_eos(self, bus, msg):
75         self.quit()
76
77     def on_error(self, bus, msg):
78         (err, debug) = msg.parse_error()
79         print("Error: %s" % err)
80         print("Error: %s" % debug)
81         self.quit()
82
83     def on_state_changed(self, bus, msg):
84         if msg.src != self.pipeline:
85             return
86
87         old_state, new_state, pending = msg.parse_state_changed()
88         print("%s from %s to %s" % (msg.src.get_name(), old_state, new_state))
89
90
91 def main():
92     player = Player()
93
94     def stdin_cb(source, condition):
95         source.readline()
96         player.on_switch()
97         return True
98
99     GLib.io_add_watch(sys.stdin, GLib.IO_IN, stdin_cb)
100
101     print("\nPress Enter to switch the source\n")
102     player.run()
103
104
105 if __name__ == '__main__':
106     sys.exit(main())