From 0f00d1ce2ca6ab0e69be3dd43c1c7cfa2412217b Mon Sep 17 00:00:00 2001 From: "Suraj kumar (sk)" <71056993+surajkumar-sk@users.noreply.github.com> Date: Wed, 12 May 2021 00:36:30 +0530 Subject: [PATCH] feat: Add a buffer fullness method (#3392) Closes #3389 --- lib/player.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lib/player.js b/lib/player.js index b13cfa1197..768931749f 100644 --- a/lib/player.js +++ b/lib/player.js @@ -2838,6 +2838,32 @@ shaka.Player = class extends shaka.util.FakeEventTarget { return this.config_; } + /** + * Returns the ratio of video length buffered compared to buffering Goal + * @return {number} + * @export + */ + getBufferFullness() { + if (this.video_) { + const bufferedLength = this.video_.buffered.length; + const bufferedEnd = + bufferedLength ? this.video_.buffered.end(bufferedLength - 1) : 0; + const bufferingGoal = this.getConfiguration().streaming.bufferingGoal; + const lengthToBeBuffered = Math.min(this.video_.currentTime + + bufferingGoal, this.seekRange().end); + + if (bufferedEnd >= lengthToBeBuffered) { + return 1; + } else if (bufferedEnd <= this.video_.currentTime) { + return 0; + } else if (bufferedEnd < lengthToBeBuffered) { + return ((bufferedEnd - this.video_.currentTime) / + (lengthToBeBuffered - this.video_.currentTime)); + } + } + return 0; + } + /** * Reset configuration to default. * @export