From 12b6f33de336403d5d13b815f1f1ccc6daea95fa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Hamal=20Dvo=C5=99=C3=A1k?= <mordae@anilinux.org>
Date: Sat, 8 Mar 2025 21:26:29 +0100
Subject: [PATCH] Add Gst pipeline

---
 lazy_player/__init__.py | 36 ++++++++++++++++++++++++++++++++----
 1 file changed, 32 insertions(+), 4 deletions(-)

diff --git a/lazy_player/__init__.py b/lazy_player/__init__.py
index e3cb326..292283d 100644
--- a/lazy_player/__init__.py
+++ b/lazy_player/__init__.py
@@ -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