Decode LocalCast native render descriptors
This commit is contained in:
parent
9eb40cf6c4
commit
cfb48d796c
2 changed files with 167 additions and 0 deletions
|
|
@ -1,9 +1,79 @@
|
|||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Aquarium.LocalCast;
|
||||
|
||||
public delegate bool LocalCastNativeRenderPayloadDecoder(
|
||||
LocalCastNativeSampleHandle sample,
|
||||
out LocalCastVisualFrame frame);
|
||||
|
||||
public delegate bool LocalCastNativeRenderPointBufferDecoder(
|
||||
in LocalCastNativeRenderPacketDescriptor descriptor,
|
||||
LocalCastNativeSampleHandle sample,
|
||||
out IReadOnlyList<LocalCastVisualPoint> points);
|
||||
|
||||
public sealed class LocalCastNativeRenderDescriptorDecoder
|
||||
{
|
||||
private readonly LocalCastNativeRenderPointBufferDecoder decodePointBuffer;
|
||||
private readonly string spoutSenderName;
|
||||
|
||||
public LocalCastNativeRenderDescriptorDecoder(
|
||||
LocalCastNativeRenderPointBufferDecoder decodePointBuffer,
|
||||
string spoutSenderName = "LocalCastBridge Point Cloud")
|
||||
{
|
||||
this.decodePointBuffer = decodePointBuffer;
|
||||
this.spoutSenderName = spoutSenderName;
|
||||
}
|
||||
|
||||
public bool TryDecode(LocalCastNativeSampleHandle sample, out LocalCastVisualFrame frame)
|
||||
{
|
||||
frame = EmptyFrame();
|
||||
if (!sample.IsLive || sample.PayloadHandle == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var descriptor = Marshal.PtrToStructure<LocalCastNativeRenderPacketDescriptor>((IntPtr)sample.PayloadHandle);
|
||||
if (!decodePointBuffer(in descriptor, sample, out var points))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
frame = new LocalCastVisualFrame
|
||||
{
|
||||
SchemaVersion = LocalCastVisualStateReader.RenderFrameSchemaId,
|
||||
FrameId = checked((long)sample.Sequence),
|
||||
CreatedMonotonicNs = checked((long)sample.ArrivalNs),
|
||||
SourceTimeMinNs = checked((long)descriptor.SourceTimeMinNs),
|
||||
SourceTimeMaxNs = checked((long)descriptor.SourceTimeMaxNs),
|
||||
PresentTimeNs = checked((long)descriptor.PresentTimeNs),
|
||||
AudioAlignmentTimeNs = checked((long)descriptor.AudioAlignmentTimeNs),
|
||||
SpoutSenderName = spoutSenderName,
|
||||
TargetWidth = checked((int)descriptor.TargetWidth),
|
||||
TargetHeight = checked((int)descriptor.TargetHeight),
|
||||
Points = points,
|
||||
};
|
||||
return true;
|
||||
}
|
||||
|
||||
private static LocalCastVisualFrame EmptyFrame()
|
||||
{
|
||||
return new LocalCastVisualFrame
|
||||
{
|
||||
SchemaVersion = LocalCastVisualStateReader.RenderFrameSchemaId,
|
||||
FrameId = -1,
|
||||
CreatedMonotonicNs = 0,
|
||||
SourceTimeMinNs = 0,
|
||||
SourceTimeMaxNs = 0,
|
||||
PresentTimeNs = 0,
|
||||
AudioAlignmentTimeNs = 0,
|
||||
SpoutSenderName = string.Empty,
|
||||
TargetWidth = 0,
|
||||
TargetHeight = 0,
|
||||
Points = [],
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class LocalCastNativeVisualFrameSource : ILocalCastVisualFrameSource
|
||||
{
|
||||
private readonly ILocalCastNativeRuntime runtime;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using System.Numerics;
|
||||
using System.Runtime.InteropServices;
|
||||
using Aquarium.LocalCast;
|
||||
|
||||
namespace Aquarium.LocalCast.Tests;
|
||||
|
|
@ -64,6 +65,102 @@ public sealed class LocalCastNativeVisualFrameSourceTests
|
|||
Assert.False(empty.TryReadLatest(out _));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DescriptorDecoderReadsTimingAndDelegatesPointBuffer()
|
||||
{
|
||||
var descriptor = new LocalCastNativeRenderPacketDescriptor
|
||||
{
|
||||
PointBufferHandle = 500,
|
||||
PointCount = 1,
|
||||
PointStrideBytes = 48,
|
||||
TargetWidth = 1280,
|
||||
TargetHeight = 720,
|
||||
SourceTimeMinNs = 900,
|
||||
SourceTimeMaxNs = 1_000,
|
||||
PresentTimeNs = 1_050,
|
||||
AudioAlignmentTimeNs = 1_025,
|
||||
MetadataHandle = 77,
|
||||
};
|
||||
var pointer = Marshal.AllocHGlobal(Marshal.SizeOf<LocalCastNativeRenderPacketDescriptor>());
|
||||
try
|
||||
{
|
||||
Marshal.StructureToPtr(descriptor, pointer, false);
|
||||
var sample = new LocalCastNativeSampleHandle
|
||||
{
|
||||
SensorIdHash = 11,
|
||||
TimestampNs = 1_000,
|
||||
ArrivalNs = 1_010,
|
||||
Sequence = 42,
|
||||
PayloadHandle = (ulong)pointer,
|
||||
Flags = LocalCastNativeSampleFlags.None,
|
||||
};
|
||||
var decoder = new LocalCastNativeRenderDescriptorDecoder(
|
||||
(in LocalCastNativeRenderPacketDescriptor read, LocalCastNativeSampleHandle _, out IReadOnlyList<LocalCastVisualPoint> points) =>
|
||||
{
|
||||
Assert.Equal(500u, read.PointBufferHandle);
|
||||
Assert.Equal(1u, read.PointCount);
|
||||
points =
|
||||
[
|
||||
new LocalCastVisualPoint(
|
||||
"native:decoded",
|
||||
new Vector3(0.2f, 0.1f, 1.0f),
|
||||
0.04f,
|
||||
new Vector4(0.5f, 0.4f, 0.3f, 0.9f),
|
||||
0.7f,
|
||||
1_000),
|
||||
];
|
||||
return true;
|
||||
});
|
||||
|
||||
Assert.True(decoder.TryDecode(sample, out var frame));
|
||||
Assert.Equal(42, frame.FrameId);
|
||||
Assert.Equal(1_010, frame.CreatedMonotonicNs);
|
||||
Assert.Equal(900, frame.SourceTimeMinNs);
|
||||
Assert.Equal(1_000, frame.SourceTimeMaxNs);
|
||||
Assert.Equal(1_050, frame.PresentTimeNs);
|
||||
Assert.Equal(1_025, frame.AudioAlignmentTimeNs);
|
||||
Assert.Equal(1280, frame.TargetWidth);
|
||||
Assert.Equal(720, frame.TargetHeight);
|
||||
Assert.Single(frame.Points);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Marshal.FreeHGlobal(pointer);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DescriptorDecoderRejectsNullPayloadAndPointDecoderFailure()
|
||||
{
|
||||
var decoder = new LocalCastNativeRenderDescriptorDecoder(
|
||||
(in LocalCastNativeRenderPacketDescriptor _, LocalCastNativeSampleHandle _, out IReadOnlyList<LocalCastVisualPoint> points) =>
|
||||
{
|
||||
points = [];
|
||||
return false;
|
||||
});
|
||||
|
||||
Assert.False(decoder.TryDecode(new LocalCastNativeSampleHandle { Flags = LocalCastNativeSampleFlags.None }, out _));
|
||||
|
||||
var descriptor = new LocalCastNativeRenderPacketDescriptor();
|
||||
var pointer = Marshal.AllocHGlobal(Marshal.SizeOf<LocalCastNativeRenderPacketDescriptor>());
|
||||
try
|
||||
{
|
||||
Marshal.StructureToPtr(descriptor, pointer, false);
|
||||
Assert.False(
|
||||
decoder.TryDecode(
|
||||
new LocalCastNativeSampleHandle
|
||||
{
|
||||
PayloadHandle = (ulong)pointer,
|
||||
Flags = LocalCastNativeSampleFlags.None,
|
||||
},
|
||||
out _));
|
||||
}
|
||||
finally
|
||||
{
|
||||
Marshal.FreeHGlobal(pointer);
|
||||
}
|
||||
}
|
||||
|
||||
private static LocalCastVisualFrame FrameFromPayload(ulong payloadHandle, ulong timestampNs)
|
||||
{
|
||||
return new LocalCastVisualFrame
|
||||
|
|
|
|||
Loading…
Reference in a new issue