Skip to content

Fix CameraView crash when switching camera on Windows and other CameraView enhancements #2634

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 3 commits into
base: main
Choose a base branch
from

Conversation

zhitaop
Copy link
Contributor

@zhitaop zhitaop commented Apr 17, 2025

Description of Change

  • Fixes [BUG] CameraView App crashes when switching camera on Windows #2559: 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.

    • Proposed solution: instantiate new MediaCapture in each iteration and properly dispose it after use
  • Fixes [BUG] SIGABRT on iOS when Permissions.Camera not Granted #2519:

    As per [BUG] SIGABRT on iOS when Permissions.Camera not Granted #2519 (comment):

    CameraViewHandler.ConnectHandler doesn't actually check the result of the permission request

    • Proposed solution: bubble-up a PermissionException and let consumers respond
  • Also found that unnecessary calls to CameraProvider.RefreshAvailableCameras() are scattered throughout the code, which is a time consuming operation especially on Windows. IMO this call is only necessary when the CameraProvider is initialized, or when consumer of the API knows that available cameras on device has been updated (e.g. external camera has been plugged in after the initialization)

    • Proposed solution: add InitializeAsync task in CameraProvider, which is automatically started to initialize the camera list, and can be awaited by the consumer. (This might need more consideration as it change the API ?)
    • Remove all other RefreshAvailableCameras() calls throughout CameraManager
    • Updated inline documentation for to better explain purposes of each function in CameraProvider. This could potential be the solution to [BUG] CameraView on Android not recognizing external cameras #2272?
  • Also fixes a few issues in CameraViewPage and CameraViewViewModel:

    • Add camera switcher on the camera view page
    • Make Cameras observable in the view model, and make sure it's assigned after the CameraProvider is initialized so that camera list is not empty.

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
  • Documentation created or updated: https://github.com/MicrosoftDocs/CommunityToolkit/pulls

Additional information

* Add automatic initialization in CameraProvider
* Remove unnecessary refresh cameras calls in CameraManager
* Add camera switcher in camera sample page
@VR-Architect
Copy link

Any guess when this will deploy to main? Hoping it fixes some issues I have with Windows camera.

@bijington
Copy link
Contributor

Any guess when this will deploy to main? Hoping it fixes some issues I have with Windows camera.

Perhaps you could help us and test this branch on your machine to see if it helps?

@VR-Architect
Copy link

Any guess when this will deploy to main? Hoping it fixes some issues I have with Windows camera.

Perhaps you could help us and test this branch on your machine to see if it helps?

I would love to help but don't know how do download the branch to test. Let me Google it.

* Unbind ProcessCameraProvider when dispose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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 [BUG] SIGABRT on iOS when Permissions.Camera not Granted
4 participants