Make more use of sequenceNumber in getNextPacket

This commit is contained in:
Vanilagy
2025-12-22 23:20:51 +01:00
parent 4f8c8836ec
commit 58d73b7de3
4 changed files with 14 additions and 14 deletions
+2 -6
View File
@@ -14,7 +14,6 @@ import { PacketRetrievalOptions } from '../media-sink';
import {
assert,
AsyncMutex4,
binarySearchExact,
binarySearchLessOrEqual,
Bitstream,
ResultValue,
@@ -261,16 +260,13 @@ class AdtsAudioTrackBacking implements InputAudioTrackBacking {
using lock = this.demuxer.readingMutex.lock();
if (lock.pending) await lock.ready;
const sampleIndex = binarySearchExact(
this.demuxer.loadedSamples,
packet.timestamp,
x => x.timestamp,
);
const sampleIndex = packet.sequenceNumber;
if (sampleIndex === -1) {
throw new Error('Packet was not created from this track.');
}
const nextIndex = sampleIndex + 1;
// Ensure the next sample exists
while (
nextIndex >= this.demuxer.loadedSamples.length
+4
View File
@@ -644,6 +644,10 @@ class FlacAudioTrackBacking implements InputAudioTrackBacking {
using lock = this.demuxer.readingMutex.lock();
if (lock.pending) await lock.ready;
if (packet.sequenceNumber < 0) {
throw new Error('Packet was not created from this track.');
}
const nextIndex = packet.sequenceNumber + 1;
if (
this.demuxer.lastSampleLoaded
+3 -7
View File
@@ -15,7 +15,6 @@ import { PacketRetrievalOptions } from '../media-sink';
import {
assert,
AsyncMutex4,
binarySearchExact,
binarySearchLessOrEqual,
ResultValue,
UNDETERMINED_LANGUAGE,
@@ -318,16 +317,13 @@ class Mp3AudioTrackBacking implements InputAudioTrackBacking {
using lock = this.demuxer.readingMutex.lock();
if (lock.pending) await lock.ready;
const sampleIndex = binarySearchExact(
this.demuxer.loadedSamples,
packet.timestamp,
x => x.timestamp,
);
if (sampleIndex === -1) {
const sampleIndex = packet.sequenceNumber;
if (sampleIndex < 0) {
throw new Error('Packet was not created from this track.');
}
const nextIndex = sampleIndex + 1;
// Ensure the next sample exists
while (
nextIndex >= this.demuxer.loadedSamples.length
+5 -1
View File
@@ -516,7 +516,11 @@ class WaveAudioTrackBacking implements InputAudioTrackBacking {
options: PacketRetrievalOptions,
): Promise<Yo> {
assert(this.demuxer.audioInfo);
const packetIndex = Math.round(packet.timestamp * this.demuxer.audioInfo.sampleRate / PACKET_SIZE_IN_FRAMES);
const packetIndex = packet.sequenceNumber;
if (packetIndex < 0) {
throw new Error('Packet was not created from this track.');
}
return this.getPacketAtIndex(res, packetIndex + 1, options);
}