Skip to content

Commit

Permalink
Add MINIMUM_MAX_BUFFER_AHEAD config
Browse files Browse the repository at this point in the history
  • Loading branch information
peaBerberian committed Dec 15, 2023
1 parent 267c2bd commit abf56b8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
23 changes: 7 additions & 16 deletions src/core/stream/orchestrator/stream_orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,17 @@ export default function StreamOrchestrator(
wantedBufferAhead,
maxVideoBufferSize } = options;

const { MAXIMUM_MAX_BUFFER_AHEAD,
const { MINIMUM_MAX_BUFFER_AHEAD,
MAXIMUM_MAX_BUFFER_AHEAD,
MAXIMUM_MAX_BUFFER_BEHIND } = config.getCurrent();

// Keep track of a unique BufferGarbageCollector created per
// SegmentBuffer.
const garbageCollectors =
new WeakMapMemory((segmentBuffer : SegmentBuffer) => {
const { bufferType } = segmentBuffer;
const defaultMaxBehind = MAXIMUM_MAX_BUFFER_BEHIND[bufferType] !== undefined ?
MAXIMUM_MAX_BUFFER_BEHIND[bufferType] as number :
Infinity;
const defaultMaxAhead = MAXIMUM_MAX_BUFFER_AHEAD[bufferType] !== undefined ?
MAXIMUM_MAX_BUFFER_AHEAD[bufferType] as number :
Infinity;
const defaultMaxBehind = MAXIMUM_MAX_BUFFER_BEHIND[bufferType] ?? Infinity;
const maxAheadHigherBound = MAXIMUM_MAX_BUFFER_AHEAD[bufferType] ?? Infinity;
return (gcCancelSignal : CancellationSignal) => {
BufferGarbageCollector(
{ segmentBuffer,
Expand All @@ -131,15 +128,9 @@ export default function StreamOrchestrator(
Math.min(val, defaultMaxBehind),
gcCancelSignal),
maxBufferAhead: createMappedReference(maxBufferAhead, (val) => {
const actualMaxBuff = bufferType === "text" ?
// Text segments are both much lighter on resources and might
// actually be much larger than other types of segments in terms
// of duration. Let's make an exception here by authorizing a
// larger text buffer ahead, to avoid unnecesarily reloading the
// same text track.
Math.max(val, 2 * 60) :
val;
return Math.min(actualMaxBuff, defaultMaxAhead);
const lowerBound = Math.max(val,
MINIMUM_MAX_BUFFER_AHEAD[bufferType] ?? 0);
return Math.min(lowerBound, maxAheadHigherBound);
}, gcCancelSignal) },
gcCancelSignal
);
Expand Down
17 changes: 17 additions & 0 deletions src/default_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,23 @@ const DEFAULT_CONFIG = {
} as Partial<Record<"audio"|"video"|"image"|"text", number>>,
/* eslint-enable @typescript-eslint/consistent-type-assertions */

/* eslint-disable @typescript-eslint/consistent-type-assertions */
/**
* Minimum possible buffer ahead for each type of buffer, to avoid Garbage
* Collecting too much data when it would have adverse effects.
* Equal to `0` if not defined here.
* @type {Object}
*/
MINIMUM_MAX_BUFFER_AHEAD: {
// Text segments are both much lighter on resources and might
// actually be much larger than other types of segments in terms
// of duration. Let's make an exception here by authorizing a
// larger text buffer ahead, to avoid unnecesarily reloading the
// same text track.
text: 2 * 60,
} as Partial<Record<"audio"|"video"|"image"|"text", number>>,
/* eslint-enable @typescript-eslint/consistent-type-assertions */

/* eslint-disable @typescript-eslint/consistent-type-assertions */
/**
* Maximum possible buffer behind for each type of buffer, to avoid too much
Expand Down

0 comments on commit abf56b8

Please sign in to comment.