Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import wiringpi2 as wiringpi
- import gi
- gi.require_version('Gst', '1.0')
- from gi.repository import GObject, Gst, Gtk, Gdk
- # Needed for window.get_xid(), xvimagesink.set_window_handle(), respectively:
- from gi.repository import GdkX11, GstVideo
- GObject.threads_init()
- Gst.init(None)
- window_width = 800
- window_height = 480
- video_width = 320
- video_height = 240
- class Webcam:
- def __init__(self):
- wiringpi.wiringPiSetup() #enable pins
- wiringpi.pinMode(7, 1) # sets GPIO 7 to output
- wiringpi.pinMode(8, 1) # sets GPIO 8 to output
- wiringpi.pinMode(9, 1) # sets GPIO 9 to output
- self.window = Gtk.Window()
- self.window.set_title("Hello")
- self.window.connect('destroy', self.quit)
- self.window.set_default_size(window_width, window_height)
- self.geometry = Gdk.Geometry()
- self.geometry.max_width = window_width
- self.geometry.max_height = window_height
- self.geometry.min_width = 0
- self.geometry.min_height = 0
- self.hints = Gdk.WindowHints(Gdk.WindowHints.MAX_SIZE)
- self.window.set_geometry_hints(self.window,
- self.geometry,
- self.hints)
- self.window.set_position(Gtk.WindowPosition.CENTER)
- self.window.fullscreen()
- self.drawingarea = Gtk.DrawingArea()
- #self.drawingarea.set_size_request(video_width, video_height)
- grid = Gtk.Grid()
- self.window.add(grid)
- button1 = Gtk.Button(label="LED on")
- button1.connect("clicked", self.button1_on_click)
- button2 = Gtk.Button(label="LED off")
- button2.connect("clicked", self.button2_on_click)
- button3 = Gtk.Button(label="Button 3")
- button4 = Gtk.Button(label="Button 4")
- button5 = Gtk.Button(label="Button 5")
- button6 = Gtk.Button(label="Button 6")
- #grid.add(button1)
- grid.attach(button1, 0, 0, 1, 2)
- grid.attach(button2, 0, 2, 2, 2)
- grid.attach(button3, 0, 4, 2, 2)
- grid.attach(button4, 0, 6, 2, 2)
- grid.attach(button5, 0, 8, 2, 2)
- grid.attach_next_to(self.drawingarea, button3, Gtk.PositionType.RIGHT, 1, 2)
- self.drawingarea.set_size_request(video_width, video_height)
- #grid.attach_next_to(button4, button3, Gtk.PositionType.RIGHT, 2, 1)
- #grid.attach(self.drawingarea, 1, 2, 1, 1)
- #grid.attach_next_to(button6, button5, Gtk.PositionType.RIGHT, 1, 1)
- # Create GStreamer pipeline
- #self.pipeline = Gst.Pipeline()
- video_pipeline = 'rpicamsrc bitrate=1000000 preview=false ! video/x-h264,width=' + str(video_width) + ',height=' + str(video_height) +' ! decodebin ! queue ! videoconvert ! ximagesink'
- #video_pipeline = 'v4l2src ! autovideosink'
- #video_pipeline = 'rpicamsrc bitrate=1000000 ! decodebin ! queue ! videoconvert ! autovideosink'
- print ('pipeline :' + video_pipeline)
- self.pipeline = Gst.parse_launch(video_pipeline)
- # Create bus to get events from GStreamer pipeline
- self.bus = self.pipeline.get_bus()
- self.bus.add_signal_watch()
- self.bus.connect('message::error', self.on_error)
- # This is needed to make the video output in our DrawingArea:
- self.bus.enable_sync_message_emission()
- self.bus.connect('sync-message::element', self.on_sync_message)
- def button1_on_click(self, button):
- wiringpi.digitalWrite(7, 1)
- def button2_on_click(self, button):
- wiringpi.digitalWrite(7, 0)
- def run(self):
- self.window.show_all()
- # You need to get the XID after window.show_all(). You shouldn't get it
- # in the on_sync_message() handler because threading issues will cause
- # segfaults there.
- self.xid = self.drawingarea.get_property('window').get_xid()
- print (self.xid)
- self.pipeline.set_state(Gst.State.PLAYING)
- #Gdk.threads_init()
- Gtk.main()
- def quit(self, window):
- self.pipeline.set_state(Gst.State.NULL)
- Gtk.main_quit()
- def on_sync_message(self, bus, msg):
- if msg.get_structure().get_name() == 'prepare-window-handle':
- print('prepare-window-handle')
- msg.src.set_property('force-aspect-ratio', True)
- msg.src.set_window_handle(self.xid)
- def on_error(self, bus, msg):
- print('on_error():', msg.parse_error())
- webcam = Webcam()
- webcam.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement