Media Foundation: Cannot change a FPS on webcam

1

I try to replace codes with Directshow ("DS") on Media Foundation ("MF") in my app and met one problem - cannot set a needed fps using MF on a webcam. MF allowed me to set only 30 fps. If I try to set 25 fps, I always get the error 0xc00d5212 on SetCurrentMediaType(). In DS I could change that parameter.

My codes:

ASSERT(m_pReader); //IMFSourceReader *m_pReader;
IMFMediaType *pNativeType = NULL;
IMFMediaType *pType = NULL;
UINT32 w = 1280; 
UINT32 h = 720; 
UINT32 fps = 25; // or 30

DWORD dwStreamIndex = MF_SOURCE_READER_FIRST_VIDEO_STREAM;

// Find the native format of the stream.
HRESULT hr = m_pReader->GetNativeMediaType(dwStreamIndex, 0, &pNativeType);
if (FAILED(hr))
{
  //error
}

GUID majorType, subtype;

// Find the major type.
hr = pNativeType->GetGUID(MF_MT_MAJOR_TYPE, &majorType);
if (FAILED(hr))
{
  //error
}
// Define the output type.
hr = MFCreateMediaType(&pType);
if (FAILED(hr))
{
  //error
}
hr = pType->SetGUID(MF_MT_MAJOR_TYPE, majorType);
if (FAILED(hr))
{
  //error
}
// Select a subtype.
if (majorType == MFMediaType_Video)
{
    subtype= MFVideoFormat_RGB24;
}
else
{
  //error
}
hr = pType->SetGUID(MF_MT_SUBTYPE, subtype);
if (FAILED(hr))
{
  //error
}
hr = MFSetAttributeSize(pType, MF_MT_FRAME_SIZE, w, h);
if (FAILED(hr))
{
  //error
}
hr = MFSetAttributeSize(pType, MF_MT_FRAME_RATE, fps, 1);
if (FAILED(hr))
{
  //error
}
hr = m_pReader->SetCurrentMediaType(dwStreamIndex, NULL, pType);
if (FAILED(hr))
{// hr = 0xc00d5212 
  //!!!!!error - if fps == 25 
}
return hr;

Thanks for any help.

c++
webcam
directshow
video-capture
ms-media-foundation
asked on Stack Overflow Dec 7, 2016 by victor kulichkin • edited Dec 7, 2016 by Roman R.

2 Answers

3

It might so happen that the camera does not support flexible frame rate values, and can work with only among the supported set, for example: 10, 15, 20, 24, 30 fps. You should be able to enumerate supported media types and choose the one that works for you - those media types typically include frame rate options.

Even though Media Foundation and DirectShow video capture eventually ends up in the same backend, there might be discrepancies in behavior. Specifically, you are working with Media Foundation higher level API that internally interfaces to a media source, and it might so happens that frame rate leads to 0xC00D5212 MF_E_TOPO_CODEC_NOT_FOUND "No suitable transform was found to encode or decode the content" confusion even though technically the driver can capture in respective mode.

See also:

answered on Stack Overflow Dec 7, 2016 by Roman R. • edited May 23, 2017 by Community
0

I've added the timer for fps control imitation into the codes. So at the start I set 30 fps , then by fps scale I skip some frames for my app. Thank you for help.

answered on Stack Overflow Dec 11, 2016 by victor kulichkin

User contributions licensed under CC BY-SA 3.0