Skip to content

Commit d670ca9

Browse files
authored
fix: Remove sensorOrientation hack on iOS, it's always portrait (#3096)
1 parent 0654898 commit d670ca9

File tree

1 file changed

+5
-56
lines changed

1 file changed

+5
-56
lines changed

package/ios/Core/Extensions/AVCaptureDevice+sensorOrientation.swift

+5-56
Original file line numberDiff line numberDiff line change
@@ -8,67 +8,16 @@
88
import AVFoundation
99
import Foundation
1010

11-
// On iOS, all built-in Cameras are landscape-left (90deg rotated)
12-
let DEFAULT_SENSOR_ORIENTATION = Orientation.landscapeLeft
11+
// On iOS, a camera device's natural sensor orientation is either
12+
// always portrait, or .videoOrientation already takes natural sensor
13+
// orientation into account.
14+
let DEFAULT_SENSOR_ORIENTATION = Orientation.portrait
1315

1416
extension AVCaptureDevice {
1517
/**
1618
Get the natural orientation of the camera sensor of this specific device.
1719
*/
1820
var sensorOrientation: Orientation {
19-
// TODO: There is no iOS API to get native sensor orientation.
20-
// - The new `RotationCoordinator` API is a blackbox, and cannot be used statically.
21-
// - Dynamically creating an AVCaptureSession is very hacky and has a runtime overhead.
22-
// Hopefully iOS adds an API to get sensor orientation soon so we can use that!
23-
24-
// 0. If we don't have Camera permission yet, we cannot create a temporary AVCaptureSession.
25-
// So we just return the default orientation as a workaround...
26-
let cameraPermissionStatus = AVCaptureDevice.authorizationStatus(for: .video)
27-
if cameraPermissionStatus != .authorized {
28-
return DEFAULT_SENSOR_ORIENTATION
29-
}
30-
31-
// 1. Create a capture session
32-
let session = AVCaptureSession()
33-
if session.canSetSessionPreset(.low) {
34-
session.sessionPreset = .low
35-
}
36-
37-
// 2. Add this device as an input
38-
guard let input = try? AVCaptureDeviceInput(device: self) else {
39-
VisionLogger.log(level: .error, message: "Cannot dynamically determine \(uniqueID)'s sensorOrientation, " +
40-
"because the AVCaptureDeviceInput cannot be created. Falling back to \(DEFAULT_SENSOR_ORIENTATION)...")
41-
return DEFAULT_SENSOR_ORIENTATION
42-
}
43-
guard session.canAddInput(input) else {
44-
VisionLogger.log(level: .error, message: "Cannot dynamically determine \(uniqueID)'s sensorOrientation, because " +
45-
"it cannot be added to the temporary AVCaptureSession. Falling back to \(DEFAULT_SENSOR_ORIENTATION)...")
46-
return DEFAULT_SENSOR_ORIENTATION
47-
}
48-
session.addInput(input)
49-
50-
// 3. Add an output (e.g. video data output)
51-
let output = AVCaptureVideoDataOutput()
52-
output.automaticallyConfiguresOutputBufferDimensions = false
53-
output.deliversPreviewSizedOutputBuffers = true
54-
guard session.canAddOutput(output) else {
55-
VisionLogger.log(level: .error, message: "Cannot dynamically determine \(uniqueID)'s sensorOrientation, because " +
56-
"the AVCaptureVideoDataOutput cannot be added to the AVCaptureSession. Falling back to \(DEFAULT_SENSOR_ORIENTATION)...")
57-
return DEFAULT_SENSOR_ORIENTATION
58-
}
59-
session.addOutput(output)
60-
61-
// 4. Inspect the default orientation of the output
62-
let defaultOrientation = output.orientation
63-
64-
// 5. Rotate the default orientation by the default sensor orientation we know of
65-
var sensorOrientation = defaultOrientation.rotatedBy(orientation: DEFAULT_SENSOR_ORIENTATION)
66-
67-
// 6. If we are on the front Camera, AVCaptureVideoDataOutput.orientation is mirrored.
68-
if position == .front {
69-
sensorOrientation = sensorOrientation.flipped()
70-
}
71-
72-
return sensorOrientation
21+
return .portrait
7322
}
7423
}

0 commit comments

Comments
 (0)