From ac79e51d17afd626f54540ff7beeee9a298614cd Mon Sep 17 00:00:00 2001 From: Tuan Nghia Nguyen Date: Wed, 8 Jul 2026 10:42:31 +1000 Subject: [PATCH] ANSCV test: camera-dead watchdog with ReconnectRTSP (production parity) Overnight soak revealed the gap: ANSRTSPClient does not auto-reconnect by design (the application layer polls staleness and calls ReconnectRTSP - ANSVIS/LabVIEW does this in production), but the unit test never did, so when the laptop's Modern Standby killed the TCP session at ~1 AM the client returned the cached frame until morning. Poll GetRTSPLastFrameAgeMs each loop; at 20 s without a decoded frame, call ReconnectRTSP (timestamped console line), retrying at most every 15 s. Threshold sits above the corruption keyframe gate's ~12 s worst-case hold so a reconnect never preempts an in-flight recovery. Co-Authored-By: Claude Fable 5 --- tests/ANSCV-UnitTest/ANSCV-UnitTest.cpp | 27 ++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/tests/ANSCV-UnitTest/ANSCV-UnitTest.cpp b/tests/ANSCV-UnitTest/ANSCV-UnitTest.cpp index 39897f8..512116d 100644 --- a/tests/ANSCV-UnitTest/ANSCV-UnitTest.cpp +++ b/tests/ANSCV-UnitTest/ANSCV-UnitTest.cpp @@ -877,7 +877,32 @@ int RSTPTestCVClient() { //if ((index == 2000) || (index == 2300) || (index == 2700)) { StartRTSP(&rtspClient); } //if (index > 20000) break; auto loopStart = std::chrono::steady_clock::now(); - auto getStart = loopStart; + + // ── Camera-dead watchdog (mirrors ANSVIS production behaviour) ────── + // ANSRTSPClient does NOT auto-reconnect by design: the application + // layer polls frame age and calls ReconnectRTSP. Without this, a dead + // session (camera reboot, network drop, laptop standby killing the TCP + // connection) leaves the client returning the cached last frame + // forever. Threshold is 20 s: the decoder's corruption keyframe gate + // can legitimately hold delivery for up to ~12 s (300-frame safety + // valve) during heavy packet loss, and a reconnect mid-gate would + // throw away a recovery that was about to complete. Retry at most + // every 15 s so a camera that is genuinely off doesn't get hammered. + { + static auto lastReconnectAttempt = std::chrono::steady_clock::now(); + const double frameAgeMs = GetRTSPLastFrameAgeMs(&rtspClient); + if (frameAgeMs >= 20000.0 && + loopStart - lastReconnectAttempt > std::chrono::seconds(15)) { + lastReconnectAttempt = loopStart; + const std::time_t t = std::time(nullptr); + char ts[32]; std::strftime(ts, sizeof ts, "%Y-%m-%d %H:%M:%S", std::localtime(&t)); + printf("[%s] [WATCHDOG] no decoded frame for %.0f s -- calling ReconnectRTSP\n", + ts, frameAgeMs / 1000.0); + ReconnectRTSP(&rtspClient); + } + } + + auto getStart = std::chrono::steady_clock::now(); cv::Mat* image = nullptr; // ✅ Use a pointer to hold the allocated image GetRTSPCVImage(&rtspClient, width, height, pts,&image); auto getEnd = std::chrono::steady_clock::now();