From b536a43c4c74794f6cd9d8262a43a28300d2d467 Mon Sep 17 00:00:00 2001 From: Hyeong Seok <41688671+hyngsk@users.noreply.github.com> Date: Tue, 24 Nov 2026 18:17:04 +0900 Subject: [PATCH] Hide zoom buttons for PTZ cameras without zoom Pan/tilt-only cameras (fixed lens) still showed zoom in/out next to the PTZ pad in the web UI and the app; the buttons did nothing. The ONVIF probe now also asks the PTZ service for GetNodes and records whether the node lists a zoom movement space (Absolute/Relative/ ContinuousZoom...). ZoomSpeedSpace alone does not count. The result is stored as caps.ptzZoom and returned as capabilities.ptzZoom. The web UI and the app hide the zoom buttons only when ptzZoom is explicitly false. If GetNodes fails or carries no SupportedPTZSpaces, the value stays undefined and the buttons are shown as before, so cameras that do zoom keep working. --- app/src/components/ptz-pad.tsx | 20 +++++++++++--------- app/src/lib/types.ts | 2 +- server/src/app.ts | 5 +++-- server/src/cameras.ts | 3 ++- server/src/onvif.ts | 23 ++++++++++++++++++++++- server/test/misc.test.ts | 21 ++++++++++++++++++++- server/web/src/components/LiveViewer.tsx | 14 ++++++++------ server/web/src/types.ts | 2 +- 8 files changed, 68 insertions(+), 22 deletions(-) diff --git a/app/src/components/ptz-pad.tsx b/app/src/components/ptz-pad.tsx index bf6714c..e7aea1d 100644 --- a/app/src/components/ptz-pad.tsx +++ b/app/src/components/ptz-pad.tsx @@ -83,15 +83,17 @@ export function PtzPad({ api, camera }: { api: ServerApi; camera: Camera }) { - - zoom(1)} onPressOut={stop} style={styles.zoomBtn} accessibilityLabel={t('live.zoomIn')}> - - - - zoom(-1)} onPressOut={stop} style={styles.zoomBtn} accessibilityLabel={t('live.zoomOut')}> - - - + {camera.capabilities.ptzZoom !== false ? ( + + zoom(1)} onPressOut={stop} style={styles.zoomBtn} accessibilityLabel={t('live.zoomIn')}> + + + + zoom(-1)} onPressOut={stop} style={styles.zoomBtn} accessibilityLabel={t('live.zoomOut')}> + + + + ) : null} {presets.length ? ( diff --git a/app/src/lib/types.ts b/app/src/lib/types.ts index dfece07..ecf550d 100644 --- a/app/src/lib/types.ts +++ b/app/src/lib/types.ts @@ -28,7 +28,7 @@ export type Camera = { source: { kind: SourceKind; url: string; subUrl?: string }; recording: { mode: RecordingMode; useSubstream: boolean }; motion: { enabled: boolean; sensitivity: number; notify: boolean }; - capabilities: { audio: boolean; twoWayAudio: boolean; ptz: boolean; substream: boolean; ptzPresets?: PtzPreset[] }; + capabilities: { audio: boolean; twoWayAudio: boolean; ptz: boolean; substream: boolean; ptzPresets?: PtzPreset[]; ptzZoom?: boolean }; status: { online: boolean; recording: boolean; diff --git a/server/src/app.ts b/server/src/app.ts index 79c2840..a8b02ad 100644 --- a/server/src/app.ts +++ b/server/src/app.ts @@ -390,8 +390,9 @@ export class App { const ptz = await this.onvif.hasPtz(t); let presets: { id: string; name: string }[] = []; if (ptz) presets = (await this.onvif.presets(t).catch(() => [])).map((p) => ({ id: p.token, name: p.name })); - if (ptz !== !!cam.caps.ptz || JSON.stringify(presets) !== JSON.stringify(cam.caps.ptzPresets ?? [])) { - this.cameras.setCaps(cam.id, { ptz, ptzPresets: presets }); + const zoom = ptz ? await this.onvif.hasZoom(t) : undefined; + if (ptz !== !!cam.caps.ptz || zoom !== cam.caps.ptzZoom || JSON.stringify(presets) !== JSON.stringify(cam.caps.ptzPresets ?? [])) { + this.cameras.setCaps(cam.id, { ptz, ptzPresets: presets, ptzZoom: zoom }); this.emitCamera(cam.id); } } catch (e) { diff --git a/server/src/cameras.ts b/server/src/cameras.ts index e81a1be..3a25116 100644 --- a/server/src/cameras.ts +++ b/server/src/cameras.ts @@ -16,7 +16,7 @@ export type CameraStatus = { error?: string; }; -export type CameraCaps = { audio: boolean; twoWayAudio: boolean; ptz: boolean; substream: boolean; ptzPresets?: { id: string; name: string }[] }; +export type CameraCaps = { audio: boolean; twoWayAudio: boolean; ptz: boolean; substream: boolean; ptzPresets?: { id: string; name: string }[]; ptzZoom?: boolean }; export type CameraRow = { id: string; @@ -344,6 +344,7 @@ export function cameraJson(c: CameraRecord, status: CameraStatus, pushBase?: { r ptz: !!c.caps.ptz, substream: !!c.subUrl, ptzPresets: c.caps.ptz ? c.caps.ptzPresets ?? [] : undefined, + ptzZoom: c.caps.ptz ? c.caps.ptzZoom : undefined, }, status, createdAt: toIso(c.createdAt), diff --git a/server/src/onvif.ts b/server/src/onvif.ts index 092c8bb..4a9d333 100644 --- a/server/src/onvif.ts +++ b/server/src/onvif.ts @@ -1,7 +1,7 @@ import { createHash, randomBytes } from "node:crypto"; export type OnvifTarget = { host: string; port: number; username: string; password: string }; -type OnvifInfo = { ptzUrl?: string; mediaUrl?: string; profile?: string; offsetMs: number; at: number }; +type OnvifInfo = { ptzUrl?: string; mediaUrl?: string; profile?: string; zoom?: boolean; offsetMs: number; at: number }; const NS = { env: "http://www.w3.org/2003/05/soap-envelope", @@ -64,6 +64,17 @@ async function soap(url: string, body: string, auth?: { username: string; passwo return text; } +/** + * Whether a GetNodes response advertises zoom movement. `undefined` when the nodes carry no SupportedPTZSpaces, + * so callers can keep the zoom controls for cameras that do not describe themselves. + * ZoomSpeedSpace alone does not count: it only sets the speed for other moves. + */ +export function zoomFromNodes(xml: string): boolean | undefined { + const spaces = tagValues(xml, "SupportedPTZSpaces"); + if (!spaces.length) return undefined; + return spaces.some((s) => /<(?:[\w-]+:)?(?:Absolute|Relative|Continuous)Zoom\w*Space[\s>]/.test(s)); +} + function rewriteHost(xaddr: string, t: OnvifTarget): string { try { const u = new URL(xaddr); @@ -106,10 +117,15 @@ export class Onvif { const prof = await soap(rewriteHost(mediaUrl, t), "", auth); profile = attrValues(prof, "Profiles", "token")[0]; } + let zoom: boolean | undefined; + if (ptzUrl && profile) { + zoom = await soap(rewriteHost(ptzUrl, t), "", auth).then(zoomFromNodes, () => undefined); + } const info: OnvifInfo = { mediaUrl: mediaUrl ? rewriteHost(mediaUrl, t) : undefined, ptzUrl: ptzUrl ? rewriteHost(ptzUrl, t) : undefined, profile, + zoom, offsetMs, at: Date.now(), }; @@ -122,6 +138,11 @@ export class Onvif { return !!(i.ptzUrl && i.profile); } + /** false when the PTZ node lists no zoom spaces, undefined when unknown. */ + async hasZoom(t: OnvifTarget): Promise { + return (await this.info(t)).zoom; + } + private async ptz(t: OnvifTarget): Promise<{ url: string; profile: string; auth: { username: string; password: string; offsetMs: number } }> { const i = await this.info(t); if (!i.ptzUrl || !i.profile) throw new Error("Camera does not support PTZ over ONVIF"); diff --git a/server/test/misc.test.ts b/server/test/misc.test.ts index 49e7cbc..36c2023 100644 --- a/server/test/misc.test.ts +++ b/server/test/misc.test.ts @@ -4,7 +4,7 @@ import { parseDemoUrls } from "../src/config.ts"; import { titleCase, demoSource, demoId } from "../src/demo.ts"; import { unzipEntries } from "../src/deps.ts"; import { dayBounds } from "../src/recordings.ts"; -import { wsseHeader, tagValues, attrValues } from "../src/onvif.ts"; +import { wsseHeader, tagValues, attrValues, zoomFromNodes } from "../src/onvif.ts"; import { parseProbeMatch, scopeValue, guessBrand } from "../src/discovery.ts"; import { rcloneToken } from "../src/storage/gdrive.ts"; import { redact } from "../src/log.ts"; @@ -138,3 +138,22 @@ describe("misc", () => { expect(matchParts(["s", ":site", "*"], ["s", "a", "api", "x"])).toEqual({ site: "a", "*": "api/x" }); }); }); + +describe("onvif zoom detection", () => { + const node = (spaces: string) => + `${spaces}`; + const pt = "u"; + test("pan/tilt only means no zoom", () => { + expect(zoomFromNodes(node(pt))).toBe(false); + }); + test("a zoom movement space means zoom", () => { + expect(zoomFromNodes(node(pt + "u"))).toBe(true); + expect(zoomFromNodes(node(pt + "u"))).toBe(true); + }); + test("zoom speed alone is not zoom", () => { + expect(zoomFromNodes(node(pt + "u"))).toBe(false); + }); + test("unknown when spaces are missing", () => { + expect(zoomFromNodes("")).toBeUndefined(); + }); +}); diff --git a/server/web/src/components/LiveViewer.tsx b/server/web/src/components/LiveViewer.tsx index 06435d7..484b371 100644 --- a/server/web/src/components/LiveViewer.tsx +++ b/server/web/src/components/LiveViewer.tsx @@ -115,13 +115,13 @@ export function LiveViewer({ )} - {camera.capabilities.ptz && camera.status.online && } + {camera.capabilities.ptz && camera.status.online && } ); } -function PtzPad({ cameraId }: { cameraId: string }) { +function PtzPad({ cameraId, zoom }: { cameraId: string; zoom: boolean }) { const moving = useRef(false); const start = (pan: number, tilt: number, zoom: number) => (e: RPointerEvent) => { e.preventDefault(); @@ -160,10 +160,12 @@ function PtzPad({ cameraId }: { cameraId: string }) { {btn('Pan right', 0.6, 0, 0, , 'ptz-right')} {btn('Tilt down', 0, -0.6, 0, , 'ptz-down')} -
- {btn('Zoom in', 0, 0, 0.6, , '')} - {btn('Zoom out', 0, 0, -0.6, , '')} -
+ {zoom && ( +
+ {btn('Zoom in', 0, 0, 0.6, , '')} + {btn('Zoom out', 0, 0, -0.6, , '')} +
+ )} ); } diff --git a/server/web/src/types.ts b/server/web/src/types.ts index e35e354..c719e22 100644 --- a/server/web/src/types.ts +++ b/server/web/src/types.ts @@ -70,7 +70,7 @@ export interface Camera { source: { kind: SourceKind | string; url: string; subUrl?: string }; recording: { mode: RecordingMode; useSubstream: boolean }; motion: { enabled: boolean; sensitivity: number; notify: boolean }; - capabilities: { audio: boolean; twoWayAudio: boolean; ptz: boolean; substream: boolean }; + capabilities: { audio: boolean; twoWayAudio: boolean; ptz: boolean; substream: boolean; ptzZoom?: boolean }; status: { online: boolean; recording: boolean;