diff --git a/src/controller/timeline-controller.ts b/src/controller/timeline-controller.ts index 2e21be74b51..cf1037376c9 100644 --- a/src/controller/timeline-controller.ts +++ b/src/controller/timeline-controller.ts @@ -60,8 +60,8 @@ export class TimelineController implements ComponentAPI { private unparsedVttFrags: Array = []; private captionsTracks: Record = {}; private nonNativeCaptionsTracks: Record = {}; - private cea608Parser1!: Cea608Parser; - private cea608Parser2!: Cea608Parser; + private cea608Parser1?: Cea608Parser; + private cea608Parser2?: Cea608Parser; private lastCc: number = -1; // Last video (CEA-608) fragment CC private lastSn: number = -1; // Last video (CEA-608) fragment MSN private lastPartIndex: number = -1; // Last video (CEA-608) fragment Part Index @@ -98,15 +98,6 @@ export class TimelineController implements ComponentAPI { }, }; - if (this.config.enableCEA708Captions) { - const channel1 = new OutputFilter(this, 'textTrack1'); - const channel2 = new OutputFilter(this, 'textTrack2'); - const channel3 = new OutputFilter(this, 'textTrack3'); - const channel4 = new OutputFilter(this, 'textTrack4'); - this.cea608Parser1 = new Cea608Parser(1, channel1, channel2); - this.cea608Parser2 = new Cea608Parser(3, channel3, channel4); - } - hls.on(Events.MEDIA_ATTACHING, this.onMediaAttaching, this); hls.on(Events.MEDIA_DETACHING, this.onMediaDetaching, this); hls.on(Events.MANIFEST_LOADING, this.onManifestLoading, this); @@ -139,6 +130,17 @@ export class TimelineController implements ComponentAPI { this.hls = this.config = this.cea608Parser1 = this.cea608Parser2 = null; } + private lazyInit608() { + if (this.config.enableCEA708Captions) { + const channel1 = new OutputFilter(this, 'textTrack1'); + const channel2 = new OutputFilter(this, 'textTrack2'); + const channel3 = new OutputFilter(this, 'textTrack3'); + const channel4 = new OutputFilter(this, 'textTrack4'); + this.cea608Parser1 = new Cea608Parser(1, channel1, channel2); + this.cea608Parser2 = new Cea608Parser(3, channel3, channel4); + } + } + public addCues( trackName: string, startTime: number, @@ -655,11 +657,9 @@ export class TimelineController implements ComponentAPI { event: Events.FRAG_PARSING_USERDATA, data: FragParsingUserdataData ) { - const { cea608Parser1, cea608Parser2 } = this; - if (!this.enabled || !(cea608Parser1 && cea608Parser2)) { + if (!this.enabled) { return; } - const { frag, samples } = data; if ( frag.type === PlaylistLevelType.MAIN && @@ -667,6 +667,11 @@ export class TimelineController implements ComponentAPI { ) { return; } + this.lazyInit608(); + const { cea608Parser1, cea608Parser2 } = this; + if (!cea608Parser1 || !cea608Parser2) { + return; + } // If the event contains captions (found in the bytes property), push all bytes into the parser immediately // It will create the proper timestamps based on the PTS value for (let i = 0; i < samples.length; i++) {