authoravatarclover caruso<git@paperclover.net>2026-11-22 12:17:59+00:00
committeravatarclover caruso<git@paperclover.net>2026-11-22 12:18:01+00:00
loge48b8974b84edabf8620505e5be01424e35eac7e
treeb3113b81d05a46b28f5a87f4644da047607f5170
parentd559ec390582fe4bd7e029097434273bbe1ddd9b

Build a Pion Opus bridge for native Snowcord voice

Key the SFU binary by the app release bridge patch, and provide the same local Unix socket to Pion and standalone voice. Keep older app releases on the existing Pion transport during rollout and rollback. Assisted-by: gpt-6.2-sol

2 files changed, 21 insertions(+), 9 deletions(-)

service/spacebar/build/supervisor.mjs+17-7
......@@ -11,6 +11,7 @@ import { promisify } from "node:util";
1111
1212const { BRANCH, PORT, VOICE_PUBLIC_HOST, VOICE_SIGNALING_PORT, VOICE_MEDIA_PORT, VOICE_NATIVE_MEDIA_PORT } = process.env;
1313const WORK = "/work";
14const PION_BRIDGE_SOCKET = "/tmp/snowcord-sfu-audio.sock";
1415const REPO = "/source.git";
1516const POLL_MS = 250;
1617const DRAIN_MS = 500;
......@@ -157,6 +158,7 @@ async function stopVoice() {
157158 if (!voice) return;
158159 await Promise.all([stop(voice.signaling), stop(voice.sfu)]);
159160 rmSync("/tmp/sfu-ipc.sock", { force: true });
161 rmSync(PION_BRIDGE_SOCKET, { force: true });
160162 voice = null;
161163}
162164
......@@ -165,9 +167,12 @@ async function prepareSfu(release) {
165167 if (pin.owner !== "spacebarchat" || pin.repo !== "pion-webrtc" || !/^[a-f0-9]{40}$/.test(pin.rev)) {
166168 throw new Error("invalid Snowcord Pion SFU pin");
167169 }
168 const name = `pion-sfu-${pin.rev}`;
170 const patch = `${release.dir}/extra/pion-sfu-voice-bridge.patch`;
171 const bridge = Boolean(VOICE_NATIVE_MEDIA_PORT) && existsSync(patch);
172 const patchId = bridge ? `-${createHash("sha256").update(readFileSync(patch)).digest("hex")}` : "";
173 const name = `pion-sfu-${pin.rev}${patchId}`;
169174 const binary = `${WORK}/${name}`;
170 if (existsSync(binary)) return binary;
175 if (existsSync(binary)) return { binary, bridge };
171176
172177 const scratch = `${WORK}/sfu-build`;
173178 const source = `${scratch}/source`;
......@@ -178,6 +183,7 @@ async function prepareSfu(release) {
178183 await run("git", ["-C", source, "init", "--quiet"]);
179184 await run("git", ["-C", source, "fetch", "--quiet", "--depth", "1", `https://github.com/${pin.owner}/${pin.repo}`, pin.rev]);
180185 await run("git", ["-C", source, "-c", "advice.detachedHead=false", "checkout", "--quiet", "FETCH_HEAD"]);
186 if (bridge) await run("patch", ["--batch", "--fuzz=0", "-d", source, "-p1", "-i", patch]);
181187 await run("/usr/local/go/bin/go", ["build", "-trimpath", "-buildvcs=false", "-o", `${scratch}/pion-sfu`, "."], {
182188 cwd: `${source}/pion-sfu`,
183189 env: {
......@@ -192,7 +198,7 @@ async function prepareSfu(release) {
192198 });
193199 renameSync(`${scratch}/pion-sfu`, binary);
194200 for (const entry of readdirSync(WORK)) if (entry.startsWith("pion-sfu-") && entry !== name) rmSync(`${WORK}/${entry}`, { force: true });
195 return binary;
201 return { binary, bridge };
196202 } finally {
197203 rmSync(scratch, { recursive: true, force: true });
198204 }
......@@ -210,7 +216,7 @@ async function startVoice(release) {
210216 try {
211217 const addresses = await resolve4(VOICE_PUBLIC_HOST);
212218 if (addresses.length !== 1) throw new Error("voice hostname must resolve to one IPv4 address");
213 const binary = await prepareSfu(release);
219 const { binary, bridge } = await prepareSfu(release);
214220 if (stopping) return;
215221 await stopVoice();
216222 if (stopping || live !== release) return;
......@@ -219,8 +225,11 @@ async function startVoice(release) {
219225 const start = (command, args, env) => capture(spawn(command, args, {
220226 cwd: "/data", stdio: ["ignore", "pipe", "pipe"], env,
221227 })).on("error", (error) => log(`voice process: ${error.message}`));
222 voice = { release, sfu: start(binary, ["-ip", ip, "-port", VOICE_MEDIA_PORT], {}), signaling: null };
223 await waitForVoice(voice.sfu, () => statSync("/tmp/sfu-ipc.sock", { throwIfNoEntry: false })?.isSocket());
228 const bridgeEnv = bridge ? { VOICE_PION_BRIDGE_SOCKET: PION_BRIDGE_SOCKET } : {};
229 voice = { release, sfu: start(binary, ["-ip", ip, "-port", VOICE_MEDIA_PORT], bridgeEnv), signaling: null };
230 await waitForVoice(voice.sfu, () =>
231 statSync("/tmp/sfu-ipc.sock", { throwIfNoEntry: false })?.isSocket() &&
232 (!bridge || statSync(PION_BRIDGE_SOCKET, { throwIfNoEntry: false })?.isSocket()));
224233 if (stopping) return;
225234
226235 voice.signaling = start(process.execPath, ["--enable-source-maps", `${release.dir}/dist/webrtc/start.js`], {
......@@ -231,7 +240,8 @@ async function startVoice(release) {
231240 WRTC_PUBLIC_IP: ip,
232241 WRTC_PORT_MIN: VOICE_MEDIA_PORT,
233242 WRTC_PORT_MAX: VOICE_MEDIA_PORT,
234 ...(VOICE_NATIVE_MEDIA_PORT ? {
243 ...(bridge ? {
244 ...bridgeEnv,
235245 VOICE_UDP_PORT: VOICE_NATIVE_MEDIA_PORT,
236246 VOICE_UDP_PUBLIC_IP: ip,
237247 VOICE_UDP_LOOPBACK_RELAY: "true",
tools/spacebar.md+4-2
......@@ -56,8 +56,10 @@ The production service publishes the voice WebSocket at `voice.phoenix-selfhoste
5656[`voice-network.json`](../service/spacebar/voice-network.json) configures the
5757WebSocket port and separate UDP ports for Pion and native Discord voice. The supervisor builds Pion
5858from the Snowcord revision's `flake.lock`: git fetches the pinned commit, `go.sum`
59pins the modules, and the build and SFU run with a minimal environment. The binary
60is kept in `/work` for that pin, so a restart needs no network.
59pins the modules, and the build and SFU run with a minimal environment. A release
60with the voice bridge patch uses a cached binary keyed by the pin and patch hash.
61That release enables native UDP and connects the SFU and voice process through a
62local Opus socket. Older releases keep Pion voice during rollout or rollback.
6163It starts one SFU and one standalone voice process after the API is healthy.
6264Each app release restarts voice, and an unavailable voice process retries
6365while the API continues serving. Previews leave voice disabled.