1 Commits
Author SHA1 Message Date
David Ventura 58b795a86a maybe support encoded cameras 2024-07-10 12:35:52 +02:00
7 changed files with 96 additions and 8 deletions
+1 -4
View File
@@ -57,7 +57,7 @@ yargs(hideBin(process.argv))
(yargs) => { (yargs) => {
return yargs return yargs
.option("log_level", { describe: "Set log level", default: "info" }) .option("log_level", { describe: "Set log level", default: "info" })
.option("discovery_ip", { describe: "Camera discovery IP address" }) .option("discovery_ip", { describe: "Camera discovery IP address", default: "192.168.1.255" })
.option("ssid", { describe: "Wifi network for the camera to connect to" }) .option("ssid", { describe: "Wifi network for the camera to connect to" })
.option("password", { describe: "Wifi network password" }) .option("password", { describe: "Wifi network password" })
.demandOption(["ssid", "password"]) .demandOption(["ssid", "password"])
@@ -68,9 +68,6 @@ yargs(hideBin(process.argv))
if (majorVersion < 16) { if (majorVersion < 16) {
logger.error(`Node version ${majorVersion} is not supported, may malfunction`); logger.error(`Node version ${majorVersion} is not supported, may malfunction`);
} }
if (argv.discovery_ip !== undefined) {
config.discovery_ips = [argv.discovery_ip];
}
pair({ ssid: argv.ssid, password: argv.password }); pair({ ssid: argv.ssid, password: argv.password });
}, },
) )
+41
View File
@@ -0,0 +1,41 @@
const KEY_TABLE = new Uint8Array([
0x7c, 0x9c, 0xe8, 0x4a, 0x13, 0xde, 0xdc, 0xb2, 0x2f, 0x21, 0x23, 0xe4, 0x30, 0x7b, 0x3d, 0x8c, 0xbc, 0x0b, 0x27,
0x0c, 0x3c, 0xf7, 0x9a, 0xe7, 0x08, 0x71, 0x96, 0x00, 0x97, 0x85, 0xef, 0xc1, 0x1f, 0xc4, 0xdb, 0xa1, 0xc2, 0xeb,
0xd9, 0x01, 0xfa, 0xba, 0x3b, 0x05, 0xb8, 0x15, 0x87, 0x83, 0x28, 0x72, 0xd1, 0x8b, 0x5a, 0xd6, 0xda, 0x93, 0x58,
0xfe, 0xaa, 0xcc, 0x6e, 0x1b, 0xf0, 0xa3, 0x88, 0xab, 0x43, 0xc0, 0x0d, 0x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e, 0x17,
]);
const ENC_KEY = new Uint8Array([0x69, 0x97, 0xcc, 0x19]);
export const decode = (dv: DataView): DataView => {
let prevByte = 0;
let buf = new Uint8Array(dv.byteLength);
for (let i = 0; i < dv.byteLength; i++) {
const index = (ENC_KEY[prevByte & 0x03] + prevByte) & 0xff;
const origByte = dv.getUint8(i);
buf[i] = origByte ^ KEY_TABLE[index];
prevByte = origByte;
}
return new DataView(buf.buffer);
};
export const encode = (dv: DataView): DataView => {
let prevByte = 0;
let buf = new Uint8Array(dv.byteLength);
for (let i = 0; i < dv.byteLength; i++) {
const index = (ENC_KEY[prevByte & 0x03] + prevByte) & 0xff;
buf[i] = dv.getUint8(i) ^ KEY_TABLE[index];
prevByte = buf[i];
}
return new DataView(buf.buffer);
};
+2 -1
View File
@@ -255,6 +255,7 @@ const deal_with_data = (session: Session, dv: DataView) => {
// retransmit // retransmit
return; return;
} }
if (session.curImage.length == 0) return; // missed the start-of-frame packet
let b = Buffer.from(data.buffer); let b = Buffer.from(data.buffer);
@@ -269,7 +270,7 @@ const deal_with_data = (session: Session, dv: DataView) => {
return; return;
} }
if (session.curImage.length <= 1) return; // header does not have markers if (session.curImage.length == 1) return; // header does not have markers
let lastFrameSlice = session.curImage[session.curImage.length - 1]; let lastFrameSlice = session.curImage[session.curImage.length - 1];
const lastResetMarker = findAllResetMarkers(lastFrameSlice).pop(); const lastResetMarker = findAllResetMarkers(lastFrameSlice).pop();
+2 -1
View File
@@ -155,6 +155,8 @@ export const serveHttp = (port: number) => {
sessions[dev.devId] = s; sessions[dev.devId] = s;
config.cameras[dev.devId] = { rotate: 0, mirror: false, audio: true, ...(config.cameras[dev.devId] || {}) }; config.cameras[dev.devId] = { rotate: 0, mirror: false, audio: true, ...(config.cameras[dev.devId] || {}) };
const header = Buffer.from(`--${BOUNDARY}\r\nContent-Type: image/jpeg\r\n\r\n`);
s.eventEmitter.on("frame", () => { s.eventEmitter.on("frame", () => {
// Add an EXIF header to indicate if the image should be rotated or mirrored // Add an EXIF header to indicate if the image should be rotated or mirrored
let orientation = config.cameras[dev.devId].rotate; let orientation = config.cameras[dev.devId].rotate;
@@ -162,7 +164,6 @@ export const serveHttp = (port: number) => {
const exifSegment = orientations[orientation]; const exifSegment = orientations[orientation];
const jpegHeader = addExifToJpeg(s.curImage[0], exifSegment); const jpegHeader = addExifToJpeg(s.curImage[0], exifSegment);
const assembled = Buffer.concat([jpegHeader, ...s.curImage.slice(1)]); const assembled = Buffer.concat([jpegHeader, ...s.curImage.slice(1)]);
const header = Buffer.from(`\r\n--${BOUNDARY}\r\nContent-Length: ${assembled.length}\r\nContent-Type: image/jpeg\r\n\r\n`);
responses[dev.devId].forEach((res) => { responses[dev.devId].forEach((res) => {
res.write(header); res.write(header);
res.write(assembled); res.write(assembled);
+1 -1
View File
@@ -1,5 +1,5 @@
{ {
"name": "cam-reverse", "name": "cam",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
+16 -1
View File
@@ -1,6 +1,7 @@
import { createSocket, RemoteInfo } from "node:dgram"; import { createSocket, RemoteInfo } from "node:dgram";
import EventEmitter from "node:events"; import EventEmitter from "node:events";
import { decode, encode } from "./encode.js";
import { Commands, CommandsByValue } from "./datatypes.js"; import { Commands, CommandsByValue } from "./datatypes.js";
import { import {
handle_Close, handle_Close,
@@ -32,6 +33,7 @@ export type Session = {
frame_is_bad: boolean; frame_is_bad: boolean;
frame_was_fixed: boolean; frame_was_fixed: boolean;
started: boolean; started: boolean;
encoded: boolean;
close: () => void; close: () => void;
}; };
@@ -46,7 +48,16 @@ type msgCb = (
const handleIncoming: msgCb = (session, handlers, msg, rinfo) => { const handleIncoming: msgCb = (session, handlers, msg, rinfo) => {
const ab = new Uint8Array(msg).buffer; const ab = new Uint8Array(msg).buffer;
const dv = new DataView(ab); let dv = new DataView(ab);
let firstByte = dv.readU8();
if (firstByte != 0xf1) {
dv = decode(dv);
firstByte = dv.readU8();
if (firstByte == 0xf1) {
// decoded into a legal command
session.encoded = true;
}
}
const raw = dv.readU16(); const raw = dv.readU16();
const cmd = CommandsByValue[raw]; const cmd = CommandsByValue[raw];
logger.log("trace", `<< ${cmd}`); logger.log("trace", `<< ${cmd}`);
@@ -128,6 +139,9 @@ export const makeSession = (
unackedDrw[packet_id] = { sent_ts: Date.now(), data: msg }; unackedDrw[packet_id] = { sent_ts: Date.now(), data: msg };
} }
logger.log("trace", `>> ${cmd}`); logger.log("trace", `>> ${cmd}`);
if (session.encoded) {
msg = encode(msg);
}
sock.send(new Uint8Array(msg.buffer), ra.port, session.dst_ip); sock.send(new Uint8Array(msg.buffer), ra.port, session.dst_ip);
}, },
ackDrw: (id: number) => { ackDrw: (id: number) => {
@@ -139,6 +153,7 @@ export const makeSession = (
rcvSeqId: 0, rcvSeqId: 0,
frame_is_bad: false, frame_is_bad: false,
frame_was_fixed: false, frame_was_fixed: false,
encoded: false,
unackedDrw, unackedDrw,
close: () => { close: () => {
session.eventEmitter.emit("disconnect"); session.eventEmitter.emit("disconnect");
+33
View File
@@ -0,0 +1,33 @@
import assert from "assert";
import { buildLogger } from "../logger.js";
import { decode, encode } from "../encode.js";
const hstrToU8 = (hs) => new Uint8Array(hs.match(/../g).map((h) => parseInt(h, 16)));
describe("encode/decode", () => {
it("decodes example", () => {
buildLogger("trace");
const pkt = "2ccb6293bf2321ed0ad7ea318106e0d5a28d800233207369";
const expected = "f141001444474f4100000000000e3f854e44424e44000000";
const pktbuf = hstrToU8(pkt);
const expbuf = hstrToU8(expected);
assert.deepEqual(decode(new DataView(pktbuf.buffer)), new DataView(expbuf.buffer));
});
it("encodes example", () => {
buildLogger("trace");
const pkt = "f141001444474f4100000000000e3f854e44424e44000000";
const expected = "2ccb6293bf2321ed0ad7ea318106e0d5a28d800233207369";
const pktbuf = hstrToU8(pkt);
const expbuf = hstrToU8(expected);
assert.deepEqual(encode(new DataView(pktbuf.buffer)), new DataView(expbuf.buffer));
});
it("roundtrips encode/decode", () => {
buildLogger("trace");
const pkt = "f141001444474f4100000000000e3f854e44424e44000000";
const pktbuf = hstrToU8(pkt);
const encoded = encode(new DataView(pktbuf.buffer));
assert.deepEqual(decode(encoded), new DataView(pktbuf.buffer));
});
});