| ... | @@ -11,6 +11,7 @@ import { promisify } from "node:util"; | ... | @@ -11,6 +11,7 @@ import { promisify } from "node:util"; |
| 11 | | 11 | |
| 12 | const { BRANCH, PORT, VOICE_PUBLIC_HOST, VOICE_SIGNALING_PORT, VOICE_MEDIA_PORT, VOICE_NATIVE_MEDIA_PORT } = process.env; | 12 | const { BRANCH, PORT, VOICE_PUBLIC_HOST, VOICE_SIGNALING_PORT, VOICE_MEDIA_PORT, VOICE_NATIVE_MEDIA_PORT } = process.env; |
| 13 | const WORK = "/work"; | 13 | const WORK = "/work"; |
| | 14 | const PION_BRIDGE_SOCKET = "/tmp/snowcord-sfu-audio.sock"; |
| 14 | const REPO = "/source.git"; | 15 | const REPO = "/source.git"; |
| 15 | const POLL_MS = 250; | 16 | const POLL_MS = 250; |
| 16 | const DRAIN_MS = 500; | 17 | const 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 | } |
| 162 | | 164 | |
| ... | @@ -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 }; |
| 171 | | 176 | |
| 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; |
| 225 | | 234 | |
| 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", |