Skip to content

Await native video recording start acknowledgment - #3311

Merged
TheCodeTraveler merged 6 commits into
CommunityToolkit:mainfrom
Nakamir-Code:upstream-camera-start-parity
Nov 4, 2026
Merged

TheCodeTraveler merged 6 commits into
CommunityToolkit:mainfrom
Nakamir-Code:upstream-camera-start-parity

Conversation

@austinbhale

@austinbhale austinbhale commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Description of Change

StartVideoRecording now completes only after the native recording pipeline acknowledges that recording has started:

  • Apple waits for AVCaptureFileOutputRecordingDelegate.DidStartRecording.
  • Android waits for CameraX VideoRecordEvent.Start.

Both platforms use the same internal start/finalize state transitions. A native finish event that arrives before start faults the start task, repeated callbacks complete exactly once, and cancellation stops and finalizes the pending native recording before operation-specific resources are cleaned up.

This is separate from #3309, which only fixed the Apple recording delegate lifetime. This branch is based on that merged commit and does not duplicate its delegate-retention change.

Physical iOS testing showed why the additional acknowledgment is needed: the previous API could return before DidStartRecording, allowing a rapid stop roughly 22 ms later and producing an empty recording. A private package build with native start acknowledgment improved repeated rapid Record/Stop testing.

Linked Issues

PR Checklist

Additional information

Validation:

  • Focused Camera tests: 14 passed
  • Physical iPhone + Android recording tested successfully.

Copilot AI lite review requested due to automatic review settings September 11, 2026 22:07

This comment was marked as outdated.

@austinbhale
austinbhale marked this pull request as draft September 11, 2026 22:32

This comment was marked as outdated.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Critical teardown races remain on both platforms, and async tests lack timeout and cancellation safeguards.

Get a fresh assessment by requesting another Copilot review.

Review details

Suppressed comments (1)

src/CommunityToolkit.Maui.UnitTests/Views/CameraView/VideoRecordingStateTests.cs:17

  • This test (and the other async tests in the new class) awaits a TaskCompletionSource directly without a test timeout or TestContext cancellation. If a transition regresses and the task remains pending, the test runner can hang instead of reporting a failure; add a short Fact timeout and use WaitAsync(TestContext.Current.CancellationToken), as in CameraViewTests.cs:80-91.
		await recordingState.Started;
  • Files reviewed: 4/4 changed files
  • Comments generated: 2
  • Review effort level: Lite

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The tests cannot compile without internals access, and both platform stop paths ignore cancellation during copying and flushing.

Get a fresh assessment by requesting another Copilot review.

Review details

Suppressed comments (2)

src/CommunityToolkit.Maui.Camera/CameraManager.android.cs:534

  • StopVideoRecording documents that its token cancels the stop operation, and the previous implementation passed that token into both the copy and flush. Using CancellationToken.None here means cancellation after native finalization is ignored while a potentially large recording is copied, so the stop task cannot be canceled during this phase. Pass token through to preserve the documented behavior.
				await inputStream.CopyToAsync(recordingStream, CancellationToken.None);
				await recordingStream.FlushAsync(CancellationToken.None);

src/CommunityToolkit.Maui.Camera/CameraManager.macios.cs:434

  • StopVideoRecording documents that its token cancels the stop operation, and the previous implementation passed that token into both the copy and flush. Using CancellationToken.None here means cancellation after native finalization is ignored while a potentially large recording is copied, so the stop task cannot be canceled during this phase. Pass token through to preserve the documented behavior.
				await inputStream.CopyToAsync(recordingStream, CancellationToken.None);
				await recordingStream.FlushAsync(CancellationToken.None);
  • Files reviewed: 4/4 changed files
  • Comments generated: 1
  • Review effort level: Lite

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Unresolved recording lifecycle issues allow premature stops during startup, prevent cancellation of destination writes, and leave recording state occupied after export failures.

5 open issues

Review effort: Max

@TheCodeTraveler

Copy link
Copy Markdown
Collaborator

Thanks @austinbhale! Could you please take a look at CoPilot's recommendations and either apply them or respond + resolve them if they're not relevant?

@TheCodeTraveler

Copy link
Copy Markdown
Collaborator

@austinbhale Please allow maintainers to edit this PR. I need to be able to update it from main and address CoPilot's reviews.

  1. In the right-hand sidebar of the PR page, scroll past the "Reviewers" and "Assignees" sections down to the Participants list.
  2. Toggle the permission: Check the box next to Allow edits from maintainers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@austinbhale

Copy link
Copy Markdown
Contributor Author

Hi @TheCodeTraveler - I responded to the current round of Copilot comments and requested another one. Due to an issue with GitHub, I can't enable "Allow edits from maintainers" because this PR comes from a fork owned by an organization. I'll keep the branch up to date on my end instead. Let me know if you'd like anything else changed!

This comment was marked as outdated.

@TheCodeTraveler

Copy link
Copy Markdown
Collaborator

I can't enable "Allow edits from maintainers"

No worries - thanks! Keep iterating over these CoPilot comments, then once its review comes back clean, we can merge.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Human assessment is needed because recording-start races can report success without native acknowledgment, while unbounded test waits can hang the serialized test run.

No open issues 🎉

2 resolved since last review

Review effort: Max

@TheCodeTraveler TheCodeTraveler left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks @austinbhale!! Approved.

The unit test that failed in the CI pipeline is on Popup, unrelated to this PR.

@TheCodeTraveler
TheCodeTraveler merged commit 36196ed into CommunityToolkit:main Nov 4, 2026
18 of 19 checks passed
TheCodeTraveler added a commit that referenced this pull request Nov 4, 2026
Conflicts in CameraManager.android.cs and CameraManager.macios.cs, resolved so that both sides keep their behavior (details in
merge-decisions.md of the evidence folder):
- #3311's VideoRecordingState (Started/Finalized/StopSemaphore) replaces videoRecordingFinalizeTcs; StartVideoRecording returns once the
  native start is acknowledged, StopVideoRecording returns Stream.Null when the start failed.
- A start that arrives while another start waits for its acknowledgement waits for it (#3311); a start after a started recording replaces
  it (this branch).
- A cancelled or failed start ends the native recording, releases the state once and returns to the photo session only if the session
  switched (this branch's cancellation, with #3311's CancelVideoRecordingStart).
- The export's finally cleans up first and then restores the photo session, so a failing restore cannot leave the recording state behind.
- Dispose cancels the state's pending tasks; a stop that waits wakes up and returns Stream.Null.
- Existing device tests follow the renamed private seam (videoRecordingState) and the awaited start (iOS stubs acknowledge the start).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
TheCodeTraveler added a commit that referenced this pull request Nov 4, 2026
… the release of the recording state

Covers #3311 on Android: StartVideoRecording returns once CameraX acknowledged the start, a concurrent start waits for it and
starts no second recording, StopVideoRecording after a failed start returns Stream.Null, a stop whose photo session can not be
restored still releases the recording so that the next start records, and a start after a cancelled stop records into its stream.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
TheCodeTraveler added a commit that referenced this pull request Nov 4, 2026
…d recording

The merge of main (01ea1a4) dropped #3311's token.ThrowIfCancellationRequested() before the export in StopVideoRecording on both
platforms. Without it, a stop whose token is cancelled once the recording finalized discards the recording in the export's finally,
and a retried StopVideoRecording returns Stream.Null; with it, the finalized recording stays for the retry, as #3311 intended.
Also: an Android comment now names the real risk of leftover recording state (a second export into the stream), and a new line
uses `is VideoRecordingState` instead of the empty property pattern.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 5, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants