Skip to content

Commit

Permalink
fix: Make AVCaptureDevice.sensorOrientation safer (#3064)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy authored Jul 10, 2024
1 parent a8ba80d commit 9a09260
Showing 1 changed file with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,28 @@ extension AVCaptureDevice {

// 1. Create a capture session
let session = AVCaptureSession()
session.sessionPreset = .low
if session.canSetSessionPreset(.low) {
session.sessionPreset = .low
}

// 2. Add this device as an input
guard let input = try? AVCaptureDeviceInput(device: self) else {
VisionLogger.log(level: .error, message: "Cannot dynamically determine \(uniqueID)'s sensorOrientation, " +
"falling back to \(DEFAULT_SENSOR_ORIENTATION)...")
return DEFAULT_SENSOR_ORIENTATION
}
guard session.canAddInput(input) else {
return DEFAULT_SENSOR_ORIENTATION
}
session.addInput(input)

// 3. Add an output (e.g. video data output)
let output = AVCaptureVideoDataOutput()
output.automaticallyConfiguresOutputBufferDimensions = false
output.deliversPreviewSizedOutputBuffers = true
guard session.canAddOutput(output) else {
return DEFAULT_SENSOR_ORIENTATION
}
session.addOutput(output)

// 4. Inspect the default orientation of the output
Expand Down

0 comments on commit 9a09260

Please sign in to comment.