diff --git a/.claude/settings.local.json b/.claude/settings.local.json index db3449b..f9f51c1 100644 --- a/.claude/settings.local.json +++ b/.claude/settings.local.json @@ -225,7 +225,14 @@ "Bash(where dumpbin.exe)", "Bash(where llvm-symbolizer.exe)", "Bash(where cdb.exe)", - "Bash(\"C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/Llvm/x64/bin/llvm-symbolizer.exe\" --obj=\"C:/Projects/AIVISION/ANSFallDetection/ANSCustomFall/Dll1/x64/Release/ANSCustomFall.dll\" 0xb1d1)" + "Bash(\"C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/Llvm/x64/bin/llvm-symbolizer.exe\" --obj=\"C:/Projects/AIVISION/ANSFallDetection/ANSCustomFall/Dll1/x64/Release/ANSCustomFall.dll\" 0xb1d1)", + "Bash(cmake --build C:/Projects/CLionProjects/ANSCORE/cmake-build-release --target ANSLicensingSystem -- /m)", + "Bash(cmake --build C:/Projects/CLionProjects/ANSCORE/cmake-build-release --target ANSLicensingSystem)", + "Bash(cmd /c \"\\\\\"C:\\\\\\\\Program Files\\\\\\\\Microsoft Visual Studio\\\\\\\\2022\\\\\\\\Community\\\\\\\\VC\\\\\\\\Auxiliary\\\\\\\\Build\\\\\\\\vcvars64.bat\\\\\" >nul && cmake --build C:\\\\\\\\Projects\\\\\\\\CLionProjects\\\\\\\\ANSCORE\\\\\\\\cmake-build-release --target ANSLicensingSystem 2>&1\")", + "Bash(cmd /c \"call \\\\\"C:\\\\\\\\Program Files\\\\\\\\Microsoft Visual Studio\\\\\\\\2022\\\\\\\\Community\\\\\\\\VC\\\\\\\\Auxiliary\\\\\\\\Build\\\\\\\\vcvars64.bat\\\\\" && cmake --build C:\\\\\\\\Projects\\\\\\\\CLionProjects\\\\\\\\ANSCORE\\\\\\\\cmake-build-release --target ANSLicensingSystem\")", + "PowerShell($vcvars = \"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Auxiliary\\\\Build\\\\vcvars64.bat\"; cmd /c \"`\"$vcvars`\" && cmake --build C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release --target ANSLicensingSystem 2>&1\" 2>&1 | Select-Object -Last 120)", + "Bash(grep -rn \"^using namespace std\" \"C:/ANSLibs/anslicensing/\" 2>/dev/null | head -5; grep -rn \"^using namespace std\" \"C:/Projects/CLionProjects/ANSCORE/core/anslicensing/\" 2>/dev/null | head -5)", + "PowerShell(Start-Process powershell -Verb RunAs -ArgumentList '-NoProfile','-Command',\"try { Get-WmiObject MSAcpi_ThermalZoneTemperature -Namespace root\\\\wmi | Select-Object InstanceName,CurrentTemperature | Out-File C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release\\\\_smoke\\\\thermal.txt } catch { '$_' | Out-File C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release\\\\_smoke\\\\thermal.txt }\" -Wait; Get-Content C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release\\\\_smoke\\\\thermal.txt)" ] } } diff --git a/core/ANSLicensingSystem/ANSLicense.cpp b/core/ANSLicensingSystem/ANSLicense.cpp index 07f7cb2..6f27500 100644 --- a/core/ANSLicensingSystem/ANSLicense.cpp +++ b/core/ANSLicensingSystem/ANSLicense.cpp @@ -1,3 +1,13 @@ +// Windows / COM / PDH / DXGI headers must precede any include that drops +// `using namespace std;` into the global scope (base64.h does), otherwise +// the SDK's `byte` typedef in collides with `std::byte`. +#include +#include +#include +#include +#include +#include + #include "Utility.h" #include #include @@ -21,6 +31,10 @@ #include #include #include +#include +#include +#include +#include // Forward-declare OutputDebugStringA without pulling in (which // drags winsock conflicts as noted in ANSLicense.h). Used by ANSCENTER_DebugLog. @@ -529,9 +543,562 @@ extern "C" { } } +// ============================================================================ +// SystemHealth sampler — process-wide singleton. +// +// Wraps a long-lived PDH query so subsequent ::Sample() calls only pay for one +// PdhCollectQueryData and a few format calls. PDH delta-rate counters +// (Bytes/sec, % time, etc.) need two samples to produce a value, so the +// first call after process start blocks ~150 ms; later calls return cached +// values within 500 ms, otherwise re-collect on demand. +// +// Counters use English names (`PdhAddEnglishCounterW`) so the same paths work +// regardless of the OS display language. +// ============================================================================ +namespace { + +double Pdh_GetSingle(PDH_HCOUNTER c) { + if (!c) return -1.0; + PDH_FMT_COUNTERVALUE val{}; + PDH_STATUS s = PdhGetFormattedCounterValue(c, PDH_FMT_DOUBLE | PDH_FMT_NOCAP100, nullptr, &val); + if (s != ERROR_SUCCESS) return -1.0; + if (val.CStatus != PDH_CSTATUS_VALID_DATA && val.CStatus != PDH_CSTATUS_NEW_DATA) + return -1.0; + return val.doubleValue; +} + +double Pdh_SumWildcard(PDH_HCOUNTER c) { + if (!c) return -1.0; + DWORD bufSize = 0, count = 0; + PDH_STATUS s = PdhGetFormattedCounterArrayW(c, PDH_FMT_DOUBLE, &bufSize, &count, nullptr); + if (s != PDH_MORE_DATA) return -1.0; + std::vector buf(bufSize); + auto* items = reinterpret_cast(buf.data()); + s = PdhGetFormattedCounterArrayW(c, PDH_FMT_DOUBLE, &bufSize, &count, items); + if (s != ERROR_SUCCESS) return -1.0; + double sum = 0.0; + for (DWORD i = 0; i < count; ++i) { + const auto& v = items[i].FmtValue; + if (v.CStatus == PDH_CSTATUS_VALID_DATA || v.CStatus == PDH_CSTATUS_NEW_DATA) + sum += v.doubleValue; + } + return sum; +} + +// CPU temperature via WMI's MSAcpi_ThermalZoneTemperature (root\WMI namespace). +// Returns the highest reported zone, in °C, or -1.0 if unavailable. Caveats: +// - Most consumer desktops either expose nothing here or report a fixed +// placeholder (often 27.85 °C / 30.10 dK) regardless of real die temp. +// - Laptops and workstation/server boards generally report something +// plausible; a vendor SDK is still preferable for absolute accuracy. +// +// Each call self-contains COM init: we try MULTITHREADED, accept +// RPC_E_CHANGED_MODE if the calling thread is already STA-initialised, and +// only CoUninitialize when we own the init. Security init is one-shot per +// process. Total cost ~3–10 ms. +double ReadCpuTempCelsiusViaWmi() { + HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED); + const bool weInited = (hr == S_OK || hr == S_FALSE); + if (FAILED(hr) && hr != RPC_E_CHANGED_MODE) return -1.0; + + static std::once_flag s_secOnce; + std::call_once(s_secOnce, [] { + // Best-effort; RPC_E_TOO_LATE is fine — someone else initialised first. + CoInitializeSecurity(nullptr, -1, nullptr, nullptr, + RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, + nullptr, EOAC_NONE, nullptr); + }); + + double maxTempC = -1.0; + + IWbemLocator* pLoc = nullptr; + if (SUCCEEDED(CoCreateInstance(CLSID_WbemLocator, nullptr, CLSCTX_INPROC_SERVER, + IID_IWbemLocator, reinterpret_cast(&pLoc)))) { + IWbemServices* pSvc = nullptr; + if (SUCCEEDED(pLoc->ConnectServer(_bstr_t(L"ROOT\\WMI"), + nullptr, nullptr, nullptr, 0, nullptr, nullptr, &pSvc))) { + CoSetProxyBlanket(pSvc, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, nullptr, + RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, + nullptr, EOAC_NONE); + + IEnumWbemClassObject* pEnum = nullptr; + if (SUCCEEDED(pSvc->ExecQuery(_bstr_t(L"WQL"), + _bstr_t(L"SELECT CurrentTemperature FROM MSAcpi_ThermalZoneTemperature"), + WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, + nullptr, &pEnum))) { + IWbemClassObject* pObj = nullptr; + ULONG ret = 0; + while (pEnum->Next(WBEM_INFINITE, 1, &pObj, &ret) == S_OK && ret > 0) { + VARIANT v; + VariantInit(&v); + if (SUCCEEDED(pObj->Get(L"CurrentTemperature", 0, &v, nullptr, nullptr))) { + long raw = 0; + bool gotIt = true; + switch (v.vt) { + case VT_I4: raw = v.lVal; break; + case VT_UI4: raw = static_cast(v.ulVal); break; + case VT_I2: raw = v.iVal; break; + case VT_UI2: raw = v.uiVal; break; + default: gotIt = false; break; + } + if (gotIt && raw > 0) { + // ACPI reports tenths of Kelvin. Windows returns + // a constant 3010 (== 27.85 °C) when the firmware + // does not actually expose a thermal zone — drop + // that sentinel so callers see -1 instead of + // believable-looking junk. + if (raw == 3010) continue; + const double t = (raw / 10.0) - 273.15; + if (t > maxTempC) maxTempC = t; + } + } + VariantClear(&v); + pObj->Release(); + } + pEnum->Release(); + } + pSvc->Release(); + } + pLoc->Release(); + } + + if (weInited) CoUninitialize(); + return maxTempC; +} + +// ============================================================================ +// Per-adapter GPU breakdown. +// +// PDH counters under "\GPU Engine(*)" and "\GPU Process Memory(*)" report one +// row per (process, adapter, engine, ...) tuple. Naively summing them across +// every adapter is wrong on hybrid systems — an iGPU at 60% plus a dGPU at +// 60% reads as 120% (capped to 100%), and per-process VRAM mixes physically +// disjoint memories. +// +// PDH instance names embed the owning adapter's LUID: +// pid__luid_0x_0x_phys_[_eng__engtype_] +// So we enumerate adapters via DXGI to learn LUID -> {vendor, name, VRAM}, +// parse each PDH instance name's LUID, and accumulate per-adapter. +// ============================================================================ + +struct GpuAdapter { + LUID luid{}; + DWORD vendorId{0}; + std::string name; // UTF-8 + int64_t dedicatedVramMB{0}; + bool isDiscrete{false}; // heuristic: dedicated VRAM ≥ 1 GiB + + // running totals — reset every Sample() + double loadPercent{0.0}; + int64_t processMemMB{0}; +}; + +const char* VendorString(DWORD vendorId) { + switch (vendorId) { + case 0x10DE: return "NVIDIA"; + case 0x1002: return "AMD"; + case 0x8086: return "Intel"; + case 0x1414: return "Microsoft"; // Basic Render Driver + case 0x5143: return "Qualcomm"; + case 0x106B: return "Apple"; + default: return "Other"; + } +} + +std::string WideToUtf8(const wchar_t* w) { + if (!w) return {}; + const int len = WideCharToMultiByte(CP_UTF8, 0, w, -1, nullptr, 0, nullptr, nullptr); + if (len <= 1) return {}; + std::string s(static_cast(len - 1), '\0'); + WideCharToMultiByte(CP_UTF8, 0, w, -1, s.data(), len, nullptr, nullptr); + return s; +} + +std::vector EnumerateGpuAdapters() { + std::vector out; + IDXGIFactory1* factory = nullptr; + if (FAILED(CreateDXGIFactory1(__uuidof(IDXGIFactory1), + reinterpret_cast(&factory))) || !factory) { + ANS_DBG("HealthSampler", "CreateDXGIFactory1 failed; GPU detail unavailable"); + return out; + } + UINT i = 0; + IDXGIAdapter1* adapter = nullptr; + while (factory->EnumAdapters1(i++, &adapter) != DXGI_ERROR_NOT_FOUND) { + DXGI_ADAPTER_DESC1 desc{}; + if (SUCCEEDED(adapter->GetDesc1(&desc)) && + !(desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)) { + GpuAdapter a; + a.luid = desc.AdapterLuid; + a.vendorId = desc.VendorId; + a.name = WideToUtf8(desc.Description); + a.dedicatedVramMB = static_cast(desc.DedicatedVideoMemory / (1024ULL * 1024ULL)); + a.isDiscrete = a.dedicatedVramMB >= 1024; // ≥ 1 GiB ⇒ probably dGPU + out.push_back(std::move(a)); + } + adapter->Release(); + adapter = nullptr; + } + factory->Release(); + return out; +} + +bool ParseLuidFromInstance(const wchar_t* inst, LUID& out) { + if (!inst) return false; + const wchar_t* p = wcsstr(inst, L"luid_0x"); + if (!p) return false; + p += 7; // skip "luid_0x" + unsigned long high = 0, low = 0; + if (swscanf_s(p, L"%lx_0x%lx", &high, &low) != 2) return false; + out.HighPart = static_cast(high); + out.LowPart = low; + return true; +} + +GpuAdapter* FindByLuid(std::vector& adapters, const LUID& luid) { + for (auto& a : adapters) { + if (a.luid.LowPart == luid.LowPart && a.luid.HighPart == luid.HighPart) + return &a; + } + return nullptr; +} + +// Aggregate a GPU PDH wildcard counter into per-adapter buckets. +// pidFilter != 0 means "only count instances owned by this PID" (used for the +// per-process GPU memory counter); 0 means "count all" (used for utilisation). +void AggregateGpuCounter(PDH_HCOUNTER counter, + std::vector& adapters, + DWORD pidFilter, + bool isMemoryCounter) { + if (!counter) return; + DWORD bufSize = 0, count = 0; + const DWORD fmt = isMemoryCounter ? PDH_FMT_LARGE : PDH_FMT_DOUBLE; + PDH_STATUS s = PdhGetFormattedCounterArrayW(counter, fmt, &bufSize, &count, nullptr); + if (s != PDH_MORE_DATA) return; + std::vector buf(bufSize); + auto* items = reinterpret_cast(buf.data()); + s = PdhGetFormattedCounterArrayW(counter, fmt, &bufSize, &count, items); + if (s != ERROR_SUCCESS) return; + + wchar_t pidPrefix[64] = {}; + size_t pidPrefixLen = 0; + if (pidFilter) { + swprintf_s(pidPrefix, L"pid_%lu_", pidFilter); + pidPrefixLen = wcslen(pidPrefix); + } + + for (DWORD i = 0; i < count; ++i) { + const auto& v = items[i].FmtValue; + if (v.CStatus != PDH_CSTATUS_VALID_DATA && v.CStatus != PDH_CSTATUS_NEW_DATA) + continue; + if (pidFilter && + _wcsnicmp(items[i].szName, pidPrefix, pidPrefixLen) != 0) + continue; + + LUID luid{}; + if (!ParseLuidFromInstance(items[i].szName, luid)) continue; + GpuAdapter* a = FindByLuid(adapters, luid); + if (!a) continue; + + if (isMemoryCounter) + a->processMemMB += v.largeValue / (1024 * 1024); + else + a->loadPercent += v.doubleValue; + } +} + +// Choose which adapter the flat `gpuLoadPercent` / `valIGPU_MB` fields refer to. +// Hardware preference, deterministic per process: NVIDIA dGPU > AMD dGPU > +// largest VRAM. Returns nullptr only when no adapters exist at all. +GpuAdapter* PickPrimaryAdapter(std::vector& adapters) { + if (adapters.empty()) return nullptr; + for (auto& a : adapters) if (a.vendorId == 0x10DE && a.isDiscrete) return &a; + for (auto& a : adapters) if (a.vendorId == 0x1002 && a.isDiscrete) return &a; + GpuAdapter* best = &adapters.front(); + for (auto& a : adapters) + if (a.dedicatedVramMB > best->dedicatedVramMB) best = &a; + return best; +} + +// Sum a LARGE-int wildcard counter, restricted to instances whose name begins +// with "pid__". Used for per-process GPU memory counters where the +// instance name encodes the owning PID. +int64_t Pdh_SumWildcardLargeByPid(PDH_HCOUNTER c, DWORD pid) { + if (!c) return -1; + DWORD bufSize = 0, count = 0; + PDH_STATUS s = PdhGetFormattedCounterArrayW(c, PDH_FMT_LARGE, &bufSize, &count, nullptr); + if (s != PDH_MORE_DATA) return -1; + std::vector buf(bufSize); + auto* items = reinterpret_cast(buf.data()); + s = PdhGetFormattedCounterArrayW(c, PDH_FMT_LARGE, &bufSize, &count, items); + if (s != ERROR_SUCCESS) return -1; + + wchar_t prefix[64]; + swprintf_s(prefix, L"pid_%lu_", pid); + const size_t prefixLen = wcslen(prefix); + + int64_t sum = 0; + for (DWORD i = 0; i < count; ++i) { + const auto& v = items[i].FmtValue; + if (v.CStatus != PDH_CSTATUS_VALID_DATA && v.CStatus != PDH_CSTATUS_NEW_DATA) + continue; + if (_wcsnicmp(items[i].szName, prefix, prefixLen) == 0) + sum += v.largeValue; + } + return sum; +} + +class HealthSampler { +public: + static HealthSampler& Get() { + static HealthSampler inst; + return inst; + } + + void Sample(ANSCENTER::SystemHealthInfo& out) { + std::lock_guard lock(_mtx); + + const auto now = std::chrono::steady_clock::now(); + const bool fresh = + _hasCache && + std::chrono::duration_cast(now - _lastSample).count() < 500; + if (fresh) { + out = _cache; + return; + } + + if (!_initialized) Init(); + + if (_query) { + PdhCollectQueryData(_query); + if (!_firstCollectDone) { + // Delta counters need two samples; sleep just enough to make the + // first reading meaningful instead of returning zeros. + std::this_thread::sleep_for(std::chrono::milliseconds(150)); + PdhCollectQueryData(_query); + _firstCollectDone = true; + } + ReadFromPdh(out); + } + FillNonPdh(out); + + _cache = out; + _lastSample = now; + _hasCache = true; + } + +private: + HealthSampler() = default; + ~HealthSampler() { + if (_query) { + PdhCloseQuery(_query); + _query = nullptr; + } + } + HealthSampler(const HealthSampler&) = delete; + HealthSampler& operator=(const HealthSampler&) = delete; + + void Init() { + if (PdhOpenQueryW(nullptr, 0, &_query) != ERROR_SUCCESS) { + _query = nullptr; + ANS_DBG("HealthSampler", "PdhOpenQueryW failed"); + } + if (_query) { + // Best-effort: missing counters just stay null; reads return -1. + PdhAddEnglishCounterW(_query, L"\\Processor(_Total)\\% Processor Time", 0, &_cpuLoad); + PdhAddEnglishCounterW(_query, L"\\Processor Information(_Total)\\% Processor Performance", 0, &_cpuPerf); + PdhAddEnglishCounterW(_query, L"\\PhysicalDisk(_Total)\\% Disk Time", 0, &_diskTime); + PdhAddEnglishCounterW(_query, L"\\PhysicalDisk(_Total)\\Disk Read Bytes/sec", 0, &_diskRead); + PdhAddEnglishCounterW(_query, L"\\PhysicalDisk(_Total)\\Disk Write Bytes/sec", 0, &_diskWrite); + PdhAddEnglishCounterW(_query, L"\\Network Interface(*)\\Bytes Received/sec", 0, &_netRx); + PdhAddEnglishCounterW(_query, L"\\Network Interface(*)\\Bytes Sent/sec", 0, &_netTx); + PdhAddEnglishCounterW(_query, L"\\Network Interface(*)\\Current Bandwidth", 0, &_netBw); + PdhAddEnglishCounterW(_query, L"\\GPU Engine(*)\\Utilization Percentage", 0, &_gpuUtil); + PdhAddEnglishCounterW(_query, L"\\GPU Process Memory(*)\\Dedicated Usage", 0, &_gpuMem); + } + + // Cache static CPU info; nominal frequency is needed to convert the + // PDH "% Processor Performance" reading into a real GHz value. + try { + auto sockets = hwinfo::getAllSockets(); + if (!sockets.empty()) { + const auto& cpu = sockets.front().CPU(); + _cpuModel = cpu.modelName(); + _baseClockMHz = cpu.regularClockSpeed_MHz(); + if (_baseClockMHz <= 0) _baseClockMHz = cpu.maxClockSpeed_MHz(); + } + } catch (...) {} + + // Cache the GPU adapter list once. Adapter set is stable on desktops; + // hot-plug eGPU swaps would need a re-enumeration but that is rare. + try { + _gpuAdaptersTemplate = EnumerateGpuAdapters(); + ANS_DBG("HealthSampler", "DXGI: %zu GPU adapter(s) discovered", + _gpuAdaptersTemplate.size()); + } catch (...) {} + + _initialized = true; + } + + void ReadFromPdh(ANSCENTER::SystemHealthInfo& out) { + out.cpuLoadPercent = Pdh_GetSingle(_cpuLoad); + + const double perfPct = Pdh_GetSingle(_cpuPerf); + if (perfPct >= 0 && _baseClockMHz > 0) + out.cpuFreqGHz = (_baseClockMHz / 1000.0) * (perfPct / 100.0); + + double diskTime = Pdh_GetSingle(_diskTime); + if (diskTime >= 0) { + // PDH "% Disk Time" can exceed 100 on multi-disk arrays; cap so the + // value matches what users see in Task Manager. + if (diskTime > 100.0) diskTime = 100.0; + out.diskLoadPercent = diskTime; + } + + const double rd = Pdh_GetSingle(_diskRead); + const double wr = Pdh_GetSingle(_diskWrite); + if (rd >= 0) out.diskReadSpeedMBs = rd / (1024.0 * 1024.0); + if (wr >= 0) out.diskWriteSpeedMBs = wr / (1024.0 * 1024.0); + + const double netRxBps = Pdh_SumWildcard(_netRx); + const double netTxBps = Pdh_SumWildcard(_netTx); + const double netBwBps = Pdh_SumWildcard(_netBw); + if (netRxBps >= 0) out.netReceiveSpeedMBs = netRxBps / (1024.0 * 1024.0); + if (netTxBps >= 0) out.netSendSpeedMBs = netTxBps / (1024.0 * 1024.0); + if (netBwBps > 0 && netRxBps >= 0 && netTxBps >= 0) { + // Current Bandwidth is bits/s; rx+tx are bytes/s — convert before dividing. + double pct = ((netRxBps + netTxBps) * 8.0 / netBwBps) * 100.0; + if (pct > 100.0) pct = 100.0; + out.netLoadPercent = pct; + } + + // Per-adapter GPU breakdown. Falls back to the old summation only if + // DXGI gave us nothing (very rare — would mean no Direct3D-capable + // adapter at all, or the call was blocked by group policy). + if (!_gpuAdaptersTemplate.empty()) { + std::vector adapters = _gpuAdaptersTemplate; // fresh accumulators + AggregateGpuCounter(_gpuUtil, adapters, /*pidFilter*/ 0, false); + AggregateGpuCounter(_gpuMem, adapters, GetCurrentProcessId(), true); + for (auto& a : adapters) if (a.loadPercent > 100.0) a.loadPercent = 100.0; + + if (GpuAdapter* primary = PickPrimaryAdapter(adapters)) { + out.gpuLoadPercent = primary->loadPercent; + out.valIGPU_MB = primary->processMemMB; + out.gpuName = primary->name; + } + + // Build per-adapter detail string. Using '|' between fields and + // ',' between adapters keeps ';' free for the outer separator. + std::ostringstream os; + os.imbue(std::locale::classic()); + os << std::fixed << std::setprecision(2); + for (size_t i = 0; i < adapters.size(); ++i) { + if (i > 0) os << ','; + std::string n = adapters[i].name; + std::replace(n.begin(), n.end(), ';', ','); + std::replace(n.begin(), n.end(), '|', '/'); + os << n << '|' + << VendorString(adapters[i].vendorId) << '|' + << adapters[i].loadPercent << '|' + << adapters[i].processMemMB << '|' + << adapters[i].dedicatedVramMB; + } + out.gpuAdaptersDetail = os.str(); + } else { + // Fallback: lose per-adapter info but still surface something. + double gpu = Pdh_SumWildcard(_gpuUtil); + if (gpu >= 0) { + if (gpu > 100.0) gpu = 100.0; + out.gpuLoadPercent = gpu; + } + const int64_t bytes = Pdh_SumWildcardLargeByPid(_gpuMem, GetCurrentProcessId()); + if (bytes >= 0) out.valIGPU_MB = bytes / (1024 * 1024); + } + } + + void FillNonPdh(ANSCENTER::SystemHealthInfo& out) { + out.cpuModel = _cpuModel; + + MEMORYSTATUSEX ms{}; + ms.dwLength = sizeof(ms); + const BOOL memOk = GlobalMemoryStatusEx(&ms); + if (memOk) { + out.memTotalGB = ms.ullTotalPhys / (1024.0 * 1024.0 * 1024.0); + out.memoryFreeGB = ms.ullAvailPhys / (1024.0 * 1024.0 * 1024.0); + out.memoryUsedGB = (ms.ullTotalPhys - ms.ullAvailPhys) / (1024.0 * 1024.0 * 1024.0); + if (ms.ullTotalPageFile > 0) { + out.swapPercent = + ((ms.ullTotalPageFile - ms.ullAvailPageFile) * 100.0) / ms.ullTotalPageFile; + } + } + + PROCESS_MEMORY_COUNTERS_EX pmc{}; + if (GetProcessMemoryInfo(GetCurrentProcess(), + reinterpret_cast(&pmc), sizeof(pmc))) { + out.valIRSS_MB = static_cast(pmc.WorkingSetSize / (1024ULL * 1024ULL)); + out.valSwap_MB = static_cast(pmc.PrivateUsage / (1024ULL * 1024ULL)); + if (memOk && ms.ullTotalPhys > 0) + out.rssPercent = (pmc.WorkingSetSize * 100.0) / static_cast(ms.ullTotalPhys); + } + + wchar_t sysDir[MAX_PATH] = {}; + const UINT n = GetSystemDirectoryW(sysDir, MAX_PATH); + if (n >= 3) { + sysDir[3] = L'\0'; // strip down to the volume root, e.g. L"C:\\" + ULARGE_INTEGER freeAvail{}, total{}, totalFree{}; + if (GetDiskFreeSpaceExW(sysDir, &freeAvail, &total, &totalFree)) { + out.diskTotalGB = total.QuadPart / (1024.0 * 1024.0 * 1024.0); + out.diskUsedGB = (total.QuadPart - totalFree.QuadPart) / (1024.0 * 1024.0 * 1024.0); + } + } + // CPU temperature: best-effort via ACPI thermal zone. Stays -1 on + // hardware that does not expose MSAcpi_ThermalZoneTemperature. + out.cpuTempDegC = ReadCpuTempCelsiusViaWmi(); + } + + std::mutex _mtx; + bool _initialized{false}; + bool _firstCollectDone{false}; + bool _hasCache{false}; + std::chrono::steady_clock::time_point _lastSample{}; + ANSCENTER::SystemHealthInfo _cache{}; + + PDH_HQUERY _query {nullptr}; + PDH_HCOUNTER _cpuLoad {nullptr}; + PDH_HCOUNTER _cpuPerf {nullptr}; + PDH_HCOUNTER _diskTime {nullptr}; + PDH_HCOUNTER _diskRead {nullptr}; + PDH_HCOUNTER _diskWrite{nullptr}; + PDH_HCOUNTER _netRx {nullptr}; + PDH_HCOUNTER _netTx {nullptr}; + PDH_HCOUNTER _netBw {nullptr}; + PDH_HCOUNTER _gpuUtil {nullptr}; + PDH_HCOUNTER _gpuMem {nullptr}; + + int64_t _baseClockMHz{-1}; + std::string _cpuModel; + std::vector _gpuAdaptersTemplate; // immutable after Init() +}; + +} // namespace + namespace ANSCENTER { + SystemHealthInfo ANSLicenseHelper::GetSystemHealthInformation() + { + ANS_DBG("HealthSampler", "GetSystemHealthInformation: enter"); + SystemHealthInfo info{}; + try { + HealthSampler::Get().Sample(info); + } catch (const std::exception& e) { + ANS_DBG("HealthSampler", "Sample threw: %s", e.what()); + } catch (...) { + ANS_DBG("HealthSampler", "Sample threw unknown exception"); + } + return info; + } + EngineType ANSCENTER::ANSLicenseHelper::CheckHardwareInformation() { ANS_DBG("HWInfo", "CheckHardwareInformation: starting hardware detection"); diff --git a/core/ANSLicensingSystem/ANSLicense.h b/core/ANSLicensingSystem/ANSLicense.h index d12a783..f84c078 100644 --- a/core/ANSLicensingSystem/ANSLicense.h +++ b/core/ANSLicensingSystem/ANSLicense.h @@ -317,6 +317,72 @@ namespace ANSCENTER int enableFeature; }; + // ------------------------------------------------------------------------ + // Real-time system telemetry. Snapshot of CPU / GPU / RAM / disk / network + // usage at the time of the call. + // + // Cheap to call frequently — values are cached for ~500 ms via a process- + // wide PDH sampler held in ANSLicense.cpp. The first call after process + // start blocks for ~150 ms because PDH counters require two samples to + // produce a delta-rate reading; subsequent calls are sub-millisecond. + // + // Fields the platform could not provide are set to -1 (or -1.0). Common + // reasons: + // - cpuTempDegC: ACPI thermal zone is unavailable / blocked on most + // consumer hardware. Use a vendor SDK (NVML / ADL / IPMI) for real + // thermals if you need them. + // - netLoadPercent: no network adapter advertises a non-zero + // "Current Bandwidth" (e.g. all interfaces are loopback / virtual). + // - valIGPU_MB: Windows < 10 19H1 does not expose GPU Process Memory + // PDH counters. + // + // Memory / disk units use 1024 base (1 GB == 2^30 bytes); throughput uses + // MB == 2^20 bytes for parity with Task Manager. + // ------------------------------------------------------------------------ + struct SystemHealthInfo { + // CPU + double cpuLoadPercent = -1.0; // 0..100, total across all logical cores + double cpuTempDegC = -1.0; // -1 if unavailable + double cpuFreqGHz = -1.0; // current clock (turbo-aware) + std::string cpuModel; // e.g. "Intel(R) Core(TM) i9-..." + + // System memory + double memTotalGB = -1.0; + double memoryUsedGB = -1.0; + double memoryFreeGB = -1.0; + double rssPercent = -1.0; // current process WS / total physical + double swapPercent = -1.0; // page file used / page file limit + + // Current process + int64_t valIRSS_MB = -1; // working-set size (MB) + int64_t valSwap_MB = -1; // private commit / page-file usage (MB) + int64_t valIGPU_MB = -1; // dedicated GPU memory used by us (MB) + + // GPU — values report the *primary* adapter only. On hybrid systems + // (e.g. Iris Xe + RTX 4090) the primary is picked deterministically: + // NVIDIA discrete > AMD discrete > whichever has the most VRAM. Per- + // adapter numbers are available in gpuAdaptersDetail below. + double gpuLoadPercent = -1.0; // 0..100 on the primary adapter only + std::string gpuName; // primary adapter model name + // Per-adapter rollup; one row per physical GPU, comma-separated. + // Row format: |||| + // Vendors: NVIDIA / AMD / Intel / Microsoft / Other + // Numbers use the C locale ('.' decimal, 2dp for load%). + std::string gpuAdaptersDetail; + + // Disk (system drive — the volume holding %SystemRoot%) + double diskLoadPercent = -1.0; + double diskReadSpeedMBs = -1.0; + double diskWriteSpeedMBs = -1.0; + double diskUsedGB = -1.0; + double diskTotalGB = -1.0; + + // Network (sum across all non-loopback interfaces) + double netLoadPercent = -1.0; + double netReceiveSpeedMBs = -1.0; + double netSendSpeedMBs = -1.0; + }; + class ANSLICENSE_API SPDLogger @@ -418,6 +484,7 @@ namespace ANSCENTER public: [[nodiscard]] static bool LicenseVerification(std::string licenseKey, int productId, std::string registrationName); [[nodiscard]] static EngineType CheckHardwareInformation(); + [[nodiscard]] static SystemHealthInfo GetSystemHealthInformation(); [[nodiscard]] static std::string ListLicenses(); [[nodiscard]] static int ActivateTrialLicense(std::string productName); [[nodiscard]] static int DeactivateLicense(std::string productName); @@ -455,6 +522,65 @@ extern "C" __declspec(dllexport) int ExtractModelZipFile(const char* modelZipFil extern "C" __declspec(dllexport) int ZipFolderToZipFile(const char* modelFolderSource,const char* modelZipFileDes, const char* password); extern "C" __declspec(dllexport) int PrepareEdgeModel(const char* modelZipFile, const char* zipFilePassword, const char* edgePassword, const char* modelName, const char* edgeSerialNumber, LStrHandle pathArray); extern "C" __declspec(dllexport) int GetSystemHardwareInformation(LStrHandle systemHardwareInformation); + +// SystemHealthParameters cluster, one scalar at a time. Pass nullptr for any +// field you do not care about. Returns 1 on success, 0 on failure. Cached +// internally so calling at LabVIEW UI rate (e.g. 1 Hz) is essentially free. +extern "C" __declspec(dllexport) int GetSystemHealthParameters( + double* cpuLoadPercent, + double* cpuTempDegC, + double* cpuFreqGHz, + LStrHandle cpuModel, + double* memTotalGB, + double* rssPercent, + double* swapPercent, + double* gpuLoadPercent, + int64_t* valIRSS_MB, + int64_t* valSwap_MB, + int64_t* valIGPU_MB, + double* memoryUsedGB, + double* memoryFreeGB, + double* diskLoadPercent, + double* diskReadSpeedMBs, + double* diskWriteSpeedMBs, + double* diskUsedGB, + double* diskTotalGB, + double* netLoadPercent, + double* netReceiveSpeedMBs, + double* netSendSpeedMBs, + LStrHandle gpuName, // primary adapter name (e.g. "NVIDIA RTX 4090"); pass nullptr to skip + LStrHandle gpuAdaptersDetail); // per-adapter rollup; pass nullptr to skip — see SystemHealthInfo for format + +// Same payload as GetSystemHealthParameters, packed into a single string with +// ';' separators in this exact order: +// 1 cpuLoadPercent (%) +// 2 cpuTempDegC (°C, -1 if unavailable) +// 3 cpuFreqGHz (GHz) +// 4 cpuModel (string; any literal ';' in the model is rewritten to ',') +// 5 memTotalGB +// 6 rssPercent (%) +// 7 swapPercent (%) +// 8 gpuLoadPercent (%) +// 9 valIRSS_MB (current process working set, MB) +// 10 valSwap_MB (current process page-file usage, MB) +// 11 valIGPU_MB (current process GPU dedicated memory, MB) +// 12 memoryUsedGB +// 13 memoryFreeGB +// 14 diskLoadPercent (%) +// 15 diskReadSpeedMBs +// 16 diskWriteSpeedMBs +// 17 diskUsedGB +// 18 diskTotalGB +// 19 netLoadPercent (%) +// 20 netReceiveSpeedMBs +// 21 netSendSpeedMBs +// 22 gpuName (primary adapter model, e.g. "NVIDIA RTX 4090") +// 23 gpuAdaptersDetail (per-adapter rollup — see SystemHealthInfo struct) +// Numbers use the C locale ('.' decimal point) and 2-decimal fixed format. +// Any literal ';' in gpuName / gpuAdaptersDetail is rewritten to ',' so the +// outer separator is never ambiguous. +// Returns 1 on success, 0 on failure. +extern "C" __declspec(dllexport) int GetSystemHealthParametersString(LStrHandle outString); extern "C" __declspec(dllexport) int ReleaseLoggers(); extern "C" __declspec(dllexport) int ListANSLicenses(std::string &listLicense); extern "C" __declspec(dllexport) int ListANSLicenses_LV(LStrHandle ansProductLicenses); diff --git a/core/ANSLicensingSystem/CMakeLists.txt b/core/ANSLicensingSystem/CMakeLists.txt index cd06b30..7bcf164 100644 --- a/core/ANSLicensingSystem/CMakeLists.txt +++ b/core/ANSLicensingSystem/CMakeLists.txt @@ -52,6 +52,9 @@ target_link_libraries(ANSLicensingSystem PRIVATE bz2.lib zip.lib libboost_log-vc143-mt-x64-1_82.lib shlwapi.lib winhttp.lib + pdh.lib psapi.lib + wbemuuid.lib oleaut32.lib ole32.lib + dxgi.lib ${WIN_COMMON_LIBS} ) diff --git a/core/ANSLicensingSystem/dllmain.cpp b/core/ANSLicensingSystem/dllmain.cpp index e668f4b..e5fc96a 100644 --- a/core/ANSLicensingSystem/dllmain.cpp +++ b/core/ANSLicensingSystem/dllmain.cpp @@ -2,6 +2,10 @@ #include "pch.h" #include "ANSLicense.h" #include "Utility.h" +#include +#include +#include +#include BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, @@ -369,6 +373,137 @@ extern "C" __declspec(dllexport) int GetSystemHardwareInformation(LStrHandle sys } } +// SystemHealthParameters cluster, decomposed into individual scalar outputs so +// LabVIEW can wire each indicator straight to a Call Library Function Node. +// Any pointer may be nullptr (LabVIEW will pass nullptr for cluster fields the +// caller hasn't bound). cpuModel is optional but recommended; pass an LStrHandle +// or nullptr to skip. Returns 1 on success, 0 on hard failure. +extern "C" __declspec(dllexport) int GetSystemHealthParameters( + double* cpuLoadPercent, + double* cpuTempDegC, + double* cpuFreqGHz, + LStrHandle cpuModel, + double* memTotalGB, + double* rssPercent, + double* swapPercent, + double* gpuLoadPercent, + int64_t* valIRSS_MB, + int64_t* valSwap_MB, + int64_t* valIGPU_MB, + double* memoryUsedGB, + double* memoryFreeGB, + double* diskLoadPercent, + double* diskReadSpeedMBs, + double* diskWriteSpeedMBs, + double* diskUsedGB, + double* diskTotalGB, + double* netLoadPercent, + double* netReceiveSpeedMBs, + double* netSendSpeedMBs, + LStrHandle gpuName, + LStrHandle gpuAdaptersDetail) +{ + try { + const ANSCENTER::SystemHealthInfo info = + ANSCENTER::ANSLicenseHelper::GetSystemHealthInformation(); + + auto writeLStr = [](LStrHandle handle, const std::string& s) { + if (!handle) return; + const int size = static_cast(s.length()); + const MgErr err = DSSetHandleSize(handle, sizeof(int32) + size * sizeof(uChar)); + if (err == noErr) { + (*handle)->cnt = size; + if (size > 0) memcpy((*handle)->str, s.c_str(), size); + } + }; + + if (cpuLoadPercent) *cpuLoadPercent = info.cpuLoadPercent; + if (cpuTempDegC) *cpuTempDegC = info.cpuTempDegC; + if (cpuFreqGHz) *cpuFreqGHz = info.cpuFreqGHz; + if (memTotalGB) *memTotalGB = info.memTotalGB; + if (rssPercent) *rssPercent = info.rssPercent; + if (swapPercent) *swapPercent = info.swapPercent; + if (gpuLoadPercent) *gpuLoadPercent = info.gpuLoadPercent; + if (valIRSS_MB) *valIRSS_MB = info.valIRSS_MB; + if (valSwap_MB) *valSwap_MB = info.valSwap_MB; + if (valIGPU_MB) *valIGPU_MB = info.valIGPU_MB; + if (memoryUsedGB) *memoryUsedGB = info.memoryUsedGB; + if (memoryFreeGB) *memoryFreeGB = info.memoryFreeGB; + if (diskLoadPercent) *diskLoadPercent = info.diskLoadPercent; + if (diskReadSpeedMBs) *diskReadSpeedMBs = info.diskReadSpeedMBs; + if (diskWriteSpeedMBs) *diskWriteSpeedMBs = info.diskWriteSpeedMBs; + if (diskUsedGB) *diskUsedGB = info.diskUsedGB; + if (diskTotalGB) *diskTotalGB = info.diskTotalGB; + if (netLoadPercent) *netLoadPercent = info.netLoadPercent; + if (netReceiveSpeedMBs) *netReceiveSpeedMBs = info.netReceiveSpeedMBs; + if (netSendSpeedMBs) *netSendSpeedMBs = info.netSendSpeedMBs; + + writeLStr(cpuModel, info.cpuModel); + writeLStr(gpuName, info.gpuName); + writeLStr(gpuAdaptersDetail, info.gpuAdaptersDetail); + return 1; + } catch (...) { + return 0; + } +} + +extern "C" __declspec(dllexport) int GetSystemHealthParametersString(LStrHandle outString) { + try { + if (!outString) return 0; + + const ANSCENTER::SystemHealthInfo info = + ANSCENTER::ANSLicenseHelper::GetSystemHealthInformation(); + + // Keep ';' as a clean separator: rewrite any literal ';' inside any + // free-form string field (rare, but vendor strings aren't safe by + // contract). + auto sanitize = [](std::string s) { + std::replace(s.begin(), s.end(), ';', ','); + return s; + }; + const std::string model = sanitize(info.cpuModel); + const std::string gpuName_ = sanitize(info.gpuName); + const std::string gpuDetail = sanitize(info.gpuAdaptersDetail); + + std::ostringstream os; + os.imbue(std::locale::classic()); // force '.' decimal regardless of system locale + os << std::fixed << std::setprecision(2); + os << info.cpuLoadPercent << ';' + << info.cpuTempDegC << ';' + << info.cpuFreqGHz << ';' + << model << ';' + << info.memTotalGB << ';' + << info.rssPercent << ';' + << info.swapPercent << ';' + << info.gpuLoadPercent << ';' + << info.valIRSS_MB << ';' + << info.valSwap_MB << ';' + << info.valIGPU_MB << ';' + << info.memoryUsedGB << ';' + << info.memoryFreeGB << ';' + << info.diskLoadPercent << ';' + << info.diskReadSpeedMBs << ';' + << info.diskWriteSpeedMBs << ';' + << info.diskUsedGB << ';' + << info.diskTotalGB << ';' + << info.netLoadPercent << ';' + << info.netReceiveSpeedMBs << ';' + << info.netSendSpeedMBs << ';' + << gpuName_ << ';' + << gpuDetail; + + const std::string s = os.str(); + const int size = static_cast(s.length()); + const MgErr err = DSSetHandleSize(outString, sizeof(int32) + size * sizeof(uChar)); + if (err != noErr) return 0; + (*outString)->cnt = size; + if (size > 0) memcpy((*outString)->str, s.c_str(), size); + return 1; + } catch (...) { + return 0; + } +} + extern "C" __declspec(dllexport) int ReleaseLoggers() { try { bool result = ANSCENTER::ANSLSHelper::ReleaseLogger(); diff --git a/modules/ANSCV/ANSFLV.cpp b/modules/ANSCV/ANSFLV.cpp index 8346b09..4fa7331 100644 --- a/modules/ANSCV/ANSFLV.cpp +++ b/modules/ANSCV/ANSFLV.cpp @@ -224,13 +224,11 @@ namespace ANSCENTER { } } + // Honest image-equality check. Same buffer pointer (fast path) or pixel + // compare (fallback). Camera-aliveness uses getLastFrameAgeMs() — separate + // concern. With "same image -> same pts" invariant, callers usually just + // compare pts and skip this function entirely. bool ANSFLVClient::areImagesIdentical(const cv::Mat& img1, const cv::Mat& img2) { - // Use decoder frame age — returns "stale" only if no decoder output for 5+ seconds. - double ageMs = _playerClient->getLastFrameAgeMs(); - if (ageMs > 5000.0) return true; // Truly stale - if (ageMs > 0.0) return false; // Decoder alive - - // Fallback for startup (no frame decoded yet) 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; @@ -288,17 +286,19 @@ namespace ANSCENTER { return _pLastFrame; } - // Use PTS to detect duplicate frames (much faster than pixel comparison) - if (currentPts == _pts && !_pLastFrame.empty()) { + // Duplicate detection via _lastInnerPts (separate from _pts). Invariant: + // same image -> same pts. Camera-alive check uses getLastFrameAgeMs(). + 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 cursor and bump _pts monotonically. + _lastInnerPts = currentPts; + _pts++; + pts = _pts; // Handle non-rotated case if (_imageRotateDeg == 0) { @@ -636,6 +636,14 @@ namespace ANSCENTER { std::lock_guard lock(_mutex); _playerClient->setTargetFPS(intervalMs); // 0=no limit, 100=~10FPS, 200=~5FPS } + void ANSFLVClient::SetJitterBufferMs(int ms) { + std::lock_guard lock(_mutex); + _playerClient->setJitterBufferMs(ms); + ANS_DBG("FLV_Lifecycle", "SetJitterBufferMs: ms=%d url=%s", ms, _url.c_str()); + } + int ANSFLVClient::GetJitterBufferMs() const { + return _playerClient->getJitterBufferMs(); + } void ANSFLVClient::SetNV12FastPath(bool enable) { std::lock_guard lock(_mutex); _useNV12FastPath = enable; @@ -730,11 +738,11 @@ extern "C" __declspec(dllexport) int GetFLVStrImage(ANSCENTER::ANSFLVClient * *H else return 0; } catch (const std::exception& e) { - std::cerr << "Error getting FLV image: " << e.what() << std::endl; + ANS_DBG("FLV_GetImage", "exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error getting FLV image: Unknown exception." << std::endl; + ANS_DBG("FLV_GetImage", "unknown exception"); return 0; } } @@ -752,27 +760,27 @@ extern "C" __declspec(dllexport) int GetFLVImage(ANSCENTER::ANSFLVClient * *Hand return 1; } else { - std::cerr << "Error resizing jpegImage handle: " << error << std::endl; + ANS_DBG("FLV_GetImage", "DSSetHandleSize failed: error=%d", (int)error); return 0; } } else { - std::cerr << "No image data retrieved from FLV client." << std::endl; + ANS_DBG("FLV_GetImage", "no image data retrieved (jpegString empty)"); return 0; } } catch (const std::exception& e) { - std::cerr << "Error getting FLV image: " << e.what() << std::endl; + ANS_DBG("FLV_GetImage", "exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error getting FLV image: Unknown exception." << std::endl; + ANS_DBG("FLV_GetImage", "unknown exception"); return 0; } } extern "C" __declspec(dllexport) int GetFLVCVImage(ANSCENTER::ANSFLVClient** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image) { if (!Handle || !*Handle || !image) { - std::cerr << "Error: Invalid input parameters in GetFLVCVImage" << std::endl; + ANS_DBG("FLV_GetImage", "invalid input parameters"); return -1; } @@ -812,15 +820,15 @@ extern "C" __declspec(dllexport) int GetFLVCVImage(ANSCENTER::ANSFLVClient** Han return 1; } catch (const cv::Exception& e) { - std::cerr << "OpenCV exception in GetFLVCVImage: " << e.what() << std::endl; + ANS_DBG("FLV_GetImage", "OpenCV exception: %s", e.what()); return -2; } catch (const std::exception& e) { - std::cerr << "Exception in GetFLVCVImage: " << e.what() << std::endl; + ANS_DBG("FLV_GetImage", "GetFLVCVImage exception: %s", e.what()); return -2; } catch (...) { - std::cerr << "Unknown exception in GetFLVCVImage" << std::endl; + ANS_DBG("FLV_GetImage", "GetFLVCVImage unknown exception"); return -2; } } @@ -833,11 +841,11 @@ extern "C" __declspec(dllexport) int StartFLV(ANSCENTER::ANSFLVClient * *Handle) else return 0; } catch (const std::exception& e) { - std::cerr << "Error starting FLV client: " << e.what() << std::endl; + ANS_DBG("FLV_Lifecycle", "StartFLV exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error starting FLV client: Unknown exception." << std::endl; + ANS_DBG("FLV_Lifecycle", "StartFLV unknown exception"); return 0; } } @@ -849,11 +857,11 @@ extern "C" __declspec(dllexport) int ReconnectFLV(ANSCENTER::ANSFLVClient * *Han else return 0; } catch (const std::exception& e) { - std::cerr << "Error reconnecting FLV client: " << e.what() << std::endl; + ANS_DBG("FLV_Lifecycle", "ReconnectFLV exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error reconnecting FLV client: Unknown exception." << std::endl; + ANS_DBG("FLV_Lifecycle", "ReconnectFLV unknown exception"); return 0; } } @@ -865,11 +873,11 @@ extern "C" __declspec(dllexport) int StopFLV(ANSCENTER::ANSFLVClient * *Handle) else return 0; } catch (const std::exception& e) { - std::cerr << "Error stopping FLV client: " << e.what() << std::endl; + ANS_DBG("FLV_Lifecycle", "StopFLV exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error stopping FLV client: Unknown exception." << std::endl; + ANS_DBG("FLV_Lifecycle", "StopFLV unknown exception"); return 0; } } @@ -980,6 +988,18 @@ extern "C" __declspec(dllexport) void SetFLVDisplayResolution(ANSCENTER::ANSFLVC (*Handle)->SetDisplayResolution(width, height); } catch (...) { } } +extern "C" __declspec(dllexport) void SetFLVJitterBuffer(ANSCENTER::ANSFLVClient** Handle, int jitterMs) { + if (Handle == nullptr || *Handle == nullptr) return; + try { + (*Handle)->SetJitterBufferMs(jitterMs); + } catch (...) { } +} +extern "C" __declspec(dllexport) int GetFLVJitterBuffer(ANSCENTER::ANSFLVClient** Handle) { + if (Handle == nullptr || *Handle == nullptr) return -1; + try { + return (*Handle)->GetJitterBufferMs(); + } catch (...) { return -1; } +} extern "C" __declspec(dllexport) void SetFLVTargetFPS(ANSCENTER::ANSFLVClient** Handle, double intervalMs) { if (Handle == nullptr || *Handle == nullptr) return; try { @@ -1012,21 +1032,21 @@ extern "C" __declspec(dllexport) int GetFLVImage_V2(uint64_t handleVal, int& wid return 1; } else { - std::cerr << "Error resizing jpegImage handle: " << error << std::endl; + ANS_DBG("FLV_GetImage", "DSSetHandleSize failed: error=%d", (int)error); return 0; } } else { - std::cerr << "No image data retrieved from FLV client." << std::endl; + ANS_DBG("FLV_GetImage", "no image data retrieved (jpegString empty)"); return 0; } } catch (const std::exception& e) { - std::cerr << "Error getting FLV image: " << e.what() << std::endl; + ANS_DBG("FLV_GetImage", "exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error getting FLV image: Unknown exception." << std::endl; + ANS_DBG("FLV_GetImage", "unknown exception"); return 0; } } @@ -1065,15 +1085,15 @@ extern "C" __declspec(dllexport) int GetFLVCVImage_V2(uint64_t handleVal, int& w return 1; } catch (const cv::Exception& e) { - std::cerr << "OpenCV exception in GetFLVCVImage_V2: " << e.what() << std::endl; + ANS_DBG("FLV_GetImage", "GetFLVCVImage_V2 OpenCV exception: %s", e.what()); return -2; } catch (const std::exception& e) { - std::cerr << "Exception in GetFLVCVImage_V2: " << e.what() << std::endl; + ANS_DBG("FLV_GetImage", "GetFLVCVImage_V2 exception: %s", e.what()); return -2; } catch (...) { - std::cerr << "Unknown exception in GetFLVCVImage_V2" << std::endl; + ANS_DBG("FLV_GetImage", "GetFLVCVImage_V2 unknown exception"); return -2; } } @@ -1086,11 +1106,11 @@ extern "C" __declspec(dllexport) int StartFLV_V2(uint64_t handleVal) { else return 0; } catch (const std::exception& e) { - std::cerr << "Error starting FLV client: " << e.what() << std::endl; + ANS_DBG("FLV_Lifecycle", "StartFLV exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error starting FLV client: Unknown exception." << std::endl; + ANS_DBG("FLV_Lifecycle", "StartFLV unknown exception"); return 0; } } @@ -1103,11 +1123,11 @@ extern "C" __declspec(dllexport) int ReconnectFLV_V2(uint64_t handleVal) { else return 0; } catch (const std::exception& e) { - std::cerr << "Error reconnecting FLV client: " << e.what() << std::endl; + ANS_DBG("FLV_Lifecycle", "ReconnectFLV exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error reconnecting FLV client: Unknown exception." << std::endl; + ANS_DBG("FLV_Lifecycle", "ReconnectFLV unknown exception"); return 0; } } @@ -1120,11 +1140,11 @@ extern "C" __declspec(dllexport) int StopFLV_V2(uint64_t handleVal) { else return 0; } catch (const std::exception& e) { - std::cerr << "Error stopping FLV client: " << e.what() << std::endl; + ANS_DBG("FLV_Lifecycle", "StopFLV exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error stopping FLV client: Unknown exception." << std::endl; + ANS_DBG("FLV_Lifecycle", "StopFLV unknown exception"); return 0; } } diff --git a/modules/ANSCV/ANSFLV.h b/modules/ANSCV/ANSFLV.h index d48efb5..ce58369 100644 --- a/modules/ANSCV/ANSFLV.h +++ b/modules/ANSCV/ANSFLV.h @@ -35,6 +35,9 @@ namespace ANSCENTER std::string _lastJpegImage; int _imageWidth, _imageHeight; int64_t _pts; + // Last currentPts seen from inner getImage(). Used for duplicate + // detection separately from _pts so "same image -> same pts" holds. + int64_t _lastInnerPts = -1; bool _isPlaying; bool _useNV12FastPath = false; // false = original stable CPU path, true = NV12 GPU fast path std::recursive_mutex _mutex; @@ -73,6 +76,10 @@ namespace ANSCENTER void SetDisplayResolution(int width, int height); // Set display output size; 0,0 = original (no resize) void SetImageQuality(int mode); // 0=fast (AI), 1=quality (display) void SetTargetFPS(double intervalMs); // Set min interval between processed frames in ms (0 = no limit, 100 = ~10 FPS, 200 = ~5 FPS) + // Jitter buffer for the BGR delivery path (forwarded to CVideoPlayer). + // 0 = no buffering (today's getLatest semantics). Default 200 ms. + void SetJitterBufferMs(int ms); + int GetJitterBufferMs() const; void SetNV12FastPath(bool enable); // true = NV12 GPU fast path, false = original CPU path (stable) bool IsNV12FastPath() const { return _useNV12FastPath; } AVFrame* GetNV12Frame(); // Returns cloned NV12 frame for GPU fast-path (caller must av_frame_free) @@ -114,4 +121,6 @@ extern "C" __declspec(dllexport) void SetFLVImageQuality(ANSCENTER::ANSFLVClient extern "C" __declspec(dllexport) void SetFLVDisplayResolution(ANSCENTER::ANSFLVClient** Handle, int width, int height); extern "C" __declspec(dllexport) void SetFLVTargetFPS(ANSCENTER::ANSFLVClient** Handle, double intervalMs); extern "C" __declspec(dllexport) void SetFLVNV12FastPath(ANSCENTER::ANSFLVClient** Handle, int enable); +extern "C" __declspec(dllexport) void SetFLVJitterBuffer(ANSCENTER::ANSFLVClient** Handle, int jitterMs); +extern "C" __declspec(dllexport) int GetFLVJitterBuffer(ANSCENTER::ANSFLVClient** Handle); #endif \ No newline at end of file diff --git a/modules/ANSCV/ANSFilePlayer.cpp b/modules/ANSCV/ANSFilePlayer.cpp index af611e3..cb3f761 100644 --- a/modules/ANSCV/ANSFilePlayer.cpp +++ b/modules/ANSCV/ANSFilePlayer.cpp @@ -219,6 +219,14 @@ namespace ANSCENTER { _displayWidth = width; _displayHeight = height; } + void ANSFILEPLAYER::SetJitterBufferMs(int ms) { + std::lock_guard lock(_mutex); + if (_playerClient) _playerClient->setJitterBufferMs(ms); + ANS_DBG("FilePlayer_Lifecycle", "SetJitterBufferMs: ms=%d url=%s", ms, _url.c_str()); + } + int ANSFILEPLAYER::GetJitterBufferMs() const { + return _playerClient ? _playerClient->getJitterBufferMs() : 0; + } cv::Mat ANSFILEPLAYER::GetInferenceImage() { std::lock_guard lock(_mutex); return _inferenceImage; @@ -324,7 +332,7 @@ extern "C" __declspec(dllexport) int GetFilePlayerStrImage(ANSCENTER::ANSFILEPLA else return 0; } catch (const std::exception& e) { - std::cerr << "Error getting image data from FilePlayer client: " << e.what() << std::endl; + ANS_DBG("FilePlayer_GetImage", "exception: %s", e.what()); return -1; } catch (...) { @@ -351,18 +359,18 @@ extern "C" __declspec(dllexport) int GetFilePlayerImage(ANSCENTER::ANSFILEPLAYER } else { // Return failure if there was an error in resizing the handle - std::cerr << "Error resizing jpegImage handle: " << error << std::endl; + ANS_DBG("FilePlayer_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 FilePlayer client." << std::endl; + ANS_DBG("FilePlayer_GetImage", "no image data retrieved (jpegString empty)"); return 0; } } catch (const std::exception& e) { - std::cerr << "Error getting image data from FilePlayer client: " << e.what() << std::endl; + ANS_DBG("FilePlayer_GetImage", "exception: %s", e.what()); return -1; } catch (...) { @@ -371,7 +379,7 @@ extern "C" __declspec(dllexport) int GetFilePlayerImage(ANSCENTER::ANSFILEPLAYER } extern "C" __declspec(dllexport) int GetFilePlayerCVImage(ANSCENTER::ANSFILEPLAYER** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image) { if (!Handle || !(*Handle) || !image) { - std::cerr << "Error: Invalid input parameters in GetFilePlayerCVImage." << std::endl; + ANS_DBG("FilePlayer_GetImage", "GetFilePlayerCVImage invalid input parameters"); return -1; } try { @@ -439,15 +447,15 @@ extern "C" __declspec(dllexport) int GetFilePlayerCVImage(ANSCENTER::ANSFILEPLAY return 1; } catch (const cv::Exception& e) { - std::cerr << "OpenCV exception in GetFilePlayerCVImage: " << e.what() << std::endl; + ANS_DBG("FilePlayer_GetImage", "GetFilePlayerCVImage OpenCV exception: %s", e.what()); return -2; } catch (const std::exception& e) { - std::cerr << "Exception in GetFilePlayerCVImage: " << e.what() << std::endl; + ANS_DBG("FilePlayer_GetImage", "GetFilePlayerCVImage exception: %s", e.what()); return -2; } catch (...) { - std::cerr << "Unknown exception in GetFilePlayerCVImage" << std::endl; + ANS_DBG("FilePlayer_GetImage", "GetFilePlayerCVImage unknown exception"); return -2; } } @@ -460,7 +468,7 @@ extern "C" __declspec(dllexport) int StartFilePlayer(ANSCENTER::ANSFILEPLAYER** else return 0; } catch (const std::exception& e) { - std::cerr << "Error starting file player: " << e.what() << std::endl; + ANS_DBG("FilePlayer_Lifecycle", "StartFilePlayer exception: %s", e.what()); return -1; } catch (...) { @@ -475,7 +483,7 @@ extern "C" __declspec(dllexport) int ReconnectFilePlayer(ANSCENTER::ANSFILEPLAYE else return 0; } catch (const std::exception& e) { - std::cerr << "Error reconnecting file player: " << e.what() << std::endl; + ANS_DBG("FilePlayer_Lifecycle", "ReconnectFilePlayer exception: %s", e.what()); return -1; } catch (...) { @@ -490,7 +498,7 @@ extern "C" __declspec(dllexport) int StopFilePlayer(ANSCENTER::ANSFILEPLAYER** H else return 0; } catch (const std::exception& e) { - std::cerr << "Error stopping file player: " << e.what() << std::endl; + ANS_DBG("FilePlayer_Lifecycle", "StopFilePlayer exception: %s", e.what()); return -1; } catch (...) { @@ -505,7 +513,7 @@ extern "C" __declspec(dllexport) int IsFilePlayerPaused(ANSCENTER::ANSFILEPLAYER else return 0; } catch (const std::exception& e) { - std::cerr << "Error checking if file player is paused: " << e.what() << std::endl; + ANS_DBG("FilePlayer_Lifecycle", "IsFilePlayerPaused exception: %s", e.what()); return -1; } catch (...) { @@ -520,7 +528,7 @@ extern "C" __declspec(dllexport) int IsFilePlayerRunning(ANSCENTER::ANSFILEPLAYE else return 0; } catch (const std::exception& e) { - std::cerr << "Error checking if file player is running: " << e.what() << std::endl; + ANS_DBG("FilePlayer_Lifecycle", "IsFilePlayerRunning exception: %s", e.what()); return -1; } catch (...) { @@ -535,7 +543,7 @@ extern "C" __declspec(dllexport) int IsFilePlayerRecording(ANSCENTER::ANSFILEPLA else return 0; } catch (const std::exception& e) { - std::cerr << "Error checking if file player is recording: " << e.what() << std::endl; + ANS_DBG("FilePlayer_Lifecycle", "IsFilePlayerRecording exception: %s", e.what()); return -1; } catch (...) { @@ -559,7 +567,7 @@ extern "C" __declspec(dllexport) void EnableFilePlayerAudioVolume(ANSCENTER::ANS (*Handle)->EnableAudio(audioStatus); } catch (const std::exception& e) { - std::cerr << "Error enabling audio for file player: " << e.what() << std::endl; + ANS_DBG("FilePlayer_Lifecycle", "EnableFilePlayerAudio exception: %s", e.what()); } catch (...) { } } @@ -578,7 +586,7 @@ extern "C" __declspec(dllexport) int SetBBoxFilePlayer(ANSCENTER::ANSFILEPLAYER* return 1; } catch (const std::exception& e) { - std::cerr << "Error setting bounding box for file player: " << e.what() << std::endl; + ANS_DBG("FilePlayer_Cfg", "SetBBoxFilePlayer exception: %s", e.what()); return -1; } catch (...) { @@ -594,7 +602,7 @@ extern "C" __declspec(dllexport) int SetCropFlagFilePlayer(ANSCENTER::ANSFILEPLA return 1; } catch (const std::exception& e) { - std::cerr << "Error setting crop flag for file player: " << e.what() << std::endl; + ANS_DBG("FilePlayer_Cfg", "SetCropFlagFilePlayer exception: %s", e.what()); return -1; } catch (...) { @@ -621,17 +629,17 @@ extern "C" __declspec(dllexport) int GetFilePlayerImage_V2(uint64_t handleVal, i return 1; } else { - std::cerr << "Error resizing jpegImage handle: " << error << std::endl; + ANS_DBG("FilePlayer_GetImage", "DSSetHandleSize failed: error=%d", (int)error); return 0; } } else { - std::cerr << "No image data retrieved from FilePlayer client." << std::endl; + ANS_DBG("FilePlayer_GetImage", "no image data retrieved (jpegString empty)"); return 0; } } catch (const std::exception& e) { - std::cerr << "Error getting image data from FilePlayer client: " << e.what() << std::endl; + ANS_DBG("FilePlayer_GetImage", "exception: %s", e.what()); return -1; } catch (...) { @@ -704,15 +712,15 @@ extern "C" __declspec(dllexport) int GetFilePlayerCVImage_V2(uint64_t handleVal, return 1; } catch (const cv::Exception& e) { - std::cerr << "OpenCV exception in GetFilePlayerCVImage_V2: " << e.what() << std::endl; + ANS_DBG("FilePlayer_GetImage", "GetFilePlayerCVImage_V2 OpenCV exception: %s", e.what()); return -2; } catch (const std::exception& e) { - std::cerr << "Exception in GetFilePlayerCVImage_V2: " << e.what() << std::endl; + ANS_DBG("FilePlayer_GetImage", "GetFilePlayerCVImage_V2 exception: %s", e.what()); return -2; } catch (...) { - std::cerr << "Unknown exception in GetFilePlayerCVImage_V2" << std::endl; + ANS_DBG("FilePlayer_GetImage", "GetFilePlayerCVImage_V2 unknown exception"); return -2; } } @@ -726,7 +734,7 @@ extern "C" __declspec(dllexport) int StartFilePlayer_V2(uint64_t handleVal) { else return 0; } catch (const std::exception& e) { - std::cerr << "Error starting file player: " << e.what() << std::endl; + ANS_DBG("FilePlayer_Lifecycle", "StartFilePlayer exception: %s", e.what()); return -1; } catch (...) { @@ -743,7 +751,7 @@ extern "C" __declspec(dllexport) int ReconnectFilePlayer_V2(uint64_t handleVal) else return 0; } catch (const std::exception& e) { - std::cerr << "Error reconnecting file player: " << e.what() << std::endl; + ANS_DBG("FilePlayer_Lifecycle", "ReconnectFilePlayer exception: %s", e.what()); return -1; } catch (...) { @@ -760,7 +768,7 @@ extern "C" __declspec(dllexport) int StopFilePlayer_V2(uint64_t handleVal) { else return 0; } catch (const std::exception& e) { - std::cerr << "Error stopping file player: " << e.what() << std::endl; + ANS_DBG("FilePlayer_Lifecycle", "StopFilePlayer exception: %s", e.what()); return -1; } catch (...) { @@ -777,7 +785,7 @@ extern "C" __declspec(dllexport) int IsFilePlayerPaused_V2(uint64_t handleVal) { else return 0; } catch (const std::exception& e) { - std::cerr << "Error checking if file player is paused: " << e.what() << std::endl; + ANS_DBG("FilePlayer_Lifecycle", "IsFilePlayerPaused exception: %s", e.what()); return -1; } catch (...) { @@ -794,7 +802,7 @@ extern "C" __declspec(dllexport) int IsFilePlayerRunning_V2(uint64_t handleVal) else return 0; } catch (const std::exception& e) { - std::cerr << "Error checking if file player is running: " << e.what() << std::endl; + ANS_DBG("FilePlayer_Lifecycle", "IsFilePlayerRunning exception: %s", e.what()); return -1; } catch (...) { @@ -811,7 +819,7 @@ extern "C" __declspec(dllexport) int IsFilePlayerRecording_V2(uint64_t handleVal else return 0; } catch (const std::exception& e) { - std::cerr << "Error checking if file player is recording: " << e.what() << std::endl; + ANS_DBG("FilePlayer_Lifecycle", "IsFilePlayerRecording exception: %s", e.what()); return -1; } catch (...) { @@ -837,7 +845,7 @@ extern "C" __declspec(dllexport) void EnableFilePlayerAudioVolume_V2(uint64_t ha h->EnableAudio(audioStatus); } catch (const std::exception& e) { - std::cerr << "Error enabling audio for file player: " << e.what() << std::endl; + ANS_DBG("FilePlayer_Lifecycle", "EnableFilePlayerAudio exception: %s", e.what()); } catch (...) { } } @@ -860,7 +868,7 @@ extern "C" __declspec(dllexport) int SetBBoxFilePlayer_V2(uint64_t handleVal, in return 1; } catch (const std::exception& e) { - std::cerr << "Error setting bounding box for file player: " << e.what() << std::endl; + ANS_DBG("FilePlayer_Cfg", "SetBBoxFilePlayer exception: %s", e.what()); return -1; } catch (...) { @@ -878,7 +886,7 @@ extern "C" __declspec(dllexport) int SetCropFlagFilePlayer_V2(uint64_t handleVal return 1; } catch (const std::exception& e) { - std::cerr << "Error setting crop flag for file player: " << e.what() << std::endl; + ANS_DBG("FilePlayer_Cfg", "SetCropFlagFilePlayer exception: %s", e.what()); return -1; } catch (...) { @@ -892,3 +900,15 @@ extern "C" __declspec(dllexport) void SetFilePlayerDisplayResolution(ANSCENTER:: (*Handle)->SetDisplayResolution(width, height); } catch (...) { } } +extern "C" __declspec(dllexport) void SetFilePlayerJitterBuffer(ANSCENTER::ANSFILEPLAYER** Handle, int jitterMs) { + if (Handle == nullptr || *Handle == nullptr) return; + try { + (*Handle)->SetJitterBufferMs(jitterMs); + } catch (...) { } +} +extern "C" __declspec(dllexport) int GetFilePlayerJitterBuffer(ANSCENTER::ANSFILEPLAYER** Handle) { + if (Handle == nullptr || *Handle == nullptr) return -1; + try { + return (*Handle)->GetJitterBufferMs(); + } catch (...) { return -1; } +} diff --git a/modules/ANSCV/ANSFilePlayer.h b/modules/ANSCV/ANSFilePlayer.h index 5914a99..510f91a 100644 --- a/modules/ANSCV/ANSFilePlayer.h +++ b/modules/ANSCV/ANSFilePlayer.h @@ -68,6 +68,10 @@ namespace ANSCENTER if (mode > 360) _imageRotateDeg = 360; }; void SetDisplayResolution(int width, int height); + // Jitter buffer for the BGR delivery path (forwarded to CFilePlayer -> + // CVideoPlayer). 0 = no buffering. Default 200 ms (set by CVideoPlayer). + void SetJitterBufferMs(int ms); + int GetJitterBufferMs() const; [[nodiscard]] cv::Mat GetImage(int& width, int& height, int64_t& pts); [[nodiscard]] cv::Mat GetInferenceImage(); [[nodiscard]] std::string GetJpegImage(int& width, int& height, int64_t& pts); @@ -99,6 +103,8 @@ extern "C" __declspec(dllexport) void SetFilePlayerImageRotation(ANSCENTER::ANSF extern "C" __declspec(dllexport) int SetBBoxFilePlayer(ANSCENTER::ANSFILEPLAYER** Handle, int x, int y, int width, int height); extern "C" __declspec(dllexport) int SetCropFlagFilePlayer(ANSCENTER::ANSFILEPLAYER** Handle, int cropFlag); extern "C" __declspec(dllexport) void SetFilePlayerDisplayResolution(ANSCENTER::ANSFILEPLAYER** Handle, int width, int height); +extern "C" __declspec(dllexport) void SetFilePlayerJitterBuffer(ANSCENTER::ANSFILEPLAYER** Handle, int jitterMs); +extern "C" __declspec(dllexport) int GetFilePlayerJitterBuffer(ANSCENTER::ANSFILEPLAYER** Handle); // V2 functions: accept uint64_t handleVal by value (LabVIEW-safe) extern "C" __declspec(dllexport) int GetFilePlayerImage_V2(uint64_t handleVal, int& width, int& height, int64_t& timeStamp, LStrHandle jpegImage); diff --git a/modules/ANSCV/ANSMJPEG.cpp b/modules/ANSCV/ANSMJPEG.cpp index a6bc807..18c2ad1 100644 --- a/modules/ANSCV/ANSMJPEG.cpp +++ b/modules/ANSCV/ANSMJPEG.cpp @@ -214,11 +214,11 @@ namespace ANSCENTER { } } + // Honest image-equality check. Same buffer pointer (fast path) or pixel + // compare (fallback). Camera-aliveness uses getLastFrameAgeMs() — separate + // concern. With "same image -> same pts" invariant, callers usually just + // compare pts and skip this function entirely. bool ANSMJPEGClient::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; @@ -276,17 +276,19 @@ namespace ANSCENTER { return _pLastFrame; } - // Use PTS to detect duplicate frames (much faster than pixel comparison) - if (currentPts == _pts && !_pLastFrame.empty()) { + // Duplicate detection via _lastInnerPts (separate from _pts). Invariant: + // same image -> same pts. Camera-alive check uses getLastFrameAgeMs(). + 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 cursor and bump _pts monotonically. + _lastInnerPts = currentPts; + _pts++; + pts = _pts; // Handle non-rotated case if (_imageRotateDeg == 0) { @@ -634,6 +636,14 @@ namespace ANSCENTER { std::lock_guard lock(_mutex); _playerClient->setTargetFPS(intervalMs); // 0=no limit, 100=~10FPS, 200=~5FPS } + void ANSMJPEGClient::SetJitterBufferMs(int ms) { + std::lock_guard lock(_mutex); + _playerClient->setJitterBufferMs(ms); + ANS_DBG("MJPEG_Lifecycle", "SetJitterBufferMs: ms=%d url=%s", ms, _url.c_str()); + } + int ANSMJPEGClient::GetJitterBufferMs() const { + return _playerClient->getJitterBufferMs(); + } void ANSMJPEGClient::SetNV12FastPath(bool enable) { std::lock_guard lock(_mutex); _useNV12FastPath = enable; @@ -728,11 +738,11 @@ extern "C" __declspec(dllexport) int GetMJPEGStrImage(ANSCENTER::ANSMJPEGClient else return 0; } catch (const std::exception& e) { - std::cerr << "Error getting MJPEG image: " << e.what() << std::endl; + ANS_DBG("MJPEG_GetImage", "exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error getting MJPEG image: Unknown exception." << std::endl; + ANS_DBG("MJPEG_GetImage", "unknown exception"); return 0; } } @@ -750,28 +760,28 @@ extern "C" __declspec(dllexport) int GetMJPEGImage(ANSCENTER::ANSMJPEGClient * * return 1; } else { - std::cerr << "Error resizing jpegImage handle: " << error << std::endl; + ANS_DBG("MJPEG_GetImage", "DSSetHandleSize failed: error=%d", (int)error); return 0; } } else { - std::cerr << "No image data retrieved from MJPEG client." << std::endl; + ANS_DBG("MJPEG_GetImage", "no image data retrieved (jpegString empty)"); return 0; } } catch (const std::exception& e) { - std::cerr << "Error getting MJPEG image: " << e.what() << std::endl; + ANS_DBG("MJPEG_GetImage", "exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error getting MJPEG image: Unknown exception." << std::endl; + ANS_DBG("MJPEG_GetImage", "unknown exception"); return 0; } } extern "C" __declspec(dllexport) int GetMJPEGCVImage(ANSCENTER::ANSMJPEGClient** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image) { if (!Handle || !*Handle || !image) { - std::cerr << "Error: Invalid input parameters in GetMJPEGCVImage" << std::endl; + ANS_DBG("MJPEG_GetImage", "invalid input parameters"); return -1; } @@ -811,15 +821,15 @@ extern "C" __declspec(dllexport) int GetMJPEGCVImage(ANSCENTER::ANSMJPEGClient** return 1; } catch (const cv::Exception& e) { - std::cerr << "OpenCV exception in GetMJPEGCVImage: " << e.what() << std::endl; + ANS_DBG("MJPEG_GetImage", "OpenCV exception: %s", e.what()); return -2; } catch (const std::exception& e) { - std::cerr << "Exception in GetMJPEGCVImage: " << e.what() << std::endl; + ANS_DBG("MJPEG_GetImage", "GetMJPEGCVImage exception: %s", e.what()); return -2; } catch (...) { - std::cerr << "Unknown exception in GetMJPEGCVImage" << std::endl; + ANS_DBG("MJPEG_GetImage", "GetMJPEGCVImage unknown exception"); return -2; } } @@ -831,11 +841,11 @@ extern "C" __declspec(dllexport) int StartMJPEG(ANSCENTER::ANSMJPEGClient * *Han else return 0; } catch (const std::exception& e) { - std::cerr << "Error starting MJPEG client: " << e.what() << std::endl; + ANS_DBG("MJPEG_Lifecycle", "StartMJPEG exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error starting MJPEG client: Unknown exception." << std::endl; + ANS_DBG("MJPEG_Lifecycle", "StartMJPEG unknown exception"); return 0; } } @@ -847,11 +857,11 @@ extern "C" __declspec(dllexport) int ReconnectMJPEG(ANSCENTER::ANSMJPEGClient * else return 0; } catch (const std::exception& e) { - std::cerr << "Error reconnecting MJPEG client: " << e.what() << std::endl; + ANS_DBG("MJPEG_Lifecycle", "ReconnectMJPEG exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error reconnecting MJPEG client: Unknown exception." << std::endl; + ANS_DBG("MJPEG_Lifecycle", "ReconnectMJPEG unknown exception"); return 0; } } @@ -863,11 +873,11 @@ extern "C" __declspec(dllexport) int StopMJPEG(ANSCENTER::ANSMJPEGClient * *Hand else return 0; } catch (const std::exception& e) { - std::cerr << "Error stopping MJPEG client: " << e.what() << std::endl; + ANS_DBG("MJPEG_Lifecycle", "StopMJPEG exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error stopping MJPEG client: Unknown exception." << std::endl; + ANS_DBG("MJPEG_Lifecycle", "StopMJPEG unknown exception"); return 0; } } @@ -979,6 +989,18 @@ extern "C" __declspec(dllexport) void SetMJPEGDisplayResolution(ANSCENTER::ANSMJ (*Handle)->SetDisplayResolution(width, height); } catch (...) { } } +extern "C" __declspec(dllexport) void SetMJPEGJitterBuffer(ANSCENTER::ANSMJPEGClient** Handle, int jitterMs) { + if (Handle == nullptr || *Handle == nullptr) return; + try { + (*Handle)->SetJitterBufferMs(jitterMs); + } catch (...) { } +} +extern "C" __declspec(dllexport) int GetMJPEGJitterBuffer(ANSCENTER::ANSMJPEGClient** Handle) { + if (Handle == nullptr || *Handle == nullptr) return -1; + try { + return (*Handle)->GetJitterBufferMs(); + } catch (...) { return -1; } +} extern "C" __declspec(dllexport) void SetMJPEGTargetFPS(ANSCENTER::ANSMJPEGClient** Handle, double intervalMs) { if (Handle == nullptr || *Handle == nullptr) return; try { @@ -1010,21 +1032,21 @@ extern "C" __declspec(dllexport) int GetMJPEGImage_V2(uint64_t handleVal, int& w return 1; } else { - std::cerr << "Error resizing jpegImage handle: " << error << std::endl; + ANS_DBG("MJPEG_GetImage", "DSSetHandleSize failed: error=%d", (int)error); return 0; } } else { - std::cerr << "No image data retrieved from MJPEG client." << std::endl; + ANS_DBG("MJPEG_GetImage", "no image data retrieved (jpegString empty)"); return 0; } } catch (const std::exception& e) { - std::cerr << "Error getting MJPEG image: " << e.what() << std::endl; + ANS_DBG("MJPEG_GetImage", "exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error getting MJPEG image: Unknown exception." << std::endl; + ANS_DBG("MJPEG_GetImage", "unknown exception"); return 0; } } @@ -1063,15 +1085,15 @@ extern "C" __declspec(dllexport) int GetMJPEGCVImage_V2(uint64_t handleVal, int& return 1; } catch (const cv::Exception& e) { - std::cerr << "OpenCV exception in GetMJPEGCVImage_V2: " << e.what() << std::endl; + ANS_DBG("MJPEG_GetImage", "GetMJPEGCVImage_V2 OpenCV exception: %s", e.what()); return -2; } catch (const std::exception& e) { - std::cerr << "Exception in GetMJPEGCVImage_V2: " << e.what() << std::endl; + ANS_DBG("MJPEG_GetImage", "GetMJPEGCVImage_V2 exception: %s", e.what()); return -2; } catch (...) { - std::cerr << "Unknown exception in GetMJPEGCVImage_V2" << std::endl; + ANS_DBG("MJPEG_GetImage", "GetMJPEGCVImage_V2 unknown exception"); return -2; } } @@ -1084,11 +1106,11 @@ extern "C" __declspec(dllexport) int StartMJPEG_V2(uint64_t handleVal) { else return 0; } catch (const std::exception& e) { - std::cerr << "Error starting MJPEG client: " << e.what() << std::endl; + ANS_DBG("MJPEG_Lifecycle", "StartMJPEG exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error starting MJPEG client: Unknown exception." << std::endl; + ANS_DBG("MJPEG_Lifecycle", "StartMJPEG unknown exception"); return 0; } } @@ -1101,11 +1123,11 @@ extern "C" __declspec(dllexport) int ReconnectMJPEG_V2(uint64_t handleVal) { else return 0; } catch (const std::exception& e) { - std::cerr << "Error reconnecting MJPEG client: " << e.what() << std::endl; + ANS_DBG("MJPEG_Lifecycle", "ReconnectMJPEG exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error reconnecting MJPEG client: Unknown exception." << std::endl; + ANS_DBG("MJPEG_Lifecycle", "ReconnectMJPEG unknown exception"); return 0; } } @@ -1118,11 +1140,11 @@ extern "C" __declspec(dllexport) int StopMJPEG_V2(uint64_t handleVal) { else return 0; } catch (const std::exception& e) { - std::cerr << "Error stopping MJPEG client: " << e.what() << std::endl; + ANS_DBG("MJPEG_Lifecycle", "StopMJPEG exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error stopping MJPEG client: Unknown exception." << std::endl; + ANS_DBG("MJPEG_Lifecycle", "StopMJPEG unknown exception"); return 0; } } diff --git a/modules/ANSCV/ANSMJPEG.h b/modules/ANSCV/ANSMJPEG.h index 2958413..92db8cb 100644 --- a/modules/ANSCV/ANSMJPEG.h +++ b/modules/ANSCV/ANSMJPEG.h @@ -34,6 +34,9 @@ namespace ANSCENTER std::string _lastJpegImage; int _imageWidth, _imageHeight; int64_t _pts; + // Last currentPts seen from inner getImage(). Used for duplicate + // detection separately from _pts so "same image -> same pts" holds. + int64_t _lastInnerPts = -1; bool _isPlaying; bool _useNV12FastPath = false; std::recursive_mutex _mutex; @@ -72,6 +75,10 @@ namespace ANSCENTER void SetDisplayResolution(int width, int height); // Set display output size; 0,0 = original (no resize) void SetImageQuality(int mode); // 0=fast (AI), 1=quality (display) void SetTargetFPS(double intervalMs); // Set min interval between processed frames in ms (0 = no limit, 100 = ~10 FPS, 200 = ~5 FPS) + // Jitter buffer for the BGR delivery path (forwarded to CVideoPlayer). + // 0 = no buffering (today's getLatest semantics). Default 200 ms. + void SetJitterBufferMs(int ms); + int GetJitterBufferMs() const; void SetNV12FastPath(bool enable); // true = NV12 GPU fast path, false = original CPU path (stable) bool IsNV12FastPath() const { return _useNV12FastPath; } AVFrame* GetNV12Frame(); // Returns cloned NV12 frame for GPU fast-path (caller must av_frame_free) @@ -114,4 +121,6 @@ extern "C" __declspec(dllexport) void SetMJPEGImageQuality(ANSCENTER::ANSMJPEGCl extern "C" __declspec(dllexport) void SetMJPEGDisplayResolution(ANSCENTER::ANSMJPEGClient** Handle, int width, int height); extern "C" __declspec(dllexport) void SetMJPEGTargetFPS(ANSCENTER::ANSMJPEGClient** Handle, double intervalMs); extern "C" __declspec(dllexport) void SetMJPEGNV12FastPath(ANSCENTER::ANSMJPEGClient** Handle, int enable); +extern "C" __declspec(dllexport) void SetMJPEGJitterBuffer(ANSCENTER::ANSMJPEGClient** Handle, int jitterMs); +extern "C" __declspec(dllexport) int GetMJPEGJitterBuffer(ANSCENTER::ANSMJPEGClient** Handle); #endif \ No newline at end of file diff --git a/modules/ANSCV/ANSRTMP.cpp b/modules/ANSCV/ANSRTMP.cpp index 80c706e..a6aa069 100644 --- a/modules/ANSCV/ANSRTMP.cpp +++ b/modules/ANSCV/ANSRTMP.cpp @@ -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 lock(_mutex); _playerClient->setTargetFPS(intervalMs); // 0=no limit, 100=~10FPS, 200=~5FPS } + void ANSRTMPClient::SetJitterBufferMs(int ms) { + std::lock_guard 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 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; } } diff --git a/modules/ANSCV/ANSRTMP.h b/modules/ANSCV/ANSRTMP.h index 610bdb3..96d29ba 100644 --- a/modules/ANSCV/ANSRTMP.h +++ b/modules/ANSCV/ANSRTMP.h @@ -35,6 +35,11 @@ namespace ANSCENTER std::string _lastJpegImage; int _imageWidth, _imageHeight; int64_t _pts; + // Last currentPts seen from the inner getImage(). Used for duplicate + // detection separately from _pts so the invariant + // same image -> same pts + // holds for callers comparing pts (or calling areImagesIdentical()). + int64_t _lastInnerPts = -1; bool _isPlaying; bool _useNV12FastPath = false; std::recursive_mutex _mutex; @@ -73,6 +78,11 @@ namespace ANSCENTER void SetDisplayResolution(int width, int height); // Set display output size; 0,0 = original (no resize) void SetImageQuality(int mode); // 0=fast (AI), 1=quality (display) void SetTargetFPS(double intervalMs); // Set min interval between processed frames in ms (0 = no limit, 100 = ~10 FPS, 200 = ~5 FPS) + // Jitter buffer for the BGR delivery path (forwarded to CVideoPlayer). + // 0 = no buffering (today's getLatest semantics). Default 200 ms (set + // inside CVideoPlayer constructor). Adaptive: slow consumers bypass it. + void SetJitterBufferMs(int ms); + int GetJitterBufferMs() const; void SetNV12FastPath(bool enable); // true = NV12 GPU fast path, false = original CPU path (stable) bool IsNV12FastPath() const { return _useNV12FastPath; } AVFrame* GetNV12Frame(); // Returns cloned NV12 frame for GPU fast-path (caller must av_frame_free) @@ -113,4 +123,6 @@ extern "C" __declspec(dllexport) void SetRTMPImageQuality(ANSCENTER::ANSRTMPClie extern "C" __declspec(dllexport) void SetRTMPDisplayResolution(ANSCENTER::ANSRTMPClient** Handle, int width, int height); extern "C" __declspec(dllexport) void SetRTMPTargetFPS(ANSCENTER::ANSRTMPClient** Handle, double intervalMs); extern "C" __declspec(dllexport) void SetRTMPNV12FastPath(ANSCENTER::ANSRTMPClient** Handle, int enable); +extern "C" __declspec(dllexport) void SetRTMPJitterBuffer(ANSCENTER::ANSRTMPClient** Handle, int jitterMs); +extern "C" __declspec(dllexport) int GetRTMPJitterBuffer(ANSCENTER::ANSRTMPClient** Handle); #endif \ No newline at end of file diff --git a/modules/ANSCV/ANSSRT.cpp b/modules/ANSCV/ANSSRT.cpp index 7d2c454..88f2503 100644 --- a/modules/ANSCV/ANSSRT.cpp +++ b/modules/ANSCV/ANSSRT.cpp @@ -227,11 +227,11 @@ namespace ANSCENTER { } } + // Honest image-equality check. Same buffer pointer (fast path) or pixel + // compare (fallback). Camera-aliveness uses getLastFrameAgeMs() — separate + // concern. With "same image -> same pts" invariant, callers usually just + // compare pts and skip this function entirely. bool ANSSRTClient::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; @@ -290,19 +290,19 @@ 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 detection via _lastInnerPts (separate from _pts). Invariant: + // same image -> same pts. Camera-alive check uses getLastFrameAgeMs(). + 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 cursor and bump _pts monotonically. + _lastInnerPts = currentPts; + _pts++; + pts = _pts; // Handle non-rotated case if (_imageRotateDeg == 0) { @@ -666,6 +666,14 @@ namespace ANSCENTER { std::lock_guard lock(_mutex); _playerClient->setTargetFPS(intervalMs); // 0=no limit, 100=~10FPS, 200=~5FPS } + void ANSSRTClient::SetJitterBufferMs(int ms) { + std::lock_guard lock(_mutex); + _playerClient->setJitterBufferMs(ms); + ANS_DBG("SRT_Lifecycle", "SetJitterBufferMs: ms=%d url=%s", ms, _url.c_str()); + } + int ANSSRTClient::GetJitterBufferMs() const { + return _playerClient->getJitterBufferMs(); + } void ANSSRTClient::SetNV12FastPath(bool enable) { std::lock_guard lock(_mutex); _useNV12FastPath = enable; @@ -760,11 +768,11 @@ extern "C" __declspec(dllexport) int GetSRTStrImage(ANSCENTER::ANSSRTClient * *H else return 0; } catch (const std::exception& e) { - std::cerr << "Error getting SRT image: " << e.what() << std::endl; + ANS_DBG("SRT_GetImage", "exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error getting SRT image: Unknown exception." << std::endl; + ANS_DBG("SRT_GetImage", "unknown exception"); return 0; } } @@ -788,22 +796,22 @@ extern "C" __declspec(dllexport) int GetSRTImage(ANSCENTER::ANSSRTClient * *Hand } else { // Return failure if there was an error in resizing the handle - std::cerr << "Error resizing jpegImage handle: " << error << std::endl; + ANS_DBG("SRT_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 SRT client." << std::endl; + ANS_DBG("SRT_GetImage", "no image data retrieved (jpegString empty)"); return 0; } } catch (const std::exception& e) { - std::cerr << "Error getting SRT image: " << e.what() << std::endl; + ANS_DBG("SRT_GetImage", "exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error getting SRT image: Unknown exception." << std::endl; + ANS_DBG("SRT_GetImage", "unknown exception"); return 0; } } @@ -811,7 +819,7 @@ extern "C" __declspec(dllexport) int GetSRTImage(ANSCENTER::ANSSRTClient * *Hand extern "C" __declspec(dllexport) int GetSRTCVImage(ANSCENTER::ANSSRTClient** 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 GetSRTCVImage" << std::endl; + ANS_DBG("SRT_GetImage", "GetSRTCVImage invalid input parameters"); return -1; } @@ -853,15 +861,15 @@ extern "C" __declspec(dllexport) int GetSRTCVImage(ANSCENTER::ANSSRTClient** Han return 1; // Success } catch (const cv::Exception& e) { - std::cerr << "OpenCV exception in GetSRTCVImage: " << e.what() << std::endl; + ANS_DBG("SRT_GetImage", "GetSRTCVImage OpenCV exception: %s", e.what()); return -2; } catch (const std::exception& e) { - std::cerr << "Exception in GetSRTCVImage: " << e.what() << std::endl; + ANS_DBG("SRT_GetImage", "GetSRTCVImage exception: %s", e.what()); return -2; } catch (...) { - std::cerr << "Unknown exception in GetSRTCVImage" << std::endl; + ANS_DBG("SRT_GetImage", "GetSRTCVImage unknown exception"); return -2; } } @@ -873,11 +881,11 @@ extern "C" __declspec(dllexport) int StartSRT(ANSCENTER::ANSSRTClient * *Handle) else return 0; } catch (const std::exception& e) { - std::cerr << "Error starting SRT client: " << e.what() << std::endl; + ANS_DBG("SRT_Lifecycle", "StartSRT exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error starting SRT client: Unknown exception." << std::endl; + ANS_DBG("SRT_Lifecycle", "StartSRT unknown exception"); return 0; } } @@ -889,11 +897,11 @@ extern "C" __declspec(dllexport) int ReconnectSRT(ANSCENTER::ANSSRTClient * *Han else return 0; } catch (const std::exception& e) { - std::cerr << "Error reconnecting SRT client: " << e.what() << std::endl; + ANS_DBG("SRT_Lifecycle", "ReconnectSRT exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error reconnecting SRT client: Unknown exception." << std::endl; + ANS_DBG("SRT_Lifecycle", "ReconnectSRT unknown exception"); return 0; } } @@ -905,11 +913,11 @@ extern "C" __declspec(dllexport) int StopSRT(ANSCENTER::ANSSRTClient * *Handle) else return 0; } catch (const std::exception& e) { - std::cerr << "Error stopping SRT client: " << e.what() << std::endl; + ANS_DBG("SRT_Lifecycle", "StopSRT exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error stopping SRT client: Unknown exception." << std::endl; + ANS_DBG("SRT_Lifecycle", "StopSRT unknown exception"); return 0; } } @@ -1024,6 +1032,18 @@ extern "C" __declspec(dllexport) void SetSRTTargetFPS(ANSCENTER::ANSSRTClient** (*Handle)->SetTargetFPS(intervalMs); } catch (...) { } } +extern "C" __declspec(dllexport) void SetSRTJitterBuffer(ANSCENTER::ANSSRTClient** Handle, int jitterMs) { + if (Handle == nullptr || *Handle == nullptr) return; + try { + (*Handle)->SetJitterBufferMs(jitterMs); + } catch (...) { } +} +extern "C" __declspec(dllexport) int GetSRTJitterBuffer(ANSCENTER::ANSSRTClient** Handle) { + if (Handle == nullptr || *Handle == nullptr) return -1; + try { + return (*Handle)->GetJitterBufferMs(); + } catch (...) { return -1; } +} extern "C" __declspec(dllexport) void SetSRTNV12FastPath(ANSCENTER::ANSSRTClient** Handle, int enable) { if (Handle == nullptr || *Handle == nullptr) return; try { @@ -1051,21 +1071,21 @@ extern "C" __declspec(dllexport) int GetSRTImage_V2(uint64_t handleVal, int& wid return 1; } else { - std::cerr << "Error resizing jpegImage handle: " << error << std::endl; + ANS_DBG("SRT_GetImage", "DSSetHandleSize failed: error=%d", (int)error); return 0; } } else { - std::cerr << "No image data retrieved from SRT client." << std::endl; + ANS_DBG("SRT_GetImage", "no image data retrieved (jpegString empty)"); return 0; } } catch (const std::exception& e) { - std::cerr << "Error getting SRT image: " << e.what() << std::endl; + ANS_DBG("SRT_GetImage", "exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error getting SRT image: Unknown exception." << std::endl; + ANS_DBG("SRT_GetImage", "unknown exception"); return 0; } } @@ -1073,7 +1093,7 @@ extern "C" __declspec(dllexport) int GetSRTImage_V2(uint64_t handleVal, int& wid extern "C" __declspec(dllexport) int GetSRTCVImage_V2(uint64_t handleVal, int& width, int& height, int64_t& timeStamp, cv::Mat** image) { auto* h = reinterpret_cast(handleVal); if (!h) return -1; if (!image) { - std::cerr << "Error: Invalid input parameters in GetSRTCVImage_V2" << std::endl; + ANS_DBG("SRT_GetImage", "GetSRTCVImage_V2 invalid input parameters"); return -1; } @@ -1110,15 +1130,15 @@ extern "C" __declspec(dllexport) int GetSRTCVImage_V2(uint64_t handleVal, int& w return 1; } catch (const cv::Exception& e) { - std::cerr << "OpenCV exception in GetSRTCVImage_V2: " << e.what() << std::endl; + ANS_DBG("SRT_GetImage", "GetSRTCVImage_V2 OpenCV exception: %s", e.what()); return -2; } catch (const std::exception& e) { - std::cerr << "Exception in GetSRTCVImage_V2: " << e.what() << std::endl; + ANS_DBG("SRT_GetImage", "GetSRTCVImage_V2 exception: %s", e.what()); return -2; } catch (...) { - std::cerr << "Unknown exception in GetSRTCVImage_V2" << std::endl; + ANS_DBG("SRT_GetImage", "GetSRTCVImage_V2 unknown exception"); return -2; } } @@ -1131,11 +1151,11 @@ extern "C" __declspec(dllexport) int StartSRT_V2(uint64_t handleVal) { else return 0; } catch (const std::exception& e) { - std::cerr << "Error starting SRT client: " << e.what() << std::endl; + ANS_DBG("SRT_Lifecycle", "StartSRT exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error starting SRT client: Unknown exception." << std::endl; + ANS_DBG("SRT_Lifecycle", "StartSRT unknown exception"); return 0; } } @@ -1148,11 +1168,11 @@ extern "C" __declspec(dllexport) int ReconnectSRT_V2(uint64_t handleVal) { else return 0; } catch (const std::exception& e) { - std::cerr << "Error reconnecting SRT client: " << e.what() << std::endl; + ANS_DBG("SRT_Lifecycle", "ReconnectSRT exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error reconnecting SRT client: Unknown exception." << std::endl; + ANS_DBG("SRT_Lifecycle", "ReconnectSRT unknown exception"); return 0; } } @@ -1165,11 +1185,11 @@ extern "C" __declspec(dllexport) int StopSRT_V2(uint64_t handleVal) { else return 0; } catch (const std::exception& e) { - std::cerr << "Error stopping SRT client: " << e.what() << std::endl; + ANS_DBG("SRT_Lifecycle", "StopSRT exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error stopping SRT client: Unknown exception." << std::endl; + ANS_DBG("SRT_Lifecycle", "StopSRT unknown exception"); return 0; } } diff --git a/modules/ANSCV/ANSSRT.h b/modules/ANSCV/ANSSRT.h index 82d6aa0..2e82caf 100644 --- a/modules/ANSCV/ANSSRT.h +++ b/modules/ANSCV/ANSSRT.h @@ -34,6 +34,9 @@ namespace ANSCENTER std::string _lastJpegImage; int _imageWidth, _imageHeight; int64_t _pts; + // Last currentPts seen from inner getImage(). Used for duplicate + // detection separately from _pts so "same image -> same pts" holds. + int64_t _lastInnerPts = -1; bool _isPlaying; bool _useNV12FastPath = false; std::recursive_mutex _mutex; @@ -72,6 +75,10 @@ namespace ANSCENTER void SetDisplayResolution(int width, int height); // Set display output size; 0,0 = original (no resize) void SetImageQuality(int mode); // 0=fast (AI), 1=quality (display) void SetTargetFPS(double intervalMs); // Set min interval between processed frames in ms (0 = no limit, 100 = ~10 FPS, 200 = ~5 FPS) + // Jitter buffer for the BGR delivery path (forwarded to CVideoPlayer). + // 0 = no buffering (today's getLatest semantics). Default 200 ms. + void SetJitterBufferMs(int ms); + int GetJitterBufferMs() const; void SetNV12FastPath(bool enable); // true = NV12 GPU fast path, false = original CPU path (stable) bool IsNV12FastPath() const { return _useNV12FastPath; } AVFrame* GetNV12Frame(); // Returns cloned NV12 frame for GPU fast-path (caller must av_frame_free) @@ -113,4 +120,6 @@ extern "C" __declspec(dllexport) void SetSRTImageQuality(ANSCENTER::ANSSRTClient extern "C" __declspec(dllexport) void SetSRTDisplayResolution(ANSCENTER::ANSSRTClient** Handle, int width, int height); extern "C" __declspec(dllexport) void SetSRTTargetFPS(ANSCENTER::ANSSRTClient** Handle, double intervalMs); extern "C" __declspec(dllexport) void SetSRTNV12FastPath(ANSCENTER::ANSSRTClient** Handle, int enable); +extern "C" __declspec(dllexport) void SetSRTJitterBuffer(ANSCENTER::ANSSRTClient** Handle, int jitterMs); +extern "C" __declspec(dllexport) int GetSRTJitterBuffer(ANSCENTER::ANSSRTClient** Handle); #endif \ No newline at end of file diff --git a/tests/ANSUtilities-UnitTest/ANSUtilities-UnitTest.cpp b/tests/ANSUtilities-UnitTest/ANSUtilities-UnitTest.cpp index 95a4d37..bfd2ab6 100644 --- a/tests/ANSUtilities-UnitTest/ANSUtilities-UnitTest.cpp +++ b/tests/ANSUtilities-UnitTest/ANSUtilities-UnitTest.cpp @@ -291,7 +291,7 @@ int ANSFTechMinIOTest() { // Upload a jpeg image to bucket - std::string jpegFilePath = "E:\\Programs\\DemoAssets\\NV\\Test.jpg"; + std::string jpegFilePath = "C:\\ProgramData\\ANSCENTER\\Shared\\Test.jpg"; std::string objectName = "test1.jpg";