Fix: React to slow-frame congestion and keep remote audio recoverable

The adaptive quality controller counted only complete frames per second, so when
a single frame took several seconds to arrive the sample was 0 fps and the
throughput criterion abstained forever: Ultra and High kept flipping while a
foreground video played. The audio watchdog had the opposite problem: it rebuilt
the WASAPI capture every three seconds on an idle endpoint, and its sleep
detector measured the whole loop including a blocking send.

Sample the last ten seconds in a ring buffer and feed the criterion a windowed
rate, guarded so a scroll burst, a stale window or a few KB clock patch is not
mistaken for congestion. Apply the same per-frame floor to the one-second
fallback and to the post-downgrade local-bottleneck probe. Normalize delayed
timer ticks by real elapsed milliseconds, and make the 40 second
upgrade-failure window outlast the resolution cooldown it has to cross.

On the client, rebuild only when an audio session is actually rendering, and
rebuild immediately when the captured endpoint is no longer the default output;
refresh the sleep timestamps on failed as well as successful init.

Tests cover the window and fallback helpers directly; the full suite passes
845/845, and both the Debug server and Debug client build.

Co-Authored-By: deepseek-flash
This commit was merged in pull request #6.
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-08 11:45:19 +01:00
1 parent 2a97471b51
commit 5f40f3726c
6 files changed
+688 -31

No files matched your search

+35
View File
@@ -390,6 +390,20 @@ public:
ULONG m_ulFramesLastSecond = 0; // 上一秒的原始帧数(吞吐判据用,不用 EMA)
double m_dFrameRate = 0; // 平滑后的帧率 (FPS)
// 吞吐判据的输入样本:窗口均值和 1 秒样本二选一,一次评估只采一次,
// 判据和「本机瓶颈探测」都用同一份 —— 两处各采各的会出现
// 「窗口说拥塞、1 秒样本说没事」交替,确认计数永远凑不齐(见 CollectLoadSample)。
struct LoadSample {
int fps = 0; // 帧率,100 倍表示(窗口与 1 秒样本统一比例,见 CollectLoadSample)
int baseline = 0; // 基线(与 fps 同比例)
int kbps = 0;
bool usable = false; // false = 两个样本都不可用,判据必须弃权
bool windowed = false; // true = 用的是窗口均值
int seconds = 0; // 窗口秒数(windowed 时有效,日志用)
unsigned long long frames = 0; // 窗口内帧数(日志用)
};
LoadSample CollectLoadSample();
// 自适应质量
struct {
bool enabled = false; // 是否启用自适应 (默认关闭)
@@ -405,6 +419,27 @@ public:
int loadCount = 0; // 吞吐判据的确认计数(字节速率抖动大,比 RTT 多等一拍)
ULONGLONG loadProbeTime = 0; // 降档后的观察起点,0=未在观察
bool loadPinned = false; // 判定瓶颈在本机,本次会话停用吞吐判据
ULONGLONG lastLoadLogTime = 0; // 吞吐判据日志的上次打印时刻(只用于限流,不参与判定)
// ===== 慢流量采样 =====
// 整帧收全才计数,所以「一帧传好几秒」的现场每秒帧数都是 0,1 秒窗口采不到
// 样本。按秒保留原始样本,窗口可用时(判据见 GetCongestedLoadSample)优先用它,
// 否则退回 1 秒样本,见 EvaluateQuality。
enum { LOAD_WINDOW_SECONDS = 10 };
ULONG loadWindowFrames[LOAD_WINDOW_SECONDS] = {};
ULONG loadWindowBytes[LOAD_WINDOW_SECONDS] = {};
// 每格实际覆盖的毫秒数:WM_TIMER 会被 UI 线程的阻塞推迟,按「一格 = 一秒」
// 做分母会把帧率/吞吐整体高估(推迟 3 秒的格子会把速率算成 3 倍)。
ULONG loadWindowMs[LOAD_WINDOW_SECONDS] = {};
// 上一格(1 秒样本)实际覆盖的毫秒数;0 = 这一格不可信(采样断档),
// 判据和本机瓶颈探测这次都弃权。
ULONG lastLoadSampleMs = 0;
int loadWindowPos = 0; // 下一个写入位置
int loadWindowFilled = 0; // 已填入的有效秒数(窗口未满时用它做分母)
ULONGLONG loadWindowLastTick = 0; // 上次采样的时刻(识别采样断档:休眠/挂起)
// 最近一次收全帧的时刻(区分「内容停了」)。写方是网络收包线程、读方是 UI
// 线程,用 32 位的 GetTickCount 而不是 64 位:x86 上一次未加锁的 64 位读
// 可能被撕裂成两个时刻,算出的帧龄会瞬间变成天文数字或负数。
volatile DWORD lastFrameTick = 0;
// ===== 防振荡状态(只双判据模式下使用)=====
// 升档失败封顶:升档后短时间内又降回来,说明那次升档是错的,
// 在 ceilingUntil 之前不再尝试好于 levelCeiling 的档位(-1 = 无封顶)。