Capture camera stream media foundation wrong format

МОСПК ГОБУЗ 1 Reputation point
2021-03-26T05:34:40.727+00:00

Hello. Please help me solve the problem. I am trying to get a stream of data from a webcam using Media Foundation.

IMFSourceReader* pReader;
HRESULT hr = MFCreateSourceReaderFromMediaSource(captureDevice, NULL, &pReader);
if (FAILED(hr))
return hr;

// Tried setting the video format like this:
//hr = SetDeviceFormat(captureDevice, FormatIndex);   //I need to configure the camera to format
//if (FAILED(hr))
// return hr; // But this did not give the desired result.


Next, I just check what is set by default:
IMFMediaType* pType = NULL;
hr = pReader->GetCurrentMediaType((DWORD)MF_SOURCE_READER_FIRST_VIDEO_STREAM, &pType);
LogMediaType(pType, 0, NULL);
pReader->SetCurrentMediaType((DWORD)MF_SOURCE_READER_FIRST_VIDEO_STREAM, NULL, pType);

//And the following format is set there:

**MF_MT_MAJOR_TYPE MFMediaType_Video
MF_MT_SUBTYPE MFVideoFormat_MJPG
MF_MT_FRAME_SIZE 3040 x 1080
MF_MT_ALL_SAMPLES_INDEPENDENT 1
MF_MT_SAMPLE_SIZE 9849600**

In the onReadSample callback, I should receive a sample with a buffer of size 6566400.
As if this format is set:

MF_MT_MAJOR_TYPE MFMediaType_Video
MF_MT_SUBTYPE MFVideoFormat_YUY2
MF_MT_FRAME_SIZE 3040 x 1080
MF_MT_DEFAULT_STRIDE 6080
MF_MT_ALL_SAMPLES_INDEPENDENT 1
MF_MT_FIXED_SIZE_SAMPLES 1
MF_MT_SAMPLE_SIZE 6566400

Format setting function

 HRESULT SetDeviceFormat(IMFMediaSource* pSource, DWORD dwFormatIndex)
    {
    IMFPresentationDescriptor* pPD = NULL;
    IMFStreamDescriptor* pSD = NULL;
    IMFMediaTypeHandler* pHandler = NULL;
    IMFMediaType* pType = NULL;
    IMFMediaType** pTypex = NULL;

    HRESULT hr = pSource->CreatePresentationDescriptor(&pPD);
    if (FAILED(hr))
    {
    goto done;
    }

    BOOL fSelected;
    hr = pPD->GetStreamDescriptorByIndex(0, &fSelected, &pSD);
    if (FAILED(hr))
    {
    goto done;
    }

    hr = pSD->GetMediaTypeHandler(&pHandler);
    if (FAILED(hr))
    {
    goto done;
    }

    hr = pHandler->GetMediaTypeByIndex(dwFormatIndex, &pType);
    if (FAILED(hr))
    {
    goto done;
    }

    LogMediaType(pType, dwFormatIndex, NULL);

    hr = pHandler->SetCurrentMediaType(pType);
    done:
    SafeRelease(&pPD);
    SafeRelease(&pSD);
    SafeRelease(&pHandler);
    SafeRelease(&pType);
    return hr;
    }

So my question is, why the callback function does not return samples from the type I have selected? Any advice?

Thanks in advance

Best regards

Windows development | Windows API - Win32
Developer technologies | C++
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.