Düzenle

Aracılığıyla paylaş


How to Set the Video Capture Frame Rate

A video capture device might support a range of frame rates. If this information is available, the minimum and maximum frame rates are stored as media type attributes:

Attribute Description
MF_MT_FRAME_RATE_RANGE_MAX Maximum frame rate.
MF_MT_FRAME_RATE_RANGE_MIN Minimum frame rate.

 

The range can vary depending on the capture format. For example, at larger frame sizes, the maximum frame rate might be reduced.

To set the frame rate:

  1. Create the media source for the capture device. See Enumerating Video Capture Devices.
  2. Call IMFMediaSource::CreatePresentationDescriptor on the media source to get the presentation descriptor.
  3. Call IMFPresentationDescriptor::GetStreamDescriptorByIndex to get the stream descriptor for the video stream.
  4. Call IMFStreamDescriptor::GetMediaTypeHandler on the stream descriptor.
  5. Enumerate the capture formats, as described in How to Set the Video Capture Format.
  6. Select the desired output format from the list.
  7. Query the media type for the MF_MT_FRAME_RATE_RANGE_MAX and MF_MT_FRAME_RATE_RANGE_MIN attributes. This values give the range of supported frame rates. The device might support other frame rates within this range.
  8. Set the MF_MT_FRAME attribute on the media type to specify the desired frame rate.
  9. Call IMFMediaTypeHandler::SetCurrentMediaType with the modified media type.

The following example sets the frame rate equal to the maximum frame rate that the device supports:

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

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

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

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

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

    // Get the maximum frame rate for the selected capture format.

    // Note: To get the minimum frame rate, use the 
    // MF_MT_FRAME_RATE_RANGE_MIN attribute instead.

    PROPVARIANT var;
    if (SUCCEEDED(pType->GetItem(MF_MT_FRAME_RATE_RANGE_MAX, &var)))
    {
        hr = pType->SetItem(MF_MT_FRAME_RATE, var);

        PropVariantClear(&var);

        if (FAILED(hr))
        {
            goto done;
        }

        hr = pHandler->SetCurrentMediaType(pType);
    }

done:
    SafeRelease(&pPD);
    SafeRelease(&pSD);
    SafeRelease(&pHandler);
    SafeRelease(&pType);
    return hr;
}

Video Capture