From fca6c8dfea0ef92e5c3f13b729125deb9f68aaee Mon Sep 17 00:00:00 2001 From: Alberto Balbo Date: Wed, 4 Nov 2026 19:48:23 +0100 Subject: [PATCH] feat(server): route renders by settings format and adapted size; gate older servers Owner review 1, phase 4 (P5). A render job now carries the output size its settings give (RenderSize, proportions win) and the settings format it needs, resolved on the server before the job is split, so only nodes that read that format and whose encoder takes the frame claim it, and a job no online node can read no longer waits for one. Nodes and the server announce the format they read; settings of a newer Chronos fail saying so. Encoder size limits are read as long edge x short edge: Media Foundation's software H.264 encoder, announced as 4096x2304, accepts portrait 2160x3840 (probed on this PC), so EncoderSelector and the provider take either orientation and cap a portrait frame against the turned limit. On the desktop, Sync and Send-and-render are disabled with the NeedsNewerServer tooltip while the server reads an older format than the working settings need. Co-Authored-By: Claude Opus 5.5 --- src/Chronos.App/Editor/EditorViewModel.cs | 7 ++ src/Chronos.App/Editor/ServerJobs.cs | 7 ++ src/Chronos.App/Sections/SequenceView.xaml | 2 +- src/Chronos.App/Server/SyncHost.cs | 12 ++- .../Workspace/ProjectMenuViewModel.cs | 29 +++++- .../MediaFoundationEncoderProvider.cs | 6 +- src/Chronos.Core/Export/EncoderSelector.cs | 21 +++- .../Jobs/ManagedRenderJobHandler.cs | 30 +++++- .../Processing/Jobs/RenderJobHandler.cs | 11 ++- src/Chronos.Data/Processing/ProcessingJobs.cs | 96 +++++++++++++++--- src/Chronos.Data/Processing/ProjectStore.cs | 11 ++- .../Processing/RenderNodeClient.cs | 6 +- src/Chronos.Data/Projects/IProjectHome.cs | 6 ++ .../Projects/ServerProjectHome.cs | 7 +- src/Chronos.Server/Api/JobEndpoints.cs | 27 +++-- src/Chronos.Server/Api/SystemEndpoints.cs | 4 +- .../Jobs/RenderNodeAvailability.cs | 7 +- src/Chronos.Server/Jobs/RenderNodeService.cs | 44 ++++++++- .../Workspace/ServerHomeTests.cs | 52 ++++++++++ .../Export/EncoderSelectorTests.cs | 16 +++ .../Render/CapturingEncoderProvider.cs | 3 +- .../MediaFoundationEncoderTests.cs | 56 +++++++++++ .../Processing/ProjectStoreTests.cs | 54 ++++++++++ .../Processing/RenderJobTests.cs | 59 +++++++++++ .../Chronos.Server.Tests/Api/ProjectTests.cs | 36 +++++++ .../Jobs/RenderNodeClientTests.cs | 3 + .../Jobs/RenderNodeTests.cs | 98 +++++++++++++++++++ 27 files changed, 663 insertions(+), 47 deletions(-) create mode 100644 tests/Chronos.Data.Tests/Processing/ProjectStoreTests.cs diff --git a/src/Chronos.App/Editor/EditorViewModel.cs b/src/Chronos.App/Editor/EditorViewModel.cs index 64e68aec..cff714b2 100644 --- a/src/Chronos.App/Editor/EditorViewModel.cs +++ b/src/Chronos.App/Editor/EditorViewModel.cs @@ -551,10 +551,17 @@ public sealed class EditorViewModel : LocalizedViewModel, IAsyncDisposable public bool CanRender => RenderBlockedReason is null; + /// The settings format a render of needs (). + private int RenderFormat => + Chronos.Data.Processing.ProcessingJobs.RequiredFormat(Session.Project, RenderExport, Session.Content.SourceWidth, Session.Content.SourceHeight); + /// Why the project's export cannot be rendered now, localised; null when it can. public string? RenderBlockedReason => Plan switch { _ when IsRemote && !Shell.Services.Sync.CanRender => Loc.L("Tooltips.Editor.Render.NoRight", ServerName), + // Settings or proportions the server would render otherwise (owner review 1, phase 4: an older server, Send and render too). + _ when IsRemote && ServerJobs.NeedsNewerServer(RenderFormat, Shell.Services.Sync.SettingsFormat) + is { } newer => newer, _ when IsOutdated => Loc.L("Tooltips.Editor.Outdated", ServerName), null when _planned => Loc.L("Tooltips.Editor.Render.NoSource"), null => Loc.L("Tooltips.Editor.Render.Planning"), diff --git a/src/Chronos.App/Editor/ServerJobs.cs b/src/Chronos.App/Editor/ServerJobs.cs index 31160330..890c3ff9 100644 --- a/src/Chronos.App/Editor/ServerJobs.cs +++ b/src/Chronos.App/Editor/ServerJobs.cs @@ -56,6 +56,13 @@ internal sealed class ServerJobs : IAsyncDisposable /// The connection the jobs travel on (the source's other calls use it too). public IRemoteSequenceApi Api { get; } + /// + /// Why something that needs settings format cannot go to a server that reads + /// (owner review 1, phase 4: a render, a push, Send and render); null when it can. The + /// desktop disables the action with this as its tooltip. + /// + public static string? NeedsNewerServer(int required, int serverFormat) => required > serverFormat ? Loc.L("Tooltips.Server.NeedsNewerServer") : null; + /// Queues on the server (its priority set after, when it is not the user's); returns the job id. /// The sequence is gone from the server. public async Task SubmitAsync(ArchiveJobRequest request, CancellationToken cancellationToken) diff --git a/src/Chronos.App/Sections/SequenceView.xaml b/src/Chronos.App/Sections/SequenceView.xaml index 5a6eb10f..2a12b9af 100644 --- a/src/Chronos.App/Sections/SequenceView.xaml +++ b/src/Chronos.App/Sections/SequenceView.xaml @@ -118,7 +118,7 @@ - + diff --git a/src/Chronos.App/Server/SyncHost.cs b/src/Chronos.App/Server/SyncHost.cs index c01162f2..a14e3f26 100644 --- a/src/Chronos.App/Server/SyncHost.cs +++ b/src/Chronos.App/Server/SyncHost.cs @@ -81,6 +81,16 @@ public sealed class SyncHost(AppServices services) : ISyncConnector, IAsyncDispo public bool HasProjects => services.Server.IsConnected && services.Server.Flow.Info?.Capabilities is { } capabilities && capabilities.Contains(ServerCapabilities.Projects); + /// + /// The highest project settings format the signed-in server reads and renders (owner review 1, phase 4); 1 while signed + /// out and for an older server, which does not announce it. Pushes and renders that need more are held back + /// (). + /// + public int SettingsFormat => SettingsFormatOf(services.Server.IsConnected ? services.Server.Flow.Info : null); + + /// The settings format a server's server/info announces; 1 when it announces none (an older server). + public static int SettingsFormatOf(ServerInfo? info) => info?.SettingsFormat is > 0 and var format ? format : ProjectSettings.BaseVersion; + /// The home of a workspace project of server sequence ; null while signed out or when the server keeps no projects. public BundleHome? ServerHome(Guid remoteId) => Server is { } server && HasProjects ? new BundleHome(BundleHome.ServerKind, remoteId, null, server) : null; @@ -95,7 +105,7 @@ public sealed class SyncHost(AppServices services) : ISyncConnector, IAsyncDispo if (home.IsArchive || !HasProjects || Volatile.Read(ref _credentials) is not { } credentials || !string.Equals(home.Server, SyncService.ServerKey(credentials.Address), StringComparison.OrdinalIgnoreCase)) return null; - return new ServerProjectHome(SessionClient(credentials), home.SequenceId, services.Server.ServerName); + return new ServerProjectHome(SessionClient(credentials), home.SequenceId, services.Server.ServerName, SettingsFormat); } /// diff --git a/src/Chronos.App/Workspace/ProjectMenuViewModel.cs b/src/Chronos.App/Workspace/ProjectMenuViewModel.cs index cf835c23..300e5cf5 100644 --- a/src/Chronos.App/Workspace/ProjectMenuViewModel.cs +++ b/src/Chronos.App/Workspace/ProjectMenuViewModel.cs @@ -108,7 +108,7 @@ public sealed class ProjectMenuViewModel : LocalizedViewModel _source = source; _session = session; _ui = ui; - _sync = new AsyncRelayCommand(() => SyncAsync(), () => !_isSyncing); + _sync = new AsyncRelayCommand(() => SyncAsync(), () => !_isSyncing && PushBlockedReason is null); _updateMedia = new AsyncRelayCommand(UpdateMediaAsync, () => _copyJob == Guid.Empty); SaveVersionCommand = new RelayCommand(OpenSaveVersion, () => CanSaveVersion); HistoryCommand = new AsyncRelayCommand(OpenHistoryAsync); @@ -146,6 +146,18 @@ public sealed class ProjectMenuViewModel : LocalizedViewModel public string StateToolTip => WorkspaceText.StateToolTip(_status, _isSyncing, IsServerHome); + /// + /// Why the project cannot be sent to its server now (owner review 1, phase 4): its settings are of a format the server + /// does not read (an older Chronos Server, which would refuse them); null when they can, and for a project of this PC's + /// archive. Sync is disabled with this as its tooltip, and a render of the server's is held. + /// + public string? PushBlockedReason => IsServerHome && _source.Workspace is { } workspace && _host.HomeOf(workspace.Home) is { } home + ? ServerJobs.NeedsNewerServer(_session.Project.WrittenFormat, home.SettingsFormat) + : null; + + /// Sync in the menu: why it is disabled, else where the project stands. + public string SyncToolTip => PushBlockedReason ?? StateToolTip; + /// The ▾ button: its name, then where the project stands, then what the menu holds. public string MenuToolTip => Loc.L("Tooltips.Editor.Project.Menu", Name, StateText); @@ -270,6 +282,12 @@ public sealed class ProjectMenuViewModel : LocalizedViewModel if (_host.Settings.WorkspaceVersionOnRender && !await SaveForRenderAsync()) return false; // Rendered on this PC from the originals the workspace holds (phase 5c): the project renders as it is. if (!IsServerHome || _source.RendersLocally) return true; + // Settings the server cannot read are neither sent nor rendered there (owner review 1, phase 4: an older server). + if (PushBlockedReason is { } newer) + { + _ui.Notify(Loc.L("Editor.Workspace.RenderHeld"), newer, isError: true); + return false; + } await StoreEditAsync(); var status = await _host.StatusAsync(ProjectId); if (_disposed) return false; @@ -386,6 +404,12 @@ public sealed class ProjectMenuViewModel : LocalizedViewModel public async Task SyncAsync(bool quiet = false) { if (_isSyncing) return; + // Settings the server cannot read stay here until it is updated (owner review 1, phase 4): Sync says why. + if (PushBlockedReason is { } newer) + { + if (!quiet) _ui.Notify(Loc.L("Editor.Workspace.NotSent"), newer, isError: true); + return; + } SetSyncing(true); try { @@ -1026,6 +1050,8 @@ public sealed class ProjectMenuViewModel : LocalizedViewModel OnPropertyChanged(nameof(StateText)); OnPropertyChanged(nameof(StateKind)); OnPropertyChanged(nameof(StateToolTip)); + OnPropertyChanged(nameof(PushBlockedReason)); + OnPropertyChanged(nameof(SyncToolTip)); OnPropertyChanged(nameof(MenuToolTip)); OnPropertyChanged(nameof(MenuName)); OnPropertyChanged(nameof(HasMediaUpdate)); @@ -1044,6 +1070,7 @@ public sealed class ProjectMenuViewModel : LocalizedViewModel OnPropertyChanged(nameof(CopyOriginalsToolTip)); OnPropertyChanged(nameof(ProxiesOnlyToolTip)); _updateMedia.RaiseCanExecuteChanged(); + _sync.RaiseCanExecuteChanged(); CommandManager.InvalidateRequerySuggested(); } } diff --git a/src/Chronos.Core.Windows/MediaFoundation/MediaFoundationEncoderProvider.cs b/src/Chronos.Core.Windows/MediaFoundation/MediaFoundationEncoderProvider.cs index 6e5d95ad..d4fc9499 100644 --- a/src/Chronos.Core.Windows/MediaFoundation/MediaFoundationEncoderProvider.cs +++ b/src/Chronos.Core.Windows/MediaFoundation/MediaFoundationEncoderProvider.cs @@ -1,5 +1,6 @@ using System.Runtime.InteropServices; using Chronos.Core.Diagnostics; +using Chronos.Core.Export; using Chronos.Core.Video; using Chronos.Core.Windows.Interop; @@ -13,7 +14,8 @@ namespace Chronos.Core.Windows.MediaFoundation; public sealed class MediaFoundationEncoderProvider : IVideoEncoderProvider { private const string LogCategory = MediaFoundationEncoders.LogCategory; - private const int SoftwareH264MaxWidth = 4096, SoftwareH264MaxHeight = 2304; // Microsoft H.264 encoder, level 5.2 + // Microsoft H.264 encoder, level 5.2: long edge × short edge, portrait UHD included (EncoderSelector.Takes). + private const int SoftwareH264MaxWidth = 4096, SoftwareH264MaxHeight = 2304; private readonly Lazy> _capabilities = new(BuildCapabilities); public string Name => "Media Foundation"; @@ -25,7 +27,7 @@ public sealed class MediaFoundationEncoderProvider : IVideoEncoderProvider if (!SettingsSupported(settings)) return false; foreach (var c in Capabilities) { - if (Fits(c.Codec, c.Name, c.Hardware, c.SupportsTenBit, settings) && settings.Width <= c.MaxWidth && settings.Height <= c.MaxHeight) return true; + if (Fits(c.Codec, c.Name, c.Hardware, c.SupportsTenBit, settings) && EncoderSelector.Takes(c, settings.Width, settings.Height)) return true; } return false; } diff --git a/src/Chronos.Core/Export/EncoderSelector.cs b/src/Chronos.Core/Export/EncoderSelector.cs index 801baeb4..b4338b94 100644 --- a/src/Chronos.Core/Export/EncoderSelector.cs +++ b/src/Chronos.Core/Export/EncoderSelector.cs @@ -182,6 +182,17 @@ public static class EncoderSelector return new EncoderSelection { Status = EncoderSelectionStatus.Unavailable, Settings = settings, Notices = notices }; } + /// + /// The capability's size limit takes a × frame either way round: the + /// limit is a long edge × short edge (owner review 1, phase 4: Media Foundation's software H.264 encoder, announced as + /// 4096×2304, writes portrait UHD 2160×3840 too; checked on a real encode). + /// + public static bool Takes(EncoderCapability capability, int width, int height) + { + ArgumentNullException.ThrowIfNull(capability); + return (width <= capability.MaxWidth && height <= capability.MaxHeight) || (height <= capability.MaxWidth && width <= capability.MaxHeight); + } + private static List<(IVideoEncoderProvider Provider, EncoderCapability Capability)> Candidates(IReadOnlyList providers, VideoCodec codec) => [.. providers.SelectMany(p => p.Capabilities.Where(c => c.Codec == codec).Select(c => (p, c)))]; @@ -203,12 +214,16 @@ public static class EncoderSelector s = s with { Width = check.SuggestedWidth, Height = check.SuggestedHeight }; notices.Add(new EncoderNotice(EncoderNotices.ResolutionCappedByLevel, check.HighestLevel.Name, Size(s))); } - if (s.Width > capability.MaxWidth || s.Height > capability.MaxHeight) + if (!Takes(capability, s.Width, s.Height)) { - var scale = double.Min((double)capability.MaxWidth / s.Width, (double)capability.MaxHeight / s.Height); + // The limit turned to the frame's orientation (long edge × short edge, Takes). + var (maxWidth, maxHeight) = (s.Width >= s.Height) == (capability.MaxWidth >= capability.MaxHeight) + ? (capability.MaxWidth, capability.MaxHeight) + : (capability.MaxHeight, capability.MaxWidth); + var scale = double.Min((double)maxWidth / s.Width, (double)maxHeight / s.Height); var width = int.Max(2, (int)(s.Width * scale) & ~1); var height = int.Max(2, (int)double.Round(width * (double)s.Height / s.Width / 2) * 2); - if (height > capability.MaxHeight) height = capability.MaxHeight & ~1; + if (height > maxHeight) height = maxHeight & ~1; s = s with { Width = width, Height = height }; notices.Add(new EncoderNotice(EncoderNotices.ResolutionCappedByEncoder, capability.Name, Size(s))); } diff --git a/src/Chronos.Data/Processing/Jobs/ManagedRenderJobHandler.cs b/src/Chronos.Data/Processing/Jobs/ManagedRenderJobHandler.cs index 63c6c6b2..56a409eb 100644 --- a/src/Chronos.Data/Processing/Jobs/ManagedRenderJobHandler.cs +++ b/src/Chronos.Data/Processing/Jobs/ManagedRenderJobHandler.cs @@ -2,6 +2,8 @@ using Chronos.Core.Diagnostics; using Chronos.Core.Export; using Chronos.Core.Localization; using Chronos.Core.Model; +using Chronos.Core.Project; +using Chronos.Core.Render; using Chronos.Core.Video; using Chronos.Data.Jobs; using Chronos.Protocol; @@ -32,8 +34,11 @@ internal sealed class ManagedRenderJobHandler(RenderJobHandler render, Processin try { parameters = ProcessingJobs.ReadParameters(job.ParametersJson); - // Most jobs are decided by the main codec, before the export is read. - if (Produces(parameters.Codec) && RenderJobHandler.Targets(ProcessingJobs.ReadExport(parameters.PresetJson), parameters.PresetName).All(t => Produces(t.Export))) + // Most jobs are decided by the main codec, before the export is read. The job's own output is checked at the size the + // job names (the server's: the chosen proportions fitted in a fixed-size preset, owner review 1, phase 4). + if (Produces(parameters.Codec) && RenderJobHandler.Targets(ProcessingJobs.ReadExport(parameters.PresetJson), parameters.PresetName) + .Select((t, i) => i == 0 && t.Export is { Width: > 0, Height: > 0 } ? Produces(t.Export, parameters.Width, parameters.Height) : Produces(t.Export)) + .All(produced => produced)) return true; } catch (Exception ex) when (ex is not OperationCanceledException) @@ -55,7 +60,7 @@ internal sealed class ManagedRenderJobHandler(RenderJobHandler render, Processin } public Task RunAsync(JobContext context) => - render.RunAsync(context, target => Produces(target.Export) ? target : Fallback(context, target)); + render.RunAsync(context, (target, project) => Produces(target.Export, project) ? target : Fallback(context, target)); /// The output as MJPEG in AVI with the managed preset's quality, at the size and frame rate asked for. private static ExportTarget Fallback(JobContext context, ExportTarget target) @@ -83,6 +88,21 @@ internal sealed class ManagedRenderJobHandler(RenderJobHandler render, Processin private bool Produces(string codec) => _capabilities.Any(c => string.Equals(ProcessingJobs.CodecName(c.Codec), codec, StringComparison.OrdinalIgnoreCase)); - private bool Produces(ResolvedExport export) => - _capabilities.Any(c => c.Codec == export.Codec && export.Width <= c.MaxWidth && export.Height <= c.MaxHeight); + /// A managed encoder writes at the preset's size (a free-size preset is not checked by size). + private bool Produces(ResolvedExport export) => Produces(export, export.Width, export.Height); + + /// + /// A managed encoder writes at the size gives it: a fixed-size preset + /// adapted to the chosen proportions (, 4K at 9:16 = 2160×3840). + /// + private bool Produces(ResolvedExport export, ProjectSettings project) + { + if (export is not { Width: > 0, Height: > 0 }) return Produces(export); + // A fixed-size preset's output does not depend on the source's size. + var (width, height) = RenderSize.Of(project.Camera, export, export.Width, export.Height); + return Produces(export, width, height); + } + + private bool Produces(ResolvedExport export, int width, int height) => + _capabilities.Any(c => c.Codec == export.Codec && EncoderSelector.Takes(c, width, height)); } diff --git a/src/Chronos.Data/Processing/Jobs/RenderJobHandler.cs b/src/Chronos.Data/Processing/Jobs/RenderJobHandler.cs index ecf525e1..8b25f92a 100644 --- a/src/Chronos.Data/Processing/Jobs/RenderJobHandler.cs +++ b/src/Chronos.Data/Processing/Jobs/RenderJobHandler.cs @@ -30,10 +30,13 @@ internal sealed class RenderJobHandler(ChronosDatabase db, ProcessingOptions opt { public JobKind Kind => JobKind.Render; - public Task RunAsync(JobContext context) => RunAsync(context, target => target); + public Task RunAsync(JobContext context) => RunAsync(context, (target, _) => target); - /// Runs the job with each output still to render passed through (the server's managed stand-ins). - internal async Task RunAsync(JobContext context, Func adapt) + /// + /// Runs the job with each output still to render passed through with the settings rendered (the + /// server's managed stand-ins, which look at the size those settings give the output). + /// + internal async Task RunAsync(JobContext context, Func adapt) { var cancellation = context.Cancellation; var (parameters, export) = ProcessingJobs.ReadRender(context.Job.ParametersJson); @@ -72,7 +75,7 @@ internal sealed class RenderJobHandler(ChronosDatabase db, ProcessingOptions opt Discard(output, keepOutput: recorded.Any(r => r.OutputPath == Stored(paths, output))); names.Release(output); // free again for the entry's new attempt } - var targets = all.Select((t, i) => (i, t)).Where(t => !done.Contains(t.i)).Select(t => (t.i, adapt(t.t))).ToList(); + var targets = all.Select((t, i) => (i, t)).Where(t => !done.Contains(t.i)).Select(t => (t.i, adapt(t.t, project))).ToList(); if (targets.Count == 0) return; var companions = await GprCompanions.FindAsync(db, archive, frames.Frames, cancellation).ConfigureAwait(false); diff --git a/src/Chronos.Data/Processing/ProcessingJobs.cs b/src/Chronos.Data/Processing/ProcessingJobs.cs index e9632516..008ed9ef 100644 --- a/src/Chronos.Data/Processing/ProcessingJobs.cs +++ b/src/Chronos.Data/Processing/ProcessingJobs.cs @@ -5,6 +5,7 @@ using Chronos.Core.Export; using Chronos.Core.Localization; using Chronos.Core.Model; using Chronos.Core.Project; +using Chronos.Core.Render; using Chronos.Core.Video; using Chronos.Data.Archiving.Jobs; using Chronos.Protocol; @@ -28,15 +29,17 @@ public static class ProcessingJobs /// /// of one sequence. (preset, overrides, name template, /// folder, batch) replaces the project's export section; embeds the settings to render, - /// null renders the sequence's current project. × - /// stands in for a preset without a fixed size, so render nodes can match the job by resolution. + /// null renders the sequence's current project. × (the + /// frames' Exif-oriented size) gives the output size of the settings rendered (; the sequence's + /// own when is null), so render nodes can match the job by resolution, and the settings + /// format the render needs (). /// public static ArchiveJobRequest Render(Sequence sequence, ExportSettings export, int sourceWidth, int sourceHeight, ProjectSettings? project = null, bool useProxies = false) { ArgumentNullException.ThrowIfNull(sequence); ArgumentNullException.ThrowIfNull(export); - var parameters = Parameters(export, sourceWidth, sourceHeight, project?.ToJson(), useProxies); + var parameters = Parameters(export, sourceWidth, sourceHeight, project?.ToJson(), useProxies, project ?? ReadSettings(sequence.SettingsJson)); return new ArchiveJobRequest(JobKind.Render, [sequence.Id], RenderLabel(sequence.DisplayTitle, parameters.PresetName), JsonSerializer.Serialize(parameters, ProtocolJson.Default.RenderJobParameters), JobPriority.User); } @@ -80,20 +83,22 @@ public static class ProcessingJobs /// /// A posted render job as one job per preset, as makes them (a render node uploads one file /// per job): the job itself when it has no batch; otherwise its own preset, then each batch preset with its own values, - /// all with the job's project and proxy choice. × stands in - /// for a batch preset without a fixed size. + /// all with the job's project and proxy choice. × (the + /// frames' Exif-oriented size) stands in for a batch preset without a fixed size. With the + /// the job renders, every job gets the output size and the settings format its render needs (); + /// without them, the job keeps the size it was posted with and each batch preset its preset's. /// /// The preset JSON is not an export section. - public static IReadOnlyList Split(RenderJobParameters parameters, int sourceWidth, int sourceHeight) + public static IReadOnlyList Split(RenderJobParameters parameters, int sourceWidth, int sourceHeight, ProjectSettings? settings = null) { ArgumentNullException.ThrowIfNull(parameters); var export = ReadExport(parameters.PresetJson); var batch = BatchPresets(export).ToList(); - if (batch.Count == 0) return [parameters]; + var own = batch.Count == 0 ? parameters : parameters with { PresetJson = JsonSerializer.Serialize(export with { Batch = [] }, ProcessingJson.Default.ExportSettings) }; return [ - parameters with { PresetJson = JsonSerializer.Serialize(export with { Batch = [] }, ProcessingJson.Default.ExportSettings) }, - .. batch.Select(id => Parameters(BatchExport(export, id), sourceWidth, sourceHeight, parameters.SettingsJson, parameters.UseProxies) with + settings is null ? own : Routed(own, settings, sourceWidth, sourceHeight), + .. batch.Select(id => Parameters(BatchExport(export, id), sourceWidth, sourceHeight, parameters.SettingsJson, parameters.UseProxies, settings) with { ProjectId = parameters.ProjectId, ProjectVersionId = parameters.ProjectVersionId, @@ -101,6 +106,61 @@ public static class ProcessingJobs ]; } + /// + /// A render job's parameters with the output size and the settings format a render of needs + /// (owner review 1, phase 4: the chosen proportions win over a fixed-size preset, a quarter turn swaps a source-sized + /// output). A fixed-size preset's box is the size the job was posted with: an older client posts the preset's, this one + /// the adapted size, which adapts to itself. Other presets are sized on the source. The format is at least the one the + /// job was posted with. + /// + /// The preset JSON is not an export section. + public static RenderJobParameters Routed(RenderJobParameters parameters, ProjectSettings settings, int sourceWidth, int sourceHeight) + { + ArgumentNullException.ThrowIfNull(parameters); + ArgumentNullException.ThrowIfNull(settings); + var resolved = ExportResolver.Resolve(ReadExport(parameters.PresetJson)); + var box = resolved.Width > 0 && resolved.Height > 0 && parameters.Width > 0 && parameters.Height > 0 + ? resolved with { Width = parameters.Width, Height = parameters.Height } + : resolved; + var (width, height) = Size(settings, box, sourceWidth, sourceHeight) ?? (parameters.Width, parameters.Height); + return parameters with + { + Width = width, + Height = height, + RequiredSettingsFormat = int.Max(parameters.RequiredSettingsFormat, RequiredFormat(settings, resolved, sourceWidth, sourceHeight)), + }; + } + + /// + /// The settings format a render of with and each of its batch + /// presets needs (the highest of them): what a server or a render node must read + /// to render it as this Chronos would. + /// + public static int RequiredFormat(ProjectSettings settings, ExportSettings export, int sourceWidth, int sourceHeight) + { + ArgumentNullException.ThrowIfNull(settings); + ArgumentNullException.ThrowIfNull(export); + return BatchPresets(export).Select(id => ExportResolver.Resolve(BatchExport(export, id))).Prepend(ExportResolver.Resolve(export)) + .Max(resolved => RequiredFormat(settings, resolved, sourceWidth, sourceHeight)); + } + + /// + /// Project settings read for their output size and format, a newer Chronos's too (its format stands); null when there + /// are none or the text is not a project. + /// + public static ProjectSettings? ReadSettings(string? settingsJson) + { + if (string.IsNullOrWhiteSpace(settingsJson)) return null; + try + { + return ProjectSettings.FromJson(settingsJson, allowNewer: true); + } + catch (JsonException) + { + return null; + } + } + /// /// One job per preset of : its preset, then each batch preset with its /// own values. For a server: a render node uploads one file per job, so a batch must not travel as one job. @@ -128,13 +188,27 @@ public static class ProcessingJobs internal static ExportSettings BatchExport(ExportSettings export, string presetId) => new() { Preset = new Auto().WithManual(presetId), NameTemplate = export.NameTemplate, Folder = export.Folder }; - private static RenderJobParameters Parameters(ExportSettings export, int sourceWidth, int sourceHeight, string? settingsJson, bool useProxies) + /// The settings rendered, for the output size and the format (else read from ); unknown, the preset's size. + private static RenderJobParameters Parameters(ExportSettings export, int sourceWidth, int sourceHeight, string? settingsJson, bool useProxies, + ProjectSettings? settings = null) { var resolved = ExportResolver.Resolve(export); + settings ??= ReadSettings(settingsJson); + var (width, height) = (settings is null ? null : Size(settings, resolved, sourceWidth, sourceHeight)) + ?? (resolved.Width > 0 ? resolved.Width : sourceWidth, resolved.Height > 0 ? resolved.Height : sourceHeight); + var format = settings is null ? ProjectSettings.BaseVersion : RequiredFormat(settings, resolved, sourceWidth, sourceHeight); return new RenderJobParameters(PresetName(resolved), JsonSerializer.Serialize(export, ProcessingJson.Default.ExportSettings), CodecName(resolved.Codec), - resolved.Width > 0 ? resolved.Width : sourceWidth, resolved.Height > 0 ? resolved.Height : sourceHeight, useProxies, settingsJson); + width, height, useProxies, settingsJson, RequiredSettingsFormat: format); } + /// The output size of with (); null without a source size. + private static (int Width, int Height)? Size(ProjectSettings settings, ResolvedExport export, int sourceWidth, int sourceHeight) => + sourceWidth > 0 && sourceHeight > 0 ? RenderSize.Of(settings.Camera, export, sourceWidth, sourceHeight) : null; + + /// ; without a source size, the format the settings are written in. + private static int RequiredFormat(ProjectSettings settings, ResolvedExport export, int sourceWidth, int sourceHeight) => + sourceWidth > 0 && sourceHeight > 0 ? RenderRules.RequiredFormat(settings, export, sourceWidth, sourceHeight) : settings.WrittenFormat; + /// Codec name in job parameters and render-node capabilities ("h264", "hevc", "mjpeg", "pngsequence"…). public static string CodecName(VideoCodec codec) => codec.ToString().ToLowerInvariant(); diff --git a/src/Chronos.Data/Processing/ProjectStore.cs b/src/Chronos.Data/Processing/ProjectStore.cs index 915e4648..8d9dafd3 100644 --- a/src/Chronos.Data/Processing/ProjectStore.cs +++ b/src/Chronos.Data/Processing/ProjectStore.cs @@ -1,6 +1,7 @@ using System.Text.Json; using Chronos.Core.Archiving; using Chronos.Core.IO; +using Chronos.Core.Localization; using Chronos.Core.Model; using Chronos.Core.Project; using Chronos.Data.Archiving; @@ -22,7 +23,10 @@ internal sealed class ProjectStore(ChronosDatabase db, SequenceWriter writer) public static ProjectSettings Read(Sequence sequence, SequencePaths folder) => Parse(sequence.SettingsJson ?? (File.Exists(folder.ProjectPath) ? AtomicFile.ReadAllText(folder.ProjectPath) : null)); - /// The settings are not valid JSON or come from a newer Chronos. + /// + /// The settings are not valid JSON, or come from a newer Chronos (Errors.Render.NewerSettings, which a render's + /// job shows as its error). + /// public static ProjectSettings Parse(string? json) { if (string.IsNullOrWhiteSpace(json)) return new ProjectSettings(); @@ -34,6 +38,11 @@ internal sealed class ProjectStore(ChronosDatabase db, SequenceWriter writer) { throw new InvalidDataException(ex.Message, ex); } + catch (InvalidDataException ex) + { + // A settings format above this Chronos's (owner review 1, phase 4: a server, a node or a desktop that is older). + throw new InvalidDataException(Loc.L("Errors.Render.NewerSettings"), ex); + } } /// Applies to the settings in force and stores the result when it differs; false when nothing changed. diff --git a/src/Chronos.Data/Processing/RenderNodeClient.cs b/src/Chronos.Data/Processing/RenderNodeClient.cs index 377eb994..5108bab3 100644 --- a/src/Chronos.Data/Processing/RenderNodeClient.cs +++ b/src/Chronos.Data/Processing/RenderNodeClient.cs @@ -7,6 +7,7 @@ using Chronos.Core.Export; using Chronos.Core.Imaging; using Chronos.Core.Localization; using Chronos.Core.Model; +using Chronos.Core.Project; using Chronos.Core.Render; using Chronos.Core.Video; using Chronos.Data.Archiving; @@ -117,10 +118,11 @@ public sealed class RenderNodeClient : IAsyncDisposable /// Registers this node (again) and returns its id. public async Task RegisterAsync(CancellationToken cancellationToken) { - // Each encoder carries its own limit; the node-wide one (for servers that read only that) is the largest offered. + // Each encoder carries its own limit; the node-wide one (for servers that read only that) is the largest offered. The + // settings format the node reads keeps it from jobs it would render otherwise than asked (owner review 1, phase 4). var encoders = Encoders; var request = new RegisterNodeRequest(_options.ClientName, encoders, encoders.Select(e => e.MaxWidth).DefaultIfEmpty().Max(), - encoders.Select(e => e.MaxHeight).DefaultIfEmpty().Max()); + encoders.Select(e => e.MaxHeight).DefaultIfEmpty().Max(), ProjectSettings.CurrentVersion); var node = await _client.RegisterNodeAsync(request, cancellationToken).ConfigureAwait(false); _nodeId = node.Id; return node.Id; diff --git a/src/Chronos.Data/Projects/IProjectHome.cs b/src/Chronos.Data/Projects/IProjectHome.cs index e8bbd859..6776bd4f 100644 --- a/src/Chronos.Data/Projects/IProjectHome.cs +++ b/src/Chronos.Data/Projects/IProjectHome.cs @@ -76,6 +76,12 @@ public interface IProjectHome /// then. Null from a home that keeps none: the frames are read every time. /// Task FramesStampAsync(CancellationToken cancellationToken) => Task.FromResult(null); + + /// + /// The highest project settings format the home stores and renders (owner review 1, phase 4); a version of a higher one + /// is not pushed to it (an older server refuses it, 422 newer_format). This PC's archive reads what this Chronos does. + /// + int SettingsFormat => ProjectSettings.CurrentVersion; } /// A sequence of this PC's archive as its projects' home: the archive's . diff --git a/src/Chronos.Data/Projects/ServerProjectHome.cs b/src/Chronos.Data/Projects/ServerProjectHome.cs index 30386207..7f97ddb3 100644 --- a/src/Chronos.Data/Projects/ServerProjectHome.cs +++ b/src/Chronos.Data/Projects/ServerProjectHome.cs @@ -27,15 +27,20 @@ public sealed class ServerProjectHome : IProjectHome private const int MaxPage = 200; /// The server's name as its server/info gives it, for the project file; the address's host when null. - public ServerProjectHome(ChronosClient client, Guid sequenceId, string? serverName = null) + /// The settings format its server/info announces (); null, an older server's 1. + public ServerProjectHome(ChronosClient client, Guid sequenceId, string? serverName = null, int? settingsFormat = null) { ArgumentNullException.ThrowIfNull(client); Client = client; SequenceId = sequenceId; Server = SyncService.ServerKey(client.BaseAddress); ServerName = string.IsNullOrWhiteSpace(serverName) ? client.BaseAddress.Host : serverName.Trim(); + SettingsFormat = settingsFormat is > 0 and var format ? format : ProjectSettings.BaseVersion; } + /// The highest settings format the server reads (owner review 1, phase 4); 1 for an older server, which does not announce it. + public int SettingsFormat { get; } + public ChronosClient Client { get; } /// The server's sequence. diff --git a/src/Chronos.Server/Api/JobEndpoints.cs b/src/Chronos.Server/Api/JobEndpoints.cs index e919a9a7..9f40e190 100644 --- a/src/Chronos.Server/Api/JobEndpoints.cs +++ b/src/Chronos.Server/Api/JobEndpoints.cs @@ -1,6 +1,7 @@ using System.Text.Json; using Microsoft.AspNetCore.Mvc; using Chronos.Core.Model; +using Chronos.Core.Project; using Chronos.Data; using Chronos.Data.Archiving; using Chronos.Data.Jobs; @@ -102,20 +103,20 @@ public static class JobEndpoints // A render of a project version (owner review 2, R8): of the sequence's projects, a project alone pinned to its head. var (render, missing) = await ProjectRenderAsync(db, archives, targets[0], parsed, ct); if (missing is not null) return ApiErrors.NotFound(context, missing); - var parametersJson = render == parsed ? request.ParametersJson : JsonSerializer.Serialize(render, ProtocolJson.Default.RenderJobParameters); - // A batch becomes one job per preset: a render node uploads one file per job (BRIEF §14.7). + // A batch becomes one job per preset: a render node uploads one file per job (BRIEF §14.7). Each gets the size and the + // settings format of the settings it renders (owner review 1, phase 4), so it goes only to a node that renders it as asked. IReadOnlyList each; try { var (width, height) = await SourceSizeAsync(db, targets[0], render, ct); - each = ProcessingJobs.Split(render, width, height); + each = ProcessingJobs.Split(render, width, height, await RenderedSettingsAsync(db, targets[0], render, ct)); } catch (InvalidDataException) { return ApiErrors.BadRequest(context, "invalid_parameters", "parametersJson"); } - queue = each.Count == 1 - ? [(ProcessingJobs.RenderLabel(label, render.PresetName), parametersJson)] + queue = each is [var one] && one == parsed + ? [(ProcessingJobs.RenderLabel(label, render.PresetName), request.ParametersJson)] : [.. each.Select(p => (ProcessingJobs.RenderLabel(label, p.PresetName), (string?)JsonSerializer.Serialize(p, ProtocolJson.Default.RenderJobParameters)))]; } else if (request.ParametersJson is not null && !IsJsonObject(request.ParametersJson)) @@ -186,8 +187,22 @@ public static class JobEndpoints } } + /// + /// The settings a render job renders, for its size and settings format: those it embeds, else the project version's it + /// names, else the sequence's own; a newer Chronos's too (their format stands). Null when they cannot be read (or the + /// version is not there): the job keeps the size it was posted with, and its render says why it cannot read them. + /// + internal static async Task RenderedSettingsAsync(ChronosDatabase db, Guid sequenceId, RenderJobParameters render, CancellationToken ct) + { + if (render.SettingsJson is { } embedded) return ProcessingJobs.ReadSettings(embedded); + if (render.ProjectVersionId is { } versionId) + return await db.Projects.GetVersionAsync(versionId, ct) is { } version ? ProcessingJobs.ReadSettings(version.SettingsJson) : null; + if (await db.Sequences.GetAsync(sequenceId, ct) is not { } sequence) return null; + return sequence.SettingsJson is null ? new ProjectSettings() : ProcessingJobs.ReadSettings(sequence.SettingsJson); + } + /// The oriented size of the sequence's first frame (what a preset without a fixed size renders at), else the job's size. - private static async Task<(int Width, int Height)> SourceSizeAsync(ChronosDatabase db, Guid sequenceId, RenderJobParameters render, CancellationToken ct) => + internal static async Task<(int Width, int Height)> SourceSizeAsync(ChronosDatabase db, Guid sequenceId, RenderJobParameters render, CancellationToken ct) => (await db.Frames.ListBySequenceAsync(sequenceId, limit: 1, cancellationToken: ct)).FirstOrDefault() is { Width: > 0, Height: > 0 } frame ? frame.Orientation is >= 5 and <= 8 ? (frame.Height, frame.Width) : (frame.Width, frame.Height) : (render.Width, render.Height); diff --git a/src/Chronos.Server/Api/SystemEndpoints.cs b/src/Chronos.Server/Api/SystemEndpoints.cs index 1525d7c0..949e8e8e 100644 --- a/src/Chronos.Server/Api/SystemEndpoints.cs +++ b/src/Chronos.Server/Api/SystemEndpoints.cs @@ -2,6 +2,7 @@ using System.Globalization; using System.Reflection; using Chronos.Core.Imaging; using Chronos.Core.Localization; +using Chronos.Core.Project; using Chronos.Data; using Chronos.Protocol; using Chronos.Server.Configuration; @@ -47,7 +48,8 @@ public static class SystemEndpoints state.TlsFingerprint, ["jpeg", "png", "tiff", "dng", "cr2", "cr3", "nef", "arw", "raf", "orf", "rw2", "pef", "heic", "webp"], new ServerLimits(options.MaxUploadBytes, options.ChunkSizeBytes, PasswordHasher.MinimumLength), - PhotoExtensions), ProtocolJson.Default.ServerInfo)).AllowAnonymousCaller(); + PhotoExtensions, + ProjectSettings.CurrentVersion), ProtocolJson.Default.ServerInfo)).AllowAnonymousCaller(); api.MapGet("/i18n/{culture}", (string culture) => { diff --git a/src/Chronos.Server/Jobs/RenderNodeAvailability.cs b/src/Chronos.Server/Jobs/RenderNodeAvailability.cs index f2702816..c19712d4 100644 --- a/src/Chronos.Server/Jobs/RenderNodeAvailability.cs +++ b/src/Chronos.Server/Jobs/RenderNodeAvailability.cs @@ -11,8 +11,9 @@ namespace Chronos.Server.Jobs; /// Whether a queued render job that needs a platform encoder still waits for a render node (BRIEF §14.7); once it does /// not, the server's own engine renders it in a managed format. A job waits at least as long as a node may stay silent /// (), so a node that is restarting or reconnecting takes it back, and -/// then as long as an online node can encode its codec at its size. The server's render handler asks for every queued -/// job it examines, so the online nodes are read at most once a second. +/// then as long as an online node reads the settings format it needs () +/// and can encode its codec at its size. The server's render handler asks for every queued job it examines, so the online +/// nodes are read at most once a second. /// public sealed class RenderNodeAvailability(ChronosDatabase db, ServerOptions options, TimeProvider time) { @@ -34,7 +35,7 @@ public sealed class RenderNodeAvailability(ChronosDatabase db, ServerOptions opt snapshot = new Snapshot(Stopwatch.GetTimestamp(), [.. online.Select(Dto.Capabilities).OfType()]); Volatile.Write(ref _snapshot, snapshot); } - return snapshot.Nodes.Any(node => RenderNodeService.CanEncode(node, parameters)); + return snapshot.Nodes.Any(node => RenderNodeService.Renders(node, parameters)); } /// What the online nodes announced, and when it was read (a timestamp). diff --git a/src/Chronos.Server/Jobs/RenderNodeService.cs b/src/Chronos.Server/Jobs/RenderNodeService.cs index 668dd023..0b3318ee 100644 --- a/src/Chronos.Server/Jobs/RenderNodeService.cs +++ b/src/Chronos.Server/Jobs/RenderNodeService.cs @@ -6,6 +6,7 @@ using Chronos.Core.Model; using Chronos.Core.Project; using Chronos.Data; using Chronos.Data.Jobs; +using Chronos.Data.Processing; using Chronos.Data.Projects; using Chronos.Data.Repositories; using Chronos.Protocol; @@ -86,7 +87,10 @@ public sealed class RenderNodeService( // A render the server's own engine started (it left a checkpoint) resumes there: only it knows that attempt's // leftovers and the batch entries it recorded. if (queued.Checkpoint is not null) continue; - if (JobEndpoints.ParseRender(queued.ParametersJson) is not { } parameters || capabilities is null || !CanEncode(capabilities, parameters)) continue; + if (JobEndpoints.ParseRender(queued.ParametersJson) is not { } parameters || capabilities is null || !Renders(capabilities, parameters)) continue; + // What the job renders now decides too (owner review 1, phase 4): the sequence's own settings may have changed + // since it was queued, and a job of an older server carries no settings format. + if (await RoutedAsync(queued, parameters, cancellationToken) is { } routed && !Renders(capabilities, routed)) continue; if (await db.Jobs.TryClaimAsync(queued.Id, NodeKey(node), cancellationToken) is not { } job) continue; jobs.Publish(job); if (job.TargetIds.Count != 1 || await db.Sequences.GetAsync(job.TargetIds[0], cancellationToken) is not { } sequence) @@ -134,6 +138,27 @@ public sealed class RenderNodeService( : null; } + /// + /// The job's parameters with the output size and settings format of the settings a node would render now + /// (); null when they cannot be told (no sequence, settings that cannot be read, a + /// version that is not the sequence's), which the claim reports as it does today. + /// + private async Task RoutedAsync(Job queued, RenderJobParameters parameters, CancellationToken cancellationToken) + { + if (queued.TargetIds is not [var sequenceId] || await db.Sequences.GetAsync(sequenceId, cancellationToken) is not { } sequence) return null; + if (await SettingsAsync(parameters, sequence, cancellationToken) is not { } settings || ProcessingJobs.ReadSettings(settings.Json) is not { } read) return null; + try + { + var (width, height) = await JobEndpoints.SourceSizeAsync(db, sequence.Id, parameters, cancellationToken); + return ProcessingJobs.Routed(parameters, read, width, height); + } + catch (InvalidDataException) + { + // Not an export section: the render says so. + return null; + } + } + /// Settings with positions that wait for their photos (a project version's portable form, settings synced from another copy). private static bool Waits(string settingsJson) => settingsJson.Contains("pendingFrames", StringComparison.OrdinalIgnoreCase); @@ -311,14 +336,25 @@ public sealed class RenderNodeService( if (state != node.State) events.Publish(new NodeStateEvent(node.Id, node.ClientName, state)); } + /// + /// The node renders the job as asked: it reads the settings format the job needs (, + /// 1 for an older node) and can encode it (). + /// + internal static bool Renders(RegisterNodeRequest node, RenderJobParameters job) => job.RequiredSettingsFormat <= node.SettingsFormat && CanEncode(node, job); + /// /// The node has an encoder for the job's codec that handles its resolution: the encoder's own limit, else the node's - /// (0 = no announced limit). + /// (0 = no announced limit). A node of settings format 2 or more takes the limit as a long edge × short edge, as its + /// encoders do (: portrait UHD on a 4096×2304 encoder); an older node + /// would shrink a portrait frame to the limit as announced, so it is held to it. /// internal static bool CanEncode(RegisterNodeRequest node, RenderJobParameters job) => node.Encoders.Any(e => string.Equals(e.Codec, job.Codec, StringComparison.OrdinalIgnoreCase) - && Fits(job.Width, e.MaxWidth > 0 ? e.MaxWidth : node.MaxWidth) - && Fits(job.Height, e.MaxHeight > 0 ? e.MaxHeight : node.MaxHeight)); + && Fits(job.Width, job.Height, e.MaxWidth > 0 ? e.MaxWidth : node.MaxWidth, e.MaxHeight > 0 ? e.MaxHeight : node.MaxHeight, + eitherWay: node.SettingsFormat >= 2)); + + private static bool Fits(int width, int height, int maxWidth, int maxHeight, bool eitherWay) => + (Fits(width, maxWidth) && Fits(height, maxHeight)) || (eitherWay && Fits(height, maxWidth) && Fits(width, maxHeight)); private static bool Fits(int size, int limit) => limit <= 0 || size <= limit; diff --git a/tests/Chronos.App.Tests/Workspace/ServerHomeTests.cs b/tests/Chronos.App.Tests/Workspace/ServerHomeTests.cs index a9755843..43343f78 100644 --- a/tests/Chronos.App.Tests/Workspace/ServerHomeTests.cs +++ b/tests/Chronos.App.Tests/Workspace/ServerHomeTests.cs @@ -244,6 +244,55 @@ public sealed class ServerHomeTests(TestContext context) } } + [Test] + public async Task AServerThatReadsAnOlderFormatIsNotSentSettingsItCannotRead() + { + Assert.Null(ServerJobs.NeedsNewerServer(ProjectSettings.BaseVersion, ProjectSettings.BaseVersion)); + Assert.Equal(Loc.L("Tooltips.Server.NeedsNewerServer"), ServerJobs.NeedsNewerServer(2, ProjectSettings.BaseVersion)); + // A server that says no format reads the base one; a session-less home too. + Assert.Equal(ProjectSettings.BaseVersion, Chronos.App.Server.SyncHost.SettingsFormatOf(new ServerInfo("Studio NAS", "1.3.0", 1, [], null, [], new ServerLimits(0, 0)))); + Assert.Equal(ProjectSettings.BaseVersion, Chronos.App.Server.SyncHost.SettingsFormatOf(null)); + Assert.Equal(ProjectSettings.CurrentVersion, Chronos.App.Server.SyncHost.SettingsFormatOf( + new ServerInfo("Studio NAS", "1.4.0", 1, [], null, [], new ServerLimits(0, 0), SettingsFormat: ProjectSettings.CurrentVersion))); + + await using var bed = await WorkspaceTestBed.CreateAsync(context, frames: 4); + var host = bed.Host(new AppSettings { WorkspaceAutoSync = false, WorkspaceVersionOnRender = false }); + var server = new FakeServer(bed) { SettingsFormat = ProjectSettings.BaseVersion }; + Connect(host, server); + var projectId = await CheckoutAsync(bed, host); + var source = new WorkspaceSequenceSource(host.Sync, projectId, bed.Db, DecoderFactory.CreateClient(null), bed.Engine, + new WorkspaceServerHome(() => null, () => null)); + var content = await source.LoadAsync(bed.Ct); + var session = new EditorSession(source, content, [], TimeSpan.FromMilliseconds(20)); + var ui = new RecordingUi(); + await using (session) + using (var menu = new ProjectMenuViewModel(host, source, session, ui)) + { + await menu.RefreshAsync(); + // A change of the base format goes to the older server as before. + Assert.True(session.SetHero(2)); + Assert.Null(menu.PushBlockedReason); + Assert.True(menu.SyncCommand.CanExecute(null)); + + // A turn needs format 2: Sync is disabled saying why, and a render of it is held. + Assert.True(session.Edit(p => p with { Camera = p.Camera with { Rotate = 90 } })); + Assert.True(await session.FlushAsync()); + Assert.Equal(Loc.L("Tooltips.Server.NeedsNewerServer"), menu.PushBlockedReason); + Assert.Equal(menu.PushBlockedReason, menu.SyncToolTip); + Assert.False(menu.SyncCommand.CanExecute(null)); + Assert.False(await menu.ReadyToRenderAsync(), "the server cannot read it"); + Assert.Equal((Loc.L("Editor.Workspace.RenderHeld"), true), (ui.Toasts[^1].Title, ui.Toasts[^1].IsError)); + var before = host.Store.Find(projectId)!.ReadBundle().Workspace!.Base!.VersionId; + await menu.SyncAsync(); + Assert.Equal(before, host.Store.Find(projectId)!.ReadBundle().Workspace!.Base!.VersionId, "nothing was sent"); + + // A server of this format takes it. + server.SettingsFormat = ProjectSettings.CurrentVersion; + Assert.Null(menu.PushBlockedReason); + Assert.True(menu.SyncCommand.CanExecute(null)); + } + } + /// The home a workspace project of the test sequence names on . private static BundleHome ServerHome(WorkspaceTestBed bed) => new(BundleHome.ServerKind, bed.SequenceId, null, Server); @@ -276,6 +325,9 @@ public sealed class ServerHomeTests(TestContext context) public HashSet Deleted { get; } = []; + /// The settings format the server reads, as its info says. + public int SettingsFormat { get; set; } = ProjectSettings.CurrentVersion; + public async Task SequenceAsync(CancellationToken cancellationToken) { var sequence = await Reach(() => Inner.SequenceAsync(cancellationToken)); diff --git a/tests/Chronos.Core.Tests/Export/EncoderSelectorTests.cs b/tests/Chronos.Core.Tests/Export/EncoderSelectorTests.cs index c3347578..8185b09d 100644 --- a/tests/Chronos.Core.Tests/Export/EncoderSelectorTests.cs +++ b/tests/Chronos.Core.Tests/Export/EncoderSelectorTests.cs @@ -128,6 +128,22 @@ public sealed class EncoderSelectorTests Assert.Equal((1080, 606), (odd.Settings.Width, odd.Settings.Height)); } + [Test] + public void APortraitFrameReadsTheLimitTurned() + { + // An encoder announced as 4096×2304 takes 4K at 9:16: its limit is long edge × short edge. + var portrait = EncoderSelector.Select(Request(VideoCodec.H264, 2160, 3840), Client(SoftwareH264), EncoderHost.Client); + Assert.Equal((2160, 3840), (portrait.Settings.Width, portrait.Settings.Height)); + Assert.False(portrait.Notices.Any(n => n.Key is EncoderNotices.ResolutionCappedByEncoder or EncoderNotices.ResolutionCappedByLevel), "not capped"); + + var capped = EncoderSelector.Select(Request(VideoCodec.H264, 4320, 7680), Client(SoftwareH264), EncoderHost.Client); + Assert.Equal((2304, 4096), (capped.Settings.Width, capped.Settings.Height)); + Assert.True(capped.Notices.Any(n => n.Key == EncoderNotices.ResolutionCappedByEncoder)); + + Assert.True(EncoderSelector.Takes(SoftwareH264, 2304, 4096)); + Assert.False(EncoderSelector.Takes(SoftwareH264, 2400, 3840), "the short edge is over the limit"); + } + [Test] public void BitRateIsCappedByTheHighestLevel() { diff --git a/tests/Chronos.Core.Tests/Render/CapturingEncoderProvider.cs b/tests/Chronos.Core.Tests/Render/CapturingEncoderProvider.cs index 9ce98ac7..9d4fc14c 100644 --- a/tests/Chronos.Core.Tests/Render/CapturingEncoderProvider.cs +++ b/tests/Chronos.Core.Tests/Render/CapturingEncoderProvider.cs @@ -1,3 +1,4 @@ +using Chronos.Core.Export; using Chronos.Core.Imaging; using Chronos.Core.Video; @@ -26,7 +27,7 @@ internal sealed class CapturingEncoderProvider(string name, params EncoderCapabi 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)); + c.Codec == settings.Codec && EncoderSelector.Takes(c, settings.Width, settings.Height) && (settings.BitDepth == 8 || c.SupportsTenBit)); public IVideoEncoderSession Open(EncodeSettings settings) { diff --git a/tests/Chronos.Core.Windows.Tests/MediaFoundation/MediaFoundationEncoderTests.cs b/tests/Chronos.Core.Windows.Tests/MediaFoundation/MediaFoundationEncoderTests.cs index dd24d666..8a9bc2b7 100644 --- a/tests/Chronos.Core.Windows.Tests/MediaFoundation/MediaFoundationEncoderTests.cs +++ b/tests/Chronos.Core.Windows.Tests/MediaFoundation/MediaFoundationEncoderTests.cs @@ -1,3 +1,4 @@ +using Chronos.Core.Export; using Chronos.Core.Imaging; using Chronos.Core.Video; using Chronos.Core.Windows.MediaFoundation; @@ -143,6 +144,61 @@ public sealed class MediaFoundationEncoderTests(TestContext context) Assert.Equal(60, report.FrameCount); } + /// + /// Owner review 1, phase 4: 4K at 9:16 is 2160×3840. Each H.264 encoder of this machine is asked for portrait UHD and + /// its landscape twin: none takes one and refuses the other, so the announced limits are a long edge × short edge + /// (), and portrait UHD is announced wherever landscape UHD is. Encoders that refuse + /// both (Microsoft's DX12 encoder on some GPUs) are left to the provider, which then opens the next one. + /// + [Test] + [WindowsOnly] + [Timeout(180_000)] + public void PortraitUhdH264EncodesWhereLandscapeDoes() + { + RequireMediaFoundation(); + var provider = new MediaFoundationEncoderProvider(); + var h264 = provider.Capabilities.Where(c => c.Codec == VideoCodec.H264).ToList(); + if (h264.Count == 0) TestContext.Skip("No H.264 encoder on this machine"); + foreach (var capability in h264) + { + var (portrait, landscape) = (Accepts(capability, 2160, 3840), Accepts(capability, 3840, 2160)); + Assert.Equal(landscape, portrait, $"{capability.Name} takes portrait UHD as it takes landscape UHD"); + Assert.Equal(EncoderSelector.Takes(capability, 3840, 2160), EncoderSelector.Takes(capability, 2160, 3840), "announced either way round"); + } + var request = Settings(Path.Combine(context.CreateTempDirectory(), "portrait.mp4"), VideoCodec.H264, 8); + Assert.Equal(provider.CanEncode(request with { Width = 3840, Height = 2160 }), provider.CanEncode(request with { Width = 2160, Height = 3840 })); + + bool Accepts(EncoderCapability capability, int width, int height) + { + var settings = Settings(Path.Combine(context.CreateTempDirectory(), $"{width}x{height}.mp4"), VideoCodec.H264, 8) with + { + Width = width, + Height = height, + EncoderName = capability.Name, + PreferHardware = capability.Hardware, + BitRate = 20_000_000, + }; + try + { + using (var session = provider.Open(settings)) + { + using var frame = new ImageBuffer(width, height); + for (var c = 0; c < 3; c++) frame.Plane(c).Fill(0.25f); + for (var f = 0; f < 3; f++) session.WriteFrame(frame, Frame30); + session.Complete(); + } + var report = Mp4Inspector.Inspect(settings.OutputPath); + context.Log($"{capability.Name} {width}×{height}: {report}"); + return report.IsValid && (report.Width, report.Height) == (width, height); + } + catch (NotSupportedException ex) + { + context.Log($"{capability.Name} {width}×{height}: refused ({ex.InnerException?.Message ?? ex.Message})"); + return false; + } + } + } + [Test] [WindowsOnly] [Timeout(60_000)] diff --git a/tests/Chronos.Data.Tests/Processing/ProjectStoreTests.cs b/tests/Chronos.Data.Tests/Processing/ProjectStoreTests.cs new file mode 100644 index 00000000..829bad62 --- /dev/null +++ b/tests/Chronos.Data.Tests/Processing/ProjectStoreTests.cs @@ -0,0 +1,54 @@ +using Chronos.Core.Diagnostics.Synthetic; +using Chronos.Core.Localization; +using Chronos.Core.Project; +using Chronos.Data.Archiving; +using Chronos.Data.Processing; +using Chronos.Testing; + +namespace Chronos.Data.Tests.Processing; + +/// +/// The project of a sequence as the engine reads and writes it (owner review 1, phase 4): a format-1 project stays format 1 +/// through every edit that uses nothing newer, a transform raises it to 2 and its removal brings it back, and settings of +/// a newer Chronos are refused saying so in the user's language. +/// +[Category("processing")] +public sealed class ProjectStoreTests(TestContext context) +{ + [Test] + public async Task AnEditNeverRaisesTheFormatOfAProjectThatUsesNothingNewer() + { + await using var fixture = await ProcessingFixture.CreateAsync(context); + var sequence = await fixture.AddAsync(SyntheticSequence.Create(new SyntheticSequenceOptions { Width = 64, Height = 48, FrameCount = 4 })); + var store = new ProjectStore(fixture.Db, new SequenceWriter(new ArchiveServiceOptions { Owner = "test", LeaseWait = TimeSpan.FromSeconds(20) })); + async Task StoredFormatAsync() + { + var row = (await fixture.Db.Sequences.GetAsync(sequence.Id, fixture.Ct))!; + var file = await File.ReadAllTextAsync(fixture.Folder(sequence).ProjectPath, fixture.Ct); + Assert.Equal(row.SettingsJson, file, "the index and project.json hold the same text"); + return ProjectContent.FormatOf(file); + } + + Assert.True(await store.UpdateAsync(fixture.Archive.Paths, sequence, p => p with { Exposure = p.Exposure with { ExposureOffsetEv = 0.5 } }, fixture.Ct)); + Assert.Equal(ProjectSettings.BaseVersion, await StoredFormatAsync()); + // A read project of the base format is never stamped higher, even when it says the highest this Chronos reads. + Assert.True(await store.UpdateAsync(fixture.Archive.Paths, sequence, p => p with { Version = ProjectSettings.CurrentVersion, HeroFrame = 2 }, fixture.Ct)); + Assert.Equal(ProjectSettings.BaseVersion, await StoredFormatAsync()); + + Assert.True(await store.UpdateAsync(fixture.Archive.Paths, sequence, p => p with { Camera = p.Camera with { Rotate = 90 } }, fixture.Ct)); + Assert.Equal(2, await StoredFormatAsync()); + Assert.True(await store.UpdateAsync(fixture.Archive.Paths, sequence, p => p with { Camera = p.Camera with { Rotate = 0 } }, fixture.Ct)); + Assert.Equal(ProjectSettings.BaseVersion, await StoredFormatAsync()); + Assert.False(await store.UpdateAsync(fixture.Archive.Paths, sequence, p => p with { Version = ProjectSettings.BaseVersion }, fixture.Ct), "nothing changed"); + } + + [Test] + public void SettingsOfANewerChronosAreRefusedSayingSo() + { + var refused = Assert.Throws(() => ProjectStore.Parse("""{"version":3,"camera":{"rotate":90}}""")); + Assert.Equal(Loc.L("Errors.Render.NewerSettings"), refused.Message); + Assert.Equal(90, ProjectStore.Parse("""{"version":2,"camera":{"rotate":90}}""").Camera.Rotate); + // Text that is not a project keeps its own reason. + Assert.NotEqual(Loc.L("Errors.Render.NewerSettings"), Assert.Throws(() => ProjectStore.Parse("[1]")).Message); + } +} diff --git a/tests/Chronos.Data.Tests/Processing/RenderJobTests.cs b/tests/Chronos.Data.Tests/Processing/RenderJobTests.cs index c764a9f1..e68c4aa5 100644 --- a/tests/Chronos.Data.Tests/Processing/RenderJobTests.cs +++ b/tests/Chronos.Data.Tests/Processing/RenderJobTests.cs @@ -599,6 +599,65 @@ public sealed class RenderJobTests(TestContext context) Assert.SequenceEqual(["mjpeg", "gif"], ProcessingJobs.Split(posted, 96, 64).Select(p => p.Codec)); } + /// + /// Owner review 1, phase 4: a render job carries the output size its settings give it (the chosen proportions win over a + /// fixed-size preset, a quarter turn swaps a source-sized output) and the settings format its render needs, so a render + /// node is matched to what it will write; a job posted by an older client gets both from the server (). + /// + [Test] + public void RenderJobsCarryTheSizeAndFormatOfTheirSettings() + { + var sequence = new Sequence { Id = Guid.CreateVersion7(), ArchiveId = Guid.CreateVersion7(), Folder = "Synthetic", Title = "Roof" }; + var uhd = new ExportSettings { Preset = new Auto().WithManual("4k-h264") }; + var portrait = new ProjectSettings { Camera = new CameraSettings { Aspect = new Auto().WithManual("9:16") } }; + static Chronos.Protocol.RenderJobParameters Read(Chronos.Data.Archiving.Jobs.ArchiveJobRequest request) => ProcessingJobs.ReadRender(request.ParametersJson).Parameters; + + var adapted = Read(ProcessingJobs.Render(sequence, uhd, 6000, 4000, portrait)); + Assert.Equal((2160, 3840, 2), (adapted.Width, adapted.Height, adapted.RequiredSettingsFormat)); + var source = Read(ProcessingJobs.Render(sequence, uhd, 6000, 4000, new ProjectSettings())); + Assert.Equal((3840, 2160, 1), (source.Width, source.Height, source.RequiredSettingsFormat)); + // A source-sized output turns with the picture; the turn needs format 2. + var png = new ExportSettings { Preset = new Auto().WithManual("png-sequence") }; + var turned = Read(ProcessingJobs.Render(sequence, png, 6000, 4000, new ProjectSettings { Camera = new CameraSettings { Rotate = 90 } })); + Assert.Equal((4000, 6000, 2), (turned.Width, turned.Height, turned.RequiredSettingsFormat)); + // The sequence's own settings when the job embeds none. + var own = Read(ProcessingJobs.Render(sequence with { SettingsJson = portrait.ToJson() }, uhd, 6000, 4000)); + Assert.Equal((2160, 3840, 2), (own.Width, own.Height, own.RequiredSettingsFormat)); + + // Posted by an older client (the preset's size, no format) with a batch: the server sizes each job for the settings. + var posted = Read(ProcessingJobs.Render(sequence, uhd with { Batch = ["1080p-h264"] }, 6000, 4000)) with { SettingsJson = portrait.ToJson() }; + Assert.Equal((3840, 2160, 1), (posted.Width, posted.Height, posted.RequiredSettingsFormat)); + var split = ProcessingJobs.Split(posted, 6000, 4000, portrait); + Assert.SequenceEqual([(2160, 3840, 2), (1080, 1920, 2)], split.Select(p => (p.Width, p.Height, p.RequiredSettingsFormat))); + Assert.Equal(split[0], ProcessingJobs.Routed(split[0], portrait, 6000, 4000), "a size already adapted stays"); + Assert.Equal((3840, 2160, 1), ProcessingJobs.Split(posted, 6000, 4000).Select(p => (p.Width, p.Height, p.RequiredSettingsFormat)).First(), + "settings that cannot be read keep the size the job was posted with"); + // What the desktop asks of a server before a render: the highest format of the export and its batch. + Assert.Equal(1, ProcessingJobs.RequiredFormat(portrait, png, 6000, 4000), "a source-sized output keeps the proportions"); + Assert.Equal(2, ProcessingJobs.RequiredFormat(portrait, png with { Batch = ["4k-h264"] }, 6000, 4000)); + } + + /// Settings of a newer Chronos fail the render with the reason in the user's language, never as an English message. + [Test] + public async Task AnOverFormatRenderFailsSayingANewerChronosIsNeeded() + { + await using var fx = await ProcessingFixture.CreateAsync(context); + var sequence = await fx.AddAsync(Scene()); + await fx.StartEngineAsync(); + + var parameters = ProcessingJobs.ReadRender(ProcessingJobs.Render(sequence, Mjpeg, 96, 64).ParametersJson).Parameters with + { + SettingsJson = """{"version":3,"camera":{"rotate":90},"grade":{"lut":"teal"}}""", + }; + var request = new Chronos.Data.Archiving.Jobs.ArchiveJobRequest(JobKind.Render, [sequence.Id], "newer", + System.Text.Json.JsonSerializer.Serialize(parameters, Chronos.Protocol.ProtocolJson.Default.RenderJobParameters), JobPriority.User); + Assert.Equal(3, ProcessingJobs.ReadRender(ProcessingJobs.Render(sequence, Mjpeg, 96, 64, + ProjectSettings.FromJson(parameters.SettingsJson, allowNewer: true)).ParametersJson).Parameters.RequiredSettingsFormat, "routed to none older"); + var failed = await fx.WaitForAsync((await fx.EnqueueAsync(request)).Id, JobState.Failed); + Assert.Equal(Loc.L("Errors.Render.NewerSettings"), failed.Error); + Assert.Empty(await fx.Db.Renders.ListBySequenceAsync(sequence.Id, fx.Ct)); + } + /// MJPEG job parameters whose preset JSON is . private static string WithPreset(string presetJson) => System.Text.Json.JsonSerializer.Serialize( new Chronos.Protocol.RenderJobParameters("MJPEG", presetJson, "mjpeg", 96, 64, false, null), Chronos.Protocol.ProtocolJson.Default.RenderJobParameters); diff --git a/tests/Chronos.Server.Tests/Api/ProjectTests.cs b/tests/Chronos.Server.Tests/Api/ProjectTests.cs index 67648ae8..6b53274c 100644 --- a/tests/Chronos.Server.Tests/Api/ProjectTests.cs +++ b/tests/Chronos.Server.Tests/Api/ProjectTests.cs @@ -498,6 +498,42 @@ public sealed class ProjectTests(TestContext context) Assert.Contains(ServerCapabilities.Projects, (await client.GetInfoAsync(context.Cancellation)).Capabilities); } + /// + /// Owner review 1, phase 4: the server announces the settings format it reads, and takes the versions of the formats it + /// reads (1, and 2 with a turn or a look), refusing a newer one with 422 newer_format, nothing stored. + /// + [Test] + public async Task TheServerTakesTheSettingsFormatsItAnnounces() + { + await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register); + using (var anonymous = new ChronosClient(server.HttpBase, new CertificatePinning([]), "tests")) + Assert.Equal(ProjectSettings.CurrentVersion, (await anonymous.GetInfoAsync(context.Cancellation)).SettingsFormat); + Assert.Equal(2, ProjectSettings.CurrentVersion); + + var (http, _, token) = await server.UserAsync("alberto", "editor"); + using var alberto = Client(server, token); + var sid = await SequenceAsync(server, token, 2); + var main = (await alberto.ListProjectsAsync(sid)).Single(p => p.IsMain); + var head = await alberto.GetProjectHeadAsync(sid, main.Id); + var home = ProjectWire.ToHomeFrames(await alberto.GetProjectFramesAsync(sid)); + + var plain = Version(head, new ProjectSettings { Exposure = new ExposureSettings { ExposureOffsetEv = 0.3 } }, home); + Assert.Equal(1, plain.SettingsFormat); + var v2 = Assert.NotNull((await alberto.PushProjectVersionAsync(sid, main.Id, plain)).Version); + var turned = Version(v2, new ProjectSettings { Camera = new CameraSettings { Rotate = 90, Mirror = true }, Look = new LookSettings { Filter = "sepia" } }, home); + Assert.Equal(2, turned.SettingsFormat); + var v3 = Assert.NotNull((await alberto.PushProjectVersionAsync(sid, main.Id, turned)).Version); + Assert.Equal(2, ProjectContent.FormatOf(v3.SettingsJson)); + var mirrored = ProjectSettings.FromJson(Assert.NotNull((await server.Database.Sequences.GetAsync(sid))!.SettingsJson)); + Assert.Equal((90, true, "sepia"), (mirrored.Camera.Rotate, mirrored.Camera.Mirror, mirrored.Look?.Filter), "Main's head is the sequence's settings"); + + const string future = """{"version":3,"camera":{"rotate":90}}"""; + var newer = Version(v3, new ProjectSettings(), home) with { SettingsJson = future, ContentHash = ProjectContent.Hash(future), SettingsFormat = 3 }; + using (var raw = await http.PostAsJsonAsync($"/api/v1/sequences/{sid}/projects/{main.Id}/versions", newer, ProtocolJson.Default.ProjectVersionDto)) + await raw.ExpectErrorAsync(HttpStatusCode.UnprocessableEntity, ProjectErrors.NewerFormat); + Assert.Equal(3, (await alberto.GetProjectAsync(sid, main.Id)).HeadNumber); + } + [Test] public async Task TheFramesOfAHomeAreOfOneFramesVersionWithTheAliasesOfItsConversions() { diff --git a/tests/Chronos.Server.Tests/Jobs/RenderNodeClientTests.cs b/tests/Chronos.Server.Tests/Jobs/RenderNodeClientTests.cs index 0d6b90bc..0ab7a182 100644 --- a/tests/Chronos.Server.Tests/Jobs/RenderNodeClientTests.cs +++ b/tests/Chronos.Server.Tests/Jobs/RenderNodeClientTests.cs @@ -132,6 +132,9 @@ public sealed class RenderNodeClientTests(TestContext context) Assert.SequenceEqual(["apng", "gif", "mjpeg"], registered.Encoders.Select(e => e.Codec).Order(StringComparer.Ordinal)); // Each encoder announces its own largest output, so the server matches job sizes per codec. Assert.True(registered.Encoders.All(e => (e.MaxWidth, e.MaxHeight) == (65535, 65535)), "per-encoder limits"); + // And the settings format it renders (owner review 1, phase 4), so the server gives it only jobs it renders as asked. + var stored = Assert.NotNull(Chronos.Server.Api.Dto.Capabilities(Assert.NotNull(await server.Database.RenderNodes.GetAsync(id)))); + Assert.Equal(ProjectSettings.CurrentVersion, stored.SettingsFormat); } [Test] diff --git a/tests/Chronos.Server.Tests/Jobs/RenderNodeTests.cs b/tests/Chronos.Server.Tests/Jobs/RenderNodeTests.cs index 678f37dc..0fe0dce2 100644 --- a/tests/Chronos.Server.Tests/Jobs/RenderNodeTests.cs +++ b/tests/Chronos.Server.Tests/Jobs/RenderNodeTests.cs @@ -3,10 +3,14 @@ using System.Net.Http.Json; using System.Text.Json; using Microsoft.Extensions.DependencyInjection; using Chronos.Core.Archiving; +using Chronos.Core.Project; using Chronos.Data; using Chronos.Data.Archiving; +using Chronos.Data.Processing; +using Chronos.Data.Repositories; using Chronos.Protocol; using Chronos.Protocol.Client; +using Chronos.Server.Api; using Chronos.Server.Jobs; using Chronos.Server.Storage; using Chronos.Testing; @@ -94,6 +98,96 @@ public sealed class RenderNodeTests(TestContext context) Assert.Equal(uhd.Id, (await claim.ReadAsync(ProtocolJson.Default.ClaimedJob)).Job.Id); } + /// + /// Owner review 1, phase 4: a node that does not announce its settings format (an older Chronos) reads format 1, and is + /// never given a job it would render otherwise than asked: settings of format 2 (a turn), the chosen proportions in a + /// fixed-size preset (4K at 9:16 = 2160×3840), or a sequence's settings changed to format 2 after the job was queued. A + /// node of format 2 takes them, a portrait UHD frame on an encoder announced as 4096×2304 included. + /// + [Test] + public async Task OnlyNodesThatReadTheSettingsFormatTakeTheJobsThatNeedIt() + { + await using var server = await TestServer.StartAsync(context); + var (editor, _, _) = await server.UserAsync("alberto", "editor", renderNode: true); + using var old = await editor.PostAsync("/api/v1/nodes/register", new StringContent( + """{"clientName":"old-pc","encoders":[{"codec":"h264","name":"H.264 Encoder MFT","hardware":false,"tenBit":false}],"maxWidth":4096,"maxHeight":4096}""", + System.Text.Encoding.UTF8, "application/json")); + var older = await old.ReadAsync(ProtocolJson.Default.RenderNodeDto); + var stored = Assert.NotNull(Dto.Capabilities(Assert.NotNull(await server.Database.RenderNodes.GetAsync(older.Id)))); + Assert.Equal(1, stored.SettingsFormat); + + var sequence = await JobApiTests.CreateSequenceAsync(editor); + var turned = new ProjectSettings { Camera = new CameraSettings { Rotate = 90 } }.ToJson(); + var portrait = new ProjectSettings { Camera = new CameraSettings { Aspect = new Auto().WithManual("9:16") } }.ToJson(); + var v2 = await JobApiTests.CreateAsync(editor, "render", sequence, WithSettings(H264Job(3840, 2160), turned)); + var proportions = await JobApiTests.CreateAsync(editor, "render", sequence, WithSettings(H264Job(3840, 2160), portrait)); + var routed = ProcessingJobs.ReadRender((await server.Database.Jobs.GetAsync(proportions.Id))!.ParametersJson).Parameters; + Assert.Equal((2160, 3840, 2), (routed.Width, routed.Height, routed.RequiredSettingsFormat), "the server sized the job posted at the preset's size"); + // Queued as format 1, then the sequence's own settings took a turn. + var plain = await JobApiTests.CreateAsync(editor, "render", sequence, H264Job(3840, 2160)); + Assert.Equal(1, ProcessingJobs.ReadRender((await server.Database.Jobs.GetAsync(plain.Id))!.ParametersJson).Parameters.RequiredSettingsFormat); + var row = (await server.Database.Sequences.GetAsync(sequence))!; + await server.Database.Sequences.EditAsync(sequence, new SequenceEdit { SettingsJson = turned }, row.SettingsVersion); + using (var none = await editor.PostAsync($"/api/v1/nodes/{older.Id}/claim", null)) await none.ExpectAsync(HttpStatusCode.NoContent); + + // The software H.264 encoder takes portrait UHD as a long edge × short edge, on a node that knows it. + using var registered = await editor.PostAsJsonAsync("/api/v1/nodes/register", new RegisterNodeRequest("studio-pc", + [new NodeEncoder("h264", "H.264 Encoder MFT", false, false, 4096, 2304)], 4096, 2304, SettingsFormat: 2), ProtocolJson.Default.RegisterNodeRequest); + var newer = await registered.ReadAsync(ProtocolJson.Default.RenderNodeDto); + var claimed = new List(); + for (var i = 0; i < 3; i++) + { + using var claim = await editor.PostAsync($"/api/v1/nodes/{newer.Id}/claim", null); + claimed.Add((await claim.ReadAsync(ProtocolJson.Default.ClaimedJob)).Job.Id); + } + Assert.SequenceEqual(new[] { v2.Id, proportions.Id, plain.Id }.Order(), claimed.Order()); + } + + /// + /// An encoder's limit is a long edge × short edge on a node of format 2 (its encoders take portrait UHD within 4096×2304); + /// an older node shrinks a frame to the limit as announced, so it is held to that. + /// + [Test] + public void APortraitFrameFitsALandscapeLimitOnlyOnANodeThatKnowsIt() + { + var job = new RenderJobParameters("4K", "{}", "h264", 2160, 3840, UseProxies: false, null, RequiredSettingsFormat: 2); + NodeEncoder[] software = [new("h264", "H.264 Encoder MFT", false, false, 4096, 2304)]; + var older = new RegisterNodeRequest("old-pc", software, 4096, 2304); + var newer = older with { ClientName = "studio-pc", SettingsFormat = 2 }; + Assert.False(RenderNodeService.CanEncode(older, job)); + Assert.True(RenderNodeService.CanEncode(newer, job)); + Assert.True(RenderNodeService.Renders(newer, job)); + Assert.False(RenderNodeService.Renders(older, job with { Width = 1080, Height = 1920 }), "format 2 is never sent to a format-1 node"); + Assert.True(RenderNodeService.Renders(older, job with { Width = 3840, Height = 2160, RequiredSettingsFormat = 1 })); + Assert.False(RenderNodeService.CanEncode(newer, job with { Width = 2400 }), "both edges above the short limit"); + } + + /// A job waits for a node only while one online could render it: one of an older settings format cannot. + [Test] + public async Task AJobDoesNotWaitForNodesThatCannotReadItsSettings() + { + await using var server = await TestServer.StartAsync(context, o => o.NodeTimeoutSeconds = 60); + var (editor, _, _) = await server.UserAsync("alberto", "editor", renderNode: true); + await RegisterAsync(editor, "old-pc", "h264", 4096); + var sequence = await JobApiTests.CreateSequenceAsync(editor); + var turned = new ProjectSettings { Camera = new CameraSettings { Rotate = 90 } }.ToJson(); + var created = await JobApiTests.CreateAsync(editor, "render", sequence, WithSettings(H264Job(1920, 1080), turned)); + var queued = (await server.Database.Jobs.GetAsync(created.Id))!; + var parameters = ProcessingJobs.ReadRender(queued.ParametersJson).Parameters; + Assert.Equal(2, parameters.RequiredSettingsFormat); + // Waited longer than a node may stay silent: only an online node that renders it keeps it waiting. + var job = queued with { Created = DateTimeOffset.UtcNow - TimeSpan.FromSeconds(61) }; + RenderNodeAvailability Availability() => new(server.Database, server.Options, TimeProvider.System); + + Assert.True(await Availability().AwaitsNodeAsync(queued with { Created = DateTimeOffset.UtcNow }, parameters, context.Cancellation), "a node may be reconnecting"); + Assert.False(await Availability().AwaitsNodeAsync(job, parameters, context.Cancellation), "only a node of format 1 is online"); + Assert.True(await Availability().AwaitsNodeAsync(job, parameters with { RequiredSettingsFormat = 1 }, context.Cancellation), "it renders a job of format 1"); + using (var registered = await editor.PostAsJsonAsync("/api/v1/nodes/register", new RegisterNodeRequest("studio-pc", [new NodeEncoder("h264", "NVENC", true, false)], + 4096, 4096, SettingsFormat: 2), ProtocolJson.Default.RegisterNodeRequest)) + await registered.ExpectAsync(HttpStatusCode.OK); + Assert.True(await Availability().AwaitsNodeAsync(job, parameters, context.Cancellation), "a node of format 2 is online"); + } + [Test] public async Task TheOutputUploadBecomesARenderAndFinishesTheJob() { @@ -244,6 +338,10 @@ public sealed class RenderNodeTests(TestContext context) new RenderJobParameters("H.264", """{"preset":{"isAuto":false,"manual":"4k-h264"}}""", "h264", width, height, UseProxies: false, null), ProtocolJson.Default.RenderJobParameters); + /// Render job parameters with embedded. + private static string WithSettings(string parametersJson, string settingsJson) => JsonSerializer.Serialize( + JsonSerializer.Deserialize(parametersJson, ProtocolJson.Default.RenderJobParameters)! with { SettingsJson = settingsJson }, ProtocolJson.Default.RenderJobParameters); + internal static async Task RegisterAsync(HttpClient client, string name, string codec, int maxWidth) { using var response = await client.PostAsJsonAsync("/api/v1/nodes/register",