using System.IO; using System.Text.Json; using Chronos.Core.Advice; using Chronos.Core.Analysis; using Chronos.Core.Analysis.Deep; using Chronos.Core.Archiving; using Chronos.Core.Diagnostics; 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.Data; using Chronos.Data.Archiving; using Chronos.Data.Archiving.Jobs; using Chronos.Data.Jobs; using Chronos.Data.Processing; using Chronos.Data.Projects; using Chronos.Data.Workspaces; using Chronos.Protocol; using Chronos.Protocol.Client; using SequenceManifest = Chronos.Protocol.SequenceManifest; namespace Chronos.App.Editor; /// A source whose renders are made otherwise than from the project as the editor holds it (a workspace project: at its home). public interface IProjectRenders { /// The jobs that render of (the editor's settings, positions on the source's frames). IReadOnlyList RenderRequests(Sequence sequence, ProjectSettings project, ExportSettings export, int sourceWidth, int sourceHeight); /// The renders run on this PC (a workspace project holding its originals, phase 5c), whatever the home. bool RendersLocally { get; } } /// /// A workspace project's home when it is a server (owner review 2, R8, phase 5b): how the source connects for its analyses /// and renders (: a connection the source owns, null while this PC is not signed in to that /// server, when the project still opens from the workspace and its jobs are refused saying so), and the sign-in it would /// connect with (): when that changes (signed in, out, or anew), the source connects again. /// public sealed class WorkspaceServerHome(Func connect, Func session) { /// One connection for good (null: never signed in), as a test or a capture holds it. public WorkspaceServerHome(IRemoteSequenceApi? api) : this(() => api, () => api) { } /// The sign-in would use now; compared by reference. public object? Session => session(); /// A new connection of the current sign-in; null while signed out. public IRemoteSequenceApi? Connect() => connect(); } /// /// A workspace project open in the editor (owner review 2, R8: editing happens in the workspace, never inside the /// library): the frames and pixels of the media the workspace holds (media/media.json, the proxies), the project /// from its working state (project.chronos, positions placed on those frames), saved there atomically under the /// project's lease and merged against the basis like (a pull or a copy job writes it /// too): frame positions photo by photo as the sync merges them (), and settings the editor changed to /// other values than those stored meanwhile asked about (), never decided for the /// editor. A pull that brings a newer Chronos's settings turns it read-only (). Jobs run on /// this PC's engine against the home's sequence (same id): an analysis reads the home's originals, a /// render renders the home's originals with the project's portable settings and records the project and the version /// (). The deep report is the home's while its frames are the media's, else the copy the /// workspace took. A copy job that changed the media (Update media) makes saves made against the frames read before refused /// () until the editor has read them again and moved its positions by frame. /// A project whose home is a server (, phase 5b) is a remote one for the editor: its /// analyses and renders are the server's jobs (), a render of the version the server holds when the /// project is in step with it (else of the project's portable settings, recording the project), its deep report and renders /// are read from the server when it can be reached (else the copy the workspace took), and "Update media" is offered when /// the server's frames moved, or it has the proxy of a frame a thumbnail stands in for. /// public sealed class WorkspaceSequenceSource : ISequenceSource, IProjectRenders { private const int JobsListed = 200; private readonly WorkspaceSync _sync; private readonly ChronosDatabase _db; private readonly ImageDecoder _decoder; private readonly JobEngine? _engine; private readonly WorkspaceServerHome? _server; private readonly Lock _gate = new(); private readonly List _retired = []; private IRenderFrameSource? _frames; private IFrameSource? _pixels; private ServerJobs? _serverJobs; private IRemoteSequenceApi? _api; private object? _apiSession; private bool _connected; private int _frameCount; private int _framesVersion = -1; private Guid[] _frameIds = []; private HashSet _standIns = []; private FramePhotos _photos = new([]); private BundleWorkspace? _workspace; private bool _readOnly; private bool _holdsOriginals; private int _unsent; private int _disposed; /// The server the project's home is (phase 5b); null for a project of this PC's archive. public WorkspaceSequenceSource(WorkspaceSync sync, Guid projectId, ChronosDatabase db, ImageDecoder decoder, JobEngine? engine, WorkspaceServerHome? server = null) { ArgumentNullException.ThrowIfNull(sync); ArgumentNullException.ThrowIfNull(db); ArgumentNullException.ThrowIfNull(decoder); _sync = sync; _db = db; _decoder = decoder; _engine = engine; _server = server; ProjectId = projectId; // This PC's engine runs the copies of the media (Update media), whatever the home. if (engine is not null) engine.JobChanged += OnJobChanged; } public Guid ProjectId { get; } /// The project's folder in the workspace. public WorkspaceProject Project => _sync.Store.Project(ProjectId); /// The workspace section of the project file as read last (name, Main, home, base, media); null before the first read. public BundleWorkspace? Workspace => Volatile.Read(ref _workspace); /// The frames version of the home the media read last were copied at; -1 before the first read. public int MediaFramesVersion => Volatile.Read(ref _framesVersion); /// The photos of the media read last, in frame order (what positions name). public FramePhotos Photos => Volatile.Read(ref _photos); /// The project's home is a server (phase 5b): its analyses and renders run there. public bool IsServerHome => _server is not null; /// A server home's analyses and renders run on the server, as for a server sequence opened with proxies. public bool IsRemote => _server is not null; /// Settings of a newer Chronos: shown, never saved. public bool IsReadOnly => Volatile.Read(ref _readOnly); /// /// The media read last hold the originals (phase 5c): the project renders on this PC's engine from them, into its /// exports/, and the renders are sent home after (), also for a server home. /// public bool RendersLocally => Volatile.Read(ref _holdsOriginals); /// The local renders of the project's ledger not at the home yet, as read last. public int UnsentRenders => Volatile.Read(ref _unsent); public bool IsOutdated => false; /// The library's values and the editor's are asked about where both changed a setting, as a sync asks. public bool AsksConflicts => true; /// The working state read again holds a newer Chronos's settings (a pull took them), or no longer does. public event Action? ReadOnlyChanged; public event Action? JobChanged; /// /// The workspace holds no media for the project (copy them again), the project is gone, or a sync or a copy kept it for /// longer than the sync waits (). /// public async Task LoadAsync(CancellationToken cancellationToken) { WorkspaceSession session; try { session = await _sync.OpenAsync(ProjectId, cancellationToken); } catch (InvalidOperationException ex) { throw new EditorException(Loc.L("Errors.Workspace.NotCheckedOut"), ex); } catch (InvalidDataException ex) { throw new EditorException(Loc.L("Editor.Error.Project"), ex); } catch (LeaseUnavailableException ex) { // A long sync or copy holds the project: nothing was read, opening it again later reads it whole. Log.Info("editor", $"Workspace project {ProjectId} is busy: {ex.Message}"); throw new EditorException(Loc.L("Errors.Workspace.Busy"), ex); } if (session is not { Sequence: { } media, Media: { } index }) throw new EditorException(Loc.L("Errors.Workspace.MediaMissing")); var home = session.Bundle.Workspace!.Home; if (_server is not null) return await LoadFromServerAsync(session, media, index, home, cancellationToken); var row = await _db.Sequences.GetAsync(home.SequenceId, cancellationToken); var archive = row is null ? null : await _db.Archives.GetAsync(row.ArchiveId, cancellationToken); IReadOnlyList renders = row is null ? [] : await _db.Renders.ListBySequenceAsync(home.SequenceId, cancellationToken); return await Task.Run(() => { renders = [.. renders, .. UnsentOf(session.Project, home.SequenceId)]; var frames = media.Frames; Swap(media, index, session.Bundle.Workspace!, session.IsReadOnly); var times = frames.Where(f => f.CapturedAt is not null).Select(f => f.CapturedAt!.Value).Order().ToList(); // The home's row (camera, interval, tags) with the frames this workspace holds. var sequence = (row ?? media.Sequence) with { FrameCount = frames.Count, FramesVersion = index.FramesVersion, FirstCapture = times.Count > 0 ? times[0] : null, LastCapture = times.Count > 0 ? times[^1] : null, SettingsJson = session.Working.ToJson(), }; var folder = archive is null ? null : new ArchivePaths(archive.RootPath).Sequence(row!.Folder); var (deep, otherFrames) = ReadReport(folder, row?.FramesVersion == index.FramesVersion, session.Project.AnalysisPath, frames.Count); var context = SequenceContext.FromFrames(frames); return new SequenceContent(sequence, frames, session.Working, LightAnalyzer.Analyze(frames), deep, context.SourceWidth, context.SourceHeight, folder?.Exports, [.. renders.OrderByDescending(r => r.Created)], []) { DeepOfOtherFrames = otherFrames, }; }, cancellationToken); } /// /// A project whose home is a server: the media's frames and the working state as for any workspace project, with what /// the server says of its sequence when it can be reached (title, camera, tags, renders, and its deep report while its /// frames are the media's); the copy the workspace took of the report otherwise. The server's jobs are followed from /// the first read on. /// private async Task LoadFromServerAsync(WorkspaceSession session, WorkspaceSequence media, WorkspaceMedia index, BundleHome home, CancellationToken cancellationToken) { var jobs = ServerJobsOf(home.SequenceId, session.Bundle.Sequence.Title ?? media.Sequence.DisplayTitle); SequenceDto? dto = null; SequenceManifest? manifest = null; string? report = null; if (jobs is not null) { try { dto = await jobs.Api.GetSequenceAsync(home.SequenceId, cancellationToken); manifest = await jobs.Api.GetManifestAsync(home.SequenceId, cancellationToken); // The server's report only while its frames are the ones this workspace holds. if (manifest.FramesVersion == index.FramesVersion) report = await jobs.Api.GetAnalysisReportAsync(home.SequenceId, cancellationToken); } catch (Exception ex) when (ServerProjectHome.IsUnavailable(ex, cancellationToken) || ex is ChronosApiException) { // Out of reach (or the sequence gone there): the workspace's own copy opens all the same. Log.Info("editor", $"The server's view of workspace project {ProjectId} is not read now: {ex.Message}"); } } return await Task.Run(() => { var frames = media.Frames; Swap(media, index, session.Bundle.Workspace!, session.IsReadOnly); var times = frames.Where(f => f.CapturedAt is not null).Select(f => f.CapturedAt!.Value).Order().ToList(); var sequence = media.Sequence with { FrameCount = frames.Count, FramesVersion = index.FramesVersion, FirstCapture = times.Count > 0 ? times[0] : null, LastCapture = times.Count > 0 ? times[^1] : null, SettingsJson = session.Working.ToJson(), }; if (dto is not null) { sequence = sequence with { Title = dto.Title, CameraMake = dto.CameraMake, CameraModel = dto.CameraModel, Lens = dto.Lens, MedianInterval = dto.MedianInterval, Tags = dto.Tags, Rating = dto.Rating, Notes = dto.Notes, Status = (SequenceStatus)dto.Status, RenderedCount = dto.RenderedCount, WarningCount = dto.WarningCount, PreviewOnlyCount = dto.PreviewOnlyCount, OwnerUserId = dto.OwnerUserId, UpdatedAt = dto.UpdatedAt, }; } var renders = manifest?.Renders.OrderByDescending(r => r.Created).Select(r => new Render { Id = r.Id, SequenceId = home.SequenceId, PresetName = r.PresetName, OutputPath = string.Empty, Codec = r.Codec, Width = r.Width, Height = r.Height, Fps = r.Fps, DurationSeconds = r.DurationSeconds, Bytes = r.Bytes, Created = r.Created, ProducedBy = r.ProducedBy, }).ToList() ?? []; renders = [.. renders.Concat(UnsentOf(session.Project, home.SequenceId)).OrderByDescending(r => r.Created)]; var (deep, otherFrames) = ServerReport(report, session.Project.AnalysisPath, frames.Count); var context = SequenceContext.FromFrames(frames); return new SequenceContent(sequence, frames, session.Working, LightAnalyzer.Analyze(frames), deep, context.SourceWidth, context.SourceHeight, null, renders, []) { DeepOfOtherFrames = otherFrames, }; }, cancellationToken); } /// The server's jobs of the sequence, followed from now on; null while this PC is not signed in to the server. private ServerJobs? ServerJobsOf(Guid sequenceId, string title) { if (Api() is not { } api) return null; lock (_gate) { if (Volatile.Read(ref _disposed) == 1 || !ReferenceEquals(api, _api)) return null; return _serverJobs ??= new ServerJobs(api, sequenceId, title, job => JobChanged?.Invoke(job)); } } /// /// The connection of the sign-in in use now: made at first use, and again once the sign-in changed (the jobs followed /// on the old one stop with it, so a project opened signed out renders once signed in); null while signed out. /// private IRemoteSequenceApi? Api() { if (_server is null) return null; var session = _server.Session; IRemoteSequenceApi? api, stale; ServerJobs? staleJobs; lock (_gate) { if (Volatile.Read(ref _disposed) == 1) return null; if (_connected && ReferenceEquals(session, _apiSession)) return _api; (stale, staleJobs) = (_api, _serverJobs); _serverJobs = null; _api = api = _server.Connect(); (_apiSession, _connected) = (session, true); } // The jobs own their connection. if (staleJobs is not null) _ = staleJobs.DisposeAsync().AsTask(); else stale?.Dispose(); return api; } /// The server's deep report when it describes these frames, else the copy the workspace took; whether one of another number of frames was read instead. private static (DeepAnalysisReport? Report, bool OtherFrames) ServerReport(string? json, string copied, int frameCount) { var otherFrames = false; if (json is not null) { try { var report = DeepAnalysisReport.FromJson(json); if (report.FrameCount == frameCount) return (report, false); otherFrames = true; } catch (Exception ex) when (ex is JsonException or InvalidDataException) { Log.Warning("editor", "The server's deep report cannot be read; the workspace's copy is shown", ex); } } var (deep, other) = ReadReport(null, false, copied, frameCount); return (deep, deep is null && (other || otherFrames)); } /// Only a copy job changes the workspace's frames (Update media): its media index is compared with the one read last. public Task FramesCurrentAsync(CancellationToken cancellationToken) => Task.Run(() => { var (framesVersion, ids) = (Volatile.Read(ref _framesVersion), Volatile.Read(ref _frameIds)); try { return Project.ReadMedia() is { } media && media.FramesVersion == framesVersion && media.Frames.Select(f => f.Id).SequenceEqual(ids); } catch (InvalidDataException ex) { Log.Warning("editor", $"The media index of workspace project {ProjectId} cannot be read", ex); return false; } }, cancellationToken); /// /// Whether the home's frames moved past those the workspace holds (frames added or removed there), or a server now has /// the proxy of a frame a thumbnail stands in for here: "Update media" copies them again. A server out of reach says no. /// public async Task HomeFramesChangedAsync(CancellationToken cancellationToken) { if (Workspace is not { } workspace || MediaFramesVersion < 0) return false; if (_server is null) return await _db.Sequences.GetAsync(workspace.Home.SequenceId, cancellationToken) is { } row && row.FramesVersion != MediaFramesVersion; if (Api() is not { } api) return false; try { if ((await api.GetSequenceAsync(workspace.Home.SequenceId, cancellationToken)).FramesVersion != MediaFramesVersion) return true; var standIns = Volatile.Read(ref _standIns); return standIns.Count > 0 && (await api.GetManifestAsync(workspace.Home.SequenceId, cancellationToken)).Frames.Any(f => f.HasProxy && standIns.Contains(f.Id)); } catch (Exception ex) when (ServerProjectHome.IsUnavailable(ex, cancellationToken) || ex is ChronosApiException) { Log.Info("editor", $"The server's frames of workspace project {ProjectId} could not be compared: {ex.Message}"); return false; } } public async ValueTask DecodeAsync(int index, int maxLongEdge, DecodePurpose purpose, CancellationToken cancellationToken) { IFrameSource? pixels; int count; lock (_gate) (pixels, count) = (_pixels, _frameCount); if (pixels is null || (uint)index >= (uint)count) return null; // The loader decodes synchronously: an async delegate keeps a superseded decode's cancellation in user code (R4). return await Task.Run(async () => await pixels.LoadAsync(index, maxLongEdge, cancellationToken).ConfigureAwait(false), cancellationToken); } /// The project is read-only, or its media are gone. /// The media changed since they were read (a copy job): read them again first. /// /// The working state stored meanwhile (a sync took the library's versions) holds other values of settings this edit /// changed too: nothing is stored until the user keeps theirs or takes the library's. /// public Task SaveProjectAsync(ProjectSettings project, ProjectSettings basis, CancellationToken cancellationToken) { ArgumentNullException.ThrowIfNull(project); ArgumentNullException.ThrowIfNull(basis); var framesVersion = Volatile.Read(ref _framesVersion); if (framesVersion < 0) throw new InvalidOperationException("LoadAsync first."); var folder = Project; return Task.Run(async () => { using (await folder.LockAsync(_sync.LockWait, cancellationToken)) { var bundle = folder.ReadBundle(); if (bundle.IsReadOnly || project.IsNewerFormat) throw new EditorException(Loc.L("Errors.Workspace.ReadOnly")); var media = folder.ReadMedia() ?? throw new EditorException(Loc.L("Errors.Workspace.MediaMissing")); // A copy job moved the working state onto other frames: the editor reads them and moves its positions first. if (media.FramesVersion != framesVersion) throw new FramesChangedException(Loc.L("Errors.Workspace.MediaChanged")); var stored = ProjectSettings.FromJson(ProjectContent.Place(bundle.Project.ToJson(), media.Photos), allowNewer: true); var merged = project; if (!ProjectMerge.SameValues(stored, basis)) { var merge = Merge(basis, project, stored, media.Photos); if (merge.HasConflicts) throw new ProjectConflictException(Loc.L("Editor.Error.SaveConflict"), stored, merge.Conflicts); merged = merge.Merged; } if (ProjectMerge.SameValues(merged, stored)) return merged; folder.WriteBundle(bundle with { Project = merged, Saved = DateTimeOffset.UtcNow }); return merged; } }, cancellationToken); } /// /// The editor's merges as the sync merges (owner review 2, R8): member by member, and frame positions photo by photo and /// flag by flag in the portable form, so an exclusion or a keyframe a sync wrote meanwhile stays beside the editor's own /// (positional arrays would take one side whole). All three name the frames of the media read last. /// public ProjectMergeResult Merge(ProjectSettings basis, ProjectSettings ours, ProjectSettings theirs) => Merge(basis, ours, theirs, Photos); private static ProjectMergeResult Merge(ProjectSettings basis, ProjectSettings ours, ProjectSettings theirs, FramePhotos photos) { ArgumentNullException.ThrowIfNull(basis); ArgumentNullException.ThrowIfNull(ours); ArgumentNullException.ThrowIfNull(theirs); var merge = ProjectMerge.MergeWithConflicts(basis, ours, theirs); // Positions only one side moved, or none, merge alike either way; the portable form is needed where both did. if (photos.Count == 0 || !merge.Conflicts.Any(c => NamesPosition(c.Path)) || basis.IsNewerFormat || ours.IsNewerFormat || theirs.IsNewerFormat) return merge; var portable = ProjectMerge.MergeWithConflicts(Portable(basis, photos), Portable(ours, photos), Portable(theirs, photos)); var placed = ProjectSettings.FromJson(ProjectContent.Place(ProjectContent.Canonical(portable.Merged.ToJson()), photos), allowNewer: true); return portable with { Merged = placed }; static ProjectSettings Portable(ProjectSettings settings, FramePhotos photos) => ProjectSettings.FromJson(ProjectContent.ToPortable(settings, photos), allowNewer: true); } /// /// Keeps an edit the working state can no longer take (a sync brought a newer Chronos's settings) as a version file in /// versions/.replaced, where a take of the library's values keeps the working state it replaces: outside the chain /// of versions, so it is never sent, and never lost. Written under the project's lease, its positions parked by photo. /// /// Another writer kept the project for longer than the sync waits. public Task SetAsideAsync(ProjectSettings unsaved, CancellationToken cancellationToken) { ArgumentNullException.ThrowIfNull(unsaved); var folder = Project; var (photos, framesVersion, basis) = (Photos, MediaFramesVersion, Workspace?.Base); return Task.Run(async () => { using (await folder.LockAsync(_sync.LockWait, cancellationToken)) { var portable = ProjectContent.ToPortable(unsaved, photos); var version = new ProjectVersion { Id = Guid.CreateVersion7(), ProjectId = ProjectId, ParentId = basis?.VersionId, Number = (basis?.Number ?? 0) + 1, Kind = ProjectVersionKind.Commit, ClientName = Environment.MachineName, SettingsJson = portable, ContentHash = ProjectContent.Hash(portable), SettingsFormat = ProjectContent.FormatOf(portable), FramesVersion = Math.Max(0, framesVersion), CreatedAt = DateTimeOffset.UtcNow, }; var path = Path.Combine(folder.VersionsDirectory, WorkspaceProject.ReplacedFolder, Path.GetFileName(folder.VersionPath(version))); ProjectSidecars.WriteVersion(path, version); return path; } }, cancellationToken); } /// A merge path of a frame position (an exclusion, the hero, a camera keyframe, a frame waiting for its photo). private static bool NamesPosition(string path) => path is "excludedFrames" or "heroFrame" || path.StartsWith("camera.keyframes", StringComparison.Ordinal) || path.StartsWith("pendingFrames", StringComparison.Ordinal); /// A project whose home is a server queues its analyses and renders there; while this PC is not signed in to it, they are refused saying so. public async Task SubmitAsync(ArchiveJobRequest request, CancellationToken cancellationToken) { ArgumentNullException.ThrowIfNull(request); // A render from the workspace's originals and its send run on this PC's engine, whatever the home (phase 5c). if (_server is not null && !RunsHere(request.Kind)) return await ServerJobsOrThrow().SubmitAsync(request, cancellationToken); if (_engine is null) throw new InvalidOperationException(Loc.L("Editor.Error.NoEngine")); var job = await _engine.EnqueueAsync(request, cancellationToken: cancellationToken); return job.Id; } /// A server home's jobs are the server's; none are listed while it cannot be reached (the project opens all the same). public async Task> ListJobsAsync(CancellationToken cancellationToken) { var local = (await _db.Jobs.ListAsync(null, JobsListed, null, cancellationToken)).Items; if (_server is null) return local; // This PC's renders from the originals and their sends beside the server's jobs (phase 5c). IReadOnlyList here = [.. local.Where(j => RunsHere(j.Kind))]; if (Api() is null || Volatile.Read(ref _serverJobs) is not { } jobs) return here; try { return [.. (await jobs.ListAsync(cancellationToken)).Concat(here)]; } catch (Exception ex) when (ServerProjectHome.IsUnavailable(ex, cancellationToken) || ex is ChronosApiException or EditorException) { Log.Info("editor", $"The server's jobs of workspace project {ProjectId} are not read now: {ex.Message}"); return here; } } public async Task CancelJobAsync(Guid jobId, CancellationToken cancellationToken) { if (_server is not null && !await IsLocalJobAsync(jobId, cancellationToken)) await ServerJobsOrThrow().CancelAsync(jobId, cancellationToken); else await (_engine ?? throw new InvalidOperationException(Loc.L("Editor.Error.NoEngine"))).CancelAsync(jobId, cancellationToken); } public async Task RetryJobAsync(Guid jobId, CancellationToken cancellationToken) { if (_server is not null && !await IsLocalJobAsync(jobId, cancellationToken)) await ServerJobsOrThrow().RetryAsync(jobId, cancellationToken); else await (_engine ?? throw new InvalidOperationException(Loc.L("Editor.Error.NoEngine"))).ResumeAsync(jobId, cancellationToken); } /// The kinds a workspace project runs on this PC's engine whatever its home: a render from its originals and its send home. private static bool RunsHere(JobKind kind) => kind is JobKind.WorkspaceRender or JobKind.WorkspaceSend; /// A job of this PC's engine that runs here whatever the home (). private async Task IsLocalJobAsync(Guid jobId, CancellationToken cancellationToken) => await _db.Jobs.GetAsync(jobId, cancellationToken) is { } job && RunsHere(job.Kind); /// /// The render at the home (owner review 2, R8: edit with proxies, render where the originals are): one job per preset /// (of this PC's engine, or the server's), with the project's portable settings (positions parked by photo, placed on /// the home's frames by the render) and the project recorded on the render; the version too when the settings are those /// of the last version the home holds (an unsaved working state, or one saved here and not sent yet, renders as it is, /// without a version). A server home (phase 5b) renders the version it holds when the project is in step with it /// (nothing saved or sent since its base): the jobs name that version alone and the server renders its settings with /// the jobs' export; a project out of step renders its portable settings, recording the project only, as a server knows /// no version of this workspace's that it was not sent. /// public IReadOnlyList RenderRequests(Sequence sequence, ProjectSettings project, ExportSettings export, int sourceWidth, int sourceHeight) { ArgumentNullException.ThrowIfNull(sequence); ArgumentNullException.ThrowIfNull(project); ArgumentNullException.ThrowIfNull(export); var portable = ProjectContent.ToPortable(project with { Export = export }, Photos); var hash = ProjectContent.Hash(portable); if (RendersLocally && Workspace is { } workspace) return LocalRenderRequests(sequence, workspace.Home, portable, hash, export, sourceWidth, sourceHeight); if (_server is not null) { if (InStepWithHome(project) is { } basis) { var version = new ProjectVersion { Id = basis.VersionId, ProjectId = ProjectId, Number = basis.Number, SettingsJson = portable, ContentHash = hash }; return [.. ProcessingJobs.RenderVersion(sequence, version, export, sourceWidth, sourceHeight).Select(r => r with { ParametersJson = WithoutSettings(r.ParametersJson) })]; } var unsent = new ProjectVersion { Id = Guid.Empty, ProjectId = ProjectId, Number = 0, SettingsJson = portable, ContentHash = hash }; return [.. ProcessingJobs.RenderVersion(sequence, unsent, export, sourceWidth, sourceHeight).Select(r => r with { ParametersJson = WithoutVersion(r.ParametersJson) })]; } var (tipId, tipHash) = Tip(); var tip = new ProjectVersion { Id = tipId ?? Guid.Empty, ProjectId = ProjectId, Number = 0, SettingsJson = portable, ContentHash = hash, }; var requests = ProcessingJobs.RenderVersion(sequence, tip, export, sourceWidth, sourceHeight); if (tipId is not null && ProjectContent.MatchesHash(portable, tipHash)) return requests; return [.. requests.Select(r => r with { ParametersJson = WithoutVersion(r.ParametersJson) })]; } /// /// The render on this PC from the originals the workspace holds (phase 5c): one per /// preset with the project's portable settings, naming the head version of versions/ when the settings are the /// ones it holds (the render records it; an unsaved working state renders without a version), sent home after. /// private IReadOnlyList LocalRenderRequests(Sequence sequence, BundleHome home, string portable, string hash, ExportSettings export, int sourceWidth, int sourceHeight) { var head = HeadVersion(portable); var version = new ProjectVersion { Id = head ?? Guid.Empty, ProjectId = ProjectId, Number = 0, SettingsJson = portable, ContentHash = hash }; var root = _sync.Store.Root; var server = home.IsArchive ? null : home.Server; return [ .. ProcessingJobs.RenderVersion(sequence, version, export, sourceWidth, sourceHeight).Select(r => WorkspaceJobs.Render(home.SequenceId, r.Label ?? sequence.DisplayTitle, root, ProjectId, head, (head is null ? WithoutVersion(r.ParametersJson) : r.ParametersJson)!, server)), ]; } /// The id of the newest version the workspace holds when it holds (any line ends); null otherwise. private Guid? HeadVersion(string portable) { try { return Project.Versions() is [.., var last] && ProjectContent.MatchesHash(portable, last.ContentHash) ? last.Id : null; } catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException) { Log.Warning("editor", $"The versions of workspace project {ProjectId} cannot be read; the render names none", ex); return null; } } /// The local renders of the project's ledger not at the home yet, as renders of (newest first). private IReadOnlyList UnsentOf(WorkspaceProject project, Guid sequenceId) { try { var unsent = project.ReadRenders().Unsent; Volatile.Write(ref _unsent, unsent.Count); return [ .. unsent.OrderByDescending(e => e.Rendered).Select(e => new Render { Id = e.RenderId, SequenceId = sequenceId, PresetName = e.PresetName, OutputPath = project.ExportFile(e.File), Codec = e.Codec, Width = e.Width, Height = e.Height, Fps = e.Fps, DurationSeconds = e.DurationSeconds, Bytes = e.Size, Created = e.Rendered, ProducedBy = Environment.MachineName, ProjectId = e.ProjectId, ProjectVersionId = e.ProjectVersionId, }), ]; } catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException) { Log.Warning("editor", $"The render ledger of workspace project {ProjectId} cannot be read", ex); Volatile.Write(ref _unsent, 0); return []; } } /// /// The home's version the project is in step with: its base, when no version waits to be sent and the editor's project /// () holds what the base holds; null otherwise. /// public BundleBase? InStepWithHome(ProjectSettings project) { ArgumentNullException.ThrowIfNull(project); try { if (_sync.Unpushed(ProjectId).Count > 0 || Project.ReadBundle().Workspace?.Base is not { } basis) return null; var working = ProjectContent.ToPortable(project, Photos); if (ProjectContent.MatchesHash(working, basis.ContentHash)) return basis; // Saved before a hash or a conversion at the home named its photos anew: the same choices are in step too. return Project.Versions().FirstOrDefault(v => v.Id == basis.VersionId) is { } version && ProjectContent.SameChoices(version.SettingsJson, working, Photos) ? basis : null; } catch (Exception ex) when (ex is IOException or InvalidDataException or InvalidOperationException) { Log.Warning("editor", $"The versions of workspace project {ProjectId} cannot be read; the render names none", ex); return null; } } public async ValueTask DisposeAsync() { if (Interlocked.Exchange(ref _disposed, 1) == 1) return; if (_engine is not null) _engine.JobChanged -= OnJobChanged; List sources; ServerJobs? jobs; IRemoteSequenceApi? api; lock (_gate) { sources = [.. _retired]; if (_frames is not null) sources.Add(_frames); _retired.Clear(); (_frames, _pixels) = (null, null); (jobs, _serverJobs, api, _api) = (_serverJobs, null, _api, null); } // The server's connection is the source's: it closes with it, also when no read made the jobs' listener. if (jobs is not null) await jobs.DisposeAsync(); else api?.Dispose(); foreach (var source in sources) { if (source is IAsyncDisposable disposable) await disposable.DisposeAsync(); } } /// The server's jobs of the project's sequence, or why there are none. /// This PC is not signed in to the server of the project. private ServerJobs ServerJobsOrThrow() => (Workspace is { } workspace ? ServerJobsOf(workspace.Home.SequenceId, Project.ReadBundle().Sequence.Title ?? string.Empty) : null) ?? throw new EditorException(Loc.L("Editor.Error.SignedOut")); /// /// Takes the media read now: a frame source over them when they differ from those read before (the old one is disposed /// with this source, as a decode may still read it), their frames and photos, the project's workspace section, and /// whether the settings are a newer Chronos's (said by when that changed). /// private void Swap(WorkspaceSequence media, WorkspaceMedia index, BundleWorkspace workspace, bool readOnly) { var ids = media.Frames.Select(f => f.Id).ToArray(); bool changed; lock (_gate) { if (_frames is null || index.FramesVersion != _framesVersion || !ids.AsSpan().SequenceEqual(_frameIds)) { if (_frames is not null) _retired.Add(_frames); _frames = media.OpenFrames(_decoder); _pixels = (IFrameSource)_frames; _frameCount = media.Frames.Count; } _frameIds = ids; _framesVersion = index.FramesVersion; _photos = index.Photos; _standIns = [.. index.Frames.Where(f => f.IsStandIn).Select(f => f.Id)]; _workspace = workspace; _holdsOriginals = index.HoldsOriginals; changed = _readOnly != readOnly; _readOnly = readOnly; } if (changed) ReadOnlyChanged?.Invoke(); } /// /// The version a render may name, with its content hash: the base, which the home holds, while no version waits to be /// sent; none otherwise. A version not sent yet may never reach the home under its id (a replay or a save-both sends it /// under another), so a render of it names the project alone. /// private (Guid? Id, string? Hash) Tip() { try { if (_sync.Unpushed(ProjectId).Count > 0) return (null, null); return Project.ReadBundle().Workspace?.Base is { } basis ? (basis.VersionId, basis.ContentHash) : (null, null); } catch (Exception ex) when (ex is IOException or InvalidDataException or InvalidOperationException) { Log.Warning("editor", $"The versions of workspace project {ProjectId} cannot be read; the render names none", ex); return (null, null); } } private static string? WithoutVersion(string? parametersJson) { if (parametersJson is null || JsonSerializer.Deserialize(parametersJson, ProtocolJson.Default.RenderJobParameters) is not { } parameters) return parametersJson; return JsonSerializer.Serialize(parameters with { ProjectVersionId = null }, ProtocolJson.Default.RenderJobParameters); } /// A job of a version the home holds: the home renders that version's settings (with the job's export), so none travel with it. private static string? WithoutSettings(string? parametersJson) { if (parametersJson is null || JsonSerializer.Deserialize(parametersJson, ProtocolJson.Default.RenderJobParameters) is not { } parameters) return parametersJson; return JsonSerializer.Serialize(parameters with { SettingsJson = null }, ProtocolJson.Default.RenderJobParameters); } /// /// The deep report the editor shows: the home's while its frames are the workspace's, else the copy the workspace took; /// whether one of another number of frames was read instead. /// private static (DeepAnalysisReport? Report, bool OtherFrames) ReadReport(SequencePaths? home, bool sameFrames, string copied, int frameCount) { var otherFrames = false; foreach (var path in new[] { sameFrames ? home?.AnalysisPath : null, copied }) { if (path is null || !File.Exists(path)) continue; try { var report = DeepAnalysisReport.Load(path); if (report.FrameCount == frameCount) return (report, false); otherFrames = true; } catch (Exception ex) when (ex is IOException or InvalidDataException or JsonException or UnauthorizedAccessException) { Log.Warning("editor", $"{path} cannot be read", ex); } } return (null, otherFrames); } private void OnJobChanged(Job job) => JobChanged?.Invoke(job); }