Relay camera signalling and mint TURN credentials
Makes the printer cameras playable from a hosted console, which they were not: GET /printers/:id/stream handed out the gateway's own LAN address, and a browser cannot use it. go2rtc sends no CORS headers and checks its API password before CORS, so a browser's unauthenticated preflight gets a 401 (measured against 1.9.14), and a gateway is behind a workshop NAT anyway. That endpoint could only ever have worked for an operator standing on the same network, and offered everyone else a player that silently never loaded. So /:id/stream now returns a camera name and short-lived ICE servers, and POST /:id/webrtc relays the offer to the gateway over the socket it already holds open and returns go2rtc's answer. Media travels browser-to-gateway through the TURN relay, DTLS-SRTP encrypted, so neither this process nor Cloudflare can read it - which is why this is signalling rather than proxying fMP4 through the API. That alternative also needed a credential in the URL, because <video> cannot send an Authorization header. Only the browser gets TURN credentials. It is pinned to relay-only, so it allocates the relay and go2rtc sends to it; one side is enough. That keeps rotation on a per-viewer request instead of in go2rtc's config, which is read once at startup - rotating there would restart it and drop every viewer on a timer. Unconfigured TURN is supported rather than fatal, unlike R2: a viewer on the gateway's LAN connects on host candidates. turnConfigured is returned alongside, because empty iceServers means "not set up" in one case and "minting failed" in the other, and those need different answers. Camera negotiations are failed immediately when a gateway disconnects, rather than waiting out the 12s timeout - a browser is holding a request open on each one. getStreamBaseUrl is kept but no longer served to anyone: gateways still report the field and it is the only record of where one thinks it is reachable, which is useful when diagnosing one. e2e-edge.sh asserts the new shape, that no streamUrl comes back, and drives a real offer through the relay - asserting the refusal came from go2rtc rather than from a timeout, since that is what proves the round trip happened.
This commit is contained in:
+15
-12
@@ -1,7 +1,7 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { Value } from "@sinclair/typebox/value";
|
||||
import { EdgeClientMessageSchema } from "@continuum/ts-types";
|
||||
import { NO_CAMERA_MESSAGE, NO_GATEWAY_STREAM_MESSAGE, resolveStream } from "../src/stream";
|
||||
import { NO_CAMERA_MESSAGE, NO_GATEWAY_MESSAGE, resolveStream } from "../src/stream";
|
||||
|
||||
const hello = { type: "hello", gateway_id: "gw-1", farm_id: "f1", token: "t" };
|
||||
|
||||
@@ -37,26 +37,29 @@ describe("telemetry camera_stream", () => {
|
||||
});
|
||||
|
||||
describe("resolveStream", () => {
|
||||
// No streamUrl any more, and its absence is the point: a browser cannot
|
||||
// play from a gateway's own address (no CORS, and a workshop NAT besides),
|
||||
// so handing one out only invited a player that could never load. What is
|
||||
// left is the camera name to negotiate for.
|
||||
test("returns the endpoint's success shape", () => {
|
||||
expect(resolveStream("p1", "http://gateway.local:1984")).toEqual({
|
||||
ok: true,
|
||||
stream: { streamUrl: "http://gateway.local:1984", cameraId: "p1" },
|
||||
});
|
||||
expect(resolveStream("p1", true)).toEqual({ ok: true, stream: { cameraId: "p1" } });
|
||||
});
|
||||
|
||||
test("the two failures are distinguishable", () => {
|
||||
const noCamera = resolveStream(null, "http://gateway.local:1984");
|
||||
const noBase = resolveStream("p1", undefined);
|
||||
if (noCamera.ok || noBase.ok) throw new Error("expected both to fail");
|
||||
const noCamera = resolveStream(null, true);
|
||||
const offline = resolveStream("p1", false);
|
||||
if (noCamera.ok || offline.ok) throw new Error("expected both to fail");
|
||||
expect(noCamera.reason).toBe("no_camera");
|
||||
expect(noBase.reason).toBe("no_stream_base");
|
||||
expect(offline.reason).toBe("gateway_offline");
|
||||
expect(noCamera.message).toBe(NO_CAMERA_MESSAGE);
|
||||
expect(noBase.message).toBe(NO_GATEWAY_STREAM_MESSAGE);
|
||||
expect(noCamera.message).not.toBe(noBase.message);
|
||||
expect(offline.message).toBe(NO_GATEWAY_MESSAGE);
|
||||
// Different problems for whoever is debugging: a P1/A1 that can never
|
||||
// stream, versus a gateway that is merely unplugged.
|
||||
expect(noCamera.message).not.toBe(offline.message);
|
||||
});
|
||||
|
||||
test("a printer with no camera is blamed first, even if the gateway is also absent", () => {
|
||||
const both = resolveStream(undefined, undefined);
|
||||
const both = resolveStream(undefined, false);
|
||||
expect(!both.ok && both.reason).toBe("no_camera");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user