@bun-win32/mfplat
v2.0.1
Published
Zero-dependency, zero-overhead Win32 MFPLAT bindings for Bun (FFI) on Windows.
Maintainers
Readme
@bun-win32/mfplat
Zero-dependency, zero-overhead Win32 Mfplat bindings for Bun on Windows.
Overview
@bun-win32/mfplat exposes the documented mfplat.dll exports using Bun's FFI. It provides a single class, Mfplat, which lazily binds native symbols on first use. You can optionally preload a subset or all symbols up-front via Preload().
mfplat.dll is the platform layer of Microsoft Media Foundation — it owns the MF runtime lifecycle (MFStartup / MFShutdown), the system clock (MFGetSystemTime), the MF work-queue thread pool (MFAllocateWorkQueue, MFPutWorkItem), and the foundational factory entry points for media types, attributes, samples, byte streams, events, async results, and the Media Foundation Transform (MFT) registry (MFTEnumEx, MFTRegister*). The bindings are strongly typed for a smooth DX in TypeScript.
Features
- Bun-first ergonomics on Windows 10/11.
- Direct FFI to
mfplat.dll(Media Foundation platform layer: lifecycle, clock, work queues, MFT registry, media-type / sample / byte-stream / event factories). - In-source docs in
structs/Mfplat.tswith links to Microsoft Learn. - Lazy binding on first call; optional eager preload (
Mfplat.Preload()). - No wrapper overhead; calls map 1:1 to native exports.
- Strongly-typed COM pointer aliases for
IMFAttributes,IMFMediaType,IMFSample,IMFMediaBuffer,IMFByteStream,IMFActivate,IMFAsyncResult,IMFMediaEvent,IMFDXGIDeviceManager, and factory parameters (seetypes/Mfplat.ts).
Requirements
- Bun runtime
- Windows 10 or later
Installation
bun add @bun-win32/mfplatQuick Start
import Mfplat from '@bun-win32/mfplat';
Mfplat.Preload(['MFStartup', 'MFShutdown', 'MFCreateAttributes']);
const MF_VERSION = 0x0002_0070;
const MFSTARTUP_LITE = 0x1;
const startHr = Mfplat.MFStartup(MF_VERSION, MFSTARTUP_LITE);
if (startHr === 0) {
const attrsOut = Buffer.alloc(8);
const createHr = Mfplat.MFCreateAttributes(attrsOut.ptr, 4);
if (createHr === 0) {
const attributes = attrsOut.readBigUInt64LE(0);
console.log(`IMFAttributes @ 0x${attributes.toString(16)}`);
}
Mfplat.MFShutdown();
}[!NOTE] AI agents: see
AI.mdfor the package binding contract and source-navigation guidance. It explains how to use the package without scanning the entire implementation.
Examples
Run the included examples:
bun run example:mft-transform-census
bun run example:mfplat-platform-probeNotes
- Either rely on lazy binding or call
Mfplat.Preload(). - Most
MFCreate*/MFT*/ work-queue entry points requireole32!CoInitializeEx(STA or MTA) andMfplat.MFStartup(MF_VERSION)up-front. Pair them withMFShutdown+CoUninitializeon exit. - Constants:
MF_VERSION = 0x0002_0070,MFSTARTUP_LITE = 0x1(orMFSTARTUP_FULL = 0x0to include socket/network activation). Seetypes/Mfplat.ts. - The returned
IMFAttributes/IMFMediaType/IMFSample/IMFActivate/IMFByteStreamobjects are COM interfaces. Drive them through their vtable (seeexample/mft-transform-census.tsfor aGetAllocatedStringwalkthrough andexample/mfplat-platform-probe.tsforIMFAttributes::Set/GetUINT32andIMFSample::AddBuffer). MFTEnumExreturns aCoTaskMemAlloc'd array ofIMFActivate*. Release each activate via its vtable, then free the outer array withole32!CoTaskMemFree.- 25 exports from
mfplat.dllare forwarders intoRTWorkQ.dll(MFAllocateWorkQueue, MFScheduleWorkItem, etc.). Bun's loader resolves them transparently — no special handling required. - Windows only. Bun runtime required.
- SAL types & naming: nullability is in the type —
Optional<T>(formally optional, SAL_*opt_) andNullable<T>(plain[in]/[out]the docs say can be NULL), the null sentinel derived fromT(nullfor pointersLP*/P*,0nfor handles/by-value addresses); direction is in the parameter name —_out(_Out_),_in_out(_Inout_),_In_bare. SeeAI.mdand the repoAGENTS.md.
