Fix: keep remote desktop audio alive when the playback device drops

Remote desktop audio could stop permanently after an idle period and only
came back by closing and reopening the remote desktop. The ring buffer and
WAVEHDRs were shared without locking between the IOCP worker thread
(OnAudioData) and the UI thread (MM_WOM_DONE): headers were queued twice
(waveOutWrite returning WAVERR_STILLPLAYING 33) and the non-atomic
m_nRingDataLen underflowed, leaving the buffer "full" of stale data.

Audio state is now serialized by a recursive m_AudioMutex, and opening,
resetting and closing the waveOut device happens only on the window's UI
thread: waveOutReset/Close can re-enter the MM_WOM_* handlers on that
thread, and a lock-holding worker would deadlock against the UI thread.
Workers post WM_AUDIO_INIT_DEVICE instead. Rebuilds are triggered by
format changes, waveOutWrite failures (throttled to 2 s), MM_WOM_CLOSE
with a 1 s guard against our own close notices, and a 2-second retry
while audio is enabled. Failed header preparation now tears the device
down instead of leaving a handle that rejects every write.

The client re-creates its Opus encoder when the endpoint channel count or
sample rate changes, downmixes more than two channels, and clamps
oversized WASAPI packets to the PCM staging buffer. Web audio forwarding
still runs outside the lock and is gated exactly as before. Verified by
building the Debug x64 server and client; the silence scenario itself was
not reproduced at runtime.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-07 09:52:55 +01:00
1 parent 4884b080e8
commit 74d64fec87
3 files changed
+260 -42

No files matched your search

