Skip to content

Conversation

@zhitaop
Copy link
Contributor

@zhitaop zhitaop commented Oct 16, 2025

Description of Change

Root cause

Multiple calls to CameraProvider.RefreshAvailableCameras() are scattered throughout the camera initialization code (i.e. in CameraManager and CameraViewHandler.), affecting performance. Ideally, the refresh should only occur once during initialization.

In Addition, developers may also call RefreshAvailableCameras() in the application code (e.g. in a ViewModel for the CameraView) at the same time when the camera view is initializing. This results in duplicate concurrent executions.

For example, when opening the CameraView page in the sample app, a breakpoint set in CameraProvider.RefreshAvailableCameras() is hit up to four times from the following call stacks:

  • CameraViewViewModel.RefreshCameras()
  • CameraManager.PlatformConnectCamera()
  • CameraManager.PlatformStartCameraPreview()
  • CameraViewHandler.ConnectHandler

This PR includes the following changes

  • Remove redundant RefreshAvailableCameras() calls in CameraManager and CameraViewHandler. Only call the function once in CameraManager.Shared.ConnectCamera() to ensure camera list is populated before connecting the camera.
  • Introduce private PlatformRefreshAvailableCameras and refreshAvailableCamerasTask in CameraProvider to prevent duplicate concurrent refreshes. If multiple refreshes are invoked around the same time (e.g., from the ViewModel and the internal camera initialization), subsequent calls simply await the existing task.
  • Some small changes in the sample app CameraViewPage. Removed unused code and add isInitialized guard to prevent refresh when navigated back from the photo page to the camera view page.

Linked Issues

PR Checklist

  • Has a linked Issue, and the Issue has been approved(bug) or Championed (feature/proposal)
  • Has tests (if omitted, state reason in description)
  • Has samples (if omitted, state reason in description)
  • Rebased on top of main at time of PR
  • Changes adhere to coding standard

Additional information

This PR has been tested on Windows. Number of invocation of the refresh logic is reduced to one, without altering existing behaviour.

* Add PlatformRefreshAvailableCameras

* Don't invoke refresh camera task when one is in progress

* Remove redundant refresh camera calls in CameraManager
Copilot AI review requested due to automatic review settings October 16, 2025 01:33
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR optimizes camera initialization performance by eliminating redundant calls to RefreshAvailableCameras() and implementing concurrent refresh protection. The optimization reduces multiple simultaneous camera refresh operations from up to four calls down to a single execution during initialization.

  • Consolidates camera refresh logic to occur only once during initialization in CameraManager.ConnectCamera()
  • Implements task-based deduplication in CameraProvider to prevent concurrent refresh operations
  • Removes redundant refresh calls from CameraViewHandler and platform-specific CameraManager implementations

Reviewed Changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
CameraView.shared.cs Simplifies error handling logic in GetAvailableCameras() method
CameraProvider.*.cs Changes RefreshAvailableCameras() from public to private platform implementation and adds shared task deduplication
CameraViewHandler.shared.cs Removes redundant camera refresh call from ConnectHandler()
CameraManager.*.cs Removes redundant refresh calls from platform implementations and consolidates initialization logic
CameraViewPage.xaml* Adds initialization guard to prevent unnecessary refresh operations in sample app

@zhitaop
Copy link
Contributor Author

zhitaop commented Oct 16, 2025

@TheCodeTraveler @bijington

As suggested in #2907, I've created this PR to improve the performance for refresh during camera initialization. Can you please take a look, thanks!

@zhitaop
Copy link
Contributor Author

zhitaop commented Oct 28, 2025

@ne0rrmatrix Thanks for the review, I've updated the code accordingly.

Copilot AI review requested due to automatic review settings November 13, 2025 04:42
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 14 out of 14 changed files in this pull request and generated 3 comments.

@zhitaop zhitaop requested a review from ne0rrmatrix November 14, 2025 00:07
@zhitaop
Copy link
Contributor Author

zhitaop commented Nov 18, 2025

@ne0rrmatrix @TheCodeTraveler Would love to get reviews/feedback on this PR and hopefully get it merged soon, to avoid further merge conflicts, thanks!

@TheCodeTraveler TheCodeTraveler added the breaking change This label is used for PRs that include a breaking change label Nov 18, 2025
…ctHandler(NativePlatformCameraPreviewView)`
…sync handling

Refactored CameraManager to use sealed classes and private partial platform methods, and improved resource disposal logic across all platforms. CameraProvider is now sealed, implements IDisposable, and uses a semaphore to synchronize RefreshAvailableCameras, which now returns Task instead of ValueTask. Updated ICameraProvider interface and related mocks to match the new async signature.
Copilot AI review requested due to automatic review settings November 18, 2025 19:10
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copilot AI review requested due to automatic review settings November 18, 2025 19:22
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@TheCodeTraveler TheCodeTraveler changed the title Improve camera refresh logic during initialization Improve CameraView Performance Nov 18, 2025
Copy link
Collaborator

@TheCodeTraveler TheCodeTraveler left a comment

Choose a reason for hiding this comment

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

Thanks @zhitaop!

I added a few tweaks to improve performance and avoid race conditions:

  • Added SemaphoreSlim to CameraProvider to avoid multi-threading race conditions from writing to refreshAvailableCamerasTask and reading from refreshAvailableCamerasTask simultaneously
  • Made CameraManager a sealed class to improve performance and simplified its Dispose pattern
  • Made CameraProvider a sealed class and implement Disposable to improve performance and memory management
  • Call CameraManager.Disconnect in CameraViewHandler.DisconnectHandler()
  • Call CameraProvider.Dispose in CameraViewHandler.Dispose

@TheCodeTraveler TheCodeTraveler added the approved This Proposal has been approved and is ready to be added to the Toolkit label Nov 18, 2025
Copilot AI review requested due to automatic review settings November 18, 2025 20:06
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

This reverts commit 9ede2ec.
@TheCodeTraveler TheCodeTraveler merged commit ac8b610 into CommunityToolkit:main Nov 18, 2025
20 of 27 checks passed
@zhitaop zhitaop mentioned this pull request Nov 19, 2025
6 tasks
@zhitaop
Copy link
Contributor Author

zhitaop commented Nov 19, 2025

@TheCodeTraveler Thanks for the improvements to the PR, those are definitely very necessary and useful enhancements!

However, I've identified 2 regressions that would throw uncaught exceptions and cause the app to crash during my testing of the sample app. As these regressions are very likely to be hit by the developers while using the camera view, I've opened a new PR to address them. Could you please have a look?

@github-actions github-actions bot locked and limited conversation to collaborators Nov 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

approved This Proposal has been approved and is ready to be added to the Toolkit breaking change This label is used for PRs that include a breaking change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] CameraView App crashes when switching camera on Windows

3 participants