ges-cut-clip.sh: user bash in the shebang line
[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 GObject
28 GObject.threads_init()
29
30
31 class Player:
32     def __init__(self):
33         self.loop = GObject.MainLoop()
34         self.pipeline = Gst.ElementFactory.make("playbin", "player")
35
36         files = ["sample_440hz.webm", "sample_880hz.webm"]
37         self.uris = [Gst.filename_to_uri(f) for f in files]
38         self.uri_index = 0
39         self.pipeline.set_property("uri", self.uris[self.uri_index])
40         self.pipeline.connect("about-to-finish", self.on_about_to_finish)
41
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)
47
48     def on_about_to_finish(self, playbin):
49         sys.stderr.write(".")
50         sys.stderr.flush()
51         playbin.set_property("uri", self.uris[self.uri_index])
52
53     def run(self):
54         self.pipeline.set_state(Gst.State.PLAYING)
55         self.loop.run()
56
57     def quit(self):
58         self.pipeline.set_state(Gst.State.NULL)
59         self.loop.quit()
60
61     def switch(self):
62         self.uri_index ^= 1
63         print("Next: %s" % self.uris[self.uri_index])
64
65         # Seek to the end of the stream, this will trigger the about-to-finish
66         # signal
67         seek_event = Gst.Event.new_seek(1.0,
68                                         Gst.Format.TIME,
69                                         Gst.SeekFlags.FLUSH,
70                                         Gst.SeekType.END, -1,
71                                         Gst.SeekType.NONE, 0)
72
73         self.pipeline.send_event(seek_event)
74
75     def on_eos(self, bus, msg):
76         self.quit()
77
78     def on_error(self, bus, msg):
79         (err, debug) = msg.parse_error()
80         print("Error: %s" % err)
81         print("Error: %s" % debug)
82         self.quit()
83
84     def on_state_changed(self, bus, msg):
85         if msg.src != self.pipeline:
86             return
87
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))
90
91
92 def main():
93     player = Player()
94
95     def stdin_cb(source, condition):
96         source.readline()
97         player.switch()
98         return True
99
100     GObject.io_add_watch(sys.stdin, GObject.IO_IN, stdin_cb)
101
102     print("\nPress Enter to switch the source\n")
103     player.run()
104
105
106 if __name__ == '__main__':
107     sys.exit(main())