Upload CPU camera frames into Fensalir leases
This commit is contained in:
parent
9147fcd626
commit
7f3df7979b
9 changed files with 105 additions and 8 deletions
|
|
@ -757,17 +757,19 @@ static int RunFensalirTextureLeaseSmoke()
|
|||
}
|
||||
|
||||
var receiverDriver = new FakeTextureLeaseVideoDriver();
|
||||
var receiverSource = new MimirVideoCaptureDriverSource(
|
||||
new MimirStreamDescriptor("driver-camera", MimirStreamKind.Video, MimirStreamOrigin.LocalDevice),
|
||||
receiverDriver,
|
||||
static () => 0);
|
||||
using (var runtime = new MimirRuntime(
|
||||
new AquariumRuntimeOptions(Headless: true, CultCachePath: null),
|
||||
new MimirSynchronizationSettings(),
|
||||
[
|
||||
new MimirVideoCaptureDriverSource(
|
||||
new MimirStreamDescriptor("driver-camera", MimirStreamKind.Video, MimirStreamOrigin.LocalDevice),
|
||||
receiverDriver,
|
||||
static () => 0)
|
||||
]))
|
||||
[receiverSource]))
|
||||
{
|
||||
runtime.AttachServices(new AquariumRuntimeServices(broker));
|
||||
runtime.OnSceneReady();
|
||||
runtime.Update(0.016f, new InputState());
|
||||
_ = runtime.Frame;
|
||||
}
|
||||
|
||||
var buffer = new MimirRollingStreamBuffer(
|
||||
|
|
@ -790,10 +792,12 @@ static int RunFensalirTextureLeaseSmoke()
|
|||
var expectedResourceKey = MimirFensalirTextureLeaseClient.ResourceKeyForSource("kiyo-pro-rgb");
|
||||
|
||||
Console.WriteLine(
|
||||
$"fensalir-texture-lease-smoke leased={lease.IsValid} committed={broker.CommittedFenceValue > 0} driverClient={receiverDriver.HasClient} resources={frame.Resources.Count} claims={frame.Claims.Count} errors={validation.HasErrors}");
|
||||
$"fensalir-texture-lease-smoke leased={lease.IsValid} committed={broker.CommittedFenceValue > 0} driverClient={receiverDriver.HasClient} uploaded={broker.UploadedResourceKey} copies={receiverSource.LastUploadedCopyCount} resources={frame.Resources.Count} claims={frame.Claims.Count} errors={validation.HasErrors}");
|
||||
|
||||
return lease.IsValid &&
|
||||
receiverDriver.HasClient &&
|
||||
receiverSource.LastUploadedCopyCount == 1 &&
|
||||
broker.UploadedResourceKey == MimirFensalirTextureLeaseClient.ResourceKeyForSource("driver-camera") &&
|
||||
broker.CommittedResourceKey == expectedResourceKey &&
|
||||
broker.CommittedVersion == 0 &&
|
||||
broker.CommittedFenceValue == 41 &&
|
||||
|
|
@ -6049,6 +6053,8 @@ sealed class FakeFieldResourceBroker : IAquariumFieldResourceBroker
|
|||
|
||||
public ulong CommittedFenceValue { get; private set; }
|
||||
|
||||
public string UploadedResourceKey { get; private set; } = "";
|
||||
|
||||
public AquariumFieldResourceLease LeaseTexture2D(AquariumTexture2DLeaseRequest request)
|
||||
{
|
||||
var declaration = new AquariumFieldResourceDeclaration(
|
||||
|
|
@ -6082,6 +6088,12 @@ sealed class FakeFieldResourceBroker : IAquariumFieldResourceBroker
|
|||
CommittedFenceValue = producerFenceValue;
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool UploadTexture2D(AquariumTexture2DUpload upload)
|
||||
{
|
||||
UploadedResourceKey = upload.ResourceKey;
|
||||
return upload.IsValid;
|
||||
}
|
||||
}
|
||||
|
||||
sealed class FakeTextureLeaseVideoDriver : IMimirVideoCaptureDriver, IMimirFensalirTextureLeaseReceiver
|
||||
|
|
@ -6090,13 +6102,38 @@ sealed class FakeTextureLeaseVideoDriver : IMimirVideoCaptureDriver, IMimirFensa
|
|||
|
||||
public bool HasClient { get; private set; }
|
||||
|
||||
private MimirFensalirTextureLeaseClient? client;
|
||||
private bool emitted;
|
||||
|
||||
public void AttachTextureLeaseClient(MimirFensalirTextureLeaseClient? client)
|
||||
{
|
||||
this.client = client;
|
||||
HasClient = client != null;
|
||||
}
|
||||
|
||||
public bool TryCapture(out MimirVideoFrameDescriptor frame, out ReadOnlyMemory<byte> data)
|
||||
{
|
||||
if (!emitted &&
|
||||
client != null &&
|
||||
client.TryLeaseTexture2D(new MimirFensalirTextureLeaseRequest(
|
||||
"driver-camera",
|
||||
2,
|
||||
2,
|
||||
MimirVideoPixelFormat.Bgra8,
|
||||
8,
|
||||
DeviceTimestampNs: 2_000_000_000L,
|
||||
Version: 1), out var lease))
|
||||
{
|
||||
emitted = true;
|
||||
frame = lease.Frame;
|
||||
data = new byte[]
|
||||
{
|
||||
0, 0, 0, 255, 255, 255, 255, 255,
|
||||
255, 0, 0, 255, 0, 255, 0, 255,
|
||||
};
|
||||
return true;
|
||||
}
|
||||
|
||||
frame = default!;
|
||||
data = default;
|
||||
return false;
|
||||
|
|
|
|||
|
|
@ -68,6 +68,27 @@ public sealed class MimirFensalirTextureLeaseClient(IAquariumFieldResourceBroker
|
|||
public bool Commit(string resourceKey, ulong version, ulong producerFenceValue) =>
|
||||
broker.CommitLeaseVersion(resourceKey, version, producerFenceValue);
|
||||
|
||||
public bool UploadCpuFrame(MimirVideoFrameDescriptor frame, ReadOnlyMemory<byte> data)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(frame.ResourceKey) ||
|
||||
frame.Width <= 0 ||
|
||||
frame.Height <= 0 ||
|
||||
frame.StrideBytes <= 0 ||
|
||||
data.IsEmpty)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return broker.UploadTexture2D(new AquariumTexture2DUpload(
|
||||
frame.ResourceKey,
|
||||
frame.Width,
|
||||
frame.Height,
|
||||
FormatForPixelFormat(frame.PixelFormat),
|
||||
frame.StrideBytes,
|
||||
data,
|
||||
frame.ProducerFenceValue));
|
||||
}
|
||||
|
||||
public static string ResourceKeyForSource(string sourceId)
|
||||
{
|
||||
var normalized = sourceId.Trim().ToLowerInvariant().Replace('\\', '/');
|
||||
|
|
@ -78,7 +99,9 @@ public sealed class MimirFensalirTextureLeaseClient(IAquariumFieldResourceBroker
|
|||
pixelFormat switch
|
||||
{
|
||||
MimirVideoPixelFormat.Gray8 or MimirVideoPixelFormat.R8 => "R8Unorm",
|
||||
MimirVideoPixelFormat.Bayer8 => "R8Unorm",
|
||||
MimirVideoPixelFormat.Rg8 or MimirVideoPixelFormat.LeapStereoIr => "R8G8_UNorm",
|
||||
MimirVideoPixelFormat.Yuy2 => "YUY2",
|
||||
MimirVideoPixelFormat.Bgra8 => "Bgra8",
|
||||
MimirVideoPixelFormat.Nv12 => "Nv12",
|
||||
_ => "Rgba8Unorm",
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ public sealed class MimirVideoCaptureDriverSource : IMimirStreamSource, IMimirFe
|
|||
{
|
||||
private readonly IMimirVideoCaptureDriver driver;
|
||||
private readonly MimirNativeIngestStreamSource nativeSource;
|
||||
private MimirFensalirTextureLeaseClient? textureLeaseClient;
|
||||
|
||||
public MimirVideoCaptureDriverSource(
|
||||
MimirStreamDescriptor descriptor,
|
||||
|
|
@ -25,8 +26,11 @@ public sealed class MimirVideoCaptureDriverSource : IMimirStreamSource, IMimirFe
|
|||
|
||||
public Func<long> ReadArrivalNs { get; }
|
||||
|
||||
public int LastUploadedCopyCount { get; private set; }
|
||||
|
||||
public void AttachTextureLeaseClient(MimirFensalirTextureLeaseClient? client)
|
||||
{
|
||||
textureLeaseClient = client;
|
||||
if (driver is IMimirFensalirTextureLeaseReceiver receiver)
|
||||
{
|
||||
receiver.AttachTextureLeaseClient(client);
|
||||
|
|
@ -45,6 +49,16 @@ public sealed class MimirVideoCaptureDriverSource : IMimirStreamSource, IMimirFe
|
|||
return false;
|
||||
}
|
||||
|
||||
if (!data.IsEmpty && !string.IsNullOrWhiteSpace(frame.ResourceKey))
|
||||
{
|
||||
if (textureLeaseClient?.UploadCpuFrame(frame, data) == true)
|
||||
{
|
||||
frame = frame with { UnavoidableCopyCount = frame.UnavoidableCopyCount + 1 };
|
||||
LastUploadedCopyCount = frame.UnavoidableCopyCount;
|
||||
data = default;
|
||||
}
|
||||
}
|
||||
|
||||
nativeSource.PushVideoFrame(frame, ReadArrivalNs(), data);
|
||||
return nativeSource.TryRead(out sample);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,4 +24,5 @@ public sealed record MimirVideoFrameDescriptor(
|
|||
ulong NativeHandle = 0,
|
||||
string NativeHandleKind = "",
|
||||
string ResourceKey = "",
|
||||
ulong ProducerFenceValue = 0);
|
||||
ulong ProducerFenceValue = 0,
|
||||
int UnavoidableCopyCount = 0);
|
||||
|
|
|
|||
Loading…
Reference in a new issue