jpeg roomcams first pass

This commit is contained in:
legop3
2025-12-01 21:57:16 -05:00
parent 42533bf63c
commit 62ab8539c6
10 changed files with 373 additions and 148 deletions
@@ -0,0 +1,99 @@
const EventEmitter = require('events');
const logger = require('../globals/logger').child('roomCameraSnapshot');
const { getRoomCameras, roomCameraEvents } = require('./roomCameraService');
const POLL_INTERVAL_MS = 250; // 4 fps target
const FETCH_TIMEOUT_MS = 2000;
const STALE_AFTER_MS = 4000;
const cameraState = new Map(); // id -> {frame, ts, stale, error, failures, fetching}
const pollers = new Map();
const events = new EventEmitter(); // frame, status
function markState(id, updates = {}) {
const prev = cameraState.get(id) || {};
const next = { ...prev, ...updates };
if (next.ts != null) {
next.stale = Date.now() - next.ts > STALE_AFTER_MS || !!next.stale;
} else {
next.stale = true;
}
cameraState.set(id, next);
return next;
}
async function fetchSnapshot(camera) {
const { id, url } = camera;
const state = cameraState.get(id);
if (!url || state?.fetching) return;
markState(id, { fetching: true });
const abortController = new AbortController();
const timeout = setTimeout(() => abortController.abort(), FETCH_TIMEOUT_MS);
try {
const res = await fetch(url, { signal: abortController.signal });
if (!res.ok) {
throw new Error(`HTTP ${res.status}`);
}
const arrayBuffer = await res.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
const ts = Date.now();
const next = markState(id, { frame: buffer, ts, error: null, failures: 0 });
events.emit('frame', { id, buffer, ts, stale: !!next.stale });
} catch (err) {
const failures = (state?.failures || 0) + 1;
markState(id, { error: err.message, failures });
events.emit('status', { id, error: err.message });
logger.debug('Snapshot fetch failed', { id, err: err.message });
} finally {
clearTimeout(timeout);
markState(id, { fetching: false });
}
}
function startCamera(camera) {
const { id, url } = camera;
if (!url) {
logger.warn('Skipping room camera without URL', { id });
return;
}
if (pollers.has(id)) return;
markState(id, { frame: null, ts: null, error: null, failures: 0, fetching: false });
const poller = setInterval(() => fetchSnapshot(camera), POLL_INTERVAL_MS);
pollers.set(id, poller);
logger.info('Started snapshot polling', { id, url });
}
function stopAll() {
pollers.forEach((poller) => clearInterval(poller));
pollers.clear();
cameraState.clear();
}
function startAll() {
stopAll();
getRoomCameras().forEach((camera) => startCamera(camera));
}
function getState(id) {
const state = cameraState.get(id);
if (!state) return null;
const stale = state.ts == null || Date.now() - state.ts > STALE_AFTER_MS;
return {
frame: state.frame || null,
ts: state.ts || null,
stale,
error: state.error || null,
};
}
roomCameraEvents.on('update', () => {
logger.info('Room cameras changed; restarting snapshot pollers');
startAll();
});
startAll();
module.exports = {
roomCameraStreamEvents: events,
getRoomCameraState: getState,
};