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
23 changes: 23 additions & 0 deletions ui/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,29 @@ shaka.ui.Controls = class extends shaka.util.FakeEventTarget {
return this.seekBar_ ? this.seekBar_.getValue() : this.video_.currentTime;
}

/**
* Checking the efficiency of a video
surajkumar-sk marked this conversation as resolved.
Show resolved Hide resolved
* @return {?number}
surajkumar-sk marked this conversation as resolved.
Show resolved Hide resolved
* @export
*/
getBufferFillPercentage() {
surajkumar-sk marked this conversation as resolved.
Show resolved Hide resolved
const bufferedLength_ = this.video_.buffered.length;
surajkumar-sk marked this conversation as resolved.
Show resolved Hide resolved
const bufferedEnd_ =
bufferedLength_ ? this.video_.buffered.end(bufferedLength_ - 1) : 0;
const bufferingGoal_ = this.video_.currentTime +
Math.min(this.player_.getConfiguration().streaming.bufferingGoal,
surajkumar-sk marked this conversation as resolved.
Show resolved Hide resolved
(this.player_.seekRange().end - this.video_.currentTime));

if (bufferedEnd_ >= bufferingGoal_) {
return 100;
} else if (bufferedEnd_ < bufferingGoal_) {
return ((bufferedEnd_/bufferingGoal_)*100);
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 is wrong, because it doesn't account for the starting point. The start of the range should be currentTime, not 0.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@joeyparrish the formula of bufferingGoal_ does account for the starting point. I named the variable wrong, I'll try coming up with a better name. I am bad at it so I am open to suggestions.

Copy link
Contributor Author

@surajkumar-sk surajkumar-sk May 7, 2021

Choose a reason for hiding this comment

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

I think the formula is wrong . The fraction should be between buffering goal and the length buffered after the current time . So the formula will be (bufferedEnd_-currentTime)/bufferingGoal

} else if (bufferedEnd_ <= this.video_.currentTime) {
surajkumar-sk marked this conversation as resolved.
Show resolved Hide resolved
return 0;
}
return 0;
}

/**
* @param {?number} time
* @export
Expand Down