[BUG] CameraView App crashes when switching camera on Windows
Is there an existing issue for this?
- [x] I have searched the existing issues
Did you read the "Reporting a bug" section on Contributing file?
- [x] I have read the "Reporting a bug" section on Contributing file: https://github.com/CommunityToolkit/Maui/blob/main/CONTRIBUTING.md#reporting-a-bug
Current Behavior
Switching the camera between front and rear causes the app to crash on Windows. Works fine on Android and iOS.
Expected Behavior
Should be able to switch between the available camera on all platforms without any crashes.
Steps To Reproduce
- Open and run the solution on Windows.
- Click the "Switch Camera" Button
- Notice the app crash.
Link to public reproduction project repository
https://github.com/ruc12127/CameraViewWindowsCrash
Environment
- .NET MAUI CommunityToolkit.Maui.Camera: 2.0.2
- OS: Windows 10
- .NET MAUI: 9.0.30
Exception Details
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) in //src/CommunityToolkit.Maui.Camera/CameraManager.windows.cs:line 204 at CommunityToolkit.Maui.Core.CameraManager.<PlatformStartCameraPreview>d__21.MoveNext() in //src/CommunityToolkit.Maui.Camera/CameraManager.windows.cs:line 164 at CommunityToolkit.Maui.Core.CameraManager.UpdateCurrentCamera(CameraInfo cameraInfo, CancellationToken token) in //src/CommunityToolkit.Maui.Camera/CameraManager.shared.cs:line 82 at CommunityToolkit.Maui.Core.Handlers.CameraViewHandler.MapSelectedCamera(CameraViewHandler handler, ICameraView view) in //src/CommunityToolkit.Maui.Camera/Handlers/CameraViewHandler.shared.cs:line 158 at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_0(Object state) at Microsoft.UI.Dispatching.DispatcherQueueSynchronizationContext.<>c__DisplayClass2_0.<Post>b__0() --- End of stack trace from previous location --- at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|38_0(Int32 hr) at ABI.Windows.ApplicationModel.Core.IUnhandledErrorMethods.Propagate(IObjectReference _obj) at Microsoft.AppCenter.Utils.ApplicationLifecycleHelperWinUI.<.ctor>b__0_3(Object sender, UnhandledErrorDetectedEventArgs eventArgs) --- End of stack trace from previous location --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
I can also reproduce this issue on Microsoft Surface book 2 with the built-in front and rear cameras.
The root cause seems to be due to in CameraProvider.windows.cs:
https://github.com/CommunityToolkit/Maui/blob/2e624ca205d17351c17203c7c21347357d25d0ba/src/CommunityToolkit.Maui.Camera/Providers/CameraProvider.windows.cs#L17-L24
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. Therefore the above exception are thrown in CameraManager.PlatformUpdateResolution when trying to set the incorrect resolution.
PR created for this issue: https://github.com/CommunityToolkit/Maui/pull/2634