Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
214 changes: 168 additions & 46 deletions src/CommunityToolkit.Maui.Camera/CameraManager.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,32 @@

namespace CommunityToolkit.Maui.Core;

public class CameraConsumer(TaskCompletionSource finalizeTcs) : Object, IConsumer
public class CameraConsumer : Object, IConsumer
{
readonly TaskCompletionSource? finalizeTcs = finalizeTcs;
readonly TaskCompletionSource? finalizeTcs;
readonly VideoRecordingState? recordingState;

public CameraConsumer(TaskCompletionSource finalizeTcs)
{
this.finalizeTcs = finalizeTcs;
}

internal CameraConsumer(VideoRecordingState recordingState)
{
this.recordingState = recordingState;
}

public void Accept(Object? videoRecordEvent)
{
if (videoRecordEvent is VideoRecordEvent.Finalize)
switch (videoRecordEvent)
{
finalizeTcs?.SetResult();
case VideoRecordEvent.Start:
recordingState?.OnStarted();
break;
case VideoRecordEvent.Finalize finalizeEvent:
recordingState?.OnFinalized(new CameraException($"Video recording finished before it started (CameraX error {finalizeEvent.Error})."));
finalizeTcs?.TrySetResult();
break;
}
}
}
Expand All @@ -51,7 +68,7 @@ partial class CameraManager
ResolutionFilter? resolutionFilter;
OrientationListener? orientationListener;
Java.IO.File? videoRecordingFile;
TaskCompletionSource? videoRecordingFinalizeTcs;
VideoRecordingState? videoRecordingState;
Stream? videoRecordingStream;
int extensionMode = ExtensionMode.Auto;
CaptureSessionMode currentSessionMode = CaptureSessionMode.Photo;
Expand Down Expand Up @@ -420,38 +437,60 @@ private async partial Task PlatformStartVideoRecording(Stream stream, Cancellati
|| processCameraProvider is null
|| cameraPreview is null
|| videoCapture is null
|| videoRecorder is null
|| videoRecordingFile is not null)
|| videoRecorder is null)
{
return;
}

videoRecordingStream = stream;
if (VideoRecordingState.TryStart(ref videoRecordingState) is not { } recordingState)
{
if (videoRecordingState is { } activeRecordingState)
{
await activeRecordingState.Started.WaitAsync(token);
}

cameraView.SelectedCamera ??= cameraProvider.AvailableCameras?.FirstOrDefault() ?? throw new CameraException("No camera available on device");
return;
}

// Rebuild the ImageCapture use case with the CaptureModeMinimizeLatency capture mode
// to optimize for latency during video recording sessions
currentSessionMode = CaptureSessionMode.Video;
RebuildImageCapture();
bool recordingRequested = false;

if (camera is null || !IsVideoSessionBound())
try
{
await BindVideoSessionAsync(token);
}
token.ThrowIfCancellationRequested();
videoRecordingStream = stream;

cameraView.SelectedCamera ??= cameraProvider.AvailableCameras?.FirstOrDefault() ?? throw new CameraException("No camera available on device");

// Rebuild the ImageCapture use case with the CaptureModeMinimizeLatency capture mode
// to optimize for latency during video recording sessions
currentSessionMode = CaptureSessionMode.Video;
RebuildImageCapture();

videoRecordingFile = new Java.IO.File(context.CacheDir, $"{DateTime.UtcNow.Ticks}.mp4");
videoRecordingFile.CreateNewFile();
if (camera is null || !IsVideoSessionBound())
{
await BindVideoSessionAsync(token);
}

var outputOptions = new FileOutputOptions.Builder(videoRecordingFile).Build();
ObjectDisposedException.ThrowIf(!ReferenceEquals(recordingState, videoRecordingState), this);

videoRecordingFinalizeTcs = new TaskCompletionSource();
var captureListener = new CameraConsumer(videoRecordingFinalizeTcs);
var executor = ContextCompat.GetMainExecutor(context) ?? throw new CameraException($"Unable to retrieve {nameof(IExecutorService)}");
videoRecording = videoRecorder
.PrepareRecording(context, outputOptions)
?.WithAudioEnabled()
.Start(executor, captureListener) ?? throw new InvalidOperationException("Unable to prepare recording");
videoRecordingFile = new Java.IO.File(context.CacheDir, $"{DateTime.UtcNow.Ticks}.mp4");
videoRecordingFile.CreateNewFile();
var outputOptions = new FileOutputOptions.Builder(videoRecordingFile).Build();
var captureListener = new CameraConsumer(recordingState);
var executor = ContextCompat.GetMainExecutor(context) ?? throw new CameraException($"Unable to retrieve {nameof(IExecutorService)}");
videoRecording = videoRecorder
.PrepareRecording(context, outputOptions)
?.WithAudioEnabled()
.Start(executor, captureListener) ?? throw new InvalidOperationException("Unable to prepare recording");
recordingRequested = true;

