-
Notifications
You must be signed in to change notification settings - Fork 492
CameraView fix for Android: video recording and rotation #3168
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 all commits
4f2cb9e
e492f10
7c3e712
033d587
e0b24fc
0a04a9d
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 | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -82,6 +82,9 @@ public void Dispose() | |||||||||||||||||||||||||||||||||||||||||||||||||
| videoCapture?.Dispose(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| videoCapture = null; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| videoRecorder?.Dispose(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| videoRecorder = null; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| imageCallback?.Dispose(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| imageCallback = null; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -117,7 +120,7 @@ public NativePlatformCameraPreviewView CreatePlatformView() | |||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| cameraExecutor = Executors.NewSingleThreadExecutor() ?? throw new CameraException($"Unable to retrieve {nameof(IExecutorService)}"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| orientationListener = new OrientationListener(SetImageCaptureTargetRotation, context); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| orientationListener = new OrientationListener(OnOrientationChanged, context); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| orientationListener.Enable(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| return previewView; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -292,6 +295,11 @@ private partial ValueTask PlatformTakePicture(CancellationToken token) | |||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| private async partial Task PlatformStartVideoRecording(Stream stream, CancellationToken token) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if (videoCapture is null || videoRecorder is null) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| await StartUseCase(token); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| if (previewView is null | ||||||||||||||||||||||||||||||||||||||||||||||||||
| || processCameraProvider is null | ||||||||||||||||||||||||||||||||||||||||||||||||||
| || cameraPreview is null | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -377,12 +385,6 @@ void CleanupVideoRecordingResources() | |||||||||||||||||||||||||||||||||||||||||||||||||
| videoRecordingFile = null; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| videoRecorder?.Dispose(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| videoRecorder = null; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| videoCapture?.Dispose(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| videoCapture = null; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| videoRecordingFinalizeTcs = null; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -425,20 +427,23 @@ async Task<ICamera> RebindCamera(ProcessCameraProvider provider, CameraInfo came | |||||||||||||||||||||||||||||||||||||||||||||||||
| return provider.BindToLifecycle((ILifecycleOwner)context, cameraSelector, useCases); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| void SetImageCaptureTargetRotation(int rotation) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| void OnOrientationChanged(int rotation) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if (imageCapture is not null) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| imageCapture.TargetRotation = rotation switch | ||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| >= 45 and < 135 => (int)SurfaceOrientation.Rotation270, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| >= 135 and < 225 => (int)SurfaceOrientation.Rotation180, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| >= 225 and < 315 => (int)SurfaceOrientation.Rotation90, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| _ => (int)SurfaceOrientation.Rotation0 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| var targetRotation = GetSurfaceRotation(rotation); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+430
to
+433
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| imageCapture?.TargetRotation = targetRotation; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| videoCapture?.TargetRotation = targetRotation; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| cameraPreview?.TargetRotation = targetRotation; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+434
to
+436
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| imageCapture?.TargetRotation = targetRotation; | |
| videoCapture?.TargetRotation = targetRotation; | |
| cameraPreview?.TargetRotation = targetRotation; | |
| try | |
| { | |
| if (imageCapture is not null && imageCapture.Handle != IntPtr.Zero) | |
| { | |
| imageCapture.TargetRotation = targetRotation; | |
| } | |
| if (videoCapture is not null && videoCapture.Handle != IntPtr.Zero) | |
| { | |
| videoCapture.TargetRotation = targetRotation; | |
| } | |
| if (cameraPreview is not null && cameraPreview.Handle != IntPtr.Zero) | |
| { | |
| cameraPreview.TargetRotation = targetRotation; | |
| } | |
| } | |
| catch (ObjectDisposedException) | |
| { | |
| // Ignore orientation updates for disposed camera use cases. | |
| } |
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.
PlatformStartVideoRecordingcan callStartUseCase(token)to recreateimageCapture/videoCapture/cameraPreview. Because target rotation is only applied fromOnOrientationChanged, newly recreated use cases will keep the defaultTargetRotationuntil the device orientation changes again (which may never happen), leading to incorrect rotation after a rebuild (e.g., after a resolution change). Consider applying an initialTargetRotationimmediately after rebuilding (e.g., frompreviewView.Display?.Rotation/ current display rotation, or cache the last computed target rotation and reapply it).