Skip to content

Commit

Permalink
Merge pull request #6023 from video-dev/bugfix/cherry-pick-fixes-1_4_13
Browse files Browse the repository at this point in the history
Cherry-pick fixes for v1.4.13
  • Loading branch information
robwalch committed Dec 7, 2023
2 parents 62e2db2 + 02e53c7 commit 8f275af
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 25 deletions.
8 changes: 8 additions & 0 deletions build-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ const babelTsWithPresetEnvTargets = ({ targets, stripConsole }) =>
'isFiniteNumber',
path.resolve('src/polyfills/number')
);
} else if (
espath.get('callee').matchesPattern('Number.isSafeInteger')
) {
espath.node.callee = importHelper.addNamed(
espath,
'isSafeInteger',
path.resolve('src/polyfills/number')
);
} else if (
espath.get('callee').matchesPattern('Number.MAX_SAFE_INTEGER')
) {
Expand Down
79 changes: 56 additions & 23 deletions src/controller/id3-track-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,42 @@ declare global {
}
}

type Cue = VTTCue | TextTrackCue;

const MIN_CUE_DURATION = 0.25;

function getCueClass() {
function getCueClass(): typeof VTTCue | typeof TextTrackCue | undefined {
if (typeof self === 'undefined') return undefined;
return self.VTTCue || self.TextTrackCue;
}

// Attempt to recreate Safari functionality by creating
// WebKitDataCue objects when available and store the decoded
// ID3 data in the value property of the cue
return (self.WebKitDataCue || self.VTTCue || self.TextTrackCue) as any;
function createCueWithDataFields(
Cue: typeof VTTCue | typeof TextTrackCue,
startTime: number,
endTime: number,
data: Object,
type?: string
): VTTCue | TextTrackCue | undefined {
let cue = new Cue(startTime, endTime, '');
try {
(cue as any).value = data;
if (type) {
(cue as any).type = type;
}
} catch (e) {
cue = new Cue(
startTime,
endTime,
JSON.stringify(type ? { type, ...data } : data)
);
}
return cue;
}

// VTTCue latest draft allows an infinite duration, fallback
// to MAX_VALUE if necessary
const MAX_CUE_ENDTIME = (() => {
const Cue = getCueClass();
try {
new Cue(0, Number.POSITIVE_INFINITY, '');
Cue && new Cue(0, Number.POSITIVE_INFINITY, '');
} catch (e) {
return Number.MAX_VALUE;
}
Expand All @@ -70,7 +87,11 @@ class ID3TrackController implements ComponentAPI {
private media: HTMLMediaElement | null = null;
private dateRangeCuesAppended: Record<
string,
{ cues: Record<string, Cue>; dateRange: DateRange; durationKnown: boolean }
{
cues: Record<string, VTTCue | TextTrackCue>;
dateRange: DateRange;
durationKnown: boolean;
}
> = {};

constructor(hls) {
Expand Down Expand Up @@ -177,6 +198,9 @@ class ID3TrackController implements ComponentAPI {
}

const Cue = getCueClass();
if (!Cue) {
return;
}

for (let i = 0; i < samples.length; i++) {
const type = samples[i].type;
Expand Down Expand Up @@ -207,13 +231,16 @@ class ID3TrackController implements ComponentAPI {
if (!ID3.isTimeStampFrame(frame)) {
// add a bounds to any unbounded cues
this.updateId3CueEnds(startTime, type);

const cue = new Cue(startTime, endTime, '');
cue.value = frame;
if (type) {
cue.type = type;
const cue = createCueWithDataFields(
Cue,
startTime,
endTime,
frame,
type
);
if (cue) {
this.id3Track.addCue(cue);
}
this.id3Track.addCue(cue);
}
}
}
Expand Down Expand Up @@ -350,22 +377,28 @@ class ID3TrackController implements ComponentAPI {
if (!isDateRangeCueAttribute(key)) {
continue;
}
let cue = cues[key] as any;
const cue = cues[key];
if (cue) {
if (durationKnown && !appendedDateRangeCues.durationKnown) {
cue.endTime = endTime;
}
} else {
} else if (Cue) {
let data = dateRange.attr[key];
cue = new Cue(startTime, endTime, '');
if (isSCTE35Attribute(key)) {
data = hexToArrayBuffer(data);
}
cue.value = { key, data };
cue.type = MetadataSchema.dateRange;
cue.id = id;
this.id3Track.addCue(cue);
cues[key] = cue;
const cue = createCueWithDataFields(
Cue,
startTime,
endTime,
{ key, data },
MetadataSchema.dateRange
);
if (cue) {
cue.id = id;
this.id3Track.addCue(cue);
cues[key] = cue;
}
}
}
dateRangeCuesAppended[id] = {
Expand Down
8 changes: 8 additions & 0 deletions src/polyfills/number.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
// https://caniuse.com/mdn-javascript_builtins_number_isfinite
export const isFiniteNumber =
Number.isFinite ||
function (value) {
return typeof value === 'number' && isFinite(value);
};

// https://caniuse.com/mdn-javascript_builtins_number_issafeinteger
export const isSafeInteger =
Number.isSafeInteger ||
function (value) {
return typeof value === 'number' && Math.abs(value) <= MAX_SAFE_INTEGER;
};

export const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991;
4 changes: 2 additions & 2 deletions src/utils/mp4-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ export function getStartDTS(
// convert base time to seconds
const startTime = baseTime / scale;
if (
isFinite(startTime) &&
Number.isFinite(startTime) &&
(result === null || startTime < result)
) {
return startTime;
Expand All @@ -396,7 +396,7 @@ export function getStartDTS(
);
if (
start !== null &&
isFinite(start) &&
Number.isFinite(start) &&
(result === null || start < result)
) {
return start;
Expand Down

0 comments on commit 8f275af

Please sign in to comment.