Skip to content

Commit

Permalink
Merge pull request #4809 from video-dev/feature/throttle-alt-audio-fe…
Browse files Browse the repository at this point in the history
…tch-based-on-main

Throttle audio stream controller based on main buffer state
  • Loading branch information
robwalch committed Jul 27, 2022
2 parents 3010022 + db090c2 commit 85daa49
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/controller/audio-stream-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,12 @@ class AudioStreamController
if (bufferInfo === null) {
return;
}
const mainBufferInfo = this.getFwdBufferInfo(
this.videoBuffer ? this.videoBuffer : this.media,
PlaylistLevelType.MAIN
);
const bufferLen = bufferInfo.len;
const maxBufLen = this.getMaxBufferLength();
const maxBufLen = this.getMaxBufferLength(mainBufferInfo?.len);
const audioSwitch = this.audioSwitch;

// if buffer length is less than maxBufLen try to load a new fragment
Expand Down Expand Up @@ -334,6 +338,18 @@ class AudioStreamController
}
}

// buffer audio up to one target duration ahead of main buffer
if (
mainBufferInfo &&
targetBufferTime > mainBufferInfo.end + trackDetails.targetduration
) {
return;
}
// wait for main buffer after buffing some audio
if ((!mainBufferInfo || !mainBufferInfo.len) && bufferInfo.len) {
return;
}

const frag = this.getNextFragment(targetBufferTime, trackDetails);
if (!frag) {
this.bufferFlushed = true;
Expand All @@ -347,16 +363,12 @@ class AudioStreamController
}
}

protected getMaxBufferLength(): number {
protected getMaxBufferLength(mainBufferLength?: number): number {
const maxConfigBuffer = super.getMaxBufferLength();
const mainBufferInfo = this.getFwdBufferInfo(
this.videoBuffer ? this.videoBuffer : this.media,
PlaylistLevelType.MAIN
);
if (mainBufferInfo === null) {
if (!mainBufferLength) {
return maxConfigBuffer;
}
return Math.max(maxConfigBuffer, mainBufferInfo.len);
return Math.max(maxConfigBuffer, mainBufferLength);
}

onMediaDetaching() {
Expand Down

0 comments on commit 85daa49

Please sign in to comment.