replay capture ?

This commit is contained in:
legop3
2026-01-03 20:53:38 -05:00
parent bbb4014f61
commit e0cadb493f
4 changed files with 210 additions and 1 deletions
@@ -3,9 +3,11 @@ const logger = require('../globals/logger').child('roomCameraSnapshot');
const { getRoomCameras, roomCameraEvents } = require('./roomCameraService');
const POLL_INTERVAL_MS = 800;
const REPLAY_FRAME_COUNT = 15;
const FETCH_TIMEOUT_MS = 2000;
const cameraState = new Map(); // id -> {frame, ts, error, failures, fetching}
const frameHistory = new Map(); // id -> [{buffer, ts}]
const events = new EventEmitter(); // frame, status
let pollTimer = null;
@@ -16,6 +18,15 @@ function markState(id, updates = {}) {
return next;
}
function recordFrame(id, buffer, ts) {
const history = frameHistory.get(id) || [];
history.push({ buffer, ts });
if (history.length > REPLAY_FRAME_COUNT) {
history.splice(0, history.length - REPLAY_FRAME_COUNT);
}
frameHistory.set(id, history);
}
async function fetchSnapshot(camera) {
const { id, url } = camera;
const state = cameraState.get(id);
@@ -32,6 +43,7 @@ async function fetchSnapshot(camera) {
const buffer = Buffer.from(arrayBuffer);
const ts = Date.now();
markState(id, { frame: buffer, ts, error: null, failures: 0 });
recordFrame(id, buffer, ts);
events.emit('frame', { id, buffer, ts });
} catch (err) {
const failures = (state?.failures || 0) + 1;
@@ -50,6 +62,7 @@ function stopAll() {
pollTimer = null;
}
cameraState.clear();
frameHistory.clear();
}
function startAll() {
@@ -71,6 +84,20 @@ function getState(id) {
};
}
function getReplayFrames(id, count = REPLAY_FRAME_COUNT) {
const history = frameHistory.get(id) || [];
if (!history.length) return [];
return history.slice(Math.max(0, history.length - count));
}
function getReplayFrameDelayMs() {
return POLL_INTERVAL_MS;
}
function getReplayFrameCount() {
return REPLAY_FRAME_COUNT;
}
roomCameraEvents.on('update', () => {
logger.info('Room cameras changed; restarting snapshot pollers');
startAll();
@@ -81,4 +108,7 @@ startAll();
module.exports = {
roomCameraStreamEvents: events,
getRoomCameraState: getState,
getRoomCameraFrames: getReplayFrames,
getRoomCameraReplayDelayMs: getReplayFrameDelayMs,
getRoomCameraReplayFrameCount: getReplayFrameCount,
};