Fix incorrect GOP timestamp verification logic for first GOP (fixes #321)

This commit is contained in:
Vanilagy
2026-03-13 10:45:34 +01:00
parent 87ed3a1934
commit 35e16ca23e
3 changed files with 27 additions and 20 deletions
+1 -1
View File
@@ -189,7 +189,7 @@
}, },
trim: { trim: {
//start: 0, //start: 0,
//end: 10 end: 10
}, },
}); });
console.log(conversion); console.log(conversion);
+5 -1
View File
@@ -21,7 +21,11 @@
const sink = new Mediabunny.EncodedPacketSink(videoTrack); const sink = new Mediabunny.EncodedPacketSink(videoTrack);
for await (const packet of sink.packets()) { for await (const packet of sink.packets()) {
console.log(packet.timestamp); console.log(packet.type, packet.timestamp);
if (packet.timestamp >= 5) {
break;
}
} }
/* /*
+21 -18
View File
@@ -46,12 +46,16 @@ export abstract class Muxer {
private trackTimestampInfo = new WeakMap<OutputTrack, { private trackTimestampInfo = new WeakMap<OutputTrack, {
maxTimestamp: number; maxTimestamp: number;
maxTimestampBeforeLastKeyPacket: number; maxTimestampBeforeLastKeyPacket: number | null;
}>(); }>();
protected validateAndNormalizeTimestamp(track: OutputTrack, timestampInSeconds: number, isKeyPacket: boolean) { protected validateAndNormalizeTimestamp(track: OutputTrack, timestampInSeconds: number, isKeyPacket: boolean) {
timestampInSeconds += track.source._timestampOffset; timestampInSeconds += track.source._timestampOffset;
if (timestampInSeconds < 0) {
throw new Error(`Timestamps must be non-negative (got ${timestampInSeconds}s).`);
}
let timestampInfo = this.trackTimestampInfo.get(track); let timestampInfo = this.trackTimestampInfo.get(track);
if (!timestampInfo) { if (!timestampInfo) {
if (!isKeyPacket) { if (!isKeyPacket) {
@@ -60,29 +64,28 @@ export abstract class Muxer {
timestampInfo = { timestampInfo = {
maxTimestamp: timestampInSeconds, maxTimestamp: timestampInSeconds,
maxTimestampBeforeLastKeyPacket: timestampInSeconds, maxTimestampBeforeLastKeyPacket: null,
}; };
this.trackTimestampInfo.set(track, timestampInfo); this.trackTimestampInfo.set(track, timestampInfo);
} } else {
if (isKeyPacket) {
timestampInfo.maxTimestampBeforeLastKeyPacket = timestampInfo.maxTimestamp;
}
if (timestampInSeconds < 0) { if (
throw new Error(`Timestamps must be non-negative (got ${timestampInSeconds}s).`); timestampInfo.maxTimestampBeforeLastKeyPacket !== null
} && timestampInSeconds < timestampInfo.maxTimestampBeforeLastKeyPacket
) {
throw new Error(
`Timestamps cannot be smaller than the largest timestamp of the previous GOP (a GOP begins with a`
+ ` key packet and ends right before the next key packet). Got ${timestampInSeconds}s, but largest`
+ ` timestamp is ${timestampInfo.maxTimestampBeforeLastKeyPacket}s.`,
);
}
if (isKeyPacket) { timestampInfo.maxTimestamp = Math.max(timestampInfo.maxTimestamp, timestampInSeconds);
timestampInfo.maxTimestampBeforeLastKeyPacket = timestampInfo.maxTimestamp;
} }
if (timestampInSeconds < timestampInfo.maxTimestampBeforeLastKeyPacket) {
throw new Error(
`Timestamps cannot be smaller than the largest timestamp of the previous GOP (a GOP begins with a key`
+ ` packet and ends right before the next key packet). Got ${timestampInSeconds}s, but largest`
+ ` timestamp is ${timestampInfo.maxTimestampBeforeLastKeyPacket}s.`,
);
}
timestampInfo.maxTimestamp = Math.max(timestampInfo.maxTimestamp, timestampInSeconds);
return timestampInSeconds; return timestampInSeconds;
} }
} }