The code beneath fashions two digital camera duties utilizing AVFoundation – configuring Seize Session and receiving/processing frames from digital camera. Each the options are carried out as actor they usually would possibly generally discuss to 1 one other. Swift 6 is about because the Swift language model for compilation. That is the place I’ve few doubts:
-
The perform
configureSessionfails to construct as I entrycaptureManager.videoOutputinactor CaptureSession. I may repair it by making the perform configureSession as async and wrap all of the code in Process closure. However then because the code expands I might want to take steps to stop reentrancy. Is that the one approach two actors can discuss or there’s a higher approach out right here? -
There’s nonetheless a warning Sending ‘sampleBuffer’ dangers inflicting knowledge races. Process-isolated ‘sampleBuffer’ is captured by a actor-isolated closure. actor-isolated makes use of in closure could race towards later nonisolated makes use of within the delegate callback methodology. Do I must forcefully wrap it in a
@Unchecked Sendablestruct or there’s a safer approach on the market?@preconcurrency import AVFoundation actor CaptureManager: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate { let captureQueue = DispatchSerialQueue(label: "Output Queue") let videoOutput = AVCaptureVideoDataOutput() // Units the session queue because the actor's executor. nonisolated var unownedExecutor: UnownedSerialExecutor { captureQueue.asUnownedSerialExecutor() } override init() { tremendous.init() // Set the delegate to obtain video frames from digital camera videoOutput.setSampleBufferDelegate(self, queue: captureQueue) } //Delegate methodology for receiving video frames nonisolated func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) { self.assumeIsolated { supervisor in if let videoDataOutput = output as? AVCaptureVideoDataOutput { supervisor.processVideoSampleBuffer(sampleBuffer, fromOutput: videoDataOutput) } } } func processVideoSampleBuffer(_ sampleBuffer:CMSampleBuffer, fromOutput: AVCaptureVideoDataOutput) { //Do the processing } }
And right here is the opposite actor.
actor CaptureSession {
non-public let session = AVCaptureSession()
non-public let captureManager = CaptureManager()
init() {
Process {
await configureSession()
}
}
func configureSession() {
let videoOutput = captureManager.videoOutput //Construct fails right here
session.addOutput(videoOutput)
}
}