Repository navigation
[camera]fix a crash when switching camera while recording due to disconnected capturePhotoOputput - #13248
Conversation
There was a problem hiding this comment.
Code Review
This pull request addresses a crash when switching cameras during video recording by ensuring the photo output is reconnected. Specifically, DefaultCamera.swift is updated to use addInput instead of addInputWithNoConnections when switching video inputs. To support this, mock classes for the capture session and photo output are enhanced, and a new test is added to verify the reconnection and updating of photo dimensions. The review feedback points out a missing return statement in the error handling blocks of DefaultCamera.swift which could trigger multiple completion calls, and suggests refining the mock removeInput implementation to only remove connections associated with the removed input.
| } | ||
| videoCaptureSession.addInputWithNoConnections(captureVideoInput) | ||
|
|
||
| // Do not use addInputWithNoConnections. The session still has the disconnected | ||
| // capturePhotoOutput, and we need to reconnect it to the captureVideoInput. | ||
| videoCaptureSession.addInput(captureVideoInput) |
There was a problem hiding this comment.
There is a missing return statement inside the if !videoCaptureSession.canAddInput(captureVideoInput) block. Without an early return, the method will continue executing, attempting to add the input and eventually calling completion(.success(())) even after a failure has already been reported. This violates the platform channel protocol (calling the completion handler multiple times) and can lead to unexpected behavior or crashes.
Please also apply this fix to the subsequent canAddOutput and canAddConnection checks in this method, as they suffer from the same missing return issue.
| } | |
| videoCaptureSession.addInputWithNoConnections(captureVideoInput) | |
| // Do not use addInputWithNoConnections. The session still has the disconnected | |
| // capturePhotoOutput, and we need to reconnect it to the captureVideoInput. | |
| videoCaptureSession.addInput(captureVideoInput) | |
| return | |
| } | |
| // Do not use addInputWithNoConnections. The session still has the disconnected | |
| // capturePhotoOutput, and we need to reconnect it to the captureVideoInput. | |
| videoCaptureSession.addInput(captureVideoInput) |
There was a problem hiding this comment.
Good find! This is an existing bug, so filed flutter/flutter#194348
There was a problem hiding this comment.
Also simply adding the return doesn't balance out the beginConfiguration() call, so the suggestion is wrong.
There was a problem hiding this comment.
There was a problem hiding this comment.
Code Review
This pull request modifies DefaultCamera.swift to use addInput instead of addInputWithNoConnections when switching video inputs during recording, ensuring the disconnected photo output is reconnected. It also enhances mock classes (MockCapturePhotoOutput, MockCaptureSession) and adds a unit test to verify this behavior. Feedback points out a missing return statement in the error handling block for canAddInput (and potentially subsequent checks) in DefaultCamera.swift, which could lead to multiple completion calls and crashes.
b69597f to
be090e5
Compare
be090e5 to
e7f84c4
Compare
1859f73 to
ed63b2d
Compare
flutter/packages@da40eaf...93eede7 2026-10-16 [email protected] [camera]fix a crash when switching camera while recording due to disconnected capturePhotoOputput (flutter/packages#13248) 2026-10-16 [email protected] [various] Update iOS example apps to the 15.0 deployment target (flutter/packages#13223) 2026-10-16 [email protected] [material_ui] Add M3E `IconButton` example (flutter/packages#13085) 2026-10-15 [email protected] [ci] Increase deprecation check minimum to iOS 16 (flutter/packages#13211) 2026-10-15 [email protected] [google_sign_in] Remove the public iOS handleOpen helper (flutter/packages#13273) 2026-10-15 [email protected] [google_fonts] Make the pendingFonts test deterministic (flutter/packages#13262) 2026-10-15 [email protected] [vector_graphics_compiler] Fix tspan transform applied twice (flutter/packages#13129) 2026-10-15 [email protected] Roll Flutter from 426be96 to 5c7e45a (21 revisions) (flutter/packages#13270) 2026-10-15 [email protected] [flutter_svg] Add golden widget tests for SVG alpha masks (flutter/packages#13245) If this roll has caused a breakage, revert this CL and set the roller to dry run mode using the controls here: https://autoroll.skia.org/r/flutter-packages-flutter-autoroll Please CC [email protected] on the revert to ensure that a human is aware of the problem. To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose To report a problem with the AutoRoller itself, please file a bug: https://issues.skia.org/issues/new?component=1389291&template=1850622 Documentation for the AutoRoller is here: https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
flutter/packages@da40eaf...93eede7 2026-10-16 [email protected] [camera]fix a crash when switching camera while recording due to disconnected capturePhotoOputput (flutter/packages#13248) 2026-10-16 [email protected] [various] Update iOS example apps to the 15.0 deployment target (flutter/packages#13223) 2026-10-16 [email protected] [material_ui] Add M3E `IconButton` example (flutter/packages#13085) 2026-10-15 [email protected] [ci] Increase deprecation check minimum to iOS 16 (flutter/packages#13211) 2026-10-15 [email protected] [google_sign_in] Remove the public iOS handleOpen helper (flutter/packages#13273) 2026-10-15 [email protected] [google_fonts] Make the pendingFonts test deterministic (flutter/packages#13262) 2026-10-15 [email protected] [vector_graphics_compiler] Fix tspan transform applied twice (flutter/packages#13129) 2026-10-15 [email protected] Roll Flutter from 426be96 to 5c7e45a (21 revisions) (flutter/packages#13270) 2026-10-15 [email protected] [flutter_svg] Add golden widget tests for SVG alpha masks (flutter/packages#13245) If this roll has caused a breakage, revert this CL and set the roller to dry run mode using the controls here: https://autoroll.skia.org/r/flutter-packages-flutter-autoroll Please CC [email protected] on the revert to ensure that a human is aware of the problem. To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose To report a problem with the AutoRoller itself, please file a bug: https://issues.skia.org/issues/new?component=1389291&template=1850622 Documentation for the AutoRoller is here: https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md

This PR fixes one of the test failures here flutter/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:
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:
Now when adding back the camera input, we simply do:
So our capture session ends up with:
Then a subsequent code calls
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/flutter#194344
List which issues are fixed by this PR. You must list at least one issue.
Pre-Review Checklist
[shared_preferences]///).If you need help, consider asking for advice on the #hackers-new channel on Discord.
Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the
gemini-code-assistbot 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.Footnotes
Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. ↩ ↩2