Import OBS scene into Mimir program state

This commit is contained in:
Meta Vens 2026-06-09 03:04:51 +01:00
commit 28536681f6
8 changed files with 384 additions and 0 deletions

View file

@ -109,3 +109,11 @@ The proof path is:
dotnet run --project .\src\Mimir.BufferSmoke\Mimir.BufferSmoke.csproj -- --perfect-machine-contract-smoke
dotnet run --project .\src\Mimir.BufferSmoke\Mimir.BufferSmoke.csproj -- --perfect-machine-profile-smoke
```
Import the current OBS scene as Mimir-owned typed state:
```powershell
dotnet run --project .\src\Mimir.BufferSmoke\Mimir.BufferSmoke.csproj -- --import-obs-program-scene --output state\mimir-program-composition.cc
```
The live imported state file is `state/mimir-program-composition.cc`.

View file

@ -107,6 +107,8 @@ The first typed program contracts live in `src/Mimir.Runtime/Synchronization`:
OBS scene JSON is import/mirror evidence for the initial crop/key/transform
layout. It is not the editor of record after Mimir owns the scene graph.
The current imported local scene state lives at
`state/mimir-program-composition.cc`.
## OBS Bridge Utility

View file

@ -49,6 +49,11 @@ Get-Content .\state\evidence.jsonl -Tail 8
`mimir.program_scene.v1`, `mimir.program_output.v1`, and
`mimir.eve_operator_surface.v1`; `Mimir.BufferSmoke
--perfect-machine-contract-smoke` writes them into CultCache.
- `Mimir.BufferSmoke --import-obs-program-scene --output
state/mimir-program-composition.cc` imports the current OBS scene JSON into
Mimir-owned typed state. The latest imported scene has eight layers, six
visible, two cropped, one chroma-keyed, and Raven mapped as
`muninn:raven:monitor:primary`.
- Local six-camera ingest should use direct driver adapters. Process-backed
sources are bridge/network edges only.
- Leap stereo IR is the first timing-camera candidate for direct ingest.

View file

@ -91,6 +91,15 @@ if (args.Any(arg => string.Equals(arg, "--perfect-machine-contract-smoke", Strin
.ConfigureAwait(false);
}
if (args.Any(arg => string.Equals(arg, "--import-obs-program-scene", StringComparison.OrdinalIgnoreCase)))
{
return await ImportObsProgramSceneAsync(
ParseStringOption(args, "--input", DefaultObsScenePath()),
ParseStringOption(args, "--scene", ""),
ParseStringOption(args, "--output", "state/mimir-program-composition.cc"))
.ConfigureAwait(false);
}
if (args.Any(arg => string.Equals(arg, "--perfect-machine-manifest", StringComparison.OrdinalIgnoreCase)))
{
return await WritePerfectMachineManifestAsync(
@ -1595,6 +1604,72 @@ static async Task<int> RunPerfectMachineContractSmokeAsync(string outputPath)
return 0;
}
static async Task<int> ImportObsProgramSceneAsync(string inputPath, string sceneName, string outputPath)
{
if (!File.Exists(inputPath))
{
Console.Error.WriteLine($"OBS scene file not found: {inputPath}");
return 1;
}
var importedAt = DateTimeOffset.UtcNow;
var scene = MimirObsSceneImporter.ImportFile(
inputPath,
string.IsNullOrWhiteSpace(sceneName) ? null : sceneName,
importedAt);
var output = MimirCultMeshContractFactory.CreateProgramOutput(
"mimir-imported-program",
scene.SceneId,
MimirProgramPublicationConfigurations.YggdrasilSiteProgram,
importedAt);
var eveSurface = MimirCultMeshContractFactory.CreateOperatorSurface(
"mimir-eve-gui-compositor",
scene.SceneId,
MimirProgramPublicationConfigurations.OperatorSurfaces[0],
importedAt);
Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(outputPath)) ?? ".");
using var cache = await CultCacheMessagePack.OpenAsync(outputPath, new CultCacheOpenOptions
{
PullOnOpen = File.Exists(outputPath)
}).ConfigureAwait(false);
await cache.UpsertAsync(
scene,
new CultRecordHandle<MimirProgramSceneDocument>(new CultRecordKey($"mimir-program-scene:{scene.SceneId}")))
.ConfigureAwait(false);
await cache.UpsertAsync(
output,
new CultRecordHandle<MimirProgramOutputDocument>(new CultRecordKey($"mimir-program-output:{output.OutputId}")))
.ConfigureAwait(false);
await cache.UpsertAsync(
eveSurface,
new CultRecordHandle<MimirEveOperatorSurfaceDocument>(new CultRecordKey($"mimir-eve-surface:{eveSurface.SurfaceId}")))
.ConfigureAwait(false);
await cache.FlushAsync().ConfigureAwait(false);
var visible = scene.Layers.Count(layer => layer.Visible);
var keyed = scene.Layers.Count(layer => layer.ChromaKey is not null);
var cropped = scene.Layers.Count(layer =>
layer.Crop.Left != 0.0 || layer.Crop.Top != 0.0 || layer.Crop.Right != 0.0 || layer.Crop.Bottom != 0.0);
Console.WriteLine(
$"obs-program-scene-import input={inputPath} output={outputPath} scene={scene.SceneId} canvas={scene.CanvasWidth}x{scene.CanvasHeight} layers={scene.Layers.Length} visible={visible} cropped={cropped} keyed={keyed} route={output.PublisherRoute}");
foreach (var layer in scene.Layers.OrderBy(layer => layer.ZIndex))
{
Console.WriteLine(
$"layer {layer.ZIndex}:{layer.LayerId} visible={layer.Visible} source={layer.SourceRef} kind={layer.SourceKind} pos={layer.X:0.###},{layer.Y:0.###} size={layer.Width:0.###}x{layer.Height:0.###} crop={layer.Crop.Left:0.###},{layer.Crop.Top:0.###},{layer.Crop.Right:0.###},{layer.Crop.Bottom:0.###} key={(layer.ChromaKey is null ? "none" : layer.ChromaKey.KeyColorRgba.ToString("x8"))}");
}
return 0;
}
static string DefaultObsScenePath() =>
Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"obs-studio",
"basic",
"scenes",
"Untitled.json");
static async Task<int> WritePerfectMachineManifestAsync(string outputPath)
{
var manifest = MimirPerfectMachineManifestFactory.Create();

View file

@ -0,0 +1,288 @@
using System.Text.Json;
namespace Mimir.Runtime.Synchronization;
public static class MimirObsSceneImporter
{
public static MimirProgramSceneDocument ImportFile(
string path,
string? sceneName = null,
DateTimeOffset? updatedAt = null)
{
using var stream = File.OpenRead(path);
using var document = JsonDocument.Parse(stream);
return Import(document.RootElement, sceneName, updatedAt);
}
public static MimirProgramSceneDocument Import(
JsonElement root,
string? sceneName = null,
DateTimeOffset? updatedAt = null)
{
var sources = root.GetProperty("sources").EnumerateArray().ToArray();
var sourceByName = sources
.Where(source => source.TryGetProperty("name", out _))
.ToDictionary(source => source.GetProperty("name").GetString() ?? "", StringComparer.Ordinal);
var selectedSceneName = !string.IsNullOrWhiteSpace(sceneName)
? sceneName
: root.TryGetProperty("current_program_scene", out var currentProgram)
? currentProgram.GetString()
: root.TryGetProperty("current_scene", out var currentScene)
? currentScene.GetString()
: null;
if (string.IsNullOrWhiteSpace(selectedSceneName))
{
selectedSceneName = "Scene";
}
if (!sourceByName.TryGetValue(selectedSceneName, out var sceneSource))
{
throw new InvalidOperationException($"OBS scene '{selectedSceneName}' was not found.");
}
var settings = sceneSource.GetProperty("settings");
var itemElements = settings.GetProperty("items").EnumerateArray().ToArray();
var layers = new List<MimirProgramSceneLayer>(itemElements.Length);
for (var index = 0; index < itemElements.Length; index++)
{
var item = itemElements[index];
var name = item.GetProperty("name").GetString() ?? $"layer-{index}";
sourceByName.TryGetValue(name, out var source);
var sourceKind = SourceKind(source);
var crop = CombineCrop(ItemCrop(item), FilterCrop(source));
var chromaKey = FilterChromaKey(source);
var (x, y, width, height) = Bounds(item);
layers.Add(new MimirProgramSceneLayer(
LayerId: LayerId(name, index),
SourceRef: SourceRef(name, sourceKind),
SourceKind: sourceKind,
Visible: ReadBool(item, "visible", fallback: true),
X: x,
Y: y,
Width: width,
Height: height,
ZIndex: index,
Crop: crop,
ChromaKey: chromaKey));
}
var resolution = root.TryGetProperty("resolution", out var resolutionElement)
? resolutionElement
: default;
return new MimirProgramSceneDocument(
SceneId: SceneId(selectedSceneName),
UpdatedAtUtc: (updatedAt ?? DateTimeOffset.UtcNow).ToString("O"),
CanvasWidth: ReadInt(resolution, "x", 1920),
CanvasHeight: ReadInt(resolution, "y", 1080),
Owner: "Mimir",
Layers: layers.ToArray());
}
private static string SourceKind(JsonElement source)
{
if (source.ValueKind == JsonValueKind.Undefined)
{
return "unknown";
}
return (source.GetProperty("id").GetString() ?? "") switch
{
"monitor_capture" => "monitor",
"window_capture" => "window",
"dshow_input" => "camera",
"browser_source" => "browser",
"ffmpeg_source" => "network-monitor",
"mimir_program_texture_source" => "mimir-program-texture",
"muninn_stream_source" => "muninn-stream",
_ => "unknown"
};
}
private static string SourceRef(string name, string sourceKind)
{
var normalized = LayerId(name, 0);
if (name.Contains("raven", StringComparison.OrdinalIgnoreCase))
{
return sourceKind.Contains("audio", StringComparison.OrdinalIgnoreCase)
? "muninn:raven:audio:realtek-loopback"
: "muninn:raven:monitor:primary";
}
if (sourceKind == "monitor")
{
return name.Contains("bigscreen", StringComparison.OrdinalIgnoreCase)
? "muninn:starfire:monitor:bigscreen"
: "muninn:starfire:monitor:primary";
}
if (sourceKind == "window")
{
return $"muninn:starfire:window:{normalized}";
}
if (sourceKind == "camera")
{
return $"muninn:starfire:camera:{normalized}";
}
if (sourceKind == "mimir-program-texture")
{
return "mimir:generated:program-texture";
}
if (sourceKind == "browser")
{
return $"mimir:browser:{normalized}";
}
if (sourceKind == "muninn-stream")
{
return $"muninn:starfire:stream:{normalized}";
}
return $"mimir:unknown:{normalized}";
}
private static (double X, double Y, double Width, double Height) Bounds(JsonElement item)
{
var pos = item.TryGetProperty("pos", out var posElement) ? posElement : default;
var scale = item.TryGetProperty("scale", out var scaleElement) ? scaleElement : default;
var scaleRef = item.TryGetProperty("scale_ref", out var scaleRefElement) ? scaleRefElement : default;
var bounds = item.TryGetProperty("bounds", out var boundsElement) ? boundsElement : default;
var width = ReadDouble(bounds, "x", 0.0);
var height = ReadDouble(bounds, "y", 0.0);
var scaledWidth = ReadDouble(scaleRef, "x", 0.0) * Math.Abs(ReadDouble(scale, "x", 1.0));
var scaledHeight = ReadDouble(scaleRef, "y", 0.0) * Math.Abs(ReadDouble(scale, "y", 1.0));
if (scaledWidth > width)
{
width = scaledWidth;
}
if (scaledHeight > height)
{
height = scaledHeight;
}
return (
ReadDouble(pos, "x", 0.0),
ReadDouble(pos, "y", 0.0),
width,
height);
}
private static MimirProgramCrop ItemCrop(JsonElement item) =>
new(
ReadDouble(item, "crop_left", 0.0),
ReadDouble(item, "crop_top", 0.0),
ReadDouble(item, "crop_right", 0.0),
ReadDouble(item, "crop_bottom", 0.0));
private static MimirProgramCrop FilterCrop(JsonElement source)
{
if (source.ValueKind == JsonValueKind.Undefined || !source.TryGetProperty("filters", out var filters))
{
return new MimirProgramCrop(0, 0, 0, 0);
}
foreach (var filter in filters.EnumerateArray())
{
if ((filter.GetProperty("id").GetString() ?? "") != "crop_filter")
{
continue;
}
var settings = filter.GetProperty("settings");
return new MimirProgramCrop(
ReadDouble(settings, "left", 0.0),
ReadDouble(settings, "top", 0.0),
ReadDouble(settings, "right", 0.0),
ReadDouble(settings, "bottom", 0.0));
}
return new MimirProgramCrop(0, 0, 0, 0);
}
private static MimirProgramChromaKey? FilterChromaKey(JsonElement source)
{
if (source.ValueKind == JsonValueKind.Undefined || !source.TryGetProperty("filters", out var filters))
{
return null;
}
MimirProgramChromaKey? strongest = null;
foreach (var filter in filters.EnumerateArray())
{
if ((filter.GetProperty("id").GetString() ?? "") != "chroma_key_filter")
{
continue;
}
var settings = filter.GetProperty("settings");
strongest = new MimirProgramChromaKey(
ReadUInt(settings, "key_color", 0),
ReadDouble(settings, "similarity", 0.0),
ReadDouble(settings, "smoothness", 0.0),
ReadDouble(settings, "spill", 0.0));
}
return strongest;
}
private static MimirProgramCrop CombineCrop(MimirProgramCrop item, MimirProgramCrop filter) =>
new(
item.Left + filter.Left,
item.Top + filter.Top,
item.Right + filter.Right,
item.Bottom + filter.Bottom);
private static string SceneId(string name) =>
$"mimir-scene-{LayerId(name, 0)}";
private static string LayerId(string name, int fallback)
{
var chars = name
.Trim()
.ToLowerInvariant()
.Select(character => char.IsLetterOrDigit(character) ? character : '-')
.ToArray();
var id = string.Join("-", new string(chars).Split('-', StringSplitOptions.RemoveEmptyEntries));
return string.IsNullOrWhiteSpace(id) ? $"layer-{fallback}" : id;
}
private static bool ReadBool(JsonElement element, string property, bool fallback) =>
element.ValueKind != JsonValueKind.Undefined &&
element.TryGetProperty(property, out var value) &&
value.ValueKind is JsonValueKind.True or JsonValueKind.False
? value.GetBoolean()
: fallback;
private static int ReadInt(JsonElement element, string property, int fallback) =>
element.ValueKind != JsonValueKind.Undefined &&
element.TryGetProperty(property, out var value) &&
value.TryGetInt32(out var parsed)
? parsed
: fallback;
private static double ReadDouble(JsonElement element, string property, double fallback) =>
element.ValueKind != JsonValueKind.Undefined &&
element.TryGetProperty(property, out var value) &&
value.TryGetDouble(out var parsed)
? parsed
: fallback;
private static uint ReadUInt(JsonElement element, string property, uint fallback)
{
if (element.ValueKind == JsonValueKind.Undefined ||
!element.TryGetProperty(property, out var value))
{
return fallback;
}
return value.TryGetUInt32(out var parsed)
? parsed
: value.TryGetInt64(out var signed)
? unchecked((uint)signed)
: fallback;
}
}

View file

@ -122,3 +122,4 @@
{"ts": "2026-06-08T09:29:43+01:00", "type": "decision", "status": "ok", "note": "Added CultMesh media bridge for Raven program feed: Raven sender publishes rolling mimir.cultmesh_media_frame MPEG-TS chunks over CultNet reliable UDP to Yggdrasil 10.77.0.1:3075; Starfire receiver lowers raven-primary-av to local UDP 127.0.0.1:5200 for OBS. Yggdrasil relay and Starfire receiver were started; Raven SSH timed out, so sender must be launched on Raven directly."}
{"ts": "2026-06-08T09:59:22+01:00", "type": "verification", "status": "blocked", "note": "CultMesh media bridge verification: Yggdrasil UFW was opened for 3075/udp on wg-gamecult and Starfire receiver now connects from 10.77.0.2. Synthetic sender on Yggdrasil connects and sends chunks without User Not Verified after CultLib pre-verification schema-v0 policy patch, but relay cache does not receive mimir.cultmesh_media_frame updates and Starfire receiver logs no frames. Remaining blocker is CultNet document put/apply path, not WireGuard routing or OBS endpoint."}
{"ts": "2026-06-09T02:17:25+01:00", "type": "decision", "status": "ok", "note": "Mimir program composition authority moved out of OBS: Muninn hosts publish local stream capabilities/media; Mimir owns selected-stream subscription, scene graph, Eve operator surfaces, and program publication intent; Yggdrasil-facing publisher daemon consumes Mimir output; OBS is compatibility-only. Added typed mimir.program_scene.v1, mimir.program_output.v1, and mimir.eve_operator_surface.v1 contracts and BufferSmoke proof coverage."}
{"ts": "2026-06-09T02:54:54+01:00", "type": "verification", "status": "ok", "note": "Imported the current OBS scene into Mimir-owned typed program state with Mimir.BufferSmoke --import-obs-program-scene. state/mimir-program-composition.cc now carries mimir.program_scene.v1, mimir.program_output.v1, and mimir.eve_operator_surface.v1 for eight current layers: Starfire monitor, Discord crop/chroma key, Raven monitor, Kiyo Pro, Mimir program texture, Muninn stream placeholder, SP browser, and hidden bigscreen."}

View file

@ -294,6 +294,11 @@ accepted_design:
- `src/Mimir.Runtime/Synchronization` now includes typed program contracts:
`mimir.program_scene.v1`, `mimir.program_output.v1`, and
`mimir.eve_operator_surface.v1`.
- `state/mimir-program-composition.cc` is the current imported Mimir program
state, generated from OBS scene JSON by `Mimir.BufferSmoke
--import-obs-program-scene`. It preserves the current Starfire monitor,
Discord crop/chroma key, Raven monitor, camera, browser, and Mimir texture
layers as Mimir-owned typed state.
- `src/Mimir.CultMeshMedia` owns the first explicit CultMesh media/body
bridge. Raven publishes muxed H.264/AAC MPEG-TS chunks as rolling
`mimir.cultmesh_media_frame` documents over CultNet reliable UDP,

Binary file not shown.