Removed Gtk dependency for example1 (pure CLI)
[python-gstreamer-examples] / example2+3.py
index d203020205698866099d8f4e9e53218ee18e4684..9a39c4e6048d7f35115a764a5e29659cb1d80de3 100755 (executable)
@@ -2,17 +2,21 @@
 # Let's see if we can get a viewport AND audio working, combining ex. 2 and 3
 
 # GdkX11 to get access to xid, GstVideo to get access to set_window_handle
 # Let's see if we can get a viewport AND audio working, combining ex. 2 and 3
 
 # GdkX11 to get access to xid, GstVideo to get access to set_window_handle
+import gi
+gi.require_version('Gtk', '3.0')
+gi.require_version('Gst', '1.0')
+gi.require_version('GstVideo', '1.0')
 from gi.repository import Gtk, Gst, GdkX11, GstVideo
 import signal
 
 class Main:
     def __init__(self):
 from gi.repository import Gtk, Gst, GdkX11, GstVideo
 import signal
 
 class Main:
     def __init__(self):
-        
+
         # Create gui bits and bobs
 
         self.mainwindow = Gtk.Builder()
         self.mainwindow.add_from_file("example3.glade")
         # Create gui bits and bobs
 
         self.mainwindow = Gtk.Builder()
         self.mainwindow.add_from_file("example3.glade")
-        
+
         signals = {
             "on_play_clicked" : self.OnPlay,
             "on_stop_clicked" : self.OnStop,
         signals = {
             "on_play_clicked" : self.OnPlay,
             "on_stop_clicked" : self.OnStop,
@@ -25,18 +29,18 @@ class Main:
 
         # Initiate the pipeline
         Gst.init(None)
 
         # Initiate the pipeline
         Gst.init(None)
-        self.pipeline = Gst.Pipeline("mypipeline")
+        self.pipeline = Gst.Pipeline()
 
         # Add a videotestsrc element to the pipeline, set it to pattern "snow."
         self.videotestsrc = Gst.ElementFactory.make("videotestsrc", "videosource")
         self.videotestsrc.set_property("pattern", "snow")
         self.pipeline.add(self.videotestsrc)
 
         # Add a videotestsrc element to the pipeline, set it to pattern "snow."
         self.videotestsrc = Gst.ElementFactory.make("videotestsrc", "videosource")
         self.videotestsrc.set_property("pattern", "snow")
         self.pipeline.add(self.videotestsrc)
-        
+
         # Add a capsfilter that we want to apply to our videotestsrc
         self.videotestcaps = Gst.ElementFactory.make("capsfilter", "videotestcaps")
         self.videotestcaps.set_property("caps",Gst.Caps.from_string("video/x-raw,width=640,height=480"))
         self.pipeline.add(self.videotestcaps)
         # Add a capsfilter that we want to apply to our videotestsrc
         self.videotestcaps = Gst.ElementFactory.make("capsfilter", "videotestcaps")
         self.videotestcaps.set_property("caps",Gst.Caps.from_string("video/x-raw,width=640,height=480"))
         self.pipeline.add(self.videotestcaps)
-        
+
         # Link the capsfilter to the videotestsrc
         self.videotestsrc.link(self.videotestcaps)
 
         # Link the capsfilter to the videotestsrc
         self.videotestsrc.link(self.videotestcaps)
 
@@ -58,38 +62,38 @@ class Main:
 
         # Link the two elements together
         self.audiotestsrc.link(self.pulsesink)
 
         # Link the two elements together
         self.audiotestsrc.link(self.pulsesink)
-        
+
         # Set up a bus to our pipeline to get notified when the video is ready
         self.bus = self.pipeline.get_bus()
         self.bus.enable_sync_message_emission()
         self.bus.connect("sync-message::element", self.OnSyncElement)
         # Set up a bus to our pipeline to get notified when the video is ready
         self.bus = self.pipeline.get_bus()
         self.bus.enable_sync_message_emission()
         self.bus.connect("sync-message::element", self.OnSyncElement)
-        
+
         # Summon the window and connect the window's close button to quit
         self.window = self.mainwindow.get_object("mainwindow")
         self.window.connect("delete-event", Gtk.main_quit)
         self.window.show_all()
         # Summon the window and connect the window's close button to quit
         self.window = self.mainwindow.get_object("mainwindow")
         self.window.connect("delete-event", Gtk.main_quit)
         self.window.show_all()
-        
+
         # Get window ID of the viewport widget from the GUI
         self.win_id = self.mainwindow.get_object("viewport").get_window().get_xid()
 
 
         # Get window ID of the viewport widget from the GUI
         self.win_id = self.mainwindow.get_object("viewport").get_window().get_xid()
 
 
-    # When we get a message that video is ready to display, set the 
+    # When we get a message that video is ready to display, set the
     # correct window id to hook it to our viewport
     def OnSyncElement(self, bus, message):
         if message.get_structure().get_name() == "prepare-window-handle":
     # correct window id to hook it to our viewport
     def OnSyncElement(self, bus, message):
         if message.get_structure().get_name() == "prepare-window-handle":
-            print "prepare-window-handle"
+            print("prepare-window-handle")
             message.src.set_window_handle(self.win_id)
 
     def OnPlay(self, widget):
             message.src.set_window_handle(self.win_id)
 
     def OnPlay(self, widget):
-        print "play"
+        print("play")
         self.pipeline.set_state(Gst.State.PLAYING)
 
     def OnStop(self, widget):
         self.pipeline.set_state(Gst.State.PLAYING)
 
     def OnStop(self, widget):
-        print "stop"
+        print("stop")
         self.pipeline.set_state(Gst.State.READY)
         self.pipeline.set_state(Gst.State.READY)
-        
+
     def OnQuit(self, widget):
     def OnQuit(self, widget):
-        print "quit"
+        print("quit")
         Gtk.main_quit()
 
     # Workaround to get Ctrl+C to terminate from command line
         Gtk.main_quit()
 
     # Workaround to get Ctrl+C to terminate from command line