Skip to content

[camera_avfoundation] Fix crash when restarting image stream after recording #9628

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

Closed

Conversation

t09tanaka
Copy link

Summary

Added a guard in captureOutput:didOutputSampleBuffer: to safely handle nil pixel buffers that occur when transitioning from recording to image streaming. This prevents app crashes caused by force unwrapping nil values.

What

  • Added guard let check for CMSampleBufferGetImageBuffer result in DefaultCamera.swift
  • Decrements pending frame count when skipping nil buffers to maintain accurate state
  • Added comprehensive test coverage for nil buffer scenarios

Why

When stopping video recording and immediately restarting the image stream, the app crashes with:

Thread 55: Fatal error: Unexpectedly found nil while unwrapping an Optional value

This occurs because:

  1. After recording stops, the recording format buffers continue to flow for several frames
  2. These buffers don't contain actual image data, causing CMSampleBufferGetImageBuffer to return NULL
  3. The force unwrap (\!) on the nil result causes an immediate crash

Technical Details

The issue happens during the transition period between recording and streaming:

  • Recording uses AVAssetWriter which has a specific buffer format
  • When transitioning back to streaming, there's a brief period where invalid buffers are received
  • These buffers have no image data, resulting in nil from CMSampleBufferGetImageBuffer

Note

Ideally, Flutter should be able to query buffer state before processing. However, this PR focuses solely on preventing the crash by safely handling nil buffers. Future improvements could include exposing buffer state information to the Flutter side for more sophisticated handling.

Test

Added StreamingNilBufferTests.swift with comprehensive tests for:

  • Handling nil image buffers during streaming
  • Mixed sequences of valid and nil buffers
  • Verifying frame count accuracy when buffers are skipped

🤖 Generated with Claude Code

- Guard against nil image buffer that can occur right after recording stops
- Decrement pending frame count when skipping nil buffers
- Add comprehensive test coverage for nil buffer handling

Fixes crash: "Thread 55: Fatal error: Unexpectedly found nil while unwrapping an Optional value"

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Copy link

google-cla bot commented Jul 15, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@stuartmorgan-g
Copy link
Contributor

🤖 Generated with Claude Code

While it's fine to use AI tools as part of developing a PR, we still require all PRs to follow our standard PR process. Please file a new PR whose description follows our template (including completing the checklist) rather than a completely AI-generated description that ignores our process, and we'll be happy to take a look. Thanks!

guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {
// Skip this frame if pixelBuffer is nil (can happen right after recording stops)
streamingPendingFramesCount -= 1
return
Copy link
Contributor

Choose a reason for hiding this comment

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

This early return isn't quite correct. While we should ignore non-image (usually audio) buffers for purposes of streaming, they should be recorded. I would expect that this change would cause videos recording with streaming to have no audio

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.

3 participants