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

+58 -32
View File
@@ -4,6 +4,7 @@
#include "stdafx.h"
#include "CaptureVideo.h"
#include <dvdmedia.h> // VIDEOINFOHEADER2:不少摄像头/驱动给的是 FORMAT_VideoInfo2
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
@@ -46,13 +47,14 @@ CCaptureVideo::~CCaptureVideo()
HRESULT CCaptureVideo::Open(int iDeviceID,int iPress)
{
Mprintf("CCaptureVideo call Open\n");
HRESULT hResult = S_FALSE;
HRESULT hResult = E_FAIL; // 默认失败:只有图真正跑起来才算成功
bool graphRunning = false;
do {
hResult = InitCaptureGraphBuilder();
if (FAILED(hResult))
break;
if (!BindVideoFilter(iDeviceID, &m_pBF)) {
hResult = S_FALSE;
hResult = E_FAIL; // 没有可用的 DirectShow 采集设备
break;
}
@@ -97,10 +99,29 @@ HRESULT CCaptureVideo::Open(int iDeviceID,int iPress)
//3 扑捉数据 FDO 一旦有数据就进行 回调函数调用 属于一个类
VIDEOINFOHEADER * vih = (VIDEOINFOHEADER*) mt.pbFormat;
// 只按 VIDEOINFOHEADER 解释 mt.pbFormat 不够:很多摄像头/驱动(尤其
// 笔记本内置摄像头)给出的格式是 FORMAT_VideoInfo2,两者头部布局不同,
// 直接强转会读出垃圾宽高,后面按它分配缓冲/逐帧 memcpy 就会崩。两种都认,
// 其它格式或非法尺寸一律当失败处理,让上层优雅退出而不是带病跑。
BITMAPINFOHEADER* bmi = NULL;
if (mt.formattype == FORMAT_VideoInfo && mt.pbFormat != NULL
&& mt.cbFormat >= sizeof(VIDEOINFOHEADER)) {
bmi = &((VIDEOINFOHEADER*)mt.pbFormat)->bmiHeader;
} else if (mt.formattype == FORMAT_VideoInfo2 && mt.pbFormat != NULL
&& mt.cbFormat >= sizeof(VIDEOINFOHEADER2)) {
bmi = &((VIDEOINFOHEADER2*)mt.pbFormat)->bmiHeader;
}
if (bmi == NULL || bmi->biWidth <= 0 || bmi->biHeight == 0
|| bmi->biWidth > 16384 || abs(bmi->biHeight) > 16384) {
Mprintf("CCaptureVideo: unsupported media type (formattype=%08lx cbFormat=%lu), abort\n",
(unsigned long)mt.formattype.Data1, (unsigned long)mt.cbFormat);
FreeMediaType(mt);
hResult = E_FAIL;
break;
}
//mCB 是个另外一个类 并且全局变量 有个回调
mCB.m_ulFullWidth = vih->bmiHeader.biWidth;
mCB.m_ulFullHeight = vih->bmiHeader.biHeight;
mCB.m_ulFullWidth = (ULONG)bmi->biWidth;
mCB.m_ulFullHeight = (ULONG)abs(bmi->biHeight);
FreeMediaType(mt);
@@ -120,11 +141,19 @@ HRESULT CCaptureVideo::Open(int iDeviceID,int iPress)
if(FAILED(hResult))
break;
// IMediaControl::Run 在「状态切换尚未完成、稍后异步完成」时会返回 S_FALSE,
// 这时图已经在跑、SampleGrabber 回调随时会到。以前把它原样返回给上层,
// 上层用 == S_OK 判断就误判成「摄像头没打开」并提前中止(8d4c118 引入),
// 而回调随后拿还没分配的 m_BitmapData_Full 做 memcpy —— 写地址 0 崩溃。
hResult = S_OK;
graphRunning = true;
} while (false);
Mprintf("CCaptureVideo Open %s\n", FAILED(hResult) ? "failed" : "succeed");
Mprintf("CCaptureVideo Open %s (hr=%08lX)\n",
graphRunning ? "succeed" : "failed", (unsigned long)hResult);
return hResult;
// 图没跑起来时一律返回失败码(S_FALSE 也是「成功」语义,会让上层误判)
return graphRunning ? S_OK : (FAILED(hResult) ? hResult : E_FAIL);
}
@@ -164,43 +193,40 @@ LPBITMAPINFO CCaptureVideo::GetBmpInfor()
BOOL CCaptureVideo::BindVideoFilter(int deviceId, IBaseFilter **pFilter)
{
if (deviceId < 0)
if (deviceId < 0 || pFilter == NULL)
return FALSE;// enumerate all video capture devices
*pFilter = NULL;
CComPtr<ICreateDevEnum> pCreateDevEnum;
HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
IID_ICreateDevEnum, (void**)&pCreateDevEnum);
if (hr != NOERROR) {
if (hr != NOERROR)
return FALSE;
}
CComPtr<IEnumMoniker> pEm;
hr = pCreateDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory,&pEm, 0);
if (hr != NOERROR) {
hr = pCreateDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pEm, 0);
if (hr != S_OK) // S_FALSE = 该类别下没有任何 DirectShow 设备
return FALSE;
}
pEm->Reset();
ULONG cFetched;
IMoniker *pM;
ULONG cFetched = 0;
IMoniker* pM = NULL;
int index = 0;
while(hr = pEm->Next(1, &pM, &cFetched), hr==S_OK, index <= deviceId) {
IPropertyBag *pBag;
//通过BindToStorage 可以访问该设备的标识集
hr = pM->BindToStorage(0, 0, IID_IPropertyBag, (void **)&pBag);
if(SUCCEEDED(hr)) {
VARIANT var;
var.vt = VT_BSTR;
hr = pBag->Read(L"FriendlyName", &var, NULL);
if (hr == NOERROR) {
if (index == deviceId) {
pM->BindToObject(0, 0, IID_IBaseFilter, (void**)pFilter);
}
SysFreeString(var.bstrVal);
}
pBag->Release(); //引用计数--
// 原实现写成逗号表达式 `while (hr = Next(...), hr == S_OK, index <= deviceId)`:
// 循环条件实际只看 index,枚举结束后还会继续用未初始化/已释放的 pM 调
// BindToStorage。机器上 VFW 能枚举到摄像头、DirectShow 却一个都没有时必崩。
while (pEm->Next(1, &pM, &cFetched) == S_OK && pM != NULL) {
if (index == deviceId) {
hr = pM->BindToObject(0, 0, IID_IBaseFilter, (void**)pFilter);
pM->Release();
return SUCCEEDED(hr) && *pFilter != NULL;
}
pM->Release();
index++;
pM = NULL;
++index;
}
return TRUE;
Mprintf("CCaptureVideo: no DirectShow video device #%d (enumerated %d)\n", deviceId, index);
return FALSE;
}