14 Commits
Author SHA1 Message Date
nghianguyenandClaude Fable 5 6344062179 MediaClient: keyframe gating (startup + corruption re-arm), libyuv NV12 path
Restores the previously shelved decoder/player hardening and extends it:

1. Startup keyframe gating (video_decoder): after (re)start the decoder
   joins the stream mid-GOP; frames referencing unseen pictures render as
   green/blocky concealment garbage. Drop everything until the first IDR
   (300-frame safety valve so unmarked streams can't blank forever).

2. NEW - corruption re-arm: on lossy links (Wi-Fi cameras) dropped slices
   surface as "First slice in a frame missing" / "Could not find ref with
   POC n" and FFmpeg marks the output via AVFrame::decode_error_flags.
   Re-arm the keyframe gate and drop the rest of the GOP so the display
   holds the last clean frame instead of showing concealment artifacts;
   corrupt drops count toward the safety valve.

3. libyuv NV12->BGR conversion (video_player): honors NVDEC/D3D11VA padded
   strides (1080->1088 rows) that cv::cvtColorTwoPlane mishandled (green
   cast + striped noise), picks BT.601/709/2020 coefficients from the
   frame's signalled colorspace, and is ~3-5x faster than sws_scale.

Verified live against an RTSP HEVC camera (NVDEC path): steady frame flow,
no regressions on the clean-stream path.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-06 21:49:22 +10:00
nghianguyenandClaude Fable 5 044fedd344 ANSCV test: pump GUI messages while waiting for frames
The empty-frame path slept 1 s without calling cv::waitKey, so the
HighGUI window stopped pumping Windows messages and went "Not
Responding" whenever the RTSP stream stalled for a few seconds.
Wait via cv::waitKey(1000) instead (ESC still exits). Same fix in
all three test loops that shared the pattern.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-06 21:39:39 +10:00
nghianguyenandClaude Fable 5 005f3bc3d5 ANSCV tests: update RTSP camera address, drop unused ans_imshow include
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-06 20:23:31 +10:00
nghianguyenandClaude Fable 5 13180921c9 Pool dispatch: prefer slot on the GPU where inputs already reside
Two-pass slot acquisition in runInferenceFromPool: Pass A picks an idle
slot whose deviceIndex matches the input GpuMats' resident device
(zero-copy dispatch); Pass B falls back to plain round-robin, where
runInference migrates the inputs via the pinned-host two-hop copy.

Callers that prepare frames on GPU A no longer pay the ~1 ms/frame PCIe
migration when a same-GPU slot is idle, while spillover to the other GPU
under load still works. Input residency is queried outside the slot
mutex via cudaPointerGetAttributes (cheap driver lookup).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-06 20:05:00 +10:00
nghianguyenandClaude Fable 5 a4ea532f36 Fix multi-GPU inference on Blackwell (dual RTX 5080): four stacked fixes
Verified on dual RTX 5080: 355/355 detections, both GPUs in round-robin,
zero CUDA errors. Requires TensorRT >= 10.16.1 in SharedCore (10.15.1 has
a Myelin l2cm crash when creating a context on a second Blackwell device).

1. blobFromGpuMats: thread_local GpuMat caches (tl_blob/tl_floatImg) were
   device-blind — a pool thread warming GPU[1] then GPU[0] reused device-1
   memory with a device-0 stream, causing an async illegal memory access
   that poisoned every CUDA context (sticky 700). Track owning device and
   rebuild the cache on device switch.

2. runInference: pool round-robin can dispatch to a slot on a different
   GPU than the one the caller prepared inputs on. Detect foreign input
   GpuMats via cudaPointerGetAttributes and migrate them through a pinned
   host staging buffer (two-hop D2H+H2D; direct cross-device cudaMemcpy2D
   is unsupported and GeForce has no P2P).

3. warmUp: now pins its own CUDA device before uploading dummy inputs and
   returns success/failure; loadNetwork rejects engines whose warmup fails
   so broken slots can never join the pool silently.

4. Blackwell builder workaround: setMaxAuxStreams(0) on SM 12.x plus an
   .aux0 engine-cache suffix so pre-workaround caches rebuild.

Also: ANSFR-UnitTest gains --gpu N flag (sets CUDA_VISIBLE_DEVICES) for
single-GPU bisection, and rich diagnostics on input-copy failures.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-06 20:00:19 +10:00
nghianguyen 43ebd25d07 Unitiest 2026-06-23 05:56:06 +10:00
nghianguyenandClaude Opus 4.8 eb29945cb7 ANSCV tests: add ans_imshow include + headless ConsoleTest
Add ConsoleTest.cpp, a headless (no-GUI) smoke test for ANSOPENCV image
ops plus an optional RTSP capture path, built as its own executable
(ANSCV-ConsoleTest) so it does not collide with ANSCV-UnitTest's GUI main.

Make the ans_imshow.hpp include (shipped with ANSMIND, outside the repo)
configurable via the ANSMIND_INCLUDE_DIR cache variable, defaulting to
C:/ProgramData/ANSCENTER/ANSMIND. Warn rather than fail configure when the
header is absent so unrelated targets still build.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 05:51:01 +10:00
nghianguyen cb3a32cdfb Merge remote-tracking branch 'ANSCORE/main' 2026-06-18 05:55:50 +10:00
nghianguyen b3436a61ee Prevent OOM 2026-06-18 05:55:31 +10:00
nghianguyen bc1517598d Plans for CMS 2026-06-12 14:13:26 +10:00
nghianguyen ebba70b9d9 Support Get log events 2026-05-24 09:19:24 +10:00
nghianguyen 47b80102a8 Add port to scanning devices 2026-05-24 08:33:34 +10:00
nghianguyen a2fa2c8ceb Support scanning ANSVIS servers device 2026-05-23 18:03:22 +10:00
nghianguyen 4678d87e33 Add plan to port to linux 2026-05-23 17:29:57 +10:00
88 changed files with 8224 additions and 103 deletions
+92 -1
View File
@@ -232,7 +232,98 @@
"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\")", "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)", "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)", "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)" "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)",
"Bash(ls \"C:/Projects/AIVISION/RTM_POSE\")",
"Bash(ls C:/Projects/AIVISION/ANSLittering 2>&1 | head -50)",
"WebFetch(domain:www.example-code.com)",
"Bash(find / -name \"CkHttp.h\" 2>/dev/null | head; echo \"---\"; find / -name \"CkRest.h\" 2>/dev/null | head)",
"Bash(ls cmake-build-* build out)",
"Bash(ls CMakePresets.json)",
"Bash(cmake --build . --target ANSUtilities)",
"Bash(cmd /c '\"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Auxiliary\\\\Build\\\\vcvars64.bat\" && cmake --build . --target ANSUtilities')",
"Bash(ls \"C:/Projects/CLionProjects/AIBOX\" 2>&1 | head -30)",
"mcp__desktop-commander__list_directory",
"mcp__desktop-commander__read_file",
"PowerShell(& \"C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\build_ansutil.bat\" 2>&1)",
"Bash(ls -la /c/Projects/ANSVIS 2>/dev/null || echo \"ANSVIS directory does not exist\")",
"PowerShell($cmake = \"C:\\\\Users\\\\nghia\\\\AppData\\\\Local\\\\Programs\\\\CLion 2026.1\\\\bin\\\\cmake\\\\win\\\\x64\\\\bin\\\\cmake.exe\"; & $cmake --build \"C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release-clangcl\" --target ANSUtilities -j 8 2>&1 | Select-Object -Last 40)",
"Bash(cmake --build C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release-clangcl --target ANSODEngine -j 8)",
"Bash(cmake --build \"C:/Projects/CLionProjects/ANSCORE/cmake-build-release-clangcl\" --target ANSODEngine -j 8)",
"Bash(cmake --build \"C:/Projects/CLionProjects/ANSCORE/cmake-build-release-clangcl\" --target all -j 8)",
"Bash(cmake --build \"C:/Projects/CLionProjects/ANSCORE/cmake-build-release\" --target ANSODEngine -j 8)",
"Bash(cmd.exe /c 'call \"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Auxiliary\\\\Build\\\\vcvarsall.bat\" x64 >nul && cmake --build \"C:/Projects/CLionProjects/ANSCORE/cmake-build-release\" --target ANSODEngine -j 8')",
"PowerShell($ErrorActionPreference = 'Continue'; cmd /c '\"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Auxiliary\\\\Build\\\\vcvarsall.bat\" x64 >nul 2>&1 && set' | Where-Object { $_ -match '^\\(PATH|INCLUDE|LIB|LIBPATH|VC|WindowsSdk|UCRT|VSCMD_\\)' } | ForEach-Object { $name, $value = $_ -split '=', 2; [Environment]::SetEnvironmentVariable\\($name, $value\\) }; cmake --build \"C:/Projects/CLionProjects/ANSCORE/cmake-build-release\" --target ANSODEngine -j 8 2>&1 | Select-Object -Last 30)",
"Bash(sleep 20 && cat \"C:/Users/nghia/AppData/Local/Temp/clear-elevated.txt\" 2>&1 | grep -E \"\\(clear-flag|before clear|with clear|after clear|RESULT|EvtClearLog\\)\" | head -20)",
"Bash(ls \"C:\\\\Projects\\\\SharedCore\")",
"Bash(ls \"C:\\\\Projects\\\\SharedCore\\\\ep\")",
"PowerShell(Get-ChildItem \"C:\\\\Projects\\\\SharedCore\\\\ep\\\\directml\" | Select-Object Name, Length, @{n='Ver';e={$_.VersionInfo.FileVersion}})",
"PowerShell(& \"C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\tools\\\\Check-SharedCoreVersions.ps1\" -Root \"C:\\\\Projects\\\\SharedCore\" -CompareWith \"C:\\\\ProgramData\\\\ANSCENTER\\\\Shared\")",
"PowerShell(Write-Output \"=== SSH dir contents ===\"; if \\(Test-Path \"$env:USERPROFILE\\\\.ssh\"\\) { Get-ChildItem \"$env:USERPROFILE\\\\.ssh\" -Force | Select-Object Name, Length } else { Write-Output \"No .ssh directory\" }; Write-Output \"=== ssh-agent service ===\"; Get-Service ssh-agent | Select-Object Name, Status, StartType; Write-Output \"=== git remote ===\"; git -C C:\\\\Projects\\\\CLionProjects\\\\ANSCORE remote -v)",
"Read(//c/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/**)",
"Read(//c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v13.0/bin/**)",
"Bash(tasklist)",
"Bash(echo \"=== Removing stale CMake caches \\(safe: regenerated by CLion\\) ===\" *)",
"Read(//c/ANSLibs/cudnn/**)",
"Read(//c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v13.0/include/**)",
"Read(//c/Program Files \\(x86\\)/Intel/oneAPI/**)",
"Read(//c/Program Files \\(x86\\)/**)",
"Bash('C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.16.27023/bin/HostX64/x64/dumpbin.exe' /SYMBOLS C:/ANSLibs/faiss/lib/faiss.lib)",
"Bash(winget --version)",
"Bash(winget search *)",
"Bash(winget install *)",
"Bash('C:/Program Files \\(x86\\)/Microsoft Visual Studio/Installer/vswhere.exe' -latest -property installationVersion)",
"Bash('C:/Program Files \\(x86\\)/Microsoft Visual Studio/Installer/vswhere.exe' -latest -property displayName)",
"Bash('C:/Program Files \\(x86\\)/Microsoft Visual Studio/Installer/vswhere.exe' -latest -property catalog_productDisplayVersion)",
"Bash(winget list *)",
"Bash(\"C:/Program Files \\(x86\\)/Microsoft Visual Studio/Installer/vswhere.exe\" -latest -property channelId)",
"Bash(./vs_installer.exe update *)",
"Bash(echo \"launch exit: $?\")",
"Bash('C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.16.27023/bin/HostX64/x64/dumpbin.exe' /ARCHIVEMEMBERS C:/ANSLibs/ODHUBYOLODarknet/lib/odhublib.lib)",
"Bash('C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.16.27023/bin/HostX64/x64/dumpbin.exe' /HEADERS C:/ANSLibs/ODHUBYOLODarknet/lib/odhublib.lib)",
"Read(//c/ANSLibs/TensorRT/lib/**)",
"Read(//c/ANSLibs/TensorRT/include/**)",
"Bash(ls -la --block-size=M nvinfer*.dll nvonnx*.dll)",
"Bash(stat -c '%n %s bytes' nvinfer*.dll nvonnx*.dll)",
"Bash(cd \"C:/Projects/SharedCore\" *)",
"Bash(cd \"C:/Projects/CLionProjects/ANSCORE\" *)",
"Read(//c/ANSLibs/**)",
"Read(//c/Windows/System32/**)",
"Bash(grep -niE \"ep/|ep\\\\\\\\\\\\\\\\|libraryDir|AddDllDirectory|SetDllDirectory|LoadLibrary|directml|openvino|cuda|cpu|subdir|\\\\\\\\\\\\\"ep\\\\\\\\\\\\\"|selectEP|chooseEP|provider\" EPLoader.cpp)",
"Bash(grep -viE \"^\\\\s*//|comment\")",
"Bash(echo \"=== Search for a 1.24 ONNX Runtime SDK \\(headers/lib\\) anywhere on disk ===\" *)",
"WebSearch",
"WebFetch(domain:github.com)",
"WebFetch(domain:raw.githubusercontent.com)",
"WebFetch(domain:onnxruntime.ai)",
"Bash([ -f \"$CM/OpenVINOTargets-debug.cmake\" ])",
"Bash(mv C:/ANSLibs/OpenVINO/runtime/cmake/OpenVINOTargets-debug.cmake C:/ANSLibs/OpenVINO/runtime/cmake/OpenVINOTargets-debug.cmake.disabled)",
"Bash(xargs -n1 basename)",
"PowerShell(cmd /c 'set \"PATH=C:\\\\ANSLibs\\\\Python311;C:\\\\ANSLibs\\\\Python311\\\\Scripts;%PATH%\" && call \"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Auxiliary\\\\Build\\\\vcvars64.bat\" >nul && call \"C:\\\\ANSLibs\\\\OpenVINO\\\\setupvars.bat\" >nul && cd /d C:\\\\Projects\\\\onnxruntime-ov && build.bat --update --config Release --use_openvino GPU --build_shared_lib --skip_tests --cmake_generator \"Visual Studio 17 2022\" 2>&1')",
"Bash(ls \"C:/Program *)",
"Bash(\"C:/Program Files/CMake/bin/cmake.exe\" --version)",
"Read(//c/Program Files/JetBrains/CLion 2026.1.3/bin/ninja/win/x64/**)",
"Bash(\"C:/Program Files/JetBrains/CLion 2026.1.3/bin/ninja/win/x64/ninja.exe\" -C \"C:/Projects/CLionProjects/ANSCORE/cmake-build-release\" -n)",
"Bash(sed -n '462,466p' core/ANSLibsLoader/EPLoader.cpp)",
"Bash(sed -n '576,581p' engines/ONNXEngine/ONNXEngine.cpp)",
"Bash(sed -n '63,66p' modules/ANSODEngine/ANSONNXCL.cpp)",
"Bash(awk 'NR>=1240 && NR<=1322 && /::|void |int |bool |Initialize|Detect|Run|LoadModel|Inference/{last=NR\": \"$0} END{}' modules/ANSODEngine/ANSFaceDetectorEngine.cpp)",
"Bash(grep -viE \"//|comment\")",
"Bash(sed -n '1,40p' tests/ANSFR-UnitTest/ANSFR-UnitTest.cpp)",
"Bash(awk '/int main[ ]*\\\\\\(/{f=1} f{print NR\": \"$0} f&&/^}/{exit}' tests/ANSFR-UnitTest/ANSFR-UnitTest.cpp)",
"Bash(cmd /c \"C:\\\\\\\\Projects\\\\\\\\CLionProjects\\\\\\\\ANSCORE\\\\\\\\tools\\\\\\\\_run_frtest.bat\")",
"Bash(mv __TRACKED_VAR__/ANSFR.dll.working __TRACKED_VAR__/ANSFR.dll)",
"Bash(md5sum __TRACKED_VAR__/ANSFR.dll)",
"Bash(BIN=\"C:/Projects/CLionProjects/ANSCORE/cmake-build-release/bin\" *)",
"Bash(echo \"=== first 30 lines \\(structure\\) ===\")",
"Read(//c/Users/Neo/Downloads/**)",
"Bash(echo \"\")",
"Bash(echo \"=== key error markers ===\")",
"Bash(sed -n '695,725p' modules/ANSODEngine/SCRFDFaceDetector.cpp)",
"Bash(sed -n '160,210p' modules/ANSODEngine/NV12PreprocessHelper.cpp)",
"Bash(sed -n '1,12p' modules/ANSODEngine/SCRFDFaceDetector.cpp)",
"Bash(sed -n '1,12p' modules/ANSODEngine/NV12PreprocessHelper.cpp)",
"Bash(sed -n '188,205p' modules/ANSODEngine/NV12PreprocessHelper.cpp)",
"Bash('C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v13.0/bin/cuobjdump.exe' C:/Projects/ANSTrainingTools/ODHUB/darknet/build/darknet/x64/odhublib.dll)"
] ]
} }
} }
+14 -1
View File
@@ -9,7 +9,14 @@ if(CMAKE_CUDA_COMPILER)
set(ANSCORE_HAS_CUDA ON) set(ANSCORE_HAS_CUDA ON)
set(CMAKE_CUDA_STANDARD 20) set(CMAKE_CUDA_STANDARD 20)
set(CMAKE_CUDA_STANDARD_REQUIRED ON) set(CMAKE_CUDA_STANDARD_REQUIRED ON)
set(CMAKE_CUDA_ARCHITECTURES 75 80 86 87 88 89 90 100) # SASS (-real) for each shipping arch from Turing (sm_75) through Blackwell
# consumer (sm_120), plus compute_75 PTX (-virtual) so any newer/unlisted GPU
# JIT-compiles at first run (forward compatibility). NOTE: this only governs
# this project's own .cu files; prebuilt deps (odhublib, TensorRT, ORT, cuBLAS)
# carry their own arch coverage independently.
set(CMAKE_CUDA_ARCHITECTURES
75-real 80-real 86-real 87-real 88-real 89-real 90-real 100-real 120-real
75-virtual)
if(MSVC) if(MSVC)
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --use-fast-math -Xcompiler=/utf-8") set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --use-fast-math -Xcompiler=/utf-8")
else() else()
@@ -36,6 +43,12 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON) set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# ANSCORE uses no C++20 modules. CMake auto-enables module dependency scanning
# for C++20 + Ninja, which adds a fragile per-TU scan/dyndep (.ddi/.modmap) step
# that can fail (empty .ddi -> "cannot open ...modmap" / dyndep parse errors),
# especially after an interrupted build. Disable it: faster builds, no dyndep.
set(CMAKE_CXX_SCAN_FOR_MODULES OFF)
# Output all binaries to a single directory # Output all binaries to a single directory
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
+76
View File
@@ -761,6 +761,10 @@ void CVideoDecoder::Start() {
if (m_pContext) { if (m_pContext) {
avcodec_flush_buffers(m_pContext); avcodec_flush_buffers(m_pContext);
} }
// Re-arm keyframe gating: after a flush the DPB is empty, so wait for the
// next IDR before delivering frames again (avoids post-restart green/blocky).
m_bWaitKeyframe = true;
m_nKeyframeWaitCount = 0;
m_bRunning = TRUE; m_bRunning = TRUE;
log_print(HT_LOG_INFO, "%s, Video decoder started\r\n", __FUNCTION__); log_print(HT_LOG_INFO, "%s, Video decoder started\r\n", __FUNCTION__);
} }
@@ -1093,6 +1097,78 @@ BOOL CVideoDecoder::readFrame()
continue; continue;
} }
// Mid-stream corruption gating — re-arm the keyframe gate on decode errors.
//
// Packet loss on lossy links (Wi-Fi cameras) drops slices mid-GOP:
// FFmpeg logs "First slice in a frame missing" / "Could not find ref
// with POC n", conceals the damage, and marks the output frame via
// AVFrame::decode_error_flags (missing refs, concealment active, ...).
// Every subsequent frame in the GOP references the concealed one, so
// rather than display green/blocky garbage until the next IDR, drop
// the frame and re-arm the keyframe gate — the display holds the last
// clean frame and resumes on the next clean keyframe.
if (m_pFrame->decode_error_flags != 0) {
if (!m_bWaitKeyframe.load(std::memory_order_relaxed)) {
m_bWaitKeyframe.store(true, std::memory_order_relaxed);
m_nKeyframeWaitCount = 0;
ANS_DBG("MEDIA_Decode",
"corrupt frame (decode_error_flags=0x%x) — re-arming keyframe gate",
m_pFrame->decode_error_flags);
} else {
// Count corrupt drops toward the safety valve so a stream that
// is corrupt for a long stretch eventually shows *something*
// (concealed frames) instead of freezing indefinitely.
++m_nKeyframeWaitCount;
}
if (m_nKeyframeWaitCount < 300) {
av_frame_unref(m_pFrame);
continue;
}
// Safety valve tripped: fall through and let the frame pass
// (concealment artifacts beat a permanently frozen display).
m_bWaitKeyframe.store(false, std::memory_order_relaxed);
}
// Startup keyframe gating — discard everything until the first IDR.
//
// On (re)connect we join the stream mid-GOP. Until a keyframe arrives,
// every frame references pictures we never received; the decoder (and
// NVDEC especially) conceals them into green / blocky garbage and reports
// success, so there is no error flag to test. Waiting for the first
// keyframe gives a clean intra reference, after which inter frames decode
// correctly. This is the standard RTSP "wait for IDR" behaviour.
//
// Keyframe flag is set from the bitstream parse and is reliable for both
// SW and HW decode (it does not depend on the HW surface). pict_type is a
// fallback. The counter is a safety valve: if a stream never advertises a
// keyframe within ~300 frames (~10 s @30fps) we stop gating rather than
// blank the display forever.
if (m_bWaitKeyframe.load(std::memory_order_relaxed)) {
// pict_type==I is version-independent and set for HEVC IDR/I frames
// on both SW and HW decode. AV_FRAME_FLAG_KEY (FFmpeg 5.1+) is added
// when available for robustness; guarded so it also builds against
// the older vendored headers that predate that flag.
bool isKey = (m_pFrame->pict_type == AV_PICTURE_TYPE_I);
#if defined(AV_FRAME_FLAG_KEY)
isKey = isKey || ((m_pFrame->flags & AV_FRAME_FLAG_KEY) != 0);
#endif
if (isKey) {
m_bWaitKeyframe.store(false, std::memory_order_relaxed);
ANS_DBG("MEDIA_Decode",
"keyframe acquired after %d dropped frame(s) — resuming output",
m_nKeyframeWaitCount);
} else if (++m_nKeyframeWaitCount >= 300) {
m_bWaitKeyframe.store(false, std::memory_order_relaxed);
ANS_DBG("MEDIA_Decode",
"no keyframe after %d frames — releasing gate (safety valve)",
m_nKeyframeWaitCount);
} else {
// Pre-IDR frame: drop it (would render as green/blocky garbage).
av_frame_unref(m_pFrame);
continue;
}
}
if (m_pFrame->format == m_hwPixFmt) if (m_pFrame->format == m_hwPixFmt)
{ {
// CUDA HW accel: clone the HW frame BEFORE transfer so inference // CUDA HW accel: clone the HW frame BEFORE transfer so inference
+8
View File
@@ -165,6 +165,14 @@ private:
std::atomic<BOOL> m_bRunning; std::atomic<BOOL> m_bRunning;
BOOL m_bHardwareDecoderEnabled; // Track if hardware decoder is enabled BOOL m_bHardwareDecoderEnabled; // Track if hardware decoder is enabled
bool m_bCudaHWAccel; // true when using AV_HWDEVICE_TYPE_CUDA bool m_bCudaHWAccel; // true when using AV_HWDEVICE_TYPE_CUDA
// Startup keyframe gating. On connect/reconnect we join the stream mid-GOP,
// before the first IDR, so early frames reference data we never received and
// the decoder conceals them (green / blocky macroblocks). We drop everything
// until the first keyframe (clean reference point), then deliver normally.
// The frame counter is a safety valve so a stream that never advertises a
// keyframe can't blank the display forever.
std::atomic<bool> m_bWaitKeyframe{ true }; // true until first IDR after (re)start
int m_nKeyframeWaitCount{ 0 }; // frames dropped while waiting
int m_hwGpuIndex; // GPU index assigned by HWDecoderPool (-1 = legacy) int m_hwGpuIndex; // GPU index assigned by HWDecoderPool (-1 = legacy)
AVFrame* m_pCudaHWFrame; // Cloned CUDA HW frame (device ptrs) for inference AVFrame* m_pCudaHWFrame; // Cloned CUDA HW frame (device ptrs) for inference
const AVCodec* m_pCodec; const AVCodec* m_pCodec;
+82 -3
View File
@@ -1405,6 +1405,80 @@ cv::Mat CVideoPlayer::avframeNV12ToCvMat(const AVFrame* frame)
m_nv12OrigWidth = width; m_nv12OrigWidth = width;
m_nv12OrigHeight = height; m_nv12OrigHeight = height;
// Preferred path: libyuv NV12→RGB24 (BGR in memory, matches cv::Mat).
// SIMD (SSSE3/AVX2) and ~3-5x faster than sws_scale's quality config.
// Crucially it honors src_stride_uv, so the padded strides/height that
// NVDEC and D3D11VA emit (e.g. 1080→1088 rows, aligned strides) convert
// correctly — that padding is what cv::cvtColorTwoPlane mishandled,
// reading the chroma plane at the wrong offset and rendering a green
// cast with striped noise toward the bottom of the frame.
// Coefficients are chosen from the frame's signalled colorspace/range,
// mirroring initNV12SwsContext() (BT.709 for HD/4K, BT.601 for SD,
// BT.2020 if tagged). The constants map studio-range YUV to full-range
// BGR, so the result is display-ready — no manual range expansion.
#if defined(ANSCORE_HAS_LIBYUV) && ANSCORE_HAS_LIBYUV
{
const bool fullRange = (frame->color_range == AVCOL_RANGE_JPEG);
const libyuv::YuvConstants* yuvc;
switch (frame->colorspace) {
case AVCOL_SPC_BT470BG:
case AVCOL_SPC_SMPTE170M:
yuvc = fullRange ? &libyuv::kYuvJPEGConstants : &libyuv::kYuvI601Constants;
break;
case AVCOL_SPC_BT2020_NCL:
case AVCOL_SPC_BT2020_CL:
yuvc = fullRange ? &libyuv::kYuvV2020Constants : &libyuv::kYuv2020Constants;
break;
case AVCOL_SPC_BT709:
yuvc = fullRange ? &libyuv::kYuvF709Constants : &libyuv::kYuvH709Constants;
break;
default:
// Auto-detect by resolution (matches initNV12SwsContext()).
if (width >= 1280 || height >= 720)
yuvc = fullRange ? &libyuv::kYuvF709Constants : &libyuv::kYuvH709Constants;
else
yuvc = fullRange ? &libyuv::kYuvJPEGConstants : &libyuv::kYuvI601Constants;
break;
}
cv::Mat bgrImage(height, width, CV_8UC3);
int ret = libyuv::NV12ToRGB24Matrix(
frame->data[0], frame->linesize[0],
frame->data[1], frame->linesize[1],
bgrImage.data, static_cast<int>(bgrImage.step),
yuvc, width, height);
if (ret == 0) {
return bgrImage;
}
ANS_DBG("MEDIA_Convert",
"libyuv NV12ToRGB24 failed (ret=%d) — falling back to sws_scale", ret);
}
#endif
// Fallback path: sws_scale via the dedicated NV12→BGR24 context. Also
// stride-safe (honors frame->linesize[]) and colorspace/range-correct;
// used on non-libyuv builds or if libyuv ever reports an error.
{
// Dedicated lock (not _mutex) so close()/dtor cannot free the context
// mid-scale, while still keeping onVideoFrame off the hot _mutex.
std::lock_guard<std::mutex> nv12Lock(m_nv12SwsMutex);
initNV12SwsContext(frame);
if (m_nv12SwsCtx) {
cv::Mat bgrImage(height, width, CV_8UC3);
uint8_t* dst[1] = { bgrImage.data };
int dstStride[1] = { static_cast<int>(bgrImage.step[0]) };
int ret = sws_scale(m_nv12SwsCtx, frame->data, frame->linesize, 0,
height, dst, dstStride);
if (ret > 0) {
return bgrImage;
}
ANS_DBG("MEDIA_Convert",
"NV12 sws_scale failed (ret=%d) — falling back to cvtColorTwoPlane", ret);
}
}
// Fallback: OpenCV two-plane conversion. Correct only when the planes
// are unpadded (linesize == width); kept for builds/inputs where the
// sws context could not be created.
cv::Mat yPlane(height, width, CV_8UC1, frame->data[0], frame->linesize[0]); cv::Mat yPlane(height, width, CV_8UC1, frame->data[0], frame->linesize[0]);
cv::Mat uvPlane(height / 2, width / 2, CV_8UC2, frame->data[1], frame->linesize[1]); cv::Mat uvPlane(height / 2, width / 2, CV_8UC2, frame->data[1], frame->linesize[1]);
@@ -2200,9 +2274,14 @@ CVideoPlayer::~CVideoPlayer()
sws_freeContext(swsCtx); sws_freeContext(swsCtx);
swsCtx = nullptr; swsCtx = nullptr;
} }
if (m_nv12SwsCtx != nullptr) { {
sws_freeContext(m_nv12SwsCtx); // Lock order: _mutex (held) → m_nv12SwsMutex. Protects against a
m_nv12SwsCtx = nullptr; // consumer thread still inside avframeNV12ToCvMat's sws_scale.
std::lock_guard<std::mutex> nv12Lock(m_nv12SwsMutex);
if (m_nv12SwsCtx != nullptr) {
sws_freeContext(m_nv12SwsCtx);
m_nv12SwsCtx = nullptr;
}
} }
if (this->_tjInstance) { if (this->_tjInstance) {
tjDestroy(this->_tjInstance); tjDestroy(this->_tjInstance);
+7
View File
@@ -428,6 +428,13 @@ protected:
int m_nv12LastWidth = 0, m_nv12LastHeight = 0; int m_nv12LastWidth = 0, m_nv12LastHeight = 0;
int m_nv12LastColorspace = -1; int m_nv12LastColorspace = -1;
int m_nv12LastRange = -1; int m_nv12LastRange = -1;
// Guards m_nv12SwsCtx create/use/free. The NV12→BGR conversion deliberately
// runs WITHOUT _mutex (see avframeToCvMat, commit "Fix RTSP stall by
// releasing _mutex during BGR conversion"), so this dedicated lock protects
// the context against close()/dtor freeing it mid-sws_scale without
// reintroducing contention with onVideoFrame. Lock order: _mutex → this
// (close()/dtor hold _mutex first); avframeNV12ToCvMat takes only this lock.
std::mutex m_nv12SwsMutex;
void initSwsContext(int width, int height, AVPixelFormat pixFmt, AVPixelFormat outputPixFmt = AV_PIX_FMT_YUVJ420P); void initSwsContext(int width, int height, AVPixelFormat pixFmt, AVPixelFormat outputPixFmt = AV_PIX_FMT_YUVJ420P);
void initNV12SwsContext(const AVFrame* frame); void initNV12SwsContext(const AVFrame* frame);
+55 -3
View File
@@ -78,6 +78,30 @@ else()
endif() endif()
target_compile_definitions(onnxruntime INTERFACE ORT_API_MANUAL_INIT) target_compile_definitions(onnxruntime INTERFACE ORT_API_MANUAL_INIT)
# ── cuDNN ───────────────────────────────────────────────────────
# TensorRT API headers (engines/TensorRTAPI) include <cudnn_version.h>, so
# cuDNN's include dir must be on the path. cuDNN ships separately from the
# CUDA toolkit, so wire it explicitly rather than relying on it being copied
# into the CUDA install.
if(NOT APPLE)
if(WIN32)
set(CUDNN_INCLUDE_DIR "${ANSLIBS_DIR}/cudnn/include")
set(CUDNN_LIB_DIR "${ANSLIBS_DIR}/cudnn/lib/x64")
else()
set(CUDNN_INCLUDE_DIR "${ANSLIBS_DIR}/cudnn/include")
set(CUDNN_LIB_DIR "${ANSLIBS_DIR}/cudnn/lib")
endif()
add_library(cudnn INTERFACE)
target_include_directories(cudnn INTERFACE ${CUDNN_INCLUDE_DIR})
target_link_directories(cudnn INTERFACE ${CUDNN_LIB_DIR})
if(WIN32)
target_link_libraries(cudnn INTERFACE cudnn.lib)
else()
target_link_libraries(cudnn INTERFACE cudnn)
endif()
message(STATUS "cuDNN: using ANSLibs at ${CUDNN_INCLUDE_DIR}")
endif()
# ── TensorRT (Windows + Linux only) ──────────────────────────── # ── TensorRT (Windows + Linux only) ────────────────────────────
if(NOT APPLE) if(NOT APPLE)
set(TENSORRT_INCLUDE_DIR "${ANSLIBS_DIR}/TensorRT/include") set(TENSORRT_INCLUDE_DIR "${ANSLIBS_DIR}/TensorRT/include")
@@ -86,6 +110,9 @@ if(NOT APPLE)
add_library(tensorrt INTERFACE) add_library(tensorrt INTERFACE)
target_include_directories(tensorrt INTERFACE ${TENSORRT_INCLUDE_DIR}) target_include_directories(tensorrt INTERFACE ${TENSORRT_INCLUDE_DIR})
target_link_directories(tensorrt INTERFACE ${TENSORRT_LIB_DIR}) target_link_directories(tensorrt INTERFACE ${TENSORRT_LIB_DIR})
# cuDNN is a transitive dependency of the TensorRT API headers/engine,
# so anything linking `tensorrt` also gets cuDNN's headers and import lib.
target_link_libraries(tensorrt INTERFACE cudnn)
if(WIN32) if(WIN32)
target_link_libraries(tensorrt INTERFACE nvinfer_plugin_10.lib) target_link_libraries(tensorrt INTERFACE nvinfer_plugin_10.lib)
else() else()
@@ -211,9 +238,34 @@ endif()
# ── Intel MKL ─────────────────────────────────────────────────── # ── Intel MKL ───────────────────────────────────────────────────
add_library(mkl INTERFACE) add_library(mkl INTERFACE)
if(WIN32) if(WIN32)
set(MKL_LIB_DIR "C:/Program Files (x86)/Intel/oneAPI/mkl/2025.0/lib") # oneAPI installs under .../oneAPI/mkl/<version>/ where <version> varies
target_link_directories(mkl INTERFACE "${MKL_LIB_DIR}") # by install (e.g. 2025.0, 2025.3, or a "latest" junction) and the import
target_link_libraries(mkl INTERFACE mkl_rt.lib) # lib lives in either lib/ (newer flat layout) or lib/intel64/ (older).
# Auto-detect rather than hardcoding so it survives MKL version bumps.
set(MKL_LIB_DIR "")
file(GLOB _mkl_versions "C:/Program Files (x86)/Intel/oneAPI/mkl/*")
list(SORT _mkl_versions)
list(REVERSE _mkl_versions) # prefer the newest version dir
foreach(_mkl_root IN LISTS _mkl_versions)
foreach(_sub "lib" "lib/intel64")
if(EXISTS "${_mkl_root}/${_sub}/mkl_rt.lib")
set(MKL_LIB_DIR "${_mkl_root}/${_sub}")
break()
endif()
endforeach()
if(MKL_LIB_DIR)
break()
endif()
endforeach()
if(MKL_LIB_DIR)
target_link_directories(mkl INTERFACE "${MKL_LIB_DIR}")
target_link_libraries(mkl INTERFACE mkl_rt.lib)
message(STATUS "Intel MKL: using ${MKL_LIB_DIR}")
else()
message(WARNING "Intel MKL not found under C:/Program Files (x86)/Intel/oneAPI/mkl. "
"ANSFR (and ANSLIB/ANSFR-UnitTest that link it) will fail to link. "
"Install Intel oneMKL or adjust MKL_LIB_DIR in cmake/Dependencies.cmake.")
endif()
else() else()
# Try system MKL on Linux # Try system MKL on Linux
find_library(MKL_RT_LIB mkl_rt PATHS /opt/intel/oneapi/mkl/latest/lib/intel64) find_library(MKL_RT_LIB mkl_rt PATHS /opt/intel/oneapi/mkl/latest/lib/intel64)
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -461,7 +461,7 @@ namespace ANSCENTER {
") returned null — the DLL may be corrupt."); ") returned null — the DLL may be corrupt.");
s_ort_api = api; s_ort_api = api;
Ort::Global<void>::api_ = api; Ort::InitApi(api);
std::cout << "[EPLoader] ORT loaded successfully." << std::endl; std::cout << "[EPLoader] ORT loaded successfully." << std::endl;
std::cout << "[EPLoader] ORT DLL version : " << base->GetVersionString() << std::endl; std::cout << "[EPLoader] ORT DLL version : " << base->GetVersionString() << std::endl;
+21
View File
@@ -1427,6 +1427,27 @@ namespace ANSCENTER
return str.c_str(); return str.c_str();
} }
// Static, handle-free hardware id. Mirrors ANSLSHelper::GetCurrentHardwareId but has no
// instance/_logger, so diagnostics go to DebugView via ANS_DBG. Returns "" on failure.
std::string ANSLicenseHelper::GetHardwareId() {
try {
const wchar_t* hwid = KeyHelper::GetCurrentHardwareId();
std::wstring result_t = std::wstring(hwid);
std::string result = WString2String(result_t);
ANS_DBG("ANSLicenseHelper", "GetHardwareId -> '%s'", result.c_str());
return result;
}
catch (ANSCENTER::Licensing::Exception* ex) {
ANS_DBG("ANSLicenseHelper", "GetHardwareId failed: %s", ex->GetExceptionMessage());
ex->Destroy();
return "";
}
catch (...) {
ANS_DBG("ANSLicenseHelper", "GetHardwareId failed: unknown exception");
return "";
}
}
bool ANSLicenseHelper::isPathSafe(const std::string& pathStr) { bool ANSLicenseHelper::isPathSafe(const std::string& pathStr) {
// Basic safety checks - customize as needed // Basic safety checks - customize as needed
if (pathStr.empty()) return false; if (pathStr.empty()) return false;
+11
View File
@@ -523,6 +523,10 @@ namespace ANSCENTER
[[nodiscard]] static bool isPathSafe(const std::string& pathStr); [[nodiscard]] static bool isPathSafe(const std::string& pathStr);
public: public:
[[nodiscard]] static bool LicenseVerification(std::string licenseKey, int productId, std::string registrationName); [[nodiscard]] static bool LicenseVerification(std::string licenseKey, int productId, std::string registrationName);
// Returns this machine's hardware id (same value as ANSLSHelper::GetCurrentHardwareId)
// but as a static call requiring no handle/instance — convenient for LabVIEW and other
// callers that just need the HWID. Returns "" on failure.
[[nodiscard]] static std::string GetHardwareId();
[[nodiscard]] static EngineType CheckHardwareInformation(); [[nodiscard]] static EngineType CheckHardwareInformation();
[[nodiscard]] static SystemHealthInfo GetSystemHealthInformation(); [[nodiscard]] static SystemHealthInfo GetSystemHealthInformation();
@@ -564,6 +568,13 @@ extern "C" __declspec(dllexport) void SetLicenseServiceURL(ANSCENTER::ANSLSHelpe
extern "C" __declspec(dllexport) int GetLicenseData(ANSCENTER::ANSLSHelper * *Handle, const char* licenseKey, LStrHandle licenseData); extern "C" __declspec(dllexport) int GetLicenseData(ANSCENTER::ANSLSHelper * *Handle, const char* licenseKey, LStrHandle licenseData);
extern "C" __declspec(dllexport) int LoadLicenseTemplate(ANSCENTER::ANSLSHelper * *Handle, const char* templateFilePath); extern "C" __declspec(dllexport) int LoadLicenseTemplate(ANSCENTER::ANSLSHelper * *Handle, const char* templateFilePath);
extern "C" __declspec(dllexport) int GetCurrentHardwareId(ANSCENTER::ANSLSHelper * *Handle, LStrHandle currentHwID); extern "C" __declspec(dllexport) int GetCurrentHardwareId(ANSCENTER::ANSLSHelper * *Handle, LStrHandle currentHwID);
// Handle-free static hardware id, two flavours:
// - GetHardwareId: LabVIEW native string handle (LStrHandle) output. Returns 1 on success, 0 on failure.
// - GetHardwareIdBuffer: caller-provided C buffer; works from plain C/C++ too (no LabVIEW manager).
// Writes a null-terminated string into 'buffer' (capacity 'bufferSize') and returns the number of
// characters written (excluding the null terminator), or 0 on failure / insufficient buffer.
extern "C" __declspec(dllexport) int GetHardwareId(LStrHandle currentHwID);
extern "C" __declspec(dllexport) int GetHardwareIdBuffer(char* buffer, int bufferSize);
extern "C" __declspec(dllexport) int GenerateKey(ANSCENTER::ANSLSHelper * *Handle, int productId, const char* registrationName, int productFeature, int trialLicense, LStrHandle generatedKey); extern "C" __declspec(dllexport) int GenerateKey(ANSCENTER::ANSLSHelper * *Handle, int productId, const char* registrationName, int productFeature, int trialLicense, LStrHandle generatedKey);
extern "C" __declspec(dllexport) int GenerateKeyWithExpiredDate(ANSCENTER::ANSLSHelper * *Handle, int productId, const char* registrationName, int productFeature, int expiredYear, int expiredMonth, int expiredDate, LStrHandle generatedKey); extern "C" __declspec(dllexport) int GenerateKeyWithExpiredDate(ANSCENTER::ANSLSHelper * *Handle, int productId, const char* registrationName, int productFeature, int expiredYear, int expiredMonth, int expiredDate, LStrHandle generatedKey);
extern "C" __declspec(dllexport) int CheckLicenseKey(ANSCENTER::ANSLSHelper * *Handle, const char* licenseKey, const char* registrationName, int productId);// , LStrHandle strEnableFeature); // Check if the license key is valid extern "C" __declspec(dllexport) int CheckLicenseKey(ANSCENTER::ANSLSHelper * *Handle, const char* licenseKey, const char* registrationName, int productId);// , LStrHandle strEnableFeature); // Check if the license key is valid
+37
View File
@@ -109,6 +109,43 @@ extern "C" __declspec(dllexport) int GetCurrentHardwareId(ANSCENTER::ANSLSHelper
return 0; return 0;
} }
} }
extern "C" __declspec(dllexport) int GetHardwareId(LStrHandle currentHwID) {
try {
std::string st = ANSCENTER::ANSLicenseHelper::GetHardwareId();
int size = (int)st.length();
if (size > 0) {
MgErr error;
error = DSSetHandleSize(currentHwID, sizeof(int32) + size * sizeof(uChar));
if (error == noErr)
{
(*currentHwID)->cnt = size;
memcpy((*currentHwID)->str, st.c_str(), size);
return 1;
}
else return 0;
}
else return 0;
}
catch (...) {
return 0;
}
}
extern "C" __declspec(dllexport) int GetHardwareIdBuffer(char* buffer, int bufferSize) {
try {
if (!buffer || bufferSize <= 0) return 0;
std::string st = ANSCENTER::ANSLicenseHelper::GetHardwareId();
int size = (int)st.length();
if (size <= 0) return 0;
// Need room for the string plus the null terminator.
if (size + 1 > bufferSize) return 0;
memcpy(buffer, st.c_str(), size);
buffer[size] = '\0';
return size;
}
catch (...) {
return 0;
}
}
extern "C" __declspec(dllexport) int GenerateKey(ANSCENTER::ANSLSHelper * *Handle, int productId, const char* registrationName, int productFeature, int trialLicense, LStrHandle generatedKey) { extern "C" __declspec(dllexport) int GenerateKey(ANSCENTER::ANSLSHelper * *Handle, int productId, const char* registrationName, int productFeature, int trialLicense, LStrHandle generatedKey) {
try { try {
if (!Handle || !(*Handle) || !registrationName) return 0; if (!Handle || !(*Handle) || !registrationName) return 0;
+699
View File
@@ -0,0 +1,699 @@
---
title: "ANSCORE — System Architecture (L2 + L3)"
subtitle: "Developer reference for the ANSVIS Communication Layer (CL) backend and the Qt/QML cross-platform desktop UI"
author: "ANSCENTER"
date: "2026-05-26"
---
# Executive summary
ANSCORE is a Vision-AI Video Management System split into three layers:
- **Layer 1 — Edge runtime** (per target). Owns cameras, AI inference, trackers, rules, triggers. Built into the existing `ANSCORE/modules/*` C++ tree (ANSCV, ANSODEngine, ANSFR, ANSLPR, ANSOCR, ANSMOT). For this document L1 is *abstract*: we describe only the interface it exposes upward to L2.
- **Layer 2 — ANSVIS CL** (one per customer). Aggregates targets, persists configuration, ingests events, brokers live media. Exposes a single REST contract (`/api/v1`) to L3. In v1 it is built as an **adapter layer in front of the existing ANSVIS server** — no rewrite required.
- **Layer 3 — Qt/QML desktop UI** (Win / macOS / Linux). The operator-facing application. Talks to L2 exclusively.
**Build assignment:**
- L1 and L2 are built by the **core team** under architect-level review.
- L3 is built by a **6-engineer team**.
- L2 ↔ L3 is **REST + SSE + (HLS / RTSP / WebRTC)** — defined by `contracts/openapi.v1.json` (67 paths, 102 operations, 91 schemas).
The contract is the only artifact both sides block on. Once published, UI engineers develop against a Prism mock; the backend **core team** implements adapters per resource in parallel. Integration is the joining point.
\newpage
# Part I — System overview
## The three layers
![System context](diagrams/png/01-system-context.png){ width=100% }
L3 only ever talks to L2. L2 brokers everything else: configuration push down to edges, event upload from edges, media relay. This keeps the UI team fully decoupled from L1 velocity.
## Three transports between L3 and L2
| # | Transport | Purpose | Carries |
|---|---|---|---|
| 1 | REST over HTTPS | Control plane: all CRUD, configuration, deploy commands | JSON request/response, RFC 7807 errors |
| 2 | Server-Sent Events (SSE) over HTTPS | Live event tail (push of detections, target status) | `text/event-stream`, JSON per event |
| 3 | HLS / RTSP / WebRTC | Live video | Media segments; transport chosen per stream |
The REST API brokers the other two: the UI calls REST to obtain a stream descriptor or SSE endpoint URL.
## Why this split
The earlier proposal of using gRPC end-to-end was reversed because the existing ANSVIS server already exposes a REST API. Pragmatic v1: **reuse what exists**, adapt it into a standards-compliant `/api/v1` surface, ship faster. gRPC remains an option for a future v2 if needed.
## Build ownership at a glance
| Layer | Pace |
|---|---|
| L1 (edge runtime) | Slower — high complexity (frame bus, inference batching, plugin SDK) |
| L2 (CL) | Faster — modular per-resource adapters |
| L3 (Qt/QML UI) | Independent — mock-first development |
\newpage
# Part II — Layer 1 (abstract overview)
L1 is the **edge runtime** that runs on each target. From L2's perspective it is a black box that:
- Receives **configuration** (cameras, AI models, AI tasks, rules, triggers) from L2 and persists it locally.
- Captures frames from cameras (RTSP / RTMP / SRT / ONVIF / simulated).
- Runs **AI pipelines** (object detection, FR, LPR, OCR, MOT, custom plugins).
- Evaluates **rules** and emits **events** with evidence (snapshot frame, optional clip).
- Dispatches **triggers** (HTTP, MQTT, email, push, broker integrations).
- Exposes **liveview** streams (RTSP / SRT / future WebRTC).
- Reports **health / metrics / logs** upward.
L1 ↔ L2 protocol is out of scope for this document. For v1, L2's adapter calls into the legacy ANSVIS server's existing protocol with edges (this is what currently works in production).
For L2 and L3 developers, L1 is treated as a remote service:
- Configuration changes go: **L3 (UI) → L2 (REST) → L1 (legacy protocol) → target reconciles**.
- Events flow: **target → L1 → L2 (legacy uplink) → L2 event store → L2 SSE → L3**.
- Liveview: **L3 asks L2 for a stream descriptor → L2 media relay pulls from L1 → L3 plays from L2's relay URL**.
The detailed L1 architecture (camera pool, inference server, frame bus, plugin SDK, etc.) is documented separately and does not affect L2 / L3 work.
\newpage
# Part III — Layer 2 (ANSVIS CL)
## Architecture
![Layer 2 internals](diagrams/png/02-l2-internals.png){ width=100% }
L2 is composed of four logical groups:
1. **`/api/v1` surface** — REST router, auth middleware, OpenAPI request validation, RFC 7807 error mapper, rate limiter, SSE broadcaster.
2. **v1 adapter layer** — one module per resource (cameras, models, edges, deployment-groups, ai-tasks, events, fr, alpr, notifications, system, jobs). Each adapter translates v1 requests to one or more legacy ANSVIS calls.
3. **Legacy ANSVIS core** — reused as-is. Hosts the existing config store, AI task engine, FR engine, ALPR engine. The adapter is the only layer that calls into it.
4. **Infrastructure** — auth + token store, async job queue, event metadata DB, clip / image object store (S3 / MinIO / FS), logs + metrics, media relay.
## Ownership inside L2
![L2 build ownership](diagrams/png/03-l2-ownership.png){ width=100% }
- **Architect (human)** keeps direct review on the cross-cutting concerns: REST router, auth, validation pipeline, RFC 7807 errors, RBAC, L2 ↔ L1 tunnel security.
- The **core team** builds the per-resource adapters and infrastructure helpers (job queue, media relay). Each module has a sharp boundary so the team can work in parallel without colliding.
- **Existing ANSVIS** is reused as-is. The adapter is the only entry point.
## REST API surface — `/api/v1`
The full contract is `contracts/openapi.v1.json` (OpenAPI 3.1, 67 paths, 102 operations, 91 typed schemas). Summary by resource group:
| Tag | Operations | Purpose |
|---|---:|---|
| `auth` | 3 | Login, logout, refresh |
| `users` | 5 | System users CRUD |
| `cameras` | 10 | CRUD + snapshot + liveview + ONVIF scan + probe |
| `models` | 7 | CRUD + multipart upload + initiate/complete large upload |
| `edges` | 7 | CRUD + health + status |
| `deployment-groups` | 8 | CRUD + status + start/stop |
| `ai-tasks` | 11 | CRUD + status + start/stop + liveview + catalog |
| `events` | 6 | List + summary + SSE stream + image + clip + single |
| `fr` | 14 | Persons + faces + groups + detection config |
| `fr-recording` | 7 | Face-recording tasks + read/assign |
| `alpr` | 8 | Plate + plate-group CRUD |
| `notifications` | 5 | Recipients + groups |
| `triggers` | 2 | Type catalog + test dispatch |
| `system` | 7 | Status, health, info, logs, version, update |
| `jobs` | 1 | Async job polling |
| `composite` | 1 | Convenience endpoint (camera + tasks in one call) |
| **Total** | **102** | |
## Conventions enforced by `openapi.v1.json`
- Plural kebab-case resource paths (`/cameras`, `/ai-tasks`, `/deployment-groups`).
- `snake_case` field names. Units in the field name (`time_interval_seconds`, not `time_interval` with `(s)` suffix).
- IDs in the path, never as query-string keys (replaces legacy `?867560018`).
- `GET / POST / PUT / PATCH / DELETE` used semantically.
- State changes via `POST /resource/{id}/start` and `POST /resource/{id}/stop` (pragmatic exception).
- All errors as RFC 7807 problem+json with the matching HTTP status code.
- List responses use a uniform pagination wrapper: `{items, page, page_size, total, next_cursor}`.
- Auth: `Authorization: Bearer <token>` header, never in URL.
- File uploads up to 10 MB use `multipart/form-data`; larger uploads use the initiate / complete pattern with a presigned PUT URL.
- Live events as SSE; live media via HLS / RTSP / WebRTC behind a descriptor RPC.
- All previously stringified-JSON fields (`Parameters(json)`, `RuleConfig`, `TriggerConfig`, `ModelConfigJson`) are now fully typed nested objects, validated against the schema.
## Migration from legacy ANSVIS endpoints
Every v1 endpoint description in the OpenAPI spec ends with `Replaces:` listing the legacy ANSVIS path(s) it supersedes. Examples:
| Legacy | v1 |
|---|---|
| `GET /Camera` (list mode) | `GET /api/v1/cameras` |
| `GET /Camera?{id}` | `GET /api/v1/cameras/{id}` |
| `DELETE /Camera?{id}` + `POST /delete/Camera?{id}` | `DELETE /api/v1/cameras/{id}` |
| `GET /ControlAITask?ID=&State=true` | `POST /api/v1/ai-tasks/{id}/start` |
| `POST /event/Get` (filter in body) | `GET /api/v1/events?task_id=&from=&to=&...` |
| `POST /FR/AddFace` (base64 in JSON) | `POST /api/v1/fr/persons/{id}/faces` (multipart) |
| `GET /SW/GetSoftwareVersion`, etc. | `GET /api/v1/system/version`, `/health`, `/info`, `/logs`, `/update` |
Legacy endpoints remain available during one release cycle with `Deprecation:` + `Sunset:` headers (RFC 8594).
\newpage
# Part IV — Layer 3 (Qt / QML desktop UI)
## Architecture
![Layer 3 internals](diagrams/png/04-l3-internals.png){ width=100% }
L3 is organized in four layers inside the Qt app:
1. **App Shell** — main window, navigation router, design system / theme, i18n, auth state + token store, global error toast / dialog.
2. **Network layer** — generated REST client (from `openapi.v1.json`), SSE subscriber (chunked `QNetworkReply`), media client (QtMultimedia or GStreamer), bbox overlay renderer (custom `QQuickItem` with `QSGGeometryNode`), multipart + presigned uploader.
3. **Domain state** — `QObject` + `Q_PROPERTY` + signals models per resource (CamerasModel, ModelsModel, EdgesModel, DeploymentGroupsModel, AITasksModel, EventsModel + LiveBuffer, FRPersonsModel, ALPRPlatesModel, RecipientsModel, SystemHealthModel).
4. **Screens (QML)** — the operator surface: Login, Dashboard, Camera management, Pipeline editor, AI task config, Rules / triggers, Liveview grid, Eventview timeline, FR persons + faces, ALPR plates, Model registry, Users / admin, Settings.
## Screen → endpoint map
| Screen | Reads | Mutates | Notes |
|---|---|---|---|
| Login | — | `POST /auth/login` | Stores token in `AuthState` |
| Dashboard | `GET /edges`, `GET /edges/{id}/health`, `GET /system/health` | — | Auto-refresh every 5 s |
| Camera management | `GET /cameras`, `GET /cameras/{id}/snapshot`, `POST /cameras/probe` | `POST/PATCH/DELETE /cameras`, `POST /cameras/scan` (async job) | ONVIF discovery + calibration |
| Pipeline editor | `GET /deployment-groups/{id}`, `GET /ai-tasks/catalog`, `GET /models` | `PUT /deployment-groups/{id}` | Highest-risk feature; QGraphicsView |
| AI task config | `GET /ai-tasks/{id}`, `GET /ai-tasks/catalog` | `POST/PATCH/DELETE /ai-tasks`, `POST /ai-tasks/{id}/{start,stop}` | Schema-driven forms |
| Rules / triggers | `GET /triggers/types` | Embedded in DG / task body | `POST /triggers/test` |
| Liveview | `GET /cameras/{id}/liveview` | — | HLS v1, WebRTC v2; overlay sync to PTS |
| Eventview | `GET /events?...`, `GET /events/stream` (SSE), `/events/{id}/{image,clip}` | — | Virtualized list for 100k+ events |
| FR persons + faces | `GET /fr/persons`, `GET /fr/persons/{id}/faces` | `POST/PATCH/DELETE /fr/persons`, `POST /fr/persons/{id}/faces` (multipart) | Image upload |
| ALPR plates | `GET /alpr/plates`, `GET /alpr/plate-groups` | `POST/DELETE /alpr/plates`, `PATCH /alpr/plate-groups/{id}` | Bulk register / deregister |
| Model registry | `GET /models` | `POST /models` (multipart) or initiate / complete pattern for >100 MB | Upload progress |
| Users / admin | `GET /users` | `POST/PATCH/DELETE /users/{id}` | Admin role only |
| Settings | `GET /system/info`, `GET /system/version`, `GET /system/update` | `POST /system/update` | |
## Engineer ownership
![L3 engineer ownership](diagrams/png/11-l3-engineer-ownership.png){ width=100% }
\
Six engineers, each owning a vertical slice:
| # | Role | Owns |
|---|---|---|
| **E1** | UI Lead / Shell Architect | Qt 6 + CMake monorepo, generated REST client, auth state, design system, navigation router, packaging (Win/Mac/Linux signing, notarize, auto-update), CI matrix |
| **E2** | Pipeline Editor Specialist | Visual DAG editor (`QGraphicsView` in `QQuickWidget`), node library, connection validation, schema-driven param panels, undo / redo, save / version |
| **E3** | Liveview / Media | QtMultimedia HLS player (v1), multi-stream grid, bbox overlay (`QQuickItem` + `QSGGeometryNode`), clip player, WebRTC via GStreamer (v2) |
| **E4** | Config Forms | Schema-driven QML forms from `openapi.v1.json`, camera + ONVIF discovery flow, camera calibration UI (ROI / zones / homography), model + plugin + trigger forms |
| **E5** | Dashboard / Eventview | Target dashboard cards + charts, virtualized event timeline, SSE subscriber + LiveBuffer, filter / search / evidence viewer, multi-target monitoring |
| **E6** | UX / Cross-platform QA | Design polish, accessibility, i18n, install / upgrade testing on Win / Mac / Linux, crash reporting, QML Profiler perf passes, user docs surface |
**Critical-path order for week 1–2:** E1 alone delivers the shell that unblocks E2–E6. E2 runs a node-editor spike in parallel to de-risk the hardest feature early. Once E1's shell lands, E3 / E4 / E5 work in parallel against the mock CL; E6 starts cross-platform validation.
\newpage
# Part V — L2 ↔ L3 communication
## 1. REST control plane
Every CRUD operation, every state change, every configuration push. JSON in, JSON out, RFC 7807 problem+json on error.
### Request envelope
```http
GET /api/v1/cameras?page=1&page_size=50 HTTP/1.1
Host: cms.example.com
Authorization: Bearer eyJhbGc…
Accept: application/json
```
### Success list response
```json
{
"items": [
{ "id": 123, "name": "Front gate", "driver": "rtsp", "active": true, ... }
],
"page": 1,
"page_size": 50,
"total": 247,
"next_cursor": null
}
```
### Error response
```json
{
"type": "https://anscenter.com/errors/validation",
"title": "Validation failed",
"status": 422,
"detail": "One or more fields are invalid",
"instance": "/api/v1/cameras",
"errors": [
{ "field": "address", "message": "Must be a valid RTSP URL" },
{ "field": "capturing.time_interval_seconds", "message": "Must be > 0" }
]
}
```
### Camera create — body example (clean v1)
```json
{
"name": "Front gate",
"driver": "rtsp",
"address": "rtsp://192.168.1.87:554/h264Preview_01_main",
"credentials": { "username": "root", "password": "admin123" },
"capturing": { "mode": "timer", "time_interval_seconds": 0.1 },
"active": true,
"logging_enabled": false,
"edge_id": null,
"deployment_group_ids": []
}
```
Note: no more stringified `Parameters(json)`. No more keys with parentheses or apostrophes. Credentials are nested. Units are in the field name.
## 2. SSE event stream
For live event tail:
```http
GET /api/v1/events/stream?task_id=456 HTTP/1.1
Host: cms.example.com
Authorization: Bearer eyJhbGc…
Accept: text/event-stream
```
Each event:
```
event: detection
data: {"id":"20260526_181023-3","task_id":456,"camera_id":123,
"timestamp":"2026-05-26T18:10:23Z","object":"car","score":0.91,
"bbox":{"x":340,"y":210,"w":120,"h":80}}
```
Qt side: `QNetworkAccessManager` with chunked reads, parse newline-delimited frames, drive `EventsModel.LiveBuffer`.
## 3. Live media
`GET /api/v1/cameras/{id}/liveview?transport=hls` returns a stream descriptor:
```json
{
"url": "https://cms.example.com/relay/cam123.m3u8",
"transport": "hls",
"auth_token": "tkn_short_lived_…",
"expires_at": "2026-05-26T18:15:00Z",
"overlay_stream": "/api/v1/events/stream?task_id=456"
}
```
Qt side: hand the URL to QtMultimedia for HLS playback (v1). For sub-second latency in v2, switch transport to `webrtc` and use GStreamer `webrtcbin`.
\newpage
# Part VI — Key sequence flows
## 6.1 Operator configures a camera + AI task and deploys
![Configure & deploy sequence](diagrams/png/05-seq-configure-deploy.png){ width=92% }
Walk-through:
1. UI lists cameras via `GET /cameras?page=1`.
2. Operator submits the new-camera form. UI sends `POST /cameras` with the *clean* body (nested credentials, capturing, image_size — no stringified JSON).
3. UI creates an AI task for the new camera via `POST /ai-tasks`.
4. Operator clicks "Start". UI calls `POST /ai-tasks/{id}/start` — returns `202 Accepted` with `{state: starting}`.
5. UI polls `GET /ai-tasks/{id}/status` until `state=running`. (Will be replaced by SSE in a later iteration so polling is optional.)
## 6.2 Live event tail with evidence
![Live event tail sequence](diagrams/png/06-seq-live-events.png){ width=92% }
Walk-through:
1. UI fetches historical events via `GET /events?task_id=&page=1` and renders the timeline.
2. UI opens an SSE stream `GET /events/stream?task_id=` for live tail.
3. Edge produces an event → L2 ingests → SSE broadcaster pushes the `EventRecord` to all subscribed UIs (filtered by their query).
4. Operator clicks an event. UI fetches `GET /events/{id}/image?width=640` for the snapshot.
5. Operator plays the clip. UI fetches `GET /events/{id}/clip` to get a presigned URL, then GETs the MP4 directly from the object store. The CL process never touches the video bytes — they flow client ↔ S3/MinIO.
## 6.3 Liveview with bbox overlay
![Liveview sequence](diagrams/png/07-seq-liveview.png){ width=92% }
Walk-through:
1. UI requests a liveview descriptor for camera 123: `GET /cameras/123/liveview?transport=hls`.
2. L2's media relay ensures an HLS endpoint is running for that camera (pulling RTSP from the edge in the background).
3. L2 returns `{url, transport:hls, auth_token, expires_at}`.
4. UI plays the HLS URL in QtMultimedia.
5. Simultaneously the UI subscribes to `/events/stream?task_id=456` (SSE) for the AI task's detections.
6. The Qt overlay layer (custom `QQuickItem`) renders bounding boxes synced to the video PTS.
For v2 with WebRTC, the descriptor returns `transport:webrtc` and a signaling URL; GStreamer's `webrtcbin` handles negotiation.
## 6.4 Async camera scan via job queue
![Async scan sequence](diagrams/png/08-seq-async-scan.png){ width=92% }
Walk-through:
1. Operator triggers an ONVIF scan of a subnet. UI calls `POST /cameras/scan` with `{ip_range, credentials, timeout_seconds}`.
2. L2 enqueues the work and immediately returns `202 Accepted` with `{job_id, status:queued, links:{self:"/jobs/j1"}}`.
3. The job worker performs the scan asynchronously.
4. UI polls `GET /jobs/{id}` until `status` becomes `succeeded` or `failed`. The terminal response includes `result` with the discovered cameras.
This pattern is used for every potentially-long operation: ONVIF scan, large model upload, deploy-to-many-targets, software update.
## 6.5 Large model upload (presigned URL)
![Large model upload sequence](diagrams/png/09-seq-large-upload.png){ width=92% }
Walk-through:
1. UI calls `POST /models:initiate-upload` with `{filename, size_bytes, checksum}`. L2 returns `{upload_id, presigned_url, expires_at}`.
2. UI PUTs the bytes directly to the object store using the presigned URL. The CL process never sees the bytes — saves CPU and avoids HTTP-body-size limits.
3. UI calls `POST /models:complete-upload` with `{upload_id, metadata}`. L2 verifies the checksum and registers the model.
For files under 10 MB, the simple `POST /models` multipart endpoint can be used instead.
\newpage
# Part VII — Deployment topology
![Deployment topology](diagrams/png/10-deployment-topology.png){ width=100% }
## Hosts
| Host | Runs | Notes |
|---|---|---|
| Operator workstation (Win / Mac / Linux) | Qt UI application | One per concurrent operator |
| ANSVIS server host | Reverse proxy (nginx / caddy) + ANSVIS app (adapter + legacy core) + Media relay + Object store + Config / Event DBs | One per customer site (can be HA later) |
| Edge target | L1 runtime | One per physical capture site |
## Customer network rules
| Direction | Port | Purpose |
|---|---|---|
| Workstation → ANSVIS server | TCP 443 | REST + SSE + HLS over TLS |
| ANSVIS server → workstation | — (response only) | |
| ANSVIS server ↔ edges | TBD per deployment | Existing ANSVIS protocol |
| Workstation → edges | **None** | Workstations never reach edges directly. Media relay proxies. |
## Why workstations don't reach edges
- Edges typically sit behind customer firewalls / NAT.
- One TLS endpoint (ANSVIS) is easier to firewall, monitor, and authenticate against than dozens of edges.
- Token rotation happens centrally.
- The media relay can transcode, cache, or split streams.
\newpage
# Part VIII — Delivery plan
## Mock-first development
The contract `openapi.v1.json` is the only artifact both teams block on. Once it is published:
- **UI team** runs Stoplight Prism (`prism mock contracts/openapi.v1.json`) as a local mock CL and develops against it from day 1. No need to wait for the real adapter.
- The **backend core team** implements the adapter resource-by-resource in parallel, validating each endpoint against the same OpenAPI schema.
- **Integration** is the joining point. UI tests run against both mock and real to detect drift.
## Critical path
![Dependency chain](diagrams/png/12-dependency-chain.png){ width=100% }
## Sequencing
| Phase | Week | Activities |
|---|---|---|
| **Contract freeze** | 1 | Lock `openapi.v1.json` v1.0.0 (3 small decisions outstanding — see Part IX) |
| **Foundations** | 1–2 | E1: Qt 6 + CMake monorepo, generated REST client, Prism mock wired up, design system stub, app shell. <br/> E2: node-editor spike (de-risk). <br/> Core team: cameras / models / edges / system adapters in parallel. |
| **Vertical slices** | 3–6 | E3 (Liveview), E4 (Config forms), E5 (Dashboard / Eventview) build full features against the mock. <br/> Core team: ai-tasks / deployment-groups / events / fr / alpr / notifications adapters. |
| **Real integration** | 7–8 | UI cuts over from mock to real `/api/v1` per screen as adapters land. E6 runs cross-platform install / upgrade testing on Win / Mac / Linux. |
| **Hardening** | 9–10 | Performance passes (QML Profiler, REST tracing), security review (architect-led), crash reporting, packaging signing / notarization. |
| **Ship v1** | 11–12 | Pilot deployment, customer training, feedback loop. |
\newpage
# Part IX — Open decisions
These need to be locked before contract v1.0.0 publishes.
| # | Decision | Default in spec | Notes |
|---|---|---|---|
| 1 | Field naming | `snake_case` | Could switch to `camelCase` — match Qt/C++ style. One find-replace pass on `openapi.v1.json` either way. |
| 2 | Auth scheme | Bearer (existing ANSVIS token) | v1 keeps Bearer; OAuth2/OIDC for v2. |
| 3 | Mock server tool | Stoplight Prism (free) | Alternative: build a richer custom mock with scenarios. Recommend Prism. |
| 4 | Liveview transport for v1 | HLS via QtMultimedia | Easiest. Add WebRTC in v2 if sub-second latency is needed. |
| 5 | Object store | S3 / MinIO / local FS | Choose per deployment; behind a generic `ObjectStore` interface. |
| 6 | Auto-update mechanism | Sparkle (Mac), MSIX / custom (Win), AppImage (Linux) | Confirm budget for code signing per platform. |
| 7 | Contract repo location | `ANSCORE/contracts/` (current) | Separate `anscore-contracts` repo if teams diverge. |
\newpage
# Appendix A — Full endpoint catalog
Complete list of v1 operations. See `contracts/openapi.v1.json` for full request / response schemas.
## auth
| Method | Path | Purpose |
|---|---|---|
| POST | `/auth/login` | Log in (returns `access_token`, `expires_at`, `user`) |
| POST | `/auth/logout` | Invalidate token |
| POST | `/auth/refresh` | Exchange refresh token for new access token |
## users
| Method | Path | Purpose |
|---|---|---|
| GET | `/users` | List users (paginated) |
| POST | `/users` | Create user |
| GET | `/users/{id}` | Get user |
| PATCH | `/users/{id}` | Update user |
| DELETE | `/users/{id}` | Delete user |
## cameras
| Method | Path | Purpose |
|---|---|---|
| GET | `/cameras` | List cameras (filters: driver, active, edge_id, deployment_group_id) |
| POST | `/cameras` | Create camera |
| GET | `/cameras/{id}` | Get camera |
| PUT | `/cameras/{id}` | Replace camera |
| PATCH | `/cameras/{id}` | Patch camera |
| DELETE | `/cameras/{id}` | Delete camera |
| GET | `/cameras/{id}/snapshot` | Get JPEG snapshot (or presigned URL) |
| GET | `/cameras/{id}/liveview` | Get liveview descriptor (`{url, transport, token, expires_at}`) |
| POST | `/cameras/scan` | Async ONVIF scan (returns job) |
| POST | `/cameras/probe` | Probe single IP for ONVIF profiles |
## models
| Method | Path | Purpose |
|---|---|---|
| GET | `/models` | List models (filter by type) |
| POST | `/models` | Upload + register (multipart, ≤10 MB) |
| GET | `/models/{id}` | Get model |
| PATCH | `/models/{id}` | Update model metadata |
| DELETE | `/models/{id}` | Delete model |
| POST | `/models:initiate-upload` | Begin large upload (returns presigned PUT URL) |
| POST | `/models:complete-upload` | Finalize large upload |
## edges
| Method | Path | Purpose |
|---|---|---|
| GET | `/edges` | List edges |
| POST | `/edges` | Register edge |
| GET | `/edges/{id}` | Get edge |
| PATCH | `/edges/{id}` | Update edge |
| DELETE | `/edges/{id}` | Delete edge |
| GET | `/edges/{id}/health` | Resource / temp / uptime |
| GET | `/edges/{id}/status` | State (online/offline/degraded) + counts |
## deployment-groups
| Method | Path | Purpose |
|---|---|---|
| GET | `/deployment-groups` | List groups |
| POST | `/deployment-groups` | Create group |
| GET | `/deployment-groups/{id}` | Get group |
| PUT | `/deployment-groups/{id}` | Replace group |
| DELETE | `/deployment-groups/{id}` | Delete group |
| GET | `/deployment-groups/{id}/status` | Runtime status |
| POST | `/deployment-groups/{id}/start` | Start group |
| POST | `/deployment-groups/{id}/stop` | Stop group |
## ai-tasks
| Method | Path | Purpose |
|---|---|---|
| GET | `/ai-tasks` | List tasks |
| POST | `/ai-tasks` | Create task |
| GET | `/ai-tasks/{id}` | Get task |
| PUT | `/ai-tasks/{id}` | Replace task |
| PATCH | `/ai-tasks/{id}` | Patch task |
| DELETE | `/ai-tasks/{id}` | Delete task |
| GET | `/ai-tasks/{id}/status` | Get task runtime status |
| POST | `/ai-tasks/{id}/start` | Start task |
| POST | `/ai-tasks/{id}/stop` | Stop task |
| GET | `/ai-tasks/{id}/liveview` | Liveview descriptor for annotated stream |
| GET | `/ai-tasks/catalog` | List supported task types |
## events
| Method | Path | Purpose |
|---|---|---|
| GET | `/events` | List events (filters: task_id, camera_id, object, from, to) |
| GET | `/events/summary` | Analytics rollup |
| GET | `/events/stream` | Live event tail (Server-Sent Events) |
| GET | `/events/{id}` | Get one event |
| GET | `/events/{id}/image` | JPEG snapshot |
| GET | `/events/{id}/clip` | Presigned URL for evidence clip |
## fr (face recognition)
| Method | Path | Purpose |
|---|---|---|
| GET | `/fr/persons` | List persons |
| POST | `/fr/persons` | Create person |
| GET | `/fr/persons/{id}` | Get person |
| PATCH | `/fr/persons/{id}` | Update person |
| DELETE | `/fr/persons/{id}` | Delete person |
| GET | `/fr/persons/{id}/faces` | List faces for person |
| POST | `/fr/persons/{id}/faces` | Add face (multipart, no base64) |
| DELETE | `/fr/faces/{face_id}` | Delete face |
| GET | `/fr/person-groups` | List person groups |
| POST | `/fr/person-groups` | Create group |
| PATCH | `/fr/person-groups/{id}` | Update group |
| DELETE | `/fr/person-groups/{id}` | Delete group |
| GET | `/fr/config` | Get FR detection config |
| PATCH | `/fr/config` | Update FR detection config |
## fr-recording
| Method | Path | Purpose |
|---|---|---|
| GET | `/fr/recording-tasks` | List tasks |
| POST | `/fr/recording-tasks` | Create task |
| DELETE | `/fr/recording-tasks/{id}` | Delete task |
| POST | `/fr/recording-tasks/{id}/start` | Start task |
| POST | `/fr/recording-tasks/{id}/stop` | Stop task |
| GET | `/fr/recording-tasks/{id}/faces` | Read recorded faces |
| PATCH | `/fr/recording-tasks/{id}/faces` | Assign recorded faces to a person (bulk) |
## alpr
| Method | Path | Purpose |
|---|---|---|
| GET | `/alpr/plates` | List plates |
| POST | `/alpr/plates` | Register plates (bulk) |
| DELETE | `/alpr/plates/{plate}` | Deregister single plate |
| POST | `/alpr/plates:bulk-delete` | Deregister bulk |
| GET | `/alpr/plate-groups` | List plate groups |
| POST | `/alpr/plate-groups` | Create group |
| PATCH | `/alpr/plate-groups/{id}` | Update group |
| DELETE | `/alpr/plate-groups/{id}` | Delete group |
## notifications
| Method | Path | Purpose |
|---|---|---|
| GET | `/notifications/recipients` | List recipients |
| POST | `/notifications/recipients` | Create recipient |
| PATCH | `/notifications/recipients/{id}` | Update recipient |
| DELETE | `/notifications/recipients/{id}` | Delete recipient |
| GET | `/notifications/recipient-groups` | List recipient groups |
## triggers
| Method | Path | Purpose |
|---|---|---|
| GET | `/triggers/types` | List supported trigger types + config schemas |
| POST | `/triggers/test` | Dispatch a test trigger (HTTP / MQTT / Email / etc.) |
## system
| Method | Path | Purpose |
|---|---|---|
| GET | `/system/status` | Liveness probe |
| GET | `/system/health` | Resources + counts |
| GET | `/system/info` | Hostname / OS / hardware |
| GET | `/system/logs` | Paginated logs |
| GET | `/system/version` | Version + build date + commit |
| GET | `/system/update` | Update status |
| POST | `/system/update` | Trigger update (async job) |
## jobs
| Method | Path | Purpose |
|---|---|---|
| GET | `/jobs/{id}` | Get async job status / result |
## composite
| Method | Path | Purpose |
|---|---|---|
| POST | `/composite/camera-with-tasks` | Create camera + deployment groups in one call |
\newpage
# Appendix B — Artifacts in the repository
```
ANSCORE/
├── contracts/
│ └── openapi.v1.json # The single source of truth for L2 ↔ L3
│
├── docs/
│ ├── ANSCORE-system-architecture.md # This document (source)
│ ├── ANSCORE-system-architecture.pdf # This document (rendered)
│ ├── L2-L3-architecture.md # Same content with Mermaid diagrams
│ └── diagrams/
│ ├── 01-system-context.puml # PlantUML sources
│ ├── 02-l2-internals.puml
│ ├── 03-l2-ownership.puml
│ ├── 04-l3-internals.puml
│ ├── 05-seq-configure-deploy.puml
│ ├── 06-seq-live-events.puml
│ ├── 07-seq-liveview.puml
│ ├── 08-seq-async-scan.puml
│ ├── 09-seq-large-upload.puml
│ ├── 10-deployment-topology.puml
│ ├── 11-l3-engineer-ownership.puml
│ ├── 12-dependency-chain.puml
│ ├── README.md
│ ├── PREVIEW.md # Clickable plantuml.com URLs
│ └── png/ # Pre-rendered PNGs (used in this PDF)
│
└── modules/ # Existing C++ building blocks (L1 ingredients)
├── ANSCV/ ANSODEngine/ ANSFR/ ANSLPR/ ANSOCR/
├── ANSMOT/ ANSPulsar/ ANSRabbitMQ/ ANSONVIF/
└── ...
```
# Appendix C — Glossary
| Term | Meaning |
|---|---|
| **L1** | Layer 1 — edge runtime (per target) |
| **L2** | Layer 2 — ANSVIS CL (per customer) |
| **L3** | Layer 3 — Qt/QML desktop UI (per operator workstation) |
| **Adapter (v1)** | The L2 module that translates `/api/v1` requests to legacy ANSVIS calls |
| **Edge / target** | A physical device running L1 (Windows/Linux on x86 or NVIDIA Jetson) |
| **Deployment group** | A bundle of cameras + models + rules + triggers deployed to a target |
| **AI task** | A configured pipeline (camera + model + rules + triggers) ready to run |
| **Event** | A detection emitted by an AI task, with evidence (snapshot + optional clip) |
| **Liveview** | Real-time video from a camera, with optional bbox overlay |
| **Trigger** | An outbound action when a rule fires (HTTP, MQTT, email, push, broker) |
| **ONVIF** | Camera-discovery and control protocol used by IP cameras |
| **SSE** | Server-Sent Events — one-way push from server to client over HTTPS |
| **RFC 7807** | The `application/problem+json` standard error format |
| **Prism** | Stoplight's CLI for spinning up a mock REST server from an OpenAPI spec |
| **OpenAPI 3.1** | The contract format used in `openapi.v1.json` |
---
*End of document.*
Binary file not shown.
+600
View File
@@ -0,0 +1,600 @@
# ANSCORE — Layer 2 & Layer 3 Architecture
> Scope: Layer 2 (ANSVIS Communication Layer (CL), REST `/api/v1` surface, supporting services) and Layer 3 (Qt/QML cross-platform desktop UI). Layer 1 is summarized only where it interacts.
>
> Contract of record: [`contracts/openapi.v1.json`](../contracts/openapi.v1.json). All UI ↔ CL communication conforms to it.
>
> Transports:
> 1. **REST / HTTPS** — control, CRUD, configuration, deploy
> 2. **SSE (`text/event-stream`)** — live event tail
> 3. **HLS / RTSP / WebRTC** — live video (descriptor obtained via REST)
---
## 1. System context — L1 / L2 / L3
```mermaid
graph TB
subgraph L3["Layer 3 — Qt/QML Desktop UI (Win / Mac / Linux)"]
UI[Qt/QML application]
end
subgraph L2["Layer 2 — ANSVIS CL"]
APIV1["/api/v1 REST surface"]
LEGACY["Legacy ANSVIS core<br/>(reused as-is)"]
RELAY["Media relay<br/>HLS / RTSP / WebRTC"]
SSE["SSE event broadcaster"]
STORE["Event DB<br/>+ clip object store"]
JOBS["Async job queue"]
AUTH["Auth + RBAC"]
end
subgraph L1["Layer 1 — ANSCORE Edge Runtime<br/>(one per target)"]
EDGE["Camera + Inference + Triggers"]
end
UI -- "① REST / HTTPS<br/>control + CRUD" --> APIV1
UI -- "② SSE / HTTPS<br/>live event tail" --> SSE
UI -- "③ HLS / RTSP / WebRTC<br/>live video" --> RELAY
APIV1 --> AUTH
APIV1 --> LEGACY
APIV1 --> JOBS
APIV1 --> STORE
LEGACY --> STORE
STORE --> SSE
RELAY <--> EDGE
LEGACY <--> EDGE
```
**Key idea:** L3 only ever talks to L2. L1 ↔ L2 is a separate protocol (existing ANSVIS behavior, plus new edge work later). The UI team is fully decoupled from L1 velocity.
---
## 2. Layer 2 — internal architecture
```mermaid
graph TB
subgraph CLIENTS["Clients"]
QT["Qt/QML UI"]
EXT["3rd-party integrations<br/>(optional)"]
end
subgraph EDGE_API["L2 / api / v1 surface"]
ROUTER["REST router"]
AUTHMW["Auth middleware<br/>(Bearer)"]
VALID["Request validation<br/>(OpenAPI schemas)"]
ERR["Error mapper<br/>→ RFC 7807"]
RATE["Rate limiter"]
SSE_HUB["SSE broadcaster<br/>(per-subscription filter)"]
end
subgraph ADAPTER["v1 Adapter Layer<br/>(per-resource modules)"]
CAM_AD["cameras"]
MOD_AD["models"]
EDGE_AD["edges"]
DG_AD["deployment-groups"]
TASK_AD["ai-tasks"]
EV_AD["events"]
FR_AD["fr"]
ALPR_AD["alpr"]
NOTIF_AD["notifications"]
SYS_AD["system"]
JOB_AD["jobs"]
end
subgraph LEGACY["Legacy ANSVIS server<br/>(reused as-is)"]
LEG_CFG["Config store"]
LEG_TASKS["AI task engine"]
LEG_FR["FR engine"]
LEG_ALPR["ALPR engine"]
end
subgraph INFRA["Infrastructure"]
AUTH_SVC["Auth + token store"]
JOBS_Q["Job queue<br/>(scans, uploads, deploys, updates)"]
EVENTS_DB["Event metadata DB"]
CLIPS["Clip / image object store<br/>(S3 / MinIO / FS)"]
LOGS["Logs + metrics + health"]
RELAY["Media relay<br/>HLS / RTSP / WebRTC"]
end
subgraph L1["Layer 1 edges"]
E1["Edge target(s)"]
end
QT --> ROUTER
EXT --> ROUTER
ROUTER --> AUTHMW --> VALID --> RATE
RATE --> ADAPTER
ADAPTER --> ERR
ADAPTER -.-> ROUTER
AUTHMW --> AUTH_SVC
CAM_AD --> LEG_CFG
MOD_AD --> LEG_CFG
MOD_AD --> CLIPS
EDGE_AD --> LEG_CFG
DG_AD --> LEG_TASKS
TASK_AD --> LEG_TASKS
EV_AD --> EVENTS_DB
EV_AD --> CLIPS
FR_AD --> LEG_FR
ALPR_AD --> LEG_ALPR
SYS_AD --> LOGS
JOB_AD --> JOBS_Q
CAM_AD -. "scan / probe" .-> JOBS_Q
MOD_AD -. "large upload" .-> JOBS_Q
LEG_TASKS --> EVENTS_DB
EVENTS_DB --> SSE_HUB
SSE_HUB --> ROUTER
LEGACY <--> E1
RELAY <--> E1
```
**Responsibilities by module:**
| Module | Owns |
|---|---|
| REST router | URL → handler, content negotiation |
| Auth middleware | Bearer validation, principal injection, RBAC checks |
| Request validation | OpenAPI schema-based body/query validation |
| Error mapper | Translates legacy errors / exceptions to `application/problem+json` |
| Rate limiter | Per-token / per-IP throttling |
| SSE broadcaster | Subscribes to event bus, fans out to UI clients with filter + auth |
| v1 adapter (per resource) | Translates v1 request → legacy call(s), reshapes response |
| Job queue | Async ops (camera scan, model upload, software update, large deploy) |
| Event DB | Indexed event metadata for `/events?...` queries |
| Clip / object store | Snapshots and evidence video (presigned URLs) |
| Media relay | Brokers live video from edge to UI (HLS now, WebRTC later) |
| Auth + token store | Login, refresh, RBAC |
**Build ownership inside L2** (AI coders managed by you):
```mermaid
graph TB
subgraph HUMAN["Human-owned (architect — you)"]
H1["REST router + auth middleware"]
H2["RFC 7807 error mapper"]
H3["OpenAPI validation pipeline"]
H4["Auth flow + token store"]
H5["RBAC + security review"]
H6["L2 ↔ L1 tunnel + signing"]
end
subgraph AI["AI-coder-built (modular adapters)"]
A1["cameras adapter"]
A2["models adapter + upload pipeline"]
A3["edges adapter"]
A4["deployment-groups adapter"]
A5["ai-tasks adapter"]
A6["events adapter + SSE broadcaster"]
A7["fr adapter"]
A8["alpr adapter"]
A9["notifications adapter"]
A10["system / health / logs adapter"]
A11["job queue + workers"]
A12["media relay"]
end
subgraph EXIST["Existing ANSVIS (reused as-is)"]
L1["Task engine"]
L2["FR engine"]
L3["ALPR engine"]
L4["Config store"]
end
HUMAN --> AI
AI --> EXIST
```
---
## 3. Layer 3 — Qt/QML internal architecture
```mermaid
graph TB
subgraph QT["Qt 6 + QML — Layer 3"]
subgraph SHELL["App Shell"]
MAIN["Main window"]
ROUTE["Navigation router"]
THEME["Design system / theme"]
I18N["i18n (qsTr / Linguist)"]
AUTH_ST["Auth state + token store"]
ERR_UI["Error toast / dialog"]
end
subgraph NET["Network layer"]
REST["Generated REST client<br/>(openapi.v1.json → C++/Qt)"]
SSE_CL["SSE subscriber<br/>(QNetworkReply chunked)"]
MEDIA["Media client<br/>QtMultimedia / GStreamer"]
OVERLAY["Bbox overlay renderer<br/>(custom QQuickItem)"]
UPLOAD["Multipart + presigned uploader"]
end
subgraph STATE["Domain state (QObject + Q_PROPERTY + signals)"]
ST_CAM["CamerasModel"]
ST_MOD["ModelsModel"]
ST_EDGE["EdgesModel"]
ST_DG["DeploymentGroupsModel"]
ST_TASK["AITasksModel"]
ST_EV["EventsModel + LiveBuffer"]
ST_FR["FRPersonsModel"]
ST_ALPR["ALPRPlatesModel"]
ST_NOTIF["RecipientsModel"]
ST_SYS["SystemHealthModel"]
end
subgraph SCREENS["Screens (QML)"]
LOGIN["Login"]
DASH["Dashboard / targets overview"]
CAM_S["Camera management<br/>+ ONVIF discovery<br/>+ calibration"]
DG_EDIT["Pipeline editor<br/>(visual DAG)"]
TASK_EDIT["AI Task config"]
RULE_S["Rules / triggers"]
LIVE["Liveview grid"]
EV_S["Eventview timeline"]
FR_S["FR persons + faces"]
ALPR_S["ALPR plates"]
MODEL_S["Model registry"]
USERS["Users / admin"]
SETTINGS["Settings"]
end
end
subgraph L2["ANSVIS / api / v1"]
BACKEND["REST + SSE + media"]
end
SCREENS --> STATE
STATE --> NET
NET <--> BACKEND
OVERLAY -.-> MEDIA
AUTH_ST -.-> REST
AUTH_ST -.-> SSE_CL
```
**Module → screen map** (for tasking):
| Screen | Reads from | Writes via | Notes |
|---|---|---|---|
| Login | — | `POST /auth/login` | Stores token in `AuthState` |
| Dashboard | `GET /edges`, `GET /edges/{id}/health`, `GET /system/health` | — | Auto-refresh every 5s |
| Camera management | `GET /cameras`, `GET /cameras/{id}/snapshot`, `POST /cameras/probe` | `POST/PATCH/DELETE /cameras`, `POST /cameras/scan` (async job) | ONVIF discovery + calibration overlay |
| Pipeline editor | `GET /deployment-groups/{id}`, `GET /ai-tasks/catalog`, `GET /models` | `PUT /deployment-groups/{id}` | Highest-risk feature; QGraphicsView |
| AI Task config | `GET /ai-tasks/{id}`, `GET /ai-tasks/catalog` | `POST/PATCH/DELETE /ai-tasks`, `POST /ai-tasks/{id}/start|stop` | Schema-driven forms |
| Rules / triggers | `GET /triggers/types` | embedded in deployment-group / task body | `POST /triggers/test` |
| Liveview | `GET /cameras/{id}/liveview` | — | HLS in v1, WebRTC later; overlay sync to PTS |
| Eventview | `GET /events?filters`, `GET /events/stream` (SSE), `GET /events/{id}/image`, `GET /events/{id}/clip` | — | Virtualized list for 100k+ events |
| FR persons + faces | `GET /fr/persons`, `GET /fr/persons/{id}/faces` | `POST/PATCH/DELETE /fr/persons`, `POST /fr/persons/{id}/faces` (multipart) | Multipart image upload |
| ALPR plates | `GET /alpr/plates`, `GET /alpr/plate-groups` | `POST/DELETE /alpr/plates`, `PATCH /alpr/plate-groups/{id}` | Bulk register/deregister |
| Model registry | `GET /models` | `POST /models` (multipart), `POST /models:initiate-upload` for big files | Upload progress UI |
| Users / admin | `GET /users` | `POST/PATCH/DELETE /users/{id}` | Admin role only |
| Settings | `GET /system/info`, `GET /system/version`, `GET /system/update` | `POST /system/update` | |
---
## 4. Sequence flows
### 4.1 Configure & deploy — typical operator workflow
```mermaid
sequenceDiagram
actor User as Operator
participant UI as Qt UI
participant API as ANSVIS /api/v1
participant LEG as ANSVIS core
participant EDGE as L1 edge
User->>UI: Open Cameras screen
UI->>API: GET /cameras?page=1
API->>LEG: legacy list
LEG-->>API: cameras
API-->>UI: 200 {items, page, total}
User->>UI: Add RTSP camera (form)
UI->>API: POST /cameras
Note right of UI: body has nested credentials,<br/>capturing, image_size — no<br/>stringified JSON
API->>LEG: legacy add
LEG-->>API: id=123
API-->>UI: 201 Location: /cameras/123
User->>UI: Configure AI task
UI->>API: POST /ai-tasks {camera_id:123, ...}
API->>LEG: legacy create
LEG-->>API: id=456
API-->>UI: 201 {id:456}
User->>UI: Start task
UI->>API: POST /ai-tasks/456/start
API->>LEG: dispatch
LEG->>EDGE: config + start
EDGE-->>LEG: running
API-->>UI: 202 {state:starting}
loop status poll (or SSE later)
UI->>API: GET /ai-tasks/456/status
API-->>UI: 200 {state:running, fps:14.2}
end
```
### 4.2 Live event tail + clicking into evidence
```mermaid
sequenceDiagram
actor User
participant UI
participant API as /api/v1
participant SSE as SSE broadcaster
participant CLIPS as Clip store
participant EDGE
User->>UI: Open Eventview (task=456)
UI->>API: GET /events?task_id=456&page=1
API-->>UI: 200 historical events
UI->>API: GET /events/stream?task_id=456
API-->>UI: text/event-stream (open)
EDGE->>API: event arrives (via L1↔L2)
API->>SSE: push EventRecord
SSE-->>UI: data: {id:E1, ts, object:car, bbox, ...}
User->>UI: Click event E1
UI->>API: GET /events/E1
API-->>UI: 200 EventRecord (full)
UI->>API: GET /events/E1/image?width=640
API-->>UI: image/jpeg bytes
User->>UI: Play clip
UI->>API: GET /events/E1/clip
API->>CLIPS: presign GET
CLIPS-->>API: presigned URL
API-->>UI: 200 {url, expires_at}
UI->>CLIPS: GET <presigned URL>
CLIPS-->>UI: MP4 bytes
UI->>UI: play in QtMultimedia
```
### 4.3 Liveview with bbox overlay
```mermaid
sequenceDiagram
actor User
participant UI
participant API
participant RELAY as Media relay
participant EDGE
participant SSE
User->>UI: Open Liveview (cam 123, task 456)
UI->>API: GET /cameras/123/liveview?transport=hls
API->>RELAY: ensure HLS for cam 123
RELAY->>EDGE: pull RTSP
RELAY-->>API: hls_url + short-lived token
API-->>UI: 200 {url, transport:hls, auth_token, expires_at}
UI->>RELAY: GET hls_url (token)
RELAY-->>UI: m3u8 + segments
UI->>UI: Render video (QtMultimedia)
UI->>API: GET /events/stream?task_id=456 (SSE)
API-->>UI: text/event-stream
loop per event
SSE-->>UI: bbox + ts
UI->>UI: overlay synced to PTS
end
```
### 4.4 Async job — camera network scan
```mermaid
sequenceDiagram
actor User
participant UI
participant API
participant JOBS as Job queue
participant SCAN as ONVIF scanner
User->>UI: Scan 192.168.1.0/24
UI->>API: POST /cameras/scan
API->>JOBS: enqueue
JOBS-->>API: job_id=j1
API-->>UI: 202 {job_id:j1, status:queued, links:{self:/jobs/j1}}
JOBS->>SCAN: run
loop poll until terminal
UI->>API: GET /jobs/j1
API-->>UI: 200 {status:running, progress:0.4}
end
SCAN-->>JOBS: results
UI->>API: GET /jobs/j1
API-->>UI: 200 {status:succeeded, result:{discovered:[...]}}
```
### 4.5 Large model upload — initiate / complete pattern
```mermaid
sequenceDiagram
actor User
participant UI
participant API
participant OBJ as Object store
User->>UI: Pick model.zip (500 MB)
UI->>API: POST /models:initiate-upload {filename, size_bytes, checksum}
API->>OBJ: presign PUT
OBJ-->>API: presigned_url
API-->>UI: 200 {upload_id, presigned_url, expires_at}
UI->>OBJ: PUT <presigned_url> (binary stream, with progress)
OBJ-->>UI: 200 OK
UI->>API: POST /models:complete-upload {upload_id, metadata}
API->>OBJ: verify checksum
OBJ-->>API: ok
API-->>UI: 201 Model
```
---
## 5. Deployment topology
```mermaid
graph LR
subgraph CUSTOMER["Customer site / cloud"]
subgraph DESKTOPS["Operator workstations"]
D1["Win desktop<br/>Qt UI"]
D2["Mac desktop<br/>Qt UI"]
D3["Linux desktop<br/>Qt UI"]
end
subgraph SERVER["ANSVIS server host"]
NGINX["Reverse proxy<br/>TLS terminator"]
APP["ANSVIS app<br/>(adapter + legacy core)"]
RELAY["Media relay"]
OBJ["Object store<br/>(S3/MinIO/FS)"]
DB["DBs<br/>(config / events)"]
end
subgraph EDGES["Edge targets"]
E1["Linux x86 + NV"]
E2["Win x86 + NV"]
E3["Jetson Orin"]
end
end
D1 -- "TLS 443<br/>REST + SSE + HLS" --> NGINX
D2 -- "TLS 443" --> NGINX
D3 -- "TLS 443" --> NGINX
NGINX --> APP
NGINX --> RELAY
APP --> DB
APP --> OBJ
RELAY <--> EDGES
APP <--> EDGES
```
**Network rules to communicate to the customer:**
- Outbound TCP 443 from each operator workstation to ANSVIS server.
- Inbound 443 on ANSVIS server.
- Whatever protocol L1 ↔ L2 needs (TCP 443 if everything tunneled; or per-edge ports if direct).
- No need for the workstation to reach edges directly — Media relay proxies.
---
## 6. L3 — engineer ownership (6 engineers)
```mermaid
graph TB
subgraph E1["E1 — UI Lead / Shell Architect"]
E1A["Qt 6 + CMake monorepo"]
E1B["Generated REST client from openapi.v1.json"]
E1C["Auth state + token store"]
E1D["Design system / theme"]
E1E["Navigation router"]
E1F["Packaging Win/Mac/Linux<br/>signing, notarize, auto-update"]
E1G["CI matrix"]
end
subgraph E2["E2 — Pipeline Editor Specialist"]
E2A["Visual DAG editor<br/>QGraphicsView in QQuickWidget"]
E2B["Node library + connections"]
E2C["Schema-driven param panels"]
E2D["Undo / redo, save / version"]
end
subgraph E3["E3 — Liveview / Media"]
E3A["QtMultimedia HLS player<br/>(v1)"]
E3B["Multi-stream grid"]
E3C["Bbox overlay (QQuickItem + QSGGeometryNode)"]
E3D["Clip player (evidence)"]
E3E["WebRTC via GStreamer (v2)"]
end
subgraph E4["E4 — Config Forms"]
E4A["Schema-driven QML forms<br/>(from openapi.v1.json)"]
E4B["Camera + ONVIF discovery flow"]
E4C["Camera calibration UI<br/>(ROI / zones / homography)"]
E4D["Model + plugin + trigger forms"]
end
subgraph E5["E5 — Dashboard / Eventview"]
E5A["Target dashboard cards + charts"]
E5B["Event timeline (virtualized)"]
E5C["SSE subscriber + LiveBuffer"]
E5D["Filter / search / evidence viewer"]
E5E["Multi-target monitoring"]
end
subgraph E6["E6 — UX / Cross-platform QA"]
E6A["Design polish, a11y, i18n"]
E6B["Install / upgrade testing<br/>Win + Mac + Linux"]
E6C["Crash reporting (Sentry/Crashpad)"]
E6D["QML Profiler perf passes"]
E6E["User docs surface"]
end
CONTRACT[(openapi.v1.json<br/>frozen contract)]
E1 -.-> CONTRACT
E2 -.-> CONTRACT
E3 -.-> CONTRACT
E4 -.-> CONTRACT
E5 -.-> CONTRACT
```
**Critical-path order:**
1. **Week 1–2 (E1 only)**: monorepo, generated REST client, auth, design system stub, `make dev` brings up Prism mock. This unblocks everyone else.
2. **Week 1–2 (E2 in parallel)**: node-editor spike. Highest schedule risk → de-risk early.
3. **Week 2+ (E3, E4, E5 in parallel)**: each owns a vertical against the mock CL. E6 starts integration testing.
4. **Week 4+**: cut from mock CL to real ANSVIS `/api/v1` as adapter endpoints land.
---
## 7. What gates what (dependency chain)
```mermaid
graph LR
CONTRACT["openapi.v1.json<br/>(frozen)"]
MOCK["Prism mock server"]
ADAPT["ANSVIS /api/v1 adapter<br/>(AI coders, per-resource)"]
LEGACY["Legacy ANSVIS"]
UI_SHELL["L3 shell + REST client (E1)"]
UI_FEAT["L3 screens (E2-E6)"]
REAL["Real backend integration"]
SHIP["Ship v1"]
CONTRACT --> MOCK
CONTRACT --> ADAPT
ADAPT --> LEGACY
MOCK --> UI_SHELL --> UI_FEAT
UI_FEAT --> REAL
ADAPT --> REAL
REAL --> SHIP
```
- The contract is the *only* thing both sides block on.
- UI does not wait for the adapter — develops against Prism.
- Adapter team does not wait for UI — implements per-resource modules in parallel.
- Integration is the joining point.
---
## 8. Open decisions that affect this architecture
These are unchanged from earlier conversations; capturing here so the team has them in one place:
1. **Field naming** — `snake_case` (current default in `openapi.v1.json`) or `camelCase`?
2. **Auth scheme** — keep Bearer (current) or move to OAuth2/OIDC for v1?
3. **Mock-server pinning** — Prism for free, or build a richer custom mock?
4. **Liveview transport for v1** — HLS only (recommended), or RTSP-direct on LAN, or jump straight to WebRTC?
5. **Object store choice** — local FS (single host), MinIO (single host scalable), or S3 (cloud)?
6. **Auto-update** for the Qt client — Sparkle on Mac, Squirrel/MSIX on Windows, AppImage on Linux. Confirm budget for each.
7. **Contract repo** — co-located in `ANSCORE/contracts/` (current) or split into a separate `anscore-contracts` repo?
---
*Last updated: 2026-05-26 — diagrams reflect `openapi.v1.json` v1.0.0 draft.*
+573
View File
@@ -0,0 +1,573 @@
# PLAN — Port ANSCORE Licensing Core to Linux (x86_64 + Jetson aarch64)
**Status:** Draft, awaiting product-side decisions before execution
**Target platforms:** Ubuntu 22.04 x86_64, JetPack 6.x (Ubuntu 22.04 aarch64)
**Components in scope:** `core/anslicensing`, `core/ANSLicensingSystem`, `core/ANSLibsLoader`
**Estimated effort:** ~6–7 focused weeks for one engineer
**Risk:** Medium (HWID compatibility with license server is the dominant risk)
**Owner:** Tuan Nghia Nguyen
**Branch to create:** `linux-licensing-port`
---
## Decisions already made (do not re-litigate)
1. **Three components are in scope, in this order**: `ANSLibsLoader` (easiest), `anslicensing` (medium), `ANSLicensingSystem` (hardest). Engine modules (`ANSODEngine`, `ANSCV`, etc.) are NOT part of this plan — they come after licensing is up.
2. **No LabVIEW support on Linux.** The `_LV` exports and `LabVIEWHeader/extcode.h` includes are stripped from the Linux build via `#ifdef _WIN32`. The C++ and C-string (`_CS`) entry points become the only Linux surface.
3. **`lfreist/hwinfo` stays.** It is already vendored at `C:/ANSLibs/ANS-HWiNFO/` and is cross-platform (verified — its `platform.h` defines `HWINFO_UNIX` on Linux). All 8 `hwinfo::` call sites in `ANSLicense.cpp` are portable as-is. No replacement library needed.
4. **HWID gains real signals on Linux.** Today `hwid.cpp` Linux branches stub the SMBIOS UUID slot with `"4294967296"` and leave `GetHDDPropertyList` empty (2-point HWID strength vs Windows 4-point). We will fill those slots using `hwinfo::MainBoard::serialNumber()` and `hwinfo::RAM::serialNumber()` for parity.
5. **`rand.cpp` Linux path is replaced with `getrandom(2)`**. The current `srand(time(NULL)) + rand()` path is cryptographically broken and a security-correctness issue, not just a portability one.
6. **`DetectClockManipulation` on Linux ships as a best-effort stub.** Reading `journalctl` history is expensive and not always available (containers, no systemd). It will log a warning and return `false`. Acceptable anti-tamper degradation; flagged for follow-up.
7. **MediaClient and other non-licensing Windows surface stays Windows-only.** This plan does not touch them.
8. **HWiNFO is NOT the commercial HWiNFO64.** It's the open-source MIT-licensed [`lfreist/hwinfo`](https://github.com/lfreist/hwinfo) C++ library. No commercial license concern.
## Pre-work already completed (discovery, not code)
- ✅ Confirmed `core/anslicensing/` already has Linux scaffolding: `CMakeLists.txt` branches on `WIN32` and links `curl ssl crypto`; `precomp.h` has POSIX shims; `download.cpp` has a complete libcurl path; `hwid.cpp` has Linux CPU-ID and MAC code.
- ✅ Inventoried Windows API surface in `ANSLicensingSystem/ANSLicense.cpp` (4,021 LOC):
- Only 2 `#ifdef _WIN32` blocks — the file assumes Windows top-to-bottom
- 8 × `hwinfo::` calls — **portable as-is**
- 12 × PDH counter sites — need `/proc` rewrite (Phase 3.3)
- 7 × DXGI sites — need NVML/libdrm rewrite (Phase 3.4)
- 1 × WMI `MSAcpi_ThermalZoneTemperature` query — Linux `/sys/class/thermal/` (Phase 3.2)
- 1 × WinHTTP block — replace with libcurl already in `anslicensing/download.cpp` (Phase 3.5)
- 1 × `DnsQuery_A` — replace with `getaddrinfo` (Phase 3.6)
- Misc: `GlobalMemoryStatusEx`, `GetProcessMemoryInfo`, `GetSystemDirectoryW + GetDiskFreeSpaceExW`, `GetCurrentProcessId` (Phase 3.7)
- ✅ Confirmed `lfreist/hwinfo` cross-platform support via its `platform.h` and the header set at `C:/ANSLibs/ANS-HWiNFO/include/hwinfo/`.
---
## Open product decisions (BLOCK Phase 0)
These must be resolved with the licensing-backend team before any code is written. They determine the shape of Phase 1.
### Decision 1 — HWID format compatibility with the license server
The license server validates a HWID hash that's currently produced from Windows WMI data (`Win32_ComputerSystemProduct.UUID`, `Win32_NetworkAdapter.MACAddress`, `Win32_PhysicalMedia.SerialNumber`, `Win32_Processor.ProcessorId`). On Linux we will produce hashes from different data sources (SMBIOS via `/sys/class/dmi/id/`, NIC MACs via `getifaddrs`, RAM serial via DMI, CPU ID via `__get_cpuid`). The hash format (the bit-packed 125-bit `BitStream3` in `hwid.cpp`) stays identical, but the **values being hashed differ**. Three options:
- **A. Unified HWID, same algorithm**: server treats Windows and Linux licenses identically; admin has to physically activate each device once and the hash is opaque. **Simplest. Recommended.**
- **B. Separate Linux license pool**: server tracks platform field; Linux activations come from a dedicated key range. More work backend-side.
- **C. Backward-compat dance**: keep some Windows-specific fields empty on Linux (status quo) — license matching weakens. **Reject.**
**Owner to confirm with backend team before Phase 0.**
### Decision 2 — HWiNFO `MainBoard::serialNumber()` availability on Jetson
`lfreist/hwinfo` reads `/sys/class/dmi/id/board_serial` on Linux. **On Jetson this file may not exist** — ARM SBCs use device-tree, not DMI. We need to:
- Test on a real Jetson Orin/AGX board early.
- Define fallback signal: `/proc/device-tree/serial-number` (Jetson exposes this) or eMMC CID (`/sys/block/mmcblk*/device/cid`).
**Owner needs a Jetson devkit available before Phase 1.**
### Decision 3 — Boost dependencies on Jetson
`ANSLicensingSystem` currently links `libboost_log-vc143-mt-x64-1_82.lib` and uses `boost::property_tree::ptree` and `boost::date_time`. Ubuntu 22.04 ships Boost 1.74 by default (`libboost-all-dev`). Choices:
- **A. Use system Boost on Linux** (1.74). Accept minor API differences; pin via `find_package(Boost 1.74 REQUIRED)`.
- **B. Vendor Boost 1.82**. Painful build but exact parity.
- **C. Drop Boost.PropertyTree on Linux** and use `nlohmann/json` (already a project dep via `ANSLIBS_DIR/nlohmann`). Smaller dep surface but a code change in `ANSLicense.cpp` JSON output paths.
**Recommendation: A.** Defer until Phase 0.5 spike confirms it builds.
---
## Quick Start for a new session
If you're a fresh Claude session and the user has asked you to execute this plan:
1. **Read this entire document end-to-end** before editing anything. The per-file checklist in Appendix A is the source of truth for what changes.
2. **Confirm prerequisites are in place**:
```bash
ls "C:/ANSLibs/ANS-HWiNFO/include/hwinfo/platform.h" # cross-platform lib present
git status # clean working tree
git branch --show-current # should be linux-licensing-port
```
3. **Verify the three product decisions above are resolved.** Ask the user to paste the answers if they're not in this doc. Do NOT start Phase 1 without Decision 1 (HWID format) answered.
4. **Ask the user which phase to start with.** Default to Phase 0.1.
5. **Use TaskCreate** to track progress through the phases — the seven-phase structure (0 through 4 plus per-3.x sub-phases) is the natural top-level task list.
6. **Work in small commits.** One file, one logical change, verify build on Windows AND Linux before the next change. The Windows build must stay green at every step.
7. **Never modify a `*_LV` function signature.** Even on Linux where we don't compile it, the source still exists on the Windows-shared file. Touch only what's gated by `#ifdef _WIN32`.
### How to invoke this plan
The user can paste any of these to start:
> "Read `docs/PLAN_Linux_Licensing_Port.md` and walk me through Phase 0."
> "Continue the Linux licensing port. Read `docs/PLAN_Linux_Licensing_Port.md` and ask what's done."
> "Execute Phase 1.1 from `docs/PLAN_Linux_Licensing_Port.md`."
### Context the new session might need that isn't in this plan
- **Build system on Linux**: CMake 3.28+, Ninja, GCC 11+ on Ubuntu 22.04. Cross-compile to Jetson via JetPack 6.x SDK Manager + sysroot.
- **CUDA on Linux**: CUDA 12.x, cuDNN, TensorRT 10.x from `apt` or NVIDIA's repo.
- **Licensing server endpoint**: ask the user for the staging URL — it is NOT in source control.
- **Test hardware**: x86 Ubuntu box AND a Jetson Orin Nano/AGX devkit are both required. Do not declare Phase 1 done from x86 testing alone.
---
## 1. Background
ANSCORE targets Linux (Jetson + industrial x86) per the product roadmap. The Flutter mobile app will be replaced by a Qt QML UI talking to ANSCORE over gRPC + RTSP. Licensing is the blocking dependency: **no module can be loaded without `ANSLicensingSystem` validating the license**, so it must come first.
The three components are layered:
```
┌──────────────────────────────┐
│ Module DLLs (ANSCV, ANSOD,…) │ ← consumers, NOT part of this plan
├──────────────────────────────┤
│ ANSLicensingSystem (façade) │ ← Phase 3 (4–5 weeks)
├──────────────────────────────┤
│ anslicensing (SDK) │ ← Phase 1 (2–3 weeks)
│ ANSLibsLoader (dynload) │ ← Phase 2 (1 week)
└──────────────────────────────┘
```
`ANSLicensingSystem` depends on `anslicensing`; `ANSLibsLoader` is independent of both but consumed by `ANSCV` later. Phase ordering keeps the build green at every step.
## 2. Goals
- All three components build on Ubuntu 22.04 x86_64 and JetPack 6.x aarch64.
- `libANSLicensingSystem.so` exports the same C++ symbols and C ABI as the Windows DLL (less LabVIEW exports).
- Linux HWID is a 4-signal hash, equivalent strength to Windows.
- License validation works against the existing license server.
- All Windows behavior preserved unchanged. Zero behavioral regressions on Windows builds.
- `rand.cpp` on Linux uses `getrandom(2)` instead of insecure `rand()`.
## 3. Non-goals
- LabVIEW support on Linux.
- macOS port (separate effort).
- `DetectClockManipulation` feature parity on Linux (acceptable degradation, logged).
- Re-architecting the licensing protocol (wire compat preserved).
- Engine modules (`ANSODEngine`, `ANSCV`, etc.) — separate plans.
- Build infra changes (Conan/vcpkg adoption, etc.).
## 4. Phases
### Phase 0 — Windows-side prep (1 week, zero behavioral change)
Done on Windows. No Linux changes yet. Each step must keep the Windows build green and tests passing.
- **0.1** Add `ANSCORE_API` visibility macro to all three components' public headers:
```cpp
#if defined(_WIN32)
#if defined(ANSCORE_EXPORTS)
#define ANSCORE_API __declspec(dllexport)
#else
#define ANSCORE_API __declspec(dllimport)
#endif
#elif defined(__GNUC__)
#define ANSCORE_API __attribute__((visibility("default")))
#else
#define ANSCORE_API
#endif
```
Replace `__declspec(dllexport)` / `__declspec(dllimport)` site-by-site. Test on Windows that the resulting DLL exports the same symbol set (`dumpbin /exports`).
- **0.2** Split LabVIEW exports out of `ANSLicense.h` into a new `ANSLicense_LV.h` that includes the LabVIEW header and declares only the `_LV` functions. Update Windows-side `ANSLicense.cpp` to include `ANSLicense_LV.h`. **Result: `ANSLicense.h` has zero LabVIEW types**, so Linux consumers don't drag in `extcode.h`.
- **0.3** Strip `<windows.h>`, `<winhttp.h>`, etc. from public headers (`ANSLIB.h:6 #include <windows.h>` line is the worst offender). Use forward declarations or guard with `#ifdef _WIN32` where needed in implementation files only.
- **0.4** Vendor `lfreist/hwinfo` as a git submodule at `3rdparty/hwinfo` (or confirm `C:/ANSLibs/ANS-HWiNFO/` is the canonical copy and update CMake to point at it consistently). Add a top-level CMake option `ANSCORE_USE_VENDORED_HWINFO` that defaults to ON.
- **0.5** Stand up an Ubuntu 22.04 build environment (CI runner or local VM). Install: `build-essential cmake ninja-build libboost-all-dev libcurl4-openssl-dev libssl-dev libzip-dev zlib1g-dev libnvidia-ml-dev libdrm-dev libsensors4-dev`. Verify CUDA 12.x + TensorRT 10.x are reachable. Smoke test: `cmake -B build && ninja -C build core/anslicensing` should currently fail — but it should fail with **predictable errors** (missing hwinfo Linux build, etc.), not config errors.
**Acceptance for Phase 0**:
- Windows build green, all existing tests pass.
- `dumpbin /exports ANSLicensingSystem.dll` symbol set is byte-identical to baseline.
- `ANSLicense.h` does not include `LabVIEWHeader/extcode.h`.
- Ubuntu environment is reproducible.
- Decision 1, 2, 3 above are answered in writing.
### Phase 1 — Port `core/anslicensing` (2–3 weeks)
This is the lowest-level SDK. Already 80% portable.
- **1.1** **Strengthen Linux HWID** in `hwid.cpp`:
- Replace the `propList.push_back("4294967296")` stub at line 254 with `hwinfo::MainBoard board; propList.push_back(board.serialNumber());`. Falls back to `/proc/device-tree/serial-number` on aarch64 if board_serial is missing.
- Implement `GetHDDPropertyList` on Linux at line 142: enumerate block devices via `/sys/block/*/device/wwid` or `/sys/block/*/device/serial`. Filter to non-removable (skip `/sys/block/*/removable == 1`).
- Add the same source to the `MatchCurrentHardwareId` Linux branches (lines 416, 538).
- Cross-test: HWID produced on Ubuntu must validate on the same machine across reboots.
- **1.2** **Replace `rand.cpp` Linux RNG** with `getrandom(2)`:
```cpp
#include <sys/random.h>
// ::srand(time(NULL)); ← DELETE
// rnd = ::rand(); ← DELETE
ssize_t n = getrandom(&rnd, sizeof(rnd), 0);
if (n != sizeof(rnd)) {
// fallback: open /dev/urandom directly
}
```
Keep `random::random()` constructor cheap (no kernel call); make `random::rand()` the syscall site. Document the change in a header comment.
- **1.3** **`DetectClockManipulation` Linux stub** in `helper.cpp` — return `false` with `ANS_DBG("ClockTamper", "skipped on Linux — no-op fallback")`. Document as a known degradation.
- **1.4** **Wire CMake on Linux**. The CMakeLists already has the Linux branch; verify:
- `find_package(CURL REQUIRED)` resolves
- `target_link_libraries(... curl ssl crypto)` links cleanly
- Add `find_package(hwinfo REQUIRED)` or use the submodule
- **1.5** **Round-trip test**: a small test binary that calls `HardwareId::GetCurrentHardwareId()`, persists the string, then calls `HardwareId::MatchCurrentHardwareId(saved)` and asserts true. Run on Ubuntu x86 AND a Jetson devkit.
**Acceptance for Phase 1**:
- `libanslicensing.so` builds on Ubuntu 22.04 x86_64 and Jetson aarch64.
- Round-trip HWID test passes on both targets across at least 3 reboots.
- `strings libanslicensing.so | grep -c '4294967296'` returns 0.
- Static analysis (cppcheck or clang-tidy) shows no new warnings on Linux build.
### Phase 2 — Port `core/ANSLibsLoader` (1 week)
Mechanical port from `LoadLibrary`/`GetProcAddress` to `dlopen`/`dlsym`. Independent of Phase 1 and Phase 3 — can be done in parallel.
- **2.1** Port `DynLibUtils.cpp` core helpers:
```cpp
#ifdef _WIN32
HMODULE LoadLib(const char* p) { return LoadLibraryA(p); }
void* GetSym(HMODULE h, const char* s) { return GetProcAddress(h, s); }
void UnloadLib(HMODULE h) { FreeLibrary(h); }
#else
void* LoadLib(const char* p) { return dlopen(p, RTLD_NOW | RTLD_LOCAL); }
void* GetSym(void* h, const char* s) { return dlsym(h, s); }
void UnloadLib(void* h) { dlclose(h); }
#endif
```
Wrap the handle type behind an opaque `DynLibHandle` typedef so callers don't see the difference.
- **2.2** Update `CvLoader.cpp`, `NvDynLoader.cpp`, `OvLoader.cpp`, `EPLoader.cpp` to use the cross-platform helpers. Most logic is search-path discovery — that needs branching too:
- Windows: `C:/ANSLibs/...` paths
- Linux: `/opt/ANSLibs/...`, `/usr/lib/x86_64-linux-gnu/`, `/usr/local/cuda/lib64/`, and `${LD_LIBRARY_PATH}`
- **2.3** Naming convention: Windows `.dll`, Linux `.so` with `lib` prefix. Centralize in a `PLATFORM_DLL_NAME("opencv_world")` macro that expands to `"opencv_world.dll"` on Windows and `"libopencv_world.so"` on Linux.
- **2.4** Symbol visibility: Linux `.so` exports everything by default unless built with `-fvisibility=hidden`. Apply `-fvisibility=hidden` globally in the top-level CMakeLists and use the `ANSCORE_API` macro to opt in to exports.
- **2.5** Smoke test: `ANSLibsLoader` opens `libopencv_world.so` on Ubuntu and returns valid handles for a known set of OpenCV symbols.
**Acceptance for Phase 2**:
- `libANSLibsLoader.so` builds and runs.
- All four sub-loaders (CV/OV/NV/EP) load their respective libraries successfully on Linux.
- No symbol-visibility regressions on Windows (verify with `dumpbin /exports`).
### Phase 3 — Port `core/ANSLicensingSystem` (4–5 weeks)
The big one. Split into independently mergeable PRs.
#### 3.1 — Misc Win32 cleanup (1 day)
- `GlobalMemoryStatusEx` → read `/proc/meminfo` MemTotal / MemAvailable
- `GetProcessMemoryInfo` → read `/proc/self/status` VmRSS, VmSwap, VmPeak
- `GetSystemDirectoryW + GetDiskFreeSpaceExW` (line 1056-1064) → `statvfs("/")`
- `GetCurrentProcessId` → `getpid()`
- `WideCharToMultiByte` / `WideToUtf8` → `std::wstring_convert` or just use `std::string` throughout the Linux path (no wide chars needed without WMI)
#### 3.2 — CPU thermal (1 day)
Replace `ReadCpuTempCelsiusViaWmi()` (line 603) with a Linux reader:
```cpp
double ReadCpuTempCelsiusLinux() {
// Scan /sys/class/thermal/thermal_zone*/type for "x86_pkg_temp" (x86)
// or "CPU-therm" / "thermal-fan-est" (Jetson)
// Read corresponding /sys/class/thermal/thermal_zone*/temp (milli-Celsius)
// Return max across all CPU zones, or -1 if none found
}
```
Keep the Windows function intact behind `#ifdef _WIN32`. Dispatch through a single `ReadCpuTempCelsius()` wrapper.
#### 3.3 — System health sampler / PDH rewrite (1 week)
The `HealthSampler` class (lines 906–1089) is the largest single port. Build a Linux `HealthSamplerLinux` class with the same public API as the Windows one. Then dispatch at instantiation:
```cpp
#ifdef _WIN32
using HealthSampler = HealthSamplerWin;
#else
using HealthSampler = HealthSamplerLinux;
#endif
```
PDH counter → Linux source mapping:
| PDH counter | Linux source |
|---|---|
| `\Processor Information(_Total)\% Processor Time` | `/proc/stat` delta of (user+nice+system) vs idle |
| `\Processor Information(_Total)\% Processor Performance` | `/sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq` / base freq |
| `\Processor Information(_Total)\% Processor Utility` | Same as Processor Time on Linux (no MS-specific util concept) |
| `\PhysicalDisk(_Total)\% Disk Time` | `/proc/diskstats` field 13 (io_time ms / interval ms) |
| `\PhysicalDisk(_Total)\Disk Read Bytes/sec` | `/proc/diskstats` field 6 × 512 / interval |
| `\PhysicalDisk(_Total)\Disk Write Bytes/sec` | `/proc/diskstats` field 10 × 512 / interval |
| `\Network Interface(*)\Bytes Received/Sent/sec` | `/proc/net/dev` rx_bytes / tx_bytes deltas |
| `\Network Interface(*)\Current Bandwidth` | `/sys/class/net/*/speed` (Mbit/s) |
| `\GPU Engine(*)\Utilization Percentage` | NVML `nvmlDeviceGetUtilizationRates` |
| `\GPU Process Memory(*)\Dedicated Usage` | NVML `nvmlDeviceGetMemoryInfo` |
The sampler keeps a `prev_*` snapshot for delta computation (no PDH built-in differencing on Linux). Sample interval: same 2-second cadence as Windows.
Acceptance: the populated `ANSCENTER::SystemHealthInfo` struct has all the same fields filled on Linux as on Windows (allowing -1 / empty for fields that genuinely don't exist).
#### 3.4 — GPU adapter enumeration (1 week)
Replace `EnumerateGpuAdapters` (line 721) with NVML on systems with NVIDIA drivers, falling back to libdrm:
```cpp
std::vector<GpuAdapter> EnumerateGpuAdaptersLinux() {
std::vector<GpuAdapter> out;
// Try NVML first (covers Jetson + dGPU x86)
if (nvmlInit_v2() == NVML_SUCCESS) {
unsigned int count = 0;
nvmlDeviceGetCount_v2(&count);
for (unsigned int i = 0; i < count; ++i) {
nvmlDevice_t dev;
nvmlDeviceGetHandleByIndex_v2(i, &dev);
GpuAdapter a;
// populate a.name, a.vendorId=0x10DE, a.dedicatedVramMB...
out.push_back(a);
}
nvmlShutdown();
}
// Then enumerate non-NVIDIA via libdrm (/dev/dri/card*)
// for Intel/AMD users on x86 Linux
// ...
return out;
}
```
`AggregateGpuCounter` (line 773) — the LUID-based attribution machinery — collapses on Linux because NVML hands you per-device handles directly. Replace with a simpler `for (auto& dev : nvmlDevices)` loop.
Caveat: `nvmlDeviceGetUtilizationRates` returns whole-device utilization, not per-process. Per-process GPU memory needs `nvmlDeviceGetProcessUtilization` (where supported) or `nvmlDeviceGetComputeRunningProcesses`. On Jetson, NVML support is partial — test early.
#### 3.5 — License-server HTTP (1 day)
Delete the `WinHttpHandle` block (lines 1101–1166) on Linux. Reuse `anslicensing/download.cpp`'s `UrlDownloadToString` libcurl path. The function signature is already compatible. One small wrinkle: `UrlDownloadToString` is in `anslicensing.dll`, so `ANSLicensingSystem` must link against it — it already does.
#### 3.6 — DNS public-IP probe (0.5 day)
Replace `DnsQuery_A` (line 1228) with `getaddrinfo`:
```cpp
struct addrinfo hints{}, *res = nullptr;
hints.ai_family = AF_INET;
if (getaddrinfo("myip.opendns.com", nullptr, &hints, &res) == 0 && res) {
char buf[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &((sockaddr_in*)res->ai_addr)->sin_addr, buf, sizeof(buf));
publicIp = buf;
freeaddrinfo(res);
}
```
If the existing Windows query uses a TXT record specifically, fall back to `libresolv res_query` for parity. Verify with the user which probe is in use.
#### 3.7 — CMake + linking (3 days)
Rewrite `ANSLicensingSystem/CMakeLists.txt` Linux branch:
```cmake
if(WIN32)
target_link_libraries(ANSLicensingSystem PRIVATE
HWiNFO.lib OpenCL.lib OpenCLExt.lib
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 dnsapi.lib
${WIN_COMMON_LIBS}
)
else()
find_package(Boost REQUIRED COMPONENTS log date_time)
find_package(CURL REQUIRED)
target_link_libraries(ANSLicensingSystem PRIVATE
hwinfo::hwinfo # vendored or apt
Boost::log Boost::date_time
CURL::libcurl
zip # libzip-dev
z # zlib1g-dev
nvidia-ml # libnvidia-ml-dev (NVML)
drm # libdrm-dev (optional, for non-NVIDIA)
pthread dl
)
endif()
target_compile_definitions(ANSLicensingSystem PRIVATE
ANSLICENSE_EXPORTS
ANSLICENSINGSYSTEM_EXPORTS
$<$<PLATFORM_ID:Windows>:UNICODE _UNICODE>
)
```
Drop `ANSLIBS_DIR/ANS-HWiNFO/lib` and `ANSLIBS_DIR/boost/lib` from `target_link_directories` on Linux.
#### 3.8 — End-to-end license activation (3 days)
Run the full activation flow from a Linux client against the staging license server:
1. `GenerateHardwareId()` returns a string
2. Operator pastes it into the activation portal
3. Server returns license blob
4. Linux client `ValidateLicense(blob)` returns `SUCCESS`
5. Subsequent `IsLicenseValid()` calls return true
6. Restart process — license still valid
7. Move drive to second machine — license **invalid** (HWID mismatch detected)
The last item is the critical anti-piracy check. If it fails (license validates on second machine), the HWID is too lax — go back to Phase 1.1.
**Acceptance for Phase 3 overall**:
- `libANSLicensingSystem.so` builds and links cleanly.
- All public symbols (less `_LV` set) match Windows-side exports.
- Full activation round-trip succeeds against staging server.
- `SystemHealthInfo` struct populates all fields with real (non -1) data on Jetson and on x86 Ubuntu.
- Anti-piracy test passes (HWID mismatch is detected).
### Phase 4 — Integration & Hardening (1 week)
- **4.1** **Cross-arch CI**: add an `aarch64` build matrix entry. Cross-compile from x86 host using JetPack 6.x sysroot, or natively on a Jetson runner. Both options work; pick whichever the team prefers.
- **4.2** **Soak test**: run an ANSLicensingSystem-based test harness on a Jetson for 24 hours. Watch for memory leaks (`valgrind --leak-check=full` for a short run, then `top` / smaps tracking long-term). NVML on Jetson has known leak patterns — defend against them.
- **4.3** **Documentation**: write `docs/LINUX_BUILD_GUIDE.md` covering apt deps, Boost version notes, Jetson cross-compile, and the HWID format change (so support knows licenses don't transfer Windows↔Linux without re-issuance).
- **4.4** **Top-level CMake gating**: in the root `CMakeLists.txt`, remove `if(WIN32)` gates around the three components — they now build everywhere. Engine modules stay Windows-only for now since this plan doesn't cover them.
**Acceptance for Phase 4**:
- CI green on Windows, Ubuntu x86, Jetson aarch64.
- 24h soak shows stable memory profile.
- LINUX_BUILD_GUIDE.md merged and reviewed.
---
## 5. Risks & mitigations
| Risk | Likelihood | Impact | Mitigation |
|---|---|---|---|
| License server rejects Linux HWID format | Medium | **Blocks Phase 1 acceptance** | Resolve Decision 1 before Phase 0; coordinate with backend team early |
| Jetson `/sys/class/dmi/id/board_serial` missing | High | Phase 1 HWID is weak on Jetson | Decision 2: validate on real Jetson hardware; fall back to device-tree serial |
| NVML on Jetson missing some APIs | Medium | GPU telemetry sparse on Jetson | Test `nvmlDeviceGetUtilizationRates` early; gracefully degrade fields that aren't available |
| Boost version mismatch (1.74 system vs 1.82 vendored) | Medium | Subtle behavioral differences | Phase 0.5 spike build; switch to nlohmann/json if PropertyTree breaks |
| `lfreist/hwinfo` ARM support gaps | Low | HWID source incomplete on Jetson | Read its `cpuid.h` Linux branch; have device-tree fallback ready |
| Symbol visibility differences cause ABI breakage | Low | Linker errors at integration time | `-fvisibility=hidden` from day one; `ANSCORE_API` discipline |
| WMI thermal-zone has no Linux semantic equivalent | Low | `cpuTempDegC` field is less accurate | Document field as "best-effort, may be -1 on hardware without thermal zones" |
| `libnvidia-ml` not present on dev-only x86 box | Low | Build fails on developer machines | dynamic-load NVML via dlopen with weak symbol; fallback to "no GPU" state |
---
## 6. Dependencies for Ubuntu / Jetson development
```bash
# Build essentials
sudo apt install -y \
build-essential cmake ninja-build pkg-config git \
libboost-all-dev libcurl4-openssl-dev libssl-dev \
libzip-dev zlib1g-dev \
libnvidia-ml-dev libdrm-dev libsensors4-dev \
libgtest-dev cppcheck clang-tidy
# CUDA 12.x + cuDNN + TensorRT 10.x — from NVIDIA's apt repo (separate setup)
# JetPack 6.x — flash via SDK Manager onto target board
```
For Jetson cross-compile: install JetPack 6.x SDK on host, point CMake at `/opt/nvidia/jetson-linux-sysroot/aarch64-ubuntu22.04`.
---
## 7. Testing strategy
- **Unit tests** (`tests/anslicensing-unittest`): exercise each platform-specific helper with known synthetic data. New tests: HWID-determinism, getrandom-fallback, /proc/meminfo parser, /proc/stat delta computation.
- **Integration tests** (`tests/anslicensingsystem-integration`): in-process activation round-trip against a mock license server. Run on Windows + Linux + Jetson.
- **E2E tests**: real staging server. Run **once per phase acceptance**, not on every commit (slow + flaky over real network).
- **Anti-piracy regression test**: documented in Phase 3.8. **Must run before merging Phase 3 to main.**
- **Determinism test**: generate HWID 10× across reboots on the same machine; assert all identical. Generate on two physically different Jetsons; assert different.
---
## Appendix A — Per-file change checklist
Files marked **NEW** are created; **MOD** is modified; **DELETE** is removed.
### `core/anslicensing/`
| File | Action | Phase | Change |
|---|---|---|---|
| `hwid.cpp` | MOD | 1.1 | Linux branches: use `hwinfo::MainBoard::serialNumber()` for UUID slot; implement `GetHDDPropertyList` reading `/sys/block/*/device/wwid` |
| `rand.cpp` | MOD | 1.2 | Replace `srand`/`rand` with `getrandom(2)` + `/dev/urandom` fallback |
| `helper.cpp` | MOD | 1.3 | `DetectClockManipulation` Linux stub with ANS_DBG warning |
| `download.cpp` | — | — | Already done — libcurl path exists |
| `precomp.h` | — | — | Already done — POSIX shims exist |
| `CMakeLists.txt` | MOD | 1.4 | Add `find_package(hwinfo)` on non-Windows |
### `core/ANSLicensingSystem/`
| File | Action | Phase | Change |
|---|---|---|---|
| `ANSLicense.h` | MOD | 0.2 | Remove `#include "LabVIEWHeader/extcode.h"`; remove `LDbl`/`LDblPtr`/`LDblHandle` (moved to `ANSLicense_LV.h`) |
| `ANSLicense_LV.h` | NEW | 0.2 | Windows-only header with all `_LV` exports, includes `extcode.h` |
| `ANSLicense.cpp` | MOD | 3.1–3.6 | Replace each Win32 call site with `#ifdef _WIN32` / `#else` branch |
| `HealthSamplerLinux.cpp` | NEW | 3.3 | New file: `/proc`-based sampler |
| `HealthSamplerLinux.h` | NEW | 3.3 | Class declaration matching `HealthSampler`'s public API |
| `Utility.h` | MOD | 0.3 | Strip any Windows-specific types from declarations |
| `Utility.cpp` | MOD | 3.1 | Verify std::filesystem path operations work on Linux |
| `CMakeLists.txt` | MOD | 3.7 | Add Linux branch with curl/boost/hwinfo/nvml |
### `core/ANSLibsLoader/`
| File | Action | Phase | Change |
|---|---|---|---|
| `DynLibUtils.cpp` | MOD | 2.1 | dlopen/dlsym/dlclose Linux branches |
| `CvLoader.cpp` | MOD | 2.2 | Linux search paths + `.so` naming |
| `NvDynLoader.cpp` | MOD | 2.2 | Linux search paths |
| `OvLoader.cpp` | MOD | 2.2 | Linux search paths |
| `EPLoader.cpp` | MOD | 2.2 | Linux search paths |
| `ANSLibsLoader.def` | — | — | Windows-only `.def`; leave alone, use `__attribute__((visibility))` on Linux |
| `CMakeLists.txt` | MOD | 2.4 | `-fvisibility=hidden`; link `dl` on Linux |
### Top-level
| File | Action | Phase | Change |
|---|---|---|---|
| `CMakeLists.txt` | MOD | 0.4, 4.4 | `ANSCORE_USE_VENDORED_HWINFO` option; ungate three core components from `if(WIN32)` |
| `cmake/Dependencies.cmake` | MOD | 0.4, 3.7 | Add `hwinfo`, `NVML` (`CUDA::nvml`), `libdrm` resolution |
| `3rdparty/hwinfo/` | NEW (submodule) | 0.4 | `git submodule add https://github.com/lfreist/hwinfo 3rdparty/hwinfo` |
| `docs/LINUX_BUILD_GUIDE.md` | NEW | 4.3 | Developer onboarding |
---
## Appendix B — Symbol map (Win32 → Linux)
Reference table for the engineer porting `ANSLicense.cpp`:
| Win32 symbol / API | File:line | Linux replacement |
|---|---|---|
| `PdhOpenQueryW` / `PdhAddEnglishCounterW` / `PdhCollectQueryData` | ANSLicense.cpp:906–928 | Custom `/proc/stat`, `/proc/diskstats`, `/proc/net/dev` delta sampler (Phase 3.3) |
| `IWbemLocator::ConnectServer("ROOT\\WMI")` + `MSAcpi_ThermalZoneTemperature` | ANSLicense.cpp:618–663 | `/sys/class/thermal/thermal_zone*/temp` (Phase 3.2) |
| `CreateDXGIFactory1` / `IDXGIAdapter1::GetDesc1` | ANSLicense.cpp:721–747 | `nvmlDeviceGetCount_v2` + per-device queries; libdrm for non-NVIDIA (Phase 3.4) |
| `WinHttpOpen` / `WinHttpSendRequest` / etc. | ANSLicense.cpp:1101–1166 | Reuse `anslicensing/download.cpp` libcurl path (Phase 3.5) |
| `DnsQuery_A` | ANSLicense.cpp:1228 | `getaddrinfo` or `res_query` (Phase 3.6) |
| `GlobalMemoryStatusEx` | ANSLicense.cpp:1036 | `/proc/meminfo` parser (Phase 3.1) |
| `GetProcessMemoryInfo` | ANSLicense.cpp:1048 | `/proc/self/status` parser (Phase 3.1) |
| `GetSystemDirectoryW` + `GetDiskFreeSpaceExW` | ANSLicense.cpp:1056–1064 | `statvfs("/")` (Phase 3.1) |
| `GetCurrentProcessId` | ANSLicense.cpp:993, 1026 | `getpid()` (Phase 3.1) |
| `WideCharToMultiByte` / `WideToUtf8` | ANSLicense.cpp:712 | drop on Linux — no wide chars needed without WMI |
| `CoInitializeEx` / `CoSetProxyBlanket` / `CoUninitialize` | ANSLicense.cpp:604, 611, 669 | delete on Linux — COM not needed |
| `__declspec(dllexport)` | (everywhere) | `__attribute__((visibility("default")))` via `ANSCORE_API` macro (Phase 0.1) |
| `LStrHandle`, `LDbl`, `extcode.h` | ANSLicense.h:75 + many | move to `ANSLicense_LV.h`, Windows-only (Phase 0.2) |
---
## Appendix C — License-server coordination checklist
Before Phase 1 starts, the license-server team confirms in writing:
- [ ] Server accepts HWID format generated by the new Linux `hwid.cpp` path
- [ ] Server's HWID validation algorithm matches the new client-side hash inputs
- [ ] Decision on license-pool separation: unified vs. Linux-only pool
- [ ] Staging environment endpoint available for E2E testing in Phase 3.8
- [ ] Activation portal supports pasting a Linux-generated HWID
---
## Appendix D — What this plan deliberately does NOT cover
So a future session doesn't accidentally scope-creep:
- **MediaClient port to Linux** — separate plan; ANSCV depends on this but licensing does not
- **GStreamer backend for ANSCV** — separate plan
- **Engine modules** (ANSODEngine, ANSFR, ANSLPR, ANSOCR, etc.) — separate plans, post-licensing
- **gRPC service layer** (ANSService) — planned after engine modules are up
- **Qt QML UI** — separate repo, depends on gRPC layer
- **macOS support** — not in roadmap currently
- **Dropping LabVIEW on Windows** — `_LV` exports stay on Windows builds untouched
- **License protocol changes** — wire compatibility preserved; only the input data to the HWID hash changes on Linux
---
*End of plan.*
+48
View File
@@ -0,0 +1,48 @@
@startuml 01-system-context
title ANSCORE — System Context (L1 / L2 / L3)
skinparam componentStyle rectangle
skinparam packageStyle rectangle
package "Layer 3 — Qt/QML Desktop UI\n(Windows / macOS / Linux)" as L3 {
component "Qt/QML application" as UI
}
package "Layer 2 — ANSVIS Communication Layer (CL)" as L2 {
component "/api/v1\nREST surface" as APIV1
component "Legacy ANSVIS core\n(reused as-is)" as LEGACY
component "Media relay\nHLS / RTSP / WebRTC" as RELAY
component "SSE event\nbroadcaster" as SSEHUB
database "Event DB\n+ clip object store" as STORE
queue "Async job queue" as JOBS
component "Auth + RBAC" as AUTH
}
package "Layer 1 — ANSCORE Edge Runtime\n(one per target)" as L1 {
component "Camera + Inference\n+ Triggers" as EDGE
}
UI --> APIV1 : (1) REST / HTTPS\ncontrol + CRUD
UI --> SSEHUB : (2) SSE / HTTPS\nlive event tail
UI --> RELAY : (3) HLS / RTSP / WebRTC\nlive video
APIV1 --> AUTH
APIV1 --> LEGACY
APIV1 --> JOBS
APIV1 --> STORE
LEGACY --> STORE
STORE --> SSEHUB
RELAY <--> EDGE
LEGACY <--> EDGE
note bottom of L3
L3 only talks to L2 — never directly to edges.
end note
note bottom of L2
All three UI transports are brokered by the REST API:
the URL + token for live media and the SSE filter for
events are obtained via REST calls first.
end note
@enduml
+84
View File
@@ -0,0 +1,84 @@
@startuml 02-l2-internals
title Layer 2 — ANSVIS internal architecture
skinparam componentStyle rectangle
skinparam packageStyle rectangle
actor "Qt / QML UI" as QT
actor "3rd-party\nintegrations" as EXT
package "L2 /api/v1 surface" as EDGE_API {
component "REST router" as ROUTER
component "Auth middleware\n(Bearer)" as AUTHMW
component "Request validation\n(OpenAPI schemas)" as VALID
component "Error mapper\n(RFC 7807)" as ERR
component "Rate limiter" as RATE
component "SSE broadcaster\n(per-subscription filter)" as SSEHUB
}
package "v1 Adapter Layer\n(per-resource modules)" as ADAPTER {
component "cameras" as CAM_AD
component "models" as MOD_AD
component "edges" as EDGE_AD
component "deployment-groups" as DG_AD
component "ai-tasks" as TASK_AD
component "events" as EV_AD
component "fr" as FR_AD
component "alpr" as ALPR_AD
component "notifications" as NOTIF_AD
component "system" as SYS_AD
component "jobs" as JOB_AD
}
package "Legacy ANSVIS server\n(reused as-is)" as LEGACY {
component "Config store" as LEG_CFG
component "AI task engine" as LEG_TASKS
component "FR engine" as LEG_FR
component "ALPR engine" as LEG_ALPR
}
package "Infrastructure" as INFRA {
component "Auth + token store" as AUTH_SVC
queue "Job queue\n(scans, uploads, deploys, updates)" as JOBS_Q
database "Event metadata DB" as EVENTS_DB
database "Clip / image object store\n(S3 / MinIO / FS)" as CLIPS
component "Logs + metrics + health" as LOGS
component "Media relay\nHLS / RTSP / WebRTC" as RELAY
}
actor "L1 edge target(s)" as E1
QT --> ROUTER
EXT --> ROUTER
ROUTER --> AUTHMW
AUTHMW --> VALID
VALID --> RATE
RATE --> ADAPTER
ADAPTER --> ERR
ADAPTER ..> ROUTER : response
AUTHMW --> AUTH_SVC
CAM_AD --> LEG_CFG
MOD_AD --> LEG_CFG
MOD_AD --> CLIPS
EDGE_AD --> LEG_CFG
DG_AD --> LEG_TASKS
TASK_AD --> LEG_TASKS
EV_AD --> EVENTS_DB
EV_AD --> CLIPS
FR_AD --> LEG_FR
ALPR_AD --> LEG_ALPR
SYS_AD --> LOGS
JOB_AD --> JOBS_Q
CAM_AD ..> JOBS_Q : scan / probe
MOD_AD ..> JOBS_Q : large upload
LEG_TASKS --> EVENTS_DB
EVENTS_DB --> SSEHUB
SSEHUB --> ROUTER
LEGACY <--> E1
RELAY <--> E1
@enduml
+51
View File
@@ -0,0 +1,51 @@
@startuml 03-l2-ownership
title Layer 2 — Build ownership
skinparam componentStyle rectangle
skinparam packageStyle rectangle
package "Architect-owned\n(lead review)" as HUMAN #LightYellow {
component "REST router\n+ auth middleware" as H1
component "RFC 7807\nerror mapper" as H2
component "OpenAPI validation\npipeline" as H3
component "Auth flow\n+ token store" as H4
component "RBAC\n+ security review" as H5
component "L2 ↔ L1 tunnel\n+ signing" as H6
}
package "Core team\n(modular adapters)" as AI #LightBlue {
component "cameras adapter" as A1
component "models adapter\n+ upload pipeline" as A2
component "edges adapter" as A3
component "deployment-groups\nadapter" as A4
component "ai-tasks adapter" as A5
component "events adapter\n+ SSE broadcaster" as A6
component "fr adapter" as A7
component "alpr adapter" as A8
component "notifications adapter" as A9
component "system / health /\nlogs adapter" as A10
component "job queue + workers" as A11
component "media relay" as A12
}
package "Existing ANSVIS\n(reused as-is)" as EXIST #LightGray {
component "Task engine" as L_TASKS
component "FR engine" as L_FR
component "ALPR engine" as L_ALPR
component "Config store" as L_CFG
}
HUMAN --> AI
AI --> EXIST
note bottom of HUMAN
Cross-cutting concerns kept under direct architect review:
request validation, errors, auth, RBAC, tunnel security.
end note
note bottom of AI
Highly modular by resource — the core team works in parallel
on independent adapters without colliding.
end note
@enduml
+65
View File
@@ -0,0 +1,65 @@
@startuml 04-l3-internals
title Layer 3 — Qt 6 / QML internal architecture
skinparam componentStyle rectangle
skinparam packageStyle rectangle
package "Qt 6 + QML — Layer 3" as QT {
package "App Shell" as SHELL {
component "Main window" as MAIN
component "Navigation router" as ROUTE
component "Design system / theme" as THEME
component "i18n (qsTr / Linguist)" as I18N
component "Auth state + token store" as AUTH_ST
component "Error toast / dialog" as ERR_UI
}
package "Network layer" as NET {
component "Generated REST client\n(openapi.v1.json → C++/Qt)" as REST
component "SSE subscriber\n(QNetworkReply chunked)" as SSE_CL
component "Media client\nQtMultimedia / GStreamer" as MEDIA
component "Bbox overlay renderer\n(custom QQuickItem)" as OVERLAY
component "Multipart + presigned\nuploader" as UPLOAD
}
package "Domain state (QObject + signals)" as STATE {
component "CamerasModel" as ST_CAM
component "ModelsModel" as ST_MOD
component "EdgesModel" as ST_EDGE
component "DeploymentGroupsModel" as ST_DG
component "AITasksModel" as ST_TASK
component "EventsModel + LiveBuffer" as ST_EV
component "FRPersonsModel" as ST_FR
component "ALPRPlatesModel" as ST_ALPR
component "RecipientsModel" as ST_NOTIF
component "SystemHealthModel" as ST_SYS
}
package "Screens (QML)" as SCREENS {
component "Login" as LOGIN
component "Dashboard /\ntargets overview" as DASH
component "Camera management\n+ ONVIF discovery\n+ calibration" as CAM_S
component "Pipeline editor\n(visual DAG)" as DG_EDIT
component "AI Task config" as TASK_EDIT
component "Rules / triggers" as RULE_S
component "Liveview grid" as LIVE
component "Eventview timeline" as EV_S
component "FR persons + faces" as FR_S
component "ALPR plates" as ALPR_S
component "Model registry" as MODEL_S
component "Users / admin" as USERS
component "Settings" as SETTINGS
}
}
actor "ANSVIS /api/v1" as BACKEND
SCREENS --> STATE : reads / mutates
STATE --> NET : calls
NET <--> BACKEND
OVERLAY ..> MEDIA : sync to PTS
AUTH_ST ..> REST : injects Bearer
AUTH_ST ..> SSE_CL : injects Bearer
@enduml
@@ -0,0 +1,45 @@
@startuml 05-seq-configure-deploy
title Sequence — Operator configures camera + AI task and deploys
actor "Operator" as User
participant "Qt UI" as UI
participant "ANSVIS\n/api/v1" as API
participant "ANSVIS core" as LEG
participant "L1 edge" as EDGE
User -> UI : Open Cameras screen
UI -> API : GET /cameras?page=1
API -> LEG : legacy list
LEG --> API : cameras
API --> UI : 200 {items, page, total}
User -> UI : Add RTSP camera (form)
UI -> API : POST /cameras
note right of UI
body has nested credentials,
capturing, image_size —
no stringified JSON
end note
API -> LEG : legacy add
LEG --> API : id=123
API --> UI : 201 Location: /cameras/123
User -> UI : Configure AI task
UI -> API : POST /ai-tasks\n{camera_id:123, ...}
API -> LEG : legacy create
LEG --> API : id=456
API --> UI : 201 {id:456}
User -> UI : Start task
UI -> API : POST /ai-tasks/456/start
API -> LEG : dispatch
LEG -> EDGE : config + start
EDGE --> LEG : running
API --> UI : 202 {state:starting}
loop status poll (or SSE later)
UI -> API : GET /ai-tasks/456/status
API --> UI : 200 {state:running, fps:14.2}
end
@enduml
+36
View File
@@ -0,0 +1,36 @@
@startuml 06-seq-live-events
title Sequence — Live event tail + clicking into evidence
actor "Operator" as User
participant "Qt UI" as UI
participant "ANSVIS\n/api/v1" as API
participant "SSE broadcaster" as SSE
participant "Clip store\n(S3 / MinIO / FS)" as CLIPS
participant "L1 edge" as EDGE
User -> UI : Open Eventview (task=456)
UI -> API : GET /events?task_id=456&page=1
API --> UI : 200 historical events
UI -> API : GET /events/stream?task_id=456
API --> UI : text/event-stream (open)
EDGE -> API : event arrives (via L1↔L2)
API -> SSE : push EventRecord
SSE --> UI : data: {id:E1, ts, object:car, bbox, ...}
User -> UI : Click event E1
UI -> API : GET /events/E1
API --> UI : 200 EventRecord (full)
UI -> API : GET /events/E1/image?width=640
API --> UI : image/jpeg bytes
User -> UI : Play clip
UI -> API : GET /events/E1/clip
API -> CLIPS : presign GET
CLIPS --> API : presigned URL
API --> UI : 200 {url, expires_at}
UI -> CLIPS : GET <presigned URL>
CLIPS --> UI : MP4 bytes
UI -> UI : play in QtMultimedia
@enduml
+30
View File
@@ -0,0 +1,30 @@
@startuml 07-seq-liveview
title Sequence — Liveview with bbox overlay
actor "Operator" as User
participant "Qt UI" as UI
participant "ANSVIS\n/api/v1" as API
participant "Media relay" as RELAY
participant "L1 edge" as EDGE
participant "SSE broadcaster" as SSE
User -> UI : Open Liveview (cam 123, task 456)
UI -> API : GET /cameras/123/liveview?transport=hls
API -> RELAY : ensure HLS for cam 123
RELAY -> EDGE : pull RTSP
RELAY --> API : hls_url + short-lived token
API --> UI : 200 {url, transport:hls,\nauth_token, expires_at}
UI -> RELAY : GET hls_url (token)
RELAY --> UI : m3u8 + segments
UI -> UI : Render video\n(QtMultimedia)
UI -> API : GET /events/stream?task_id=456 (SSE)
API --> UI : text/event-stream
loop per event
SSE --> UI : bbox + ts
UI -> UI : overlay synced to PTS
end
@enduml
+27
View File
@@ -0,0 +1,27 @@
@startuml 08-seq-async-scan
title Sequence — Async camera network scan (job pattern)
actor "Operator" as User
participant "Qt UI" as UI
participant "ANSVIS\n/api/v1" as API
participant "Job queue" as JOBS
participant "ONVIF scanner" as SCAN
User -> UI : Scan 192.168.1.0/24
UI -> API : POST /cameras/scan
API -> JOBS : enqueue
JOBS --> API : job_id=j1
API --> UI : 202 {job_id:j1,\n status:queued,\n links:{self:/jobs/j1}}
JOBS -> SCAN : run
loop poll until terminal
UI -> API : GET /jobs/j1
API --> UI : 200 {status:running, progress:0.4}
end
SCAN --> JOBS : results
UI -> API : GET /jobs/j1
API --> UI : 200 {status:succeeded,\n result:{discovered:[...]}}
@enduml
+23
View File
@@ -0,0 +1,23 @@
@startuml 09-seq-large-upload
title Sequence — Large model upload (initiate / complete pattern)
actor "Operator" as User
participant "Qt UI" as UI
participant "ANSVIS\n/api/v1" as API
participant "Object store\n(S3 / MinIO / FS)" as OBJ
User -> UI : Pick model.zip (500 MB)
UI -> API : POST /models:initiate-upload\n{filename, size_bytes, checksum}
API -> OBJ : presign PUT
OBJ --> API : presigned_url
API --> UI : 200 {upload_id, presigned_url, expires_at}
UI -> OBJ : PUT <presigned_url>\n(binary stream, with progress)
OBJ --> UI : 200 OK
UI -> API : POST /models:complete-upload\n{upload_id, metadata}
API -> OBJ : verify checksum
OBJ --> API : ok
API --> UI : 201 Model
@enduml
+51
View File
@@ -0,0 +1,51 @@
@startuml 10-deployment-topology
title Deployment Topology
skinparam componentStyle rectangle
node "Customer site / cloud" {
node "Operator workstations" {
component "Windows desktop\nQt UI" as D1
component "macOS desktop\nQt UI" as D2
component "Linux desktop\nQt UI" as D3
}
node "ANSVIS server host" {
component "Reverse proxy\nTLS terminator\n(nginx / caddy)" as NGINX
component "ANSVIS app\n(adapter + legacy core)" as APP
component "Media relay" as RELAY
database "Object store\n(S3 / MinIO / FS)" as OBJ
database "Config + Event DBs" as DB
}
node "Edge targets" {
node "Linux x86 + NVIDIA" as E1
node "Windows x86 + NVIDIA" as E2
node "Jetson Orin" as E3
}
}
D1 --> NGINX : TLS 443\nREST + SSE + HLS
D2 --> NGINX : TLS 443
D3 --> NGINX : TLS 443
NGINX --> APP
NGINX --> RELAY
APP --> DB
APP --> OBJ
RELAY <--> E1
RELAY <--> E2
RELAY <--> E3
APP <--> E1
APP <--> E2
APP <--> E3
note bottom
Network rules for the customer:
- Outbound TCP 443 from each workstation → ANSVIS host
- Inbound TCP 443 on ANSVIS host
- L2 ↔ L1 protocol per edge (port + tunnel TBD)
- Workstations never reach edges directly — Media relay proxies
end note
@enduml
@@ -0,0 +1,71 @@
@startuml 11-l3-engineer-ownership
title Layer 3 — Engineer ownership (6 engineers)
skinparam componentStyle rectangle
skinparam packageStyle rectangle
package "E1 — UI Lead / Shell Architect" as E1 #LightYellow {
component "Qt 6 + CMake monorepo" as E1A
component "Generated REST client\nfrom openapi.v1.json" as E1B
component "Auth state + token store" as E1C
component "Design system / theme" as E1D
component "Navigation router" as E1E
component "Packaging Win/Mac/Linux\nsigning, notarize,\nauto-update" as E1F
component "CI matrix" as E1G
}
package "E2 — Pipeline Editor Specialist" as E2 #LightBlue {
component "Visual DAG editor\n(QGraphicsView in QQuickWidget)" as E2A
component "Node library +\nconnection validation" as E2B
component "Schema-driven param panels" as E2C
component "Undo / redo,\nsave / version" as E2D
}
package "E3 — Liveview / Media" as E3 #LightGreen {
component "QtMultimedia HLS player\n(v1)" as E3A
component "Multi-stream grid" as E3B
component "Bbox overlay\n(QQuickItem + QSGGeometryNode)" as E3C
component "Clip player (evidence)" as E3D
component "WebRTC via GStreamer\n(v2)" as E3E
}
package "E4 — Config Forms" as E4 #LightPink {
component "Schema-driven QML forms\n(from openapi.v1.json)" as E4A
component "Camera + ONVIF discovery flow" as E4B
component "Camera calibration UI\n(ROI / zones / homography)" as E4C
component "Model + plugin +\ntrigger forms" as E4D
}
package "E5 — Dashboard / Eventview" as E5 #LightCoral {
component "Target dashboard\ncards + charts" as E5A
component "Event timeline\n(virtualized)" as E5B
component "SSE subscriber + LiveBuffer" as E5C
component "Filter / search /\nevidence viewer" as E5D
component "Multi-target monitoring" as E5E
}
package "E6 — UX / Cross-platform QA" as E6 #Lavender {
component "Design polish,\na11y, i18n" as E6A
component "Install / upgrade testing\nWin + Mac + Linux" as E6B
component "Crash reporting\n(Sentry / Crashpad)" as E6C
component "QML Profiler perf passes" as E6D
component "User docs surface" as E6E
}
artifact "openapi.v1.json\n(frozen contract)" as CONTRACT
E1 ..> CONTRACT
E2 ..> CONTRACT
E3 ..> CONTRACT
E4 ..> CONTRACT
E5 ..> CONTRACT
note bottom of E1
Week 1–2: E1 alone delivers the shell that unblocks E2–E6.
end note
note bottom of E2
Week 1–2 spike in parallel — de-risks the hardest UI feature.
end note
@enduml
+43
View File
@@ -0,0 +1,43 @@
@startuml 12-dependency-chain
title Dependency chain — what gates what
skinparam componentStyle rectangle
artifact "openapi.v1.json\n(frozen)" as CONTRACT
component "Prism mock server" as MOCK
component "ANSVIS /api/v1 adapter\n(core team, per-resource)" as ADAPT
component "Legacy ANSVIS" as LEGACY
component "L3 shell + REST client\n(E1)" as UI_SHELL
component "L3 screens\n(E2 – E6)" as UI_FEAT
component "Real backend integration" as REAL
component "Ship v1" as SHIP
CONTRACT --> MOCK
CONTRACT --> ADAPT
ADAPT --> LEGACY
MOCK --> UI_SHELL
UI_SHELL --> UI_FEAT
UI_FEAT --> REAL
ADAPT --> REAL
REAL --> SHIP
note bottom of CONTRACT
Only artifact that BOTH sides block on.
Frozen once published; additive changes only.
end note
note bottom of MOCK
UI develops here from day 1.
end note
note bottom of ADAPT
Built in parallel by the core team.
Per-resource modularity = independent progress.
end note
note bottom of REAL
Joining point. UI tests run against both mock and real
to detect drift.
end note
@enduml
+19
View File
@@ -0,0 +1,19 @@
# ANSCORE — L2 & L3 diagrams (preview)
Click each PNG link to view the rendered diagram in your browser.
SVG links are higher-quality (vector). Both come from plantuml.com.
| # | Diagram | What it shows | PNG | SVG |
|---|---|---|---|---|
| 01 | **system context** | L1 / L2 / L3 with the 3 transports (REST / SSE / HLS-RTSP-WebRTC). | [PNG](https://www.plantuml.com/plantuml/png/RLJRRjf047ttLupo51HH2dchgYe6h43bD8alYIhnihO7ic6zw-wkQL1LgH_HB-oNT7ODWf2y6E_ud3ENiyELiKpRjXRmSTWtMsEnxfTAMdonWULM88JVqibj4i6_FtyXzG2OT00uZuSmW7Za7fUz831hBXkcMGsbgXibKThKRab3OscPN0ayWZIiNBCLdWA2NHpEOhP53PS-yRqTtD_4C4Mpjgg1VBwGvuzSLkgdeUGrAszJLmINxKll39YXUk1N08Swu6mdmPf6y99PhgG7vlFWzvka8v-KEd-Ofp2vIJl9qQdaW3Lyi1akP1Ab6PXMBrc97XlUpH-6hy4nhbYvtOkMIYFreB4rM16ZpyskyEWwd7n_JRt1YZEQaM3RXPp5hjKaI-_evn6B99juOXB5uGalJIF03RqkPA4Lgqf6zccFfa-pV4pmYbbMC4EjHmu9q_52Na0fU0EgU2PRm5YgjIDbT168ywF5bWYXsSeIdbNH1Jpaw-quVLr4sDedk81a77PrXdasUsVam_t8_MMBAlgIjDBoses9fA0X4DtL5ThTe8QdTanOZPfHhhbSeaPPecibqtorGcqyAPfUHovvFeT-_qjd4to2ys4Fl8S3c6NPNRgGRWUq4YGsIVBf7jzDpH56FNSuu0NVO3ThAfABFS6Rul2NFNZ7k1rlmojKGT1Lu-joCpeSTtVY4F1JFXmxNphKKS0_Z-eEkce-kuWVn8vn20HIMOH2MQjgK4lQ8femhPAIOajzYRK1gzmYEAyajQkXucvttMS5IAwP3m7A2fpGMxaHoOL2W7tIYBHwO3MJfb7Q6c2Qe5gjoRaA2j9xmiuJwlCJyTmvJs9on198mb9fyDEh_NOmIkeWpfKb5tJF7O9utfHENXNaZIJv3H6yTic4C8JNnXxNVKMlz9yO_0S0) | [SVG](https://www.plantuml.com/plantuml/svg/RLJRRjf047ttLupo51HH2dchgYe6h43bD8alYIhnihO7ic6zw-wkQL1LgH_HB-oNT7ODWf2y6E_ud3ENiyELiKpRjXRmSTWtMsEnxfTAMdonWULM88JVqibj4i6_FtyXzG2OT00uZuSmW7Za7fUz831hBXkcMGsbgXibKThKRab3OscPN0ayWZIiNBCLdWA2NHpEOhP53PS-yRqTtD_4C4Mpjgg1VBwGvuzSLkgdeUGrAszJLmINxKll39YXUk1N08Swu6mdmPf6y99PhgG7vlFWzvka8v-KEd-Ofp2vIJl9qQdaW3Lyi1akP1Ab6PXMBrc97XlUpH-6hy4nhbYvtOkMIYFreB4rM16ZpyskyEWwd7n_JRt1YZEQaM3RXPp5hjKaI-_evn6B99juOXB5uGalJIF03RqkPA4Lgqf6zccFfa-pV4pmYbbMC4EjHmu9q_52Na0fU0EgU2PRm5YgjIDbT168ywF5bWYXsSeIdbNH1Jpaw-quVLr4sDedk81a77PrXdasUsVam_t8_MMBAlgIjDBoses9fA0X4DtL5ThTe8QdTanOZPfHhhbSeaPPecibqtorGcqyAPfUHovvFeT-_qjd4to2ys4Fl8S3c6NPNRgGRWUq4YGsIVBf7jzDpH56FNSuu0NVO3ThAfABFS6Rul2NFNZ7k1rlmojKGT1Lu-joCpeSTtVY4F1JFXmxNphKKS0_Z-eEkce-kuWVn8vn20HIMOH2MQjgK4lQ8femhPAIOajzYRK1gzmYEAyajQkXucvttMS5IAwP3m7A2fpGMxaHoOL2W7tIYBHwO3MJfb7Q6c2Qe5gjoRaA2j9xmiuJwlCJyTmvJs9on198mb9fyDEh_NOmIkeWpfKb5tJF7O9utfHENXNaZIJv3H6yTic4C8JNnXxNVKMlz9yO_0S0) |
| 02 | **l2 internals** | L2 adapter modules in front of legacy ANSVIS + infrastructure. | [PNG](https://www.plantuml.com/plantuml/png/TLPVRzis47_NfpZagSFcfaaVEWp3KDcM7NLoR4jAkW0535ewosmaIYEf34PHO1zYdt2VP4TIScmPUp6fkzyTx-yl-QWqaxgjIdXtEoflHrneb8ALojDSbmWHEw24M_Ztxt_0lqyUmmHw232PxRd6JBSIFKyzSz4moIh8wggf1GgTw0Eva8HWeYZn1DAmx9aLE0HuBDErXAkrXcjOBo9u2A-0ALYdlUQzp4VaGX--21D78PdcjL0M5VoHUbxd6QwYMxXc3Rz-kG7LoXtBq86cys3ZhqBuvi5hf70L1qaAicufDukBbmzf49zZ_5RleU9vNk9VJE8NyMQCTCeVh8N_aDujFW-yufyjAWqlhEIv3PMCbWqA4u7AzbWnvQmV_IYSdXi7KbBA5MiQb6GMpoRmuUTt7nm-Y0V1nKmZbBpYnmpyD3Y794a0MrcpF6DAMv_aUQJQhSeaRqnqiEEbxXCY-Dt3sFj-KbGgff-pXY1kCZeN4bNToWoXglEsn2ubV-glg8R3IcUiGibSooR-OkCFqYOVM3hrOZczK6DUe3ffvK2TOrFMXuhkeuBQsJZeT7u1P7oacNfs-jHFVhzywOMExgd72-tELNaMNtek6wVpezMbLjIQxtXsChVtopISNU3KWNfKkKOy9HVgh_NMMNzQZetoj4iH5YmxzFkgKBxOJabi5UPaC-9TXw9Wxa-UXWsQr6B72r2qSDZ3DfFPVB0C8PZw0Og2YrUSgMHoZfp5Gz1ik5XKg276oCwo2iMEnaRBrXAEnONtizWVXc-tz4VGzJEAaoJCUcwInmc1QITRSlYfthehrKPbJAYVeANXOJbTt1XP2UrjFz5KwsIp9WyaOrkco4bWXWGgrCp8O3hkvYMuJvFDT7o6dPIy8MBZbScdtdubtdC1KW39Uz8ik0YNTCuIzzua2bU3OaPreIWxUb3opDpso4gzTsLRpWVe1UQS4S6MZ0ZpBah8TvmcApe-upPE9uueWiX_CgNkE3Qw0RDar5vPe7xJvHxSUDuwXT7ejvuRYMzFFzrX9HqLkiDA7BtPNsTZoCdyEBnZ2gzd32Cp1DT_lttRlmA_K3QAqbFqr-B4_x6xdgCKA-pdrj78_ubSaJioEGDPrZXAt6HtJ36GMdvmOHyx_ofpBrYcEDhH2dJqS1JPYNUxxcIcdMwzxNStVLs2fYPEGZKnuqjDRMIznJwtCq1fUjcDj-ST0xy8kRjPUSV_xZZjjDVnng_M-iQp0tJyyZwYoEa_2ky_) | [SVG](https://www.plantuml.com/plantuml/svg/TLPVRzis47_NfpZagSFcfaaVEWp3KDcM7NLoR4jAkW0535ewosmaIYEf34PHO1zYdt2VP4TIScmPUp6fkzyTx-yl-QWqaxgjIdXtEoflHrneb8ALojDSbmWHEw24M_Ztxt_0lqyUmmHw232PxRd6JBSIFKyzSz4moIh8wggf1GgTw0Eva8HWeYZn1DAmx9aLE0HuBDErXAkrXcjOBo9u2A-0ALYdlUQzp4VaGX--21D78PdcjL0M5VoHUbxd6QwYMxXc3Rz-kG7LoXtBq86cys3ZhqBuvi5hf70L1qaAicufDukBbmzf49zZ_5RleU9vNk9VJE8NyMQCTCeVh8N_aDujFW-yufyjAWqlhEIv3PMCbWqA4u7AzbWnvQmV_IYSdXi7KbBA5MiQb6GMpoRmuUTt7nm-Y0V1nKmZbBpYnmpyD3Y794a0MrcpF6DAMv_aUQJQhSeaRqnqiEEbxXCY-Dt3sFj-KbGgff-pXY1kCZeN4bNToWoXglEsn2ubV-glg8R3IcUiGibSooR-OkCFqYOVM3hrOZczK6DUe3ffvK2TOrFMXuhkeuBQsJZeT7u1P7oacNfs-jHFVhzywOMExgd72-tELNaMNtek6wVpezMbLjIQxtXsChVtopISNU3KWNfKkKOy9HVgh_NMMNzQZetoj4iH5YmxzFkgKBxOJabi5UPaC-9TXw9Wxa-UXWsQr6B72r2qSDZ3DfFPVB0C8PZw0Og2YrUSgMHoZfp5Gz1ik5XKg276oCwo2iMEnaRBrXAEnONtizWVXc-tz4VGzJEAaoJCUcwInmc1QITRSlYfthehrKPbJAYVeANXOJbTt1XP2UrjFz5KwsIp9WyaOrkco4bWXWGgrCp8O3hkvYMuJvFDT7o6dPIy8MBZbScdtdubtdC1KW39Uz8ik0YNTCuIzzua2bU3OaPreIWxUb3opDpso4gzTsLRpWVe1UQS4S6MZ0ZpBah8TvmcApe-upPE9uueWiX_CgNkE3Qw0RDar5vPe7xJvHxSUDuwXT7ejvuRYMzFFzrX9HqLkiDA7BtPNsTZoCdyEBnZ2gzd32Cp1DT_lttRlmA_K3QAqbFqr-B4_x6xdgCKA-pdrj78_ubSaJioEGDPrZXAt6HtJ36GMdvmOHyx_ofpBrYcEDhH2dJqS1JPYNUxxcIcdMwzxNStVLs2fYPEGZKnuqjDRMIznJwtCq1fUjcDj-ST0xy8kRjPUSV_xZZjjDVnng_M-iQp0tJyyZwYoEa_2ky_) |
| 03 | **l2 ownership** | Human-owned vs AI-coder-built vs reused legacy modules in L2. | [PNG](https://www.plantuml.com/plantuml/png/PLNRRXCn47tVhnYL5r2xj0tG5XvGjo5f8qAfkWM15AborfEDYTTUR6_32Y7nn1Vq2_aIPYzfO_CKQtNE-FZCcScfTSougf1my2AMWrYl5Hgx56NaX9C8KrQZWG7y_NqFPvMG71uHaLq9LJB32iXqKMg5ogMk9fR1p36LIzo2b2nRiHn3GDH_XvsBgc2g5S1dwYapsL8uWZtR0MRXujE7v1AUJ4M-T5zHIhs6dn4ytWixrwFq1eok79gPsWLMkIKKWdE9QsQmAt8OKCP3E3uvE9xHYumsKB2oHDD11pxqOuagkPh07PE2CoUqcgbIb2Y5wck_y0b9S_-2T3PYd5wX0klqHiZBGCXPCcnm5hFA25UJFtS2rntsbO-TKZF-tCFq45ob5CgM9t8bLDxXZw9VMwucapZJ74qyf-uvChRGl9BC0EEi9AzivsyowSqzanM6rcQiG4EWdj8H0Y-fBCe7I2EgAgLc73oNai1Mv3a6TGCVELAPkg1pd5DtIpjJ7ZnmaedOCRiAQWOMuXtzU5hJT0HpGt8pPXzOHpvhOVoYny7DiWm09pv0QISM8cl34-XxxIDjJI8As8SbCaansfyfgVFG_mEVz4tFuNk5rBrTM6kpejRsmB1Jo0MZX4bMzu21bvZH3s4TfGcIo_Jp9AN86AmiSiB6ee_BwCk4fgrBpBbXTPYO6-e1eCetVP_UtYJf-zG7ZQzzoFWw6A7fLO1ellYOeLOBaM-DrlHsE3vltjDjZ3X-IzcEADxDgPKTHTG9XBbsJXUW5zrkeRBlQ2CXPSp2N50yt1A1tcrWXLXQg1HD4N3HhAv-FjyGoI2PRjtMOjY3TflOlNO7xK5cjBLnLhdMrCs8FuzGSMYK_AU7z09SaBMoXisqpfkTO7LbCcntCJse7MiB6LDjlq4eQ3Qjb2Y9hnLzeE6XMngZDjCEQ-6Mj2I9BKamATeMSaf7-Y-8_W40) | [SVG](https://www.plantuml.com/plantuml/svg/PLNRRXCn47tVhnYL5r2xj0tG5XvGjo5f8qAfkWM15AborfEDYTTUR6_32Y7nn1Vq2_aIPYzfO_CKQtNE-FZCcScfTSougf1my2AMWrYl5Hgx56NaX9C8KrQZWG7y_NqFPvMG71uHaLq9LJB32iXqKMg5ogMk9fR1p36LIzo2b2nRiHn3GDH_XvsBgc2g5S1dwYapsL8uWZtR0MRXujE7v1AUJ4M-T5zHIhs6dn4ytWixrwFq1eok79gPsWLMkIKKWdE9QsQmAt8OKCP3E3uvE9xHYumsKB2oHDD11pxqOuagkPh07PE2CoUqcgbIb2Y5wck_y0b9S_-2T3PYd5wX0klqHiZBGCXPCcnm5hFA25UJFtS2rntsbO-TKZF-tCFq45ob5CgM9t8bLDxXZw9VMwucapZJ74qyf-uvChRGl9BC0EEi9AzivsyowSqzanM6rcQiG4EWdj8H0Y-fBCe7I2EgAgLc73oNai1Mv3a6TGCVELAPkg1pd5DtIpjJ7ZnmaedOCRiAQWOMuXtzU5hJT0HpGt8pPXzOHpvhOVoYny7DiWm09pv0QISM8cl34-XxxIDjJI8As8SbCaansfyfgVFG_mEVz4tFuNk5rBrTM6kpejRsmB1Jo0MZX4bMzu21bvZH3s4TfGcIo_Jp9AN86AmiSiB6ee_BwCk4fgrBpBbXTPYO6-e1eCetVP_UtYJf-zG7ZQzzoFWw6A7fLO1ellYOeLOBaM-DrlHsE3vltjDjZ3X-IzcEADxDgPKTHTG9XBbsJXUW5zrkeRBlQ2CXPSp2N50yt1A1tcrWXLXQg1HD4N3HhAv-FjyGoI2PRjtMOjY3TflOlNO7xK5cjBLnLhdMrCs8FuzGSMYK_AU7z09SaBMoXisqpfkTO7LbCcntCJse7MiB6LDjlq4eQ3Qjb2Y9hnLzeE6XMngZDjCEQ-6Mj2I9BKamATeMSaf7-Y-8_W40) |
| 04 | **l3 internals** | Qt/QML layers: app shell, network, domain state, screens. | [PNG](https://www.plantuml.com/plantuml/png/RPRTQkD65CVlzHI7NAM4n0rRoXBAieebEsBbBqasB0J2M3gHPosDr9cHiwOKUjK7A7t2VPAU6IcXzYGtiSU_cVFrdx_qMMacTLTNyFClrzM7QouqIi4gvMckAuIO7L721_Znrx-mql0hZ60rY-4L0oRp7TUOwqwYvwaz5osJh8QygTj6eD2fFj8naWWcoWh_XxGitxCIpm5lM8SB6-_AnZFXXrGkW2bOPV27vm6yiNxRGhh3gh8_fmzX71C1zFUM2LpC61VmmaNHl5XivaVpSsRE3hnacZS2PDDHbPPC5kiiF4S3LBmKe8vAOqsDqJkiqUBPGpXpS7xxKS3bxogJnCPSb1rN-YVBHxSVdKJyJk-0fgEHUg2RFL8atSW-WB_E7fxIx7nFA6KZ2MPAKuo2iwefBHycoTCw8lpFawxDKRyqSW-LQQq5vs7czcsA0YKbKa0IfXda5QVbHt7Pj2XOosyEjpVV53Ninz__mFZgQhGQoZBq-Lbf6eBgjYgNV8kI3baDIIJOLaV8TvtOOz7l9_Pf73jJH2hiBOcLddMLvhLT7C4qrH9PFLGp2uF8Fzz_lss-GtD0ILMJxaI1qYQITzJV6bQhZkVxY4RQ9x7OX4diVtMoC559o9gcqqgh12mUHTTM3Ik6yEjblF03f-j1KniXzhEzN2sstqZyT8mvWsxUK7lcPw4xYh6fZQbPK-0WzUnfxC-SxCplfzHi4JXoAKeyXS9W-exCgQPZJP-dT2FQqmt1rD5jb36rFwKoF_tY13_GlnwZscD-mFlk-Nded4bbSxvZaYnHaivEpvuaJWRnCbbMrDnJq2oVemdclELlYRoYyqKMJHpbsdl-WApIknCw_PewCqvpYIWKZNSM3mCT9s4uJzsHnat9XKNYnTGrf82ftRPXie3Heo2pBb4hAzy3nzx60ZzzU5ydK3D1wTJsfbp1Ohw99cGBAZVxZsOfPnNVIkjtzYpIqbDwVjYIjrXnWK2NZ2o8xig1guxSF_2dVN71bAGJEPVTZy28WPR4C--To4ZXNJRfAbJ6HIKlIvfpxo3hE7JpCM8nrKCfUT5tBjeukhK2ivYn1vD_RuKRzy190cqlBPBYCyknZpv9NDIe25ghhjw9wRjBzRAMM9B5osFlHOiWZ5rohIWmbSsAUb31EWqJ1qjHQtfaz37JCCkY-RHN7UcEvJGLIcsURg8KHcJ9eyEj9U_zyPTm7dZUg_gkhpyDrd97wR72XAuxuqJAwzSDOPu2TqORz1vWFlzc5b_F6WmHRcu-zGPBg3gAd9uxiCnIRtWssT_jy-AENXUCnL4woCXhJuZUurt6-qpEJEybtdy0) | [SVG](https://www.plantuml.com/plantuml/svg/RPRTQkD65CVlzHI7NAM4n0rRoXBAieebEsBbBqasB0J2M3gHPosDr9cHiwOKUjK7A7t2VPAU6IcXzYGtiSU_cVFrdx_qMMacTLTNyFClrzM7QouqIi4gvMckAuIO7L721_Znrx-mql0hZ60rY-4L0oRp7TUOwqwYvwaz5osJh8QygTj6eD2fFj8naWWcoWh_XxGitxCIpm5lM8SB6-_AnZFXXrGkW2bOPV27vm6yiNxRGhh3gh8_fmzX71C1zFUM2LpC61VmmaNHl5XivaVpSsRE3hnacZS2PDDHbPPC5kiiF4S3LBmKe8vAOqsDqJkiqUBPGpXpS7xxKS3bxogJnCPSb1rN-YVBHxSVdKJyJk-0fgEHUg2RFL8atSW-WB_E7fxIx7nFA6KZ2MPAKuo2iwefBHycoTCw8lpFawxDKRyqSW-LQQq5vs7czcsA0YKbKa0IfXda5QVbHt7Pj2XOosyEjpVV53Ninz__mFZgQhGQoZBq-Lbf6eBgjYgNV8kI3baDIIJOLaV8TvtOOz7l9_Pf73jJH2hiBOcLddMLvhLT7C4qrH9PFLGp2uF8Fzz_lss-GtD0ILMJxaI1qYQITzJV6bQhZkVxY4RQ9x7OX4diVtMoC559o9gcqqgh12mUHTTM3Ik6yEjblF03f-j1KniXzhEzN2sstqZyT8mvWsxUK7lcPw4xYh6fZQbPK-0WzUnfxC-SxCplfzHi4JXoAKeyXS9W-exCgQPZJP-dT2FQqmt1rD5jb36rFwKoF_tY13_GlnwZscD-mFlk-Nded4bbSxvZaYnHaivEpvuaJWRnCbbMrDnJq2oVemdclELlYRoYyqKMJHpbsdl-WApIknCw_PewCqvpYIWKZNSM3mCT9s4uJzsHnat9XKNYnTGrf82ftRPXie3Heo2pBb4hAzy3nzx60ZzzU5ydK3D1wTJsfbp1Ohw99cGBAZVxZsOfPnNVIkjtzYpIqbDwVjYIjrXnWK2NZ2o8xig1guxSF_2dVN71bAGJEPVTZy28WPR4C--To4ZXNJRfAbJ6HIKlIvfpxo3hE7JpCM8nrKCfUT5tBjeukhK2ivYn1vD_RuKRzy190cqlBPBYCyknZpv9NDIe25ghhjw9wRjBzRAMM9B5osFlHOiWZ5rohIWmbSsAUb31EWqJ1qjHQtfaz37JCCkY-RHN7UcEvJGLIcsURg8KHcJ9eyEj9U_zyPTm7dZUg_gkhpyDrd97wR72XAuxuqJAwzSDOPu2TqORz1vWFlzc5b_F6WmHRcu-zGPBg3gAd9uxiCnIRtWssT_jy-AENXUCnL4woCXhJuZUurt6-qpEJEybtdy0) |
| 05 | **seq configure deploy** | Operator workflow: add camera → AI task → deploy → start. | [PNG](https://www.plantuml.com/plantuml/png/XLJHRjem57ttLrpnr6e98QpT0rAxeWuX9bHOq-wfKkN59bWpTchVJA88QH-nBzoNz3efJ0IalUJ1vvnxp_6zper7xh1QQUXTnbw-nBar2rLKJiP2bjgk6IhK4ZBvKacJI_Zx-m_CIkauMWTxieUSh-WGFi1m0iZzJ-16G5F2CyRpGE_iX1tW7XwzTAoaxYfN9JS8dMy8ZvC6cnmYmxli-oHxCWalLV8hhJd3-KaIcNAo9an7uqF2D0KfYWOSVHcF60iU8BwcVZ08iGpSrZay-Dn9QHW115ChmiUZ1qYQdFvpoGjvbRA0485Q4K7BWkThqCeZ2oVnNlgkQkYxVlrU3pOAvSf74Cf5W1QvthPi3OM0-uTilhlYiuLrg_C3P_DPziyQCnObE5Ki4UmYN2N03olMiAHOHdgK0YYSa0OLrpuYEESbLawP8WAr8YVFNhtMeoRCMF0OCBLGfFoQpUwOfDc6BYVZSo5QwPMuIliVs-5JcDgSex9ciFUU1DvX_DlTZkqswqHohkA0-2UpQGezAp6WKX5qkztjIPTq0vm271czkFnqR7H3vGXeJoOBR-T_dX8I9lKhE_GXb2yvvil6mdMza65JwhZqZXf9VHhlHQuoXaRHTjY73T5H3ceH4SYfjhOCHR3oK5gjuOnUO9QDG1FHdTDaZtQxRPcaH3lUsQRNkvS85gKVf1VT_ZRi1MCtzAKV2di3) | [SVG](https://www.plantuml.com/plantuml/svg/XLJHRjem57ttLrpnr6e98QpT0rAxeWuX9bHOq-wfKkN59bWpTchVJA88QH-nBzoNz3efJ0IalUJ1vvnxp_6zper7xh1QQUXTnbw-nBar2rLKJiP2bjgk6IhK4ZBvKacJI_Zx-m_CIkauMWTxieUSh-WGFi1m0iZzJ-16G5F2CyRpGE_iX1tW7XwzTAoaxYfN9JS8dMy8ZvC6cnmYmxli-oHxCWalLV8hhJd3-KaIcNAo9an7uqF2D0KfYWOSVHcF60iU8BwcVZ08iGpSrZay-Dn9QHW115ChmiUZ1qYQdFvpoGjvbRA0485Q4K7BWkThqCeZ2oVnNlgkQkYxVlrU3pOAvSf74Cf5W1QvthPi3OM0-uTilhlYiuLrg_C3P_DPziyQCnObE5Ki4UmYN2N03olMiAHOHdgK0YYSa0OLrpuYEESbLawP8WAr8YVFNhtMeoRCMF0OCBLGfFoQpUwOfDc6BYVZSo5QwPMuIliVs-5JcDgSex9ciFUU1DvX_DlTZkqswqHohkA0-2UpQGezAp6WKX5qkztjIPTq0vm271czkFnqR7H3vGXeJoOBR-T_dX8I9lKhE_GXb2yvvil6mdMza65JwhZqZXf9VHhlHQuoXaRHTjY73T5H3ceH4SYfjhOCHR3oK5gjuOnUO9QDG1FHdTDaZtQxRPcaH3lUsQRNkvS85gKVf1VT_ZRi1MCtzAKV2di3) |
| 06 | **seq live events** | Eventview: SSE event tail + click into evidence (image + clip). | [PNG](https://www.plantuml.com/plantuml/png/VLJ1Rjf04BtlLumuLA1YZ5FA0HMIYBeHacb8N7gA52ts52PPR6TtJOYYI3tr2_A5-PBksawAQUd9gtblthnvk_A9qbpgV2sWstSKtZc2DkZW1XEjc2Oj44Ayop6945v-FaDWO2XWq9m4l8T8K7HBoH8eqQc1ABPandYaKmcDymmbDwS6S0LpXP9bPY15b76ZqRZGC9-KsAIEd7uDlq_2gyJb6RaRh-2SplP8OUZ3GgOyZhZIM0unjJffB2W3PJpWLT8CFu0BKqecv-Rx9MmLBUDWCWlhJO4762-nWFtFPpvZrZmu8sCK1c3MIi2tEMm8xw6fkReTzZxsMyp0XcIS6jQP_mtSCinZoxYcs9BUPNo9GuzPalEgUDJjmegiJOguWEeA3gYvIalawrtHkfh6hIwvJac5PceijnYpo_pHBA-IIsdkLK5pGnm2x-NNSt3KAWL7Daz3p7ArAXU-n2YLCRFbjt4nrtm0ZnGFVAyDMhKXNTnWf0SHbsrOBD9j6pgTpjDUYcFxTYeFldTmMVyVKUrOWUQFN8Y3oPjsbzOcyUDxYlLgsEzrwt85wDvak8J5WqQrPt8c-8DzvTd_1XHu5LZnccna4XKj4qjbPSrvQwymZ65-6VoztMCkHHjmcv7XNNFzLCr-bRRJFzKqHZiZ2fdfh5UjKxOMnSpkGWbSw6akDAqn9ixO2IQn-GMmtm00) | [SVG](https://www.plantuml.com/plantuml/svg/VLJ1Rjf04BtlLumuLA1YZ5FA0HMIYBeHacb8N7gA52ts52PPR6TtJOYYI3tr2_A5-PBksawAQUd9gtblthnvk_A9qbpgV2sWstSKtZc2DkZW1XEjc2Oj44Ayop6945v-FaDWO2XWq9m4l8T8K7HBoH8eqQc1ABPandYaKmcDymmbDwS6S0LpXP9bPY15b76ZqRZGC9-KsAIEd7uDlq_2gyJb6RaRh-2SplP8OUZ3GgOyZhZIM0unjJffB2W3PJpWLT8CFu0BKqecv-Rx9MmLBUDWCWlhJO4762-nWFtFPpvZrZmu8sCK1c3MIi2tEMm8xw6fkReTzZxsMyp0XcIS6jQP_mtSCinZoxYcs9BUPNo9GuzPalEgUDJjmegiJOguWEeA3gYvIalawrtHkfh6hIwvJac5PceijnYpo_pHBA-IIsdkLK5pGnm2x-NNSt3KAWL7Daz3p7ArAXU-n2YLCRFbjt4nrtm0ZnGFVAyDMhKXNTnWf0SHbsrOBD9j6pgTpjDUYcFxTYeFldTmMVyVKUrOWUQFN8Y3oPjsbzOcyUDxYlLgsEzrwt85wDvak8J5WqQrPt8c-8DzvTd_1XHu5LZnccna4XKj4qjbPSrvQwymZ65-6VoztMCkHHjmcv7XNNFzLCr-bRRJFzKqHZiZ2fdfh5UjKxOMnSpkGWbSw6akDAqn9ixO2IQn-GMmtm00) |
| 07 | **seq liveview** | Liveview: HLS player + bbox overlay synced to PTS. | [PNG](https://www.plantuml.com/plantuml/png/PLFHQjj047o_hrZyP1CRsKdRb42Q62fIWzCwbbCe6CnPsiP7JYVbRgKub48_8b_OB-dUIMhYzaK8pUpEpExgqf6qLEKQngSZXmyZhMgi5JuAKgGH4doeqAG8ltyzmxp5u57H3hRROWz5ZLRB9o5aIeM5tfSIhUIt7aW7jmwjABc_IbKf3K7lXk1src2pGsJwEVawIzOcagMAwadWJ1V_aAunKn8iic8WBEFvzDiXPJu1pEummF77g_WGJP8OjhQGMIeTOUEIlmdXhSBe0zk2C-0GvYLiFvKvJ8vFXa3ItSERj-y6WcbCPd_ClefN4367Wxk8UL4tmWkoqhYoi7I-qqvuDXS5pro6nbKMuTCyWUyykLP4D33pl7ccbPNMi5mbYmxvgyizDvNLS0HknnfXSnbGSO-cqUhY78_7y8EP7A0pTCR5mxMH5UqseM88k2-LHRUHz5Eq-JghFc6drW_qmIitGIC_gTvx9tYNeo7NDWZG4at6mwrLXiNQz6_ekjAaSh_AWVX_abZx1f4ZYpA_y2FVgEoSfmvztjJWC1hXdfgAKLCWX2wA4lWA8NmMuFVxKX2EzWZO8S0hZ-qPWtlYM_T3XCKg4MnSY4j-ylyX_W00) | [SVG](https://www.plantuml.com/plantuml/svg/PLFHQjj047o_hrZyP1CRsKdRb42Q62fIWzCwbbCe6CnPsiP7JYVbRgKub48_8b_OB-dUIMhYzaK8pUpEpExgqf6qLEKQngSZXmyZhMgi5JuAKgGH4doeqAG8ltyzmxp5u57H3hRROWz5ZLRB9o5aIeM5tfSIhUIt7aW7jmwjABc_IbKf3K7lXk1src2pGsJwEVawIzOcagMAwadWJ1V_aAunKn8iic8WBEFvzDiXPJu1pEummF77g_WGJP8OjhQGMIeTOUEIlmdXhSBe0zk2C-0GvYLiFvKvJ8vFXa3ItSERj-y6WcbCPd_ClefN4367Wxk8UL4tmWkoqhYoi7I-qqvuDXS5pro6nbKMuTCyWUyykLP4D33pl7ccbPNMi5mbYmxvgyizDvNLS0HknnfXSnbGSO-cqUhY78_7y8EP7A0pTCR5mxMH5UqseM88k2-LHRUHz5Eq-JghFc6drW_qmIitGIC_gTvx9tYNeo7NDWZG4at6mwrLXiNQz6_ekjAaSh_AWVX_abZx1f4ZYpA_y2FVgEoSfmvztjJWC1hXdfgAKLCWX2wA4lWA8NmMuFVxKX2EzWZO8S0hZ-qPWtlYM_T3XCKg4MnSY4j-ylyX_W00) |
| 08 | **seq async scan** | ONVIF camera scan via async job queue (poll /jobs/{id}). | [PNG](https://www.plantuml.com/plantuml/png/TPBTJjj048Nlvob6kMeb_1UXYbe24Qgs2XS9oC0DL6XP3sZ3PcnsrrHL58c7u0dx98pNuLVYRktlUCuvuzttGRdGBItcEucdkqJvVwmJhnL3CC4ILdJN4Ml2_m-FEEafQhKafv0f_6tSBVPY_B9ehh1L8P3Zhm1AXyRXQDwAK4uZL1vFFJbenSveqoeEE3eEU3eTsFGzcSogisbrmPbgJNPVHCtaw8FeK2mbN4SH7yuFglTyFZkR_ehnc8O8rO_93A0FWicUc6A9LP---3vEYsyxQP7csNWR10WMF-57y-e4iw6op-9YUY2yDnG1SSm0yJ5v-KxsSMdgtKKno9_jnlaOLmCh5yNM1QFyWT3vCWwf-nVMyAqlLvxiTPc9r6UBOhs6ZS5UB26JNCS0jcbQR1jhiUDWBChwbuQL1SIt9Nx_b0wRKO8-9CfnjGaXCzdmpHQshhbnv7sPfzjh8Au1ectosbjmPuE7Jtq-TV6TraJrq7OOKwvgutLpJuxgyZnDqpzzutqnbhi9Jm00) | [SVG](https://www.plantuml.com/plantuml/svg/TPBTJjj048Nlvob6kMeb_1UXYbe24Qgs2XS9oC0DL6XP3sZ3PcnsrrHL58c7u0dx98pNuLVYRktlUCuvuzttGRdGBItcEucdkqJvVwmJhnL3CC4ILdJN4Ml2_m-FEEafQhKafv0f_6tSBVPY_B9ehh1L8P3Zhm1AXyRXQDwAK4uZL1vFFJbenSveqoeEE3eEU3eTsFGzcSogisbrmPbgJNPVHCtaw8FeK2mbN4SH7yuFglTyFZkR_ehnc8O8rO_93A0FWicUc6A9LP---3vEYsyxQP7csNWR10WMF-57y-e4iw6op-9YUY2yDnG1SSm0yJ5v-KxsSMdgtKKno9_jnlaOLmCh5yNM1QFyWT3vCWwf-nVMyAqlLvxiTPc9r6UBOhs6ZS5UB26JNCS0jcbQR1jhiUDWBChwbuQL1SIt9Nx_b0wRKO8-9CfnjGaXCzdmpHQshhbnv7sPfzjh8Au1ectosbjmPuE7Jtq-TV6TraJrq7OOKwvgutLpJuxgyZnDqpzzutqnbhi9Jm00) |
| 09 | **seq large upload** | Large model upload: initiate → presigned PUT → complete. | [PNG](https://www.plantuml.com/plantuml/png/TPB1Jjj048Rl-nIZd1B9RWA80wX2mA5I2i519Zr5YYRs4AQnrylkc3OW91wYJzWdwJXEqYPIJxlQ-NU-V_xToo3efIuB69p5WPxZ0ls2ujeL5UP6M0g2b9vhiXd1xuzVSDlKeQnoAg1LGPSj2wCGz26hIbUGRXsAaBSzOp2JoaCdSUHHTnt009D0tZWbSyOEhK3dNc0oR6l3_ShLNVfjc4vj7nttNuxMcglnWIYPVwTC82Y0fhQRdgYN4TjXekkNjBU-b5n_DQOXGtoX53Y7CMVBTfPFh-oWUpeOmEYwPxIe4eKqcYHzWFvQ5Cwtasu2cjgtHox8OaaH17wbsNmb52B8dYXRXhfyDqqJxQLixUKy1LvO64yUJ7CIxoYR2kMpsXVjfQt9OpNrrl9cd4VxqWZefsCzcQ6ycutn5gOG-Bmdlj1avcpHhpGeJrX6y8FbIJjM2zM5tixK3fpScF-diNtilsdyOxCamHm53o9u8S-Fgrr01p5KoyFPZs3Km8ov99lhFpL_0000) | [SVG](https://www.plantuml.com/plantuml/svg/TPB1Jjj048Rl-nIZd1B9RWA80wX2mA5I2i519Zr5YYRs4AQnrylkc3OW91wYJzWdwJXEqYPIJxlQ-NU-V_xToo3efIuB69p5WPxZ0ls2ujeL5UP6M0g2b9vhiXd1xuzVSDlKeQnoAg1LGPSj2wCGz26hIbUGRXsAaBSzOp2JoaCdSUHHTnt009D0tZWbSyOEhK3dNc0oR6l3_ShLNVfjc4vj7nttNuxMcglnWIYPVwTC82Y0fhQRdgYN4TjXekkNjBU-b5n_DQOXGtoX53Y7CMVBTfPFh-oWUpeOmEYwPxIe4eKqcYHzWFvQ5Cwtasu2cjgtHox8OaaH17wbsNmb52B8dYXRXhfyDqqJxQLixUKy1LvO64yUJ7CIxoYR2kMpsXVjfQt9OpNrrl9cd4VxqWZefsCzcQ6ycutn5gOG-Bmdlj1avcpHhpGeJrX6y8FbIJjM2zM5tixK3fpScF-diNtilsdyOxCamHm53o9u8S-Fgrr01p5KoyFPZs3Km8ov99lhFpL_0000) |
| 10 | **deployment topology** | Workstations / ANSVIS server / edges + ports + customer network. | [PNG](https://www.plantuml.com/plantuml/png/RLJ1Rjf04BtxAqQSY2hKWgEgYgeg4Bkj8m8qfaahSLdiYREDlMljhXEig59F_O0gNvWlwQmtW25mC3kUDxCpR-RvL1kcJ5Naq7_NIx7CPLsWC3qZIvdBhFOCDpb2iF70VEtnz1qN9LEiW4GMfHJaZKrDQ8M9OIBBqVE4J14wPvKsia05cXk4jv3aiaexyEXv00unBL4n8nKyI7L7DHakXRO8eDycFNIkkKZbWuOKzHtLk13V37oFEi0q1FrzSC6IQNmGEjY7ZhceLWUXFa5_RmiTJkAhA0QDwfxwkPNQ7AZo4icf4KebL_L2pCSn65G55xR5XUYAZ8kLvO6bQNtKt3Fv4ar-xETvkOoLL5ANfQoaBF06SinOKXDGeGiUpcRxeHUOSaQJo5dTG2x3yV1d0qgPOKkcBUdBNpGfeDaef0jYdqgwu2AQql_dsAMUZixteiwakE4PbH7Usvk2aNP4ZNQ92jCCWPOhGxCPfFCuhbSVtbEEoLKKHCCcGTXlOTPpVeqQj53db5iAc2ekdDCDYwe8-j3hVNAimWdO2HmV-mjn6SPpIXZ78Js_ZcCl61m2Ue5_yBMphSTIlhKSk_IkiOYAzT7IrpZXepMfnxOrsB7y9cgDsvu7hRDl9KKQMaf3Yg9k9sYiP41LEMgu8GMPMuJaHN0dbgmUJ2kpb9L8ONusitt0ZP8584jkstg3vx__u6Nbx6Axs4ZiXXBk5MOyeDWd6FVjnXkPo1n8pu1s1RgbL8R8DfKGcCDy51oveEkMqa5OnT2wsf9i62cSsyz8NiFpdoTeRNCZAexQGohASk5vftIaJvZt7m00) | [SVG](https://www.plantuml.com/plantuml/svg/RLJ1Rjf04BtxAqQSY2hKWgEgYgeg4Bkj8m8qfaahSLdiYREDlMljhXEig59F_O0gNvWlwQmtW25mC3kUDxCpR-RvL1kcJ5Naq7_NIx7CPLsWC3qZIvdBhFOCDpb2iF70VEtnz1qN9LEiW4GMfHJaZKrDQ8M9OIBBqVE4J14wPvKsia05cXk4jv3aiaexyEXv00unBL4n8nKyI7L7DHakXRO8eDycFNIkkKZbWuOKzHtLk13V37oFEi0q1FrzSC6IQNmGEjY7ZhceLWUXFa5_RmiTJkAhA0QDwfxwkPNQ7AZo4icf4KebL_L2pCSn65G55xR5XUYAZ8kLvO6bQNtKt3Fv4ar-xETvkOoLL5ANfQoaBF06SinOKXDGeGiUpcRxeHUOSaQJo5dTG2x3yV1d0qgPOKkcBUdBNpGfeDaef0jYdqgwu2AQql_dsAMUZixteiwakE4PbH7Usvk2aNP4ZNQ92jCCWPOhGxCPfFCuhbSVtbEEoLKKHCCcGTXlOTPpVeqQj53db5iAc2ekdDCDYwe8-j3hVNAimWdO2HmV-mjn6SPpIXZ78Js_ZcCl61m2Ue5_yBMphSTIlhKSk_IkiOYAzT7IrpZXepMfnxOrsB7y9cgDsvu7hRDl9KKQMaf3Yg9k9sYiP41LEMgu8GMPMuJaHN0dbgmUJ2kpb9L8ONusitt0ZP8584jkstg3vx__u6Nbx6Axs4ZiXXBk5MOyeDWd6FVjnXkPo1n8pu1s1RgbL8R8DfKGcCDy51oveEkMqa5OnT2wsf9i62cSsyz8NiFpdoTeRNCZAexQGohASk5vftIaJvZt7m00) |
| 11 | **l3 engineer ownership** | L3 engineers 1–6 and the components each owns. | [PNG](https://www.plantuml.com/plantuml/png/RLRRRYet57tdAxRIbnmb194BglfG7K88HI8NGYwjn8kPsGmkWpsoFIJagDBvX_OBpvTqUSOamISl8VQiVLlxvg_M2UFALKxjTZC_QRBAf68sJVsisDY5B1fEkfnf93Piw8H-VF-F-W54Ro3Qxz1Mr7vfDEnIga8OiQ94hmgjMBc9sq2DuSG9bUNy0LA8P2aoZW6DS4zx_NPbzc588nOfjMYou3odhaaMqW6_Hy8IGB-CPBPmV-6RVgPl3NetJdjZHnqweDwLM3AjjDA62nq4kxlG0ICguJYbk_xadf9SudggvaQlI1UiH26FrksZlwrMGS7vhe9kwHO4OXt3ejDBLZZ1O43tTj4NR6K6nCOwNY4uj-3L5dgn2xqMQva99xKYeql79i3wkx3RYZYfCdgIgdKbajP8gl9bghmXN1-IqaYxVEN3gHAbqysoIE5jq7Qvgwqtf9LmHhw4puF6FnzpSrpbvbOMd2Fzr4ybGgL9mOaKkRGXESSXEUTvoN5g7gKjHKuNtG5n9JrL--E14SL29lPHyZD9HUDnAPFbaqmpTb-2pYXlrpfbokNC2BEXWwbAj5Ae3i_M6gwa5N51DChO907fefaQkKQojcMfEBS17-NiGQKQgJASQb1enPfnMgFstorSxD9KTyu8-jS-eXPT8LPHOqy2EGF3iFvJuLwLkPCh3wS_HXCgSj-Au6ZT3aISH4HK0ath32ECpCWqmAAWpsVwXJIyXa9FUSNmq1VX0OqdWm7h5JkpyRHk3KKqz7AqVkqFxIEmb5NoXergzubdT_SzMYEAmQHohGxYU8llxn9sMX7MqsekCxhKPXLISHh8kfLg6NErcyRnrOZcNX1cFclUOFWqOg_dFHFWuERwSNX9gRI99sb3SuoL87B-gKWYgkghAkvX2AjtDqDa-XKeYz-5NkdCb_LcQpcYymfCvp1Sv2MwrvSmsYxBGExyG_nHOPrLF5q8kvXfOVnqx2Duvwki5ZWBXFMqGPT5ZDqBWuQYT2kEhi5V2oUI1HP3i7aMKLHP85-KlkTz5YMM2A9_vJJ4TXPtsAHFjfpPnCWP0ZgeMk6ydC-tG-mi8kDIvXXm2CUomA2drbHjAunyS6zo5vyLlwlZmg3t0mKpBu2Z8klKY-LFcEaPRMqJnUmysJJkrX8Ti8VsLYby-VRfu2uqnjp2Jz9sUtD8ilrh606TYBMXmax0tcfHMQ0ECAmSMmVVfkh9fvimgIjYCAk3XhZE3F94VdEPMcv_WdkKfdSVdmgnfRyJiUbxuTRekSmHHy5cZl5cBOVyTY8E7onGgKuiKcRc8WcReLEnXxAGk0CiwgUwpLxHUPYypW1IEzExkRw_w_Rk6mtiwAEZtzyli1PspoVH-JGwd-sU6zXZJ3FjdE_kEPOJudXYNbBxn_T_Zt_pZmAH8oX2Oqa_d_rs9Liz7zn2E2hLBDV9qWziIFGxHmsasgz7_bdvyOvoieN4KqBMwoBFqRc-bb9k6ccNjI6qK8eK-vVBd8Kh3N_K_nN_ukNL-1y0) | [SVG](https://www.plantuml.com/plantuml/svg/RLRRRYet57tdAxRIbnmb194BglfG7K88HI8NGYwjn8kPsGmkWpsoFIJagDBvX_OBpvTqUSOamISl8VQiVLlxvg_M2UFALKxjTZC_QRBAf68sJVsisDY5B1fEkfnf93Piw8H-VF-F-W54Ro3Qxz1Mr7vfDEnIga8OiQ94hmgjMBc9sq2DuSG9bUNy0LA8P2aoZW6DS4zx_NPbzc588nOfjMYou3odhaaMqW6_Hy8IGB-CPBPmV-6RVgPl3NetJdjZHnqweDwLM3AjjDA62nq4kxlG0ICguJYbk_xadf9SudggvaQlI1UiH26FrksZlwrMGS7vhe9kwHO4OXt3ejDBLZZ1O43tTj4NR6K6nCOwNY4uj-3L5dgn2xqMQva99xKYeql79i3wkx3RYZYfCdgIgdKbajP8gl9bghmXN1-IqaYxVEN3gHAbqysoIE5jq7Qvgwqtf9LmHhw4puF6FnzpSrpbvbOMd2Fzr4ybGgL9mOaKkRGXESSXEUTvoN5g7gKjHKuNtG5n9JrL--E14SL29lPHyZD9HUDnAPFbaqmpTb-2pYXlrpfbokNC2BEXWwbAj5Ae3i_M6gwa5N51DChO907fefaQkKQojcMfEBS17-NiGQKQgJASQb1enPfnMgFstorSxD9KTyu8-jS-eXPT8LPHOqy2EGF3iFvJuLwLkPCh3wS_HXCgSj-Au6ZT3aISH4HK0ath32ECpCWqmAAWpsVwXJIyXa9FUSNmq1VX0OqdWm7h5JkpyRHk3KKqz7AqVkqFxIEmb5NoXergzubdT_SzMYEAmQHohGxYU8llxn9sMX7MqsekCxhKPXLISHh8kfLg6NErcyRnrOZcNX1cFclUOFWqOg_dFHFWuERwSNX9gRI99sb3SuoL87B-gKWYgkghAkvX2AjtDqDa-XKeYz-5NkdCb_LcQpcYymfCvp1Sv2MwrvSmsYxBGExyG_nHOPrLF5q8kvXfOVnqx2Duvwki5ZWBXFMqGPT5ZDqBWuQYT2kEhi5V2oUI1HP3i7aMKLHP85-KlkTz5YMM2A9_vJJ4TXPtsAHFjfpPnCWP0ZgeMk6ydC-tG-mi8kDIvXXm2CUomA2drbHjAunyS6zo5vyLlwlZmg3t0mKpBu2Z8klKY-LFcEaPRMqJnUmysJJkrX8Ti8VsLYby-VRfu2uqnjp2Jz9sUtD8ilrh606TYBMXmax0tcfHMQ0ECAmSMmVVfkh9fvimgIjYCAk3XhZE3F94VdEPMcv_WdkKfdSVdmgnfRyJiUbxuTRekSmHHy5cZl5cBOVyTY8E7onGgKuiKcRc8WcReLEnXxAGk0CiwgUwpLxHUPYypW1IEzExkRw_w_Rk6mtiwAEZtzyli1PspoVH-JGwd-sU6zXZJ3FjdE_kEPOJudXYNbBxn_T_Zt_pZmAH8oX2Oqa_d_rs9Liz7zn2E2hLBDV9qWziIFGxHmsasgz7_bdvyOvoieN4KqBMwoBFqRc-bb9k6ccNjI6qK8eK-vVBd8Kh3N_K_nN_ukNL-1y0) |
| 12 | **dependency chain** | Critical path: contract → mock → UI + adapter → integration → ship. | [PNG](https://www.plantuml.com/plantuml/png/VLFRRjD047ttLnpb2GH9bIBnWa1rKvS6GXF50Ga92MtiYRDqlMljJeB24_y0NyYNC5utQG9INxoxupDdRcSk0YlFczfWUDulgI5RaYrs_MAjj4rOio5S7So8Plpv-HlVrufHAQOGhqaIxhHjb5Sr2bStpfBbd7VYxgbWPIj3IIAXz4eLZ9uJIjNemNOu-1QS_MAVhBpxGVPf3ofWDBrTpDFH8ZbGeJVpEjIeNN67G7vBFY8_J4VlZr7fRVvfdEDCkC-sGwXIDKnUwDEnv5MI3y_HaEzx2cxZ2-hYfLVfx2JOX2ebvNPi4JB9tgQZpoUO5mXhCWRFCC_o1GgZnIwXic77-d7yDR_99fD_dGfFP4CBF9TM_aBsye2_pjAJFEQa39Qgk9C1G5kcoYlMpaRyF4jFkFErRh0Tnd_vpNYM9Fi-ejz_qxNgnDAL7R_nVLzZ2uplGm7xozuQqxm_eoscya0Kd-qdlhfSh6F2qZ6x6cxrC69WQiqE1sbmAwpBwU86GPUYhgLfH-xiG93NKIFoA0ZDPccq3A1y9LCkDUijjVAqbVWuOHmaRS_QgF_5Zfs058AIjcHS4x0cJn09rYZL3iD7dBkc0PSRRLXcWbRqnf3CIGIpLrcRxUn8PoBSScEKrxp3Q_7QhngZyQuIL7WaP6mdyCvfgss5HauUjEdBzd60trYeIXOpSEkqxfP42PiN-OWZEocKPGrHUhtYuqWNSfNjJ_u2) | [SVG](https://www.plantuml.com/plantuml/svg/VLFRRjD047ttLnpb2GH9bIBnWa1rKvS6GXF50Ga92MtiYRDqlMljJeB24_y0NyYNC5utQG9INxoxupDdRcSk0YlFczfWUDulgI5RaYrs_MAjj4rOio5S7So8Plpv-HlVrufHAQOGhqaIxhHjb5Sr2bStpfBbd7VYxgbWPIj3IIAXz4eLZ9uJIjNemNOu-1QS_MAVhBpxGVPf3ofWDBrTpDFH8ZbGeJVpEjIeNN67G7vBFY8_J4VlZr7fRVvfdEDCkC-sGwXIDKnUwDEnv5MI3y_HaEzx2cxZ2-hYfLVfx2JOX2ebvNPi4JB9tgQZpoUO5mXhCWRFCC_o1GgZnIwXic77-d7yDR_99fD_dGfFP4CBF9TM_aBsye2_pjAJFEQa39Qgk9C1G5kcoYlMpaRyF4jFkFErRh0Tnd_vpNYM9Fi-ejz_qxNgnDAL7R_nVLzZ2uplGm7xozuQqxm_eoscya0Kd-qdlhfSh6F2qZ6x6cxrC69WQiqE1sbmAwpBwU86GPUYhgLfH-xiG93NKIFoA0ZDPccq3A1y9LCkDUijjVAqbVWuOHmaRS_QgF_5Zfs058AIjcHS4x0cJn09rYZL3iD7dBkc0PSRRLXcWbRqnf3CIGIpLrcRxUn8PoBSScEKrxp3Q_7QhngZyQuIL7WaP6mdyCvfgss5HauUjEdBzd60trYeIXOpSEkqxfP42PiN-OWZEocKPGrHUhtYuqWNSfNjJ_u2) |
+78
View File
@@ -0,0 +1,78 @@
# ANSCORE — L2 & L3 Architecture Diagrams (PlantUML)
12 PlantUML diagrams covering Layer 2 (ANSVIS Communication Layer (CL)) and Layer 3 (Qt/QML UI).
## Files
| # | File | Type | Purpose |
|---|---|---|---|
| 01 | `01-system-context.puml` | Component | L1 / L2 / L3 overview with the 3 transports |
| 02 | `02-l2-internals.puml` | Component | L2 adapter layer + legacy + infrastructure |
| 03 | `03-l2-ownership.puml` | Component | Human-owned vs AI-coder vs reused legacy modules |
| 04 | `04-l3-internals.puml` | Component | Qt/QML shell / network / state / screens |
| 05 | `05-seq-configure-deploy.puml` | Sequence | Operator adds camera → AI task → deploys |
| 06 | `06-seq-live-events.puml` | Sequence | SSE event tail + evidence image/clip |
| 07 | `07-seq-liveview.puml` | Sequence | Liveview with bbox overlay sync |
| 08 | `08-seq-async-scan.puml` | Sequence | Async ONVIF scan via job queue |
| 09 | `09-seq-large-upload.puml` | Sequence | Large model upload via presigned URL |
| 10 | `10-deployment-topology.puml` | Deployment | Workstations / server / edges + ports |
| 11 | `11-l3-engineer-ownership.puml` | Component | 6-engineer L3 task allocation |
| 12 | `12-dependency-chain.puml` | Component | What gates what, critical path |
---
## How to view these in StarUML
StarUML doesn't render `.puml` natively, but the official **PlantUML extension** does:
1. In StarUML: `Tools → Extension Manager → search "PlantUML" → Install`
2. Restart StarUML.
3. `File → Open` → pick any `.puml` file from this folder, **or** use `Tools → PlantUML → Preview Diagram`.
4. To get an editable StarUML diagram (`.mdj`), use the extension's "Generate Diagram from PlantUML" feature, then save as `.mdj`.
Caveat: the extension renders accurately but the auto-generated StarUML elements may need light layout cleanup. If you want fully native StarUML diagrams, use these `.puml` files as the specification and recreate in StarUML — they're small enough that this takes ~10 min each.
---
## Alternative viewers (no install needed)
| Tool | How |
|---|---|
| **PlantUML web server** | <https://www.plantuml.com/plantuml> → paste any `.puml` content → get PNG/SVG/PDF |
| **VS Code** | Install the "PlantUML" extension by jebbs → `Alt+D` to preview |
| **JetBrains (CLion / IntelliJ / Rider)** | Install the "PlantUML Integration" plugin → preview on the side panel |
| **PlantUML CLI** (batch export) | `brew install plantuml` then `plantuml -tpng docs/diagrams/*.puml -o ../out/` |
| **PlantUML CLI** (SVG for slides) | `plantuml -tsvg docs/diagrams/*.puml -o ../out/` |
The web server is the fastest path if you just want to grab images for a slide deck or PDF.
---
## Render all to PNG in one shot
```bash
# from the repo root
brew install plantuml # macOS; or: apt install plantuml
mkdir -p out/diagrams
plantuml -tpng -o "$(pwd)/out/diagrams" docs/diagrams/*.puml
```
Output: one PNG per diagram in `out/diagrams/`.
For high-DPI presentations use `-tsvg` instead of `-tpng`.
---
## Editing tips
- Diagrams use only stable PlantUML features — should render in any PlantUML version ≥ 1.2022.0.
- Colors used: `#LightYellow` (human/lead), `#LightBlue` (AI/E2), `#LightGreen` (E3), `#LightPink` (E4), `#LightCoral` (E5), `#Lavender` (E6), `#LightGray` (reused legacy). Change consistently if you want a different palette.
- Each file declares its own `@startuml NAME` so multiple files can render to predictable filenames.
- Notes (`note bottom of ...`) are intentional — leave them in; they explain non-obvious decisions.
---
## Related
- `docs/L2-L3-architecture.md` — same diagrams in Mermaid + accompanying prose.
- `contracts/openapi.v1.json` — the REST contract these diagrams reference.
Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 59 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 104 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

+191
View File
@@ -0,0 +1,191 @@
@page {
size: A4;
margin: 22mm 18mm 22mm 18mm;
@top-right {
content: "ANSCORE — System Architecture (L2 + L3)";
font-family: -apple-system, "Helvetica Neue", Arial, sans-serif;
font-size: 8.5pt;
color: #888;
}
@bottom-right {
content: "Page " counter(page) " / " counter(pages);
font-family: -apple-system, "Helvetica Neue", Arial, sans-serif;
font-size: 8.5pt;
color: #888;
}
@bottom-left {
content: "v1.0 • 2026-05-26 • ANSCENTER";
font-family: -apple-system, "Helvetica Neue", Arial, sans-serif;
font-size: 8.5pt;
color: #888;
}
}
@page :first {
@top-right { content: ""; }
@bottom-right { content: ""; }
@bottom-left { content: ""; }
}
html, body {
font-family: -apple-system, "Helvetica Neue", "Segoe UI", Arial, "Apple Symbols", "STIXGeneral", "Lucida Grande", sans-serif;
font-size: 10.5pt;
line-height: 1.45;
color: #222;
}
h1 {
font-size: 22pt;
color: #102a55;
border-bottom: 2px solid #102a55;
padding-bottom: 4pt;
margin-top: 0;
page-break-before: always;
page-break-after: avoid;
}
h1:first-of-type {
page-break-before: avoid;
}
h2 {
font-size: 15pt;
color: #1a4080;
margin-top: 1.4em;
page-break-after: avoid;
}
h3 {
font-size: 12.5pt;
color: #1a4080;
margin-top: 1.1em;
page-break-after: avoid;
}
h4 {
font-size: 11pt;
color: #333;
margin-top: 0.9em;
page-break-after: avoid;
}
p, ul, ol {
margin: 0.5em 0;
}
ul, ol {
padding-left: 1.4em;
}
li {
margin: 0.15em 0;
}
a {
color: #1a4080;
text-decoration: none;
}
code {
font-family: "SF Mono", "Menlo", Consolas, monospace;
font-size: 9pt;
background: #f4f4f6;
padding: 1px 4px;
border-radius: 3px;
color: #2c2c2c;
}
pre {
font-family: "SF Mono", "Menlo", Consolas, monospace;
font-size: 9pt;
background: #f6f8fa;
border: 1px solid #e1e4e8;
border-radius: 4px;
padding: 10pt 12pt;
line-height: 1.4;
overflow-x: auto;
page-break-inside: avoid;
}
pre code {
background: transparent;
padding: 0;
font-size: 9pt;
}
table {
border-collapse: collapse;
width: 100%;
margin: 0.8em 0;
font-size: 9.5pt;
page-break-inside: avoid;
}
th, td {
border: 1px solid #d0d4dc;
padding: 5pt 7pt;
text-align: left;
vertical-align: top;
}
th {
background: #eef2f8;
color: #102a55;
font-weight: 600;
}
tr:nth-child(even) td {
background: #fafbfd;
}
img {
max-width: 100%;
display: block;
margin: 0.6em auto;
page-break-inside: avoid;
}
hr {
border: 0;
border-top: 1px solid #d0d4dc;
margin: 1.5em 0;
}
blockquote {
border-left: 3px solid #c3cad6;
margin: 0.6em 0;
padding: 0.2em 0.9em;
color: #555;
background: #f7f8fa;
font-style: italic;
}
/* Title block (pandoc emits a header div) */
header#title-block-header {
text-align: center;
margin-top: 18%;
padding-bottom: 35%;
border-bottom: none;
}
header#title-block-header h1.title {
font-size: 30pt;
color: #102a55;
border-bottom: none;
page-break-before: avoid;
margin-bottom: 12pt;
}
header#title-block-header p.subtitle {
font-size: 14pt;
color: #555;
font-style: italic;
margin: 6pt 0;
}
header#title-block-header p.author,
header#title-block-header p.date {
font-size: 11pt;
color: #777;
margin: 4pt 0;
}
+1 -1
View File
@@ -540,7 +540,7 @@ namespace ANSCENTER {
") returned null — the DLL may be corrupt."); ") returned null — the DLL may be corrupt.");
s_ort_api = api; s_ort_api = api;
Ort::Global<void>::api_ = api; Ort::InitApi(api);
std::cout << "[EPLoader] ORT loaded successfully." << std::endl; std::cout << "[EPLoader] ORT loaded successfully." << std::endl;
std::cout << "[EPLoader] ORT DLL version : " << base->GetVersionString() << std::endl; std::cout << "[EPLoader] ORT DLL version : " << base->GetVersionString() << std::endl;
+11 -3
View File
@@ -141,7 +141,7 @@ namespace ANSCENTER {
{ {
try { try {
// Make sure the Ort API pointer is initialised in THIS DLL. // Make sure the Ort API pointer is initialised in THIS DLL.
if (Ort::Global<void>::api_ == nullptr) { if (Ort::detail::Global::Api() == nullptr) {
Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw())); Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw()));
} }
@@ -574,10 +574,10 @@ namespace ANSCENTER {
const auto& epInfo = EPLoader::Current(); const auto& epInfo = EPLoader::Current();
ANS_DBG("OrtHandler", "initialize_handler: EPLoader type=%d dir=%s", ANS_DBG("OrtHandler", "initialize_handler: EPLoader type=%d dir=%s",
static_cast<int>(epInfo.type), epInfo.libraryDir.c_str()); static_cast<int>(epInfo.type), epInfo.libraryDir.c_str());
if (Ort::Global<void>::api_ == nullptr) if (Ort::detail::Global::Api() == nullptr)
Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw())); Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw()));
std::cout << "[ORT] api_ = " << (void*)Ort::Global<void>::api_ << std::endl; std::cout << "[ORT] api_ = " << (void*)Ort::detail::Global::Api() << std::endl;
EngineType engine = (static_cast<int>(m_engineType) == -1) EngineType engine = (static_cast<int>(m_engineType) == -1)
? epInfo.type : m_engineType; ? epInfo.type : m_engineType;
@@ -1900,4 +1900,12 @@ namespace ANSCENTER {
return std::fabs(width() * height()); return std::fabs(width() * height());
} }
// Explicit instantiations of every BoundingBoxType member must live
// AFTER the template definitions above so the compiler actually emits
// symbols (the directives in ONNXEngine.h sit before the definitions
// and ClangCL, unlike MSVC, refuses to emit symbols from there).
template struct ANSCENTER::types::BoundingBoxType<int, float>;
template struct ANSCENTER::types::BoundingBoxType<float, float>;
template struct ANSCENTER::types::BoundingBoxType<double, double>;
} // namespace ANSCENTER } // namespace ANSCENTER
+4 -3
View File
@@ -73,9 +73,10 @@ namespace ANSCENTER {
} }
}; };
template class BoundingBoxType<int, float>; // Explicit instantiations now live at the end of ONNXEngine.cpp
template class BoundingBoxType<float, float>; // (after the template member definitions) so the compiler actually
template class BoundingBoxType<double, double>; // emits symbols. ClangCL refuses to emit them from a directive that
// precedes the definitions, whereas MSVC silently tolerated it.
typedef BoundingBoxType<int, float> Boxi; typedef BoundingBoxType<int, float> Boxi;
typedef BoundingBoxType<float, float> Boxf; typedef BoundingBoxType<float, float> Boxf;
+1 -1
View File
@@ -331,7 +331,7 @@ namespace ANSCENTER
{ {
// Initialize ORT API // Initialize ORT API
const auto& epInfo = EPLoader::Current(); const auto& epInfo = EPLoader::Current();
if (Ort::Global<void>::api_ == nullptr) if (Ort::detail::Global::Api() == nullptr)
Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw())); Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw()));
m_env = new Ort::Env(ORT_LOGGING_LEVEL_ERROR, "ONNXSAM3"); m_env = new Ort::Env(ORT_LOGGING_LEVEL_ERROR, "ONNXSAM3");
@@ -4,6 +4,7 @@
#include <sstream> #include <sstream>
#include "Utility.h" #include "Utility.h"
#include "TRTCompat.h" #include "TRTCompat.h"
#include "GpuMemoryGuard.h"
// ============================================================================ // ============================================================================
// Crash-safe wrappers for TensorRT operations that can crash the process. // Crash-safe wrappers for TensorRT operations that can crash the process.
@@ -805,13 +806,22 @@ bool Engine<T>::loadNetwork(std::string trtModelPath, const std::array<float, 3>
// (cudaMalloc failure during inference) when too many tasks are loaded. // (cudaMalloc failure during inference) when too many tasks are loaded.
{ {
size_t freeVRAM = 0, totalVRAM = 0; size_t freeVRAM = 0, totalVRAM = 0;
cudaError_t memErr = cudaMemGetInfo(&freeVRAM, &totalVRAM); bool haveMem = ANSCENTER::GpuMemoryGuard::Query(freeVRAM, totalVRAM);
constexpr size_t kMinFreeBytes = 256ULL * 1024 * 1024; // 256 MiB minimum // (1) Admission control: refuse to load this engine on the GPU if it
if (memErr != cudaSuccess) { // does not fit while keeping the GpuMemoryGuard safety margin free.
// cudaMemGetInfo failed — CUDA context may not be initialized on this thread. // Prevents VRAM over-subscription across concurrent engines (FR/OD/LPR/
// Don't reject: let TRT try to deserialize (it may succeed). // liveness) that would otherwise OOM and permanently poison the CUDA
} else if (freeVRAM < kMinFreeBytes) { // context. The engine file is valid — m_lastLoadFailedVRAM tells the
m_lastLoadFailedVRAM = true; // signal to buildLoadNetwork: engine file is NOT corrupt // caller to preserve it (no rebuild) and just skip GPU load for now.
if (haveMem && !ANSCENTER::GpuMemoryGuard::CanLoad(buffer.size())) {
m_lastLoadFailedVRAM = true;
std::cerr << "[GpuMemGuard] REFUSING engine load on GPU "
<< m_options.deviceIndex << ": need ~"
<< (buffer.size() / (1024 * 1024)) << " MiB + "
<< (ANSCENTER::GpuMemoryGuard::Margin() / (1024 * 1024))
<< " MiB margin, only " << (freeVRAM / (1024 * 1024))
<< " MiB free. Reduce concurrent engines/cameras or batch size."
<< std::endl;
return false; return false;
} }
if (m_verbose) { if (m_verbose) {
@@ -1425,7 +1435,15 @@ trt_cache_create_context:
// ============================================================================ // ============================================================================
if (m_verbose) std::cout << "\nInfo: Starting warm-up inference..." << std::endl; if (m_verbose) std::cout << "\nInfo: Starting warm-up inference..." << std::endl;
warmUp(m_verbose ? 10 : 1); if (!warmUp(m_verbose ? 10 : 1)) {
// A failed warmup means this engine cannot run inference (wrong-device
// buffers, poisoned context, ...). Report failure so pool creation
// rejects the slot instead of silently adding a broken engine that
// corrupts every subsequent inference dispatched to it.
std::cout << "Error: Warm-up inference FAILED on GPU[" << m_options.deviceIndex
<< "] -- engine rejected (loadNetwork returns false)" << std::endl;
return false;
}
if (m_verbose) std::cout << "Info: Warm-up complete" << std::endl; if (m_verbose) std::cout << "Info: Warm-up complete" << std::endl;
} // end of trt_cache_create_context scope } // end of trt_cache_create_context scope
@@ -1798,6 +1816,25 @@ bool Engine<T>::build(std::string onnxModelPath, const std::array<float, 3>& sub
// Set hardware compatibility // Set hardware compatibility
config->setHardwareCompatibilityLevel(nvinfer1::HardwareCompatibilityLevel::kNONE); config->setHardwareCompatibilityLevel(nvinfer1::HardwareCompatibilityLevel::kNONE);
// -- Blackwell (SM 12.x) aux-stream workaround ----------------------------
// On sm_120 (RTX 50-series), TRT 10.15's Myelin runtime faults when a second
// concurrent execution context runs with auto-selected aux streams:
// "Internal Error: MyelinCheckException ... l2cm_layer_internal.cpp:174"
// followed by CUDA error 700 (illegal memory access) poisoning the context.
// Aux streams only provide intra-inference parallelism (irrelevant for the
// small per-slot models we run); throughput comes from the engine pool's
// one-context-per-slot design, which is unaffected. Force 0 aux streams on
// Blackwell until NVIDIA fixes the Myelin multi-stream path.
// NOTE: baked in at build time -- stale cached .engine files must be deleted
// for this to take effect.
#if NV_TENSORRT_MAJOR >= 10
if (prop.major >= 12) {
config->setMaxAuxStreams(0);
std::cout << "Aux streams disabled on SM " << prop.major << "." << prop.minor
<< " (Blackwell Myelin multi-stream workaround)" << std::endl;
}
#endif
// -- TensorRT 10+ tactic DRAM pool ---------------------------------------- // -- TensorRT 10+ tactic DRAM pool ----------------------------------------
// Separate scratch pool for kernel-selection during build. Without this, // Separate scratch pool for kernel-selection during build. Without this,
// tactic evaluation competes with the workspace allocation and tactics // tactic evaluation competes with the workspace allocation and tactics
@@ -606,6 +606,25 @@ bool Engine<T>::runInferenceFromPool(
bool kickedGrowth = false; bool kickedGrowth = false;
auto _poolAcquireStart = std::chrono::steady_clock::now(); auto _poolAcquireStart = std::chrono::steady_clock::now();
// -- DEVICE AFFINITY: find which GPU the caller's inputs live on ------
// Dispatching to a slot on the same GPU avoids the pinned-host migration
// copy (~1 ms per 1080p frame) that runInference performs for foreign
// inputs. Queried outside the slot mutex — it's a cheap driver lookup.
int inputDevice = -1;
for (const auto& batch : inputs) {
for (const auto& img : batch) {
if (!img.data) continue;
cudaPointerAttributes attr{};
if (cudaPointerGetAttributes(&attr, img.data) == cudaSuccess &&
attr.type == cudaMemoryTypeDevice) {
inputDevice = attr.device;
}
cudaGetLastError(); // clear any error the query left behind
break; // first image is representative
}
if (inputDevice >= 0) break;
}
{ {
std::unique_lock<std::mutex> lock(m_slotMutex); std::unique_lock<std::mutex> lock(m_slotMutex);
@@ -616,13 +635,31 @@ bool Engine<T>::runInferenceFromPool(
const size_t n = m_slots.size(); const size_t n = m_slots.size();
if (n > 0) { if (n > 0) {
const size_t start = m_nextSlotHint.load() % n; const size_t start = m_nextSlotHint.load() % n;
for (size_t i = 0; i < n; ++i) {
auto& s = m_slots[(start + i) % n]; // Pass A: idle slot on the inputs' own GPU (zero-copy dispatch)
if (!s.busy && s.engine) { // alive and idle if (inputDevice >= 0) {
s.busy = true; for (size_t i = 0; i < n; ++i) {
slot = &s; auto& s = m_slots[(start + i) % n];
m_nextSlotHint = (start + i + 1) % n; if (!s.busy && s.engine && s.deviceIndex == inputDevice) {
break; s.busy = true;
slot = &s;
m_nextSlotHint = (start + i + 1) % n;
break;
}
}
}
// Pass B: any idle slot (round-robin) — runInference migrates
// the inputs across GPUs when needed.
if (!slot) {
for (size_t i = 0; i < n; ++i) {
auto& s = m_slots[(start + i) % n];
if (!s.busy && s.engine) { // alive and idle
s.busy = true;
slot = &s;
m_nextSlotHint = (start + i + 1) % n;
break;
}
} }
} }
} }
@@ -130,13 +130,31 @@ static inline cudaError_t cudaStreamSynchronize_Safe(cudaStream_t stream) {
} }
template <typename T> template <typename T>
void Engine<T>::warmUp(int iterations) { bool Engine<T>::warmUp(int iterations) {
if (m_verbose) { if (m_verbose) {
std::cout << "\n========================================" << std::endl; std::cout << "\n========================================" << std::endl;
std::cout << "Engine Warmup" << std::endl; std::cout << "Engine Warmup" << std::endl;
std::cout << "========================================" << std::endl; std::cout << "========================================" << std::endl;
} }
// Ensure this thread's CUDA device matches the engine BEFORE the dummy-input
// uploads below. warmUp() does GPU work (GpuMat uploads) ahead of
// runInference()'s own device guard, so it must not rely on the caller
// having left the right device current (multi-GPU pool creation touches
// several devices on one thread).
{
int currentDev = -1;
cudaGetDevice(&currentDev);
if (currentDev != m_options.deviceIndex) {
std::cout << "Warning [Warmup]: thread was on GPU[" << currentDev
<< "], engine expects GPU[" << m_options.deviceIndex
<< "] -- switching device" << std::endl;
cudaSetDevice(m_options.deviceIndex);
}
}
bool allBatchesOk = true;
// Determine batch sizes to warm up // Determine batch sizes to warm up
std::vector<int> batchSizes; std::vector<int> batchSizes;
@@ -212,7 +230,9 @@ void Engine<T>::warmUp(int iterations) {
auto firstTime = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); auto firstTime = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
if (!firstSuccess) { if (!firstSuccess) {
if (m_verbose) std::cout << " ✗ ERROR: First iteration failed for batch=" << batchSize << std::endl; std::cout << " ✗ ERROR: Warmup first iteration failed for batch=" << batchSize
<< " on GPU[" << m_options.deviceIndex << "]" << std::endl;
allBatchesOk = false;
continue; continue;
} }
@@ -231,7 +251,9 @@ void Engine<T>::warmUp(int iterations) {
for (int i = 1; i < iters; ++i) { for (int i = 1; i < iters; ++i) {
bool success = runInference(dummyInputs, dummyOutputs); bool success = runInference(dummyInputs, dummyOutputs);
if (!success) { if (!success) {
if (m_verbose) std::cout << " ✗ ERROR: Iteration " << i << " failed" << std::endl; std::cout << " ✗ ERROR: Warmup iteration " << i << " failed for batch="
<< batchSize << " on GPU[" << m_options.deviceIndex << "]" << std::endl;
allBatchesOk = false;
break; break;
} }
} }
@@ -262,6 +284,7 @@ void Engine<T>::warmUp(int iterations) {
std::cout << "Kernels pre-compiled for all batch sizes." << std::endl; std::cout << "Kernels pre-compiled for all batch sizes." << std::endl;
std::cout << "========================================\n" << std::endl; std::cout << "========================================\n" << std::endl;
} }
return allBatchesOk;
} }
template <typename T> template <typename T>
bool Engine<T>::runInference(const std::vector<std::vector<cv::cuda::GpuMat>>& inputs,std::vector<std::vector<std::vector<T>>>& featureVectors) { bool Engine<T>::runInference(const std::vector<std::vector<cv::cuda::GpuMat>>& inputs,std::vector<std::vector<std::vector<T>>>& featureVectors) {
@@ -778,6 +801,118 @@ bool Engine<T>::runInference(const std::vector<std::vector<cv::cuda::GpuMat>>& i
} }
} }
// Pass 1.5: INPUT DEVICE MIGRATION (multi-GPU pool dispatch).
//
// The caller prepares input GpuMats on ITS device (e.g. camera preprocessing
// on GPU 0), but round-robin dispatch may select a slot on another GPU.
// Kernels cannot touch cross-device memory without peer access: preprocessing
// a device-A GpuMat with a device-B stream raises an async illegal memory
// access that permanently poisons every CUDA context in the process
// (sticky error 700). Detect foreign inputs and migrate them onto this
// engine's device via a UVA copy (cudaMemcpyDefault routes over PCIe).
std::vector<std::vector<cv::cuda::GpuMat>> migratedInputs;
const std::vector<std::vector<cv::cuda::GpuMat>>* activeInputs = &inputs;
{
bool anyForeign = false;
for (size_t i = 0; i < numInputs && !anyForeign; ++i) {
for (const auto& img : inputs[i]) {
if (!img.data) continue;
cudaPointerAttributes attr{};
if (cudaPointerGetAttributes(&attr, img.data) == cudaSuccess &&
attr.type == cudaMemoryTypeDevice &&
attr.device != m_options.deviceIndex) {
anyForeign = true;
}
cudaGetLastError(); // clear any error the attribute query left behind
break; // first image per tensor is representative
}
}
if (anyForeign) {
migratedInputs.resize(numInputs);
for (size_t i = 0; i < numInputs; ++i) {
migratedInputs[i].reserve(inputs[i].size());
for (const auto& src : inputs[i]) {
cudaPointerAttributes attr{};
const bool foreign = src.data &&
cudaPointerGetAttributes(&attr, src.data) == cudaSuccess &&
attr.type == cudaMemoryTypeDevice &&
attr.device != m_options.deviceIndex;
cudaGetLastError();
if (!foreign) { migratedInputs[i].push_back(src); continue; }
// Two-hop copy through pinned host staging. A direct
// device-to-device cudaMemcpy2D across GPUs is not supported
// (2D copies have no peer path, and GeForce has no P2P), so:
// hop 1: strided D2H from the foreign GPU into pinned host
// hop 2: strided H2D onto this engine's GPU
// Synchronous copies keep the single staging buffer safe to
// reuse across images; ~1 ms per 1080p frame over PCIe 4.
struct PinnedStaging { // thread-local, grow-only
void* ptr = nullptr;
size_t cap = 0;
~PinnedStaging() { if (ptr) cudaFreeHost(ptr); }
};
thread_local PinnedStaging tl_staging;
const size_t rowBytes = static_cast<size_t>(src.cols) * src.elemSize();
const size_t needed = rowBytes * static_cast<size_t>(src.rows);
if (needed > tl_staging.cap) {
if (tl_staging.ptr) cudaFreeHost(tl_staging.ptr);
tl_staging.ptr = nullptr; tl_staging.cap = 0;
if (cudaMallocHost(&tl_staging.ptr, needed) != cudaSuccess) {
std::cout << "Error [MultiGPU]: pinned staging alloc failed ("
<< needed << " bytes)" << std::endl;
return false;
}
tl_staging.cap = needed;
}
// Hop 1: D2H. UVA lets us read the foreign pointer from this
// device's context; if the runtime rejects it, retry with the
// source device current.
cudaError_t mErr = cudaMemcpy2D(
tl_staging.ptr, rowBytes, src.data, src.step,
rowBytes, src.rows, cudaMemcpyDeviceToHost);
if (mErr != cudaSuccess) {
cudaGetLastError();
cudaSetDevice(attr.device);
mErr = cudaMemcpy2D(
tl_staging.ptr, rowBytes, src.data, src.step,
rowBytes, src.rows, cudaMemcpyDeviceToHost);
cudaSetDevice(m_options.deviceIndex);
}
if (mErr != cudaSuccess) {
std::cout << "Error [MultiGPU]: migration hop1 (D2H from GPU["
<< attr.device << "]) failed: " << cudaGetErrorString(mErr) << std::endl;
return false;
}
// Hop 2: H2D onto this engine's GPU.
cv::cuda::GpuMat dst(src.rows, src.cols, src.type());
mErr = cudaMemcpy2D(
dst.data, dst.step, tl_staging.ptr, rowBytes,
rowBytes, src.rows, cudaMemcpyHostToDevice);
if (mErr != cudaSuccess) {
std::cout << "Error [MultiGPU]: migration hop2 (H2D to GPU["
<< m_options.deviceIndex << "]) failed: "
<< cudaGetErrorString(mErr) << std::endl;
return false;
}
migratedInputs[i].push_back(dst);
}
}
activeInputs = &migratedInputs;
static std::atomic<bool> s_migrationLogged{false};
if (!s_migrationLogged.exchange(true)) {
std::cout << "Info [MultiGPU]: inputs resided on another GPU -- migrated to GPU["
<< m_options.deviceIndex << "] via UVA copy (logged once; recurs per"
<< " cross-device dispatch)" << std::endl;
}
}
}
// Pass 2: Preprocess + D2D copies — all on m_inferenceStream (no null stream). // Pass 2: Preprocess + D2D copies — all on m_inferenceStream (no null stream).
// //
// All OpenCV CUDA ops (convertTo, subtract, divide, split) in blobFromGpuMats // All OpenCV CUDA ops (convertTo, subtract, divide, split) in blobFromGpuMats
@@ -794,7 +929,7 @@ bool Engine<T>::runInference(const std::vector<std::vector<cv::cuda::GpuMat>>& i
preprocessedBuffers.reserve(numInputs); preprocessedBuffers.reserve(numInputs);
for (size_t i = 0; i < numInputs; ++i) { for (size_t i = 0; i < numInputs; ++i) {
const auto& batchInput = inputs[i]; const auto& batchInput = (*activeInputs)[i];
// Preprocess on m_inferenceStream (not the null stream). // Preprocess on m_inferenceStream (not the null stream).
preprocessedBuffers.push_back( preprocessedBuffers.push_back(
@@ -811,8 +946,23 @@ bool Engine<T>::runInference(const std::vector<std::vector<cv::cuda::GpuMat>>& i
m_inferenceStream); m_inferenceStream);
if (copyErr != cudaSuccess) { if (copyErr != cudaSuccess) {
// Rich diagnostics — this fires on multi-GPU slot warmup failures
// (Blackwell debugging). Identify WHICH precondition broke:
// wrong current device, empty blob (failed allocation), null dest
// buffer, or a pre-existing sticky async error.
int curDev = -1; cudaGetDevice(&curDev);
std::cout << "Error: Failed to copy input " << i std::cout << "Error: Failed to copy input " << i
<< " to inference buffer: " << cudaGetErrorString(copyErr) << std::endl; << " to inference buffer: " << cudaGetErrorString(copyErr) << "\n"
<< " [diag] engine deviceIndex = " << m_options.deviceIndex
<< ", thread current device = " << curDev << "\n"
<< " [diag] src blob ptr = " << static_cast<const void*>(blobMat.data)
<< " (" << blobMat.rows << "x" << blobMat.cols
<< ", elemSize " << blobMat.elemSize() << ")\n"
<< " [diag] dst buffer ptr = " << m_buffers[i]
<< ", copySize = " << copySize << " bytes\n"
<< " [diag] inference stream = " << static_cast<const void*>(m_inferenceStream)
<< ", pending lastError = " << cudaGetErrorString(cudaGetLastError())
<< std::endl;
return false; return false;
} }
} }
@@ -207,6 +207,21 @@ std::string Engine<T>::serializeEngineOptions(const ANSCENTER::Options& options,
+ std::to_string(CUDNN_MAJOR) + "." + std::to_string(CUDNN_MAJOR) + "."
+ std::to_string(CUDNN_MINOR); + std::to_string(CUDNN_MINOR);
// -- Blackwell aux-stream workaround marker --------------------------------
// On SM 12.x the builder forces maxAuxStreams=0 (see EngineBuildLoadNetwork.inl:
// TRT 10.15 Myelin faults with concurrent contexts + aux streams). Tag those
// engines so caches built *before* the workaround (with aux streams baked in)
// miss and rebuild automatically instead of loading and crashing.
#if NV_TENSORRT_MAJOR >= 10
{
cudaDeviceProp prop{};
if (cudaGetDeviceProperties(&prop, options.deviceIndex) == cudaSuccess
&& prop.major >= 12) {
engineName += ".aux0";
}
}
#endif
return engineName; return engineName;
} }
@@ -231,6 +246,32 @@ cv::cuda::GpuMat Engine<T>::blobFromGpuMats(const std::vector<cv::cuda::GpuMat>
thread_local cv::cuda::GpuMat tl_blob; thread_local cv::cuda::GpuMat tl_blob;
thread_local cv::cuda::GpuMat tl_floatImg; thread_local cv::cuda::GpuMat tl_floatImg;
thread_local int tl_blobMaxElems = 0; thread_local int tl_blobMaxElems = 0;
thread_local int tl_blobDevice = -1;
// DEVICE AFFINITY: the cached GpuMats live on ONE device. When the same
// thread serves engines on different GPUs (multi-GPU pool creation warms
// probe on GPU A, then slots on GPU B), reusing a device-A blob with a
// device-B stream causes invalid-argument launches and an async illegal
// memory access that permanently poisons both CUDA contexts (sticky 700).
// Detect the device switch and rebuild the cache on the current device.
{
int curDev = -1;
cudaGetDevice(&curDev);
if (curDev != tl_blobDevice) {
if (tl_blobDevice >= 0) {
// Free on the owning device — the async allocator frees via the
// CURRENT device's null-stream pool, so switch, release, switch back.
cudaSetDevice(tl_blobDevice);
tl_blob.release();
tl_floatImg.release();
cudaSetDevice(curDev);
ANS_DBG("TRT_Preproc", "blobFromGpuMats: device switch GPU[%d]->GPU[%d], preproc cache rebuilt",
tl_blobDevice, curDev);
}
tl_blobMaxElems = 0;
tl_blobDevice = curDev;
}
}
if (totalElems > tl_blobMaxElems) { if (totalElems > tl_blobMaxElems) {
tl_blob = cv::cuda::GpuMat(1, totalElems, CV_32FC1); tl_blob = cv::cuda::GpuMat(1, totalElems, CV_32FC1);
+96
View File
@@ -0,0 +1,96 @@
#pragma once
// ============================================================================
// GpuMemoryGuard — process-wide GPU VRAM safeguard.
//
// Purpose: prevent the out-of-memory cascade that happens when many TensorRT /
// CUDA engines (FR-SCRFD, OD-YOLO, LPR, liveness, ORT-CUDA, OpenCV-CUDA) share
// one GPU and collectively exhaust VRAM. The first cudaMalloc/stream OOM
// poisons the CUDA context ("permanently corrupted after N failures"), after
// which every GPU op fails and the service appears dead until restart.
//
// Three coordinated layers (all process-wide, thread-safe, lock-free):
// (1) CanLoad(need) — admission control: refuse to load an engine on GPU
// if it won't fit while keeping a safety margin free.
// (2) UnderPressure() — pressure guard: skip a frame's GPU work when free
// VRAM is below the margin (or breaker is open).
// (3) NoteOom()/Breaker— circuit breaker: after a caught OOM, open a short
// cooldown so callers back off instead of hammering
// the fragile context (which is what corrupts it).
//
// Margin default = 768 MiB; override at runtime with env ANSCENTER_GPU_MARGIN_MB.
// All queries operate on the CURRENT CUDA device (callers cudaSetDevice first).
// ============================================================================
#include <cuda_runtime.h>
#include <atomic>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
namespace ANSCENTER {
class GpuMemoryGuard {
public:
// ---- configuration ----------------------------------------------------
static size_t Margin() { return s_marginBytes.load(std::memory_order_relaxed); }
static void SetMargin(size_t b) { s_marginBytes.store(b, std::memory_order_relaxed); }
// ---- (1) admission control at load ------------------------------------
// True if 'needBytes' can be committed on the current device while keeping
// the safety margin free. If VRAM can't be queried, returns true (never
// block on a failed query — let the normal path handle it).
static bool CanLoad(size_t needBytes) {
size_t freeB = 0, totalB = 0;
if (!Query(freeB, totalB)) return true;
return freeB >= needBytes + Margin();
}
// ---- (2) pressure guard at inference ----------------------------------
// True if this frame should SKIP GPU work: free VRAM below margin, or the
// circuit breaker is currently open.
static bool UnderPressure() {
if (BreakerOpen()) return true;
size_t freeB = 0, totalB = 0;
if (!Query(freeB, totalB)) return false;
return freeB < Margin();
}
// ---- (3) OOM-safe alloc + circuit breaker -----------------------------
static bool IsOom(cudaError_t e) { return e == cudaErrorMemoryAllocation; }
// Open the breaker for a short cooldown after a (recoverable) OOM so all
// callers back off together instead of repeatedly faulting the context.
static void NoteOom() {
s_breakerUntilMs.store(NowMs() + kCooldownMs, std::memory_order_relaxed);
}
static bool BreakerOpen() {
return NowMs() < s_breakerUntilMs.load(std::memory_order_relaxed);
}
// Lightweight current-device VRAM query. Clears the non-fatal sticky error
// that a failed query can leave on the context.
static bool Query(size_t& freeB, size_t& totalB) {
cudaError_t e = cudaMemGetInfo(&freeB, &totalB);
cudaGetLastError();
return e == cudaSuccess;
}
private:
static int64_t NowMs() {
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count();
}
static size_t InitMargin() {
if (const char* e = std::getenv("ANSCENTER_GPU_MARGIN_MB")) {
long mb = std::atol(e);
if (mb > 0) return static_cast<size_t>(mb) * 1024ULL * 1024ULL;
}
return 768ULL * 1024 * 1024; // 768 MiB
}
static constexpr int64_t kCooldownMs = 1500; // breaker cooldown after OOM
inline static std::atomic<size_t> s_marginBytes{ InitMargin() };
inline static std::atomic<int64_t> s_breakerUntilMs{ 0 };
};
} // namespace ANSCENTER
@@ -162,7 +162,7 @@ bool PaddleOCRV5Engine::Initialize(const std::string& detModelPath,
// Ensure this DLL's copy of Ort::Global<void>::api_ is initialized. // Ensure this DLL's copy of Ort::Global<void>::api_ is initialized.
// BasicOrtHandler sets it in ONNXEngine.dll, but each DLL has its own // BasicOrtHandler sets it in ONNXEngine.dll, but each DLL has its own
// inline-static copy. Without this, inference calls from ANSOCR.dll crash. // inline-static copy. Without this, inference calls from ANSOCR.dll crash.
if (Ort::Global<void>::api_ == nullptr) { if (Ort::detail::Global::Api() == nullptr) {
Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw())); Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw()));
} }
+17
View File
@@ -761,6 +761,16 @@ namespace ANSCENTER
float CosineSimilarity(const std::vector<float>& a, const std::vector<float>& b, bool normalized); float CosineSimilarity(const std::vector<float>& a, const std::vector<float>& b, bool normalized);
}; };
class ANSENGINE_API ANSODBase { class ANSENGINE_API ANSODBase {
public:
// Explicit non-copy/non-move: _modelLoading (std::atomic) deletes the
// implicit copy ops; spelling them out here gives ClangCL a clean
// anchor and documents that subclasses are never copied/moved (they
// are heap-allocated and passed by shared_ptr<...>).
ANSODBase(const ANSODBase&) = delete;
ANSODBase& operator=(const ANSODBase&) = delete;
ANSODBase(ANSODBase&&) = delete;
ANSODBase& operator=(ANSODBase&&) = delete;
ANSODBase() = default;
protected: protected:
bool _licenseValid{ false }; bool _licenseValid{ false };
bool _isInitialized{ false }; bool _isInitialized{ false };
@@ -1085,6 +1095,13 @@ namespace ANSCENTER
std::vector<Object> RunInferenceInTrackingMode(const cv::Mat& input, const std::string& camera_id, std::vector<int> trackingObjectIds);// Get detected objects std::vector<Object> RunInferenceInTrackingMode(const cv::Mat& input, const std::string& camera_id, std::vector<int> trackingObjectIds);// Get detected objects
}; };
class ANSENGINE_API ANSFDBase { class ANSENGINE_API ANSFDBase {
public:
// Explicit non-copy/non-move (see ANSODBase for rationale).
ANSFDBase(const ANSFDBase&) = delete;
ANSFDBase& operator=(const ANSFDBase&) = delete;
ANSFDBase(ANSFDBase&&) = delete;
ANSFDBase& operator=(ANSFDBase&&) = delete;
ANSFDBase() = default;
protected: protected:
bool _licenseValid{ false }; bool _licenseValid{ false };
bool _isInitialized{ false }; bool _isInitialized{ false };
@@ -1316,7 +1316,7 @@ namespace ANSCENTER
{ {
try { try {
const auto& ep = ANSCENTER::EPLoader::Current(); const auto& ep = ANSCENTER::EPLoader::Current();
if (Ort::Global<void>::api_ == nullptr) if (Ort::detail::Global::Api() == nullptr)
Ort::InitApi(static_cast<const OrtApi*>(ANSCENTER::EPLoader::GetOrtApiRaw())); Ort::InitApi(static_cast<const OrtApi*>(ANSCENTER::EPLoader::GetOrtApiRaw()));
std::cout << "[ANSFDBase] EP ready: "<< ANSCENTER::EPLoader::EngineTypeName(ep.type) << std::endl; std::cout << "[ANSFDBase] EP ready: "<< ANSCENTER::EPLoader::EngineTypeName(ep.type) << std::endl;
+1 -1
View File
@@ -61,7 +61,7 @@ namespace ANSCENTER
inputImageShape_ = targetInputShape; inputImageShape_ = targetInputShape;
const auto& ep = ANSCENTER::EPLoader::Current(); const auto& ep = ANSCENTER::EPLoader::Current();
if (Ort::Global<void>::api_ == nullptr) if (Ort::detail::Global::Api() == nullptr)
Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw())); Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw()));
std::cout << "[ANSONNXCL] EP ready: " std::cout << "[ANSONNXCL] EP ready: "
<< ANSCENTER::EPLoader::EngineTypeName(ep.type) << std::endl; << ANSCENTER::EPLoader::EngineTypeName(ep.type) << std::endl;
+7
View File
@@ -25,6 +25,13 @@ namespace ANSCENTER {
// ──────────────────────────────────────────────────────────────────────── // ────────────────────────────────────────────────────────────────────────
class ANSENGINE_API ANSONNXCL :public ANSODBase { class ANSENGINE_API ANSONNXCL :public ANSODBase {
public: public:
// Explicit non-copy/non-move (see ANSONNXYOLO for dllexport rationale).
ANSONNXCL(const ANSONNXCL&) = delete;
ANSONNXCL& operator=(const ANSONNXCL&) = delete;
ANSONNXCL(ANSONNXCL&&) = delete;
ANSONNXCL& operator=(ANSONNXCL&&) = delete;
ANSONNXCL() = default;
virtual bool Initialize(std::string licenseKey, ModelConfig modelConfig, const std::string& modelZipFilePath, const std::string& modelZipPassword, std::string& labelMap) override; virtual bool Initialize(std::string licenseKey, ModelConfig modelConfig, const std::string& modelZipFilePath, const std::string& modelZipPassword, std::string& labelMap) override;
virtual bool LoadModel(const std::string& modelZipFilePath, const std::string& modelZipPassword)override; virtual bool LoadModel(const std::string& modelZipFilePath, const std::string& modelZipPassword)override;
virtual bool LoadModelFromFolder(std::string licenseKey, ModelConfig modelConfig, std::string modelName, std::string className, const std::string& modelFolder, std::string& labelMap)override; virtual bool LoadModelFromFolder(std::string licenseKey, ModelConfig modelConfig, std::string modelName, std::string className, const std::string& modelFolder, std::string& labelMap)override;
+1 -1
View File
@@ -692,7 +692,7 @@ namespace ANSCENTER {
deviceId_ = deviceId; deviceId_ = deviceId;
const auto& ep = ANSCENTER::EPLoader::Current(); const auto& ep = ANSCENTER::EPLoader::Current();
if (Ort::Global<void>::api_ == nullptr) if (Ort::detail::Global::Api() == nullptr)
Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw())); Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw()));
std::cout << "[ANSONNXOBB] EP ready: " std::cout << "[ANSONNXOBB] EP ready: "
<< ANSCENTER::EPLoader::EngineTypeName(ep.type) << std::endl; << ANSCENTER::EPLoader::EngineTypeName(ep.type) << std::endl;
+1 -1
View File
@@ -276,7 +276,7 @@ namespace ANSCENTER {
deviceId_ = deviceId; deviceId_ = deviceId;
const auto& ep = ANSCENTER::EPLoader::Current(); const auto& ep = ANSCENTER::EPLoader::Current();
if (Ort::Global<void>::api_ == nullptr) if (Ort::detail::Global::Api() == nullptr)
Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw())); Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw()));
std::cout << "[ANSONNXPOSE] EP ready: " std::cout << "[ANSONNXPOSE] EP ready: "
<< ANSCENTER::EPLoader::EngineTypeName(ep.type) << std::endl; << ANSCENTER::EPLoader::EngineTypeName(ep.type) << std::endl;
+7
View File
@@ -108,6 +108,13 @@ namespace ANSCENTER {
// ==================================================================== // ====================================================================
class ANSENGINE_API ANSONNXRTMPOSE : public ANSODBase { class ANSENGINE_API ANSONNXRTMPOSE : public ANSODBase {
public: public:
// Explicit non-copy/non-move (see ANSONNXYOLO for dllexport rationale).
ANSONNXRTMPOSE(const ANSONNXRTMPOSE&) = delete;
ANSONNXRTMPOSE& operator=(const ANSONNXRTMPOSE&) = delete;
ANSONNXRTMPOSE(ANSONNXRTMPOSE&&) = delete;
ANSONNXRTMPOSE& operator=(ANSONNXRTMPOSE&&) = delete;
ANSONNXRTMPOSE() = default;
bool Initialize(std::string licenseKey, ModelConfig modelConfig, bool Initialize(std::string licenseKey, ModelConfig modelConfig,
const std::string& modelZipFilePath, const std::string& modelZipFilePath,
const std::string& modelZipPassword, const std::string& modelZipPassword,
+1 -1
View File
@@ -204,7 +204,7 @@ namespace ANSCENTER
} }
try { try {
m_engine = std::make_unique<ONNXSAM3>(modelFolder, EngineType::NVIDIA_GPU); m_engine = std::make_unique<class ONNXSAM3>(modelFolder, EngineType::NVIDIA_GPU);
m_tokenLength = m_engine->getTokenLength(); m_tokenLength = m_engine->getTokenLength();
_logger.LogDebug("ANSONNXSAM3::InitEngine", _logger.LogDebug("ANSONNXSAM3::InitEngine",
+11 -1
View File
@@ -16,6 +16,13 @@ namespace ANSCENTER
class ANSENGINE_API ANSONNXSAM3 : public ANSODBase class ANSENGINE_API ANSONNXSAM3 : public ANSODBase
{ {
public: public:
// Explicit non-copy/non-move (see ANSONNXYOLO for dllexport rationale).
ANSONNXSAM3(const ANSONNXSAM3&) = delete;
ANSONNXSAM3& operator=(const ANSONNXSAM3&) = delete;
ANSONNXSAM3(ANSONNXSAM3&&) = delete;
ANSONNXSAM3& operator=(ANSONNXSAM3&&) = delete;
ANSONNXSAM3() = default;
virtual bool Initialize(std::string licenseKey, ModelConfig modelConfig, virtual bool Initialize(std::string licenseKey, ModelConfig modelConfig,
const std::string& modelZipFilePath, const std::string& modelZipPassword, const std::string& modelZipFilePath, const std::string& modelZipPassword,
std::string& labelMap) override; std::string& labelMap) override;
@@ -45,7 +52,10 @@ namespace ANSCENTER
bool _fp16 = false; bool _fp16 = false;
// ONNX Runtime engine // ONNX Runtime engine
std::unique_ptr<ONNXSAM3> m_engine; // Elaborated `class ONNXSAM3` disambiguates from the
// ANSCENTER::ModelType::ONNXSAM3 enumerator (ANSLicense.h) which
// shares the same unqualified name in this namespace.
std::unique_ptr<class ONNXSAM3> m_engine;
// Tokenizer (loaded from merges.txt) // Tokenizer (loaded from merges.txt)
std::unique_ptr<ANSCLIPTokenizer> m_tokenizer; std::unique_ptr<ANSCLIPTokenizer> m_tokenizer;
+1 -1
View File
@@ -273,7 +273,7 @@ namespace ANSCENTER {
deviceId_ = deviceId; deviceId_ = deviceId;
const auto& ep = ANSCENTER::EPLoader::Current(); const auto& ep = ANSCENTER::EPLoader::Current();
if (Ort::Global<void>::api_ == nullptr) if (Ort::detail::Global::Api() == nullptr)
Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw())); Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw()));
std::cout << "[ANSONNXSEG] EP ready: " std::cout << "[ANSONNXSEG] EP ready: "
<< ANSCENTER::EPLoader::EngineTypeName(ep.type) << std::endl; << ANSCENTER::EPLoader::EngineTypeName(ep.type) << std::endl;
+5 -5
View File
@@ -129,7 +129,7 @@ namespace ANSCENTER {
// future ORT shape-mismatch failure (e.g. /model.12/Concat for // future ORT shape-mismatch failure (e.g. /model.12/Concat for
// non-stride-32 inputs) points at the offending HxW directly. // non-stride-32 inputs) points at the offending HxW directly.
// One line per session — does not flood DebugView. // One line per session — does not flood DebugView.
static thread_local const ONNXYOLO* s_loggedFirst = nullptr; static thread_local const class ONNXYOLO* s_loggedFirst = nullptr;
if (s_loggedFirst != this) { if (s_loggedFirst != this) {
s_loggedFirst = this; s_loggedFirst = this;
ANS_DBG("ONNXYOLO", ANS_DBG("ONNXYOLO",
@@ -1680,7 +1680,7 @@ namespace ANSCENTER {
"Model file does not exist: " + _modelFilePath, __FILE__, __LINE__); "Model file does not exist: " + _modelFilePath, __FILE__, __LINE__);
return false; return false;
} }
m_ortEngine = std::make_shared<ONNXYOLO>(_modelFilePath); m_ortEngine = std::make_shared<class ONNXYOLO>(_modelFilePath);
return true; return true;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
@@ -1696,7 +1696,7 @@ namespace ANSCENTER {
"Model file does not exist: " + _modelFilePath, __FILE__, __LINE__); "Model file does not exist: " + _modelFilePath, __FILE__, __LINE__);
return false; return false;
} }
m_ortEngine = std::make_shared<ONNXYOLO>(_modelFilePath, engineType); m_ortEngine = std::make_shared<class ONNXYOLO>(_modelFilePath, engineType);
return true; return true;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
@@ -2244,7 +2244,7 @@ namespace ANSCENTER {
int numKps; int numKps;
std::vector<std::string> classes; std::vector<std::string> classes;
bool trackerEnabled, stabilizationEnabled; bool trackerEnabled, stabilizationEnabled;
std::shared_ptr<ONNXYOLO> engine; std::shared_ptr<class ONNXYOLO> engine;
{ {
ANS_DBG("ONNXYOLO", "DetectObjects: cam=%s acquiring mutex...", camera_id.c_str()); ANS_DBG("ONNXYOLO", "DetectObjects: cam=%s acquiring mutex...", camera_id.c_str());
auto lk = TryLockWithTimeout("ANSONNXYOLO::DetectObjects"); auto lk = TryLockWithTimeout("ANSONNXYOLO::DetectObjects");
@@ -2446,7 +2446,7 @@ namespace ANSCENTER {
int numKps; int numKps;
std::vector<std::string> classes; std::vector<std::string> classes;
bool trackerEnabled, stabilizationEnabled; bool trackerEnabled, stabilizationEnabled;
std::shared_ptr<ONNXYOLO> engine; std::shared_ptr<class ONNXYOLO> engine;
{ {
auto lk = TryLockWithTimeout("ANSONNXYOLO::DetectObjectsBatch"); auto lk = TryLockWithTimeout("ANSONNXYOLO::DetectObjectsBatch");
if (!lk.owns_lock()) return {}; if (!lk.owns_lock()) return {};
+14 -1
View File
@@ -205,6 +205,16 @@ namespace ANSCENTER {
// ==================================================================== // ====================================================================
class ANSENGINE_API ANSONNXYOLO : public ANSODBase { class ANSENGINE_API ANSONNXYOLO : public ANSODBase {
public: public:
// Explicit non-copy/non-move. The class has atomic + shared_ptr members
// that already implicitly delete these, but __declspec(dllexport)
// makes ClangCL eagerly instantiate the implicit copy/move ctor
// bodies; an explicit user-declared =delete short-circuits that.
ANSONNXYOLO(const ANSONNXYOLO&) = delete;
ANSONNXYOLO& operator=(const ANSONNXYOLO&) = delete;
ANSONNXYOLO(ANSONNXYOLO&&) = delete;
ANSONNXYOLO& operator=(ANSONNXYOLO&&) = delete;
ANSONNXYOLO() = default;
bool Initialize(std::string licenseKey, ModelConfig modelConfig, bool Initialize(std::string licenseKey, ModelConfig modelConfig,
const std::string& modelZipFilePath, const std::string& modelZipFilePath,
const std::string& modelZipPassword, const std::string& modelZipPassword,
@@ -254,7 +264,10 @@ namespace ANSCENTER {
// caught a DML E_FAIL and rebuilt the session — that was the // caught a DML E_FAIL and rebuilt the session — that was the
// 0xc0000005 access violation observed in ANSODEngine.dll under // 0xc0000005 access violation observed in ANSODEngine.dll under
// AMD/DirectML when multiple AI tasks ran in parallel. // AMD/DirectML when multiple AI tasks ran in parallel.
std::shared_ptr<ONNXYOLO> m_ortEngine; // Elaborated `class ONNXYOLO` disambiguates from the
// ANSCENTER::ModelType::ONNXYOLO enumerator (ANSLicense.h) which
// shares the same unqualified name in this namespace.
std::shared_ptr<class ONNXYOLO> m_ortEngine;
// ──────────────────────────────────────────────────────────────── // ────────────────────────────────────────────────────────────────
// DML stay-on-GPU recovery state. // DML stay-on-GPU recovery state.
+1 -1
View File
@@ -281,7 +281,7 @@ namespace ANSCENTER {
{ {
try { try {
const auto& ep = ANSCENTER::EPLoader::Current(); const auto& ep = ANSCENTER::EPLoader::Current();
if (Ort::Global<void>::api_ == nullptr) if (Ort::detail::Global::Api() == nullptr)
Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw())); Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw()));
std::cout << "[YOLO12OD] EP ready: " std::cout << "[YOLO12OD] EP ready: "
<< ANSCENTER::EPLoader::EngineTypeName(ep.type) << std::endl; << ANSCENTER::EPLoader::EngineTypeName(ep.type) << std::endl;
+1 -1
View File
@@ -208,7 +208,7 @@ namespace ANSCENTER
{ {
try { try {
const auto& ep = ANSCENTER::EPLoader::Current(); const auto& ep = ANSCENTER::EPLoader::Current();
if (Ort::Global<void>::api_ == nullptr) if (Ort::detail::Global::Api() == nullptr)
Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw())); Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw()));
std::cout << "[YOLOOD] EP ready: " std::cout << "[YOLOOD] EP ready: "
<< ANSCENTER::EPLoader::EngineTypeName(ep.type) << std::endl; << ANSCENTER::EPLoader::EngineTypeName(ep.type) << std::endl;
@@ -2,6 +2,7 @@
#include "ANSGpuFrameRegistry.h" #include "ANSGpuFrameRegistry.h"
#include "ANSEngineCommon.h" #include "ANSEngineCommon.h"
#include "ANSLicense.h" // ANS_DBG macro #include "ANSLicense.h" // ANS_DBG macro
#include "GpuMemoryGuard.h"
#include <opencv2/cudaimgproc.hpp> #include <opencv2/cudaimgproc.hpp>
#include <opencv2/cudawarping.hpp> #include <opencv2/cudawarping.hpp>
#include <opencv2/core/cuda_stream_accessor.hpp> #include <opencv2/core/cuda_stream_accessor.hpp>
@@ -168,6 +169,12 @@ namespace ANSCENTER {
} }
++m_cudaFailStreak; ++m_cudaFailStreak;
// (3) If this failure is a GPU OOM, open the process-wide circuit
// breaker so every engine (FR/OD/OCR/LPR) backs off via UnderPressure()
// before the streak reaches CUDA_FAIL_LIMIT and the context is lost.
if (GpuMemoryGuard::IsOom(err) || GpuMemoryGuard::IsOom(pending)) {
GpuMemoryGuard::NoteOom();
}
if (m_cudaFailStreak >= CUDA_FAIL_LIMIT && !m_cudaContextDead) { if (m_cudaFailStreak >= CUDA_FAIL_LIMIT && !m_cudaContextDead) {
m_cudaContextDead = true; m_cudaContextDead = true;
logger.LogFatal(std::string(tag) + "::NV12Helper", logger.LogFatal(std::string(tag) + "::NV12Helper",
+20
View File
@@ -2,6 +2,7 @@
#include "ANSGpuFrameRegistry.h" #include "ANSGpuFrameRegistry.h"
#include "NV12PreprocessHelper.h" // tl_currentGpuFrame() #include "NV12PreprocessHelper.h" // tl_currentGpuFrame()
#include "Utility.h" #include "Utility.h"
#include "GpuMemoryGuard.h"
#include <chrono> #include <chrono>
//#define FNS_DEBUG //#define FNS_DEBUG
namespace ANSCENTER { namespace ANSCENTER {
@@ -638,6 +639,17 @@ namespace ANSCENTER {
// Phase 2: CUDA preprocessing + inference (mutex released — pool dispatches to idle GPU slot) // Phase 2: CUDA preprocessing + inference (mutex released — pool dispatches to idle GPU slot)
std::vector<std::vector<cv::cuda::GpuMat>> inputs; std::vector<std::vector<cv::cuda::GpuMat>> inputs;
bool usedNV12 = false; bool usedNV12 = false;
// (2) Pressure guard: if free GPU VRAM is below the safety margin (or the
// OOM circuit breaker is open), SKIP this frame's GPU work instead of
// allocating and triggering an OOM that permanently poisons the context.
if (ANSCENTER::GpuMemoryGuard::UnderPressure()) {
this->_logger.LogWarn("ANSSCRFDFD::Detect",
"GPU under memory pressure — skipping SCRFD frame to avoid OOM",
__FILE__, __LINE__);
return {};
}
try { try {
// Clear any sticky CUDA error from transient graph-capture failures // Clear any sticky CUDA error from transient graph-capture failures
cudaError_t priorErr = cudaGetLastError(); cudaError_t priorErr = cudaGetLastError();
@@ -706,6 +718,14 @@ namespace ANSCENTER {
m_nv12Helper.tickInference(); m_nv12Helper.tickInference();
} }
catch (const std::exception& e) { catch (const std::exception& e) {
// (3) On a GPU OOM, open the circuit breaker so subsequent frames
// back off (UnderPressure) instead of repeatedly faulting the
// already-fragile CUDA context — that repetition is what corrupts it.
const std::string msg = e.what();
if (msg.find("out of memory") != std::string::npos ||
msg.find("Gpu API call") != std::string::npos) {
ANSCENTER::GpuMemoryGuard::NoteOom();
}
this->_logger.LogError( this->_logger.LogError(
"ANSSCRFDFD::Detect", "ANSSCRFDFD::Detect",
std::string("CUDA preprocessing failed: ") + e.what(), std::string("CUDA preprocessing failed: ") + e.what(),
+3 -1
View File
@@ -240,7 +240,9 @@ public:
void clearGpuBuffers(); void clearGpuBuffers();
int getBindingIndexByName(const std::string& name); int getBindingIndexByName(const std::string& name);
void UpdateOptions(const ANSCENTER::Options& options) { m_options = options; } void UpdateOptions(const ANSCENTER::Options& options) { m_options = options; }
void warmUp(int iterations = 5); // Returns false if any warmup inference failed — callers must treat the
// engine as unusable (a slot that fails warmup must NOT join the pool).
bool warmUp(int iterations = 5);
void setVerbose(bool v) { m_verbose = v; } void setVerbose(bool v) { m_verbose = v; }
void setDisableGraphs(bool v) { m_disableGraphs = v; } void setDisableGraphs(bool v) { m_disableGraphs = v; }
+785
View File
@@ -1,6 +1,8 @@
#include "ANSUtilities.h" #include "ANSUtilities.h"
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
#include <winevt.h> // EvtQuery / EvtRender for GetANSLoggerErrorEvents
#pragma comment(lib, "wevtapi.lib")
#endif #endif
#include <iostream> #include <iostream>
#include <CkFileAccess.h> #include <CkFileAccess.h>
@@ -1309,5 +1311,788 @@ namespace ANSCENTER
} }
return result; return result;
} }
// ====================================================================
// GetANSLoggerErrorEvents / GetANSLoggerErrorEventsSinceLastCall
//
// Reads every Error-level event written by the "ANSLogger" source in
// the Application channel using the modern winevt API. Events are
// produced by WindowsEventSink in ANSLicensingSystem (see
// ANSLicense.cpp::ReportEventW) — each carries a single inserted
// string in <EventData><Data>...</Data></EventData>.
//
// DebugView: filter "[EVTLOG]".
// ====================================================================
// ---- Watermark helpers (HKCU, no admin needed) ----------------------
static constexpr const wchar_t* kWatermarkRegKey =
L"Software\\ANSCENTER\\ANSUtilities\\EventLogWatermark";
static constexpr const wchar_t* kWatermarkValueName = L"ANSLogger";
static uint64_t ReadAnsLoggerWatermark() {
HKEY hKey = nullptr;
if (RegOpenKeyExW(HKEY_CURRENT_USER, kWatermarkRegKey, 0,
KEY_READ, &hKey) != ERROR_SUCCESS) {
return 0;
}
uint64_t value = 0;
DWORD size = sizeof(value), type = 0;
LONG rc = RegQueryValueExW(hKey, kWatermarkValueName, nullptr,
&type, reinterpret_cast<LPBYTE>(&value),
&size);
RegCloseKey(hKey);
return (rc == ERROR_SUCCESS && type == REG_QWORD) ? value : 0;
}
static void WriteAnsLoggerWatermark(uint64_t value) {
HKEY hKey = nullptr;
if (RegCreateKeyExW(HKEY_CURRENT_USER, kWatermarkRegKey, 0,
nullptr, REG_OPTION_NON_VOLATILE, KEY_WRITE,
nullptr, &hKey, nullptr) != ERROR_SUCCESS) {
ANS_DBG("EVTLOG", "watermark RegCreateKey failed err=%lu",
GetLastError());
return;
}
LONG rc = RegSetValueExW(hKey, kWatermarkValueName, 0, REG_QWORD,
reinterpret_cast<const BYTE*>(&value),
sizeof(value));
if (rc != ERROR_SUCCESS) {
ANS_DBG("EVTLOG", "watermark RegSetValue failed err=%ld", rc);
}
RegCloseKey(hKey);
}
// Extracts <EventRecordID>NNN</EventRecordID> from a rendered event XML.
// Returns 0 if not present (shouldn't happen — every Windows event has one).
static uint64_t ExtractRecordIdFromXml(const std::wstring& xml) {
auto open = xml.find(L"<EventRecordID>");
if (open == std::wstring::npos) return 0;
open += 15; // length of "<EventRecordID>"
auto close = xml.find(L"</EventRecordID>", open);
if (close == std::wstring::npos) return 0;
std::wstring numStr = xml.substr(open, close - open);
return std::wcstoull(numStr.c_str(), nullptr, 10);
}
// Returns the RecordID of the most recently written event in `channel`.
// Used to detect channel clears: if our stored watermark > channelMax,
// the channel was cleared and the new event counter restarted at 1, so
// we must reset our watermark or risk skipping all new events.
static uint64_t GetChannelMaxRecordId(const wchar_t* channel) {
EVT_HANDLE hQuery = EvtQuery(nullptr, channel, nullptr,
EvtQueryChannelPath | EvtQueryReverseDirection);
if (!hQuery) return 0;
EVT_HANDLE hEvent = nullptr;
DWORD returned = 0;
uint64_t maxId = 0;
if (EvtNext(hQuery, 1, &hEvent, 1000, 0, &returned) && returned > 0) {
std::vector<wchar_t> buf(4096);
DWORD used = 0, propCount = 0;
BOOL ok = EvtRender(nullptr, hEvent, EvtRenderEventXml,
static_cast<DWORD>(buf.size() * sizeof(wchar_t)),
buf.data(), &used, &propCount);
if (!ok && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
buf.resize(used / sizeof(wchar_t) + 1);
ok = EvtRender(nullptr, hEvent, EvtRenderEventXml,
static_cast<DWORD>(buf.size() * sizeof(wchar_t)),
buf.data(), &used, &propCount);
}
if (ok) maxId = ExtractRecordIdFromXml(std::wstring(buf.data()));
EvtClose(hEvent);
}
EvtClose(hQuery);
return maxId;
}
// ---- Shared implementation -----------------------------------------
// Runs `query` against the Application channel, formats events as
// "<DD/MM/YYYY h:mm:ss tt>: <event data>\n"
// and optionally clears the channel afterwards. If trackWatermark is
// true, also persists the highest EventRecordID seen to HKCU on return.
static std::string ReadAnsLoggerEventsImpl(const wchar_t* query,
bool clearAfterRead,
bool trackWatermark)
{
EVT_HANDLE hResults = EvtQuery(nullptr, L"Application", query,
EvtQueryChannelPath | EvtQueryForwardDirection);
if (!hResults) {
ANS_DBG("EVTLOG", "EvtQuery failed err=%lu", GetLastError());
return std::string();
}
std::string out;
uint64_t highestRecordId = 0;
constexpr DWORD kBatch = 64;
EVT_HANDLE events[kBatch];
DWORD returned = 0;
// Render-buffer reused across events to avoid per-event allocation.
std::vector<wchar_t> buf(8192);
while (EvtNext(hResults, kBatch, events, INFINITE, 0, &returned)) {
for (DWORD i = 0; i < returned; ++i) {
DWORD used = 0, propCount = 0;
if (!EvtRender(nullptr, events[i], EvtRenderEventXml,
static_cast<DWORD>(buf.size() * sizeof(wchar_t)),
buf.data(), &used, &propCount)) {
DWORD lastErr = GetLastError();
if (lastErr == ERROR_INSUFFICIENT_BUFFER) {
buf.resize(used / sizeof(wchar_t) + 1);
if (!EvtRender(nullptr, events[i], EvtRenderEventXml,
static_cast<DWORD>(buf.size() * sizeof(wchar_t)),
buf.data(), &used, &propCount)) {
EvtClose(events[i]);
continue;
}
} else {
ANS_DBG("EVTLOG", "EvtRender failed err=%lu", lastErr);
EvtClose(events[i]);
continue;
}
}
std::wstring xml(buf.data());
// Track the highest RecordID we've seen this call so we can
// persist it as the new watermark on exit. Done before the
// continue-on-malformed-XML paths below so an unrenderable
// event still advances the watermark.
if (trackWatermark) {
uint64_t rid = ExtractRecordIdFromXml(xml);
if (rid > highestRecordId) highestRecordId = rid;
}
// ---- TimeCreated SystemTime='2026-05-18T11:52:07.123Z' ----
// EvtRender produces either single- or double-quoted attrs
// depending on Windows version, so handle both.
std::string formattedTime;
auto tcPos = xml.find(L"TimeCreated SystemTime=");
if (tcPos != std::wstring::npos) {
tcPos += 23; // length of "TimeCreated SystemTime="
if (tcPos < xml.size() &&
(xml[tcPos] == L'"' || xml[tcPos] == L'\'')) {
wchar_t quote = xml[tcPos];
++tcPos;
auto endQuote = xml.find(quote, tcPos);
if (endQuote != std::wstring::npos) {
std::wstring iso = xml.substr(tcPos, endQuote - tcPos);
int Y = 0, M = 0, D = 0, h = 0, m = 0, s = 0;
if (swscanf_s(iso.c_str(), L"%d-%d-%dT%d:%d:%d",
&Y, &M, &D, &h, &m, &s) == 6) {
SYSTEMTIME utc{};
utc.wYear = (WORD)Y; utc.wMonth = (WORD)M; utc.wDay = (WORD)D;
utc.wHour = (WORD)h; utc.wMinute = (WORD)m; utc.wSecond = (WORD)s;
SYSTEMTIME local{};
if (!SystemTimeToTzSpecificLocalTime(nullptr, &utc, &local)) {
local = utc; // fallback: show UTC
}
wchar_t dateBuf[64] = {}, timeBuf[64] = {};
GetDateFormatW(LOCALE_USER_DEFAULT, 0, &local, nullptr,
dateBuf, _countof(dateBuf));
GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &local, nullptr,
timeBuf, _countof(timeBuf));
std::wstring combined = std::wstring(dateBuf) + L" " + timeBuf;
int need = WideCharToMultiByte(CP_UTF8, 0,
combined.c_str(), -1, nullptr, 0, nullptr, nullptr);
if (need > 1) {
formattedTime.resize(need - 1);
WideCharToMultiByte(CP_UTF8, 0, combined.c_str(), -1,
formattedTime.data(), need, nullptr, nullptr);
}
} // close: if (swscanf_s ... == 6)
} // close: if (endQuote != npos)
} // close: if (xml[tcPos] is quote)
} // close: if (tcPos != npos)
// ---- Concatenate every <Data>...</Data> inside <EventData> ----
// ReportEventW with N strings produces N consecutive <Data>
// elements. ANSLogger sends just one but we handle the general
// case so the function works for other sources too.
std::wstring wdata;
auto edStart = xml.find(L"<EventData");
auto edEnd = xml.find(L"</EventData>", edStart);
if (edStart != std::wstring::npos && edEnd != std::wstring::npos) {
auto bodyEnd = edEnd;
auto cursor = edStart;
while (cursor < bodyEnd) {
auto dOpen = xml.find(L"<Data", cursor);
if (dOpen == std::wstring::npos || dOpen >= bodyEnd) break;
auto dGt = xml.find(L'>', dOpen);
if (dGt == std::wstring::npos || dGt >= bodyEnd) break;
auto dClose = xml.find(L"</Data>", dGt);
if (dClose == std::wstring::npos || dClose > bodyEnd) break;
if (!wdata.empty()) wdata += L' ';
wdata.append(xml, dGt + 1, dClose - dGt - 1);
cursor = dClose + sizeof("</Data>") - 1;
}
}
std::string utf8Data;
if (!wdata.empty()) {
int need = WideCharToMultiByte(CP_UTF8, 0, wdata.c_str(), -1,
nullptr, 0, nullptr, nullptr);
if (need > 1) {
utf8Data.resize(need - 1);
WideCharToMultiByte(CP_UTF8, 0, wdata.c_str(), -1,
utf8Data.data(), need, nullptr, nullptr);
}
}
// Flatten embedded CR/LF in the message body so the
// "one line per event" contract holds. ChilkatLog and
// similar multi-line payloads otherwise break callers
// that split on '\n'.
for (char& c : utf8Data) if (c == '\r' || c == '\n') c = ' ';
out += formattedTime;
out += ": ";
out += utf8Data;
out += '\n';
EvtClose(events[i]);
}
}
DWORD lastErr = GetLastError();
if (lastErr != ERROR_NO_MORE_ITEMS) {
ANS_DBG("EVTLOG", "EvtNext stopped err=%lu", lastErr);
}
EvtClose(hResults);
// Persist watermark BEFORE clearing — once EvtClearLog succeeds,
// future events restart at RecordID 1, and we'd never see them
// again if the watermark stayed at e.g. 547.
// Special case: if clear is also going to succeed, the new
// watermark is irrelevant (we'll reset on next call via the
// GetChannelMaxRecordId check), so writing it here is harmless.
if (trackWatermark && highestRecordId > 0) {
WriteAnsLoggerWatermark(highestRecordId);
ANS_DBG("EVTLOG", "watermark updated to %llu", highestRecordId);
}
if (clearAfterRead) {
// Clears the entire Application channel — see header warning.
// DIAGNOSTIC: also print to stderr so callers without DebugView
// can see what's going on. Common failure codes:
// 5 ERROR_ACCESS_DENIED — process is not elevated
// 1314 ERROR_PRIVILEGE_NOT_HELD — token missing SeSecurityPriv
// 87 ERROR_INVALID_PARAMETER — channel name wrong
if (!EvtClearLog(nullptr, L"Application", nullptr, 0)) {
DWORD e = GetLastError();
ANS_DBG("EVTLOG", "EvtClearLog failed err=%lu (need admin)", e);
std::fprintf(stderr,
"[ANSUtilities] EvtClearLog(Application) FAILED, "
"GetLastError=%lu (5=AccessDenied, 1314=PrivNotHeld)\n", e);
} else {
ANS_DBG("EVTLOG", "EvtClearLog: Application channel cleared");
std::fprintf(stderr,
"[ANSUtilities] EvtClearLog(Application) OK\n");
// Clear succeeded → counter resets to 1, so our watermark
// is stale. Reset it now so the next call doesn't skip
// legitimate new events whose RecordID < old watermark.
if (trackWatermark) {
WriteAnsLoggerWatermark(0);
ANS_DBG("EVTLOG", "watermark reset to 0 after clear");
}
}
}
return out;
}
// ---- Public: full read, no skip ------------------------------------
std::string ANSUtilities::GetANSLoggerErrorEvents(bool clearAfterRead) {
// XPath: Provider name "ANSLogger" AND Level 2 (EVENTLOG_ERROR_TYPE).
// Forward direction keeps chronological insertion order so output
// matches a fresh "oldest first" scroll through Event Viewer.
return ReadAnsLoggerEventsImpl(
L"*[System[Provider[@Name='ANSLogger'] and Level=2]]",
clearAfterRead,
/*trackWatermark=*/false);
}
// ---- Public: incremental read using HKCU watermark -----------------
std::string ANSUtilities::GetANSLoggerErrorEventsSinceLastCall(bool clearAfterRead) {
uint64_t watermark = ReadAnsLoggerWatermark();
// Channel-clear detection: if the channel's current max RecordID
// is lower than our stored watermark, the log was cleared (by us
// or by something else like Event Viewer's "Clear Log") and the
// counter restarted at 1. Reset the watermark so we don't skip
// events 1..watermark that are actually fresh.
uint64_t channelMax = GetChannelMaxRecordId(L"Application");
if (channelMax > 0 && watermark > channelMax) {
ANS_DBG("EVTLOG", "watermark reset: stored=%llu channelMax=%llu "
"(channel was cleared externally)",
watermark, channelMax);
watermark = 0;
WriteAnsLoggerWatermark(0);
}
// Embed the watermark in the XPath so Windows does the filtering;
// we never see events <= watermark, which is more efficient than
// post-filtering thousands of events on the client side.
wchar_t queryBuf[256];
swprintf_s(queryBuf, _countof(queryBuf),
L"*[System[Provider[@Name='ANSLogger'] and Level=2 and "
L"(EventRecordID>%llu)]]",
watermark);
ANS_DBG("EVTLOG", "GetANSLoggerErrorEventsSinceLastCall watermark=%llu",
watermark);
return ReadAnsLoggerEventsImpl(queryBuf, clearAfterRead,
/*trackWatermark=*/true);
}
} // namespace ANSCENTER
// ============================================================================
// ScanDevices — AIBOX / ANSVIS discovery on the local LAN
//
// Port of the AIBOX Flutter app's MulticastService
// (lib/src/core/network/multicast/multicast_service.dart). AIBOX boxes and
// ANSVIS servers answer the same UDP probe and the same /ServerApi HTTP
// endpoint, so one scan finds both. DebugView: filter "[ANSDISC]".
//
// Wire protocol:
// UDP: probe text "Hello, Multicast" -> 230.50.200.150:58839 and subnet
// broadcast :58839. Devices reply to UDP/58838 with
// "<a>:<b>:<deviceName>:<firmware>".
// HTTP: GET http://<ip>/ServerApi -> 200 + JSON with
// {"OK": true, "Version": "<firmware>"}.
//
// Implementation: a UDP listener thread + a periodic UDP sender + a 64-way
// HTTP sweep populate one mutex-guarded map. After `timeoutMs` the map is
// converted to a deduplicated vector and returned.
// ============================================================================
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <winhttp.h>
#include <mutex>
#include <condition_variable>
#include <unordered_map>
#include <chrono>
#include <queue>
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "winhttp.lib")
namespace {
// ---- Protocol constants (mirror AIBOX MulticastService) -----------------
constexpr const char* kMulticastGroup = "230.50.200.150";
constexpr unsigned short kProbePort = 58839; // device listens here
constexpr unsigned short kListenPort = 58838; // we listen here
constexpr const char* kProbeMessage = "Hello, Multicast";
constexpr int kMulticastTtl = 225;
constexpr int kHttpConcurrency = 64;
constexpr int kHttpConnectTimeoutMs = 700;
constexpr int kHttpProbeTimeoutMs = 2500;
constexpr int kUdpProbeIntervalMs = 2000;
constexpr const wchar_t* kHttpUserAgent = L"ANSUtilities-Scan/1.0";
constexpr const wchar_t* kHttpPath = L"/ServerApi";
// HTTP ports answered by AIBOX boxes / ANSVIS servers. AIBOX listens on the
// default HTTP port; the ANSVIS LabVIEW web server (App_serverConfig.httpPort
// in ANSVIS.lvproj) listens on 22211. Each host gets probed on both ports.
constexpr unsigned short kPortAibox = 80;
constexpr unsigned short kPortAnsvis = 22211;
constexpr unsigned short kSweepPorts[] = { kPortAibox, kPortAnsvis };
// ---- Winsock init (refcounted; safe to call repeatedly) -----------------
struct WsaScope {
bool ok{false};
WsaScope() {
WSADATA d{};
ok = (WSAStartup(MAKEWORD(2, 2), &d) == 0);
}
~WsaScope() { if (ok) WSACleanup(); }
WsaScope(const WsaScope&) = delete;
WsaScope& operator=(const WsaScope&) = delete;
};
// ---- Local IPv4 interfaces ----------------------------------------------
struct LocalSubnet {
std::string prefix; // "192.168.1." (final dot included)
in_addr localAddr{};
in_addr broadcast{};
};
std::vector<LocalSubnet> EnumerateLocalSubnets() {
std::vector<LocalSubnet> out;
ULONG bufLen = 16 * 1024;
std::vector<char> buf(bufLen);
DWORD rc = GetAdaptersAddresses(AF_INET,
GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER,
nullptr, reinterpret_cast<PIP_ADAPTER_ADDRESSES>(buf.data()), &bufLen);
if (rc == ERROR_BUFFER_OVERFLOW) {
buf.resize(bufLen);
rc = GetAdaptersAddresses(AF_INET,
GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER,
nullptr, reinterpret_cast<PIP_ADAPTER_ADDRESSES>(buf.data()), &bufLen);
}
if (rc != NO_ERROR) {
ANS_DBG("ANSDISC", "GetAdaptersAddresses failed rc=%lu", static_cast<unsigned long>(rc));
return out;
}
auto* adapter = reinterpret_cast<PIP_ADAPTER_ADDRESSES>(buf.data());
for (; adapter; adapter = adapter->Next) {
if (adapter->OperStatus != IfOperStatusUp) continue;
if (adapter->IfType == IF_TYPE_SOFTWARE_LOOPBACK) continue;
for (auto* ua = adapter->FirstUnicastAddress; ua; ua = ua->Next) {
if (!ua->Address.lpSockaddr || ua->Address.lpSockaddr->sa_family != AF_INET) continue;
auto* sin = reinterpret_cast<sockaddr_in*>(ua->Address.lpSockaddr);
// Skip APIPA 169.254.x.x — multicast on link-local rarely useful.
unsigned char b0 = reinterpret_cast<unsigned char*>(&sin->sin_addr)[0];
unsigned char b1 = reinterpret_cast<unsigned char*>(&sin->sin_addr)[1];
if (b0 == 169 && b1 == 254) continue;
if (b0 == 127) continue;
char ipStr[INET_ADDRSTRLEN] = {};
inet_ntop(AF_INET, &sin->sin_addr, ipStr, sizeof(ipStr));
std::string s(ipStr);
auto lastDot = s.find_last_of('.');
if (lastDot == std::string::npos) continue;
LocalSubnet net;
net.prefix = s.substr(0, lastDot + 1);
net.localAddr = sin->sin_addr;
inet_pton(AF_INET, (net.prefix + "255").c_str(), &net.broadcast);
out.push_back(net);
ANS_DBG("ANSDISC", "iface ip=%s subnet=%s/24", ipStr, net.prefix.c_str());
}
}
return out;
} }
// ---- Parse "<a>:<b>:<name>:<firmware>" reply ----------------------------
// parts[0] and parts[1] are not used by the AIBOX Flutter app but the device
// still sends them. Observed devices put their listen port in one of those
// two fields, so we try both — first numeric wins. Unparseable -> port = 0.
void ParseUdpReply(const std::string& body, std::string& name,
std::string& firmware, int& port) {
std::vector<std::string> parts;
size_t start = 0;
for (size_t i = 0; i <= body.size(); ++i) {
if (i == body.size() || body[i] == ':') {
parts.emplace_back(body.data() + start, i - start);
start = i + 1;
}
}
if (parts.size() > 3) { name = parts[2]; firmware = parts[3]; }
else { name = "Unknown"; firmware = "-"; }
port = 0;
auto tryPort = [&](const std::string& s) {
if (port != 0 || s.empty() || s.size() > 5) return;
for (char c : s) if (c < '0' || c > '9') return;
int v = std::atoi(s.c_str());
if (v > 0 && v < 65536) port = v;
};
if (parts.size() > 0) tryPort(parts[0]);
if (parts.size() > 1) tryPort(parts[1]);
}
// ---- Parse {"OK":true,"Version":"x"} ------------------------------------
// Tiny scan rather than a JSON dependency. Looks for the two keys with any
// amount of whitespace; rejects anything that doesn't say "OK":true.
bool ParseServerApiJson(const std::string& body, std::string& version) {
auto pos = body.find("\"OK\"");
if (pos == std::string::npos) return false;
pos = body.find(':', pos);
if (pos == std::string::npos) return false;
++pos;
while (pos < body.size() && (body[pos] == ' ' || body[pos] == '\t')) ++pos;
if (body.compare(pos, 4, "true") != 0) return false;
version.clear();
pos = body.find("\"Version\"");
if (pos == std::string::npos) { version = "-"; return true; }
pos = body.find(':', pos);
if (pos == std::string::npos) { version = "-"; return true; }
pos = body.find('"', pos);
if (pos == std::string::npos) { version = "-"; return true; }
++pos;
auto end = body.find('"', pos);
if (end == std::string::npos) { version = "-"; return true; }
version = body.substr(pos, end - pos);
if (version.empty()) version = "-";
return true;
}
// ---- Shared scan state --------------------------------------------------
enum SourceMask { kSrcMulticast = 1, kSrcSweep = 2 };
struct ScanState {
std::mutex mu;
std::unordered_map<std::string, ANSCENTER::ANSDeviceInfo> found;
std::atomic<bool> stop{false};
void merge(const std::string& ip, const std::string& name,
const std::string& firmware, int port, int srcBit) {
std::lock_guard<std::mutex> lk(mu);
auto& d = found[ip];
if (d.ip.empty()) d.ip = ip;
// Prefer non-empty / non-"-" values, regardless of which source ran first.
if (!name.empty() && d.name.empty()) d.name = name;
if (!firmware.empty() && firmware != "-" &&
(d.firmware.empty() || d.firmware == "-")) d.firmware = firmware;
else if (!firmware.empty() && d.firmware.empty()) d.firmware = firmware;
// Port precedence: an HTTP probe proves the port actually responds,
// so a sweep-supplied port overwrites whatever UDP parsing guessed.
// A UDP-supplied port is only kept if HTTP hasn't filled it in.
if (port > 0) {
const bool fromHttp = (srcBit == kSrcSweep);
const bool fromHttpAlready = (d.source & kSrcSweep) != 0;
if (fromHttp || !fromHttpAlready) d.port = port;
}
d.source |= srcBit;
}
};
// ---- UDP listener thread -------------------------------------------------
// Single socket bound to 0.0.0.0:58838, joined to 230.50.200.150 on every
// local interface. Reads with a short timeout so it can poll `stop`.
void RunUdpListener(ScanState& state, const std::vector<LocalSubnet>& subnets) {
SOCKET s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (s == INVALID_SOCKET) {
ANS_DBG("ANSDISC", "UDP socket() failed err=%d", WSAGetLastError());
return;
}
BOOL on = TRUE;
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<char*>(&on), sizeof(on));
setsockopt(s, SOL_SOCKET, SO_BROADCAST, reinterpret_cast<char*>(&on), sizeof(on));
sockaddr_in bindAddr{};
bindAddr.sin_family = AF_INET;
bindAddr.sin_port = htons(kListenPort);
bindAddr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(s, reinterpret_cast<sockaddr*>(&bindAddr), sizeof(bindAddr)) != 0) {
ANS_DBG("ANSDISC", "UDP bind :%u failed err=%d", kListenPort, WSAGetLastError());
closesocket(s);
return;
}
// Join multicast group on every interface so we receive replies on any path.
for (const auto& net : subnets) {
ip_mreq mreq{};
inet_pton(AF_INET, kMulticastGroup, &mreq.imr_multiaddr);
mreq.imr_interface = net.localAddr;
if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP,
reinterpret_cast<char*>(&mreq), sizeof(mreq)) != 0) {
ANS_DBG("ANSDISC", "IP_ADD_MEMBERSHIP on %.*s failed err=%d",
static_cast<int>(net.prefix.size() - 1), net.prefix.c_str(),
WSAGetLastError());
}
}
char buf[2048];
sockaddr_in from{};
int fromLen = sizeof(from);
while (!state.stop.load(std::memory_order_relaxed)) {
fd_set rfds; FD_ZERO(&rfds); FD_SET(s, &rfds);
timeval tv{ 0, 200 * 1000 }; // 200 ms
int sel = select(0, &rfds, nullptr, nullptr, &tv);
if (sel <= 0) continue;
int n = recvfrom(s, buf, sizeof(buf), 0,
reinterpret_cast<sockaddr*>(&from), &fromLen);
if (n <= 0) continue;
std::string body(buf, n);
if (body == kProbeMessage) continue; // our own echo
char ipStr[INET_ADDRSTRLEN] = {};
inet_ntop(AF_INET, &from.sin_addr, ipStr, sizeof(ipStr));
std::string name, fw;
int port = 0;
ParseUdpReply(body, name, fw, port);
ANS_DBG("ANSDISC", "UDP reply from %s name=%s fw=%s port=%d raw=%s",
ipStr, name.c_str(), fw.c_str(), port, body.c_str());
state.merge(ipStr, name, fw, port, kSrcMulticast);
}
closesocket(s);
}
// ---- UDP sender thread ---------------------------------------------------
// Sends "Hello, Multicast" to the multicast group out each interface and
// to each subnet broadcast. Repeats every kUdpProbeIntervalMs.
void RunUdpSender(ScanState& state, const std::vector<LocalSubnet>& subnets) {
SOCKET s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (s == INVALID_SOCKET) {
ANS_DBG("ANSDISC", "sender socket() failed err=%d", WSAGetLastError());
return;
}
BOOL on = TRUE;
setsockopt(s, SOL_SOCKET, SO_BROADCAST, reinterpret_cast<char*>(&on), sizeof(on));
int ttl = kMulticastTtl;
setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL,
reinterpret_cast<char*>(&ttl), sizeof(ttl));
sockaddr_in mc{};
mc.sin_family = AF_INET;
mc.sin_port = htons(kProbePort);
inet_pton(AF_INET, kMulticastGroup, &mc.sin_addr);
auto sendOnce = [&]() {
for (const auto& net : subnets) {
// Pin multicast egress to this interface so it leaves each NIC.
setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF,
reinterpret_cast<const char*>(&net.localAddr),
sizeof(net.localAddr));
sendto(s, kProbeMessage, static_cast<int>(strlen(kProbeMessage)), 0,
reinterpret_cast<sockaddr*>(&mc), sizeof(mc));
sockaddr_in bc{};
bc.sin_family = AF_INET;
bc.sin_port = htons(kProbePort);
bc.sin_addr = net.broadcast;
sendto(s, kProbeMessage, static_cast<int>(strlen(kProbeMessage)), 0,
reinterpret_cast<sockaddr*>(&bc), sizeof(bc));
}
};
sendOnce();
auto next = std::chrono::steady_clock::now() + std::chrono::milliseconds(kUdpProbeIntervalMs);
while (!state.stop.load(std::memory_order_relaxed)) {
auto now = std::chrono::steady_clock::now();
if (now >= next) {
sendOnce();
next = now + std::chrono::milliseconds(kUdpProbeIntervalMs);
} else {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
closesocket(s);
}
// ---- One synchronous HTTP probe via WinHTTP -----------------------------
// On AIBOX/ANSVIS reply: out_version is set and returns true. Otherwise false.
bool ProbeHttp(const std::string& ip, unsigned short port, std::string& out_version) {
HINTERNET hSession = WinHttpOpen(kHttpUserAgent,
WINHTTP_ACCESS_TYPE_NO_PROXY,
WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
if (!hSession) return false;
WinHttpSetTimeouts(hSession,
kHttpConnectTimeoutMs, kHttpConnectTimeoutMs,
kHttpProbeTimeoutMs, kHttpProbeTimeoutMs);
std::wstring host(ip.begin(), ip.end());
HINTERNET hConn = WinHttpConnect(hSession, host.c_str(),
static_cast<INTERNET_PORT>(port), 0);
if (!hConn) { WinHttpCloseHandle(hSession); return false; }
HINTERNET hReq = WinHttpOpenRequest(hConn, L"GET", kHttpPath,
nullptr, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
if (!hReq) {
WinHttpCloseHandle(hConn); WinHttpCloseHandle(hSession);
return false;
}
bool ok = false;
if (WinHttpSendRequest(hReq, L"Accept: application/json\r\n", static_cast<DWORD>(-1L),
WINHTTP_NO_REQUEST_DATA, 0, 0, 0) &&
WinHttpReceiveResponse(hReq, nullptr))
{
DWORD status = 0, statusSize = sizeof(status);
if (WinHttpQueryHeaders(hReq,
WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,
WINHTTP_HEADER_NAME_BY_INDEX, &status, &statusSize,
WINHTTP_NO_HEADER_INDEX) && status == 200)
{
std::string body;
DWORD avail = 0;
// Cap body at 64 KB — /ServerApi is a few hundred bytes.
while (body.size() < 65536 && WinHttpQueryDataAvailable(hReq, &avail) && avail) {
std::vector<char> buf(avail);
DWORD read = 0;
if (!WinHttpReadData(hReq, buf.data(), avail, &read)) break;
body.append(buf.data(), read);
}
ok = ParseServerApiJson(body, out_version);
}
}
WinHttpCloseHandle(hReq);
WinHttpCloseHandle(hConn);
WinHttpCloseHandle(hSession);
return ok;
}
// ---- HTTP sweep: 64-way worker pool consuming a queue of (ip, port) -----
// Each host is probed on every port in kSweepPorts (80 + 22211) so one
// sweep covers both AIBOX and ANSVIS.
void RunHttpSweep(ScanState& state, const std::vector<LocalSubnet>& subnets) {
struct Target { std::string ip; unsigned short port; };
std::queue<Target> q;
for (const auto& net : subnets) {
for (int h = 1; h <= 254; ++h) {
std::string ip = net.prefix + std::to_string(h);
for (unsigned short p : kSweepPorts) q.push({ ip, p });
}
}
ANS_DBG("ANSDISC", "HTTP sweep targets=%zu workers=%d", q.size(), kHttpConcurrency);
std::mutex qmu;
auto worker = [&]() {
for (;;) {
if (state.stop.load(std::memory_order_relaxed)) return;
Target t;
{
std::lock_guard<std::mutex> lk(qmu);
if (q.empty()) return;
t = std::move(q.front());
q.pop();
}
std::string ver;
if (ProbeHttp(t.ip, t.port, ver)) {
ANS_DBG("ANSDISC", "HTTP hit %s:%u version=%s",
t.ip.c_str(), static_cast<unsigned>(t.port), ver.c_str());
state.merge(t.ip, std::string(), ver, static_cast<int>(t.port), kSrcSweep);
}
}
};
std::vector<std::thread> pool;
pool.reserve(kHttpConcurrency);
for (int i = 0; i < kHttpConcurrency; ++i) pool.emplace_back(worker);
for (auto& t : pool) t.join();
}
} // anonymous namespace
namespace ANSCENTER {
std::vector<ANSDeviceInfo> ANSUtilities::ScanDevices(int timeoutMs) {
if (timeoutMs < 500) timeoutMs = 500; // floor: enough for one UDP burst
ANS_DBG("ANSDISC", "ScanDevices: enter timeoutMs=%d", timeoutMs);
WsaScope wsa;
if (!wsa.ok) {
ANS_DBG("ANSDISC", "WSAStartup failed");
return {};
}
auto subnets = EnumerateLocalSubnets();
if (subnets.empty()) {
ANS_DBG("ANSDISC", "no usable IPv4 interfaces; aborting scan");
return {};
}
ScanState state;
std::thread tListen([&]{ RunUdpListener(state, subnets); });
std::thread tSend ([&]{ RunUdpSender (state, subnets); });
std::thread tSweep ([&]{ RunHttpSweep (state, subnets); });
std::this_thread::sleep_for(std::chrono::milliseconds(timeoutMs));
state.stop.store(true, std::memory_order_relaxed);
tListen.join();
tSend.join();
tSweep.join();
std::vector<ANSDeviceInfo> result;
{
std::lock_guard<std::mutex> lk(state.mu);
result.reserve(state.found.size());
for (auto& kv : state.found) result.push_back(std::move(kv.second));
}
ANS_DBG("ANSDISC", "ScanDevices: exit found=%zu", result.size());
return result;
}
} // namespace ANSCENTER
+85
View File
@@ -20,6 +20,16 @@
#include <functional> #include <functional>
namespace ANSCENTER { namespace ANSCENTER {
// Discovered AIBOX / ANSVIS server on the local network.
struct ANSDeviceInfo {
std::string ip; // IPv4 address (dotted-decimal)
std::string name; // Device name from UDP probe, empty if HTTP-only
std::string firmware; // Version string ("-" if device did not report one)
int port{ 0 }; // TCP port the device's HTTP API answers on
// (AIBOX = 80, ANSVIS = 22211). 0 = unknown.
int source{ 0 }; // 1 = multicast, 2 = HTTP sweep, 3 = both
};
class ANSULT_API ANSUtilities { class ANSULT_API ANSUtilities {
private: private:
// Protects all shared mutable data // Protects all shared mutable data
@@ -144,6 +154,60 @@ namespace ANSCENTER {
// Unescape double-escaped Unicode: "Th\\u1ee7y" becomes "Th\u1ee7y". // Unescape double-escaped Unicode: "Th\\u1ee7y" becomes "Th\u1ee7y".
// Reverses DoubleEscapeUnicode so other languages can parse \uXXXX correctly. // Reverses DoubleEscapeUnicode so other languages can parse \uXXXX correctly.
static std::string UnescapeDoubleEscapedUnicode(const std::string& str); static std::string UnescapeDoubleEscapedUnicode(const std::string& str);
// Read every Error-level event from "ANSLogger" in the Windows
// Application event log. Output: one line per event in the form
// "<DD/MM/YYYY h:mm:ss tt>: <event data string>\n"
// Time is converted to the user's local timezone and formatted
// using LOCALE_USER_DEFAULT (matches Event Viewer's display).
//
// clearAfterRead=true behaviour:
// * Calls EvtClearLog on the entire Application channel after
// reading. WARNING: this wipes every event from every source
// in the Application log, not just ANSLogger entries — there
// is no per-event delete API in Windows.
// * REQUIRES THE CALLING PROCESS TO BE ELEVATED (run as
// Administrator). Without elevation, EvtClearLog returns
// ERROR_ACCESS_DENIED (Win32 error 5) and the events remain
// in the log. The read still succeeds either way.
// * Failures are reported on stderr in the form
// "[ANSUtilities] EvtClearLog(Application) FAILED, GetLastError=N"
// and via DebugView under the "EVTLOG" tag. The C++ return
// value does NOT signal clear success — capture stderr or
// check Event Viewer afterwards to confirm.
[[nodiscard]] static std::string GetANSLoggerErrorEvents(bool clearAfterRead = false);
// Same as GetANSLoggerErrorEvents but only returns events that
// have NOT been returned by a previous call from this user
// account. The highest EventRecordID seen is persisted to
// HKCU\Software\ANSCENTER\ANSUtilities\EventLogWatermark\ANSLogger
// (REG_QWORD) and used as a Windows XPath filter
// (EventRecordID > N) on the next call. No admin needed for the
// skip mechanism — only for the optional clearAfterRead.
//
// Channel-clear handling: if Windows resets the Application
// channel's RecordID counter (typical after EvtClearLog), this
// function detects that the channel's current max RecordID is
// less than the stored watermark and resets the watermark to 0
// so no newly-written events are accidentally skipped.
//
// To force a re-read of all events, delete the registry value
// above and call again.
[[nodiscard]] static std::string GetANSLoggerErrorEventsSinceLastCall(bool clearAfterRead = false);
// Scan the local network for AIBOX / ANSVIS servers.
// Runs two strategies in parallel and merges results by IP:
// 1) UDP multicast probe to 230.50.200.150:58839 (with subnet
// broadcast fallback). App listens on UDP/58838 for replies
// in the form "<a>:<b>:<deviceName>:<firmware>".
// 2) HTTP /24 subnet sweep on every local IPv4 interface, GETing
// http://<host>/ServerApi with up to 64 concurrent connections.
// A device is recognized when the JSON body contains
// {"OK": true, "Version": "<firmware>"}.
// `timeoutMs` is the maximum scan window (default 5000 ms). Shorter
// values return faster but may miss slow devices. The call blocks
// until the window elapses, then returns the deduplicated list.
[[nodiscard]] static std::vector<ANSDeviceInfo> ScanDevices(int timeoutMs = 5000);
}; };
// Connection bundle for pool // Connection bundle for pool
struct S3Connection { struct S3Connection {
@@ -345,6 +409,27 @@ extern "C" ANSULT_API int SendEmail(const char* smtpServer, int port,
extern "C" ANSULT_API int RebootSystem(); extern "C" ANSULT_API int RebootSystem();
// Discover AIBOX / ANSVIS servers on the local network.
// Returns a JSON array of objects: [{"ip":"...","name":"...","firmware":"...","source":3}, ...]
// `source` is 1 = multicast, 2 = HTTP sweep, 3 = both.
// `timeoutMs` caps the scan duration (typical: 3000-10000 ms).
extern "C" ANSULT_API int ScanANSDevices(int timeoutMs, LStrHandle resultJson);
extern "C" ANSULT_API int ScanANSDevicesCpp(int timeoutMs, std::string& resultJson);
// Read all Error-level events from "ANSLogger" in the Windows Application
// event log. Returns a UTF-8 string with one event per line:
// "<DD/MM/YYYY h:mm:ss tt>: <event data>\n"
// `clearAfterRead` != 0 clears the entire Application channel afterwards
// (requires Administrator; see header comment on the C++ method).
extern "C" ANSULT_API int GetANSLoggerErrorEvents(int clearAfterRead, LStrHandle result);
extern "C" ANSULT_API int GetANSLoggerErrorEventsCpp(int clearAfterRead, std::string& result);
// Watermarked variant: only returns events not seen by previous calls.
// Watermark is stored in HKCU\Software\ANSCENTER\ANSUtilities\EventLogWatermark\ANSLogger
// (REG_QWORD). No admin needed for the skip; only for clearAfterRead.
extern "C" ANSULT_API int GetANSLoggerErrorEventsSinceLastCall(int clearAfterRead, LStrHandle result);
extern "C" ANSULT_API int GetANSLoggerErrorEventsSinceLastCallCpp(int clearAfterRead, std::string& result);
// Unicode conversion utilities for LabVIEW // Unicode conversion utilities for LabVIEW
extern "C" ANSULT_API int ANSConvertUTF8ToUTF16LE(const char* utf8Str, LStrHandle result, int includeBOM = 1); extern "C" ANSULT_API int ANSConvertUTF8ToUTF16LE(const char* utf8Str, LStrHandle result, int includeBOM = 1);
extern "C" ANSULT_API int ANSDecodeJsonUnicodeToUTF16LE(const char* escapedStr, LStrHandle result); extern "C" ANSULT_API int ANSDecodeJsonUnicodeToUTF16LE(const char* escapedStr, LStrHandle result);
+4 -1
View File
@@ -21,7 +21,10 @@ target_link_libraries(ANSUtilities
) )
if(WIN32) if(WIN32)
target_link_libraries(ANSUtilities PRIVATE ${WIN_COMMON_LIBS} crypt32) # WIN_COMMON_LIBS covers ws2_32, iphlpapi, etc. winhttp is needed for the
# ScanDevices HTTP /ServerApi sweep. wevtapi is needed for
# GetANSLoggerErrorEvents (EvtQuery / EvtRender / EvtClearLog).
target_link_libraries(ANSUtilities PRIVATE ${WIN_COMMON_LIBS} crypt32 winhttp wevtapi)
endif() endif()
target_compile_definitions(ANSUtilities PRIVATE UNICODE _UNICODE ANSUTILITIES_EXPORTS) target_compile_definitions(ANSUtilities PRIVATE UNICODE _UNICODE ANSUTILITIES_EXPORTS)
+159
View File
@@ -2973,3 +2973,162 @@ extern "C" ANSULT_API int DownloadFileStreamANSAWSHandle_V2(uint64_t handleVal,
catch (std::exception& e) { return 0; } catch (std::exception& e) { return 0; }
catch (...) { return 0; } catch (...) { return 0; }
} }
// ---------------------------------------------------------------------------
// ScanANSDevices — discover AIBOX / ANSVIS servers on the local network.
// Returns 1 on success (resultJson populated, possibly with empty "[]"), 0
// on internal failure. The static C++ method does its own license/error
// handling; this wrapper just serializes the device list to JSON.
// ---------------------------------------------------------------------------
static std::string JsonEscape(const std::string& s) {
std::string out;
out.reserve(s.size() + 2);
for (char c : s) {
switch (c) {
case '"': out += "\\\""; break;
case '\\': out += "\\\\"; break;
case '\n': out += "\\n"; break;
case '\r': out += "\\r"; break;
case '\t': out += "\\t"; break;
default:
if (static_cast<unsigned char>(c) < 0x20) {
char buf[8];
snprintf(buf, sizeof(buf), "\\u%04x", static_cast<unsigned char>(c));
out += buf;
} else {
out += c;
}
}
}
return out;
}
static std::string DevicesToJson(const std::vector<ANSCENTER::ANSDeviceInfo>& devs) {
std::string j = "[";
for (size_t i = 0; i < devs.size(); ++i) {
if (i) j += ',';
j += "{\"ip\":\""; j += JsonEscape(devs[i].ip); j += "\",";
j += "\"name\":\""; j += JsonEscape(devs[i].name); j += "\",";
j += "\"firmware\":\""; j += JsonEscape(devs[i].firmware); j += "\",";
j += "\"port\":"; j += std::to_string(devs[i].port); j += ',';
j += "\"source\":"; j += std::to_string(devs[i].source);
j += '}';
}
j += ']';
return j;
}
extern "C" ANSULT_API int ScanANSDevicesCpp(int timeoutMs, std::string& resultJson) {
ANS_DBG("ANSUTIL", "ScanANSDevicesCpp: timeoutMs=%d", timeoutMs);
try {
auto devs = ANSCENTER::ANSUtilities::ScanDevices(timeoutMs);
resultJson = DevicesToJson(devs);
return 1;
}
catch (const std::exception& e) {
ANS_DBG("ANSUTIL", "ScanANSDevicesCpp threw: %s", e.what());
resultJson = "[]";
return 0;
}
catch (...) {
ANS_DBG("ANSUTIL", "ScanANSDevicesCpp threw unknown");
resultJson = "[]";
return 0;
}
}
extern "C" ANSULT_API int ScanANSDevices(int timeoutMs, LStrHandle resultJson) {
ANS_DBG("ANSUTIL", "ScanANSDevices: timeoutMs=%d", timeoutMs);
if (!resultJson) return -1;
try {
std::string j;
ScanANSDevicesCpp(timeoutMs, j);
int size = static_cast<int>(j.size());
MgErr error = DSSetHandleSize(resultJson, sizeof(int32) + size * sizeof(uChar));
if (error != noErr) return -2;
(*resultJson)->cnt = size;
if (size > 0) memcpy((*resultJson)->str, j.data(), size);
return 1;
}
catch (...) { return -1; }
}
// ---------------------------------------------------------------------------
// GetANSLoggerErrorEvents — read Error-level events from the ANSLogger source
// in the Windows Application event log. Returns 1 on success (string may be
// empty if no events match), 0 on internal failure.
// `clearAfterRead != 0` triggers EvtClearLog on the entire Application
// channel — see ANSUtilities.h header comment for caveats.
// ---------------------------------------------------------------------------
extern "C" ANSULT_API int GetANSLoggerErrorEventsCpp(int clearAfterRead, std::string& result) {
ANS_DBG("ANSUTIL", "GetANSLoggerErrorEventsCpp: clearAfterRead=%d", clearAfterRead);
try {
result = ANSCENTER::ANSUtilities::GetANSLoggerErrorEvents(clearAfterRead != 0);
return 1;
}
catch (const std::exception& e) {
ANS_DBG("ANSUTIL", "GetANSLoggerErrorEventsCpp threw: %s", e.what());
result.clear();
return 0;
}
catch (...) {
ANS_DBG("ANSUTIL", "GetANSLoggerErrorEventsCpp threw unknown");
result.clear();
return 0;
}
}
extern "C" ANSULT_API int GetANSLoggerErrorEvents(int clearAfterRead, LStrHandle result) {
ANS_DBG("ANSUTIL", "GetANSLoggerErrorEvents: clearAfterRead=%d", clearAfterRead);
if (!result) return -1;
try {
std::string s;
GetANSLoggerErrorEventsCpp(clearAfterRead, s);
int size = static_cast<int>(s.size());
MgErr error = DSSetHandleSize(result, sizeof(int32) + size * sizeof(uChar));
if (error != noErr) return -2;
(*result)->cnt = size;
if (size > 0) memcpy((*result)->str, s.data(), size);
return 1;
}
catch (...) { return -1; }
}
// ---------------------------------------------------------------------------
// GetANSLoggerErrorEventsSinceLastCall — watermark-based incremental read.
// Stores highest EventRecordID seen in HKCU; subsequent calls return only
// newer events. No admin needed for the skip (only for clearAfterRead).
// ---------------------------------------------------------------------------
extern "C" ANSULT_API int GetANSLoggerErrorEventsSinceLastCallCpp(int clearAfterRead, std::string& result) {
ANS_DBG("ANSUTIL", "GetANSLoggerErrorEventsSinceLastCallCpp: clearAfterRead=%d", clearAfterRead);
try {
result = ANSCENTER::ANSUtilities::GetANSLoggerErrorEventsSinceLastCall(clearAfterRead != 0);
return 1;
}
catch (const std::exception& e) {
ANS_DBG("ANSUTIL", "GetANSLoggerErrorEventsSinceLastCallCpp threw: %s", e.what());
result.clear();
return 0;
}
catch (...) {
ANS_DBG("ANSUTIL", "GetANSLoggerErrorEventsSinceLastCallCpp threw unknown");
result.clear();
return 0;
}
}
extern "C" ANSULT_API int GetANSLoggerErrorEventsSinceLastCall(int clearAfterRead, LStrHandle result) {
ANS_DBG("ANSUTIL", "GetANSLoggerErrorEventsSinceLastCall: clearAfterRead=%d", clearAfterRead);
if (!result) return -1;
try {
std::string s;
GetANSLoggerErrorEventsSinceLastCallCpp(clearAfterRead, s);
int size = static_cast<int>(s.size());
MgErr error = DSSetHandleSize(result, sizeof(int32) + size * sizeof(uChar));
if (error != noErr) return -2;
(*result)->cnt = size;
if (size > 0) memcpy((*result)->str, s.data(), size);
return 1;
}
catch (...) { return -1; }
}
+13 -4
View File
@@ -833,7 +833,7 @@ int RSTPTestCVClient() {
// std::string rtspUrl = "rtsp://admin:admin123@192.168.1.23:554";//"rtsp://alex:admin123@192.168.1.11:554";// // std::string rtspUrl = "rtsp://admin:admin123@192.168.1.23:554";//"rtsp://alex:admin123@192.168.1.11:554";//
//std::string rtspUrl = "rtsp://anscamuser:SonTay2020@anssontay.tplinkdns.com:554/Streaming/Channels/401";// "rtsp://admin:JA!uV_mx@42.1.117.99:8093/1/1"; //std::string rtspUrl = "rtsp://anscamuser:SonTay2020@anssontay.tplinkdns.com:554/Streaming/Channels/401";// "rtsp://admin:JA!uV_mx@42.1.117.99:8093/1/1";
std::string rtspUrl = "rtsp://admin:Grb%401212@14.224.237.228:15541/Streaming/channels/101";//"rtsp://admin:AnsHome16%&@192.168.1.104:554";//"rtsp://alex:admin123@192.168.1.11:554";//112 std::string rtspUrl ="rtsp://admin:AnsHome16%&@192.168.1.103:554";//"rtsp://alex:admin123@192.168.1.11:554";//112
CreateANSRTSPHandle(&rtspClient, "", "", "", rtspUrl.c_str()); CreateANSRTSPHandle(&rtspClient, "", "", "", rtspUrl.c_str());
//void SetRTSPImageQuality(ANSCENTER::ANSRTSPClient** Handle, int mode) //void SetRTSPImageQuality(ANSCENTER::ANSRTSPClient** Handle, int mode)
@@ -887,7 +887,10 @@ int RSTPTestCVClient() {
// ✅ Check if the image is valid BEFORE accessing it // ✅ Check if the image is valid BEFORE accessing it
if (!image || image->empty()) { if (!image || image->empty()) {
ANSCV_ReleaseImage_S(&image); ANSCV_ReleaseImage_S(&image);
std::this_thread::sleep_for(std::chrono::seconds(1)); // Wait via waitKey (not sleep) so the HighGUI window keeps pumping
// Windows messages — a raw sleep here makes the "Image" window go
// "Not Responding" whenever the stream stalls for a few seconds.
if (cv::waitKey(1000) == 27) break;
continue; // Skip processing if the image is empty continue; // Skip processing if the image is empty
} }
@@ -1060,7 +1063,10 @@ int FPTestCVClient() {
// ✅ Check if the image is valid BEFORE accessing it // ✅ Check if the image is valid BEFORE accessing it
if (!image || image->empty()) { if (!image || image->empty()) {
ANSCV_ReleaseImage_S(&image); ANSCV_ReleaseImage_S(&image);
std::this_thread::sleep_for(std::chrono::seconds(1)); // Wait via waitKey (not sleep) so the HighGUI window keeps pumping
// Windows messages — a raw sleep here makes the "Image" window go
// "Not Responding" whenever the stream stalls for a few seconds.
if (cv::waitKey(1000) == 27) break;
continue; // Skip processing if the image is empty continue; // Skip processing if the image is empty
} }
@@ -1549,7 +1555,10 @@ int VideoPlayerClientTest() {
// ✅ Check if the image is valid BEFORE accessing it // ✅ Check if the image is valid BEFORE accessing it
if (!image || image->empty()) { if (!image || image->empty()) {
ANSCV_ReleaseImage_S(&image); ANSCV_ReleaseImage_S(&image);
std::this_thread::sleep_for(std::chrono::seconds(1)); // Wait via waitKey (not sleep) so the HighGUI window keeps pumping
// Windows messages — a raw sleep here makes the "Image" window go
// "Not Responding" whenever the stream stalls for a few seconds.
if (cv::waitKey(1000) == 27) break;
continue; // Skip processing if the image is empty continue; // Skip processing if the image is empty
} }
+56 -33
View File
@@ -4,40 +4,63 @@ add_executable(ANSCV-UnitTest
OpenCVTest.cpp OpenCVTest.cpp
) )
target_include_directories(ANSCV-UnitTest PRIVATE # ANSCV Console Test — headless smoke test (no GUI/camera/disk), prints
${CMAKE_SOURCE_DIR}/modules/ANSCV # PASS/FAIL per check and exits with the failure count. Built as its own
${CMAKE_SOURCE_DIR}/MediaClient # executable so it does not collide with ANSCV-UnitTest's GUI main().
${CMAKE_SOURCE_DIR}/MediaClient/media add_executable(ANSCV-ConsoleTest
${CMAKE_SOURCE_DIR}/MediaClient/rtsp ConsoleTest.cpp
${CMAKE_SOURCE_DIR}/MediaClient/rtp
${CMAKE_SOURCE_DIR}/MediaClient/bm
${CMAKE_SOURCE_DIR}/MediaClient/http
${CMAKE_SOURCE_DIR}/MediaClient/directx
${CMAKE_SOURCE_DIR}/MediaClient/rtmp
${CMAKE_SOURCE_DIR}/MediaClient/librtmp
${CMAKE_SOURCE_DIR}/MediaClient/srt
${CMAKE_SOURCE_DIR}/MediaClient/ffmpeg/include
${CMAKE_SOURCE_DIR}/MediaClient/openssl/include
${CMAKE_SOURCE_DIR}/MediaClient/libsrt/include
${SHARED_INCLUDE_DIR}
${ANSLIBS_DIR}/TurboJpeg
${ANSLIBS_DIR}/QRCode
) )
target_link_libraries(ANSCV-UnitTest # ans_imshow.hpp lives outside the repo (shipped with ANSMIND). Default to the
PRIVATE ANSCV # standard install location but allow overriding on other machines/CI via
PRIVATE ANSLicensingSystem # -DANSMIND_INCLUDE_DIR=<path>. Warn (don't fail configure) if it's missing, so
PRIVATE anslicensing # unrelated targets still build; ConsoleTest will then fail to compile clearly.
PRIVATE labview set(ANSMIND_INCLUDE_DIR "C:/ProgramData/ANSCENTER/ANSMIND"
PRIVATE opencv CACHE PATH "Directory containing ans_imshow.hpp (shipped with ANSMIND)")
PRIVATE turbojpeg if(NOT EXISTS "${ANSMIND_INCLUDE_DIR}/ans_imshow.hpp")
PRIVATE zxing message(WARNING
PRIVATE CUDA::cudart "ans_imshow.hpp not found in ANSMIND_INCLUDE_DIR='${ANSMIND_INCLUDE_DIR}'. "
PRIVATE CUDA::nvjpeg "ANSCV-ConsoleTest will fail to compile. "
) "Set -DANSMIND_INCLUDE_DIR=<path> to the directory containing ans_imshow.hpp.")
if(WIN32)
target_link_libraries(ANSCV-UnitTest PRIVATE ${WIN_COMMON_LIBS})
endif() endif()
target_compile_definitions(ANSCV-UnitTest PRIVATE UNICODE _UNICODE) foreach(_anscv_test ANSCV-UnitTest ANSCV-ConsoleTest)
target_include_directories(${_anscv_test} PRIVATE
${CMAKE_SOURCE_DIR}/modules/ANSCV
${CMAKE_SOURCE_DIR}/MediaClient
${CMAKE_SOURCE_DIR}/MediaClient/media
${CMAKE_SOURCE_DIR}/MediaClient/rtsp
${CMAKE_SOURCE_DIR}/MediaClient/rtp
${CMAKE_SOURCE_DIR}/MediaClient/bm
${CMAKE_SOURCE_DIR}/MediaClient/http
${CMAKE_SOURCE_DIR}/MediaClient/directx
${CMAKE_SOURCE_DIR}/MediaClient/rtmp
${CMAKE_SOURCE_DIR}/MediaClient/librtmp
${CMAKE_SOURCE_DIR}/MediaClient/srt
${FFMPEG_INCLUDE_DIR} # FFmpeg 8.1 (n8.1); was vendored MediaClient/ffmpeg/include 4.0.2
${CMAKE_SOURCE_DIR}/MediaClient/openssl/include
${CMAKE_SOURCE_DIR}/MediaClient/libsrt/include
${SHARED_INCLUDE_DIR}
${ANSLIBS_DIR}/TurboJpeg
${ANSLIBS_DIR}/QRCode
${ANSMIND_INCLUDE_DIR} # ans_imshow.hpp (ConsoleTest.cpp)
)
target_link_libraries(${_anscv_test}
PRIVATE ANSCV
PRIVATE ANSLicensingSystem
PRIVATE anslicensing
PRIVATE labview
PRIVATE opencv
PRIVATE turbojpeg
PRIVATE zxing
PRIVATE CUDA::cudart
PRIVATE CUDA::nvjpeg
)
if(WIN32)
target_link_libraries(${_anscv_test} PRIVATE ${WIN_COMMON_LIBS})
endif()
target_compile_definitions(${_anscv_test} PRIVATE UNICODE _UNICODE)
endforeach()
+379
View File
@@ -0,0 +1,379 @@
// ANSCV Console Test
// ------------------------------------------------------------------
// A headless (no GUI) smoke test for ANSCENTER::ANSOPENCV image ops plus an
// optional RTSP capture test (ANSCENTER::ANSRTSPClient). It prints a PASS/FAIL
// line per check so results are visible in a plain console (and in CI).
// Exit code == number of failed checks (0 == all OK).
//
// Usage:
// ANSCV-ConsoleTest.exe # image ops only (no camera needed)
// ANSCV-ConsoleTest.exe <rtsp-url> # image ops + headless RTSP capture
// ANSCV-ConsoleTest.exe <rtsp-url> <maxFrames>
// ANSCV-ConsoleTest.exe <rtsp-url> <maxFrames> <saveDir>
// When <saveDir> is given:
// <saveDir>/frames/NNNN.jpg full-res captured frames (MP4 input)
// <saveDir>/preview_NNNN.jpg downscaled previews for quick viewing
// <saveDir>/rtsp_clip.mp4 short clip (NVENC, via ANSCV ImagesToMP4HW)
//
// This is intentionally separate from ANSCV-UnitTest.cpp, whose main()
// opens OpenCV windows (cv::imshow) for the same RTSP stream.
#include "ANSOpenCV.h"
#include "ANSRTSP.h"
#include <opencv2/opencv.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
#include <chrono>
#include <thread>
#include <filesystem>
#ifdef _WIN32
# define NOMINMAX
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#endif
using ANSCENTER::ANSOPENCV;
using ANSCENTER::ANSRTSPClient;
using ANSCENTER::DetectionObject;
namespace {
int g_pass = 0;
int g_fail = 0;
// Report a single check. `detail` is appended for context (sizes, counts, ...).
void check(const std::string& name, bool ok, const std::string& detail = "") {
if (ok) {
++g_pass;
std::cout << " [PASS] " << name;
} else {
++g_fail;
std::cout << " [FAIL] " << name;
}
if (!detail.empty()) std::cout << " (" << detail << ")";
std::cout << std::endl;
}
std::string dims(const cv::Mat& m) {
return std::to_string(m.cols) + "x" + std::to_string(m.rows) +
" ch=" + std::to_string(m.channels());
}
// A 640x480 BGR test image: smooth gradient plus a few filled shapes so the
// transforms operate on something with real structure (not a flat color).
cv::Mat MakeTestImage(int w = 640, int h = 480) {
cv::Mat img(h, w, CV_8UC3);
for (int y = 0; y < h; ++y) {
for (int x = 0; x < w; ++x) {
img.at<cv::Vec3b>(y, x) = cv::Vec3b(
static_cast<uchar>(x * 255 / w),
static_cast<uchar>(y * 255 / h),
static_cast<uchar>((x + y) * 255 / (w + h)));
}
}
cv::rectangle(img, cv::Rect(50, 50, 200, 150), cv::Scalar(0, 0, 255), -1);
cv::circle(img, cv::Point(450, 300), 80, cv::Scalar(0, 255, 0), -1);
return img;
}
// Headless RTSP capture test. Connects to `url`, pulls up to `maxFrames`
// frames (or until `timeoutMs` elapses), and reports resolution, frame count,
// JPEG sizes, per-grab latency and approximate FPS. No GUI — every frame is
// reported as text. Bounded so it always terminates even if the camera is
// unreachable. Adds PASS/FAIL checks to the global tally.
void RtspConsoleTest(const std::string& url, int maxFrames,
const std::string& saveDir = "", int timeoutMs = 15000) {
std::cout << "-- RTSP capture --" << std::endl;
std::cout << "URL: " << url << std::endl;
// Give slow/high-res streams enough wall-clock to reach maxFrames.
timeoutMs = (std::max)(timeoutMs, maxFrames * 1000);
std::cout << "Grabbing up to " << maxFrames << " frames (timeout "
<< timeoutMs << " ms)..." << std::endl;
// When saving, full-res frames go into <saveDir>/frames (clean, zero-padded
// names) so the MP4 encoder globs only those; previews go into <saveDir>.
std::string framesDir;
if (!saveDir.empty()) {
framesDir = saveDir + "/frames";
std::error_code ec;
std::filesystem::create_directories(framesDir, ec);
std::cout << "Saving frames to: " << saveDir << std::endl;
}
ANSOPENCV::InitCameraNetwork();
ANSRTSPClient* client = nullptr;
// CreateANSRTSPHandle(handle, licenseKey, username, password, url).
// Credentials embedded in the URL → pass empty user/pass, matching the
// existing RSTPTestClient() usage.
int created = CreateANSRTSPHandle(&client, "", "", "", url.c_str());
check("CreateANSRTSPHandle", client != nullptr,
"rc=" + std::to_string(created));
if (client == nullptr) {
ANSOPENCV::DeinitCameraNetwork();
return;
}
StartRTSP(&client);
int width = 0, height = 0;
int64_t pts = 0;
int frames = 0;
long long totalJpegBytes = 0;
long long totalGrabMs = 0;
int firstW = 0, firstH = 0;
int64_t firstPts = 0, lastPts = 0;
const auto t0 = std::chrono::steady_clock::now();
auto elapsedMs = [&] {
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - t0).count();
};
int polls = 0;
while (frames < maxFrames && elapsedMs() < timeoutMs) {
++polls;
std::string jpeg;
const auto g0 = std::chrono::steady_clock::now();
GetRTSPStrImage(&client, width, height, pts, jpeg);
const auto grabMs = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - g0).count();
if (jpeg.empty()) {
// No frame ready yet (connecting / between frames). Back off briefly.
std::this_thread::sleep_for(std::chrono::milliseconds(50));
continue;
}
if (frames == 0) { firstW = width; firstH = height; firstPts = pts; }
lastPts = pts;
++frames;
totalJpegBytes += static_cast<long long>(jpeg.size());
totalGrabMs += grabMs;
// Optionally persist the frame so it can be viewed outside the console.
// The grabbed bytes are already a JPEG, so the full-res file is a direct
// dump (no re-encode). The preview is decoded + downscaled for quick view.
if (!saveDir.empty()) {
// Zero-pad to 4 digits (lexicographic sort == capture order) without
// snprintf: some included headers #define snprintf -> _snprintf.
std::string idx = std::to_string(frames);
if (idx.size() < 4) idx.insert(0, 4 - idx.size(), '0');
// Full-res frame -> frames/ (MP4 input). Already JPEG: direct dump.
std::ofstream f(framesDir + "/" + idx + ".jpg", std::ios::binary);
f.write(jpeg.data(), static_cast<std::streamsize>(jpeg.size()));
f.close();
// Downscaled preview -> saveDir (chat-friendly viewing).
std::vector<uchar> buf(jpeg.begin(), jpeg.end());
cv::Mat full = cv::imdecode(buf, cv::IMREAD_COLOR);
if (!full.empty()) {
const int previewW = 960;
cv::Mat preview;
cv::resize(full, preview,
cv::Size(previewW, full.rows * previewW / full.cols));
cv::imwrite(saveDir + "/preview_" + idx + ".jpg", preview);
}
}
// Report every frame for the first few, then every 10th to stay readable.
if (frames <= 5 || frames % 10 == 0) {
std::cout << " frame " << frames << ": " << width << "x" << height
<< " pts=" << pts << " jpeg=" << jpeg.size() << "B"
<< " grab=" << grabMs << "ms" << std::endl;
}
}
const long long elapsed = elapsedMs();
StopRTSP(&client);
ReleaseANSRTSPHandle(&client);
ANSOPENCV::DeinitCameraNetwork();
std::cout << " polls=" << polls << " frames=" << frames
<< " elapsed=" << elapsed << "ms" << std::endl;
if (frames > 0) {
const double fps = elapsed > 0 ? frames * 1000.0 / elapsed : 0.0;
std::cout << " resolution=" << firstW << "x" << firstH
<< " avgJpeg=" << (totalJpegBytes / frames) << "B"
<< " avgGrab=" << (totalGrabMs / frames) << "ms"
<< " ~fps=" << fps
<< " ptsDelta=" << (lastPts - firstPts) << std::endl;
}
check("RTSP received >=1 frame", frames > 0,
std::to_string(frames) + " frames in " + std::to_string(elapsed) + "ms");
if (frames > 0) {
check("RTSP frame has valid resolution", firstW > 0 && firstH > 0,
std::to_string(firstW) + "x" + std::to_string(firstH));
}
// Encode the captured frames into a short MP4 clip using ANSCV's own
// hardware (NVENC) encoder, so the stream can actually be watched as video.
// maxWidth=1280 downscales the 4K stills to a manageable file; playback fps
// is fixed at 5 so a small burst yields a watchable clip.
if (!saveDir.empty() && frames >= 2) {
const std::string mp4Path = saveDir + "/rtsp_clip.mp4";
const int playbackFps = 5;
std::cout << " encoding MP4 (" << frames << " frames -> " << mp4Path
<< ", maxW=1280, " << playbackFps << " fps)..." << std::endl;
const int rc = ANSCV_ImagesToMP4HW_S(framesDir.c_str(), mp4Path.c_str(),
1280, playbackFps);
bool ok = rc != 0 && std::filesystem::exists(mp4Path);
std::string detail = "rc=" + std::to_string(rc);
if (ok) {
const auto sz = std::filesystem::file_size(mp4Path);
detail += ", " + std::to_string(sz) + " bytes";
}
check("RTSP frames encoded to MP4", ok, detail);
}
}
} // namespace
int main(int argc, char** argv) {
std::cout << "==== ANSCV Console Test ====" << std::endl;
// Optional RTSP target. argv[1] = URL, argv[2] = max frames (default 60),
// argv[3] = directory to save captured frames into (optional).
const std::string rtspUrl = (argc > 1) ? argv[1] : "";
const int rtspMaxFrames = (argc > 2) ? std::atoi(argv[2]) : 60;
const std::string rtspSaveDir = (argc > 3) ? argv[3] : "";
ANSOPENCV cv_;
const bool licensed = cv_.Init(""); // empty license key, matching existing tests
std::cout << "Init(\"\") license valid: " << (licensed ? "yes" : "no") << std::endl;
const cv::Mat src = MakeTestImage();
std::cout << "Source image: " << dims(src) << std::endl;
std::cout << "-- Geometry / resize --" << std::endl;
{
cv::Mat out;
cv_.ImageResize(src, 320, 240, out);
check("ImageResize 320x240", out.cols == 320 && out.rows == 240, dims(out));
}
{
cv::Mat out;
cv_.ImageResizeWithRatio(src, 320, out);
check("ImageResizeWithRatio width=320", out.cols == 320 && !out.empty(), dims(out));
}
{
cv::Mat out = cv_.ImageResizeV2(src, 200);
check("ImageResizeV2 width=200", !out.empty() && out.cols == 200, dims(out));
}
{
cv::Mat out = cv_.ImageCrop(src, cv::Rect(10, 20, 100, 80));
check("ImageCrop 100x80", out.cols == 100 && out.rows == 80, dims(out));
}
{
cv::Mat out = cv_.RotateImage(src, 90.0);
check("RotateImage 90deg", !out.empty(), dims(out));
}
{
cv::Mat out = cv_.FlipImage(src, 1); // horizontal
check("FlipImage horizontal", out.size() == src.size() && !out.empty(), dims(out));
}
{
// ShiftImage expands the canvas by |shiftX| x |shiftY| to keep all
// pixels after the translation (640+30, 480+40).
cv::Mat out = cv_.ShiftImage(src, 30, 40);
check("ShiftImage (30,40) expands canvas",
out.cols == src.cols + 30 && out.rows == src.rows + 40, dims(out));
}
std::cout << "-- Color / enhancement --" << std::endl;
{
cv::Mat out = cv_.ToGray(src);
check("ToGray -> 1 channel", out.channels() == 1 && !out.empty(), dims(out));
}
{
cv::Mat out = cv_.ImageContrastEnhancement(src);
check("ImageContrastEnhancement", !out.empty(), dims(out));
}
{
cv::Mat out = cv_.ImageWhiteBalance(src);
check("ImageWhiteBalance", !out.empty(), dims(out));
}
{
cv::Mat out = cv_.ImageDarkEnhancement(src, 1.5);
check("ImageDarkEnhancement x1.5", !out.empty(), dims(out));
}
std::cout << "-- Noise augmentation --" << std::endl;
{
cv::Mat out = cv_.AddGaussianNoise(src, 0.0, 20.0);
check("AddGaussianNoise", out.size() == src.size() && !out.empty(), dims(out));
}
{
cv::Mat out = cv_.AddSaltAndPepperNoise(src, 0.02);
check("AddSaltAndPepperNoise", out.size() == src.size() && !out.empty(), dims(out));
}
{
cv::Mat out = cv_.AddSpeckleNoise(src, 0.1);
check("AddSpeckleNoise", out.size() == src.size() && !out.empty(), dims(out));
}
std::cout << "-- Encode --" << std::endl;
{
std::string bin = cv_.MatToBinaryData(src);
check("MatToBinaryData non-empty", !bin.empty(),
std::to_string(bin.size()) + " bytes");
}
{
std::string b64 = cv_.MatToBase64(src);
check("MatToBase64 non-empty", !b64.empty(),
std::to_string(b64.size()) + " chars");
}
std::cout << "-- Parse / serialize --" << std::endl;
{
// GetBoundingBoxes parses a JSON object with a "results" array, each
// entry carrying x/y/width/height.
const std::string boxesJson =
R"({"results":[)"
R"({"x":10,"y":20,"width":30,"height":40},)"
R"({"x":100,"y":110,"width":50,"height":60}]})";
std::vector<cv::Rect> boxes = cv_.GetBoundingBoxes(boxesJson);
bool ok = boxes.size() == 2 &&
boxes[0] == cv::Rect(10, 20, 30, 40) &&
boxes[1] == cv::Rect(100, 110, 50, 60);
check("GetBoundingBoxes parses 2 boxes", ok,
std::to_string(boxes.size()) + " boxes");
}
{
std::vector<DetectionObject> dets(2);
dets[0].classId = 1; dets[0].className = "person";
dets[0].confidence = 0.9f; dets[0].box = cv::Rect(10, 10, 40, 80);
dets[1].classId = 2; dets[1].className = "car";
dets[1].confidence = 0.8f; dets[1].box = cv::Rect(60, 60, 100, 50);
std::string json = cv_.VectorDetectionToJsonString(dets);
check("VectorDetectionToJsonString non-empty", !json.empty(),
std::to_string(json.size()) + " chars");
}
if (!rtspUrl.empty()) {
RtspConsoleTest(rtspUrl, rtspMaxFrames, rtspSaveDir);
} else {
std::cout << "-- RTSP capture --" << std::endl;
std::cout << " (skipped: no RTSP URL given; pass one as the first "
"argument to run it)" << std::endl;
}
std::cout << "============================" << std::endl;
std::cout << "Result: " << g_pass << " passed, " << g_fail << " failed."
<< std::endl;
// NOTE: at process exit one of the loaded DLLs throws a C++ exception
// during DllMain(PROCESS_DETACH) (observed exit code 0xE06D7363),
// regardless of test outcome. All test work is complete by this point, so
// force a deterministic exit code for CI. TerminateProcess is used because
// both return and quick_exit go through ExitProcess, which still triggers
// the faulty DLL-detach path; TerminateProcess skips it.
std::cout.flush();
std::cerr.flush();
#ifdef _WIN32
::TerminateProcess(::GetCurrentProcess(), static_cast<UINT>(g_fail));
#endif
return g_fail;
}
+103 -6
View File
@@ -731,7 +731,7 @@ int ANSVISTest() {
//std::string videoFilePath = "E:\\Programs\\DemoAssets\\Videos\\ANSVIS_Issues\\face.mp4"; //std::string videoFilePath = "E:\\Programs\\DemoAssets\\Videos\\ANSVIS_Issues\\face.mp4";
//std::string videoFilePath = "E:\\Programs\\DemoAssets\\Videos\\TestFR\\Face31.mp4"; //std::string videoFilePath = "E:\\Programs\\DemoAssets\\Videos\\TestFR\\Face31.mp4";
std::string videoFilePath = "E:\\Programs\\DemoAssets\\Videos\\classroom.mp4"; std::string videoFilePath = "C:\\Programs\\DemoAssets\\Videos\\classroom.mp4";
//std::string videoFilePath = "E:\\Programs\\DemoAssets\\Videos\\TestFR\\school1.mp4";// "C:\\ProgramData\\ANSCENTER\\Shared\\classroom.mp4"; //std::string videoFilePath = "E:\\Programs\\DemoAssets\\Videos\\TestFR\\school1.mp4";// "C:\\ProgramData\\ANSCENTER\\Shared\\classroom.mp4";
const char* configFilePath = ""; const char* configFilePath = "";
@@ -1631,15 +1631,113 @@ int TestCompleteFR1() {
std::cout << "End of program.\n"; std::cout << "End of program.\n";
return 1; return 1;
} }
int main() // ---- SCRFD-TRT face-detection reproduction on a video (headless) ----------
int SCRFDVideoRepro()
{ {
std::string licenseKey = "";
const char* configFilePath = "";
std::string databaseFilePath = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFR\\ANSFR.db";
std::string recognizerFile = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFR\\ServerOptimised\\ANS_FaceRecognizer_v1.1_NVIDIAGeForceRTX3070LaptopGPU.zip";
std::string detectorFile = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFDET\\ServerOptimised\\ANS_GenericFD(GPU)_v1.0_NVIDIAGeForceRTX3070LaptopGPU.zip";
std::string videoPath = "C:\\Programs\\DemoAssets\\Videos\\classroom.mp4";
std::cout << "[REPRO] detector = " << detectorFile << std::endl;
std::cout << "[REPRO] recognizer = " << recognizerFile << std::endl;
ANSCENTER::ANSFacialRecognition* h = nullptr;
int cr = CreateANSRFHandle(&h, licenseKey.c_str(), configFilePath,
databaseFilePath.c_str(), recognizerFile.c_str(),
detectorFile.c_str(), 0.3, 1, 1, 1);
std::cout << "[REPRO] CreateANSRFHandle = " << cr << std::endl;
int le = LoadANSRFEngine(&h);
std::cout << "[REPRO] LoadANSRFEngine = " << le << std::endl;
cv::VideoCapture cap(videoPath);
if (!cap.isOpened()) { std::cout << "[REPRO] ERROR: cannot open video " << videoPath << std::endl; return 1; }
const std::string win = "ANSFR - SCRFD/TensorRT Face Detection (ESC to quit)";
cv::namedWindow(win, cv::WINDOW_NORMAL);
cv::resizeWindow(win, 1280, 720);
int frameIdx = 0, totalDet = 0, saved = 0;
cv::Mat frame;
while (cap.read(frame)) {
if (frame.empty()) break;
++frameIdx;
if (frameIdx > 300) break; // proof run: first 300 frames
unsigned int bufLen = 0;
unsigned char* jpg = ANSCENTER::ANSFRHelper::CVMatToBytes(frame, bufLen);
std::string res = RunANSRFInferenceBinary(&h, jpg, frame.cols, frame.rows);
delete jpg;
int n = 0;
try {
boost::property_tree::ptree pt; std::stringstream ss; ss << res;
boost::property_tree::read_json(ss, pt);
if (pt.count("results")) {
BOOST_FOREACH(const boost::property_tree::ptree::value_type& child, pt.get_child("results")) {
const boost::property_tree::ptree& r = child.second;
const int uid = GetData<int>(r, "user_id");
const std::string uname = GetData<std::string>(r, "user_name");
const float x = GetData<float>(r, "x");
const float y = GetData<float>(r, "y");
const float w = GetData<float>(r, "width");
const float ht = GetData<float>(r, "height");
cv::rectangle(frame, cv::Rect((int)x, (int)y, (int)w, (int)ht), cv::Scalar(0, 255, 0), 2);
cv::putText(frame, cv::format("%s:%d", uname.c_str(), uid), cv::Point((int)x, (int)y - 8),
cv::FONT_HERSHEY_SIMPLEX, 0.7, cv::Scalar(0, 0, 255), 2, cv::LINE_AA);
++n;
}
}
} catch (...) {}
totalDet += n;
cv::putText(frame, cv::format("Frame %d faces detected = %d", frameIdx, n),
cv::Point(25, 45), cv::FONT_HERSHEY_SIMPLEX, 1.1, cv::Scalar(0, 255, 255), 2, cv::LINE_AA);
if (frameIdx % 30 == 0) {
std::string fp = cv::format("C:\\Programs\\DemoAssets\\ANSFR_proof_frame_%04d_faces%d.jpg", frameIdx, n);
cv::imwrite(fp, frame);
++saved;
std::cout << "[REPRO] saved annotated frame: " << fp << std::endl;
}
cv::imshow(win, frame);
if (cv::waitKey(1) == 27) break; // ESC to quit
}
std::cout << "[REPRO] played " << frameIdx << " frames, total faces drawn = " << totalDet
<< ", saved " << saved << " annotated images to C:\\Programs\\DemoAssets\\" << std::endl;
cap.release();
cv::destroyAllWindows();
// NOTE: intentionally skipping ReleaseANSRFHandle/cudaDeviceReset here — there is
// a known C++ exception at teardown (0xE06D7363); process exit reclaims everything.
return 0;
}
int main(int argc, char** argv)
{
// ── GPU visibility bisect (Blackwell Myelin l2cm debugging) ─────────────
// Usage: ANSFR-UnitTest.exe --gpu 0 → only GPU0 visible (Test A, display GPU)
// ANSFR-UnitTest.exe --gpu 1 → only GPU1 visible (Test B, headless GPU)
// ANSFR-UnitTest.exe → all GPUs (default behaviour)
// Sets CUDA_VISIBLE_DEVICES before the first CUDA call in this process, so
// the CUDA runtime only enumerates the chosen physical GPU (it becomes
// device 0 inside the process).
for (int i = 1; i + 1 < argc; ++i) {
if (std::string(argv[i]) == "--gpu") {
_putenv_s("CUDA_VISIBLE_DEVICES", argv[i + 1]);
std::cout << "==============================================\n"
<< " GPU BISECT: CUDA_VISIBLE_DEVICES=" << argv[i + 1] << "\n"
<< " (only this physical GPU is visible to CUDA)\n"
<< "==============================================" << std::endl;
}
}
//FaceDetectorTest(); //FaceDetectorTest();
//TestFaceRecognition(); //TestFaceRecognition();
//TestCompleteFR1(); //TestCompleteFR1();
//ANSVISImageTest(); //ANSVISImageTest();
//ANSVISTest(); ANSVISTest();
//ANSVISTestFilePlayer(); //ANSVISTestFilePlayer();
// ANSVISRecordingTest(); // ANSVISRecordingTest();
//FRStressTest(); //FRStressTest();
@@ -1653,8 +1751,7 @@ int main()
// TestCompleteFR(); // TestCompleteFR();
//TestFaceRecognition(); //TestFaceRecognition();
//FaceRecognitionBenchmark(); //FaceRecognitionBenchmark();
ANSVISTestCPU_Lightweight(); //SCRFDVideoRepro();
//ANSVISTestCPU(); return 0;
std::cin.get();
} }
@@ -1647,6 +1647,9 @@ int HelmetDetectionTest() {
} }
// ============================================================================ // ============================================================================
// ElasticGPUTest — Verify elastic pool, round-robin dispatch, multi-GPU, // ElasticGPUTest — Verify elastic pool, round-robin dispatch, multi-GPU,
// concurrent inference, and idle-slot cleanup. // concurrent inference, and idle-slot cleanup.
@@ -2222,14 +2225,24 @@ int RTMPOSETest() {
VideoDetectorEngine(onnxclmodelFilePath, videoFile, modelType);// 22 is RT, 21 is onnx VideoDetectorEngine(onnxclmodelFilePath, videoFile, modelType);// 22 is RT, 21 is onnx
return 0; return 0;
} }
int FireNSmokeTest() {
int modelType = 16; // Custom model type for helmet detection, adjust as needed based on how the model is set up in the engine
std::string onnxclmodelFilePath = "C:\\Projects\\ANSVIS\\Models\\ANS_FireSmoke_v2.0\\ANS_FireSmoke_v2.0.zip";
std::string videoFile = "E:\\Programs\\DemoAssets\\Videos\\FireNSmoke\\BCA1.mp4";
VideoDetectorEngine(onnxclmodelFilePath, videoFile, modelType);// 22 is RT, 21 is onnx
return 0;
}
int main() int main()
{ {
//FallDetectorTest(); //FallDetectorTest();
//ALPROD_Test(); //ALPROD_Test();
//FireNSmokeTest(); FireNSmokeTest();
//ElasticGPUTest(); //ElasticGPUTest();
RTMPOSETest_CPP(); //RTMPOSETest_CPP();
//RTMPOSETest(); //RTMPOSETest();
//YOLO26SEGYolo11Test(); //YOLO26SEGYolo11Test();
//YOLO26POSEYolo11Test(); //YOLO26POSEYolo11Test();
@@ -1,3 +1,7 @@
// windows.h must be included before any header that pulls in wincrypt.h
// (Chilkat, OpenCV transitively). HKEY/RegOpenKeyExW are used by the
// watermark readout in GetEventLogErrorsWatermarkTest.
#include <windows.h>
#include <iostream> #include <iostream>
#include "ANSUtilities.h" #include "ANSUtilities.h"
#include "ANSLLM.h" #include "ANSLLM.h"
@@ -386,6 +390,226 @@ int ANSLLMOllamaTest() {
return 0; return 0;
} }
// Exercise ANSCENTER::ANSUtilities::ScanDevices — discovers AIBOX boxes and
// ANSVIS servers on the local LAN via UDP multicast + HTTP /ServerApi sweep.
// Run on a machine connected to the same subnet as at least one device.
// DebugView (filter "[ANSDISC]") shows per-device hits and per-interface joins.
int ScanDevicesTest()
{
std::cout << "\n===== ScanDevices (AIBOX / ANSVIS discovery) =====" << std::endl;
// 1) Direct static C++ call.
const int timeoutMs = 5000;
std::cout << "Scanning for " << timeoutMs << " ms ..." << std::endl;
auto devices = ANSCENTER::ANSUtilities::ScanDevices(timeoutMs);
std::cout << "Found " << devices.size() << " device(s):" << std::endl;
for (const auto& d : devices) {
const char* src = (d.source == 3) ? "both"
: (d.source == 2) ? "sweep"
: (d.source == 1) ? "multicast"
: "unknown";
std::cout << " " << d.ip << ":" << d.port
<< " name=\"" << d.name << "\""
<< " firmware=\"" << d.firmware << "\""
<< " source=" << src << std::endl;
}
// 2) Exercise the C extern entrypoint that LabVIEW uses.
std::string json;
int rc = ScanANSDevicesCpp(timeoutMs, json);
std::cout << "\nScanANSDevicesCpp rc=" << rc << " json=" << json << std::endl;
return static_cast<int>(devices.size());
}
// ============================================================================
// Example: ANSCENTER::ANSUtilities::GetANSLoggerErrorEvents
//
// Reads every Error-level event written by the "ANSLogger" source in the
// Windows Application event log. Output is a UTF-8 string with one event
// per line in the form:
// "<DD/MM/YYYY h:mm:ss tt>: <event data>\n"
//
// Three usage patterns are demonstrated below:
// 1. Read everything as one blob — simplest, what LabVIEW will do.
// 2. Iterate line-by-line and split out timestamp + message — useful for
// programmatic filtering, alerting, or writing to your own sink.
// 3. Destructive clear (commented out) — wipes the entire Application
// channel; only use if you genuinely want every event from every
// source removed. Requires Administrator privileges.
//
// To verify against Event Viewer:
// eventvwr.msc → Windows Logs → Application → Filter Current Log:
// Event sources = ANSLogger, Event level = Error
// The number this function returns must match Event Viewer's "Number of
// events" line shown above the list.
// ============================================================================
int GetEventLogErrorsTest()
{
std::cout << "\n===== GetANSLoggerErrorEvents (Windows Event Log) =====" << std::endl;
// ── 1. Read every Error event from ANSLogger ────────────────────────────
const bool kClearAfterRead = false; // NEVER true unless you really mean it
std::string events = ANSCENTER::ANSUtilities::GetANSLoggerErrorEvents(kClearAfterRead);
// Count events (one per newline).
int lineCount = 0;
for (char c : events) if (c == '\n') ++lineCount;
std::cout << "Read " << lineCount << " ANSLogger error event(s), "
<< events.size() << " bytes total" << std::endl;
// ── 2. Iterate line-by-line: extract timestamp + message ────────────────
// Format is "<timestamp>: <message>" — split on the first ": ".
// Prints the most recent 5 events so output isn't overwhelming.
std::cout << "\n--- last 5 events (timestamp / message) ---" << std::endl;
std::vector<std::pair<std::string, std::string>> parsed;
parsed.reserve(static_cast<size_t>(lineCount));
size_t lineStart = 0;
for (size_t i = 0; i < events.size(); ++i) {
if (events[i] != '\n') continue;
std::string line = events.substr(lineStart, i - lineStart);
lineStart = i + 1;
auto sep = line.find(": ");
if (sep == std::string::npos) {
parsed.emplace_back(std::string(), std::move(line));
} else {
parsed.emplace_back(line.substr(0, sep), line.substr(sep + 2));
}
}
const size_t showFrom = parsed.size() > 5 ? parsed.size() - 5 : 0;
for (size_t i = showFrom; i < parsed.size(); ++i) {
// Truncate noisy messages so the console stays readable.
std::string msg = parsed[i].second;
if (msg.size() > 120) msg = msg.substr(0, 117) + "...";
std::cout << " [" << parsed[i].first << "] " << msg << std::endl;
}
// ── 3. C extern entry point (this is what LabVIEW calls) ────────────────
// Signature: int GetANSLoggerErrorEventsCpp(int clearAfterRead, std::string& out)
// Passing 0 for clearAfterRead = read only.
std::string fromExtern;
int rc = GetANSLoggerErrorEventsCpp(0, fromExtern);
std::cout << "\nGetANSLoggerErrorEventsCpp rc=" << rc
<< " bytes=" << fromExtern.size() << std::endl;
// ── 4. DIAGNOSTIC: read → read+clear → read, prove whether the clear
// flag actually does anything (and if not, capture the OS error).
{
std::cout << "\n--- clear-flag diagnostic ---" << std::endl;
auto countLines = [](const std::string& s) {
int n = 0;
for (char c : s) if (c == '\n') ++n;
return n;
};
std::string before = ANSCENTER::ANSUtilities::GetANSLoggerErrorEvents(false);
std::cout << " before clear : " << countLines(before) << " events" << std::endl;
std::string cleared = ANSCENTER::ANSUtilities::GetANSLoggerErrorEvents(true);
std::cout << " with clear=true call returned: "
<< countLines(cleared) << " events" << std::endl;
std::string after = ANSCENTER::ANSUtilities::GetANSLoggerErrorEvents(false);
std::cout << " after clear : " << countLines(after)
<< " events (should be 0 if clear worked)" << std::endl;
const int beforeN = countLines(before);
const int afterN = countLines(after);
if (beforeN == 0) {
std::cout << " RESULT: inconclusive (no events to clear). "
"Check stderr above for the actual EvtClearLog status."
<< std::endl;
} else if (afterN == 0) {
std::cout << " RESULT: clear SUCCEEDED — process was elevated."
<< std::endl;
} else if (afterN >= beforeN) {
std::cout << " RESULT: clear FAILED. "
"Check stderr above for the GetLastError code "
"(5 = ACCESS_DENIED — re-run as Administrator)."
<< std::endl;
} else {
std::cout << " RESULT: partial clear (" << beforeN << " -> "
<< afterN << "). Likely a concurrent writer."
<< std::endl;
}
}
return lineCount;
}
// ============================================================================
// Example: ANSCENTER::ANSUtilities::GetANSLoggerErrorEventsSinceLastCall
//
// Watermark-based variant. Two consecutive calls demonstrate the skip:
// 1st call: returns every Error event from ANSLogger (same as the full read)
// 2nd call: returns 0 (or only events written between the two calls)
// The watermark is stored at
// HKCU\Software\ANSCENTER\ANSUtilities\EventLogWatermark\ANSLogger (REG_QWORD)
// To force a re-read of everything, delete that registry value.
// ============================================================================
int GetEventLogErrorsWatermarkTest()
{
std::cout << "\n===== GetANSLoggerErrorEventsSinceLastCall (watermark) ====="
<< std::endl;
auto countLines = [](const std::string& s) {
int n = 0;
for (char c : s) if (c == '\n') ++n;
return n;
};
// Read the current watermark so we can show it before and after.
auto readWatermark = []() -> unsigned long long {
HKEY hKey = nullptr;
if (RegOpenKeyExW(HKEY_CURRENT_USER,
L"Software\\ANSCENTER\\ANSUtilities\\EventLogWatermark",
0, KEY_READ, &hKey) != ERROR_SUCCESS) return 0ULL;
unsigned long long v = 0;
DWORD sz = sizeof(v), tp = 0;
LONG rc = RegQueryValueExW(hKey, L"ANSLogger", nullptr, &tp,
reinterpret_cast<LPBYTE>(&v), &sz);
RegCloseKey(hKey);
return (rc == ERROR_SUCCESS && tp == REG_QWORD) ? v : 0ULL;
};
std::cout << " watermark before: " << readWatermark() << std::endl;
std::string first =
ANSCENTER::ANSUtilities::GetANSLoggerErrorEventsSinceLastCall(false);
std::cout << " 1st call returned: " << countLines(first) << " event(s)"
<< std::endl;
std::cout << " watermark after 1st call: " << readWatermark() << std::endl;
std::string second =
ANSCENTER::ANSUtilities::GetANSLoggerErrorEventsSinceLastCall(false);
std::cout << " 2nd call returned: " << countLines(second)
<< " event(s) (should be 0 if no new errors were logged "
"between the two calls)" << std::endl;
if (countLines(second) == 0 && countLines(first) > 0) {
std::cout << " RESULT: watermark works — duplicates suppressed."
<< std::endl;
} else if (countLines(first) == 0) {
std::cout << " RESULT: nothing to test (no ANSLogger errors in log)."
<< std::endl;
} else {
std::cout << " RESULT: 2nd call returned " << countLines(second)
<< " event(s) — either new errors arrived between calls, "
"or the watermark didn't persist." << std::endl;
}
// Note: passing the LabVIEW extern below as well so its parameter wiring
// is exercised. Pass 0 = no clear; clear path is identical to the
// already-tested admin-required version in GetEventLogErrorsTest.
std::string fromExtern;
int rc = GetANSLoggerErrorEventsSinceLastCallCpp(0, fromExtern);
std::cout << " GetANSLoggerErrorEventsSinceLastCallCpp rc=" << rc
<< " bytes=" << fromExtern.size()
<< " (should be 0 — already watermarked above)" << std::endl;
return countLines(first);
}
int main() int main()
{ {
//ANSLLMOllamaTest(); //ANSLLMOllamaTest();
@@ -393,11 +617,15 @@ int main()
//std::cout << "===== MinIO (path-style) =====" << std::endl; //std::cout << "===== MinIO (path-style) =====" << std::endl;
//ANSFTechMinIOTest(); //ANSFTechMinIOTest();
std::cout << "\n===== AWS S3 (virtual-host) =====" << std::endl; //std::cout << "\n===== AWS S3 (virtual-host) =====" << std::endl;
ANSAWSTest(); //ANSAWSTest();
//ANSLLMTest(); //ANSLLMTest();
//SendEmailExample(); //SendEmailExample();
//uploadFileToGoogleStorage(); //uploadFileToGoogleStorage();
// EncrypyionExample(); // EncrypyionExample();
ScanDevicesTest();
GetEventLogErrorsTest();
GetEventLogErrorsWatermarkTest();
} }
+290
View File
@@ -0,0 +1,290 @@
<#
.SYNOPSIS
Reports versions and ABI compatibility of the inference runtime stack
(CUDA, cuDNN, TensorRT, OpenVINO, DirectML, ONNX Runtime per-EP) inside an
ANSCENTER "Shared" folder, and optionally diffs two such folders.
.DESCRIPTION
Rerun this after upgrading any DLL in SharedCore to confirm the pieces still
line up. It does three things:
1. Reads the version of every key library.
- ORT: FileVersion of ep\<x>\onnxruntime.dll
- OpenVINO / DirectML / cuDNN: FileVersion resource
- CUDA components: inferred from the versioned file name (cudart64_13 => CUDA 13)
- TensorRT: nvinfer_*.dll has NO version resource, so we p/invoke its
exported getInferLibVersion() (packed int, e.g. 101501 => 10.15.1).
2. Uses dumpbin /DEPENDENTS (if Visual Studio is installed) to list which
CUDA / cuDNN / TensorRT / OpenVINO / DirectML DLLs each EP actually
imports, and verifies every one exists in the root.
3. Flags gaps: empty ep\cpu with no flat fallback, missing dependencies,
ORT-version split across EP folders.
NOTE: the TensorRT probe uses a plain [DllImport] to nvinfer's exported
getInferLibVersion(). It deliberately avoids LoadLibrary+GetProcAddress+
GetDelegateForFunctionPointer, because that dynamic-resolution pattern trips
Windows Defender AMSI ("malicious content") and blocks the whole script.
.PARAMETER Root
The Shared folder to inspect. Default: C:\Projects\SharedCore
.PARAMETER CompareWith
Optional second Shared folder to diff against Root (e.g. the deployed
C:\ProgramData\ANSCENTER\Shared). Reports any version mismatches.
.PARAMETER SkipTensorRT
Skip the TensorRT p/invoke probe (useful if your AV still objects, or to run
purely on file-version metadata).
.EXAMPLE
.\Check-SharedCoreVersions.ps1
.EXAMPLE
.\Check-SharedCoreVersions.ps1 -Root C:\Projects\SharedCore `
-CompareWith C:\ProgramData\ANSCENTER\Shared
#>
[CmdletBinding()]
param(
[string] $Root = "C:\Projects\SharedCore",
[string] $CompareWith,
[switch] $SkipTensorRT
)
$ErrorActionPreference = "Stop"
# ----------------------------------------------------------------------------
# Helpers
# ----------------------------------------------------------------------------
function Get-FileVer([string]$path) {
if (Test-Path -LiteralPath $path) { (Get-Item -LiteralPath $path).VersionInfo.FileVersion }
else { $null }
}
# Windows PowerShell 5.1 has no ?? operator - use this instead.
function Coalesce($value, $fallback) {
if ($value) { $value } else { $fallback }
}
function Find-Dumpbin {
$candidates = Get-ChildItem `
"C:\Program Files\Microsoft Visual Studio\*\*\VC\Tools\MSVC\*\bin\Hostx64\x64\dumpbin.exe", `
"C:\Program Files (x86)\Microsoft Visual Studio\*\*\VC\Tools\MSVC\*\bin\Hostx64\x64\dumpbin.exe" `
-ErrorAction SilentlyContinue | Sort-Object FullName -Descending
if ($candidates) { return $candidates[0].FullName }
return $null
}
# Declares plain DllImports to nvinfer's getInferLibVersion. Compiled once per
# session. The DLLs are resolved via PATH, so callers must put the root dir on
# PATH before invoking (Get-TensorRTVersion does this).
function Initialize-TrtProbe {
if ('AnsTrtProbe.Lib' -as [type]) { return }
Add-Type -Namespace AnsTrtProbe -Name Lib -MemberDefinition @'
[System.Runtime.InteropServices.DllImport("nvinfer_dispatch_10.dll", EntryPoint = "getInferLibVersion", CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)]
public static extern int Dispatch10();
[System.Runtime.InteropServices.DllImport("nvinfer_lean_10.dll", EntryPoint = "getInferLibVersion", CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)]
public static extern int Lean10();
[System.Runtime.InteropServices.DllImport("nvinfer_10.dll", EntryPoint = "getInferLibVersion", CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)]
public static extern int Full10();
'@
}
# Returns the packed TensorRT version int (e.g. 101501) or $null.
function Get-TensorRTVersion([string]$dir) {
if ($SkipTensorRT) { return $null }
if (-not (Get-ChildItem -LiteralPath $dir -Filter 'nvinfer*_10.dll' -ErrorAction SilentlyContinue)) {
return $null
}
$origPath = $env:Path
$env:Path = "$dir;$env:Path" # so the stub and its deps resolve
try {
Initialize-TrtProbe
foreach ($m in 'Dispatch10','Lean10','Full10') {
try {
$v = [AnsTrtProbe.Lib]::$m()
if ($v -gt 0) { return $v }
} catch { }
}
return $null
} finally {
$env:Path = $origPath
}
}
function Format-TensorRT($raw) {
if (-not $raw) { return "UNKNOWN (probe skipped or failed)" }
$maj = [math]::Floor($raw / 10000)
$min = [math]::Floor(($raw % 10000) / 100)
$pat = $raw % 100
return "$maj.$min.$pat (raw $raw)"
}
# Returns the lower-cased set of dependency DLLs (CUDA/TRT/OV/DML family) that
# $path imports, via dumpbin /DEPENDENTS. Empty array if dumpbin unavailable.
function Get-Deps([string]$dumpbin, [string]$path) {
if (-not $dumpbin -or -not (Test-Path -LiteralPath $path)) { return @() }
$rx = '(?i)cudart|cublas|cudnn|cufft|cusparse|cusolver|nvinfer|nvonnx|nvrtc|directml|openvino'
& $dumpbin /DEPENDENTS $path 2>$null |
Select-String -Pattern '^\s+\S+\.dll\s*$' |
ForEach-Object { $_.Line.Trim().ToLower() } |
Where-Object { $_ -match $rx } |
Sort-Object -Unique
}
$epMap = [ordered]@{
"cpu" = "CPU"
"cuda" = "NVIDIA_GPU (CUDA + TensorRT)"
"directml" = "AMD_GPU (DirectML)"
"openvino" = "OPENVINO_GPU (Intel)"
}
# ----------------------------------------------------------------------------
# Inspect one root
# ----------------------------------------------------------------------------
function Inspect-Root([string]$root) {
if (-not (Test-Path -LiteralPath $root)) {
Write-Host "ERROR: root not found: $root" -ForegroundColor Red
return $null
}
$dumpbin = Find-Dumpbin
Write-Host ""
Write-Host "=================================================================="
Write-Host " Shared root : $root"
Write-Host " dumpbin : $(if($dumpbin){$dumpbin}else{'NOT FOUND - dependency checks skipped'})"
Write-Host "=================================================================="
$result = [ordered]@{ Root = $root; Ort = [ordered]@{} }
# ---- Core libraries -----------------------------------------------------
$trtRaw = Get-TensorRTVersion $root
$result.TensorRT = $trtRaw
Write-Host ""
Write-Host "-- Core runtime libraries -----------------------------------------"
Write-Host ("{0,-14} {1}" -f "TensorRT:", (Format-TensorRT $trtRaw))
Write-Host ("{0,-14} {1}" -f "OpenVINO:", (Coalesce (Get-FileVer (Join-Path $root 'openvino.dll')) 'MISSING'))
Write-Host ("{0,-14} {1}" -f "DirectML:", (Coalesce (Get-FileVer (Join-Path $root 'DirectML.dll')) 'MISSING'))
Write-Host ("{0,-14} {1}" -f "cuDNN:", (Coalesce (Get-FileVer (Join-Path $root 'cudnn64_9.dll')) 'MISSING'))
# CUDA toolkit generation is encoded in the file name suffix.
$cudaFiles = Get-ChildItem -LiteralPath $root -Filter "cudart64_*.dll" -ErrorAction SilentlyContinue
if ($cudaFiles) {
$gen = ($cudaFiles[0].BaseName -replace 'cudart64_','')
Write-Host ("{0,-14} {1}" -f "CUDA:", "toolkit $gen.x (cudart64_$gen.dll present)")
} else {
Write-Host ("{0,-14} {1}" -f "CUDA:", "MISSING (no cudart64_*.dll)")
}
# ---- ORT per EP ---------------------------------------------------------
Write-Host ""
Write-Host "-- ONNX Runtime per EP --------------------------------------------"
foreach ($ep in $epMap.Keys) {
$epDir = Join-Path $root "ep\$ep"
$ortDll = Join-Path $epDir "onnxruntime.dll"
$ver = Get-FileVer $ortDll
$result.Ort[$ep] = $ver
if ($ver) {
Write-Host ("{0,-10} {1,-26} [{2}]" -f $ep, $ver, $epMap[$ep])
} elseif (Test-Path -LiteralPath $epDir) {
Write-Host ("{0,-10} {1,-26} [{2}]" -f $ep, "EMPTY / no onnxruntime.dll", $epMap[$ep])
} else {
Write-Host ("{0,-10} {1,-26} [{2}]" -f $ep, "FOLDER MISSING", $epMap[$ep])
}
}
# ---- Dependency / presence check ---------------------------------------
Write-Host ""
Write-Host "-- EP dependency resolution (imports must exist in root) ----------"
$missingDeps = @()
$epProviders = @{
"cuda" = @("onnxruntime_providers_cuda.dll","onnxruntime_providers_tensorrt.dll")
"directml" = @() # DML is built into onnxruntime.dll; needs DirectML.dll from root
"openvino" = @("onnxruntime_providers_openvino.dll")
"cpu" = @()
}
foreach ($ep in $epProviders.Keys) {
$epDir = Join-Path $root "ep\$ep"
if (-not (Test-Path -LiteralPath (Join-Path $epDir 'onnxruntime.dll'))) { continue }
$checkFiles = @("onnxruntime.dll") + $epProviders[$ep]
$deps = @()
foreach ($cf in $checkFiles) { $deps += Get-Deps $dumpbin (Join-Path $epDir $cf) }
$deps = $deps | Sort-Object -Unique
if (-not $deps) { continue }
Write-Host (" [{0}] imports: {1}" -f $ep, ($deps -join ', '))
foreach ($d in $deps) {
# provider DLLs live in the ep folder; everything else in root
$inEp = Test-Path -LiteralPath (Join-Path $epDir $d)
$inRoot = Test-Path -LiteralPath (Join-Path $root $d)
if (-not $inEp -and -not $inRoot) {
Write-Host (" MISSING: $d") -ForegroundColor Red
$missingDeps += "$ep -> $d"
}
}
}
if (-not $missingDeps) { Write-Host " All imported CUDA/TRT/OV/DML DLLs are present." -ForegroundColor Green }
$result.MissingDeps = $missingDeps
# ---- Gap checks ---------------------------------------------------------
Write-Host ""
Write-Host "-- Findings -------------------------------------------------------"
$findings = @()
# CPU reachability: EngineType::CPU needs ep\cpu\onnxruntime.dll OR a flat root onnxruntime.dll
$cpuDll = Test-Path -LiteralPath (Join-Path $root 'ep\cpu\onnxruntime.dll')
$flatDll = Test-Path -LiteralPath (Join-Path $root 'onnxruntime.dll')
if (-not $cpuDll -and -not $flatDll) {
$findings += "CPU branch UNREACHABLE: ep\cpu is empty and no flat onnxruntime.dll fallback. A CPU-only machine will fail to load ORT."
}
# ORT version split across EP folders
$distinct = $result.Ort.Values | Where-Object { $_ } | Sort-Object -Unique
if ($distinct.Count -gt 1) {
$findings += "ORT versions differ across EP folders ($($distinct -join ' / ')). Fine ONLY because EPLoader loads one EP per process - never mix DLLs across folders."
}
if ($missingDeps) { $findings += "Unresolved dependencies: $($missingDeps -join '; ')" }
if (-not $findings) {
Write-Host " No issues detected." -ForegroundColor Green
} else {
foreach ($f in $findings) { Write-Host " ! $f" -ForegroundColor Yellow }
}
return $result
}
# ----------------------------------------------------------------------------
# Main
# ----------------------------------------------------------------------------
$primary = Inspect-Root $Root
if ($CompareWith) {
$secondary = Inspect-Root $CompareWith
if ($primary -and $secondary) {
Write-Host ""
Write-Host "=================================================================="
Write-Host " DIFF: $Root vs $CompareWith"
Write-Host "=================================================================="
$mismatch = $false
if ($primary.TensorRT -ne $secondary.TensorRT) {
Write-Host (" TensorRT: {0} vs {1}" -f (Format-TensorRT $primary.TensorRT), (Format-TensorRT $secondary.TensorRT)) -ForegroundColor Yellow
$mismatch = $true
}
foreach ($ep in $epMap.Keys) {
$a = $primary.Ort[$ep]; $b = $secondary.Ort[$ep]
if ($a -ne $b) {
Write-Host (" ORT ep\{0}: {1} vs {2}" -f $ep, (Coalesce $a 'MISSING'), (Coalesce $b 'MISSING')) -ForegroundColor Yellow
$mismatch = $true
}
}
foreach ($lib in @('openvino.dll','DirectML.dll','cudnn64_9.dll')) {
$a = Get-FileVer (Join-Path $Root $lib); $b = Get-FileVer (Join-Path $CompareWith $lib)
if ($a -ne $b) {
Write-Host (" {0}: {1} vs {2}" -f $lib, (Coalesce $a 'MISSING'), (Coalesce $b 'MISSING')) -ForegroundColor Yellow
$mismatch = $true
}
}
if (-not $mismatch) { Write-Host " Identical across both roots." -ForegroundColor Green }
}
}
Write-Host ""
+5
View File
@@ -0,0 +1,5 @@
@echo off
call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat" >nul 2>nul
cd /d "C:\Projects\CLionProjects\ANSCORE"
"C:\Program Files\CMake\bin\cmake.exe" --build cmake-build-release --target ANSFR-UnitTest
exit /b %errorlevel%
+5
View File
@@ -0,0 +1,5 @@
@echo off
set "PATH=C:\Projects\CLionProjects\ANSCORE\cmake-build-release\bin;C:\Projects\SharedCore;%PATH%"
cd /d "C:\Projects\CLionProjects\ANSCORE\cmake-build-release\bin"
ANSFR-UnitTest.exe
echo [REPRO] exitcode=%errorlevel%
+77
View File
@@ -0,0 +1,77 @@
@echo off
REM ============================================================================
REM build_openvino_ep.bat
REM Build the ONNX Runtime OpenVINO Execution Provider (ORT 1.24 / API 24) and
REM stage the 3 resulting DLLs into SharedCore\ep\openvino so verify_ep.ps1
REM -RequiredApi 24 goes green.
REM
REM Self-contained: sets up MSVC + an isolated Python venv + OpenVINO env, then
REM builds and stages. Run from a PLAIN cmd.exe (NOT PowerShell - OpenVINO's
REM setupvars.bat env vars only persist in cmd).
REM
REM Usage: tools\build_openvino_ep.bat
REM ============================================================================
setlocal enableextensions
REM ---- Paths (edit here if your layout differs) ------------------------------
set "ORT_SRC=C:\Projects\onnxruntime-ov"
set "PYDIR=C:\ANSLibs\Python311"
set "OV_SETUP=C:\ANSLibs\OpenVINO\setupvars.bat"
set "EP_DST=C:\Projects\SharedCore\ep\openvino"
set "OV_DEVICE=GPU"
echo === [1/6] Locate MSVC (vcvars64) ===
set "VCVARS="
for /f "usebackq tokens=*" %%i in (`"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -property installationPath 2^>nul`) do set "VSPATH=%%i"
if defined VSPATH if exist "%VSPATH%\VC\Auxiliary\Build\vcvars64.bat" set "VCVARS=%VSPATH%\VC\Auxiliary\Build\vcvars64.bat"
if not defined VCVARS set "VCVARS=C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat"
if not exist "%VCVARS%" ( echo ERROR: vcvars64.bat not found. Install VS 2022 C++ workload. & exit /b 1 )
call "%VCVARS%" || ( echo ERROR: vcvars64 failed & exit /b 1 )
echo === [2/6] OpenVINO environment ===
if not exist "%OV_SETUP%" ( echo ERROR: OpenVINO setupvars not found at %OV_SETUP% & exit /b 1 )
call "%OV_SETUP%"
REM Release-only OpenVINO archives still ship a Debug cmake config whose DLLs are
REM absent, which breaks find_package(OpenVINO). Neutralize it (safe: ANSCORE links
REM OpenVINO via explicit paths, not find_package, and this build is Release-only).
set "OV_DBG=C:\ANSLibs\OpenVINO\runtime\cmake\OpenVINOTargets-debug.cmake"
if exist "%OV_DBG%" ( ren "%OV_DBG%" "OpenVINOTargets-debug.cmake.disabled" && echo neutralized OpenVINO Debug cmake config )
REM This ANSLibs OpenVINO is stripped of its runtime DLLs (deployed to SharedCore),
REM but find_package(OpenVINO) validates DLL presence. Restore Release DLLs from SharedCore.
set "OV_REL=C:\ANSLibs\OpenVINO\runtime\bin\intel64\Release"
if not exist "%OV_REL%\openvino_onnx_frontend.dll" (
echo restoring OpenVINO Release DLLs from SharedCore...
if not exist "%OV_REL%" mkdir "%OV_REL%"
copy /Y "C:\Projects\SharedCore\openvino*.dll" "%OV_REL%\" >nul
copy /Y "C:\Projects\SharedCore\tbb*.dll" "%OV_REL%\" >nul
)
echo === [3/6] Isolated Python venv (keeps build deps out of %PYDIR%) ===
if not exist "%PYDIR%\python.exe" ( echo ERROR: python not at %PYDIR% & exit /b 1 )
if not exist "%ORT_SRC%\.buildvenv\Scripts\python.exe" (
"%PYDIR%\python.exe" -m venv "%ORT_SRC%\.buildvenv" || ( echo ERROR: venv create failed & exit /b 1 )
)
call "%ORT_SRC%\.buildvenv\Scripts\activate.bat"
python --version || ( echo ERROR: venv python not usable & exit /b 1 )
python -m pip install --upgrade pip setuptools wheel packaging numpy >nul
echo === [4/6] Build (Release, shared lib, OpenVINO %OV_DEVICE%) ===
cd /d "%ORT_SRC%" || ( echo ERROR: ORT source not found at %ORT_SRC% & exit /b 1 )
REM NOTE: full path, not bare 'build.bat' - VS dev env sets
REM NoDefaultCurrentDirectoryInExePath, so cmd won't resolve build.bat from CWD.
call "%ORT_SRC%\build.bat" --config Release --use_openvino %OV_DEVICE% --build_shared_lib --parallel --skip_tests --cmake_generator "Visual Studio 17 2022"
if errorlevel 1 ( echo ERROR: build failed - see log above & exit /b 1 )
echo === [5/6] Stage DLLs into %EP_DST% (with one-time backup) ===
set "OUT=%ORT_SRC%\build\Windows\Release\Release"
if not exist "%OUT%\onnxruntime_providers_openvino.dll" ( echo ERROR: build output missing in %OUT% & exit /b 1 )
if not exist "%EP_DST%._bak_123" ( xcopy /E /I /Y "%EP_DST%" "%EP_DST%._bak_123" >nul )
copy /Y "%OUT%\onnxruntime.dll" "%EP_DST%\" >nul
copy /Y "%OUT%\onnxruntime_providers_openvino.dll" "%EP_DST%\" >nul
copy /Y "%OUT%\onnxruntime_providers_shared.dll" "%EP_DST%\" >nul
echo === [6/6] Done ===
echo Staged into %EP_DST% (1.23 backup at %EP_DST%._bak_123)
echo Validate:
echo powershell -ExecutionPolicy Bypass -File C:\Projects\CLionProjects\ANSCORE\tools\verify_ep.ps1 -RequiredApi 24
endlocal
+131
View File
@@ -0,0 +1,131 @@
<#
.SYNOPSIS
Bump the link-time ONNX Runtime SDK in C:\ANSLibs\onnxruntime to a new version
(e.g. 1.23 -> 1.24), safely and reversibly. ANSCORE compiles against this folder.
.DESCRIPTION
Replaces C:\ANSLibs\onnxruntime\{include,lib} + the runtime<ver>_openvino<ver>.txt
marker with a new ORT SDK, after validating the new SDK and backing up the old one.
It does NOT touch the runtime DLLs in SharedCore\ep - those are a separate step.
ANSCORE only #includes onnxruntime_cxx_api.h (the EP-agnostic C++ API), so any
matching ORT build's include\ works (GPU build's headers are fine).
SEQUENCING NOTE: bumping the link header to API N makes any runtime EP folder
whose core is < N a latent risk. Unify the runtime (incl. the OpenVINO EP) to the
target version FIRST, then run this. After running, rebuild ANSCORE and validate
the runtime with: tools\verify_ep.ps1 -RequiredApi <N>
.PARAMETER SdkPath
Folder of the NEW ORT SDK. Must contain:
include\onnxruntime_c_api.h (with the new ORT_API_VERSION)
lib\onnxruntime.lib
Typically the unzipped onnxruntime-win-x64-gpu-<ver> release, or an Intel
OpenVINO ORT build of the same version.
.PARAMETER AnsLibs
ANSLibs root. Default: C:\ANSLibs
.PARAMETER Marker
New marker filename to drop in (documents the version, mirrors the existing
'runtime1.23.0_openvino2025.3.0.txt'). Optional - if omitted, no marker change.
.PARAMETER WhatIf
Show what would happen without changing anything.
.EXAMPLE
# Dry run first
.\bump_ort_sdk.ps1 -SdkPath D:\dl\onnxruntime-win-x64-gpu-1.24.0 `
-Marker runtime1.24.0_openvino2025.3.0.txt -WhatIf
# Then for real
.\bump_ort_sdk.ps1 -SdkPath D:\dl\onnxruntime-win-x64-gpu-1.24.0 `
-Marker runtime1.24.0_openvino2025.3.0.txt
#>
[CmdletBinding(SupportsShouldProcess = $true)]
param(
[Parameter(Mandatory = $true)][string] $SdkPath,
[string] $AnsLibs = "C:\ANSLibs",
[string] $Marker
)
$ErrorActionPreference = "Stop"
function Get-OrtApiFromHeader([string]$header) {
if (-not (Test-Path -LiteralPath $header)) { return $null }
$line = Select-String -LiteralPath $header -Pattern 'define\s+ORT_API_VERSION\s+(\d+)' | Select-Object -First 1
if ($line) { return [int]$line.Matches[0].Groups[1].Value }
return $null
}
if (-not (Test-Path -LiteralPath $SdkPath)) {
Write-Host "FAIL - SdkPath not found: $SdkPath" -ForegroundColor Red
Write-Host "Point -SdkPath at an unzipped ORT release folder (with include\ and lib\)." -ForegroundColor Yellow
exit 2
}
$ortDir = Join-Path $AnsLibs 'onnxruntime'
$newInc = Join-Path $SdkPath 'include'
$newLib = Join-Path $SdkPath 'lib'
$newHdr = Join-Path $newInc 'onnxruntime_c_api.h'
# ---- Validate NEW sdk -------------------------------------------------------
Write-Host "== Validating new SDK at: $SdkPath =="
$problems = @()
if (-not (Test-Path -LiteralPath $newHdr)) { $problems += "missing include\onnxruntime_c_api.h" }
if (-not (Test-Path -LiteralPath (Join-Path $newInc 'onnxruntime_cxx_api.h'))) { $problems += "missing include\onnxruntime_cxx_api.h" }
if (-not (Test-Path -LiteralPath (Join-Path $newLib 'onnxruntime.lib'))) { $problems += "missing lib\onnxruntime.lib" }
if ($problems) {
Write-Host "FAIL - new SDK is incomplete:" -ForegroundColor Red
$problems | ForEach-Object { Write-Host " - $_" -ForegroundColor Red }
Write-Host "Point -SdkPath at an unzipped ORT release that has include\ and lib\." -ForegroundColor Yellow
exit 2
}
$newApi = Get-OrtApiFromHeader $newHdr
$oldApi = Get-OrtApiFromHeader (Join-Path $ortDir 'include\onnxruntime_c_api.h')
$newApiDisp = if ($newApi) { $newApi } else { '??' }
$oldApiDisp = if ($oldApi) { $oldApi } else { 'none' }
Write-Host (" new ORT_API_VERSION : {0}" -f $newApiDisp)
Write-Host (" current (ANSLibs) : {0}" -f $oldApiDisp)
if ($newApi -and $oldApi -and $newApi -le $oldApi) {
Write-Host ("WARNING: new API ({0}) is not greater than current ({1}). Continue only if intentional." -f $newApi,$oldApi) -ForegroundColor Yellow
}
# ---- Back up CURRENT onnxruntime SDK ---------------------------------------
$stamp = (Get-Date -Format 'yyyyMMdd_HHmmss')
$backup = Join-Path $AnsLibs ("onnxruntime.bak_$stamp")
Write-Host ""
Write-Host "== Backing up current SDK -> $backup =="
if ($PSCmdlet.ShouldProcess($ortDir, "copy to $backup")) {
Copy-Item -LiteralPath $ortDir -Destination $backup -Recurse -Force
}
# ---- Swap include\ and lib\ -------------------------------------------------
Write-Host "== Installing new include\ and lib\ =="
foreach ($sub in @('include','lib')) {
$dst = Join-Path $ortDir $sub
$src = Join-Path $SdkPath $sub
if ($PSCmdlet.ShouldProcess($dst, "replace from $src")) {
if (Test-Path -LiteralPath $dst) { Remove-Item -LiteralPath $dst -Recurse -Force }
Copy-Item -LiteralPath $src -Destination $dst -Recurse -Force
}
}
# ---- Marker file ------------------------------------------------------------
if ($Marker) {
Write-Host "== Updating version marker -> $Marker =="
if ($PSCmdlet.ShouldProcess($ortDir, "swap marker to $Marker")) {
Get-ChildItem -LiteralPath $ortDir -Filter 'runtime*.txt' -File -ErrorAction SilentlyContinue |
ForEach-Object { Remove-Item -LiteralPath $_.FullName -Force }
New-Item -ItemType File -Path (Join-Path $ortDir $Marker) -Force | Out-Null
}
}
Write-Host ""
Write-Host "== DONE ==" -ForegroundColor Green
Write-Host "Next steps:" -ForegroundColor Cyan
$apiForSteps = if ($newApi) { $newApi } else { 'N' }
Write-Host (" 1. Clean-rebuild ANSCORE (it now compiles against ORT_API_VERSION {0})." -f $apiForSteps)
Write-Host (" 2. Validate the runtime: tools\verify_ep.ps1 -RequiredApi {0}" -f $apiForSteps)
Write-Host " (the openvino EP folder must also be at the new version or it will FAIL.)"
Write-Host (" 3. If anything breaks, revert: Remove-Item '$ortDir' -Recurse -Force; Rename-Item '$backup' 'onnxruntime'")
+115
View File
@@ -0,0 +1,115 @@
<#
.SYNOPSIS
Assemble a complete link-time ONNX Runtime C/C++ SDK (include\ + lib\) by
downloading the public headers at a given source commit and pairing them with
an existing import lib. Output feeds straight into tools\bump_ort_sdk.ps1.
.DESCRIPTION
ORT nightly builds (e.g. 1.24.YYYYMMDD.N.<hash>) ship DLLs + import libs but
the matching C/C++ headers are only the public source files in the GitHub repo
at the build's commit. ANSCORE compiles against ORT_API_VERSION via these
headers, so this script pulls the exact header set for -Commit and copies in a
matching onnxruntime.lib, producing a ready-to-install SDK folder.
Default -Commit 058787c matches SharedCore\ep\cuda (1.24.20260218) AND the
onnxruntime.lib already on disk under LabellingTool\vendor (same build).
.PARAMETER Commit
ORT source commit/branch/tag to fetch headers from. Default: 058787c (API 24).
.PARAMETER OutDir
Where to assemble the SDK. Default: %TEMP%\ort_sdk_<commit>.
.PARAMETER LibSource
Folder containing onnxruntime.lib (+ onnxruntime_providers_shared.lib) of the
SAME build. Default: the LabellingTool vendor folder found on this machine.
.EXAMPLE
.\fetch_ort_headers.ps1
.\bump_ort_sdk.ps1 -SdkPath $env:TEMP\ort_sdk_058787c -Marker runtime1.24.0_openvino2025.3.0.txt -WhatIf
#>
[CmdletBinding()]
param(
[string] $Commit = "058787c",
[string] $OutDir,
[string] $LibSource = "C:\Projects\CLionProjects\LabellingTool\vendor\onnxruntime-win\lib"
)
$ErrorActionPreference = "Stop"
$ProgressPreference = "SilentlyContinue"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
if (-not $OutDir) { $OutDir = Join-Path $env:TEMP "ort_sdk_$Commit" }
$incDir = Join-Path $OutDir 'include'
$libDir = Join-Path $OutDir 'lib'
New-Item -ItemType Directory -Force -Path $incDir, $libDir | Out-Null
$base = "https://raw.githubusercontent.com/microsoft/onnxruntime/$Commit/include/onnxruntime/core"
# Header set ANSCORE links against (mirrors ANSLibs, minus unused training headers).
# Each entry: subpath under .../core/ -> flattened into include\
$headers = @(
"session/onnxruntime_c_api.h",
"session/onnxruntime_cxx_api.h",
"session/onnxruntime_cxx_inline.h",
"session/onnxruntime_float16.h",
"session/onnxruntime_lite_custom_op.h",
"session/onnxruntime_run_options_config_keys.h",
"session/onnxruntime_session_options_config_keys.h",
"session/onnxruntime_ep_c_api.h",
"session/onnxruntime_ep_device_ep_metadata_keys.h",
"providers/cpu/cpu_provider_factory.h",
"framework/provider_options.h"
)
Write-Host "== Fetching ORT headers @ $Commit -> $incDir =="
$ok = 0; $fail = @()
foreach ($h in $headers) {
$name = Split-Path $h -Leaf
$url = "$base/$h"
try {
Invoke-WebRequest -Uri $url -OutFile (Join-Path $incDir $name) -UseBasicParsing
# Guard against a 200-with-HTML (GitHub 404 page) sneaking through
$first = Get-Content -LiteralPath (Join-Path $incDir $name) -TotalCount 1 -ErrorAction SilentlyContinue
if ($first -match '^\s*<!DOCTYPE|^\s*<html') { throw "got HTML, not a header" }
Write-Host (" OK {0}" -f $name) -ForegroundColor Green
$ok++
} catch {
Write-Host (" FAIL {0} ({1})" -f $name, $_.Exception.Message) -ForegroundColor Red
$fail += $name
}
}
# Verify the new API version actually parsed
$apiLine = Select-String -LiteralPath (Join-Path $incDir 'onnxruntime_c_api.h') `
-Pattern 'define\s+ORT_API_VERSION\s+(\d+)' -ErrorAction SilentlyContinue | Select-Object -First 1
$api = if ($apiLine) { $apiLine.Matches[0].Groups[1].Value } else { '??' }
Write-Host (" ORT_API_VERSION in fetched headers: {0}" -f $api)
Write-Host ""
Write-Host "== Copying import libs from: $LibSource =="
$libsOk = $true
foreach ($l in @('onnxruntime.lib','onnxruntime_providers_shared.lib')) {
$src = Join-Path $LibSource $l
if (Test-Path -LiteralPath $src) {
Copy-Item -LiteralPath $src -Destination (Join-Path $libDir $l) -Force
Write-Host (" OK {0}" -f $l) -ForegroundColor Green
} else {
Write-Host (" MISSING {0} in {1}" -f $l, $LibSource) -ForegroundColor Yellow
if ($l -eq 'onnxruntime.lib') { $libsOk = $false }
}
}
Write-Host ""
if ($fail.Count -eq 0 -and $libsOk) {
Write-Host "== SDK assembled OK ==" -ForegroundColor Green
Write-Host (" $OutDir (include\ = {0} headers @ API {1}, lib\ = import libs)" -f $ok, $api)
Write-Host " Next:" -ForegroundColor Cyan
Write-Host (" .\bump_ort_sdk.ps1 -SdkPath `"$OutDir`" -Marker runtime1.24.0_openvino2025.3.0.txt -WhatIf")
exit 0
} else {
Write-Host "== INCOMPLETE ==" -ForegroundColor Red
if ($fail.Count) { Write-Host (" headers failed: {0}" -f ($fail -join ', ')) -ForegroundColor Red }
if (-not $libsOk) { Write-Host " onnxruntime.lib not found - set -LibSource to a folder that has it." -ForegroundColor Red }
exit 1
}
+6
View File
@@ -0,0 +1,6 @@
@echo off
REM Rebuild ANSCORE (Release/Ninja) with the MSVC+CUDA env set up.
call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat" >nul 2>nul
cd /d "C:\Projects\CLionProjects\ANSCORE"
"C:\Program Files\CMake\bin\cmake.exe" --build cmake-build-release --parallel
exit /b %errorlevel%
+243
View File
@@ -0,0 +1,243 @@
<#
.SYNOPSIS
Acceptance gate for an ANSCENTER ep\ ONNX Runtime tree. Returns exit code 0
(all PASS) or 1 (any FAIL) so it can be wired into CI / a release checklist.
.DESCRIPTION
Run this after swapping any onnxruntime*.dll in SharedCore\ep (e.g. when
unifying all EP folders onto a single ORT version). It enforces the three
rules that make an ep\ tree correct for ANSCORE:
CHECK 1 - API floor. Each EP folder's core onnxruntime.dll must expose an
ORT C-API version >= the version ANSCORE links against
(-RequiredApi, default 23 for ORT 1.23). ORT is backward
compatible, so a NEWER core (1.24 => API 24) is fine; an OLDER
one is not. API is derived from the file version (1.<minor> =>
API <minor>).
CHECK 2 - Provider/core lock. Every onnxruntime_providers_*.dll in a
folder must have the EXACT same file version as that folder's
onnxruntime.dll. ORT provider DLLs are version-locked to the core;
a mismatch is a silent crash waiting to happen.
CHECK 3 - Backend resolution. Every CUDA / cuDNN / TensorRT / OpenVINO /
DirectML DLL imported by the core or its providers must exist in
the ep folder, the Shared root, or System32.
It also prints a UNIFICATION summary (how many distinct core versions exist
across the folders) so you can see when you've reached one-version-everywhere.
.PARAMETER EpRoot
The ep\ folder to verify. Default: C:\Projects\SharedCore\ep
(You may also pass the Shared root itself; the script appends \ep if needed.)
.PARAMETER SharedRoot
The Shared folder that holds the backend DLLs (nvinfer_10.dll, openvino.dll,
cudart64_13.dll, DirectML.dll, ...). Default: parent of EpRoot.
.PARAMETER RequiredApi
Minimum acceptable ORT C-API version. Default: 23 (ANSCORE links ORT 1.23).
.EXAMPLE
.\verify_ep.ps1
.\verify_ep.ps1 -EpRoot C:\ProgramData\ANSCENTER\Shared\ep
.\verify_ep.ps1 -RequiredApi 24 # after bumping ANSCORE's link SDK to 1.24
#>
[CmdletBinding()]
param(
[string] $EpRoot = "C:\Projects\SharedCore\ep",
[string] $SharedRoot,
[int] $RequiredApi = 23
)
$ErrorActionPreference = "Stop"
# ---------------------------------------------------------------------------
# Helpers (kept consistent with Check-SharedCoreVersions.ps1)
# ---------------------------------------------------------------------------
function Coalesce($value, $fallback) { if ($value) { $value } else { $fallback } }
function Get-FileVer([string]$path) {
if (Test-Path -LiteralPath $path) { (Get-Item -LiteralPath $path).VersionInfo.FileVersion }
else { $null }
}
function Find-Dumpbin {
$c = Get-ChildItem `
"C:\Program Files\Microsoft Visual Studio\*\*\VC\Tools\MSVC\*\bin\Hostx64\x64\dumpbin.exe", `
"C:\Program Files (x86)\Microsoft Visual Studio\*\*\VC\Tools\MSVC\*\bin\Hostx64\x64\dumpbin.exe" `
-ErrorAction SilentlyContinue | Sort-Object FullName -Descending
if ($c) { return $c[0].FullName }
return $null
}
# Derive the ORT C-API version from a file version string. ORT 1.<minor> exposes
# C-API version <minor> (1.23.* => 23, 1.24.* => 24). Returns $null if unparsable.
function Get-OrtApi([string]$fileVer) {
if (-not $fileVer) { return $null }
$parts = $fileVer.Split('.')
if ($parts.Count -lt 2) { return $null }
$minor = 0
if ([int]::TryParse($parts[1], [ref]$minor)) { return $minor }
return $null
}
# Lower-cased CUDA/TRT/OV/DML deps imported by $path (via dumpbin /DEPENDENTS).
function Get-BackendDeps([string]$dumpbin, [string]$path) {
if (-not $dumpbin -or -not (Test-Path -LiteralPath $path)) { return @() }
$rx = '(?i)cudart|cublas|cudnn|cufft|cusparse|cusolver|nvinfer|nvonnx|nvrtc|directml|openvino'
& $dumpbin /DEPENDENTS $path 2>$null |
Select-String -Pattern '^\s+\S+\.dll\s*$' |
ForEach-Object { $_.Line.Trim().ToLower() } |
Where-Object { $_ -match $rx } |
Sort-Object -Unique
}
# ---------------------------------------------------------------------------
# Resolve paths
# ---------------------------------------------------------------------------
if (-not (Test-Path -LiteralPath $EpRoot)) {
Write-Host "ERROR: EpRoot not found: $EpRoot" -ForegroundColor Red; exit 2
}
# Allow passing the Shared root instead of the ep folder.
if ((Split-Path $EpRoot -Leaf) -ine 'ep' -and (Test-Path -LiteralPath (Join-Path $EpRoot 'ep'))) {
$EpRoot = Join-Path $EpRoot 'ep'
}
if (-not $SharedRoot) { $SharedRoot = Split-Path $EpRoot -Parent }
$sys32 = Join-Path $env:WINDIR 'System32'
$dumpbin = Find-Dumpbin
# Providers expected per EP (DML is baked into the core; CPU has none).
$epProviders = [ordered]@{
"cpu" = @()
"cuda" = @("onnxruntime_providers_cuda.dll","onnxruntime_providers_tensorrt.dll")
"directml" = @()
"openvino" = @("onnxruntime_providers_openvino.dll")
}
Write-Host ""
Write-Host "=================================================================="
Write-Host " verify_ep - ANSCORE ONNX Runtime acceptance gate"
Write-Host " ep root : $EpRoot"
Write-Host " shared root : $SharedRoot"
Write-Host " required API : >= $RequiredApi (ANSCORE link version)"
Write-Host " dumpbin : $(Coalesce $dumpbin 'NOT FOUND - CHECK 3 will be skipped')"
Write-Host "=================================================================="
$overallFail = $false
$coreVersions = @{} # ep -> core file version (for unification summary)
foreach ($ep in $epProviders.Keys) {
$epDir = Join-Path $EpRoot $ep
$core = Join-Path $epDir 'onnxruntime.dll'
Write-Host ""
Write-Host ("-- ep\{0} ------------------------------------------------------" -f $ep)
if (-not (Test-Path -LiteralPath $core)) {
if (Test-Path -LiteralPath $epDir) {
Write-Host " SKIP: folder present but no onnxruntime.dll" -ForegroundColor DarkYellow
} else {
Write-Host " SKIP: folder absent" -ForegroundColor DarkGray
}
continue
}
$coreVer = Get-FileVer $core
$coreVersions[$ep] = $coreVer
$epFail = $false
# ----- CHECK 1: API floor ------------------------------------------------
$api = Get-OrtApi $coreVer
if ($null -eq $api) {
Write-Host (" CHECK 1 API : FAIL - cannot parse version '{0}'" -f $coreVer) -ForegroundColor Red
$epFail = $true
} elseif ($api -lt $RequiredApi) {
Write-Host (" CHECK 1 API : FAIL - core {0} => API {1} < required {2}" -f $coreVer,$api,$RequiredApi) -ForegroundColor Red
$epFail = $true
} else {
Write-Host (" CHECK 1 API : PASS - core {0} => API {1} (>= {2})" -f $coreVer,$api,$RequiredApi) -ForegroundColor Green
}
# ----- CHECK 2: provider/core version lock -------------------------------
$providers = Get-ChildItem -LiteralPath $epDir -Filter 'onnxruntime_providers_*.dll' -ErrorAction SilentlyContinue
if (-not $providers) {
Write-Host " CHECK 2 LOCK : PASS - no provider DLLs in folder (core-only EP)" -ForegroundColor Green
} else {
$bad = @()
foreach ($p in $providers) {
$pv = $p.VersionInfo.FileVersion
if ($pv -ne $coreVer) { $bad += ("{0} ({1})" -f $p.Name, (Coalesce $pv 'no-version')) }
}
if ($bad) {
Write-Host (" CHECK 2 LOCK : FAIL - version != core {0}: {1}" -f $coreVer, ($bad -join ', ')) -ForegroundColor Red
$epFail = $true
} else {
Write-Host (" CHECK 2 LOCK : PASS - {0} provider DLL(s) all == {1}" -f $providers.Count, $coreVer) -ForegroundColor Green
}
}
# ----- CHECK 3: backend resolution ---------------------------------------
if (-not $dumpbin) {
Write-Host " CHECK 3 DEPS : SKIP - dumpbin not available" -ForegroundColor DarkYellow
} else {
$checkFiles = @('onnxruntime.dll') + $epProviders[$ep]
$deps = @()
foreach ($cf in $checkFiles) { $deps += Get-BackendDeps $dumpbin (Join-Path $epDir $cf) }
$deps = $deps | Sort-Object -Unique
if (-not $deps) {
Write-Host " CHECK 3 DEPS : PASS - no CUDA/TRT/OV/DML imports" -ForegroundColor Green
} else {
$missing = @()
foreach ($d in $deps) {
$found = (Test-Path -LiteralPath (Join-Path $epDir $d)) -or `
(Test-Path -LiteralPath (Join-Path $SharedRoot $d)) -or `
(Test-Path -LiteralPath (Join-Path $sys32 $d))
if (-not $found) { $missing += $d }
}
if ($missing) {
Write-Host (" CHECK 3 DEPS : FAIL - missing: {0}" -f ($missing -join ', ')) -ForegroundColor Red
Write-Host (" (imports: {0})" -f ($deps -join ', ')) -ForegroundColor DarkGray
$epFail = $true
} else {
Write-Host (" CHECK 3 DEPS : PASS - all {0} backend import(s) resolved" -f $deps.Count) -ForegroundColor Green
Write-Host (" {0}" -f ($deps -join ', ')) -ForegroundColor DarkGray
}
}
}
if ($epFail) { $overallFail = $true }
}
# ---------------------------------------------------------------------------
# Unification summary
# ---------------------------------------------------------------------------
Write-Host ""
Write-Host "-- Unification summary --------------------------------------------"
$distinct = $coreVersions.Values | Where-Object { $_ } | Sort-Object -Unique
if ($distinct.Count -eq 0) {
Write-Host " No ORT cores found." -ForegroundColor Red
$overallFail = $true
} elseif ($distinct.Count -eq 1) {
Write-Host (" UNIFIED: all EP folders on a single ORT version ({0})." -f $distinct[0]) -ForegroundColor Green
} else {
Write-Host (" {0} distinct ORT versions across folders:" -f $distinct.Count) -ForegroundColor Yellow
foreach ($ep in $coreVersions.Keys) {
Write-Host (" ep\{0,-9} {1}" -f $ep, $coreVersions[$ep])
}
Write-Host " Safe at runtime (one EP per process) but not yet unified - see tools recommendation." -ForegroundColor Yellow
}
# ---------------------------------------------------------------------------
# Verdict
# ---------------------------------------------------------------------------
Write-Host ""
Write-Host "=================================================================="
if ($overallFail) {
Write-Host " RESULT: FAIL - one or more EP folders did not pass." -ForegroundColor Red
Write-Host "=================================================================="
exit 1
} else {
Write-Host " RESULT: PASS - ep\ tree is correct for ANSCORE." -ForegroundColor Green
Write-Host "=================================================================="
exit 0
}