-
Notifications
You must be signed in to change notification settings - Fork 463
Improve camera refresh logic during initialization #2909
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
4713b19
e86b9ab
b9e9345
9cf7487
8ab0319
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -119,16 +119,6 @@ protected virtual void Dispose(bool disposing) | |||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| protected virtual async partial Task PlatformConnectCamera(CancellationToken token) | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| if (cameraProvider.AvailableCameras is null) | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| await cameraProvider.RefreshAvailableCameras(token); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (cameraProvider.AvailableCameras is null) | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| throw new CameraException("Unable to refresh cameras"); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| await StartCameraPreview(token); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
@@ -139,13 +129,9 @@ protected virtual async partial Task PlatformStartCameraPreview(CancellationToke | |||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| mediaCapture = new MediaCapture(); | ||||||||||||||||||||||||||
| cameraView.SelectedCamera ??= cameraProvider.AvailableCameras?.FirstOrDefault() ?? throw new CameraException("No camera available on device"); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (cameraView.SelectedCamera is null) | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| await cameraProvider.RefreshAvailableCameras(token); | ||||||||||||||||||||||||||
| cameraView.SelectedCamera = cameraProvider.AvailableCameras?.FirstOrDefault() ?? throw new CameraException("No camera available on device"); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| mediaCapture = new MediaCapture(); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| await mediaCapture.InitializeCameraForCameraView(cameraView.SelectedCamera.DeviceId, token); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
@@ -180,22 +166,17 @@ protected virtual partial void PlatformStopCameraPreview() | |||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| protected async Task PlatformUpdateResolution(Size resolution, CancellationToken token) | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| if (!IsInitialized || mediaCapture is null) | ||||||||||||||||||||||||||
| if (!IsInitialized || mediaCapture is null || cameraView.SelectedCamera is null) | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (cameraView.SelectedCamera is null) | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| await cameraProvider.RefreshAvailableCameras(token); | ||||||||||||||||||||||||||
| cameraView.SelectedCamera = cameraProvider.AvailableCameras?.FirstOrDefault() ?? throw new CameraException("No camera available on device"); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| var filteredPropertiesList = cameraView.SelectedCamera.ImageEncodingProperties.Where(p => p.Width <= resolution.Width && p.Height <= resolution.Height).ToList(); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| filteredPropertiesList = filteredPropertiesList.Count is not 0 | ||||||||||||||||||||||||||
| ? filteredPropertiesList | ||||||||||||||||||||||||||
| : [.. cameraView.SelectedCamera.ImageEncodingProperties.OrderByDescending(p => p.Width * p.Height)]; | ||||||||||||||||||||||||||
| if (filteredPropertiesList.Count is 0) | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| filteredPropertiesList = [.. cameraView.SelectedCamera.ImageEncodingProperties.OrderByDescending(p => p.Width * p.Height)]; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (filteredPropertiesList.Count is not 0) | ||||||||||||||||||||||||||
|
Comment on lines
+176
to
181
|
||||||||||||||||||||||||||
| if (filteredPropertiesList.Count is 0) | |
| { | |
| filteredPropertiesList = [.. cameraView.SelectedCamera.ImageEncodingProperties.OrderByDescending(p => p.Width * p.Height)]; | |
| } | |
| if (filteredPropertiesList.Count is not 0) | |
| if (filteredPropertiesList.Count == 0) | |
| { | |
| filteredPropertiesList = [.. cameraView.SelectedCamera.ImageEncodingProperties.OrderByDescending(p => p.Width * p.Height)]; | |
| } | |
| if (filteredPropertiesList.Count != 0) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,8 @@ | |
| partial class CameraProvider : ICameraProvider | ||
| { | ||
| readonly WeakEventManager availableCamerasChangedEventManager = new(); | ||
|
|
||
| Task? refreshAvailableCamerasTask; | ||
|
|
||
| public event EventHandler<IReadOnlyList<CameraInfo>?> AvailableCamerasChanged | ||
| { | ||
| add => availableCamerasChangedEventManager.AddEventHandler(value); | ||
|
|
@@ -22,13 +23,23 @@ private set | |
| if (!AreCameraInfoListsEqual(field, value)) | ||
| { | ||
| field = value; | ||
| availableCamerasChangedEventManager.HandleEvent(this, value, nameof(AvailableCamerasChanged)); | ||
| availableCamerasChangedEventManager.HandleEvent(this, value, nameof(AvailableCamerasChanged)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private partial ValueTask PlatformRefreshAvailableCameras(CancellationToken token); | ||
|
|
||
| /// <inheritdoc/> | ||
| public partial ValueTask RefreshAvailableCameras(CancellationToken token); | ||
| public async ValueTask RefreshAvailableCameras(CancellationToken token) | ||
| { | ||
| if (refreshAvailableCamerasTask is null || refreshAvailableCamerasTask.IsCompleted) | ||
| { | ||
| refreshAvailableCamerasTask = PlatformRefreshAvailableCameras(token).AsTask(); | ||
|
||
| } | ||
|
|
||
| await refreshAvailableCamerasTask; | ||
|
Comment on lines
+36
to
+41
|
||
| } | ||
|
|
||
| internal static bool AreCameraInfoListsEqual(in IReadOnlyList<CameraInfo>? cameraInfoList1, in IReadOnlyList<CameraInfo>? cameraInfoList2) | ||
| { | ||
|
|
@@ -47,4 +58,4 @@ internal static bool AreCameraInfoListsEqual(in IReadOnlyList<CameraInfo>? camer | |
|
|
||
| return cameraInfosInList1ButNotInList2.Count is 0 && cameraInfosInList2ButNotInList1.Count is 0; | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] This line combines null-conditional assignment, null-conditional access, null-coalescing, and exception throwing in a single statement, making it difficult to read and debug. Consider breaking this into multiple lines for better clarity.