feat(server): render MP4 jobs no render node can take as MJPEG, and say so

BRIEF §14.7: the server falls back to managed formats when no node is
available and says so in the job log. An MP4 job waits for render nodes at
least the node timeout, then as long as an online node can encode its codec
at its size; after that the server's engine takes it and writes each output
it cannot encode as MJPEG in AVI at the same size and frame rate, with a job
log line. A job the server started resumes there. Selective acceptance is
asynchronous for that; when node availability cannot be read the job keeps
waiting.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-03 09:39:17 +00:00
1 parent 1e255fb075
commit 4007293833
15 files changed
+247 -48

No files matched your search

@@ -1,39 +1,87 @@
using Titano.Core.Diagnostics;
using Titano.Core.Export;
using Titano.Core.Localization;
using Titano.Core.Model;
using Titano.Core.Video;
using Titano.Data.Jobs;
using Titano.Protocol;
namespace Titano.Data.Processing.Jobs;
/// <summary>
/// <see cref="JobKind.Render"/> on the server (BRIEF §4.4, §14.5): it takes only jobs whose every output (the preset and
/// each batch preset) its managed encoders produce, and runs them like a client does. Jobs that need a platform encoder
/// (MP4) stay queued for render nodes (§14.7). Jobs whose parameters cannot be read are taken, so they fail with their
/// reason instead of waiting for a claimer that never comes.
/// <see cref="JobKind.Render"/> on the server (BRIEF §4.4, §14.5, §14.7): it takes the jobs whose every output (the preset
/// and each batch preset) its managed encoders produce, and runs them like a client does. Jobs that need a platform
/// encoder (MP4) wait for render nodes while <see cref="ProcessingOptions.AwaitsRenderNode"/> says one may take them;
/// after that the server takes them too, writes each output it cannot encode as MJPEG in AVI at the same size and frame
/// rate, and says so in the job log. A job it started (it left a checkpoint) resumes here, and jobs whose parameters
/// cannot be read are taken, so they fail with their reason instead of waiting for a claimer that never comes.
/// </summary>
internal sealed class ManagedRenderJobHandler(RenderJobHandler render, IReadOnlyList<IVideoEncoderProvider> encoders) : ISelectiveJobHandler
internal sealed class ManagedRenderJobHandler(RenderJobHandler render, ProcessingOptions options) : ISelectiveJobHandler
{
private readonly EncoderCapability[] _capabilities = [.. encoders.SelectMany(e => e.Capabilities)];
/// <summary>The managed preset whose values stand in for an output only a render node could make.</summary>
private const string FallbackPresetId = "mjpeg-avi";
private readonly EncoderCapability[] _capabilities = [.. options.Encoders.SelectMany(e => e.Capabilities)];
public JobKind Kind => JobKind.Render;
public bool Accepts(Job job)
public async ValueTask<bool> AcceptsAsync(Job job, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(job);
RenderJobParameters parameters;
try
{
var parameters = ProcessingJobs.ReadParameters(job.ParametersJson);
// Most declined jobs are decided by the main codec, before the export is read.
return _capabilities.Any(c => string.Equals(ProcessingJobs.CodecName(c.Codec), parameters.Codec, StringComparison.OrdinalIgnoreCase))
&& RenderJobHandler.Targets(ProcessingJobs.ReadExport(parameters.PresetJson), parameters.PresetName).All(t => Produces(t.Export));
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)))
return true;
}
catch (InvalidDataException)
catch (Exception ex) when (ex is not OperationCanceledException)
{
return true;
}
if (job.Checkpoint is not null) return true;
if (options.AwaitsRenderNode is not { } awaits) return false;
try
{
return !await awaits(job, parameters, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
// Unknown whether a node could take it: the job keeps waiting rather than losing its format.
Log.Warning("processing", $"Render job {job.Id}: render node availability unknown", ex);
return false;
}
}
public Task RunAsync(JobContext context) => render.RunAsync(context);
public Task RunAsync(JobContext context) =>
render.RunAsync(context, target => Produces(target.Export) ? target : Fallback(context, target));
/// <summary>The output as MJPEG in AVI with the managed preset's quality, at the size and frame rate asked for.</summary>
private static ExportTarget Fallback(JobContext context, ExportTarget target)
{
var mjpeg = ExportResolver.FromPreset(ExportPresets.Find(FallbackPresetId)!);
context.Log(Loc.L("Export.Log.NoRenderNode", target.PresetName, ExportNaming.CodecLabel(target.Export.Codec), ExportNaming.CodecLabel(mjpeg.Codec)));
return target with
{
Export = target.Export with
{
PresetId = ExportResolver.CustomId,
NameKey = null,
Codec = mjpeg.Codec,
Profile = mjpeg.Profile,
BitDepth = mjpeg.BitDepth,
BitRate = 0,
BitsPerPixel = 0,
Quality = mjpeg.Quality,
RateControl = mjpeg.RateControl,
Encoder = null,
},
};
}
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);
@@ -25,7 +25,10 @@ internal sealed class RenderJobHandler(TitanoDatabase db, ProcessingOptions opti
{
public JobKind Kind => JobKind.Render;
public async Task RunAsync(JobContext context)
public Task RunAsync(JobContext context) => RunAsync(context, target => target);
/// <summary>Runs the job with each output still to render passed through <paramref name="adapt"/> (the server's managed stand-ins).</summary>
internal async Task RunAsync(JobContext context, Func<ExportTarget, ExportTarget> adapt)
{
var cancellation = context.Cancellation;
var (parameters, export) = ProcessingJobs.ReadRender(context.Job.ParametersJson);
@@ -56,7 +59,7 @@ internal sealed class RenderJobHandler(TitanoDatabase db, ProcessingOptions opti
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)).ToList();
var targets = all.Select((t, i) => (i, t)).Where(t => !done.Contains(t.i)).Select(t => (t.i, adapt(t.t))).ToList();
if (targets.Count == 0) return;
var frames = await SequenceFrames.LoadAsync(db, archive, sequence, parameters.UseProxies, cancellation).ConfigureAwait(false);