using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using Chronos.Core.Export;
using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Core.Project;
using Chronos.Core.Video;
using Chronos.Data.Archiving.Jobs;
using Chronos.Protocol;
namespace Chronos.Data.Processing;
///
/// Builds the analysis and render jobs; enqueue them with (local
/// engine) or post the same kind, targets and parameters to /api/v1/jobs (server).
///
public static class ProcessingJobs
{
/// : targets = sequences; no parameters.
public static ArchiveJobRequest LightAnalysis(IReadOnlyList sequenceIds, string? label = null) =>
new(JobKind.LightAnalyse, sequenceIds, label, null, JobPriority.Background);
/// : targets = sequences; no parameters.
public static ArchiveJobRequest DeepAnalysis(IReadOnlyList sequenceIds, string? label = null) =>
new(JobKind.DeepAnalyse, sequenceIds, label, null, JobPriority.User);
///
/// 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.
///
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);
return new ArchiveJobRequest(JobKind.Render, [sequence.Id], RenderLabel(sequence.DisplayTitle, parameters.PresetName),
JsonSerializer.Serialize(parameters, ProtocolJson.Default.RenderJobParameters), JobPriority.User);
}
///
/// of a project version (owner review 2, R8) on the sequence's home: the version's portable
/// settings, placed on the sequence's frames by the render, with as their export section; the
/// render records the project and the version. One job per preset, as .
///
public static IReadOnlyList RenderVersion(Sequence sequence, ProjectVersion version, ExportSettings export, int sourceWidth, int sourceHeight,
bool useProxies = false)
{
ArgumentNullException.ThrowIfNull(sequence);
ArgumentNullException.ThrowIfNull(version);
ArgumentNullException.ThrowIfNull(export);
return
[
VersionJob(sequence, version, export with { Batch = [] }, sourceWidth, sourceHeight, useProxies),
.. BatchPresets(export).Select(id => VersionJob(sequence, version, BatchExport(export, id), sourceWidth, sourceHeight, useProxies)),
];
}
private static ArchiveJobRequest VersionJob(Sequence sequence, ProjectVersion version, ExportSettings export, int sourceWidth, int sourceHeight, bool useProxies)
{
var parameters = Parameters(export, sourceWidth, sourceHeight, version.SettingsJson, useProxies) with
{
ProjectId = version.ProjectId,
ProjectVersionId = version.Id,
};
return new ArchiveJobRequest(JobKind.Render, [sequence.Id], RenderLabel(sequence.DisplayTitle, parameters.PresetName),
JsonSerializer.Serialize(parameters, ProtocolJson.Default.RenderJobParameters), JobPriority.User);
}
///
/// The label of a render job, "Harbour sunset → 4K HEVC". It is stored as written, in the language of the moment like
/// the preset name: its arrow points from the sequence to the preset in that language's reading direction.
///
public static string RenderLabel(string title, string presetName) =>
Loc.L(Loc.IsRightToLeft ? "Jobs.Target.RenderRtl" : "Jobs.Target.Render", title, presetName);
///
/// 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.
///
/// The preset JSON is not an export section.
public static IReadOnlyList Split(RenderJobParameters parameters, int sourceWidth, int sourceHeight)
{
ArgumentNullException.ThrowIfNull(parameters);
var export = ReadExport(parameters.PresetJson);
var batch = BatchPresets(export).ToList();
if (batch.Count == 0) return [parameters];
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
{
ProjectId = parameters.ProjectId,
ProjectVersionId = parameters.ProjectVersionId,
}),
];
}
///
/// 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.
///
public static IReadOnlyList RenderEach(Sequence sequence, ExportSettings export, int sourceWidth, int sourceHeight,
ProjectSettings? project = null, bool useProxies = false)
{
ArgumentNullException.ThrowIfNull(export);
return
[
Render(sequence, export with { Batch = [] }, sourceWidth, sourceHeight, project, useProxies),
.. BatchPresets(export).Select(id => Render(sequence, BatchExport(export, id), sourceWidth, sourceHeight, project, useProxies)),
];
}
/// The batch presets of an export besides its own, each once (blank entries of a hand-written batch are skipped).
internal static IEnumerable BatchPresets(ExportSettings export)
{
var main = ExportResolver.Resolve(export).BasePresetId;
return export.Batch.Where(id => !string.IsNullOrWhiteSpace(id)).Distinct(StringComparer.OrdinalIgnoreCase)
.Where(id => !id.Equals(main, StringComparison.OrdinalIgnoreCase));
}
/// A batch preset rendered with its own values, named and placed like the export it belongs to.
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)
{
var resolved = ExportResolver.Resolve(export);
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);
}
/// Codec name in job parameters and render-node capabilities ("h264", "hevc", "mjpeg", "pngsequence"…).
public static string CodecName(VideoCodec codec) => codec.ToString().ToLowerInvariant();
/// Display name of an export's preset in the current language ("Custom" when the values match none).
public static string PresetName(ResolvedExport export)
{
ArgumentNullException.ThrowIfNull(export);
return Loc.L(export.NameKey ?? "Export.Preset.Custom");
}
/// The parameters of a render job and the export they select.
/// The job has no valid render parameters.
public static (RenderJobParameters Parameters, ExportSettings Export) ReadRender(string? parametersJson)
{
var parameters = ReadParameters(parametersJson);
return (parameters, ReadExport(parameters.PresetJson));
}
/// The protocol record of a render job, without reading its export.
/// The job has no valid render parameters.
internal static RenderJobParameters ReadParameters(string? parametersJson)
{
try
{
if (parametersJson is { Length: > 0 } && JsonSerializer.Deserialize(parametersJson, ProtocolJson.Default.RenderJobParameters)
is { PresetName.Length: > 0, PresetJson.Length: > 0, Codec.Length: > 0 } parameters)
return parameters;
}
catch (JsonException ex)
{
throw new InvalidDataException(Loc.L("Errors.Render.Parameters"), ex);
}
throw new InvalidDataException(Loc.L("Errors.Render.Parameters"));
}
/// The export section in a render job's preset JSON; values it leaves out take their defaults.
/// The JSON is not an export section.
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);
}
}
}
///
/// 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) with the sequence's archive-relative folder when it
/// chose that output (null in checkpoints written before it was kept), so the leftovers are found after a Reorganize.
///
internal sealed record RenderCheckpoint(IReadOnlyList Done, int? Pending, string? PendingOutput, string? Folder = null)
{
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);
}
/// JSON written into render jobs: presets (the export section of project.json) and checkpoints.
[JsonSourceGenerationOptions(
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
UseStringEnumConverter = true,
PropertyNameCaseInsensitive = true)]
[JsonSerializable(typeof(ExportSettings))]
[JsonSerializable(typeof(RenderCheckpoint))]
internal sealed partial class ProcessingJson : JsonSerializerContext;