From 95e8157277818e96ad1c9a9fe773af6a0b3ea0e6 Mon Sep 17 00:00:00 2001 From: Christoph Stahl Date: Thu, 10 Oct 2024 16:05:04 +0200 Subject: [PATCH] Client starting and stopping from gui --- pyproject.toml | 6 +++--- syng/client.py | 4 +++- syng/gui.py | 16 +++++++++------- syng/player_libmpv.py | 14 ++++++++++---- 4 files changed, 25 insertions(+), 15 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index dee7197..9b162f3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -42,8 +42,8 @@ pymediainfo = { version = "^6.1.0", optional = true } pyyaml = { version = "^6.0.1", optional = true } alt-profanity-check = {version = "^1.4.1", optional = true} pyqt6 = {version="^6.7.1", optional = true} -mpv = "^1.0.7" -qasync = "^0.27.1" +mpv = {versions="^1.0.7", optional = true} +qasync = {versions="^0.27.1", optional = true} [tool.poetry.group.dev.dependencies] types-pyyaml = "^6.0.12.12" @@ -54,7 +54,7 @@ requirements-parser = "^0.11.0" [tool.poetry.extras] -client = ["minio", "pillow", "qrcode", "pymediainfo", "pyyaml", "pyqt6"] +client = ["minio", "pillow", "qrcode", "pymediainfo", "pyyaml", "pyqt6", "mpv", "qasync"] server = ["alt-profanity-check"] [build-system] diff --git a/syng/client.py b/syng/client.py index 6354ccf..0c72bc1 100644 --- a/syng/client.py +++ b/syng/client.py @@ -118,6 +118,7 @@ class State: class Client: def __init__(self, config: dict[str, Any]): + self.is_running = False self.sio = socketio.AsyncClient(json=jsonencoder) self.loop: Optional[asyncio.AbstractEventLoop] = None self.skipped = [] @@ -452,7 +453,6 @@ class Client: :type config: dict[str, Any] :rtype: None """ - print("Starting client") self.loop = asyncio.get_running_loop() @@ -487,10 +487,12 @@ class Client: asyncio.get_event_loop().add_signal_handler(signal.SIGTERM, self.signal_handler) try: + self.is_running = True await self.sio.wait() except asyncio.CancelledError: pass finally: + self.is_running = False if self.player is not None: self.player.mpv.terminate() diff --git a/syng/gui.py b/syng/gui.py index 3e624cd..6f72cc3 100644 --- a/syng/gui.py +++ b/syng/gui.py @@ -3,6 +3,7 @@ from io import BytesIO import sys import logging from logging.handlers import QueueListener +from logging.handlers import QueueHandler # from multiprocessing import Process, Queue from queue import Queue @@ -58,6 +59,7 @@ import platformdirs from . import resources # noqa from .client import Client, create_async_and_start_client, default_config +from .log import logger from .sources import available_sources from .config import ( @@ -632,6 +634,7 @@ class SyngGui(QMainWindow): self.loop = asyncio.get_event_loop() self.syng_server: Optional[threading.Thread] = None self.syng_client: Optional[threading.Thread] = None + self.client: Optional[Client] = None self.syng_client_logging_listener: Optional[QueueListener] = None self.configfile = os.path.join(platformdirs.user_config_dir("syng"), "config.yaml") @@ -751,16 +754,14 @@ class SyngGui(QMainWindow): dump(config, f, Dumper=Dumper) def check_if_client_is_running(self) -> None: - if self.syng_client is None: + if self.client is None: self.timer.stop() return - if not self.syng_client.is_alive(): - print("Client is not running") - self.syng_client = None + if not self.client.is_running: + self.client = None self.set_client_button_start() else: - print("Client is running") self.set_client_button_stop() def set_client_button_stop(self) -> None: @@ -770,7 +771,7 @@ class SyngGui(QMainWindow): self.startbutton.setText("Save and Start") def start_syng_client(self) -> None: - if self.syng_client is None or not self.syng_client.is_alive(): + if self.client is None or not self.client.is_running: self.save_config() config = self.gather_config() queue: Queue[logging.LogRecord] = Queue() @@ -783,6 +784,7 @@ class SyngGui(QMainWindow): # self.syng_client = multiprocessing.Process( # target=create_async_and_start_client, args=[config, queue] # ) + logger.addHandler(QueueHandler(queue)) self.client = Client(config) asyncio.run_coroutine_threadsafe(self.client.start_client(config), self.loop) # self.syng_client = threading.Thread( @@ -794,7 +796,7 @@ class SyngGui(QMainWindow): self.set_client_button_stop() else: self.client.quit_callback() - self.syng_client.join(1.0) + # self.syng_client.join(1.0) self.set_client_button_start() # def start_syng_server(self) -> None: diff --git a/syng/player_libmpv.py b/syng/player_libmpv.py index 379ecad..e2ab334 100644 --- a/syng/player_libmpv.py +++ b/syng/player_libmpv.py @@ -79,7 +79,10 @@ class Player: 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") + try: + await loop.run_in_executor(None, self.mpv.wait_for_property, "eof-reached") + except mpv.ShutdownError: + self.quit_callback() def play_image(self, image: str, duration: int, sub_file: Optional[str] = None) -> None: for property, value in self.default_options.items(): @@ -113,9 +116,12 @@ class Player: else: 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") + try: + 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") + except mpv.ShutdownError: + self.quit_callback() def skip_current(self) -> None: self.mpv.image_display_duration = 0