Error Handling

This commit is contained in:
ingalls
2025-12-16 10:03:22 -07:00
parent 62e2a8dbfe
commit 26db7b0a71

View File

@@ -391,13 +391,9 @@ async function createPlayer(): Promise<void> {
player.value = new Hls({
enableWorker: true,
lowLatencyMode: false, // More forgiving for stream restarts
debug: false,
backBufferLength: 90, // Keep more buffer for smoother playback
maxBufferLength: 30, // Larger buffer for resilience
maxMaxBufferLength: 600,
liveSyncDurationCount: 3, // More tolerant of discontinuities
liveMaxLatencyDurationCount: 10,
lowLatencyMode: true,
debug: localStorage.getItem('debug') === 'true',
backBufferLength: 90,
xhrSetup: (xhr: XMLHttpRequest) => {
// Add authentication if stream requires it
if (url.username && url.password) {
@@ -428,7 +424,7 @@ async function createPlayer(): Promise<void> {
switch (data.type) {
case Hls.ErrorTypes.NETWORK_ERROR:
if (!data.fatal) {
handleStreamRestart(); // Handle muxer restart scenario
video.value.recoverMediaError();
break;
} else {
console.log("Fatal network error:", data);
@@ -437,7 +433,7 @@ async function createPlayer(): Promise<void> {
}
case Hls.ErrorTypes.MEDIA_ERROR:
if (!data.fatal) {
handleStreamRestart(); // Handle muxer restart scenario
video.value.recoverMediaError();
break;
} else {
console.log("Fatal media error:", data);
@@ -469,36 +465,6 @@ async function createPlayer(): Promise<void> {
}
}
/**
* Handle MediaMTX muxer restarts gracefully
* This occurs when MediaMTX creates new segment naming due to source hiccups
*/
function handleStreamRestart(): void {
const hls = player.value;
if (!hls || !videoProtocols.value?.hls) return;
console.log('Handling HLS stream restart (muxer restart detected)');
try {
hls.recoverMediaError();
hls.stopLoad();
hls.loadSource(hls.url!);
const videoElement = hls.media;
if (videoElement) {
hls.once(Hls.Events.LEVEL_LOADED, () => {
// Seek to the end (live edge) to bypass the stalled gap
videoElement.currentTime = videoElement.duration;
hls.startLoad();
videoElement.play().catch(e => console.error("Play failed", e));
});
}
} catch (err) {
console.error('Error handling stream restart:', err);
handleStreamError(err instanceof Error ? err : new Error(String(err)));
}
}
/**
* Handle stream errors with exponential backoff retry logic
* Implements 3-attempt retry system with increasing delays: 1s, 2s, 4s