From 23941f895393e94b59e75bb283d87674b85fb657 Mon Sep 17 00:00:00 2001 From: Casey Occhialini <1508707+littlespex@users.noreply.github.com> Date: Fri, 4 Aug 2023 01:55:27 -0700 Subject: [PATCH] bugfix/reset-race-conditions (#4246) * fix: clear timeout on reset * fix: add null check to prevent errors when _onRemove is called after reset --- src/streaming/controllers/BufferController.js | 4 ++++ src/streaming/models/VideoModel.js | 10 ++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/streaming/controllers/BufferController.js b/src/streaming/controllers/BufferController.js index adf97b1b98..605d18fad6 100644 --- a/src/streaming/controllers/BufferController.js +++ b/src/streaming/controllers/BufferController.js @@ -1013,6 +1013,10 @@ function BufferController(config) { function _onRemoved(e) { logger.debug('onRemoved buffer from:', e.from, 'to', e.to); + if (!sourceBufferSink) { + return; + } + const ranges = sourceBufferSink.getAllBufferRanges(); _showBufferRanges(ranges); diff --git a/src/streaming/models/VideoModel.js b/src/streaming/models/VideoModel.js index 2623294662..85a7da2ab5 100644 --- a/src/streaming/models/VideoModel.js +++ b/src/streaming/models/VideoModel.js @@ -51,7 +51,8 @@ function VideoModel() { _currentTime, TTMLRenderingDiv, vttRenderingDiv, - previousPlaybackRate; + previousPlaybackRate, + timeout; const VIDEO_MODEL_WRONG_ELEMENT_TYPE = 'element is not video or audio DOM type!'; @@ -69,6 +70,7 @@ function VideoModel() { } function reset() { + clearTimeout(timeout); eventBus.off(Events.PLAYBACK_PLAYING, onPlaying, this); } @@ -94,6 +96,10 @@ function VideoModel() { if (element) { _currentTime = currentTime; waitForReadyState(Constants.VIDEO_ELEMENT_READY_STATES.HAVE_METADATA, () => { + if (!element) { + return; + } + // We don't set the same currentTime because it can cause firing unexpected Pause event in IE11 // providing playbackRate property equals to zero. if (element.currentTime === _currentTime) { @@ -112,7 +118,7 @@ function VideoModel() { _currentTime = NaN; } catch (e) { if (element.readyState === 0 && e.code === e.INVALID_STATE_ERR) { - setTimeout(function () { + timeout = setTimeout(function () { element.currentTime = _currentTime; _currentTime = NaN; }, 400);