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
20 changes: 11 additions & 9 deletions app/src/components/ptz-pad.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,17 @@ export function PtzPad({ api, camera }: { api: ServerApi; camera: Camera }) {
<Animated.View style={[styles.knob, knob]} />
</View>
</GestureDetector>
<View style={styles.zoom}>
<Pressable onPressIn={() => zoom(1)} onPressOut={stop} style={styles.zoomBtn} accessibilityLabel={t('live.zoomIn')}>
<Icon name="plus" size={18} color="#fff" />
</Pressable>
<View style={styles.zoomDivider} />
<Pressable onPressIn={() => zoom(-1)} onPressOut={stop} style={styles.zoomBtn} accessibilityLabel={t('live.zoomOut')}>
<Icon name="minus" size={18} color="#fff" />
</Pressable>
</View>
{camera.capabilities.ptzZoom !== false ? (
<View style={styles.zoom}>
<Pressable onPressIn={() => zoom(1)} onPressOut={stop} style={styles.zoomBtn} accessibilityLabel={t('live.zoomIn')}>
<Icon name="plus" size={18} color="#fff" />
</Pressable>
<View style={styles.zoomDivider} />
<Pressable onPressIn={() => zoom(-1)} onPressOut={stop} style={styles.zoomBtn} accessibilityLabel={t('live.zoomOut')}>
<Icon name="minus" size={18} color="#fff" />
</Pressable>
</View>
) : null}
</View>
{presets.length ? (
<ScrollView horizontal showsHorizontalScrollIndicator={false} contentContainerStyle={styles.presets}>
Expand Down
2 changes: 1 addition & 1 deletion app/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion server/src/cameras.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down
23 changes: 22 additions & 1 deletion server/src/onvif.ts
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -106,10 +117,15 @@ export class Onvif {
const prof = await soap(rewriteHost(mediaUrl, t), "<trt:GetProfiles/>", auth);
profile = attrValues(prof, "Profiles", "token")[0];
}
let zoom: boolean | undefined;
if (ptzUrl && profile) {
zoom = await soap(rewriteHost(ptzUrl, t), "<tptz:GetNodes/>", 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(),
};
Expand All @@ -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<boolean | undefined> {
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");
Expand Down
21 changes: 20 additions & 1 deletion server/test/misc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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) =>
`<s:Envelope><s:Body><tptz:GetNodesResponse><tptz:PTZNode token="n"><tt:SupportedPTZSpaces>${spaces}</tt:SupportedPTZSpaces></tptz:PTZNode></tptz:GetNodesResponse></s:Body></s:Envelope>`;
const pt = "<tt:ContinuousPanTiltVelocitySpace><tt:URI>u</tt:URI></tt:ContinuousPanTiltVelocitySpace>";
test("pan/tilt only means no zoom", () => {
expect(zoomFromNodes(node(pt))).toBe(false);
});
test("a zoom movement space means zoom", () => {
expect(zoomFromNodes(node(pt + "<tt:ContinuousZoomVelocitySpace><tt:URI>u</tt:URI></tt:ContinuousZoomVelocitySpace>"))).toBe(true);
expect(zoomFromNodes(node(pt + "<tt:AbsoluteZoomPositionSpace><tt:URI>u</tt:URI></tt:AbsoluteZoomPositionSpace>"))).toBe(true);
});
test("zoom speed alone is not zoom", () => {
expect(zoomFromNodes(node(pt + "<tt:ZoomSpeedSpace><tt:URI>u</tt:URI></tt:ZoomSpeedSpace>"))).toBe(false);
});
test("unknown when spaces are missing", () => {
expect(zoomFromNodes("<s:Envelope><s:Body><tptz:GetNodesResponse/></s:Body></s:Envelope>")).toBeUndefined();
});
});
14 changes: 8 additions & 6 deletions server/web/src/components/LiveViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ export function LiveViewer({
<IconChevronRight size={22} />
</button>
)}
{camera.capabilities.ptz && camera.status.online && <PtzPad cameraId={camera.id} />}
{camera.capabilities.ptz && camera.status.online && <PtzPad cameraId={camera.id} zoom={camera.capabilities.ptzZoom !== false} />}
</div>
</Modal>
);
}

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();
Expand Down Expand Up @@ -160,10 +160,12 @@ function PtzPad({ cameraId }: { cameraId: string }) {
{btn('Pan right', 0.6, 0, 0, <IconArrowUp size={16} style={{ transform: 'rotate(90deg)' }} />, 'ptz-right')}
{btn('Tilt down', 0, -0.6, 0, <IconArrowDown size={16} />, 'ptz-down')}
</div>
<div className="ptz-zoom">
{btn('Zoom in', 0, 0, 0.6, <IconZoomIn size={16} />, '')}
{btn('Zoom out', 0, 0, -0.6, <IconZoomOut size={16} />, '')}
</div>
{zoom && (
<div className="ptz-zoom">
{btn('Zoom in', 0, 0, 0.6, <IconZoomIn size={16} />, '')}
{btn('Zoom out', 0, 0, -0.6, <IconZoomOut size={16} />, '')}
</div>
)}
</div>
);
}
2 changes: 1 addition & 1 deletion server/web/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down