From 118887ab6f0a7afbd5fc23019ae930bf67333c57 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 10 Mar 2026 22:02:44 +0100 Subject: [PATCH] perf(player): add MpegTS auto-recovery and optimize initial buffer to prevent Stream Error --- web/player.html | 11 +++++++++-- web/player_lite.html | 17 +++++++++++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/web/player.html b/web/player.html index 9940e5d..87c7679 100644 --- a/web/player.html +++ b/web/player.html @@ -292,13 +292,20 @@ if (!mpegts.isSupported()) return false; const player = mpegts.createPlayer({ type: 'mpegts', isLive: isLive, url: url }, { enableWorker: true, - stashInitialSize: 64 * 1024, // Turbo Start: 64KB initial buffer + stashInitialSize: 256 * 1024, // Optimized Turbo Start maxStashSize: 20 * 1024 * 1024 // 20MB buffer capacity }); player.attachMediaElement(video); player.load(); player.play().catch(() => playOverlay.style.display = 'flex'); - player.on(mpegts.Events.ERROR, () => showError('Stream error')); + + player.on(mpegts.Events.ERROR, (type, detail) => { + log('MpegTS Error: ' + type + ' - ' + detail); + setTimeout(() => { + player.destroy(); + playWithMpegTS(url, isLive); + }, 2000); + }); return true; } diff --git a/web/player_lite.html b/web/player_lite.html index 55e74ae..cbda247 100644 --- a/web/player_lite.html +++ b/web/player_lite.html @@ -296,7 +296,7 @@ url: url }, { enableWorker: true, - stashInitialSize: 64 * 1024, // Turbo Start: 64KB initial buffer + stashInitialSize: 256 * 1024, // Optimized Turbo Start: 256KB initial buffer enableStashBuffer: true, liveBufferLatencyChasing: true, liveBufferLatencyChasingOnPaused: true, @@ -310,7 +310,20 @@ player.load(); player.play().catch(() => playOverlay.style.display = 'flex'); - player.on(mpegts.Events.ERROR, () => showError('MpegTS Error')); + player.on(mpegts.Events.ERROR, (type, detail, info) => { + log('MpegTS Error: ' + type + ' - ' + detail); + // Auto-recover after 2 seconds + setTimeout(() => { + try { + player.unload(); + player.detachMediaElement(); + player.destroy(); + playWithMpegTS(url, isLive); + } catch (e) { + showError('MpegTS Recovery Failed'); + } + }, 2000); + }); return true; }