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
+86
View File
@@ -0,0 +1,86 @@
using System.Collections.ObjectModel;
using System.Windows.Input;
using ytLive.Helpers;
using ytLive.Services;
namespace ytLive.ViewModels;
public class CameraPickerCandidate
{
public CameraDeviceInfo Device { get; }
public string Name => Device.DisplayName;
public CameraPickerCandidate(CameraDeviceInfo device)
{
Device = device;
}
}
public class CameraPickerViewModel : ViewModelBase
{
private readonly ICameraEnumerator _enumerator;
private CameraPickerCandidate? _selectedCamera;
private bool _isLoading = true;
public ObservableCollection<CameraPickerCandidate> Cameras { get; } = new();
public bool IsLoading
{
get => _isLoading;
private set => SetProperty(ref _isLoading, value);
}
public bool HasCameras => !IsLoading && Cameras.Count > 0;
public bool NoCameras => !IsLoading && Cameras.Count == 0;
public CameraPickerCandidate? SelectedCamera
{
get => _selectedCamera;
set
{
if (SetProperty(ref _selectedCamera, value))
CommandManager.InvalidateRequerySuggested();
}
}
public ICommand UseSelectedCommand { get; }
public ICommand CancelCommand { get; }
public event Action<CameraDeviceInfo>? UseRequested;
public event Action? CancelRequested;
public CameraPickerViewModel(ICameraEnumerator enumerator)
{
_enumerator = enumerator;
UseSelectedCommand = new RelayCommand(
_ => { if (SelectedCamera != null) UseRequested?.Invoke(SelectedCamera.Device); },
_ => SelectedCamera != null);
CancelCommand = new RelayCommand(_ => CancelRequested?.Invoke());
_ = LoadAsync();
}
private async Task LoadAsync()
{
IsLoading = true;
try
{
var devices = await _enumerator.GetCamerasAsync();
Cameras.Clear();
foreach (var device in devices)
Cameras.Add(new CameraPickerCandidate(device));
if (Cameras.Count > 0)
SelectedCamera = Cameras[0];
}
catch (Exception ex)
{
AppLog.Write($"CameraPickerViewModel: enumerating cameras failed: {ex.Message}");
}
finally
{
IsLoading = false;
OnPropertyChanged(nameof(HasCameras));
OnPropertyChanged(nameof(NoCameras));
CommandManager.InvalidateRequerySuggested();
}
}
}
+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));
+2 -1
View File
@@ -4,9 +4,10 @@ MVVM layer. See [`schema.md`](../schema.md) for the memory-map conventions.
| File | Purpose |
|------|---------|
| `MainViewModel.cs` | The app brain: scenes/sources collections + commands, stream state (`IsLive`/`IsOffline`/`IsConnected`), chat feed, overlays, layout save/open, **resolution dropdown** (`QualityOptions`, `SelectedQuality`, `ResolutionHelp`, `ApplyStreamQuality`) that computes the output rect over the 1920×1080 master (`OutputRectX/Y/W/H`, `DimRects`, `IsOutputCropped`, `ResolutionBadgeText`). Auth: loads the saved DPAPI session at startup (`LoadSavedSessionAsync`), `SignInAsync` feeds the GoLive dialog; **End Livestream signs out** (`StopStream` clears session + token — crash-safe) |
| `MainViewModel.cs` | The app brain: scenes/sources collections + commands, stream state (`IsLive`/`IsOffline`/`IsConnected`), chat feed, overlays, layout save/open, **resolution dropdown** (`QualityOptions`, `SelectedQuality`, `ResolutionHelp`, `ApplyStreamQuality`) that computes the output rect over the 1920×1080 master (`OutputRectX/Y/W/H`, `DimRects`, `IsOutputCropped`, `ResolutionBadgeText`). Auth: loads the saved DPAPI session at startup (`LoadSavedSessionAsync`), `SignInAsync` feeds the GoLive dialog; **End Livestream signs out** (`StopStream` clears session + token — crash-safe). **Webcam:** owns `CameraManager` (MediaCapture), `AddWebcamSourceAsync` (picker → default 480×270 bottom-right placement → acquire), one-camera app-wide (`CanAddWebcam` greys the menu), `ReacquireWebcam` after layout load, `OnCameraPreviewBitmapChanged` forwards the shared bitmap into `Source.VideoImageSource` |
| `GoLiveViewModel.cs` | Start Stream dialog: **account row** (saved channel shown with Change Account, or Sign in to YouTube; Start gated on `IsSignedIn`/`IsBusy`) + title/description/visibility, start/cancel requests |
| `ReuseImageViewModel.cs` | Add Image dialog: candidate list (`ReuseImageCandidate`), reuse/new/cancel |
| `CameraPickerViewModel.cs` | Add Webcam dialog: async camera list (loading / has / none states), `UseRequested(CameraDeviceInfo)`/`CancelRequested` |
Related: [`Models/index.md`](../Models/index.md) for the data; [`Services/index.md`](../Services/index.md)
for what the ViewModels drive; base class in [`Helpers/ViewModelBase.cs`](../Helpers/ViewModelBase.cs).