Fix: repair two quality-switch regressions (monitor jump, H264 stall)

Every quality change restarts the capturer on both sides, and two defects
showed up along that path.

Monitor jump: on a multi-monitor client the view moved to another monitor.
switchScreen=false means "reuse the index", but index is the *next*
monitor to switch to, not the one being shown. Track `current` and read it
on the non-switch path; the switch path still advances.

H264 stall: switching from RGB565/DIFF back to H264 froze the view for up
to ten seconds. The client encoder kept its reference frames across the
gap, so it emitted P frames the server could not decode until the next
natural IDR. It is now rebuilt on the first H264 frame after an algorithm
or tuning change. The rebuild is requested with an atomic dirty flag that
the capture thread consumes in ensureEncoder(), because resetting the
encoder from the network thread raced with encode().

On the server, ApplyQualityLevel() only sets m_bResolutionChanging when
the client will really restart the capture: it compares the target
maxWidth with the client's reported bitmap width (cached atomically on
TOKEN_BITMAPINFO) and stays conservative while a previous resize is still
in flight. The fallback timeout drops from 5s to 1s, flushes the decoder
and asks the client for a keyframe (COMMAND_SCREEN_KEYFRAME_REQ).

Verified: Debug client and server build clean; the 1920-wide High->Good
switch no longer waits out the gate. Not yet retested on a live session.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-05 18:10:56 +01:00
1 parent d6b04a98f0
commit 6a51116873
4 files changed
+100 -19

No files matched your search

