python: update python examples to modern python, Gst, and Glib versions master
authorAntonio Ospite <ao2@ao2.it>
Tue, 28 Sep 2021 21:27:43 +0000 (23:27 +0200)
committerAntonio Ospite <ao2@ao2.it>
Tue, 28 Sep 2021 21:27:43 +0000 (23:27 +0200)
12 files changed:
python/gst-custom-player.py
python/gst-decoupled-pipelines.py
python/gst-input-selector-switch.py
python/gst-looping-video-1.py
python/gst-looping-video-2.py
python/gst-playbin-switch.py
python/gst-player-example.py
python/gst-test-perspective.py
python/gst-trick-mode-looping-1.py
python/gst-trick-mode-looping-2.py
python/gst-trick-mode-looping-3.py
python/gst-trick-mode.py

index c95434d..93cb548 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 
 # Simple media player with GStreamer
 #
@@ -29,8 +29,8 @@ gi.require_version('Gst', '1.0')
 from gi.repository import Gst
 Gst.init(None)
 
+from gi.repository import GLib
 from gi.repository import GObject
-GObject.threads_init()
 
 # The player window will have a fixed width and height.
 # This is just to demonstrate the use of capabilities.
@@ -49,7 +49,7 @@ class CustomVideoBin(Gst.Bin):
         self.add(rescale)
         queue.link(rescale)
 
-        caps = Gst.Caps("video/x-raw-yuv,format=(fourcc)AYUV,width=%d,height=%d,pixel-aspect-ratio=1/1" % (WIDTH, HEIGHT))
+        caps = Gst.Caps("video/x-raw,format=(fourcc)AYUV,width=%d,height=%d,pixel-aspect-ratio=1/1" % (WIDTH, HEIGHT))
         capsfilter = Gst.ElementFactory.make("capsfilter", "filter")
         capsfilter.set_property("caps", caps)
         self.add(capsfilter)
@@ -88,21 +88,21 @@ class CustomAudioBin(Gst.Bin):
 
 class CustomPlayBin(Gst.Pipeline):
     __gproperties__ = {
-        'source': (Gst.Element, "source", "Source element", GObject.PARAM_READABLE)
+        'source': (Gst.Element, "source", "Source element", GObject.ParamFlags.READABLE)
     }
 
     def __init__(self, uri=None):
         Gst.Pipeline.__init__(self, 'CustomPlayBin')
 
-        self._uri = uri
-
         self._playbin = Gst.ElementFactory.make("playbin", "playbin")
         self.add(self._playbin)
 
-        self._playbin.set_property("uri", self._uri)
         self._playbin.set_property("video-sink", CustomVideoBin())
         self._playbin.set_property("audio-sink", CustomAudioBin())
 
+        if uri:
+            self.set_uri(uri)
+
     def set_uri(self, uri):
         self._uri = uri
         self._playbin.set_property("uri", self._uri)
@@ -124,24 +124,24 @@ class GstPlayer:
         bus.connect('message::state-changed', self.on_state_changed)
 
     def on_eos(self, bus, msg):
-        print 'on_eos'
+        print('on_eos')
         self.stop()
         if self.eos_cb:
             self.eos_cb()
 
     def on_tag(self, bus, msg):
-        print 'on_tag:'
+        print('on_tag:')
         taglist = msg.parse_tag()
-        print '\t', taglist.to_string()
+        print('\t', taglist.to_string())
 
     def on_error(self, bus, msg):
-        print 'on_error'
+        print('on_error')
         error, debug = msg.parse_error()
-        print "Error: %s" % error, debug
+        print("Error: %s" % error, debug)
         self.stop()
 
     def on_state_changed(self, bus, msg):
-        print 'on_state_changed'
+        print('on_state_changed')
         if msg.src != self.pipeline:
             return
 
@@ -166,12 +166,12 @@ class PlayerTUI():
         self.player = GstPlayer()
         self.player.eos_cb = self.quit
 
-        self.mainloop = GObject.MainLoop()
+        self.mainloop = GLib.MainLoop()
 
         self.player.set_location(location)
         self.player.play()
 
-        GObject.io_add_watch(sys.stdin, GObject.IO_IN, self.on_stdin)
+        GLib.io_add_watch(sys.stdin, GLib.IO_IN, self.on_stdin)
 
         try:
             self.mainloop.run()
