Skip to content

Conversation

@TheCodeTraveler
Copy link
Collaborator

Description of Change

This PR replaces #2634 by @zhitaop, focusing on the only the bug fixes.

This PR is built on top of the #2906 PR which has been broken out into a separate PR to make the review process easier. This PR should be merged only after #2906 has been merged.

From #2634 by @zhitaop:

Root cause :
In CameraProvider.windows.cs, only one instance of MediaCapture is instantiated when iterating through the videoCaptureSourceGroup. This causes only the first mediaCapture.InitializeCameraForCameraView() to be successful. Wrong camera meta data from the first camera, such as the supported resolutions, are added to the subsequent CameraInfos (i.e. all subsequent CameraInfo contains metadata from the first CameraInfo). This causes the following exception when the camera is being updated with incorrect resolution:

System.Runtime.InteropServices.COMException (0xC00D36B4): The data specified for the media type is invalid, inconsistent, or not supported by this object.
The data specified for the media type is invalid, inconsistent, or not supported by this object.
at CommunityToolkit.Maui.Core.CameraManager.PlatformUpdateResolution(Size resolution, CancellationToken token)

Code changes:

  • Instantiate new MediaCapture in each iteration and properly dispose it after use

  • Enhancement: refactor camera refresh logic

Root cause:
Multiple calls to CameraProvider.RefreshAvailableCameras() are scattered throughout the camera initialization code (i.e.in CameraManager.ConnectCamera, CameraManager.StartCameraPreview, CameraManager.UpdateResolution), which are unnecessary as the available cameras are unlikely to change during initialization, and also time consuming especially on Windows as it needs to initialize MediaCapture.

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 will remain in Draft until #2906 is merged.

@TheCodeTraveler TheCodeTraveler marked this pull request as ready for review October 14, 2025 20:30
@zhitaop
Copy link
Contributor

zhitaop commented Oct 15, 2025

@TheCodeTraveler Thanks for the update, the fix looks good.

That said, I think a few other changes from #2634 that didn’t make it into this PR are still worth including, as they bring measurable performance benefits:

  • Removing redundant RefreshAvailableCameras() calls in CameraManager and CameraViewHandler.
  • Introducing private PlatformRefreshAvailableCameras and refreshAvailableCamerasTask in CameraProvider.

These changes significantly reduce unnecessary refresh calls during camera initialization. For example, from my testing, when opening the CameraView page in the sample app, a breakpoint set in CameraProvider.Windows.RefreshAvailableCameras() is hit up to four times from the following call stacks:

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

2 out of the last 3 calls are redundant and affect performance, especially since RefreshAvailableCameras() on Windows is relatively expensive. Ideally, the refresh should only occur once during initialization.

The introduction of the private refreshAvailableCamerasTask and PlatformRefreshAvailableCameras helps prevent duplicate concurrent executions - 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 rather than executing another refresh.

In my testing, these changes reduce invocation of the refresh logic to one, without altering existing behaviour.
I’ve prepared a branch based on your PR with these changes applied: refresh-camera-refactor

It would be great if you could take a look - I believe merging these improvements would make the overall fix more robust and efficient.

@TheCodeTraveler
Copy link
Collaborator Author

Thanks @zhitaop! Yea, if you could submit a PR that improves performance without introducing an InitializeAsync() method, that'd be great 💯

@TheCodeTraveler TheCodeTraveler merged commit fb6e664 into main Oct 15, 2025
11 checks passed
@TheCodeTraveler TheCodeTraveler deleted the Fix-CameraView-App-crashes-when-switching-camera-on-Windows branch October 15, 2025 23:20
@github-actions github-actions bot locked and limited conversation to collaborators Oct 17, 2025
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.

[BUG] CameraView App crashes when switching camera on Windows

2 participants