Advertisement
Biggie

Custom Gst.Element in GTK3 (not working)

Jul 16th, 2014
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.75 KB | None | 0 0
  1. from gi.repository import GLib
  2. from gi.repository import GObject
  3. from gi.repository import Gst
  4. from gi.repository import Gtk
  5.  
  6. # Needed for window.get_xid(), xvimagesink.set_window_handle(), respectively:
  7. from gi.repository import GdkX11, GstVideo
  8.  
  9. GObject.threads_init()
  10. Gst.init(None)
  11.  
  12.  
  13.  
  14. class TestFilter(Gst.Element):
  15.  
  16.     __gstmetadata__ = (
  17.         "TestFilter plugin",
  18.         "test_filter.py",
  19.         "Description",
  20.         "Contact"
  21.     )
  22.    
  23.     # src pad template: used to forward buffers
  24.     _srctemplate = Gst.PadTemplate.new('video_src',
  25.         Gst.PadDirection.SRC,
  26.         Gst.PadPresence.ALWAYS,
  27.         Gst.caps_from_string('video/x-raw, format=(string){ YV12, AYUV, YUY2, UYVY }')
  28.     )
  29.  
  30.     # sink pad template: used to receive buffers
  31.     _sinktemplate = Gst.PadTemplate.new('video_sink',
  32.         Gst.PadDirection.SINK,
  33.         Gst.PadPresence.ALWAYS,
  34.         Gst.caps_from_string('video/x-raw, format=(string){ YV12, AYUV, YUY2, UYVY }')
  35.     )
  36.    
  37.     # register pad templates
  38.     __gsttemplates__ = (_srctemplate, _sinktemplate)
  39.  
  40.     def __init__(self):  
  41.         Gst.Element.__init__(self)
  42.        
  43.         # src pad
  44.         self.srcpad = Gst.Pad.new_from_template(self._srctemplate, 'video_src')
  45.        
  46.         # sink pad
  47.         self.sinkpad = Gst.Pad.new_from_template(self._sinktemplate, 'video_sink')
  48.         self.sinkpad.set_chain_function_full(self._sink_chain)
  49.        
  50.         # add pads
  51.         self.add_pad(self.srcpad)
  52.         self.add_pad(self.sinkpad)
  53.    
  54.     def _sink_chain(self, pad, parent, buf):
  55.         # ... do analyzing stuff ...
  56.         # forward buffer
  57.         return self.srcpad.push(buf)
  58.  
  59.  
  60.  
  61. if __name__=='__main__':
  62.     def on_message(bus, message):
  63.         if message.type == Gst.MessageType.EOS:
  64.             player.set_state(Gst.State.NULL)
  65.         elif message.type == Gst.MessageType.ERROR:
  66.             player.set_state(Gst.State.NULL)
  67.             (err, debug) = message.parse_error()
  68.             print "Error: %s" % err, debug
  69.  
  70.     def on_sync_message(bus, message):
  71.         if message.get_structure().get_name() == "prepare-window-handle":
  72.             win_id = videowidget.get_property('window').get_xid()
  73.             imagesink = message.src
  74.             imagesink.set_property("force-aspect-ratio", True)
  75.             imagesink.set_window_handle(win_id)
  76.  
  77.     def on_delete(*args):
  78.         player.set_state(Gst.State.NULL)
  79.         Gtk.main_quit()
  80.  
  81.     win = Gtk.Window()
  82.     win.connect("delete_event", on_delete)
  83.     videowidget = Gtk.DrawingArea()
  84.     videowidget.set_size_request(400, 250)
  85.     vbox = Gtk.VBox(False, 0)
  86.     vbox.pack_start(videowidget, True, True, 0)
  87.     win.add(vbox)
  88.     win.show_all()
  89.  
  90.     # Setup GStreamer
  91.     player = Gst.ElementFactory.make("playbin", "MultimediaPlayer")
  92.     bus = player.get_bus()
  93.     bus.add_signal_watch()
  94.     bus.enable_sync_message_emission()
  95.     bus.connect("message", on_message)
  96.     bus.connect("sync-message::element", on_sync_message)
  97.  
  98.     # create a Bin containing my own TestFilter, textoverlay and autovideosink
  99.     testfilter = TestFilter()
  100.     textoverlay = Gst.ElementFactory.make('textoverlay', 'textoverlay')
  101.     autovideosink = Gst.ElementFactory.make('autovideosink')
  102.     video_bin = Gst.Bin.new('video_bin')
  103.     video_bin.add(testfilter)
  104.     video_bin.add(textoverlay)
  105.     video_bin.add(autovideosink)
  106.     sink_pad = Gst.GhostPad.new('sink', testfilter.get_static_pad('video_sink'))
  107.     video_bin.add_pad(sink_pad)
  108.     testfilter.link(textoverlay)
  109.     textoverlay.link(autovideosink)
  110.     player.set_property('video-sink', video_bin)
  111.  
  112.     textoverlay.set_property('text', 'ABC')
  113.  
  114.     player.set_property('uri', "file:///.../somefile.avi")
  115.     player.set_state(Gst.State.PLAYING)
  116.  
  117.     Gtk.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement