Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions server/src/http/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ r.post("/api/cameras/:id/ptz", "user", async (ctx) => {
const t = ctx.app.onvifTarget(cam);
if (!t) throw new HttpError(400, "PTZ is not available for this camera");
const b = await readJson<{ action?: string; pan?: number; tilt?: number; zoom?: number; preset?: string }>(ctx.req);
// Before the command goes out, so the first frames of the move are already ignored by motion detection.
if (b.action === "move" || b.action === "stop" || b.action === "preset") ctx.app.motion.ptzActivity(cam.id, b.action);
try {
if (b.action === "move") await ctx.app.onvif.move(t, Number(b.pan) || 0, Number(b.tilt) || 0, Number(b.zoom) || 0);
else if (b.action === "stop") await ctx.app.onvif.stop(t);
Expand Down
37 changes: 37 additions & 0 deletions server/src/motion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,37 @@ export type MotionCallbacks = {

type Det = { sup: Supervisor; tracker: MotionTracker; key: string; cam: CameraRecord };

export type PtzAction = "move" | "stop" | "preset";

/**
* Keeps motion detection quiet while a camera is being moved over PTZ: the whole picture changes because the camera
* turns, not because something happened, and every pan used to raise a motion event and a push notification.
* A move is quiet until the matching stop (capped, in case the stop never arrives), then for a settle time so the last
* step and the video delay can pass. A preset is quiet for a fixed time because there is no stop for it.
*/
export class PtzQuiet {
private moveUntil = 0;
private settleUntil = 0;
private presetUntil = 0;

constructor(private o: { settleMs: number; moveCapMs: number; presetMs: number } = { settleMs: 5000, moveCapMs: 60_000, presetMs: 15_000 }) {}

ptz(action: PtzAction, now = Date.now()): void {
if (action === "move") this.moveUntil = now + this.o.moveCapMs;
else if (action === "stop") {
this.moveUntil = 0;
this.settleUntil = now + this.o.settleMs;
} else this.presetUntil = now + this.o.presetMs;
}

muted(now = Date.now()): boolean {
return now < Math.max(this.moveUntil, this.settleUntil, this.presetUntil);
}
}

export class MotionDetector {
private dets = new Map<string, Det>();
private quiet = new Map<string, PtzQuiet>();
private timer?: ReturnType<typeof setInterval>;

constructor(
Expand All @@ -129,6 +158,13 @@ export class MotionDetector {
}, 1000);
}

/** Called for every PTZ command sent to a camera; motion is ignored while it moves (see PtzQuiet). */
ptzActivity(cameraId: string, action: PtzAction): void {
let q = this.quiet.get(cameraId);
if (!q) this.quiet.set(cameraId, (q = new PtzQuiet()));
q.ptz(action);
}

active(cameraId: string): boolean {
return !!this.dets.get(cameraId)?.tracker.inEvent;
}
Expand Down Expand Up @@ -183,6 +219,7 @@ export class MotionDetector {
onStderr: (line) => {
const score = parseSceneScore(line);
if (score === undefined || Date.now() - startedAt < 5000) return;
if (this.quiet.get(cam.id)?.muted()) return;
for (const ev of tracker.feed(Date.now(), score)) this.dispatch(det, ev);
},
onExit: () => {
Expand Down
28 changes: 27 additions & 1 deletion server/test/motion.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, test } from "bun:test";
import { MotionTracker, parseSceneScore, segmentDecidable, sensitivityToThreshold, shouldKeepSegment, buildMotionArgs } from "../src/motion.ts";
import { MotionTracker, PtzQuiet, parseSceneScore, segmentDecidable, sensitivityToThreshold, shouldKeepSegment, buildMotionArgs } from "../src/motion.ts";

describe("motion", () => {
test("sensitivity maps to decreasing thresholds", () => {
Expand Down Expand Up @@ -57,3 +57,29 @@ describe("motion", () => {
expect(buildMotionArgs("ffmpeg", "x", true)).toContain("nokey");
});
});

describe("ptz quiet", () => {
const o = { settleMs: 5000, moveCapMs: 60_000, presetMs: 15_000 };
test("quiet while moving and for the settle time after stop", () => {
const q = new PtzQuiet(o);
expect(q.muted(0)).toBe(false);
q.ptz("move", 1000);
expect(q.muted(30_000)).toBe(true);
q.ptz("stop", 31_000);
expect(q.muted(35_999)).toBe(true);
expect(q.muted(36_000)).toBe(false);
});
test("a move without stop is capped", () => {
const q = new PtzQuiet(o);
q.ptz("move", 0);
expect(q.muted(59_999)).toBe(true);
expect(q.muted(60_000)).toBe(false);
});
test("presets are quiet for a fixed time and a stop does not cut them short", () => {
const q = new PtzQuiet(o);
q.ptz("preset", 0);
q.ptz("stop", 1000);
expect(q.muted(14_999)).toBe(true);
expect(q.muted(15_000)).toBe(false);
});
});