await recordingState.Started.WaitAsync(token);
}
catch
{
await CancelVideoRecordingStart(recordingState, recordingRequested, CancellationToken.None);
throw;
}

// `.PrepareRecording()` should never return null
// According to the Android docs, `Recorder.prepareRecording(Context, eMediaSoreOutputOptions)` returns a `NonNull` object
Expand All @@ -462,38 +501,112 @@ private async partial Task PlatformStartVideoRecording(Stream stream, Cancellati
private async partial Task<Stream> PlatformStopVideoRecording(CancellationToken token)
{
ArgumentNullException.ThrowIfNull(cameraExecutor);
if (videoRecording is null
|| videoRecordingFile is null
|| videoRecordingFinalizeTcs is null
|| videoRecordingStream is null)
if (videoRecordingState is not { } recordingState
|| !await recordingState.TryWaitForStartAsync(token))
{
return Stream.Null;
}

videoRecording.Stop();
await videoRecordingFinalizeTcs.Task.WaitAsync(token);
await recordingState.StopSemaphore.WaitAsync(token);
try
{
if (!ReferenceEquals(recordingState, videoRecordingState)
|| videoRecording is not { } recording
|| videoRecordingFile is not { } recordingFile
|| videoRecordingStream is not { } recordingStream)
{
return Stream.Null;
}

await using var inputStream = new FileStream(videoRecordingFile.AbsolutePath, FileMode.Open, FileAccess.Read, FileShare.Read);
await inputStream.CopyToAsync(videoRecordingStream, token);
await videoRecordingStream.FlushAsync(token);
CleanupVideoRecordingResources();
if (!recordingState.Finalized.IsCompleted)
{
recording.Stop();
}

// Rebuild the ImageCapture use case with the CaptureModeMaximizeQuality capture mode
// to optimize for quality during photo capture sessions
currentSessionMode = CaptureSessionMode.Photo;
RebuildImageCapture();
await BindPhotoSessionAsync(token);
await recordingState.Finalized.WaitAsync(token);
ObjectDisposedException.ThrowIf(!ReferenceEquals(recordingState, videoRecordingState), this);

await RestorePhotoSession(recordingState, token);
ObjectDisposedException.ThrowIf(!ReferenceEquals(recordingState, videoRecordingState), this);
token.ThrowIfCancellationRequested();

// Once export begins the recording cannot be retried without appending it twice, so any failure is terminal
try
{
await using var inputStream = new FileStream(recordingFile.AbsolutePath, FileMode.Open, FileAccess.Read, FileShare.Read);
await inputStream.CopyToAsync(recordingStream, token);
await recordingStream.FlushAsync(token);
if (recordingStream.CanSeek)
{
recordingStream.Position = 0;
}

ObjectDisposedException.ThrowIf(!ReferenceEquals(recordingState, videoRecordingState), this);
}
finally
{
CleanupVideoRecordingResources(recordingState);
}

if (videoRecordingStream.CanSeek)
return recordingStream;
}
finally
{
videoRecordingStream.Position = 0;
recordingState.StopSemaphore.Release();
}
}

return videoRecordingStream;
async Task CancelVideoRecordingStart(VideoRecordingState recordingState, bool recordingRequested, CancellationToken token)
{
await recordingState.StopSemaphore.WaitAsync(token);
try
{
if (!ReferenceEquals(recordingState, videoRecordingState))
{
return;
}

if (recordingRequested)
{
if (!recordingState.Finalized.IsCompleted)
{
videoRecording?.Stop();
}

await recordingState.Finalized.WaitAsync(token);
}

try
{
if (currentSessionMode is CaptureSessionMode.Video)
{
await RestorePhotoSession(recordingState, token);
}
}
finally
{
CleanupVideoRecordingResources(recordingState);
}
}
catch (System.Exception exception)
{
System.Diagnostics.Trace.WriteLine($"Unable to stop video recording after start failed: {exception}");
}
finally
{
recordingState.StopSemaphore.Release();
}
}

void CleanupVideoRecordingResources()
void CleanupVideoRecordingResources(VideoRecordingState? expectedState = null)
{
if (expectedState is not null && !ReferenceEquals(expectedState, videoRecordingState))
{
return;
}

videoRecordingState?.CancelPendingTasks();
Comment thread
austinbhale marked this conversation as resolved.
Resolved

videoRecording?.Dispose();
videoRecording = null;

Expand All @@ -508,7 +621,16 @@ void CleanupVideoRecordingResources()
videoRecordingFile = null;
}

videoRecordingFinalizeTcs = null;
videoRecordingState = null;
}

async Task RestorePhotoSession(VideoRecordingState recordingState, CancellationToken token)
{
ObjectDisposedException.ThrowIf(!ReferenceEquals(recordingState, videoRecordingState), this);

currentSessionMode = CaptureSessionMode.Photo;
RebuildImageCapture();
await BindPhotoSessionAsync(token);
}

async Task<CameraSelector> EnableModes(CameraInfo selectedCamera, CancellationToken token)
Expand Down
Loading
Loading