f2aa4eafa3fe9e3de6b51cdf4b7d2ab772866a9c
[python-gstreamer-examples] / example5.py
1 #!/usr/bin/env python
2 # Attempting to dynamically switch between input sources
3
4 # GdkX11 to get access to xid, GstVideo to get access to set_window_handle
5 import gi, signal, time
6 gi.require_version('Gtk', '3.0')
7 gi.require_version('Gst', '1.0')
8 gi.require_version('GstVideo', '1.0')
9 from gi.repository import Gtk, Gst, GdkX11, GstVideo
10
11 stream1="http://192.168.0.90/video/mjpg.cgi"
12
13 class Main:
14 def __init__(self):
15
16 # Create gui bits and bobs
17
18 self.mainwindow = Gtk.Builder()
19 self.mainwindow.add_from_file("example5.glade")
20
21 signals = {
22 "on_input1_clicked" : self.SetInput1,
23 "on_input2_clicked" : self.SetInput2,
24 "on_input3_clicked" : self.SetInput3,
25 "on_input4_clicked" : self.SetInput4,
26 "on_input5_clicked" : self.SetInput5,
27 "on_input6_clicked" : self.SetInput6,
28 "on_quit_clicked" : self.OnQuit,
29 }
30
31 self.mainwindow.connect_signals(signals)
32
33 # Create GStreamer bits and bobs
34
35 # Initiate Gstreamer
36 Gst.init(None)
37
38 # Some capabilities we want to share between different elements
39 jpegcaps = Gst.Caps.from_string("image/jpeg,width=1280,height=720,framerate=30/1")
40 xrawcaps = Gst.Caps.from_string("video/x-raw,width=1280,height=720,framerate=30/1")
41
42 # Set up the pipeline
43 self.pipeline = Gst.Pipeline()
44
45 # Add elements
46
47 # src1
48 self.src1 = Gst.ElementFactory.make("v4l2src", "src1")
49 self.src1.set_property("device", "/dev/video0")
50 self.src1caps = Gst.ElementFactory.make("capsfilter", "src1caps")
51 self.src1caps.set_property("caps", jpegcaps)
52 self.src1dec = Gst.ElementFactory.make("decodebin", "src1dec")
53
54 # src2
55 self.src2 = Gst.ElementFactory.make("v4l2src", "src2")
56 self.src2.set_property("device", "/dev/video1")
57 self.src2caps = Gst.ElementFactory.make("capsfilter", "src2caps")
58 self.src2caps.set_property("caps", jpegcaps)
59 self.src2dec = Gst.ElementFactory.make("decodebin", "src2dec")
60
61 # src3
62 self.src3 = Gst.ElementFactory.make("videotestsrc", "src3")
63 self.src3caps = Gst.ElementFactory.make("capsfilter", "src3caps")
64 self.src3caps.set_property("caps", xrawcaps)
65 self.src3dec = Gst.ElementFactory.make("decodebin", "src3dec")
66
67 # Video mix
68 self.videomix = Gst.ElementFactory.make("videomixer", "videomix")
69 self.src1pad = self.videomix.get_request_pad("sink_1")
70 self.src1pad.set_property("zorder", 3)
71 self.src2pad = self.videomix.get_request_pad("sink_2")
72 self.src2pad.set_property("zorder", 2)
73 self.src3pad = self.videomix.get_request_pad("sink_3")
74 self.src3pad.set_property("zorder", 1)
75
76 # Add main videosink
77 self.videosink = Gst.ElementFactory.make("xvimagesink", "videosink")
78 self.videosink.set_property("sync", False)
79
80 # Add initial elements to the pipeline
81 self.pipeline.add(self.src1)
82 self.pipeline.add(self.src1caps)
83 self.pipeline.add(self.src1dec)
84 self.src1dec.connect("pad-added", self.OnPadAdded, self.src1pad)
85
86 self.pipeline.add(self.src2)
87 self.pipeline.add(self.src2caps)
88 self.pipeline.add(self.src2dec)
89 self.src2dec.connect("pad-added", self.OnPadAdded, self.src2pad)
90
91 self.pipeline.add(self.src3)
92 self.pipeline.add(self.src3caps)
93 self.pipeline.add(self.src3dec)
94 self.src3dec.connect("pad-added", self.OnPadAdded, self.src3pad)
95
96 self.pipeline.add(self.videomix)
97 self.pipeline.add(self.videosink)
98
99 # Link the initial pipeline
100 if not self.src1.link(self.src1caps):
101 print("Failed to link src1 to src1caps")
102 if not self.src1caps.link(self.src1dec):
103 print("Failed to link src1caps to src1dec")
104 if not self.videomix.link(self.videosink):
105 print("Failed to link videomix to videosink")
106
107 if not self.src2.link(self.src2caps):
108 print("Failed to link src2 to src2caps")
109 if not self.src2caps.link(self.src2dec):
110 print("Failed to link src2caps to src2dec")
111
112 if not self.src3.link(self.src3caps):
113 print("Failed to link src3 to src3caps")
114 if not self.src3caps.link(self.src3dec):
115 print("Failed to link src3caps to src3dec")
116
117 # Set up a bus to our pipeline to get notified when the video is ready
118 self.bus = self.pipeline.get_bus()
119 self.bus.enable_sync_message_emission()
120 self.bus.connect("sync-message::element", self.OnSyncElement)
121
122 # Summon the window and connect the window's close button to quit
123 self.window = self.mainwindow.get_object("mainwindow")
124 self.window.connect("delete-event", Gtk.main_quit)
125 self.window.show_all()
126
127 # Get window ID of the viewport widget from the GUI
128 self.win_id = self.mainwindow.get_object("viewport_main").get_window().get_xid()
129
130 # Play!
131 print(self.pipeline.set_state(Gst.State.READY))
132 print(self.pipeline.set_state(Gst.State.PLAYING))
133 self.pipeline.get_state
134
135
136 # When we get a message that video is ready to display, set the
137 # correct window id to hook it to our viewport
138 def OnSyncElement(self, bus, message):
139 if message.get_structure().get_name() == "prepare-window-handle":
140 print("prepare-window-handle")
141 message.src.set_window_handle(self.win_id)
142
143 def OnPadAdded(self, element, pad, sink):
144 print("Dynamic pad added")
145 print(pad.link(sink))
146 print(element.link(self.videomix))
147
148 def SetInput1(self, widget):
149 print("Switch to input 1")
150 self.src1pad.set_property("zorder", 3)
151 self.src2pad.set_property("zorder", 2)
152 self.src3pad.set_property("zorder", 1)
153
154 def SetInput2(self, widget):
155 print("Switch to input 2")
156 self.src1pad.set_property("zorder", 2)
157 self.src2pad.set_property("zorder", 3)
158 self.src3pad.set_property("zorder", 1)
159
160 def SetInput3(self, widget):
161 print("Switch to input 3")
162 self.src1pad.set_property("zorder", 1)
163 self.src2pad.set_property("zorder", 2)
164 self.src3pad.set_property("zorder", 3)
165
166 def SetInput4(self, widget):
167 print("Switch to input 4")
168
169 def SetInput5(self, widget):
170 print("Switch to input 5")
171
172 def SetInput6(self, widget):
173 print("Switch to input 6")
174
175 def OnQuit(self, widget):
176 print("quit")
177 Gtk.main_quit()
178
179 # Workaround to get Ctrl+C to terminate from command line
180 # ref: https://bugzilla.gnome.org/show_bug.cgi?id=622084#c12
181 signal.signal(signal.SIGINT, signal.SIG_DFL)
182
183 start = Main()
184 Gtk.main()