Fix debug

This commit is contained in:
2026-05-07 23:35:18 +10:00
parent 75693cadd9
commit aa323a07a3
4 changed files with 220 additions and 186 deletions
+92 -86
View File
@@ -451,28 +451,31 @@ namespace ANSCENTER {
}
}
// areImagesIdentical: honest image-equality check. Returns true iff the two
// Mats refer to the same pixels. Used by callers who want to know "did I
// just get the same frame back?" — typical pattern after GetImage to skip
// expensive downstream work on rate-limited / buffered duplicates.
//
// For camera-aliveness ("did the decoder go silent?") use
// GetRTSPLastFrameAgeMs() instead — that's a separate concern and was the
// wrong thing to put inside this function before. With the new pts
// discipline (same image -> same pts), most callers can simply compare
// pts instead of calling this function at all.
bool ANSRTSPClient::areImagesIdentical(const cv::Mat& img1, const cv::Mat& img2) {
double ageMs = _playerClient->getLastFrameAgeMs();
if (ageMs > 5000.0) {
ANS_DBG("RTSP_Stale", "FROZEN DETECTED: ageMs=%.1f url=%s playing=%d — camera truly stale",
ageMs, _url.c_str(), (int)_isPlaying);
return true; // Truly stale — no decoder output for 5+ seconds
}
if (ageMs > 0.0) {
return false; // Decoder is receiving frames — camera is alive
}
// ageMs == 0 means no frame has been decoded yet (startup).
// Fall back to pixel comparison for backward compatibility.
if (img1.empty() && img2.empty()) return true;
if (img1.empty() || img2.empty()) return false;
if (img1.size() != img2.size() || img1.type() != img2.type()) return false;
// Same data pointer = same cv::Mat (shallow copy)
// Fast path — same buffer pointer. With the new GetImage logic, the
// duplicate path returns _pLastFrame, so the caller gets back the
// same buffer it received last time → this branch hits and we're done
// in O(1). The vast majority of "is this a duplicate?" queries land
// here.
if (img1.data == img2.data) return true;
// Quick 5-point sampling
// Distinct buffers — fall back to a real pixel comparison. Quick
// 4-point sample first to early-out on obvious mismatches, then a
// full memcmp for confirmation.
if (img1.isContinuous() && img2.isContinuous()) {
const size_t totalBytes = img1.total() * img1.elemSize();
const size_t quarter = totalBytes / 4;
@@ -545,31 +548,34 @@ namespace ANSCENTER {
}
// Duplicate-frame detection. We track the inner player's currentPts
// (in _lastInnerPts) separately from our own _pts. This is important:
// _pts gets synthetically advanced on every duplicate so LabVIEW's
// staleness check sees a changing timestamp. If we compared currentPts
// against _pts, the synthetic advance would make the comparison fail
// on the next call and route the same cached frame through the
// "new frame" path — pointless resize + storage on identical pixels.
// (in _lastInnerPts) separately from our own _pts so that:
// 1. duplicate detection stays correct even when _pts has been
// bumped past currentPts in earlier "new frame" calls
// 2. LabVIEW's invariant "same pts == same image" holds — required
// so a caller comparing pts (or calling areImagesIdentical())
// correctly recognises rate-limited / buffered duplicates.
//
// Camera-alive detection MUST use GetRTSPLastFrameAgeMs() — pts is no
// longer abused as a heartbeat. ageMs is updated for every decoded
// frame inside CVideoPlayer::onVideoFrame, independent of pacing.
if (currentPts == _lastInnerPts && !_pLastFrame.empty()) {
width = _imageWidth;
height = _imageHeight;
// Optional: log the stale case so DebugView still surfaces it.
double ageMs = _playerClient->getLastFrameAgeMs();
if (ageMs > 0.0 && ageMs < 5000.0) {
_pts++; // synthetic advance — LabVIEW staleness signal
} else if (ageMs >= 5000.0) {
ANS_DBG("RTSP_GetImage", "STALE PTS: ageMs=%.1f pts=%lld url=%s — not advancing PTS",
if (ageMs >= 5000.0) {
ANS_DBG("RTSP_GetImage", "STALE PTS: ageMs=%.1f pts=%lld url=%s",
ageMs, (long long)_pts, _url.c_str());
}
pts = _pts;
pts = _pts; // unchanged — same image, same pts
g_rtspPulseDupPts.fetch_add(1, std::memory_order_relaxed);
return _pLastFrame;
}
// Inner advanced — real new frame. Sync our duplicate-detection cursor
// and bump _pts monotonically (never assign to currentPts, which would
// make _pts go backwards if it had been advanced past currentPts on
// previous duplicates).
// and bump _pts monotonically. Each ++ corresponds to a unique frame
// delivered to the consumer, so a caller can use pts as a stable
// "did I get a new frame?" signal.
_lastInnerPts = currentPts;
_pts++;
pts = _pts;
@@ -1202,11 +1208,11 @@ extern "C" __declspec(dllexport) int GetRTSPStrImage(ANSCENTER::ANSRTSPClient *
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error getting RTSP image: " << e.what() << std::endl;
ANS_DBG("RTSP_GetImage", "exception: %s", e.what());
return 0;
}
catch (...) {
std::cerr << "Error getting RTSP image: Unknown exception." << std::endl;
ANS_DBG("RTSP_GetImage", "unknown exception");
return 0;
}
}
@@ -1219,7 +1225,7 @@ extern "C" __declspec(dllexport) int GetRTSPCVImage(
{
// Validate input parameters
if (!Handle || !*Handle || !image) {
std::cerr << "Error: Invalid input parameters in GetRTSPCVImage" << std::endl;
ANS_DBG("RTSP_GetImage", "invalid input parameters");
return -1;
}
@@ -1327,15 +1333,15 @@ extern "C" __declspec(dllexport) int GetRTSPCVImage(
return 1; // Success
}
catch (const cv::Exception& e) {
std::cerr << "OpenCV exception in GetRTSPCVImage: " << e.what() << std::endl;
ANS_DBG("RTSP_GetImage", "OpenCV exception: %s", e.what());
return -2;
}
catch (const std::exception& e) {
std::cerr << "Exception in GetRTSPCVImage: " << e.what() << std::endl;
ANS_DBG("RTSP_GetImage", "exception in GetRTSPCVImage: %s", e.what());
return -2;
}
catch (...) {
std::cerr << "Unknown exception in GetRTSPCVImage" << std::endl;
ANS_DBG("RTSP_GetImage", "unknown exception in GetRTSPCVImage");
return -2;
}
}
@@ -1379,11 +1385,11 @@ extern "C" __declspec(dllexport) int GetRTSPCVImage(
// return 1; // Success
// }
// catch (const cv::Exception& e) {
// std::cerr << "OpenCV exception in GetRTSPCVImage: " << e.what() << std::endl;
// ANS_DBG("RTSP_GetImage", "OpenCV exception: %s", e.what());
// return -2; // Error code for OpenCV exceptions
// }
// catch (const std::exception& e) {
// std::cerr << "Exception in GetRTSPCVImage: " << e.what() << std::endl;
// ANS_DBG("RTSP_GetImage", "exception in GetRTSPCVImage: %s", e.what());
// return -2; // Error code for standard exceptions
// }
// catch (...) {
@@ -1418,7 +1424,7 @@ extern "C" __declspec(dllexport) int GetRTSPCVImage(
// return 1; // Success
// }
// catch (const std::exception& e) {
// std::cerr << "Exception in GetRTSPCVImage: " << e.what() << std::endl;
// ANS_DBG("RTSP_GetImage", "exception in GetRTSPCVImage: %s", e.what());
// return -2; // Error code for exceptions
// }
// catch (...) {
@@ -1447,22 +1453,22 @@ extern "C" __declspec(dllexport) int GetRTSPImage(ANSCENTER::ANSRTSPClient** Han
}
else {
// Return failure if there was an error in resizing the handle
std::cerr << "Error resizing jpegImage handle: " << error << std::endl;
ANS_DBG("RTSP_GetImage", "DSSetHandleSize failed: error=%d", (int)error);
return 0;
}
}
else {
// If the JPEG image string is empty, return failure
std::cerr << "No image data retrieved from FLV client." << std::endl;
ANS_DBG("RTSP_GetImage", "no image data retrieved (jpegString empty)");
return 0;
}
}
catch (const std::exception& e) {
std::cerr << "Error getting RTSP image: " << e.what() << std::endl;
ANS_DBG("RTSP_GetImage", "exception: %s", e.what());
return 0;
}
catch (...) {
std::cerr << "Error getting RTSP image: Unknown exception." << std::endl;
ANS_DBG("RTSP_GetImage", "unknown exception");
return 0;
}
}
@@ -1475,11 +1481,11 @@ extern "C" __declspec(dllexport) int StartRTSP(ANSCENTER::ANSRTSPClient **Handle
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error starting RTSP client: " << e.what() << std::endl;
ANS_DBG("RTSP_Lifecycle", "StartRTSP exception: %s", e.what());
return 0;
}
catch (...) {
std::cerr << "Error starting RTSP client: Unknown exception." << std::endl;
ANS_DBG("RTSP_Lifecycle", "StartRTSP unknown exception");
return 0;
}
}
@@ -1491,11 +1497,11 @@ extern "C" __declspec(dllexport) int ReconnectRTSP(ANSCENTER::ANSRTSPClient * *H
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error reconnecting RTSP client: " << e.what() << std::endl;
ANS_DBG("RTSP_Lifecycle", "ReconnectRTSP exception: %s", e.what());
return 0;
}
catch (...) {
std::cerr << "Error reconnecting RTSP client: Unknown exception." << std::endl;
ANS_DBG("RTSP_Lifecycle", "ReconnectRTSP unknown exception");
return 0;
}
}
@@ -1508,11 +1514,11 @@ extern "C" __declspec(dllexport) int StopRTSP(ANSCENTER::ANSRTSPClient * *Handle
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error stopping RTSP client: " << e.what() << std::endl;
ANS_DBG("RTSP_Lifecycle", "StopRTSP exception: %s", e.what());
return 0;
}
catch (...) {
std::cerr << "Error stopping RTSP client: Unknown exception." << std::endl;
ANS_DBG("RTSP_Lifecycle", "StopRTSP unknown exception");
return 0;
}
@@ -1525,11 +1531,11 @@ extern "C" __declspec(dllexport) int PauseRTSP(ANSCENTER::ANSRTSPClient** Handle
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error pausing RTSP client: " << e.what() << std::endl;
ANS_DBG("RTSP_Lifecycle", "PauseRTSP exception: %s", e.what());
return 0;
}
catch (...) {
std::cerr << "Error pausing RTSP client: Unknown exception." << std::endl;
ANS_DBG("RTSP_Lifecycle", "PauseRTSP unknown exception");
return 0;
}
}
@@ -1541,11 +1547,11 @@ extern "C" __declspec(dllexport) int IsRTSPPaused(ANSCENTER::ANSRTSPClient** Han
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error checking if RTSP client is paused: " << e.what() << std::endl;
ANS_DBG("RTSP_Lifecycle", "IsRTSPPaused exception: %s", e.what());
return 0;
}
catch (...) {
std::cerr << "Error checking if RTSP client is paused: Unknown exception." << std::endl;
ANS_DBG("RTSP_Lifecycle", "IsRTSPPaused unknown exception");
return 0;
}
}
@@ -1557,11 +1563,11 @@ extern "C" __declspec(dllexport) int IsRTSPRunning(ANSCENTER::ANSRTSPClient **Ha
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error checking if RTSP client is running: " << e.what() << std::endl;
ANS_DBG("RTSP_Lifecycle", "IsRTSPRunning exception: %s", e.what());
return 0;
}
catch (...) {
std::cerr << "Error checking if RTSP client is running: Unknown exception." << std::endl;
ANS_DBG("RTSP_Lifecycle", "IsRTSPRunning unknown exception");
return 0;
}
}
@@ -1573,11 +1579,11 @@ extern "C" __declspec(dllexport) int IsRTSPRecording(ANSCENTER::ANSRTSPClient **
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error checking if RTSP client is recording: " << e.what() << std::endl;
ANS_DBG("RTSP_Lifecycle", "IsRTSPRecording exception: %s", e.what());
return 0;
}
catch (...) {
std::cerr << "Error checking if RTSP client is recording: Unknown exception." << std::endl;
ANS_DBG("RTSP_Lifecycle", "IsRTSPRecording unknown exception");
return 0;
}
}
@@ -1609,11 +1615,11 @@ extern "C" __declspec(dllexport) int SetBBoxRTSP(ANSCENTER::ANSRTSPClient** Hand
return 1;
}
catch (const std::exception& e) {
std::cerr << "Error setting RTSP bounding box: " << e.what() << std::endl;
ANS_DBG("RTSP_Cfg", "SetBBoxRTSP exception: %s", e.what());
return 0;
}
catch (...) {
std::cerr << "Error setting RTSP bounding box: Unknown exception." << std::endl;
ANS_DBG("RTSP_Cfg", "SetBBoxRTSP unknown exception");
return 0;
}
}
@@ -1698,11 +1704,11 @@ extern "C" __declspec(dllexport) int SetCropFlagRTSP(ANSCENTER::ANSRTSPClient**
return 1;
}
catch (const std::exception& e) {
std::cerr << "Error setting RTSP crop flag: " << e.what() << std::endl;
ANS_DBG("RTSP_Cfg", "SetCropFlagRTSP exception: %s", e.what());
return 0;
}
catch (...) {
std::cerr << "Error setting RTSP crop flag: Unknown exception." << std::endl;
ANS_DBG("RTSP_Cfg", "SetCropFlagRTSP unknown exception");
return 0;
}
}
@@ -1727,21 +1733,21 @@ extern "C" __declspec(dllexport) int GetRTSPImage_V2(uint64_t handleVal, int& wi
return 1;
}
else {
std::cerr << "Error resizing jpegImage handle: " << error << std::endl;
ANS_DBG("RTSP_GetImage", "DSSetHandleSize failed: error=%d", (int)error);
return 0;
}
}
else {
std::cerr << "No image data retrieved from FLV client." << std::endl;
ANS_DBG("RTSP_GetImage", "no image data retrieved (jpegString empty)");
return 0;
}
}
catch (const std::exception& e) {
std::cerr << "Error getting RTSP image: " << e.what() << std::endl;
ANS_DBG("RTSP_GetImage", "exception: %s", e.what());
return 0;
}
catch (...) {
std::cerr << "Error getting RTSP image: Unknown exception." << std::endl;
ANS_DBG("RTSP_GetImage", "unknown exception");
return 0;
}
}
@@ -1755,7 +1761,7 @@ extern "C" __declspec(dllexport) int GetRTSPCVImage_V2(
{
auto* h = reinterpret_cast<ANSCENTER::ANSRTSPClient*>(handleVal); if (!h) return -1;
if (!image) {
std::cerr << "Error: Invalid input parameters in GetRTSPCVImage_V2" << std::endl;
ANS_DBG("RTSP_GetImage", "GetRTSPCVImage_V2 invalid input parameters");
return -1;
}
try {
@@ -1767,15 +1773,15 @@ extern "C" __declspec(dllexport) int GetRTSPCVImage_V2(
return 1;
}
catch (const cv::Exception& e) {
std::cerr << "OpenCV exception in GetRTSPCVImage_V2: " << e.what() << std::endl;
ANS_DBG("RTSP_GetImage", "GetRTSPCVImage_V2 OpenCV exception: %s", e.what());
return -2;
}
catch (const std::exception& e) {
std::cerr << "Exception in GetRTSPCVImage_V2: " << e.what() << std::endl;
ANS_DBG("RTSP_GetImage", "GetRTSPCVImage_V2 exception: %s", e.what());
return -2;
}
catch (...) {
std::cerr << "Unknown exception in GetRTSPCVImage_V2" << std::endl;
ANS_DBG("RTSP_GetImage", "GetRTSPCVImage_V2 unknown exception");
return -2;
}
}
@@ -1788,11 +1794,11 @@ extern "C" __declspec(dllexport) int StartRTSP_V2(uint64_t handleVal) {
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error starting RTSP client: " << e.what() << std::endl;
ANS_DBG("RTSP_Lifecycle", "StartRTSP exception: %s", e.what());
return 0;
}
catch (...) {
std::cerr << "Error starting RTSP client: Unknown exception." << std::endl;
ANS_DBG("RTSP_Lifecycle", "StartRTSP unknown exception");
return 0;
}
}
@@ -1805,11 +1811,11 @@ extern "C" __declspec(dllexport) int ReconnectRTSP_V2(uint64_t handleVal) {
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error reconnecting RTSP client: " << e.what() << std::endl;
ANS_DBG("RTSP_Lifecycle", "ReconnectRTSP exception: %s", e.what());
return 0;
}
catch (...) {
std::cerr << "Error reconnecting RTSP client: Unknown exception." << std::endl;
ANS_DBG("RTSP_Lifecycle", "ReconnectRTSP unknown exception");
return 0;
}
}
@@ -1822,11 +1828,11 @@ extern "C" __declspec(dllexport) int StopRTSP_V2(uint64_t handleVal) {
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error stopping RTSP client: " << e.what() << std::endl;
ANS_DBG("RTSP_Lifecycle", "StopRTSP exception: %s", e.what());
return 0;
}
catch (...) {
std::cerr << "Error stopping RTSP client: Unknown exception." << std::endl;
ANS_DBG("RTSP_Lifecycle", "StopRTSP unknown exception");
return 0;
}
}
@@ -1839,11 +1845,11 @@ extern "C" __declspec(dllexport) int PauseRTSP_V2(uint64_t handleVal) {
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error pausing RTSP client: " << e.what() << std::endl;
ANS_DBG("RTSP_Lifecycle", "PauseRTSP exception: %s", e.what());
return 0;
}
catch (...) {
std::cerr << "Error pausing RTSP client: Unknown exception." << std::endl;
ANS_DBG("RTSP_Lifecycle", "PauseRTSP unknown exception");
return 0;
}
}
@@ -1856,11 +1862,11 @@ extern "C" __declspec(dllexport) int IsRTSPPaused_V2(uint64_t handleVal) {
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error checking if RTSP client is paused: " << e.what() << std::endl;
ANS_DBG("RTSP_Lifecycle", "IsRTSPPaused exception: %s", e.what());
return 0;
}
catch (...) {
std::cerr << "Error checking if RTSP client is paused: Unknown exception." << std::endl;
ANS_DBG("RTSP_Lifecycle", "IsRTSPPaused unknown exception");
return 0;
}
}
@@ -1873,11 +1879,11 @@ extern "C" __declspec(dllexport) int IsRTSPRunning_V2(uint64_t handleVal) {
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error checking if RTSP client is running: " << e.what() << std::endl;
ANS_DBG("RTSP_Lifecycle", "IsRTSPRunning exception: %s", e.what());
return 0;
}
catch (...) {
std::cerr << "Error checking if RTSP client is running: Unknown exception." << std::endl;
ANS_DBG("RTSP_Lifecycle", "IsRTSPRunning unknown exception");
return 0;
}
}
@@ -1890,11 +1896,11 @@ extern "C" __declspec(dllexport) int IsRTSPRecording_V2(uint64_t handleVal) {
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error checking if RTSP client is recording: " << e.what() << std::endl;
ANS_DBG("RTSP_Lifecycle", "IsRTSPRecording exception: %s", e.what());
return 0;
}
catch (...) {
std::cerr << "Error checking if RTSP client is recording: Unknown exception." << std::endl;
ANS_DBG("RTSP_Lifecycle", "IsRTSPRecording unknown exception");
return 0;
}
}
@@ -1933,11 +1939,11 @@ extern "C" __declspec(dllexport) int SetBBoxRTSP_V2(uint64_t handleVal, int x, i
return 1;
}
catch (const std::exception& e) {
std::cerr << "Error setting RTSP bounding box: " << e.what() << std::endl;
ANS_DBG("RTSP_Cfg", "SetBBoxRTSP exception: %s", e.what());
return 0;
}
catch (...) {
std::cerr << "Error setting RTSP bounding box: Unknown exception." << std::endl;
ANS_DBG("RTSP_Cfg", "SetBBoxRTSP unknown exception");
return 0;
}
}
@@ -1951,11 +1957,11 @@ extern "C" __declspec(dllexport) int SetCropFlagRTSP_V2(uint64_t handleVal, int
return 1;
}
catch (const std::exception& e) {
std::cerr << "Error setting RTSP crop flag: " << e.what() << std::endl;
ANS_DBG("RTSP_Cfg", "SetCropFlagRTSP exception: %s", e.what());
return 0;
}
catch (...) {
std::cerr << "Error setting RTSP crop flag: Unknown exception." << std::endl;
ANS_DBG("RTSP_Cfg", "SetCropFlagRTSP unknown exception");
return 0;
}
}