Add mutex to packet lookup algorithm, fix incorrect next packet retrieval following packet lookup

This commit is contained in:
Vanilagy
2026-01-14 21:53:23 +01:00
parent 4a85aad012
commit 0719a17c85
4 changed files with 211 additions and 133 deletions
+38 -2
View File
@@ -204,8 +204,8 @@ test('MPEG-TS video seeking', async () => {
const firstTimestamp = await videoTrack.getFirstTimestamp();
const firstPacket = await sink.getPacket(firstTimestamp);
assert(firstPacket);
expect(firstPacket.timestamp).toBe(firstTimestamp);
expect(firstPacket.sequenceNumber).toBe((await sink.getFirstPacket())?.sequenceNumber);
const lastPacket = await sink.getPacket(Infinity);
assert(lastPacket);
@@ -227,6 +227,8 @@ test('MPEG-TS video seeking', async () => {
currentPacket = await sink.getNextPacket(currentPacket);
}
expect(allPackets).toHaveLength(298);
for (const packet of allPackets) {
const seekedPacked = await sink.getPacket(packet.timestamp);
assert(seekedPacked);
@@ -249,8 +251,8 @@ test('MPEG-TS audio seeking', async () => {
const firstTimestamp = await audioTrack.getFirstTimestamp();
const firstPacket = await sink.getPacket(firstTimestamp);
assert(firstPacket);
expect(firstPacket.timestamp).toBe(firstTimestamp);
expect(firstPacket.sequenceNumber).toBe((await sink.getFirstPacket())?.sequenceNumber);
const lastPacket = await sink.getPacket(Infinity);
assert(lastPacket);
@@ -272,6 +274,8 @@ test('MPEG-TS audio seeking', async () => {
currentPacket = await sink.getNextPacket(currentPacket);
}
expect(allPackets).toHaveLength(234);
for (const packet of allPackets) {
const seekedPacket = await sink.getPacket(packet.timestamp);
assert(seekedPacket);
@@ -280,6 +284,38 @@ test('MPEG-TS audio seeking', async () => {
}
});
test('MPEG-TS seeking race condition test', async () => {
using input = new Input({
source: new FilePathSource(path.join(__dirname, '../public/0.ts')),
formats: ALL_FORMATS,
});
const videoTrack = await input.getPrimaryVideoTrack();
assert(videoTrack);
const sink = new EncodedPacketSink(videoTrack);
const allPackets: EncodedPacket[] = [];
let currentPacket: EncodedPacket | null = await sink.getFirstPacket();
while (currentPacket) {
allPackets.push(currentPacket);
currentPacket = await sink.getNextPacket(currentPacket);
}
// Perform all seeks concurrently
const seekPromises = allPackets.map(packet => sink.getPacket(packet.timestamp));
const seekedPackets = await Promise.all(seekPromises);
for (let i = 0; i < allPackets.length; i++) {
const originalPacket = allPackets[i]!;
const seekedPacket = seekedPackets[i]!;
assert(seekedPacket);
expect(seekedPacket.timestamp).toBe(originalPacket.timestamp);
expect(seekedPacket.sequenceNumber).toBe(originalPacket.sequenceNumber);
}
});
test('MPEG-TS video key packets', async () => {
using input = new Input({
source: new FilePathSource(path.join(__dirname, '../public/193039199_mp4_h264_aac_fhd_7.ts')),