3 # gst-playbin-switch - an example of switching file inputs with playbin
5 # Copyright (C) 2016 Antonio Ospite <ao2@ao2.it>
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.
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.
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/>.
23 gi.require_version('Gst', '1.0')
24 from gi.repository import Gst
27 from gi.repository import GLib
32 self.loop = GLib.MainLoop()
33 self.pipeline = Gst.ElementFactory.make("playbin3", "player")
35 files = ["sample_440hz.webm", "sample_880hz.webm"]
36 self.uris = [Gst.filename_to_uri(f) for f in files]
38 self.pipeline.set_property("uri", self.uris[self.uri_index])
39 self.pipeline.connect("about-to-finish", self.on_about_to_finish)
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)
47 def on_about_to_finish(self, playbin):
50 playbin.set_property("uri", self.uris[self.uri_index])
53 self.pipeline.set_state(Gst.State.PLAYING)
57 self.pipeline.set_state(Gst.State.NULL)
62 print("Next: %s" % self.uris[self.uri_index])
64 # Seek to the end of the stream, this will trigger the about-to-finish
66 seek_event = Gst.Event.new_seek(1.0,
72 self.pipeline.send_event(seek_event)
74 def on_eos(self, bus, msg):
77 def on_error(self, bus, msg):
78 (err, debug) = msg.parse_error()
79 print("Error: %s" % err)
80 print("Error: %s" % debug)
83 def on_state_changed(self, bus, msg):
84 if msg.src != self.pipeline:
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))
94 def stdin_cb(source, condition):
99 GLib.io_add_watch(sys.stdin, GLib.IO_IN, stdin_cb)
101 print("\nPress Enter to switch the source\n")
105 if __name__ == '__main__':