Free, open-source screen sharing for private live streams with friends. Watch together in a browser or self-host Piik. 免费开源的私密屏幕共享,支持游戏直播、一起看电影与自部署。
test(native): probe Media Foundation H264 hardware encode (#70)
AAA2c38d14f5490
diff --git a/docs/README.md b/docs/README.mdindex b4beaa5b..713229ba 100644--- a/docs/README.md+++ b/docs/README.md@@ -12,7 +12,7 @@- [Built-in peer ICE TURN candidate](./research/built-in-peer-ice-turn.md): implemented default-off direct-first TURN fallback, short-lived participant-session bearer, refresh ownership, idle-allocation cost, and deployment gates.- [Advanced peer distribution](./research/advanced-peer-distribution.md): multi-tree/SVC, encoded-object relay, native RTP forwarding, FEC/network-coding, MoQ, and measurable go/no-go gates.- [Native shared-encode sender](./research/native-shared-encode-sender.md): consolidated Draft #16/#18/#22/#23/#25/#28 ladder, unclassified product-gate failure, staged revalidation, and stop line.-- [Native H.264 hardware decision](./research/native-h264-hardware-decision.md): bounded WebCodecs H.264 hardware no-go, constraint/fmtp compatibility boundary, Pion Annex-B assessment, and the hardware-only Media Foundation next slice.+- [Native H.264 hardware decision](./research/native-h264-hardware-decision.md): bounded WebCodecs no-go, proved Media Foundation/NVIDIA hardware encode, the default-fmtp `42c01f` stop, and the retained interop boundary.- [Realtime quality adaptation](./research/realtime-quality-adaptation.md): correlated capture/send/receive diagnosis, production generation-rebuild gate, path-isolated dual representations, and bounded simulcast/Dynacast/SVC checks.- [Browser screen-audio quality](./research/browser-screen-audio-quality.md): capture/source compatibility, standard readback limits, Opus negotiation boundaries, and the retained no-control decision.- [Display name and presence boundaries](./research/display-name-presence.md): browser-local storage failure behavior, Unicode normalization, control-character policy, and the Web-only presence capability boundary.diff --git a/docs/project-memory.md b/docs/project-memory.mdindex 3f1cfac8..514dcdeb 100644--- a/docs/project-memory.md+++ b/docs/project-memory.md@@ -23,7 +23,7 @@ Last updated: 2026-08-20- Use direct host P2P for one or two viewers. Keep ADR-0004 off broadly; use an isolated exact-room candidate until resource, quality, recovery, SFU/UDP, bounded-failure, and browser/mobile gates pass.- Browser relays resend remote `MediaStreamTrack` values and re-encode at each hop; WebRTC does not guarantee a shared encoder across peer connections, so measure the cost.- Keep experiments bounded: the standard representation sequence is below; native RTP relay, encoded-object striping, and FEC remain separate.-- ADR-0006 stays no-go: the second run reached first WebCodecs output, then zero Go frame/RTP and no Viewer; cleanup passed 5/5. Its unrun gate rechecks one Viewer/Pion/bridge generation and cleanup-before-pass. No product bug or further run is authorized; Native v2 stays memory-only and leaves SQLite/Web unchanged.+- ADR-0006 stays no-go: the second product run reached first WebCodecs output, then zero Go frame/RTP and no Viewer; cleanup passed 5/5. A separate offline Media Foundation fixture proved one RTX 4070 SUPER H.264 hardware path over 360/360 frames, but its valid Constrained Baseline SPS `42c01f` is outside Pion's default exact fmtp set; Pion/Viewer interop remains unproven. Native v2 stays memory-only and leaves SQLite/Web unchanged.- Mobile Web Host is unsupported; feature-detect and fail clearly. Mobile Viewer stays leaf-only. After Windows native, gate Android 14+; iOS waits for stable iOS 27 ScreenCaptureKit.- ADR-0005 accepts direct-first Peer ICE and bounded SFU roots. Its default-off exact-room slice is undeployed; production/coturn stay STUN-only. PR #12/selected-edge superseded.- Keep the controller exact-room only: room `1` is the STUN/SFU smoke. Preserve sticky progressing P2P, mobile leaves and break-before-make. Recovery spends one attempt per layer (ICE restart, same-parent rebuild, alternate peer, then SFU), never three identical retries; active SFU gets one fresh grant before Peer failback.diff --git a/docs/research/native-h264-hardware-decision.md b/docs/research/native-h264-hardware-decision.mdindex b7e96e83..30a7661b 100644--- a/docs/research/native-h264-hardware-decision.md+++ b/docs/research/native-h264-hardware-decision.md@@ -2,20 +2,19 @@Date: 2026-08-20-Status: `no-go-webcodecs-h264-hardware-unclassified`+Status: `no-go-native-h264-hardware-pinned-fmtp`This spike does not amend ADR-0006 or authorize another product, room, or-production run. It answers one narrow question: can the existing local-browser-Native candidate switch from VP8 to WebCodecs H.264 and satisfy the project's-physical hardware-encode contract with a small codec change?--The answer is no. The H.264 bitstream and Pion packetization path are-mechanically plausible, but the one bounded run did not observe a hardware-encoder and emitted SPS constraint bytes that differ from the requested-WebCodecs codec string. Their negotiated-fmtp compatibility remains-unclassified. A WebCodecs H.264 product switch is frozen. The next candidate is-a separate Windows hardware-only Media Foundation fixture, not a codec ladder-or fallback framework.+production run. It answers one narrow question: can a bounded Windows H.264+path satisfy the project's physical hardware-encode and existing default Pion+fmtp contracts before product integration?++The WebCodecs answer remains no: its bounded run did not prove hardware and its+emitted SPS differed from the requested codec string. A subsequent+hardware-only Media Foundation run did prove the selected NVIDIA encoder and+GPU engine, but emitted exact `profile-level-id=42c01f`, which is absent from+the default Pion mode-1 fmtp set. It therefore stopped before Pion, a Viewer,+rooms, or product wiring. Neither result authorizes an H.264 product switch.## Decision Summary@@ -27,7 +26,9 @@ or fallback framework.| Did forced key frames carry in-band recovery data? | Yes in this fixture: all six key chunks contained AUD, SPS, PPS, and IDR NAL units. || Can Pion v1.10.5 packetize the resulting Annex-B access units? | Yes statically: its `H264Payloader` splits 3/4-byte start codes, suppresses AUD/filler, emits SPS/PPS as STAP-A, and fragments slices as FU-A. || Is unmodified browser Viewer decode proven? | No. No PeerConnection, room, Viewer, or production service participated. |-| Should the small WebCodecs/Pion codec diff be implemented? | No. It would preserve the two unresolved product gates: physical encoder identity and exact negotiated H.264 fmtp/constraint compatibility. |+| Should the small WebCodecs/Pion codec diff be implemented? | No. It would preserve the two unresolved product gates: physical encoder identity and exact emitted-fmtp registration plus Viewer interoperability. |+| Did the Media Foundation fixture prove physical H.264 hardware encode? | Yes for one local RTX 4070 SUPER run: adapter-LUID-bound hardware enumeration, D3D11 awareness, the NVIDIA H.264 Encoder MFT, and process-plus-LUID `VideoEncode` activity agreed. |+| Did that hardware MFT emit an exact default Pion mode-1 fmtp string? | No. It emitted `42c01f`; the default set is `42001f`, `42e01f`, `4d001f`, and `64001f`. The fixture failed closed without changing the request or adding a fallback. This is not a standards-level profile incompatibility. |## Standards And Platform Constraints@@ -40,6 +41,10 @@ or fallback framework.- WebRTC browsers must implement VP8 and H.264 Constrained Baseline. H.264endpoints must support RFC 6184 and `packetization-mode=1`, include`profile-level-id`, and send parameter sets in-band.+- RFC 6184 Table 5 identifies Constrained Baseline as `profile_idc=0x42` with+ profile-iop matching `x1xx0000`. Both `42c01f` and `42e01f` match that same+ Constrained Baseline sub-profile at level 3.1 even though their exact fmtp+ strings differ.- NVIDIA lists the local GeForce RTX 4070 SUPER as an Ada, eighth-generationNVENC device with H.264 4:2:0 support. That proves the machine has suitablehardware; it does not prove Chrome selected it.@@ -49,7 +54,7 @@ or fallback framework.selected device. Hardware enumeration and D3D awareness are read-back gates,not hints.-## Bounded Fixture+## WebCodecs Bounded Fixture### Isolation@@ -164,6 +169,84 @@ latency or CPU comparison is claimed. The current ADR-0006 candidate deletesshared-encode research already classifies that behavior as an implementationpreference, not physical hardware evidence.+## Media Foundation Hardware Fixture++The follow-up fixture is retained at+`native/fixtures/mf-h264-hardware/`. It is a Windows-only offline C++ program,+not a sender component. Its generated object and executable stay outside the+repository. It has one H.264 path and no software MFT, codec, adapter, room,+capture, audio, signaling, Pion, or Viewer fallback.++### Fixed Contract++- Toolchain: MSVC 19.42 and Windows SDK 10.0.22621.0.+- Selected adapter: DXGI index 0, NVIDIA GeForce RTX 4070 SUPER, LUID+ `0x00000000:0x0001a496`, driver 610.88.+- Selected transform: the sole adapter-bound candidate, NVIDIA H.264 Encoder+ MFT, CLSID `{60F44560-5A20-4857-BFEF-D29773CB8040}`.+- Enumeration: `MFTEnum2` with the selected LUID, NV12 input, H.264 output, and+ only `MFT_ENUM_FLAG_HARDWARE | MFT_ENUM_FLAG_SORTANDFILTER`.+- Device path: explicit D3D11 hardware device with video support, a reset DXGI+ device manager, `MF_TRANSFORM_ASYNC=true`,+ `MF_SA_D3D11_AWARE=true`, and accepted `MFT_MESSAGE_SET_D3D_MANAGER`.+- Media: NV12 GPU-surface inputs, 1280x720 at 30 fps, 3 Mbps H.264 Baseline+ level 3.1, 360 paced inputs, and a maximum eight in flight.+- Required `ICodecAPI` readback: CBR, 3 Mbps mean bitrate, 12,500-byte+ one-frame VBV, low-latency mode, and a 60-frame GOP. Every value required+ `IsSupported=S_OK`, `SetValue=S_OK`, and exact `GetValue` readback.+- Recovery requests: `CODECAPI_AVEncVideoForceKeyFrame` immediately before+ inputs 0, 60, 120, 180, 240, and 300.++The NVIDIA MFT returned `E_NOTIMPL` for at least the CBR `IsModifiable` query.+That advisory result was not treated as proof either way; an actual+`SetValue=S_OK` followed by exact `GetValue` was required. `S_FALSE`, a failed+set, missing readback, or a changed value still fails. Three redundant controls+were deliberately excluded:++- `AVEncCommonRealTime` was unsupported, while `AVLowLatencyMode` already+ requires no reordering delay and one output per input;+- `AVEncCommonMaxBitRate` applies to peak-constrained VBR, not the selected+ CBR mode; and+- the optional B-picture-count property was unsupported, while Baseline+ excludes B slices and the run independently required ordered timestamps.++This keeps the contract on observable behavior rather than on optional aliases.++### Retained Run++One final instrumented run used the exact adapter and transform above. No+encoded sample was written to disk and no network or Screener process was+started.++| Measure | Retained value |+| --- | --- |+| Inputs / outputs | `360 / 360` |+| Recovery access units | `6 / 6`, each with Annex-B SPS, PPS, and IDR |+| Maximum in flight | `1` |+| Encoded bytes | `2,956,354` |+| Wall time | `12.106 s` |+| Input-to-output latency p95 | `11.575 ms` |+| Fixture process CPU | `2.094 CPU-s` over the run; includes synthetic NV12 generation and event handling |+| Process-and-adapter `VideoEncode` | 45 samples; mean `2.949%`, max `4.863%` |+| Output SPS `profile-level-id` | `42c01f` |+| Final result | `no-go-native-h264-hardware-pinned-fmtp` |++The physical evidence is correlated rather than inferred from low CPU: the+hardware-only activation was bound to the selected DXGI LUID, accepted that+device's D3D manager, and Windows observed `engtype_videoencode` activity for+both the fixture PID and that same LUID. The existing unrelated NVIDIA encoder+session cannot satisfy this PID-and-LUID filter.++The run failed its local gate because `42c01f` is not exactly one of Pion+v4.2.18's default mode-1 values (`42001f`, `42e01f`, `4d001f`, or `64001f`).+RFC 6184 Table 5 nevertheless classifies both `42c01f` and `42e01f` as the same+Constrained Baseline sub-profile. The result is therefore a default-capability+miss with unmodified Viewer interoperability unproven, not an incompatible+H.264 profile. The fixture did not change its request, register a new fmtp, try+the AMD adapter, activate another MFT, or add an NVENC/software fallback. Pion+and Viewer work did not begin, so the result proves a usable hardware-encode+primitive but not a transportable Screener stream.+## Static Product-Path EvaluationThe small bitstream path is plausible:@@ -195,15 +278,16 @@ shape credible: one merged PLI/FIR can generate one access unit for everybound Viewer. This is static evidence plus an encoder-only fixture, not aViewer proof.-The unresolved constraint-byte/fmtp compatibility boundary blocks the-mechanical switch. Pion v4.2.18's-default H.264 mode-1 capabilities include `42001f`, `42e01f`, `4d001f`, and-`64001f`; they do not include the observed `42041f`. Binding a track by MIME-type alone can fall back to a partial codec match and advertise a different-format. The gate must instead:+Two exact-string boundaries remain distinct. The earlier WebCodecs run emitted+`42041f`; the Media Foundation run emitted `42c01f`. Pion v4.2.18's default+H.264 mode-1 capabilities include `42001f`, `42e01f`, `4d001f`, and `64001f`,+so neither observed string is present by default. Unlike the earlier WebCodecs+value, the Media Foundation `42c01f` value matches RFC 6184's Constrained+Baseline pattern. Binding a track by MIME type alone can fall back to a partial+codec match and advertise a different format. The next gate must instead:1. parse emitted SPS before publishing;-2. register and offer only an exactly compatible mode-1 capability;+2. register and offer the exact emitted, standards-valid mode-1 capability;3. retain the selected answer `profile-level-id` without raw SDP;4. prove Chrome, Edge, Firefox, and Safari Viewer decode/render;5. prove every join/PLI/FIR recovery unit carries usable in-band SPS/PPS and@@ -220,79 +304,47 @@ exception despite being textually small.| Route | Advantage | Blocking cost / risk | Decision || --- | --- | --- | --- || WebCodecs H.264 Annex B + Pion | Smallest diff; observed SPS/PPS/IDR cadence; existing shared fanout remains intact | Hardware request is a hint; no encoder identity/readback; emitted constraint bytes differ and fmtp compatibility is unclassified; no Viewer proof | Freeze |-| Media Foundation hardware H.264 MFT | Windows hardware-only enumeration, exact transform activation, D3D11 device manager, ICodecAPI support/readback, vendor-neutral across installed hardware MFTs | COM/asynchronous MFT lifecycle and GPU-surface ownership are materially larger than 300 core lines | Next bounded fixture |+| Media Foundation hardware H.264 MFT | Proved one physical NVIDIA path with exact transform, D3D11 manager, codec readback, 360 outputs, and six recovery units | Emitted standards-valid Constrained Baseline `42c01f`, outside Pion's default exact fmtp set; no Pion or Viewer proof | Retain fixture; product no-go || Direct NVENC | Explicit NVIDIA encoder session and detailed low-latency controls | NVIDIA-only, SDK/API lifecycle and redistribution review, separate AMD/Intel future decisions | Hold unless the Media Foundation hardware contract fails |-Media Foundation is the Occam candidate because it can fail closed on an-official hardware category while avoiding a vendor dispatch layer. It is one-Windows H.264 path, not an abstraction for VP8/VP9/AV1/HEVC and not a software-fallback.--## Next Native Slice--Build an offline Windows executable fixture before touching capture, rooms, or-the existing sender:--1. Create one D3D11 device on the explicitly selected adapter with- `D3D11_CREATE_DEVICE_VIDEO_SUPPORT`; create and reset one DXGI device- manager.-2. Enumerate only `MFT_CATEGORY_VIDEO_ENCODER` transforms matching H.264 output- with `MFT_ENUM_FLAG_HARDWARE | MFT_ENUM_FLAG_SORTANDFILTER`. Record bounded- activation identity and fail when the count is zero.-3. Activate exactly one transform. Require `MF_SA_D3D11_AWARE=true`, send- `MFT_MESSAGE_SET_D3D_MANAGER`, and never retry a synchronous/software MFT.-4. Use fixed NV12 1280x720@30 input and H.264 3 Mbps output. Require and read- back Constrained Baseline or another profile that exactly matches a Pion- mode-1 capability, level 3.1 or lower, realtime/low-latency mode, CBR mean- bitrate, bounded VBV, no frame reordering, and a 60-frame maximum GOP.- Unsupported, read-only, or weakened properties fail the fixture.-5. Feed 360 synthetic GPU-resident frames. Retain input-to-output latency,- output cadence, queue depth, CPU, and adapter-specific Windows- `VideoEncode`. Add a vendor session counter only where the selected adapter- exposes one. Capability enumeration alone does not pass.-6. Force an IDR at frames 0/60/120/180/240/300 and through the same- key-frame-request entry point. Parse every recovery access unit for Annex-B- SPS/PPS/IDR and exact profile/level bytes.-7. Pass the same encoded access unit through Pion `H264Payloader` to two local- transport bindings, then prove an unmodified browser Viewer answer and- decode/render. Do not enter a Screener room.--NVIDIA's low-latency guidance supports CBR, a very small (approximately one-frame) VBV, low/ultra-low-latency tuning, and explicit IDR recovery. The first-Media Foundation fixture should use only controls that its selected hardware-MFT reports as supported and modifiable and then returns unchanged. Do not add-an NVENC fallback when a Media Foundation control is absent.--### Rough Size--| Responsibility | Estimated core LOC |-| --- | ---: |-| D3D11 adapter/device, DXGI manager, synthetic NV12 surfaces | 180-260 |-| Hardware-only MFT enumeration, activation, identity/readback | 100-160 |-| Media types, ICodecAPI configuration, asynchronous input/output pump | 260-380 |-| Annex-B/profile/key-frame validation and bounded measurements | 120-180 |-| **Offline fixture total** | **660-980** |--Focused tests and harness glue are likely another 200-350 lines. Windows-Graphics Capture, BGRA-to-NV12 conversion, audio, packaging, and product bridge-integration are explicitly outside that estimate. This is not a 200-300-line-product change, so no implementation was started in this branch.+Media Foundation remains the smallest proved physical-encode primitive because+it fails closed on an official hardware category without a vendor dispatch+layer. Its default-fmtp miss leaves product interoperability unproven. It is one+Windows H.264 fixture, not an abstraction for VP8/VP9/AV1/HEVC and not a+software fallback.++## Retained Stop Line++The hardware-only fixture is complete for this decision. Do not alter its+requested media type, local default-fmtp gate, or sender wiring merely to make+this fixture return zero. A separately bounded follow-up may register and offer+exact `42c01f`, then test Pion/two-binding and unmodified Viewer decode without+turning that experiment into product integration.++Direct NVENC is still a separately authorized candidate if tighter bitstream+control remains necessary after the bounded `42c01f` interop test. It is+NVIDIA-only and brings a Video Codec SDK license and distribution review, so it+is not an automatic fallback from this Media Foundation result. The existing+WebCodecs-to-Go bridge product gap also remains independent and should not be+hidden inside codec work.## Go / No-Go GatesThe Media Foundation slice is `go-native-h264-hardware-fixture` only when all-of these are retained in one run:--- exact hardware MFT identity and D3D11-aware readback;-- adapter-attributed `VideoEncode` activity and no software fallback;-- 360/360 bounded outputs with p95 input-to-output latency recorded;-- exact SPS profile/constraint/level matching the offered mode-1 fmtp;-- SPS/PPS/IDR after startup, late-join request, PLI, and FIR;-- the same encoded access unit packetized once and delivered over two transport- bindings; and-- unmodified Viewer decode/render in the target browser matrix.--Any missing physical, profile, feedback, or Viewer evidence remains no-go. Do+of these are retained in one run. The current result is shown inline:++- pass: exact hardware MFT identity and D3D11-aware readback;+- pass: process-and-adapter-attributed `VideoEncode` activity and no software+ fallback;+- pass: 360/360 bounded outputs with p95 input-to-output latency recorded;+- fail: exact SPS string membership in the default Pion mode-1 fmtp set; this+ does not mean the emitted Constrained Baseline profile is incompatible;+- pass for encoder requests only: six SPS/PPS/IDR recovery access units;+- not run: the same encoded access unit packetized once and delivered over two+ transport bindings; and+- not run: unmodified Viewer decode/render in the target browser matrix.++Any missing physical, fmtp, feedback, or Viewer evidence remains no-go. Donot infer success from codec support, adapter model, transform enumeration,low CPU, or a single browser decode.@@ -323,12 +375,19 @@ Accessed 2026-08-20:- [NVIDIA System Management Interface](https://docs.nvidia.com/deploy/nvidia-smi/index.html)- [Microsoft `typeperf`](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/typeperf)- [Microsoft `MFTEnumEx`](https://learn.microsoft.com/en-us/windows/win32/api/mfapi/nf-mfapi-mftenumex)+- [Microsoft `MFTEnum2`](https://learn.microsoft.com/en-us/windows/win32/api/mfapi/nf-mfapi-mftenum2)- [Microsoft hardware MFTs](https://learn.microsoft.com/en-us/windows/win32/medfound/hardware-mfts)+- [Microsoft asynchronous MFTs](https://learn.microsoft.com/en-us/windows/win32/medfound/asynchronous-mfts)+- [Microsoft async MFT unlock](https://learn.microsoft.com/en-us/windows/win32/medfound/mf-transform-async-unlock)- [Microsoft `MF_SA_D3D11_AWARE`](https://learn.microsoft.com/en-us/windows/win32/medfound/mf-sa-d3d11-aware)- [Microsoft `MFCreateDXGIDeviceManager`](https://learn.microsoft.com/en-us/windows/win32/api/mfapi/nf-mfapi-mfcreatedxgidevicemanager)+- [Microsoft `MFCreateDXGISurfaceBuffer`](https://learn.microsoft.com/en-us/windows/win32/api/mfapi/nf-mfapi-mfcreatedxgisurfacebuffer)- [Microsoft H.264 video encoder](https://learn.microsoft.com/en-us/windows/win32/medfound/h-264-video-encoder)- [Microsoft low-latency codec property](https://learn.microsoft.com/en-us/windows/win32/medfound/codecapi-avlowlatencymode)- [Microsoft force-key-frame property](https://learn.microsoft.com/en-us/windows/win32/medfound/codecapi-avencvideoforcekeyframe)+- [Microsoft `ICodecAPI::IsModifiable`](https://learn.microsoft.com/en-us/windows/win32/api/strmif/nf-strmif-icodecapi-ismodifiable)+- [Microsoft `ICodecAPI::SetValue`](https://learn.microsoft.com/en-us/windows/win32/api/strmif/nf-strmif-icodecapi-setvalue)+- [Microsoft D3D multithread protection](https://learn.microsoft.com/en-us/windows/win32/api/d3d10/nf-d3d10-id3d10multithread-setmultithreadprotected)- [Microsoft H.264 profile enumeration](https://learn.microsoft.com/en-us/windows/win32/api/codecapi/ne-codecapi-eavench264vprofile)- [Pion RTP v1.10.5 H.264 payloader](https://github.com/pion/rtp/blob/v1.10.5/codecs/h264_packet.go)- [Pion WebRTC v4.2.18 media engine](https://github.com/pion/webrtc/blob/v4.2.18/mediaengine.go)diff --git a/docs/status.md b/docs/status.mdindex af730f25..ba8d1d9f 100644--- a/docs/status.md+++ b/docs/status.md@@ -37,7 +37,7 @@ also contains a no-go Native v2 candidate, but production does not run it.- Host A+B and authenticated P2P Viewer C are sanitized, generation-bound, read-only, and fail closed for stale, ambiguous, or SFU-fed evidence; no raw media metadata is retained.- SVC is `no-go-web-svc-cross-path-hardware-contract`: no direct/peer selection, cross-PC shared encode, or portable hardware proof; pinned screen share is `L1T3`. No browser run was warranted.- Source TURN tests pass; production/coturn stay STUN-only and untested.-- Native remains experimental. The second 2026-08-20 run reached first WebCodecs output, then zero Go frame/RTP and no Viewer; cleanup passed 5/5. No product bug or retry followed.+- Native stays no-go at WebCodecs -> Go RTP. MF proved RTX H.264 hardware (360/360, p95 11.575 ms); `42c01f` misses Pion's default fmtp, so Viewer interop is unproven.- Access focused tests pass protocol/config/HTTP/storage/RoomStore/SQLite/signaling, including commit-first teardown, persistence-failure continuity, relay/SFU retirement, grant bounds, and v1 migration rollback.## Unverified Boundariesdiff --git a/native/fixtures/mf-h264-hardware/README.md b/native/fixtures/mf-h264-hardware/README.mdnew file mode 100644index 00000000..d82505dd--- /dev/null+++ b/native/fixtures/mf-h264-hardware/README.md@@ -0,0 +1,55 @@+# Media Foundation H.264 Hardware Fixture++This is an offline Windows-only decision fixture. It is not linked into the+Screener sender and does not contact a room, signaling service, Viewer, or+production endpoint.++The fixture selects one DXGI adapter and one adapter-bound Media Foundation+transform. It never enumerates a software MFT and has no codec or software+fallback. Exit code zero requires all of these checks in one 360-frame run:++- adapter-LUID-bound hardware H.264 enumeration;+- asynchronous and D3D11-aware transform readback;+- exact low-latency, CBR, 3 Mbps, one-frame VBV, and 60-frame GOP property+ readback, plus Baseline media-type request/readback and output-order+ no-reordering checks;+- 360 GPU-surface NV12 inputs and 360 ordered outputs;+- Annex-B SPS/PPS/IDR at frames 0, 60, 120, 180, 240, and 300;+- an SPS `profile-level-id` exactly present in the fixture's default Pion+ mode-1 fmtp set; and+- process-and-adapter-attributed Windows `VideoEncode` activity.++The generated executable must stay outside the repository. From PowerShell:++```powershell+$out = Join-Path ([IO.Path]::GetTempPath()) 'screener-mf-h264-fixture-build'+./native/fixtures/mf-h264-hardware/build.ps1 -OutputDirectory $out+& "$out/screener-mf-h264-fixture.exe" --list-adapters+& "$out/screener-mf-h264-fixture.exe" --adapter-index 0 --mft-index 0+```++Select the adapter index explicitly from the first command. The MFT index+defaults to zero but is printed and accepted explicitly so a retained run+cannot silently switch transforms.++The fixture prints bounded key/value evidence to stdout and never writes the+encoded bitstream. A nonzero exit is a measured no-go at the reported stage;+it must not be worked around by adding a software retry.++The Pion/two-binding and browser decode boundary remains separate. The current+machine has no Go toolchain in `PATH`, and the existing product bridge is still+no-go before first Go RTP. Coupling either boundary into this encoder fixture+would hide which layer failed.++Primary API references, accessed 2026-08-20:++- <https://learn.microsoft.com/en-us/windows/win32/api/mfapi/nf-mfapi-mftenum2>+- <https://learn.microsoft.com/en-us/windows/win32/medfound/asynchronous-mfts>+- <https://learn.microsoft.com/en-us/windows/win32/medfound/mf-transform-async-unlock>+- <https://learn.microsoft.com/en-us/windows/win32/medfound/mf-sa-d3d11-aware>+- <https://learn.microsoft.com/en-us/windows/win32/api/mfapi/nf-mfapi-mfcreatedxgidevicemanager>+- <https://learn.microsoft.com/en-us/windows/win32/api/mfapi/nf-mfapi-mfcreatedxgisurfacebuffer>+- <https://learn.microsoft.com/en-us/windows/win32/medfound/h-264-video-encoder>+- <https://learn.microsoft.com/en-us/windows/win32/medfound/codecapi-avlowlatencymode>+- <https://learn.microsoft.com/en-us/windows/win32/medfound/codecapi-avencvideoforcekeyframe>+- <https://docs.nvidia.com/video-technologies/video-codec-sdk/13.0/nvenc-video-encoder-api-prog-guide/index.html>diff --git a/native/fixtures/mf-h264-hardware/build.ps1 b/native/fixtures/mf-h264-hardware/build.ps1new file mode 100644index 00000000..46883b3b--- /dev/null+++ b/native/fixtures/mf-h264-hardware/build.ps1@@ -0,0 +1,47 @@+[CmdletBinding()]+param(+ [Parameter(Mandatory = $true)]+ [string]$OutputDirectory+)++Set-StrictMode -Version Latest+$ErrorActionPreference = 'Stop'++$fixtureDirectory = Split-Path -Parent $MyInvocation.MyCommand.Path+$repositoryRoot = [IO.Path]::GetFullPath((Join-Path $fixtureDirectory '..\..\..'))+$outputPath = [IO.Path]::GetFullPath($OutputDirectory)+$repositoryPrefix = $repositoryRoot.TrimEnd([IO.Path]::DirectorySeparatorChar) + [IO.Path]::DirectorySeparatorChar++if ($outputPath.Equals($repositoryRoot, [StringComparison]::OrdinalIgnoreCase) -or+ $outputPath.StartsWith($repositoryPrefix, [StringComparison]::OrdinalIgnoreCase)) {+ throw 'OutputDirectory must be outside the repository.'+}++$vswhere = Join-Path ${env:ProgramFiles(x86)} 'Microsoft Visual Studio\Installer\vswhere.exe'+if (-not (Test-Path -LiteralPath $vswhere -PathType Leaf)) {+ throw 'Visual Studio Installer vswhere.exe was not found.'+}++$installationPath = & $vswhere -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath+if ([string]::IsNullOrWhiteSpace($installationPath)) {+ throw 'Visual Studio C++ Build Tools were not found.'+}++$developerCommand = Join-Path $installationPath 'Common7\Tools\VsDevCmd.bat'+$sourcePath = Join-Path $fixtureDirectory 'main.cpp'+$executablePath = Join-Path $outputPath 'screener-mf-h264-fixture.exe'+$objectPath = Join-Path $outputPath 'main.obj'++New-Item -ItemType Directory -Path $outputPath -Force | Out-Null++$compile = @(+ 'call "{0}" -arch=x64 -host_arch=x64 >nul' -f $developerCommand+ 'cl.exe /nologo /std:c++20 /EHsc /W4 /WX /DUNICODE /D_UNICODE /DWIN32_LEAN_AND_MEAN /D_WIN32_WINNT=0x0A00 "{0}" /Fo:"{1}" /Fe:"{2}" /link mfplat.lib mf.lib mfuuid.lib d3d11.lib dxgi.lib dxguid.lib evr.lib ole32.lib oleaut32.lib pdh.lib' -f $sourcePath, $objectPath, $executablePath+) -join ' && '++& cmd.exe /d /s /c $compile+if ($LASTEXITCODE -ne 0) {+ throw "Native fixture compilation failed with exit code $LASTEXITCODE."+}++Write-Output $executablePathdiff --git a/native/fixtures/mf-h264-hardware/main.cpp b/native/fixtures/mf-h264-hardware/main.cppnew file mode 100644index 00000000..53e5821b--- /dev/null+++ b/native/fixtures/mf-h264-hardware/main.cpp@@ -0,0 +1,1290 @@+#define NOMINMAX+#include <windows.h>++#include <codecapi.h>+#include <d3d10_1.h>+#include <d3d11.h>+#include <dxgi1_2.h>+#include <evr.h>+#include <mfapi.h>+#include <mferror.h>+#include <mfidl.h>+#include <mftransform.h>+#include <pdh.h>+#include <pdhmsg.h>+#include <propvarutil.h>+#include <wrl/client.h>++#include <algorithm>+#include <atomic>+#include <chrono>+#include <cmath>+#include <cstdint>+#include <cwctype>+#include <exception>+#include <iomanip>+#include <iostream>+#include <limits>+#include <mutex>+#include <numeric>+#include <optional>+#include <sstream>+#include <stdexcept>+#include <string>+#include <thread>+#include <utility>+#include <vector>++using Microsoft::WRL::ComPtr;++namespace {++constexpr UINT32 kWidth = 1280;+constexpr UINT32 kHeight = 720;+constexpr UINT32 kFrameRate = 30;+constexpr UINT32 kBitRate = 3'000'000;+constexpr UINT32 kVbvBytes = kBitRate / kFrameRate / 8;+constexpr UINT32 kFrameCount = 360;+constexpr UINT32 kGopFrames = 60;+constexpr UINT32 kMaxInFlight = 8;+constexpr UINT32 kMaxEventsPerPump = 64;+constexpr DWORD kMaxEncodedSampleBytes = 4 * 1024 * 1024;+constexpr DWORD kMaxPdhArrayBytes = 4 * 1024 * 1024;+constexpr DWORD kMaxPdhArrayItems = 16 * 1024;+constexpr LONGLONG kFrameDuration100ns = 10'000'000 / kFrameRate;++class GateFailure final : public std::runtime_error {+ public:+ GateFailure(std::string stage, std::string detail, HRESULT result = S_OK)+ : std::runtime_error(detail), stage_(std::move(stage)), result_(result) {}++ const std::string& stage() const noexcept { return stage_; }+ HRESULT result() const noexcept { return result_; }++ private:+ std::string stage_;+ HRESULT result_;+};++[[noreturn]] void Fail(const std::string& stage, const std::string& detail) {+ throw GateFailure(stage, detail);+}++void Check(HRESULT result, const std::string& stage) {+ if (FAILED(result)) {+ throw GateFailure(stage, "Windows API returned a failing HRESULT", result);+ }+}++std::wstring Lower(std::wstring value) {+ std::transform(value.begin(), value.end(), value.begin(),+ [](wchar_t character) { return std::towlower(character); });+ return value;+}++std::string NarrowAscii(const std::wstring& value) {+ std::string result;+ result.reserve(value.size());+ for (wchar_t character : value) {+ result.push_back(character >= 32 && character <= 126+ ? static_cast<char>(character)+ : '?');+ }+ return result;+}++std::string GuidString(const GUID& value) {+ wchar_t buffer[40] = {};+ if (StringFromGUID2(value, buffer, static_cast<int>(std::size(buffer))) == 0) {+ return "unavailable";+ }+ return NarrowAscii(buffer);+}++std::string LuidString(const LUID& luid) {+ std::ostringstream output;+ output << "0x" << std::hex << std::setfill('0') << std::setw(8)+ << static_cast<UINT32>(luid.HighPart) << ":0x" << std::setw(8)+ << luid.LowPart;+ return output.str();+}++std::string HexByte(UINT8 value) {+ std::ostringstream output;+ output << std::hex << std::setfill('0') << std::setw(2)+ << static_cast<UINT32>(value);+ return output.str();+}++std::string ProfileLevelId(UINT8 profile, UINT8 constraints, UINT8 level) {+ return HexByte(profile) + HexByte(constraints) + HexByte(level);+}++struct Runtime final {+ Runtime() {+ Check(CoInitializeEx(nullptr, COINIT_MULTITHREADED), "com-initialize");+ com_started = true;+ Check(MFStartup(MF_VERSION, MFSTARTUP_FULL), "mf-startup");+ mf_started = true;+ }++ ~Runtime() {+ if (mf_started) {+ MFShutdown();+ }+ if (com_started) {+ CoUninitialize();+ }+ }++ bool com_started = false;+ bool mf_started = false;+};++struct Adapter final {+ UINT index = 0;+ DXGI_ADAPTER_DESC1 description = {};+ ComPtr<IDXGIAdapter1> value;+};++std::vector<Adapter> EnumerateAdapters() {+ ComPtr<IDXGIFactory1> factory;+ Check(CreateDXGIFactory1(IID_PPV_ARGS(&factory)), "dxgi-factory");++ std::vector<Adapter> adapters;+ for (UINT index = 0;; ++index) {+ ComPtr<IDXGIAdapter1> adapter;+ HRESULT result = factory->EnumAdapters1(index, &adapter);+ if (result == DXGI_ERROR_NOT_FOUND) {+ break;+ }+ Check(result, "dxgi-enumerate-adapter");++ Adapter current;+ current.index = index;+ current.value = adapter;+ Check(adapter->GetDesc1(¤t.description), "dxgi-adapter-description");+ if ((current.description.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) == 0) {+ adapters.push_back(std::move(current));+ }+ }+ return adapters;+}++void PrintAdapters(const std::vector<Adapter>& adapters) {+ for (const Adapter& adapter : adapters) {+ std::cout << "adapter_index=" << adapter.index+ << " name=" << NarrowAscii(adapter.description.Description)+ << " vendor=0x" << std::hex << std::setw(4) << std::setfill('0')+ << adapter.description.VendorId << " device=0x" << std::setw(4)+ << adapter.description.DeviceId << std::dec+ << " luid=" << LuidString(adapter.description.AdapterLuid) << '\n';+ }+}++const Adapter& SelectAdapter(const std::vector<Adapter>& adapters, UINT index) {+ auto found = std::find_if(adapters.begin(), adapters.end(),+ [index](const Adapter& adapter) {+ return adapter.index == index;+ });+ if (found == adapters.end()) {+ Fail("adapter-select", "selected DXGI adapter index is unavailable");+ }+ return *found;+}++struct DeviceContext final {+ ComPtr<ID3D11Device> device;+ ComPtr<ID3D11DeviceContext> context;+ ComPtr<IMFDXGIDeviceManager> manager;+ D3D_FEATURE_LEVEL feature_level = D3D_FEATURE_LEVEL_9_1;+};++DeviceContext CreateDevice(const Adapter& adapter) {+ DeviceContext result;+ constexpr D3D_FEATURE_LEVEL levels[] = {+ D3D_FEATURE_LEVEL_12_1, D3D_FEATURE_LEVEL_12_0,+ D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0,+ };+ Check(D3D11CreateDevice(adapter.value.Get(), D3D_DRIVER_TYPE_UNKNOWN, nullptr,+ D3D11_CREATE_DEVICE_VIDEO_SUPPORT |+ D3D11_CREATE_DEVICE_BGRA_SUPPORT,+ levels, static_cast<UINT>(std::size(levels)),+ D3D11_SDK_VERSION, &result.device,+ &result.feature_level, &result.context),+ "d3d11-create-device");++ ComPtr<ID3D10Multithread> multithread;+ Check(result.device.As(&multithread), "d3d11-multithread-interface");+ multithread->SetMultithreadProtected(TRUE);+ if (!multithread->GetMultithreadProtected()) {+ Fail("d3d11-multithread-protection",+ "D3D11 device did not enable multithread protection");+ }++ UINT reset_token = 0;+ Check(MFCreateDXGIDeviceManager(&reset_token, &result.manager),+ "dxgi-device-manager-create");+ Check(result.manager->ResetDevice(result.device.Get(), reset_token),+ "dxgi-device-manager-reset");+ return result;+}++class ActivationList final {+ public:+ ActivationList() = default;+ ActivationList(const ActivationList&) = delete;+ ActivationList& operator=(const ActivationList&) = delete;+ ActivationList(ActivationList&& other) noexcept+ : values(std::exchange(other.values, nullptr)),+ count(std::exchange(other.count, 0)) {}++ ~ActivationList() {+ for (UINT32 index = 0; index < count; ++index) {+ values[index]->Release();+ }+ CoTaskMemFree(values);+ }++ IMFActivate** values = nullptr;+ UINT32 count = 0;+};++std::wstring GetOptionalString(IMFAttributes* attributes, const GUID& key) {+ wchar_t* raw = nullptr;+ UINT32 length = 0;+ HRESULT result = attributes->GetAllocatedString(key, &raw, &length);+ if (result == MF_E_ATTRIBUTENOTFOUND) {+ return L"";+ }+ Check(result, "mft-activation-string");+ std::wstring value(raw, length);+ CoTaskMemFree(raw);+ return value;+}++ActivationList EnumerateHardwareEncoders(const Adapter& adapter) {+ ComPtr<IMFAttributes> attributes;+ Check(MFCreateAttributes(&attributes, 1), "mft-enumeration-attributes");+ Check(attributes->SetBlob(MFT_ENUM_ADAPTER_LUID,+ reinterpret_cast<const UINT8*>(+ &adapter.description.AdapterLuid),+ sizeof(LUID)),+ "mft-enumeration-adapter-luid");++ MFT_REGISTER_TYPE_INFO input = {MFMediaType_Video, MFVideoFormat_NV12};+ MFT_REGISTER_TYPE_INFO output = {MFMediaType_Video, MFVideoFormat_H264};+ ActivationList list;+ Check(MFTEnum2(MFT_CATEGORY_VIDEO_ENCODER,+ MFT_ENUM_FLAG_HARDWARE | MFT_ENUM_FLAG_SORTANDFILTER,+ &input, &output, attributes.Get(), &list.values, &list.count),+ "mft-hardware-enumeration");+ if (list.count == 0) {+ Fail("mft-hardware-enumeration",+ "selected adapter exposed no hardware NV12-to-H264 MFT");+ }+ return list;+}++struct SelectedTransform final {+ std::string name;+ std::string clsid;+ ComPtr<IMFTransform> transform;+ ComPtr<ICodecAPI> codec;+ ComPtr<IMFMediaEventGenerator> events;+};++SelectedTransform ActivateTransform(const ActivationList& list, UINT index,+ IMFDXGIDeviceManager* manager) {+ if (index >= list.count) {+ Fail("mft-select", "selected hardware MFT index is unavailable");+ }++ IMFActivate* activation = list.values[index];+ std::wstring friendly = GetOptionalString(activation, MFT_FRIENDLY_NAME_Attribute);+ if (friendly.empty()) {+ Fail("mft-identity", "hardware MFT has no friendly-name attribute");+ }++ GUID clsid = {};+ Check(activation->GetGUID(MFT_TRANSFORM_CLSID_Attribute, &clsid),+ "mft-clsid");++ PROPVARIANT hardware_url;+ PropVariantInit(&hardware_url);+ HRESULT hardware_result = activation->GetItem(+ MFT_ENUM_HARDWARE_URL_Attribute, &hardware_url);+ PropVariantClear(&hardware_url);+ if (FAILED(hardware_result)) {+ Fail("mft-hardware-identity",+ "hardware-only activation omitted its hardware URL attribute");+ }++ SelectedTransform selected;+ selected.name = NarrowAscii(friendly);+ selected.clsid = GuidString(clsid);+ Check(activation->ActivateObject(IID_PPV_ARGS(&selected.transform)),+ "mft-activate");++ ComPtr<IMFAttributes> transform_attributes;+ Check(selected.transform->GetAttributes(&transform_attributes),+ "mft-global-attributes");+ UINT32 is_async = 0;+ Check(transform_attributes->GetUINT32(MF_TRANSFORM_ASYNC, &is_async),+ "mft-async-readback");+ if (is_async != TRUE) {+ Fail("mft-async-readback", "hardware MFT is not asynchronous");+ }+ Check(transform_attributes->SetUINT32(MF_TRANSFORM_ASYNC_UNLOCK, TRUE),+ "mft-async-unlock");++ UINT32 d3d11_aware = 0;+ Check(transform_attributes->GetUINT32(MF_SA_D3D11_AWARE, &d3d11_aware),+ "mft-d3d11-aware-readback");+ if (d3d11_aware != TRUE) {+ Fail("mft-d3d11-aware-readback", "hardware MFT is not D3D11-aware");+ }++ Check(selected.transform->ProcessMessage(+ MFT_MESSAGE_SET_D3D_MANAGER,+ reinterpret_cast<ULONG_PTR>(manager)),+ "mft-set-d3d-manager");+ Check(selected.transform.As(&selected.codec), "mft-codec-api");+ Check(selected.transform.As(&selected.events), "mft-event-generator");+ return selected;+}++void RequireProperty(ICodecAPI* codec, const GUID& key,+ const std::string& name) {+ HRESULT supported = codec->IsSupported(&key);+ if (supported != S_OK) {+ if (FAILED(supported) && supported != E_NOTIMPL) {+ Check(supported, name + "-supported-query");+ }+ Fail(name + "-unsupported", "required codec property is unsupported");+ }+ HRESULT modifiable = codec->IsModifiable(&key);+ if (modifiable == S_FALSE) {+ Fail(name + "-readonly", "required codec property is read-only");+ }+ // Some hardware MFTs do not implement this advisory query. SetValue=S_OK+ // followed by exact GetValue readback remains the authoritative gate.+ if (modifiable != S_OK && modifiable != E_NOTIMPL) {+ Check(modifiable, name + "-modifiable-query");+ }+}++UINT32 ReadU32(ICodecAPI* codec, const GUID& key, const std::string& name) {+ VARIANT value;+ VariantInit(&value);+ Check(codec->GetValue(&key, &value), name + "-readback");+ if (value.vt != VT_UI4) {+ VariantClear(&value);+ Fail(name + "-readback-type", "codec property did not return VT_UI4");+ }+ UINT32 result = value.ulVal;+ VariantClear(&value);+ return result;+}++bool ReadBool(ICodecAPI* codec, const GUID& key, const std::string& name) {+ VARIANT value;+ VariantInit(&value);+ Check(codec->GetValue(&key, &value), name + "-readback");+ if (value.vt != VT_BOOL) {+ VariantClear(&value);+ Fail(name + "-readback-type", "codec property did not return VT_BOOL");+ }+ bool result = value.boolVal != VARIANT_FALSE;+ VariantClear(&value);+ return result;+}++void SetU32(ICodecAPI* codec, const GUID& key, UINT32 expected,+ const std::string& name) {+ RequireProperty(codec, key, name);+ VARIANT value;+ VariantInit(&value);+ value.vt = VT_UI4;+ value.ulVal = expected;+ HRESULT set_result = codec->SetValue(&key, &value);+ VariantClear(&value);+ if (set_result != S_OK) {+ if (FAILED(set_result)) {+ Check(set_result, name + "-set");+ }+ Fail(name + "-readonly", "codec property rejected the requested value");+ }+ if (ReadU32(codec, key, name) != expected) {+ Fail(name + "-weakened", "codec property readback changed the requested value");+ }+}++void SetBool(ICodecAPI* codec, const GUID& key, bool expected,+ const std::string& name) {+ RequireProperty(codec, key, name);+ VARIANT value;+ VariantInit(&value);+ value.vt = VT_BOOL;+ value.boolVal = expected ? VARIANT_TRUE : VARIANT_FALSE;+ HRESULT set_result = codec->SetValue(&key, &value);+ VariantClear(&value);+ if (set_result != S_OK) {+ if (FAILED(set_result)) {+ Check(set_result, name + "-set");+ }+ Fail(name + "-readonly", "codec property rejected the requested value");+ }+ if (ReadBool(codec, key, name) != expected) {+ Fail(name + "-weakened", "codec property readback changed the requested value");+ }+}++void ConfigureCodec(ICodecAPI* codec) {+ SetU32(codec, CODECAPI_AVEncCommonRateControlMode,+ eAVEncCommonRateControlMode_CBR, "codec-cbr");+ SetBool(codec, CODECAPI_AVLowLatencyMode, true, "codec-low-latency");+ SetU32(codec, CODECAPI_AVEncCommonMeanBitRate, kBitRate,+ "codec-mean-bitrate");+ SetU32(codec, CODECAPI_AVEncCommonBufferSize, kVbvBytes,+ "codec-vbv-bytes");+ SetU32(codec, CODECAPI_AVEncMPVGOPSize, kGopFrames, "codec-gop");+ RequireProperty(codec, CODECAPI_AVEncVideoForceKeyFrame,+ "codec-force-keyframe");+}++ComPtr<IMFMediaType> CreateOutputType() {+ ComPtr<IMFMediaType> type;+ Check(MFCreateMediaType(&type), "output-type-create");+ Check(type->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video),+ "output-type-major");+ Check(type->SetGUID(MF_MT_SUBTYPE, MFVideoFormat_H264),+ "output-type-subtype");+ Check(type->SetUINT32(MF_MT_AVG_BITRATE, kBitRate),+ "output-type-bitrate");+ Check(MFSetAttributeSize(type.Get(), MF_MT_FRAME_SIZE, kWidth, kHeight),+ "output-type-size");+ Check(MFSetAttributeRatio(type.Get(), MF_MT_FRAME_RATE, kFrameRate, 1),+ "output-type-framerate");+ Check(MFSetAttributeRatio(type.Get(), MF_MT_PIXEL_ASPECT_RATIO, 1, 1),+ "output-type-pixel-aspect");+ Check(type->SetUINT32(MF_MT_INTERLACE_MODE,+ MFVideoInterlace_Progressive),+ "output-type-interlace");+ Check(type->SetUINT32(MF_MT_MPEG2_PROFILE, eAVEncH264VProfile_Base),+ "output-type-profile");+ Check(type->SetUINT32(MF_MT_MPEG2_LEVEL, 31), "output-type-level");+ return type;+}++ComPtr<IMFMediaType> CreateInputType() {+ ComPtr<IMFMediaType> type;+ Check(MFCreateMediaType(&type), "input-type-create");+ Check(type->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video),+ "input-type-major");+ Check(type->SetGUID(MF_MT_SUBTYPE, MFVideoFormat_NV12),+ "input-type-subtype");+ Check(MFSetAttributeSize(type.Get(), MF_MT_FRAME_SIZE, kWidth, kHeight),+ "input-type-size");+ Check(MFSetAttributeRatio(type.Get(), MF_MT_FRAME_RATE, kFrameRate, 1),+ "input-type-framerate");+ Check(MFSetAttributeRatio(type.Get(), MF_MT_PIXEL_ASPECT_RATIO, 1, 1),+ "input-type-pixel-aspect");+ Check(type->SetUINT32(MF_MT_INTERLACE_MODE,+ MFVideoInterlace_Progressive),+ "input-type-interlace");+ Check(type->SetUINT32(MF_MT_DEFAULT_STRIDE, kWidth),+ "input-type-stride");+ return type;+}++void RequireTypeU32(IMFAttributes* type, const GUID& key, UINT32 expected,+ const std::string& stage) {+ UINT32 actual = 0;+ Check(type->GetUINT32(key, &actual), stage + "-readback");+ if (actual != expected) {+ Fail(stage + "-weakened", "media type changed a required value");+ }+}++void ValidateMediaTypes(IMFTransform* transform) {+ ComPtr<IMFMediaType> output;+ Check(transform->GetOutputCurrentType(0, &output),+ "output-type-current");+ GUID subtype = {};+ Check(output->GetGUID(MF_MT_SUBTYPE, &subtype), "output-type-subtype-readback");+ if (subtype != MFVideoFormat_H264) {+ Fail("output-type-subtype-weakened", "output is not H264");+ }+ RequireTypeU32(output.Get(), MF_MT_AVG_BITRATE, kBitRate,+ "output-type-bitrate");+ RequireTypeU32(output.Get(), MF_MT_MPEG2_PROFILE,+ eAVEncH264VProfile_Base, "output-type-profile");+ RequireTypeU32(output.Get(), MF_MT_MPEG2_LEVEL, 31, "output-type-level");++ UINT32 width = 0;+ UINT32 height = 0;+ Check(MFGetAttributeSize(output.Get(), MF_MT_FRAME_SIZE, &width, &height),+ "output-type-size-readback");+ if (width != kWidth || height != kHeight) {+ Fail("output-type-size-weakened", "output dimensions changed");+ }+ UINT32 numerator = 0;+ UINT32 denominator = 0;+ Check(MFGetAttributeRatio(output.Get(), MF_MT_FRAME_RATE, &numerator,+ &denominator),+ "output-type-framerate-readback");+ if (numerator != kFrameRate || denominator != 1) {+ Fail("output-type-framerate-weakened", "output frame rate changed");+ }+}++void ValidateCodecReadback(ICodecAPI* codec) {+ if (ReadU32(codec, CODECAPI_AVEncCommonRateControlMode, "codec-cbr") !=+ eAVEncCommonRateControlMode_CBR ||+ !ReadBool(codec, CODECAPI_AVLowLatencyMode, "codec-low-latency") ||+ ReadU32(codec, CODECAPI_AVEncCommonMeanBitRate,+ "codec-mean-bitrate") != kBitRate ||+ ReadU32(codec, CODECAPI_AVEncCommonBufferSize, "codec-vbv-bytes") !=+ kVbvBytes ||+ ReadU32(codec, CODECAPI_AVEncMPVGOPSize, "codec-gop") != kGopFrames) {+ Fail("codec-final-readback", "one or more codec properties were weakened");+ }+}++std::vector<UINT8> SyntheticNv12(UINT32 frame_index) {+ std::vector<UINT8> pixels(kWidth * kHeight * 3 / 2);+ const UINT32 bar_start = (frame_index * 11) % kWidth;+ for (UINT32 y = 0; y < kHeight; ++y) {+ for (UINT32 x = 0; x < kWidth; ++x) {+ UINT32 distance = (x + kWidth - bar_start) % kWidth;+ UINT8 base = static_cast<UINT8>(16 + ((x / 8 + y / 8 + frame_index) % 180));+ pixels[y * kWidth + x] = distance < 96 ? static_cast<UINT8>(235) : base;+ }+ }+ UINT8* uv = pixels.data() + kWidth * kHeight;+ for (UINT32 y = 0; y < kHeight / 2; ++y) {+ for (UINT32 x = 0; x < kWidth; x += 2) {+ uv[y * kWidth + x] = static_cast<UINT8>(96 + (frame_index % 64));+ uv[y * kWidth + x + 1] = static_cast<UINT8>(160 - (frame_index % 64));+ }+ }+ return pixels;+}++ComPtr<IMFSample> CreateInputSample(ID3D11Device* device, UINT32 frame_index) {+ std::vector<UINT8> pixels = SyntheticNv12(frame_index);+ D3D11_TEXTURE2D_DESC description = {};+ description.Width = kWidth;+ description.Height = kHeight;+ description.MipLevels = 1;+ description.ArraySize = 1;+ description.Format = DXGI_FORMAT_NV12;+ description.SampleDesc.Count = 1;+ description.Usage = D3D11_USAGE_DEFAULT;++ D3D11_SUBRESOURCE_DATA initial = {};+ initial.pSysMem = pixels.data();+ initial.SysMemPitch = kWidth;+ initial.SysMemSlicePitch = static_cast<UINT>(pixels.size());++ ComPtr<ID3D11Texture2D> texture;+ Check(device->CreateTexture2D(&description, &initial, &texture),+ "input-texture-create");++ ComPtr<IMFMediaBuffer> buffer;+ Check(MFCreateDXGISurfaceBuffer(IID_ID3D11Texture2D, texture.Get(), 0,+ FALSE, &buffer),+ "input-dxgi-buffer");+ ComPtr<IMFSample> sample;+ Check(MFCreateVideoSampleFromSurface(nullptr, &sample),+ "input-video-sample");+ Check(sample->AddBuffer(buffer.Get()), "input-sample-buffer");+ Check(sample->SetSampleTime(static_cast<LONGLONG>(frame_index) *+ kFrameDuration100ns),+ "input-sample-time");+ Check(sample->SetSampleDuration(kFrameDuration100ns),+ "input-sample-duration");+ return sample;+}++struct NalSummary final {+ bool annex_b = false;+ bool sps = false;+ bool pps = false;+ bool idr = false;+ std::optional<std::string> profile_level_id;+};++std::optional<std::pair<size_t, size_t>> FindStartCode(+ const std::vector<UINT8>& bytes, size_t from) {+ for (size_t index = from; index + 3 <= bytes.size(); ++index) {+ if (bytes[index] != 0 || bytes[index + 1] != 0) {+ continue;+ }+ if (bytes[index + 2] == 1) {+ return std::pair<size_t, size_t>{index, 3};+ }+ if (index + 4 <= bytes.size() && bytes[index + 2] == 0 &&+ bytes[index + 3] == 1) {+ return std::pair<size_t, size_t>{index, 4};+ }+ }+ return std::nullopt;+}++NalSummary InspectAnnexB(const std::vector<UINT8>& bytes) {+ NalSummary summary;+ std::optional<std::pair<size_t, size_t>> current = FindStartCode(bytes, 0);+ if (!current || current->first != 0) {+ return summary;+ }+ summary.annex_b = true;+ while (current) {+ size_t nal_start = current->first + current->second;+ std::optional<std::pair<size_t, size_t>> next = FindStartCode(bytes, nal_start);+ size_t nal_end = next ? next->first : bytes.size();+ if (nal_start < nal_end) {+ UINT8 nal_type = bytes[nal_start] & 0x1f;+ summary.sps = summary.sps || nal_type == 7;+ summary.pps = summary.pps || nal_type == 8;+ summary.idr = summary.idr || nal_type == 5;+ if (nal_type == 7 && nal_end - nal_start >= 4) {+ std::string value = ProfileLevelId(bytes[nal_start + 1],+ bytes[nal_start + 2],+ bytes[nal_start + 3]);+ if (summary.profile_level_id && *summary.profile_level_id != value) {+ Fail("bitstream-sps-consistency",+ "one access unit contained conflicting SPS profile bytes");+ }+ summary.profile_level_id = value;+ }+ }+ current = next;+ }+ return summary;+}++std::vector<UINT8> ReadSample(IMFSample* sample) {+ DWORD total_length = 0;+ Check(sample->GetTotalLength(&total_length), "output-total-length");+ if (total_length == 0 || total_length > kMaxEncodedSampleBytes) {+ Fail("output-buffer-bounds", "encoded output size is empty or unbounded");+ }++ ComPtr<IMFMediaBuffer> buffer;+ Check(sample->ConvertToContiguousBuffer(&buffer), "output-contiguous-buffer");+ DWORD length = 0;+ Check(buffer->GetCurrentLength(&length), "output-buffer-length");+ if (length != total_length) {+ Fail("output-buffer-length", "contiguous output length changed unexpectedly");+ }+ std::vector<UINT8> bytes(length);+ UINT8* raw = nullptr;+ DWORD maximum = 0;+ DWORD current = 0;+ Check(buffer->Lock(&raw, &maximum, ¤t), "output-buffer-lock");+ if (raw == nullptr || current != length || current > kMaxEncodedSampleBytes) {+ buffer->Unlock();+ Fail("output-buffer-lock-bounds",+ "locked output length is inconsistent or unbounded");+ }+ std::copy(raw, raw + current, bytes.begin());+ Check(buffer->Unlock(), "output-buffer-unlock");+ return bytes;+}++ULONGLONG ProcessCpu100ns() {+ FILETIME created = {};+ FILETIME exited = {};+ FILETIME kernel = {};+ FILETIME user = {};+ if (!GetProcessTimes(GetCurrentProcess(), &created, &exited, &kernel, &user)) {+ Check(HRESULT_FROM_WIN32(GetLastError()), "process-cpu-sample");+ }+ ULARGE_INTEGER kernel_value = {};+ kernel_value.LowPart = kernel.dwLowDateTime;+ kernel_value.HighPart = kernel.dwHighDateTime;+ ULARGE_INTEGER user_value = {};+ user_value.LowPart = user.dwLowDateTime;+ user_value.HighPart = user.dwHighDateTime;+ return kernel_value.QuadPart + user_value.QuadPart;+}++class VideoEncodeSampler final {+ public:+ explicit VideoEncodeSampler(LUID adapter_luid)+ : adapter_luid_(adapter_luid), process_id_(GetCurrentProcessId()) {}++ ~VideoEncodeSampler() { Stop(); }++ void Start() {+ worker_ = std::thread([this]() { Run(); });+ }++ void Stop() {+ stop_.store(true);+ if (worker_.joinable()) {+ worker_.join();+ }+ }++ std::vector<double> Samples() const {+ std::lock_guard<std::mutex> lock(mutex_);+ if (!error_.empty()) {+ Fail("videoencode-counter", error_);+ }+ return samples_;+ }++ private:+ void Run() noexcept {+ PDH_HQUERY query = nullptr;+ PDH_HCOUNTER counter = nullptr;+ PDH_STATUS status = PdhOpenQueryW(nullptr, 0, &query);+ if (status == ERROR_SUCCESS) {+ status = PdhAddEnglishCounterW(+ query, L"\\GPU Engine(*)\\Utilization Percentage", 0, &counter);+ }+ if (status == ERROR_SUCCESS) {+ status = PdhCollectQueryData(query);+ }+ if (status != ERROR_SUCCESS) {+ SetError("could not initialize the Windows GPU Engine counter");+ if (query != nullptr) {+ PdhCloseQuery(query);+ }+ return;+ }++ std::wostringstream prefix;+ prefix << L"pid_" << process_id_ << L"_luid_0x" << std::hex+ << std::setfill(L'0') << std::setw(8)+ << static_cast<UINT32>(adapter_luid_.HighPart) << L"_0x"+ << std::setw(8) << adapter_luid_.LowPart << L"_";+ std::wstring expected_prefix = Lower(prefix.str());++ while (!stop_.load()) {+ std::this_thread::sleep_for(std::chrono::milliseconds(250));+ status = PdhCollectQueryData(query);+ if (status != ERROR_SUCCESS) {+ SetError("could not sample the Windows GPU Engine counter");+ break;+ }++ DWORD bytes = 0;+ DWORD count = 0;+ status = PdhGetFormattedCounterArrayW(counter,+ PDH_FMT_DOUBLE | PDH_FMT_NOCAP100,+ &bytes, &count, nullptr);+ if (status != PDH_MORE_DATA) {+ if (status == PDH_CSTATUS_NO_INSTANCE || status == PDH_NO_DATA) {+ StoreSample(0.0);+ continue;+ }+ SetError("could not size the Windows GPU Engine counter array");+ break;+ }+ if (bytes == 0 || bytes > kMaxPdhArrayBytes ||+ count > kMaxPdhArrayItems ||+ count > bytes / sizeof(PDH_FMT_COUNTERVALUE_ITEM_W)) {+ SetError("Windows GPU Engine counter array exceeded its bounds");+ break;+ }+ std::vector<UINT8> storage(bytes);+ auto* items = reinterpret_cast<PDH_FMT_COUNTERVALUE_ITEM_W*>(+ storage.data());+ status = PdhGetFormattedCounterArrayW(+ counter, PDH_FMT_DOUBLE | PDH_FMT_NOCAP100, &bytes, &count, items);+ if (status != ERROR_SUCCESS) {+ SetError("could not read the Windows GPU Engine counter array");+ break;+ }+ if (bytes > storage.size() || count > kMaxPdhArrayItems ||+ count > storage.size() / sizeof(PDH_FMT_COUNTERVALUE_ITEM_W)) {+ SetError("Windows GPU Engine counter array changed beyond its bounds");+ break;+ }++ double total = 0.0;+ for (DWORD index = 0; index < count; ++index) {+ std::wstring name = Lower(items[index].szName);+ if (name.find(expected_prefix) != std::wstring::npos &&+ name.find(L"engtype_videoencode") != std::wstring::npos &&+ items[index].FmtValue.CStatus == ERROR_SUCCESS) {+ total += std::max(0.0, items[index].FmtValue.doubleValue);+ }+ }+ StoreSample(total);+ }+ PdhCloseQuery(query);+ }++ void StoreSample(double value) {+ std::lock_guard<std::mutex> lock(mutex_);+ samples_.push_back(value);+ }++ void SetError(const std::string& error) {+ std::lock_guard<std::mutex> lock(mutex_);+ error_ = error;+ }++ LUID adapter_luid_;+ DWORD process_id_;+ std::atomic<bool> stop_ = false;+ std::thread worker_;+ mutable std::mutex mutex_;+ std::vector<double> samples_;+ std::string error_;+};++struct RunEvidence final {+ UINT32 inputs = 0;+ UINT32 outputs = 0;+ UINT32 max_in_flight = 0;+ UINT32 recovery_units = 0;+ UINT64 output_bytes = 0;+ std::vector<double> latencies_ms;+ std::vector<double> video_encode;+ std::string profile_level_id;+ ULONGLONG cpu_100ns = 0;+ double wall_seconds = 0.0;+};++void ForceKeyFrame(ICodecAPI* codec) {+ VARIANT value;+ VariantInit(&value);+ value.vt = VT_UI4;+ value.ulVal = 1;+ HRESULT set_result =+ codec->SetValue(&CODECAPI_AVEncVideoForceKeyFrame, &value);+ VariantClear(&value);+ if (set_result != S_OK) {+ if (FAILED(set_result)) {+ Check(set_result, "codec-force-keyframe-set");+ }+ Fail("codec-force-keyframe-readonly",+ "force-key-frame command was not accepted");+ }+}++ComPtr<IMFSample> CreateCallerOutputSample(const MFT_OUTPUT_STREAM_INFO& info) {+ if (info.cbSize > kMaxEncodedSampleBytes) {+ Fail("output-buffer-contract",+ "hardware MFT requested an output buffer larger than four MiB");+ }+ ComPtr<IMFSample> sample;+ Check(MFCreateSample(&sample), "output-sample-create");+ ComPtr<IMFMediaBuffer> buffer;+ Check(MFCreateMemoryBuffer(kMaxEncodedSampleBytes, &buffer),+ "output-memory-buffer");+ Check(sample->AddBuffer(buffer.Get()), "output-sample-buffer");+ return sample;+}++ComPtr<IMFSample> PullOutput(IMFTransform* transform,+ const MFT_OUTPUT_STREAM_INFO& info) {+ bool transform_provides =+ (info.dwFlags & (MFT_OUTPUT_STREAM_PROVIDES_SAMPLES |+ MFT_OUTPUT_STREAM_CAN_PROVIDE_SAMPLES)) != 0;+ ComPtr<IMFSample> caller_sample;+ if (!transform_provides) {+ caller_sample = CreateCallerOutputSample(info);+ }++ MFT_OUTPUT_DATA_BUFFER output = {};+ output.dwStreamID = 0;+ output.pSample = caller_sample.Get();+ DWORD status = 0;+ HRESULT result = transform->ProcessOutput(0, 1, &output, &status);++ ComPtr<IMFCollection> events;+ if (output.pEvents != nullptr) {+ events.Attach(output.pEvents);+ }+ Check(result, "mft-process-output");+ if ((output.dwStatus & MFT_OUTPUT_DATA_BUFFER_FORMAT_CHANGE) != 0 ||+ (status & MFT_PROCESS_OUTPUT_STATUS_NEW_STREAMS) != 0) {+ Fail("mft-output-format-change",+ "hardware MFT changed format during the fixed run");+ }++ if (caller_sample) {+ if (output.pSample != caller_sample.Get()) {+ if (output.pSample != nullptr) {+ output.pSample->Release();+ }+ Fail("mft-output-allocation",+ "hardware MFT replaced the caller-owned output sample");+ }+ return caller_sample;+ }+ if (output.pSample == nullptr) {+ Fail("mft-output-sample", "hardware MFT produced no output sample");+ }+ ComPtr<IMFSample> produced;+ produced.Attach(output.pSample);+ return produced;+}++bool IsRecoveryFrame(UINT32 frame) {+ return frame % kGopFrames == 0;+}++double Percentile95(std::vector<double> values) {+ if (values.empty()) {+ return 0.0;+ }+ std::sort(values.begin(), values.end());+ size_t index = static_cast<size_t>(+ std::ceil(static_cast<double>(values.size()) * 0.95)) - 1;+ return values[std::min(index, values.size() - 1)];+}++RunEvidence RunEncoder(const Adapter& adapter, const DeviceContext& device,+ SelectedTransform& selected) {+ ConfigureCodec(selected.codec.Get());+ ComPtr<IMFMediaType> output_type = CreateOutputType();+ Check(selected.transform->SetOutputType(0, output_type.Get(), 0),+ "mft-set-output-type");+ ComPtr<IMFMediaType> input_type = CreateInputType();+ Check(selected.transform->SetInputType(0, input_type.Get(), 0),+ "mft-set-input-type");+ ValidateMediaTypes(selected.transform.Get());+ ValidateCodecReadback(selected.codec.Get());++ MFT_OUTPUT_STREAM_INFO output_info = {};+ Check(selected.transform->GetOutputStreamInfo(0, &output_info),+ "mft-output-stream-info");++ Check(selected.transform->ProcessMessage(MFT_MESSAGE_NOTIFY_BEGIN_STREAMING,+ 0),+ "mft-begin-streaming");+ Check(selected.transform->ProcessMessage(MFT_MESSAGE_NOTIFY_START_OF_STREAM,+ 0),+ "mft-start-stream");++ RunEvidence evidence;+ std::vector<std::chrono::steady_clock::time_point> submitted(kFrameCount);+ std::vector<bool> received(kFrameCount, false);+ std::optional<std::string> observed_profile;+ UINT32 pending_input = 0;+ bool drain_sent = false;+ bool drain_complete = false;++ VideoEncodeSampler video_encode(adapter.description.AdapterLuid);+ ULONGLONG cpu_start = ProcessCpu100ns();+ auto wall_start = std::chrono::steady_clock::now();+ auto next_input = wall_start;+ auto deadline = wall_start + std::chrono::seconds(30);+ video_encode.Start();++ try {+ while ((!drain_complete || evidence.outputs != kFrameCount) &&+ std::chrono::steady_clock::now() < deadline) {+ for (UINT32 event_count = 0; event_count < kMaxEventsPerPump;+ ++event_count) {+ if (std::chrono::steady_clock::now() >= deadline) {+ break;+ }+ ComPtr<IMFMediaEvent> event;+ HRESULT event_result = selected.events->GetEvent(MF_EVENT_FLAG_NO_WAIT,+ &event);+ if (event_result == MF_E_NO_EVENTS_AVAILABLE) {+ break;+ }+ Check(event_result, "mft-get-event");+ HRESULT event_status = S_OK;+ Check(event->GetStatus(&event_status), "mft-event-status-read");+ Check(event_status, "mft-event-status");+ MediaEventType type = MEUnknown;+ Check(event->GetType(&type), "mft-event-type");++ if (type == METransformNeedInput) {+ if (evidence.inputs < kFrameCount) {+ if (pending_input >= kMaxInFlight) {+ Fail("mft-input-request-bound",+ "hardware MFT queued more than eight input requests");+ }+ ++pending_input;+ }+ } else if (type == METransformHaveOutput) {+ ComPtr<IMFSample> sample = PullOutput(selected.transform.Get(),+ output_info);+ LONGLONG sample_time = -1;+ Check(sample->GetSampleTime(&sample_time), "output-sample-time");+ if (sample_time < 0 || sample_time % kFrameDuration100ns != 0) {+ Fail("output-timestamp", "output timestamp is off the input cadence");+ }+ UINT32 frame = static_cast<UINT32>(sample_time / kFrameDuration100ns);+ if (frame >= kFrameCount || received[frame]) {+ Fail("output-order", "output timestamp is duplicate or out of range");+ }+ if (frame != evidence.outputs) {+ Fail("output-reordering", "output frames were reordered");+ }++ std::vector<UINT8> bytes = ReadSample(sample.Get());+ NalSummary nal = InspectAnnexB(bytes);+ if (!nal.annex_b) {+ Fail("bitstream-annexb", "output is not Annex-B H264");+ }+ if (nal.profile_level_id) {+ if (observed_profile && *observed_profile != *nal.profile_level_id) {+ Fail("bitstream-profile-change",+ "SPS profile-level-id changed during the run");+ }+ observed_profile = nal.profile_level_id;+ }+ if (IsRecoveryFrame(frame) && !(nal.sps && nal.pps && nal.idr)) {+ Fail("bitstream-recovery-unit",+ "requested recovery access unit lacks SPS, PPS, or IDR");+ }+ if (IsRecoveryFrame(frame)) {+ ++evidence.recovery_units;+ }++ received[frame] = true;+ ++evidence.outputs;+ evidence.output_bytes += bytes.size();+ auto now = std::chrono::steady_clock::now();+ evidence.latencies_ms.push_back(+ std::chrono::duration<double, std::milli>(now - submitted[frame])+ .count());+ } else if (type == METransformDrainComplete) {+ drain_complete = true;+ } else if (type == MEError) {+ Fail("mft-error-event", "hardware MFT emitted MEError");+ }+ }++ auto now = std::chrono::steady_clock::now();+ if (pending_input > 0 && evidence.inputs < kFrameCount &&+ evidence.inputs - evidence.outputs < kMaxInFlight &&+ now >= next_input) {+ UINT32 frame = evidence.inputs;+ if (IsRecoveryFrame(frame)) {+ ForceKeyFrame(selected.codec.Get());+ }+ ComPtr<IMFSample> sample = CreateInputSample(device.device.Get(), frame);+ submitted[frame] = std::chrono::steady_clock::now();+ Check(selected.transform->ProcessInput(0, sample.Get(), 0),+ "mft-process-input");+ ++evidence.inputs;+ --pending_input;+ evidence.max_in_flight = std::max(+ evidence.max_in_flight, evidence.inputs - evidence.outputs);+ if (evidence.max_in_flight > kMaxInFlight) {+ Fail("mft-in-flight-bound", "hardware MFT exceeded eight in-flight frames");+ }+ next_input = wall_start + std::chrono::microseconds(+ static_cast<int64_t>(evidence.inputs) *+ 1'000'000 / kFrameRate);+ if (evidence.inputs == kFrameCount) {+ Check(selected.transform->ProcessMessage(+ MFT_MESSAGE_NOTIFY_END_OF_STREAM, 0),+ "mft-end-stream");+ Check(selected.transform->ProcessMessage(MFT_MESSAGE_COMMAND_DRAIN,+ 0),+ "mft-drain");+ drain_sent = true;+ }+ } else {+ std::this_thread::sleep_for(std::chrono::milliseconds(1));+ }+ }+ } catch (...) {+ video_encode.Stop();+ selected.transform->ProcessMessage(MFT_MESSAGE_COMMAND_FLUSH, 0);+ selected.transform->ProcessMessage(MFT_MESSAGE_NOTIFY_END_STREAMING, 0);+ throw;+ }++ video_encode.Stop();+ auto wall_end = std::chrono::steady_clock::now();+ ULONGLONG cpu_end = ProcessCpu100ns();+ Check(selected.transform->ProcessMessage(MFT_MESSAGE_NOTIFY_END_STREAMING, 0),+ "mft-end-streaming");++ if (!drain_sent || !drain_complete) {+ Fail("mft-drain-timeout", "hardware MFT did not drain within 30 seconds");+ }+ if (evidence.inputs != kFrameCount || evidence.outputs != kFrameCount) {+ Fail("frame-count", "hardware MFT did not produce 360 outputs for 360 inputs");+ }+ if (!observed_profile) {+ Fail("bitstream-sps", "no SPS profile-level-id was observed");+ }+ evidence.profile_level_id = *observed_profile;+ evidence.video_encode = video_encode.Samples();+ evidence.cpu_100ns = cpu_end - cpu_start;+ evidence.wall_seconds =+ std::chrono::duration<double>(wall_end - wall_start).count();++ double max_video_encode = evidence.video_encode.empty()+ ? 0.0+ : *std::max_element(evidence.video_encode.begin(),+ evidence.video_encode.end());+ double mean_video_encode = evidence.video_encode.empty()+ ? 0.0+ : std::accumulate(evidence.video_encode.begin(),+ evidence.video_encode.end(),+ 0.0) /+ evidence.video_encode.size();+ std::cout << "evidence_inputs=" << evidence.inputs << '\n'+ << "evidence_outputs=" << evidence.outputs << '\n'+ << "evidence_recovery_units=" << evidence.recovery_units << '\n'+ << "evidence_max_in_flight=" << evidence.max_in_flight << '\n'+ << "evidence_output_bytes=" << evidence.output_bytes << '\n'+ << "evidence_profile_level_id=" << evidence.profile_level_id << '\n'+ << std::fixed << std::setprecision(3)+ << "evidence_latency_p95_ms="+ << Percentile95(evidence.latencies_ms) << '\n'+ << "evidence_wall_seconds=" << evidence.wall_seconds << '\n'+ << "evidence_process_cpu_seconds="+ << static_cast<double>(evidence.cpu_100ns) / 10'000'000.0 << '\n'+ << "evidence_videoencode_samples=" << evidence.video_encode.size()+ << '\n'+ << "evidence_videoencode_mean_percent=" << mean_video_encode << '\n'+ << "evidence_videoencode_max_percent=" << max_video_encode << '\n'+ << std::flush;++ if (evidence.video_encode.size() < 10 || max_video_encode <= 0.1) {+ Fail("videoencode-attribution",+ "no process-and-adapter-attributed VideoEncode activity was observed");+ }+ constexpr const char* default_pion_fmtp[] = {+ "42001f", "42e01f", "4d001f", "64001f",+ };+ if (std::find(std::begin(default_pion_fmtp), std::end(default_pion_fmtp),+ *observed_profile) == std::end(default_pion_fmtp)) {+ Fail("bitstream-pinned-fmtp",+ "SPS profile-level-id is not in the default Pion mode-1 fmtp set");+ }+ return evidence;+}++UINT ParseIndex(const wchar_t* value, const std::string& stage) {+ try {+ size_t consumed = 0;+ unsigned long parsed = std::stoul(value, &consumed, 10);+ if (value[consumed] != L'\0' || parsed > std::numeric_limits<UINT>::max()) {+ Fail(stage, "index is not a bounded unsigned integer");+ }+ return static_cast<UINT>(parsed);+ } catch (const std::exception&) {+ Fail(stage, "index is not a bounded unsigned integer");+ }+}++struct Arguments final {+ bool list = false;+ std::optional<UINT> adapter_index;+ UINT mft_index = 0;+};++Arguments ParseArguments(int count, wchar_t** values) {+ Arguments arguments;+ for (int index = 1; index < count; ++index) {+ std::wstring argument = values[index];+ if (argument == L"--list-adapters") {+ arguments.list = true;+ } else if (argument == L"--adapter-index" && index + 1 < count) {+ arguments.adapter_index = ParseIndex(values[++index], "argument-adapter");+ } else if (argument == L"--mft-index" && index + 1 < count) {+ arguments.mft_index = ParseIndex(values[++index], "argument-mft");+ } else {+ Fail("arguments", "unsupported or incomplete command-line argument");+ }+ }+ if (!arguments.list && !arguments.adapter_index) {+ Fail("arguments", "--adapter-index is required for an encode run");+ }+ return arguments;+}++void PrintEvidence(const Adapter& adapter, UINT mft_index,+ const SelectedTransform& selected,+ const RunEvidence& evidence) {+ double max_video_encode = *std::max_element(evidence.video_encode.begin(),+ evidence.video_encode.end());+ double mean_video_encode = std::accumulate(evidence.video_encode.begin(),+ evidence.video_encode.end(), 0.0) /+ evidence.video_encode.size();+ std::cout << "result=go-native-h264-hardware-fixture\n"+ << "adapter_index=" << adapter.index << '\n'+ << "adapter_name=" << NarrowAscii(adapter.description.Description)+ << '\n'+ << "adapter_luid=" << LuidString(adapter.description.AdapterLuid)+ << '\n'+ << "mft_index=" << mft_index << '\n'+ << "mft_name=" << selected.name << '\n'+ << "mft_clsid=" << selected.clsid << '\n'+ << "inputs=" << evidence.inputs << '\n'+ << "outputs=" << evidence.outputs << '\n'+ << "recovery_units=" << evidence.recovery_units << '\n'+ << "max_in_flight=" << evidence.max_in_flight << '\n'+ << "output_bytes=" << evidence.output_bytes << '\n'+ << "profile_level_id=" << evidence.profile_level_id << '\n'+ << std::fixed << std::setprecision(3)+ << "latency_p95_ms=" << Percentile95(evidence.latencies_ms) << '\n'+ << "wall_seconds=" << evidence.wall_seconds << '\n'+ << "process_cpu_seconds="+ << static_cast<double>(evidence.cpu_100ns) / 10'000'000.0 << '\n'+ << "videoencode_samples=" << evidence.video_encode.size() << '\n'+ << "videoencode_mean_percent=" << mean_video_encode << '\n'+ << "videoencode_max_percent=" << max_video_encode << '\n';+}++} // namespace++int wmain(int argc, wchar_t** argv) {+ try {+ Arguments arguments = ParseArguments(argc, argv);+ Runtime runtime;+ std::vector<Adapter> adapters = EnumerateAdapters();+ if (arguments.list) {+ PrintAdapters(adapters);+ return 0;+ }++ const Adapter& adapter = SelectAdapter(adapters, *arguments.adapter_index);+ DeviceContext device = CreateDevice(adapter);+ ActivationList activations = EnumerateHardwareEncoders(adapter);+ for (UINT32 index = 0; index < activations.count; ++index) {+ std::wstring name = GetOptionalString(activations.values[index],+ MFT_FRIENDLY_NAME_Attribute);+ std::cout << "mft_candidate_index=" << index+ << " name=" << NarrowAscii(name) << '\n';+ }+ SelectedTransform selected = ActivateTransform(+ activations, arguments.mft_index, device.manager.Get());+ std::cout << "selected_adapter_index=" << adapter.index << '\n'+ << "selected_adapter_name="+ << NarrowAscii(adapter.description.Description) << '\n'+ << "selected_adapter_luid="+ << LuidString(adapter.description.AdapterLuid) << '\n'+ << "selected_mft_index=" << arguments.mft_index << '\n'+ << "selected_mft_name=" << selected.name << '\n'+ << "selected_mft_clsid=" << selected.clsid << '\n'+ << std::flush;+ RunEvidence evidence = RunEncoder(adapter, device, selected);+ PrintEvidence(adapter, arguments.mft_index, selected, evidence);+ return 0;+ } catch (const GateFailure& error) {+ std::cerr << "result=no-go-native-h264-hardware-fixture\n"+ << "stage=" << error.stage() << '\n'+ << "detail=" << error.what() << '\n';+ if (FAILED(error.result())) {+ std::cerr << "hresult=0x" << std::hex << std::setfill('0')+ << std::setw(8) << static_cast<UINT32>(error.result()) << '\n';+ }+ return 2;+ } catch (const std::exception& error) {+ std::cerr << "result=no-go-native-h264-hardware-fixture\n"+ << "stage=unclassified\n"+ << "detail=" << error.what() << '\n';+ return 3;+ }+}