From df5bd2d23b8cb138fbd23b09559922dd18243eb1 Mon Sep 17 00:00:00 2001 From: Vinayak Ram Date: Sat, 14 Mar 2026 16:20:06 -0500 Subject: [PATCH 1/2] Fix InvalidStateError when SourceBuffer is removed during updateend Wrap the buffer management block in the MSE updateend handler with try-catch to handle the case where the SourceBuffer has been removed from the parent MediaSource before the handler executes. The sb.buffered getter throws InvalidStateError when the SourceBuffer is detached, which causes the stream to crash and reload in a loop. This matches the existing error handling pattern already used for the appendBuffer call in the same handler. Fixes #901 --- custom_components/webrtc/www/video-rtc.js | 30 +++++++++++++---------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/custom_components/webrtc/www/video-rtc.js b/custom_components/webrtc/www/video-rtc.js index 8ecbce72..c11f0889 100644 --- a/custom_components/webrtc/www/video-rtc.js +++ b/custom_components/webrtc/www/video-rtc.js @@ -449,20 +449,24 @@ export class VideoRTC extends HTMLElement { } } - if (!sb.updating && sb.buffered && sb.buffered.length) { - const end = sb.buffered.end(sb.buffered.length - 1); - const start = end - 5; - const start0 = sb.buffered.start(0); - if (start > start0) { - sb.remove(start0, start); - ms.setLiveSeekableRange(start, end); + try { + if (!sb.updating && sb.buffered && sb.buffered.length) { + const end = sb.buffered.end(sb.buffered.length - 1); + const start = end - 5; + const start0 = sb.buffered.start(0); + if (start > start0) { + sb.remove(start0, start); + ms.setLiveSeekableRange(start, end); + } + if (this.video.currentTime < start) { + this.video.currentTime = start; + } + const gap = end - this.video.currentTime; + this.video.playbackRate = gap > 0.1 ? gap : 0.1; + // console.debug('VideoRTC.buffered', gap, this.video.playbackRate, this.video.readyState); } - if (this.video.currentTime < start) { - this.video.currentTime = start; - } - const gap = end - this.video.currentTime; - this.video.playbackRate = gap > 0.1 ? gap : 0.1; - // console.debug('VideoRTC.buffered', gap, this.video.playbackRate, this.video.readyState); + } catch (e) { + // console.debug(e); } }); From 2bb04d4a2e6787e07d5383c2314bb250c91c6f89 Mon Sep 17 00:00:00 2001 From: Vinayak Ram Date: Sat, 14 Mar 2026 16:34:15 -0500 Subject: [PATCH 2/2] Log caught SourceBuffer errors with console.warn instead of silently swallowing --- custom_components/webrtc/www/video-rtc.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/webrtc/www/video-rtc.js b/custom_components/webrtc/www/video-rtc.js index c11f0889..622ba91b 100644 --- a/custom_components/webrtc/www/video-rtc.js +++ b/custom_components/webrtc/www/video-rtc.js @@ -466,7 +466,7 @@ export class VideoRTC extends HTMLElement { // console.debug('VideoRTC.buffered', gap, this.video.playbackRate, this.video.readyState); } } catch (e) { - // console.debug(e); + console.warn('VideoRTC.updateend', e); } });