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