Advertisement
Guest User

Everything but the one important thing works now...

a guest
Dec 12th, 2013
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.93 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. from os import path
  4.  
  5. import gi
  6. gi.require_version('Gst', '1.0')
  7.  
  8. #For threading support
  9. from gi.repository import GObject
  10.  
  11. GObject.threads_init()
  12.  
  13. #For the as of this moment not working video...
  14. from gi.repository import Gst, Gtk
  15. # Needed for window.get_xid(), xvimagesink.set_window_handle(), respectively:
  16. from gi.repository import GdkX11, GstVideo
  17.  
  18. #for keypresses
  19. from gi.repository import Gdk
  20.  
  21. #for resize
  22. import Xlib
  23. import Xlib.display
  24.  
  25.  
  26. Gst.init(None)
  27.  
  28. class Player(object):
  29.     def __init__(self):
  30.         self.resolution = Xlib.display.Display().screen().root.get_geometry()
  31.         width = 800
  32.         height = 450
  33.         self.win = Gtk.Window()
  34.         self.win.connect('destroy', self.quit)
  35.         self.win.set_default_size(width, height)
  36.         self.win.set_decorated(False)
  37.         self.win.set_keep_above(True)
  38.         self.win.set_property('skip-taskbar-hint', True)
  39.  
  40.         self.drawingarea = Gtk.DrawingArea()
  41.         self.win.add(self.drawingarea)
  42.  
  43.         # Create GStreamer pipeline
  44.         print('Getting pipe')
  45.         self.pipeline = Gst.parse_launch ("v4l2src ! autovideosink")
  46.         #self.pipeline = Gst.parse_launch ("v4l2src ! videoconvert ! xvimagesink")
  47.  
  48.         # Create bus to get events from GStreamer pipeline
  49.         print('Getting bus')
  50.         self.bus = self.pipeline.get_bus()
  51.         self.bus.add_signal_watch()
  52.         self.bus.connect('message::eos', self.on_eos)
  53.         self.bus.connect('message::error', self.on_error)
  54.  
  55.         # This is needed to make the video output in our DrawingArea:
  56.         self.bus.enable_sync_message_emission()
  57.         self.bus.connect('sync-message::element', self.on_sync_message)
  58.  
  59.         self.win.connect("key-press-event", self.on_keypress)
  60.         self.on_keypress_dict = {
  61.             Gdk.KEY_Escape : Gtk.main_quit,
  62.             Gdk.KEY_F1 : self.pipeline.set_state,
  63.             Gdk.KEY_F2 : self.pipeline.set_state,
  64.         }
  65.         self.state_args = {
  66.             Gdk.KEY_Escape : Gtk.main_quit,
  67.             Gdk.KEY_F1 : Gst.State.PLAYING,
  68.             Gdk.KEY_F2 : Gst.State.NULL
  69.         }
  70.         self.win.move(self.resolution.width - width, self.resolution.height - height)
  71.  
  72.     def on_keypress(self, widget, event):
  73.         '''
  74.        A case switch statement to eliminate the
  75.        five checks kludginess
  76.        '''
  77.         print('In keypress')
  78.         if event.keyval == Gdk.KEY_F5: self.properties(widget, event)
  79.         else: self.on_keypress_dict[event.keyval](self.state_args[event.keyval])
  80.  
  81.     def properties(self, widget, event) :
  82.         self.prop_win = Gtk.Window(Gtk.WindowType.TOPLEVEL)
  83.         self.prop_win.set_title("Properties")
  84.         self.prop_win.set_size_request(320, 120)
  85.         self.prop_win.set_resizable(False)
  86.         self.prop_win.set_keep_above(True)
  87.         self.prop_win.set_property('skip-taskbar-hint', True)
  88.         self.win.connect("destroy", self.closeproperties)
  89.         vbox = Gtk.VBox(spacing=4)
  90.         hbox = Gtk.HBox(spacing=4)
  91.         hbox2 = Gtk.HBox(spacing=4)
  92.         check = Gtk.CheckButton("Pin")
  93.         check.set_active(True)
  94.         check.set_size_request(100, 35)
  95.         check.connect("clicked", self.pinning)
  96.         hbox.pack_start(check, True, True, 0)
  97.         scale = Gtk.HScale()
  98.         scale.set_range(0, 100)
  99.         scale.set_value(100)
  100.         scale.set_size_request(320, 35)
  101.         scale.connect("value-changed", self.opac_slider)
  102.         hbox.pack_start(scale, True, True, 0)
  103.         self.entry = Gtk.Entry()
  104.         self.entry2 = Gtk.Entry()
  105.         self.entry.set_text("width")
  106.         self.entry2.set_text("height")
  107.         hbox2.pack_start(self.entry, True, True, 0)
  108.         hbox2.pack_start(self.entry2, True, True, 0)
  109.         hbox3 = Gtk.HBox(spacing=4)
  110.         ok = Gtk.Button("OK")
  111.         ok.connect("clicked", self.change_size)
  112.         hbox3.pack_start(ok, True, True, 0)
  113.         exit = Gtk.Button("Exit")
  114.         exit.connect("clicked", self.closeproperties)
  115.         hbox3.pack_start(exit, True, True, 0)
  116.         vbox.pack_start(hbox, True, True, 0)
  117.         vbox.pack_start(hbox2, True, True, 0)
  118.         vbox.pack_start(hbox3, True, True, 0)
  119.         self.prop_win.add(vbox)
  120.         self.prop_win.show_all()
  121.  
  122.     def pinning(self, checkbox):
  123.         if checkbox.get_active():
  124.             self.set_keep_above(True)
  125.         else:
  126.             self.set_keep_above(False)
  127.  
  128.     def opac_slider(self, w):
  129.         self.win.set_opacity(w.get_value()/100.0)
  130.                
  131.     def change_size(self, w):
  132.         print('in change size')
  133.         width = int(self.entry.get_text())
  134.         height = int(self.entry2.get_text())
  135.         self.win.resize(width,height)
  136.         self.win.move(self.resolution.width - width, self.resolution.height - height)
  137.         self.win.show_all()
  138.  
  139.     def closeproperties(self, w):
  140.         self.prop_win.hide()
  141.  
  142.     def run(self):
  143.         self.win.show_all()
  144.         # You need to get the XID after window.show_all().  You shouldn't get it
  145.         # in the on_sync_message() handler because threading issues will cause
  146.         # segfaults there.
  147.         self.xid = self.drawingarea.get_property('window').get_xid()
  148.  
  149.         print('in get_xid')
  150.         self.pipeline.set_state(Gst.State.READY)
  151.         Gtk.main()
  152.  
  153.     def quit(self, window):
  154.         self.pipeline.set_state(Gst.State.NULL)
  155.         Gtk.main_quit()
  156.  
  157.     def on_sync_message(self, bus, msg):
  158.         if msg.get_structure().get_name() == 'prepare-window-handle':
  159.             print('prepare-window-handle')
  160.             msg.src.set_window_handle(self.xid)
  161.             print('prepped')
  162.  
  163.     def on_eos(self, bus, msg):
  164.         print('on_eos(): seeking to start of video')
  165.         self.pipeline.seek_simple(
  166.             Gst.Format.TIME,        
  167.             Gst.SeekFlags.FLUSH | Gst.SeekFlags.KEY_UNIT,
  168.             0
  169.         )
  170.  
  171.     def on_error(self, bus, msg):
  172.         print('on_error():', msg.parse_error())
  173.  
  174.  
  175. p = Player()
  176. p.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement