Skip to content

Instantly share code, notes, and snippets.

@lubosz
Created September 11, 2014 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lubosz/956002b007650915221a to your computer and use it in GitHub Desktop.
Save lubosz/956002b007650915221a to your computer and use it in GitHub Desktop.
Event handling in the Gstreamer glimagesink
#!/usr/bin/env python3
from gi.repository import Gdk, Gtk, Gst
import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)
class GstOverlaySink(Gtk.DrawingArea):
def __init__(self, name, w, h):
Gtk.DrawingArea.__init__(self)
from gi.repository import GdkX11, GstVideo
self.sink = Gst.ElementFactory.make(name, None)
self.set_size_request(w, h)
self.connect("motion-notify-event", self.on_motion)
self.connect("button-press-event", self.on_press)
self.connect("button-release-event", self.on_release)
"""
When handle_events is set to True (Default, when nothing is set here)
Only the press event is captured when setting Gdk events
The motion event is captured only "on drag", when press is triggered
The BUTTON_PRESS_MASK needs to be set for the POINTER_MOTION_MASK
to work during drag. ALL_EVENTS_MASK also works.
The motion event does not work without drag.
Also setting custom cursors does not work.
"""
# self.sink.handle_events (True)
# self.add_events(Gdk.EventMask.BUTTON_PRESS_MASK)
# self.add_events(Gdk.EventMask.POINTER_MOTION_MASK)
# or
#self.add_events(Gdk.EventMask.ALL_EVENTS_MASK)
"""
When handle_events is set to False
The behaviour is the same as in the xvimagesink
"""
self.sink.handle_events (False)
self.add_events(Gdk.EventMask.BUTTON_PRESS_MASK)
self.add_events(Gdk.EventMask.BUTTON_RELEASE_MASK)
self.add_events(Gdk.EventMask.POINTER_MOTION_MASK)
def on_press(self, sink, event):
print("press")
def on_release(self, sink, event):
print("release")
def on_motion(self, sink, event):
print(event.x, event.y)
def xid(self):
return self.get_window().get_xid()
def set_handle(self):
self.sink.set_window_handle(self.xid())
def quit_app(widget):
widget.hide()
Gtk.main_quit()
def window_closed(widget, event):
quit_app(widget)
def key_pressed(widget, key):
if key.keyval == Gdk.KEY_Escape:
quit_app(widget)
Gtk.init([])
Gst.init([])
window = Gtk.Window()
window.connect("delete-event", window_closed)
window.set_default_size(640, 480)
window.set_title("Foo")
pipeline = Gst.Pipeline()
src = Gst.ElementFactory.make("gltestsrc", None)
sink = GstOverlaySink("glimagesink", 640, 480)
#sink = GstOverlaySink("xvimagesink", 640, 480)
pipeline.add(src, sink.sink)
src.link(sink.sink)
window.add(sink)
window.show_all()
sink.set_handle()
if pipeline.set_state(Gst.State.PLAYING) == Gst.StateChangeReturn.FAILURE:
pipeline.set_state(Gst.State.NULL)
else:
Gtk.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment