gst-test-perspective.py: fix a typo
[experiments/gstreamer.git] / python / gst-custom-player.py
index cbeace2..c95434d 100755 (executable)
@@ -2,7 +2,7 @@
 
 # Simple media player with GStreamer
 #
-# Copyright (C) 2013  Antonio Ospite <ospite@studenti.unina.it>
+# Copyright (C) 2013-2014  Antonio Ospite <ao2@ao2.it>
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 import sys
 import os
 
-import gobject
-gobject.threads_init()
+import gi
+gi.require_version('Gst', '1.0')
+from gi.repository import Gst
+Gst.init(None)
 
-import gst
+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.
@@ -35,61 +38,65 @@ WIDTH = 640
 HEIGHT = 300
 
 
-class CustomVideoBin(gst.Bin):
+class CustomVideoBin(Gst.Bin):
     def __init__(self):
-        gst.Bin.__init__(self, 'CustomVideoBin')
+        Gst.Bin.__init__(self, 'CustomVideoBin')
 
-        queue = gst.element_factory_make('queue', 'vqueue')
+        queue = Gst.ElementFactory.make('queue', 'vqueue')
         self.add(queue)
 
-        rescale = gst.element_factory_make("videoscale", "rescale")
+        rescale = Gst.ElementFactory.make("videoscale", "rescale")
         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))
-        capsfilter = gst.element_factory_make("capsfilter", "filter")
+        caps = Gst.Caps("video/x-raw-yuv,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)
+        rescale.link(capsfilter)
 
-        colorspace = gst.element_factory_make("colorspace", "colorspace")
-        self.add(colorspace)
+        videoconvert = Gst.ElementFactory.make("videoconvert", "videoconvert")
+        self.add(videoconvert)
+        capsfilter.link(videoconvert)
 
-        videosink = gst.element_factory_make("autovideosink", "vidoesink")
+        videosink = Gst.ElementFactory.make("autovideosink", "vidoesink")
         self.add(videosink)
+        videoconvert.link(videosink)
 
-        gst.element_link_many(queue, rescale, capsfilter, colorspace, videosink)
-        sink = queue.get_pad('sink')
-        self.add_pad(gst.GhostPad('sink', sink))
+        sink = queue.get_static_pad('sink')
+        self.add_pad(Gst.GhostPad('sink', sink))
 
 
-class CustomAudioBin(gst.Bin):
+class CustomAudioBin(Gst.Bin):
     def __init__(self):
-        gst.Bin.__init__(self, 'CustomAudioBin')
+        Gst.Bin.__init__(self, 'CustomAudioBin')
 
-        queue = gst.element_factory_make('queue', 'aqueue')
+        queue = Gst.ElementFactory.make('queue', 'aqueue')
         self.add(queue)
 
-        audioconvert = gst.element_factory_make("audioconvert", "audioconverter")
+        audioconvert = Gst.ElementFactory.make("audioconvert", "audioconverter")
         self.add(audioconvert)
+        queue.link(audioconvert)
 
-        audiosink = gst.element_factory_make("autoaudiosink", "audiosink")
+        audiosink = Gst.ElementFactory.make("autoaudiosink", "audiosink")
         self.add(audiosink)
+        audioconvert.link(audiosink)
 
-        gst.element_link_many(queue, audioconvert, audiosink)
-        sink = queue.get_pad('sink')
-        self.add_pad(gst.GhostPad('sink', sink))
+        sink = queue.get_static_pad('sink')
+        self.add_pad(Gst.GhostPad('sink', sink))
 
 
-class CustomPlayBin(gst.Pipeline):
+class CustomPlayBin(Gst.Pipeline):
     __gproperties__ = {
-        'source': (gst.Element, "source", "Source element", gobject.PARAM_READABLE)
+        'source': (Gst.Element, "source", "Source element", GObject.PARAM_READABLE)
     }
 
     def __init__(self, uri=None):
-        gst.Pipeline.__init__(self, 'CustomPlayBin')
+        Gst.Pipeline.__init__(self, 'CustomPlayBin')
 
         self._uri = uri
 
-        self._playbin = gst.element_factory_make("playbin2", "playbin")
+        self._playbin = Gst.ElementFactory.make("playbin", "playbin")
         self.add(self._playbin)
 
         self._playbin.set_property("uri", self._uri)
@@ -125,8 +132,7 @@ class GstPlayer:
     def on_tag(self, bus, msg):
         print 'on_tag:'
         taglist = msg.parse_tag()
-        for key in taglist.keys():
-            print '\t%s = %s' % (key, taglist[key])
+        print '\t', taglist.to_string()
 
     def on_error(self, bus, msg):
         print 'on_error'
@@ -145,13 +151,13 @@ class GstPlayer:
         self.pipeline.set_uri(location)
 
     def play(self):
-        self.pipeline.set_state(gst.STATE_PLAYING)
+        self.pipeline.set_state(Gst.State.PLAYING)
 
     def pause(self):
-        self.pipeline.set_state(gst.STATE_PAUSED)
+        self.pipeline.set_state(Gst.State.PAUSED)
 
     def stop(self):
-        self.pipeline.set_state(gst.STATE_NULL)
+        self.pipeline.set_state(Gst.State.NULL)
 
 
 class PlayerTUI():
@@ -160,12 +166,12 @@ class PlayerTUI():
         self.player = GstPlayer()
         self.player.eos_cb = self.quit
 
-        self.mainloop = gobject.MainLoop()
+        self.mainloop = GObject.MainLoop()
 
         self.player.set_location(location)
         self.player.play()
 
-        gobject.io_add_watch(sys.stdin, gobject.IO_IN, self.on_stdin)
+        GObject.io_add_watch(sys.stdin, GObject.IO_IN, self.on_stdin)
 
         try:
             self.mainloop.run()
@@ -199,11 +205,9 @@ def main(args):
         usage()
         sys.exit(1)
 
-    if not gst.uri_is_valid(args[1]):
-        sys.stderr.write("Error: Invalid URI: %s\n" % args[1])
-        sys.exit(1)
+    uri = Gst.filename_to_uri(args[1])
 
-    tui = PlayerTUI(args[1])
+    tui = PlayerTUI(uri)
 
 if __name__ == '__main__':
     sys.exit(main(sys.argv))