首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用IMFSourceReaderCallback检测USB摄像头断开

用IMFSourceReaderCallback检测USB摄像头断开
EN

Stack Overflow用户
提问于 2015-12-17 10:45:16
回答 1查看 1.5K关注 0票数 2

我正在使用IMFSourceReaderCallback异步c++实现来读取和处理USB摄像机视频流。

它的工作很好,除了如果相机被拔掉(这可能经常发生,因为我们正在使用许多USB中继器),我没有收到通知。下面是代码的摘录:

代码语言:javascript
复制
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轮询可用的设备,这会很费时.?

提前谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-17 19:21:22

下面解释了处理捕获设备丢失的推荐方法:处理视频设备丢失

您需要注册设备通知: RegisterDeviceNotification。您需要一个窗口句柄(HWND)。

在message循环中,您将收到WM_DEVICECHANGE。您必须检查符号链接(是USB摄像头吗?)

当您完成任务后,请调用UnregisterDeviceNotification。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34332619

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档