mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 17:11:10 +00:00
Implements player queue engine
This commit is contained in:
1 parent
45f9aa3bd5
commit
93821a2776
27 files changed
+4114
-186
No files matched your search
@@ -23,6 +23,8 @@
|
||||
* whether a seek target is buffered, in which case the player never shows a loading state for a seek.
|
||||
* - discardCurrentAsync() (optional): releases the currently prepared track without destroying the engine, so a stop
|
||||
* during an automatic continuation does not leave the track it started loaded behind an idle player.
|
||||
* - cancelUpcomingAsync() (optional): drops a prefetched or appended handoff of the current track when the player queue is
|
||||
* replaced under it, for example when shuffle is turned on or off, so audio prepared from the old queue never plays.
|
||||
* - destroyAsync(): releases every resource the engine holds, including any audio context.
|
||||
*
|
||||
* The engine host
|
||||
@@ -175,8 +177,7 @@
|
||||
|
||||
/**
|
||||
* A registry of engine factories, keyed by name, used to resolve the engine the player runs with.
|
||||
* The later stages of the player register the real download, decode and render engine here; the
|
||||
* core module only registers the silent engine.
|
||||
* The real download, decode and render engine is registered here; the core module only registers the silent engine.
|
||||
*/
|
||||
class PlayerEngineRegistry {
|
||||
/**
|
||||
|
||||
@@ -37,7 +37,9 @@
|
||||
// The container of a track could not be parsed into encoded packets, or the bytes it holds do not describe a stream the demuxer supports.
|
||||
DEMUX_FAILED: 'demux-failed',
|
||||
// The stored playback preferences of the user could not be read.
|
||||
PREFERENCES_LOAD_FAILED: 'preferences-load-failed'
|
||||
PREFERENCES_LOAD_FAILED: 'preferences-load-failed',
|
||||
// A play context, a queue batch or a shuffle batch could not be built from the web endpoints.
|
||||
QUEUE_LOAD_FAILED: 'queue-load-failed'
|
||||
});
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Event bus of the audio player.
|
||||
*
|
||||
* The player is decoupled from its consumers (the player bar, the transport UI, the queue panel, themes) through a single observable.
|
||||
* The player is decoupled from its consumers (the player bar, the transport UI, the queue panel, themes) through a single observable.
|
||||
* Every module of the player publishes to this bus, and every consumer subscribes to it, so that no module needs a direct reference to another one.
|
||||
*/
|
||||
(function (LuminaAudioPlayer) {
|
||||
@@ -17,7 +17,11 @@
|
||||
STATE_CHANGED: 'statechange',
|
||||
// The currently playing track changed. Payload: the play descriptor.
|
||||
DESCRIPTOR_CHANGED: 'descriptorchange',
|
||||
// The queue changed. Payload: the ordered list of play descriptors.
|
||||
// The playable state of the queue changed: cursor, current entry, counts, repeat mode, shuffle mode or context.
|
||||
// Payload: the compact summary returned by getQueueStateSummary(), not the full queue.
|
||||
QUEUE_STATE_CHANGED: 'queuestatechange',
|
||||
// Deprecated legacy queue event. It keeps its original payload, the ordered list of play descriptors, and is emitted
|
||||
// only when a legacy consumer is subscribed. New code must use QUEUE_STATE_CHANGED and its compact summary.
|
||||
QUEUE_CHANGED: 'queuechange',
|
||||
// The playhead moved. Payload: { currentTime, duration }.
|
||||
TIME_UPDATE: 'timeupdate',
|
||||
@@ -81,6 +85,16 @@
|
||||
return () => this.off(event, handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether an event currently has any subscriber, so a producer can skip an expensive payload nobody reads.
|
||||
* @param {string} event - One of the {@link LuminaAudioPlayer.PlayerEvent} values.
|
||||
* @returns {boolean} True when the event has at least one handler.
|
||||
*/
|
||||
hasListeners(event) {
|
||||
const handlers = this.listeners.get(event);
|
||||
return !!handlers && handlers.size > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribes a handler to an event, removing it after the first emission.
|
||||
* @param {string} event - One of the {@link LuminaAudioPlayer.PlayerEvent} values.
|
||||
|
||||
@@ -16,16 +16,6 @@
|
||||
*/
|
||||
const MANIFEST_REQUEST_TIMEOUT_MS = 20000;
|
||||
|
||||
/**
|
||||
* Checks whether a value is a Guid, the only form the manifest route accepts for its Ids.
|
||||
* @param {*} value - The value to check.
|
||||
* @returns {boolean} True when the value is a Guid string.
|
||||
*/
|
||||
function isGuid(value) {
|
||||
return typeof value === 'string'
|
||||
&& /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the origin this module resolves relative URLs against. The page origin is used in a browser; a fixed local
|
||||
* origin keeps the route and origin checks testable in a host without a DOM.
|
||||
@@ -148,11 +138,11 @@
|
||||
if (!Object.prototype.hasOwnProperty.call(expectedIds, name))
|
||||
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The stream route template carries an unknown placeholder.');
|
||||
const expected = expectedIds[name];
|
||||
if (!isGuid(expected))
|
||||
if (!LuminaAudioPlayer.isGuid(expected))
|
||||
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.INVALID_DESCRIPTOR, `The play descriptor carries no valid ${name}.`, { field: name });
|
||||
const actual = streamSegments[index];
|
||||
// The path must describe the track of this descriptor, not merely another track of the same library.
|
||||
if (!isGuid(actual) || actual.toLowerCase() !== expected.toLowerCase())
|
||||
if (!LuminaAudioPlayer.isGuid(actual) || actual.toLowerCase() !== expected.toLowerCase())
|
||||
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The stream path does not belong to the expected track.');
|
||||
continue;
|
||||
}
|
||||
@@ -167,7 +157,7 @@
|
||||
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The stream path does not match the stream route.');
|
||||
}
|
||||
const libraryId = descriptor && descriptor.libraryId;
|
||||
if (!isGuid(libraryId))
|
||||
if (!LuminaAudioPlayer.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())
|
||||
@@ -256,10 +246,10 @@
|
||||
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))
|
||||
if (!LuminaAudioPlayer.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())
|
||||
if (!LuminaAudioPlayer.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;
|
||||
}
|
||||
@@ -267,7 +257,7 @@
|
||||
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))
|
||||
if (!LuminaAudioPlayer.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())
|
||||
@@ -325,7 +315,7 @@
|
||||
if (!ids[name])
|
||||
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.INVALID_DESCRIPTOR, `The play descriptor carries no ${name}.`, { field: name });
|
||||
// The manifest route only accepts Guids, and a value that is not a Guid must never be interpolated into a path the browser resolves against this origin.
|
||||
if (!isGuid(ids[name]))
|
||||
if (!LuminaAudioPlayer.isGuid(ids[name]))
|
||||
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.INVALID_DESCRIPTOR, `The play descriptor carries an invalid ${name}.`, { field: name });
|
||||
}
|
||||
let url = String(template)
|
||||
|
||||
+34
@@ -272,6 +272,40 @@
|
||||
await this.teardownPlaybackAsync(null, null, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops the next item and the prefetch of the current track, without touching the audio that is already playing.
|
||||
*
|
||||
* The player calls it when it replaces the queue under a playing track, for example when the user turns shuffle on or
|
||||
* off. A prefetched or appended handoff was prepared from the queue that no longer exists, so leaving it in place
|
||||
* would play a track the queue no longer describes. When the handoff is already audible it is left alone, because
|
||||
* truncating it would rewind audio that has played.
|
||||
* @returns {Promise<void>} A promise that completes when the upcoming handoff was released.
|
||||
*/
|
||||
async cancelUpcomingAsync() {
|
||||
if (this.destroyed)
|
||||
return;
|
||||
const item = this.current;
|
||||
if (!item)
|
||||
return;
|
||||
await this.cancelPrefetchAsync(item);
|
||||
item.continueToEntry = null;
|
||||
const next = this.nextItem;
|
||||
if (!next)
|
||||
return;
|
||||
// A handoff that has not reached the playhead can be removed from the schedule; one that is already audible stays.
|
||||
if (typeof this.renderer.truncateScheduleAt !== 'function'
|
||||
|| next.base <= this.renderer.getCurrentTime())
|
||||
return;
|
||||
this.nextItem = null;
|
||||
if (typeof this.renderer.cancelTrackGainChange === 'function')
|
||||
this.renderer.cancelTrackGainChange();
|
||||
this.renderer.truncateScheduleAt(next.base);
|
||||
this.renderer.markStreamComplete();
|
||||
this.disposeItemAsync(next).catch((error) => {
|
||||
console.debug('Audio player: the upcoming track of the replaced queue could not be released.', error);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts, or resumes, audible playback.
|
||||
* @returns {Promise<void>} A promise that completes when the renderer is running.
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -39,7 +39,7 @@
|
||||
* markStreamComplete. Completion is never inferred from the duration of the manifest, because that duration is a whole
|
||||
* number of seconds and a decode that is still on its way near the end would then have its last frames cut off. A drained
|
||||
* schedule that is not complete is a gap: the renderer keeps playing, reports the playhead pinned to the frontier of the
|
||||
* scheduled audio, and waits for more, which is what lets the later stages treat a gap as an underrun and recover from
|
||||
* scheduled audio, and waits for more, which is what lets the pipeline treat a gap as an underrun and recover from
|
||||
* it instead of ending the track. The end is also detected from the end of the last source, so a tab that the browser
|
||||
* hides, and whose animation frame loop it suspends, still reaches the end while the audio keeps playing. The end is
|
||||
* reported through onEnded once per configured track: a buffer scheduled after an end resumes playback, but the callback
|
||||
|
||||
@@ -10,6 +10,28 @@
|
||||
(function (LuminaAudioPlayer) {
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Checks whether a value is a Guid string, the only form the routes and the player descriptors accept for their Ids.
|
||||
* @param {*} value - The value to check.
|
||||
* @returns {boolean} True when the value is a Guid string.
|
||||
*/
|
||||
LuminaAudioPlayer.isGuid = function isGuid(value) {
|
||||
return typeof value === 'string'
|
||||
&& /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(value);
|
||||
};
|
||||
|
||||
/**
|
||||
* Coerces an Id into a non empty string, or an empty string when it carries no value.
|
||||
* @param {*} value - The Id to coerce.
|
||||
* @returns {string} The Id as a string, or an empty string.
|
||||
*/
|
||||
LuminaAudioPlayer.toIdString = function toIdString(value) {
|
||||
if (value === null || value === undefined)
|
||||
return '';
|
||||
const text = String(value).trim();
|
||||
return text === '' ? '' : text;
|
||||
};
|
||||
|
||||
/**
|
||||
* Clamps a number into an inclusive range, defaulting a non finite value to the lower bound.
|
||||
* @param {number} value - The value to clamp.
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user