Skip to content

Commit

Permalink
If loading fails, indicate there's an error at the server
Browse files Browse the repository at this point in the history
  • Loading branch information
ozankaraali committed Oct 18, 2024
1 parent 88993a8 commit 5826cd9
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 6 deletions.
2 changes: 1 addition & 1 deletion config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down
78 changes: 73 additions & 5 deletions video_player.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import platform
import sys

Expand All @@ -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):
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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()

Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()

0 comments on commit 5826cd9

Please sign in to comment.