Even more cleanups, trivial stuff
[python-gstreamer-examples] / example2+3.py
1 #!/usr/bin/env python
2 # Let's see if we can get a viewport AND audio working, combining ex. 2 and 3
3
4 # GdkX11 to get access to xid, GstVideo to get access to set_window_handle
5 import gi, signal
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 class Main:
12 def __init__(self):
13
14 # Create gui bits and bobs
15
16 self.mainwindow = Gtk.Builder()
17 self.mainwindow.add_from_file("example3.glade")
18
19 signals = {
20 "on_play_clicked" : self.OnPlay,
21 "on_stop_clicked" : self.OnStop,
22 "on_quit_clicked" : self.OnQuit,
23 }
24
25 self.mainwindow.connect_signals(signals)
26
27 # Create GStreamer bits and bobs
28
29 # Initiate the pipeline
30 Gst.init(None)
31 self.pipeline = Gst.Pipeline()
32
33 # Add a videotestsrc element to the pipeline, set it to pattern "snow."
34 self.videotestsrc = Gst.ElementFactory.make("videotestsrc", "videosource")
35 self.videotestsrc.set_property("pattern", "snow")
36 self.pipeline.add(self.videotestsrc)
37
38 # Add a capsfilter that we want to apply to our videotestsrc
39 self.videotestcaps = Gst.ElementFactory.make("capsfilter", "videotestcaps")
40 self.videotestcaps.set_property("caps",Gst.Caps.from_string("video/x-raw,width=640,height=480"))
41 self.pipeline.add(self.videotestcaps)
42
43 # Link the capsfilter to the videotestsrc
44 self.videotestsrc.link(self.videotestcaps)
45
46 # Add a videosink element to the pipeline
47 self.videosink = Gst.ElementFactory.make("autovideosink", "videosink")
48 self.pipeline.add(self.videosink)
49
50 # Link the already linked videotestcaps to the sink
51 self.videotestcaps.link(self.videosink)
52
53 # Add an audiotestsrc element to the pipeline
54 self.audiotestsrc = Gst.ElementFactory.make("audiotestsrc", "audio")
55 self.audiotestsrc.set_property("freq", 800)
56 self.pipeline.add(self.audiotestsrc)
57
58 # Add a pulsesink element to the pipeline
59 self.pulsesink = Gst.ElementFactory.make("pulsesink", "sink")
60 self.pipeline.add(self.pulsesink)
61
62 # Link the two elements together
63 self.audiotestsrc.link(self.pulsesink)
64
65 # Set up a bus to our pipeline to get notified when the video is ready
66 self.bus = self.pipeline.get_bus()
67 self.bus.enable_sync_message_emission()
68 self.bus.connect("sync-message::element", self.OnSyncElement)
69
70 # Summon the window and connect the window's close button to quit
71 self.window = self.mainwindow.get_object("mainwindow")
72 self.window.connect("delete-event", Gtk.main_quit)
73 self.window.show_all()
74
75 # Get window ID of the viewport widget from the GUI
76 self.win_id = self.mainwindow.get_object("viewport").get_window().get_xid()
77
78
79 # When we get a message that video is ready to display, set the
80 # correct window id to hook it to our viewport
81 def OnSyncElement(self, bus, message):
82 if message.get_structure().get_name() == "prepare-window-handle":
83 print("prepare-window-handle")
84 message.src.set_window_handle(self.win_id)
85
86 def OnPlay(self, widget):
87 print("play")
88 self.pipeline.set_state(Gst.State.PLAYING)
89
90 def OnStop(self, widget):
91 print("stop")
92 self.pipeline.set_state(Gst.State.READY)
93
94 def OnQuit(self, widget):
95 print("quit")
96 Gtk.main_quit()
97
98 # Workaround to get Ctrl+C to terminate from command line
99 # ref: https://bugzilla.gnome.org/show_bug.cgi?id=622084#c12
100 signal.signal(signal.SIGINT, signal.SIG_DFL)
101
102 start = Main()
103 Gtk.main()