fixed conflict
[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 import gi
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
12 import signal
13
14 class Main:
15 def __init__(self):
16
17 # Create gui bits and bobs
18
19 self.mainwindow = Gtk.Builder()
20 self.mainwindow.add_from_file("example3.glade")
21
22 signals = {
23 "on_play_clicked" : self.OnPlay,
24 "on_stop_clicked" : self.OnStop,
25 "on_quit_clicked" : self.OnQuit,
26 }
27
28 self.mainwindow.connect_signals(signals)
29
30 # Create GStreamer bits and bobs
31
32 # Initiate the pipeline
33 Gst.init(None)
34 self.pipeline = Gst.Pipeline()
35
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)
40
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)
45
46 # Link the capsfilter to the videotestsrc
47 self.videotestsrc.link(self.videotestcaps)
48
49 # Add a videosink element to the pipeline
50 self.videosink = Gst.ElementFactory.make("autovideosink", "videosink")
51 self.pipeline.add(self.videosink)
52
53 # Link the already linked videotestcaps to the sink
54 self.videotestcaps.link(self.videosink)
55
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)
60
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()
65
66 # Get window ID of the viewport widget from the GUI
67 self.win_id = self.mainwindow.get_object("viewport").get_window().get_xid()
68
69
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)
76
77 def OnPlay(self, widget):
78 print("play")
79 self.pipeline.set_state(Gst.State.PLAYING)
80
81 def OnStop(self, widget):
82 print("stop")
83 self.pipeline.set_state(Gst.State.READY)
84
85 def OnQuit(self, widget):
86 print("quit")
87 Gtk.main_quit()
88
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)
92
93 start = Main()
94 Gtk.main()