From c9fdfd9702ff572992cbe377fd751c1fbdcbe816 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 29 Nov 2026 18:53:54 +0000 Subject: [PATCH] ONVIF AbsoluteMove: send the speed in a named speed space, at its maximum AbsoluteMove sent with no space attribute. A device then reads the value in its own default speed space (the one of the PTZConfiguration DefaultPTZSpeed), where 1 isn't necessarily the maximum. On such cameras the ABS moves were slower than the preset moves. - pOnvif::ptz_GetNode() now parses SupportedPTZSpaces and keeps the PanTiltSpeedSpace / ZoomSpeedSpace to use: the generic one if the node lists it, otherwise the first one with its own XRange. Without GetNode it falls back to GenericSpeedSpace / ZoomGenericSpeedSpace (0..1). The debug printf of the whole reply is removed. - ptz_AbsoluteMove() sends the speed with the space attribute, at the space's maximum (optional speed argument 0..1 of the range). - protocolPTZ_ONVIF::create() reads the node's speed spaces once and logs which speed is used. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16 --- lib/data/ponvif/ponvif.cpp | 109 +++++++++++++++++++----- lib/data/ponvif/ponvif.h | 35 +++++++- lib/data/protocol/protocolPTZ_ONVIF.cpp | 9 ++ 3 files changed, 131 insertions(+), 22 deletions(-) diff --git a/lib/data/ponvif/ponvif.cpp b/lib/data/ponvif/ponvif.cpp index 865c674a..56132f3d 100644 --- a/lib/data/ponvif/ponvif.cpp +++ b/lib/data/ponvif/ponvif.cpp @@ -132,6 +132,7 @@ pOnvif::pOnvif() m_bDiscoveryHideDuplicates = true; m_pHttpAccessManager = NULL; memset( &m_sCapabilities, 0, sizeof( nodeCapability ) ); + setGenericSpeedSpaces(); m_bAuthMode = 0; parser_done = false; m_pPendingRequest = NULL; @@ -2050,32 +2051,94 @@ bool pOnvif::ptz_GetPresets(CSTR profileToken) return false; } +void pOnvif::setGenericSpeedSpaces(void) +{ + memset(&m_sPanTiltSpeedSpace, 0, sizeof(m_sPanTiltSpeedSpace)); + memset(&m_sZoomSpeedSpace, 0, sizeof(m_sZoomSpeedSpace)); + safeCopy(m_sPanTiltSpeedSpace.uri, sizeof(m_sPanTiltSpeedSpace.uri), PONVIF_SPACE_PT_GENERIC_SPEED); + safeCopy(m_sZoomSpeedSpace.uri, sizeof(m_sZoomSpeedSpace.uri), PONVIF_SPACE_ZOOM_GENERIC_SPEED); + m_sPanTiltSpeedSpace.max = m_sZoomSpeedSpace.max = 1.0f; +} + +/* + Picks the speed space to use out of the node's / list: + the generic one if the node has it, otherwise the first one (with its own range). +*/ +static bool pickSpeedSpace(XMLNode & spaces, LPCTSTR name, LPCTSTR genericUri, ptzSpeedSpace * out) +{ + // nChildNode(name) compares the prefixed name ("tt:..."), getChildNode(name, i) ignores the prefix + int tPick = -1; + for (int i = 0;; i++) + { + XMLNode tSpace = spaces.getChildNode(name, i); + if (tSpace.isEmpty()) + { + break; + }; + LPCTSTR tUri = tSpace.getChildNode("URI", 0).getText(0); + if (tUri == NULL) + { + continue; + }; + if (tPick < 0 || strcmp(tUri, genericUri) == 0) + { + tPick = i; + }; + if (strcmp(tUri, genericUri) == 0) + { + break; + }; + }; + if (tPick < 0) + { + return false; + }; + XMLNode tSpace = spaces.getChildNode(name, tPick); + XMLNode tRange = tSpace.getChildNode("XRange", 0); + LPCTSTR tMin = tRange.getChildNode("Min", 0).getText(0); + LPCTSTR tMax = tRange.getChildNode("Max", 0).getText(0); + float fMax = (tMax != NULL) ? (float) atof(tMax) : 0.0f; + if (fMax <= 0.0f) + { + return false; // no usable range: keep the generic space + }; + memset(out, 0, sizeof(ptzSpeedSpace)); + safeCopy(out->uri, sizeof(out->uri), tSpace.getChildNode("URI", 0).getText(0)); + out->min = (tMin != NULL) ? (float) atof(tMin) : 0.0f; + out->max = fMax; + out->valid = true; + return true; +} + bool pOnvif::ptz_GetNode(CSTR ptzNodeToken) { - if (m_pPTZURI == NULL) + if (m_pPTZURI == NULL || ptzNodeToken == NULL || ptzNodeToken[0] == 0) { return false; } char tBuffer[DEFAULT_BUFFER_SIZE]; _buildRequest(tBuffer, sizeof(tBuffer), PONVIF_BODY_GET_PTZ_NODE, xesc(ptzNodeToken).c_str()); - if (_send_request(m_pPTZURI, tBuffer)) + if (! _send_request(m_pPTZURI, tBuffer, true)) { - XMLResults tParseResults; - XMLNode response = XMLNode::parseString(m_pLastResponse, "root", &tParseResults); - LOG("RCPT:"); - printf("%s\n", m_pLastResponse); - if (tParseResults.error) - { - char tBuf[CTEXT]; - sprintf(tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn); - LOG_ERROR(tBuf); - } - else - { - // return $response['Envelope']['Body']['GetNodeResponse']; - }; + return false; }; - return false; + XMLResults tParseResults; + XMLNode response = XMLNode::parseString(m_pLastResponse, "root", &tParseResults); + if (tParseResults.error) + { + char tBuf[CTEXT]; + sprintf(tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn); + LOG_ERROR(tBuf); + return false; + }; + XMLNode tSpaces = response.getChildNode("Body", 0).getChildNode("GetNodeResponse", 0).getChildNode("PTZNode", 0).getChildNode("SupportedPTZSpaces", 0); + if (tSpaces.isEmpty()) + { + return false; + }; + bool tPT = pickSpeedSpace(tSpaces, "PanTiltSpeedSpace", PONVIF_SPACE_PT_GENERIC_SPEED, &m_sPanTiltSpeedSpace); + bool tZM = pickSpeedSpace(tSpaces, "ZoomSpeedSpace", PONVIF_SPACE_ZOOM_GENERIC_SPEED, &m_sZoomSpeedSpace); + return tPT || tZM; } bool pOnvif::ptz_GotoPreset(CSTR profileToken, CSTR presetToken, float speed_pantilt_x, float speed_pantilt_y, float speed_zoom_x) @@ -2133,14 +2196,20 @@ bool pOnvif::ptz_RelativeMoveZoom(CSTR profileToken, float zoom, float speedZoom return _send_request(m_pPTZURI, tBuffer, true); } -bool pOnvif::ptz_AbsoluteMove(CSTR profileToken, float position_pantilt_x, float position_pantilt_y, float zoom) +bool pOnvif::ptz_AbsoluteMove(CSTR profileToken, float position_pantilt_x, float position_pantilt_y, float zoom, float speed) { if (m_pPTZURI == NULL) { return false; } - char tBuffer[DEFAULT_BUFFER_SIZE]; - _buildRequest(tBuffer, sizeof(tBuffer), PONVIF_BODY_ABSOLUTE_MOVE, xesc(profileToken).c_str(), position_pantilt_x, position_pantilt_y, zoom); + // The Speed is sent with its space: without it the device uses its default speed space, where + // x="1" is not necessarily the maximal speed (the moves were slower than the preset moves). + // The generic pan/tilt speed is a scalar (x); y is sent with the same value for devices reading it. + float tPT = (speed < 0.0f) ? m_sPanTiltSpeedSpace.max : m_sPanTiltSpeedSpace.min + speed * (m_sPanTiltSpeedSpace.max - m_sPanTiltSpeedSpace.min); + float tZM = (speed < 0.0f) ? m_sZoomSpeedSpace.max : m_sZoomSpeedSpace.min + speed * (m_sZoomSpeedSpace.max - m_sZoomSpeedSpace.min); + char tBuffer[DEFAULT_BUFFER_SIZE]; + _buildRequest(tBuffer, sizeof(tBuffer), PONVIF_BODY_ABSOLUTE_MOVE, xesc(profileToken).c_str(), position_pantilt_x, position_pantilt_y, zoom, tPT, tPT, + xesc(m_sPanTiltSpeedSpace.uri).c_str(), tZM, xesc(m_sZoomSpeedSpace.uri).c_str()); // Wait for the answer so that a SOAP Fault (e.g. position out of range) is detected return _send_request(m_pPTZURI, tBuffer, true); } diff --git a/lib/data/ponvif/ponvif.h b/lib/data/ponvif/ponvif.h index 91e569ce..37f706f6 100644 --- a/lib/data/ponvif/ponvif.h +++ b/lib/data/ponvif/ponvif.h @@ -72,7 +72,14 @@ #define PONVIF_BODY_ABSOLUTE_MOVE \ "%s" + "\"/>" +// Generic speed spaces (ONVIF PTZ spec: every node with configurable speed provides them, range 0..1). +// The speed of an AbsoluteMove is sent in these spaces (or in the node's own speed space, see ptzSpeedSpace). +// Without a space attribute, a device interprets the value in its own default speed space (the one of +// the PTZConfiguration's DefaultPTZSpeed): there "1" can be a slow speed. +#define PONVIF_SPACE_PT_GENERIC_SPEED "http://www.onvif.org/ver10/tptz/PanTiltSpaces/GenericSpeedSpace" +#define PONVIF_SPACE_ZOOM_GENERIC_SPEED "http://www.onvif.org/ver10/tptz/ZoomSpaces/ZoomGenericSpeedSpace" // ContinuousMove is assembled from these parts (pan/tilt and zoom in one request) #define PONVIF_BODY_CONTINUOUS_MOVE_BEGIN "%s" #define PONVIF_BODY_CONTINUOUS_MOVE_PT \ @@ -114,6 +121,15 @@ typedef struct S_videoSourceProfile char ptznodetoken[CTEXT]; // PTZConfiguration/NodeToken } videoSourceProfile; +// A speed space of the PTZ node (GetNode -> SupportedPTZSpaces/PanTiltSpeedSpace | ZoomSpeedSpace) +typedef struct S_ptzSpeedSpace +{ + char uri[CTEXT]; + float min; + float max; + bool valid; // read from the device +} ptzSpeedSpace; + // Result of the last request (pOnvif::getLastError) typedef enum E_onvifError { @@ -503,6 +519,7 @@ class pOnvif { private: void cleanNodeCapability( nodeCapability *ptr ); + void setGenericSpeedSpaces(void); curlRequestsManager * m_pHttpAccessManager; basicMutex m_mResponseProtector; CURL * m_pCURL; @@ -541,6 +558,8 @@ class pOnvif volatile long m_nLastHttpCode; // HTTP status of the last reply (set by parser()) onvifError m_eLastError; char m_cLastFault[CTEXT]; + ptzSpeedSpace m_sPanTiltSpeedSpace; // speed space used for AbsoluteMove (generic 0..1 by default) + ptzSpeedSpace m_sZoomSpeedSpace; #ifndef CURL_MANAGER static int _writer(char * data, size_t size, size_t nmemb, pOnvif * Ptr); #endif @@ -602,7 +621,18 @@ class pOnvif CSTR media_GetSnapshotUri(CSTR profileToken); bool ptz_GetPresets(CSTR profileToken); + // Reads the node's speed spaces (used by ptz_AbsoluteMove: maximal speed). False if not read bool ptz_GetNode(CSTR ptzNodeToken); + + const ptzSpeedSpace & getPanTiltSpeedSpace(void) const + { + return m_sPanTiltSpeedSpace; + } + + const ptzSpeedSpace & getZoomSpeedSpace(void) const + { + return m_sZoomSpeedSpace; + } bool ptz_GetStatus(CSTR profileToken, ptzStatus * tStatus); // PTZ service capabilities (ONVIF PTZ GetServiceCapabilities). // Each output is 1 = supported, 0 = not supported, -1 = not reported by the device. @@ -612,7 +642,8 @@ class pOnvif bool ptz_SetPreset(CSTR profileToken, CSTR presetName); bool ptz_RelativeMove(CSTR profileToken, float translation_pantilt_x, float translation_pantilt_y, float speed_pantilt_x, float speed_pantilt_y); bool ptz_RelativeMoveZoom(CSTR profileToken, float zoom, float speedZoom); - bool ptz_AbsoluteMove(CSTR profileToken, float position_pantilt_x, float position_pantilt_y, float zoom); + // Moves at the maximal speed of the speed space (speed < 0, default) or at speed (0..1 of that range) + bool ptz_AbsoluteMove(CSTR profileToken, float position_pantilt_x, float position_pantilt_y, float zoom, float speed = -1.0f); bool ptz_ContinuousMove(CSTR profileToken, float velocity_pantilt_x, float velocity_pantilt_y); bool ptz_ContinuousMoveZoom(CSTR profileToken, float zoom); // One ContinuousMove for pan/tilt and zoom; axes with has* == false are left out of the request diff --git a/lib/data/protocol/protocolPTZ_ONVIF.cpp b/lib/data/protocol/protocolPTZ_ONVIF.cpp index 047ec916..6e9e1b67 100644 --- a/lib/data/protocol/protocolPTZ_ONVIF.cpp +++ b/lib/data/protocol/protocolPTZ_ONVIF.cpp @@ -217,6 +217,15 @@ bool protocolPTZ_ONVIF::create(void) LOG(tB); } detectStatusSupport(); + // Speed space of the node: AbsoluteMove at the maximal speed of it (generic 0..1 if not read) + { + bool tRead = m_pOnVif->ptz_GetNode(m_pProfile->ptznodetoken); + const ptzSpeedSpace & tPT = m_pOnVif->getPanTiltSpeedSpace(); + const ptzSpeedSpace & tZM = m_pOnVif->getZoomSpeedSpace(); + char tB[CTEXT * 4]; + snprintf(tB, sizeof(tB), "PTZ speed (%s): PanTilt %.120s max %.3f, Zoom %.120s max %.3f", tRead ? "from node" : "generic default", tPT.uri, tPT.max, tZM.uri, tZM.max); + LOG(tB); + } LOG("DONE"); m_bStatus = 1; return true; -- 2.39.5