AVCaptureMultiCamSession & AVCaptureSession runtime error

I am dynamically switching between AVCaptureMultiCamSession & AVCaptureSession and here is the problem I face. I add back camera to MultiCam session as demonstrated in AVMultiCamPIP sample code and it works. Next I deallocate this session and create an AVCaptureSessio. But when I add the back camera device to AVCaptureSession & set the sessionPreset to 4K (as done in AVCam sample code), I get this error:



Capture session runtime error: related decl 'e' for AVError(_nsError: Error Domain=AVFoundationErrorDomain Code=-11873 "Cannot Record" UserInfo={NSLocalizedDescription=Cannot Record, AVErrorDeviceKey=<AVCaptureFigVideoDevice: 0x15d0074f0 [Back Camera][com.apple.avfoundation.avcapturedevice.built-in_video:0]>, NSLocalizedRecoverySuggestion=Reset the camera to a supported active format, then restart the session., NSLocalizedFailureReason=The camera's active format is unsupported by this session.})




If on the other hand I set the sessionPreset to 1080p, I get a different error - "Capture session was interrupted (video device is in use by another client)".


I even tried to set the activeFormat of device before adding it to session, but no difference. Wondering what could be the problem.

Sounds like you didn't actually deallocate the multicam session. You may have released it, but someone was holding onto it (perhaps in an autorelease pool) at the time you made your new AVCaptureSession and tried to add that same camera to it. Making the sessionPreset of the new AVCaptureSession 4K implicitly changes the activeFormat of the camera to 4K, which is an unsupported config for the multicam session.


In both cases, the correct thing to do is to stop the multicam session ([session stopRunning]) before trying to get rid of it. Then you can proceed with your 4K session without fear. The multicam session only cares what format you've set on your cameras when it's running.

You were right, the multicam session was not released, KVO observation was holding it and that was copied from AVMultiCamPip sample code. I fixed the KVO observation and the multicam session is now released. But the problem never stops. I have the same AVCaptureVideoPreviewLayer that is passed between the two sessions and on stopping the multi cam session, deallocating it, and creating AVCaptureSession with default 1080p session preset now crashes the app. I am wondering if this piece of code of switching between sessions is bug free in AVFoundation.



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

And even if I set the videoPreviewLayer.session = nil after stopping session, I get crashes when I add AVCaptureVideoDataOutput:


#1

0x00000001bdd83058 in KVO_IS_RETAINING_ALL_OBSERVERS_OF_THIS_OBJECT_IF_IT_CRASHES_AN_OBSERVER_WAS_OVERRELEASED_OR_SMASHED ()

#20x00000001bdd80a20 in NSKeyValueWillChangeWithPerThreadPendingNotifications ()
#30x00000001c7eff9ac in -[AVCaptureFigVideoDevice _setActiveFormat:resetVideoZoomFactorAndMinMaxFrameDurations:] ()
#40x00000001c7f324f0 in -[AVCaptureSession _updateDeviceActiveFormatsAndActiveConnections] ()
#50x00000001c7f31224 in -[AVCaptureSession _buildAndRunGraph] ()


#60x00000001c7f2b174 in -[AVCaptureSession _commitConfiguration] ()
#70x00000001c7f2ccec in -[AVCaptureSession addOutput:] ()

I'd suggest you send us a lite version of your code that reproduces the problem, using https://feedbackassistant.apple.com


You could of course swap in a different AVCaptureVideoPreviewLayer, though you shouldn't have to.

@bford I would send it through DTS request I raised. Thank you.

AVCaptureMultiCamSession &amp; AVCaptureSession runtime error
 
 
Q