Screen video capture in mcap

Planning document · 2026-10-10 · for mcap after 0.1.1 (still captures with PrintScreen) · phases 1 and 2 built, mcap 0.1.4

In short

1. Scope

In:

Out, for now:

Windows only, like the stills overlay. It needs Windows 10 version 2004 or later; the Windows.Graphics.Capture features used below need that version.

2. Using it

  1. Ctrl+PrintScreen freezes the screens and shows the overlay from stills, in its video mode. The outline and the magnifier are red, not orange, so you can see it will record.
  2. Drag a region. The magnifier's label shows its size.
    • The drag is kept on the screen where it started (see 3.4).
    • Below 240 × 180, the outline is drawn dashed and the label reads 320 × 120 · at least 240 × 180. Letting go then starts nothing, and the overlay stays open to drag again.
    • A click without dragging, or Enter, takes the whole screen, as for stills.
    • Escape or a right click cancels, as for stills.
  3. Recording starts as soon as you let go, with no countdown (see open question 4).
    • A thin red border is drawn just outside the region, so it's never in the video.
    • A small toolbar sits beside the region, outside it where there's room. It shows the time, the size and frames dropped, with Pause, Stop and Discard.
    • Both the border and the toolbar are kept out of the capture with SetWindowDisplayAffinity(WDA_EXCLUDEFROMCAPTURE), which needs Windows 10 2004. Whether Desktop Duplication respects this is checked in phase 1. The border is outside the region either way, so only the toolbar depends on it.
  4. Ctrl+PrintScreen again, or Stop, ends the recording at once:
    • The encoder is flushed and the file closed.
    • The file becomes an .mcap.
    • mcap comes to the front showing it in the Capture window, as it does after a still.

Dimensions are rounded down to even numbers after the minimum is checked, since H.264's 4:2:0 colour needs that. 240 × 180 is already even, so a region that passes the check stays at least 240 × 180.

3. Grabbing frames

One interface, two implementations, chosen when the recording starts:

Capture_Source :: struct {
  start:      (source: *Capture_Source, screen: Screen_Ref, region: Rect, cursor: bool) -> ok: bool, error: string, fall_back: bool;
  next_frame: (source: *Capture_Source, timeout_ms: u32) -> Captured_Frame, status: Frame_Status;  // NEW, SAME, LOST, FAILED
  stop:       (source: *Capture_Source);
  name:       string;   // "DXGI" or "WGC", for the log and the toolbar's tooltip
}

Captured_Frame :: struct {
  texture: *ID3D11Texture2D;  // the region only, region-sized, owned by the source until the next call
  time:    s64;               // QueryPerformanceCounter ticks: when it was on screen
  cursor:  Cursor_State;      // DXGI only: where, visible, and the shape when it changed
}

