Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a method to get the ratio of video length buffered compared to bufferingGoal #3392

Merged
merged 11 commits into from
May 11, 2021
26 changes: 26 additions & 0 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -2837,6 +2837,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() {
const bufferedLength = this.video_ ?
this.video_.buffered.length : 0;
const bufferedEnd =
bufferedLength ? this.video_.buffered.end(bufferedLength - 1) : 0;
const bufferingGoal = this.getConfiguration().streaming.bufferingGoal;
const lengthToBeBuffered = this.video_ ? Math.min(this.video_.currentTime +
bufferingGoal, this.seekRange().end) : 0;

if (this.video_) {
surajkumar-sk marked this conversation as resolved.
Show resolved Hide resolved
if (bufferedEnd >= lengthToBeBuffered) {
return 1;
} else if (bufferedEnd <= this.video_.currentTime) {
return 0;
} else if (bufferedEnd < lengthToBeBuffered) {
return ((bufferedEnd - this.video_.currentTime) / bufferingGoal);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On further consideration, I think this is not quite right. In the edge case where seekRange.end() limits buffering, this would not approach 1.0, because the buffered amount would never reach bufferingGoal.

I think it should probably be:

    return (bufferedEnd - this.video_.currentTime) /
        (lengthToBeBuffered - this.video_.currentTime);

Does that make sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it makes complete sense. I used this formula in my previous commit thinking about that edge case but when I rechecked later I couldn't recall the exception case and changed the formula.
Thank you

}
}
return 0;
}

/**
* Reset configuration to default.
* @export
Expand Down