Files
SimpleRemoter/client/CaptureVideo.h
T
yuanyuanxianganddeepseek-flash 94cb4584c8 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
2026-11-21 16:44:42 +01:00

260 lines
7.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// CaptureVideo.h: interface for the CCaptureVideo class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_CAPTUREVIDEO_H__0984BB8E_6DCB_4A5C_8E03_1217AE6E409D__INCLUDED_)
#define AFX_CAPTUREVIDEO_H__0984BB8E_6DCB_4A5C_8E03_1217AE6E409D__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <Objbase.h>
#include <uuids.h>
#include <strmif.h>
#include <CONTROL.H>
#include <ATLBASE.H>
#include <amvideo.h>
#include <DShow.h>
#pragma comment(lib,"Strmiids.lib")
// TODO 全局变量, 定义位置:qedit.h
// 接口 ID 回调接口,用于在每一帧抓取时通知应用
EXTERN_C const IID IID_ISampleGrabberCB;
// 类 ID 创建 Sample Grabber COM 对象
EXTERN_C const CLSID CLSID_SampleGrabber;
// 接口 ID 设置 Sample Grabber 的参数、格式、回调等操作接口
EXTERN_C const IID IID_ISampleGrabber;
struct ISampleGrabberCB : public IUnknown {
public:
virtual HRESULT STDMETHODCALLTYPE SampleCB(
double SampleTime,
IMediaSample * pSample) = 0;
virtual HRESULT STDMETHODCALLTYPE BufferCB(
double SampleTime,
BYTE* pBuffer,
long BufferLen) = 0;
};
struct ISampleGrabber : public IUnknown {
public:
virtual HRESULT STDMETHODCALLTYPE SetOneShot(
BOOL OneShot) = 0;
virtual HRESULT STDMETHODCALLTYPE SetMediaType(
const AM_MEDIA_TYPE* pType) = 0;
virtual HRESULT STDMETHODCALLTYPE GetConnectedMediaType(
AM_MEDIA_TYPE* pType) = 0;
virtual HRESULT STDMETHODCALLTYPE SetBufferSamples(
BOOL BufferThem) = 0;
virtual HRESULT STDMETHODCALLTYPE GetCurrentBuffer(
/* [out][in] */ long* pBufferSize,
/* [out] */ long* pBuffer) = 0;
virtual HRESULT STDMETHODCALLTYPE GetCurrentSample(
/* [retval][out] */ IMediaSample** ppSample) = 0;
virtual HRESULT STDMETHODCALLTYPE SetCallback(
ISampleGrabberCB* pCallback,
long WhichMethodToCallback) = 0;
};
enum {
CMD_CAN_COPY,
CMD_CAN_SEND
};
#ifndef SAFE_RELEASE
#define SAFE_RELEASE( x ) if ( NULL != x ){ x->Release(); x = NULL; }
#endif
class CSampleGrabberCB : public ISampleGrabberCB
{
public:
ULONG m_ulFullWidth;
ULONG m_ulFullHeight;
LPBITMAPINFO m_BitmapInfor_Full;
BYTE* m_BitmapData_Full;
BOOL bStact;
DWORD m_dwSize; // 视频图像数据大小
CSampleGrabberCB()
{
m_ulFullWidth = 0 ;
m_ulFullHeight = 0 ;
m_BitmapInfor_Full = NULL;
m_BitmapData_Full = NULL;
m_dwSize = 0;
bStact = CMD_CAN_COPY;
}
~CSampleGrabberCB()
{
if (m_BitmapInfor_Full!=NULL) {
delete[] m_BitmapInfor_Full;
}
if (m_BitmapData_Full!=NULL) {
delete[] m_BitmapData_Full;
}
m_ulFullWidth = 0 ;
m_ulFullHeight = 0 ;
}
LPBITMAPINFO GetBmpInfor()
{
if (m_BitmapInfor_Full==NULL) { //头信息
ConstructBI(24);
}
return m_BitmapInfor_Full;
}
LPBITMAPINFO ConstructBI(ULONG ulbiBitCount)
{
// 宽高来自摄像头驱动,必须挡住 0/超大值:否则 biSizeImage 会溢出成小值,
// 之后每帧 memcpy 都变成堆越界——「换一台机器一开视频就崩」多半出在这里。
if (m_ulFullWidth == 0 || m_ulFullHeight == 0
|| m_ulFullWidth > 16384 || m_ulFullHeight > 16384)
return NULL;
// 分配「头部 + 完整 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);
BitmapInforHeader->biSize = sizeof(BITMAPINFOHEADER);//pi si
BitmapInforHeader->biWidth = m_ulFullWidth;
BitmapInforHeader->biHeight = m_ulFullHeight;
BitmapInforHeader->biPlanes = 1;
BitmapInforHeader->biBitCount = ulbiBitCount;
BitmapInforHeader->biCompression = BI_RGB;
BitmapInforHeader->biXPelsPerMeter = 0;
BitmapInforHeader->biYPelsPerMeter = 0;
BitmapInforHeader->biClrUsed = 0;
BitmapInforHeader->biClrImportant = 0;
BitmapInforHeader->biSizeImage = //图像数据
(((BitmapInforHeader->biWidth * BitmapInforHeader->biBitCount + 31) & ~31) >> 3)
* BitmapInforHeader->biHeight;
// 16位和以后的没有颜色表,直接返回
//!!
m_dwSize=BitmapInforHeader->biSizeImage; //数据大小
m_BitmapData_Full=new BYTE[m_dwSize+10];
ZeroMemory(m_BitmapData_Full,m_dwSize+10);
return m_BitmapInfor_Full;
}
STDMETHODIMP_(ULONG) AddRef()
{
return 2;
}
STDMETHODIMP_(ULONG) Release()
{
return 1;
}
STDMETHODIMP QueryInterface(REFIID riid, void ** lParam)
{
if( riid == IID_ISampleGrabberCB || riid == IID_IUnknown ) {
*lParam = (void *) static_cast<ISampleGrabberCB*> ( this );
return NOERROR;
}
return E_NOINTERFACE;
}
BYTE* GetNextScreen(DWORD &dwSize)
{
dwSize=m_dwSize;
return (BYTE*)m_BitmapData_Full;
}
STDMETHODIMP SampleCB(double dbSampleTime, IMediaSample * Sample)
{
return 0;
}
//回调函数 在这里得到 bmp 的数据
STDMETHODIMP BufferCB(double dblSampleTime, BYTE * szBuffer, long ulBufferSize)
{
if (!szBuffer) {
return E_POINTER;
}
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,(size_t)ulBufferSize); //位图
InterlockedExchange((LPLONG)&bStact,CMD_CAN_SEND); //原子自增可以发送
return S_OK;
}
return -1;
}
};
class CCaptureVideo
{
public:
CCaptureVideo();
virtual ~CCaptureVideo();
LPBITMAPINFO GetBmpInfor();
HRESULT InitCaptureGraphBuilder();
HRESULT Open(int iDeviceID,int iPress);
BOOL BindVideoFilter(int deviceId, IBaseFilter **pFilter);
LPBYTE GetDIB(DWORD& dwSize);
int GetDIBBufSize() const
{
return mCB.m_dwSize;
}
BOOL m_bExit;
HWND m_hWnd;
static CSampleGrabberCB mCB;
IGraphBuilder * m_pGB; //通过该值可以访问 FCDO Filter Control Device Object
ICaptureGraphBuilder2* m_pCapture; //通过该值可以访问 真实CDO
IMediaControl* m_pMC; //过滤设备的接口
IVideoWindow* m_pVW;
IBaseFilter* m_pBF; //FDO
ISampleGrabber* m_pGrabber; //引脚 24Color
void FreeMediaType(AM_MEDIA_TYPE& mt);
void ResizeVideoWindow();
HRESULT SetupVideoWindow();
void SendEnd();
};
#endif // !defined(AFX_CAPTUREVIDEO_H__0984BB8E_6DCB_4A5C_8E03_1217AE6E409D__INCLUDED_)