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

+31 -13
View File
@@ -97,6 +97,8 @@ public:
CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&pFactory);
if (!pFactory) return;
// 输出回落标记:索引在当前适配器上不存在时回到主输出重试一次
bool forceFirstOutput = false;
do {
// 3. 获取设备
static UINT idx = 0;
@@ -105,25 +107,31 @@ public:
if (!dxgiAdapter)break;
// 4. 获取 DXGI 输出(屏幕)
// screen = 下一次切屏要用的输出索引(沿用原语义:切屏时自增)
// current = 当前正在用的输出。重建截屏对象(质量/分辨率变化)必须沿用它:
// 读 screen 会跳到下一个输出,多显示器下表现为"调个质量就换屏",
// 单显示器下 EnumOutputs(1) 不存在,会让 DXGI 初始化失败降级成 GDI。
static UINT screen = 0;
static UINT current = 0;
UINT outIndex = switchScreen ? screen++ : current;
// 屏索引与 GDI 共用一份(ScreenCapture::MonitorCur):0=虚拟桌面(DXGI 抓不了,
// 落到主输出),k>=1 表示第 k 块显示器,映射到输出 k-1。重建截屏对象
// (质量/分辨率变化)沿用当前屏,只有"切换显示器"才前进一格。
// 这里只取不提交:失败会回退 GDI,提交在初始化成功之后(见 break 前)。
UINT outIndex = 0;
if (all && !forceFirstOutput) {
const int n = m_nScreenCount + 1;
const int sel = (switchScreen ? MonitorNext() : MonitorCur()) % n;
outIndex = sel >= 1 ? (UINT)(sel - 1) : 0u;
}
HRESULT r = dxgiAdapter->EnumOutputs(outIndex, &dxgiOutput);
if (r == DXGI_ERROR_NOT_FOUND && all) {
// 索引在当前适配器上不存在(拔掉外接屏等):回到主输出再试,
// 避免拿一个无效索引去遍历适配器,最后降级成 GDI。
screen = 0;
current = 0;
if (r == DXGI_ERROR_NOT_FOUND && all && !forceFirstOutput) {
// 索引在当前适配器上不存在(拔掉外接屏、显示器挂在另一块显卡上等):
// 回到主输出再试一次,避免拿一个无效索引去遍历适配器,最后降级成 GDI。
// 只回落一次:若连主输出都枚举不到(无显示器的会话),必须落到下面
// 的 break 去回退 GDI,否则会在「切适配器 → 再枚举 → 又 NOT_FOUND」
// 之间空转,把抓屏线程挂死。
Mprintf("[DXGI] output %u not found on adapter %u, retry with primary output\n",
outIndex, (unsigned)idx);
forceFirstOutput = true;
idx ++;
dxgiAdapter->Release();
dxgiAdapter = nullptr;
continue;
}
current = outIndex;
if (!dxgiOutput)break;
// 4.1 获取屏幕位置(多显示器支持)
@@ -209,6 +217,13 @@ public:
desc.Usage = D3D11_USAGE_STAGING;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
d3dDevice->CreateTexture2D(&desc, NULL, &cpuTexture);
// IsInitSucceed() 判的就是 cpuTexture;建不出来就别再往下分配十几 MB
// 缓冲、也别提交屏索引——直接 break,由 InitScreenSpy 回退 GDI,
// GDI 会从同一份“未提交”的索引重建,两条路径仍然落在同一块屏上。
if (!cpuTexture) {
Mprintf("[DXGI] CreateTexture2D failed, fall back to GDI\n");
break;
}
// 9. 初始化 BITMAPINFO
m_BitmapInfor_Full = ConstructBitmapInfo(32, m_ulFullWidth, m_ulFullHeight);
@@ -257,6 +272,9 @@ public:
m_BitmapInfor_Send->bmiHeader.biHeight = h;
m_BitmapInfor_Send->bmiHeader.biSizeImage = w * h * 4;
}
// 初始化到这里才算成功,此时才能提交屏索引:中途失败(DuplicateOutput 等)
// 会回退 GDI,如果把失败尝试的索引提交了,回退后第一次重建就会换屏。
if (all && m_nScreenCount > 0) CommitMonitorIndex((int)outIndex + 1);
Mprintf("[DXGI] out=%u mode=%ux%u surf=%ux%u fmt=%d rot=%d -> send=%dx%d\n",
outIndex, duplDesc.ModeDesc.Width, duplDesc.ModeDesc.Height,
m_surfW, m_surfH, (int)realFmt, (int)m_rotation,