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
@@ -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");
|
||||
|
||||
|
||||
Reference in new issue
Block a user