TASK 3 milestone 1: webcam capture (MediaCapture CPU-first, CameraManager refcount, picker, clip/mirror, schema v2, tests)

This commit is contained in:
2026-08-06 14:35:34 -07:00
parent d97b5d375c
commit 98cdc4b3f4
23 changed files with 1099 additions and 40 deletions
+106 -1
View File
@@ -6,6 +6,7 @@ using System.IO;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using Microsoft.Win32;
using ytLive.Helpers;
@@ -56,6 +57,12 @@ public class MainViewModel : ViewModelBase
private DispatcherTimer? _saveDebounce;
private bool _isLoading;
// Webcam: one camera app-wide. The single Source is tracked here so the
// Add menu can be disabled and the live preview bitmap can be forwarded.
private readonly ICameraEnumerator _cameraEnumerator;
private readonly CameraManager _cameraManager;
private Source? _webcamSource;
// Branding flash (monetization): a full-frame "made with ytLlive!" shown
// for ~1s every 300s on the live output, ~25% opacity. Paid unlock sets
// BrandFlashEnabled = false. See ai.md "Monetization".
@@ -99,9 +106,19 @@ public class MainViewModel : ViewModelBase
public Source? SelectedSource
{
get => _selectedSource;
set => SetProperty(ref _selectedSource, value);
set
{
if (SetProperty(ref _selectedSource, value))
OnPropertyChanged(nameof(IsWebcamSelected));
}
}
/// <summary>The Add → Webcam menu item. One webcam app-wide — once one exists it's greyed out.</summary>
public bool CanAddWebcam => _webcamSource == null;
/// <summary>Shows the mirror / clip-shape row in the source chip when a webcam is selected.</summary>
public bool IsWebcamSelected => SelectedSource?.Type == SourceType.Webcam;
public StreamStatus StreamStatus
{
get => _streamStatus;
@@ -466,6 +483,14 @@ public class MainViewModel : ViewModelBase
_layoutStore = new LayoutStore(DefaultLayoutPath);
_activeLayoutPath = _layoutStore.ActivePath;
_cameraEnumerator = new MediaCaptureCameraEnumerator();
_cameraManager = new CameraManager(
_cameraEnumerator,
id => new MediaCaptureFrameSource(id),
System.Windows.Application.Current?.Dispatcher);
_cameraManager.PreviewBitmapChanged += OnCameraPreviewBitmapChanged;
LoadLayout();
_ = LoadSavedSessionAsync();
AppLog.Write("MainViewModel ctor end");
@@ -533,14 +558,41 @@ public class MainViewModel : ViewModelBase
}
ActiveScene = Scenes.FirstOrDefault();
UpdateActiveBackground();
ReacquireWebcam();
ScheduleSave();
AppLog.Write("LoadLayout end");
}
// Finds the persisted webcam source (at most one app-wide) and re-acquires
// its camera after a layout load / file open. Releasing the previous session
// unconditionally keeps the refcount honest even when the device is unchanged.
private void ReacquireWebcam()
{
var webcam = Scenes.SelectMany(s => s.Sources).FirstOrDefault(s => s.Type == SourceType.Webcam);
if (ReferenceEquals(_webcamSource, webcam)) return;
if (_webcamSource != null && !string.IsNullOrWhiteSpace(_webcamSource.DeviceId))
_ = _cameraManager.ReleaseAsync(_webcamSource.DeviceId);
_webcamSource = webcam;
OnPropertyChanged(nameof(CanAddWebcam));
if (webcam != null && !string.IsNullOrWhiteSpace(webcam.DeviceId))
_ = _cameraManager.AcquireAsync(webcam.DeviceId);
}
// CameraManager creates the shared WriteableBitmap on the UI thread at the
// device's frame size; the webcam Source's preview picks it up from here.
private void OnCameraPreviewBitmapChanged(string deviceId, WriteableBitmap bitmap)
{
if (_webcamSource?.DeviceId == deviceId)
_webcamSource.VideoImageSource = bitmap;
}
public void Shutdown()
{
_saveDebounce?.Stop();
SaveLayoutNow();
_cameraManager.Dispose();
_layoutStore.Dispose();
}
@@ -691,6 +743,12 @@ public class MainViewModel : ViewModelBase
_ => "Source",
};
if (sourceType == SourceType.Webcam)
{
_ = AddWebcamSourceAsync();
return;
}
if (sourceType == SourceType.Background)
{
var bytes = PickImageBytes("Choose a backdrop image");
@@ -722,6 +780,46 @@ public class MainViewModel : ViewModelBase
UpdateActiveBackground();
}
private async Task AddWebcamSourceAsync()
{
var scene = ActiveScene;
if (scene == null || _webcamSource != null) return;
var dialog = new CameraPickerDialog(new CameraPickerViewModel(_cameraEnumerator))
{
Owner = Application.Current.MainWindow
};
if (dialog.ShowDialog() != true || dialog.PickedDevice == null) return;
var device = dialog.PickedDevice;
var source = new Source
{
Name = "Webcam",
Type = SourceType.Webcam,
DeviceId = device.Id,
Width = 480,
Height = 270,
X = 1920 - 480 - 32,
Y = 1080 - 270 - 32,
};
_webcamSource = source;
OnPropertyChanged(nameof(CanAddWebcam));
scene.Sources.Add(source);
SelectedSource = source;
OnPropertyChanged(nameof(ShowEmptySceneHint));
OnPropertyChanged(nameof(ShowSourcesEmptyHint));
UpdateActiveBackground();
var started = await _cameraManager.AcquireAsync(device.Id);
if (!started)
{
MessageBox.Show(
"Couldn't start that camera. It may be in use by another app, or Windows camera access may be turned off.",
"ytLlive", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
private void AddImage()
{
var scene = ActiveScene;
@@ -842,6 +940,13 @@ public class MainViewModel : ViewModelBase
{
var scene = ActiveScene;
if (scene == null || source == null) return;
if (source.Type == SourceType.Webcam && ReferenceEquals(source, _webcamSource))
{
_webcamSource = null;
OnPropertyChanged(nameof(CanAddWebcam));
if (!string.IsNullOrWhiteSpace(source.DeviceId))
_ = _cameraManager.ReleaseAsync(source.DeviceId);
}
scene.Sources.Remove(source);
OnPropertyChanged(nameof(ShowEmptySceneHint));
OnPropertyChanged(nameof(ShowSourcesEmptyHint));