Fix other ANSCV classes

This commit is contained in:
2026-05-09 16:47:46 +10:00
parent aa323a07a3
commit a94bf88a5b
16 changed files with 1197 additions and 197 deletions
+76 -41
View File
@@ -219,14 +219,23 @@ namespace ANSCENTER {
}
}
// 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 downstream work
// on rate-limited / buffered duplicates.
//
// Camera-aliveness ("did the decoder go silent?") is a separate concern;
// use GetRTMPLastFrameAgeMs (or _playerClient->getLastFrameAgeMs()) for
// that. With the new pts discipline (same image -> same pts), most callers
// can simply compare pts instead of calling this function at all.
bool ANSRTMPClient::areImagesIdentical(const cv::Mat& img1, const cv::Mat& img2) {
double ageMs = _playerClient->getLastFrameAgeMs();
if (ageMs > 5000.0) return true;
if (ageMs > 0.0) return false;
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;
// Fast path — same buffer pointer. With the new GetImage discipline,
// the duplicate path returns _pLastFrame so this branch hits and we
// are done in O(1).
if (img1.data == img2.data) return true;
if (img1.isContinuous() && img2.isContinuous()) {
@@ -282,19 +291,25 @@ namespace ANSCENTER {
return _pLastFrame;
}
// Use PTS to detect duplicate frames (much faster than pixel comparison)
// The sequence-based check in getImage() already skips conversion for same frames,
// so if PTS hasn't changed, it's the same frame — no need for expensive memcmp
if (currentPts == _pts && !_pLastFrame.empty()) {
// Duplicate-frame detection. Track the inner player's currentPts in
// _lastInnerPts (separate from our own _pts). On a duplicate, return
// _pLastFrame and DO NOT advance _pts — the invariant is:
// same image -> same pts
// so a caller comparing pts (or calling areImagesIdentical()) can
// recognise rate-limited / buffered duplicates.
// Camera-alive detection should use getLastFrameAgeMs() instead.
if (currentPts == _lastInnerPts && !_pLastFrame.empty()) {
width = _imageWidth;
height = _imageHeight;
pts = _pts;
pts = _pts; // unchanged
return _pLastFrame;
}
// Update PTS first
_pts = currentPts;
pts = currentPts;
// Inner advanced — real new frame. Sync the duplicate cursor and bump
// _pts monotonically (each ++ corresponds to a unique frame).
_lastInnerPts = currentPts;
_pts++;
pts = _pts;
// Handle non-rotated case
if (_imageRotateDeg == 0) {
@@ -648,6 +663,14 @@ namespace ANSCENTER {
std::lock_guard<std::recursive_mutex> lock(_mutex);
_playerClient->setTargetFPS(intervalMs); // 0=no limit, 100=~10FPS, 200=~5FPS
}
void ANSRTMPClient::SetJitterBufferMs(int ms) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
_playerClient->setJitterBufferMs(ms);
ANS_DBG("RTMP_Lifecycle", "SetJitterBufferMs: ms=%d url=%s", ms, _url.c_str());
}
int ANSRTMPClient::GetJitterBufferMs() const {
return _playerClient->getJitterBufferMs();
}
void ANSRTMPClient::SetNV12FastPath(bool enable) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
_useNV12FastPath = enable;
@@ -742,11 +765,11 @@ extern "C" __declspec(dllexport) int GetRTMPtrImage(ANSCENTER::ANSRTMPClient * *
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error getting RTMP image: " << e.what() << std::endl;
ANS_DBG("RTMP_GetImage", "exception: %s", e.what());
return 0;
}
catch (...) {
std::cerr << "Error getting RTMP image: Unknown exception." << std::endl;
ANS_DBG("RTMP_GetImage", "unknown exception");
return 0;
}
}
@@ -770,22 +793,22 @@ extern "C" __declspec(dllexport) int GetRTMPImage(ANSCENTER::ANSRTMPClient * *Ha
}
else {
// Return failure if there was an error in resizing the handle
std::cerr << "Error resizing jpegImage handle: " << error << std::endl;
ANS_DBG("RTMP_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 RTMP client." << std::endl;
ANS_DBG("RTMP_GetImage", "no image data retrieved (jpegString empty)");
return 0;
}
}
catch (const std::exception& e) {
std::cerr << "Error getting RTMP image: " << e.what() << std::endl;
ANS_DBG("RTMP_GetImage", "exception: %s", e.what());
return 0;
}
catch (...) {
std::cerr << "Error getting RTMP image: Unknown exception." << std::endl;
ANS_DBG("RTMP_GetImage", "unknown exception");
return 0;
}
}
@@ -793,7 +816,7 @@ extern "C" __declspec(dllexport) int GetRTMPImage(ANSCENTER::ANSRTMPClient * *Ha
extern "C" __declspec(dllexport) int GetRTMPCVImage(ANSCENTER::ANSRTMPClient** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image) {
// Validate input parameters
if (!Handle || !*Handle || !image) {
std::cerr << "Error: Invalid input parameters in GetRTMPCVImage" << std::endl;
ANS_DBG("RTMP_GetImage", "invalid input parameters");
return -1;
}
@@ -835,15 +858,15 @@ extern "C" __declspec(dllexport) int GetRTMPCVImage(ANSCENTER::ANSRTMPClient** H
return 1; // Success
}
catch (const cv::Exception& e) {
std::cerr << "OpenCV exception in GetRTMPCVImage: " << e.what() << std::endl;
ANS_DBG("RTMP_GetImage", "OpenCV exception: %s", e.what());
return -2;
}
catch (const std::exception& e) {
std::cerr << "Exception in GetRTMPCVImage: " << e.what() << std::endl;
ANS_DBG("RTMP_GetImage", "GetRTMPCVImage exception: %s", e.what());
return -2;
}
catch (...) {
std::cerr << "Unknown exception in GetRTMPCVImage" << std::endl;
ANS_DBG("RTMP_GetImage", "GetRTMPCVImage unknown exception");
return -2;
}
}
@@ -855,11 +878,11 @@ extern "C" __declspec(dllexport) int StartRTMP(ANSCENTER::ANSRTMPClient * *Handl
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error starting RTMP client: " << e.what() << std::endl;
ANS_DBG("RTMP_Lifecycle", "StartRTMP exception: %s", e.what());
return 0;
}
catch (...) {
std::cerr << "Error starting RTMP client: Unknown exception." << std::endl;
ANS_DBG("RTMP_Lifecycle", "StartRTMP unknown exception");
return 0;
}
}
@@ -871,11 +894,11 @@ extern "C" __declspec(dllexport) int ReconnectRTMP(ANSCENTER::ANSRTMPClient * *H
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error reconnecting RTMP client: " << e.what() << std::endl;
ANS_DBG("RTMP_Lifecycle", "ReconnectRTMP exception: %s", e.what());
return 0;
}
catch (...) {
std::cerr << "Error reconnecting RTMP client: Unknown exception." << std::endl;
ANS_DBG("RTMP_Lifecycle", "ReconnectRTMP unknown exception");
return 0;
}
}
@@ -887,11 +910,11 @@ extern "C" __declspec(dllexport) int StopRTMP(ANSCENTER::ANSRTMPClient * *Handle
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error stopping RTMP client: " << e.what() << std::endl;
ANS_DBG("RTMP_Lifecycle", "StopRTMP exception: %s", e.what());
return 0;
}
catch (...) {
std::cerr << "Error stopping RTMP client: Unknown exception." << std::endl;
ANS_DBG("RTMP_Lifecycle", "StopRTMP unknown exception");
return 0;
}
}
@@ -1007,6 +1030,18 @@ extern "C" __declspec(dllexport) void SetRTMPTargetFPS(ANSCENTER::ANSRTMPClient*
(*Handle)->SetTargetFPS(intervalMs);
} catch (...) { }
}
extern "C" __declspec(dllexport) void SetRTMPJitterBuffer(ANSCENTER::ANSRTMPClient** Handle, int jitterMs) {
if (Handle == nullptr || *Handle == nullptr) return;
try {
(*Handle)->SetJitterBufferMs(jitterMs);
} catch (...) { }
}
extern "C" __declspec(dllexport) int GetRTMPJitterBuffer(ANSCENTER::ANSRTMPClient** Handle) {
if (Handle == nullptr || *Handle == nullptr) return -1;
try {
return (*Handle)->GetJitterBufferMs();
} catch (...) { return -1; }
}
extern "C" __declspec(dllexport) void SetRTMPNV12FastPath(ANSCENTER::ANSRTMPClient** Handle, int enable) {
if (Handle == nullptr || *Handle == nullptr) return;
try {
@@ -1033,21 +1068,21 @@ extern "C" __declspec(dllexport) int GetRTMPImage_V2(uint64_t handleVal, int& wi
return 1;
}
else {
std::cerr << "Error resizing jpegImage handle: " << error << std::endl;
ANS_DBG("RTMP_GetImage", "DSSetHandleSize failed: error=%d", (int)error);
return 0;
}
}
else {
std::cerr << "No image data retrieved from RTMP client." << std::endl;
ANS_DBG("RTMP_GetImage", "no image data retrieved (jpegString empty)");
return 0;
}
}
catch (const std::exception& e) {
std::cerr << "Error getting RTMP image: " << e.what() << std::endl;
ANS_DBG("RTMP_GetImage", "exception: %s", e.what());
return 0;
}
catch (...) {
std::cerr << "Error getting RTMP image: Unknown exception." << std::endl;
ANS_DBG("RTMP_GetImage", "unknown exception");
return 0;
}
}
@@ -1086,15 +1121,15 @@ extern "C" __declspec(dllexport) int GetRTMPCVImage_V2(uint64_t handleVal, int&
return 1;
}
catch (const cv::Exception& e) {
std::cerr << "OpenCV exception in GetRTMPCVImage_V2: " << e.what() << std::endl;
ANS_DBG("RTMP_GetImage", "GetRTMPCVImage_V2 OpenCV exception: %s", e.what());
return -2;
}
catch (const std::exception& e) {
std::cerr << "Exception in GetRTMPCVImage_V2: " << e.what() << std::endl;
ANS_DBG("RTMP_GetImage", "GetRTMPCVImage_V2 exception: %s", e.what());
return -2;
}
catch (...) {
std::cerr << "Unknown exception in GetRTMPCVImage_V2" << std::endl;
ANS_DBG("RTMP_GetImage", "GetRTMPCVImage_V2 unknown exception");
return -2;
}
}
@@ -1107,11 +1142,11 @@ extern "C" __declspec(dllexport) int StartRTMP_V2(uint64_t handleVal) {
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error starting RTMP client: " << e.what() << std::endl;
ANS_DBG("RTMP_Lifecycle", "StartRTMP exception: %s", e.what());
return 0;
}
catch (...) {
std::cerr << "Error starting RTMP client: Unknown exception." << std::endl;
ANS_DBG("RTMP_Lifecycle", "StartRTMP unknown exception");
return 0;
}
}
@@ -1124,11 +1159,11 @@ extern "C" __declspec(dllexport) int ReconnectRTMP_V2(uint64_t handleVal) {
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error reconnecting RTMP client: " << e.what() << std::endl;
ANS_DBG("RTMP_Lifecycle", "ReconnectRTMP exception: %s", e.what());
return 0;
}
catch (...) {
std::cerr << "Error reconnecting RTMP client: Unknown exception." << std::endl;
ANS_DBG("RTMP_Lifecycle", "ReconnectRTMP unknown exception");
return 0;
}
}
@@ -1141,11 +1176,11 @@ extern "C" __declspec(dllexport) int StopRTMP_V2(uint64_t handleVal) {
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error stopping RTMP client: " << e.what() << std::endl;
ANS_DBG("RTMP_Lifecycle", "StopRTMP exception: %s", e.what());
return 0;
}
catch (...) {
std::cerr << "Error stopping RTMP client: Unknown exception." << std::endl;
ANS_DBG("RTMP_Lifecycle", "StopRTMP unknown exception");
return 0;
}
}