Skip to content

Commit 8890030

Browse files
authored
[camera_avfoundation] Replace the deprecated 'videoOrientation' with 'videoRotationAngle' on iOS 17+ (flutter#13247)
Remove deprecated AVFoundation camera APIs, replace with iOS 17+ API `videoRotationAngle`. This will allow us to check for deprecations higher than iOS 17 in CI, like flutter/packages#9431. Migrate CameraSetDeviceOrientationTests to Swift Testing while we're at it to take advantage of parameterization. I tested this manually and orientation worked as expected. I would upload a video but it seems camera capture doesn't work when connected through a DeviceHub session (presumably a privacy protection) and QuickTime stopped the recording whenever there was an orientation change, so you'll have to trust me it worked manually. Fixes flutter#170438 ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent ddf386a commit 8890030

6 files changed

Lines changed: 165 additions & 59 deletions

File tree

‎packages/camera/camera_avfoundation/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.11.0+2
2+
3+
* Replaces the deprecated `videoOrientation` with `videoRotationAngle` on iOS 17 and later.
4+
15
## 0.11.0+1
26

37
* Removes availability checks for iOS versions below the package minimum iOS 13.

‎packages/camera/camera_avfoundation/darwin/Tests/CameraSetDeviceOrientationTests.swift‎

Lines changed: 103 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,106 +3,156 @@
33
// found in the LICENSE file.
44

55
import AVFoundation
6-
import XCTest
6+
import Testing
7+
import UIKit
78

89
@testable import camera_avfoundation
910

10-
final class CameraSetDeviceOrientationTests: XCTestCase {
11-
private func createCamera() -> (Camera, MockCaptureConnection, MockCaptureConnection) {
11+
struct CameraSetDeviceOrientationTests {
12+
private func createCamera() -> (Camera, [MockCaptureConnection]) {
1213
let camera = CameraTestUtils.createTestCamera()
1314

1415
let mockCapturePhotoOutput = MockCapturePhotoOutput()
1516
let mockPhotoCaptureConnection = MockCaptureConnection()
1617
mockPhotoCaptureConnection.isVideoOrientationSupported = true
18+
mockPhotoCaptureConnection.isVideoRotationAngleSupported = true
1719

1820
mockCapturePhotoOutput.connectionWithMediaTypeStub = { _ in mockPhotoCaptureConnection }
1921
camera.capturePhotoOutput = mockCapturePhotoOutput
2022

2123
let mockCaptureVideoDataOutput = MockCaptureVideoDataOutput()
2224
let mockVideoCaptureConnection = MockCaptureConnection()
2325
mockVideoCaptureConnection.isVideoOrientationSupported = true
26+
mockVideoCaptureConnection.isVideoRotationAngleSupported = true
2427

2528
mockCaptureVideoDataOutput.connectionWithMediaTypeStub = { _ in mockVideoCaptureConnection }
2629
camera.captureVideoOutput = mockCaptureVideoDataOutput
2730

28-
return (camera, mockPhotoCaptureConnection, mockVideoCaptureConnection)
31+
return (camera, [mockPhotoCaptureConnection, mockVideoCaptureConnection])
2932
}
3033

31-
func testSetDeviceOrientation_setsOrientationsOfCaptureConnections() {
32-
let (camera, mockPhotoCaptureConnection, mockVideoCaptureConnection) = createCamera()
33-
var photoSetVideoOrientationCalled = false
34-
mockPhotoCaptureConnection.setVideoOrientationStub = { orientation in
35-
// Device orientation is flipped compared to video orientation. When UIDeviceOrientation
36-
// is landscape left the video orientation should be landscape right.
37-
XCTAssertEqual(orientation, .landscapeRight)
38-
photoSetVideoOrientationCalled = true
34+
private func stubOrientationUpdates(
35+
on connection: MockCaptureConnection,
36+
_ handler: @escaping (AVCaptureVideoOrientation) -> Void
37+
) {
38+
connection.setVideoOrientationStub = handler
39+
connection.setVideoRotationAngleStub = { angle in
40+
switch angle {
41+
case 0:
42+
handler(.landscapeRight)
43+
case 90:
44+
handler(.portrait)
45+
case 180:
46+
handler(.landscapeLeft)
47+
case 270:
48+
handler(.portraitUpsideDown)
49+
default:
50+
Issue.record("Unexpected video rotation angle: \(angle)")
51+
}
3952
}
53+
}
4054

41-
var videoSetVideoOrientationCalled = false
42-
mockVideoCaptureConnection.setVideoOrientationStub = { orientation in
43-
// Device orientation is flipped compared to video orientation. When UIDeviceOrientation
44-
// is landscape left the video orientation should be landscape right.
45-
XCTAssertEqual(orientation, .landscapeRight)
46-
videoSetVideoOrientationCalled = true
55+
@Test func setDeviceOrientation_setsOrientationsOfCaptureConnections() {
56+
let (camera, mockConnections) = createCamera()
57+
var setVideoOrientations: [AVCaptureVideoOrientation] = []
58+
for connection in mockConnections {
59+
stubOrientationUpdates(on: connection) { setVideoOrientations.append($0) }
4760
}
4861

4962
camera.deviceOrientation = .landscapeLeft
5063

51-
XCTAssertTrue(photoSetVideoOrientationCalled)
52-
XCTAssertTrue(videoSetVideoOrientationCalled)
64+
// Device orientation is flipped compared to video orientation. When UIDeviceOrientation
65+
// is landscape left the video orientation should be landscape right.
66+
#expect(setVideoOrientations == [.landscapeRight, .landscapeRight])
5367
}
5468

55-
func
56-
testSetDeviceOrientation_setsLockedOrientationsOfCaptureConnection_ifCaptureOrientationIsLocked()
69+
@Test
70+
func setDeviceOrientation_setsLockedOrientationsOfCaptureConnection_ifCaptureOrientationIsLocked()
5771
{
58-
let (camera, mockPhotoCaptureConnection, mockVideoCaptureConnection) = createCamera()
59-
var photoSetVideoOrientationCalled = false
60-
mockPhotoCaptureConnection.setVideoOrientationStub = { orientation in
61-
XCTAssertEqual(orientation, .portraitUpsideDown)
62-
photoSetVideoOrientationCalled = true
63-
}
72+
let (camera, mockConnections) = createCamera()
73+
camera.lockCaptureOrientation(PlatformDeviceOrientation.portraitDown)
6474

65-
var videoSetVideoOrientationCalled = false
66-
mockVideoCaptureConnection.setVideoOrientationStub = { orientation in
67-
XCTAssertEqual(orientation, .portraitUpsideDown)
68-
videoSetVideoOrientationCalled = true
75+
var setVideoOrientations: [AVCaptureVideoOrientation] = []
76+
for connection in mockConnections {
77+
stubOrientationUpdates(on: connection) { setVideoOrientations.append($0) }
6978
}
7079

71-
camera.lockCaptureOrientation(PlatformDeviceOrientation.portraitDown)
72-
7380
camera.deviceOrientation = .landscapeLeft
7481

75-
XCTAssertTrue(photoSetVideoOrientationCalled)
76-
XCTAssertTrue(videoSetVideoOrientationCalled)
82+
#expect(setVideoOrientations == [.portraitUpsideDown, .portraitUpsideDown])
7783
}
7884

79-
func testSetDeviceOrientation_doesNotSetOrientations_ifRecordingIsInProgress() {
80-
let (camera, mockPhotoCaptureConnection, mockVideoCaptureConnection) = createCamera()
81-
85+
@Test func setDeviceOrientation_doesNotSetOrientations_ifRecordingIsInProgress() {
86+
let (camera, mockConnections) = createCamera()
8287
camera.startVideoRecording(completion: { _ in }, messengerForStreaming: nil)
8388

84-
mockPhotoCaptureConnection.setVideoOrientationStub = { _ in XCTFail() }
85-
mockVideoCaptureConnection.setVideoOrientationStub = { _ in XCTFail() }
89+
for connection in mockConnections {
90+
stubOrientationUpdates(on: connection) { _ in Issue.record() }
91+
}
8692

8793
camera.deviceOrientation = .landscapeLeft
8894
}
8995

90-
func testSetDeviceOrientation_doesNotSetOrientations_forDuplicateUpdates() {
91-
let (camera, mockPhotoCaptureConnection, mockVideoCaptureConnection) = createCamera()
92-
var photoSetVideoOrientationCallCount = 0
93-
mockPhotoCaptureConnection.setVideoOrientationStub = { _ in
94-
photoSetVideoOrientationCallCount += 1
95-
}
96-
97-
var videoSetVideoOrientationCallCount = 0
98-
mockVideoCaptureConnection.setVideoOrientationStub = { _ in
99-
videoSetVideoOrientationCallCount += 1
96+
@Test func setDeviceOrientation_doesNotSetOrientations_forDuplicateUpdates() {
97+
let (camera, mockConnections) = createCamera()
98+
var setVideoOrientationCallCount = 0
99+
for connection in mockConnections {
100+
stubOrientationUpdates(on: connection) { _ in setVideoOrientationCallCount += 1 }
100101
}
101102

102103
camera.deviceOrientation = .landscapeRight
103104
camera.deviceOrientation = .landscapeRight
104105

105-
XCTAssertEqual(photoSetVideoOrientationCallCount, 1)
106-
XCTAssertEqual(videoSetVideoOrientationCallCount, 1)
106+
#expect(setVideoOrientationCallCount == mockConnections.count)
107+
}
108+
109+
@Test func setDeviceOrientation_doesNotSetOrientations_ifNotSupportedByConnection() {
110+
let (camera, mockConnections) = createCamera()
111+
for connection in mockConnections {
112+
connection.isVideoOrientationSupported = false
113+
connection.isVideoRotationAngleSupported = false
114+
stubOrientationUpdates(on: connection) { _ in Issue.record() }
115+
}
116+
117+
camera.deviceOrientation = .landscapeLeft
118+
}
119+
120+
@Test(arguments: [
121+
(UIDeviceOrientation.portrait, AVCaptureVideoOrientation.portrait),
122+
(.portraitUpsideDown, .portraitUpsideDown),
123+
(.landscapeLeft, .landscapeRight),
124+
(.landscapeRight, .landscapeLeft),
125+
// Orientations without a video equivalent fall back to portrait.
126+
(.faceUp, .portrait),
127+
])
128+
func setDeviceOrientation_mapsDeviceOrientationToVideoOrientation(
129+
deviceOrientation: UIDeviceOrientation, expectedVideoOrientation: AVCaptureVideoOrientation
130+
) {
131+
let (camera, mockConnections) = createCamera()
132+
// Orientation updates are skipped for unchanged values, so start from a non-matching one.
133+
camera.deviceOrientation = .unknown
134+
var setVideoOrientations: [AVCaptureVideoOrientation] = []
135+
for connection in mockConnections {
136+
stubOrientationUpdates(on: connection) { setVideoOrientations.append($0) }
137+
}
138+
139+
camera.deviceOrientation = deviceOrientation
140+
141+
#expect(setVideoOrientations == [expectedVideoOrientation, expectedVideoOrientation])
142+
}
143+
144+
@Test func unlockCaptureOrientation_restoresDeviceOrientation() {
145+
let (camera, mockConnections) = createCamera()
146+
camera.lockCaptureOrientation(PlatformDeviceOrientation.portraitDown)
147+
camera.deviceOrientation = .landscapeLeft
148+
149+
var setVideoOrientations: [AVCaptureVideoOrientation] = []
150+
for connection in mockConnections {
151+
stubOrientationUpdates(on: connection) { setVideoOrientations.append($0) }
152+
}
153+
154+
camera.unlockCaptureOrientation()
155+
156+
#expect(setVideoOrientations == [.landscapeRight, .landscapeRight])
107157
}
108158
}

‎packages/camera/camera_avfoundation/darwin/Tests/Mocks/MockCaptureConnection.swift‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import AVFoundation
99
/// A mock implementation of `CaptureConnection` that allows injecting a custom implementation.
1010
final class MockCaptureConnection: NSObject, CaptureConnection {
1111
var setVideoOrientationStub: ((AVCaptureVideoOrientation) -> Void)?
12+
var setVideoRotationAngleStub: ((CGFloat) -> Void)?
1213

1314
var connection: AVCaptureConnection {
1415
preconditionFailure("Attempted to access unimplemented property: connection")
@@ -20,8 +21,19 @@ final class MockCaptureConnection: NSObject, CaptureConnection {
2021
setVideoOrientationStub?(newValue)
2122
}
2223
}
24+
var videoRotationAngle: CGFloat {
25+
get { 90 }
26+
set {
27+
setVideoRotationAngleStub?(newValue)
28+
}
29+
}
2330
var inputPorts: [AVCaptureInput.Port] = []
2431
var isVideoMirroringSupported = false
2532
var isVideoOrientationSupported = false
33+
var isVideoRotationAngleSupported = false
2634
var preferredVideoStabilizationMode = AVCaptureVideoStabilizationMode.off
35+
36+
func isVideoRotationAngleSupported(_ videoRotationAngle: CGFloat) -> Bool {
37+
return isVideoRotationAngleSupported
38+
}
2739
}

‎packages/camera/camera_avfoundation/darwin/camera_avfoundation/Sources/camera_avfoundation/CaptureConnection.swift‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,27 @@ protocol CaptureConnection: NSObjectProtocol {
1111
var isVideoMirrored: Bool { get set }
1212

1313
/// Corresponds to the `videoOrientation` property of `AVCaptureConnection`
14+
@available(iOS, deprecated: 17.0, renamed: "videoRotationAngle")
1415
var videoOrientation: AVCaptureVideoOrientation { get set }
1516

17+
/// Corresponds to the `videoRotationAngle` property of `AVCaptureConnection`
18+
@available(iOS 17.0, *)
19+
var videoRotationAngle: CGFloat { get set }
20+
1621
/// Corresponds to the `inputPorts` property of `AVCaptureConnection`
1722
var inputPorts: [AVCaptureInput.Port] { get }
1823

1924
/// Corresponds to the `supportsVideoMirroring` property of `AVCaptureConnection`
2025
var isVideoMirroringSupported: Bool { get }
2126

2227
/// Corresponds to the `supportsVideoOrientation` property of `AVCaptureConnection`
28+
@available(iOS, deprecated: 17.0, renamed: "isVideoRotationAngleSupported(_:)")
2329
var isVideoOrientationSupported: Bool { get }
2430

31+
/// Corresponds to the `isVideoRotationAngleSupported(_:)` method of `AVCaptureConnection`
32+
@available(iOS 17.0, *)
33+
func isVideoRotationAngleSupported(_ videoRotationAngle: CGFloat) -> Bool
34+
2535
/// Corresponds to the preferredVideoStabilizationMode property of `AVCaptureConnection`
2636
var preferredVideoStabilizationMode: AVCaptureVideoStabilizationMode { get set }
2737

‎packages/camera/camera_avfoundation/darwin/camera_avfoundation/Sources/camera_avfoundation/DefaultCamera.swift‎

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -873,13 +873,36 @@ final class DefaultCamera: NSObject, Camera {
873873
private func updateOrientation(
874874
_ orientation: UIDeviceOrientation, forCaptureOutput captureOutput: CaptureOutput
875875
) {
876-
if let connection = captureOutput.connection(with: .video),
877-
connection.isVideoOrientationSupported
878-
{
876+
guard let connection = captureOutput.connection(with: .video) else { return }
877+
878+
if #available(iOS 17.0, *) {
879+
let angle = videoRotationAngle(forDeviceOrientation: orientation)
880+
if connection.isVideoRotationAngleSupported(angle) {
881+
connection.videoRotationAngle = angle
882+
}
883+
} else if connection.isVideoOrientationSupported {
879884
connection.videoOrientation = videoOrientation(forDeviceOrientation: orientation)
880885
}
881886
}
882887

888+
/// Matches the `AVCaptureVideoOrientation` mapping.
889+
private func videoRotationAngle(forDeviceOrientation deviceOrientation: UIDeviceOrientation)
890+
-> CGFloat
891+
{
892+
switch deviceOrientation {
893+
case .portrait:
894+
return 90
895+
case .landscapeLeft:
896+
return 0
897+
case .landscapeRight:
898+
return 180
899+
case .portraitUpsideDown:
900+
return 270
901+
default:
902+
return 90
903+
}
904+
}
905+
883906
private func videoOrientation(forDeviceOrientation deviceOrientation: UIDeviceOrientation)
884907
-> AVCaptureVideoOrientation
885908
{
@@ -1232,8 +1255,15 @@ final class DefaultCamera: NSObject, Camera {
12321255
}
12331256

12341257
// Keep the same orientation the old connections had.
1235-
if let oldConnection = oldConnection, newConnection.isVideoOrientationSupported {
1236-
newConnection.videoOrientation = oldConnection.videoOrientation
1258+
if let oldConnection = oldConnection {
1259+
if #available(iOS 17.0, *) {
1260+
let angle = oldConnection.videoRotationAngle
1261+
if newConnection.isVideoRotationAngleSupported(angle) {
1262+
newConnection.videoRotationAngle = angle
1263+
}
1264+
} else if newConnection.isVideoOrientationSupported {
1265+
newConnection.videoOrientation = oldConnection.videoOrientation
1266+
}
12371267
}
12381268

12391269
// Add the new connections to the session.

‎packages/camera/camera_avfoundation/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: camera_avfoundation
22
description: iOS implementation of the camera plugin.
33
repository: https://github.com/flutter/packages/tree/main/packages/camera/camera_avfoundation
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
5-
version: 0.11.0+1
5+
version: 0.11.0+2
66

77
environment:
88
sdk: ^3.11.0

0 commit comments

Comments
 (0)