From 21674ad9caf4e5247a6b3855a3fbc9c92a7817a6 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 2 Dec 2026 14:58:56 +0000 Subject: [PATCH] ONVIF PTZ debug: log AbsoluteMove coordinates and the camera's position limits - Every AbsoluteMove is logged with pan/tilt/zoom and speed (DEBUG_PONVIF_PTZ in ponvif.h). - A rejected AbsoluteMove is always logged with the coordinates and the fault, followed by the camera ranges. - The node's absolute position spaces (GetNode) and the profile's PTZConfiguration limits and default position spaces (GetProfiles) are read and logged after the protocol is created. - A move outside the known limits is flagged (PAN/TILT/ZOOM) before it is sent; the request is still sent unchanged. - deviceNodeCamera logs the ROI position and the zoom correction before goAbsolute. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16 --- lib/App/common/deviceNodeCamera.cpp | 7 + lib/data/ponvif/ponvif.cpp | 185 +++++++++++++++++++++++- lib/data/ponvif/ponvif.h | 36 +++++ lib/data/protocol/protocolPTZ_ONVIF.cpp | 2 + 4 files changed, 228 insertions(+), 2 deletions(-) diff --git a/lib/App/common/deviceNodeCamera.cpp b/lib/App/common/deviceNodeCamera.cpp index 5e2a2103..bf6c4bb4 100644 --- a/lib/App/common/deviceNodeCamera.cpp +++ b/lib/App/common/deviceNodeCamera.cpp @@ -1929,6 +1929,13 @@ void deviceNodeCamera::m_fThreadProc(void) m_fCP_Tilt = m_pCurrentROI->cam_orientation.tilt; m_fCP_Zoom = m_pCurrentROI->cam_orientation.zoom; // Do Movement + { + char tB[CTEXT * 2]; + snprintf(tB, sizeof(tB), "Go to ROI (%u targets): pan=%0.6f tilt=%0.6f zoom=%0.6f (x zoom correction %0.4f = %0.6f)", (unsigned) m_pCurrentROI->targets.size(), + m_pCurrentROI->cam_orientation.pan, m_pCurrentROI->cam_orientation.tilt, m_pCurrentROI->cam_orientation.zoom, m_fZoomCorrection, + m_pCurrentROI->cam_orientation.zoom * m_fZoomCorrection); + LOG(tB); + } if (! m_pProtocol->goAbsolute(m_pCurrentROI->cam_orientation.pan, m_pCurrentROI->cam_orientation.tilt, m_pCurrentROI->cam_orientation.zoom * m_fZoomCorrection)) { ptzCommandFailed("goAbsolute"); diff --git a/lib/data/ponvif/ponvif.cpp b/lib/data/ponvif/ponvif.cpp index 4c842cb5..ed6b3e91 100644 --- a/lib/data/ponvif/ponvif.cpp +++ b/lib/data/ponvif/ponvif.cpp @@ -1647,6 +1647,8 @@ bool pOnvif::media_GetVideoSources(void) return false; } +static bool readPtzRange(XMLNode & node, ptzRange * out); + bool pOnvif::media_GetProfiles(void) { char tBuffer[DEFAULT_BUFFER_SIZE]; @@ -1737,6 +1739,16 @@ bool pOnvif::media_GetProfiles(void) { tPtr->ptz = true; safeCopy(tPtr->ptznodetoken, sizeof(tPtr->ptznodetoken), nNode.getChildNode("NodeToken", 0).getText(0)); + safeCopy(tPtr->ptzdefaultptspace, sizeof(tPtr->ptzdefaultptspace), nNode.getChildNode("DefaultAbsolutePantTiltPositionSpace", 0).getText(0)); + safeCopy(tPtr->ptzdefaultzoomspace, sizeof(tPtr->ptzdefaultzoomspace), nNode.getChildNode("DefaultAbsoluteZoomPositionSpace", 0).getText(0)); + { + XMLNode tL = nNode.getChildNode("PanTiltLimits", 0).getChildNode("Range", 0); + readPtzRange(tL, &tPtr->ptzpantiltlimits); + } + { + XMLNode tL = nNode.getChildNode("ZoomLimits", 0).getChildNode("Range", 0); + readPtzRange(tL, &tPtr->ptzzoomlimits); + } }; } m_vVideoProfiles.push_back(tPtr); @@ -2059,6 +2071,95 @@ void pOnvif::setGenericSpeedSpaces(void) 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; + memset(&m_sPanTiltPosSpace, 0, sizeof(m_sPanTiltPosSpace)); + memset(&m_sZoomPosSpace, 0, sizeof(m_sZoomPosSpace)); +} + +/* + Reads a <...> element with , and optional (a node's position space + or the Range of a PTZConfiguration's PanTiltLimits / ZoomLimits). +*/ +static bool readPtzRange(XMLNode & node, ptzRange * out) +{ + memset(out, 0, sizeof(ptzRange)); + if (node.isEmpty()) + { + return false; + }; + LPCTSTR tUri = node.getChildNode("URI", 0).getText(0); + if (tUri != NULL) + { + safeCopy(out->uri, sizeof(out->uri), tUri); + }; + XMLNode tX = node.getChildNode("XRange", 0); + LPCTSTR tXMin = tX.getChildNode("Min", 0).getText(0); + LPCTSTR tXMax = tX.getChildNode("Max", 0).getText(0); + if (tXMin == NULL || tXMax == NULL) + { + return false; + }; + out->xmin = (float) atof(tXMin); + out->xmax = (float) atof(tXMax); + XMLNode tY = node.getChildNode("YRange", 0); + LPCTSTR tYMin = tY.getChildNode("Min", 0).getText(0); + LPCTSTR tYMax = tY.getChildNode("Max", 0).getText(0); + if (tYMin != NULL && tYMax != NULL) + { + out->ymin = (float) atof(tYMin); + out->ymax = (float) atof(tYMax); + out->hasY = true; + }; + out->valid = true; + return true; +} + +// The position space of the node: the generic one if offered, otherwise the first one +static void pickPositionSpace(XMLNode & spaces, LPCTSTR name, LPCTSTR genericUri, ptzRange * out) +{ + memset(out, 0, sizeof(ptzRange)); + for (int i = 0;; i++) + { + XMLNode tSpace = spaces.getChildNode(name, i); + if (tSpace.isEmpty()) + { + break; + }; + ptzRange tR; + if (! readPtzRange(tSpace, &tR)) + { + continue; + }; + if (! out->valid || strcmp(tR.uri, genericUri) == 0) + { + *out = tR; + }; + if (strcmp(tR.uri, genericUri) == 0) + { + break; + }; + }; +} + +static void ptzRangeText(char * buf, size_t size, const ptzRange & r) +{ + if (! r.valid) + { + snprintf(buf, size, "n/a"); + } + else if (r.hasY) + { + snprintf(buf, size, "x [%.4f .. %.4f] y [%.4f .. %.4f] (%.100s)", r.xmin, r.xmax, r.ymin, r.ymax, r.uri); + } + else + { + snprintf(buf, size, "[%.4f .. %.4f] (%.100s)", r.xmin, r.xmax, r.uri); + }; +} + +// Small tolerance: values read back from the device are printed with limited precision +static bool ptzInRange(float v, float vmin, float vmax) +{ + return (v >= vmin - 0.0001f) && (v <= vmax + 0.0001f); } /* @@ -2139,6 +2240,8 @@ bool pOnvif::ptz_GetNode(CSTR ptzNodeToken) }; bool tPT = pickSpeedSpace(tSpaces, "PanTiltSpeedSpace", PONVIF_SPACE_PT_GENERIC_SPEED, &m_sPanTiltSpeedSpace); bool tZM = pickSpeedSpace(tSpaces, "ZoomSpeedSpace", PONVIF_SPACE_ZOOM_GENERIC_SPEED, &m_sZoomSpeedSpace); + pickPositionSpace(tSpaces, "AbsolutePanTiltPositionSpace", PONVIF_SPACE_PT_GENERIC_POS, &m_sPanTiltPosSpace); + pickPositionSpace(tSpaces, "AbsoluteZoomPositionSpace", PONVIF_SPACE_ZOOM_GENERIC_POS, &m_sZoomPosSpace); return tPT || tZM; } @@ -2208,11 +2311,89 @@ bool pOnvif::ptz_AbsoluteMove(CSTR profileToken, float position_pantilt_x, float // 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]; + // Limits that apply to this move: the profile's PTZConfiguration limits, else the node's position spaces + const videoSourceProfile * tProfile = NULL; + for (size_t i = 0; i < m_vVideoProfiles.size(); i++) + { + if (m_vVideoProfiles[i] != NULL && strcmp(m_vVideoProfiles[i]->profiletoken, profileToken) == 0) + { + tProfile = m_vVideoProfiles[i]; + break; + }; + }; + const ptzRange & tPTR = (tProfile != NULL && tProfile->ptzpantiltlimits.valid) ? tProfile->ptzpantiltlimits : m_sPanTiltPosSpace; + const ptzRange & tZR = (tProfile != NULL && tProfile->ptzzoomlimits.valid) ? tProfile->ptzzoomlimits : m_sZoomPosSpace; + char tOut[64]; + tOut[0] = (char) 0; + int tLen = 0; + if (tPTR.valid && ! ptzInRange(position_pantilt_x, tPTR.xmin, tPTR.xmax)) + { + tLen += snprintf(tOut + tLen, sizeof(tOut) - tLen, " PAN"); + }; + if (tPTR.valid && tPTR.hasY && ! ptzInRange(position_pantilt_y, tPTR.ymin, tPTR.ymax) && tLen < (int) sizeof(tOut)) + { + tLen += snprintf(tOut + tLen, sizeof(tOut) - tLen, " TILT"); + }; + if (tZR.valid && ! ptzInRange(zoom, tZR.xmin, tZR.xmax) && tLen < (int) sizeof(tOut)) + { + tLen += snprintf(tOut + tLen, sizeof(tOut) - tLen, " ZOOM"); + }; + char tMove[CTEXT * 2]; + snprintf(tMove, sizeof(tMove), "AbsoluteMove pan=%0.6f tilt=%0.6f zoom=%0.6f (speed pt=%0.4f zoom=%0.4f)", position_pantilt_x, position_pantilt_y, zoom, tPT, tZM); + if (tOut[0] != 0) + { + char tB[CTEXT * 4]; + snprintf(tB, sizeof(tB), "%s: OUT OF CAMERA LIMITS:%s", tMove, tOut); + LOG_ERROR(tB); + } +#ifdef DEBUG_PONVIF_PTZ + else + { + LOG(tMove); + }; +#endif + 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); + bool tOk = _send_request(m_pPTZURI, tBuffer, true); + if (! tOk) + { + char tB[CTEXT * 4]; + snprintf(tB, sizeof(tB), "%s REJECTED (%s)", tMove, (m_cLastFault[0] != 0) ? m_cLastFault : "no answer"); + LOG_ERROR(tB); + if (m_eLastError == PONVIF_ERR_FAULT) + { + ptz_LogRanges(profileToken); + }; + }; + return tOk; +} + +void pOnvif::ptz_LogRanges(CSTR profileToken) +{ + char tR1[CTEXT * 2]; + char tR2[CTEXT * 2]; + char tB[CTEXT * 6]; + ptzRangeText(tR1, sizeof(tR1), m_sPanTiltPosSpace); + ptzRangeText(tR2, sizeof(tR2), m_sZoomPosSpace); + snprintf(tB, sizeof(tB), "PTZ node position space: PanTilt %s, Zoom %s", tR1, tR2); + LOG(tB); + for (size_t i = 0; i < m_vVideoProfiles.size(); i++) + { + const videoSourceProfile * tP = m_vVideoProfiles[i]; + if (tP == NULL || ! tP->ptz || (profileToken != NULL && strcmp(tP->profiletoken, profileToken) != 0)) + { + continue; + }; + ptzRangeText(tR1, sizeof(tR1), tP->ptzpantiltlimits); + ptzRangeText(tR2, sizeof(tR2), tP->ptzzoomlimits); + snprintf(tB, sizeof(tB), "PTZ profile %.60s limits: PanTilt %s, Zoom %s", tP->profiletoken, tR1, tR2); + LOG(tB); + snprintf(tB, sizeof(tB), "PTZ profile %.60s default spaces: PanTilt %.120s, Zoom %.120s (the PanTilt position is sent without a space)", tP->profiletoken, + tP->ptzdefaultptspace[0] ? tP->ptzdefaultptspace : "n/a", tP->ptzdefaultzoomspace[0] ? tP->ptzdefaultzoomspace : "n/a"); + LOG(tB); + }; } bool pOnvif::ptz_ContinuousMove(CSTR profileToken, float velocity_pantilt_x, float velocity_pantilt_y) diff --git a/lib/data/ponvif/ponvif.h b/lib/data/ponvif/ponvif.h index 13444944..e64da897 100644 --- a/lib/data/ponvif/ponvif.h +++ b/lib/data/ponvif/ponvif.h @@ -80,6 +80,10 @@ // 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" +#define PONVIF_SPACE_PT_GENERIC_POS "http://www.onvif.org/ver10/tptz/PanTiltSpaces/PositionGenericSpace" +#define PONVIF_SPACE_ZOOM_GENERIC_POS "http://www.onvif.org/ver10/tptz/ZoomSpaces/PositionGenericSpace" +// Log every AbsoluteMove with its coordinates (a rejected move is always logged with them) +#define DEBUG_PONVIF_PTZ // 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 \ @@ -107,6 +111,18 @@ #define PONVIF_PCK_GET_CAPABILITIES (CSTR) "All" +// A position range of the PTZ node or a PTZConfiguration limit (URI + X range, Y range for pan/tilt) +typedef struct S_ptzRange +{ + char uri[CTEXT]; + float xmin; + float xmax; + float ymin; + float ymax; + bool hasY; + bool valid; // read from the device +} ptzRange; + typedef struct S_videoSourceProfile { char sourcetoken[CTEXT]; @@ -119,6 +135,12 @@ typedef struct S_videoSourceProfile int bitrate; bool ptz; // profile has a PTZConfiguration (PTZ commands need such a profile) char ptznodetoken[CTEXT]; // PTZConfiguration/NodeToken + // PTZConfiguration: default absolute position spaces (used when a Position has no space attribute) + // and the configured limits (a move outside them is answered with ter:InvalidPosition) + char ptzdefaultptspace[CTEXT]; + char ptzdefaultzoomspace[CTEXT]; + ptzRange ptzpantiltlimits; + ptzRange ptzzoomlimits; } videoSourceProfile; // A speed space of the PTZ node (GetNode -> SupportedPTZSpaces/PanTiltSpeedSpace | ZoomSpeedSpace) @@ -561,6 +583,8 @@ class pOnvif char m_cLastFault[CTEXT]; ptzSpeedSpace m_sPanTiltSpeedSpace; // speed space used for AbsoluteMove (generic 0..1 by default) ptzSpeedSpace m_sZoomSpeedSpace; + ptzRange m_sPanTiltPosSpace; // node: absolute pan/tilt position space (generic one if offered) + ptzRange m_sZoomPosSpace; // node: absolute zoom position space #ifndef CURL_MANAGER static int _writer(char * data, size_t size, size_t nmemb, pOnvif * Ptr); #endif @@ -634,6 +658,18 @@ class pOnvif { return m_sZoomSpeedSpace; } + + const ptzRange & getPanTiltPositionSpace(void) const + { + return m_sPanTiltPosSpace; + } + + const ptzRange & getZoomPositionSpace(void) const + { + return m_sZoomPosSpace; + } + // Logs the position spaces of the node and the limits of the profile (debug of out-of-bounds moves) + void ptz_LogRanges(CSTR profileToken); 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. diff --git a/lib/data/protocol/protocolPTZ_ONVIF.cpp b/lib/data/protocol/protocolPTZ_ONVIF.cpp index 6e9e1b67..824bd3b3 100644 --- a/lib/data/protocol/protocolPTZ_ONVIF.cpp +++ b/lib/data/protocol/protocolPTZ_ONVIF.cpp @@ -226,6 +226,8 @@ bool protocolPTZ_ONVIF::create(void) 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); } + // Camera ranges, to compare with the coordinates of the AbsoluteMove requests + m_pOnVif->ptz_LogRanges(m_pProfile->profiletoken); LOG("DONE"); m_bStatus = 1; return true;