From a7c5e76e543c4a850c765a46e2a008f7d03a0f7d Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 2 Dec 2026 13:11:30 +0000 Subject: [PATCH] Camera: re-create only the PTZ protocol on ONVIF trouble, keep the node and its stream A pOnvif problem could delete and re-create the whole camera node, which dropped the video stream bound to it, so recordings were cut although the video was fine. deviceNodeCamera::validate() compared the whole camera record with memcmp; any difference rebuilt the node: - CAM_WS_INIT sets m_sCamData.online = false before re-creating the PTZ protocol and writes the DB only afterwards. A validate() during a slow ONVIF re-init saw "online" differ and deleted the camera mid-re-init. - The ONVIF discovery (updateIPCam) wrote online=1 and the whole XAddrs list into onvif_ctrl (the web UI and la_mojo store only the path), so the record changed on discovery rounds. - A failed read (SQLite busy, queries fail at once) returned NULL = "camera deleted". Now: - Field classes (camDataChange): - runtime and discovery fields (online, status, session, max_range, uuid, mac): ignored; - name and hardware: taken over; - PTZ access (onvif_ctrl, onvif_access, port, auth_mode, ptz_token): the camera thread re-creates only the PTZ protocol, keeping the node and the stream; - anything else (ip and credentials used by the stream, stream_id, type, geometry, ...): rebuild as before, logged with the reason. - A lasting PTZ problem rebuilds the camera only after CAM_REBUILD_AFTER_FAILED_INITS (10) failed protocol initialisations in a row (several minutes with the retry back-off). - Node / camera record not read: rebuild only after NODE_DATA_MISSES_TO_REBUILD (3) validations in a row, for all nodes (deviceNode::validate). - updateIPCam: onvif_ctrl = path of the first XAddr. Host and port come from the first XAddr only: ':' was searched in the whole list, so a second, IPv6 XAddr gave a broken IP. A result without an address doesn't wipe the IP. Tested (functions extracted unchanged): camDataChange classifies online / status / max_range / uuid / mac as ignored, rename as cosmetic, port / onvif_ctrl / auth / ptz token as PTZ re-init, and ip / password / stream / geometry as rebuild. The XAddrs parser was checked with a port, without a port, with an IPv6 second XAddr, with a query, and with no address. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16 --- lib/App/common/SQLite_dataBinder.cpp | 75 ++++++++------ lib/App/common/deviceNode.cpp | 7 ++ lib/App/common/deviceNode.h | 7 ++ lib/App/common/deviceNodeCamera.cpp | 149 ++++++++++++++++++++++++++- lib/App/common/deviceNodeCamera.h | 13 +++ 5 files changed, 214 insertions(+), 37 deletions(-) diff --git a/lib/App/common/SQLite_dataBinder.cpp b/lib/App/common/SQLite_dataBinder.cpp index 5cd83b28..f65fec04 100644 --- a/lib/App/common/SQLite_dataBinder.cpp +++ b/lib/App/common/SQLite_dataBinder.cpp @@ -5779,56 +5779,63 @@ bool sqliteDBC::updateIPCam(uint32_t CamID, CSTR Addr) { if (m_bStoIsConnected) { + // onvif_ctrl = path of the device service only (as la_mojo's own discovery INSERT and the web UI + // store it; la_mojo builds http://ip[:port]onvif_ctrl). The whole XAddrs list was stored: the record + // changed on every discovery round and the camera node was rebuilt (its stream dropped). + char tPath[256] = "/onvif/device_service"; + { + const char * tH = strstr(Addr, "//"); + if (tH != NULL) + { + const char * tP = strchr(tH + 2, '/'); + if (tP != NULL) + { + size_t tLen = strcspn(tP, " \t\r\n"); // first XAddr only + if (tLen > 0 && tLen < sizeof(tPath) && memchr(tP, '\'', tLen) == NULL) + { + memcpy(tPath, tP, tLen); + tPath[tLen] = 0; + }; + }; + }; + } char tQBuf[MAX_PATH_LEN]; char tIpBuf[CTEXT]; - char * tPos; - char * tPos2; uint16_t tPort = 0; - tPos = strstr(Addr, "//"); - if (tPos != NULL) + // host[:port] of the FIRST XAddr only: the old parser searched ':' in the whole list (a second, IPv6 + // XAddr or a ':' in the path gave a broken IP / port) + tIpBuf[0] = (char) 0; { - tPos2 = strstr(tPos + 2, ":"); - if (tPos2 != NULL) + const char * tH = strstr(Addr, "//"); + if (tH != NULL) { - //LOG("IP Port Defined!"); - int len;//, len2, len3; - len = tPos2 - tPos - 2; - strncpy(tIpBuf, tPos + 2, len); - tIpBuf[len] = (char) 0; - tPort = atoi(tPos2 + 1); - // strncpy( tPath, tPos + 2 + len, CTEXT ); - } - else - { - tPos2 = strstr(tPos + 2, "/"); - if (tPos2 != NULL) - { - int len;//, len2, len3; - len = tPos2 - tPos - 2; - strncpy(tIpBuf, tPos + 2, len); - tIpBuf[len] = (char) 0; - // strncpy( tPath, tPos + 2 + len, CTEXT ); - } - else + tH += 2; + size_t tLen = strcspn(tH, "/ \t\r\n"); + if (tLen > 0 && tLen < sizeof(tIpBuf)) { - tPos2 = strstr(tPos + 2, "/"); - //LOG_ERROR("Pos 2 - err"); - tIpBuf[0] = (char) 0; + memcpy(tIpBuf, tH, tLen); + tIpBuf[tLen] = (char) 0; + char * tC = strrchr(tIpBuf, ':'); + if (tC != NULL && strchr(tIpBuf, ']') == NULL) // not [IPv6] + { + *tC = (char) 0; + tPort = (uint16_t) atoi(tC + 1); + }; }; }; } - else + if (tIpBuf[0] == (char) 0 || strchr(tIpBuf, '\'') != NULL) { - //LOG_ERROR("Pos 0 - err"); - tIpBuf[0] = (char) 0; + LOG_ERROR("updateIPCam: no address in the discovery result - camera not updated"); + return false; }; if (tPort) { - sprintf(tQBuf, "UPDATE `cameras` SET `online`=1, `onvif_ctrl`='%s', `ip`='%s', `port`=%d WHERE `id`=%d", Addr, tIpBuf, tPort, CamID); + snprintf(tQBuf, sizeof(tQBuf), "UPDATE `cameras` SET `online`=1, `onvif_ctrl`='%s', `ip`='%s', `port`=%d WHERE `id`=%d", tPath, tIpBuf, tPort, CamID); } else { - sprintf(tQBuf, "UPDATE `cameras` SET `online`=1, `onvif_ctrl`='%s', `ip`='%s' WHERE `id`=%d", Addr, tIpBuf, CamID); + snprintf(tQBuf, sizeof(tQBuf), "UPDATE `cameras` SET `online`=1, `onvif_ctrl`='%s', `ip`='%s' WHERE `id`=%d", tPath, tIpBuf, CamID); } #ifdef DEBUG_ALL LOG(tQBuf); diff --git a/lib/App/common/deviceNode.cpp b/lib/App/common/deviceNode.cpp index 15b69109..0beeb2dc 100644 --- a/lib/App/common/deviceNode.cpp +++ b/lib/App/common/deviceNode.cpp @@ -24,6 +24,7 @@ deviceNode::deviceNode(uint32_t id) : basicEngine() { m_pLog = basicLogCollection::getInstance(); + m_nNodeDataMisses = 0; #ifdef USE_MYSQL m_pDBC = mysqlDBC::getInstance(); #else @@ -58,6 +59,7 @@ deviceNode::deviceNode(DBC_NODE_DATA * Data) : basicEngine() { m_pLog = basicLogCollection::getInstance(); + m_nNodeDataMisses = 0; #ifdef USE_MYSQL m_pDBC = mysqlDBC::getInstance(); #else @@ -86,8 +88,13 @@ deviceNode::~deviceNode() bool deviceNode::validate() { DBC_NODE_DATA * tDATA = m_pDBC->getNodeData(m_sNodeData.node_id); + if (tDATA == NULL && ++m_nNodeDataMisses < NODE_DATA_MISSES_TO_REBUILD) + { + return true; // probably a failed read, not a deleted node: check again next time + }; if (tDATA) { + m_nNodeDataMisses = 0; // Configuration only: "online" is written by every node object of this id (setNodeOnline / // setNodeOffline) - deleting a second object of the same node made this one "changed" bool tChanged = (m_sNodeData.aux_id != tDATA->aux_id) || (m_sNodeData.aux_parameter != tDATA->aux_parameter) || (m_sNodeData.type != tDATA->type) || diff --git a/lib/App/common/deviceNode.h b/lib/App/common/deviceNode.h index fe56732d..80eab5ca 100644 --- a/lib/App/common/deviceNode.h +++ b/lib/App/common/deviceNode.h @@ -5,6 +5,12 @@ #include "../../components/base/basicEvent.h" #include "cmdNodeConveyor.h" +// The node record must be missing this many validations in a row before the node is deleted: a single failed +// read (SQLite busy - queries fail at once, no busy wait) deleted the node (a camera dropped its stream) +#ifndef NODE_DATA_MISSES_TO_REBUILD + #define NODE_DATA_MISSES_TO_REBUILD 3 +#endif + class deviceNode : public cmdNodeConveyor, public basicEngine { private: @@ -13,6 +19,7 @@ class deviceNode : public cmdNodeConveyor, public basicEngine basicLogCollection * m_pLog; dataBinder * m_pDBC; DBC_NODE_DATA m_sNodeData; + uint8_t m_nNodeDataMisses; // validate(): node record not read N times in a row public: diff --git a/lib/App/common/deviceNodeCamera.cpp b/lib/App/common/deviceNodeCamera.cpp index 8a72d09d..63466002 100644 --- a/lib/App/common/deviceNodeCamera.cpp +++ b/lib/App/common/deviceNodeCamera.cpp @@ -50,6 +50,9 @@ deviceNodeCamera::deviceNodeCamera(uint32_t NodeID) : m_sPtzHealth.state = PTZ_HEALTH_INIT; m_sPtzHealth.since = time(NULL); m_tPtzLastProbe = 0; + m_bPendingCamData = false; + m_bPendingProtoReinit = false; + memset(&m_sPendingCamData, 0, sizeof(m_sPendingCamData)); m_bNoAbsPositionning = false; m_bCameraOnHomePosition = false; m_bCameraAIControled = false; @@ -88,6 +91,9 @@ deviceNodeCamera::deviceNodeCamera(DBC_NODE_DATA * Data) : m_sPtzHealth.state = PTZ_HEALTH_INIT; m_sPtzHealth.since = time(NULL); m_tPtzLastProbe = 0; + m_bPendingCamData = false; + m_bPendingProtoReinit = false; + memset(&m_sPendingCamData, 0, sizeof(m_sPendingCamData)); m_bNoAbsPositionning = false; m_bCameraOnHomePosition = false; m_tTimeTSeconds = 0; @@ -1256,6 +1262,7 @@ void deviceNodeCamera::m_fThreadProc(void) if ((bNewSecondLoop = (cTimeSeconds != oTimeSeconds))) oTimeSeconds = cTimeSeconds; + applyPendingCamData(); // settings changed in the DB that need no rebuild (validate) uint8_t tState = getWM(); ptzCheck(tState); tState = getWM(); @@ -2880,6 +2887,113 @@ bool deviceNodeCamera::validateStream(void) return true; } +/* + Which camera fields changed (DB record a -> b)? + - runtime state written while running (online, status, session, max_range) and discovery data (uuid, mac): + ignored. Before, any difference rebuilt the node: CAM_WS_INIT set m_sCamData.online = false before + re-creating the PTZ protocol and wrote the DB only afterwards, so a validate() during a slow ONVIF + re-init deleted the camera (and dropped its video stream); so did the ONVIF discovery writing online=1. + - cosmetic (name, hardware): taken over, no action + - PTZ protocol only (onvif_ctrl, onvif_access, port, auth_mode, ptz_token): the protocol is re-created + - anything else (ip, credentials and stream_id used by the stream, type, geometry ...): node rebuilt +*/ +typedef enum E_CAM_DATA_CHANGE +{ + CAM_CHANGE_NONE = 0, + CAM_CHANGE_COSMETIC = 1, + CAM_CHANGE_PROTOCOL = 2, + CAM_CHANGE_NODE = 3 +} CAM_DATA_CHANGE; + +static CAM_DATA_CHANGE camDataChange(const DBC_CAM_DATA * a, const DBC_CAM_DATA * b, const char ** what) +{ + DBC_CAM_DATA tA, tB; + memcpy(&tA, a, sizeof(tA)); + memcpy(&tB, b, sizeof(tB)); + *what = ""; + // runtime / discovery fields: never a change + tA.online = tB.online; + tA.status = tB.status; + memcpy(tA.session, tB.session, sizeof(tA.session)); + tA.max_range = tB.max_range; + memcpy(tA.uuid, tB.uuid, sizeof(tA.uuid)); + memcpy(tA.mac, tB.mac, sizeof(tA.mac)); + // PTZ protocol fields + bool tProto = (tA.port != tB.port) || (tA.auth_mode != tB.auth_mode) || strncmp(tA.onvif_ctrl, tB.onvif_ctrl, sizeof(tA.onvif_ctrl)) != 0 || + strncmp(tA.onvif_access, tB.onvif_access, sizeof(tA.onvif_access)) != 0 || strncmp(tA.ptz_token, tB.ptz_token, sizeof(tA.ptz_token)) != 0; + tA.port = tB.port; + tA.auth_mode = tB.auth_mode; + memcpy(tA.onvif_ctrl, tB.onvif_ctrl, sizeof(tA.onvif_ctrl)); + memcpy(tA.onvif_access, tB.onvif_access, sizeof(tA.onvif_access)); + memcpy(tA.ptz_token, tB.ptz_token, sizeof(tA.ptz_token)); + // cosmetic fields + bool tCosm = strncmp(tA.name, tB.name, sizeof(tA.name)) != 0 || strncmp(tA.hardware, tB.hardware, sizeof(tA.hardware)) != 0; + memcpy(tA.name, tB.name, sizeof(tA.name)); + memcpy(tA.hardware, tB.hardware, sizeof(tA.hardware)); + // everything else (also fields added later) needs the node rebuilt + if (memcmp(&tA, &tB, sizeof(tA)) != 0) + { + if (strncmp(a->ip, b->ip, sizeof(a->ip)) != 0) + { + *what = "ip"; + } + else if (strncmp(a->username, b->username, sizeof(a->username)) != 0 || strncmp(a->password, b->password, sizeof(a->password)) != 0) + { + *what = "credentials"; + } + else if (a->stream_id != b->stream_id) + { + *what = "stream"; + } + else if (a->protocol != b->protocol) + { + *what = "control protocol"; + } + else + { + *what = "camera parameters"; + }; + return CAM_CHANGE_NODE; + }; + if (tProto) + { + *what = "PTZ access"; + return CAM_CHANGE_PROTOCOL; + }; + return tCosm ? CAM_CHANGE_COSMETIC : CAM_CHANGE_NONE; +} + +// Camera thread: take over the fields validate() found changed (only cosmetic / PTZ protocol fields) +void deviceNodeCamera::applyPendingCamData(void) +{ + if (! m_bPendingCamData) + { + return; + }; + m_mPendingCamData.lock(); + DBC_CAM_DATA * tN = &m_sPendingCamData; + memcpy(m_sCamData.name, tN->name, sizeof(m_sCamData.name)); + memcpy(m_sCamData.hardware, tN->hardware, sizeof(m_sCamData.hardware)); + m_sCamData.port = tN->port; + m_sCamData.auth_mode = tN->auth_mode; + memcpy(m_sCamData.onvif_ctrl, tN->onvif_ctrl, sizeof(m_sCamData.onvif_ctrl)); + memcpy(m_sCamData.onvif_access, tN->onvif_access, sizeof(m_sCamData.onvif_access)); + memcpy(m_sCamData.ptz_token, tN->ptz_token, sizeof(m_sCamData.ptz_token)); + bool tReinit = m_bPendingProtoReinit; + m_bPendingCamData = false; + m_bPendingProtoReinit = false; + m_mPendingCamData.unlock(); + if (tReinit && m_sCamData.protocol != 0) + { + LOG("PTZ access settings changed - re-initialising the PTZ protocol (camera and stream kept)"); + m_mPtzHealth.lock(); + m_sPtzHealth.next_retry = 0; // new settings: no back-off + m_sPtzHealth.failed_inits = 0; + m_mPtzHealth.unlock(); + setWM(CAM_WS_INIT); + }; +} + bool deviceNodeCamera::validate() { if (m_pDBC == NULL) @@ -2889,16 +3003,45 @@ bool deviceNodeCamera::validate() DBC_CAM_DATA * tDATA = m_pDBC->getCamData(m_sCamData.id); if (tDATA == NULL) { + // Probably a failed read (SQLite busy), not a deleted camera: rebuild only when it stays missing + if (++m_nNodeDataMisses < NODE_DATA_MISSES_TO_REBUILD) + { + return true; + }; + LOG_ERROR("Camera record not found - rebuilding the camera"); return false; } - if (memcmp(tDATA, &m_sCamData, sizeof(DBC_CAM_DATA)) != 0) + const char * tWhat = ""; + CAM_DATA_CHANGE tChange = camDataChange(&m_sCamData, tDATA, &tWhat); + if (tChange == CAM_CHANGE_NODE) { - // Settings Changed - // memcpy(&m_sCamData, tDATA, sizeof(DBC_CAM_DATA) ); + char tB[128]; + snprintf(tB, sizeof(tB), "Camera settings changed (%s) - rebuilding the camera", tWhat); + LOG(tB); free(tDATA); return false; }; + if (tChange != CAM_CHANGE_NONE) + { + m_mPendingCamData.lock(); + memcpy(&m_sPendingCamData, tDATA, sizeof(DBC_CAM_DATA)); + m_bPendingProtoReinit = m_bPendingProtoReinit || (tChange == CAM_CHANGE_PROTOCOL); + m_bPendingCamData = true; + m_mPendingCamData.unlock(); + }; free(tDATA); + // Only a real, lasting PTZ problem rebuilds the camera (and drops its stream) + 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; + m_mPtzHealth.unlock(); + if (tGiveUp) + { + char tB[128]; + snprintf(tB, sizeof(tB), "PTZ protocol failed %u initialisations in a row - rebuilding the camera", tFails); + LOG_ERROR(tB); + return false; + }; if (! validateStream()) { return false; diff --git a/lib/App/common/deviceNodeCamera.h b/lib/App/common/deviceNodeCamera.h index 256d0370..e2d48191 100644 --- a/lib/App/common/deviceNodeCamera.h +++ b/lib/App/common/deviceNodeCamera.h @@ -130,6 +130,11 @@ 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 +#ifndef CAM_REBUILD_AFTER_FAILED_INITS + #define CAM_REBUILD_AFTER_FAILED_INITS 10 +#endif typedef struct S_CAM_TASK { @@ -207,6 +212,14 @@ class deviceNodeCamera : public deviceNode, public basicThread void ptzCheck(uint8_t tState); void ptzLost(const char * reason); void ptzCommandFailed(const char * command); + + // Camera settings changed in the DB (validate, node factory thread) that need only a PTZ protocol + // re-initialisation or no action: applied by the camera thread (it owns m_sCamData) + basicMutex m_mPendingCamData; + DBC_CAM_DATA m_sPendingCamData; + volatile bool m_bPendingCamData; + volatile bool m_bPendingProtoReinit; + void applyPendingCamData(void); // basicTransport * m_pTransport; CAM_TASK_QUEUE m_qCAM_TASK;