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); }); });