Files
Chronos/tests/Chronos.Core.Tests/Render/RenderPipelineTests.cs
T
Alby96andClaude Opus 5.5 728ffa35cb
CI / Build and test (push) Successful in 9m41s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 9s
Release / Build, test and publish (push) Successful in 14m49s
CI / PostgreSQL 16 integration (push) Successful in 8m51s
CI / PostgreSQL 17 integration (push) Successful in 9m48s
CI / Docker image (push) Successful in 54s
Release / Server image (push) Successful in 1m39s
feat: export panel and Render menu, playback options, videos library (owner review 1 phase 5) (#27)
Owner review 1 R3, R8, R9. A grouped Export step and Render menu; encoding, playback (reverse, boomerang, loops, fades, hold) and still frames; one videos library on the desktop and the server with a verified trash and copies to a videos folder.

Closes #21

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-11-05 09:18:40 +01:00

251 lines
13 KiB
C#

using Chronos.Core.Codecs.Jpeg;
using Chronos.Core.Codecs.Png;
using Chronos.Core.Codecs.Tiff;
using Chronos.Core.Export;
using Chronos.Core.Imaging;
using Chronos.Core.Project;
using Chronos.Core.Render;
using Chronos.Core.Render.Diagnostics;
using Chronos.Core.Video;
using Chronos.Core.Video.Managed;
using Chronos.Testing;
using static Chronos.Core.Tests.Render.RenderFixture;
namespace Chronos.Core.Tests.Render;
public sealed class RenderPipelineTests
{
private readonly TestContext _context;
public RenderPipelineTests(TestContext context) => _context = context;
[Theory]
[Case("mjpeg-avi", 8)]
[Case("jpeg-sequence", 8)]
[Case("png-sequence", 8)]
[Case("png-sequence", 16)]
[Case("tiff16-sequence", 16)]
[Case("dng-sequence", 16)]
[Case("gif-preview", 8)]
[Case("apng", 8)]
public async Task RendersEveryManagedFormat(string presetId, int bitDepth)
{
var dir = _context.CreateTempDirectory();
var source = Source();
var export = Preset(presetId) with { BitDepth = bitDepth };
var target = Target(dir, export);
var result = await Pipeline().RenderAsync(Input(source), target, cancellationToken: _context.Cancellation);
Assert.Equal(target.OutputPath, result.OutputPath);
Assert.SequenceEqual(new[] { Path.GetFileName(target.OutputPath) }, Directory.GetFileSystemEntries(dir).Select(Path.GetFileName));
Assert.Equal((96, 64), (result.Width, result.Height));
Assert.Equal(12, result.FrameCount);
Assert.Equal(RenderResultBytes(target.OutputPath), result.Bytes);
Assert.False(result.Hardware);
Assert.Approximately(12 / export.FrameRate.Value, result.DurationSeconds, 1e-4);
using var expected = source.Sequence.Render(5);
using var decoded = DecodeFrame(export.Codec, target.OutputPath, 5, out var count);
Assert.Equal(12, count);
var psnr = Psnr(expected, decoded);
_context.Log($"{presetId} {bitDepth}-bit: {result.Bytes} bytes, PSNR {psnr:F1} dB");
Assert.Greater(psnr, export.Codec switch { VideoCodec.Gif => 25, VideoCodec.Mjpeg or VideoCodec.JpegSequence => 35, _ => 60 });
}
[Test]
public async Task LosslessOutputsAreByteIdenticalAcrossRuns()
{
var dir = _context.CreateTempDirectory();
var project = Plain with
{
Exposure = new ExposureSettings { Deflicker = new Auto<bool>().WithManual(true), HighlightKnee = 0.9 },
Timing = new TimingSettings { Mode = new Auto<TimingMode>().WithManual(TimingMode.Interpolated), FrameRate = new Auto<string>().WithManual("15") },
};
var first = await Pipeline().RenderAsync(Input(Source(flickerEv: 0.2), project), Target(dir, Preset("png-sequence"), "a"), cancellationToken: _context.Cancellation);
var second = await Pipeline().RenderAsync(Input(Source(flickerEv: 0.2), project), Target(dir, Preset("png-sequence"), "b"), cancellationToken: _context.Cancellation);
var a = Directory.GetFiles(first.OutputPath).Order(StringComparer.Ordinal).ToArray();
var b = Directory.GetFiles(second.OutputPath).Order(StringComparer.Ordinal).ToArray();
Assert.Equal(a.Length, b.Length);
Assert.Greater(first.FrameCount, 12);
for (var i = 0; i < a.Length; i++) Assert.BytesEqual(File.ReadAllBytes(a[i]), File.ReadAllBytes(b[i]));
var apngA = await Pipeline().RenderAsync(Input(Source(), project), Target(dir, Preset("apng"), "c"), cancellationToken: _context.Cancellation);
var apngB = await Pipeline().RenderAsync(Input(Source(), project), Target(dir, Preset("apng"), "d"), cancellationToken: _context.Cancellation);
Assert.BytesEqual(File.ReadAllBytes(apngA.OutputPath), File.ReadAllBytes(apngB.OutputPath));
}
[Test]
public async Task TransformedOutputsAreByteIdenticalAcrossRuns()
{
var dir = _context.CreateTempDirectory();
var project = Plain with
{
Camera = new CameraSettings
{
Rotate = 90,
Mirror = true,
Straighten = 3.5,
Aspect = new Auto<string>().WithManual("4:5"),
Crop = [0.1, 0.05, 0.8, 0.8],
Keyframes = [new CameraKey(0, 0.45, 0.5, 1.2, 2), new CameraKey(11, 0.55, 0.5, 1.4, -2)],
},
};
var export = Preset("png-sequence", 64, 64);
var first = await Pipeline().RenderAsync(Input(Source(), project), Target(dir, export, "a"), cancellationToken: _context.Cancellation);
var second = await Pipeline().RenderAsync(Input(Source(), project), Target(dir, export, "b"), cancellationToken: _context.Cancellation);
Assert.Equal((51, 64), (first.Width, first.Height));
var a = Directory.GetFiles(first.OutputPath).Order(StringComparer.Ordinal).ToArray();
var b = Directory.GetFiles(second.OutputPath).Order(StringComparer.Ordinal).ToArray();
Assert.Equal(12, a.Length);
Assert.Equal(a.Length, b.Length);
for (var i = 0; i < a.Length; i++) Assert.BytesEqual(File.ReadAllBytes(a[i]), File.ReadAllBytes(b[i]));
}
[Theory]
[Case("mjpeg-avi")]
[Case("png-sequence")]
[Case("gif-preview")]
public async Task CancellationLeavesNoOutput(string presetId)
{
var dir = _context.CreateTempDirectory();
using var cts = CancellationTokenSource.CreateLinkedTokenSource(_context.Cancellation);
var progress = new Sink<RenderProgress>(p =>
{
if (p.Stage == RenderStage.Rendering && p.Done == 5) cts.Cancel();
});
await Assert.ThrowsAsync<OperationCanceledException>(() => Pipeline().RenderAsync(Input(Source(24)), Target(dir, Preset(presetId)), progress: progress, cancellationToken: cts.Token));
Assert.Empty(Directory.GetFileSystemEntries(dir));
}
[Test]
public async Task SourceFailurePropagatesOnceAndLeavesNoOutput()
{
var dir = _context.CreateTempDirectory();
var input = Input(Source()) with { Source = new FailingSource(Source(), failAt: 7) };
var error = await Assert.ThrowsAsync<IOException>(() => Pipeline().RenderAsync(input, Target(dir, Preset("mjpeg-avi"))));
Assert.Equal("frame 7 is unreadable", error.Message);
Assert.Empty(Directory.GetFileSystemEntries(dir));
}
[Test]
public async Task EncoderFailurePropagatesAndDisposesTheSession()
{
var capture = new CapturingEncoderProvider("capture", new EncoderCapability(VideoCodec.PngSequence, "Capture", false, 4096, 4096, true))
{
FailAt = (4, new InvalidDataException("encoder broke")),
};
var error = await Assert.ThrowsAsync<InvalidDataException>(() => Pipeline(capture).RenderAsync(Input(Source()), Target(_context.CreateTempDirectory(), Preset("png-sequence"))));
Assert.Equal("encoder broke", error.Message);
var encode = capture.Encodes.Single();
Assert.True(encode.Disposed);
Assert.False(encode.Completed);
Assert.Equal(4, encode.Frames.Count);
}
[Test]
public async Task SettingsTheEncoderIgnoredAreRenderWarnings()
{
var capture = new CapturingEncoderProvider("capture", new EncoderCapability(VideoCodec.PngSequence, "Capture", false, 4096, 4096, true))
{
Ignores = [EncoderSetting.RateControl, EncoderSetting.BFrames],
};
var result = await Pipeline(capture).RenderAsync(Input(Source()), Target(_context.CreateTempDirectory(), Preset("png-sequence")));
Assert.Contains(result.Warnings, w => w.Key == "Export.Encoder.Ignored.RateControl");
Assert.Contains(result.Warnings, w => w.Key == "Export.Encoder.Ignored.BFrames");
// Each key is a message of the catalogue.
foreach (var setting in Enum.GetValues<EncoderSetting>())
Assert.NotEqual("Export.Encoder.Ignored." + setting, Chronos.Core.Localization.Loc.L("Export.Encoder.Ignored." + setting));
}
[Test]
public async Task ProgressReportsEveryFrameAndPreviews()
{
var reports = new List<RenderProgress>();
var options = new RenderOptions { PreviewInterval = 5, PreviewLongEdge = 32 };
var project = Plain with { Exposure = new ExposureSettings { Deflicker = new Auto<bool>().WithManual(true), HighlightKnee = null } };
await Pipeline().RenderAsync(Input(Source(), project), Target(_context.CreateTempDirectory(), Preset("jpeg-sequence")), options,
new Sink<RenderProgress>(reports.Add), _context.Cancellation);
var preparing = reports.Where(r => r.Stage == RenderStage.Preparing).ToList();
var rendering = reports.Where(r => r.Stage == RenderStage.Rendering).ToList();
Assert.Equal(12, preparing.Count);
Assert.SequenceEqual(Enumerable.Range(1, 12), rendering.Select(r => r.Done));
Assert.True(rendering.All(r => r.Total == 12 && r.FramesPerSecond > 0 && r.Remaining >= TimeSpan.Zero));
Assert.SequenceEqual(new[] { 1, 6, 11 }, rendering.Where(r => r.Preview is not null).Select(r => r.Done));
Assert.Equal(32, rendering[0].Preview!.Width);
Assert.Equal(RenderStage.Finishing, reports[^1].Stage);
foreach (var r in rendering) r.Preview?.Dispose();
}
[Test]
public async Task BatchRendersEachTargetAndReportsFailures()
{
var dir = _context.CreateTempDirectory();
var server = new RenderPipeline([new ManagedEncoderProvider()], EncoderHost.Server);
var targets = new[]
{
Target(dir, Preset("png-sequence"), "png"),
Target(dir, Preset("1080p-h264"), "h264"),
Target(dir, Preset("gif-preview"), "gif"),
};
var results = await server.RenderBatchAsync(Input(Source()), targets, cancellationToken: _context.Cancellation);
Assert.Equal(3, results.Count);
Assert.True(results[0].Succeeded && results[2].Succeeded);
var error = Assert.NotNull(results[1].Error as RenderException);
Assert.Equal(EncoderNotices.NeedsRenderNode, error.ReasonKey);
Assert.SequenceEqual(new[] { "gif.gif", "png" }, Directory.GetFileSystemEntries(dir).Select(Path.GetFileName).Order(StringComparer.Ordinal));
}
[Test]
public async Task EverythingExcludedIsARenderError()
{
var project = Plain with { ExcludedFrames = [.. Enumerable.Range(0, 12)] };
var error = await Assert.ThrowsAsync<RenderException>(() => Pipeline().RenderAsync(Input(Source(), project), Target(_context.CreateTempDirectory(), Preset("png-sequence"))));
Assert.Equal("Export.Render.NoFrames", error.ReasonKey);
}
[Test]
public async Task ResultBecomesARenderHistoryEntity()
{
var result = await Pipeline().RenderAsync(Input(Source()), Target(_context.CreateTempDirectory(), Preset("apng")), cancellationToken: _context.Cancellation);
var sequence = Guid.CreateVersion7();
var entity = result.ToEntity(sequence, "APNG", "{}", "test");
Assert.Equal(sequence, entity.SequenceId);
Assert.Equal(result.OutputPath, entity.OutputPath);
Assert.Equal("Apng", entity.Codec);
Assert.Equal(result.Bytes, entity.Bytes);
Assert.Approximately(15, entity.Fps, 1e-12);
Assert.Equal("Managed APNG", result.EncoderName);
}
private static long RenderResultBytes(string path) => File.Exists(path)
? new FileInfo(path).Length
: new DirectoryInfo(path).EnumerateFiles().Sum(f => f.Length);
/// <summary>Decodes output picture <paramref name="index"/> and counts the pictures of the output.</summary>
private static ImageBuffer DecodeFrame(VideoCodec codec, string path, int index, out int count)
{
switch (codec)
{
case VideoCodec.Mjpeg:
var avi = AviReader.Read(File.ReadAllBytes(path));
count = avi.Frames.Count;
return JpegDecoder.DecodeToLinear(avi.Frames[index]);
case VideoCodec.Gif or VideoCodec.Apng:
var bytes = File.ReadAllBytes(path);
var animation = codec == VideoCodec.Gif ? GifReader.Decode(bytes) : ApngReader.Decode(bytes);
count = animation.Frames.Count;
return animation.ToLinear(index);
default:
var files = Directory.GetFiles(path).Order(StringComparer.Ordinal).ToArray();
count = files.Length;
var data = File.ReadAllBytes(files[index]);
return codec switch
{
VideoCodec.JpegSequence => JpegDecoder.DecodeToLinear(data),
VideoCodec.PngSequence => PngDecoder.DecodeToLinear(data),
_ => TiffImageReader.Decode(data),
};
}
}
}