+38 -8
View File
@@ -153,6 +153,11 @@ public:
// Web 端请求下一帧强制 IDR(COMMAND_SCREEN_KEYFRAME_REQ)。只对 H.264 生效;
// 必须在成功编码出 IDR 后清除,否则 ScanNextScreen 失败/硬编 EAGAIN 时请求会丢。
std::atomic<bool> m_bForceKeyFrame{false};
// 编码器重建请求。算法/编码参数由网络线程改(SetAlgorithm/SetTuningFlags),
// 但 m_encoder 归抓屏线程的 GetNextScreenData 使用,跨线程 reset 会和 encode()
// 撞成 use-after-free、和 ensureEncoder 的赋值撞成 unique_ptr 双重释放。
// 所以网络线程只置脏,真正的销毁+重建在 ensureEncoder 里做(单线程)。
std::atomic<bool> m_bEncoderDirty{false};
std::unique_ptr<VideoEncoderBase> m_encoder; // 编码器,ensureEncoder() lazy 创建,走 EncoderFactory 探测
bool m_bEncoderPrimed = false; // encoder 是否已成功产出过一个包;
// false 时禁止 skip——避免单显示器路径
@@ -224,11 +229,17 @@ public:
m_BlockNum = 8;
m_ThreadPool = new ThreadPool(m_BlockNum);
auto monitors = GetAllMonitors();
// index = 下一次切屏要用的索引(沿用原语义:切屏时自增)
// current = 当前正在显示的索引。重建截屏对象(质量档位、分辨率、ROI、窗口捕获)
// 必须沿用它,不能用 index —— index 永远指向"下一块",比正在显示的那块
// 大 1,所以 switchScreen=false 时读 index 会跳到下一块屏幕上。
static int index = 0;
static int current = 0;
m_nScreenCount = monitors.size();
m_bEnableMultiScreen = all;
if (all && !monitors.empty()) {
int idx = (switchScreen ? index++ : index) % (monitors.size()+1);
int idx = (switchScreen ? index++ : current) % (monitors.size()+1);
current = idx;
if (idx == 0) {
m_iScreenX = GetSystemMetrics(SM_XVIRTUALSCREEN);
m_iScreenY = GetSystemMetrics(SM_YVIRTUALSCREEN);
@@ -888,8 +899,14 @@ public:
}
// 编码器 lazy 创建。委托 EncoderFactory 完成"硬编探测 + 软编 fallback"。
// 只在抓屏线程调用:m_encoder 的销毁/重建全部收敛到这里,网络线程通过
// SetAlgorithm/SetTuningFlags 置 m_bEncoderDirty,避免跨线程 reset。
void ensureEncoder(int width, int height)
{
if (m_bEncoderDirty.exchange(false, std::memory_order_acq_rel)) {
m_encoder.reset();
m_bEncoderPrimed = false; // 重建后首帧必须真正 encode,不能走 memcmp skip
}
if (m_encoder) return;
if (width < 2 || height < 2) return; // x264 做偶数对齐 &~1,1→0,触发内部 strdup 泄漏;< 2 一并拦住
EncoderRequest req;
@@ -1072,7 +1089,19 @@ public:
virtual BYTE SetAlgorithm(int algo)
{
BYTE oldAlgo = m_bAlgorithm;
m_bAlgorithm = (DISABLE_X264_FOR_TEST && algo == ALGORITHM_H264) ? ALGORITHM_RGB565 : algo;
BYTE newAlgo = (DISABLE_X264_FOR_TEST && algo == ALGORITHM_H264) ? ALGORITHM_RGB565 : algo;
// 换算法必须换编码器。从 DIFF/RGB565 切回 H264 时,旧编码器还持有上一次的
// 参考帧状态,它下一帧只会吐 P 帧;服务端解码器没有对应参考帧,解不出来,
// 只能等下一个自然 IDR(GOP 最长 15 秒)—— 用户看到的就是"切到 H264 卡十秒"。
// 这里只置脏:销毁必须在抓屏线程的 ensureEncoder 里做(本函数跑在网络线程,
// 直接 reset 会与正在进行的 encode()/ensureEncoder 竞争)。下一帧重建后,
// 新编码器首帧必然是 IDR,立即可解。
// 先写算法再置脏:脏标记用 release,保证抓屏线程看到标记时一定能看到新算法,
// 以及此前由 SetBitRate 写好的 m_nBitRate。
m_bAlgorithm = newAlgo;
if (oldAlgo != newAlgo) {
m_bEncoderDirty.store(true, std::memory_order_release);
}
return oldAlgo;
}
@@ -1095,18 +1124,19 @@ public:
// 只有影响编码器参数的位(TUNING_ENCODER_BITS)变化才丢弃编码器;GDI 抓屏、发送节流
// 这类位由抓屏/发送线程每次现读,改了立刻生效,不该为它们白丢一个 IDR 让画面闪一下。
// 编码器重建后第一帧必然是 IDR,服务端能正确解码,不需要额外的关键帧握手。
// 与 SetAlgorithm 相同:本函数跑在网络线程,编码器销毁交给 ensureEncoder。
virtual bool SetTuningFlags(int flags)
{
if (m_nTuningFlags == flags) return false;
const bool encoderChanged = ((m_nTuningFlags ^ flags) & TUNING_ENCODER_BITS) != 0;
m_nTuningFlags = flags;
if (encoderChanged && m_encoder) {
Mprintf("=> TuningFlags: 0x%08X,重建编码器以应用新参数\n", flags);
m_encoder.reset();
m_bEncoderPrimed = false; // 重建后首帧必须真正 encode,不能走 skip
if (encoderChanged) {
// 不读 m_encoder(那是抓屏线程的成员):尚未创建时置脏也无害,
// 创建时本来就会用新参数。
Mprintf("=> TuningFlags: 0x%08X,编码器参数变化,下一帧重建\n", flags);
m_bEncoderDirty.store(true, std::memory_order_release);
} else {
Mprintf("=> TuningFlags: 0x%08X(%s)\n", flags,
m_encoder ? "不影响编码器,无需重建" : "编码器尚未创建,首帧时生效");
Mprintf("=> TuningFlags: 0x%08X(不影响编码器,无需重建)\n", flags);
}
return true;
}