Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Lazy init CEA608 parsers (2) #6127

Merged
merged 2 commits into from
Jan 25, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions src/controller/timeline-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,21 +468,19 @@ export class TimelineController implements ComponentAPI {
}

private onFragLoading(event: Events.FRAG_LOADING, data: FragLoadingData) {
this.initCea608Parsers();
const { cea608Parser1, cea608Parser2, lastCc, lastSn, lastPartIndex } =
this;
if (!this.enabled || !cea608Parser1 || !cea608Parser2) {
return;
}
// if this frag isn't contiguous, clear the parser so cues with bad start/end times aren't added to the textTrack
if (data.frag.type === PlaylistLevelType.MAIN) {
if (this.enabled && data.frag.type === PlaylistLevelType.MAIN) {
const { cea608Parser1, cea608Parser2, lastSn } = this;
if (!cea608Parser1 || !cea608Parser2) {
return;
}
const { cc, sn } = data.frag;
const partIndex = data?.part?.index ?? -1;
const partIndex = data.part?.index ?? -1;
if (
!(
sn === lastSn + 1 ||
(sn === lastSn && partIndex === lastPartIndex + 1) ||
cc === lastCc
(sn === lastSn && partIndex === this.lastPartIndex + 1) ||
cc === this.lastCc
)
) {
cea608Parser1.reset();
Expand Down Expand Up @@ -669,9 +667,7 @@ export class TimelineController implements ComponentAPI {
event: Events.FRAG_PARSING_USERDATA,
data: FragParsingUserdataData,
) {
this.initCea608Parsers();
const { cea608Parser1, cea608Parser2 } = this;
if (!this.enabled || !cea608Parser1 || !cea608Parser2) {
if (!this.enabled) {
return;
}
const { frag, samples } = data;
Expand All @@ -686,9 +682,13 @@ export class TimelineController implements ComponentAPI {
for (let i = 0; i < samples.length; i++) {
const ccBytes = samples[i].bytes;
if (ccBytes) {
this.initCea608Parsers();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would think about moving the initCea608Parsers() call outside of the for loop. I realize that it will only trigger once, but it will run needless function overhead and checks. Seems like you could do one check on samples.length and initialize before the for loop.

Copy link
Collaborator Author

@robwalch robwalch Jan 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the same but samples can contain type 5 rather than type 4 samples (no ccBytes) so we'd have to search the array first anyway (array.some which would loop through everything if no samples with bytes are found).

Copy link
Collaborator Author

@robwalch robwalch Jan 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to move early return and guards out of initCea608Parsers and into this method to prevent entering this loop when enableCEA708Captions is false and to skip calling initCea608Parsers when cea608Parser1 is already defined.

if (!this.cea608Parser1 || !this.cea608Parser2) {
return;
}
const ccdatas = this.extractCea608Data(ccBytes);
cea608Parser1.addData(samples[i].pts, ccdatas[0]);
cea608Parser2.addData(samples[i].pts, ccdatas[1]);
this.cea608Parser1.addData(samples[i].pts, ccdatas[0]);
this.cea608Parser2.addData(samples[i].pts, ccdatas[1]);
}
}
}
Expand Down
Loading