AVCaptureSession setting preset has no effect if HDR configured with AVCaptureVideoDataOutput

I want to confirm if this is a bug or a programming error. Very easy to reproduce it by modifying AVCam sample code. Steps to reproduce:

  1. Add AVCaptureVideoDataOutput to AVCaptureSession, no need to set delegate in AVCam sample code (CaptureService actor)

    private let videoDataOutput = AVCaptureVideoDataOutput()

    and then in configureSession method, add the following line

       try addOutput(videoDataOutput)
         
         if videoDataOutput.availableVideoPixelFormatTypes.contains(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange) {
             videoDataOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as AnyHashable as! String : kCVPixelFormatType_420YpCbCr8BiPlanarFullRange]
    

}

And next modify set HDR method:

      /// Sets whether the app captures HDR video.
func setHDRVideoEnabled(_ isEnabled: Bool) {
    // Bracket the following configuration in a begin/commit configuration pair.
    captureSession.beginConfiguration()
    defer { captureSession.commitConfiguration() }
    do {
        // If the current device provides a 10-bit HDR format, enable it for use.
        if isEnabled, let format = currentDevice.activeFormat10BitVariant {
            try currentDevice.lockForConfiguration()
            currentDevice.activeFormat = format
            currentDevice.unlockForConfiguration()
            isHDRVideoEnabled = true
            
            if videoDataOutput.availableVideoPixelFormatTypes.contains(kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange) {
                videoDataOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as AnyHashable as! String : kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange]
            }
          
        } else {
            captureSession.sessionPreset = .high
            isHDRVideoEnabled = false
            
            
            if videoDataOutput.availableVideoPixelFormatTypes.contains(kCVPixelFormatType_32BGRA) {
                print("Setting sdr pixel format \(kCVPixelFormatType_32BGRA)")
                videoDataOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as AnyHashable as! String : kCVPixelFormatType_32BGRA]
            }
            try currentDevice.lockForConfiguration()
            currentDevice.activeColorSpace = .sRGB
            currentDevice.unlockForConfiguration()
        }
    } catch {
        logger.error("Unable to obtain lock on device and can't enable HDR video capture.")
    }

The problem now is toggling HDR on and off no longer works in video mode. If after setting HDR on, you set HDR to off, active format of device does not change (setting sessionPreset has no effect). This does not happen if video data output is not added to session.

Is there any workaround available?

Dear AVFoundation Engineers,

Here is a workaround I found (apart from unconfiguring and reconfiguring AVCaptureSession). It works by removing VDO from captureSession and adding it back and resetting pixel format to 32BGRA, any other pixel format fails! Your comments awaited.

 captureSession.removeOutput(videoDataOutput) //Workaround to reset deviceformat back to SDR
  captureSession.sessionPreset = .high
  isHDRVideoEnabled = false
                
   //Only 32BGRA works, setting it to YUV420 8 bit full range fails!
    if videoDataOutput.availableVideoPixelFormatTypes.contains(kCVPixelFormatType_32BGRA) {
                    print("Setting sdr pixel format \(kCVPixelFormatType_32BGRA)")
                    videoDataOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as AnyHashable as! String : kCVPixelFormatType_32BGRA]
                }
                
                captureSession.addOutput(videoDataOutput) //End workaround
                try currentDevice.lockForConfiguration()
                currentDevice.activeColorSpace = .sRGB
                currentDevice.unlockForConfiguration()
AVCaptureSession setting preset has no effect if HDR configured with AVCaptureVideoDataOutput
 
 
Q