diff --git a/service/spacebar/build/supervisor.mjs b/service/spacebar/build/supervisor.mjs index af55b6cb8f8a9c9442df50c5bdb13c3e144c6ecd..91ea037116c6b9b8f070dad99a93043887cd27a5 100644 --- a/service/spacebar/build/supervisor.mjs +++ b/service/spacebar/build/supervisor.mjs @@ -11,6 +11,7 @@ import { promisify } from "node:util"; const { BRANCH, PORT, VOICE_PUBLIC_HOST, VOICE_SIGNALING_PORT, VOICE_MEDIA_PORT, VOICE_NATIVE_MEDIA_PORT } = process.env; const WORK = "/work"; +const PION_BRIDGE_SOCKET = "/tmp/snowcord-sfu-audio.sock"; const REPO = "/source.git"; const POLL_MS = 250; const DRAIN_MS = 500; @@ -157,6 +158,7 @@ async function stopVoice() { if (!voice) return; await Promise.all([stop(voice.signaling), stop(voice.sfu)]); rmSync("/tmp/sfu-ipc.sock", { force: true }); + rmSync(PION_BRIDGE_SOCKET, { force: true }); voice = null; } @@ -165,9 +167,12 @@ async function prepareSfu(release) { if (pin.owner !== "spacebarchat" || pin.repo !== "pion-webrtc" || !/^[a-f0-9]{40}$/.test(pin.rev)) { throw new Error("invalid Snowcord Pion SFU pin"); } - const name = `pion-sfu-${pin.rev}`; + const patch = `${release.dir}/extra/pion-sfu-voice-bridge.patch`; + const bridge = Boolean(VOICE_NATIVE_MEDIA_PORT) && existsSync(patch); + const patchId = bridge ? `-${createHash("sha256").update(readFileSync(patch)).digest("hex")}` : ""; + const name = `pion-sfu-${pin.rev}${patchId}`; const binary = `${WORK}/${name}`; - if (existsSync(binary)) return binary; + if (existsSync(binary)) return { binary, bridge }; const scratch = `${WORK}/sfu-build`; const source = `${scratch}/source`; @@ -178,6 +183,7 @@ async function prepareSfu(release) { await run("git", ["-C", source, "init", "--quiet"]); await run("git", ["-C", source, "fetch", "--quiet", "--depth", "1", `https://github.com/${pin.owner}/${pin.repo}`, pin.rev]); await run("git", ["-C", source, "-c", "advice.detachedHead=false", "checkout", "--quiet", "FETCH_HEAD"]); + if (bridge) await run("patch", ["--batch", "--fuzz=0", "-d", source, "-p1", "-i", patch]); await run("/usr/local/go/bin/go", ["build", "-trimpath", "-buildvcs=false", "-o", `${scratch}/pion-sfu`, "."], { cwd: `${source}/pion-sfu`, env: { @@ -192,7 +198,7 @@ async function prepareSfu(release) { }); renameSync(`${scratch}/pion-sfu`, binary); for (const entry of readdirSync(WORK)) if (entry.startsWith("pion-sfu-") && entry !== name) rmSync(`${WORK}/${entry}`, { force: true }); - return binary; + return { binary, bridge }; } finally { rmSync(scratch, { recursive: true, force: true }); } @@ -210,7 +216,7 @@ async function startVoice(release) { try { const addresses = await resolve4(VOICE_PUBLIC_HOST); if (addresses.length !== 1) throw new Error("voice hostname must resolve to one IPv4 address"); - const binary = await prepareSfu(release); + const { binary, bridge } = await prepareSfu(release); if (stopping) return; await stopVoice(); if (stopping || live !== release) return; @@ -219,8 +225,11 @@ async function startVoice(release) { const start = (command, args, env) => capture(spawn(command, args, { cwd: "/data", stdio: ["ignore", "pipe", "pipe"], env, })).on("error", (error) => log(`voice process: ${error.message}`)); - voice = { release, sfu: start(binary, ["-ip", ip, "-port", VOICE_MEDIA_PORT], {}), signaling: null }; - await waitForVoice(voice.sfu, () => statSync("/tmp/sfu-ipc.sock", { throwIfNoEntry: false })?.isSocket()); + const bridgeEnv = bridge ? { VOICE_PION_BRIDGE_SOCKET: PION_BRIDGE_SOCKET } : {}; + voice = { release, sfu: start(binary, ["-ip", ip, "-port", VOICE_MEDIA_PORT], bridgeEnv), signaling: null }; + await waitForVoice(voice.sfu, () => + statSync("/tmp/sfu-ipc.sock", { throwIfNoEntry: false })?.isSocket() && + (!bridge || statSync(PION_BRIDGE_SOCKET, { throwIfNoEntry: false })?.isSocket())); if (stopping) return; voice.signaling = start(process.execPath, ["--enable-source-maps", `${release.dir}/dist/webrtc/start.js`], { @@ -231,7 +240,8 @@ async function startVoice(release) { WRTC_PUBLIC_IP: ip, WRTC_PORT_MIN: VOICE_MEDIA_PORT, WRTC_PORT_MAX: VOICE_MEDIA_PORT, - ...(VOICE_NATIVE_MEDIA_PORT ? { + ...(bridge ? { + ...bridgeEnv, VOICE_UDP_PORT: VOICE_NATIVE_MEDIA_PORT, VOICE_UDP_PUBLIC_IP: ip, VOICE_UDP_LOOPBACK_RELAY: "true", diff --git a/tools/spacebar.md b/tools/spacebar.md index 905ab21052eeef021166bfadd3d964a275e6e305..f2a2e6ce27f3753bc5b2d61c9f1c385136d86413 100644 --- a/tools/spacebar.md +++ b/tools/spacebar.md @@ -56,8 +56,10 @@ The production service publishes the voice WebSocket at `voice.phoenix-selfhoste [`voice-network.json`](../service/spacebar/voice-network.json) configures the WebSocket port and separate UDP ports for Pion and native Discord voice. The supervisor builds Pion from the Snowcord revision's `flake.lock`: git fetches the pinned commit, `go.sum` -pins the modules, and the build and SFU run with a minimal environment. The binary -is kept in `/work` for that pin, so a restart needs no network. +pins the modules, and the build and SFU run with a minimal environment. A release +with the voice bridge patch uses a cached binary keyed by the pin and patch hash. +That release enables native UDP and connects the SFU and voice process through a +local Opus socket. Older releases keep Pion voice during rollout or rollback. It starts one SFU and one standalone voice process after the API is healthy. Each app release restarts voice, and an unavailable voice process retries while the API continues serving. Previews leave voice disabled.