2022 Audio & VideoGraphics & Games
WWDC22 · 15 min · Audio & Video / Graphics & Games
Meet ScreenCaptureKit
Learn how ScreenCaptureKit can deliver high-performance screen capture for your macOS screen sharing applications, video conferencing apps, game streaming services, and more. We’ll explore the building blocks of this API, learn how to configure streams to capture on-screen video and audio content, and share tips for integrating it into your existing apps.
Watch at developer.apple.com ↗Code shown on screen · 5 snippets
Creating a SCShareableContent object
// Creating a SCShareableContent object
// Get the content that's available to capture.
let content = try await SCShareableContent.excludingDesktopWindows(
false,
onScreenWindowsOnly: true
) Creating a SCContentFilter object
// Creating a SCContentFilter object
// Get the content that's available to capture.
let content = try await SCShareableContent.excludingDesktopWindows(
false,
onScreenWindowsOnly: true
)
// Exclude the sample app by matching the bundle identifier.
let excludedApps = content.applications.filter { app in
Bundle.main.bundleIdentifier == app.bundleIdentifier
}
// Create a content filter that excludes the sample app.
filter = SCContentFilter(display: display,
excludingApplications: excludedApps,
exceptingWindows: []) Creating a SCStreamConfiguration object
// Creating a SCStreamConfiguration object
let streamConfig = SCStreamConfiguration()
// Set output resolution to 1080p
streamConfig.width = 1920
streamConfig.height = 1080
// Set the capture interval at 60 fps
streamConfig.minimumFrameInterval = CMTime(value: 1, timescale: CMTimeScale(60))
// Hides cursor
streamConfig.showsCursor = false
// Enable audio capture
streamConfig.capturesAudio = true
// Set sample rate to 48000 kHz stereo
streamConfig.sampleRate = 48000
streamConfig.channelCount = 2 Creating and starting a SCStream object
// Creating and starting a SCStream object
// Create a capture stream with the filter and stream configuration
stream = SCStream(filter: filter, configuration: streamConfig, delegate: self)
// Start the capture session
try await stream?.startCapture()
// ...
// Error handling delegate
func stream(_ stream: SCStream, didStopWithError error: Error) {
DispatchQueue.main.async {
self.logger.error("Stream stopped with error: \(error.localizedDescription)")
self.error = error
self.isRecording = false
}
} Getting media samples
// SCStreamOutput protocol implementation
func stream(_ stream: SCStream, didOutputSampleBuffer sampleBuffer: CMSampleBuffer, of type: SCStreamOutputType) {
switch type {
case .screen:
handleLatestScreenSample(sampleBuffer)
case .audio:
handleLatestAudioSample(sampleBuffer)
}
}
// ...
// Create a capture stream with the filter and stream configuration
stream = SCStream(filter: filter, configuration: streamConfig, delegate: self)
// Add a stream output to capture screen and audio content
try stream?.addStreamOutput(self, type: .screen, sampleHandlerQueue: screenFrameOutputQueue)
try stream?.addStreamOutput(self, type: .audio, sampleHandlerQueue: audioFrameOutputQueue)
// Start the capture session
try await stream?.startCapture() Resources
Related sessions
-
14 min -
37 min