From 824198acf68f924ebc7f89350be73083a315bd81 Mon Sep 17 00:00:00 2001
From: Christoph Stahl <christoph.stahl@tu-dortmund.de>
Date: Thu, 10 Oct 2024 10:11:08 +0200
Subject: [PATCH] skipping is more consistent, but still does not handle
 multiple rapid skips (edgecase)

---
 syng/client.py        | 10 +++++++++-
 syng/player_libmpv.py | 18 ++++++++++++------
 typings/mpv.pyi       |  4 +++-
 3 files changed, 24 insertions(+), 8 deletions(-)

diff --git a/syng/client.py b/syng/client.py
index d7b0b48..fb6e4ca 100644
--- a/syng/client.py
+++ b/syng/client.py
@@ -119,6 +119,7 @@ class State:
 class Client:
     def __init__(self, config: dict[str, Any]):
         self.sio = socketio.AsyncClient(json=jsonencoder)
+        self.skipped = False
         self.config = config
         self.sources = configure_sources(config["sources"])
         self.state = State()
@@ -165,6 +166,12 @@ class Client:
         :rtype: None
         """
         logger.info("Skipping current")
+        entry = Entry(**data)
+        print("Skipping: ", entry.title)
+        source = self.sources[entry.source]
+
+        self.skipped = True
+        await source.skip_current(Entry(**data))
         self.player.skip_current()
         # if self.state.current_source is not None:
         #     await self.state.current_source.skip_current(Entry(**data))
@@ -288,7 +295,8 @@ class Client:
         except Exception:  # pylint: disable=broad-except
             print_exc()
         self.state.current_source = None
-        if entry.skip:
+        if self.skipped:
+            self.skipped = False
             await self.sio.emit("get-first")
         else:
             await self.sio.emit("pop-then-get-next")
diff --git a/syng/player_libmpv.py b/syng/player_libmpv.py
index 2d28927..da1c4da 100644
--- a/syng/player_libmpv.py
+++ b/syng/player_libmpv.py
@@ -48,7 +48,6 @@ class Player:
 
     async def queue_next(self, entry: Entry) -> None:
         loop = asyncio.get_running_loop()
-        self.play_image(f"{__dirname__}/static/background20perc.png", 3)
 
         frame = sys._getframe()
         stream_name = f"__python_mpv_play_generator_{hash(frame)}"
@@ -63,16 +62,21 @@ class Player:
             preview.unregister()
 
         self.mpv.sub_pos = 50
-        self.mpv.sub_add(f"python://{stream_name}")
+        self.play_image(
+            f"{__dirname__}/static/background20perc.png", 3, sub_file=f"python://{stream_name}"
+        )
 
         await loop.run_in_executor(None, self.mpv.wait_for_property, "eof-reached")
 
-    def play_image(self, image: str, duration: int) -> None:
+    def play_image(self, image: str, duration: int, sub_file: Optional[str] = None) -> None:
         for property, value in self.default_options.items():
             self.mpv[property] = value
         self.mpv.image_display_duration = duration
         self.mpv.keep_open = "yes"
-        self.mpv.play(image)
+        if sub_file:
+            self.mpv.loadfile(image, sub_file=sub_file)
+        else:
+            self.mpv.loadfile(image)
         self.mpv.pause = False
 
     async def play(
@@ -97,10 +101,12 @@ class Player:
             self.mpv.loadfile(video)
         self.mpv.pause = False
         await loop.run_in_executor(None, self.mpv.wait_for_property, "eof-reached")
+        self.mpv.image_display_duration = 0
         self.mpv.play(f"{__dirname__}/static/background.png")
 
     def skip_current(self) -> None:
-        self.mpv.playlist_append(
+        self.mpv.image_display_duration = 0
+        self.mpv.play(
             f"{__dirname__}/static/background.png",
         )
-        self.mpv.playlist_next()
+        # self.mpv.playlist_next()
diff --git a/typings/mpv.pyi b/typings/mpv.pyi
index ef73912..4e9ee4a 100644
--- a/typings/mpv.pyi
+++ b/typings/mpv.pyi
@@ -34,6 +34,8 @@ class MPV:
     def create_image_overlay(self, image: Image, pos: tuple[int, int]) -> ImageOverlay: ...
     def remove_overlay(self, overlay_id: int) -> None: ...
     def observe_property(self, property: str, callback: Callable[[str, Any], None]) -> None: ...
-    def loadfile(self, file: str, audio_file: Optional[str] = None) -> None: ...
+    def loadfile(
+        self, file: str, audio_file: Optional[str] = None, sub_file: Optional[str] = None
+    ) -> None: ...
     def __setitem__(self, key: str, value: str) -> None: ...
     def __getitem__(self, key: str) -> str: ...