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;
}
} }
/* /*
+14 -11
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,28 +64,27 @@ 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 (timestampInSeconds < 0) {
throw new Error(`Timestamps must be non-negative (got ${timestampInSeconds}s).`);
}
if (isKeyPacket) { if (isKeyPacket) {
timestampInfo.maxTimestampBeforeLastKeyPacket = timestampInfo.maxTimestamp; timestampInfo.maxTimestampBeforeLastKeyPacket = timestampInfo.maxTimestamp;
} }
if (timestampInSeconds < timestampInfo.maxTimestampBeforeLastKeyPacket) { if (
timestampInfo.maxTimestampBeforeLastKeyPacket !== null
&& timestampInSeconds < timestampInfo.maxTimestampBeforeLastKeyPacket
) {
throw new Error( throw new Error(
`Timestamps cannot be smaller than the largest timestamp of the previous GOP (a GOP begins with a key` `Timestamps cannot be smaller than the largest timestamp of the previous GOP (a GOP begins with a`
+ ` packet and ends right before the next key packet). Got ${timestampInSeconds}s, but largest` + ` key packet and ends right before the next key packet). Got ${timestampInSeconds}s, but largest`
+ ` timestamp is ${timestampInfo.maxTimestampBeforeLastKeyPacket}s.`, + ` timestamp is ${timestampInfo.maxTimestampBeforeLastKeyPacket}s.`,
); );
} }
timestampInfo.maxTimestamp = Math.max(timestampInfo.maxTimestamp, timestampInSeconds); timestampInfo.maxTimestamp = Math.max(timestampInfo.maxTimestamp, timestampInSeconds);
}
return timestampInSeconds; return timestampInSeconds;
} }