Add jitter buffer
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
#include "media_util.h"
|
||||
#include <list>
|
||||
#include <string>
|
||||
#include <array>
|
||||
#include <opencv2/imgproc.hpp>
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <opencv2/opencv.hpp>
|
||||
@@ -51,6 +52,16 @@ private:
|
||||
uint64_t m_frameSeq = 0; // Sequence counter — increments on each new frame push
|
||||
|
||||
public:
|
||||
// Pulse counter for evicted-unread frames (instrumentation, not behavior).
|
||||
// Incremented every time a push has to drop the oldest frame because the
|
||||
// queue hit maxFrames AND the oldest never got read. With getLatestFrame()
|
||||
// the consumer reads the back, so anything pushed before the consumer's
|
||||
// last read sits in the middle/front and gets silently evicted here.
|
||||
static std::atomic<uint64_t>& evictedCounter() {
|
||||
static std::atomic<uint64_t> g{0};
|
||||
return g;
|
||||
}
|
||||
|
||||
// Push a new frame to the queue (removes oldest if full)
|
||||
void pushFrame(AVFrame* newFrame) {
|
||||
if (!newFrame) return;
|
||||
@@ -74,6 +85,7 @@ public:
|
||||
frameQueue.pop();
|
||||
av_frame_free(&oldFrame);
|
||||
g_queueFrees.fetch_add(1, std::memory_order_relaxed);
|
||||
evictedCounter().fetch_add(1, std::memory_order_relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,6 +189,16 @@ public:
|
||||
virtual void setImageQuality(int mode) { m_nImageQuality = mode; }
|
||||
void setTargetFPS(double intervalMs); // Set minimum interval between processed frames in ms (0 = no limit, 100 = ~10 FPS)
|
||||
double getLastFrameAgeMs(); // Milliseconds since last frame arrived from decoder (0 if no frame yet)
|
||||
// Jitter buffer: hold decoded frames for ~ms before delivering to consumer.
|
||||
// Smooths network/decoder jitter at the cost of added end-to-end latency.
|
||||
// 0 disables (today's getLatest semantics). Default 200 ms.
|
||||
// Adaptive: if the consumer is slower than the producer, drop-ahead kicks in
|
||||
// and bypasses the buffer (latency stays ~0 frames).
|
||||
// Note: applies only to the BGR cv::Mat returned by getImage(); the NV12/CUDA
|
||||
// fast-path frames (m_currentNV12Frame / m_currentCudaHWFrame) remain
|
||||
// updated at push time so inference always sees the latest.
|
||||
void setJitterBufferMs(int ms);
|
||||
int getJitterBufferMs() const { return m_jitterBufferMs; }
|
||||
virtual void setRtpMulticast(BOOL flag) {}
|
||||
virtual void setRtpOverUdp(BOOL flag) {}
|
||||
|
||||
@@ -317,6 +339,31 @@ protected:
|
||||
std::chrono::steady_clock::time_point m_lastDecoderFrameTime;
|
||||
bool m_lastDecoderFrameTimeSet = false;
|
||||
|
||||
// --- Jitter buffer (BGR delivery only) ---
|
||||
// Default 200 ms — absorbs typical network/decoder hiccups.
|
||||
int m_jitterBufferMs = 200;
|
||||
// Observed inter-arrival interval, ms. Seeds at 40 ms (=25 fps) and is
|
||||
// refined to a rolling median of the last m_arrivalCount samples.
|
||||
double m_observedFrameIntervalMs = 40.0;
|
||||
// Ring buffer of recent arrival ticks (steady_clock raw ticks). Used to
|
||||
// compute the rolling median frame interval. Sized at 32 — covers >1 s at
|
||||
// 30 fps so a single GOP-boundary jitter doesn't dominate.
|
||||
static constexpr size_t kArrivalRingSize = 32;
|
||||
std::array<long long, kArrivalRingSize> m_arrivalTicks{};
|
||||
size_t m_arrivalIdx = 0;
|
||||
size_t m_arrivalCount = 0;
|
||||
// Wall-clock of the last frame released to the consumer. Used for rate-paced
|
||||
// delivery — if the consumer polls faster than the producer rate, between
|
||||
// releases we return the cached frame so the buffer doesn't drain instantly.
|
||||
std::chrono::steady_clock::time_point m_lastReleaseTime;
|
||||
bool m_lastReleaseTimeSet = false;
|
||||
// Fill-phase: when true, hold releases until queue depth reaches target.
|
||||
// Set true at startup and again whenever the queue drains to empty mid-stream.
|
||||
// This is what makes the buffer actually buffer — without it, pacing alone
|
||||
// can't grow the queue from empty (producer and consumer would just match
|
||||
// rates with depth=0 forever).
|
||||
bool m_jitterBufferRefilling = true;
|
||||
|
||||
BOOL m_bPlaying;
|
||||
BOOL m_bPaused;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user