mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-01 18:57:24 +00:00
Adds the audio player download engine with range fetching, resume and streaming progress.
This commit is contained in:
1 parent
b7ed83c82f
commit
687371bfd2
8 files changed
+1477
-43
No files matched your search
@@ -45,8 +45,10 @@
|
||||
<script defer src="~/js/dialogs.js" asp-append-version="true"></script>
|
||||
<script defer src="~/js/image-viewer.js" asp-append-version="true"></script>
|
||||
<script defer src="~/js/site.js" asp-append-version="true"></script>
|
||||
<script defer src="~/js/audio-player/audio-player-utils.js" asp-append-version="true"></script>
|
||||
<script defer src="~/js/audio-player/audio-player-errors.js" asp-append-version="true"></script>
|
||||
<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-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>
|
||||
@@ -111,8 +111,8 @@
|
||||
* Detects every capability the player needs, in parallel, and caches the report.
|
||||
*
|
||||
* The returned report has the shape:
|
||||
* <code>{ webCodecs, audioDecoder, audioContext, codecs, missing, supported }</code>, where <code>codecs</code> maps each probed codec name to a boolean, <code>missing</code> lists the
|
||||
* {@link LuminaAudioPlayer.PlayerCapability} values that are absent, and <code>supported</code> is true only when every required capability is present.
|
||||
* <code>{ hasWebCodecs, hasAudioDecoder, hasAudioContext, codecs, missing, isSupported }</code>, where <code>codecs</code> maps each probed codec name to a boolean, <code>missing</code> lists the
|
||||
* {@link LuminaAudioPlayer.PlayerCapability} values that are absent, and <code>isSupported</code> is true only when every required capability is present.
|
||||
* @returns {Promise<Object>} The capability report.
|
||||
*/
|
||||
async detectAsync() {
|
||||
@@ -136,19 +136,19 @@
|
||||
* @returns {Promise<Object>} The capability report.
|
||||
*/
|
||||
async runDetectionAsync() {
|
||||
const webCodecs = this.hasWebCodecs();
|
||||
const audioDecoder = this.hasAudioDecoder();
|
||||
const audioContext = this.hasAudioContext();
|
||||
const hasWebCodecs = this.hasWebCodecs();
|
||||
const hasAudioDecoder = this.hasAudioDecoder();
|
||||
const hasAudioContext = this.hasAudioContext();
|
||||
|
||||
const codecs = {};
|
||||
if (audioDecoder) {
|
||||
if (hasAudioDecoder) {
|
||||
// Each isConfigSupported is an independent round trip to the browser, so they are probed in parallel; the
|
||||
// results are folded in afterwards, in probe order, so the report never depends on which probe finishes first.
|
||||
const probeResults = await Promise.all(LuminaAudioPlayer.PROBED_AUDIO_CODECS.map(async (probe) => {
|
||||
return { name: probe.name, supported: await this.supportsAudioConfigAsync(probe.config) };
|
||||
return { name: probe.name, isSupported: await this.supportsAudioConfigAsync(probe.config) };
|
||||
}));
|
||||
for (const probeResult of probeResults)
|
||||
codecs[probeResult.name] = probeResult.supported;
|
||||
codecs[probeResult.name] = probeResult.isSupported;
|
||||
} else {
|
||||
// Without a decoder nothing can be probed, but every codec key is still reported as false, so that a
|
||||
// consumer reads the same shape whether the decoder exists or not, instead of testing for a missing key.
|
||||
@@ -159,21 +159,21 @@
|
||||
// Only the three hard capabilities decide whether the player can run at all; a codec the browser cannot decode is
|
||||
// not fatal here, because the server can normalize such a track, which the delivery decision handles at play time.
|
||||
const missing = [];
|
||||
if (!webCodecs)
|
||||
if (!hasWebCodecs)
|
||||
missing.push(LuminaAudioPlayer.PlayerCapability.WEB_CODECS);
|
||||
if (!audioDecoder)
|
||||
if (!hasAudioDecoder)
|
||||
missing.push(LuminaAudioPlayer.PlayerCapability.AUDIO_DECODER);
|
||||
if (!audioContext)
|
||||
if (!hasAudioContext)
|
||||
missing.push(LuminaAudioPlayer.PlayerCapability.AUDIO_CONTEXT);
|
||||
|
||||
// The report is shared and cached, so it is frozen to stop one consumer from mutating what every other consumer reads.
|
||||
return Object.freeze({
|
||||
webCodecs,
|
||||
audioDecoder,
|
||||
audioContext,
|
||||
hasWebCodecs,
|
||||
hasAudioDecoder,
|
||||
hasAudioContext,
|
||||
codecs: Object.freeze(codecs),
|
||||
missing: Object.freeze(missing),
|
||||
supported: missing.length === 0
|
||||
isSupported: missing.length === 0
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -22,11 +22,15 @@
|
||||
* The factory also receives a host object that the engine uses to talk back to the controller, so
|
||||
* that the controller stays the single source of truth:
|
||||
* - host.events: the player event bus, for {@link LuminaAudioPlayer.PlayerEvent} values the engine emits itself.
|
||||
* - host.getPreferences(): the playback preferences of the user the player currently holds, for example the
|
||||
* 'keep downloading while paused' setting an engine's downloader honors.
|
||||
* - host.reportTime(currentTime, duration): reports the playhead and the known duration.
|
||||
* - host.reportDuration(duration): reports a duration discovered after loading.
|
||||
* - host.reportProgress(downloadedFraction): reports how much of the track has been downloaded, between 0 and 1.
|
||||
* - host.reportEnded(): reports that the current track reached its end.
|
||||
* - host.reportError(error): reports an engine failure, wrapped into a {@link LuminaAudioPlayer.PlayerError}.
|
||||
* - 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.
|
||||
*
|
||||
* Registering and selecting an engine
|
||||
* -----------------------------------
|
||||
|
||||
@@ -25,6 +25,10 @@
|
||||
ENGINE_LOAD_FAILED: 'engine-load-failed',
|
||||
// The engine reported a failure while decoding, buffering or rendering audio.
|
||||
ENGINE_ERROR: 'engine-error',
|
||||
// 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.
|
||||
DOWNLOAD_ABORTED: 'download-aborted',
|
||||
// The stored playback preferences of the user could not be read.
|
||||
PREFERENCES_LOAD_FAILED: 'preferences-load-failed'
|
||||
});
|
||||
|
||||
@@ -37,7 +37,7 @@
|
||||
CAPABILITIES_CHANGED: 'capabilitieschange',
|
||||
// The stored playback preferences of the user were loaded. Payload: the preferences object.
|
||||
PREFERENCES_CHANGED: 'preferenceschange',
|
||||
// The player bar became visible or hidden. Payload: { visible }. The value is deliberately not 'visibilitychange',
|
||||
// The player bar became visible or hidden. Payload: { isVisible }. The value is deliberately not 'visibilitychange',
|
||||
// which is the name of the DOM page visibility event, so that the two can never be confused.
|
||||
VISIBILITY_CHANGED: 'barvisibilitychange'
|
||||
});
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* Shared helpers of the audio player.
|
||||
*
|
||||
* This module has no dependency on any other player module, and it is loaded before them, so it is the home of the small
|
||||
* numeric helpers several modules need. Keeping one implementation here avoids the copies that otherwise drift apart, and
|
||||
* because this file is a dedicated, dependency free util, no consumer depends on the load order of a feature module such
|
||||
* as the event bus or the download engine.
|
||||
*/
|
||||
(function (LuminaAudioPlayer) {
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Clamps a number into an inclusive range, defaulting a non finite value to the lower bound.
|
||||
* @param {number} value - The value to clamp.
|
||||
* @param {number} minimum - The inclusive lower bound.
|
||||
* @param {number} maximum - The inclusive upper bound.
|
||||
* @returns {number} The clamped value.
|
||||
*/
|
||||
LuminaAudioPlayer.clamp = function clamp(value, minimum, maximum) {
|
||||
if (!Number.isFinite(value))
|
||||
return minimum;
|
||||
return Math.min(maximum, Math.max(minimum, value));
|
||||
};
|
||||
})(window.LuminaAudioPlayer = window.LuminaAudioPlayer || {});
|
||||
@@ -156,16 +156,15 @@
|
||||
}
|
||||
|
||||
/**
|
||||
* Clamps a number into an inclusive range.
|
||||
* Clamps a number into an inclusive range, through the shared helper on the namespace. The reference is resolved on
|
||||
* every call rather than captured at load time, so this module does not depend on the load order of the util.
|
||||
* @param {number} value - The value to clamp.
|
||||
* @param {number} minimum - The inclusive lower bound.
|
||||
* @param {number} maximum - The inclusive upper bound.
|
||||
* @returns {number} The clamped value.
|
||||
*/
|
||||
function clamp(value, minimum, maximum) {
|
||||
if (!Number.isFinite(value))
|
||||
return minimum;
|
||||
return Math.min(maximum, Math.max(minimum, value));
|
||||
return LuminaAudioPlayer.clamp(value, minimum, maximum);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -317,6 +316,8 @@
|
||||
this.duration = 0;
|
||||
this.downloadedFraction = 0;
|
||||
this.volume = 1;
|
||||
// Boolean state keeps its conventional is/has prefix, except where a public is*() accessor already owns that name:
|
||||
// isMuted() is that accessor here, so the backing field keeps the participle instead of shadowing the method.
|
||||
this.muted = false;
|
||||
this.lastError = null;
|
||||
this.preferences = {
|
||||
@@ -329,14 +330,14 @@
|
||||
this.capabilityReport = null;
|
||||
this.capabilitiesReady = null;
|
||||
this.engine = null;
|
||||
this.available = false;
|
||||
this.initialized = false;
|
||||
this.isAvailable = false;
|
||||
this.isInitialized = false;
|
||||
this.container = null;
|
||||
this.main = null;
|
||||
this.dom = {};
|
||||
this.descriptorClickHandler = null;
|
||||
this.controlHandlers = [];
|
||||
this.progressRenderScheduled = false;
|
||||
this.isProgressRenderScheduled = false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -349,17 +350,17 @@
|
||||
* @returns {LuminaAudioPlayer.AudioPlayer} This player, for chaining.
|
||||
*/
|
||||
initialize() {
|
||||
if (this.initialized)
|
||||
if (this.isInitialized)
|
||||
return this;
|
||||
this.initialized = true;
|
||||
this.isInitialized = true;
|
||||
|
||||
this.container = this.options.container || document.querySelector('.audio-player-container');
|
||||
this.main = this.options.main || document.querySelector('main');
|
||||
if (!this.container) {
|
||||
this.available = false;
|
||||
this.isAvailable = false;
|
||||
return this;
|
||||
}
|
||||
this.available = true;
|
||||
this.isAvailable = true;
|
||||
|
||||
this.collectDomReferences();
|
||||
this.bindPlaybackControls();
|
||||
@@ -421,6 +422,9 @@
|
||||
* into a play request, so that pages never need a direct reference to the player.
|
||||
*/
|
||||
bindDescriptorDelegation() {
|
||||
// One listener on the document rather than one per affordance, because the pages and their theme templates are
|
||||
// swapped in place by the navigation manager and never re-bound, and because a trigger can be rendered at any
|
||||
// time without the player being told about it.
|
||||
this.descriptorClickHandler = (event) => {
|
||||
if (!(event.target instanceof Element))
|
||||
return;
|
||||
@@ -441,7 +445,7 @@
|
||||
*/
|
||||
async playAsync(descriptorInput) {
|
||||
// The player is only available when the layout rendered the bar, so a caller on a page without one is ignored instead of failing.
|
||||
if (!this.available)
|
||||
if (!this.isAvailable)
|
||||
return false;
|
||||
const descriptor = createPlayDescriptor(descriptorInput);
|
||||
if (!descriptor) {
|
||||
@@ -451,7 +455,7 @@
|
||||
// Capability detection runs in the background while the page loads, so the first play waits for its answer instead
|
||||
// of starting a track the browser may not be able to decode.
|
||||
const report = await this.capabilitiesReady;
|
||||
if (report && !report.supported) {
|
||||
if (report && !report.isSupported) {
|
||||
this.raiseError(new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.CAPABILITY_MISSING, 'The browser is missing a capability the player requires.',
|
||||
{ missing: report.missing }));
|
||||
return false;
|
||||
@@ -628,10 +632,10 @@
|
||||
toggleFullHeight() {
|
||||
if (!this.container)
|
||||
return false;
|
||||
const expanded = this.container.classList.toggle('full-height');
|
||||
const isExpanded = this.container.classList.toggle('full-height');
|
||||
if (this.main)
|
||||
this.main.classList.toggle('audio-player-full-height', expanded);
|
||||
return expanded;
|
||||
this.main.classList.toggle('audio-player-full-height', isExpanded);
|
||||
return isExpanded;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -661,7 +665,7 @@
|
||||
if (this.main)
|
||||
this.main.classList.remove('audio-player-not-shown');
|
||||
if (wasHidden)
|
||||
this.events.emit(LuminaAudioPlayer.PlayerEvent.VISIBILITY_CHANGED, { visible: true });
|
||||
this.events.emit(LuminaAudioPlayer.PlayerEvent.VISIBILITY_CHANGED, { isVisible: true });
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -676,7 +680,7 @@
|
||||
this.container.classList.add('not-shown');
|
||||
if (this.main)
|
||||
this.main.classList.add('audio-player-not-shown');
|
||||
this.events.emit(LuminaAudioPlayer.PlayerEvent.VISIBILITY_CHANGED, { visible: false });
|
||||
this.events.emit(LuminaAudioPlayer.PlayerEvent.VISIBILITY_CHANGED, { isVisible: false });
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -748,7 +752,8 @@
|
||||
* @returns {Object} The preferences.
|
||||
*/
|
||||
getPreferences() {
|
||||
return this.preferences;
|
||||
// A shallow frozen copy, so a consumer cannot mutate the preferences the player uses.
|
||||
return Object.freeze({ ...this.preferences });
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -944,8 +949,12 @@
|
||||
* @returns {Object} The engine host.
|
||||
*/
|
||||
createEngineHost() {
|
||||
// The host is the only channel an engine uses to talk back, so the player stays the single source of truth and
|
||||
// an engine never reaches into the controller state; a report is routed to the matching handler here.
|
||||
return {
|
||||
events: this.events,
|
||||
// A shallow frozen copy, so an engine or a test cannot mutate the controller state through the host.
|
||||
getPreferences: () => Object.freeze({ ...this.preferences }),
|
||||
reportTime: (currentTime, duration) => this.handleEngineTime(currentTime, duration),
|
||||
reportDuration: (duration) => this.handleEngineDuration(duration),
|
||||
reportProgress: (fraction) => this.handleEngineProgress(fraction),
|
||||
@@ -974,6 +983,8 @@
|
||||
* @param {number} duration - The known duration, in seconds.
|
||||
*/
|
||||
handleEngineDuration(duration) {
|
||||
// An invalid, non positive or unchanged value is ignored: an engine may report its duration repeatedly, and
|
||||
// re-rendering or re-emitting for a value that did not move would churn the bar and its subscribers for nothing.
|
||||
const value = Number(duration);
|
||||
if (!Number.isFinite(value) || value <= 0 || value === this.duration)
|
||||
return;
|
||||
@@ -999,11 +1010,11 @@
|
||||
* high frequency stream of engine reports does not turn into a high frequency stream of DOM writes.
|
||||
*/
|
||||
scheduleProgressRender() {
|
||||
if (this.progressRenderScheduled)
|
||||
if (this.isProgressRenderScheduled)
|
||||
return;
|
||||
this.progressRenderScheduled = true;
|
||||
this.isProgressRenderScheduled = true;
|
||||
const render = () => {
|
||||
this.progressRenderScheduled = false;
|
||||
this.isProgressRenderScheduled = false;
|
||||
this.renderProgress();
|
||||
this.events.emit(LuminaAudioPlayer.PlayerEvent.TIME_UPDATE, { currentTime: this.currentTime, duration: this.duration });
|
||||
};
|
||||
@@ -1030,7 +1041,7 @@
|
||||
const report = await this.capabilities.detectAsync();
|
||||
this.capabilityReport = report;
|
||||
this.events.emit(LuminaAudioPlayer.PlayerEvent.CAPABILITIES_CHANGED, report);
|
||||
if (!report.supported) {
|
||||
if (!report.isSupported) {
|
||||
this.raiseError(new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.CAPABILITY_MISSING, 'The browser is missing a capability the player requires.',
|
||||
{ missing: report.missing }));
|
||||
}
|
||||
@@ -1062,7 +1073,8 @@
|
||||
replayGainMode: settings.replayGainMode || 'Off',
|
||||
streamingQuality: settings.streamingQuality || 'Auto'
|
||||
};
|
||||
this.events.emit(LuminaAudioPlayer.PlayerEvent.PREFERENCES_CHANGED, this.preferences);
|
||||
// The payload is a frozen copy, so a subscriber that mutates it cannot change the preferences the player uses.
|
||||
this.events.emit(LuminaAudioPlayer.PlayerEvent.PREFERENCES_CHANGED, Object.freeze({ ...this.preferences }));
|
||||
}
|
||||
if (playbackState) {
|
||||
if (Number.isFinite(Number(playbackState.volume)))
|
||||
@@ -1077,6 +1089,9 @@
|
||||
* @returns {Object} The endpoints configuration.
|
||||
*/
|
||||
getConfiguration() {
|
||||
// Options win, so a composition or a test can inject endpoints; otherwise the global object the layout emits is
|
||||
// used, and an empty object is returned when neither exists, so a read of a missing endpoint is a no-op rather
|
||||
// than a crash.
|
||||
return this.options.configuration || window.audioPlayerConfiguration || {};
|
||||
}
|
||||
|
||||
@@ -1135,6 +1150,8 @@
|
||||
* @returns {number} The effective volume, between 0 and 1.
|
||||
*/
|
||||
getEffectiveVolume() {
|
||||
// Mute is a flag of its own, so the volume is kept untouched and only the value sent to the output is zeroed;
|
||||
// that is what lets unmuting restore the volume the user had set rather than a default.
|
||||
return this.muted ? 0 : this.volume;
|
||||
}
|
||||
|
||||
@@ -1151,6 +1168,8 @@
|
||||
* @param {MouseEvent} event - The click event on the progress bar.
|
||||
*/
|
||||
handleProgressClick(event) {
|
||||
// Without a track, without a known duration, without the bar, or with a zero width bar there is no position to
|
||||
// map the click to, and dividing by the width would produce a NaN seek, so the click is ignored instead.
|
||||
if (!this.descriptor || this.duration <= 0 || !this.dom.progress)
|
||||
return;
|
||||
const bounds = this.dom.progress.getBoundingClientRect();
|
||||
@@ -1227,6 +1246,8 @@
|
||||
* Renders the three state progress bar: the played part, the downloaded part, and the remainder.
|
||||
*/
|
||||
renderProgress() {
|
||||
// Only widths are written here, as percentages; the colors, shape and layering of the three states belong to the
|
||||
// theme's CSS, which selects them through the classes on the bar elements, so the player never hardcodes a look.
|
||||
if (this.dom.currentTime)
|
||||
this.dom.currentTime.textContent = formatTime(this.currentTime);
|
||||
if (this.dom.totalTime)
|
||||
@@ -1243,9 +1264,9 @@
|
||||
* Renders the play or pause affordance of the transport control.
|
||||
*/
|
||||
renderTransportState() {
|
||||
const playing = this.state === LuminaAudioPlayer.PlayerState.PLAYING;
|
||||
const isPlaying = this.state === LuminaAudioPlayer.PlayerState.PLAYING;
|
||||
if (this.dom.playPause)
|
||||
this.dom.playPause.classList.toggle('is-playing', playing);
|
||||
this.dom.playPause.classList.toggle('is-playing', isPlaying);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user