Media Foundation API Reference
Overview
Media Foundation is Microsoft’s multimedia platform for audio‑video playback, encoding, transcoding, and streaming. It provides a unified framework for handling media pipelines, device enumeration, and media processing.
Getting Started
To start using Media Foundation, initialize the platform with MFStartup, create a media session, and add sources or sinks as needed. The following minimal example creates a session and shuts it down:
#include <mfapi.h>
#include <mfmediaengine.h>
int main() {
MFStartup(MF_VERSION);
IMFMediaSession *pSession = nullptr;
MFCreateMediaSession(nullptr, &pSession);
// ... configure session ...
pSession->Close();
pSession->Release();
MFShutdown();
return 0;
}
Core Interfaces
- IMFMediaSession – Central object that manages the playback graph.
- IMFSourceResolver – Resolves URLs or file paths to media sources.
- IMFMediaSource – Represents a media file, stream, or capture device.
- IMFMediaSink – Handles rendering or writing output data.
- IMFTransform – Base interface for Media Foundation Transforms (MFTs).
- IMFAttributes – Stores key/value pairs used throughout the API.
Media Types
Media types describe the format of audio or video streams. Use MFCreateMediaType to build a type and set attributes such as MF_MT_MAJOR_TYPE and MF_MT_SUBTYPE.
Media Session
The media session orchestrates the flow of data between sources, transforms, and sinks. Use IMFMediaSession::SetTopology to attach a topology describing the pipeline.
Media Sources
Media sources provide audio/video data. Common implementations include:
- File source –
MFCreateSourceReaderFromURL - Capture device –
MFCreateDeviceSource - Network stream –
MFCreateSourceReaderFromMediaSource
Media Sinks
Media sinks consume processed data. Typical sinks are:
- Video renderer –
MFCreateVideoRendererActivate - Audio renderer –
MFCreateAudioRendererActivate - File sink –
MFCreateSinkWriterFromURL
Media Foundation Transforms (MFT)
MFTs are plug‑in components that modify media streams (e.g., decoders, encoders, effects). Register custom MFTs with MFRegisterLocalMFT and enumerate them via MFEnumMFTs.
Sample Grabber
The Sample Grabber transforms allow you to intercept raw samples for processing or analysis. Use MFCreateSampleGrabberSinkActivate to create a sink that delivers samples to your callback.
Attributes
Attributes are key/value pairs (GUID‑based) used throughout Media Foundation. Common attribute categories include:
- Presentation attributes –
MF_PD_DURATION,MF_PD_TOTAL_FILE_SIZE - Media type attributes –
MF_MT_FRAME_SIZE,MF_MT_AUDIO_BITS_PER_SAMPLE - Transform attributes –
MFT_ENUM_FLAG_SYNCMFT
Error Codes
Media Foundation error codes start with MF_E_. For a full list see the MF Error Reference.