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 GObject
28 GObject.threads_init()
33 self.loop = GObject.MainLoop()
34 self.pipeline = Gst.ElementFactory.make("playbin", "player")
36 files = ["sample_440hz.webm", "sample_880hz.webm"]
37 self.uris = [Gst.filename_to_uri(f) for f in files]
39 self.pipeline.set_property("uri", self.uris[self.uri_index])
40 self.pipeline.connect("about-to-finish", self.on_about_to_finish)
42 bus = self.pipeline.get_bus()
43 bus.add_signal_watch()
44 bus.connect('message::eos', self.on_eos)
45 bus.connect('message::error', self.on_error)
46 bus.connect('message::state-changed', self.on_state_changed)
48 def on_about_to_finish(self, playbin):
51 playbin.set_property("uri", self.uris[self.uri_index])
54 self.pipeline.set_state(Gst.State.PLAYING)
58 self.pipeline.set_state(Gst.State.NULL)
63 print("Next: %s" % self.uris[self.uri_index])
65 # Seek to the end of the stream, this will trigger the about-to-finish
67 seek_event = Gst.Event.new_seek(1.0,
73 self.pipeline.send_event(seek_event)
75 def on_eos(self, bus, msg):
78 def on_error(self, bus, msg):
79 (err, debug) = msg.parse_error()
80 print("Error: %s" % err)
81 print("Error: %s" % debug)
84 def on_state_changed(self, bus, msg):
85 if msg.src != self.pipeline:
88 old_state, new_state, pending = msg.parse_state_changed()
89 print("%s from %s to %s" % (msg.src.get_name(), old_state, new_state))
95 def stdin_cb(source, condition):
100 GObject.io_add_watch(sys.stdin, GObject.IO_IN, stdin_cb)
102 print("\nPress Enter to switch the source\n")
106 if __name__ == '__main__':