Feature: throughput criterion for adaptive quality, plus anti-oscillation

Adaptive quality only looked at RTT, so a short RTT over a narrow pipe
read as "excellent": at 20-40ms it picks Ultra/High (DIFF/RGB565), both
with bitRate == 0, and never steps down. One 1080p RGB565 frame is
4.15 MB -- 1.66s of transmission on 20 Mbps.

Adds GetTargetQualityLevelByLoad(): congestion needs both fps < 60% of
baseline and bytes/frame still near the nominal bitrate, because a static
screen has a near-zero byte rate on any link. Merged with the RTT result
as max() -- averaging lets a good RTT mask a narrow pipe. Step-down is 3
confirmations and jumps straight to the level the throughput can carry
(capped at +2); step-up stays RTT-only, so the criterion is one-way and
the worst case is one level too low instead of a 30s/15s oscillation.

Anti-oscillation, all inside the switch: RTT hysteresis (85%/115% dead
band against the target level's threshold), failed step-up pinned for 3
minutes, cooldowns 3s -> 10s down / 15s up, counters interrupt each
other, and a hard limit of 6 changes per 5 minutes.

Server-only, no client change. m_ulBytesThisSecond had to be fixed first:
it is accumulated before the switch, so file transfer, audio and
clipboard counted as screen traffic; added m_ulScreenBytesThisSecond for
the four frame cases. Also gates a real bug in the 10s post-step-down
probe that disabled the criterion permanently on a static screen.

Default on (same migration caveat as the other four). Unticking takes the
original path unchanged. Verified on Debug: 27 hysteresis cases and the
load cases pass, menu defaults correct; not yet measured end to end.
This commit is contained in:
yuanyuanxiang committed 2026-11-05 14:24:42 +01:00
1 parent ab83f5d685
commit d6b04a98f0
12 files changed
+543 -23

No files matched your search

+93 -3
View File
@@ -1406,8 +1406,9 @@ inline const PreviewProfile& GetScreenPreviewProfile(int level) {
return g_PreviewProfiles[level];
}
// 根据RTT获取目标质量等级 (控制端使用)
inline int GetTargetQualityLevel(int rtt, int usingFRP) {
// RTT 阈值表(毫秒,上界):rtt 小于阈值 i 就落在等级 i。
// 单独提出来是因为迟滞判据要按当前档位回查阈值,不能再藏在 GetTargetQualityLevel 里。
inline const int* GetRttThresholds(int usingFRP) {
// 根据模式应用不同 RTT阈值 (毫秒)
static const int g_RttThresholds[2][QUALITY_COUNT] = {
// 直连: ULTRA, HIGH, GOOD, MEDIUM, LOW, MINIMAL
@@ -1415,13 +1416,102 @@ inline int GetTargetQualityLevel(int rtt, int usingFRP) {
// FRP:
/* PROXY */ { 60, 160, 300, 500, 800, INT_MAX },
};
return g_RttThresholds[(usingFRP != 0) ? 1 : 0];
}
// 根据RTT获取目标质量等级 (控制端使用)
inline int GetTargetQualityLevel(int rtt, int usingFRP) {
const int* thr = GetRttThresholds(usingFRP);
for (int i = 0; i < QUALITY_COUNT; i++) {
if (rtt < g_RttThresholds[usingFRP][i])
if (rtt < thr[i])
return i;
}
return QUALITY_MINIMAL;
}
// 带迟滞的 RTT 判据:把"升档"和"降档"的阈值错开,落在死区里就保持当前档位。
//
// 为什么需要:RTT 是每秒采样一次的抖动量,±5ms 很常见。硬阈值下 rtt 在 78↔82
// 之间晃就会让目标档在 1↔2 之间反复跳,而 1↔2 正好是"原始分辨率 RGB565" ↔
// "1920 H264" 的分界 —— 每跳一次都是一次编码器重建 + 整屏重传,用户看到的是
// 周期性花屏/卡顿。加上死区后,只有 RTT 明显变好或明显变坏才会动档位。
//
// 死区宽度取 ±15%:
// 想升档到 raw → rtt 必须低于【目标档】阈值(raw) 的 85%
// 想降档 → rtt 必须高于【当前档】阈值 的 115%
// 升档用目标档阈值是必须的:如果拿当前档阈值去比,由于阈值表单调递增,
// "raw < currentLevel" 本身就意味着 rtt 已经低于当前档阈值一大截,
// 85% 那条恒成立,迟滞等于没加(这个坑是写完算例才发现的)。
//
// currentLevel 不合法时退化成无迟滞的 GetTargetQualityLevel。
inline int GetTargetQualityLevelHysteresis(int rtt, int usingFRP, int currentLevel)
{
if (rtt <= 0) return currentLevel;
if (currentLevel < 0 || currentLevel >= QUALITY_COUNT)
return GetTargetQualityLevel(rtt, usingFRP);
const int* thr = GetRttThresholds(usingFRP);
const int raw = GetTargetQualityLevel(rtt, usingFRP);
// 升档(数字变小):必须比目标档的边界再好 15%。用 long long 防 INT_MAX 溢出。
if (raw < currentLevel) {
return ((long long)rtt * 100 < (long long)thr[raw] * 85) ? raw : currentLevel;
}
// 降档(数字变大):必须比当前档的边界再差 15%。
if (raw > currentLevel) {
return ((long long)rtt * 100 > (long long)thr[currentLevel] * 115) ? raw : currentLevel;
}
return currentLevel;
}
// 吞吐判据:根据实测帧率与屏幕字节速率,判断链路是否已经饱和(控制端使用)。
//
// 返回该判据要求的最低质量等级(数字越大越差);链路没在拖后腿时返回 -1(无约束)。
// 由调用方与 RTT 判据取 max() 合并 —— 两个约束方向相反,任一都能单独压低档位:
// RTT = 延迟预算 → 质量上限(延迟大就别给高清)
// 吞吐 = 带宽预算 → 质量下限(带宽小就装不下高码率)
// 相加/加权平均是错的:两者不互补,取平均会让"RTT 好"掩盖"带宽窄",反之亦然。
//
// 关键点:这里判断的是"链路是否拒绝交付要求的东西",不是"传了多少字节"。
// 后者是个陷阱 —— 画面静止时字节速率接近零,哪怕链路是 1Gbps 也会被判成"带宽差"。
// 所以必须两个条件同时成立:
// ① 帧率明显低于基线(基线由调用方实测学习,见 CScreenSpyDlg::EvaluateQuality)
// ② 每帧字节数明显超过该档标称的每帧字节数 → 一帧要塞很久,帧又大又慢
// 只满足 ① 是画面本来就没变化(客户端 memcmp 跳帧),不构成带宽问题。
//
// ② 比的是"每帧字节数"而不是"每秒总字节数",这一点很关键:总字节数会随链路容量
// 一起变小,容量远低于标称码率时(比如 900kbps 跑 3000kbps 的档)永远达不到
// "半满",反而漏判。每帧字节数只反映"链路交付一帧要多久",与容量无关,
// 容量略低和远低两种情形都能覆盖。
//
// nominalKbps == 0 表示该档没有码率控制(Ultra/High 走 DIFF/RGB565 原始像素),
// 此时没有可比的标称码率:帧率缺口本身就是唯一证据,直接按实测吞吐反查目标档。
inline int GetTargetQualityLevelByLoad(int fps, int baselineFps, int kbps, int nominalKbps)
{
// kbps <= 0 一并挡掉:没有实测吞吐就没法反查目标档,
// 否则 nominalKbps == 0 的档位会走到反查循环、误返回最低档。
if (baselineFps <= 0 || fps <= 0 || kbps <= 0) return -1;
// 帧率缺口:低于基线的 60% 才算,避免正常抖动误触发
if (fps * 100 >= baselineFps * 60) return -1;
if (nominalKbps > 0) {
// 实际每帧字节 / 标称每帧字节 = (kbps / fps) / (nominalKbps / baselineFps)
// = kbps * baselineFps / (nominalKbps * fps)
// 不到 2 倍就说明帧本身不大,链路没在拖后腿(只是画面变化少),不构成带宽问题。
if ((long long)kbps * baselineFps <= 2LL * nominalKbps * fps) return -1;
}
// 按实测吞吐(饱和时它 ≈ 链路容量)反查能装下的最高档位,允许跨级直达。
// 逐级降要经历多次分辨率重建,每次都是一次编码器重建 + 可见卡顿;
// 一次跳到对的位置只重建一次。0.85 是给码率波动留的余量。
for (int level = QUALITY_GOOD; level < QUALITY_COUNT; level++) {
const int need = GetQualityProfile(level).bitRate;
if (need > 0 && need <= kbps * 85 / 100) return level;
}
return QUALITY_MINIMAL;
}
typedef struct ScreenSettings {
int MaxFPS; // 偏移 0, 最大帧率
int CompressThread; // 偏移 4, 压缩线程数