使用IMFSourceReaderCallback检测USB摄像头断开连接
Detect USB camera disconnection using IMFSourceReaderCallback
我正在使用IMFSourceReaderCallback异步c++实现来读取和处理USB摄像头视频流
它工作得很好,只是如果相机被拔下(这种情况经常发生,因为我们使用了很多USB中继器),我不会收到通知。以下是代码摘录:
HRESULT CMFSourceReaderCallback::OnEvent(DWORD dwStreamIndex, IMFMediaEvent *pEvent)
{
// I was hoping to get an Event here if I lost the camera, but no...
// The break point nether hits, and from EvilCorp documentation, there is no such event type
return S_OK;
}
HRESULT CMFSourceReaderCallback::OnReadSample(
HRESULT hrStatus,
DWORD dwStreamIndex,
DWORD dwStreamFlags,
LONGLONG llTimestamp,
IMFSample *pSample)
{
bool isGrabbing = false;
try
{
if (SUCCEEDED(hrStatus))
{
if (pSample)
{
// Do something with the sample.
IMFMediaBuffer * pMediaBuffer;
HRESULT hr;
hr = pSample->ConvertToContiguousBuffer(&pMediaBuffer);
if (FAILED(hr))
{
// Inform other thread of the disconnection
//...
return S_OK;
}
byte *imgBuff;
DWORD buffCurrLen = 0;
DWORD buffMaxLen = 0;
pMediaBuffer->Lock(&imgBuff, &buffMaxLen, &buffCurrLen);
// Process image byte buffer
pMediaBuffer->Unlock();
pMediaBuffer->Release();
}
}
else
{
// Inform other thread of the disconnection
//...
return S_OK;
}
if ((MF_SOURCE_READERF_ENDOFSTREAM & dwStreamFlags) ||
(MF_SOURCE_READERF_ERROR & dwStreamFlags))
{
// Inform other thread of the disconnection
//...
return S_OK;
}
} catch (std::exception &ex )
{
// Inform other thread of the disconnection
//...
return S_OK;
}
// check if other thread has not requested a stop
isGrabbing = ...;
if (isGrabbing)
{
//Re-arm callback
HRESULT hr = _dataStruct->Reader->ReadSample(MF_SOURCE_READER_FIRST_VIDEO_STREAM,
0, NULL, NULL, NULL, NULL);
if (FAILED(hr))
{
// Inform other thread of the disconnection
//...
return S_OK;
}
}
return S_OK;
}
有没有一种方法可以通过IMFSourceReader获得这样的通知,而不用担心不时使用MFEnumDeviceSources轮询可用设备,这可能很耗时?
提前感谢!
这里解释了处理捕获设备丢失的推荐方法:处理视频设备丢失
您需要注册设备通知:RegisterDeviceNotification。您需要一个窗口句柄(HWND)。
在消息循环中,您将收到WM_DEVICECHANGE。你必须检查符号链接(是USB摄像头吗?)。
完成后,请调用UnregisterDeviceNotification。
相关文章:
- 升压信号2将插槽传递到成员功能以断开连接
- 视频在唤醒其他线程时输入设备断开连接
- SNMP 代理在单元测试期间断开连接
- 断开连接后重新连接boost beast(asio)websocket和http连接时出错
- C++套接字客户端断开连接
- 如何在 Windows 进程回调中断开连接时获取设备信息(硬件 ID)
- 如何在 Grpc 中使用双向流时检测(物理)断开连接
- 对等方断开连接后未释放 SSL 内存
- SIGPIPE C++确定哪个 TCP 套接字已断开连接
- C++中断开连接的记录集
- 断开连接时Arduino系列读取
- Poco::Net HTTPServer:如何检测断开连接的客户端?
- C++执行会导致监视器断开连接
- QJSONRPC 中的客户端连接/断开连接事件
- 如何检测用户何时与网络断开连接
- AJAX与C HTTP服务器断开连接
- 如何在 Thrift 中检测服务器断开连接
- 如何检测程序运行时 Kinect 何时断开连接/拔出
- Arduino 以太网扩展板无法检测到断开连接
- 加速和 Windows 套接字 - 正确处理 TCP 客户端断开连接方案