ScreenCaptureKit - Sample project doesn't work on macOS Sonoma

I'm getting this error when I try to run the ScreenCaptureKit sample project on macOS Sonoma:

[ERROR] _SCStream_RemoteAudioQueueOperationHandlerWithError:1,053 streamOutput NOT found. Dropping frame
[ERROR] _SCStream_RemoteVideoQueueOperationHandlerWithError:1,020 stream output NOT found. Dropping frame

Both streamOutput are being set like this:

try stream?.addStreamOutput(streamOutput, type: .screen, sampleHandlerQueue: videoSampleBufferQueue)
try stream?.addStreamOutput(streamOutput, type: .audio, sampleHandlerQueue: audioSampleBufferQueue)

Link to sample project https://developer.apple.com/documentation/screencapturekit/capturing_screen_content_in_macos

Any idea of what is causing this?

Answered by Natan in 761023022

I downloaded the sample project as well and I was getting the same error.

Funnily enough, an idea came to my mind while showering. Maybe now in Sonoma SCStream keeps a weak reference to the outputs, and because the sample project declares the CaptureEngineStreamOutput instance locally, it gets discarded right away. That was my guess.

I came back to the Xcode and confirmed when I saw this line:

func startCapture(configuration: SCStreamConfiguration, filter: SCContentFilter) -> AsyncThrowingStream<CapturedFrame, Error> {
    AsyncThrowingStream<CapturedFrame, Error> { continuation in
            // The stream output object.
            let output = CaptureEngineStreamOutput(continuation: continuation)

So I declared right above the method a property to keep the reference alive:

private var streamOutput: CaptureEngineStreamOutput?

And then, assign the local property to the instance one:

streamOutput = output

That immediately fixed the error!

Did you get to the bottom of this? I am also experiencing it. Weirdly, the sample project worked for me the first couple of compiles, and then without any code changes this error started spewing in the logs and the capture preview is solid black.

@lzell I was able to "fix" it by changing the CaptureEngine class to also implement SCStreamOutput and SCStreamDelegate. So now, I'm sending self instead of streamOutput to addStreamOutput.

try stream?.addStreamOutput(self, type: .screen, sampleHandlerQueue: videoSampleBufferQueue)
Accepted Answer

I downloaded the sample project as well and I was getting the same error.

Funnily enough, an idea came to my mind while showering. Maybe now in Sonoma SCStream keeps a weak reference to the outputs, and because the sample project declares the CaptureEngineStreamOutput instance locally, it gets discarded right away. That was my guess.

I came back to the Xcode and confirmed when I saw this line:

func startCapture(configuration: SCStreamConfiguration, filter: SCContentFilter) -> AsyncThrowingStream<CapturedFrame, Error> {
    AsyncThrowingStream<CapturedFrame, Error> { continuation in
            // The stream output object.
            let output = CaptureEngineStreamOutput(continuation: continuation)

So I declared right above the method a property to keep the reference alive:

private var streamOutput: CaptureEngineStreamOutput?

And then, assign the local property to the instance one:

streamOutput = output

That immediately fixed the error!

12
ScreenCaptureKit - Sample project doesn't work on macOS Sonoma
 
 
Q