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 { import {
assert, assert,
AsyncMutex4, AsyncMutex4,
binarySearchExact,
binarySearchLessOrEqual, binarySearchLessOrEqual,
Bitstream, Bitstream,
ResultValue, ResultValue,
@@ -261,16 +260,13 @@ class AdtsAudioTrackBacking implements InputAudioTrackBacking {
using lock = this.demuxer.readingMutex.lock(); using lock = this.demuxer.readingMutex.lock();
if (lock.pending) await lock.ready; if (lock.pending) await lock.ready;
const sampleIndex = binarySearchExact( const sampleIndex = packet.sequenceNumber;
this.demuxer.loadedSamples,
packet.timestamp,
x => x.timestamp,
);
if (sampleIndex === -1) { if (sampleIndex === -1) {
throw new Error('Packet was not created from this track.'); throw new Error('Packet was not created from this track.');
} }
const nextIndex = sampleIndex + 1; const nextIndex = sampleIndex + 1;
// Ensure the next sample exists // Ensure the next sample exists
while ( while (
nextIndex >= this.demuxer.loadedSamples.length nextIndex >= this.demuxer.loadedSamples.length
+4
View File
@@ -644,6 +644,10 @@ class FlacAudioTrackBacking implements InputAudioTrackBacking {
using lock = this.demuxer.readingMutex.lock(); using lock = this.demuxer.readingMutex.lock();
if (lock.pending) await lock.ready; 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; const nextIndex = packet.sequenceNumber + 1;
if ( if (
this.demuxer.lastSampleLoaded this.demuxer.lastSampleLoaded
+3 -7
View File
@@ -15,7 +15,6 @@ import { PacketRetrievalOptions } from '../media-sink';
import { import {
assert, assert,
AsyncMutex4, AsyncMutex4,
binarySearchExact,
binarySearchLessOrEqual, binarySearchLessOrEqual,
ResultValue, ResultValue,
UNDETERMINED_LANGUAGE, UNDETERMINED_LANGUAGE,
@@ -318,16 +317,13 @@ class Mp3AudioTrackBacking implements InputAudioTrackBacking {
using lock = this.demuxer.readingMutex.lock(); using lock = this.demuxer.readingMutex.lock();
if (lock.pending) await lock.ready; if (lock.pending) await lock.ready;
const sampleIndex = binarySearchExact( const sampleIndex = packet.sequenceNumber;
this.demuxer.loadedSamples, if (sampleIndex < 0) {
packet.timestamp,
x => x.timestamp,
);
if (sampleIndex === -1) {
throw new Error('Packet was not created from this track.'); throw new Error('Packet was not created from this track.');
} }
const nextIndex = sampleIndex + 1; const nextIndex = sampleIndex + 1;
// Ensure the next sample exists // Ensure the next sample exists
while ( while (
nextIndex >= this.demuxer.loadedSamples.length nextIndex >= this.demuxer.loadedSamples.length
+5 -1
View File
@@ -516,7 +516,11 @@ class WaveAudioTrackBacking implements InputAudioTrackBacking {
options: PacketRetrievalOptions, options: PacketRetrievalOptions,
): Promise<Yo> { ): Promise<Yo> {
assert(this.demuxer.audioInfo); 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); return this.getPacketAtIndex(res, packetIndex + 1, options);
} }