Add Raven Eve network ingest setup

This commit is contained in:
Meta Vens 2026-05-30 22:47:21 +01:00
commit 19ed000cd8
12 changed files with 609 additions and 6 deletions

View file

@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Globalization;
using System.Numerics;
using System.Security.Cryptography;
using System.Text.Json;
@ -169,6 +170,11 @@ if (args.Any(arg => string.Equals(arg, "--mf-gpu-driver-smoke", StringComparison
return RunMfGpuDriverSmoke(args);
}
if (args.Any(arg => string.Equals(arg, "--ffmpeg-rawvideo-source-smoke", StringComparison.OrdinalIgnoreCase)))
{
return RunFfmpegRawVideoSourceSmoke(args);
}
if (args.Any(arg => string.Equals(arg, "--mimir-spectrum-upload-smoke", StringComparison.OrdinalIgnoreCase)))
{
return RunMimirSpectrumUploadSmoke();
@ -1039,6 +1045,69 @@ static int RunMfGpuDriverSmoke(string[] args)
return 1;
}
static int RunFfmpegRawVideoSourceSmoke(string[] args)
{
var ffmpeg = ParseStringOption(args, "--ffmpeg", "ffmpeg");
var sourceId = ParseStringOption(args, "--source-id", "ffmpeg-rawvideo-smoke");
var width = ParseIntOption(args, "--width", 160);
var height = ParseIntOption(args, "--height", 90);
var fps = ParseIntOption(args, "--fps", 10);
var frames = ParseIntOption(args, "--frames", 3);
var timeoutMs = ParseIntOption(args, "--timeout-ms", 3000);
using var source = new MimirFfmpegRawVideoStreamSource(
new MimirStreamDescriptor(sourceId, MimirStreamKind.Video, MimirStreamOrigin.Network),
new MimirFfmpegRawVideoStreamSourceOptions(
ffmpeg,
[
"-hide_banner",
"-nostdin",
"-loglevel",
"error",
"-f",
"lavfi",
"-i",
$"testsrc2=size={width}x{height}:rate={fps}",
"-frames:v",
frames.ToString(CultureInfo.InvariantCulture),
"-f",
"rawvideo",
"-pix_fmt",
"bgra",
"-"
],
width,
height,
MimirVideoPixelFormat.Bgra8));
var deadline = Stopwatch.GetTimestamp() + (long)(timeoutMs / 1000.0 * Stopwatch.Frequency);
while (Stopwatch.GetTimestamp() < deadline)
{
if (!source.TryRead(out var sample))
{
Thread.Sleep(1);
continue;
}
var frame = sample.VideoFrame;
var expectedBytes = width * height * 4;
Console.WriteLine(
$"ffmpeg-rawvideo-source-smoke source={sample.SourceId} {frame?.Width}x{frame?.Height} format={frame?.PixelFormat} liveBytes={sample.ByteLength} payloadBytes={sample.Data.Length} stride={frame?.StrideBytes}");
return frame != null &&
frame.Width == width &&
frame.Height == height &&
frame.PixelFormat == MimirVideoPixelFormat.Bgra8 &&
frame.StrideBytes == width * 4 &&
sample.ByteLength == expectedBytes &&
sample.Data.Length == expectedBytes
? 0
: 1;
}
Console.WriteLine($"ffmpeg-rawvideo-source-smoke timeout source={sourceId} {width}x{height}@{fps}");
return 1;
}
static long StopwatchTicksToNs(long ticks)
{
return checked((long)(ticks * (1_000_000_000.0 / Stopwatch.Frequency)));

View file

@ -0,0 +1,183 @@
using System.Collections.Concurrent;
using System.Diagnostics;
namespace Mimir.Runtime.Synchronization;
public sealed record MimirFfmpegRawVideoStreamSourceOptions(
string Command,
IReadOnlyList<string> Arguments,
int Width,
int Height,
MimirVideoPixelFormat PixelFormat,
int FrameBytes = 0,
int StrideBytes = 0);
public sealed class MimirFfmpegRawVideoStreamSource : IMimirStreamSource
{
private readonly ConcurrentQueue<MimirStreamSample> samples = new();
private readonly CancellationTokenSource cancellation = new();
private readonly Process process;
private readonly Task stdoutTask;
private readonly Task stderrTask;
private readonly int frameBytes;
private readonly int strideBytes;
private ulong sequence;
private bool disposed;
public MimirFfmpegRawVideoStreamSource(
MimirStreamDescriptor descriptor,
MimirFfmpegRawVideoStreamSourceOptions options)
{
if (descriptor.Kind != MimirStreamKind.Video)
{
throw new ArgumentException("Raw FFmpeg video sources require a video descriptor.", nameof(descriptor));
}
if (options.Width <= 0 || options.Height <= 0)
{
throw new ArgumentOutOfRangeException(nameof(options), "Raw FFmpeg video sources require positive width and height.");
}
Descriptor = descriptor;
Options = options;
strideBytes = options.StrideBytes > 0 ? options.StrideBytes : InferStride(options.Width, options.PixelFormat);
frameBytes = options.FrameBytes > 0 ? options.FrameBytes : InferFrameBytes(options.Width, options.Height, options.PixelFormat, strideBytes);
if (strideBytes <= 0 || frameBytes <= 0)
{
throw new ArgumentOutOfRangeException(nameof(options), "Could not infer raw video frame size.");
}
process = StartProcess(options);
stdoutTask = Task.Run(() => ReadStdout(cancellation.Token));
stderrTask = Task.Run(() => DrainStderr(cancellation.Token));
}
public MimirStreamDescriptor Descriptor { get; }
public MimirFfmpegRawVideoStreamSourceOptions Options { get; }
public bool TryRead(out MimirStreamSample sample)
{
return samples.TryDequeue(out sample);
}
private static Process StartProcess(MimirFfmpegRawVideoStreamSourceOptions options)
{
var startInfo = new ProcessStartInfo(options.Command)
{
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
};
foreach (var argument in options.Arguments)
{
startInfo.ArgumentList.Add(argument);
}
return Process.Start(startInfo)
?? throw new InvalidOperationException($"Could not start FFmpeg raw video source process: {options.Command}");
}
private async Task ReadStdout(CancellationToken token)
{
var stream = process.StandardOutput.BaseStream;
while (!token.IsCancellationRequested)
{
var buffer = new byte[frameBytes];
var offset = 0;
while (offset < buffer.Length)
{
var read = await stream.ReadAsync(buffer.AsMemory(offset, buffer.Length - offset), token).ConfigureAwait(false);
if (read <= 0)
{
return;
}
offset += read;
}
var timestampNs = StopwatchTicksToNs(Stopwatch.GetTimestamp());
var videoFrame = new MimirVideoFrameDescriptor(
Options.Width,
Options.Height,
Options.PixelFormat,
strideBytes,
timestampNs);
samples.Enqueue(new MimirStreamSample(
Descriptor.SourceId,
Descriptor.Kind,
Descriptor.Origin,
timestampNs,
timestampNs,
sequence++,
0,
buffer.Length,
buffer,
videoFrame));
}
}
private async Task DrainStderr(CancellationToken token)
{
while (!token.IsCancellationRequested)
{
var line = await process.StandardError.ReadLineAsync(token).ConfigureAwait(false);
if (line == null)
{
break;
}
}
}
private static int InferStride(int width, MimirVideoPixelFormat pixelFormat)
{
return pixelFormat switch
{
MimirVideoPixelFormat.Bgra8 => checked(width * 4),
MimirVideoPixelFormat.Nv12 => width,
MimirVideoPixelFormat.R8 or MimirVideoPixelFormat.Gray8 or MimirVideoPixelFormat.Bayer8 => width,
MimirVideoPixelFormat.Rg8 or MimirVideoPixelFormat.Yuy2 or MimirVideoPixelFormat.LeapStereoIr => checked(width * 2),
_ => 0,
};
}
private static int InferFrameBytes(int width, int height, MimirVideoPixelFormat pixelFormat, int strideBytes)
{
return pixelFormat switch
{
MimirVideoPixelFormat.Nv12 => checked(width * height * 3 / 2),
_ => checked(strideBytes * height),
};
}
private static long StopwatchTicksToNs(long ticks)
{
return checked((long)(ticks * (1_000_000_000.0 / Stopwatch.Frequency)));
}
public void Dispose()
{
if (disposed)
{
return;
}
disposed = true;
cancellation.Cancel();
try
{
if (!process.HasExited)
{
process.Kill(entireProcessTree: true);
}
}
catch
{
}
process.Dispose();
cancellation.Dispose();
}
}

View file

@ -234,6 +234,26 @@ public sealed class MimirRuntimeConfiguration
: null)));
}
if (string.Equals(stream.Adapter, "ffmpeg-rawvideo", StringComparison.OrdinalIgnoreCase)
|| string.Equals(stream.Adapter, "rawvideo-process", StringComparison.OrdinalIgnoreCase))
{
if (string.IsNullOrWhiteSpace(stream.Command))
{
return null;
}
return new MimirStreamSourceFactory(descriptor, () => new MimirFfmpegRawVideoStreamSource(
descriptor,
new MimirFfmpegRawVideoStreamSourceOptions(
ResolveCommand(stream.Command, configDirectory),
stream.Arguments,
stream.Width,
stream.Height,
ParsePixelFormat(stream.PixelFormat),
stream.FrameBytes,
stream.StrideBytes)));
}
if (!string.Equals(stream.Adapter, "process", StringComparison.OrdinalIgnoreCase))
{
return null;
@ -282,6 +302,26 @@ public sealed class MimirRuntimeConfiguration
? origin
: throw new InvalidOperationException($"Unknown Mimir stream origin: {value}");
}
private static MimirVideoPixelFormat ParsePixelFormat(string value)
{
return string.IsNullOrWhiteSpace(value)
? MimirVideoPixelFormat.Unknown
: value.Trim().ToUpperInvariant() switch
{
"BAYER" or "BAYER8" => MimirVideoPixelFormat.Bayer8,
"GRAY8" or "Y8" => MimirVideoPixelFormat.Gray8,
"R8" => MimirVideoPixelFormat.R8,
"RG8" => MimirVideoPixelFormat.Rg8,
"YUY2" => MimirVideoPixelFormat.Yuy2,
"MJPG" or "MJPEG" => MimirVideoPixelFormat.Mjpg,
"H264" => MimirVideoPixelFormat.H264,
"NV12" => MimirVideoPixelFormat.Nv12,
"BGRA8" or "BGRA" => MimirVideoPixelFormat.Bgra8,
"LEAP_STEREO_IR" or "LEAPSTEREOIR" => MimirVideoPixelFormat.LeapStereoIr,
_ => MimirVideoPixelFormat.Unknown,
};
}
}
public sealed record MimirStreamSourceFactory(
@ -394,6 +434,10 @@ public sealed class MimirStreamConfig
public string PixelFormat { get; set; } = "";
public int FrameBytes { get; set; }
public int StrideBytes { get; set; }
public string InputFormat { get; set; } = "";
public string OutputFormat { get; set; } = "Nv12";