/** * Camera stream resolution, on its own with no imports - the same reason as * `dispatch/job_status.ts`: `../db` opens a connection at import time, and * the distinct failure messages here are worth testing without one. * * This used to hand back the gateway's own LAN address for the browser to * play from. It cannot: 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. * So there is no URL to give out. What a viewer needs is a camera name to ask * for and a gateway that is connected to relay the SDP exchange. */ export const NO_CAMERA_MESSAGE = "this printer reports no camera that can be streamed"; export const NO_GATEWAY_MESSAGE = "this farm's gateway is not connected, so its camera cannot be negotiated"; export type StreamResolution = | { ok: true; stream: { cameraId: string } } | { ok: false; reason: "no_camera" | "gateway_offline"; message: string }; /** * A camera name, or an error naming what is missing. The printer is checked * first: if it can never stream, saying the gateway is offline would send * someone off to fix the wrong thing - and a P1/A1 can never stream, so this * is the common case rather than the unlucky one. */ export function resolveStream( cameraStream: string | null | undefined, gatewayOnline: boolean, ): StreamResolution { if (!cameraStream) return { ok: false, reason: "no_camera", message: NO_CAMERA_MESSAGE }; if (!gatewayOnline) { return { ok: false, reason: "gateway_offline", message: NO_GATEWAY_MESSAGE }; } return { ok: true, stream: { cameraId: cameraStream } }; }