diff --git a/dev/convert.html b/dev/convert.html
index 9f18bfd..658b6b4 100644
--- a/dev/convert.html
+++ b/dev/convert.html
@@ -189,7 +189,7 @@
},
trim: {
//start: 0,
- //end: 10
+ end: 10
},
});
console.log(conversion);
diff --git a/dev/demux.html b/dev/demux.html
index baa5541..3b87013 100644
--- a/dev/demux.html
+++ b/dev/demux.html
@@ -21,7 +21,11 @@
const sink = new Mediabunny.EncodedPacketSink(videoTrack);
for await (const packet of sink.packets()) {
- console.log(packet.timestamp);
+ console.log(packet.type, packet.timestamp);
+
+ if (packet.timestamp >= 5) {
+ break;
+ }
}
/*
diff --git a/src/muxer.ts b/src/muxer.ts
index ee0e7bd..83def00 100644
--- a/src/muxer.ts
+++ b/src/muxer.ts
@@ -46,12 +46,16 @@ export abstract class Muxer {
private trackTimestampInfo = new WeakMap();
protected validateAndNormalizeTimestamp(track: OutputTrack, timestampInSeconds: number, isKeyPacket: boolean) {
timestampInSeconds += track.source._timestampOffset;
+ if (timestampInSeconds < 0) {
+ throw new Error(`Timestamps must be non-negative (got ${timestampInSeconds}s).`);
+ }
+
let timestampInfo = this.trackTimestampInfo.get(track);
if (!timestampInfo) {
if (!isKeyPacket) {
@@ -60,29 +64,28 @@ export abstract class Muxer {
timestampInfo = {
maxTimestamp: timestampInSeconds,
- maxTimestampBeforeLastKeyPacket: timestampInSeconds,
+ maxTimestampBeforeLastKeyPacket: null,
};
this.trackTimestampInfo.set(track, timestampInfo);
- }
+ } else {
+ if (isKeyPacket) {
+ timestampInfo.maxTimestampBeforeLastKeyPacket = timestampInfo.maxTimestamp;
+ }
- if (timestampInSeconds < 0) {
- throw new Error(`Timestamps must be non-negative (got ${timestampInSeconds}s).`);
- }
+ if (
+ 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.maxTimestampBeforeLastKeyPacket = timestampInfo.maxTimestamp;
+ timestampInfo.maxTimestamp = Math.max(timestampInfo.maxTimestamp, timestampInSeconds);
}
- 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;
}
}