mirror of
https://github.com/remix-bot/fluxer.git
synced 2026-11-24 06:01:44 -05:00
feat(player): add %previous, %replay, livekit skip guard, and nodelink auto-reconnect (#11, #13, #18, #19, #23)
This commit is contained in:
1 parent
53d0990ebe
commit
e33fbb2fdb
14 files changed
+348
-31
No files matched your search
@@ -73,14 +73,17 @@ export class LavalinkManager extends EventEmitter {
|
||||
nodeMgr.on("connect", (node) => {
|
||||
this._nodeConnected = true;
|
||||
logger.lavalink("[Lavalink] Node connected (nodeManager): " + node.id);
|
||||
this.emit("nodeConnect", node);
|
||||
});
|
||||
nodeMgr.on("disconnect", (node, reason) => {
|
||||
this._nodeConnected = false;
|
||||
logger.warn("[Lavalink] Node disconnected (nodeManager):", node?.id, reason);
|
||||
this.emit("nodeDisconnect", node, reason);
|
||||
});
|
||||
nodeMgr.on("reconnect", (node) => {
|
||||
this._nodeConnected = true;
|
||||
logger.lavalink("[Lavalink] Node reconnecting (nodeManager): " + node.id);
|
||||
this.emit("nodeReconnect", node);
|
||||
});
|
||||
} catch (e) {
|
||||
logger.warn("[Lavalink] nodeManager event setup error:", e?.message);
|
||||
@@ -92,16 +95,19 @@ export class LavalinkManager extends EventEmitter {
|
||||
this.lavalink.on("nodeConnect", (node) => {
|
||||
this._nodeConnected = true;
|
||||
logger.lavalink("[Lavalink] Node connected (manager): " + node.id);
|
||||
this.emit("nodeConnect", node);
|
||||
});
|
||||
|
||||
this.lavalink.on("nodeReconnect", (node) => {
|
||||
this._nodeConnected = true;
|
||||
logger.lavalink("[Lavalink] Node reconnecting (manager): " + node.id);
|
||||
this.emit("nodeReconnect", node);
|
||||
});
|
||||
|
||||
this.lavalink.on("nodeDisconnect", (node, reason) => {
|
||||
this._nodeConnected = false;
|
||||
logger.warn("[Lavalink] Node disconnected (manager):", node.id, reason);
|
||||
this.emit("nodeDisconnect", node, reason);
|
||||
});
|
||||
|
||||
this.lavalink.on("nodeError", (node, err) => {
|
||||
|
||||
@@ -330,6 +330,11 @@ export class FluxerAudioBridge extends EventEmitter {
|
||||
streamToPlay = remuxed;
|
||||
}
|
||||
this._stream = streamToPlay;
|
||||
await this._awaitPendingUnpublishes(conn);
|
||||
if (generation !== this._playGeneration) {
|
||||
streamToPlay?.destroy?.();
|
||||
return "stopped";
|
||||
}
|
||||
await conn.play(streamToPlay);
|
||||
} catch (e) {
|
||||
if (generation !== this._playGeneration) return "stopped";
|
||||
@@ -457,7 +462,9 @@ export class FluxerAudioBridge extends EventEmitter {
|
||||
}
|
||||
return await this._playOpened(conn, opened, durationMs, generation);
|
||||
}
|
||||
// Zero-copy: the voice connection fetches the URL itself.
|
||||
/* Zero-copy: ensure prior unpublish settled before publishing */
|
||||
await this._awaitPendingUnpublishes(conn);
|
||||
if (stale()) return "stopped";
|
||||
await conn.play(direct.url);
|
||||
} catch (e) {
|
||||
if (stale()) return "stopped";
|
||||
@@ -508,14 +515,12 @@ export class FluxerAudioBridge extends EventEmitter {
|
||||
try {
|
||||
const room = conn?.room;
|
||||
const participant = room?.localParticipant;
|
||||
if (!room?.isConnected || typeof participant?.unpublishTrack !== "function") return;
|
||||
if (!room?.isConnected || !participant || typeof participant.unpublishTrack !== "function") return;
|
||||
|
||||
const publications = participant.trackPublications;
|
||||
if (!publications || typeof publications.entries !== "function") return;
|
||||
|
||||
// One guard map per participant, shared by every bridge instance in this
|
||||
// process: a SID currently being unpublished is skipped by later sweeps,
|
||||
// and its promise is awaitable via _awaitPendingUnpublishes().
|
||||
/* One guard map per participant, shared by every bridge instance in this process */
|
||||
let pending = pendingUnpublishSids.get(participant);
|
||||
if (!pending) {
|
||||
pending = new Map();
|
||||
@@ -528,11 +533,20 @@ export class FluxerAudioBridge extends EventEmitter {
|
||||
if (pending.has(sid)) continue;
|
||||
try {
|
||||
const settle = Promise.resolve(participant.unpublishTrack(sid, true))
|
||||
.catch(() => {}) // failed unpublish is still a settled state — don't block on it
|
||||
.catch((err) => {
|
||||
/* Suppress benign race-condition errors during rapid track skips */
|
||||
if (!err || /Publication not found|AudioSource is closed|Participant not found|Track not found/i.test(err?.message || '')) {
|
||||
return;
|
||||
}
|
||||
logger.warn?.("[AudioBridge] unpublishTrack warning: " + err.message);
|
||||
})
|
||||
.finally(() => { if (pending.get(sid) === settle) pending.delete(sid); });
|
||||
pending.set(sid, settle);
|
||||
logger.player("[AudioBridge] Unpublished stale audio track: " + sid);
|
||||
} catch (_) {
|
||||
} catch (err) {
|
||||
if (!err || /Publication not found|AudioSource is closed|Participant not found|Track not found/i.test(err?.message || '')) {
|
||||
/* Benign race condition */
|
||||
}
|
||||
pending.delete(sid);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,6 +110,28 @@ const PlaybackMixin = {
|
||||
finally { if (generation === this._playNextGeneration) this._playingNext = false; }
|
||||
},
|
||||
|
||||
/**
|
||||
* @async Step backward to the previous track in history.
|
||||
* Restores the current track to the top of the queue and plays the previous track.
|
||||
* @this {import('./Player.mjs').Player}
|
||||
* @returns {Promise<object|null>} The previous track or null if history is empty.
|
||||
*/
|
||||
async playPrevious() {
|
||||
if (!this.queue || this.queue.history.length === 0) return null;
|
||||
this._skipping = true;
|
||||
this._bridgeStop();
|
||||
const prevSong = this.queue.previous();
|
||||
if (!prevSong) {
|
||||
this._skipping = false;
|
||||
return null;
|
||||
}
|
||||
this._playingNext = false;
|
||||
this._radioAnnounced = false;
|
||||
await this.playNext({ reuseCurrent: true });
|
||||
this._skipping = false;
|
||||
return prevSong;
|
||||
},
|
||||
|
||||
/**
|
||||
* @private @async Core logic for advancing to the next track: resolves
|
||||
* track options, announces, starts playback via the bridge, arms the
|
||||
@@ -121,8 +143,8 @@ const PlaybackMixin = {
|
||||
this._bridgeStop();
|
||||
|
||||
const currentBeforeNext = this.queue.getCurrent();
|
||||
if (currentBeforeNext) this._lastPlayedTrack = currentBeforeNext;
|
||||
const songData = this.queue.next(opts);
|
||||
if (currentBeforeNext && !opts.reuseCurrent) this._lastPlayedTrack = currentBeforeNext;
|
||||
const songData = opts.reuseCurrent ? this.queue.getCurrent() : this.queue.next(opts);
|
||||
if (!songData) {
|
||||
this.emit("stopplay");
|
||||
this.emit("queueEnd");
|
||||
|
||||
@@ -827,9 +827,9 @@ class Player extends EventEmitter {
|
||||
async resumePlaybackAfterNodeReconnect() {
|
||||
if (this._destroyed || this.leaving || !this._voiceConn) return false;
|
||||
const current = this.queue?.getCurrent();
|
||||
if (!current || !this._audioBridge?.playing) return false;
|
||||
if (!current || this._paused) return false;
|
||||
|
||||
const posMs = this.computePosition();
|
||||
const posMs = Math.max(0, this.computePosition());
|
||||
logger.player(`[Player:${this._guildId}] Node reconnected — resuming "${current.title}" at ${Math.round(posMs / 1000)}s`);
|
||||
|
||||
try {
|
||||
@@ -1061,8 +1061,8 @@ class Player extends EventEmitter {
|
||||
|
||||
/** @private Stop the audio bridge if currently playing. */
|
||||
_bridgeStop() {
|
||||
if (this._audioBridge?.playing) {
|
||||
this._audioBridge.stop();
|
||||
if (this._audioBridge) {
|
||||
try { this._audioBridge.stop(); } catch (_) {}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,10 @@ export class Queue extends EventEmitter {
|
||||
loop = false;
|
||||
/** @type {boolean} Whether single-song loop is enabled. */
|
||||
songLoop = false;
|
||||
/** @type {Array<object>} Circular buffer of recently played tracks. */
|
||||
history = [];
|
||||
/** @type {number} Maximum number of tracks preserved in playback history. */
|
||||
static MAX_HISTORY = 20;
|
||||
|
||||
/** Initialize an empty queue with no loop enabled. */
|
||||
constructor() {
|
||||
@@ -50,6 +54,13 @@ export class Queue extends EventEmitter {
|
||||
if (!ignoreSongLoop && this.songLoop && this.current) return this.current;
|
||||
if (this.loop && this.current) this.data.push(this.current);
|
||||
|
||||
if (previous && (!this.songLoop || ignoreSongLoop)) {
|
||||
this.history.push(previous);
|
||||
if (this.history.length > Queue.MAX_HISTORY) {
|
||||
this.history.shift();
|
||||
}
|
||||
}
|
||||
|
||||
if (this.isEmpty()) {
|
||||
this.current = null;
|
||||
return null;
|
||||
@@ -143,10 +154,33 @@ export class Queue extends EventEmitter {
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Step backward to the previous track from history.
|
||||
* Restores the current track to the front of the queue so it is not lost.
|
||||
* @returns {object|null} The restored previous track, or null if history is empty.
|
||||
*/
|
||||
previous() {
|
||||
if (this.history.length === 0) return null;
|
||||
const prevTrack = this.history.pop();
|
||||
if (this.current) {
|
||||
this.data.unshift(this.current);
|
||||
}
|
||||
const old = this.current;
|
||||
this.current = prevTrack;
|
||||
this.emit("queue", {
|
||||
type: "update",
|
||||
data: { current: this.current, old, restored: true, loop: this.loop }
|
||||
});
|
||||
return this.current;
|
||||
}
|
||||
|
||||
/** @returns {Array<object>} Copy of recent playback history. */
|
||||
getHistory() { return this.history.slice(); }
|
||||
|
||||
/** Remove all tracks from the queue. */
|
||||
clear() { this.data.length = 0; }
|
||||
/** Clear the queue and reset all state (current, loops). */
|
||||
reset() { this.clear(); this.current = null; this.songLoop = false; this.loop = false; }
|
||||
/** Clear the queue and reset all state (current, loops, history). */
|
||||
reset() { this.clear(); this.current = null; this.songLoop = false; this.loop = false; this.history.length = 0; }
|
||||
|
||||
/** @param {boolean} bool - Enable or disable song loop. */
|
||||
setSongLoop(bool) { this.songLoop = bool; }
|
||||
|
||||
Reference in new issue
Block a user