Add Gst pipeline

This commit is contained in:
Jan Hamal Dvořák 2025-03-08 21:26:29 +01:00
parent d4601a77b9
commit 12b6f33de3

View file

@ -17,6 +17,8 @@ class MainWindow(Gtk.ApplicationWindow):
stack: Gtk.Stack
list_view: Gtk.ListView
list_store: Gtk.StringList
video_widget: Gtk.Picture
pipeline: Gst.Pipeline
def __init__(self, *args: Any, **kwargs: Any):
super().__init__(*args, **kwargs)
@ -36,11 +38,33 @@ class MainWindow(Gtk.ApplicationWindow):
# Create stack to hold our widgets
self.stack = Gtk.Stack()
# Create black overlay
# Create video widget and overlay
self.video_widget = Gtk.Picture()
self.video_widget.set_can_shrink(True)
self.video_widget.set_keep_aspect_ratio(True)
self.video_widget.set_vexpand(True)
self.video_widget.set_hexpand(True)
overlay_box = Gtk.Box()
overlay_box.set_name("black-overlay")
overlay_box.set_vexpand(True)
overlay_box.set_hexpand(True)
overlay_box.append(self.video_widget)
# Setup GStreamer pipeline
self.pipeline = Gst.Pipeline.new("video-player")
playbin = Gst.ElementFactory.make("playbin", "playbin")
video_sink = Gst.ElementFactory.make("gtk4paintablesink", "gtk4paintablesink")
if not playbin or not video_sink:
raise RuntimeError("Required GStreamer elements could not be created")
playbin.set_property("video-sink", video_sink)
self.pipeline.add(playbin)
# Link video widget to sink
paintable = video_sink.get_property("paintable")
self.video_widget.set_paintable(paintable)
# Add both main menu and overlay to stack
self.stack.add_named(main_box, "menu")
@ -89,9 +113,12 @@ class MainWindow(Gtk.ApplicationWindow):
self._populate_file_list()
return
print("activated", string)
self.stack.set_visible_child_name("overlay")
# Start playing the video
playbin = self.pipeline.get_by_name("playbin")
if playbin:
playbin.set_property("uri", f"file://{os.path.abspath(string)}")
self.pipeline.set_state(Gst.State.PLAYING)
self.stack.set_visible_child_name("overlay")
self.list_view.connect("activate", on_activate)
@ -143,6 +170,7 @@ class MainWindow(Gtk.ApplicationWindow):
state: Gdk.ModifierType,
) -> bool:
if keyval == Gdk.keyval_from_name("Escape"):
self.pipeline.set_state(Gst.State.NULL)
self.stack.set_visible_child_name("menu")
self.list_view.grab_focus()
return True