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

+130
View File
@@ -1549,6 +1549,136 @@ inline int GetTargetQualityLevelByLoad(int fps, int baselineFps, int kbps, int n
return QUALITY_MINIMAL;
}
// 慢流量的吞吐采样:把「最近 seconds 秒累计的帧数/字节数」换算成吞吐判据要的三个数。
//
// 为什么需要窗口:大帧把链路堵住时,一帧要好几秒才发得完,而控制端只在整帧收全后
// 才计数(ScreenSpyDlg.cpp 的 m_ulFramesThisSecond)—— 这类现场每秒的帧数都是 0,
// 1 秒窗口的样本为空,判据只能弃权;偶尔收到一帧的那一秒又被下一 0 帧秒清零,
// 连续 3 次永远凑不齐,档位一次也降不下来。用窗口均值才能表达 0.2fps 这种速率。
//
// 帧率与基线同时放大 100 倍:判据里只用两者的比值(帧率缺口、每帧字节数),
// 两边同乘不改变结论,但避开了整数帧率把 0.3fps 截成 0 的问题。
// kbps 用 1024 进制,与调用方 m_dScreenKBps * 8 的口径保持一致。
inline void GetWindowedLoadSample(unsigned long long frames,
unsigned long long bytes,
int seconds,
int baselineFps,
int& fps, int& baseline, int& kbps)
{
if (seconds <= 0) {
fps = 0;
baseline = 0;
kbps = 0;
return;
}
fps = (int)(frames * 100 / (unsigned long long)seconds);
baseline = baselineFps * 100;
kbps = (int)(bytes * 8 / (1024ULL * (unsigned long long)seconds));
}
// 慢流量窗口能不能作为「链路拥塞」的证据。
//
// 窗口均值本身分不清「链路交付不动」和「内容本来就是一阵一阵的」,直接喂给
// GetTargetQualityLevelByLoad 会误降档。以下三条必须同时成立:
//
// ① 窗口里至少 3 帧,且分布在至少 3 个不同的秒里:一两个大帧只是内容闪了一下
// (滚动一下、开个窗口)。「3 个不同的秒」专门挡「3 帧挤在同一秒」的短促突发
// —— 那种现场窗口均值同样是低帧率,但链路一秒就交完了,不是交付不动。
// 真拥塞时一帧要好几秒,帧必然散落在大半窗口里;代价是 0.3fps 这种极慢流
// 偶尔凑不满 3 个秒(两帧落在同一秒),这一拍弃权,下一拍窗口滑动后会再判。
// ② 最后一帧就在 4 秒内:滚动/播放停下之后画面静止,客户端 memcmp 跳帧,
// 窗口里剩下的是停顿前那段流量 —— 均值同样是「低帧率 + 高吞吐」,但这说明
// 内容停了,不是链路慢。真拥塞时帧在持续到达(一帧几秒),不会触发这一条。
// ③ 没有任何一秒交付过接近满速的帧:滚动 2 秒发 20 帧然后停住,10 秒窗口把
// 5fps 摊成 1fps;但这种现场里总有一秒交付过 ≥0.6 基线的帧 —— 链路明明
// 能交付满速,就不是带宽问题(这与判据「每帧字节数」的用意一致)。
// 「某一秒侥幸收全两帧」不会因此让判据沉默:0.6 基线的门槛远高于 1~2 帧。
//
// minFrameBytes 是唯一的绝对量,用于排除「画面几乎静止、只有几 KB 的时钟/进度条
// 补丁」这类小帧;只该对没有码率控制的档位(DIFF/RGB565)设它 —— 有码率控制的
// 档位由判据自己的「每帧是否明显超过标称」把关,再加一道绝对下限只会误伤
// 小屏/慢链路(1024×768 的 30KB 增量帧在 300kbps 上确实堵,但不该被当成小补丁)。
//
// 已知覆盖不到的一档:低于 0.3fps 的链路(一帧要 3 秒以上,1.5~2Mbps 上的
// 1~2MB DIFF 帧就是这种)窗口里最多两帧,①②③ 凑不齐,这里仍然弃权 ——
// 两帧撑不起「持续交付不动」的结论(两次孤立点击也是两帧)。这类现场靠 RTT
// 判据、±2 限幅和升档失败封顶兜底,不会失控,只是降档可能要来得晚一些。
//
// 仍然分不清的一种现场:3 帧摊在 3 秒里然后停住。从控制端能看到的证据(低帧率、
// 大帧、内容还在来)看,它和「链路真的交付不动」完全一样,只能靠降档后的效果来
// 分辨 —— 所以调用方要用本机瓶颈探测 + 升档失败封顶兜底,限幅(最多比 RTT 判据
// 再低 2 档)也限制了误判的代价。
//
// 返回 TRUE 表示可以拿 fps/baseline/kbps 去调 GetTargetQualityLevelByLoad。
inline bool GetCongestedLoadSample(unsigned long long framesInWindow,
unsigned long long bytesInWindow,
int activeSeconds,
unsigned int busiestSecondFrames,
int seconds,
int lastFrameAgeSec,
int baselineFps,
int minFrameBytes,
int& fps, int& baseline, int& kbps)
{
fps = 0;
baseline = 0;
kbps = 0;
if (seconds <= 0 || baselineFps <= 0) return false;
if (framesInWindow < 3 || activeSeconds < 3) return false; // ①
if (lastFrameAgeSec < 0 || lastFrameAgeSec > 4) return false; // ②
if ((long long)busiestSecondFrames * 5 >= (long long)baselineFps * 3) return false; // ③
if (minFrameBytes > 0 && bytesInWindow / framesInWindow < (unsigned long long)minFrameBytes)
return false;
GetWindowedLoadSample(framesInWindow, bytesInWindow, seconds, baselineFps, fps, baseline, kbps);
return true;
}
// 1 秒样本的可用性判定(窗口不可用时的退路)。
//
// 必须和窗口样本过同一道小帧下限:Ultra/High(bitRate == 0,DIFF/RGB565)上画面
// 静止时,客户端每秒只发一两帧几 KB 的时钟/光标补丁 —— 帧率低、吞吐低,判据会
// 反查出最低档,把 LAN 上的画质一路压到 Minimal。窗口路径用 minFrameBytes 挡掉的
// 正是它;窗口被拒后恰好退到这条路,1 秒样本不过门槛就等于白挡。
//
// 帧率/基线同样放大 100 倍,与 GetWindowedLoadSample 保持同一比例。
//
// elapsedMs 是这批计数实际覆盖的时间:WM_TIMER 会被 UI 线程阻塞推迟,一格可能
// 代表两秒多的流量,按 1000ms 算会把速率高估。
inline bool GetSecondLoadSample(unsigned long long frames,
unsigned long long bytes,
unsigned int elapsedMs,
int baselineFps,
int minFrameBytes,
int& fps, int& baseline, int& kbps)
{
fps = 0;
baseline = 0;
kbps = 0;
// frames 上限只是防下面乘 100 * 1000 溢出;真实会话每秒最多几十帧。
if (frames == 0 || frames > 10000ULL || baselineFps <= 0) return false;
if (bytes == 0 || elapsedMs == 0) return false; // 没有实测吞吐/时长,判据没法算
if (minFrameBytes > 0 && bytes / frames < (unsigned long long)minFrameBytes) return false;
fps = (int)(frames * 100 * 1000 / elapsedMs);
baseline = baselineFps * 100;
kbps = (int)(bytes * 8 * 1000 / (1024ULL * elapsedMs));
return true;
}
// 降档后的「本机瓶颈」判定:帧率仍低于基线,但按同一套判据管道并没有满
// (帧不大却发不出来 → 瓶颈在抓屏/编码,而不在链路)。
//
// 这两个条件在画面静止时也会同时成立 —— 客户端 memcmp 跳帧,窗口均值就是
// 「帧率低 + 吞吐小」,notSaturated 必然为真。所以还要一道「最近一秒真的在发帧」
// 的门槛:一次静止就永久停用吞吐判据,代价比漏判一次本机瓶颈大得多。
inline bool IsLocalBottleneck(int fps, int baseline, int kbps, int nominalKbps,
unsigned int lastSecondFrames)
{
if (lastSecondFrames < 2) return false;
if ((long long)fps * 5 >= (long long)baseline * 3) return false;
return GetTargetQualityLevelByLoad(fps, baseline, kbps, nominalKbps) < 0;
}
typedef struct ScreenSettings {
int MaxFPS; // 偏移 0, 最大帧率
int CompressThread; // 偏移 4, 压缩线程数