Files
Chronos/tests/Titano.Core.Tests/Render/CapturingEncoderProvider.cs
T
Claude f9c83af609 feat(export): encoder selection with hardware preference, fallbacks and level caps
Client renders prefer hardware Media Foundation encoders, fall back from
HEVC/AV1 to H.264, drop to 8-bit and cap size/bit rate with reason keys;
the server reports that MP4 codecs need a render node.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 10:48:34 +00:00

58 lines
2.3 KiB
C#

using Titano.Core.Imaging;
using Titano.Core.Video;
namespace Titano.Core.Tests.Render;
/// <summary>One encode recorded by <see cref="CapturingEncoderProvider"/>.</summary>
internal sealed class CapturedEncode(EncodeSettings settings)
{
public EncodeSettings Settings { get; } = settings;
public List<ImageBuffer> Frames { get; } = [];
public List<long> Durations { get; } = [];
public bool Completed { get; set; }
public bool Disposed { get; set; }
}
/// <summary>Test encoder: keeps a copy of every frame, can pretend to be hardware or to fail at a given frame.</summary>
internal sealed class CapturingEncoderProvider(string name, params EncoderCapability[] capabilities) : IVideoEncoderProvider
{
public string Name => name;
public IReadOnlyList<EncoderCapability> Capabilities => capabilities;
public List<CapturedEncode> Encodes { get; } = [];
/// <summary>When set, WriteFrame throws this for the frame with that zero-based index.</summary>
public (int Frame, Exception Error)? FailAt { get; set; }
public bool CanEncode(EncodeSettings settings) => capabilities.Any(c =>
c.Codec == settings.Codec && settings.Width <= c.MaxWidth && settings.Height <= c.MaxHeight && (settings.BitDepth == 8 || c.SupportsTenBit));
public IVideoEncoderSession Open(EncodeSettings settings)
{
var encode = new CapturedEncode(settings);
Encodes.Add(encode);
var capability = capabilities.First(c => c.Codec == settings.Codec);
return new Session(this, encode, capability);
}
private sealed class Session(CapturingEncoderProvider owner, CapturedEncode encode, EncoderCapability capability) : IVideoEncoderSession
{
public string EncoderName => capability.Name;
public bool IsHardware => capability.Hardware;
public void WriteFrame(ImageBuffer frame, long duration90k)
{
if (owner.FailAt is { } fail && fail.Frame == encode.Frames.Count) throw fail.Error;
if (frame.Width != encode.Settings.Width || frame.Height != encode.Settings.Height) throw new ArgumentException("size");
encode.Frames.Add(frame.Clone());
encode.Durations.Add(duration90k);
}
public void Complete() => encode.Completed = true;
public void Dispose() => encode.Disposed = true;
}
}