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";...@@ -11,6 +11,7 @@ import { promisify } from "node:util";
1111
12const { BRANCH, PORT, VOICE_PUBLIC_HOST, VOICE_SIGNALING_PORT, VOICE_MEDIA_PORT, VOICE_NATIVE_MEDIA_PORT } = process.env;12const { BRANCH, PORT, VOICE_PUBLIC_HOST, VOICE_SIGNALING_PORT, VOICE_MEDIA_PORT, VOICE_NATIVE_MEDIA_PORT } = process.env;
13const WORK = "/work";13const WORK = "/work";
14const PION_BRIDGE_SOCKET = "/tmp/snowcord-sfu-audio.sock";
14const REPO = "/source.git";15const REPO = "/source.git";
15const POLL_MS = 250;16const POLL_MS = 250;
16const DRAIN_MS = 500;17const DRAIN_MS = 500;
...@@ -157,6 +158,7 @@ async function stopVoice() {...@@ -157,6 +158,7 @@ async function stopVoice() {
157 if (!voice) return;158 if (!voice) return;
158 await Promise.all([stop(voice.signaling), stop(voice.sfu)]);159 await Promise.all([stop(voice.signaling), stop(voice.sfu)]);
159 rmSync("/tmp/sfu-ipc.sock", { force: true });160 rmSync("/tmp/sfu-ipc.sock", { force: true });
161 rmSync(PION_BRIDGE_SOCKET, { force: true });
160 voice = null;162 voice = null;
161}163}
162164
...@@ -165,9 +167,12 @@ async function prepareSfu(release) {...@@ -165,9 +167,12 @@ async function prepareSfu(release) {
165 if (pin.owner !== "spacebarchat" || pin.repo !== "pion-webrtc" || !/^[a-f0-9]{40}$/.test(pin.rev)) {167 if (pin.owner !== "spacebarchat" || pin.repo !== "pion-webrtc" || !/^[a-f0-9]{40}$/.test(pin.rev)) {
166 throw new Error("invalid Snowcord Pion SFU pin");168 throw new Error("invalid Snowcord Pion SFU pin");
167 }169 }
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}`;
169 const binary = `${WORK}/${name}`;174 const binary = `${WORK}/${name}`;
170 if (existsSync(binary)) return binary;175 if (existsSync(binary)) return { binary, bridge };
171176
172 const scratch = `${WORK}/sfu-build`;177 const scratch = `${WORK}/sfu-build`;
173 const source = `${scratch}/source`;178 const source = `${scratch}/source`;
...@@ -178,6 +183,7 @@ async function prepareSfu(release) {...@@ -178,6 +183,7 @@ async function prepareSfu(release) {
178 await run("git", ["-C", source, "init", "--quiet"]);183 await run("git", ["-C", source, "init", "--quiet"]);
179 await run("git", ["-C", source, "fetch", "--quiet", "--depth", "1", `https://github.com/${pin.owner}/${pin.repo}`, pin.rev]);184 await run("git", ["-C", source, "fetch", "--quiet", "--depth", "1", `https://github.com/${pin.owner}/${pin.repo}`, pin.rev]);
180 await run("git", ["-C", source, "-c", "advice.detachedHead=false", "checkout", "--quiet", "FETCH_HEAD"]);185 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]);
181 await run("/usr/local/go/bin/go", ["build", "-trimpath", "-buildvcs=false", "-o", `${scratch}/pion-sfu`, "."], {187 await run("/usr/local/go/bin/go", ["build", "-trimpath", "-buildvcs=false", "-o", `${scratch}/pion-sfu`, "."], {
182 cwd: `${source}/pion-sfu`,188 cwd: `${source}/pion-sfu`,
183 env: {189 env: {
...@@ -192,7 +198,7 @@ async function prepareSfu(release) {...@@ -192,7 +198,7 @@ async function prepareSfu(release) {
192 });198 });
193 renameSync(`${scratch}/pion-sfu`, binary);199 renameSync(`${scratch}/pion-sfu`, binary);
194 for (const entry of readdirSync(WORK)) if (entry.startsWith("pion-sfu-") && entry !== name) rmSync(`${WORK}/${entry}`, { force: true });200 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 };
196 } finally {202 } finally {
197 rmSync(scratch, { recursive: true, force: true });203 rmSync(scratch, { recursive: true, force: true });
198 }204 }
...@@ -210,7 +216,7 @@ async function startVoice(release) {...@@ -210,7 +216,7 @@ async function startVoice(release) {
210 try {216 try {
211 const addresses = await resolve4(VOICE_PUBLIC_HOST);217 const addresses = await resolve4(VOICE_PUBLIC_HOST);
212 if (addresses.length !== 1) throw new Error("voice hostname must resolve to one IPv4 address");218 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);
214 if (stopping) return;220 if (stopping) return;
215 await stopVoice();221 await stopVoice();
216 if (stopping || live !== release) return;222 if (stopping || live !== release) return;
...@@ -219,8 +225,11 @@ async function startVoice(release) {...@@ -219,8 +225,11 @@ async function startVoice(release) {
219 const start = (command, args, env) => capture(spawn(command, args, {225 const start = (command, args, env) => capture(spawn(command, args, {
220 cwd: "/data", stdio: ["ignore", "pipe", "pipe"], env,226 cwd: "/data", stdio: ["ignore", "pipe", "pipe"], env,
221 })).on("error", (error) => log(`voice process: ${error.message}`));227 })).on("error", (error) => log(`voice process: ${error.message}`));
222 voice = { release, sfu: start(binary, ["-ip", ip, "-port", VOICE_MEDIA_PORT], {}), signaling: null };228 const bridgeEnv = bridge ? { VOICE_PION_BRIDGE_SOCKET: PION_BRIDGE_SOCKET } : {};
223 await waitForVoice(voice.sfu, () => statSync("/tmp/sfu-ipc.sock", { throwIfNoEntry: false })?.isSocket());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()));
224 if (stopping) return;233 if (stopping) return;
225234
226 voice.signaling = start(process.execPath, ["--enable-source-maps", `${release.dir}/dist/webrtc/start.js`], {235 voice.signaling = start(process.execPath, ["--enable-source-maps", `${release.dir}/dist/webrtc/start.js`], {
...@@ -231,7 +240,8 @@ async function startVoice(release) {...@@ -231,7 +240,8 @@ async function startVoice(release) {
231 WRTC_PUBLIC_IP: ip,240 WRTC_PUBLIC_IP: ip,
232 WRTC_PORT_MIN: VOICE_MEDIA_PORT,241 WRTC_PORT_MIN: VOICE_MEDIA_PORT,
233 WRTC_PORT_MAX: VOICE_MEDIA_PORT,242 WRTC_PORT_MAX: VOICE_MEDIA_PORT,
234 ...(VOICE_NATIVE_MEDIA_PORT ? {243 ...(bridge ? {
244 ...bridgeEnv,
235 VOICE_UDP_PORT: VOICE_NATIVE_MEDIA_PORT,245 VOICE_UDP_PORT: VOICE_NATIVE_MEDIA_PORT,
236 VOICE_UDP_PUBLIC_IP: ip,246 VOICE_UDP_PUBLIC_IP: ip,
237 VOICE_UDP_LOOPBACK_RELAY: "true",247 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...@@ -56,8 +56,10 @@ The production service publishes the voice WebSocket at `voice.phoenix-selfhoste
56[`voice-network.json`](../service/spacebar/voice-network.json) configures the56[`voice-network.json`](../service/spacebar/voice-network.json) configures the
57WebSocket port and separate UDP ports for Pion and native Discord voice. The supervisor builds Pion57WebSocket port and separate UDP ports for Pion and native Discord voice. The supervisor builds Pion
58from the Snowcord revision's `flake.lock`: git fetches the pinned commit, `go.sum`58from 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 binary59pins the modules, and the build and SFU run with a minimal environment. A release
60is kept in `/work` for that pin, so a restart needs no network.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.
61It starts one SFU and one standalone voice process after the API is healthy.63It starts one SFU and one standalone voice process after the API is healthy.
62Each app release restarts voice, and an unavailable voice process retries64Each app release restarts voice, and an unavailable voice process retries
63while the API continues serving. Previews leave voice disabled.65while the API continues serving. Previews leave voice disabled.