PacketReader read -> get method naming

This commit is contained in:
Vanilagy
2025-12-30 12:07:08 +01:00
parent 8467a9d672
commit ba26367529
6 changed files with 70 additions and 70 deletions
+32 -32
View File
@@ -84,7 +84,7 @@ export class PacketReader<T extends InputTrack = InputTrack> {
this.track = track;
}
private maybeVerifyPacketType(
private _maybeVerifyPacketType(
packet: EncodedPacket | null,
options: PacketRetrievalOptions,
): MaybePromise<EncodedPacket | null> {
@@ -102,7 +102,7 @@ export class PacketReader<T extends InputTrack = InputTrack> {
});
}
readFirst(options: PacketRetrievalOptions = {}): MaybePromise<EncodedPacket | null> {
getFirst(options: PacketRetrievalOptions = {}): MaybePromise<EncodedPacket | null> {
validatePacketRetrievalOptions(options);
if (this.track.input._disposed) {
@@ -113,13 +113,13 @@ export class PacketReader<T extends InputTrack = InputTrack> {
const promise = this.track._backing.getFirstPacket(result, options);
if (result.pending) {
return promise.then(() => this.maybeVerifyPacketType(result.value, options));
return promise.then(() => this._maybeVerifyPacketType(result.value, options));
} else {
return this.maybeVerifyPacketType(result.value, options);
return this._maybeVerifyPacketType(result.value, options);
}
}
readAt(timestamp: number, options: PacketRetrievalOptions = {}): MaybePromise<EncodedPacket | null> {
getAt(timestamp: number, options: PacketRetrievalOptions = {}): MaybePromise<EncodedPacket | null> {
validateTimestamp(timestamp);
validatePacketRetrievalOptions(options);
@@ -131,13 +131,13 @@ export class PacketReader<T extends InputTrack = InputTrack> {
const promise = this.track._backing.getPacket(result, timestamp, options);
if (result.pending) {
return promise.then(() => this.maybeVerifyPacketType(result.value, options));
return promise.then(() => this._maybeVerifyPacketType(result.value, options));
} else {
return this.maybeVerifyPacketType(result.value, options);
return this._maybeVerifyPacketType(result.value, options);
}
}
readKeyAt(timestamp: number, options: PacketRetrievalOptions = {}): MaybePromise<EncodedPacket | null> {
getKeyAt(timestamp: number, options: PacketRetrievalOptions = {}): MaybePromise<EncodedPacket | null> {
validateTimestamp(timestamp);
validatePacketRetrievalOptions(options);
@@ -146,7 +146,7 @@ export class PacketReader<T extends InputTrack = InputTrack> {
}
if (options.verifyKeyPackets) {
return this.readKeyAtVerified(timestamp, options);
return this._readKeyAtVerified(timestamp, options);
}
const result = new ResultValue<EncodedPacket | null>();
@@ -159,7 +159,7 @@ export class PacketReader<T extends InputTrack = InputTrack> {
}
}
private async readKeyAtVerified(
private async _readKeyAtVerified(
timestamp: number,
options: PacketRetrievalOptions,
): Promise<EncodedPacket | null> {
@@ -175,13 +175,13 @@ export class PacketReader<T extends InputTrack = InputTrack> {
const determinedType = await this.track.determinePacketType(packet);
if (determinedType === 'delta') {
// Try returning the previous key packet (in hopes that it's actually a key packet)
return this.readKeyAtVerified(packet.timestamp - 1 / this.track.timeResolution, options);
return this._readKeyAtVerified(packet.timestamp - 1 / this.track.timeResolution, options);
}
return packet;
}
readNext(from: EncodedPacket, options: PacketRetrievalOptions = {}): MaybePromise<EncodedPacket | null> {
getNext(from: EncodedPacket, options: PacketRetrievalOptions = {}): MaybePromise<EncodedPacket | null> {
if (!(from instanceof EncodedPacket)) {
throw new TypeError('from must be an EncodedPacket.');
}
@@ -195,13 +195,13 @@ export class PacketReader<T extends InputTrack = InputTrack> {
const promise = this.track._backing.getNextPacket(result, from, options);
if (result.pending) {
return promise.then(() => this.maybeVerifyPacketType(result.value, options));
return promise.then(() => this._maybeVerifyPacketType(result.value, options));
} else {
return this.maybeVerifyPacketType(result.value, options);
return this._maybeVerifyPacketType(result.value, options);
}
}
readNextKey(from: EncodedPacket, options: PacketRetrievalOptions = {}): MaybePromise<EncodedPacket | null> {
getNextKey(from: EncodedPacket, options: PacketRetrievalOptions = {}): MaybePromise<EncodedPacket | null> {
if (!(from instanceof EncodedPacket)) {
throw new TypeError('from must be an EncodedPacket.');
}
@@ -212,7 +212,7 @@ export class PacketReader<T extends InputTrack = InputTrack> {
}
if (options.verifyKeyPackets) {
return this.readNextKeyVerified(from, options);
return this._getNextKeyVerified(from, options);
}
const result = new ResultValue<EncodedPacket | null>();
@@ -225,7 +225,7 @@ export class PacketReader<T extends InputTrack = InputTrack> {
}
}
private async readNextKeyVerified(
private async _getNextKeyVerified(
from: EncodedPacket,
options: PacketRetrievalOptions,
): Promise<EncodedPacket | null> {
@@ -241,7 +241,7 @@ export class PacketReader<T extends InputTrack = InputTrack> {
const determinedType = await this.track.determinePacketType(nextPacket);
if (determinedType === 'delta') {
// Try returning the next key packet (in hopes that it's actually a key packet)
return this.readNextKeyVerified(nextPacket, options);
return this._getNextKeyVerified(nextPacket, options);
}
return nextPacket;
@@ -269,7 +269,7 @@ export class PacketCursor<T extends InputTrack = InputTrack> {
}
private _seekToFirstDirect(): MaybePromise<EncodedPacket | null> {
const result = this._reader.readFirst(this._options);
const result = this._reader.getFirst(this._options);
const onPacket = (packet: EncodedPacket | null) => {
this._nextIsFirst = false;
@@ -291,7 +291,7 @@ export class PacketCursor<T extends InputTrack = InputTrack> {
validateTimestamp(timestamp);
return this._callSerializer.call(() => {
const result = this._reader.readAt(timestamp, this._options);
const result = this._reader.getAt(timestamp, this._options);
const onPacket = (packet: EncodedPacket | null) => {
this._nextIsFirst = !packet;
@@ -310,7 +310,7 @@ export class PacketCursor<T extends InputTrack = InputTrack> {
validateTimestamp(timestamp);
return this._callSerializer.call(() => {
const result = this._reader.readKeyAt(timestamp, this._options);
const result = this._reader.getKeyAt(timestamp, this._options);
const onPacket = (packet: EncodedPacket | null) => {
this._nextIsFirst = !packet;
@@ -335,7 +335,7 @@ export class PacketCursor<T extends InputTrack = InputTrack> {
return null;
}
const result = this._reader.readNext(this.current, this._options);
const result = this._reader.getNext(this.current, this._options);
const onPacket = (packet: EncodedPacket | null) => {
return this.current = packet;
@@ -359,7 +359,7 @@ export class PacketCursor<T extends InputTrack = InputTrack> {
return null;
}
const result = this._reader.readNextKey(this.current, this._options);
const result = this._reader.getNextKey(this.current, this._options);
const onPacket = (packet: EncodedPacket | null) => {
return this.current = packet;
@@ -574,17 +574,17 @@ export abstract class SampleCursor<
}
seekToFirst(): MaybePromise<TransformedSample | null> {
return this._getSample(result => this._seekToPacket(result, this._packetReader.readFirst()));
return this._getSample(result => this._seekToPacket(result, this._packetReader.getFirst()));
}
seekTo(timestamp: number): MaybePromise<TransformedSample | null> {
validateTimestamp(timestamp);
return this._getSample(result => this._seekToPacket(result, this._packetReader.readAt(timestamp)));
return this._getSample(result => this._seekToPacket(result, this._packetReader.getAt(timestamp)));
}
seekToKey(timestamp: number): MaybePromise<TransformedSample | null> {
validateTimestamp(timestamp);
return this._getSample(result => this._seekToPacket(result, this._packetReader.readKeyAt(timestamp)));
return this._getSample(result => this._seekToPacket(result, this._packetReader.getKeyAt(timestamp)));
}
next(): MaybePromise<TransformedSample | null> {
@@ -940,7 +940,7 @@ export abstract class SampleCursor<
} else {
if (this._packetCursor.current) {
// We need to see if the target packet is ahead of the decoder, GOP-wise
let nextKey = this._packetReader.readNextKey(
let nextKey = this._packetReader.getNextKey(
this._packetCursor.current,
{ verifyKeyPackets: true },
);
@@ -1003,7 +1003,7 @@ export abstract class SampleCursor<
if (this._nextIsFirst) {
// Easy, just seek to the first sample
// await is important so that the lock doesn't release too early
return await this._seekToPacket(res, this._packetReader.readFirst(), lock);
return await this._seekToPacket(res, this._packetReader.getFirst(), lock);
}
// See if the request can be satisfied using already-decoded samples
@@ -1059,7 +1059,7 @@ export abstract class SampleCursor<
if (this._nextIsFirst) {
// await is important so that the lock doesn't release too early
return await this._seekToPacket(res, this._packetReader.readFirst(), lock);
return await this._seekToPacket(res, this._packetReader.getFirst(), lock);
}
let timestampToCheck: number;
@@ -1094,11 +1094,11 @@ export abstract class SampleCursor<
// are ascending in timestamp, so we first get the current key (based on a presentation-order search), then
// get the next key after that, which will be the answer we're looking for.
let key = this._packetReader.readKeyAt(timestampToCheck, { verifyKeyPackets: true });
let key = this._packetReader.getKeyAt(timestampToCheck, { verifyKeyPackets: true });
if (key instanceof Promise) key = await key;
assert(key); // Must be
let nextKey = this._packetReader.readNextKey(key, { verifyKeyPackets: true });
let nextKey = this._packetReader.getNextKey(key, { verifyKeyPackets: true });
if (nextKey instanceof Promise) nextKey = await nextKey;
if (!nextKey) {
@@ -1116,7 +1116,7 @@ export abstract class SampleCursor<
this._ensureNotClosed();
if (this._nextIsFirst) {
let first = this._packetReader.readFirst();
let first = this._packetReader.getFirst();
if (first instanceof Promise) first = await first;
return res.set(!!first);
+1 -1
View File
@@ -43,7 +43,7 @@ test('Can decode faulty transparent video and behaves gracefully', async () => {
const videoTrack = (await input.getPrimaryVideoTrack())!;
const reader = new PacketReader(videoTrack);
const secondKeyPacket = (await reader.readNextKey((await reader.readFirst())!))!;
const secondKeyPacket = (await reader.getNextKey((await reader.getFirst())!))!;
await using cursor = new VideoSampleCursor(videoTrack);
+2 -2
View File
@@ -23,7 +23,7 @@ test('Annex B to length-prefixed conversion, MP4', async () => {
expect(originalVideoTrack.codec).toBe('avc');
const originalReader = new PacketReader(originalVideoTrack);
const originalFirstPacket = await originalReader.readFirst();
const originalFirstPacket = await originalReader.getFirst();
expect([...originalFirstPacket!.data.slice(0, 4)]).toEqual([0, 0, 0, 1]);
const originalNalUnits = extractAvcNalUnits(originalFirstPacket!.data, originalDecoderConfig);
@@ -46,7 +46,7 @@ test('Annex B to length-prefixed conversion, MP4', async () => {
expect(newVideoTrack.codec).toBe('avc');
const newReader = new PacketReader(newVideoTrack);
const newFirstPacket = await newReader.readFirst();
const newFirstPacket = await newReader.getFirst();
expect([...newFirstPacket!.data.slice(0, 4)]).not.toEqual([0, 0, 0, 1]); // Successfully converted
const newNalUnits = extractAvcNalUnits(newFirstPacket!.data, newDecoderConfig);
+10 -10
View File
@@ -13,52 +13,52 @@ const __dirname = new URL('.', import.meta.url).pathname;
const testBasicPacketReading = async (track: InputTrack) => {
const reader = new PacketReader(track);
const first = await reader.readFirst();
const first = await reader.getFirst();
expect(first).not.toBe(null);
expect(first!.timestamp).toBe(0);
const next = await reader.readNext(first!);
const next = await reader.getNext(first!);
expect(next).not.toBe(null);
expect(next!.timestamp).toBeCloseTo(first!.timestamp + first!.duration);
expect(next!.sequenceNumber).toBeGreaterThan(first!.sequenceNumber);
const nextKey = await reader.readNextKey(first!);
const nextKey = await reader.getNextKey(first!);
expect(nextKey).not.toBe(null);
expect(nextKey!.type).toBe('key');
expect(nextKey!.timestamp).toBeGreaterThanOrEqual(next!.timestamp);
const seeked = await reader.readAt(1);
const seeked = await reader.getAt(1);
expect(seeked).not.toBe(null);
expect(seeked!.timestamp).toBeGreaterThan(0.9);
expect(seeked!.timestamp).toBeLessThanOrEqual(1);
expect(seeked!.sequenceNumber).toBeGreaterThan(next!.sequenceNumber);
const seekedKey = await reader.readKeyAt(1);
const seekedKey = await reader.getKeyAt(1);
expect(seekedKey).not.toBe(null);
expect(seekedKey!.type).toBe('key');
expect(seekedKey!.sequenceNumber).toBeGreaterThanOrEqual(first!.sequenceNumber);
expect(seekedKey!.sequenceNumber).toBeLessThanOrEqual(seeked!.sequenceNumber);
const last = await reader.readAt(Infinity);
const last = await reader.getAt(Infinity);
expect(last).not.toBe(null);
expect(last!.sequenceNumber).toBeGreaterThan(seeked!.sequenceNumber);
const afterLast = await reader.readNext(last!);
const afterLast = await reader.getNext(last!);
expect(afterLast).toBe(null);
};
const testSyncPacketReading = (track: InputTrack) => {
const reader = new PacketReader(track);
const seeked = reader.readAt(1) as EncodedPacket | null;
const seeked = reader.getAt(1) as EncodedPacket | null;
expect(seeked).toBeInstanceOf(EncodedPacket);
let current = reader.readFirst() as EncodedPacket | null;
let current = reader.getFirst() as EncodedPacket | null;
expect(current).toBeInstanceOf(EncodedPacket);
let count = 0;
while (current) {
current = reader.readNext(current) as EncodedPacket | null;
current = reader.getNext(current) as EncodedPacket | null;
expect(current instanceof EncodedPacket || current === null).toBe(true);
count++;
}
+6 -6
View File
@@ -67,21 +67,21 @@ test('can do random access', async () => {
const reader = new PacketReader(track);
const packet = await reader.readAt(10);
const packet = await reader.getAt(10);
assert(packet);
expect(packet.timestamp).toBe(9.93814058956916);
expect(packet.data.byteLength).toBe(8345);
expect(packet.sequenceNumber).toBe(107);
expect(packet.duration).toBe(0.09287981859410431);
const nextPacket = await reader.readNext(packet);
const nextPacket = await reader.getNext(packet);
assert(nextPacket);
expect(nextPacket.timestamp).toBe(10.031020408163265);
expect(nextPacket.data.byteLength).toBe(8988);
expect(nextPacket.sequenceNumber).toBe(108);
expect(nextPacket.duration).toBe(0.09287981859410431);
const priorPacket = await reader.readAt(3);
const priorPacket = await reader.getAt(3);
assert(priorPacket);
expect(priorPacket.timestamp).toBe(2.972154195011338);
expect(priorPacket.data.byteLength).toBe(6877);
@@ -101,7 +101,7 @@ test('can get metadata-only packets', async () => {
const reader = new PacketReader(track);
const packet = await reader.readAt(10, { metadataOnly: true });
const packet = await reader.getAt(10, { metadataOnly: true });
assert(packet);
expect(packet.timestamp).toBe(9.93814058956916);
expect(packet.isMetadataOnly).toBe(true);
@@ -227,8 +227,8 @@ test('can re-mux a .flac', async () => {
expect(timestamp).toBe(19.690521541950112);
// Test that packets are byte-identical
const inputPacket = await inputReader.readAt(10);
const outputPacket = await outputReader.readAt(10);
const inputPacket = await inputReader.getAt(10);
const outputPacket = await outputReader.getAt(10);
assert(inputPacket);
assert(outputPacket);
+19 -19
View File
@@ -18,21 +18,21 @@ test('Packet reader', async () => {
const videoTrack = (await input.getPrimaryVideoTrack())!;
const reader = new PacketReader(videoTrack);
const packet1 = (await reader.readFirst())!;
const packet1 = (await reader.getFirst())!;
expect(packet1.timestamp).toBe(0);
const packet3 = (await reader.readNext(packet1))!;
const packet3 = (await reader.getNext(packet1))!;
expect(packet3.sequenceNumber).toBeGreaterThan(packet1.sequenceNumber);
const packet4 = (await reader.readNextKey(packet1))!;
const packet4 = (await reader.getNextKey(packet1))!;
expect(packet4.sequenceNumber).toBeGreaterThan(packet3.sequenceNumber);
expect(packet4.type).toBe('key');
const packet5 = (await reader.readNext(packet3))!;
const packet5 = (await reader.getNext(packet3))!;
expect(packet5.sequenceNumber).toBeGreaterThan(packet3.sequenceNumber);
expect(packet5.sequenceNumber).toBeLessThan(packet4.sequenceNumber);
const packet6 = (await reader.readAt(2.4))!;
const packet6 = (await reader.getAt(2.4))!;
expect(packet6.timestamp).toBeGreaterThan(2);
expect(packet6.timestamp).toBeLessThanOrEqual(2.4);
});
@@ -46,15 +46,15 @@ test('Packet reading throwing after Input disposal', async () => {
const videoTrack = (await input.getPrimaryVideoTrack())!;
const reader = new PacketReader(videoTrack);
const first = await reader.readFirst();
const first = await reader.getFirst();
input.dispose();
expect(() => reader.readFirst()).toThrow(InputDisposedError);
expect(() => reader.readAt(0)).toThrow(InputDisposedError);
expect(() => reader.readKeyAt(0)).toThrow(InputDisposedError);
expect(() => reader.readNext(first!)).toThrow(InputDisposedError);
expect(() => reader.readNextKey(first!)).toThrow(InputDisposedError);
expect(() => reader.getFirst()).toThrow(InputDisposedError);
expect(() => reader.getAt(0)).toThrow(InputDisposedError);
expect(() => reader.getKeyAt(0)).toThrow(InputDisposedError);
expect(() => reader.getNext(first!)).toThrow(InputDisposedError);
expect(() => reader.getNextKey(first!)).toThrow(InputDisposedError);
});
test('Packet cursor seeking', async () => {
@@ -223,7 +223,7 @@ test('Synchronous packet reading', async () => {
const reader = new PacketReader(videoTrack);
const cursor = new PacketCursor(videoTrack);
expect(reader.readFirst()).not.toBeInstanceOf(Promise);
expect(reader.getFirst()).not.toBeInstanceOf(Promise);
expect(cursor.seekToFirst()).not.toBeInstanceOf(Promise);
expect(cursor.seekTo(0.1)).not.toBeInstanceOf(Promise);
@@ -320,31 +320,31 @@ test('verifyKeyPackets with faultily-labeled key frames', async () => {
const videoTrack = (await input.getPrimaryVideoTrack())!;
const reader = new PacketReader(videoTrack);
const firstPacket = (await reader.readFirst())!;
const firstPacket = (await reader.getFirst())!;
expect(firstPacket.type).toBe('key');
const fakeKeyPacket = (await reader.readNextKey(firstPacket))!;
const fakeKeyPacket = (await reader.getNextKey(firstPacket))!;
expect(fakeKeyPacket).not.toBe(null);
expect(fakeKeyPacket.type).toBe('key'); // Metadata says it's a key frame
expect(fakeKeyPacket.sequenceNumber).toBeGreaterThan(firstPacket.sequenceNumber);
const verifiedPacket = (await reader.readAt(fakeKeyPacket.timestamp, { verifyKeyPackets: true }))!;
const verifiedPacket = (await reader.getAt(fakeKeyPacket.timestamp, { verifyKeyPackets: true }))!;
expect(verifiedPacket.sequenceNumber).toBe(fakeKeyPacket.sequenceNumber);
expect(verifiedPacket.type).toBe('delta'); // After verification, it's actually a delta frame
const unverifiedKeyAt = (await reader.readKeyAt(fakeKeyPacket.timestamp))!;
const unverifiedKeyAt = (await reader.getKeyAt(fakeKeyPacket.timestamp))!;
expect(unverifiedKeyAt.sequenceNumber).toBe(fakeKeyPacket.sequenceNumber);
expect(unverifiedKeyAt.type).toBe('key');
const verifiedKeyAt = (await reader.readKeyAt(fakeKeyPacket.timestamp, { verifyKeyPackets: true }))!;
const verifiedKeyAt = (await reader.getKeyAt(fakeKeyPacket.timestamp, { verifyKeyPackets: true }))!;
expect(verifiedKeyAt.sequenceNumber).toBe(firstPacket.sequenceNumber);
expect(verifiedKeyAt.type).toBe('key');
const unverifiedNextKey = (await reader.readNextKey(firstPacket))!;
const unverifiedNextKey = (await reader.getNextKey(firstPacket))!;
expect(unverifiedNextKey).not.toBe(null);
expect(unverifiedNextKey.type).toBe('key');
expect(unverifiedNextKey.sequenceNumber).toBe(fakeKeyPacket.sequenceNumber);
const verifiedNextKey = await reader.readNextKey(firstPacket, { verifyKeyPackets: true });
const verifiedNextKey = await reader.getNextKey(firstPacket, { verifyKeyPackets: true });
expect(verifiedNextKey).toBe(null);
});