index 25a95b7..50ff31c 100755 (executable)
@@ -9,8 +9,7 @@ gi.require_version('Gst', '1.0')
 from gi.repository import Gst
 Gst.init(None)
 
-from gi.repository import GObject
-GObject.threads_init()
+from gi.repository import GLib
 
 
 PIPELINE_SRC = """
@@ -28,7 +27,7 @@ intervideosrc timeout=-1 ! videoconvert ! rotate name=rotate ! videoconvert ! au
 
 class Player:
     def __init__(self):
-        self.loop = GObject.MainLoop()
+        self.loop = GLib.MainLoop()
         self.pipeline_src = Gst.parse_launch(PIPELINE_SRC)
         self.pipeline_sink = Gst.parse_launch(PIPELINE_SINK)
 
@@ -94,8 +93,8 @@ def main():
         player.on_rotate()
         return True
 
-    GObject.io_add_watch(sys.stdin, GObject.IO_IN, stdin_cb)
-    GObject.timeout_add(100, timeout_cb)
+    GLib.io_add_watch(sys.stdin, GLib.IO_IN, stdin_cb)
+    GLib.timeout_add(100, timeout_cb)
 
     print("\nPress Enter to freeze the video\n")
     player.run()
index 4f622c6..db4754e 100755 (executable)
@@ -7,8 +7,7 @@ gi.require_version('Gst', '1.0')
 from gi.repository import Gst
 Gst.init(None)
 
-from gi.repository import GObject
-GObject.threads_init()
+from gi.repository import GLib
 
 
 # The following pipeline works
@@ -21,7 +20,7 @@ input-selector name=selector ! autovideosink
 
 class Player:
     def __init__(self):
-        self.loop = GObject.MainLoop()
+        self.loop = GLib.MainLoop()
         self.pipeline = Gst.parse_launch(PIPELINE)
         self.selector = self.pipeline.get_by_name('selector')
 
@@ -79,7 +78,7 @@ def main():
         player.on_switch()
         return True
 
-    GObject.io_add_watch(sys.stdin, GObject.IO_IN, stdin_cb)
+    GLib.io_add_watch(sys.stdin, GLib.IO_IN, stdin_cb)
 
     print("\nPress Enter to switch the source\n")
     player.run()
index 7d1cc3f..7d8bd01 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 #
 # A simple looping player.
 # Version 1, based on EOS handling.
@@ -13,8 +13,7 @@ gi.require_version('Gst', '1.0')
 from gi.repository import Gst
 Gst.init(None)
 
-from gi.repository import GObject
-GObject.threads_init()
+from gi.repository import GLib
 
 
 class Player:
@@ -29,7 +28,7 @@ class Player:
 
     def run(self):
         self._player.set_state(Gst.State.PLAYING)
-        self.loop = GObject.MainLoop()
+        self.loop = GLib.MainLoop()
         self.loop.run()
 
     def quit(self):
@@ -42,7 +41,7 @@ class Player:
 
     def on_error(self, bus, msg):
         (err, debug) = msg.parse_error()
-        print "Error: %s" % err, debug
+        print("Error: %s" % err, debug)
         self.quit()
 
 
index 074dbda..2976906 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 #
 # A simple looping player.
 # Version 2, based on "about-to-finish" handling.
@@ -13,14 +13,13 @@ gi.require_version('Gst', '1.0')
 from gi.repository import Gst
 Gst.init(None)
 
-from gi.repository import GObject
-GObject.threads_init()
+from gi.repository import GLib
 
 
 class Player:
     def __init__(self, uri):
         self._uri = uri
-        self._player = Gst.ElementFactory.make("playbin", "player")
+        self._player = Gst.ElementFactory.make("playbin3", "player")
         self._player.set_property("uri", uri)
         self._player.connect("about-to-finish", self.on_about_to_finish)
 
@@ -30,7 +29,7 @@ class Player:
 
     def run(self):
         self._player.set_state(Gst.State.PLAYING)
-        self.loop = GObject.MainLoop()
+        self.loop = GLib.MainLoop()
         self.loop.run()
 
     def quit(self):
@@ -43,7 +42,7 @@ class Player:
 
     def on_error(self, bus, msg):
         (err, debug) = msg.parse_error()
-        print "Error: %s" % err, debug
+        print("Error: %s" % err, debug)
         self.quit()
 
 
index 85be7f2..0128a94 100755 (executable)
@@ -24,14 +24,13 @@ gi.require_version('Gst', '1.0')
 from gi.repository import Gst
 Gst.init(None)
 
-from gi.repository import GObject
-GObject.threads_init()
+from gi.repository import GLib
 
 
 class Player:
     def __init__(self):
-        self.loop = GObject.MainLoop()
-        self.pipeline = Gst.ElementFactory.make("playbin", "player")
+        self.loop = GLib.MainLoop()
+        self.pipeline = Gst.ElementFactory.make("playbin3", "player")
 
         files = ["sample_440hz.webm", "sample_880hz.webm"]
         self.uris = [Gst.filename_to_uri(f) for f in files]
@@ -97,7 +96,7 @@ def main():
         player.on_switch()
         return True
 
-    GObject.io_add_watch(sys.stdin, GObject.IO_IN, stdin_cb)
+    GLib.io_add_watch(sys.stdin, GLib.IO_IN, stdin_cb)
 
     print("\nPress Enter to switch the source\n")
     player.run()
index b7b838e..0ef074f 100755 (executable)
@@ -27,8 +27,7 @@ Gst.init(None)
 gi.require_version('GstPlayer', '1.0')
 from gi.repository import GstPlayer
 
-from gi.repository import GObject
-GObject.threads_init()
+from gi.repository import GLib
 
 
 class Player(object):
@@ -38,7 +37,7 @@ class Player(object):
         self.player.connect("end-of-stream", self.end_of_stream_cb)
         self.player.connect("state-changed", self.state_changed_cb)
 
-        self.mainloop = GObject.MainLoop()
+        self.mainloop = GLib.MainLoop()
 
     def end_of_stream_cb(self, player):
         self.mainloop.quit()
index 1b094c2..e40217d 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 
 # test program for the "perspective" geometric transform element
 
@@ -11,8 +11,8 @@ gi.require_version('Gst', '1.0')
 from gi.repository import Gst
 Gst.init(None)
 
+from gi.repository import GLib
 from gi.repository import GObject
-GObject.threads_init()
 
 
 def calc_matrix():
@@ -57,15 +57,18 @@ def main():
     perspective = Gst.ElementFactory.make("perspective", None)
     pipeline.add(perspective)
 
-    print perspective.get_property("matrix")
+    print(perspective.get_property("matrix"))
 
     M = calc_rotation_matrix()
-    perspective.set_property("matrix", M)
-    print perspective.get_property("matrix")
+    M2 = GObject.ValueArray()
+    for v in M:
+        M2.append(float(v))
+    perspective.set_property("matrix", M2)
+    print(perspective.get_property("matrix"))
 
     # This should fail!
     perspective.set_property("matrix", [0])
-    print perspective.get_property("matrix")
+    print(perspective.get_property("matrix"))
 
     videoconvert = Gst.ElementFactory.make("autovideoconvert", None)
     pipeline.add(videoconvert)
@@ -82,7 +85,7 @@ def main():
 
     pipeline.set_state(Gst.State.PLAYING)
 
-    loop = GObject.MainLoop()
+    loop = GLib.MainLoop()
     loop.run()
 
 
index 5a14848..9d4ecbd 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 #
 # A simple "trick-mode" looping player.
 # Version 1, based on EOS handling.
@@ -13,8 +13,7 @@ gi.require_version('Gst', '1.0')
 from gi.repository import Gst
 Gst.init(None)
 
-from gi.repository import GObject
-GObject.threads_init()
+from gi.repository import GLib
 
 
 class Player:
@@ -32,7 +31,7 @@ class Player:
 
     def run(self):
         self._player.set_state(Gst.State.PLAYING)
-        self.loop = GObject.MainLoop()
+        self.loop = GLib.MainLoop()
         self.loop.run()
 
     def quit(self):
@@ -45,16 +44,16 @@ class Player:
 
     def on_error(self, bus, msg):
         (err, debug) = msg.parse_error()
-        print "Error: %s" % err, debug
+        print("Error: %s" % err, debug)
         self.quit()
 
     def on_state_changed(self, bus, msg):
         if msg.src != self._player:
             return
 
-        print 'on_state_changed'
+        print('on_state_changed')
         old_state, new_state, pending = msg.parse_state_changed()
-        print "%s -> %s" % (old_state, new_state)
+        print("%s -> %s" % (old_state, new_state))
         if old_state == Gst.State.READY and new_state == Gst.State.PAUSED:
             self.set_rate(self._rate)
 
index b3487dc..a398331 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 #
 # A simple "trick-mode" looping player.
 # Version 2, based on "about-to-finish" handling.
@@ -15,8 +15,7 @@ gi.require_version('Gst', '1.0')
 from gi.repository import Gst
 Gst.init(None)
 
-from gi.repository import GObject
-GObject.threads_init()
+from gi.repository import GLib
 
 
 class Player:
@@ -24,7 +23,7 @@ class Player:
         self._uri = uri
         self._rate = rate
 
-        self._player = Gst.ElementFactory.make("playbin", "player")
+        self._player = Gst.ElementFactory.make("playbin3", "player")
         self._player.set_property("uri", uri)
         self._player.connect("about-to-finish", self.on_about_to_finish)
 
@@ -35,7 +34,7 @@ class Player:
 
     def run(self):
         self._player.set_state(Gst.State.PLAYING)
-        self.loop = GObject.MainLoop()
+        self.loop = GLib.MainLoop()
         self.loop.run()
 
     def quit(self):
@@ -54,16 +53,16 @@ class Player:
 
     def on_error(self, bus, msg):
         (err, debug) = msg.parse_error()
-        print "Error: %s" % err, debug
+        print("Error: %s" % err, debug)
         self.quit()
 
     def on_state_changed(self, bus, msg):
         if msg.src != self._player:
             return
 
-        print 'on_state_changed'
+        print('on_state_changed')
         old_state, new_state, pending = msg.parse_state_changed()
-        print "%s -> %s" % (old_state, new_state)
+        print("%s -> %s" % (old_state, new_state))
         if old_state == Gst.State.READY and new_state == Gst.State.PAUSED:
             self.set_rate(self._rate)
 
index 1fef9d3..cb9d8da 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 #
 # A simple "trick-mode" looping player.
 # Version 3, based on segment seeking.
@@ -16,8 +16,7 @@ gi.require_version('Gst', '1.0')
 from gi.repository import Gst
 Gst.init(None)
 
-from gi.repository import GObject
-GObject.threads_init()
+from gi.repository import GLib
 
 
 class Player:
@@ -35,7 +34,7 @@ class Player:
 
     def run(self):
         self._player.set_state(Gst.State.PLAYING)
-        self.loop = GObject.MainLoop()
+        self.loop = GLib.MainLoop()
         self.loop.run()
 
     def quit(self):
@@ -47,16 +46,16 @@ class Player:
 
     def on_error(self, bus, msg):
         (err, debug) = msg.parse_error()
-        print "Error: %s" % err, debug
+        print("Error: %s" % err, debug)
         self.quit()
 
     def on_state_changed(self, bus, msg):
         if msg.src != self._player:
             return
 
-        print 'on_state_changed'
+        print('on_state_changed')
         old_state, new_state, pending = msg.parse_state_changed()
-        print "%s -> %s" % (old_state, new_state)
+        print("%s -> %s" % (old_state, new_state))
         if old_state == Gst.State.READY and new_state == Gst.State.PAUSED:
             self.set_rate(self._rate)
 
index cc0e7a6..5ae62b6 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 #
 # A simple "trick-mode" player to play a file at a given speed rate.
 #
@@ -12,8 +12,7 @@ gi.require_version('Gst', '1.0')
 from gi.repository import Gst
 Gst.init(None)
 
-from gi.repository import GObject
-GObject.threads_init()
+from gi.repository import GLib
 
 
 class Player:
@@ -31,7 +30,7 @@ class Player:
 
     def run(self):
         self._player.set_state(Gst.State.PLAYING)
-        self.loop = GObject.MainLoop()
+        self.loop = GLib.MainLoop()
         self.loop.run()
 
     def quit(self):
@@ -43,16 +42,16 @@ class Player:
 
     def on_error(self, bus, msg):
         (err, debug) = msg.parse_error()
-        print "Error: %s" % err, debug
+        print("Error: %s" % err, debug)
         self.quit()
 
     def on_state_changed(self, bus, msg):
         if msg.src != self._player:
             return
 
-        print 'on_state_changed'
+        print('on_state_changed')
         old_state, new_state, pending = msg.parse_state_changed()
-        print "%s -> %s" % (old_state, new_state)
+        print("%s -> %s" % (old_state, new_state))
         if old_state == Gst.State.READY and new_state == Gst.State.PAUSED:
             self.set_rate(self._rate)