Unable to reliably control torch level using AVCaptureDevice APIs

I first posted this to StackOverflow, then on feedback assistant, but got no response in either of those places. Hoping to try my luck here. It's infuriating how there is no standard bug-reporting or tracking for Apple APIs.


Basically, the documentation states that using

setTorchModeOn
with the levels parameter will allow me to vary torch brightness. This doesn't seem to work reliably, and it seems that the API sets the brightness not in absolute terms, but as a fraction of current brightness (that too, erratically). Which means, that it's almost impossible to get back to max brightness once you've decreased it because that would mean setting it >100% of the current which is not allowed.


I'm using iOS Version: 13.4, iPhone 11, using Swift 5.2 on XCode 11.4


What I am doing:

- Get the camera device for torch control.

- device = AVCaptureDevice.default(.builtInDualWideCamera, for: .video, position: .back)

- device.lockForConfiguration()

- device.setTorchModeOn(level: levelToBeSet)

- device.unlockForConfiguration()


What I expect: If I give a high value to levelToBeSet, torch has a high brightness. If I decrease the value, the brightness decreases, if I increase the value, brightness increases too (as long as I stay in the 0.0 to 1.0 limit). This remains deterministic, and if overheating hasn't been brought in yet, I can change the torch brightness in a controlled fashion.


What happens: If running the app for the first time, and you set the level once, everything works as expected. If you change the levelToBeSet again, erratic behaviour starts appearing.

Example: Set levelToBeSet to the following values, in a sequential manner:

- 0.5 : Torch lights up at some brightness.

- 0.7 : Torch brightness decreases.

- 1.0 : Torch brightness increases to maximum (appears so, unable to measure illumination).

- 0.2 : Torch brightness decreases to a low amount.

- 0.8 : Torch brightness decreases further.

- 0.9 : Torch brightness decreases even further.

- 0.5 : Torch has turned off.

- Now whatever value you set as the level, torch remains off, unless you turn it to 1.0, where it lights up very very faintly. No way to get the torch beyond this faint brightness now.


- Restart the app.

- Set torch level to 0.5 : Torch doesn't light up.


The behaviour remains even after restarting the app. The only way to go back to normal which I know is to restart the phone, or as I found out later, take a photo in the camera app with the flash turned on.


Some sample code on stackoverflow: https://stackoverflow.com/questions/61139537/torch-brightness-control-using-avcapturedevice-settorchmodeon-behaves-very-errat

A very small sample app which just creates a slider to change brightness levels, to demo how this API doesn't work : https://github.com/mavidser/fantastic-couscous