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