Skip to content

mac-capture: Auto-restart ScreenCaptureKit streams after sleep/wake#13252

Closed
AlanHuang99 wants to merge 4 commits intoobsproject:masterfrom
AlanHuang99:fix/macos-screencapture-sleep-wake
Closed

mac-capture: Auto-restart ScreenCaptureKit streams after sleep/wake#13252
AlanHuang99 wants to merge 4 commits intoobsproject:masterfrom
AlanHuang99:fix/macos-screencapture-sleep-wake

Conversation

@AlanHuang99
Copy link
Copy Markdown

Summary

  • Registers ScreenCaptureDelegate for NSWorkspaceWillSleepNotification, NSWorkspaceDidWakeNotification, and NSWorkspaceScreensDidWakeNotification
  • On sleep: suppresses capture_failed to avoid showing the restart button for an expected transition
  • On wake: sets a flag checked by video_tick after a 2-second delay (allowing GPU/display subsystem to reinitialize), then tears down and rebuilds the SCStream with a refreshed content list
  • Applies to both video and audio ScreenCaptureKit captures

Motivation

ScreenCaptureKit streams silently freeze or error out (error -3821) when macOS sleeps and wakes. OBS had no mechanism to detect or recover from this, leaving users with a frozen capture until manual restart via the "Restart Capture" button.

This addresses the root cause: OBS's ScreenCaptureKit integration had zero sleep/wake awareness. Two failure modes exist:

  1. Silent freeze: SCStream stops delivering frames but never calls didStopWithError: — OBS shows a frozen image
  2. Error-triggered stop: didStopWithError: fires with SCStreamErrorNoCaptureSource or error -3821 — OBS marks capture_failed but never auto-recovers

Design decisions

  • 2-second delay after wake before restarting — the GPU/display subsystem needs time to fully reinitialize
  • Restart from video_tick (not notification handler) — avoids thread safety issues since tick runs on OBS's video thread
  • Content list refresh before restart — handles cases where displays changed during sleep (e.g., external monitor disconnected)
  • Three notifications: WillSleep (marks expected shutdown), DidWake (system wake), ScreensDidWake (lid-open without full system sleep)

Test plan

  • Start OBS with a macOS Screen Capture source active
  • Put Mac to sleep (lid close or Apple menu → Sleep)
  • Wake Mac and verify capture resumes automatically within ~3 seconds
  • Check OBS log for "Restarting screen capture after wake from sleep" message
  • Verify audio capture also recovers after sleep/wake
  • Test with external monitor disconnect during sleep
  • Verify manual "Restart Capture" button still works for non-sleep failures

Fixes #8928
Related: #10858, #11435, #9056, #9540

🤖 Generated with Claude Code

Alan Huang and others added 4 commits March 23, 2026 14:37
ScreenCaptureKit streams silently freeze or error out (error -3821)
when macOS sleeps and wakes. OBS had no mechanism to detect or recover
from this, leaving users with a frozen capture until manual restart.

Register for NSWorkspaceWillSleepNotification, DidWakeNotification,
and ScreensDidWakeNotification on the ScreenCaptureDelegate. On sleep,
suppress capture_failed to avoid showing the restart button for an
expected transition. On wake, set a flag that the video_tick callback
checks after a 2-second delay (allowing GPU/display subsystem to
reinitialize), then tears down and rebuilds the SCStream with a
refreshed content list.

Applies to both video and audio ScreenCaptureKit captures.

Fixes: obsproject#8928

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Temporary workflow to verify the mac-capture plugin compiles
on macOS with the sleep/wake auto-restart changes.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add #include <util/platform.h> for os_gettime_ns() declaration.
Declare sleep/wake notification handler methods in the
ScreenCaptureDelegate @interface to fix undeclared selector warnings.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Update workflow to build the complete OBS Studio application
and upload OBS.app as a downloadable artifact for testing.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@Fenrirthviti
Copy link
Copy Markdown
Member

We do not accept AI generated submissions.

In the future, please take at least a moment to read a project's contribution guidelines instead of blowing away our PR template and submitting AI code we do not want.

@D1no
Copy link
Copy Markdown

D1no commented Apr 11, 2026

@Fenrirthviti even if its AI generated, this bug is around now for 2+ years and counting. Great that finally someone/thing went ahead and tried doing something about it 😑....

For anybody coming by, the only built-in solution to continue seeing your screen after your monitor turned off, is to use the depreciated Display Capture:

Sources >> + (add) >> select Deprecated >> select Display Capture

This one survives a turning displays on and off.

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.

macOS Screen Capture stops working after waking from sleep

4 participants