mirror of
https://github.com/legop3/MultiRoombaRover.git
synced 2026-09-16 09:31:20 -04:00
replay capture ?
This commit is contained in:
@@ -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,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user