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

+27 -3
View File
@@ -96,6 +96,10 @@ extern "C"
// 来自 web 命令的音频开关,PostMessage 到对话框的 UI 线程,避免 WS 线程
// 直接动 waveOut 句柄
#define WM_AUDIO_TOGGLE_FROM_WEB (WM_USER + 0x202)
// 音频播放设备(打开/重建)请求,由 worker 线程 PostMessage 给 UI 线程处理。
// waveOutOpen/Reset/Close 会同步派发 MM_WOM_*,只能在窗口所属线程调用,
// 否则 worker 持锁等回调、UI 线程等锁,必然死锁。
#define WM_AUDIO_INIT_DEVICE (WM_USER + 0x203)
// ScreenSpyDlg 系统菜单命令 ID
enum {
@@ -424,14 +428,32 @@ public:
std::mutex m_AudioWebMutex; // 保护音频发送状态的互斥锁
// 注意:m_Settings.AudioEnabled 是全局的音频启用/禁用状态
// 环形缓冲区 / waveOut 状态的多线程保护。
// OnAudioData 跑在 IOCP worker 线程(见 CMy2015RemoteDlg::NotifyProc),
// OnWaveOutDone 跑在 UI 线程,二者都会改写环形缓冲区与 WAVEHDR。此前无锁,
// 两个线程会抢同一个缓冲区:waveOutWrite 返回 WAVERR_STILLPLAYING(33),且
// m_nRingDataLen 的非原子读改写会下溢,之后整场会话都在播缓冲区里的陈旧数据
// (表现为「一段时间后突然没声音,关掉重开远程桌面才恢复」)。
// 用递归锁:waveOutReset/waveOutClose 可能在调用线程里同步派发 MM_WOM_*,
// UI 线程重入处理器时必须能再次加锁(std::mutex 上重入 try_lock 是未定义行为)。
std::recursive_mutex m_AudioMutex;
AudioFormat m_LastAudioFormat = {}; // 最近一次收到的线上格式(设备重建用)
BOOL m_bHasAudioFormat = FALSE;
BOOL m_bAudioInitPending = FALSE; // 已在 worker 线程登记重建,等待 UI 线程处理
ULONGLONG m_nLastAudioInitTick = 0; // 上次尝试打开 waveOut 的时刻(失败重试节流)
ULONGLONG m_nLastAudioErrLog = 0; // 上次打印音频错误日志的时刻(日志节流)
void OnAudioData(BYTE* pData, UINT32 len); // 处理音频数据
BOOL InitAudioPlayback(const AudioFormat* fmt); // 初始化音频播放
void StopAudioPlayback(); // 停止音频播放
void StopAudioPlayback(); // 停止音频播放(任一线程,内部加锁)
BOOL InitAudioPlaybackLocked(const AudioFormat* fmt); // 调用方须持有 m_AudioMutex
void StopAudioPlaybackLocked(); // 调用方须持有 m_AudioMutex
void FeedAudioBuffersLocked(); // 调用方须持有 m_AudioMutex
void RequestAudioRebuildLocked(); // 调用方须持有 m_AudioMutex
void SanitizeRingBufferLocked(); // 调用方须持有 m_AudioMutex
void DisableAudio(); // 禁用音频(从网页命令)
void EnableAudio(); // 启用音频(从网页命令)
LRESULT OnAudioToggleFromWeb(WPARAM wParam, LPARAM lParam); // PostMessage 处理器
void SendAudioCtrl(BYTE enable, BYTE persist); // 发送音频控制命令
void FeedAudioBuffers(); // 填充音频缓冲区
void SendAudioToWeb(const BYTE* pAudioData, UINT32 len, const WAVEFORMATEX* pFormat, BYTE compression); // 发送音频到网页 (compression=AudioCompression)
int GetClientRTT(); // 获取客户端RTT(ms)
@@ -465,6 +487,8 @@ public:
afx_msg void OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized);
afx_msg LRESULT OnDisconnect(WPARAM wParam, LPARAM lParam);
afx_msg LRESULT OnWaveOutDone(WPARAM wParam, LPARAM lParam);
afx_msg LRESULT OnWaveOutClose(WPARAM wParam, LPARAM lParam);
afx_msg LRESULT OnAudioInitDevice(WPARAM wParam, LPARAM lParam);
afx_msg LRESULT OnRecvFileV2Chunk(WPARAM wParam, LPARAM lParam);
afx_msg LRESULT OnRecvFileV2Complete(WPARAM wParam, LPARAM lParam);
afx_msg void OnExitFullscreen()