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
+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;
|
||||
|
||||
Reference in new issue
Block a user