Dunfey · Hotel WWDC as data, est. 1983
Front desk everything
Years
Topics

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 ↗

Transcript all transcripts

Code shown on screen · 5 snippets

Creating a SCShareableContent object swift · at 6:53 ↗
// 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 swift · at 8:32 ↗
// 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 swift · at 10:23 ↗
// 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 swift · at 11:46 ↗
// 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 swift · at 13:07 ↗
// 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