Skip to content

About

Industrial-grade native screen recording pipeline for macOS (Apple Silicon). Delivers stutter-free 4K @ 60 FPS CFR, 180Hz ProMotion capture with zero dropped frames, millisecond A/V lockstep sync, and native HEVC hardware acceleration.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

VCaptureCore

English | 中文说明 | Engineering Whitepaper (EN) | 深度工程白皮书 (中文)


💡 What is this? What exact problems does it solve? (In Plain English)

If you are developing a screen recorder for macOS and using Apple's official ScreenCaptureKit and AVAssetWriter, you will inevitably run into these exact infuriating, real-world issues:

  1. Stutter & frame drops on high-refresh screens: On 120Hz / 144Hz / 180Hz displays (like MacBook Pro built-in ProMotion or external gaming monitors), dragging windows or rapidly scrolling makes the recorded video stutter, drop frames, or freeze for a fraction of a second every few moments;
  2. Audio and video getting more and more out of sync: As recording progresses, sound and picture drift apart—eventually resulting in video racing ahead of audio by seconds, or audio leading video;
  3. Recorder freezing or corrupting files: Clicking stop hangs for 10~15 seconds, or periods of system silence followed by sudden audio cause the asset writer to crash, leaving behind a useless 68-byte broken file;
  4. Washed-out, greyish colors: Recorded 4K videos have grey blacks and collapsed contrast, looking noticeably duller than the real screen;
  5. Hardware encoder error storms: Logs flood with isReadyForMoreMediaData == false backpressure warnings, dropping dozens of frames per second.

👉 This repository shares the exact core logic that permanently fixes all of these issues.
It enables you to record native 4K UHD @ 180Hz with violent window drags and fast scrolls while producing strictly constant 60 FPS, millisecond-accurate A/V sync, 100% native color fidelity, zero stutter, and zero dropped frames.


🤖 How to use it? Just hand it to your AI Agent!

You don't need to manually struggle through complicated low-level multimedia details:
Simply git clone this repository, and point your daily AI Coding Agent (Cursor / Claude Code / Antigravity / Windsurf, etc.) directly at this repo folder. Tell the AI Agent to read and analyze the code and whitepapers here.
The AI Agent will immediately understand the decoupled producer-consumer architecture, anti-jitter ring buffer, single-tick pacing, and dual-track lockstep synchronization, and seamlessly transplant this battle-tested pipeline into your own project!


🎯 Key Problems Solved & Engineering Benchmarks

In traditional macOS screen recording implementations, violent window dragging or rapid scrolling on high-refresh 4K screens triggers severe A/V drift, dozens of hardware backpressure stalls per second, and periodic visual stutter. VCaptureCore achieves the following uncompromising industrial benchmarks:

Metric Traditional / Naive Approaches VCaptureCore Production Baseline
Resolution & Color Downscaled to 1080p or washed-out NV12 Native 4K (3840×2160) @ 32BGRA Full Range 4:4:4
Framerate Model Variable Frame Rate (VFR) causing editor drift Strict 60.000000 FPS CFR, constant 16.6667ms frame intervals
Hardware Backpressure Burst writes induce 1066 backpressure stalls 0 stalls (ZERO backpressure), no FIFO queue saturation
Hardware Dropped Frames 5.47% drop rate (116 frames) under motion 0.06% (only 1 frame in 30s extreme stress test)
Timeline Continuity 50 slot discontinuity gaps (freeze-and-jump) 0 timeline holes, 100% monotonically continuous
A/V Sync Drift Audio desynchronizes by 1.55 ~ 1.74 seconds Constant 12.71 ms (strictly within broadcast $\le 16.7\text{ms}$ lockstep)
Delivery Model Lengthy second-pass re-encoding required Stop & Deliver, streaming direct disk write, zero post-transcoding