+60 -16
View File
@@ -2618,8 +2618,10 @@ DWORD WINAPI CScreenManager::AudioThreadProc(LPVOID lpParam)
// Opus 编码器和累积缓冲区
COpusEncoder opusEncoder;
BOOL opusInitialized = FALSE;
int opusChannels = 0; // 编码器当前声道数(设备切换后可能变化)
BOOL warnedUnsupportedRate = FALSE;
const int OPUS_FRAME_SIZE = 960; // 20ms @ 48kHz
short* pOpusAccumBuffer = new short[OPUS_FRAME_SIZE * 2]; // 立体声
short* pOpusAccumBuffer = new short[OPUS_FRAME_SIZE * 2]; // 最多立体声
int nAccumSamples = 0; // 已累积的样本数(每声道)
BYTE* pOpusOutBuffer = new BYTE[4000]; // Opus 输出缓冲区
Mprintf("音频线程启动 (Opus 压缩启用)\n");
@@ -2670,7 +2672,14 @@ DWORD WINAPI CScreenManager::AudioThreadProc(LPVOID lpParam)
WAVEFORMATEX* pWaveFmt = (WAVEFORMATEX*)pThis->m_pWaveFormat;
if (numFramesAvailable > 0 && pWaveFmt) {
UINT32 numChannels = pWaveFmt->nChannels;
UINT32 numSamples = numFramesAvailable * numChannels;
// pPcmBuffer 只有 MAX_BUFFER/2 个 16-bit 样本。设备休眠/长时间静音后
// WASAPI 一次可能返回一个很大的包(整段静音),超过容量就要截断,
// 否则下面的 memset/memcpy 会写爆缓冲区、累积循环会读到堆外。
UINT32 framesAvail = numFramesAvailable;
if (numChannels > 0 && framesAvail > (UINT32)(MAX_BUFFER / 2) / numChannels) {
framesAvail = ((UINT32)(MAX_BUFFER / 2)) / numChannels;
}
UINT32 numSamples = framesAvail * numChannels;
// 判断源格式(使用精确的 SubFormat GUID 检测)
BOOL isFloat = IsFloatFormat(pWaveFmt);
@@ -2708,27 +2717,62 @@ DWORD WINAPI CScreenManager::AudioThreadProc(LPVOID lpParam)
#if USING_OPUS
// ===== Opus 编码模式 =====
// 初始化 Opus 编码器
if (!opusInitialized && pWaveFmt->nSamplesPerSec == 48000) {
if (opusEncoder.Init(48000, numChannels, 64000)) {
opusInitialized = TRUE;
Mprintf("Opus 编码器初始化成功: 48000 Hz, %d ch, 64 kbps\n", numChannels);
// Opus 只接受 mono/stereo。多声道设备(如 5.1)如果按原声道数拷贝,
// 会写爆 pOpusAccumBuffer(只按立体声分配),且主控解码出来也是噪声。
int encChannels = numChannels > 2 ? 2 : (int)numChannels;
// 设备休眠唤醒/默认端点变化会换掉混音格式。编码器必须跟着重建,
// 并重新下发格式包 —— 否则主控仍按旧声道数解码,表现为没声音。
// 端点采样率变了(48kHz -> 44.1kHz 等):固定 48kHz 的编码器不能继续用,
// 否则主控仍按 48kHz 播放,声音会变调/变速。销毁后走下面的"不支持"分支。
if (opusInitialized && pWaveFmt->nSamplesPerSec != 48000) {
opusEncoder.Destroy();
opusInitialized = FALSE;
nAccumSamples = 0;
}
if (pWaveFmt->nSamplesPerSec == 48000 &&
(!opusInitialized || opusChannels != encChannels)) {
if (opusInitialized) {
opusEncoder.Destroy();
opusInitialized = FALSE;
}
if (opusEncoder.Init(48000, encChannels, 64000)) {
opusInitialized = TRUE;
opusChannels = encChannels;
firstFrame = TRUE; // 让主控按新格式重建解码器/播放设备
nAccumSamples = 0;
Mprintf("Opus 编码器初始化成功: 48000 Hz, %d ch, 64 kbps\n", encChannels);
}
}
if (!opusInitialized && pWaveFmt->nSamplesPerSec != 48000 && !warnedUnsupportedRate) {
warnedUnsupportedRate = TRUE;
// 非 48kHz 端点当前没有重采样,Opus 无法编码:明确记录,别静默无声
Mprintf("音频端点采样率 %u Hz 不受支持(需 48000),该端点期间无音频\n",
pWaveFmt->nSamplesPerSec);
}
if (opusInitialized) {
// 累积样本到 Opus 帧缓冲区
int frameSamples = numFramesAvailable; // 每声道样本数
int srcOffset = 0;
while (srcOffset < (int)numFramesAvailable) {
while (srcOffset < (int)framesAvail) {
// 计算可以复制的样本数
int toCopy = min((int)numFramesAvailable - srcOffset, OPUS_FRAME_SIZE - nAccumSamples);
int toCopy = min((int)framesAvail - srcOffset, OPUS_FRAME_SIZE - nAccumSamples);
// 复制到累积缓冲区
memcpy(pOpusAccumBuffer + nAccumSamples * numChannels,
pConvertedPcm + srcOffset * numChannels,
toCopy * numChannels * sizeof(short));
if (encChannels == (int)numChannels) {
memcpy(pOpusAccumBuffer + nAccumSamples * encChannels,
pConvertedPcm + srcOffset * numChannels,
toCopy * encChannels * sizeof(short));
} else {
// 多声道设备只取前两个声道(左右)
const short* src = pConvertedPcm + srcOffset * numChannels;
short* dst = pOpusAccumBuffer + nAccumSamples * encChannels;
for (int s = 0; s < toCopy; ++s) {
for (int c = 0; c < encChannels; ++c) {
*dst++ = src[s * numChannels + c];
}
}
}
nAccumSamples += toCopy;
srcOffset += toCopy;
@@ -2744,10 +2788,10 @@ DWORD WINAPI CScreenManager::AudioThreadProc(LPVOID lpParam)
if (firstFrame) {
pSendBuffer[offset++] = 1; // hasFormat = true
AudioFormat fmt;
fmt.channels = (WORD)numChannels;
fmt.channels = (WORD)encChannels;
fmt.sampleRate = pWaveFmt->nSamplesPerSec;
fmt.bitsPerSample = 16;
fmt.blockAlign = (WORD)(numChannels * 2);
fmt.blockAlign = (WORD)(encChannels * 2);
fmt.compression = AUDIO_COMPRESS_OPUS;
fmt.reserved = 0;
memcpy(pSendBuffer + offset, &fmt, sizeof(AudioFormat));