Fix: Keep the captured monitor stable across restarts

GDI and DXGI each kept their own static monitor index, and DXGI
committed the index before its initialization had succeeded. When a
quality change restarted capture and DXGI fell back to GDI, the next
DXGI attempt read the failed attempt's index and jumped to the other
monitor. Both backends now share one process-wide index with separate
peek and commit steps, and the commit only happens after the backend
is actually up.

Manual switching is made a one-shot request: InitScreenSpy consumes
the flag and RestartScreen only sets it, so an adaptive restart that
lands in the middle of a switch no longer drops the request or
re-applies it on every later rebuild. Window capture no longer
touches the index at all, so leaving window mode returns to the
monitor that was on screen.

Also: restart capture on a bitrate change only when the target level
is still H.264 (switching to RGB565/DIFF never uses the bitrate), and
bound the DXGI output fallback so a session without any enumerable
output cannot spin forever in InitDXGI.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-08 20:44:49 +01:00
1 parent d8048979e3
commit 2e34e49186
5 files changed
+77 -27

No files matched your search

+17 -8
View File
@@ -168,6 +168,15 @@ public:
int m_nScreenCount; // 屏幕数量
BOOL m_bEnableMultiScreen;// 多显示器支持
// ===== 屏索引(GDI 与 DXGI 共用同一份)=====
// 0 = 所有显示器拼成的虚拟桌面,k>=1 = 第 k 块显示器(DXGI 下对应输出 k-1)。
// 选屏(Peek)和确认(Commit)必须分开:DXGI 初始化失败会回退 GDI,失败的尝试
// 若推进了索引,回退后第一次重建就会跳到另一块屏上(现场表现:调个质量档就换屏)。
static int& MonitorCur() { static int v = 0; return v; } // 当前正在显示的屏
static int& MonitorNext() { static int v = 0; return v; } // 下一次"切换显示器"要去的屏
static void CommitMonitorIndex(int idx) { MonitorCur() = idx; MonitorNext() = idx + 1; }
int m_nMonitorIndex = 0; // 本次构造选中的屏;后端初始化成功后由它提交
// 滚动检测相关
CScrollDetector* m_pScrollDetector; // 滚动检测器
bool m_bEnableScrollDetect; // 是否启用滚动检测
@@ -230,17 +239,16 @@ 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;
// 屏索引全进程只有一份(MonitorCur/MonitorNext),GDI 与 DXGI 共用。
// 这里只“取”不“提交”:重建截屏对象时(质量档位、分辨率、ROI、窗口捕获)
// 必须沿用当前屏(switchScreen=false),只有"切换显示器"才前进一格。
// 提交留给真正建成的后端——DXGI 构造完才知道初始化成不成功,而失败要回退
// GDI,此时索引不能已经前进过。
m_nScreenCount = monitors.size();
m_bEnableMultiScreen = all;
if (all && !monitors.empty()) {
int idx = (switchScreen ? index++ : current) % (monitors.size()+1);
current = idx;
int idx = (switchScreen ? MonitorNext() : MonitorCur()) % (int)(monitors.size()+1);
m_nMonitorIndex = idx;
if (idx == 0) {
m_iScreenX = GetSystemMetrics(SM_XVIRTUALSCREEN);
m_iScreenY = GetSystemMetrics(SM_YVIRTUALSCREEN);
@@ -254,6 +262,7 @@ public:
m_ulFullHeight = rt.bottom - rt.top;
}
} else {
m_nMonitorIndex = 0; // 单屏模式不参与屏索引
//::GetSystemMetrics(SM_CXSCREEN/SM_CYSCREEN)获取屏幕大小不准
//例如当屏幕显示比例为125%时,获取到的屏幕大小需要乘以1.25才对
DEVMODE devmode;