Files
parking_solution/packages/devices/src/drivers/rfc2435.test.ts
T
julian 39fb5fb7c2 feat(cameras): booth camera tiles — latest snapshot at rest, live video while a vehicle is in view
Footer camera chips open/close a tile per camera (bottom right, ~15% wide, stacking
leftwards); the open set is a per-user preference (users.open_cameras). A tile shows the
camera's newest entry/exit snapshot and switches to live video while that camera pushes
vehicle events (per-camera activity, same 30 s TTL as lane status).

- devices: RTSP MJPEG client + RFC 2435 RTP/JPEG reassembly; Hikvision openLiveStream()
  on the sub-stream (x02, must be MJPEG — H.264 is refused with that reason)
- server: CameraLiveHub (one shared upstream per camera, latest-frame delivery, fps cap,
  linger, backoff), CameraActivity, /api/cameras/:id/latest-snapshot, WS
  /api/cameras/:id/live (shared handshake auth with /api/ws + device:read),
  PUT /api/auth/open-cameras, migration 0033 (open_cameras + snapshot device/time index)
- web: CameraDock, footer toggles, binary frames in the platform socket (Tauri too)
- .gitattributes: *.jpg / *.bin binary (byte-exact test fixtures)

Verified on park-lab cameras end to end and in headless Firefox.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 12:48:50 +01:00

101 lines
3.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { describe, expect, it } from "vitest";
import { JpegDepacketizer, MAX_FRAME_BYTES, rtpPayload, tablesForQ } from "./rfc2435.js";
// Fixture: two frames of RTP/JPEG captured from a park-lab Hikvision DS-2CD1043G2-LIU
// (sub-stream 102 set to MJPEG, 640×360, type 1, Q 48, last packet of a frame padded),
// stored as [u16 length][packet]... The expected JPEGs were rebuilt independently and
// verified to decode with Pillow (2026-11-17).
const dir = fileURLToPath(new URL("./__fixtures__/", import.meta.url));
const capture = readFileSync(`${dir}rtp-jpeg-2frames.bin`);
const expected = [readFileSync(`${dir}rtp-jpeg-frame0.jpg`), readFileSync(`${dir}rtp-jpeg-frame1.jpg`)];
function packets(buf: Buffer): Buffer[] {
const out: Buffer[] = [];
for (let i = 0; i < buf.length; ) {
const len = buf.readUInt16BE(i);
out.push(buf.subarray(i + 2, i + 2 + len));
i += 2 + len;
}
return out;
}
describe("JpegDepacketizer", () => {
it("rebuilds the camera's frames byte-for-byte", () => {
const d = new JpegDepacketizer();
const frames = packets(capture)
.map((p) => d.push(p))
.filter((f): f is Buffer => f !== null);
expect(frames).toHaveLength(2);
expect(frames[0]!.equals(expected[0]!)).toBe(true);
expect(frames[1]!.equals(expected[1]!)).toBe(true);
expect(d.dropped).toBe(0);
});
it("drops a frame with a missing packet and recovers on the next one", () => {
const d = new JpegDepacketizer();
const all = packets(capture);
// Remove the 3rd packet of frame 0.
const lossy = [...all.slice(0, 2), ...all.slice(3)];
const frames = lossy.map((p) => d.push(p)).filter((f): f is Buffer => f !== null);
expect(frames).toHaveLength(1);
expect(frames[0]!.equals(expected[1]!)).toBe(true);
expect(d.dropped).toBe(1);
});
it("ignores packets until a frame start when joining mid-frame", () => {
const d = new JpegDepacketizer();
const all = packets(capture);
const firstEnd = all.findIndex((p) => (p[1]! & 0x80) !== 0);
const frames = all
.slice(2) // join inside frame 0
.map((p) => d.push(p))
.filter((f): f is Buffer => f !== null);
expect(firstEnd).toBeGreaterThan(2);
expect(frames).toHaveLength(1);
expect(frames[0]!.equals(expected[1]!)).toBe(true);
});
it("bounds a runaway frame", () => {
const d = new JpegDepacketizer();
const big = 60_000;
let seq = 1;
let offset = 0;
let out: Buffer | null = null;
while (offset <= MAX_FRAME_BYTES + big) {
const pkt = Buffer.alloc(12 + 8 + big);
pkt[0] = 0x80;
pkt[1] = 26;
pkt.writeUInt16BE(seq++ & 0xffff, 2);
pkt.writeUIntBE(offset, 13, 3);
pkt[16] = 1; // type
pkt[17] = 50; // Q
pkt[18] = 80; // 640/8
pkt[19] = 45; // 360/8
out = d.push(pkt) ?? out;
offset += big;
}
expect(out).toBeNull();
expect(d.dropped).toBe(1);
});
});
describe("rtpPayload", () => {
it("strips RTP padding", () => {
const pkt = Buffer.from([0xa0, 0x9a, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0xaa, 0xbb, 0, 0, 3]);
const r = rtpPayload(pkt)!;
expect(r.marker).toBe(true);
expect([...r.payload]).toEqual([0xaa, 0xbb]);
});
});
describe("tablesForQ", () => {
it("Q 50 is the unscaled standard table", () => {
const t = tablesForQ(50);
expect(t.length).toBe(128);
expect(t[0]).toBe(16);
expect(t[64]).toBe(17);
});
});