Enhance audio processing: add event emitter max listeners, improve trackstream handling, and optimize loadstream probing

This commit is contained in:
NoLogicAlan committed 2026-10-14 02:30:32 +03:00
1 parent 93f12487d3
commit 15a84cce69
3 files changed
+21 -3

No files matched your search

+8 -2
View File
@@ -64,6 +64,7 @@ export class FluxerAudioBridge extends EventEmitter {
/** Called by prefetch on success: a working trackstream closes the circuit breaker. */
_tsOk() { tsBreakerOk(); }
_tsBreakerOpen() { return tsBreakerOpen(); }
/** @param {LavalinkManager|null} lavalinkManager - LavalinkManager for trackstream/loadstream. */
constructor(lavalinkManager = null) {
@@ -431,8 +432,13 @@ export class FluxerAudioBridge extends EventEmitter {
}
if (!/webm|opus/i.test(String(direct.format || ""))) {
logger.player("[AudioBridge] trackstream format not opus/webm (" + (direct.format || "?") + "), refreshing encoded track once...");
const freshEncoded = trackInfo.url && trackInfo.url.startsWith("http")
// A fresh resolve only helps when YouTube handed us a non-WebM itag (googlevideo).
// Mirror sources (JioSaavn/Deezer/SoundCloud... AAC/MP3/MP4) return the same thing
// again, so skip the ~7-13s loadtracks round-trip for them.
const refreshable = /googlevideo\.com|youtube\.com|ytimg/i.test(String(direct.url || ""));
logger.player("[AudioBridge] trackstream format not opus/webm (" + (direct.format || "?") + ")" +
(refreshable ? ", refreshing encoded track once..." : " from a non-YouTube source, skipping refresh."));
const freshEncoded = refreshable && trackInfo.url && trackInfo.url.startsWith("http")
? await this._resolveUrlViaLavalink(trackInfo.url, trackInfo.guildId)
: null;
if (stale()) return "stopped";
+10 -1
View File
@@ -10,6 +10,7 @@
* FluxerAudioBridge instance.
*/
import { isLoadstreamEnabled } from "./AudioSettings.mjs";
import { logger } from "../../core/Logger.mjs";
import { WebMOpusMuxer, OPUS_FRAME_MS } from "./WebMOpusMuxer.mjs";
import { StreamPositioner } from "./StreamPositioner.mjs";
@@ -20,6 +21,7 @@ const { Encoder: PrismOpusEncoder, OggDemuxer: PrismOggDemuxer } = prismMedia.op
/** Prefetched trackstream results keyed by encoded track (shared across players). */
const _tsCache = new Map();
const TS_CACHE_TTL_MS = 4 * 60_000;
let _lastProbeAt = 0;
/** @type {number} @description PCM sample rate expected from NodeLink loadstream (48kHz stereo s16le). */
const PCM_RATE = 48000;
@@ -313,6 +315,11 @@ const StreamPipeline = {
if (enc) song.encoded = enc;
}
if (!song.encoded || _tsCache.has(song.encoded)) return;
// While trackstream is known-bad, probe at most once a minute (each probe is a hung request on the node).
if (this._tsBreakerOpen?.()) {
if (Date.now() - _lastProbeAt < 60_000) return;
_lastProbeAt = Date.now();
}
const info = { encoded: song.encoded, guildId };
const entry = { at: Date.now(), result: null, promise: null };
entry.promise = this._queryTrackstream(info).then(
@@ -348,7 +355,9 @@ const StreamPipeline = {
logger.player("[AudioBridge] Querying /v4/trackstream...");
const body = await this._httpGetJson(url, headers);
// A healthy trackstream answers in a few seconds. When loadstream can take over,
// fail fast (8s) instead of making the listener wait the full 15s.
const body = await this._httpGetJson(url, headers, 0, isLoadstreamEnabled() ? 8_000 : 15_000);
const directUrl = body?.url;
if (!directUrl || typeof directUrl !== "string") {