Skip to content

Commit

Permalink
Fix issues with detached and destroyed level selection (#6216)
Browse files Browse the repository at this point in the history
  • Loading branch information
robwalch committed Feb 16, 2024
1 parent b474062 commit beb3a94
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
30 changes: 20 additions & 10 deletions src/controller/abr-controller.ts
Expand Up @@ -533,27 +533,20 @@ class AbrController implements AbrComponentAPI {
}

private getAutoLevelKey(): string {
return `${this.getBwEstimate()}_${this.hls.mainForwardBufferInfo?.len}`;
return `${this.getBwEstimate()}_${this.getStarvationDelay().toFixed(2)}`;
}

private getNextABRAutoLevel(): number {
const { fragCurrent, partCurrent, hls } = this;
const { maxAutoLevel, config, minAutoLevel, media } = hls;
const { maxAutoLevel, config, minAutoLevel } = hls;
const currentFragDuration = partCurrent
? partCurrent.duration
: fragCurrent
? fragCurrent.duration
: 0;

// playbackRate is the absolute value of the playback rate; if media.playbackRate is 0, we use 1 to load as
// if we're playing back at the normal rate.
const playbackRate =
media && media.playbackRate !== 0 ? Math.abs(media.playbackRate) : 1.0;
const avgbw = this.getBwEstimate();
// bufferStarvationDelay is the wall-clock time left until the playback buffer is exhausted.
const bufferInfo = hls.mainForwardBufferInfo;
const bufferStarvationDelay =
(bufferInfo ? bufferInfo.len : 0) / playbackRate;
const bufferStarvationDelay = this.getStarvationDelay();

let bwFactor = config.abrBandWidthFactor;
let bwUpFactor = config.abrBandWidthUpFactor;
Expand Down Expand Up @@ -629,6 +622,20 @@ class AbrController implements AbrComponentAPI {
return hls.loadLevel;
}

private getStarvationDelay(): number {
const hls = this.hls;
const media = hls.media;
if (!media) {
return Infinity;
}
// playbackRate is the absolute value of the playback rate; if media.playbackRate is 0, we use 1 to load as
// if we're playing back at the normal rate.
const playbackRate =
media && media.playbackRate !== 0 ? Math.abs(media.playbackRate) : 1.0;
const bufferInfo = hls.mainForwardBufferInfo;
return (bufferInfo ? bufferInfo.len : 0) / playbackRate;
}

private getBwEstimate(): number {
return this.bwEstimator.canEstimate()
? this.bwEstimator.getEstimate()
Expand Down Expand Up @@ -737,6 +744,9 @@ class AbrController implements AbrComponentAPI {
mediaCapabilities,
);
levelInfo.supportedPromise.then((decodingInfo) => {
if (!this.hls) {
return;
}
levelInfo.supportedResult = decodingInfo;
const levels = this.hls.levels;
const index = levels.indexOf(levelInfo);
Expand Down
4 changes: 2 additions & 2 deletions src/controller/stream-controller.ts
Expand Up @@ -221,14 +221,13 @@ export default class StreamController

private doTickIdle() {
const { hls, levelLastLoaded, levels, media } = this;
const { config, nextLoadLevel: level } = hls;

// if start level not parsed yet OR
// if video not attached AND start fragment already requested OR start frag prefetch not enabled
// exit loop, as we either need more info (level not parsed) or we need media to be attached to load new fragment
if (
levelLastLoaded === null ||
(!media && (this.startFragRequested || !config.startFragPrefetch))
(!media && (this.startFragRequested || !hls.config.startFragPrefetch))
) {
return;
}
Expand All @@ -238,6 +237,7 @@ export default class StreamController
return;
}

const level = hls.nextLoadLevel;
if (!levels?.[level]) {
return;
}
Expand Down

0 comments on commit beb3a94

Please sign in to comment.