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

+9 -2
View File
@@ -20,6 +20,13 @@ CScreenSpy::CScreenSpy(ULONG ulbiBitCount, BYTE algo, BOOL vDesk, int gop, BOOL
m_bDynamicForeground = dynamicFg;
m_GOP = gop;
// GDI 构造本身没有失败路径,到这里就确认当前屏。DXGI 初始化失败回退到 GDI 时
// 也走这里:两条路径必须落到同一块屏上,否则下一次重建会换屏。
// 窗口捕获(含动态前景)不参与屏索引:下面的窗口矩形会覆盖基类的显示器几何,
// 此时若提交,一次窗口重建就会把索引往前推一格,取消窗口捕获后画面就换屏了。
if (!hwnd && !m_bDynamicForeground && m_bEnableMultiScreen && m_nScreenCount > 0)
CommitMonitorIndex(m_nMonitorIndex);
// 窗口捕获模式:用 DWM 真实边界覆盖基类的全屏尺寸,并缓存阴影偏移量
if (hwnd) {
RECT wndRc = {}, frameRc = {};
@@ -49,8 +56,8 @@ CScreenSpy::CScreenSpy(ULONG ulbiBitCount, BYTE algo, BOOL vDesk, int gop, BOOL
m_BitmapInfor_Send = new BITMAPINFO(*m_BitmapInfor_Full);
m_nInstructionSet = cfg.GetInt("settings", "CpuSpeedup", 0);
Mprintf("CScreenSpy: strategy=%d, maxWidth=%d, fullWidth=%d, algo=%d\n",
strategy, maxWidth, m_BitmapInfor_Send->bmiHeader.biWidth, m_bAlgorithm);
Mprintf("CScreenSpy: strategy=%d, maxWidth=%d, fullWidth=%d, algo=%d, monitor=%d\n",
strategy, maxWidth, m_BitmapInfor_Send->bmiHeader.biWidth, m_bAlgorithm, m_nMonitorIndex);
if (strategy == 1) {
// strategy=1: 用户明确选择原始分辨率