Ryan Patterson
Ryan Patterson

Reputation: 627

How can I use ScreenCaptureKit to capture an entire desktop?

There appears to be a limitation or bug in ScreenCaptureKit where it cannot simply capture a desktop. However, many applications do this, so it must be possible. Using the the official Apple sample application exhibits this bug.

Here's the code in question:

guard let display = selectedDisplay else { fatalError("No display selected.") }
var excludedApps = [SCRunningApplication]()
// If a user chooses to exclude the app from the stream,
// exclude it by matching its bundle identifier.
if isAppExcluded {
    excludedApps = availableApps.filter { app in
        Bundle.main.bundleIdentifier == app.bundleIdentifier
    }
}
// Create a content filter with excluded apps.
filter = SCContentFilter(display: display,
                         excludingApplications: excludedApps,
                         exceptingWindows: [])

In the sample application, when isAppExcluded is enabled, everything works fine. However, when you set this to false (there is a checkbox in the application), the stream immediately stops.
If we recompile the application to start the stream with the variable set to false, the stream never starts.
In both cases, toggling the variable to true fixes the stream and it works normally.

Question: How can I capture an entire display and all of its windows with ScreenCaptureKit?

Upvotes: 2

Views: 815

Answers (1)

Federico Terzi
Federico Terzi

Reputation: 101

I've experienced the same problem. I think it's a bug in the ScreenCaptureKit API that prevents unfiltered calls (aka. without any app or window filter) from being processed correctly.

A possible workaround I've had success with is using the initWithDisplay:includingApplications:exceptingWindows: initialization, passing all applications as input.

For more information and a complete snippet, see also: https://federicoterzi.com/blog/screencapturekit-failing-to-capture-the-entire-display

Upvotes: 1

Related Questions