Skip to content

fix: capture screens with ScreenCaptureKit on macOS - #354

Merged
devopvoid merged 1 commit into
mainfrom
fix/macos-screen-capture-sck
Oct 12, 2026
Merged

devopvoid merged 1 commit into
mainfrom
fix/macos-screen-capture-sck

Conversation

@devopvoid

Copy link
Copy Markdown
Owner

Addresses #344: screen capture on macOS freezing on its first frame while the cursor keeps moving.

Screens are captured with ScreenCaptureKit on macOS 14 and newer

VideoDesktopSource always got ScreenCapturerMac on macOS, because WebRTC tries ScreenCaptureKit only when allow_sck_capturer is set, and it never was. ScreenCapturerMac takes its first frame from a display snapshot and afterwards only copies in what its CGDisplayStream reports as changed. When that stream stops calling back, it keeps delivering the first frame as SUCCESS, and DesktopAndCursorComposer draws a live cursor over it: the symptom in the issue.

VideoTrackDesktopSource::capture() now sets allow_sck_capturer on macOS. On releases before 14, CreateScreenCapturerSck returns null and WebRTC falls back to ScreenCapturerMac as before. The capturer is also given the source's frame rate, since ScreenCaptureKit otherwise captures at the display refresh rate.

What stays as it was:

  • Source lists (ScreenCapturer.getDesktopSources()): without its system picker, ScreenCaptureKit lists no screens, so DesktopCapturer keeps ScreenCapturerMac. Both name a screen by its CGDirectDisplayID, so the listed IDs select the same screen.
  • Window capture: this WebRTC branch has no ScreenCaptureKit window capturer outside the system picker, so WindowCapturerMac is still used.

One behavioural difference, from reading WebRTC rather than testing it: without screen recording permission, ScreenCaptureKit fails with ERROR_PERMANENT and the capture stops, where ScreenCapturerMac delivered the wallpaper and the app's own windows.

Reproduction

Not reproduced. On macOS 14.5 (Apple silicon, screen recording permission granted), the unfixed build followed a window switching between black and white in another process every 400 ms, in three 12 s runs, and window capture followed it too. The reporter's macOS version isn't known yet.

Testing

macOS 14.5 arm64: mvn -pl webrtc test passes (284 tests, 0 failures, 15 skipped), with the desktop capture tests run, not skipped.

With the fix, the WebRTC log shows ScreenCapturerSck capturing the display listed by ScreenCapturerMac. Frames follow the same switching window as before, the first one arriving 120 to 230 ms after start(), at the right size and colours, with a single cursor.

VideoDesktopSourceTests.screenFramesFollowScreen (macOS only) switches a window of its own between black and white while the screen is captured, and fails if the frames don't follow. It passes on this Mac with either capturer, so it guards against a frozen capture rather than reproducing #344.

A VideoDesktopSource capturing a screen on macOS always used
ScreenCapturerMac, since WebRTC tries ScreenCaptureKit only when
allow_sck_capturer is set. That capturer takes its first frame from a
display snapshot and afterwards only copies what its CGDisplayStream
reports as changed, so when the stream stops calling back, it keeps
sending the first frame, with a live cursor composited on top (#344).

Screens are now captured with ScreenCaptureKit on macOS 14 and newer,
and with ScreenCapturerMac as before on older releases. ScreenCaptureKit
is told the source's frame rate, as it otherwise captures at the display
refresh rate. Source lists stay with ScreenCapturerMac: without its
system picker ScreenCaptureKit lists no screens, and both name a screen
by its display ID. Window capture is unchanged.

A new test switches a window of its own between black and white while
the screen is captured, and checks the frames follow.
@devopvoid
devopvoid merged commit abc19d5 into main Oct 12, 2026
22 checks passed
@devopvoid
devopvoid deleted the fix/macos-screen-capture-sck branch October 12, 2026 21:11
SendableMetatype added a commit to EduGeyser/webrtc-java that referenced this pull request Oct 15, 2026
Brings in the work upstream added from October 11 to 15: hardware video
codecs on every platform, through Media Foundation, VideoToolbox, VA-API
and NVDEC, with WebRTC built with H.265 (devopvoid#346 to devopvoid#348, devopvoid#352, devopvoid#353, devopvoid#356,
devopvoid#359, devopvoid#371 to devopvoid#377); HEVC and AV1 decoding in the media player, with
dav1d and Vulkan Video (devopvoid#331, devopvoid#345); encoded frame injectors (devopvoid#365);
receiver source changes and audio packet infos (devopvoid#366); audio codec
factories (devopvoid#340); network thread observers called on a callback thread
(devopvoid#364); screen capture with ScreenCaptureKit (devopvoid#354); Windows audio
devices that mix more than two channels (devopvoid#358); autoninja (devopvoid#357); and
the release channels of the docs site (devopvoid#361).

Fork-side adaptations:
- The factory keeps the CreatePeerConnectionFactory overload without a
  worker thread, and still switches device capture on the network
  thread. devopvoid#355 copies WebRTC's factory setup and has the audio module
  learn a worker thread, which only matters for a worker thread distinct
  from the network thread, and WebRTC is removing those. The audio codec
  factories and hardware codec switches of devopvoid#340 and devopvoid#359 go to that
  overload.
- WebRTC stays on a commit on main, so the move to branch 8059 (devopvoid#360)
  and its fixed clang package are left out.
- The data channels variant builds WebRTC without H.265 and without the
  extra targets, leaves out the audio codec factory bindings and
  addTransceiverOfType, and skips the new tests that need audio or
  video.
- dav1d is built against the sysroot on native Linux builds as well, as
  FFmpeg is. CI installs Meson for the full variant only, checks out the
  dav1d submodule, and adds its commit to the FFmpeg cache key. The
  fork's Windows SDK step stays, without upstream's second one.
- The release workflow records the docs' version in the tagged commit,
  as upstream's now does. The docs site still builds from main, since
  this fork publishes none.
- The Maven files stay deleted: Jetty is bumped in the version catalog,
  and the docs lockfile takes upstream's bumps on top of the fork's
  newer VitePress.
- The media module's README builds with Gradle.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant