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

+38
View File
@@ -611,6 +611,7 @@ BEGIN_MESSAGE_MAP(CScreenSpyDlg, CDialog)
ON_MESSAGE(MM_WOM_DONE, &CScreenSpyDlg::OnWaveOutDone)
ON_MESSAGE(MM_WOM_CLOSE, &CScreenSpyDlg::OnWaveOutClose)
ON_MESSAGE(WM_AUDIO_INIT_DEVICE, &CScreenSpyDlg::OnAudioInitDevice)
ON_MESSAGE(WM_POWERBROADCAST, &CScreenSpyDlg::OnPowerResume)
ON_MESSAGE(WM_RECVFILEV2_CHUNK, &CScreenSpyDlg::OnRecvFileV2Chunk)
ON_MESSAGE(WM_RECVFILEV2_COMPLETE, &CScreenSpyDlg::OnRecvFileV2Complete)
ON_MESSAGE(WM_AUDIO_TOGGLE_FROM_WEB, &CScreenSpyDlg::OnAudioToggleFromWeb)
@@ -4400,6 +4401,22 @@ void CScreenSpyDlg::OnAudioData(BYTE* pData, UINT32 len)
}
}
// 格式包丢失:客户端的音频线程在子连接建立时就开始跑,带格式的首包常常比
// TOKEN_BITMAPINFO 先到 —— 那时远程桌面对话框还没创建,主窗口把这批包当未知命令
// 丢掉(见 CMy2015RemoteDlg::MessageHandle 的 default 分支)。没有格式就开不了设备,
// 客户端又只在「关→开」时才重发格式,于是整场会话无声,直到用户手动拨一次音频开关。
// 这里借后续数据包主动请客户端重发格式:enable + persist=0,只唤醒音频线程、
// 不改客户端配置。次数封顶,老客户端不认这个请求时不会无限发。
// 触发点是「已经路由到本对话框的包」,所以对话框必然已就绪,不存在再次丢包的窗口。
if (!m_bHasAudioFormat && m_Settings.AudioEnabled &&
m_nAudioFmtReqCount < 15 &&
GetTickCount64() - m_nLastAudioFmtReqTick >= 2000) {
m_nLastAudioFmtReqTick = GetTickCount64();
m_nAudioFmtReqCount++;
SendAudioCtrl(CYCLEAUDIO_ENABLE, 0);
Mprintf("[Audio] 未收到音频格式包,请求客户端重发 (%d)\n", m_nAudioFmtReqCount);
}
// 播放设备未就绪:首包格式包丢失、或 waveOutOpen 因设备休眠/被独占而一时失败。
// 有缓存格式就定期请 UI 线程重试,否则整场会话无声,只能关掉重开远程桌面。
// 只在音频处于启用状态时重试,避免用户关掉后又被残留包拉起来。
@@ -4679,6 +4696,27 @@ LRESULT CScreenSpyDlg::OnAudioInitDevice(WPARAM, LPARAM)
return 0;
}
LRESULT CScreenSpyDlg::OnPowerResume(WPARAM wParam, LPARAM wParamUnused)
{
// 系统休眠(现代待机)时音频端点会被拆除再重建。waveOut 句柄有时既不派发
// MM_WOM_CLOSE、waveOutWrite 也照样返回成功,声音却已经没有了 —— 表现为
// 「闲置一段时间后再放声音没声音,只能重开远程桌面」。恢复后主动重建一次,
// 打开失败也没关系:m_bAudioPlaying 保持 FALSE,OnAudioData 会每 2 秒重试。
if (wParam != PBT_APMRESUMEAUTOMATIC && wParam != PBT_APMRESUMESUSPEND) {
return TRUE;
}
std::lock_guard<std::recursive_mutex> lock(m_AudioMutex);
if (!m_bHasAudioFormat || !m_Settings.AudioEnabled) return TRUE;
Mprintf("[Audio] 系统从休眠恢复,重建播放设备\n");
if (m_bAudioPlaying) {
StopAudioPlaybackLocked();
}
InitAudioPlaybackLocked(&m_LastAudioFormat);
return TRUE;
}
LRESULT CScreenSpyDlg::OnWaveOutClose(WPARAM wParam, LPARAM wParamUnused)
{
// 设备被移除/切换(显示器休眠、默认输出端点变化等)时,系统会关闭 waveOut 句柄