iPhone 7+, ios 11.2: Depth data delivery is not supported in the current configuration

I'm trying to produce the absolute minimal code to get

AVDepthData
from an iPhone 7+ using its DualCam.

I have this code:



import UIKit
import AVFoundation
class RecorderViewController: UIViewController {
    @IBOutlet weak var previewView: UIView!
  
    @IBAction func onTapTakePhoto(_ sender: Any) {
      
        guard let capturePhotoOutput = self.capturePhotoOutput else { return }
      
        let photoSettings = AVCapturePhotoSettings()
      
        photoSettings.isDepthDataDeliveryEnabled = true // Error
      
        capturePhotoOutput.capturePhoto(with: photoSettings, delegate: self)
      
    }
  
    var session: AVCaptureSession?
    var videoPreviewLayer: AVCaptureVideoPreviewLayer?
    var capturePhotoOutput: AVCapturePhotoOutput?
  
    override func viewDidLoad() {
        super.viewDidLoad()
      
        AVCaptureDevice.requestAccess(for: .video, completionHandler: { _ in })
      
        let captureDevice = AVCaptureDevice.default(.builtInDualCamera, for: .depthData, position: .back)
      
        do {
            print(captureDevice!)
            let input = try AVCaptureDeviceInput(device: captureDevice!)
          
            self.capturePhotoOutput = AVCapturePhotoOutput()
            self.capturePhotoOutput?.isDepthDataDeliveryEnabled = true // Error
          
            self.session = AVCaptureSession()
            self.session?.addInput(input)
          
            self.videoPreviewLayer = AVCaptureVideoPreviewLayer(session: self.session!)
            self.videoPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
            self.videoPreviewLayer?.frame = view.layer.bounds
            previewView.layer.addSublayer(self.videoPreviewLayer!)
          
            self.session?.addOutput(self.capturePhotoOutput!)
            self.session?.startRunning()
          
        } catch {
            print(error)
        }
      
    }
  
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        /
    }
  
}
extension RecorderViewController : AVCapturePhotoCaptureDelegate {
  
    func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
        print(photo.depthData)
    }
  
  
}



If I comment out the lines that are marked with "Error" the code works as I would expect, and prints

nil
for
depthData
.

However, leaving the lines as they are, I get an exception. The error message states:

AVCapturePhotoOutput setDepthDataDeliveryEnabled:] Depth data delivery is not supported in the current configuration
.

How do I change the "current configuration" so that depth delivery is supported?

I've watched this video: https://developer.apple.com/videos/play/wwdc2017/507/ which was helpful, and I believe I've followed the exact steps required to make this work.

Any tips would be gratefully received!

You must pick an AVCaptureSession session preset or AVCaptureDevice activeFormat that support depth data delivery. Change the sessionPreset to AVCaptureSessionPresetPhoto.

Thanks, I tried that, adding the following code:



self.session = AVCaptureSession()
self.session?.sessionPreset = AVCaptureSession.Preset.photo
self.session?.addInput(input)


but I'm still getting the same error.

Accepted Answer

The photo output won't let you enable a feature such as depth data delivery until it's sure the source can make good on that feature. You need to hook up your ins and outs first, then do your configuration. In your case, you're trying to opt in for depth data delivery on the output before it's attached to a session. It doesn't know if depth data delivery can be enabled yet. If you check the depthDataDeliverySupported property, it will tell you if you're properly configured to enable the feature.

Thanks vey much, that's incredibly helpful. I have fixed my issue.

iPhone 7+, ios 11.2: Depth data delivery is not supported in the current configuration
 
 
Q