Media Foundation (MF)

Media Foundation (MF) is Microsoft's modern platform for multimedia processing. It provides a set of APIs for creating and manipulating media files and streams, supporting a wide range of formats and hardware acceleration. MF is designed to be more flexible and performant than its predecessor, DirectShow.

Key Concepts

Understanding the core components of Media Foundation is crucial for effective development:

  • Topology: Represents the data flow graph within Media Foundation.
  • Sources: Components that read media data (e.g., file readers, network sources).
  • Sinks: Components that write media data (e.g., file writers, renderers).
  • Transforms: Components that process media data (e.g., decoders, encoders, effects).
  • Media Types: Define the format of media data (e.g., video resolution, audio sample rate).
  • Samples: The fundamental unit of media data.

Getting Started with Media Foundation

The following sections provide guidance on how to use Media Foundation APIs:

Core Interfaces

Media Foundation relies on a rich set of COM interfaces. Here are some of the most commonly used:

Commonly Used Interfaces

Interface Name Description
IMFMediaSource Represents a source of media data.
IMFMediaSink Represents a destination for media data.
IMFTransform Represents a component that processes media data.
IMFSample Represents a media sample (a chunk of data).
IMFMediaSession Manages the media pipeline for playback or processing.
IMFMediaType Defines the format of media data.

Code Examples

Illustrative code snippets to demonstrate common tasks:

Opening a Media File


#include <mfapi.h>
#include <mfidl.h>
#include <mfreadwrite.h> // For MFCreateReader
#include <iostream>

// ... initialization code ...

IMFMediaSource* pSource = NULL;
HRESULT hr = MFCreateSourceReaderFromURL(
    L"C:\\path\\to\\your\\media.mp4",
    NULL,
    &pSource
);

if (SUCCEEDED(hr)) {
    std::wcout << L"Successfully created media source reader." << std::endl;
    // Use pSource...
    pSource->Release();
} else {
    std::wcerr << L"Failed to create media source reader. HRESULT: " << std::hex << hr << std::endl;
}