🏗️ Core Architecture & Key Innovations

  [1. Producer Layer: SCStream 180Hz Direct Passthrough]
     └─► minimumFrameInterval = .zero (delivers every 5.55ms)
     └─► deposit() ──► Atomic pointer swap (< 5µs, zero blocking on capture queue)

  [2. Memory Pool: 16-Frame Anti-Jitter Ring Pool (Elastic Horizon Queue)]
     └─► 16 frames capacity (~530MB uncompressed 4K RAM), 88.8ms temporal span
     └─► Nearest-PTS matching (accommodates 0.44ms AppKit drag jitter, 0 fake freezes)

  [3. Consumer Layer: Strict Single-Tick Engine (Tick-Lock 60 FPS CFR)]
     └─► DispatchSourceTimer fires every 16.6667ms
     └─► 1 Tick = 1 Video Frame + 1 Audio Slice (strictly forbids burst catch-up)
     └─► Microsecond probe (<= 3ms), never starves the dispatch queue with sleeps

  [4. Hardware Channel: Apple Silicon Native 4K HEVC Hardware Acceleration]
     └─► Bound to AVVideoCodecType.hevc (M-series dedicated matrix media engine)
     └─► 4K single-frame encode latency of only 2~3ms (slashing compute by 85% vs H.264)
     └─► Explicit end-to-end ITU-R BT.709 color metadata binding

  [5. Dual-Track Pacing: Millisecond-Accurate A/V Lockstep (AudioPacer)]
     └─► Real audio committed synchronously with video in consumer tick: (slot + 1) / 60
     └─► Exact binary descriptor alignment (48kHz, Non-Interleaved Float32)
     └─► Permanently eliminates AVAssetWriter 200ms bidirectional interleaving deadlocks

📦 Module Overview

File Responsibility
FrameSlotBuffer.swift Zero-copy ring buffer (16 frames) providing Nearest-PTS elastic temporal harvesting.
AudioPacer.swift Dual-track millisecond lockstep audio pacer; synthesizes aligned silence to prevent interleaving stalls.
ScreenRecorder.swift Core recording driver pipeline (producer-consumer decoupling, HEVC setup, Tick-Lock 60fps scheduler).
RecordingConfig.swift Recording presets, display geometry, and hardware even-dimension alignment calculations.
RecordingDiagnostics.swift Nanosecond-level asynchronous telemetry counters (backpressures, drops, framerate statistics).
TimelineLogger.swift Microsecond-level per-frame timeline CSV logger (provides empirical CFR and drift proof).
SourceStreamTracker.swift Producer-side raw arrival timestamp tracker.

🚀 Quick Start (Swift Package Manager)

1. Add Package Dependency

Add this sub-repository to your Package.swift:

dependencies: [
    .package(path: "../VCaptureCore") // Or remote Git repository URL
],
targets: [
    .target(
        name: "YourApp",
        dependencies: ["VCaptureCore"]
    )
]

2. Basic Usage

import VCaptureCore
import CoreGraphics

// 1. Configure recording parameters
var config = RecordingConfig(
    selectedDisplayID: CGMainDisplayID(),
    resolution: .native,    // Preserve native 4K resolution
    frameRate: .fps60,      // Strict 60 FPS CFR
    captureSystemAudio: true
)

// 2. Initialize the core screen recorder
let outputURL = URL(fileURLWithPath: "/path/to/output/video.mp4")
let recorder = ScreenRecorder(config: config, outputURL: outputURL)

// 3. Start recording asynchronously
Task {
    do {
        try await recorder.startCapture()
        print("Screen recording running at 60 FPS CFR.")
        
        // Record for desired duration...
        try await Task.sleep(nanoseconds: 10_000_000_000)
        
        // 4. Stop and deliver (container finalized immediately, zero re-encoding)
        try await recorder.stopCapture()
        print("Recording saved to \(outputURL.path)")
    } catch {
        print("Capture failed: \(error)")
    }
}

📖 Deep-Dive Engineering Documentation

For the full physics derivations, root-cause dissections of all 27 technical traps (e.g. WindowServer throttling watchdog, real-time FIFO saturation, 88.8ms temporal pool eviction, 200ms bidirectional interleaving deadlocks), see:


📄 License

MIT License.

About

Industrial-grade native screen recording pipeline for macOS (Apple Silicon). Delivers stutter-free 4K @ 60 FPS CFR, 180Hz ProMotion capture with zero dropped frames, millisecond A/V lockstep sync, and native HEVC hardware acceleration.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages