Add Mimir output controls
This commit is contained in:
parent
a1de06a100
commit
f083a4fbae
8 changed files with 321 additions and 66 deletions
|
|
@ -45,6 +45,7 @@ public sealed class MimirRuntime : IAquariumRuntime, IAquariumRuntimeServicesRec
|
|||
private readonly MimirPresentationControlState presentationControls = new();
|
||||
private readonly MimirSceneEditorState sceneEditor = new();
|
||||
private readonly MimirProgramRecorder programRecorder = new();
|
||||
private readonly MimirProgramStreamer programStreamer = new();
|
||||
private readonly MimirProgramSurfaceConfigDocument programSurfaceConfig;
|
||||
private readonly MimirObsStemPublicationState obsStemPublication = new(MimirObsPublicationConfigurations.NativeProgram);
|
||||
private readonly MimirObsStemSharedMemoryPublisher? obsStemPublisher;
|
||||
|
|
@ -827,6 +828,7 @@ public sealed class MimirRuntime : IAquariumRuntime, IAquariumRuntimeServicesRec
|
|||
UpdateAudioSpectra();
|
||||
UpdateAudioSynchronization();
|
||||
programRecorder.Refresh();
|
||||
programStreamer.Refresh();
|
||||
EmitTelemetry();
|
||||
}
|
||||
|
||||
|
|
@ -858,6 +860,7 @@ public sealed class MimirRuntime : IAquariumRuntime, IAquariumRuntimeServicesRec
|
|||
{
|
||||
obsStemPublisher?.Dispose();
|
||||
programRecorder.Dispose();
|
||||
programStreamer.Dispose();
|
||||
synchronization.Dispose();
|
||||
}
|
||||
|
||||
|
|
@ -871,12 +874,6 @@ public sealed class MimirRuntime : IAquariumRuntime, IAquariumRuntimeServicesRec
|
|||
shell.Pane("mimir.video-sources", "Video Sources", graph =>
|
||||
{
|
||||
graph.Text("video.program-summary", DescribeProgramVideo, "caption", weight: 0.75f);
|
||||
graph.Row("video.program-recording", row =>
|
||||
{
|
||||
row.Button("program.record", "Record", StartProgramRecording, weight: 0.7f);
|
||||
row.Button("program.record-stop", "Stop", programRecorder.Stop, weight: 0.55f);
|
||||
row.Text("program.record-status", programRecorder.Describe, "caption", weight: 1.65f);
|
||||
}, weight: 0.65f);
|
||||
graph.Text("video.source-list-title", "Program Sources", "strong", weight: 0.55f);
|
||||
for (var slot = 0; slot < VideoSourceListSlotCount; slot++)
|
||||
{
|
||||
|
|
@ -926,34 +923,56 @@ public sealed class MimirRuntime : IAquariumRuntime, IAquariumRuntimeServicesRec
|
|||
}, weight: 0.95f);
|
||||
}, weight: 1.75f);
|
||||
|
||||
shell.Pane("mimir.inspector", "Inspector", inspector =>
|
||||
shell.Vertical("mimir.right-rail", right =>
|
||||
{
|
||||
inspector.Text("inspector.selection", sceneEditor.DescribeSelection, "strong");
|
||||
inspector.Options("inspector.video-feed", "Feed", () => presentationControls.SelectedVideoIndex, SelectVideoLayer, VideoFeedOptions());
|
||||
inspector.Vertical("inspector.source-controls", source =>
|
||||
right.Pane("mimir.inspector", "Inspector", inspector =>
|
||||
{
|
||||
source.Toggle("video.visible", "Visible", () => sceneEditor.SelectedNode?.Visible ?? false, SetSelectedVideoVisible);
|
||||
source.Toggle("video.solo", "Solo", () => presentationControls.SelectedVideo?.Solo ?? false, SetSelectedVideoSolo);
|
||||
source.Slider("video.opacity", "Opacity", () => presentationControls.SelectedVideo?.Opacity ?? 1.0f, SetSelectedVideoOpacity, 0.0f, 1.0f, "0.00");
|
||||
source.Text("inspector.program-placement", "Program Frame", "strong");
|
||||
source.Slider("program.x", "X", () => sceneEditor.SelectedProgramX, sceneEditor.SetSelectedProgramX, 0.0f, 1.0f, "0.000");
|
||||
source.Slider("program.y", "Y", () => sceneEditor.SelectedProgramY, sceneEditor.SetSelectedProgramY, 0.0f, 1.0f, "0.000");
|
||||
source.Slider("program.w", "W", () => sceneEditor.SelectedProgramWidth, sceneEditor.SetSelectedProgramWidth, 0.01f, 1.0f, "0.000");
|
||||
source.Slider("program.h", "H", () => sceneEditor.SelectedProgramHeight, sceneEditor.SetSelectedProgramHeight, 0.01f, 1.0f, "0.000");
|
||||
source.Slider("program.rotation", "Rotation", () => sceneEditor.SelectedNode?.Transform.RotationRadians ?? 0.0f, sceneEditor.SetSelectedRotation, -MathF.PI, MathF.PI, "0.00");
|
||||
source.Options("program.fit-mode", "Fit", () => sceneEditor.SelectedFitModeIndex, sceneEditor.SetSelectedFitModeIndex, sceneEditor.FitModeOptions);
|
||||
source.Row("program.fit", fit =>
|
||||
inspector.Text("inspector.selection", sceneEditor.DescribeSelection, "strong");
|
||||
inspector.Options("inspector.video-feed", "Feed", () => presentationControls.SelectedVideoIndex, SelectVideoLayer, VideoFeedOptions());
|
||||
inspector.Vertical("inspector.source-controls", source =>
|
||||
{
|
||||
fit.Button("program.center", "Center", sceneEditor.CenterSelectedProgramLayer);
|
||||
fit.Button("program.fit-selected", "Fit", sceneEditor.FitSelectedProgramLayer);
|
||||
fit.Button("program.fill-selected", "Fill", sceneEditor.FillSelectedProgramLayer);
|
||||
});
|
||||
source.Row("program.order", order =>
|
||||
source.Toggle("video.visible", "Visible", () => sceneEditor.SelectedNode?.Visible ?? false, SetSelectedVideoVisible);
|
||||
source.Toggle("video.solo", "Solo", () => presentationControls.SelectedVideo?.Solo ?? false, SetSelectedVideoSolo);
|
||||
source.Slider("video.opacity", "Opacity", () => presentationControls.SelectedVideo?.Opacity ?? 1.0f, SetSelectedVideoOpacity, 0.0f, 1.0f, "0.00");
|
||||
source.Text("inspector.program-placement", "Program Frame", "strong");
|
||||
source.Slider("program.x", "X", () => sceneEditor.SelectedProgramX, sceneEditor.SetSelectedProgramX, 0.0f, 1.0f, "0.000");
|
||||
source.Slider("program.y", "Y", () => sceneEditor.SelectedProgramY, sceneEditor.SetSelectedProgramY, 0.0f, 1.0f, "0.000");
|
||||
source.Slider("program.w", "W", () => sceneEditor.SelectedProgramWidth, sceneEditor.SetSelectedProgramWidth, 0.01f, 1.0f, "0.000");
|
||||
source.Slider("program.h", "H", () => sceneEditor.SelectedProgramHeight, sceneEditor.SetSelectedProgramHeight, 0.01f, 1.0f, "0.000");
|
||||
source.Slider("program.rotation", "Rotation", () => sceneEditor.SelectedNode?.Transform.RotationRadians ?? 0.0f, sceneEditor.SetSelectedRotation, -MathF.PI, MathF.PI, "0.00");
|
||||
source.Options("program.fit-mode", "Fit", () => sceneEditor.SelectedFitModeIndex, sceneEditor.SetSelectedFitModeIndex, sceneEditor.FitModeOptions);
|
||||
source.Row("program.fit", fit =>
|
||||
{
|
||||
fit.Button("program.center", "Center", sceneEditor.CenterSelectedProgramLayer);
|
||||
fit.Button("program.fit-selected", "Fit", sceneEditor.FitSelectedProgramLayer);
|
||||
fit.Button("program.fill-selected", "Fill", sceneEditor.FillSelectedProgramLayer);
|
||||
});
|
||||
source.Row("program.order", order =>
|
||||
{
|
||||
order.Button("program.layer-up", "Layer Up", () => MoveSelectedVideoLayer(-1));
|
||||
order.Button("program.layer-down", "Layer Down", () => MoveSelectedVideoLayer(1));
|
||||
});
|
||||
}, isVisible: () => sceneEditor.HasSelectedProgramSource);
|
||||
}, weight: 1.05f);
|
||||
|
||||
right.Pane("mimir.output", "Output", output =>
|
||||
{
|
||||
output.Text("output.program-summary", DescribeProgramOutput, "caption", weight: 0.6f);
|
||||
output.Row("output.recording", row =>
|
||||
{
|
||||
order.Button("program.layer-up", "Layer Up", () => MoveSelectedVideoLayer(-1));
|
||||
order.Button("program.layer-down", "Layer Down", () => MoveSelectedVideoLayer(1));
|
||||
});
|
||||
}, isVisible: () => sceneEditor.HasSelectedProgramSource);
|
||||
row.Button("program.record", "Record", StartProgramRecording, weight: 0.7f);
|
||||
row.Button("program.record-stop", "Stop", programRecorder.Stop, weight: 0.55f);
|
||||
row.Text("program.record-status", programRecorder.Describe, "caption", weight: 1.75f);
|
||||
}, weight: 0.7f);
|
||||
output.Text("output.record-path", () => $"folder {programRecorder.OutputDirectory}", "caption", weight: 0.7f);
|
||||
output.Row("output.streaming", row =>
|
||||
{
|
||||
row.Button("program.stream", "Live", StartProgramStreaming, weight: 0.7f);
|
||||
row.Button("program.stream-stop", "Stop", programStreamer.Stop, weight: 0.55f);
|
||||
row.Text("program.stream-status", programStreamer.Describe, "caption", weight: 1.75f);
|
||||
}, weight: 0.7f);
|
||||
output.Text("output.stream-target", () => $"target {programStreamer.TargetUrl}", "caption", weight: 0.7f);
|
||||
}, weight: 0.95f);
|
||||
}, weight: 0.95f);
|
||||
});
|
||||
});
|
||||
|
|
@ -1135,6 +1154,11 @@ public sealed class MimirRuntime : IAquariumRuntime, IAquariumRuntimeServicesRec
|
|||
programRecorder.Start();
|
||||
}
|
||||
|
||||
private void StartProgramStreaming()
|
||||
{
|
||||
programStreamer.Start();
|
||||
}
|
||||
|
||||
private string DescribeVideoSourceSlot(int index)
|
||||
{
|
||||
var feeds = presentationControls.VideoFeeds;
|
||||
|
|
@ -1320,6 +1344,13 @@ public sealed class MimirRuntime : IAquariumRuntime, IAquariumRuntimeServicesRec
|
|||
return string.Join(" | ", active);
|
||||
}
|
||||
|
||||
private string DescribeProgramOutput()
|
||||
{
|
||||
var videoCount = presentationControls.VideoFeeds.Count(feed => presentationControls.IncludesVideo(feed.SourceId));
|
||||
var audioCount = presentationControls.AudioFeeds.Count;
|
||||
return $"program {videoCount} video / {audioCount} audio";
|
||||
}
|
||||
|
||||
private string DescribePostprocess()
|
||||
{
|
||||
var post = presentationControls.Postprocess;
|
||||
|
|
|
|||
|
|
@ -15,13 +15,16 @@ public sealed class MimirProgramRecorder : IDisposable
|
|||
this.repositoryRoot = string.IsNullOrWhiteSpace(repositoryRoot)
|
||||
? ResolveRepositoryRoot()
|
||||
: Path.GetFullPath(repositoryRoot);
|
||||
LatestOutputPath = Path.Combine(this.repositoryRoot, "artifacts", "runtime", "stream-proof", "mimir-program-record-latest.mp4");
|
||||
OutputDirectory = ResolveOutputDirectory();
|
||||
LatestOutputPath = CreateDefaultOutputPath(OutputDirectory);
|
||||
}
|
||||
|
||||
public bool IsRecording => process is { HasExited: false };
|
||||
|
||||
public string LatestOutputPath { get; private set; }
|
||||
|
||||
public string OutputDirectory { get; }
|
||||
|
||||
public string Describe()
|
||||
{
|
||||
Refresh();
|
||||
|
|
@ -53,7 +56,7 @@ public sealed class MimirProgramRecorder : IDisposable
|
|||
|
||||
var duration = Math.Max(1, durationSeconds ?? ReadDurationSeconds());
|
||||
LatestOutputPath = Path.GetFullPath(string.IsNullOrWhiteSpace(outputPath)
|
||||
? Path.Combine(repositoryRoot, "artifacts", "runtime", "stream-proof", "mimir-program-record-latest.mp4")
|
||||
? CreateDefaultOutputPath(OutputDirectory)
|
||||
: Path.IsPathRooted(outputPath) ? outputPath : Path.Combine(repositoryRoot, outputPath));
|
||||
var logRoot = Path.Combine(repositoryRoot, "artifacts", "runtime", "stream-proof");
|
||||
Directory.CreateDirectory(logRoot);
|
||||
|
|
@ -164,6 +167,32 @@ public sealed class MimirProgramRecorder : IDisposable
|
|||
: DefaultDurationSeconds;
|
||||
}
|
||||
|
||||
private static string ResolveOutputDirectory()
|
||||
{
|
||||
var configured = Environment.GetEnvironmentVariable("MIMIR_RECORD_OUTPUT_DIR");
|
||||
if (!string.IsNullOrWhiteSpace(configured))
|
||||
{
|
||||
return Path.GetFullPath(configured);
|
||||
}
|
||||
|
||||
var videos = Environment.GetFolderPath(Environment.SpecialFolder.MyVideos);
|
||||
if (string.IsNullOrWhiteSpace(videos))
|
||||
{
|
||||
var profile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
|
||||
videos = string.IsNullOrWhiteSpace(profile)
|
||||
? @"C:\Users\Meta\Videos"
|
||||
: Path.Combine(profile, "Videos");
|
||||
}
|
||||
|
||||
return Path.Combine(videos, "Mimir");
|
||||
}
|
||||
|
||||
private static string CreateDefaultOutputPath(string outputDirectory)
|
||||
{
|
||||
var stamp = DateTimeOffset.Now.ToString("yyyyMMdd-HHmmss", System.Globalization.CultureInfo.InvariantCulture);
|
||||
return Path.Combine(outputDirectory, $"mimir-program-{stamp}.mp4");
|
||||
}
|
||||
|
||||
private static bool IsTruthy(string? value) =>
|
||||
!string.IsNullOrWhiteSpace(value) &&
|
||||
(string.Equals(value, "1", StringComparison.OrdinalIgnoreCase) ||
|
||||
|
|
|
|||
179
src/Mimir.Runtime/Synchronization/MimirProgramStreamer.cs
Normal file
179
src/Mimir.Runtime/Synchronization/MimirProgramStreamer.cs
Normal file
|
|
@ -0,0 +1,179 @@
|
|||
using System.Diagnostics;
|
||||
|
||||
namespace Mimir.Runtime.Synchronization;
|
||||
|
||||
public sealed class MimirProgramStreamer : IDisposable
|
||||
{
|
||||
private readonly string repositoryRoot;
|
||||
private Process? process;
|
||||
private string status = "idle";
|
||||
private DateTimeOffset? startedAt;
|
||||
|
||||
public MimirProgramStreamer(string? repositoryRoot = null)
|
||||
{
|
||||
this.repositoryRoot = string.IsNullOrWhiteSpace(repositoryRoot)
|
||||
? ResolveRepositoryRoot()
|
||||
: Path.GetFullPath(repositoryRoot);
|
||||
}
|
||||
|
||||
public bool IsStreaming => process is { HasExited: false };
|
||||
|
||||
public string TargetUrl => Environment.GetEnvironmentVariable("MIMIR_STREAM_TARGET")
|
||||
?? "rtmp://127.0.0.1:11935/live/mimir";
|
||||
|
||||
public string Describe()
|
||||
{
|
||||
Refresh();
|
||||
if (IsStreaming)
|
||||
{
|
||||
var elapsed = startedAt == null
|
||||
? TimeSpan.Zero
|
||||
: DateTimeOffset.Now - startedAt.Value;
|
||||
return $"live {elapsed.TotalSeconds:0}s pid={process!.Id}";
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
public bool Start()
|
||||
{
|
||||
Refresh();
|
||||
if (IsStreaming)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var projectPath = Path.Combine(repositoryRoot, "src", "Mimir.Broadcast", "Mimir.Broadcast.csproj");
|
||||
if (!File.Exists(projectPath))
|
||||
{
|
||||
status = "broadcast project missing";
|
||||
return false;
|
||||
}
|
||||
|
||||
var inputPath = Environment.GetEnvironmentVariable("MIMIR_STREAM_INPUT");
|
||||
var allowTestPattern = IsTruthy(Environment.GetEnvironmentVariable("MIMIR_STREAM_ALLOW_TEST_PATTERN"));
|
||||
if (string.IsNullOrWhiteSpace(inputPath) && !allowTestPattern)
|
||||
{
|
||||
status = "stream input missing: set MIMIR_STREAM_INPUT";
|
||||
return false;
|
||||
}
|
||||
|
||||
var logRoot = Path.Combine(repositoryRoot, "artifacts", "runtime", "stream-proof");
|
||||
Directory.CreateDirectory(logRoot);
|
||||
|
||||
var startInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = "dotnet",
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
WorkingDirectory = repositoryRoot,
|
||||
};
|
||||
startInfo.ArgumentList.Add("run");
|
||||
startInfo.ArgumentList.Add("--project");
|
||||
startInfo.ArgumentList.Add(projectPath);
|
||||
startInfo.ArgumentList.Add("--");
|
||||
startInfo.ArgumentList.Add("--target");
|
||||
startInfo.ArgumentList.Add(TargetUrl);
|
||||
if (!string.IsNullOrWhiteSpace(inputPath))
|
||||
{
|
||||
startInfo.ArgumentList.Add("--no-test-pattern");
|
||||
startInfo.ArgumentList.Add("--input");
|
||||
startInfo.ArgumentList.Add(Path.GetFullPath(Path.IsPathRooted(inputPath) ? inputPath : Path.Combine(repositoryRoot, inputPath)));
|
||||
}
|
||||
|
||||
process = Process.Start(startInfo);
|
||||
if (process == null)
|
||||
{
|
||||
status = "stream launch failed";
|
||||
return false;
|
||||
}
|
||||
|
||||
_ = DrainAsync(process.StandardOutput, Path.Combine(logRoot, "mimir-app-stream.out.log"));
|
||||
_ = DrainAsync(process.StandardError, Path.Combine(logRoot, "mimir-app-stream.err.log"));
|
||||
startedAt = DateTimeOffset.Now;
|
||||
status = $"live pid={process.Id}";
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
Refresh();
|
||||
if (!IsStreaming)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
process!.Kill(entireProcessTree: true);
|
||||
status = "stream stopped";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
status = $"stream stop failed: {ex.Message}";
|
||||
}
|
||||
finally
|
||||
{
|
||||
process?.Dispose();
|
||||
process = null;
|
||||
startedAt = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void Refresh()
|
||||
{
|
||||
if (process == null || !process.HasExited)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var exitCode = process.ExitCode;
|
||||
process.Dispose();
|
||||
process = null;
|
||||
startedAt = null;
|
||||
status = exitCode == 0
|
||||
? "stream ended"
|
||||
: $"stream failed exit={exitCode}";
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Stop();
|
||||
}
|
||||
|
||||
private static async Task DrainAsync(StreamReader reader, string path)
|
||||
{
|
||||
await using var stream = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.Read);
|
||||
await using var writer = new StreamWriter(stream);
|
||||
while (await reader.ReadLineAsync().ConfigureAwait(false) is { } line)
|
||||
{
|
||||
await writer.WriteLineAsync(line).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsTruthy(string? value) =>
|
||||
!string.IsNullOrWhiteSpace(value) &&
|
||||
(string.Equals(value, "1", StringComparison.OrdinalIgnoreCase) ||
|
||||
string.Equals(value, "true", StringComparison.OrdinalIgnoreCase) ||
|
||||
string.Equals(value, "yes", StringComparison.OrdinalIgnoreCase) ||
|
||||
string.Equals(value, "on", StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
private static string ResolveRepositoryRoot()
|
||||
{
|
||||
var directory = new DirectoryInfo(AppContext.BaseDirectory);
|
||||
while (directory != null)
|
||||
{
|
||||
if (Directory.Exists(Path.Combine(directory.FullName, "scripts")) &&
|
||||
File.Exists(Path.Combine(directory.FullName, "src", "Mimir.Runtime", "Mimir.Runtime.csproj")))
|
||||
{
|
||||
return directory.FullName;
|
||||
}
|
||||
|
||||
directory = directory.Parent;
|
||||
}
|
||||
|
||||
return Directory.GetCurrentDirectory();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue