Skip to content

Commit

Permalink
Handle ManagedMediaSource endStreaming events without aborting requests
Browse files Browse the repository at this point in the history
  • Loading branch information
robwalch committed Feb 6, 2024
1 parent c779064 commit 6de6587
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 17 deletions.
10 changes: 10 additions & 0 deletions api-extractor/report/hls.js.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,8 @@ export class BaseStreamController extends TaskLoop implements NetworkComponentAP
// (undocumented)
protected bufferFragmentData(data: RemuxedTrack, frag: Fragment, part: Part | null, chunkMeta: ChunkMetadata, noBacktracking?: boolean): void;
// (undocumented)
protected buffering: boolean;
// (undocumented)
protected checkLiveUpdate(details: LevelDetails): void;
// (undocumented)
protected clearTrackerIfNeeded(frag: Fragment): void;
Expand Down Expand Up @@ -453,6 +455,8 @@ export class BaseStreamController extends TaskLoop implements NetworkComponentAP
// (undocumented)
protected onTickEnd(): void;
// (undocumented)
pauseBuffering(): void;
// (undocumented)
protected playlistType: PlaylistLevelType;
// (undocumented)
protected recoverWorkerError(data: ErrorData): void;
Expand All @@ -477,6 +481,8 @@ export class BaseStreamController extends TaskLoop implements NetworkComponentAP
// (undocumented)
protected resetWhenMissingContext(chunkMeta: ChunkMetadata): void;
// (undocumented)
resumeBuffering(): void;
// (undocumented)
protected retryDate: number;
// (undocumented)
protected seekToStartPos(): void;
Expand Down Expand Up @@ -2945,6 +2951,10 @@ export type MP4RemuxerConfig = {
//
// @public (undocumented)
export interface NetworkComponentAPI extends ComponentAPI {
// (undocumented)
pauseBuffering?(): void;
// (undocumented)
resumeBuffering?(): void;
// (undocumented)
startLoad(startPosition: number): void;
// (undocumented)
Expand Down
6 changes: 4 additions & 2 deletions src/controller/audio-stream-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,14 @@ class AudioStreamController
const { hls, levels, media, trackId } = this;
const config = hls.config;

// 1. if video not attached AND
// 1. if buffering is suspended
// 2. if video not attached AND
// start fragment already requested OR start frag prefetch not enabled
// 2. if tracks or track not loaded and selected
// 3. if tracks or track not loaded and selected
// then exit loop
// => if media not attached but start frag prefetch is enabled and start frag not requested yet, we will not exit loop
if (
!this.buffering ||
(!media && (this.startFragRequested || !config.startFragPrefetch)) ||
!levels?.[trackId]
) {
Expand Down
9 changes: 9 additions & 0 deletions src/controller/base-stream-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export default class BaseStreamController
protected startFragRequested: boolean = false;
protected decrypter: Decrypter;
protected initPTS: RationalTimestamp[] = [];
protected buffering: boolean = true;

constructor(
hls: Hls,
Expand Down Expand Up @@ -161,6 +162,14 @@ export default class BaseStreamController
this.state = State.STOPPED;
}

public pauseBuffering() {
this.buffering = false;
}

public resumeBuffering() {
this.buffering = true;
}

protected _streamEnded(
bufferInfo: BufferInfo,
levelDetails: LevelDetails,
Expand Down
1 change: 1 addition & 0 deletions src/controller/buffer-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ export default class BufferController extends Logger implements ComponentAPI {
this.resetBuffer(type);
});
this._initSourceBuffer();
this.hls.resumeBuffering();
}

private resetBuffer(type: SourceBufferName) {
Expand Down
2 changes: 1 addition & 1 deletion src/controller/stream-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export default class StreamController
return;
}

if (!levels?.[level]) {
if (!this.buffering || !levels?.[level]) {
return;
}

Expand Down
23 changes: 9 additions & 14 deletions src/hls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ export default class Hls implements HlsEventEmitter {

private coreComponents: ComponentAPI[];
private networkControllers: NetworkComponentAPI[];
private started: boolean = false;
private _emitter: HlsEventEmitter = new EventEmitter();
private _autoLevelCapping: number = -1;
private _maxHdcpLevel: HdcpLevel = null;
Expand Down Expand Up @@ -441,7 +440,6 @@ export default class Hls implements HlsEventEmitter {
*/
startLoad(startPosition: number = -1) {
this.logger.log(`startLoad(${startPosition})`);
this.started = true;
this.networkControllers.forEach((controller) => {
controller.startLoad(startPosition);
});
Expand All @@ -452,33 +450,30 @@ export default class Hls implements HlsEventEmitter {
*/
stopLoad() {
this.logger.log('stopLoad');
this.started = false;
this.networkControllers.forEach((controller) => {
controller.stopLoad();
});
}

/**
* Resumes stream controller segment loading if previously started.
* Resumes stream controller segment loading after `pauseBuffering` has been called.
*/
resumeBuffering() {
if (this.started) {
this.networkControllers.forEach((controller) => {
if ('fragmentLoader' in controller) {
controller.startLoad(-1);
}
});
}
this.networkControllers.forEach((controller) => {
if (controller.resumeBuffering) {
controller.resumeBuffering();
}
});
}

/**
* Stops stream controller segment loading without changing 'started' state like stopLoad().
* Prevents stream controller from loading new segments until `resumeBuffering` is called.
* This allows for media buffering to be paused without interupting playlist loading.
*/
pauseBuffering() {
this.networkControllers.forEach((controller) => {
if ('fragmentLoader' in controller) {
controller.stopLoad();
if (controller.pauseBuffering) {
controller.pauseBuffering();
}
});
}
Expand Down
2 changes: 2 additions & 0 deletions src/types/component-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ export interface AbrComponentAPI extends ComponentAPI {
export interface NetworkComponentAPI extends ComponentAPI {
startLoad(startPosition: number): void;
stopLoad(): void;
pauseBuffering?(): void;
resumeBuffering?(): void;
}

0 comments on commit 6de6587

Please sign in to comment.