mirror of
https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs.git
synced 2024-12-28 21:10:34 +00:00
gtk4: Clean up Python example
It's not more or less equivalent to the Rust example. Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs/-/merge_requests/1573>
This commit is contained in:
parent
71cd80f204
commit
49d3dd17a2
1 changed files with 92 additions and 42 deletions
|
@ -1,57 +1,107 @@
|
|||
import gi
|
||||
import sys
|
||||
|
||||
gi.require_version("Gtk", "4.0")
|
||||
gi.require_version('Gst', '1.0')
|
||||
|
||||
gi.require_version('GLib', '2.0')
|
||||
from gi.repository import GLib
|
||||
gi.require_version('Gtk', '4.0')
|
||||
from gi.repository import Gtk
|
||||
gi.require_version('Gst', '1.0')
|
||||
from gi.repository import Gst
|
||||
|
||||
Gst.init(sys.argv[1:])
|
||||
|
||||
gtksink = Gst.ElementFactory.make("gtk4paintablesink", "sink")
|
||||
# Get the paintable from the sink
|
||||
paintable = gtksink.props.paintable
|
||||
|
||||
# Use GL if available
|
||||
if paintable.props.gl_context:
|
||||
print("Using GL")
|
||||
source = Gst.ElementFactory.make("gltestsrc", "source")
|
||||
glsink = Gst.ElementFactory.make("glsinkbin", "sink")
|
||||
glsink.props.sink = gtksink
|
||||
sink = glsink
|
||||
else:
|
||||
source = Gst.ElementFactory.make("videotestsrc", "source")
|
||||
sink = gtksink
|
||||
|
||||
pipeline = Gst.Pipeline.new()
|
||||
|
||||
if not pipeline or not source or not sink:
|
||||
print("Not all elements could be created.")
|
||||
exit(-1)
|
||||
|
||||
pipeline.add(source)
|
||||
pipeline.add(sink)
|
||||
source.link(sink)
|
||||
|
||||
def on_activate(app):
|
||||
win = Gtk.ApplicationWindow(application=app)
|
||||
box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
|
||||
pipeline = Gst.Pipeline.new()
|
||||
|
||||
overlay = Gst.ElementFactory.make('clockoverlay', None)
|
||||
overlay.set_property('font-desc', 'Monospace 42')
|
||||
|
||||
gtksink = Gst.ElementFactory.make('gtk4paintablesink', None)
|
||||
paintable = gtksink.get_property('paintable')
|
||||
|
||||
if paintable.props.gl_context:
|
||||
print('Using GL')
|
||||
src = Gst.ElementFactory.make('gltestsrc', None)
|
||||
|
||||
sink = Gst.ElementFactory.make('glsinkbin', None)
|
||||
sink.set_property('sink', gtksink)
|
||||
else:
|
||||
print('Not using GL')
|
||||
src = Gst.ElementFactory.make('videotestsrc', None)
|
||||
|
||||
sink = Gst.Bin.new()
|
||||
convert = Gst.ElementFactory.make('videoconvert', None)
|
||||
|
||||
sink.add(convert)
|
||||
sink.add(gtksink)
|
||||
convert.link(gtksink)
|
||||
|
||||
sink.add_pad(Gst.GhostPad.new('sink', gtksink.get_static_pad('sink')))
|
||||
|
||||
pipeline.add(src)
|
||||
pipeline.add(overlay)
|
||||
pipeline.add(sink)
|
||||
|
||||
src.link_filtered(overlay, Gst.Caps.from_string('video/x-raw(ANY), width=640, height=480'))
|
||||
overlay.link(sink)
|
||||
|
||||
window = Gtk.ApplicationWindow(application=app)
|
||||
window.set_default_size(640, 480)
|
||||
|
||||
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
|
||||
|
||||
picture = Gtk.Picture.new()
|
||||
picture.set_size_request(640, 480)
|
||||
# Set the paintable on the picture
|
||||
picture.set_paintable(paintable)
|
||||
box.append(picture)
|
||||
|
||||
btn = Gtk.Button(label="▶/⏸")
|
||||
box.append(btn)
|
||||
btn.connect('clicked', lambda _: pipeline.set_state(Gst.State.PAUSED) if pipeline.get_state(1)[1]==Gst.State.PLAYING else pipeline.set_state(Gst.State.PLAYING))
|
||||
vbox.append(picture)
|
||||
|
||||
win.set_child(box)
|
||||
win.present()
|
||||
label = Gtk.Label.new('Position: 00:00:00')
|
||||
vbox.append(label)
|
||||
|
||||
app = Gtk.Application()
|
||||
app.connect('activate', on_activate)
|
||||
window.set_child(vbox)
|
||||
window.present()
|
||||
|
||||
app.run(None)
|
||||
app.add_window(window)
|
||||
|
||||
def on_timeout():
|
||||
(res, position) = pipeline.query_position(Gst.Format.TIME)
|
||||
if not res or position == -1:
|
||||
label.set_text('Position: 00:00:00')
|
||||
else:
|
||||
seconds = int(position / 1000000000)
|
||||
minutes = int(seconds / 60)
|
||||
hours = int(minutes / 60)
|
||||
seconds = seconds % 60
|
||||
minutes = minutes % 60
|
||||
label.set_text(f'Position: {hours:02}:{minutes:02}:{seconds:02}')
|
||||
|
||||
return True
|
||||
|
||||
GLib.timeout_add(500, on_timeout)
|
||||
|
||||
pipeline.set_state(Gst.State.PLAYING)
|
||||
|
||||
def on_bus_msg(_, msg):
|
||||
match msg.type:
|
||||
case Gst.MessageType.EOS:
|
||||
app.quit()
|
||||
case Gst.MessageType.ERROR:
|
||||
(err, _) = msg.parse_error()
|
||||
print(f'Error from {msg.src.path_string()}: {err}')
|
||||
app.quit()
|
||||
|
||||
return True
|
||||
|
||||
bus = pipeline.get_bus()
|
||||
bus.add_watch(GLib.PRIORITY_DEFAULT, on_bus_msg)
|
||||
|
||||
def on_close(_):
|
||||
window.close()
|
||||
pipeline.set_state(Gst.State.NULL)
|
||||
app.connect('shutdown', on_close)
|
||||
|
||||
if __name__ == '__main__':
|
||||
Gst.init(None)
|
||||
app = Gtk.Application()
|
||||
app.connect('activate', on_activate)
|
||||
res = app.run(None)
|
||||
sys.exit(res)
|
||||
|
|
Loading…
Reference in a new issue