mirror of
https://github.com/remix-bot/fluxer.git
synced 2026-11-24 06:01:44 -05:00
...
This commit is contained in:
1 parent
64f5782d53
commit
93f12487d3
3 files changed
+85
No files matched your search
@@ -62,6 +62,9 @@ export class FluxerAudioBridge extends EventEmitter {
|
||||
/** @type {boolean} @description Whether the current track was served via /v4/trackstream. */
|
||||
get usedTrackstream() { return this._usedTrackstream; }
|
||||
|
||||
/** Called by prefetch on success: a working trackstream closes the circuit breaker. */
|
||||
_tsOk() { tsBreakerOk(); }
|
||||
|
||||
/** @param {LavalinkManager|null} lavalinkManager - LavalinkManager for trackstream/loadstream. */
|
||||
constructor(lavalinkManager = null) {
|
||||
super();
|
||||
|
||||
@@ -17,6 +17,10 @@ import prismMedia from "prism-media";
|
||||
|
||||
const { Encoder: PrismOpusEncoder, OggDemuxer: PrismOggDemuxer } = prismMedia.opus;
|
||||
|
||||
/** Prefetched trackstream results keyed by encoded track (shared across players). */
|
||||
const _tsCache = new Map();
|
||||
const TS_CACHE_TTL_MS = 4 * 60_000;
|
||||
|
||||
/** @type {number} @description PCM sample rate expected from NodeLink loadstream (48kHz stereo s16le). */
|
||||
const PCM_RATE = 48000;
|
||||
/** @type {number} @description PCM channel count expected from NodeLink loadstream. */
|
||||
@@ -273,6 +277,62 @@ const StreamPipeline = {
|
||||
* @private
|
||||
*/
|
||||
async _getTrackstreamUrl(trackInfo) {
|
||||
const key = trackInfo.encoded;
|
||||
const hit = _tsCache.get(key);
|
||||
if (hit) {
|
||||
// One-shot: a prefetched URL is consumed by the play that needs it (a replay
|
||||
// or loop re-queries, so we never reuse an expired direct URL).
|
||||
if (hit.result && Date.now() - hit.at < TS_CACHE_TTL_MS) {
|
||||
_tsCache.delete(key);
|
||||
logger.player("[AudioBridge] trackstream: using prefetched URL (instant start)");
|
||||
return hit.result;
|
||||
}
|
||||
if (hit.promise) {
|
||||
logger.player("[AudioBridge] trackstream: joining in-flight prefetch...");
|
||||
return hit.promise;
|
||||
}
|
||||
_tsCache.delete(key);
|
||||
}
|
||||
return this._queryTrackstream(trackInfo);
|
||||
},
|
||||
|
||||
/**
|
||||
* Warm the next track: resolve its encoded payload (if missing) and its
|
||||
* trackstream URL in the background so the track change is near-instant.
|
||||
* Never throws. Doubles as a free health probe for the trackstream circuit
|
||||
* breaker (success closes it, so playback returns to the fast route).
|
||||
* @param {object} song - Queue track object (its .encoded is filled in when resolved).
|
||||
* @param {string} [guildId]
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async prefetchTrack(song, guildId) {
|
||||
try {
|
||||
if (!this._lavalink || !song || song.type === "bilibili" || song.type === "external") return;
|
||||
if (!song.encoded && song.url && String(song.url).startsWith("http")) {
|
||||
const enc = await this._resolveUrlViaLavalink(song.url, guildId);
|
||||
if (enc) song.encoded = enc;
|
||||
}
|
||||
if (!song.encoded || _tsCache.has(song.encoded)) return;
|
||||
const info = { encoded: song.encoded, guildId };
|
||||
const entry = { at: Date.now(), result: null, promise: null };
|
||||
entry.promise = this._queryTrackstream(info).then(
|
||||
(r) => { entry.result = r; entry.at = Date.now(); entry.promise = null; this._tsOk?.(); return r; },
|
||||
(e) => { _tsCache.delete(song.encoded); throw e; },
|
||||
);
|
||||
entry.promise.catch(() => {}); // prefetch failures are silent
|
||||
_tsCache.set(song.encoded, entry);
|
||||
while (_tsCache.size > 40) _tsCache.delete(_tsCache.keys().next().value);
|
||||
await entry.promise.catch(() => {});
|
||||
if (entry.result) logger.player("[AudioBridge] Prefetched next track: " + (song.title || "unknown"));
|
||||
} catch (_) { /* never block playback */ }
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {object} trackInfo
|
||||
* @returns {Promise<{url: string, format: string}>}
|
||||
* @private
|
||||
*/
|
||||
async _queryTrackstream(trackInfo) {
|
||||
const nlInfo = this._lavalink.getNodeLinkInfo?.();
|
||||
if (!nlInfo) throw new Error("NodeLink info not available");
|
||||
|
||||
|
||||
@@ -253,6 +253,26 @@ const PlaybackMixin = {
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* @private Shortly after a track starts, warm the NEXT queued track (resolve +
|
||||
* trackstream URL) so the transition does not wait on NodeLink.
|
||||
* @this {import('./Player.mjs').Player}
|
||||
*/
|
||||
_schedulePrefetchNext() {
|
||||
if (this._prefetchTimer) clearTimeout(this._prefetchTimer);
|
||||
this._prefetchTimer = setTimeout(() => {
|
||||
this._prefetchTimer = null;
|
||||
try {
|
||||
if (this._destroyed || this.leaving || !this._audioBridge?.prefetchTrack) return;
|
||||
if (this.queue.songLoop) return; // next track is the same one
|
||||
const next = this.queue.data?.[0];
|
||||
if (!next) return;
|
||||
this._audioBridge.prefetchTrack(next, this._guildId);
|
||||
} catch (_) {}
|
||||
}, 6_000);
|
||||
this._prefetchTimer.unref?.();
|
||||
},
|
||||
|
||||
/**
|
||||
* @private @async Play a track through the FluxerAudioBridge. For
|
||||
* radio/external tracks without an encoded payload, attempts Lavalink URL
|
||||
@@ -318,6 +338,8 @@ const PlaybackMixin = {
|
||||
const totalMs = this._getTrackDurationMs(songData);
|
||||
const durationMs = totalMs > 0 ? Math.max(0, totalMs - seekMs) : 0;
|
||||
|
||||
this._schedulePrefetchNext();
|
||||
|
||||
const result = await this._audioBridge.play(this._voiceConn, {
|
||||
encoded: songData.encoded,
|
||||
url: bridgeUrl,
|
||||
|
||||
Reference in new issue
Block a user