Adds the audio player WebCodecs decode pipeline.

This commit is contained in:
BinaryAlley committed 2026-10-16 18:33:25 +03:00
1 parent 4e06cc47d3
commit fbc67c81c3
6 files changed
+1213 -7

No files matched your search

@@ -69,6 +69,7 @@
<script defer src="~/js/audio-player/audio-player-events.js" asp-append-version="true"></script>
<script defer src="~/js/audio-player/audio-player-download.js" asp-append-version="true"></script>
<script defer src="~/js/audio-player/audio-player-render.js" asp-append-version="true"></script>
<script defer src="~/js/audio-player/audio-player-decode.js" asp-append-version="true"></script>
<script defer src="~/js/audio-player/audio-player-capabilities.js" asp-append-version="true"></script>
<script defer src="~/js/audio-player/audio-player-engine.js" asp-append-version="true"></script>
<script defer src="~/js/audio-player/audio-player.js" asp-append-version="true"></script>
File diff suppressed because it is too large. Load diff
@@ -131,12 +131,13 @@
}
/**
* Checks whether a value is the AbortError a cancelled fetch or stream read rejects with.
* Checks whether a value is the AbortError a cancelled fetch or stream read rejects with. It is tested by name rather
* than by type, because in a browser an AbortError is a DOMException, which is not an Error.
* @param {*} error - The value to check.
* @returns {boolean} True when the value is an AbortError.
*/
function isAbortError(error) {
return error instanceof Error && error.name === 'AbortError';
return !!error && error.name === 'AbortError';
}
/**
@@ -35,9 +35,10 @@
* bar shows the same loading state for an engine's own range fetch as it does for a seek. The state it sets
* stays until the engine reports it is no longer buffering, and a playhead report never clears it.
* - host.reportEnded(): reports that the current track reached its end.
* - host.reportError(error): reports an engine failure, wrapped into a {@link LuminaAudioPlayer.PlayerError}. A download
* engine must forward its downloader's onError here, because the downloader only reports to its own callback and
* never emits on the event bus, so a failure that is not forwarded stays unobserved.
* - host.reportError(error): reports an engine failure, wrapped into a {@link LuminaAudioPlayer.PlayerError}. Every sub
* component that reports through its own callback must be forwarded here, because such a component only reports to its
* own callback and never emits on the event bus, so a failure that is not forwarded stays unobserved. The download
* engine must forward its downloader's onError, and an engine that decodes must forward its decoder's onError as well.
*
* Registering and selecting an engine
* -----------------------------------
@@ -25,6 +25,11 @@
ENGINE_LOAD_FAILED: 'engine-load-failed',
// The engine reported a failure while decoding, buffering or rendering audio.
ENGINE_ERROR: 'engine-error',
// The WebCodecs decode pipeline could not be configured, refused a chunk, or failed to decode one.
DECODE_FAILED: 'decode-failed',
// The decoder has no room for another chunk right now, so a caller that paces itself should wait. Unlike
// DECODE_FAILED, which is fatal, this is a transient, recoverable pacing condition.
DECODER_BUSY: 'decoder-busy',
// The download of a track failed, or was rejected by the stream.
DOWNLOAD_FAILED: 'download-failed',
// The download of a track was intentionally stopped, by an abort or a destroy, so a reader can tell a teardown apart from a stream failure.
@@ -33,6 +38,27 @@
PREFERENCES_LOAD_FAILED: 'preferences-load-failed'
});
/**
* Enum-like object for the stable <c>details.reason</c> values that cross a module boundary of the player.
*
* A reason is a structured detail of a {@link LuminaAudioPlayer.PlayerError} that a consumer branches on, so its value
* is defined once here and shared, never written as a literal by the producer and the consumer separately: two copies
* of a literal drift apart silently, and a consumer that keys on a stale one reclassifies a recoverable condition as
* a fatal failure without anything failing to compile.
* @readonly
* @enum {string}
*/
LuminaAudioPlayer.PlayerErrorReason = Object.freeze({
// A scheduler refused a decoded frame because its own scheduled ahead ceiling is tighter than the caller's, so the
// frame is backpressure rather than a failure. Published by the renderer's schedule and recognized by the decode
// pipeline, which pauses feeding instead of stopping.
SCHEDULED_AHEAD_LIMIT: 'scheduled-ahead-limit',
// A scheduler's scheduled ahead ceiling is smaller than one frame plus its scheduling lead, so it can never accept a
// frame at all. Unlike SCHEDULED_AHEAD_LIMIT, which is recoverable backpressure, this is terminal: the decode
// pipeline stops because it can never make progress.
SCHEDULED_AHEAD_UNSATISFIABLE: 'scheduled-ahead-unsatisfiable'
});
/**
* An error raised by the player, carrying a stable code and optional structured details.
*/
@@ -674,7 +674,7 @@
* the current track resumes the playback state, so the playhead and the callbacks continue to run.
* @param {AudioData} audioData - The decoded audio to schedule. Ownership transfers to the renderer, which closes it.
* @returns {number} The media position, in seconds, at the end of the scheduled audio.
* @throws {LuminaAudioPlayer.PlayerError} When the renderer was destroyed, or the audio could not be converted or scheduled.
* @throws {LuminaAudioPlayer.PlayerError} When the renderer was destroyed, or the audio could not be converted or scheduled. A refusal caused by the renderer's own <c>maxScheduledAheadSeconds</c> ceiling carries <c>details.reason === {@link LuminaAudioPlayer.PlayerErrorReason}.SCHEDULED_AHEAD_LIMIT</c> and is backpressure rather than corruption: it is thrown without being recorded as the renderer's last error, so a caller can treat it as recoverable. The frame is closed before this call returns or throws, whichever happens, so a caller must never close it and a refusal never leaks. Both properties are load-bearing for a caller, such as the decode pipeline, that classifies the refusal from the thrown value.
*/
schedule(audioData) {
// The decoded audio is consumed by this call whatever happens, so it is released even when the renderer refuses
@@ -701,7 +701,10 @@
// the clock and one lead ahead otherwise.
const prospectiveStart = hasScheduled && this.nextStartTime > now ? this.nextStartTime : now + MIN_SCHEDULE_LEAD_SECONDS;
if (this.maxScheduledAheadSeconds !== null && prospectiveStart + wallDuration - now > this.maxScheduledAheadSeconds)
throw this.raise(new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_ERROR, 'The schedule would run further ahead than the renderer allows.', { reason: 'scheduled-ahead-limit' }));
// The refusal is backpressure, not a failure: a caller that paces itself retries once the schedule has
// drained, so it is thrown without being recorded as the renderer's last error, which would mark a
// renderer that is still healthy as failed, contradicting the reason the shared enum publishes.
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_ERROR, 'The schedule would run further ahead than the renderer allows.', { reason: LuminaAudioPlayer.PlayerErrorReason.SCHEDULED_AHEAD_LIMIT });
let when = this.nextStartTime;
// The chain is anchored on its first buffer, and re-anchored whenever it fell behind the context clock, which
// is an underrun: the buffer starts as soon as it can and the playhead follows the audio that actually plays.
@@ -970,6 +973,18 @@
return Math.max(0, this.nextStartTime - this.context.currentTime);
}
/**
* Gets the scheduled ahead ceiling the renderer enforces, in seconds, or null when it enforces none.
*
* It is published so that a caller that paces itself, such as the decode pipeline, can keep its own ceiling
* strictly below it and never feed a frame the renderer would refuse. Keeping a decode ceiling above the renderer's
* is what makes the renderer refuse, and a refused frame is closed and lost.
* @returns {number|null} The ceiling, in seconds, or null when the renderer enforces none.
*/
getMaxScheduledAheadSeconds() {
return this.maxScheduledAheadSeconds;
}
/**
* Checks whether the decode pipeline has stated that it fed the last decoded frame of the track.
* @returns {boolean} True when the track is known to be complete.