Add jitter buffer

This commit is contained in:
2026-05-07 22:36:27 +10:00
parent 2e0fee5e3d
commit 75693cadd9
5 changed files with 575 additions and 70 deletions
+59 -17
View File
@@ -833,14 +833,36 @@ 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://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:AnsHome16%&@192.168.1.104:554";//"rtsp://alex:admin123@192.168.1.11:554";//112
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
CreateANSRTSPHandle(&rtspClient, "", "", "", rtspUrl.c_str());
//void SetRTSPImageQuality(ANSCENTER::ANSRTSPClient** Handle, int mode)
SetRTSPImageQuality(&rtspClient,0);
//SetBBoxRTSP(&rtspClient, 0, 0, 1920, 1080);
//SetCropFlagRTSP(&rtspClient, 1);
// Hardware decode (auto-pick: CUDA if available, otherwise D3D11VA/DXVA2).
// Without this the player uses single-threaded SW decode (default since
// commit e134ebd "Use software decoder by default"), which on 4K HEVC
// can take 80-120ms/frame — too slow for 25/30fps. The decoder falls
// behind, the RTP buffer fills, and frames arrive in bursts (the
// "smooth → 1-2s freeze → catch-up" pattern). VLC uses HW decode by
// default, which is why it plays the same camera smoothly.
// MUST be called before StartRTSP — decoder is constructed inside play().
SetRTSPHWDecoding(&rtspClient, /*HW_DECODING_AUTO=*/0, /*preferredGpu=*/-1);
// Transport: default is RTP-over-RTSP/TCP (interleaved). On a LAN, UDP
// avoids TCP head-of-line stalls. Over WAN/NAT it typically does NOT
// work (port forwarding usually only covers TCP/554), so leave commented
// out for remote cameras. MUST be called before StartRTSP.
//SetRTSPRtpOverUdp(&rtspClient, 1);
StartRTSP(&rtspClient);
// Let ANSRTSP downscale internally (INTER_LINEAR, ~5x faster than the
// INTER_LANCZOS4 the test was doing). Without this, a 4K source plus
// LANCZOS4 resize stretches each loop iteration to ~150ms and the
// 20-slot decoder queue overruns — frames look "skipped" because
// intermediate ones get evicted before getLatestFrame() sees them.
SetRTSPDisplayResolution(&rtspClient, 1920, 1080);
//SetRTSPImageRotation(&rtspClient,0);
cv::namedWindow("Image", cv::WINDOW_NORMAL); // Create a resizable window.
cv::resizeWindow("Image", 1920, 1080); // Set initial size of the window (landscape).
@@ -854,12 +876,13 @@ int RSTPTestCVClient() {
//if ((index == 1800) || (index == 2200) || (index == 2500)) { StartRTSP(&rtspClient); }
//if ((index == 2000) || (index == 2300) || (index == 2700)) { StartRTSP(&rtspClient); }
//if (index > 20000) break;
auto start = std::chrono::system_clock::now();
auto loopStart = std::chrono::steady_clock::now();
auto getStart = loopStart;
cv::Mat* image = nullptr; // ✅ Use a pointer to hold the allocated image
GetRTSPCVImage(&rtspClient, width, height, pts,&image);
auto end1 = std::chrono::system_clock::now();
auto elapsed1 = std::chrono::duration_cast<std::chrono::milliseconds>(end1 - start);
if (elapsed1.count() > 0)std::cout << "Time to get image:" << elapsed1.count() << "ms" << std::endl;
auto getEnd = std::chrono::steady_clock::now();
auto getMs = std::chrono::duration_cast<std::chrono::milliseconds>(getEnd - getStart).count();
if (getMs > 0) std::cout << "Time to get image:" << getMs << "ms" << std::endl;
// ✅ Check if the image is valid BEFORE accessing it
if (!image || image->empty()) {
@@ -868,23 +891,42 @@ int RSTPTestCVClient() {
continue; // Skip processing if the image is empty
}
// High-quality downscale for display: INTER_LANCZOS4 preserves sharpness and edges
cv::Mat displayImage;
if (image->cols > 1920) {
double scale = 1920.0 / image->cols;
cv::resize(*image, displayImage, cv::Size(), scale, scale, cv::INTER_LANCZOS4);
}
else {
displayImage = *image;
}
cv::imshow("Image", displayImage);
// ANSRTSP already returned a 1920x1080 frame — no client-side resize.
// Time the imshow specifically — on Windows it can occasionally block
// for 100s of ms (window message pump, GPU sync, monitor refresh
// alignment), and that would visibly look like a freeze even though
// the producer pulse stays healthy.
auto showStart = std::chrono::steady_clock::now();
cv::imshow("Image", *image);
auto showEnd = std::chrono::steady_clock::now();
auto showMs = std::chrono::duration_cast<std::chrono::milliseconds>(showEnd - showStart).count();
ANSCV_ReleaseImage_S(&image);
//std::cout << "Index="<<index<<". Size: " << width << "x" << height << ". Timestamp: " << pts << std::endl;
if (cv::waitKey(30) == 27) {
// Time waitKey separately — also a Windows GUI pump, can block.
auto waitStart = std::chrono::steady_clock::now();
// 1ms is enough to pump GUI events; 30ms caps the loop at <33fps and
// is the main reason the camera-timer appeared to skip.
bool quit = (cv::waitKey(1) == 27);
auto waitEnd = std::chrono::steady_clock::now();
auto waitMs = std::chrono::duration_cast<std::chrono::milliseconds>(waitEnd - waitStart).count();
// Print only when something is actually slow — keeps the log readable.
// Threshold: 30ms = noticeable stutter, 100ms = visible freeze contributor.
if (showMs >= 30 || waitMs >= 30) {
std::cout << "[SLOW DISPLAY] imshow=" << showMs << "ms waitKey=" << waitMs
<< "ms (frame=" << index << ")" << std::endl;
}
if (quit) {
std::cout << "Break" << std::endl;
break;
}
// Whole-iteration timing — should now be close to 1/cameraFps.
auto loopMs = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - loopStart).count();
if (loopMs >= 50) std::cout << "Loop iteration: " << loopMs << "ms" << std::endl;
}
cv::destroyAllWindows(); // Destroy all OpenCV windows
StopRTSP(&rtspClient);