python: update python examples to modern python, Gst, and Glib versions
[experiments/gstreamer.git] / python / gst-custom-player.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()