Skip to content

[camera_avfoundation] Fix crash when streaming while recording #9691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

RobertOdrowaz
Copy link
Contributor

In captureOutput, we receive both pixel and audio buffers (audio only during recording with audio enabled, when streaming alone is enabled, everything works correctly). Currently, the streaming part of this method doesn't handle non-pixel buffers properly, causing a crash because of force unwrapping of nil.

This PR changes the logic to ignore non-pixel buffers for purposes of streaming.

Resolves flutter/flutter#172894.

Pre-Review Checklist

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-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.

Footnotes

  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. 2 3

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request fixes a crash that occurs when streaming is enabled while recording video. The crash was caused by force-unwrapping a nil value when trying to get a pixel buffer from an audio sample buffer. The fix correctly handles this by using optional binding to check if a pixel buffer exists before processing it. The change is accompanied by a new unit test that verifies audio buffers are ignored during image streaming. My feedback includes a suggestion to also handle a potential nil value for planeAddress to prevent another potential crash.

Comment on lines 797 to 806
let length = bytesPerRow * height
let bytes = Data(bytes: planeAddress!, count: length)

let planeBuffer: [String: Any] = [
"bytesPerRow": bytesPerRow,
"width": width,
"height": height,
"bytes": FlutterStandardTypedData(bytes: bytes),
]
planes.append(planeBuffer)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

There's a potential crash here if planeAddress is nil. The ! force-unwraps it when creating Data. It's safer to use optional binding (if let or guard let) to ensure planeAddress is not nil before using it.

            if let planeAddress = planeAddress {
              let length = bytesPerRow * height
              let bytes = Data(bytes: planeAddress, count: length)

              let planeBuffer: [String: Any] = [
                "bytesPerRow": bytesPerRow,
                "width": width,
                "height": height,
                "bytes": FlutterStandardTypedData(bytes: bytes),
              ]
              planes.append(planeBuffer)
            }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only case in which we should get nil planeAddress is if we use incorrect function CVPixelBufferGetBaseAddress or CVPixelBufferGetBaseAddressOfPlane for a given buffer. We are checking the whether buffer is planar so this should never happen. I don't think failing silently is necessarily better in this case

bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer)
height = CVPixelBufferGetHeight(pixelBuffer)
width = CVPixelBufferGetWidth(pixelBuffer)
if let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you use guard here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We couldn't use guard there because this method also handled recording later, so an early return would break audio recording. I've extracted the whole streaming part to handleSampleBufferStreaming and replaced all nested ifs with guards. I left the pixel buffer guard separate so that I can comment on it

kaciula added a commit to kaciula/packages that referenced this pull request Aug 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[camera_avfoundation] Crash when streaming while recording
2 participants