2 # Let's see if we can get a viewport AND audio working, combining ex. 2 and 3
4 # GdkX11 to get access to xid, GstVideo to get access to set_window_handle
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
14 # Create gui bits and bobs
16 self
.mainwindow
= Gtk
.Builder()
17 self
.mainwindow
.add_from_file("example3.glade")
20 "on_play_clicked" : self
.OnPlay
,
21 "on_stop_clicked" : self
.OnStop
,
22 "on_quit_clicked" : self
.OnQuit
,
25 self
.mainwindow
.connect_signals(signals
)
27 # Create GStreamer bits and bobs
29 # Initiate the pipeline
31 self
.pipeline
= Gst
.Pipeline()
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
)
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
)
43 # Link the capsfilter to the videotestsrc
44 self
.videotestsrc
.link(self
.videotestcaps
)
46 # Add a videosink element to the pipeline
47 self
.videosink
= Gst
.ElementFactory
.make("autovideosink", "videosink")
48 self
.pipeline
.add(self
.videosink
)
50 # Link the already linked videotestcaps to the sink
51 self
.videotestcaps
.link(self
.videosink
)
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
)
58 # Add a pulsesink element to the pipeline
59 self
.pulsesink
= Gst
.ElementFactory
.make("pulsesink", "sink")
60 self
.pipeline
.add(self
.pulsesink
)
62 # Link the two elements together
63 self
.audiotestsrc
.link(self
.pulsesink
)
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
)
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()
75 # Get window ID of the viewport widget from the GUI
76 self
.win_id
= self
.mainwindow
.get_object("viewport").get_window().get_xid()
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
)
86 def OnPlay(self
, widget
):
88 self
.pipeline
.set_state(Gst
.State
.PLAYING
)
90 def OnStop(self
, widget
):
92 self
.pipeline
.set_state(Gst
.State
.READY
)
94 def OnQuit(self
, widget
):
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
)