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:
1 parent
2a97471b51
commit
5f40f3726c
6 files changed
+688
-31
No files matched your search
@@ -1277,12 +1277,14 @@ VOID CScreenSpyDlg::OnReceiveComplete()
|
||||
DrawFirstScreen();
|
||||
m_ulFramesThisSecond++;
|
||||
m_ulScreenBytesThisSecond += len;
|
||||
m_AdaptiveQuality.lastFrameTick = GetTickCount(); // 慢流量窗口用它判断内容是否还在来
|
||||
break;
|
||||
}
|
||||
case TOKEN_NEXTSCREEN: {
|
||||
DrawNextScreenDiff(false);
|
||||
m_ulFramesThisSecond++;
|
||||
m_ulScreenBytesThisSecond += len;
|
||||
m_AdaptiveQuality.lastFrameTick = GetTickCount(); // 慢流量窗口用它判断内容是否还在来
|
||||
break;
|
||||
}
|
||||
case TOKEN_KEYFRAME: {
|
||||
@@ -1291,6 +1293,7 @@ VOID CScreenSpyDlg::OnReceiveComplete()
|
||||
}
|
||||
m_ulFramesThisSecond++;
|
||||
m_ulScreenBytesThisSecond += len;
|
||||
m_AdaptiveQuality.lastFrameTick = GetTickCount(); // 慢流量窗口用它判断内容是否还在来
|
||||
break;
|
||||
}
|
||||
case TOKEN_SCROLL_FRAME: {
|
||||
@@ -1299,6 +1302,7 @@ VOID CScreenSpyDlg::OnReceiveComplete()
|
||||
}
|
||||
m_ulFramesThisSecond++;
|
||||
m_ulScreenBytesThisSecond += len;
|
||||
m_AdaptiveQuality.lastFrameTick = GetTickCount(); // 慢流量窗口用它判断内容是否还在来
|
||||
break;
|
||||
}
|
||||
case TOKEN_CLIPBOARD_TEXT: {
|
||||
@@ -2666,6 +2670,38 @@ void CScreenSpyDlg::OnTimer(UINT_PTR nIDEvent)
|
||||
// 屏幕数据速率单独留存给吞吐判据:上面那个总量混了文件传输/音频/剪贴板,
|
||||
// 传大文件时会让判据误判"链路塞满"。
|
||||
m_dScreenKBps = m_ulScreenBytesThisSecond / 1024.0;
|
||||
// 慢流量窗口:这一秒的原始样本进环形缓冲,供 CollectLoadSample 取均值。
|
||||
// 采样断档(服务端休眠/线程被挂起)时窗口里的样本已经和现在无关,直接清空:
|
||||
// 不清的话恢复后前 10 秒还在用旧现场判定降档。
|
||||
{
|
||||
ULONGLONG sampleNow = GetTickCount64();
|
||||
// 这一格覆盖的是「上次采样到现在」这段时间,不是整一秒:WM_TIMER 会被
|
||||
// UI 线程里的阻塞(录像、慢绘制、对话框)推迟,按一格算一秒会把帧率和
|
||||
// 吞吐整体高估。把实际毫秒数一起存下来,窗口的分母用它。
|
||||
ULONGLONG gap = (m_AdaptiveQuality.loadWindowLastTick == 0)
|
||||
? 1000 : (sampleNow - m_AdaptiveQuality.loadWindowLastTick);
|
||||
m_AdaptiveQuality.loadWindowLastTick = sampleNow;
|
||||
if (gap > 3000) {
|
||||
// 采样断档(服务端休眠/线程被挂起):窗口里的样本已经和现在无关,
|
||||
// 这一格累计的计数同样跨越了断档,一并丢弃,从下一格重新开始。
|
||||
memset(m_AdaptiveQuality.loadWindowFrames, 0, sizeof(m_AdaptiveQuality.loadWindowFrames));
|
||||
memset(m_AdaptiveQuality.loadWindowBytes, 0, sizeof(m_AdaptiveQuality.loadWindowBytes));
|
||||
memset(m_AdaptiveQuality.loadWindowMs, 0, sizeof(m_AdaptiveQuality.loadWindowMs));
|
||||
m_AdaptiveQuality.loadWindowPos = 0;
|
||||
m_AdaptiveQuality.loadWindowFilled = 0;
|
||||
m_AdaptiveQuality.lastLoadSampleMs = 0; // 这一秒的样本也不可信
|
||||
} else {
|
||||
m_AdaptiveQuality.lastLoadSampleMs = (ULONG)gap;
|
||||
const int pos = m_AdaptiveQuality.loadWindowPos;
|
||||
m_AdaptiveQuality.loadWindowFrames[pos] = m_ulFramesThisSecond;
|
||||
m_AdaptiveQuality.loadWindowBytes[pos] = m_ulScreenBytesThisSecond;
|
||||
m_AdaptiveQuality.loadWindowMs[pos] = (ULONG)gap;
|
||||
if (++m_AdaptiveQuality.loadWindowPos >= m_AdaptiveQuality.LOAD_WINDOW_SECONDS)
|
||||
m_AdaptiveQuality.loadWindowPos = 0;
|
||||
if (m_AdaptiveQuality.loadWindowFilled < m_AdaptiveQuality.LOAD_WINDOW_SECONDS)
|
||||
m_AdaptiveQuality.loadWindowFilled++;
|
||||
}
|
||||
}
|
||||
m_ulScreenBytesThisSecond = 0;
|
||||
// 使用EMA平滑帧率 (alpha=0.3),避免跳跃
|
||||
double currentFPS = (double)m_ulFramesThisSecond;
|
||||
@@ -2797,6 +2833,72 @@ int CScreenSpyDlg::GetClientRTT()
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 采一份吞吐判据的输入样本:窗口均值优先,窗口不足以证明拥塞时退回 1 秒样本。
|
||||
//
|
||||
// 两处判据(吞吐降档、降档后的本机瓶颈探测)必须用同一份样本。原来两处各采各的,
|
||||
// 在「一帧要两秒」的现场会交替给出相反结论 —— 0 帧秒走窗口说拥塞、收全帧的那一秒
|
||||
// 走 1 秒计数又算出 4Mbps「没事」,确认计数被反复清零,三拍永远凑不齐。
|
||||
//(1 秒样本在大帧现场还会高估吞吐:一帧实际传了 2 秒,它只算收全那一秒的字节。)
|
||||
CScreenSpyDlg::LoadSample CScreenSpyDlg::CollectLoadSample()
|
||||
{
|
||||
LoadSample s;
|
||||
|
||||
int sampleBaseline = (int)(m_AdaptiveQuality.baselineFps + 0.5);
|
||||
// 帧龄用 32 位无符号差值算,跨 GetTickCount 回绕(49.7 天)仍是对的;
|
||||
// 字段本身是 DWORD,避免 x86 上 64 位读被撕裂(见 ScreenSpyDlg.h)。
|
||||
int lastFrameAge = (m_AdaptiveQuality.lastFrameTick == 0)
|
||||
? -1 : (int)((DWORD)(GetTickCount() - m_AdaptiveQuality.lastFrameTick) / 1000);
|
||||
|
||||
// 每帧字节下限只对没有码率控制的档位生效,理由见 GetCongestedLoadSample;
|
||||
// 窗口路径和 1 秒样本共用同一道门槛。
|
||||
const int minFrameBytes =
|
||||
(GetQualityProfile(m_AdaptiveQuality.currentLevel).bitRate == 0) ? 64 * 1024 : 0;
|
||||
|
||||
const int n = m_AdaptiveQuality.loadWindowFilled;
|
||||
if (n > 0) {
|
||||
unsigned long long winFrames = 0, winBytes = 0, winMs = 0;
|
||||
unsigned int busiest = 0;
|
||||
int activeSeconds = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
const ULONG f = m_AdaptiveQuality.loadWindowFrames[i];
|
||||
winFrames += f;
|
||||
winBytes += m_AdaptiveQuality.loadWindowBytes[i];
|
||||
winMs += m_AdaptiveQuality.loadWindowMs[i];
|
||||
if (f > busiest) busiest = f;
|
||||
if (f > 0) activeSeconds++;
|
||||
}
|
||||
// 分母用实际经过的毫秒数,不是格子数:WM_TIMER 被 UI 阻塞推迟时,
|
||||
// 一格代表两秒多的流量,按一格一秒算会把速率高估。
|
||||
int seconds = (int)((winMs + 500) / 1000);
|
||||
if (seconds < 1) seconds = 1;
|
||||
int fps = 0, baseline = 0, kbps = 0;
|
||||
if (GetCongestedLoadSample(winFrames, winBytes, activeSeconds, busiest, seconds, lastFrameAge,
|
||||
sampleBaseline, minFrameBytes, fps, baseline, kbps)) {
|
||||
s.fps = fps;
|
||||
s.baseline = baseline;
|
||||
s.kbps = kbps;
|
||||
s.usable = true;
|
||||
s.windowed = true;
|
||||
s.seconds = seconds;
|
||||
s.frames = winFrames;
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
// 正常会话(帧够快、有满速秒)走的多是这条路:1 秒样本,对抖动更敏感。
|
||||
// 帧率/基线同样放大 100 倍:判据只用两者的比值,这里统一比例是为了让日志和
|
||||
// 其它消费方不必区分两种采样。
|
||||
{
|
||||
unsigned long long bytesLastSecond = (unsigned long long)(m_dScreenKBps * 1024.0);
|
||||
if (GetSecondLoadSample(m_ulFramesLastSecond, bytesLastSecond,
|
||||
m_AdaptiveQuality.lastLoadSampleMs, sampleBaseline,
|
||||
minFrameBytes, s.fps, s.baseline, s.kbps)) {
|
||||
s.usable = true;
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
void CScreenSpyDlg::EvaluateQuality()
|
||||
{
|
||||
if (!m_AdaptiveQuality.enabled) return;
|
||||
@@ -2820,6 +2922,10 @@ void CScreenSpyDlg::EvaluateQuality()
|
||||
m_AdaptiveQuality.baselineFps = floorFps;
|
||||
}
|
||||
|
||||
// 一次评估只采一次样本:降档判据和下面的本机瓶颈探测必须用同一份数据,
|
||||
// 各采各的会在慢流量现场交替给出相反结论(见 CollectLoadSample)。
|
||||
const LoadSample load = CollectLoadSample();
|
||||
|
||||
// 0.5 降档后观察:真带宽受限时降档同时降低分辨率和码率,管道立刻缓解、帧率必须回升;
|
||||
// 本机受限(抓屏/编码慢)时降码率对帧率几乎没帮助。10 秒后帧率仍不达标、
|
||||
// 且管道明显没被填满,就判定瓶颈在本机,本次会话停用吞吐判据。
|
||||
@@ -2828,28 +2934,25 @@ void CScreenSpyDlg::EvaluateQuality()
|
||||
if (m_AdaptiveQuality.loadProbeTime != 0 &&
|
||||
now - m_AdaptiveQuality.loadProbeTime >= 10000) {
|
||||
m_AdaptiveQuality.loadProbeTime = 0;
|
||||
// 样本门槛:这 10 秒里几乎没收到帧,说明画面一直静止(客户端 memcmp 跳帧),
|
||||
// 既不能证明降档奏效也不能证明没奏效。此时若照常判定,"帧率低 + 帧很小"
|
||||
// 会被误读成"本机瓶颈",一次静止就永久停用吞吐判据。宁可放弃这次探测。
|
||||
if (m_ulFramesLastSecond < 2) {
|
||||
Mprintf("[Adaptive] 降档观察窗内仅 %u 帧,样本不足,放弃本次本机瓶颈判定\n",
|
||||
m_ulFramesLastSecond);
|
||||
// 观察窗里必须真的拿到了样本,否则既不能证明降档奏效也不能证明没奏效:
|
||||
// 此时若照常判定,"帧率低 + 帧很小"会被误读成"本机瓶颈",一次静止就永久
|
||||
// 停用吞吐判据。宁可放弃这次探测。
|
||||
// 用 CollectLoadSample 而不是 1 秒计数:慢流量现场每秒帧数基本都是 0,
|
||||
// 拿 1 秒计数去卡门槛只会每次都弃权 —— 判据现在覆盖的场景正好是它。
|
||||
if (!load.usable) {
|
||||
Mprintf("[Adaptive] 降档观察窗内没有可用样本,放弃本次本机瓶颈判定\n");
|
||||
} else {
|
||||
int needKbps = GetQualityProfile(m_AdaptiveQuality.currentLevel).bitRate;
|
||||
int nowKbps = (int)(m_dScreenKBps * 8);
|
||||
// 帧率仍然不达标、但按同一套判据已经不算饱和 → 帧并不大却发不出来,
|
||||
// 说明瓶颈在本机(抓屏/编码),而不是链路。这里复用同一个判据,
|
||||
// 不再另写一套阈值,避免两处口径不一致。
|
||||
bool stillShort = (m_dFrameRate < m_AdaptiveQuality.baselineFps * 0.6);
|
||||
bool notSaturated = (GetTargetQualityLevelByLoad(
|
||||
(int)m_ulFramesLastSecond,
|
||||
(int)(m_AdaptiveQuality.baselineFps + 0.5),
|
||||
nowKbps, needKbps) < 0);
|
||||
if (stillShort && notSaturated) {
|
||||
// 不再另写一套阈值,避免两处口径不一致;「画面静止不算证据」的门槛
|
||||
// 也放在同一个 helper 里(见 IsLocalBottleneck)。
|
||||
const int needKbps = GetQualityProfile(m_AdaptiveQuality.currentLevel).bitRate;
|
||||
if (IsLocalBottleneck(load.fps, load.baseline, load.kbps, needKbps,
|
||||
m_ulFramesLastSecond)) {
|
||||
m_AdaptiveQuality.loadPinned = true;
|
||||
Mprintf("[Adaptive] 降档后帧率未回升 (%.1f < %.1f) 且帧已不大 (%d/%dkbps),"
|
||||
"判定瓶颈在本机,本次会话停用吞吐判据\n",
|
||||
m_dFrameRate, m_AdaptiveQuality.baselineFps * 0.6, nowKbps, needKbps);
|
||||
load.fps / 100.0, load.baseline * 0.6 / 100.0, load.kbps, needKbps);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2893,23 +2996,38 @@ void CScreenSpyDlg::EvaluateQuality()
|
||||
int levelLoad = -1;
|
||||
if (dual && !m_AdaptiveQuality.loadPinned) {
|
||||
const QualityProfile& prof = GetQualityProfile(currentLevel);
|
||||
int nowKbps = (int)(m_dScreenKBps * 8);
|
||||
levelLoad = GetTargetQualityLevelByLoad(
|
||||
(int)(m_ulFramesLastSecond + 0.5),
|
||||
(int)(m_AdaptiveQuality.baselineFps + 0.5),
|
||||
nowKbps,
|
||||
prof.bitRate);
|
||||
// 一次评估只采一次样本,窗口路径的可用性判定全在 CollectLoadSample 里:
|
||||
// 大帧把链路堵住时 1 秒窗口是空的(整帧收全才计数),要靠 10 秒均值才表达得出
|
||||
// 0.2fps;但只有「内容还在持续来、且没有任何一秒交付过满速」才算拥塞证据,
|
||||
// 否则滚动一下停住、或画面本来就没变化,都会被误判成带宽不足。
|
||||
if (load.usable) {
|
||||
levelLoad = GetTargetQualityLevelByLoad(load.fps, load.baseline, load.kbps, prof.bitRate);
|
||||
}
|
||||
// 最多比 RTT 判据再压低 2 级。判据本身分不清"链路慢"和"本机慢",限幅保证
|
||||
// 误判时只掉两档,不会被某一秒的抖动打到最低。
|
||||
if (levelLoad >= 0) {
|
||||
int cap = (levelRTT >= 0 ? levelRTT : currentLevel) + 2;
|
||||
if (levelLoad > cap) levelLoad = cap;
|
||||
}
|
||||
if (levelLoad >= 0 && levelLoad > levelRTT) {
|
||||
Mprintf("[Adaptive] 吞吐判据: fps=%u(基线%.0f) 屏幕=%.0fKB/s(≈%dkbps) "
|
||||
"本档标称=%dkbps → level %d(RTT 判据给 %d)\n",
|
||||
m_ulFramesLastSecond, m_AdaptiveQuality.baselineFps,
|
||||
m_dScreenKBps, nowKbps, prof.bitRate, levelLoad, levelRTT);
|
||||
// 判据每秒评估一次,而降档要等冷却和连续确认,期间每一拍都可能满足
|
||||
// levelLoad > levelRTT(甚至 targetLevel 就等于 currentLevel)。慢流量现场
|
||||
// 窗口采样让判据几乎每秒都可用,不限流一次会话能刷几千行;10 秒一条既够
|
||||
// 追踪演进,又不至于把日志打满。限流只作用于日志,不影响计数和应用逻辑。
|
||||
if (levelLoad >= 0 && levelLoad > levelRTT &&
|
||||
now - m_AdaptiveQuality.lastLoadLogTime >= 10000) {
|
||||
m_AdaptiveQuality.lastLoadLogTime = now;
|
||||
if (load.windowed) {
|
||||
Mprintf("[Adaptive] 吞吐判据: %d 秒窗口 %llu 帧(≈%.1ffps, 基线%.0f) "
|
||||
"屏幕≈%dkbps 本档标称=%dkbps → level %d(RTT 判据给 %d)\n",
|
||||
load.seconds, load.frames, load.fps / 100.0,
|
||||
load.baseline / 100.0, load.kbps, prof.bitRate,
|
||||
levelLoad, levelRTT);
|
||||
} else {
|
||||
Mprintf("[Adaptive] 吞吐判据: fps=%u(基线%.0f) 屏幕=%.0fKB/s(≈%dkbps) "
|
||||
"本档标称=%dkbps → level %d(RTT 判据给 %d)\n",
|
||||
m_ulFramesLastSecond, m_AdaptiveQuality.baselineFps,
|
||||
m_dScreenKBps, load.kbps, prof.bitRate, levelLoad, levelRTT);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2926,12 +3044,16 @@ void CScreenSpyDlg::EvaluateQuality()
|
||||
targetLevel = QUALITY_GOOD;
|
||||
}
|
||||
|
||||
// 3.5 升档失败封顶:升档后 20 秒内又降回升档前的档位或更差,说明那次升档是错的
|
||||
// 3.5 升档失败封顶:升档后 40 秒内又降回升档前的档位或更差,说明那次升档是错的
|
||||
// (典型是"降档→队列排空→RTT 变好→升档→队列又满→RTT 变差"的 bufferbloat 循环,
|
||||
// 跨洲际 + frp 大缓冲链路最容易出现)。记下升档前的档位,3 分钟内不再尝试好于它。
|
||||
// 宁可停在低一档,也不要在两个档位之间每 30 秒重建一次编码器和分辨率。
|
||||
// 思路同 TCP 的 ssthresh:失败一次就把天花板降下来,而不是继续试探。
|
||||
// 惩罚期取 3 分钟:够长以压住振荡,又不至于网络真恢复后一直卡在低档。
|
||||
// 窗口取 40 秒而不是 20 秒:升档本身会刷新 lastResChangeTime,回到升档前的
|
||||
// 档位多半要跨分辨率,得先等 15 秒冷却,再叠上吞吐判据的连续 3 拍确认,
|
||||
// 20 秒经常在降档被允许之前就过期 —— 于是"升上去→卡住→降回来"每次都被
|
||||
// 判成新情况,封顶永远不触发,循环一直重复。
|
||||
if (dual && m_AdaptiveQuality.levelCeiling >= 0) {
|
||||
if (now >= m_AdaptiveQuality.ceilingUntil) {
|
||||
m_AdaptiveQuality.levelCeiling = -1; // 惩罚期结束,恢复自由升档
|
||||
@@ -2990,9 +3112,10 @@ void CScreenSpyDlg::EvaluateQuality()
|
||||
const int need = byLoad ? 3 : 2;
|
||||
const int have = byLoad ? m_AdaptiveQuality.loadCount : m_AdaptiveQuality.stableCount;
|
||||
if (have >= need) {
|
||||
// 升档失败检测:上一次升档后 20 秒内就降回升档前的档位或更差
|
||||
// 升档失败检测:上一次升档后 40 秒内就降回升档前的档位或更差
|
||||
// (40 秒的由来见 3.5 节:要等过升档刷新的 15 秒分辨率冷却)
|
||||
if (dual && m_AdaptiveQuality.lastUpFromLevel >= 0 &&
|
||||
now - m_AdaptiveQuality.lastUpTime < 20000 &&
|
||||
now - m_AdaptiveQuality.lastUpTime < 40000 &&
|
||||
targetLevel >= m_AdaptiveQuality.lastUpFromLevel) {
|
||||
m_AdaptiveQuality.levelCeiling = m_AdaptiveQuality.lastUpFromLevel;
|
||||
m_AdaptiveQuality.ceilingUntil = now + 180000;
|
||||
@@ -3072,6 +3195,13 @@ void CScreenSpyDlg::ApplyQualityLevel(int level, bool persist)
|
||||
m_AdaptiveQuality.lastChangeTime = GetTickCount64();
|
||||
m_AdaptiveQuality.stableCount = 0;
|
||||
m_AdaptiveQuality.loadCount = 0; // 吞吐判据的确认计数同样清零,避免跨档位累积
|
||||
// 换档前的流量属于旧档位(帧更大更慢、分辨率不同),留在窗口里会继续参与新档位
|
||||
// 的判定:清空,让新档位重新积累 10 秒样本。
|
||||
memset(m_AdaptiveQuality.loadWindowFrames, 0, sizeof(m_AdaptiveQuality.loadWindowFrames));
|
||||
memset(m_AdaptiveQuality.loadWindowBytes, 0, sizeof(m_AdaptiveQuality.loadWindowBytes));
|
||||
memset(m_AdaptiveQuality.loadWindowMs, 0, sizeof(m_AdaptiveQuality.loadWindowMs));
|
||||
m_AdaptiveQuality.loadWindowPos = 0;
|
||||
m_AdaptiveQuality.loadWindowFilled = 0;
|
||||
|
||||
// 手动选档(persist=1)时清掉升档封顶和锁档:那是自适应在本会话里试探出来的结论,
|
||||
// 用户显式指定的档位不该被它压住,否则会出现"选了高档却一直被拉回去"。
|
||||
|
||||
Reference in new issue
Block a user