Fix: recover remote desktop audio after dropped formats and sleep

The client's audio thread starts before the capture thread sends
TOKEN_BITMAPINFO, so its first packet - the only one carrying the
AudioFormat - reaches the master before CScreenSpyDlg exists and is
dropped as an unknown command. Without a format the master cannot open
waveOut, and the client re-announced only on an audio off/on, so a
remote desktop opened with audio enabled stayed silent until the toolbar
button was toggled twice. The master now asks for a re-announce when
data arrives with no format on record (CMD_AUDIO_CTRL enable,
persist=0, one request per 2 s, 15 attempts max), and the client
attaches the format to its next packet.

Entering Modern Standby tears down the render endpoint. The client got
AUDCLNT_E_DEVICE_INVALIDATED and rebuilt its loopback capture in that
same instant; the new client initialized and started fine but was bound
to the dying audio engine, so after resume GetNextPacketSize returned
zero packets forever without error, and only a fresh screen session
recovered. The audio thread now measures its own loop gap (GetTickCount64
plus wall clock, 2 s; a normal pass is ~10 ms) and rebuilds the capture
client after the machine was frozen, waits a second after an
invalidation, and rebuilds on an audio off-to-on.

The master rebuilds waveOut on resume from WM_POWERBROADCAST. The format
recovery was verified live; the resume recovery is build-verified and
awaits a field check on a sleeping machine.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-07 12:26:49 +01:00
1 parent 74d64fec87
commit f96e4a7320
4 files changed
+107 -5

No files matched your search

+7
View File
@@ -119,6 +119,13 @@ public:
// ========== 系统音频捕获 (WASAPI Loopback) ==========
volatile BOOL m_bAudioThreadRunning = FALSE;// 音频线程运行标志(独立于视频线程)
volatile BOOL m_bAudioInitialized = FALSE; // WASAPI 是否已初始化
// 主控请求重发音频格式包:带格式的首包可能比 TOKEN_BITMAPINFO 先到,被主控当未知
// 命令丢掉;主控发现没收过格式时会发 enable 控制命令来要一次。幂等:格式没变主控不重建。
volatile BOOL m_bAudioFormatResend = FALSE;
// 请求音频线程重建 WASAPI 捕获客户端。系统休眠/恢复后旧的捕获客户端会作废,
// 但 GetNextPacketSize 不报错、永远返回 0,从此再也收不到包(主控那边看起来
// 一切正常)。COM 对象只允许音频线程自己释放,所以这里只登记标志。
volatile BOOL m_bAudioDeviceDirty = FALSE;
HANDLE m_hAudioThread = NULL; // 音频线程句柄
HANDLE m_hAudioEvent = NULL; // 控制线程挂起/唤醒