Quantcast
Channel: Media Foundation Development for Windows Desktop forum
Viewing all articles
Browse latest Browse all 1079

How to capture h264 raw stream from a camera device?

$
0
0

My Logitech C920 cam has 3 streams to output,and the stream3 output h264 raw stream,I wrote below code to capture h264 raw stream with HD1080p30 format,but the "ReadSample()" function failed,it return "MF_E_INVALIDMEDIATYPE",so what's wrong with my code?How to capture h264 raw stream?

    HRESULT hr;
    LPCWSTR pwsDevSymbLink = (LPCWSTR)m_cbCapDevices.GetItemData(m_cbCapDevices.GetCurSel());
    DWORD dwStreamIdx = (DWORD)m_cbDevStreams.GetCurSel();    // is 2 beacause camera output h264 on stream3
    DWORD dwMediaTypeIdx = (DWORD)m_cbStreamMediaTypes.GetCurSel();    // the mediatype index for HD1080p30 video format

    CVideoCapCallback vidCapCB;        // capture callback class

    CComPtr<IMFAttributes> pAttrs;
    MFCreateAttributes(&pAttrs,2);
    hr = pAttrs->SetUnknown(MF_SOURCE_READER_ASYNC_CALLBACK,&vidCapCB);
    assert(hr == S_OK);
    hr = pAttrs->SetUINT32(MF_READWRITE_DISABLE_CONVERTERS,TRUE);
    assert(hr == S_OK);

    CComPtr<IMFMediaSource> pSourceDev;
    mfcCreateDeviceSource(pwsDevSymbLink,TRUE,&pSourceDev);
    assert(pSourceDev);

    CComPtr<IMFSourceReader> pReader;
    hr = MFCreateSourceReaderFromMediaSource(pSourceDev,pAttrs,&pReader);
    assert(hr == S_OK);
    vidCapCB.m_pReader = pReader;
    vidCapCB.m_dwRdStream = dwStreamIdx;
    CComPtr<IMFMediaType> pTargetMT;
    hr = pReader->GetNativeMediaType(dwStreamIdx,dwMediaTypeIdx,&pTargetMT);    // get the HD1080p30 mediatype
    assert(SUCCEEDED(hr));
    hr = pReader->SetCurrentMediaType(dwStreamIdx,NULL,pTargetMT);
    assert(SUCCEEDED(hr));
    hr = pReader->SetStreamSelection(vidCapCB.m_dwRdStream,TRUE);
    assert(hr == S_OK);
    hr = pReader->ReadSample(vidCapCB.m_dwRdStream,0,NULL,NULL,NULL,NULL);
    assert(SUCCEEDED(hr));    // return S_OK,because I use async callback,but the "hrStatus" of "OnReadSample()" is MF_E_INVALIDMEDIATYPE;

    AfxMessageBox(_T("InTest..."));

    pReader = NULL;    


Viewing all articles
Browse latest Browse all 1079

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>