From 5826cd917eee7ce797b46995754fd1d362089534 Mon Sep 17 00:00:00 2001 From: ozankaraali Date: Fri, 18 Oct 2024 22:08:46 +0200 Subject: [PATCH] If loading fails, indicate there's an error at the server --- config_manager.py | 2 +- video_player.py | 78 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 74 insertions(+), 6 deletions(-) diff --git a/config_manager.py b/config_manager.py index 7941646..1c148e9 100644 --- a/config_manager.py +++ b/config_manager.py @@ -6,7 +6,7 @@ class ConfigManager: - CURRENT_VERSION = "1.5.4" # Set your current version here + CURRENT_VERSION = "1.5.5" # Set your current version here def __init__(self): self.config = {} diff --git a/video_player.py b/video_player.py index 9429bc6..9192d08 100644 --- a/video_player.py +++ b/video_player.py @@ -1,3 +1,4 @@ +import logging import platform import sys @@ -6,6 +7,20 @@ from PySide6.QtGui import QCursor, QGuiApplication from PySide6.QtWidgets import QFrame, QMainWindow, QProgressBar, QVBoxLayout +logging.basicConfig(level=logging.ERROR) + + +class VLCLogger: + def __init__(self): + self.latest_error = "" + + def log(self, message): + self.latest_error = message + logging.error(f"VLC Error: {message}") + + def get_latest_error(self): + return self.latest_error + class VideoPlayer(QMainWindow): def __init__(self, config_manager, *args, **kwargs): @@ -42,13 +57,23 @@ def __init__(self, config_manager, *args, **kwargs): # Custom user-agent string user_agent = "Mozilla/5.0 (QtEmbedded; U; Linux; C) AppleWebKit/533.3 (KHTML, like Gecko) MAG200 stbapp ver: 2 rev: 250 Safari/533.3" + self.vlc_logger = VLCLogger() + + # Initialize VLC instance self.instance = vlc.Instance( ["--video-on-top", f"--http-user-agent={user_agent}"] - ) + ) # vlc.Instance(["--verbose=2"]) # Enable verbose logging + self.media_player = self.instance.media_player_new() self.media_player.video_set_mouse_input(False) self.media_player.video_set_key_input(False) + # Set up event manager for logging + self.event_manager = self.media_player.event_manager() + self.event_manager.event_attach( + vlc.EventType.MediaPlayerEncounteredError, self.on_vlc_error + ) + if sys.platform.startswith("linux"): self.media_player.set_xwindow(self.video_frame.winId()) elif sys.platform == "win32": @@ -97,7 +122,8 @@ def format_time(self, milliseconds): return f"{minutes:02d}:{seconds:02d}" def update_progress(self): - if self.media_player.is_playing(): + state = self.media_player.get_state() + if state == vlc.State.Playing: current_time = self.media_player.get_time() total_time = self.media.get_duration() @@ -109,6 +135,17 @@ def update_progress(self): else: self.progress_bar.setFormat("Live") self.progress_bar.setValue(0) + elif state == vlc.State.Error: + self.handle_error("Playback error") + elif state == vlc.State.Ended: + self.progress_bar.setFormat("Playback ended") + self.progress_bar.setValue(1000) # Set to 100% + elif state == vlc.State.Opening: + self.progress_bar.setFormat("Opening...") + self.progress_bar.setValue(0) + elif state == vlc.State.Buffering: + self.progress_bar.setFormat("Buffering...") + self.progress_bar.setValue(0) def wheelEvent(self, event): delta = event.angleDelta().y() @@ -191,9 +228,21 @@ def play_video(self, video_url): ) self.media.parse_with_options(1, 0) - self.media_player.play() - self.adjust_aspect_ratio() - self.show() + play_result = self.media_player.play() + if play_result == -1: + self.handle_error("Failed to start playback") + else: + self.adjust_aspect_ratio() + self.show() + QTimer.singleShot(5000, self.check_playback_status) + + def check_playback_status(self): + if not self.media_player.is_playing(): + media_state = self.media.get_state() + if media_state == vlc.State.Error: + self.handle_error("Playback error") + else: + self.handle_error("Failed to start playback") def stop_video(self): self.media_player.stop() @@ -369,3 +418,22 @@ def media_length_changed(self): self.progress_bar.setVisible(False) # Hide the progress bar self.progress_bar.setFormat("Live") # self.update_timer.start() + + def on_vlc_error(self, event): + # We don't use event data here, just log that an error occurred + self.vlc_logger.log("An error occurred during playback") + QMetaObject.invokeMethod(self, "media_error_occurred", Qt.QueuedConnection) + + @Slot() + def media_error_occurred(self): + self.handle_error("Playback error occurred") + + def handle_error(self, error_message): + vlc_error = self.vlc_logger.get_latest_error() + if vlc_error: + error_message += f": {vlc_error}" + logging.error(f"VLC Error: {error_message}") + self.progress_bar.setVisible(True) + self.progress_bar.setFormat(f"Error: {error_message}") + self.progress_bar.setValue(0) + self.update_timer.stop()