mirror of
https://github.com/remix-bot/fluxer.git
synced 2026-11-24 06:01:44 -05:00
Compare commits
2
Commits
ada28d465b
...
87aba7434e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
87aba7434e | ||
|
|
5b19ee2a72 |
No files matched your search
@@ -11,9 +11,12 @@ import HttpStreams from "./HttpStreams.mjs";
|
|||||||
/** @type {number} @description livekit TrackKind.KIND_AUDIO — publications with this kind are managed by the bridge. */
|
/** @type {number} @description livekit TrackKind.KIND_AUDIO — publications with this kind are managed by the bridge. */
|
||||||
const TRACK_KIND_AUDIO = 1;
|
const TRACK_KIND_AUDIO = 1;
|
||||||
|
|
||||||
/** @type {WeakMap<object, Set<string>>} @description Audio SIDs already handed to LiveKit for unpublish, keyed per local participant. LiveKit's unpublishTrack() is async — the publication stays listed in trackPublications until the RTC layer completes it, so repeated sweeps (stop() -> play() -> stop()) re-unpublish the same SID, producing duplicate "Unpublished stale audio track" logs and LiveKit "RoomEvent.LocalTrackSubscribed: Publication not found" warnings. The WeakMap GCs itself when the participant is destroyed. */
|
/** @type {WeakMap<object, Map<string, Promise<void>>>} @description Audio SIDs currently being unpublished from LiveKit, keyed per local participant, mapped to a promise that resolves once that specific unpublish settles. LiveKit's unpublishTrack() is async — the publication stays listed in trackPublications until the RTC layer completes it, so repeated sweeps (stop() -> play() -> stop()) would otherwise re-unpublish the same SID, producing duplicate "Unpublished stale audio track" logs and LiveKit "RoomEvent.LocalTrackSubscribed: Publication not found" warnings. Beyond dedup, play() now also awaits these promises before publishing a new track (see _awaitPendingUnpublishes): without that wait, stop()'s fire-and-forget unpublish of the previous track could still be in flight when the next track's publish lands, briefly overlapping two audio publications on the same participant — heard as static/garbled audio until the stale one finished tearing down (fixed by a manual pause/resume, which forces a clean renegotiation). Entries remove themselves once their unpublish settles, so the map cannot grow unbounded across a 24/7 session. The WeakMap GCs itself when the participant is destroyed. */
|
||||||
const pendingUnpublishSids = new WeakMap();
|
const pendingUnpublishSids = new WeakMap();
|
||||||
|
|
||||||
|
/** @type {number} @description Max time to wait for a stale-track unpublish to settle before publishing anyway (defensive — an unpublish should resolve almost instantly; this only guards against a hung/never-resolving LiveKit promise blocking playback indefinitely). */
|
||||||
|
const UNPUBLISH_WAIT_TIMEOUT_MS = 1200;
|
||||||
|
|
||||||
/** @extends {EventEmitter} */
|
/** @extends {EventEmitter} */
|
||||||
export class FluxerAudioBridge extends EventEmitter {
|
export class FluxerAudioBridge extends EventEmitter {
|
||||||
_conn = null;
|
_conn = null;
|
||||||
@@ -88,6 +91,9 @@ export class FluxerAudioBridge extends EventEmitter {
|
|||||||
this._currentUri = trackInfo?.url ?? trackInfo?.title ?? "unknown";
|
this._currentUri = trackInfo?.url ?? trackInfo?.title ?? "unknown";
|
||||||
this._usedTrackstream = false;
|
this._usedTrackstream = false;
|
||||||
|
|
||||||
|
await this._awaitPendingUnpublishes(conn);
|
||||||
|
if (generation !== this._playGeneration) return "stopped";
|
||||||
|
|
||||||
const seekMs = Math.floor((options.seekSeconds ?? 0) * 1000);
|
const seekMs = Math.floor((options.seekSeconds ?? 0) * 1000);
|
||||||
let durationMs = options.durationMs || 0;
|
let durationMs = options.durationMs || 0;
|
||||||
const filterPayload = options.filterPayload ?? null;
|
const filterPayload = options.filterPayload ?? null;
|
||||||
@@ -501,41 +507,66 @@ export class FluxerAudioBridge extends EventEmitter {
|
|||||||
const publications = participant.trackPublications;
|
const publications = participant.trackPublications;
|
||||||
if (!publications || typeof publications.entries !== "function") return;
|
if (!publications || typeof publications.entries !== "function") return;
|
||||||
|
|
||||||
// One guard set per participant, shared by every bridge instance in this
|
// One guard map per participant, shared by every bridge instance in this
|
||||||
// process: a SID currently being unpublished is skipped by later sweeps.
|
// process: a SID currently being unpublished is skipped by later sweeps,
|
||||||
let seen = pendingUnpublishSids.get(participant);
|
// and its promise is awaitable via _awaitPendingUnpublishes().
|
||||||
if (!seen) {
|
let pending = pendingUnpublishSids.get(participant);
|
||||||
seen = new Set();
|
if (!pending) {
|
||||||
pendingUnpublishSids.set(participant, seen);
|
pending = new Map();
|
||||||
|
pendingUnpublishSids.set(participant, pending);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prune SIDs whose publications are gone (unpublish completed) so the
|
|
||||||
// guard set cannot grow unbounded across a 24/7 session.
|
|
||||||
try {
|
|
||||||
if (typeof publications.has === "function") {
|
|
||||||
for (const sid of seen) {
|
|
||||||
if (!publications.has(sid)) seen.delete(sid);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (_) {}
|
|
||||||
|
|
||||||
for (const [sid, pub] of publications.entries()) {
|
for (const [sid, pub] of publications.entries()) {
|
||||||
if (keepSid && sid === keepSid) continue;
|
if (keepSid && sid === keepSid) continue;
|
||||||
if (pub?.kind !== TRACK_KIND_AUDIO) continue;
|
if (pub?.kind !== TRACK_KIND_AUDIO) continue;
|
||||||
if (seen.has(sid)) continue;
|
if (pending.has(sid)) continue;
|
||||||
try {
|
try {
|
||||||
seen.add(sid);
|
const settle = Promise.resolve(participant.unpublishTrack(sid, true))
|
||||||
const p = participant.unpublishTrack(sid, true);
|
.catch(() => {}) // failed unpublish is still a settled state — don't block on it
|
||||||
if (p?.catch) p.catch(() => seen.delete(sid)); // failed unpublish -> allow retry
|
.finally(() => { if (pending.get(sid) === settle) pending.delete(sid); });
|
||||||
|
pending.set(sid, settle);
|
||||||
logger.player("[AudioBridge] Unpublished stale audio track: " + sid);
|
logger.player("[AudioBridge] Unpublished stale audio track: " + sid);
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
seen.delete(sid);
|
pending.delete(sid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wait for any in-flight unpublishes on `conn`'s participant to settle
|
||||||
|
* (bounded by UNPUBLISH_WAIT_TIMEOUT_MS) before the caller publishes a new
|
||||||
|
* track. Without this, a fire-and-forget unpublish from the previous
|
||||||
|
* stop() can still be tearing down when the next track's publish lands,
|
||||||
|
* briefly overlapping two audio publications — heard as static/garbled
|
||||||
|
* audio at the start of the next track.
|
||||||
|
* @param {object} conn - Fluxer voice connection
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
async _awaitPendingUnpublishes(conn) {
|
||||||
|
try {
|
||||||
|
const participant = conn?.room?.localParticipant;
|
||||||
|
if (!participant) return;
|
||||||
|
const pending = pendingUnpublishSids.get(participant);
|
||||||
|
if (!pending || pending.size === 0) return;
|
||||||
|
|
||||||
|
const waits = [...pending.values()];
|
||||||
|
let timedOut = false;
|
||||||
|
const timeout = new Promise((resolve) => {
|
||||||
|
setTimeout(() => { timedOut = true; resolve(); }, UNPUBLISH_WAIT_TIMEOUT_MS).unref?.();
|
||||||
|
});
|
||||||
|
await Promise.race([Promise.allSettled(waits), timeout]);
|
||||||
|
if (timedOut) {
|
||||||
|
logger.warn(
|
||||||
|
`[AudioBridge] Timed out waiting for ${waits.length} stale unpublish(es) to settle after ${UNPUBLISH_WAIT_TIMEOUT_MS}ms — publishing new track anyway.`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (_) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the playback volume on the voice connection.
|
* Set the playback volume on the voice connection.
|
||||||
* @param {number} percent - Volume 0–100.
|
* @param {number} percent - Volume 0–100.
|
||||||
|
|||||||
@@ -63,6 +63,19 @@ export class VoiceStateCache {
|
|||||||
const maxEntries = isBot ? this._maxBots : this._maxUsers;
|
const maxEntries = isBot ? this._maxBots : this._maxUsers;
|
||||||
const uKey = VoiceStateCache.userKey(cleanGuild, cleanUser);
|
const uKey = VoiceStateCache.userKey(cleanGuild, cleanUser);
|
||||||
|
|
||||||
|
const otherLocations = isBot ? this.userLocations : this.botLocations;
|
||||||
|
const otherChannelIdx = isBot ? this.channelMembers : this.botChannelMembers;
|
||||||
|
const otherPrev = otherLocations.get(uKey);
|
||||||
|
if (otherPrev) {
|
||||||
|
const otherPrevCKey = VoiceStateCache.channelKey(otherPrev.guildId, otherPrev.channelId);
|
||||||
|
const otherPrevSet = otherChannelIdx.get(otherPrevCKey);
|
||||||
|
if (otherPrevSet) {
|
||||||
|
otherPrevSet.delete(cleanUser);
|
||||||
|
if (otherPrevSet.size === 0) otherChannelIdx.delete(otherPrevCKey);
|
||||||
|
}
|
||||||
|
otherLocations.delete(uKey);
|
||||||
|
}
|
||||||
|
|
||||||
const prev = locations.get(uKey);
|
const prev = locations.get(uKey);
|
||||||
if (prev) {
|
if (prev) {
|
||||||
const prevCKey = VoiceStateCache.channelKey(prev.guildId, prev.channelId);
|
const prevCKey = VoiceStateCache.channelKey(prev.guildId, prev.channelId);
|
||||||
|
|||||||
@@ -168,6 +168,34 @@ export function hasHumansInChannel({ guildId, channelId, client, voiceCache, obs
|
|||||||
export function countHumansInChannel({ guildId, channelId, client, voiceCache, observedVoiceUsers, botId }) {
|
export function countHumansInChannel({ guildId, channelId, client, voiceCache, observedVoiceUsers, botId }) {
|
||||||
if (!channelId || !guildId) return 0;
|
if (!channelId || !guildId) return 0;
|
||||||
|
|
||||||
|
if (voiceCache && typeof voiceCache.getHumansInChannel === "function") {
|
||||||
|
const cachedHumans = voiceCache.getHumansInChannel(guildId, channelId);
|
||||||
|
|
||||||
|
if (client) {
|
||||||
|
let guild = null;
|
||||||
|
try { guild = client.guilds?.get?.(guildId); } catch (_) { guild = null; }
|
||||||
|
|
||||||
|
let humanCount = 0;
|
||||||
|
for (const userId of cachedHumans) {
|
||||||
|
const isBot = resolveIsBotUser({
|
||||||
|
userId,
|
||||||
|
member: guild?.members?.get?.(userId) ?? null,
|
||||||
|
guild,
|
||||||
|
client,
|
||||||
|
botId,
|
||||||
|
});
|
||||||
|
if (isBot) {
|
||||||
|
try { voiceCache.updateUser({ guildId, userId, channelId, isBot: true }); } catch (_) {}
|
||||||
|
} else {
|
||||||
|
humanCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return humanCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
return cachedHumans.length;
|
||||||
|
}
|
||||||
|
|
||||||
if (voiceCache && typeof voiceCache.getHumanCount === "function") {
|
if (voiceCache && typeof voiceCache.getHumanCount === "function") {
|
||||||
return voiceCache.getHumanCount(guildId, channelId);
|
return voiceCache.getHumanCount(guildId, channelId);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user