From 5cab4abc0f4dc8224d6698c8292903cfb600c929 Mon Sep 17 00:00:00 2001 From: Hyeong Seok <41688671+hyngsk@users.noreply.github.com> Date: Tue, 24 Nov 2026 18:38:27 +0900 Subject: [PATCH] Ignore motion while the camera is moved over PTZ Turning a PTZ camera changes the whole picture, so every pan or tilt from the app raised a motion event and a push notification, and in motion recording mode kept the segment. The PTZ endpoint now tells the motion detector about each command before sending it. PtzQuiet keeps the camera quiet: - move: until the matching stop, capped at 60 s in case the stop is lost - stop: 5 s more, for the last step and the video delay - preset: 15 s, since a preset has no stop Scene scores that arrive while quiet are dropped before they reach the tracker; an event that was already running ends on its usual quiet timeout. --- server/src/http/api.ts | 2 ++ server/src/motion.ts | 37 +++++++++++++++++++++++++++++++++++++ server/test/motion.test.ts | 28 +++++++++++++++++++++++++++- 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/server/src/http/api.ts b/server/src/http/api.ts index aa26494..6708534 100644 --- a/server/src/http/api.ts +++ b/server/src/http/api.ts @@ -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); diff --git a/server/src/motion.ts b/server/src/motion.ts index 71e2c13..c590299 100644 --- a/server/src/motion.ts +++ b/server/src/motion.ts @@ -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(); + private quiet = new Map(); private timer?: ReturnType; constructor( @@ -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; } @@ -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: () => { diff --git a/server/test/motion.test.ts b/server/test/motion.test.ts index 1f4be79..9e640eb 100644 --- a/server/test/motion.test.ts +++ b/server/test/motion.test.ts @@ -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", () => { @@ -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); + }); +});