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_ =
Copy link
Member

Choose a reason for hiding this comment

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

Style-wise, we don't end local variables with underscores. That is the convention for private member variables.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated.

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.

This calculation looks correct to me now. Thanks!

}
}
return 0;
}

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