iosswiftavcapture

Swift - captureOutput is not being executed


I am currently trying to implement a camera live feed to my app. I've got it setup but somehow it's not working as expected. As far as I understand, captureOutput should be executed every time a frame is recognized and the print message should be output in console, but somehow it's not - the console won't show the print command.

Does anybody see any possible mistake inside of the code?

I don't know whether that's connected to my problem, but at start of the app the console shows the following:

[BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C1.1:2][0x106b24530] get output frames failed, state 8196

import UIKit
import AVKit
import Vision

class CameraViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate {    

    override func viewDidLoad() {
        super.viewDidLoad()

        let captureSession = AVCaptureSession()

        guard let captureDevice = AVCaptureDevice.default(for: .video) else { return }
        guard let input = try? AVCaptureDeviceInput(device: captureDevice) else{ return }
        captureSession.addInput(input)

        captureSession.startRunning()

        let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        view.layer.addSublayer(previewLayer)
        previewLayer.frame = view.frame

        let dataOutput = AVCaptureVideoDataOutput()
        dataOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "videoQueue"))
        captureSession.addOutput(dataOutput)

        // let request = VNCoreMLRequest
        // VNImageRequestHandler(cgImage: <#T##CGImage#>, options: [:]).perform(request)
    }

    func captureOutput(_ output: AVCaptureOutput, didDrop sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
        print("Es hat funktioniert")
    }

}

Solution

  • You need to implement captureOutput(_:didOutput:from:) not captureOutput(_:didDrop:from:)

    func captureOutput(_ output: AVCaptureOutput,
                                didOutput sampleBuffer: CMSampleBuffer,
                                from connection: AVCaptureConnection) {
         print("Es hat funktioniert") 
    }