Stabilize ASIO readiness in Well frames
This commit is contained in:
parent
adbaa3342b
commit
3df9ae0768
5 changed files with 76 additions and 10 deletions
|
|
@ -108,6 +108,8 @@ public sealed class MimirAsioStreamSource : IMimirMultiplexedStreamSource
|
|||
|
||||
started.Set();
|
||||
var sampleBuffer = new float[Math.Max(1, maxFrames)];
|
||||
long firstDeviceTimestampNs = 0;
|
||||
long firstCanonicalEndNs = 0;
|
||||
while (!cancellation.IsCancellationRequested)
|
||||
{
|
||||
if (!Native.Read(
|
||||
|
|
@ -131,12 +133,23 @@ public sealed class MimirAsioStreamSource : IMimirMultiplexedStreamSource
|
|||
var sourceId = channel < sourceIds.Length ? sourceIds[channel] : $"asio-ch{channel}";
|
||||
var bytes = new byte[frameCount * sizeof(float)];
|
||||
var arrivalNs = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() * 1_000_000L;
|
||||
var durationNs = checked((long)Math.Ceiling(frameCount * 1_000_000_000.0 / Math.Max(1, sampleRate)));
|
||||
if (timestampNs > 0 && firstDeviceTimestampNs == 0)
|
||||
{
|
||||
firstDeviceTimestampNs = timestampNs;
|
||||
firstCanonicalEndNs = arrivalNs;
|
||||
}
|
||||
|
||||
var endNs = timestampNs > 0 && firstDeviceTimestampNs > 0
|
||||
? checked(firstCanonicalEndNs + (timestampNs - firstDeviceTimestampNs))
|
||||
: arrivalNs;
|
||||
var startNs = Math.Max(0, endNs - durationNs);
|
||||
MemoryMarshal.AsBytes(sampleBuffer.AsSpan(0, frameCount)).CopyTo(bytes);
|
||||
samples.Enqueue(new MimirStreamSample(
|
||||
sourceId,
|
||||
MimirStreamKind.Audio,
|
||||
Descriptor.Origin,
|
||||
arrivalNs,
|
||||
startNs,
|
||||
arrivalNs,
|
||||
sequence,
|
||||
0,
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ namespace Mimir.Runtime.Synchronization;
|
|||
public sealed class MimirCanonicalClockMapper
|
||||
{
|
||||
private const long MaxArrivalDriftBeforeRebaseNs = 10_000_000_000L;
|
||||
private const long LocalEpochTimestampNs = 60_000_000_000L;
|
||||
private readonly Dictionary<string, ClockMap> maps = new(StringComparer.Ordinal);
|
||||
|
||||
public IReadOnlyList<MimirCanonicalClockMapSnapshot> Snapshots =>
|
||||
|
|
@ -30,13 +31,17 @@ public sealed class MimirCanonicalClockMapper
|
|||
var key = $"{sample.Kind}:{sample.Origin}:{sample.SourceId}";
|
||||
if (!maps.TryGetValue(key, out var map))
|
||||
{
|
||||
map = new ClockMap(checked(arrivalNs - sourceTimestampNs), sourceTimestampNs, arrivalNs);
|
||||
var initialOffsetNs = LooksLikeLocalEpoch(sourceTimestampNs, arrivalNs)
|
||||
? 0L
|
||||
: checked(arrivalNs - sourceTimestampNs);
|
||||
map = new ClockMap(initialOffsetNs, sourceTimestampNs, arrivalNs);
|
||||
maps.Add(key, map);
|
||||
}
|
||||
else
|
||||
{
|
||||
var projectedArrivalNs = checked(sourceTimestampNs + map.OffsetNs);
|
||||
if (Math.Abs(projectedArrivalNs - arrivalNs) > MaxArrivalDriftBeforeRebaseNs)
|
||||
if (!LooksLikeLocalEpoch(sourceTimestampNs, arrivalNs) &&
|
||||
Math.Abs(projectedArrivalNs - arrivalNs) > MaxArrivalDriftBeforeRebaseNs)
|
||||
{
|
||||
map.OffsetNs = checked(arrivalNs - sourceTimestampNs);
|
||||
}
|
||||
|
|
@ -55,6 +60,11 @@ public sealed class MimirCanonicalClockMapper
|
|||
|
||||
private static long NowNs() => DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() * 1_000_000L;
|
||||
|
||||
private static bool LooksLikeLocalEpoch(long sourceTimestampNs, long arrivalNs) =>
|
||||
sourceTimestampNs > 0 &&
|
||||
arrivalNs > 0 &&
|
||||
Math.Abs(sourceTimestampNs - arrivalNs) <= LocalEpochTimestampNs;
|
||||
|
||||
private sealed class ClockMap(long offsetNs, long firstSourceTimestampNs, long firstArrivalNs)
|
||||
{
|
||||
public long OffsetNs { get; set; } = offsetNs;
|
||||
|
|
|
|||
|
|
@ -56,6 +56,9 @@ public sealed record MimirSynchronizedBufferFrame(
|
|||
|
||||
public sealed class MimirSynchronizedBufferPlanner
|
||||
{
|
||||
private const long MaxInferredAudioGapNs = 250_000_000L;
|
||||
private const long ReadyToleranceNs = 40_000_000L;
|
||||
|
||||
public static IReadOnlyList<MimirSourceTimingCorrection> CorrectionsFromAudioStates(
|
||||
IEnumerable<MimirAudioSynchronizationState> states,
|
||||
IEnumerable<MimirRollingStreamBuffer>? buffers = null)
|
||||
|
|
@ -165,6 +168,11 @@ public sealed class MimirSynchronizedBufferPlanner
|
|||
var latest = buffer.Latest!.Value;
|
||||
return SampleCanonicalEndNs(latest, CorrectionFor(corrections, buffer.Descriptor));
|
||||
});
|
||||
if (windowEndNs < windowStartNs)
|
||||
{
|
||||
windowStartNs = windowEndNs;
|
||||
}
|
||||
|
||||
var delayNs = checked((long)Math.Round(Math.Max(0.0, presentationDelay.TotalSeconds) * 1_000_000_000.0));
|
||||
var presentationTimeNs = Math.Clamp(windowEndNs - delayNs, windowStartNs, windowEndNs);
|
||||
|
||||
|
|
@ -236,8 +244,11 @@ public sealed class MimirSynchronizedBufferPlanner
|
|||
long presentationTimeNs,
|
||||
MimirSourceTimingCorrection correction)
|
||||
{
|
||||
var samples = buffer.Snapshot();
|
||||
if (samples.Count == 0)
|
||||
var samples = buffer.Snapshot()
|
||||
.OrderBy(static sample => sample.TimestampNs)
|
||||
.ThenBy(static sample => sample.Sequence)
|
||||
.ToArray();
|
||||
if (samples.Length == 0)
|
||||
{
|
||||
return new MimirSynchronizedStreamSlice(
|
||||
buffer.Descriptor.SourceId,
|
||||
|
|
@ -259,14 +270,14 @@ public sealed class MimirSynchronizedBufferPlanner
|
|||
var bestIndex = -1;
|
||||
MimirSynchronizedSliceStatus status = MimirSynchronizedSliceStatus.Missing;
|
||||
long bestDistance = long.MaxValue;
|
||||
for (var index = 0; index < samples.Count; index++)
|
||||
for (var index = 0; index < samples.Length; index++)
|
||||
{
|
||||
var sample = samples[index];
|
||||
var previous = index > 0 ? samples[index - 1] : (MimirStreamSample?)null;
|
||||
var next = index < samples.Count - 1 ? samples[index + 1] : (MimirStreamSample?)null;
|
||||
var next = index < samples.Length - 1 ? samples[index + 1] : (MimirStreamSample?)null;
|
||||
var startNs = correction.ToCanonicalNs(sample.TimestampNs);
|
||||
var endNs = SampleCanonicalEndNs(sample, correction, previous, next);
|
||||
if (presentationTimeNs >= startNs && presentationTimeNs < endNs)
|
||||
if (presentationTimeNs >= startNs && presentationTimeNs <= endNs)
|
||||
{
|
||||
best = sample;
|
||||
bestIndex = index;
|
||||
|
|
@ -289,10 +300,15 @@ public sealed class MimirSynchronizedBufferPlanner
|
|||
}
|
||||
}
|
||||
|
||||
if (status != MimirSynchronizedSliceStatus.Missing && bestDistance <= ReadyToleranceNs)
|
||||
{
|
||||
status = MimirSynchronizedSliceStatus.Ready;
|
||||
}
|
||||
|
||||
var selected = best!.Value;
|
||||
var selectedStartNs = correction.ToCanonicalNs(selected.TimestampNs);
|
||||
var selectedPrevious = bestIndex > 0 ? samples[bestIndex - 1] : (MimirStreamSample?)null;
|
||||
var selectedNext = bestIndex >= 0 && bestIndex < samples.Count - 1
|
||||
var selectedNext = bestIndex >= 0 && bestIndex < samples.Length - 1
|
||||
? samples[bestIndex + 1]
|
||||
: (MimirStreamSample?)null;
|
||||
var selectedEndNs = SampleCanonicalEndNs(selected, correction, selectedPrevious, selectedNext);
|
||||
|
|
@ -330,7 +346,26 @@ public sealed class MimirSynchronizedBufferPlanner
|
|||
{
|
||||
if (sample.AudioBlock is { SampleRate: > 0, FrameCount: > 0 } block)
|
||||
{
|
||||
return checked((long)Math.Ceiling(block.FrameCount * 1_000_000_000.0 / block.SampleRate));
|
||||
var blockDurationNs = checked((long)Math.Ceiling(block.FrameCount * 1_000_000_000.0 / block.SampleRate));
|
||||
if (nextSample.HasValue && nextSample.Value.TimestampNs > sample.TimestampNs)
|
||||
{
|
||||
var nextGapNs = nextSample.Value.TimestampNs - sample.TimestampNs;
|
||||
if (nextGapNs <= MaxInferredAudioGapNs)
|
||||
{
|
||||
return Math.Max(blockDurationNs, nextGapNs);
|
||||
}
|
||||
}
|
||||
|
||||
if (previousSample.HasValue && sample.TimestampNs > previousSample.Value.TimestampNs)
|
||||
{
|
||||
var previousGapNs = sample.TimestampNs - previousSample.Value.TimestampNs;
|
||||
if (previousGapNs <= MaxInferredAudioGapNs)
|
||||
{
|
||||
return Math.Max(blockDurationNs, previousGapNs);
|
||||
}
|
||||
}
|
||||
|
||||
return blockDurationNs;
|
||||
}
|
||||
|
||||
if (sample.VideoFrame is not null)
|
||||
|
|
|
|||
|
|
@ -277,6 +277,8 @@ internal sealed class MimirWellLatencyController
|
|||
? 0.0
|
||||
: hub.AudioSynchronizationStates.Average(static state => Math.Clamp(state.Confidence, 0.0, 1.0));
|
||||
var previousReady = lastFrame?.Frame.Slices.Count(static slice => slice.Status == MimirSynchronizedSliceStatus.Ready) ?? 0;
|
||||
var previousFuture = lastFrame?.Frame.Slices.Count(static slice => slice.Status == MimirSynchronizedSliceStatus.FutureSampleOnly) ?? 0;
|
||||
var previousMissing = lastFrame?.Frame.Slices.Count(static slice => slice.Status == MimirSynchronizedSliceStatus.Missing) ?? 0;
|
||||
var previousTotal = lastFrame?.Frame.Slices.Count ?? 0;
|
||||
var readiness = previousTotal == 0 ? 0.0 : previousReady / (double)previousTotal;
|
||||
|
||||
|
|
@ -305,6 +307,11 @@ internal sealed class MimirWellLatencyController
|
|||
target += TimeSpan.FromMilliseconds(options.LatencyStepMs * Math.Max(1, degradedStreak));
|
||||
reason = "backoff-after-incomplete-frame";
|
||||
}
|
||||
else if (previousFuture > 0 && previousMissing == 0)
|
||||
{
|
||||
target -= TimeSpan.FromMilliseconds(options.LatencyStepMs * Math.Max(1, completeStreak / Math.Max(1, options.LatencyConvergenceFrames)));
|
||||
reason = "chase-future-samples";
|
||||
}
|
||||
else if (completeStreak >= Math.Max(1, options.LatencyConvergenceFrames) && readiness >= options.LatencyReadinessTarget)
|
||||
{
|
||||
target -= TimeSpan.FromMilliseconds(options.LatencyStepMs);
|
||||
|
|
|
|||
|
|
@ -366,3 +366,4 @@
|
|||
{"ts": "2026-06-02T18:23:46+01:00", "type": "implementation", "status": "ok", "note": "2026-06-02: Added mimir.fensalir_reservoir_pressure.v1. Fensalir daemon now persists pressure cause metrics alongside worker state: Well poll/publish ms, capture bytes, ready-slice ratio, timing confidence/distance, GPU queue ms, and missing-body/fence-unavailable/timing-degraded drop counters. Fixture on port 8803 completed one stream-frame job and health advertised worker + pressure documents."}
|
||||
{"ts": "2026-06-02T19:17:54+01:00", "type": "decision", "status": "ok", "note": "Online Verse shakedown: canonical timestamps are owned by MimirCanonicalClockMapper, which now rebases streams whose projected source clock drifts more than 10s from arrival after ASIO/device resets; MimirSynchronizationHub no longer reapplies raw clock-domain corrections to already-canonical buffers. Fensalir Eve surface now mirrors sibling daemon PID liveness so Odin can lower relay, recorder, Well, Move sync, and Fensalir statuses without taking control authority."}
|
||||
{"ts": "2026-06-02T23:49:45+01:00", "type": "decision", "status": "ok", "note": "Mimir Well/Fensalir live repair: rolling-buffer authority now uses canonical arrival UTC timestamps for ASIO/native camera samples while preserving device timestamps as calibration evidence; Fensalir CultCache persistence is best-effort so a Windows File.Replace sharing failure cannot kill the live Eve daemon."}
|
||||
{"ts": "2026-06-03T04:13:02+01:00", "type": "implementation", "status": "ok", "note": "2026-06-03: ASIO readiness repair: Mimir now derives ASIO block times from native device timestamps, preserves local-epoch timestamps in the canonical mapper, sorts planner samples chronologically, keeps frames visible when overlap pinches, and lets the latency controller chase FutureSampleOnly slices downward. Live Well reached repeated 6/6 Ready frames at roughly 75-80 ms presentation delay with ASIO distances reported around 32-36 ms."}
|
||||
|
|
|
|||
Loading…
Reference in a new issue