Skip to content

Commit db090c2

Browse files
committed
Throttle audio stream controller based on main buffer state
1 parent 3010022 commit db090c2

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

src/controller/audio-stream-controller.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,12 @@ class AudioStreamController
300300
if (bufferInfo === null) {
301301
return;
302302
}
303+
const mainBufferInfo = this.getFwdBufferInfo(
304+
this.videoBuffer ? this.videoBuffer : this.media,
305+
PlaylistLevelType.MAIN
306+
);
303307
const bufferLen = bufferInfo.len;
304-
const maxBufLen = this.getMaxBufferLength();
308+
const maxBufLen = this.getMaxBufferLength(mainBufferInfo?.len);
305309
const audioSwitch = this.audioSwitch;
306310

307311
// if buffer length is less than maxBufLen try to load a new fragment
@@ -334,6 +338,18 @@ class AudioStreamController
334338
}
335339
}
336340

341+
// buffer audio up to one target duration ahead of main buffer
342+
if (
343+
mainBufferInfo &&
344+
targetBufferTime > mainBufferInfo.end + trackDetails.targetduration
345+
) {
346+
return;
347+
}
348+
// wait for main buffer after buffing some audio
349+
if ((!mainBufferInfo || !mainBufferInfo.len) && bufferInfo.len) {
350+
return;
351+
}
352+
337353
const frag = this.getNextFragment(targetBufferTime, trackDetails);
338354
if (!frag) {
339355
this.bufferFlushed = true;
@@ -347,16 +363,12 @@ class AudioStreamController
347363
}
348364
}
349365

350-
protected getMaxBufferLength(): number {
366+
protected getMaxBufferLength(mainBufferLength?: number): number {
351367
const maxConfigBuffer = super.getMaxBufferLength();
352-
const mainBufferInfo = this.getFwdBufferInfo(
353-
this.videoBuffer ? this.videoBuffer : this.media,
354-
PlaylistLevelType.MAIN
355-
);
356-
if (mainBufferInfo === null) {
368+
if (!mainBufferLength) {
357369
return maxConfigBuffer;
358370
}
359-
return Math.max(maxConfigBuffer, mainBufferInfo.len);
371+
return Math.max(maxConfigBuffer, mainBufferLength);
360372
}
361373

362374
onMediaDetaching() {

0 commit comments

Comments
 (0)