feat(text): composite shared text rasters in output
Raster static text on UI-thread changes and reuse the same transparent BGRA frame in preview and all compositor paths. Cache text frame identity in the frame-pump signature and invalidate static bakes on edits. Follow OBS text-source conventions: https://obsproject.com/kb/text-sources
This commit is contained in:
@@ -164,7 +164,6 @@ public sealed class SceneCompositor
|
||||
{
|
||||
case Source { IsBackground: true }:
|
||||
case Source { Type: SourceType.Background }:
|
||||
case Source { Type: SourceType.TextOverlay }:
|
||||
case ZoomSceneElement { IsZoomActive: true }:
|
||||
continue;
|
||||
}
|
||||
@@ -187,7 +186,6 @@ public sealed class SceneCompositor
|
||||
{
|
||||
case Source { IsBackground: true }:
|
||||
case Source { Type: SourceType.Background }:
|
||||
case Source { Type: SourceType.TextOverlay }:
|
||||
// ⛔ The brand logo is drawn AFTER the blur group (creator ruling 2026-11-01):
|
||||
// the blur is universal and must never hide the brand.
|
||||
case Source { IsBrandLogo: true }:
|
||||
@@ -311,7 +309,6 @@ public sealed class SceneCompositor
|
||||
{
|
||||
case Source { IsBackground: true }:
|
||||
case Source { Type: SourceType.Background }:
|
||||
case Source { Type: SourceType.TextOverlay }:
|
||||
case ZoomSceneElement:
|
||||
continue;
|
||||
}
|
||||
@@ -370,7 +367,6 @@ public sealed class SceneCompositor
|
||||
{
|
||||
case Source { IsBackground: true }:
|
||||
case Source { Type: SourceType.Background }:
|
||||
case Source { Type: SourceType.TextOverlay }:
|
||||
// ⛔ The brand logo is drawn AFTER the blur group (creator ruling 2026-11-01):
|
||||
// the blur is universal and must never hide the brand.
|
||||
case Source { IsBrandLogo: true }:
|
||||
|
||||
@@ -856,7 +856,8 @@ public sealed class FramePump : IDisposable
|
||||
}
|
||||
else if (element is Source { Type: SourceType.TextOverlay })
|
||||
{
|
||||
h = Mix(h, 2UL); // skipped kind — never resolved by the compositor
|
||||
h = Mix(h, 2UL); // static text is resolved from its raster-on-change frame cache
|
||||
if (element.IsVisible) frame = resolver(element);
|
||||
}
|
||||
else if (element is Source { Type: SourceType.Background })
|
||||
{
|
||||
|
||||
@@ -0,0 +1,150 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using ytLive.Models;
|
||||
|
||||
namespace ytLive.Services;
|
||||
|
||||
public sealed class TextOverlayRenderer
|
||||
{
|
||||
private const int MaxRasterDimension = 4096;
|
||||
private readonly ConcurrentDictionary<Source, CacheEntry> _cache = new();
|
||||
|
||||
public VideoFrame? RenderFrame(Source source)
|
||||
=> _cache.TryGetValue(source, out var entry) ? entry.Frame : null;
|
||||
|
||||
public void Remove(Source source) => _cache.TryRemove(source, out _);
|
||||
|
||||
public void Retain(IEnumerable<Source> sources)
|
||||
{
|
||||
var active = new HashSet<Source>(sources);
|
||||
foreach (var source in _cache.Keys)
|
||||
if (!active.Contains(source))
|
||||
_cache.TryRemove(source, out _);
|
||||
}
|
||||
|
||||
public void UpdatePreview(Source source)
|
||||
{
|
||||
var dispatcher = Application.Current?.Dispatcher;
|
||||
if (dispatcher != null && !dispatcher.CheckAccess())
|
||||
{
|
||||
dispatcher.Invoke(() => UpdatePreview(source));
|
||||
return;
|
||||
}
|
||||
|
||||
var config = Capture(source);
|
||||
if (_cache.TryGetValue(source, out var cached) && cached.Config == config)
|
||||
{
|
||||
if (!ReferenceEquals(source.VideoImageSource, cached.Preview))
|
||||
source.VideoImageSource = cached.Preview;
|
||||
return;
|
||||
}
|
||||
|
||||
var frame = Rasterize(config);
|
||||
var bitmap = new WriteableBitmap(frame.Width, frame.Height, 96, 96, PixelFormats.Bgra32, null);
|
||||
bitmap.WritePixels(new Int32Rect(0, 0, frame.Width, frame.Height), frame.BgraPixels, frame.Stride, 0);
|
||||
_cache[source] = new CacheEntry(config, frame, bitmap);
|
||||
source.VideoImageSource = bitmap;
|
||||
}
|
||||
|
||||
private static RenderConfig Capture(Source source) => new(
|
||||
source.TextContent,
|
||||
source.TextFontFamily,
|
||||
source.TextFontSize,
|
||||
source.TextColor,
|
||||
source.TextBold,
|
||||
source.TextItalic,
|
||||
source.TextUnderline,
|
||||
source.TextAlignment,
|
||||
RasterDimension(source.Width),
|
||||
RasterDimension(source.Height));
|
||||
|
||||
private static int RasterDimension(double value) =>
|
||||
double.IsFinite(value) ? (int)Math.Clamp(Math.Ceiling(value), 1, MaxRasterDimension) : 1;
|
||||
|
||||
private static VideoFrame Rasterize(RenderConfig config)
|
||||
{
|
||||
var visual = new DrawingVisual();
|
||||
if (!string.IsNullOrEmpty(config.Content))
|
||||
{
|
||||
using var drawing = visual.RenderOpen();
|
||||
var family = new FontFamily(config.FontFamily);
|
||||
var typeface = new Typeface(
|
||||
family,
|
||||
config.Italic ? FontStyles.Italic : FontStyles.Normal,
|
||||
config.Bold ? FontWeights.Bold : FontWeights.Normal,
|
||||
FontStretches.Normal);
|
||||
var brush = new SolidColorBrush(ParseColor(config.Color));
|
||||
brush.Freeze();
|
||||
var formatted = new FormattedText(
|
||||
config.Content,
|
||||
CultureInfo.CurrentUICulture,
|
||||
FlowDirection.LeftToRight,
|
||||
typeface,
|
||||
config.FontSize,
|
||||
brush,
|
||||
1.0)
|
||||
{
|
||||
MaxTextWidth = config.Width,
|
||||
MaxTextHeight = config.Height,
|
||||
TextAlignment = config.Alignment switch
|
||||
{
|
||||
TextLayerAlignment.Center => System.Windows.TextAlignment.Center,
|
||||
TextLayerAlignment.Right => System.Windows.TextAlignment.Right,
|
||||
_ => System.Windows.TextAlignment.Left,
|
||||
},
|
||||
Trimming = TextTrimming.CharacterEllipsis,
|
||||
};
|
||||
if (config.Underline)
|
||||
formatted.SetTextDecorations(TextDecorations.Underline, 0, config.Content.Length);
|
||||
drawing.DrawText(formatted, new Point(0, 0));
|
||||
}
|
||||
|
||||
var bitmap = new RenderTargetBitmap(config.Width, config.Height, 96, 96, PixelFormats.Pbgra32);
|
||||
bitmap.Render(visual);
|
||||
var pixels = new byte[config.Width * config.Height * 4];
|
||||
bitmap.CopyPixels(pixels, config.Width * 4, 0);
|
||||
UnpremultiplyAlpha(pixels);
|
||||
return new VideoFrame(config.Width, config.Height, pixels);
|
||||
}
|
||||
|
||||
private static Color ParseColor(string hex) =>
|
||||
(Color)ColorConverter.ConvertFromString(hex)!;
|
||||
|
||||
private static void UnpremultiplyAlpha(byte[] pixels)
|
||||
{
|
||||
for (var i = 0; i < pixels.Length; i += 4)
|
||||
{
|
||||
var alpha = pixels[i + 3];
|
||||
if (alpha == 0)
|
||||
{
|
||||
pixels[i] = pixels[i + 1] = pixels[i + 2] = 0;
|
||||
continue;
|
||||
}
|
||||
if (alpha == 255) continue;
|
||||
pixels[i] = Unpremultiply(pixels[i], alpha);
|
||||
pixels[i + 1] = Unpremultiply(pixels[i + 1], alpha);
|
||||
pixels[i + 2] = Unpremultiply(pixels[i + 2], alpha);
|
||||
}
|
||||
}
|
||||
|
||||
private static byte Unpremultiply(byte value, byte alpha) =>
|
||||
(byte)Math.Min(255, (value * 255 + alpha / 2) / alpha);
|
||||
|
||||
private sealed record CacheEntry(RenderConfig Config, VideoFrame Frame, WriteableBitmap Preview);
|
||||
|
||||
private readonly record struct RenderConfig(
|
||||
string Content,
|
||||
string FontFamily,
|
||||
double FontSize,
|
||||
string Color,
|
||||
bool Bold,
|
||||
bool Italic,
|
||||
bool Underline,
|
||||
TextLayerAlignment Alignment,
|
||||
int Width,
|
||||
int Height);
|
||||
}
|
||||
@@ -12,6 +12,7 @@ External-facing logic: YouTube API, persistence. See
|
||||
| `YouTubeChatService.cs` | Polls `liveChat/messages`, raises `MessageReceived`; `IDisposable`. **TASK 43 rewrite (2026-09-24):** re-arms its one-shot poll on the server's `pollingIntervalMillis` (streamList semantics — reconnect on that cadence with `nextPageToken`, clamp 1000–6000ms, `maxResults=2000`); `ParsePage` (internal static seam) decodes **all six** event types into `ChatMessage.Kind`/fields (superChat / superSticker / newSponsor / membershipGifting / giftMembershipReceived / memberMilestoneChat + milestone months best-effort regex); optional `HttpClient` ctor seam endures; `InjectSimulatedMessage` (TASK 41) preserved |
|
||||
| `ChatBoxRenderer.cs` | Chat overlay rasterizer (WPF FormattedText → BGRA `VideoFrame`), live rows + Super Chat/member styling |
|
||||
| `ChatOverlayLayer.cs` | **The chat component (Commit G):** owns the message buffer, `ChatBoxRenderer`, fade/mock timers, per-source preview renders, live `RenderFrame`; raster-on-change cache pattern (OBS text-source contract) |
|
||||
| `TextOverlayRenderer.cs` | TASK 3.17 static TextOverlay rasterizer/cache: WPF `FormattedText` runs on the dispatcher only when content/style/size changes; one straight-alpha BGRA `VideoFrame` drives both the XAML preview bitmap and compositor output, while pump reads are cache-only |
|
||||
| `AlertRenderer.cs` | **The alert rasterizer (TASK 43, 2026-09-24):** renders one event's celebration animation into a BGRA `VideoFrame` — SIX DISTINCT animations (SuperChat slide-up/shine/count-up, SuperSticker scale-pop, NewMember drop-in/flash-band, MemberGift slide-left/chip-fan, GiftReceived confetti scale-in, MemberMilestone rise/growth-bar), enter/hold/exit phases, brand line on every card. **Still the fallback** when no alert clip is playable (TASK 47) |
|
||||
| `AlertOverlayLayer.cs` | **The native alert-box component (TASK 43; video branch TASK 47):** event queue (cap 10) + 33ms dispatch ticker + `RenderFrame` cache-first live path + `UpdatePreview`; **`Advance(double)` is the deterministic test clock** (tests never start the timer); `ChatEventKind.None` rows (sub mentions) never enqueue; idle = transparent (null frame). Since TASK 47 a clip branch owns `IAlertClipDecoder` per play — ctor seams `(clipDecoderFactory, clipPathResolver, audioSink)`, fade in/out over `FadeDurationSeconds = 0.30` (straight-alpha copies + volume×fade audio), EOF freeze-frame → drain. The shared ticker renderer serves the AlertBox's active event and scene-local `SourceType.Ticker` layers' current+queued feed (transient; no history) |
|
||||
| `AlertClipDecoder.cs` | **Per-play alert video decoder (TASK 47):** `IAlertClipDecoder` (Start/Stop/Dispose + `FrameAvailable`/`AudioReady`/`Completed`) + ffmpeg child emitting `rawvideo bgra` (`-vf scale=W:H`, read via `RawVideoFrameReader`) and `f32le -ar 48000 -ac 2` on a **carry-buffer** audio loop (float boundary straddles never drop samples), both real-time paced; bgra is opaque (layer owns alpha). One-shot lifetime — created per alert, disposed at drain; tests inject a fake |
|
||||
|
||||
Reference in New Issue
Block a user