Skip to content

Commit d41ac0d

Browse files
committed
Parse all init segment track headers so that all track base decode timestamps are updated
Fixes timestamp alignment of inband metadata and captions with fmp4 "pass-through" remuxer
1 parent 503b963 commit d41ac0d

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

src/utils/mp4-tools.ts

+28-10
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,24 @@ export interface InitDataTrack {
226226
supplemental: string | undefined;
227227
}
228228

229-
type HdlrType = ElementaryStreamTypes.AUDIO | ElementaryStreamTypes.VIDEO;
229+
type HdlrMetadata = 'meta';
230+
type HdlrType =
231+
| ElementaryStreamTypes.AUDIO
232+
| ElementaryStreamTypes.VIDEO
233+
| HdlrMetadata;
234+
235+
type StsdData = {
236+
codec: string;
237+
encrypted: boolean;
238+
supplemental: string | undefined;
239+
};
230240

231241
export interface InitData extends Array<any> {
232242
[index: number]:
233243
| {
234244
timescale: number;
235245
type: HdlrType;
246+
stsd: StsdData;
236247
default?: {
237248
duration: number;
238249
flags: number;
@@ -266,10 +277,20 @@ export function parseInitSegment(initSegment: Uint8Array): InitData {
266277
}[hdlrType];
267278
if (type) {
268279
// Parse codec details
269-
const stsd = findBox(trak, ['mdia', 'minf', 'stbl', 'stsd'])[0];
270-
const stsdData = parseStsd(stsd);
271-
result[trackId] = { timescale, type };
272-
result[type] = { timescale, id: trackId, ...stsdData };
280+
const stsdBox = findBox(trak, ['mdia', 'minf', 'stbl', 'stsd'])[0];
281+
const stsd = parseStsd(stsdBox);
282+
if (type) {
283+
// Add 'audio', 'video', and 'audiovideo' track records that will map to SourceBuffers
284+
result[trackId] = { timescale, type, stsd };
285+
result[type] = { timescale, id: trackId, ...stsd };
286+
} else {
287+
// Add 'meta' and other track records required by `offsetStartDTS`
288+
result[trackId] = {
289+
timescale,
290+
type: hdlrType as HdlrType,
291+
stsd,
292+
};
293+
}
273294
}
274295
}
275296
}
@@ -291,11 +312,7 @@ export function parseInitSegment(initSegment: Uint8Array): InitData {
291312
return result;
292313
}
293314

294-
function parseStsd(stsd: Uint8Array): {
295-
codec: string;
296-
encrypted: boolean;
297-
supplemental: string | undefined;
298-
} {
315+
function parseStsd(stsd: Uint8Array): StsdData {
299316
const sampleEntries = stsd.subarray(8);
300317
const sampleEntriesEnd = sampleEntries.subarray(8 + 78);
301318
const fourCC = bin2str(sampleEntries.subarray(4, 8));
@@ -818,6 +835,7 @@ export function computeRawDurationFromSamples(trun): number {
818835
return duration;
819836
}
820837

838+
// TODO: Remove `offsetStartDTS` in favor of using `timestampOffset` (issue #5715)
821839
export function offsetStartDTS(
822840
initData: InitData,
823841
fmp4: Uint8Array,

0 commit comments

Comments
 (0)