Add jitter buffer
This commit is contained in:
+107
-15
@@ -28,6 +28,51 @@ extern "C"
|
||||
// Note: per-instance thread safety is handled by ANSRTSPClient::_mutex
|
||||
// Mat registry thread safety is handled by anscv_mat_replace's internal registry_mutex
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Freeze diagnostics — once-per-second pulse for ANSRTSPClient::GetImage so
|
||||
// we can see *which* return path fired during a 1-2s freeze. Filter on
|
||||
// "RTSP_Pulse" in DebugView. Pairs with the producer/consumer pulse in
|
||||
// video_player.cpp ("MEDIA_Pulse"): together they let you localize a stall
|
||||
// to producer (decoder) vs internal player vs ANSRTSP wrapper.
|
||||
// ---------------------------------------------------------------------------
|
||||
namespace {
|
||||
std::atomic<uint64_t> g_rtspPulseGetCalls{0}; // entries to ANSRTSPClient::GetImage
|
||||
std::atomic<uint64_t> g_rtspPulseNotPlaying{0}; // !_isPlaying
|
||||
std::atomic<uint64_t> g_rtspPulseEarlyStale{0}; // ageMs >= 5000 → cached early
|
||||
std::atomic<uint64_t> g_rtspPulseInnerEmpty{0}; // _playerClient->getImage returned empty
|
||||
std::atomic<uint64_t> g_rtspPulseDupPts{0}; // currentPts == _pts → cached
|
||||
std::atomic<uint64_t> g_rtspPulseNewFrame{0}; // brand-new frame returned
|
||||
std::atomic<long long> g_rtspPulseNextEmitTick{0};
|
||||
|
||||
inline void emit_rtsp_pulse_if_due(double ageMsLastFrame) {
|
||||
using clk = std::chrono::steady_clock;
|
||||
const long long tick = clk::now().time_since_epoch().count();
|
||||
long long expected = g_rtspPulseNextEmitTick.load(std::memory_order_relaxed);
|
||||
if (tick < expected) return;
|
||||
const long long deadline = tick + std::chrono::duration_cast<clk::duration>(
|
||||
std::chrono::milliseconds(1000)).count();
|
||||
if (!g_rtspPulseNextEmitTick.compare_exchange_strong(
|
||||
expected, deadline, std::memory_order_relaxed)) {
|
||||
return;
|
||||
}
|
||||
const uint64_t calls = g_rtspPulseGetCalls.exchange(0, std::memory_order_relaxed);
|
||||
const uint64_t np = g_rtspPulseNotPlaying.exchange(0, std::memory_order_relaxed);
|
||||
const uint64_t es = g_rtspPulseEarlyStale.exchange(0, std::memory_order_relaxed);
|
||||
const uint64_t ie = g_rtspPulseInnerEmpty.exchange(0, std::memory_order_relaxed);
|
||||
const uint64_t dp = g_rtspPulseDupPts.exchange(0, std::memory_order_relaxed);
|
||||
const uint64_t nf = g_rtspPulseNewFrame.exchange(0, std::memory_order_relaxed);
|
||||
// Production: route through ANS_DBG so the pulse is gated by the
|
||||
// ANSCORE_DEBUGVIEW runtime toggle. Silent by default.
|
||||
ANS_DBG("RTSP_Pulse",
|
||||
"[1s] GetImage calls=%llu new=%llu cached(notPlay=%llu earlyStale=%llu "
|
||||
"innerEmpty=%llu dupPts=%llu) lastFrameAge=%.0fms",
|
||||
(unsigned long long)calls, (unsigned long long)nf,
|
||||
(unsigned long long)np, (unsigned long long)es,
|
||||
(unsigned long long)ie, (unsigned long long)dp,
|
||||
ageMsLastFrame);
|
||||
}
|
||||
} // anonymous namespace
|
||||
|
||||
// Debug logging. Define ANSCORE_GPU_DEBUG=1 to enable verbose per-frame logging.
|
||||
#ifndef RTSP_DBG
|
||||
#if defined(ANSCORE_GPU_DEBUG) && ANSCORE_GPU_DEBUG
|
||||
@@ -450,11 +495,17 @@ namespace ANSCENTER {
|
||||
cv::Mat ANSRTSPClient::GetImage(int& width, int& height, int64_t& pts) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
|
||||
// RTSP_Pulse: count entry, attempt once-per-second emit on every call so
|
||||
// the pulse keeps printing even when every path returns cached.
|
||||
g_rtspPulseGetCalls.fetch_add(1, std::memory_order_relaxed);
|
||||
emit_rtsp_pulse_if_due(_playerClient ? _playerClient->getLastFrameAgeMs() : -1.0);
|
||||
|
||||
// Return last known frame if not playing
|
||||
if (!_isPlaying) {
|
||||
width = _imageWidth;
|
||||
height = _imageHeight;
|
||||
pts = _pts;
|
||||
g_rtspPulseNotPlaying.fetch_add(1, std::memory_order_relaxed);
|
||||
return _pLastFrame; // Shallow copy (fast)
|
||||
}
|
||||
|
||||
@@ -473,6 +524,7 @@ namespace ANSCENTER {
|
||||
width = _imageWidth;
|
||||
height = _imageHeight;
|
||||
pts = _pts;
|
||||
g_rtspPulseEarlyStale.fetch_add(1, std::memory_order_relaxed);
|
||||
return _pLastFrame;
|
||||
}
|
||||
}
|
||||
@@ -488,36 +540,40 @@ namespace ANSCENTER {
|
||||
width = _imageWidth;
|
||||
height = _imageHeight;
|
||||
pts = _pts;
|
||||
g_rtspPulseInnerEmpty.fetch_add(1, std::memory_order_relaxed);
|
||||
return _pLastFrame;
|
||||
}
|
||||
|
||||
// Use PTS to detect duplicate frames (much faster than pixel comparison)
|
||||
// The sequence-based check in getImage() already skips conversion for same frames,
|
||||
// so if PTS hasn't changed, it's the same frame — no need for expensive memcmp
|
||||
if (currentPts == _pts && !_pLastFrame.empty()) {
|
||||
// Duplicate-frame detection. We track the inner player's currentPts
|
||||
// (in _lastInnerPts) separately from our own _pts. This is important:
|
||||
// _pts gets synthetically advanced on every duplicate so LabVIEW's
|
||||
// staleness check sees a changing timestamp. If we compared currentPts
|
||||
// against _pts, the synthetic advance would make the comparison fail
|
||||
// on the next call and route the same cached frame through the
|
||||
// "new frame" path — pointless resize + storage on identical pixels.
|
||||
if (currentPts == _lastInnerPts && !_pLastFrame.empty()) {
|
||||
width = _imageWidth;
|
||||
height = _imageHeight;
|
||||
// Return timestamp based on decoder frame age so LabVIEW can distinguish
|
||||
// "rate-limited duplicate" from "camera truly stale".
|
||||
// If decoder is still receiving frames (age < 5s), advance PTS so LabVIEW
|
||||
// sees a changing timestamp and doesn't trigger false reconnect.
|
||||
// If decoder is stale (age > 5s), return same PTS so LabVIEW detects it.
|
||||
double ageMs = _playerClient->getLastFrameAgeMs();
|
||||
if (ageMs > 0.0 && ageMs < 5000.0) {
|
||||
// Camera alive but rate-limited — advance PTS to prevent false stale detection
|
||||
_pts++;
|
||||
_pts++; // synthetic advance — LabVIEW staleness signal
|
||||
} else if (ageMs >= 5000.0) {
|
||||
// Camera stale — keep same PTS so LabVIEW triggers reconnect
|
||||
ANS_DBG("RTSP_GetImage", "STALE PTS: ageMs=%.1f pts=%lld url=%s — not advancing PTS",
|
||||
ageMs, (long long)_pts, _url.c_str());
|
||||
}
|
||||
pts = _pts;
|
||||
g_rtspPulseDupPts.fetch_add(1, std::memory_order_relaxed);
|
||||
return _pLastFrame;
|
||||
}
|
||||
|
||||
// Update PTS first
|
||||
_pts = currentPts;
|
||||
pts = currentPts;
|
||||
// Inner advanced — real new frame. Sync our duplicate-detection cursor
|
||||
// and bump _pts monotonically (never assign to currentPts, which would
|
||||
// make _pts go backwards if it had been advanced past currentPts on
|
||||
// previous duplicates).
|
||||
_lastInnerPts = currentPts;
|
||||
_pts++;
|
||||
pts = _pts;
|
||||
g_rtspPulseNewFrame.fetch_add(1, std::memory_order_relaxed);
|
||||
|
||||
// Handle non-rotated case
|
||||
if (_imageRotateDeg == 0) {
|
||||
@@ -992,6 +1048,24 @@ namespace ANSCENTER {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
_playerClient->setTargetFPS(intervalMs); // 0=no limit, 100=~10FPS, 200=~5FPS
|
||||
}
|
||||
void ANSRTSPClient::SetRtpOverUdp(bool useUdp) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
// Forwards to CRtspClient::set_rtp_over_udp via CRtspPlayer. The TRUE/FALSE
|
||||
// mapping is inverted inside (rtp_tcp = useUdp ? 0 : 1) — see rtsp_cln.cpp:3578.
|
||||
_playerClient->setRtpOverUdp(useUdp ? TRUE : FALSE);
|
||||
ANS_DBG("RTSP_Lifecycle",
|
||||
"SetRtpOverUdp: useUdp=%d url=%s — must be called before Start()",
|
||||
(int)useUdp, _url.c_str());
|
||||
}
|
||||
void ANSRTSPClient::SetJitterBufferMs(int ms) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
_playerClient->setJitterBufferMs(ms);
|
||||
ANS_DBG("RTSP_Lifecycle",
|
||||
"SetJitterBufferMs: ms=%d url=%s", ms, _url.c_str());
|
||||
}
|
||||
int ANSRTSPClient::GetJitterBufferMs() const {
|
||||
return _playerClient->getJitterBufferMs();
|
||||
}
|
||||
void ANSRTSPClient::SetNV12FastPath(bool enable) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
_useNV12FastPath = enable;
|
||||
@@ -1585,6 +1659,24 @@ extern "C" __declspec(dllexport) void SetRTSPTargetFPS(ANSCENTER::ANSRTSPClient*
|
||||
(*Handle)->SetTargetFPS(intervalMs); // 0=no limit, 100=~10FPS, 200=~5FPS
|
||||
} catch (...) { }
|
||||
}
|
||||
extern "C" __declspec(dllexport) void SetRTSPRtpOverUdp(ANSCENTER::ANSRTSPClient** Handle, int useUdp) {
|
||||
if (Handle == nullptr || *Handle == nullptr) return;
|
||||
try {
|
||||
(*Handle)->SetRtpOverUdp(useUdp != 0);
|
||||
} catch (...) { }
|
||||
}
|
||||
extern "C" __declspec(dllexport) void SetRTSPJitterBuffer(ANSCENTER::ANSRTSPClient** Handle, int jitterMs) {
|
||||
if (Handle == nullptr || *Handle == nullptr) return;
|
||||
try {
|
||||
(*Handle)->SetJitterBufferMs(jitterMs);
|
||||
} catch (...) { }
|
||||
}
|
||||
extern "C" __declspec(dllexport) int GetRTSPJitterBuffer(ANSCENTER::ANSRTSPClient** Handle) {
|
||||
if (Handle == nullptr || *Handle == nullptr) return -1;
|
||||
try {
|
||||
return (*Handle)->GetJitterBufferMs();
|
||||
} catch (...) { return -1; }
|
||||
}
|
||||
extern "C" __declspec(dllexport) void SetRTSPNV12FastPath(ANSCENTER::ANSRTSPClient** Handle, int enable) {
|
||||
if (Handle == nullptr || *Handle == nullptr) return;
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user