From d8ead6d9544e6fe44821458ca079aaf82fd6416c Mon Sep 17 00:00:00 2001 From: Gabe Kangas Date: Wed, 16 Mar 2022 23:34:17 -0700 Subject: [PATCH] Allow the latency compensator to be dynamic --- webroot/js/components/latencyCompensator.js | 19 +++++++++++++------ webroot/js/components/player.js | 1 + 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/webroot/js/components/latencyCompensator.js b/webroot/js/components/latencyCompensator.js index 2981084f8..b00896706 100644 --- a/webroot/js/components/latencyCompensator.js +++ b/webroot/js/components/latencyCompensator.js @@ -13,7 +13,7 @@ It will: const BUFFER_LIMIT = 10; // Max number of buffering events before we stop compensating for latency. const MIN_BUFFER_DURATION = 300; // Min duration a buffer event must last to be counted. -const SPEEDUP_RATE = 1.06; // The playback rate when compensating for latency. +const MAX_SPEEDUP_RATE = 1.07; // The playback rate when compensating for latency. const TIMEOUT_DURATION = 20_000; // The amount of time we stop handling latency after certain events. const CHECK_TIMER_INTERVAL = 5_000; // How often we check if we should be compensating for latency. const BUFFERING_AMNESTY_DURATION = 2 * 1000 * 60; // How often until a buffering event expires. @@ -63,6 +63,14 @@ class LatencyCompensator { return; } + let proposedPlaybackRate = bandwidthRatio * 0.2; + console.log('proposed rate', proposedPlaybackRate, this.running); + + proposedPlaybackRate = Math.max( + Math.min(proposedPlaybackRate, MAX_SPEEDUP_RATE), + 1.0 + ); + console.log('playback rate', proposedPlaybackRate, this.running); try { const segment = getCurrentlyPlayingSegment(tech); if (!segment) { @@ -85,7 +93,7 @@ class LatencyCompensator { const latency = now - segmentTime; if (latency > this.maxLatencyThreshold) { - this.start(); + this.start(proposedPlaybackRate); } else if (latency < this.minLatencyThreshold) { this.stop(); } @@ -94,13 +102,13 @@ class LatencyCompensator { } } - start() { - if (this.running || this.disabled) { + start(rate = 1.0) { + if (this.inTimeout || this.disabled) { return; } this.running = true; - this.player.playbackRate(SPEEDUP_RATE); + this.player.playbackRate(rate); } stop() { @@ -129,7 +137,6 @@ class LatencyCompensator { endTimeout() { clearTimeout(this.timeoutTimer); this.inTimeout = false; - this.start(); } handlePlaying() { diff --git a/webroot/js/components/player.js b/webroot/js/components/player.js index 20eb3dec4..64ba824c6 100644 --- a/webroot/js/components/player.js +++ b/webroot/js/components/player.js @@ -4,6 +4,7 @@ import videojs from '/js/web_modules/videojs/dist/video.min.js'; import { getLocalStorage, setLocalStorage } from '../utils/helpers.js'; import { PLAYER_VOLUME, URL_STREAM } from '../utils/constants.js'; import PlaybackMetrics from '../metrics/playback.js'; +import LatencyCompensator from './latencyCompensator.js'; const VIDEO_ID = 'video';