Both run on a capture thread of their own, with their own D3D11 device, not the app's. The app's device starts on WARP and moves to the GPU later (#WarpFirstStartup in graphics/DX11.jai). Desktop Duplication also needs a device on the GPU the screen is connected to, which may not be the one the app draws with.

3.1 DXGI Desktop Duplication (first choice)

3.2 When to fall back

The choice is made once, at the start, and logged:

Desktop Duplication saysMeaningThen
DXGI_ERROR_UNSUPPORTED from DuplicateOutput1Two GPUs, and the process or device isn't on the one driving the screen. Also returned when no driver can duplicate.Windows.Graphics.Capture
DXGI_ERROR_NOT_CURRENTLY_AVAILABLEToo many duplications of this screen at once (other recorders running)Windows.Graphics.Capture
E_ACCESSDENIEDThe secure desktop (UAC, lock screen) is showingRetry for 2 s, then tell the user
Screen rotated (DXGI_OUTDUPL_DESC.Rotation isn't identity)Frames would come sidewaysWindows.Graphics.Capture, which hands them over upright
No output holds the regionThe screens changed since the freezeError, nothing recorded

A setting forces one method, for testing and for machines where one misbehaves: capture: auto (the default), dxgi or wgc, under a new [video] section of settings.config.

3.3 Windows.Graphics.Capture (fallback)

3.4 One screen per recording

Desktop Duplication works one output at a time, and Windows.Graphics.Capture one monitor at a time. A region over two screens would need two captures stitched together, possibly at different DPI and refresh rates. In video mode the overlay keeps the drag on the screen where it started. Spanning screens is Later.

4. The frame pipeline

capture thread                                          encoder thread
  next_frame ─► region texture ─► staging ring (3) ─►   queue (≤ 8 frames) ─► AVFrame bgr0 ─► encoder ─► MP4
                                  Map the one from        drops the oldest when full,
                                  2 frames back, copy     counted as dropped
                                  out, draw the cursor

5. Encoding

6. Into the library

7. Hotkeys

Action in [global_hotkeys]DefaultWhat it does
capture_region (stills, built)PrintScreenOpens the overlay for a still.
record_regionCtrl-PrintScreenOpens the overlay in video mode. While a recording runs, it stops and saves it.

8. Audio Phase 5

9. Phases

Each phase ends with something to run and check, and is a commit (0.1.3 onwards).

Phase 1: record-probe with Desktop Duplication Done

A command-line tool, tools/record_probe.jai, built with jai first.jai - record_probe:

It answers four questions on your laptop:

Done when: 60 s of a 4K screen at 30 frames a second plays in VLC and Windows' Media Player with the cursor drawn, frames dropped are under 1%, and each of the questions above has a recorded answer. A DXGI_ERROR_UNSUPPORTED is also an answer: it means phase 2 is needed on this machine.

Result (2026-10-10). Release build, RTX 5080 laptop. The screens are a 3840 × 2160 external and the 2560 × 1600 internal; DXGI lists both on the NVIDIA adapter.
  • The done-when run: 60.2 s of the 4K screen at 30 fps, with a square moving across it the whole time (-animate):
    • 3,602 images taken from the screen and 1,802 frames sent, all of them encoded with NVENC and none dropped.
    • 18 MB a minute.
    • FFmpeg reads it back as 60.07 s at 30 fps.
    • Per frame: 4.4 ms to read back and 13.4 ms to encode, on separate threads.
    The frame decoded back matches the one captured, cursor included, by eye. Playing it in VLC and Media Player is still to check.
  • 60 fps: all 59.3 changes a second are kept. On a fixed tick grid, a 60 Hz screen beat against the 60 fps cap and lost about 7% of frames. Frames are now sent as soon as their turn is due, on a schedule that advances by exactly one frame each time. Caps of 30, 60 and 24 give 29.8, 59.3 and 23.8 frames a second.
  • The questions:
    • Both screens duplicate with DuplicateOutput1.
    • The plain DuplicateOutput works too in a per-monitor DPI-aware process. The documented failure doesn't happen here; DuplicateOutput1 stays the first choice.
    • With a CUDA context in the process, duplication still works.
    • WDA_EXCLUDEFROMCAPTURE is respected: the excluded square is 0% present in the frame and the control square 100%.
    • The two-GPU failure wasn't reproduced. Task Manager shows the Intel GPU, but no screen is attached to it now: the laptop is in discrete-GPU mode. Testing it means switching the laptop to Optimus (hybrid) mode in the NVIDIA Control Panel or the BIOS, which needs a restart.
  • Encoders, 4K at 30 fps, 8 s of the same motion. All of them keep up:
    EncoderEncode per frameMB a minute
    h264_nvenc13.7 ms23.4
    h264_qsv (the Intel GPU, though no screen is attached to it)12.3 ms16.7
    h264_mf16.8 ms24.0
    libx264 (veryfast, CRF 23)14.9 ms11.4
    h264_amfNot available: no AMD driver (amfrt64.dll), so it falls through to the next
    Most of NVENC's 13.7 ms is FFmpeg copying the 33 MB frame before the GPU sees it. Handing it the frame without the copy is a later optimisation, not needed at 4K 60.
  • A crash mid-recording: killed about 7 s in, the fragmented MP4 still reads back with 6 s of video. At most the last keyframe interval (2 s) is lost.
  • The minimum size: 239 × 300 is refused, and 241 × 181 records at 240 × 180. The minimum is checked before rounding to even.

Changes from the plan:

  • The DPI check: once, a process that isn't DPI aware saw the 4K screen as 1920 × 1080, and the region would have been the wrong part of it. This couldn't be reproduced afterwards. dxgi_open now compares the duplication's size with the desktop's and refuses when they differ.
  • The probe's extra options: -animate, which moves a square for measuring throughput, and -check FILE, which reads a recording back.

Phase 2: Windows.Graphics.Capture fallback Done

The second source behind the same interface: WinRT bindings, polling TryGetNextFrame, the cursor and border settings. The fallback rules from 3.2, and -capture dxgi|wgc|auto on the probe.

Done when:

Result (2026-10-10). Release build, the same laptop and screens as phase 1.
  • Built:
    • wgc_source.jai: hand-written bindings for eight WinRT interfaces.
    • capture_gpu.jai: what both sources share, namely the device on the screen's adapter, the region copy and the staging ring. Each source is now only "the next image's region into a texture".
    • Record_Options.capture: .AUTO, .DXGI or .WGC.
    • The probe's -capture auto|dxgi|wgc.
  • The fallback:
    • auto picks Desktop Duplication where it works.
    • With -simulate-unsupported, which makes duplication fail with DXGI_ERROR_UNSUPPORTED as on a two-GPU laptop, auto logs why and records with Windows.Graphics.Capture.
    • -capture dxgi with the same simulated failure refuses, with exit code 3.
    • The real two-GPU case is shelved: this laptop's GPU is pinned in the BIOS.
  • Recording: 4K at 60 fps with motion: 56.6 frames a second taken and every one encoded, none dropped. Windows.Graphics.Capture delivers a little under the screen's 60 Hz; Desktop Duplication delivered 59.3. A 641 × 481 region on the internal screen gives the same frame as Desktop Duplication's in phase 1.
  • The border: IsBorderRequired = false works for mcap as an unpackaged app on Windows 11, with no access request. A GDI sample of the screen's outer 6 pixels found no yellow pixels with the border off, and 14,214 with it left on (-keep-border, the control).
  • The cursor: with the mouse put in the region (-cursor-test), Windows.Graphics.Capture's own cursor and the one cursor.jai draws into Desktop Duplication's frames are the same, pixel for pixel, by eye at 4×.
  • WDA_EXCLUDEFROMCAPTURE is respected by Windows.Graphics.Capture too: the excluded square is 0% present and the control square 100%.
  • Waiting for frames: each new frame sets an event, through a small COM handler for FrameArrived, which Windows took. The polling fallback, for when Windows refuses the handler (-poll), takes 41 frames a second of a 60 Hz screen, enough for a 30 fps recording (29.0 sent), at a 1 ms timer resolution.

Changes from the plan:

  • Events, not polling. The plan said to poll TryGetNextFrame, to avoid writing a WinRT event handler in Jai. The handler took about 30 lines, so it's the default, and polling is the fallback.
  • The source opens on the capture thread, for both methods. WinRT objects belong to the apartment that made them, so record_start starts the capture thread and waits for it to say whether its source opened.
  • The WinRT calls (RoInitialize, RoGetActivationFactory, HSTRINGs) are loaded from combase.dll at run time. Their import library, runtimeobject.lib, names no DLL of its own, which Jai's #library needs.
  • Test switches: Record_Options.testing holds three, for the probe: simulate the two-GPU failure, force polling, keep the border.

Phase 3: Recording in the app

The pieces:

Done when:

Phase 4: Border, toolbar, pause and recovery

The pieces:

Done when: neither the border nor the toolbar appears in a recording. Pausing for 10 s leaves no gap in the video. A UAC prompt or a resolution change mid-recording leaves a video that plays through.

Phase 5: Audio

Section 8: swresample bindings, loopback and microphone, mixing, AAC.

Done when: a 10-minute recording with a video playing and someone talking stays in sync to within 50 ms at the end, and silence doesn't shift it.

10. Risks

Two GPUs. This is the reason for the fallback. One step beyond it: with the NVIDIA control panel set to the high-performance GPU for everything, both methods might be affected. Phase 1 tests your laptop with and without that setting.
HDR screens. Asking for 8-bit BGRA from an HDR screen gives Windows' own tone mapping, which can look washed out. Recording HDR (16-bit float frames, HEVC 10-bit) is out of scope. The toolbar will say when the screen is HDR.
Protected content (DRM video in browsers, some streaming apps) records as black with either method. Nothing can be done about it, so it's documented in the README.
Laptop power. 60 frames a second at 4K keeps the GPU awake. The default is 30, and the probe measures the cost.

11. Open questions

  1. PrintScreen during a recording: take the whole screen without the overlay (this plan), ignore it, or freeze anyway and accept the frozen moment in the video?
  2. The folder: recordings in Screen Captures beside stills, or a folder of their own (Screen Recordings)? This plan uses Screen Captures, so the grid shows both together.
  3. A time limit: none by default, apart from stopping when the disk has less than 2 GB free?
  4. A countdown (3, 2, 1) before recording starts, as Snagit has? This plan starts at once.
  5. Mouse clicks: should they be shown in the video, for example as a ring at each click, as Snagit can?