WebCam als schlanke alternative zum appVideoControl

This commit is contained in:
chk
2026-06-02 22:19:08 +02:00
commit 9b1ae2ae14
11 changed files with 842 additions and 0 deletions

50
src/snapshotService.js Normal file
View File

@@ -0,0 +1,50 @@
'use strict';
const express = require('express');
// Returns an Express router mounted at /api/snapshot
// GET /api/snapshot → JSON listing of cameras
// GET /api/snapshot/cam0 → latest JPEG from cam0
// GET /api/snapshot/cam1 → latest JPEG from cam1
function createSnapshotRouter(streams) {
const router = express.Router();
router.get('/', (_req, res) => {
res.json({
cameras: streams.map((s, i) => ({
id: `cam${i}`,
device: s.device,
running: s.isRunning,
clients: s.clientCount,
hasFrame: s.latestFrame !== null,
url: `/api/snapshot/cam${i}`,
})),
});
});
router.get('/:id', (req, res) => {
const idx = parseInt(req.params.id.replace('cam', ''), 10);
if (isNaN(idx) || !streams[idx]) {
return res.status(404).json({ error: 'camera not found' });
}
const frame = streams[idx].latestFrame;
if (!frame) {
return res.status(503).json({ error: 'no frame available yet stream may still be starting' });
}
res.set({
'Content-Type': 'image/jpeg',
'Content-Length': frame.length,
'Cache-Control': 'no-store',
'X-Camera-Id': `cam${idx}`,
'X-Camera-Device': streams[idx].device,
'X-Timestamp': new Date().toISOString(),
});
res.end(frame);
});
return router;
}
module.exports = { createSnapshotRouter };