From 87a11e66797e6799dd043d931055e9c60dbfa7b7 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 2 Dec 2026 14:32:20 +0000 Subject: [PATCH] Camera PTZ recovery: no full re-initialisation; a major problem rebuilds the node Field log: after a PTZ liveness failure the protocol was re-created (camera and stream kept), but the camera then ran its whole start-up sequence again (power mode, WB, BLC, AF, home) in the middle of the work. - A successful re-creation of a protocol that worked before in this node (a recovery) goes straight to CAM_WS_READY: "PTZ protocol re-created - camera settings kept, back to ready". The first initialisation of the node still runs the full sequence. - CAM_REBUILD_AFTER_FAILED_INITS 10 -> 3: three failed re-creations in a row (back-off 0, 5, 10 s) are a major problem (camera power supply, network). The node is rebuilt and the new node fully initialises the camera: "PTZ protocol re-creation failed 3 times in a row (camera power / network?) - rebuilding the camera". - Only for a camera whose protocol worked in this node (m_bPtzEverOk): one that never worked (e.g. switched off) keeps retrying with the back-off instead of being rebuilt every few seconds. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16 --- lib/App/common/deviceNodeCamera.cpp | 29 +++++++++++++++++++++++------ lib/App/common/deviceNodeCamera.h | 13 ++++++++++--- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/lib/App/common/deviceNodeCamera.cpp b/lib/App/common/deviceNodeCamera.cpp index 63466002..5e2a2103 100644 --- a/lib/App/common/deviceNodeCamera.cpp +++ b/lib/App/common/deviceNodeCamera.cpp @@ -52,6 +52,7 @@ deviceNodeCamera::deviceNodeCamera(uint32_t NodeID) : m_tPtzLastProbe = 0; m_bPendingCamData = false; m_bPendingProtoReinit = false; + m_bPtzEverOk = false; memset(&m_sPendingCamData, 0, sizeof(m_sPendingCamData)); m_bNoAbsPositionning = false; m_bCameraOnHomePosition = false; @@ -93,6 +94,7 @@ deviceNodeCamera::deviceNodeCamera(DBC_NODE_DATA * Data) : m_tPtzLastProbe = 0; m_bPendingCamData = false; m_bPendingProtoReinit = false; + m_bPtzEverOk = false; memset(&m_sPendingCamData, 0, sizeof(m_sPendingCamData)); m_bNoAbsPositionning = false; m_bCameraOnHomePosition = false; @@ -1532,7 +1534,20 @@ void deviceNodeCamera::m_fThreadProc(void) LOG_ERROR("No Kernel Instance!"); }; m_sCamData.online = true; - setWM(CAM_WS_POWER_INIT); + m_mPtzHealth.lock(); + bool tRecovery = m_bPtzEverOk && (m_sPtzHealth.state == PTZ_HEALTH_FAILED); + m_mPtzHealth.unlock(); + if (tRecovery) + { + // The camera kept its settings: only the connection was lost - no full initialisation + // (power mode, WB, BLC, AF, home) in the middle of the work + LOG("PTZ protocol re-created - camera settings kept, back to ready"); + setWM(CAM_WS_READY); + } + else + { + setWM(CAM_WS_POWER_INIT); + }; }; m_pDBC->updateCamOnlineStatus(m_sCamData.id, m_sCamData.online); if (m_sCamData.protocol == 0) @@ -1543,6 +1558,7 @@ void deviceNodeCamera::m_fThreadProc(void) { m_tPtzLastProbe = time(NULL); ptzHealthSet(PTZ_HEALTH_OK); + m_bPtzEverOk = true; } else { @@ -3030,15 +3046,16 @@ bool deviceNodeCamera::validate() m_mPendingCamData.unlock(); }; free(tDATA); - // Only a real, lasting PTZ problem rebuilds the camera (and drops its stream) + // A major PTZ problem (the protocol worked, then CAM_REBUILD_AFTER_FAILED_INITS re-creations failed in a + // row: power supply / network) rebuilds the camera - the new node fully initialises it m_mPtzHealth.lock(); - bool tGiveUp = (m_sPtzHealth.state == PTZ_HEALTH_FAILED) && (m_sPtzHealth.failed_inits >= CAM_REBUILD_AFTER_FAILED_INITS); - uint32_t tFails = m_sPtzHealth.failed_inits + 1; + bool tGiveUp = m_bPtzEverOk && (m_sPtzHealth.state == PTZ_HEALTH_FAILED) && (m_sPtzHealth.failed_inits >= CAM_REBUILD_AFTER_FAILED_INITS); + uint32_t tFails = m_sPtzHealth.failed_inits; m_mPtzHealth.unlock(); if (tGiveUp) { - char tB[128]; - snprintf(tB, sizeof(tB), "PTZ protocol failed %u initialisations in a row - rebuilding the camera", tFails); + char tB[160]; + snprintf(tB, sizeof(tB), "PTZ protocol re-creation failed %u times in a row (camera power / network?) - rebuilding the camera", tFails); LOG_ERROR(tB); return false; }; diff --git a/lib/App/common/deviceNodeCamera.h b/lib/App/common/deviceNodeCamera.h index e2d48191..5a0d7860 100644 --- a/lib/App/common/deviceNodeCamera.h +++ b/lib/App/common/deviceNodeCamera.h @@ -130,10 +130,16 @@ typedef struct S_PTZ_HEALTH_INFO #define CAM_PTZ_PROBE_INTERVAL_S 30 // idle liveness check of the PTZ protocol (protocolPTZ::probe) #define CAM_PTZ_RETRY_MIN_S 5 // first re-initialisation delay after a failure #define CAM_PTZ_RETRY_MAX_S 60 // delay doubles per failed attempt up to this -// The camera node (and the video stream bound to it) is rebuilt only after this many failed PTZ protocol -// initialisations in a row (with the retry back-off: several minutes); a PTZ problem re-creates the protocol only +/* + PTZ trouble: + - the protocol is re-created; when it works again within a few attempts the camera goes straight back to + CAM_WS_READY (no power mode / WB / BLC / AF / home again: its settings are unchanged) + - CAM_REBUILD_AFTER_FAILED_INITS failed re-creations in a row (with the back-off 0, 5, 10 s ...) are a major + problem (camera power supply, network): the node is rebuilt and the new node fully initialises the camera. + Only for a camera whose protocol worked in this node - one that never worked keeps retrying (back-off). +*/ #ifndef CAM_REBUILD_AFTER_FAILED_INITS - #define CAM_REBUILD_AFTER_FAILED_INITS 10 + #define CAM_REBUILD_AFTER_FAILED_INITS 3 #endif typedef struct S_CAM_TASK @@ -219,6 +225,7 @@ class deviceNodeCamera : public deviceNode, public basicThread DBC_CAM_DATA m_sPendingCamData; volatile bool m_bPendingCamData; volatile bool m_bPendingProtoReinit; + volatile bool m_bPtzEverOk; // the PTZ protocol worked in this node (re-creation = recovery) void applyPendingCamData(void); // basicTransport * m_pTransport;