Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVCaptureSession Crashing when using webRTC

I am using WebRTC and its using AVCaptureSession. It works fine a few times but sometimes its getting crashed with this Exception.

Assertion failed: (_internal->figCaptureSession == NULL), function -[AVCaptureVideoPreviewLayer attachToFigCaptureSession:], file /BuildRoot/Library/Caches/com.apple.xbs/Sources/EmbeddedAVFoundation/EmbeddedAVFoundation-1187.37.2.1/Aspen/AVCaptureVideoPreviewLayer.m

like image 952
Tina Avatar asked Jul 21 '17 05:07

Tina


2 Answers

Before using instance of RTCCameraPreviewView you have to nill'ify its captureSession and assert will go away. Faced same issue.

like image 151
D.Baidaev Avatar answered Sep 29 '22 21:09

D.Baidaev


I recently had this problem aswell. In my code i kept an instance of AVCaptureOutput and added and removed it. When trying to add the same AVCaptureOutput instance to the same capture session again, this error appeared.

This is how i solved it:

private var _captureOutput: AVCaptureOutput?
var captureOutput: AVCaptureOutput {
    guard let captureOutput = _captureOutput else {
        let photoOutput = AVCapturePhotoOutput()
        photoOutput.isHighResolutionCaptureEnabled = true
        _captureOutput = photoOutput
        return photoOutput
    }
    return captureOutput
}

Initialize the instance once when needed and when removed, also nullify it.

captureSession.outputs.forEach { [weak self] output in
    self?.captureSession.removeOutput(output)
    self?._captureOutput = nil
}
like image 41
EMart Avatar answered Sep 29 '22 21:09

EMart