gst-vocoder.sh: use vocoder_1337.so from swh-plugins
[experiments/gstreamer.git] / python / gst-input-selector-switch.py
1 #!/usr/bin/env python3
2
3 import sys
4
5 import gi
6 gi.require_version('Gst', '1.0')
7 from gi.repository import Gst
8 Gst.init(None)
9
10 from gi.repository import GObject
11 GObject.threads_init()
12
13
14 # The following pipeline works
15 PIPELINE = """
16 videotestsrc pattern=0 ! selector.
17 videotestsrc pattern=13 ! selector.
18 input-selector name=selector ! autovideosink
19 """
20
21
22 class Player:
23     def __init__(self):
24         self.loop = GObject.MainLoop()
25         self.pipeline = Gst.parse_launch(PIPELINE)
26         self.selector = self.pipeline.get_by_name('selector')
27
28         pad = self.selector.get_static_pad("sink_0")
29         self.selector.set_property("active-pad", pad)
30         print("n-pads: %d" % self.selector.get_property("n-pads"))
31
32         bus = self.pipeline.get_bus()
33         bus.add_signal_watch()
34         bus.connect('message::eos', self.on_eos)
35         bus.connect('message::error', self.on_error)
36         bus.connect('message::state-changed', self.on_state_changed)
37
38     def run(self):
39         self.pipeline.set_state(Gst.State.PLAYING)
40         self.loop.run()
41
42     def quit(self):
43         self.pipeline.set_state(Gst.State.NULL)
44         self.loop.quit()
45
46     def switch(self):
47         active_pad = self.selector.get_property("active-pad")
48         if active_pad.get_name() == "sink_0":
49             new_pad = self.selector.get_static_pad("sink_1")
50         else:
51             new_pad = self.selector.get_static_pad("sink_0")
52
53         print("switching from %s to %s" % (active_pad.get_name(),
54                                            new_pad.get_name()))
55
56         self.selector.set_property("active-pad", new_pad)
57
58     def on_eos(self, bus, msg):
59         self.quit()
60
61     def on_error(self, bus, msg):
62         (err, debug) = msg.parse_error()
63         print("Error: %s" % err)
64         self.quit()
65
66     def on_state_changed(self, bus, msg):
67         if msg.src != self.pipeline:
68             return
69
70         old_state, new_state, pending = msg.parse_state_changed()
71         print("%s from %s to %s" % (msg.src.get_name(), old_state, new_state))
72
73
74 def main():
75     player = Player()
76
77     def stdin_cb(source, condition):
78         source.readline()
79         player.switch()
80         return True
81
82     GObject.io_add_watch(sys.stdin, GObject.IO_IN, stdin_cb)
83
84     print("\nPress Enter to switch the source\n")
85     player.run()
86
87
88 if __name__ == '__main__':
89     sys.exit(main())