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
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>
139 lines
6.0 KiB
C#
139 lines
6.0 KiB
C#
using System.Security.Cryptography;
|
|
using Chronos.Core.Codecs.Jpeg;
|
|
using Chronos.Core.Diagnostics.Synthetic;
|
|
using Chronos.Core.Metadata;
|
|
using Chronos.Core.Model;
|
|
using Chronos.Data.Archiving;
|
|
using Chronos.Data.Tests.Archiving;
|
|
using Chronos.Data.Videos;
|
|
using Chronos.Testing;
|
|
|
|
namespace Chronos.Data.Tests.Videos;
|
|
|
|
/// <summary>A temporary archive with small synthetic sequences and renders recorded on files written by hand.</summary>
|
|
internal sealed class VideoFixture : IAsyncDisposable
|
|
{
|
|
private VideoFixture(TestContext context, ArchiveFixture archive)
|
|
{
|
|
Archive = archive;
|
|
VideosFolder = context.CreateTempDirectory("videos");
|
|
}
|
|
|
|
public ArchiveFixture Archive { get; }
|
|
public ChronosDatabase Db => Archive.Db;
|
|
public VideoLibrary Library => Archive.Service.Videos;
|
|
public CancellationToken Ct => Archive.Ct;
|
|
|
|
/// <summary>A folder outside the archive that copies go to.</summary>
|
|
public string VideosFolder { get; }
|
|
|
|
/// <param name="freeSpace">How much space the videos folder has (bytes); null: the drive's.</param>
|
|
public static async Task<VideoFixture> CreateAsync(TestContext context, long? freeSpace = null)
|
|
{
|
|
string? videos = null;
|
|
var archive = await ArchiveFixture.CreateAsync(context, configure: o => o with
|
|
{
|
|
FreeSpace = path => freeSpace is { } free && videos is not null && path.StartsWith(videos, StringComparison.OrdinalIgnoreCase) ? free : null,
|
|
});
|
|
var fixture = new VideoFixture(context, archive);
|
|
videos = fixture.VideosFolder;
|
|
return fixture;
|
|
}
|
|
|
|
/// <summary>A sequence of four synthetic frames in <paramref name="folder"/>, indexed.</summary>
|
|
public async Task<Sequence> SequenceAsync(string folder = "Harbour", int seed = 1)
|
|
{
|
|
var synthetic = SyntheticSequence.Create(new SyntheticSequenceOptions { FrameCount = 4, Seed = seed, Start = new DateTime(2024, 6, 21, 19, 0, 0).AddDays(seed) });
|
|
var directory = Path.Combine(Archive.Paths.Root, folder);
|
|
Directory.CreateDirectory(directory);
|
|
for (var i = 0; i < synthetic.Count; i++)
|
|
{
|
|
using var image = synthetic.Render(i);
|
|
var exif = ExifWriter.BuildJpegApp1(synthetic.Metadata[i]);
|
|
File.WriteAllBytes(Path.Combine(directory, $"IMG_{i + 1:0000}.JPG"), JpegEncoder.Encode(image, new JpegEncodeOptions { Quality = 90, App1Segments = [exif[4..]] }));
|
|
}
|
|
await Archive.ScanAsync(Chronos.Data.Archiving.ScanMode.Full);
|
|
var location = (await Db.Sequences.ListLocationsAsync(Archive.Archive.Id, Ct)).Single(l => l.Folder == folder);
|
|
return (await Db.Sequences.GetAsync(location.Id, Ct))!;
|
|
}
|
|
|
|
/// <summary>A render of <paramref name="sequence"/> recorded on a file of <paramref name="bytes"/> written into its exports.</summary>
|
|
public async Task<Render> RenderAsync(Sequence sequence, string name, int bytes, DateTimeOffset? created = null, string codec = "H264", string? kind = null,
|
|
bool hashed = true)
|
|
{
|
|
var exports = Archive.Paths.Sequence(sequence.Folder).Exports;
|
|
Directory.CreateDirectory(exports);
|
|
var content = Content(name, bytes);
|
|
await File.WriteAllBytesAsync(Path.Combine(exports, name), content, Ct);
|
|
var render = new Render
|
|
{
|
|
Id = Guid.CreateVersion7(),
|
|
SequenceId = sequence.Id,
|
|
PresetName = "Test preset",
|
|
OutputPath = $"{sequence.Folder}/exports/{name}",
|
|
Codec = codec,
|
|
Width = 96,
|
|
Height = 64,
|
|
Fps = kind == RenderKind.Still ? 0 : 24,
|
|
DurationSeconds = kind == RenderKind.Still ? 0 : 2,
|
|
Bytes = bytes,
|
|
Created = created ?? DateTimeOffset.UtcNow,
|
|
ProducedBy = "test-pc",
|
|
Sha256 = hashed ? Convert.ToHexStringLower(SHA256.HashData(content)) : null,
|
|
Kind = kind,
|
|
};
|
|
await Db.Renders.InsertAsync(render, Ct);
|
|
return render;
|
|
}
|
|
|
|
/// <summary>An image-sequence render: a folder of <paramref name="files"/> PNG-named files.</summary>
|
|
public async Task<Render> SequenceRenderAsync(Sequence sequence, string name, int files)
|
|
{
|
|
var folder = Path.Combine(Archive.Paths.Sequence(sequence.Folder).Exports, name);
|
|
Directory.CreateDirectory(folder);
|
|
long total = 0;
|
|
for (var i = 0; i < files; i++)
|
|
{
|
|
var content = Content(name + i, 300 + i);
|
|
total += content.Length;
|
|
await File.WriteAllBytesAsync(Path.Combine(folder, $"{name}_{i + 1:000000}.png"), content, Ct);
|
|
}
|
|
var render = new Render
|
|
{
|
|
Id = Guid.CreateVersion7(),
|
|
SequenceId = sequence.Id,
|
|
PresetName = "PNG",
|
|
OutputPath = $"{sequence.Folder}/exports/{name}",
|
|
Codec = "PngSequence",
|
|
Width = 96,
|
|
Height = 64,
|
|
Fps = 24,
|
|
Bytes = total,
|
|
Created = DateTimeOffset.UtcNow,
|
|
};
|
|
await Db.Renders.InsertAsync(render, Ct);
|
|
return render;
|
|
}
|
|
|
|
public string FileOf(Render render) => Archive.Paths.FromRelative(render.OutputPath);
|
|
|
|
private Chronos.Data.Jobs.JobEngine? _engine;
|
|
|
|
/// <summary>Starts the archive's job engine (its handlers include the videos copy).</summary>
|
|
public async Task StartEngineAsync() => _engine = await Archive.StartEngineAsync();
|
|
|
|
public Task<Job> EnqueueAsync(Chronos.Data.Archiving.Jobs.ArchiveJobRequest request) =>
|
|
(_engine ?? throw new InvalidOperationException("Start the engine first.")).EnqueueAsync(request, cancellationToken: Ct);
|
|
|
|
/// <summary>Deterministic bytes, different for each name.</summary>
|
|
public static byte[] Content(string name, int bytes)
|
|
{
|
|
var content = new byte[bytes];
|
|
new Random(StringComparer.Ordinal.GetHashCode(name) & 0x7FFFFFFF).NextBytes(content);
|
|
content[0] = (byte)name.Length;
|
|
return content;
|
|
}
|
|
|
|
public ValueTask DisposeAsync() => Archive.DisposeAsync();
|
|
}
|