mirror of
https://github.com/arcodange-org/mediabunny.git
synced 2026-10-03 05:43:50 +02:00
Give ResultValue return type proper names, document ResultValue
This commit is contained in:
@@ -15,9 +15,9 @@ import {
|
||||
AsyncMutex4,
|
||||
binarySearchLessOrEqual,
|
||||
Bitstream,
|
||||
MaybeRelevantPromise,
|
||||
ResultValue,
|
||||
UNDETERMINED_LANGUAGE,
|
||||
Yo,
|
||||
} from '../misc';
|
||||
import { EncodedPacket, PLACEHOLDER_DATA } from '../packet';
|
||||
import { readBytes, Reader } from '../reader';
|
||||
@@ -72,7 +72,7 @@ export class AdtsDemuxer extends Demuxer {
|
||||
})();
|
||||
}
|
||||
|
||||
async advanceReader(res: ResultValue<void>): Promise<Yo> {
|
||||
async advanceReader(res: ResultValue<void>): MaybeRelevantPromise {
|
||||
let slice = this.reader.requestSliceRange(this.lastLoadedPos, MIN_FRAME_HEADER_SIZE, MAX_FRAME_HEADER_SIZE);
|
||||
if (slice instanceof Promise) slice = await slice;
|
||||
if (!slice) {
|
||||
@@ -214,7 +214,7 @@ class AdtsAudioTrackBacking implements InputAudioTrackBacking {
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
sampleIndex: number,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
if (sampleIndex === -1) {
|
||||
return res.set(null);
|
||||
}
|
||||
@@ -256,7 +256,7 @@ class AdtsAudioTrackBacking implements InputAudioTrackBacking {
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
packet: EncodedPacket,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
using lock = this.demuxer.readingMutex.lock();
|
||||
if (lock.pending) await lock.ready;
|
||||
|
||||
@@ -284,7 +284,7 @@ class AdtsAudioTrackBacking implements InputAudioTrackBacking {
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
timestamp: number,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
using lock = this.demuxer.readingMutex.lock();
|
||||
if (lock.pending) await lock.ready;
|
||||
|
||||
@@ -321,7 +321,7 @@ class AdtsAudioTrackBacking implements InputAudioTrackBacking {
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
timestamp: number,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
return this.getPacket(res, timestamp, options);
|
||||
}
|
||||
|
||||
@@ -329,7 +329,7 @@ class AdtsAudioTrackBacking implements InputAudioTrackBacking {
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
packet: EncodedPacket,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
return this.getNextPacket(res, packet, options);
|
||||
}
|
||||
}
|
||||
|
||||
+6
-6
@@ -24,8 +24,8 @@ import {
|
||||
ResultValue,
|
||||
Rotation,
|
||||
AsyncGate,
|
||||
Yo,
|
||||
isNumber,
|
||||
MaybeRelevantPromise,
|
||||
} from './misc';
|
||||
import { EncodedPacket } from './packet';
|
||||
import { AudioSample, clampCropRectangle, CropRectangle, validateCropRectangle, VideoSample } from './sample';
|
||||
@@ -553,7 +553,7 @@ export abstract class SampleCursor<
|
||||
}
|
||||
|
||||
private _getSample(
|
||||
callback: (result: ResultValue<TransformedSample | null>) => Promise<Yo>,
|
||||
callback: (result: ResultValue<TransformedSample | null>) => MaybeRelevantPromise,
|
||||
): MaybePromise<TransformedSample | null> {
|
||||
this._ensureWillBeOpen();
|
||||
|
||||
@@ -860,7 +860,7 @@ export abstract class SampleCursor<
|
||||
res: ResultValue<TransformedSample | null>,
|
||||
targetPacketPromise: MaybePromise<EncodedPacket | null>,
|
||||
lock?: AsyncMutexLock,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
this._lazyPump++;
|
||||
|
||||
if (!lock) {
|
||||
@@ -994,7 +994,7 @@ export abstract class SampleCursor<
|
||||
return res.set(await request.promise);
|
||||
}
|
||||
|
||||
private async _nextInternal(res: ResultValue<TransformedSample | null>): Promise<Yo> {
|
||||
private async _nextInternal(res: ResultValue<TransformedSample | null>): MaybeRelevantPromise {
|
||||
using lock = this._mutex.lock();
|
||||
if (lock.pending) await lock.ready;
|
||||
|
||||
@@ -1051,7 +1051,7 @@ export abstract class SampleCursor<
|
||||
return res.set(await request.promise);
|
||||
}
|
||||
|
||||
private async _nextKeyInternal(res: ResultValue<TransformedSample | null>): Promise<Yo> {
|
||||
private async _nextKeyInternal(res: ResultValue<TransformedSample | null>): MaybeRelevantPromise {
|
||||
using lock = this._mutex.lock();
|
||||
if (lock.pending) await lock.ready;
|
||||
|
||||
@@ -1109,7 +1109,7 @@ export abstract class SampleCursor<
|
||||
return await this._seekToPacket(res, nextKey, lock);
|
||||
}
|
||||
|
||||
private async _hasNextInternal(res: ResultValue<boolean>): Promise<Yo> {
|
||||
private async _hasNextInternal(res: ResultValue<boolean>): MaybeRelevantPromise {
|
||||
using lock = this._mutex.lock();
|
||||
if (lock.pending) await lock.ready;
|
||||
|
||||
|
||||
@@ -15,10 +15,10 @@ import {
|
||||
AsyncMutex4,
|
||||
binarySearchLessOrEqual,
|
||||
Bitstream,
|
||||
MaybeRelevantPromise,
|
||||
ResultValue,
|
||||
textDecoder,
|
||||
UNDETERMINED_LANGUAGE,
|
||||
Yo,
|
||||
} from '../misc';
|
||||
import { EncodedPacket, PLACEHOLDER_DATA } from '../packet';
|
||||
import {
|
||||
@@ -266,7 +266,7 @@ export class FlacDemuxer extends Demuxer {
|
||||
startPos: number;
|
||||
isFirstPacket: boolean;
|
||||
},
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
assert(this.audioInfo);
|
||||
// we expect that there are at least `minimumFrameSize` bytes left in the file
|
||||
|
||||
@@ -483,7 +483,7 @@ export class FlacDemuxer extends Demuxer {
|
||||
return { num, blockSize, sampleRate };
|
||||
}
|
||||
|
||||
async advanceReader(res: ResultValue<void>): Promise<Yo> {
|
||||
async advanceReader(res: ResultValue<void>): MaybeRelevantPromise {
|
||||
assert(this.lastLoadedPos !== null);
|
||||
assert(this.audioInfo);
|
||||
const startPos = this.lastLoadedPos;
|
||||
@@ -585,7 +585,7 @@ class FlacAudioTrackBacking implements InputAudioTrackBacking {
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
timestamp: number,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
assert(this.demuxer.audioInfo);
|
||||
if (timestamp < 0) {
|
||||
throw new Error('Timestamp cannot be negative');
|
||||
@@ -640,7 +640,7 @@ class FlacAudioTrackBacking implements InputAudioTrackBacking {
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
packet: EncodedPacket,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
using lock = this.demuxer.readingMutex.lock();
|
||||
if (lock.pending) await lock.ready;
|
||||
|
||||
@@ -675,7 +675,7 @@ class FlacAudioTrackBacking implements InputAudioTrackBacking {
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
timestamp: number,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
return this.getPacket(res, timestamp, options);
|
||||
}
|
||||
|
||||
@@ -683,7 +683,7 @@ class FlacAudioTrackBacking implements InputAudioTrackBacking {
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
packet: EncodedPacket,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
return this.getNextPacket(res, packet, options);
|
||||
}
|
||||
|
||||
@@ -691,7 +691,7 @@ class FlacAudioTrackBacking implements InputAudioTrackBacking {
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
sampleIndex: number,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
const rawSample = this.demuxer.loadedSamples[sampleIndex];
|
||||
if (!rawSample) {
|
||||
return res.set(null);
|
||||
@@ -731,7 +731,7 @@ class FlacAudioTrackBacking implements InputAudioTrackBacking {
|
||||
async getFirstPacket(
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
const advanceResult = new ResultValue<void>();
|
||||
|
||||
// Ensure the next sample exists
|
||||
|
||||
+6
-6
@@ -10,7 +10,7 @@ import { AudioCodec, MediaCodec, VideoCodec } from './codec';
|
||||
import { determineVideoPacketType } from './codec-data';
|
||||
import { customAudioDecoders, customVideoDecoders } from './custom-coder';
|
||||
import { Input } from './input';
|
||||
import { assert, ResultValue, Rotation, Yo } from './misc';
|
||||
import { assert, MaybeRelevantPromise, ResultValue, Rotation } from './misc';
|
||||
import { TrackType } from './output';
|
||||
import { EncodedPacket, PacketType } from './packet';
|
||||
import { TrackDisposition } from './metadata';
|
||||
@@ -42,27 +42,27 @@ export interface InputTrackBacking {
|
||||
getFirstPacket(
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo>;
|
||||
): MaybeRelevantPromise;
|
||||
getNextPacket(
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
packet: EncodedPacket,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo>;
|
||||
): MaybeRelevantPromise;
|
||||
getPacket(
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
timestamp: number,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo>;
|
||||
): MaybeRelevantPromise;
|
||||
getKeyPacket(
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
timestamp: number,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo>;
|
||||
): MaybeRelevantPromise;
|
||||
getNextKeyPacket(
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
packet: EncodedPacket,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo>;
|
||||
): MaybeRelevantPromise;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -58,7 +58,7 @@ import {
|
||||
toDataView,
|
||||
roundIfAlmostInteger,
|
||||
ResultValue,
|
||||
Yo,
|
||||
MaybeRelevantPromise,
|
||||
} from '../misc';
|
||||
import { EncodedPacket, PLACEHOLDER_DATA } from '../packet';
|
||||
import { buildIsobmffMimeType } from './isobmff-misc';
|
||||
@@ -507,7 +507,7 @@ export class IsobmffDemuxer extends Demuxer {
|
||||
return sampleTable;
|
||||
}
|
||||
|
||||
async readFragment(res: ResultValue<Fragment>, startPos: number): Promise<Yo> {
|
||||
async readFragment(res: ResultValue<Fragment>, startPos: number): MaybeRelevantPromise {
|
||||
if (this.lastReadFragment?.moofOffset === startPos) {
|
||||
return res.set(this.lastReadFragment);
|
||||
}
|
||||
@@ -2354,7 +2354,7 @@ abstract class IsobmffTrackBacking implements InputTrackBacking {
|
||||
return firstPacket?.timestamp ?? 0;
|
||||
}
|
||||
|
||||
async getFirstPacket(res: ResultValue<EncodedPacket | null>, options: PacketRetrievalOptions): Promise<Yo> {
|
||||
async getFirstPacket(res: ResultValue<EncodedPacket | null>, options: PacketRetrievalOptions): MaybeRelevantPromise {
|
||||
const result = new ResultValue<EncodedPacket | null>();
|
||||
const promise = this.fetchPacketForSampleIndex(result, 0, options);
|
||||
if (result.pending) await promise;
|
||||
@@ -2398,7 +2398,7 @@ abstract class IsobmffTrackBacking implements InputTrackBacking {
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
timestamp: number,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
const timestampInTimescale = this.mapTimestampIntoTimescale(timestamp);
|
||||
|
||||
const sampleTable = this.internalTrack.demuxer.getSampleTableForTrack(this.internalTrack);
|
||||
@@ -2438,7 +2438,7 @@ abstract class IsobmffTrackBacking implements InputTrackBacking {
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
packet: EncodedPacket,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
const isFromFragment = packet._internal !== undefined;
|
||||
if (!isFromFragment) {
|
||||
const sampleIndex = packet.sequenceNumber; // sequenceNumber = sampleIndex in this case
|
||||
@@ -2492,7 +2492,7 @@ abstract class IsobmffTrackBacking implements InputTrackBacking {
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
timestamp: number,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
const timestampInTimescale = this.mapTimestampIntoTimescale(timestamp);
|
||||
|
||||
const sampleTable = this.internalTrack.demuxer.getSampleTableForTrack(this.internalTrack);
|
||||
@@ -2531,7 +2531,7 @@ abstract class IsobmffTrackBacking implements InputTrackBacking {
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
packet: EncodedPacket,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
const isFromFragment = packet._internal !== undefined;
|
||||
if (!isFromFragment) {
|
||||
const sampleIndex = packet.sequenceNumber; // sequenceNumber = sampleIndex in this case
|
||||
@@ -2594,7 +2594,7 @@ abstract class IsobmffTrackBacking implements InputTrackBacking {
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
sampleIndex: number,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
if (sampleIndex === -1) {
|
||||
return res.set(null);
|
||||
}
|
||||
@@ -2639,7 +2639,7 @@ abstract class IsobmffTrackBacking implements InputTrackBacking {
|
||||
fragment: Fragment,
|
||||
sampleIndex: number,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
if (sampleIndex === -1) {
|
||||
return res.set(null);
|
||||
}
|
||||
@@ -2691,7 +2691,7 @@ abstract class IsobmffTrackBacking implements InputTrackBacking {
|
||||
// The timestamp for which we know the correct sample will not come after it
|
||||
latestTimestamp: number,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
const demuxer = this.internalTrack.demuxer;
|
||||
|
||||
let currentPos = startOffset ?? 0;
|
||||
|
||||
@@ -40,13 +40,13 @@ import {
|
||||
isIso639Dash2LanguageCode,
|
||||
last,
|
||||
MATRIX_COEFFICIENTS_MAP_INVERSE,
|
||||
MaybeRelevantPromise,
|
||||
normalizeRotation,
|
||||
ResultValue,
|
||||
Rotation,
|
||||
roundIfAlmostInteger,
|
||||
TRANSFER_CHARACTERISTICS_MAP_INVERSE,
|
||||
UNDETERMINED_LANGUAGE,
|
||||
Yo,
|
||||
} from '../misc';
|
||||
import { EncodedPacket, EncodedPacketSideData, PLACEHOLDER_DATA } from '../packet';
|
||||
import {
|
||||
@@ -583,7 +583,7 @@ export class MatroskaDemuxer extends Demuxer {
|
||||
this.currentSegment = null;
|
||||
}
|
||||
|
||||
async readCluster(res: ResultValue<Cluster>, startPos: number, segment: Segment): Promise<Yo> {
|
||||
async readCluster(res: ResultValue<Cluster>, startPos: number, segment: Segment): MaybeRelevantPromise {
|
||||
if (segment.lastReadCluster?.elementStartPos === startPos) {
|
||||
return res.set(segment.lastReadCluster);
|
||||
}
|
||||
@@ -1864,7 +1864,10 @@ abstract class MatroskaTrackBacking implements InputTrackBacking {
|
||||
return this.internalTrack.disposition;
|
||||
}
|
||||
|
||||
async getFirstPacket(res: ResultValue<EncodedPacket | null>, options: PacketRetrievalOptions): Promise<Yo> {
|
||||
async getFirstPacket(
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
options: PacketRetrievalOptions,
|
||||
): MaybeRelevantPromise {
|
||||
return this.performClusterLookup(
|
||||
res,
|
||||
null,
|
||||
@@ -1899,7 +1902,7 @@ abstract class MatroskaTrackBacking implements InputTrackBacking {
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
timestamp: number,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
const timestampInTimescale = this.intoTimescale(timestamp);
|
||||
|
||||
return this.performClusterLookup(
|
||||
@@ -1932,7 +1935,7 @@ abstract class MatroskaTrackBacking implements InputTrackBacking {
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
packet: EncodedPacket,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
const clusterStartPos = packet._internal;
|
||||
if (clusterStartPos === undefined) {
|
||||
throw new Error('Packet was not created from this track.');
|
||||
@@ -1981,7 +1984,7 @@ abstract class MatroskaTrackBacking implements InputTrackBacking {
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
timestamp: number,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
const timestampInTimescale = this.intoTimescale(timestamp);
|
||||
|
||||
return this.performClusterLookup(
|
||||
@@ -2013,7 +2016,7 @@ abstract class MatroskaTrackBacking implements InputTrackBacking {
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
packet: EncodedPacket,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
const clusterStartPos = packet._internal;
|
||||
if (clusterStartPos === undefined) {
|
||||
throw new Error('Packet was not created from this track.');
|
||||
@@ -2066,13 +2069,12 @@ abstract class MatroskaTrackBacking implements InputTrackBacking {
|
||||
}
|
||||
|
||||
private fetchPacketInCluster(
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
cluster: Cluster,
|
||||
blockIndex: number,
|
||||
options: PacketRetrievalOptions,
|
||||
): Yo {
|
||||
) {
|
||||
if (blockIndex === -1) {
|
||||
return res.set(null);
|
||||
return null;
|
||||
}
|
||||
|
||||
const trackData = cluster.trackData.get(this.internalTrack.id)!;
|
||||
@@ -2107,7 +2109,7 @@ abstract class MatroskaTrackBacking implements InputTrackBacking {
|
||||
|
||||
packet._internal = cluster.elementStartPos;
|
||||
|
||||
return res.set(packet);
|
||||
return packet;
|
||||
}
|
||||
|
||||
/** Looks for a packet in the clusters while trying to load as few clusters as possible to retrieve it. */
|
||||
@@ -2122,7 +2124,7 @@ abstract class MatroskaTrackBacking implements InputTrackBacking {
|
||||
// The timestamp for which we know the correct block will not come after it
|
||||
latestTimestamp: number,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
const { demuxer, segment } = this.internalTrack;
|
||||
|
||||
let currentPos = startOffset ?? 0;
|
||||
@@ -2135,7 +2137,7 @@ abstract class MatroskaTrackBacking implements InputTrackBacking {
|
||||
const { blockIndex, correctBlockFound } = getMatchInCluster(startCluster);
|
||||
|
||||
if (correctBlockFound) {
|
||||
return this.fetchPacketInCluster(res, startCluster, blockIndex, options);
|
||||
return res.set(this.fetchPacketInCluster(startCluster, blockIndex, options));
|
||||
}
|
||||
|
||||
currentPos = startCluster.elementEndPos; // Start reading from the next cluster
|
||||
@@ -2237,7 +2239,7 @@ abstract class MatroskaTrackBacking implements InputTrackBacking {
|
||||
|
||||
const { blockIndex, correctBlockFound } = getMatchInCluster(currentCluster);
|
||||
if (correctBlockFound) {
|
||||
return this.fetchPacketInCluster(res, currentCluster, blockIndex, options);
|
||||
return res.set(this.fetchPacketInCluster(currentCluster, blockIndex, options));
|
||||
}
|
||||
|
||||
if (blockIndex !== -1) {
|
||||
@@ -2298,7 +2300,7 @@ abstract class MatroskaTrackBacking implements InputTrackBacking {
|
||||
|
||||
if (bestCluster) {
|
||||
// If we finished looping but didn't find a perfect match, still return the best match we found
|
||||
return this.fetchPacketInCluster(res, bestCluster, bestBlockIndex, options);
|
||||
return res.set(this.fetchPacketInCluster(bestCluster, bestBlockIndex, options));
|
||||
}
|
||||
|
||||
return res.set(null);
|
||||
|
||||
+13
-4
@@ -872,16 +872,25 @@ export const isNumber = (x: unknown) => {
|
||||
return typeof x === 'number' && !Number.isNaN(x);
|
||||
};
|
||||
|
||||
// We use a unique symbol to ensure that any function using the ResultValue system actually returns when setting it,
|
||||
// instead of simply setting it and continuing on.
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const yo: unique symbol = Symbol();
|
||||
export type Yo = typeof yo;
|
||||
const returnSymbol: unique symbol = Symbol();
|
||||
export type ReturnSymbol = typeof returnSymbol;
|
||||
export type MaybeRelevantPromise = Promise<ReturnSymbol>;
|
||||
|
||||
/**
|
||||
* Represents a wrapper that holds a function's return value. Instead of the function returning its return value, it
|
||||
* instead gets passed a ResultValue instance and writes the return value into it. This allows async functions that
|
||||
* don't hit any async path to synchronously expose their return value, allowing the caller to ignore the returned
|
||||
* promise if possible. This allows for "optinally-asynchronous code".
|
||||
*/
|
||||
export class ResultValue<T> {
|
||||
value!: T;
|
||||
pending = true;
|
||||
|
||||
// @ts-expect-error Just for the types
|
||||
set(value: T): Yo {
|
||||
// @ts-expect-error Return value just for the types
|
||||
set(value: T): ReturnSymbol {
|
||||
this.value = value;
|
||||
this.pending = false;
|
||||
}
|
||||
|
||||
@@ -15,9 +15,9 @@ import {
|
||||
assert,
|
||||
AsyncMutex4,
|
||||
binarySearchLessOrEqual,
|
||||
MaybeRelevantPromise,
|
||||
ResultValue,
|
||||
UNDETERMINED_LANGUAGE,
|
||||
Yo,
|
||||
} from '../misc';
|
||||
import { EncodedPacket, PLACEHOLDER_DATA } from '../packet';
|
||||
import { FrameHeader, getXingOffset, INFO, XING } from '../../shared/mp3-misc';
|
||||
@@ -77,7 +77,7 @@ export class Mp3Demuxer extends Demuxer {
|
||||
})();
|
||||
}
|
||||
|
||||
async advanceReader(res: ResultValue<void>): Promise<Yo> {
|
||||
async advanceReader(res: ResultValue<void>): MaybeRelevantPromise {
|
||||
if (this.lastLoadedPos === 0) {
|
||||
// Let's skip all ID3v2 tags at the start of the file
|
||||
while (true) {
|
||||
@@ -271,7 +271,7 @@ class Mp3AudioTrackBacking implements InputAudioTrackBacking {
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
sampleIndex: number,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
if (sampleIndex === -1) {
|
||||
return res.set(null);
|
||||
}
|
||||
@@ -313,7 +313,7 @@ class Mp3AudioTrackBacking implements InputAudioTrackBacking {
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
packet: EncodedPacket,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
using lock = this.demuxer.readingMutex.lock();
|
||||
if (lock.pending) await lock.ready;
|
||||
|
||||
@@ -341,7 +341,7 @@ class Mp3AudioTrackBacking implements InputAudioTrackBacking {
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
timestamp: number,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
using lock = this.demuxer.readingMutex.lock();
|
||||
if (lock.pending) await lock.ready;
|
||||
|
||||
@@ -378,7 +378,7 @@ class Mp3AudioTrackBacking implements InputAudioTrackBacking {
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
timestamp: number,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
return this.getPacket(res, timestamp, options);
|
||||
}
|
||||
|
||||
@@ -386,7 +386,7 @@ class Mp3AudioTrackBacking implements InputAudioTrackBacking {
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
packet: EncodedPacket,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
return this.getNextPacket(res, packet, options);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
*/
|
||||
|
||||
import { FRAME_HEADER_SIZE, FrameHeader, readFrameHeader } from '../../shared/mp3-misc';
|
||||
import { ResultValue, Yo } from '../misc';
|
||||
import { MaybeRelevantPromise, ResultValue } from '../misc';
|
||||
import { Reader, readU32Be } from '../reader';
|
||||
|
||||
export const readNextFrameHeader = async (
|
||||
@@ -18,7 +18,7 @@ export const readNextFrameHeader = async (
|
||||
reader: Reader,
|
||||
startPos: number,
|
||||
until: number | null,
|
||||
): Promise<Yo> => {
|
||||
): MaybeRelevantPromise => {
|
||||
let currentPos = startPos;
|
||||
|
||||
while (until === null || currentPos < until) {
|
||||
|
||||
+14
-7
@@ -19,11 +19,11 @@ import {
|
||||
binarySearchLessOrEqual,
|
||||
findLast,
|
||||
last,
|
||||
MaybeRelevantPromise,
|
||||
ResultValue,
|
||||
roundIfAlmostInteger,
|
||||
toDataView,
|
||||
UNDETERMINED_LANGUAGE,
|
||||
Yo,
|
||||
} from '../misc';
|
||||
import { EncodedPacket, PLACEHOLDER_DATA } from '../packet';
|
||||
import { readBytes, Reader } from '../reader';
|
||||
@@ -280,7 +280,11 @@ export class OggDemuxer extends Demuxer {
|
||||
readVorbisComments(secondPacket.data.subarray(8), this.metadataTags); // Skip 'OpusTags'
|
||||
}
|
||||
|
||||
async readPacket(res: ResultValue<Packet | null>, startPage: Page, startSegmentIndex: number): Promise<Yo> {
|
||||
async readPacket(
|
||||
res: ResultValue<Packet | null>,
|
||||
startPage: Page,
|
||||
startSegmentIndex: number,
|
||||
): MaybeRelevantPromise {
|
||||
assert(startSegmentIndex < startPage.lacingValues.length);
|
||||
|
||||
let startDataOffset = 0;
|
||||
@@ -369,7 +373,7 @@ export class OggDemuxer extends Demuxer {
|
||||
});
|
||||
}
|
||||
|
||||
async findNextPacketStart(res: ResultValue<PacketStart | null>, lastPacket: Packet): Promise<Yo> {
|
||||
async findNextPacketStart(res: ResultValue<PacketStart | null>, lastPacket: Packet): MaybeRelevantPromise {
|
||||
// If there's another segment in the same page, return it
|
||||
if (lastPacket.endSegmentIndex < lastPacket.endPage.lacingValues.length - 1) {
|
||||
return res.set({ startPage: lastPacket.endPage, startSegmentIndex: lastPacket.endSegmentIndex + 1 });
|
||||
@@ -540,7 +544,10 @@ class OggAudioTrackBacking implements InputAudioTrackBacking {
|
||||
return encodedPacket;
|
||||
}
|
||||
|
||||
async getFirstPacket(res: ResultValue<EncodedPacket | null>, options: PacketRetrievalOptions): Promise<Yo> {
|
||||
async getFirstPacket(
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
options: PacketRetrievalOptions,
|
||||
): MaybeRelevantPromise {
|
||||
assert(this.bitstream.lastMetadataPacket);
|
||||
|
||||
const positionResult = new ResultValue<PacketStart | null>();
|
||||
@@ -577,7 +584,7 @@ class OggAudioTrackBacking implements InputAudioTrackBacking {
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
prevPacket: EncodedPacket,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
const prevMetadata = prevPacket._internal as EncodedPacketMetadata | undefined;
|
||||
if (!prevMetadata) {
|
||||
throw new Error('Packet was not created from this track.');
|
||||
@@ -612,7 +619,7 @@ class OggAudioTrackBacking implements InputAudioTrackBacking {
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
timestamp: number,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
if (this.demuxer.reader.fileSize === null) {
|
||||
// No file size known, can't do binary search, but fall back to sequential algo instead
|
||||
return this.getPacketSequential(res, timestamp, options);
|
||||
@@ -971,7 +978,7 @@ class OggAudioTrackBacking implements InputAudioTrackBacking {
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
timestamp: number,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
using lock = this.sequentialScanMutex.lock(); // Requires exclusivity because we write to a cache
|
||||
if (lock.pending) await lock.ready;
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ import { Demuxer } from '../demuxer';
|
||||
import { Input } from '../input';
|
||||
import { InputAudioTrack, InputAudioTrackBacking } from '../input-track';
|
||||
import { DEFAULT_TRACK_DISPOSITION, MetadataTags } from '../metadata';
|
||||
import { assert, ResultValue, UNDETERMINED_LANGUAGE, Yo } from '../misc';
|
||||
import { assert, MaybeRelevantPromise, ResultValue, UNDETERMINED_LANGUAGE } from '../misc';
|
||||
import { EncodedPacket, PLACEHOLDER_DATA } from '../packet';
|
||||
import { readAscii, readBytes, Reader, readU16, readU32, readU64 } from '../reader';
|
||||
import { parseId3V2Tag, readId3V2Header } from '../id3';
|
||||
@@ -403,7 +403,7 @@ class WaveAudioTrackBacking implements InputAudioTrackBacking {
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
packetIndex: number,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
assert(this.demuxer.audioInfo);
|
||||
const startOffset = packetIndex * PACKET_SIZE_IN_FRAMES * this.demuxer.audioInfo.blockSizeInBytes;
|
||||
if (startOffset >= this.demuxer.dataSize) {
|
||||
@@ -457,7 +457,7 @@ class WaveAudioTrackBacking implements InputAudioTrackBacking {
|
||||
));
|
||||
}
|
||||
|
||||
getFirstPacket(res: ResultValue<EncodedPacket | null>, options: PacketRetrievalOptions): Promise<Yo> {
|
||||
getFirstPacket(res: ResultValue<EncodedPacket | null>, options: PacketRetrievalOptions): MaybeRelevantPromise {
|
||||
return this.getPacketAtIndex(res, 0, options);
|
||||
}
|
||||
|
||||
@@ -465,7 +465,7 @@ class WaveAudioTrackBacking implements InputAudioTrackBacking {
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
timestamp: number,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
assert(this.demuxer.audioInfo);
|
||||
|
||||
const packetIndex = Math.floor(Math.min(
|
||||
@@ -514,7 +514,7 @@ class WaveAudioTrackBacking implements InputAudioTrackBacking {
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
packet: EncodedPacket,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
assert(this.demuxer.audioInfo);
|
||||
|
||||
const packetIndex = packet.sequenceNumber;
|
||||
@@ -529,7 +529,7 @@ class WaveAudioTrackBacking implements InputAudioTrackBacking {
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
timestamp: number,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
return this.getPacket(res, timestamp, options);
|
||||
}
|
||||
|
||||
@@ -537,7 +537,7 @@ class WaveAudioTrackBacking implements InputAudioTrackBacking {
|
||||
res: ResultValue<EncodedPacket | null>,
|
||||
packet: EncodedPacket,
|
||||
options: PacketRetrievalOptions,
|
||||
): Promise<Yo> {
|
||||
): MaybeRelevantPromise {
|
||||
return this.getNextPacket(res, packet, options);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user