Skip to content

Commit 93eede7

Browse files
authored
[camera]fix a crash when switching camera while recording due to disconnected capturePhotoOputput (flutter#13248)
This PR fixes one of the test failures here flutter#191202. This failure is not iOS 27 specific. However, since it requires physical device (switch between front/back camera), it is not run on our CI, so we didn't catch it. ## How was it broken Before switching camera, the capture session has: ``` Inputs: [ backCameraInput ] Outputs: [ captureVideoOutput, capturePhotoOutput ] Connections: Wire 1: backCameraInput -> captureVideoOutput (created manually via addConnection) Wire 2: backCameraInput -> capturePhotoOutput (created automatically via addOutput) ``` When switching camera input (from back camera to front camera), we remove video input and output, but left photo output untouched. So at this point the capture session has: ``` Inputs: [ ] Outputs: [ capturePhotoOutput ] <-- still in the session, but unplugged! Connections: [ ] <-- both Wire 1 and Wire 2 were deleted! ``` Now when adding back the camera input, we simply do: ``` videoCaptureSession.addInputWithNoConnections(captureVideoInput) videoCaptureSession.addOutputWithNoConnections(captureVideoOutput.avOutput) ``` So our capture session ends up with: ``` Inputs: [ frontCameraInput ] Outputs: [ capturePhotoOutput, newVideoOutput ] Connections: Wire 1: frontCameraInput -> newVideoOutput Wire 2: (MISSING! capturePhotoOutput has no wire to frontCameraInput!) ``` Then a subsequent code calls ``` capturePhotoOutput.maxPhotoDimensions = maxSupportedDimensions ``` Which throws NSInvalidArgumentException because it has no connection. The fix is simply to use addInput rather than addInputWithNoConnections, which would automatically add the frontCameraInput -> capturePhotoOutput connection. Fixes flutter#194344 *List which issues are fixed by this PR. You must list at least one issue.* ## 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 2625b7f commit 93eede7

5 files changed

Lines changed: 108 additions & 11 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+3
2+
3+
* Fixes a crash when switching camera while recording video due to disconnected photo output.
4+
15
## 0.11.0+2
26

37
* Replaces the deprecated `videoOrientation` with `videoRotationAngle` on iOS 17 and later.

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

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ final class MockCaptureSession: NSObject, CaptureSession {
1717
var canSetSessionPresetStub: ((AVCaptureSession.Preset) -> Bool)?
1818

1919
var _sessionPreset = AVCaptureSession.Preset.high
20+
private(set) var captureInputs = [CaptureInput]()
2021
var inputs = [AVCaptureInput]()
2122
var outputs = [AVCaptureOutput]()
23+
private(set) var connections = [AVCaptureConnection]()
2224

2325
private(set) var addedAudioOutputCount: Int = 0
2426

@@ -54,24 +56,55 @@ final class MockCaptureSession: NSObject, CaptureSession {
5456
return canSetSessionPresetStub?(preset) ?? true
5557
}
5658

57-
func addInputWithNoConnections(_ input: CaptureInput) {}
59+
func addInputWithNoConnections(_ input: CaptureInput) {
60+
captureInputs.append(input)
61+
}
5862

59-
func addOutputWithNoConnections(_ output: AVCaptureOutput) {}
63+
func addOutputWithNoConnections(_ output: AVCaptureOutput) {
64+
outputs.append(output)
65+
}
6066

61-
func addConnection(_: AVCaptureConnection) {}
67+
func addConnection(_ connection: AVCaptureConnection) {
68+
connections.append(connection)
69+
}
6270

63-
func addInput(_: CaptureInput) {}
71+
func addInput(_ input: CaptureInput) {
72+
captureInputs.append(input)
73+
for output in outputs where !connections.contains(where: { $0.output === output }) {
74+
connections.append(AVCaptureConnection(inputPorts: input.ports, output: output))
75+
}
76+
}
6477

6578
func addOutput(_ output: AVCaptureOutput) {
66-
79+
outputs.append(output)
80+
if !captureInputs.isEmpty && !connections.contains(where: { $0.output === output }) {
81+
connections.append(
82+
AVCaptureConnection(inputPorts: captureInputs.flatMap(\.ports), output: output))
83+
}
6784
if output is AVCaptureAudioDataOutput {
6885
addedAudioOutputCount += 1
6986
}
7087
}
7188

72-
func removeInput(_: CaptureInput) {}
89+
func removeInput(_ input: CaptureInput) {
90+
captureInputs.removeAll { $0 === input }
91+
// In unit tests, `MockCaptureInput.ports` is empty because `AVCaptureInput.Port` cannot be
92+
// instantiated without a real hardware device.
93+
if captureInputs.isEmpty {
94+
connections.removeAll()
95+
} else {
96+
connections.removeAll { connection in
97+
connection.inputPorts.contains { port in
98+
input.ports.contains { $0 === port }
99+
}
100+
}
101+
}
102+
}
73103

74-
func removeOutput(_: AVCaptureOutput) {}
104+
func removeOutput(_ output: AVCaptureOutput) {
105+
outputs.removeAll { $0 === output }
106+
connections.removeAll { $0.output === output }
107+
}
75108

76109
func canAddInput(_: CaptureInput) -> Bool {
77110
return true
@@ -81,7 +114,8 @@ final class MockCaptureSession: NSObject, CaptureSession {
81114
return true
82115
}
83116

84-
func canAddConnection(_: AVCaptureConnection) -> Bool {
85-
return true
117+
func canAddConnection(_ connection: AVCaptureConnection) -> Bool {
118+
guard let output = connection.output else { return true }
119+
return !connections.contains(where: { $0.output === output })
86120
}
87121
}

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,4 +539,60 @@ final class CameraSampleBufferTests: XCTestCase {
539539

540540
waitForExpectations(timeout: 1.0, handler: nil)
541541
}
542+
543+
func testSetDescriptionWhileRecording_reconnectsBothOutputsAndUpdatesMaxPhotoDimensions()
544+
throws
545+
{
546+
guard #available(iOS 16.0, *) else {
547+
throw XCTSkip("maxPhotoDimensions requires iOS 16.")
548+
}
549+
550+
let inputMock = MockAssetWriterInput()
551+
let configuration = CameraTestUtils.createTestCameraConfiguration()
552+
configuration.mediaSettingsWrapper = FakeMediaSettingsAVWrapper(inputMock: inputMock)
553+
554+
let videoSessionMock = MockCaptureSession()
555+
videoSessionMock.canSetSessionPresetStub = { _ in true }
556+
configuration.videoCaptureSession = videoSessionMock
557+
558+
let initialDeviceMock = MockCaptureDevice()
559+
let newFormatMock = MockCaptureDeviceFormat()
560+
newFormatMock.supportedMaxPhotoDimensions = [
561+
CMVideoDimensions(width: 1920, height: 1080),
562+
CMVideoDimensions(width: 4032, height: 3024),
563+
]
564+
let newDeviceMock = MockCaptureDevice()
565+
newDeviceMock.activeFormatStub = { newFormatMock }
566+
567+
configuration.videoCaptureDeviceFactory = { name in
568+
return name == "new_camera" ? newDeviceMock : initialDeviceMock
569+
}
570+
571+
let camera = CameraTestUtils.createTestCamera(configuration)
572+
let mockPhotoOutput = MockCapturePhotoOutput()
573+
mockPhotoOutput.avOutput = camera.capturePhotoOutput.avOutput
574+
camera.capturePhotoOutput = mockPhotoOutput
575+
576+
camera.startVideoRecording(completion: { _ in }, messengerForStreaming: nil)
577+
578+
let expectation = self.expectation(description: "setDescriptionWhileRecording completed")
579+
camera.setDescriptionWhileRecording("new_camera") { result in
580+
let _ = self.assertSuccess(result)
581+
expectation.fulfill()
582+
}
583+
584+
waitForExpectations(timeout: 1.0, handler: nil)
585+
586+
XCTAssertEqual(videoSessionMock.connections.count, 2)
587+
XCTAssertTrue(
588+
videoSessionMock.connections.contains(where: {
589+
$0.output === camera.captureVideoOutput.avOutput
590+
}))
591+
XCTAssertTrue(
592+
videoSessionMock.connections.contains(where: {
593+
$0.output === mockPhotoOutput.avOutput
594+
}))
595+
XCTAssertEqual(mockPhotoOutput.maxPhotoDimensions.width, 4032)
596+
XCTAssertEqual(mockPhotoOutput.maxPhotoDimensions.height, 3024)
597+
}
542598
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1275,7 +1275,10 @@ final class DefaultCamera: NSObject, Camera {
12751275
message: "Unable to switch video input",
12761276
details: nil)))
12771277
}
1278-
videoCaptureSession.addInputWithNoConnections(captureVideoInput)
1278+
1279+
// Do not use addInputWithNoConnections. The session still has the disconnected
1280+
// capturePhotoOutput, and we need to reconnect it to the captureVideoInput.
1281+
videoCaptureSession.addInput(captureVideoInput)
12791282

12801283
if !videoCaptureSession.canAddOutput(captureVideoOutput.avOutput) {
12811284
completion(

‎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+2
5+
version: 0.11.0+3
66

77
environment:
88
sdk: ^3.11.0

0 commit comments

Comments
 (0)