using Titano.Core.Analysis.Deep;
using Titano.Core.Export;
using Titano.Core.Imaging;
using Titano.Core.Model;
using Titano.Core.Render;
using Titano.Core.Video;
using Titano.Core.Video.Managed;
using Titano.Data.Archiving;
using Titano.Protocol;
namespace Titano.Data.Processing;
///
/// What differs between the hosts that analyse and render (BRIEF §4.2): decoders (inside ),
/// encoders and where the work runs. Client: WIC then managed decoders, Media Foundation then managed encoders;
/// server: managed only.
///
public sealed record ProcessingOptions
{
/// after a render was recorded.
public const string RenderedChange = "renders";
/// after the advisor changed the project.
public const string SettingsChange = "settings";
/// Decoders, lease owner and lease timing for the sidecar and project writes.
public ArchiveServiceOptions Archive { get; init; } = new();
/// Encoder providers in priority order.
public IReadOnlyList Encoders { get; init; } = [new ManagedEncoderProvider()];
///
/// Whether a render of the given preset (its base preset id) may use hardware encoders (Settings › Rendering); when
/// not, it gets only the software ones. Asked at every render, so a changed preference applies to the next one.
///
public Func AllowsHardware { get; init; } = _ => true;
///
/// Told after a job changed a sequence: (a render was recorded) or
/// (the advisor changed Auto values). A server tells its connected clients (BRIEF §14.3).
/// A failure is logged and never fails the job.
///
public Func? SequenceChanged { get; init; }
/// The server delegates platform codecs to render nodes ().
public EncoderHost Host { get; init; } = EncoderHost.Client;
///
/// Server: 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 renders it itself, every output only a node could make as MJPEG in AVI at the same size and
/// frame rate, and says so in the job log. Null: such jobs always wait for a node.
///
public Func>? AwaitsRenderNode { get; init; }
/// Recorded with every render this host produces (client or server name).
public string ProducedBy { get; init; } = Environment.MachineName;
public DeepAnalysisOptions DeepAnalysis { get; init; } = DeepAnalysisOptions.Default;
/// Read-ahead and memory budget of the render decode window.
public FrameWindowOptions FrameWindow { get; init; } = new();
public RenderOptions Render { get; init; } = RenderOptions.Default;
/// Tells ; a failure is logged, never passed on to the job.
internal async Task NotifyAsync(Guid sequenceId, string change, CancellationToken cancellationToken)
{
if (SequenceChanged is null) return;
try
{
await SequenceChanged(sequenceId, change, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
Core.Diagnostics.Log.Warning("processing", $"The {change} change of sequence {sequenceId} could not be announced", ex);
}
}
/// without their hardware encoders, for renders keeps off them.
internal IReadOnlyList SoftwareEncoders =>
[.. Encoders.Select(e => e.Capabilities.Any(c => c.Hardware) ? new SoftwareEncoderProvider(e) : e)];
///
/// Encoders the advisor may count on: this machine's on a client; none listed on the server (render nodes can
/// produce any codec, so the advisor rules none out).
///
internal IReadOnlyList AdvisorCodecs =>
Host == EncoderHost.Server ? [] : [.. Encoders.SelectMany(e => e.Capabilities)];
}