Skip to content

Commit

Permalink
fix: Evict buffer on QUOTA_EXCEEDED_ERROR error (#7361)
Browse files Browse the repository at this point in the history
Evict buffer on QUOTA_EXCEEDED_ERROR error and also apply bufferingScale
to buffer behind config
  • Loading branch information
avelad authored and joeyparrish committed Sep 26, 2024
1 parent 8938ae9 commit cef9959
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions lib/media/streaming_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ shaka.media.StreamingEngine = class {
this.deferredCloseSegmentIndex_ = new Map();

/** @private {number} */
this.bufferingGoalScale_ = 1;
this.bufferingScale_ = 1;

/** @private {?shaka.extern.Variant} */
this.currentVariant_ = null;
Expand Down Expand Up @@ -1358,7 +1358,7 @@ shaka.media.StreamingEngine = class {
this.config_.bufferingGoal);

const scaledBufferingGoal = Math.max(1,
unscaledBufferingGoal * this.bufferingGoalScale_);
unscaledBufferingGoal * this.bufferingScale_);

// Check if we've buffered to the end of the presentation.
const timeUntilEnd =
Expand Down Expand Up @@ -1799,7 +1799,7 @@ shaka.media.StreamingEngine = class {
}
this.mediaStates_.delete(ContentType.TEXT);
} else if (error.code == shaka.util.Error.Code.QUOTA_EXCEEDED_ERROR) {
this.handleQuotaExceeded_(mediaState, error);
await this.handleQuotaExceeded_(mediaState, error);
} else {
shaka.log.error(logPrefix, 'failed fetch and append: code=' +
error.code);
Expand Down Expand Up @@ -1912,9 +1912,10 @@ shaka.media.StreamingEngine = class {
*
* @param {shaka.media.StreamingEngine.MediaState_} mediaState
* @param {!shaka.util.Error} error
* @return {!Promise}
* @private
*/
handleQuotaExceeded_(mediaState, error) {
async handleQuotaExceeded_(mediaState, error) {
const logPrefix = shaka.media.StreamingEngine.logPrefix_(mediaState);

// The segment cannot fit into the SourceBuffer. Ideally, MediaSource would
Expand Down Expand Up @@ -1943,11 +1944,11 @@ shaka.media.StreamingEngine = class {
}
// Reduction schedule: 80%, 60%, 40%, 20%, 16%, 12%, 8%, 4%, fail.
// Note: percentages are used for comparisons to avoid rounding errors.
const percentBefore = Math.round(100 * this.bufferingGoalScale_);
const percentBefore = Math.round(100 * this.bufferingScale_);
if (percentBefore > 20) {
this.bufferingGoalScale_ -= 0.2;
this.bufferingScale_ -= 0.2;
} else if (percentBefore > 4) {
this.bufferingGoalScale_ -= 0.04;
this.bufferingScale_ -= 0.04;
} else {
shaka.log.error(
logPrefix, 'MediaSource threw QuotaExceededError too many times');
Expand All @@ -1956,12 +1957,14 @@ shaka.media.StreamingEngine = class {
this.playerInterface_.onError(error);
return;
}
const percentAfter = Math.round(100 * this.bufferingGoalScale_);
const percentAfter = Math.round(100 * this.bufferingScale_);
shaka.log.warning(
logPrefix,
'MediaSource threw QuotaExceededError:',
'reducing buffering goals by ' + (100 - percentAfter) + '%');
mediaState.recovering = true;
const presentationTime = this.playerInterface_.getPresentationTime();
await this.evict_(mediaState, presentationTime);
} else {
shaka.log.debug(
logPrefix,
Expand Down Expand Up @@ -2490,7 +2493,8 @@ shaka.media.StreamingEngine = class {
// Use the max segment duration, if it is longer than the bufferBehind, to
// avoid accidentally clearing too much data when dealing with a manifest
// with a long keyframe interval.
const bufferBehind = Math.max(this.config_.bufferBehind,
const bufferBehind = Math.max(
this.config_.bufferBehind * this.bufferingScale_,
this.manifest_.presentationTimeline.getMaxSegmentDuration());

const startTime =
Expand Down

0 comments on commit cef9959

Please sign in to comment.