shakaran

Untitled

Jun 26th, 2012
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.01 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import gi
  4. gi.require_version('Gst', '1.0')
  5. from gi.repository import GObject, Gst, Gtk
  6.  
  7. # Needed for window.get_xid(), xvimagesink.set_window_handle(), respectively:
  8. from gi.repository import GdkX11, GstVideo
  9.  
  10.  
  11. GObject.threads_init()
  12. Gst.init(None)
  13.  
  14.  
  15. class Webcam:
  16.     def __init__(self):
  17.         self.window = Gtk.Window()
  18.         self.window.connect('destroy', self.quit)
  19.         self.window.set_default_size(800, 450)
  20.  
  21.         self.drawingarea = Gtk.DrawingArea()
  22.     self.window.add(self.drawingarea)
  23.  
  24.     self.window.show_all()
  25.    
  26.     self.xid = self.drawingarea.get_property('window').get_xid()
  27.  
  28.         # Create GStreamer pipeline
  29.         self.pipeline = Gst.Pipeline()
  30.  
  31.         # Create bus to get events from GStreamer pipeline
  32.         self.bus = self.pipeline.get_bus()
  33.         self.bus.add_signal_watch()
  34.         self.bus.connect('message::error', self.on_error)
  35.  
  36.         # This is needed to make the video output in our DrawingArea:
  37.         self.bus.enable_sync_message_emission()
  38.         self.bus.connect('sync-message::element', self.on_sync_message)
  39.  
  40.         # Create GStreamer elements
  41.         self.src = Gst.ElementFactory.make('autovideosrc', None)
  42.         self.sink = Gst.ElementFactory.make('autovideosink', None)
  43.  
  44.         # Add elements to the pipeline
  45.         self.pipeline.add(self.src)
  46.         self.pipeline.add(self.sink)
  47.  
  48.         self.src.link(self.sink)
  49.  
  50.     def run(self):
  51.         self.window.show_all()
  52.         self.pipeline.set_state(Gst.State.PLAYING)
  53.         Gtk.main()
  54.  
  55.     def quit(self, window):
  56.         self.pipeline.set_state(Gst.State.NULL)
  57.         Gtk.main_quit()
  58.  
  59.     def on_sync_message(self, bus, msg):
  60.         if msg.get_structure().get_name() == 'prepare-window-handle':
  61.             print('prepare-window-handle')
  62.             msg.src.set_property('force-aspect-ratio', True)
  63.             msg.src.set_window_handle(self.xid)
  64.  
  65.     def on_error(self, bus, msg):
  66.         print('on_error():', msg.parse_error())
  67.  
  68.  
  69. webcam = Webcam()
  70. webcam.run()
Advertisement
Add Comment
Please, Sign In to add comment