TASK 64 slice F — the two things left after the feature worked by hand. - App Settings → **Video clips → Auto Green/Blue Screen** (off by default). When on, a high-confidence detected screen is keyed as soon as the clip is added; the panel says "Removed automatically" so an automatic change is never silent. Medium confidence still asks, low says nothing — a wrong automatic key eats the picture. - **A clip is a clip:** the alert box's own replacement clip keys through the same settings block (one alert box per layout, so its settings are the ones the next alert uses), and choosing an alert clip runs the same detector. The Green/Blue Screen panel now appears for a video layer *or* an alert box with a custom clip (`Source.ShowsChromaKey`). - Spill ships as the creator-facing **Fringe** switch; nothing further to tune behind it. Test: `ChromaKeyApplyTests` gains the auto-apply case (no click, still explicit in the panel) and an alert-clip case (its pipe asks ffmpeg for colorkey+despill with the blue screen type). Docs: TASK 64 complete in TASKS.md, known limits recorded in the task file, HANDOFF rewritten. verify.sh: 546 passed, 0 failed, 0 warnings.
234 lines
9.2 KiB
C#
234 lines
9.2 KiB
C#
using System;
|
|
using System.Collections.ObjectModel;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Windows;
|
|
using Microsoft.Win32;
|
|
using ytLive.Helpers;
|
|
using ytLive.Models;
|
|
using ytLive.Services;
|
|
using ytLive.Services.Encoder;
|
|
|
|
namespace ytLive.ViewModels;
|
|
|
|
public partial class MainViewModel : ViewModelBase
|
|
{
|
|
|
|
private readonly ChatOverlayLayer _chatLayer;
|
|
private readonly AlertOverlayLayer _alertLayer;
|
|
|
|
/// <summary>The live chat message buffer — the same collection the
|
|
/// <see cref="ChatOverlayLayer"/> owns; here so XAML can bind it and the
|
|
/// left panel can subscribe to its changes.</summary>
|
|
public ObservableCollection<ChatMessage> ChatMessages => _chatLayer.Messages;
|
|
|
|
private ChatMessage? _pinnedChatMessage;
|
|
public ChatMessage? PinnedChatMessage
|
|
{
|
|
get => _pinnedChatMessage;
|
|
set => SetProperty(ref _pinnedChatMessage, value);
|
|
}
|
|
|
|
public RelayCommand<ChatMessage> PinChatMessageCommand => new((m) => { PinnedChatMessage = m; });
|
|
public RelayCommand ClearPinnedChatMessageCommand => new(_ => { PinnedChatMessage = null; });
|
|
|
|
/// <summary>The Add → YouTube Chat menu item: enabled only while no chat layer exists
|
|
/// anywhere — one chat overlay per layout.</summary>
|
|
public bool CanAddYouTubeChat
|
|
=> StagedScene != null
|
|
&& !Scenes.Any(s => s.Elements.OfType<Source>().Any(e => e.Type == SourceType.ChatBox));
|
|
|
|
/// <summary>The Add → Stream Alerts menu item: one alert zone per layout
|
|
/// (multiple zones would double-play every event).</summary>
|
|
public bool CanAddAlerts
|
|
=> StagedScene != null
|
|
&& !Scenes.Any(s => s.Elements.OfType<Source>().Any(e => e.Type == SourceType.AlertBox));
|
|
|
|
/// <summary>A recent-alert ticker is an independent layer, one per scene.</summary>
|
|
public bool CanAddTicker
|
|
=> StagedScene != null
|
|
&& !StagedScene.Elements.OfType<Source>().Any(e => e.Type == SourceType.Ticker);
|
|
|
|
/// <summary>Privacy blur is a Live-scene tool only (creator ruling 2026-11-01) — every other
|
|
/// scene's art is authored, and there is no desktop there to hide.</summary>
|
|
public bool CanAddBlur
|
|
=> StagedScene != null && SceneCatalog.Is(StagedScene.Name, SceneCatalog.Live);
|
|
|
|
public bool ShowChatInactiveMessage => !IsLive;
|
|
|
|
private void OnChatMessageReceived(ChatMessage message)
|
|
{
|
|
_chatLayer.OnMessageReceived(message, Scenes);
|
|
_alertLayer.OnMessageReceived(message, Scenes);
|
|
// Feed the session tallies LAST: the scoreboard counts what the poll decoded, and it only
|
|
// counts while a session is running (ScoreboardRecorder ignores everything else).
|
|
_scoreboard.Record(message);
|
|
}
|
|
|
|
private void UpdateChatBoxPreview(Source chatBox, IReadOnlyList<ChatMessage>? messages = null)
|
|
=> _chatLayer.UpdatePreview(chatBox, messages);
|
|
|
|
private VideoFrame? RenderChatBox(Source chatBox)
|
|
=> _chatLayer.RenderFrame(chatBox);
|
|
|
|
private void UpdateAlertBoxPreview(Source alertBox)
|
|
=> _alertLayer.UpdatePreview(alertBox);
|
|
|
|
private VideoFrame? RenderAlertBox(Source alertBox)
|
|
=> _alertLayer.RenderFrame(alertBox);
|
|
|
|
private void StartMockChatPreview()
|
|
=> _chatLayer.StartMockPreview(Scenes);
|
|
|
|
private void StopMockChatPreview()
|
|
=> _chatLayer.StopMockPreview();
|
|
|
|
private void UpdateActiveChatBoxPreviews()
|
|
=> _chatLayer.UpdateConfiguredBoxes(Scenes);
|
|
|
|
private void UpdateActiveAlertBoxPreviews()
|
|
=> _alertLayer.UpdateConfiguredBoxes(Scenes);
|
|
|
|
// ─── TASK 47: the alert clip (video) ───
|
|
|
|
private string? _alertDefaultVideoId;
|
|
private string? _defaultAlertClipScratch;
|
|
|
|
/// <summary>Runtime clip resolution for one alert box: the Creator's OWN video
|
|
/// is a plain path on disk (never stored — the 2026-09-26 decision); the shipped
|
|
/// llama placeholder lives as a BLOB in the internal Asset table and is
|
|
/// materialized to a temp file once. Null → the six celebratory animations.
|
|
/// Called from the output pump's alert frame path.</summary>
|
|
private string? ResolveAlertClipPath(Source box)
|
|
{
|
|
if (!box.AlertUseDefaultVideo
|
|
&& !string.IsNullOrWhiteSpace(box.AlertVideoPath)
|
|
&& File.Exists(box.AlertVideoPath))
|
|
return box.AlertVideoPath;
|
|
return MaterializeDefaultAlertClip();
|
|
}
|
|
|
|
private string? MaterializeDefaultAlertClip()
|
|
{
|
|
if (_defaultAlertClipScratch != null && File.Exists(_defaultAlertClipScratch))
|
|
return _defaultAlertClipScratch;
|
|
_alertDefaultVideoId ??= _layoutStore.GetAlertDefaultVideoId();
|
|
if (_alertDefaultVideoId == null) return null;
|
|
var bytes = _layoutStore.GetAssetBytes(_alertDefaultVideoId);
|
|
if (bytes == null) return null;
|
|
var scratch = Path.Combine(Path.GetTempPath(), $"ytLive-alert-{_alertDefaultVideoId}.mp4");
|
|
try
|
|
{
|
|
File.WriteAllBytes(scratch, bytes);
|
|
_defaultAlertClipScratch = scratch;
|
|
return scratch;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
AppLog.Write(ex, "Alert default materialization failed");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>One decoder per play (the refcounted MediaVideoSourceManager sessions
|
|
/// can't restart per replay). The clip is full-box in video + 48k stereo audio;
|
|
/// both steams pace themselves to real time inside the decoder — the frame-rate
|
|
/// probe is what makes the VIDEO pace (without it every frame dumps in ~1-2s and
|
|
/// the box freezes on the last frame for the rest of the audio).</summary>
|
|
private IAlertClipDecoder AlertClipDecoderFor(string path, int width, int height) =>
|
|
new AlertClipDecoder(
|
|
path, width, height,
|
|
new FfmpegLocator(),
|
|
() => new FfmpegDecodeProcess(),
|
|
frameRateProbe: new FfmpegFrameRateProbe(new FfmpegLocator(), () => new FfmpegDecodeProcess()),
|
|
// TASK 64: an alert clip is a clip — it keys through the same settings block. The
|
|
// layout has one alert box, so its settings are the ones the next alert uses.
|
|
chromaKey: AlertBoxChromaKey());
|
|
|
|
/// <summary>Stamp the shipped llama placeholder into the Asset table at startup
|
|
/// (idempotent — hash-keyed UpsertAsset dedupes). Best-effort: if it fails the
|
|
/// alert box simply falls back to the six animations.</summary>
|
|
private void StampDefaultAlertVideo()
|
|
{
|
|
try
|
|
{
|
|
_alertDefaultVideoId = _layoutStore.GetAlertDefaultVideoId();
|
|
if (!string.IsNullOrWhiteSpace(_alertDefaultVideoId)) return;
|
|
var bytes = ReadAlertDefaultVideoResource();
|
|
if (bytes is null or { Length: 0 }) return;
|
|
_alertDefaultVideoId = _layoutStore.EnsureAlertDefaultVideo(bytes);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
AppLog.Write(ex, "Alert default video stamp failed");
|
|
}
|
|
}
|
|
|
|
private static byte[]? ReadAlertDefaultVideoResource()
|
|
{
|
|
try
|
|
{
|
|
var uri = new Uri("pack://application:,,,/Assets/alert-default.mp4", UriKind.Absolute);
|
|
using var stream = Application.GetResourceStream(uri)?.Stream;
|
|
if (stream == null) return null;
|
|
using var ms = new MemoryStream();
|
|
stream.CopyTo(ms);
|
|
return ms.ToArray();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
AppLog.Write(ex, "Alert default video resource missing");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>The alert zone's own green/blue screen settings (one alert box per layout).</summary>
|
|
private ChromaKeyOptions? AlertBoxChromaKey()
|
|
=> Scenes.SelectMany(scene => scene.Elements).OfType<Source>()
|
|
.FirstOrDefault(source => source.IsAlertBox)?.ChromaKey is { Enabled: true } key
|
|
? key
|
|
: null;
|
|
|
|
private void BrowseAlertVideo(Source? box)
|
|
{
|
|
if (box == null || box.Type != SourceType.AlertBox) return;
|
|
var dialog = new OpenFileDialog
|
|
{
|
|
Title = "Choose the alert clip (mp4)",
|
|
Filter = "Video files (*.mp4;*.mov;*.webm;*.mkv)|*.mp4;*.mov;*.webm;*.mkv|All files (*.*)|*.*"
|
|
};
|
|
if (dialog.ShowDialog() != true) return;
|
|
box.AlertVideoPath = dialog.FileName;
|
|
box.AlertUseDefaultVideo = false;
|
|
// Offer the key for the chosen clip (same detector as a media layer).
|
|
_ = SuggestChromaKeyAsync(box);
|
|
}
|
|
|
|
private void BrowseAlertAudio(Source? box)
|
|
{
|
|
if (box == null || box.Type != SourceType.AlertBox) return;
|
|
var dialog = new OpenFileDialog
|
|
{
|
|
Title = "Choose the alert sound byte",
|
|
Filter = "Audio files (*.mp3;*.wav;*.m4a;*.aac;*.ogg)|*.mp3;*.wav;*.m4a;*.aac;*.ogg|All files (*.*)|*.*"
|
|
};
|
|
if (dialog.ShowDialog() != true) return;
|
|
box.AlertAudioPath = dialog.FileName;
|
|
}
|
|
|
|
private void ClearAlertAudio(Source? box)
|
|
{
|
|
if (box == null || box.Type != SourceType.AlertBox) return;
|
|
box.AlertAudioPath = null;
|
|
box.AlertAudioLoop = false;
|
|
}
|
|
|
|
private void ResetAlertVideo(Source? box)
|
|
{
|
|
if (box == null || box.Type != SourceType.AlertBox) return;
|
|
box.AlertVideoPath = null;
|
|
box.AlertShowTicker = true;
|
|
box.AlertUseDefaultVideo = true;
|
|
}
|
|
}
|