Author SHA1 Message Date
David Ventura f59a2c1ad0 implement rotation via injecting exif headers 2024-05-01 23:36:48 +02:00
David Ventura 58c58e38ac increase delay & send LanSearchExt 2024-05-01 16:57:43 +02:00
David Ventura b5d4ab6024 set noEmit flag on tsc 2024-05-01 13:26:56 +02:00
7 changed files with 91 additions and 17 deletions
+8 -8
View File
@@ -3,8 +3,10 @@
<link rel="shortcut icon" href="/favicon.ico">
<title>${name}</title>
</head><body>
<h2>${name}</h2><a href="/camera/${id}"><img src="/camera/${id}"/></a><hr/>
<h2>${name}</h2><a href="/camera/${id}"><img style="height: 640px" src="/camera/${id}"/></a><hr/>
<button onclick="toggle_audio()" id=audio disabled=true>Audio: disabled</button>
<button onclick="fetch('/rotate/${id}')">Rotate</button>
<button onclick="fetch('/mirror/${id}')">Mirror</button>
<script>
const alaw_to_s16_table = [
-5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736, -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784, -2752,
@@ -49,13 +51,11 @@
audio_context.resume();
const evtSource = new EventSource("/audio/${id}");
if (${debug}) {
evtSource.onopen = (e) => {
console.log("evtsource open");
}
evtSource.onerror = (e) => {
console.log("evtsource error", e);
}
evtSource.onopen = (e) => {
console.log("evtsource open");
}
evtSource.onerror = (e) => {
console.log("evtsource error", e);
}
let endsAt = 0;
let startAt = 0;
+11 -4
View File
@@ -1,5 +1,5 @@
import EventEmitter from "node:events";
import { create_LanSearch, parse_PunchPkt } from "./impl.js";
import { create_LanSearchExt, create_LanSearch, parse_PunchPkt } from "./impl.js";
import { createSocket, RemoteInfo } from "node:dgram";
import { Commands } from "./datatypes.js";
import { logger } from "./logger.js";
@@ -31,11 +31,18 @@ export const discoverDevices = (discovery_ip: string): EventEmitter => {
sock.setBroadcast(true);
logger.info(`Searching for devices on ${discovery_ip}`);
let buf = create_LanSearch();
let ls_buf = create_LanSearch();
let lse_buf = create_LanSearchExt();
setInterval(() => {
sock.send(new Uint8Array(buf.buffer), SEND_PORT, discovery_ip);
logger.log("trace", `>> LanSearch`);
sock.send(new Uint8Array(ls_buf.buffer), SEND_PORT, discovery_ip);
logger.log("trace", `>> LanSearchExt`);
sock.send(new Uint8Array(lse_buf.buffer), SEND_PORT, discovery_ip);
}, 2000);
sock.send(new Uint8Array(buf.buffer), SEND_PORT, discovery_ip);
logger.log("trace", `>> LanSearch`);
sock.send(new Uint8Array(ls_buf.buffer), SEND_PORT, discovery_ip);
logger.log("trace", `>> LanSearchExt`);
sock.send(new Uint8Array(lse_buf.buffer), SEND_PORT, discovery_ip);
});
sock.bind();
+28
View File
@@ -0,0 +1,28 @@
// Create a minimal EXIF segment with orientation
export const createExifOrientation = (orientation: number) => {
const tiffHeader = Buffer.from("49492A0008000000", "hex");
const ifdEntry = Buffer.concat([
Buffer.from("0100", "hex"), // Number of IFD entries
Buffer.from("1201030001000000", "hex"), // Tag, Type, Count
Buffer.from(orientation.toString(16).padStart(2, "0"), "hex"), // Orientation value
Buffer.from("0000", "hex"), // No more IFDs
Buffer.from("0000000000", "hex"), // padding??
]);
const exifData = Buffer.concat([Buffer.from("457869660000", "hex"), tiffHeader, ifdEntry]);
const segmentLength = Buffer.from([(exifData.length + 2) >> 8, (exifData.length + 2) & 0xff]);
const exifHeader = Buffer.concat([Buffer.from("FFE1", "hex"), segmentLength]);
return Buffer.concat([exifHeader, exifData]);
};
export const addExifToJpeg = (jpegData: Buffer, exifSegment: Buffer) => {
// Check for existing EXIF (simplified check)
if (jpegData.includes(Buffer.from("FFE1", "hex"))) {
throw new Error("JPEG already contains EXIF segment");
}
const soiEnd = 2; // After FFD8
const modifiedJpeg = Buffer.concat([jpegData.subarray(0, soiEnd), exifSegment, jpegData.subarray(soiEnd)]);
return modifiedJpeg;
};
+34 -3
View File
@@ -7,6 +7,7 @@ import { opt } from "./options.js";
import { discoverDevices } from "./discovery.js";
import { DevSerial, SendDevStatus } from "./impl.js";
import { Handlers, makeSession, Session, startVideoStream } from "./session.js";
import { addExifToJpeg, createExifOrientation } from "./exif.js";
// @ts-expect-error TS2307
import favicon from "./cam.ico.gz";
@@ -21,6 +22,15 @@ const sessions: Record<string, Session> = {};
// Text file containing the mapping of camera names.
const nameFile = "cameras.txt";
// https://sirv.com/help/articles/rotate-photos-to-be-upright/
const oMap = [1, 8, 3, 6];
const oMapMirror = [2, 7, 4, 5];
const orientations = [1, 2, 3, 4, 5, 6, 7, 8].reduce((acc, cur) => {
return { [cur]: createExifOrientation(cur), ...acc };
}, {});
let camSettings: { [key: string]: { orientation: number; mirror: boolean } } = {};
// Reads the mapping of serial numbers to camera names from the text file.
const cameraNames = Object.assign(
{},
@@ -90,7 +100,23 @@ export const serveHttp = (opts: opt, port: number, with_audio: boolean) => {
return;
}
if (req.url.startsWith("/camera/")) {
if (req.url.startsWith("/rotate/")) {
let devId = req.url.split("/")[2];
let curPos = camSettings[devId]?.orientation || 0;
let nextPos = (curPos + 1) % 4;
logger.info(`Rotating ${devId} to ${nextPos}`);
camSettings[devId].orientation = nextPos;
res.writeHead(204);
res.end();
return;
} else if (req.url.startsWith("/mirror/")) {
let devId = req.url.split("/")[2];
logger.info(`Mirroring ${devId}`);
camSettings[devId].mirror = !camSettings[devId].mirror;
res.writeHead(204);
res.end();
return;
} else if (req.url.startsWith("/camera/")) {
let devId = req.url.split("/")[2];
logger.info(`Video stream requested for camera ${devId}`);
let s = sessions[devId];
@@ -151,8 +177,12 @@ export const serveHttp = (opts: opt, port: number, with_audio: boolean) => {
const header = Buffer.from(`--${BOUNDARY}\r\nContent-Type: image/jpeg\r\n\r\n`);
s.eventEmitter.on("frame", () => {
const assembled = Buffer.concat(s.curImage);
// Add an EXIF header to indicate if the image should be rotated or mirrored
let orientation = camSettings[dev.devId].orientation;
orientation = camSettings[dev.devId].mirror ? oMapMirror[orientation] : oMap[orientation];
const exifSegment = orientations[orientation];
const jpegHeader = addExifToJpeg(s.curImage[0], exifSegment);
const assembled = Buffer.concat([jpegHeader, ...s.curImage.slice(1)]);
responses[dev.devId].forEach((res) => {
res.write(header);
res.write(assembled);
@@ -175,6 +205,7 @@ export const serveHttp = (opts: opt, port: number, with_audio: boolean) => {
});
}
sessions[dev.devId] = s;
camSettings[dev.devId] = { orientation: 0, mirror: false };
});
logger.info(`Starting HTTP server on port ${port}`);
+7
View File
@@ -155,6 +155,13 @@ export const SendUsrChk = (session: Session, username: string, password: string)
return makeDataReadWrite(session, ControlCommands.ConnectUser, cmd_payload);
};
export const create_LanSearchExt = (): DataView => {
const outbuf = new DataView(new Uint8Array(4).buffer);
outbuf.writeU16(Commands.LanSearchExt);
outbuf.add(2).writeU16(0x0);
return outbuf;
};
export const create_LanSearch = (): DataView => {
const outbuf = new DataView(new Uint8Array(4).buffer);
outbuf.writeU16(Commands.LanSearch);
+2 -1
View File
@@ -69,7 +69,8 @@ export const makeSession = (
session.started = true;
};
if (options.slow_startup) {
setTimeout(startup, 3000);
logger.info("Delaying session startup");
setTimeout(startup, 6500);
} else {
startup();
}
+1 -1
View File
@@ -7,7 +7,7 @@
"moduleResolution": "node",
"esModuleInterop": true,
"preserveConstEnums": true,
"outDir": "./build/",
"noEmit": true,
"rootDir": "./",
"strict": false,
"sourceMap": false