f5af59b29b05e6d6915fa254bb72e1deac1a9cce
2 # Let's see if we can get a viewport working, building on code from example2.py
3 # Some final info and updated code stolen from
4 # http://bazaar.launchpad.net/~jderose/+junk/gst-examples/view/head:/video-player-1.0
6 # GdkX11 to get access to xid, GstVideo to get access to set_window_handle
8 gi
.require_version('Gtk', '3.0')
9 gi
.require_version('Gst', '1.0')
10 gi
.require_version('GstVideo', '1.0')
11 from gi
.repository
import Gtk
, Gst
, GdkX11
, GstVideo
17 # Create gui bits and bobs
19 self
.mainwindow
= Gtk
.Builder()
20 self
.mainwindow
.add_from_file("example3.glade")
23 "on_play_clicked" : self
.OnPlay
,
24 "on_stop_clicked" : self
.OnStop
,
25 "on_quit_clicked" : self
.OnQuit
,
28 self
.mainwindow
.connect_signals(signals
)
30 # Create GStreamer bits and bobs
32 # Initiate the pipeline
34 self
.pipeline
= Gst
.Pipeline()
36 # Add a videotestsrc element to the pipeline, set it to pattern "snow."
37 self
.videotestsrc
= Gst
.ElementFactory
.make("videotestsrc", "videosource")
38 self
.videotestsrc
.set_property("pattern", "snow")
39 self
.pipeline
.add(self
.videotestsrc
)
41 # Add a capsfilter that we want to apply to our videotestsrc
42 self
.videotestcaps
= Gst
.ElementFactory
.make("capsfilter", "videotestcaps")
43 self
.videotestcaps
.set_property("caps",Gst
.Caps
.from_string("video/x-raw,width=640,height=480"))
44 self
.pipeline
.add(self
.videotestcaps
)
46 # Link the capsfilter to the videotestsrc
47 self
.videotestsrc
.link(self
.videotestcaps
)
49 # Add a videosink element to the pipeline
50 self
.videosink
= Gst
.ElementFactory
.make("autovideosink", "videosink")
51 self
.pipeline
.add(self
.videosink
)
53 # Link the already linked videotestcaps to the sink
54 self
.videotestcaps
.link(self
.videosink
)
56 # Set up a bus to our pipeline to get notified when the video is ready
57 self
.bus
= self
.pipeline
.get_bus()
58 self
.bus
.enable_sync_message_emission()
59 self
.bus
.connect("sync-message::element", self
.OnSyncElement
)
61 # Summon the window and connect the window's close button to quit
62 self
.window
= self
.mainwindow
.get_object("mainwindow")
63 self
.window
.connect("delete-event", Gtk
.main_quit
)
64 self
.window
.show_all()
66 # Get window ID of the viewport widget from the GUI
67 self
.win_id
= self
.mainwindow
.get_object("viewport").get_window().get_xid()
70 # When we get a message that video is ready to display, set the
71 # correct window id to hook it to our viewport
72 def OnSyncElement(self
, bus
, message
):
73 if message
.get_structure().get_name() == "prepare-window-handle":
74 print("prepare-window-handle")
75 message
.src
.set_window_handle(self
.win_id
)
77 def OnPlay(self
, widget
):
79 self
.pipeline
.set_state(Gst
.State
.PLAYING
)
81 def OnStop(self
, widget
):
83 self
.pipeline
.set_state(Gst
.State
.READY
)
85 def OnQuit(self
, widget
):
89 # Workaround to get Ctrl+C to terminate from command line
90 # ref: https://bugzilla.gnome.org/show_bug.cgi?id=622084#c12
91 signal
.signal(signal
.SIGINT
, signal
.SIG_DFL
)