wip(processing): analysis and render job handlers, render node client (interrupted)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
30 files changed
+2262
-21
No files matched your search
@@ -0,0 +1,122 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using System.Text.Json.Serialization;
|
||||
using Titano.Core.Export;
|
||||
using Titano.Core.Localization;
|
||||
using Titano.Core.Model;
|
||||
using Titano.Core.Project;
|
||||
using Titano.Core.Video;
|
||||
using Titano.Data.Archiving.Jobs;
|
||||
using Titano.Protocol;
|
||||
|
||||
namespace Titano.Data.Processing;
|
||||
|
||||
/// <summary>
|
||||
/// Builds the analysis and render jobs; enqueue them with <see cref="Archiving.ArchiveJobs.EnqueueAsync"/> (local
|
||||
/// engine) or post the same kind, targets and parameters to <c>/api/v1/jobs</c> (server).
|
||||
/// </summary>
|
||||
public static class ProcessingJobs
|
||||
{
|
||||
/// <summary><see cref="JobKind.LightAnalyse"/>: targets = sequences; no parameters.</summary>
|
||||
public static ArchiveJobRequest LightAnalysis(IReadOnlyList<Guid> sequenceIds, string? label = null) =>
|
||||
new(JobKind.LightAnalyse, sequenceIds, label, null, JobPriority.Background);
|
||||
|
||||
/// <summary><see cref="JobKind.DeepAnalyse"/>: targets = sequences; no parameters.</summary>
|
||||
public static ArchiveJobRequest DeepAnalysis(IReadOnlyList<Guid> sequenceIds, string? label = null) =>
|
||||
new(JobKind.DeepAnalyse, sequenceIds, label, null, JobPriority.User);
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="JobKind.Render"/> of one sequence. <paramref name="export"/> (preset, overrides, name template,
|
||||
/// folder, batch) replaces the project's export section; <paramref name="project"/> embeds the settings to render,
|
||||
/// null renders the sequence's current project. <paramref name="sourceWidth"/>×<paramref name="sourceHeight"/>
|
||||
/// stands in for a preset without a fixed size, so render nodes can match the job by resolution.
|
||||
/// </summary>
|
||||
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 resolved = ExportResolver.Resolve(export);
|
||||
var name = PresetName(resolved);
|
||||
var parameters = new RenderJobParameters(name, JsonSerializer.Serialize(export, ProcessingJson.Default.ExportSettings), CodecName(resolved.Codec),
|
||||
resolved.Width > 0 ? resolved.Width : sourceWidth, resolved.Height > 0 ? resolved.Height : sourceHeight, useProxies, project?.ToJson());
|
||||
return new ArchiveJobRequest(JobKind.Render, [sequence.Id], $"{sequence.DisplayTitle} → {name}",
|
||||
JsonSerializer.Serialize(parameters, ProtocolJson.Default.RenderJobParameters), JobPriority.User);
|
||||
}
|
||||
|
||||
/// <summary>Codec name in job parameters and render-node capabilities ("h264", "hevc", "mjpeg", "pngsequence"…).</summary>
|
||||
public static string CodecName(VideoCodec codec) => codec.ToString().ToLowerInvariant();
|
||||
|
||||
/// <summary>Display name of an export's preset in the current language ("Custom" when the values match none).</summary>
|
||||
public static string PresetName(ResolvedExport export)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(export);
|
||||
return Loc.L(export.NameKey ?? "Export.Preset.Custom");
|
||||
}
|
||||
|
||||
/// <summary>The parameters of a render job and the export they select.</summary>
|
||||
/// <exception cref="InvalidDataException">The job has no valid render parameters.</exception>
|
||||
internal static (RenderJobParameters Parameters, ExportSettings Export) ReadRender(string? parametersJson)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (parametersJson is { Length: > 0 } && JsonSerializer.Deserialize(parametersJson, ProtocolJson.Default.RenderJobParameters) is { } parameters)
|
||||
return (parameters, ReadExport(parameters.PresetJson));
|
||||
}
|
||||
catch (JsonException ex)
|
||||
{
|
||||
throw new InvalidDataException(Loc.L("Errors.Render.Parameters"), ex);
|
||||
}
|
||||
throw new InvalidDataException(Loc.L("Errors.Render.Parameters"));
|
||||
}
|
||||
|
||||
/// <summary>The export section in a render job's preset JSON; values it leaves out take their defaults.</summary>
|
||||
/// <exception cref="InvalidDataException">The JSON is not an export section.</exception>
|
||||
internal static ExportSettings ReadExport(string? presetJson)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Read as the export section of a project, so missing values keep their defaults (ProjectSettings.FromJson).
|
||||
var project = new JsonObject { ["export"] = JsonNode.Parse(presetJson ?? string.Empty) };
|
||||
return ProjectSettings.FromJson(project.ToJsonString()).Export;
|
||||
}
|
||||
catch (JsonException ex)
|
||||
{
|
||||
throw new InvalidDataException(Loc.L("Errors.Render.Parameters"), ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resume point of a render job: the batch targets already recorded, and the output a crashed attempt was writing
|
||||
/// (its leftovers are removed before that target renders again).
|
||||
/// </summary>
|
||||
internal sealed record RenderCheckpoint(IReadOnlyList<int> Done, int? Pending, string? PendingOutput)
|
||||
{
|
||||
public static RenderCheckpoint Empty { get; } = new([], null, null);
|
||||
|
||||
public static RenderCheckpoint Parse(string? json)
|
||||
{
|
||||
if (string.IsNullOrEmpty(json)) return Empty;
|
||||
try
|
||||
{
|
||||
return JsonSerializer.Deserialize(json, ProcessingJson.Default.RenderCheckpoint) ?? Empty;
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
return Empty;
|
||||
}
|
||||
}
|
||||
|
||||
public string ToJson() => JsonSerializer.Serialize(this, ProcessingJson.Default.RenderCheckpoint);
|
||||
}
|
||||
|
||||
/// <summary>JSON written into render jobs: presets (the export section of <c>project.json</c>) and checkpoints.</summary>
|
||||
[JsonSourceGenerationOptions(
|
||||
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
|
||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
|
||||
UseStringEnumConverter = true,
|
||||
PropertyNameCaseInsensitive = true)]
|
||||
[JsonSerializable(typeof(ExportSettings))]
|
||||
[JsonSerializable(typeof(RenderCheckpoint))]
|
||||
internal sealed partial class ProcessingJson : JsonSerializerContext;
|
||||
Reference in new issue
Block a user