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:
1 parent
74d64fec87
commit
f96e4a7320
4 files changed
+107
-5
No files matched your search
@@ -2452,6 +2452,15 @@ void CScreenManager::SaveQualityProfiles()
|
||||
|
||||
// ========== WASAPI 系统音频捕获实现 ==========
|
||||
|
||||
// 墙钟(100ns,UTC)。GetTickCount64 在现代待机期间是否继续走依赖平台/固件,
|
||||
// 墙钟一定走;两者一起比才能可靠识别「线程被冻结了一段时间」。
|
||||
static ULONGLONG NowFileTime100ns()
|
||||
{
|
||||
FILETIME ft;
|
||||
GetSystemTimeAsFileTime(&ft);
|
||||
return ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
|
||||
}
|
||||
|
||||
BOOL CScreenManager::InitWASAPILoopback()
|
||||
{
|
||||
if (m_bAudioInitialized) return TRUE;
|
||||
@@ -2568,6 +2577,7 @@ void CScreenManager::UninitWASAPI()
|
||||
|
||||
void CScreenManager::HandleAudioCtrl(BYTE enable, BYTE persist)
|
||||
{
|
||||
BOOL wasEnabled = m_ScreenSettings.AudioEnabled;
|
||||
m_ScreenSettings.AudioEnabled = enable;
|
||||
|
||||
// 持久化到客户端配置
|
||||
@@ -2578,6 +2588,14 @@ void CScreenManager::HandleAudioCtrl(BYTE enable, BYTE persist)
|
||||
|
||||
if (enable) {
|
||||
// 启用音频:唤醒线程(WASAPI 由音频线程自己初始化)
|
||||
// 无论当前是否已在传输,都标记重发一次格式:主控收不到格式包时正是用 enable
|
||||
// (persist=0)来要的(首包比远程桌面窗口先到会被丢)。已在传时多一个格式包无害。
|
||||
m_bAudioFormatResend = TRUE;
|
||||
// 关闭期间的休眠会让残留的捕获客户端作废(且不会报错),所以由「关」转「开」
|
||||
// 时重建一次。主控用 enable(persist=0)来要格式包时音频本来就是开的,不必重建。
|
||||
if (!wasEnabled) {
|
||||
m_bAudioDeviceDirty = TRUE;
|
||||
}
|
||||
if (m_hAudioEvent) {
|
||||
SetEvent(m_hAudioEvent);
|
||||
}
|
||||
@@ -2629,11 +2647,41 @@ DWORD WINAPI CScreenManager::AudioThreadProc(LPVOID lpParam)
|
||||
Mprintf("音频线程启动\n");
|
||||
#endif
|
||||
|
||||
// 正常一轮只有 10ms 上下。系统进入待机时本线程被冻结,恢复后这里会看到一段
|
||||
// 很长的时间差 —— 而进待机那一刻重建出来的捕获客户端已经绑在被销毁的音频引擎上
|
||||
// (日志:进现代待机时重建成功,唤醒后 GetNextPacketSize 一直返回 0,再也不出包,
|
||||
// 只有重开远程桌面才好)。恢复后必须重建。
|
||||
ULONGLONG lastTick = GetTickCount64();
|
||||
ULONGLONG lastWall = NowFileTime100ns();
|
||||
|
||||
while (pThis->m_bAudioThreadRunning) {
|
||||
// 阈值 2 秒:远大于正常循环周期,也不会误伤 1000ms 的初始化重试等待。
|
||||
ULONGLONG tickNow = GetTickCount64();
|
||||
ULONGLONG wallNow = NowFileTime100ns();
|
||||
BOOL woken = (tickNow - lastTick > 2000) || (wallNow - lastWall > 20000000ULL);
|
||||
lastTick = tickNow;
|
||||
lastWall = wallNow;
|
||||
if (woken || pThis->m_bAudioDeviceDirty) {
|
||||
if (woken) {
|
||||
Mprintf("检测到系统休眠后恢复(或线程被挂起),重建 WASAPI 捕获\n");
|
||||
}
|
||||
pThis->m_bAudioDeviceDirty = FALSE;
|
||||
pThis->UninitWASAPI();
|
||||
firstFrame = TRUE; // 新采集要重新下发格式,主控那边才有机会核对设备状态
|
||||
#if USING_OPUS
|
||||
nAccumSamples = 0;
|
||||
#endif
|
||||
// 正在停止时别再重建:紧接着的初始化会和析构里的 UninitWASAPI 抢同一组指针
|
||||
if (!pThis->m_bAudioThreadRunning) break;
|
||||
}
|
||||
|
||||
// 如果音频未启用,挂起等待
|
||||
if (!pThis->m_ScreenSettings.AudioEnabled) {
|
||||
firstFrame = TRUE; // 下次启用时重新发送格式
|
||||
WaitForSingleObject(pThis->m_hAudioEvent, INFINITE);
|
||||
// 这次等待可能跨越休眠或很长一段关闭期,别算进上面的时间差检测
|
||||
lastTick = GetTickCount64();
|
||||
lastWall = NowFileTime100ns();
|
||||
if (!pThis->m_bAudioThreadRunning) break;
|
||||
continue;
|
||||
}
|
||||
@@ -2650,10 +2698,12 @@ DWORD WINAPI CScreenManager::AudioThreadProc(LPVOID lpParam)
|
||||
UINT32 packetLength = 0;
|
||||
HRESULT hr = pThis->m_pCaptureClient->GetNextPacketSize(&packetLength);
|
||||
if (FAILED(hr)) {
|
||||
// WASAPI 调用失败,可能是设备状态变化(如切换显示器),重新初始化
|
||||
// WASAPI 调用失败,可能是设备状态变化(如切换显示器),重新初始化。
|
||||
// 等 1 秒再重建:设备正在被系统拆除/重建时抢着 Initialize 会拿到一个
|
||||
// 「初始化成功但永远没数据」的客户端(进入现代待机那一刻就踩过)。
|
||||
Mprintf("GetNextPacketSize 失败: 0x%08X,重新初始化 WASAPI\n", hr);
|
||||
pThis->UninitWASAPI();
|
||||
Sleep(100);
|
||||
Sleep(1000);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -2785,7 +2835,7 @@ DWORD WINAPI CScreenManager::AudioThreadProc(LPVOID lpParam)
|
||||
UINT32 offset = 0;
|
||||
pSendBuffer[offset++] = TOKEN_SCREEN_AUDIO;
|
||||
|
||||
if (firstFrame) {
|
||||
if (firstFrame || pThis->m_bAudioFormatResend) {
|
||||
pSendBuffer[offset++] = 1; // hasFormat = true
|
||||
AudioFormat fmt;
|
||||
fmt.channels = (WORD)encChannels;
|
||||
@@ -2797,6 +2847,7 @@ DWORD WINAPI CScreenManager::AudioThreadProc(LPVOID lpParam)
|
||||
memcpy(pSendBuffer + offset, &fmt, sizeof(AudioFormat));
|
||||
offset += sizeof(AudioFormat);
|
||||
firstFrame = FALSE;
|
||||
pThis->m_bAudioFormatResend = FALSE;
|
||||
Mprintf("发送音频格式: %d Hz, %d ch, Opus 压缩\n",
|
||||
fmt.sampleRate, fmt.channels);
|
||||
} else {
|
||||
@@ -2817,8 +2868,8 @@ DWORD WINAPI CScreenManager::AudioThreadProc(LPVOID lpParam)
|
||||
UINT32 offset = 0;
|
||||
pSendBuffer[offset++] = TOKEN_SCREEN_AUDIO;
|
||||
|
||||
// 首帧带格式信息
|
||||
if (firstFrame) {
|
||||
// 首帧带格式信息(或主控请求重发)
|
||||
if (firstFrame || pThis->m_bAudioFormatResend) {
|
||||
pSendBuffer[offset++] = 1; // hasFormat = true
|
||||
AudioFormat fmt;
|
||||
fmt.channels = (WORD)numChannels;
|
||||
@@ -2830,6 +2881,7 @@ DWORD WINAPI CScreenManager::AudioThreadProc(LPVOID lpParam)
|
||||
memcpy(pSendBuffer + offset, &fmt, sizeof(AudioFormat));
|
||||
offset += sizeof(AudioFormat);
|
||||
firstFrame = FALSE;
|
||||
pThis->m_bAudioFormatResend = FALSE;
|
||||
Mprintf("发送音频格式: %d Hz, %d ch, 16 bit PCM (源: %d bit %s)\n",
|
||||
fmt.sampleRate, fmt.channels, pWaveFmt->wBitsPerSample,
|
||||
isFloat ? "Float" : "PCM");
|
||||
|
||||
Reference in new issue
Block a user