Adds seekable normalized audio track delivery

This commit is contained in:
BinaryAlley committed 2026-10-18 14:55:56 +03:00
1 parent 4e13b23f16
commit 45f9aa3bd5
57 files changed
+3452 -344

No files matched your search

@@ -37,11 +37,13 @@
* The needs normalization event
* -----------------------------
* It is published on the player event bus as {@link LuminaAudioPlayer.PlayerEvent.NEEDS_NORMALIZATION}, with the payload
* { container, codec, reason }, plus directPlay when the caller supplied it, where the reason is one of the
* { container, codec, reason }, plus isDirectPlay when the caller supplied it, where the reason is one of the
* {@link LuminaAudioPlayer.PlayerErrorReason} values NO_DEMUXER, CODEC_UNSUPPORTED or DEMUXER_FAILED. A consumer that
* asked for a normalized delivery uses it to request one. A normalized manifest, whose directPlay flag is false, is
* asked for a normalized delivery uses it to request one. A normalized manifest, whose isDirectPlay flag is false, is
* refused by {@link LuminaAudioPlayer.createDemuxerForManifest} without an event, because the server already normalized
* such a track on purpose and its stream is not in the stored container.
* such a track on purpose and its stream is not in the stored container. The normalized delivery is parsed by
* {@link LuminaAudioPlayer.NormalizedSegmentDemuxer}, which builds one of these same demuxers per segment from the
* delivery container and codec of the manifest.
*/
(function (LuminaAudioPlayer) {
'use strict';
@@ -82,9 +84,9 @@
* @param {string} container - The canonical container of the track.
* @param {string} codec - The canonical codec of the track.
* @param {string} reason - One of the {@link LuminaAudioPlayer.PlayerErrorReason} values.
* @param {boolean} [directPlay] - The direct play flag of the manifest, when the caller has one.
* @param {boolean} [isDirectPlay] - The direct play flag of the manifest, when the caller has one.
*/
function publishNeedsNormalization(events, container, codec, reason, directPlay) {
function publishNeedsNormalization(events, container, codec, reason, isDirectPlay) {
if (!events || !LuminaAudioPlayer.PlayerEvent || typeof events.emit !== 'function')
return;
const payload = {
@@ -96,8 +98,8 @@
// know, which is not the same as a false flag, the documented signal that the server already normalized the
// track. Coercing an omitted flag to false would make every caller that has no manifest look like one whose
// track the server chose to normalize.
if (directPlay !== undefined)
payload.directPlay = directPlay === true;
if (isDirectPlay !== undefined)
payload.isDirectPlay = isDirectPlay === true;
events.emit(LuminaAudioPlayer.PlayerEvent.NEEDS_NORMALIZATION, Object.freeze(payload));
}
@@ -176,20 +178,20 @@
* would silently drop the only signal that says which delivery to request.
* @param {string} container - The container of the track.
* @param {string} codec - The codec of the track.
* @param {boolean} [directPlay] - The direct play flag of the manifest, carried into the event.
* @param {boolean} [isDirectPlay] - The direct play flag of the manifest, carried into the event.
* @returns {Promise<boolean>} True when a demuxer exists and the browser can decode the codec.
* @throws {TypeError} When no capability detector or no event bus was injected. It is a composition error, not a
* property of the track, so it rejects rather than classifying the track.
*/
async canPlayDirectlyAsync(container, codec, directPlay) {
async canPlayDirectlyAsync(container, codec, isDirectPlay) {
const events = requireInjected(this.events, 'event bus');
const descriptor = this.match(container, codec);
if (!descriptor) {
publishNeedsNormalization(events, container, codec, LuminaAudioPlayer.PlayerErrorReason.NO_DEMUXER, directPlay);
publishNeedsNormalization(events, container, codec, LuminaAudioPlayer.PlayerErrorReason.NO_DEMUXER, isDirectPlay);
return false;
}
if (!await this.isCodecSupportedAsync(descriptor)) {
publishNeedsNormalization(events, container, codec, LuminaAudioPlayer.PlayerErrorReason.CODEC_UNSUPPORTED, directPlay);
publishNeedsNormalization(events, container, codec, LuminaAudioPlayer.PlayerErrorReason.CODEC_UNSUPPORTED, isDirectPlay);
return false;
}
return true;
@@ -204,7 +206,7 @@
async canPlayManifestDirectlyAsync(manifest) {
if (!manifest)
return false;
return await this.canPlayDirectlyAsync(manifest.sourceContainer, manifest.sourceCodec, manifest.directPlay);
return await this.canPlayDirectlyAsync(manifest.sourceContainer, manifest.sourceCodec, manifest.isDirectPlay);
}
/**
@@ -288,7 +290,7 @@
const events = requireInjected(dependencies.events, 'event bus');
const descriptor = dependencies.registry ? dependencies.registry.find(container, codec) : null;
if (!descriptor) {
publishNeedsNormalization(events, container, codec, LuminaAudioPlayer.PlayerErrorReason.NO_DEMUXER, settings.directPlay);
publishNeedsNormalization(events, container, codec, LuminaAudioPlayer.PlayerErrorReason.NO_DEMUXER, settings.isDirectPlay);
return null;
}
let demuxer = null;
@@ -307,7 +309,7 @@
// whose bytes could not be fetched, so the failure is propagated and reported as the download failure it is.
if (error instanceof LuminaAudioPlayer.PlayerError && error.code === LuminaAudioPlayer.PlayerErrorCode.DOWNLOAD_FAILED)
throw error;
publishNeedsNormalization(events, container, codec, LuminaAudioPlayer.PlayerErrorReason.DEMUXER_FAILED, settings.directPlay);
publishNeedsNormalization(events, container, codec, LuminaAudioPlayer.PlayerErrorReason.DEMUXER_FAILED, settings.isDirectPlay);
return null;
}
const configuration = demuxer.getConfiguration();
@@ -323,7 +325,7 @@
}
if (!isConfigurationSupported) {
await demuxer.destroyAsync();
publishNeedsNormalization(events, container, codec, LuminaAudioPlayer.PlayerErrorReason.CODEC_UNSUPPORTED, settings.directPlay);
publishNeedsNormalization(events, container, codec, LuminaAudioPlayer.PlayerErrorReason.CODEC_UNSUPPORTED, settings.isDirectPlay);
return null;
}
// The demuxer is usable, so the caller's error callback is attached from here on: a failure while reading is a
@@ -344,9 +346,9 @@
* @returns {Promise<LuminaAudioPlayer.Demuxer|null>} The ready demuxer, or null for a manifest that is not a direct play, or one that must be normalized.
*/
LuminaAudioPlayer.createDemuxerForManifest = function (manifest, options) {
if (!manifest || manifest.directPlay !== true)
if (!manifest || manifest.isDirectPlay !== true)
return Promise.resolve(null);
return LuminaAudioPlayer.createDemuxerFor(manifest.sourceContainer, manifest.sourceCodec, Object.assign({}, options, { directPlay: true }));
return LuminaAudioPlayer.createDemuxerFor(manifest.sourceContainer, manifest.sourceCodec, Object.assign({}, options, { isDirectPlay: true }));
};
/**
@@ -74,12 +74,25 @@
// which reads it as end of file rather than as a failure, so a rename on either side cannot silently turn a
// drained window into a fatal read error.
WINDOW_UNAVAILABLE: 'window-unavailable',
// A normalized segment that the production says exists could not be read, because the track, the library or the
// audio file is gone, or the cache entry was evicted. Unlike the end of the audio, which is declared by the
// segment count of the production, this is a playback failure; the player refreshes the manifest once and resumes.
SEGMENT_NOT_FOUND: 'segment-not-found',
// A normalized segment belongs to a delivery the server no longer accepts, because the delivery handle expired or the source changed.
// The player refetches the manifest and resumes at the current position instead of failing the track.
PLAYBACK_DELIVERY_CHANGED: 'playback-delivery-changed',
// Every normalization job slot was in use for the whole retry window, so the segment of a normalized track could
// not be produced in time. The request is transient and a later play can succeed.
NORMALIZATION_UNAVAILABLE: 'normalization-unavailable',
// A segment request failed for an HTTP reason that is neither a transient normalization limit nor a missing segment.
// Unlike WINDOW_UNAVAILABLE, which the demuxer byte source reads as end of file, this is always a playback failure.
HTTP_REQUEST_FAILED: 'http-request-failed',
// A range request exceeded its timeout. Unlike an intentional abort, this is a failed request and is not retried
// by the seek recovery.
REQUEST_TIMEOUT: 'request-timeout',
// The playback manifest says the track is not a direct play, so the server has to normalize its container or its
// codec before the bundled player can play it. Published by the media pipeline engine while the normalized
// delivery pipeline does not exist yet, instead of playing the stored bytes.
// The stored format of a track cannot be demuxed and decoded by the bundled player, so the server has to normalize
// its container or its codec. Published by the media pipeline engine instead of playing bytes whose format the
// bundled demuxers cannot read; a normalized manifest is played through the segment pipeline.
NORMALIZED_DELIVERY_REQUIRED: 'normalized-delivery-required',
// A load was overtaken by a newer play request before it finished. The player ignores it, because the newer
// request owns the player state and the queue.
@@ -42,7 +42,7 @@
// A track cannot be demuxed and decoded directly by the bundled player, so the server has to normalize it (remux or
// transcode) before it can play. Payload: { container, codec, reason }, where reason is one of the
// {@link LuminaAudioPlayer.PlayerErrorReason} values NO_DEMUXER, CODEC_UNSUPPORTED or DEMUXER_FAILED, and, when the
// caller supplied one, directPlay, the direct play flag of the manifest it asked about, so a consumer can tell a
// caller supplied one, isDirectPlay, the direct play flag of the manifest it asked about, so a consumer can tell a
// client-side capability gap from a track the server already decided to normalize. The flag is absent, not false,
// when the caller had no manifest.
NEEDS_NORMALIZATION: 'needsnormalization',
@@ -190,6 +190,119 @@
return resolved.href;
}
/**
* Checks that a resolved segment path matches the segment route template this application emitted and belongs to the expected track, library and segment index.
* @param {URL} url - The resolved segment URL.
* @param {Object} descriptor - The play descriptor the manifest belongs to.
* @param {Object} configuration - The endpoints configuration of the player.
* @param {number} segmentIndex - The zero based index of the segment.
* @param {string} deliveryId - The opaque delivery handle the manifest reported.
* @throws {LuminaAudioPlayer.PlayerError} When the route template is missing, is on another origin, or the path does not
* match the route, the expected track and library, the expected segment index, or the expected delivery handle.
*/
function validateSegmentStreamRoute(url, descriptor, configuration, segmentIndex, deliveryId) {
const templateValue = configuration && configuration.streamTrackSegmentRouteTemplate;
if (typeof templateValue !== 'string' || templateValue.length === 0)
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The segment stream route template is not configured.');
let template;
try {
template = new URL(templateValue, getBaseOrigin());
} catch (error) {
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The segment stream route template is not a valid URL.', { cause: error });
}
if ((template.protocol !== 'http:' && template.protocol !== 'https:')
|| (hasWindowOrigin() && template.origin !== window.location.origin)
|| template.origin !== url.origin
|| template.username !== ''
|| template.password !== '')
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The segment stream route template is not on this origin.');
const expectedIds = {
artistId: descriptor && descriptor.artistId,
albumId: descriptor && descriptor.albumId,
trackId: descriptor && descriptor.trackId
};
const templateSegments = template.pathname.split('/');
const streamSegments = url.pathname.split('/');
if (templateSegments.length !== streamSegments.length)
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The segment path does not match the segment route.');
for (let index = 0; index < templateSegments.length; index++) {
let templateSegment;
try {
templateSegment = decodeURIComponent(templateSegments[index]);
} catch (error) {
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The segment route template carries an invalid escape.', { cause: error });
}
const placeholder = /^\{([A-Za-z0-9_]+)\}$/.exec(templateSegment);
let actualSegment;
try {
// Decode the actual segment too, so a path base or a static segment that contains an encoded character still compares equal to the template it came from.
actualSegment = decodeURIComponent(streamSegments[index]);
} catch (error) {
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The segment path carries an invalid escape.', { cause: error });
}
if (placeholder) {
const name = placeholder[1];
if (name === 'segmentIndex') {
if (!Number.isInteger(segmentIndex) || segmentIndex < 0 || actualSegment !== String(segmentIndex))
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The segment path does not belong to the expected segment index.');
continue;
}
if (name === 'deliveryId') {
// The path must name the delivery handle the manifest reported, not another delivery of the same track.
if (typeof deliveryId !== 'string' || actualSegment !== deliveryId)
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The segment path does not belong to the expected delivery.');
continue;
}
if (!Object.prototype.hasOwnProperty.call(expectedIds, name))
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The segment route template carries an unknown placeholder.');
const expected = expectedIds[name];
if (!isGuid(expected))
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.INVALID_DESCRIPTOR, `The play descriptor carries no valid ${name}.`, { field: name });
// The path must describe the track of this descriptor, not merely another track of the same library.
if (!isGuid(actualSegment) || actualSegment.toLowerCase() !== expected.toLowerCase())
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The segment path does not belong to the expected track.');
continue;
}
if (templateSegment !== actualSegment)
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The segment path does not match the segment route.');
}
const libraryId = descriptor && descriptor.libraryId;
if (!isGuid(libraryId))
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.INVALID_DESCRIPTOR, 'The play descriptor carries no valid library Id.', { field: 'libraryId' });
const requestedLibraryIds = url.searchParams.getAll('libraryId');
if (requestedLibraryIds.length !== 1 || requestedLibraryIds[0].toLowerCase() !== libraryId.toLowerCase())
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The segment path does not belong to the expected library.');
}
/**
* Resolves the segment path template of a normalized playback manifest into the URL of one segment.
* @param {Object} manifest - The playback manifest.
* @param {Object} descriptor - The play descriptor the manifest belongs to.
* @param {Object} configuration - The endpoints configuration of the player.
* @param {number} segmentIndex - The zero based index of the segment to fetch.
* @returns {string} The validated resolved segment URL.
* @throws {LuminaAudioPlayer.PlayerError} When the manifest carries no safe segment path, the index is invalid, or the
* path is not the segment route of the expected track, library and index.
*/
function resolveSegmentStreamUrl(manifest, descriptor, configuration, segmentIndex) {
if (!Number.isInteger(segmentIndex) || segmentIndex < 0)
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The segment index is not valid.');
const deliveryId = manifest && manifest.deliveryId;
// The delivery handle is an opaque server issued token, so it is confined to the characters a path segment can carry and never contains a
// separator, a query, a fragment or a control character that could change the route it is substituted into.
if (typeof deliveryId !== 'string' || !/^[0-9a-f]{32}$/.test(deliveryId))
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The manifest carries no valid delivery handle.');
const template = manifest && manifest.segmentStreamPathTemplate;
if (typeof template !== 'string' || !template.includes('{deliveryId}') || !template.includes('{segmentIndex}'))
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The manifest carries no segment stream path template.');
const value = template
.split('{deliveryId}').join(deliveryId)
.split('{segmentIndex}').join(String(segmentIndex));
const resolved = resolveSameOriginUrl(value);
validateSegmentStreamRoute(resolved.url, descriptor, configuration, segmentIndex, deliveryId);
return resolved.href;
}
/**
* Builds the URL of the playback manifest endpoint for a descriptor, including the capability list.
* @param {Object} configuration - The endpoints configuration of the player.
@@ -317,5 +430,6 @@
}
LuminaAudioPlayer.resolveStreamUrl = resolveStreamUrl;
LuminaAudioPlayer.resolveSegmentStreamUrl = resolveSegmentStreamUrl;
LuminaAudioPlayer.fetchPlaybackManifestAsync = fetchPlaybackManifestAsync;
})(window.LuminaAudioPlayer = window.LuminaAudioPlayer || {});
@@ -40,13 +40,13 @@
*
* Gapless transitions
* -------------------
* When the user enabled gapless playback and the player queue holds another item, the engine prefetches the next item as
* soon as the current one is loaded: it fetches the next manifest, opens its demuxer, and pre-decodes the first seconds of
* its audio into memory. The prefetch does not run an open-ended download next to the current one: it fetches only the
* windows its pre-decode needs, as bounded range requests, and leaves the sequential download of the next track stopped
* until the handoff. Once the current decoder has scheduled its last frame, the pre-decoded audio is appended to the
* renderer schedule, exactly after the audio of the current track, the download of the next track resumes from its first
* missing byte, and the rest of the track is decoded into the same schedule. The engine then switches the current item at the exact media position where the next
* When the user enabled gapless playback and the player queue holds another item, the engine waits until the current track is within a short lead
* of its end, then fetches the next manifest and pre-decodes its first audio. Nothing is requested at load time: a normalized next track would start
* a full server side production that is expensive and bounded in concurrency, and a direct track would hold bytes for a track the user may never
* reach. Normalized gapless therefore only happens when the production is already cached or finishes within the lead; otherwise the next track
* starts normally at the boundary. Once the current decoder has scheduled its last frame, the pre-decoded audio is appended to the renderer
* schedule, exactly after the audio of the current track, the download of the next track resumes from its first missing byte, and the rest of the
* track is decoded into the same schedule. The engine then switches the current item at the exact media position where the next
* audio starts, reports the new duration and asks the player to advance its queue, so the bar shows the new track while
* the audio never stops. Only the last track of a continuous chain declares the stream complete, which is what keeps the
* renderer from reporting an end between two gapless items.
@@ -56,10 +56,11 @@
* A download, demux or decode failure that happens mid-stream is reported once, through host.reportError, and stops the
* pipeline cleanly; it never escapes as an unhandled rejection and never leaves the renderer or the downloader running.
* A failure of a prefetch is dropped instead, because it must not interrupt the track that is playing. A track whose
* manifest is not a direct play needs the normalized delivery pipeline, which this engine does not implement yet, so it
* is reported as an engine load failure with the {@link LuminaAudioPlayer.PlayerErrorReason.NORMALIZED_DELIVERY_REQUIRED}
* reason. A play descriptor must name a track, not an album or an artist, because the manifest endpoint is addressed by the
* track and its album and artist; expanding an album or an artist context into its tracks is the queue engine's job.
* manifest is not a direct play is played from the fixed duration segments the server produces, through the normalized
* delivery pipeline (audio-player-pipeline-normalized.js), and it is seeked by segment index exactly like a direct play is
* seeked by byte range. A play descriptor must name a track, not an album or an artist, because the manifest endpoint is
* addressed by the track and its album and artist; expanding an album or an artist context into its tracks is the queue
* engine's job.
*/
(function (LuminaAudioPlayer) {
'use strict';
@@ -108,6 +109,15 @@
*/
const PREFETCH_HANDOFF_MAX_WAIT_SECONDS = 5;
/**
* The playback time, in seconds, before the end of the current track at which the next item is prefetched. A direct play needs a few seconds
* to pre-decode its first audio, and a normalized production is expensive and bounded in concurrency, so starting either at load time would
* hold resources for a track the user may never reach.
* @readonly
* @type {number}
*/
const PREFETCH_LEAD_SECONDS = 15;
/**
* Coerces a value to a finite number, or null when it is absent or not finite.
* @param {*} value - The value to coerce.
@@ -195,10 +205,6 @@
const manifest = await this.fetchManifestAsync(descriptor, signal);
if (!this.isLoadCurrent(token))
throw this.createSupersededLoadError();
if (manifest.directPlay !== true) {
// The normalized delivery pipeline is a later stage; until it exists, the track is reported instead of being played as its stored bytes, which would produce noise or a decoder error.
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The track needs a normalized delivery the player does not support yet.', { reason: LuminaAudioPlayer.PlayerErrorReason.NORMALIZED_DELIVERY_REQUIRED });
}
this.renderer.configure(manifest);
const item = await this.buildItemAsync(descriptor, manifest, false);
if (!this.isLoadCurrent(token) || this.destroyed) {
@@ -705,39 +711,83 @@
disposed: false,
failed: false,
stopped: false,
hasScheduledAudio: false
hasScheduledAudio: false,
// A delivery refresh is attempted once per item. If the refreshed handle is rejected again, the failure is terminal, so a stale or
// broken delivery cannot make the player refetch the manifest forever.
deliveryRecoveryAttempted: false,
deliveryRecoveryInProgress: false
};
const streamPath = LuminaAudioPlayer.resolveStreamUrl(manifest, descriptor, this.getConfiguration());
item.downloader = LuminaAudioPlayer.createTrackDownloader({
url: streamPath,
durationInSeconds: duration,
shouldKeepDownloadingWhilePaused: preferences.shouldKeepDownloadingWhilePaused,
events: this.events,
onProgress: (downloadedFraction) => {
if (item === this.current)
this.host.reportProgress(downloadedFraction);
},
onError: (error) => this.handleDownloadFailure(item, error)
});
if (typeof onItemCreated === 'function')
onItemCreated(item);
// The demuxer reads through the seek aware source, so a seek can fetch a window below the byte target of the download loop instead of waiting for bytes the loop never fetches.
item.source = new LuminaAudioPlayer.SeekAwareByteSource(item.downloader);
if (isPrefetch) {
// A prefetch fetches only the windows its pre-decode needs, through one-shot range requests, instead of
// running a second open-ended download from the moment the current track starts. Its sequential download is
// started at the handoff, from the first byte that is still missing.
item.source.setSeeking(true);
} else {
item.downloader.startAsync();
}
try {
const demuxer = await LuminaAudioPlayer.createDemuxerForManifest(manifest, {
source: item.source,
capabilities: this.capabilities,
events: this.events,
onError: (error) => this.failItem(item, error)
});
const isNormalizedDelivery = manifest.isDirectPlay !== true;
let demuxer = null;
if (isNormalizedDelivery) {
const segmentDurationSeconds = Number(manifest.segmentDurationSeconds);
if (!manifest.deliveryContainer || !manifest.deliveryCodec || !Number.isInteger(segmentDurationSeconds) || segmentDurationSeconds <= 0)
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The normalized delivery of the track is incomplete.');
const configuration = this.getConfiguration();
item.downloader = LuminaAudioPlayer.createNormalizedSegmentDownloader({
durationInSeconds: duration,
segmentDurationSeconds: segmentDurationSeconds,
urlBuilder: (segmentIndex) => LuminaAudioPlayer.resolveSegmentStreamUrl(manifest, descriptor, configuration, segmentIndex),
shouldRetryUnavailable: !isPrefetch,
shouldKeepDownloadingWhilePaused: preferences.shouldKeepDownloadingWhilePaused,
events: this.events,
onProgress: (downloadedFraction) => {
if (item === this.current)
this.host.reportProgress(downloadedFraction);
},
onError: (error) => this.handleDownloadFailure(item, error)
});
if (typeof onItemCreated === 'function')
onItemCreated(item);
// The normalized demuxer fetches complete segments itself, so it needs no byte source. The engine still drives a source on
// seek and teardown, so a no-op source of the same shape keeps that path uniform without a download loop behind it.
item.source = {
setSeeking: () => {},
destroy: () => {}
};
if (!isPrefetch)
item.downloader.startAsync();
demuxer = await LuminaAudioPlayer.createNormalizedSegmentDemuxer(manifest, {
downloader: item.downloader,
capabilities: this.capabilities,
events: this.events,
onError: (error) => this.failItem(item, error)
});
} else {
const streamPath = LuminaAudioPlayer.resolveStreamUrl(manifest, descriptor, this.getConfiguration());
item.downloader = LuminaAudioPlayer.createTrackDownloader({
url: streamPath,
durationInSeconds: duration,
shouldKeepDownloadingWhilePaused: preferences.shouldKeepDownloadingWhilePaused,
events: this.events,
onProgress: (downloadedFraction) => {
if (item === this.current)
this.host.reportProgress(downloadedFraction);
},
onError: (error) => this.handleDownloadFailure(item, error)
});
if (typeof onItemCreated === 'function')
onItemCreated(item);
// The demuxer reads through the seek aware source, so a seek can fetch a window below the byte target of the download loop instead of waiting for bytes the loop never fetches.
item.source = new LuminaAudioPlayer.SeekAwareByteSource(item.downloader);
if (isPrefetch) {
// A prefetch fetches only the windows its pre-decode needs, through one-shot range requests, instead of
// running a second open-ended download from the moment the current track starts. Its sequential download is
// started at the handoff, from the first byte that is still missing.
item.source.setSeeking(true);
} else {
item.downloader.startAsync();
}
}
if (!demuxer) {
demuxer = await LuminaAudioPlayer.createDemuxerForManifest(manifest, {
source: item.source,
capabilities: this.capabilities,
events: this.events,
onError: (error) => this.failItem(item, error)
});
}
if (!demuxer) {
// A cancelled or aborted header probe and a transport failure are not properties of the track, so
// they are reported as themselves. Only a probe that actually concluded the bundled player cannot
@@ -1112,9 +1162,14 @@
item.prefetch = token;
const phase = (async () => {
try {
// The prefetch waits for the monitor to report that the current track is close enough to its end. No manifest is requested and no
// normalized production is started before then, so a skipped or abandoned track never holds a delivery handle or a normalization slot.
const isWindowOpen = await this.waitForPrefetchWindowAsync(item, token);
if (!isWindowOpen)
return null;
const signal = token.abortController ? token.abortController.signal : undefined;
const manifest = await this.fetchManifestAsync(nextDescriptor, signal);
if (token.cancelled || this.destroyed || item.stopped || manifest.directPlay !== true)
if (token.cancelled || this.destroyed || item.stopped)
return null;
const prefetched = await this.buildItemAsync(nextDescriptor, manifest, true, (created) => {
token.item = created;
@@ -1157,6 +1212,70 @@
});
}
/**
* Waits until the current track is close enough to its end for the prefetch to start. The wait is resolved by the playback monitor when it
* crosses the lead, and is rejected as false when the prefetch is cancelled, so no timer polls the whole track and a paused track starts nothing until it resumes near the end.
* @param {Object} item - The current item whose successor is prefetched.
* @param {Object} token - The prefetch token of the current item.
* @returns {Promise<boolean>} True when the prefetch window opened, false when the prefetch was cancelled or the item stopped.
*/
waitForPrefetchWindowAsync(item, token) {
return new Promise((resolve) => {
token.windowResolve = resolve;
this.tryOpenPrefetchWindow(item, token);
});
}
/**
* Opens the prefetch window when the current track is within the lead of its end, or closes it when the prefetch is no longer wanted.
* @param {Object} item - The current item whose successor is prefetched.
* @param {Object} token - The prefetch token of the current item.
*/
tryOpenPrefetchWindow(item, token) {
if (!token || !token.windowResolve)
return;
if (token.cancelled || this.destroyed || !item || item.stopped || item.disposed || this.current !== item) {
this.closePrefetchWindow(token);
return;
}
// A completion that already fell back to a normal next-track start has no use for a prefetch, so it is not started only to be thrown away.
if (item.continueToEntry) {
this.closePrefetchWindow(token);
return;
}
if (this.getRemainingPlaybackSeconds(item) > PREFETCH_LEAD_SECONDS)
return;
const resolve = token.windowResolve;
token.windowResolve = null;
resolve(true);
}
/**
* Closes a pending prefetch window as false, so the prefetch phase ends instead of waiting for a track that is no longer current.
* @param {Object} token - The prefetch token whose window is closed.
*/
closePrefetchWindow(token) {
if (!token || !token.windowResolve)
return;
const resolve = token.windowResolve;
token.windowResolve = null;
resolve(false);
}
/**
* Gets how much playback time is left in an item, from the shared renderer playhead.
* @param {Object} item - The item whose remaining time is read.
* @returns {number} The remaining time, in seconds, or zero when the duration is unknown or already reached.
*/
getRemainingPlaybackSeconds(item) {
const duration = Number(item && item.duration);
if (!Number.isFinite(duration) || duration <= 0)
return 0;
const absolute = Number(this.renderer.getCurrentTime());
const local = Number.isFinite(absolute) ? Math.max(0, absolute - Number(item.base || 0)) : 0;
return Math.max(0, duration - local);
}
/**
* Appends the pre-decoded audio of the next item to the renderer schedule, exactly after the audio of the current
* track, and lets its decode continue into the same schedule. The current item stays current until the media position of the next item begins.
@@ -1180,6 +1299,10 @@
next.downloader.setPlaybackPaused(false);
next.downloader.startAsync();
}
// Prefetches are built with retries disabled, because an unavailable production abandons the prefetch instead of holding it open.
// After the handoff this item is the track that owns the playhead, so a temporarily unavailable segment is retried like any current track.
if (typeof next.downloader.setShouldRetryUnavailable === 'function')
next.downloader.setShouldRetryUnavailable(true);
// The end of the scheduled audio of the current track is the exact media position the next track starts at.
next.base = this.renderer.getScheduledMediaDuration();
this.nextItem = next;
@@ -1350,6 +1473,8 @@
return;
item.prefetch = null;
token.cancelled = true;
// A prefetch that is still waiting for its window is released immediately instead of keeping the teardown open.
this.closePrefetchWindow(token);
if (token.abortController) {
try {
token.abortController.abort();
@@ -1426,6 +1551,7 @@
// the monitor neither pauses the renderer nor advances a boundary of the timeline that is being replaced.
if (this.current.isSeeking || this.isLoadingTrack)
return;
this.tryOpenPrefetchWindow(this.current, this.current.prefetch);
this.isMonitoring = true;
try {
const absolute = this.renderer.getCurrentTime();
@@ -1603,6 +1729,138 @@
this.failItem(item, error);
}
/**
* Checks whether a failure is a delivery that the server no longer accepts, which the player can fix by fetching the manifest again.
* @param {Object} item - The item that failed.
* @param {*} error - The failure.
* @returns {boolean} True when the delivery should be refreshed and playback resumed at the current position.
*/
shouldRecoverDelivery(item, error) {
if (!item || item !== this.current || item.deliveryRecoveryAttempted || item.deliveryRecoveryInProgress || this.destroyed)
return false;
if (!error || error.code !== LuminaAudioPlayer.PlayerErrorCode.DOWNLOAD_FAILED || !error.details)
return false;
const reason = error.details.reason;
return reason === LuminaAudioPlayer.PlayerErrorReason.SEGMENT_NOT_FOUND
|| reason === LuminaAudioPlayer.PlayerErrorReason.PLAYBACK_DELIVERY_CHANGED;
}
/**
* Refetches the manifest of the current track and rebuilds the normalized pipeline at the current playback position, used when the
* delivery handle expired, the API restarted, or the source file changed since the manifest was read.
* @param {Object} item - The item whose delivery must be refreshed.
* @param {*} originalError - The failure that triggered the refresh.
* @returns {Promise<void>} A promise that completes when playback resumed on the refreshed delivery.
*/
async recoverDeliveryAsync(item, originalError) {
// The attempt is marked before the first await, because the teardown below can raise failures, and a second recovery started
// from one of them would race this one for the same item.
item.deliveryRecoveryInProgress = true;
item.deliveryRecoveryAttempted = true;
// An underrun pause is a temporary buffer stall, not a user pause, so it must be resumed after the refresh just like active playback.
const wasPlaying = !this.isUserPaused && (this.renderer.isPlaying() || this.isUnderrunPaused);
// The renderer time is shared by the whole queue, while the position to resume at is relative to this track.
const position = Math.max(0, Number(this.renderer.getCurrentTime()) - Number(item.base || 0));
// Stop observing the old pipeline before pausing it, so the monitor does not treat the intentional pause as an underrun and does
// not schedule more work against the delivery that is being replaced.
this.stopMonitor();
this.isUnderrunPaused = false;
this.host.reportBuffering(true);
try {
await this.renderer.pauseAsync();
} catch (pauseError) {
// The old item is released below regardless, so a renderer that refuses to pause must not abort the recovery.
console.debug('Audio player: the renderer could not be paused before refreshing the delivery.', pauseError);
}
// A prefetched successor was built from the old manifest, so keeping it would hand off to the expired delivery handle.
this.cancelPrefetchWait(item);
if (item.prefetch) {
try {
await this.cancelPrefetchAsync(item);
} catch (prefetchError) {
console.debug('Audio player: the prefetch of the failed delivery could not be released.', prefetchError);
}
}
// An already attached next track may have audio scheduled from the old delivery, so it is released before the new manifest
// is read instead of letting two deliveries meet at the track boundary.
if (this.nextItem) {
const attachedNextItem = this.nextItem;
this.nextItem = null;
await this.disposeItemAsync(attachedNextItem).catch((disposeError) => {
console.debug('Audio player: the attached next track of the failed delivery could not be released.', disposeError);
});
}
// The old decode loop and downloads must stop before the replacement is built, because their aborts and failures belong to the
// delivery being replaced and must not be mistaken for failures of the new one.
item.loopToken = null;
if (item.downloader)
item.downloader.abort();
let manifest;
try {
manifest = await this.fetchManifestAsync(item.descriptor);
} catch (manifestError) {
// The user needs the failure that interrupted playback, not the recovery attempt that also failed.
console.debug('Audio player: the manifest of the failed delivery could not be refreshed.', manifestError);
throw originalError;
}
// The awaits above can be overtaken by a new load or by teardown, and recovery must never touch a different current item.
if (this.destroyed || this.current !== item)
return;
if (manifest.isDirectPlay === true) {
// The server now considers the track a direct play. Converting the running normalized pipeline to the direct stream is a different
// transition, so the track ends with the original failure instead of playing mixed deliveries.
throw originalError;
}
try {
// ReplayGain and timeline metadata can change with the refreshed manifest, so the renderer is configured before the
// replacement pipeline starts producing audio.
this.renderer.configure(manifest);
} catch (configureError) {
console.debug('Audio player: the renderer could not be configured for the refreshed delivery.', configureError);
throw originalError;
}
let replacement;
try {
// The replacement owns fresh downloader, demuxer and decoder state bound to the new delivery handle.
replacement = await this.buildItemAsync(item.descriptor, manifest, false);
} catch (buildError) {
console.debug('Audio player: the refreshed delivery could not be prepared.', buildError);
throw originalError;
}
// Building the replacement took another await, so it is discarded if the user left this track in the meantime.
if (this.destroyed || this.current !== item) {
await this.disposeItemAsync(replacement);
return;
}
// The replacement starts at the beginning of this track on the shared timeline. The attempted flag is carried over so a delivery
// that is still rejected after the refresh cannot make the player refetch the manifest forever.
replacement.base = 0;
replacement.deliveryRecoveryAttempted = true;
this.current = replacement;
if (replacement.duration > 0)
this.renderer.setTimelineDuration(replacement.duration);
this.host.reportDuration(replacement.duration);
await this.disposeItemAsync(item);
// A seek rebuilds every stage at the requested position. At zero there is nothing to seek, so the decode loop and its prefetch are started explicitly.
if (position > 0)
await this.seekAsync(position);
else {
this.startDecodeLoop(replacement);
this.startGaplessPrefetch(replacement);
}
// Seeking is asynchronous, so the user may have paused, stopped or replaced the track while it ran. Do not resume a pipeline that no longer owns the playhead.
if (this.current !== replacement || replacement.failed || replacement.stopped || this.destroyed)
return;
// Resume only if audio was actually being heard before the refresh and the user has not paused since. A user pause must stay a pause, while an underrun pause is restored.
if (wasPlaying && !this.isUserPaused) {
await this.renderer.playAsync();
this.startMonitor();
}
this.host.reportBuffering(false);
}
/**
* Records a failure of an item once and reports it to the player when the item is the current one.
* @param {Object} item - The item that failed.
@@ -1613,6 +1871,17 @@
return;
if (LuminaAudioPlayer.isDownloadAbort(error))
return;
// Errors raised by the old pipeline while it is being replaced by a refreshed delivery belong to that teardown, not to a new failure.
if (item.deliveryRecoveryInProgress)
return;
if (this.shouldRecoverDelivery(item, error)) {
this.recoverDeliveryAsync(item, error).catch((recoveryError) => {
console.debug('Audio player: the refreshed delivery could not be applied.', recoveryError);
item.deliveryRecoveryInProgress = false;
this.failItem(item, error);
});
return;
}
item.failed = true;
item.stopped = true;
try {
@@ -1719,10 +1988,12 @@
async disposeItemAsync(item) {
if (!item || item.disposed)
return;
// The flags are set before any await, so callbacks raised by the teardown itself see the item as dead instead of failing it again.
item.disposed = true;
item.stopped = true;
item.loopToken = null;
this.cancelPrefetchWait(item);
// Stop scheduling before the decoder is torn down, so a frame produced during teardown can never enter the renderer schedule.
if (item.scheduler)
item.scheduler.stop();
// The item may itself have prefetched the track after it, which must not keep downloading or decoding once the
@@ -1751,6 +2022,7 @@
}
}
await this.waitForDecodeLoopAsync(item);
// The loop has exited, so the scheduler and the demuxer it could still touch are released next.
if (item.scheduler) {
try {
item.scheduler.dispose();
@@ -1767,6 +2039,7 @@
}
if (item.source)
item.source.destroy();
// The downloader is destroyed last, because the demuxer and the byte source may hold its reads until they are released.
if (item.downloader) {
try {
await item.downloader.destroyAsync();