275 lines
13 KiB
C++
275 lines
13 KiB
C++
#ifndef NUI_CORE_CONTRACT_H
|
|
#define NUI_CORE_CONTRACT_H
|
|
|
|
// Frozen, versioned IPC data contract (plan todo 2).
|
|
//
|
|
// This header is the single source of truth for the shared-memory frame layout
|
|
// and the control-plane message schema shared between the tracker (producer),
|
|
// the synth/protocol emitter (consumer), and the Python mirror in
|
|
// tracker/contract.py. It is frozen from this commit: any field-shape change is
|
|
// an explicit CONTRACT_VERSION bump, never a silent edit, because the Python
|
|
// mirror does not re-derive this layout - it hardcodes the same golden values
|
|
// and a test cross-checks them.
|
|
//
|
|
// Packing: every struct in this header is declared under
|
|
// `#pragma pack(push, 1)` so there is no compiler- or platform-dependent
|
|
// implicit padding; every field is placed at a fixed, documented byte offset.
|
|
// This trades natural alignment for cross-language, cross-compiler layout
|
|
// determinism, which this IPC boundary needs more than it needs aligned
|
|
// access (frames are copied, not hot-looped over field-by-field).
|
|
//
|
|
// Transport model (see .omo/design/architecture.md "Transport"): the frame
|
|
// side is a lock-free, single-writer / multi-reader triple buffer with a
|
|
// monotonic sequence counter (seqlock-style: writer always writes one slot
|
|
// ahead of the last published slot, then bumps `sequence`; a reader snapshots
|
|
// `sequence`, reads the indicated slot, and retries if `sequence` moved
|
|
// during the read). The control side is a named-pipe/socket message schema
|
|
// carrying ONLY handshake/config/calibration/heartbeat traffic - never
|
|
// per-frame data.
|
|
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
|
|
namespace nui::contract {
|
|
|
|
// Contract schema version. Bump only via an explicit contract-version commit;
|
|
// every golden size/offset constant below is pinned to this version's layout.
|
|
constexpr uint32_t kContractVersion = 1;
|
|
|
|
int contract_version();
|
|
|
|
// Number of Kinect v1 skeleton joints (NUI_SKELETON_POSITION_COUNT).
|
|
constexpr std::size_t kJointCount = 20;
|
|
|
|
// Number of frame slots in the triple buffer.
|
|
constexpr std::size_t kFrameSlotCount = 3;
|
|
|
|
#pragma pack(push, 1)
|
|
|
|
// --- Skeleton -------------------------------------------------------------
|
|
|
|
// A homogeneous 3D point, matching Kinect's NUI Vector4 (x, y, z, w=1.0).
|
|
struct Vector4 {
|
|
float x;
|
|
float y;
|
|
float z;
|
|
float w;
|
|
};
|
|
static_assert(sizeof(Vector4) == 16, "Vector4 golden size");
|
|
static_assert(offsetof(Vector4, x) == 0, "Vector4.x golden offset");
|
|
static_assert(offsetof(Vector4, y) == 4, "Vector4.y golden offset");
|
|
static_assert(offsetof(Vector4, z) == 8, "Vector4.z golden offset");
|
|
static_assert(offsetof(Vector4, w) == 12, "Vector4.w golden offset");
|
|
|
|
// Per-joint tracking state (NUI_SKELETON_POSITION_TRACKING_STATE), stored as a
|
|
// fixed-width uint32 field so the layout is identical in the C struct and the
|
|
// ctypes mirror.
|
|
enum class JointTrackingState : uint32_t {
|
|
NOT_TRACKED = 0,
|
|
INFERRED = 1,
|
|
TRACKED = 2,
|
|
};
|
|
|
|
// One joint's position + per-joint tracking state (nui-contracts.md §1.7).
|
|
struct JointData {
|
|
Vector4 position;
|
|
JointTrackingState tracking_state;
|
|
};
|
|
static_assert(sizeof(JointData) == 20, "JointData golden size");
|
|
static_assert(offsetof(JointData, position) == 0, "JointData.position golden offset");
|
|
static_assert(offsetof(JointData, tracking_state) == 16, "JointData.tracking_state golden offset");
|
|
|
|
// A full skeleton frame: 20 joints, an overall tracking id, and quality flags
|
|
// (NUI_SKELETON_DATA, minus bone orientations which the app derives itself -
|
|
// see nui-contracts.md §1.7).
|
|
struct SkeletonFrame {
|
|
JointData joints[kJointCount];
|
|
uint32_t skeleton_tracking_id; // dwTrackingID: stable nonzero id per player
|
|
uint32_t quality_flags; // dwQualityFlags: e.g. CLIPPED_* bits
|
|
};
|
|
static_assert(sizeof(SkeletonFrame) == 408, "SkeletonFrame golden size");
|
|
static_assert(offsetof(SkeletonFrame, joints) == 0, "SkeletonFrame.joints golden offset");
|
|
static_assert(offsetof(SkeletonFrame, skeleton_tracking_id) == 400,
|
|
"SkeletonFrame.skeleton_tracking_id golden offset");
|
|
static_assert(offsetof(SkeletonFrame, quality_flags) == 404,
|
|
"SkeletonFrame.quality_flags golden offset");
|
|
|
|
// --- Image buffer descriptors ----------------------------------------------
|
|
|
|
// Describes a depth+player-index buffer living in the shared-memory region:
|
|
// its geometry and a byte offset to its pixel data (nui-contracts.md §2.2).
|
|
// Pixel format is the packed 16-bit Kinect word: (mm & 0x1FFF) << 3 | (player & 0x7).
|
|
struct DepthBufferDescriptor {
|
|
uint32_t width;
|
|
uint32_t height;
|
|
uint32_t pitch; // bytes per row, as reported to the host (NUI_SURFACE_DESC)
|
|
uint64_t byte_offset; // offset of pixel data within the shared-memory region
|
|
};
|
|
static_assert(sizeof(DepthBufferDescriptor) == 20, "DepthBufferDescriptor golden size");
|
|
static_assert(offsetof(DepthBufferDescriptor, width) == 0, "DepthBufferDescriptor.width golden offset");
|
|
static_assert(offsetof(DepthBufferDescriptor, height) == 4, "DepthBufferDescriptor.height golden offset");
|
|
static_assert(offsetof(DepthBufferDescriptor, pitch) == 8, "DepthBufferDescriptor.pitch golden offset");
|
|
static_assert(offsetof(DepthBufferDescriptor, byte_offset) == 12,
|
|
"DepthBufferDescriptor.byte_offset golden offset");
|
|
|
|
// Describes a color (BGRA) buffer living in the shared-memory region.
|
|
struct ColorBufferDescriptor {
|
|
uint32_t width;
|
|
uint32_t height;
|
|
uint32_t pitch;
|
|
uint64_t byte_offset;
|
|
};
|
|
static_assert(sizeof(ColorBufferDescriptor) == 20, "ColorBufferDescriptor golden size");
|
|
static_assert(offsetof(ColorBufferDescriptor, width) == 0, "ColorBufferDescriptor.width golden offset");
|
|
static_assert(offsetof(ColorBufferDescriptor, height) == 4, "ColorBufferDescriptor.height golden offset");
|
|
static_assert(offsetof(ColorBufferDescriptor, pitch) == 8, "ColorBufferDescriptor.pitch golden offset");
|
|
static_assert(offsetof(ColorBufferDescriptor, byte_offset) == 12,
|
|
"ColorBufferDescriptor.byte_offset golden offset");
|
|
|
|
// --- Frame ------------------------------------------------------------------
|
|
|
|
// One published frame: frame number + QPC-style monotonic timestamp (micros
|
|
// since init, nui-contracts.md §3.6), the skeleton, and the two image buffer
|
|
// descriptors. Depth, color, and skeleton all carry the SAME frame_number and
|
|
// timestamp_micros - the most recent depth tick's stamp - matching the real
|
|
// NUI contract where skeleton timestamp = the depth frame it derived from.
|
|
struct FrameData {
|
|
uint32_t frame_number;
|
|
int64_t timestamp_micros; // monotonic, QPC-style, microseconds since init
|
|
SkeletonFrame skeleton;
|
|
DepthBufferDescriptor depth;
|
|
ColorBufferDescriptor color;
|
|
};
|
|
static_assert(sizeof(FrameData) == 460, "FrameData golden size");
|
|
static_assert(offsetof(FrameData, frame_number) == 0, "FrameData.frame_number golden offset");
|
|
static_assert(offsetof(FrameData, timestamp_micros) == 4, "FrameData.timestamp_micros golden offset");
|
|
static_assert(offsetof(FrameData, skeleton) == 12, "FrameData.skeleton golden offset");
|
|
static_assert(offsetof(FrameData, depth) == 420, "FrameData.depth golden offset");
|
|
static_assert(offsetof(FrameData, color) == 440, "FrameData.color golden offset");
|
|
|
|
// One triple-buffer slot: a sequence counter guarding `data`. Each slot owns
|
|
// its generation: odd means its producer is writing and even means stable.
|
|
// A reader accepts only an unchanged even value before and after its copy;
|
|
// it must not derive this value from published_sequence because slots wrap.
|
|
struct FrameSlot {
|
|
uint64_t sequence;
|
|
FrameData data;
|
|
};
|
|
static_assert(sizeof(FrameSlot) == 468, "FrameSlot golden size");
|
|
static_assert(offsetof(FrameSlot, sequence) == 0, "FrameSlot.sequence golden offset");
|
|
static_assert(offsetof(FrameSlot, data) == 8, "FrameSlot.data golden offset");
|
|
|
|
// The full frame contract: a triple-buffered, newest-wins frame region plus
|
|
// its version stamp. A single producer publishes by writing the slot ahead
|
|
// of the currently-published one and then bumping `published_sequence`;
|
|
// readers derive the current slot index as `published_sequence % kFrameSlotCount`.
|
|
struct FrameContract {
|
|
uint32_t contract_version;
|
|
uint32_t reserved; // explicit padding to keep 8-byte fields aligned-by-convention
|
|
uint64_t published_sequence; // monotonic; slots[published_sequence % kFrameSlotCount] is newest
|
|
FrameSlot slots[kFrameSlotCount];
|
|
};
|
|
static_assert(sizeof(FrameContract) == 1420, "FrameContract golden size");
|
|
static_assert(offsetof(FrameContract, contract_version) == 0, "FrameContract.contract_version golden offset");
|
|
static_assert(offsetof(FrameContract, reserved) == 4, "FrameContract.reserved golden offset");
|
|
static_assert(offsetof(FrameContract, published_sequence) == 8, "FrameContract.published_sequence golden offset");
|
|
static_assert(offsetof(FrameContract, slots) == 16, "FrameContract.slots golden offset");
|
|
|
|
// --- Control plane ----------------------------------------------------------
|
|
//
|
|
// Carried over a named pipe / unix socket, NEVER shared memory and NEVER
|
|
// per-frame data (frames only ever travel through FrameContract above).
|
|
|
|
enum class ControlMessageType : uint32_t {
|
|
HANDSHAKE_VERSION = 0,
|
|
CONFIG = 1,
|
|
CALIBRATION = 2,
|
|
HEARTBEAT = 3,
|
|
};
|
|
|
|
// Versioned header prefixing every control-plane message.
|
|
struct ControlMessageHeader {
|
|
ControlMessageType message_type;
|
|
uint32_t contract_version;
|
|
uint32_t payload_size; // size of the full message (header + payload), bytes
|
|
uint32_t reserved;
|
|
};
|
|
static_assert(sizeof(ControlMessageHeader) == 16, "ControlMessageHeader golden size");
|
|
static_assert(offsetof(ControlMessageHeader, message_type) == 0, "ControlMessageHeader.message_type golden offset");
|
|
static_assert(offsetof(ControlMessageHeader, contract_version) == 4,
|
|
"ControlMessageHeader.contract_version golden offset");
|
|
static_assert(offsetof(ControlMessageHeader, payload_size) == 8,
|
|
"ControlMessageHeader.payload_size golden offset");
|
|
static_assert(offsetof(ControlMessageHeader, reserved) == 12, "ControlMessageHeader.reserved golden offset");
|
|
|
|
// Sent once by the producer on connect; the consumer rejects a mismatched
|
|
// contract_version in the header rather than guessing at compatibility.
|
|
struct HandshakeMessage {
|
|
ControlMessageHeader header;
|
|
uint32_t client_version;
|
|
uint32_t capabilities;
|
|
};
|
|
static_assert(sizeof(HandshakeMessage) == 24, "HandshakeMessage golden size");
|
|
static_assert(offsetof(HandshakeMessage, header) == 0, "HandshakeMessage.header golden offset");
|
|
static_assert(offsetof(HandshakeMessage, client_version) == 16, "HandshakeMessage.client_version golden offset");
|
|
static_assert(offsetof(HandshakeMessage, capabilities) == 20, "HandshakeMessage.capabilities golden offset");
|
|
|
|
// Stream geometry the consumer has requested (nui-contracts.md §2.1: read the
|
|
// host's requested eResolution, never hardcode it).
|
|
struct ConfigMessage {
|
|
ControlMessageHeader header;
|
|
uint32_t depth_width;
|
|
uint32_t depth_height;
|
|
uint32_t color_width;
|
|
uint32_t color_height;
|
|
};
|
|
static_assert(sizeof(ConfigMessage) == 32, "ConfigMessage golden size");
|
|
static_assert(offsetof(ConfigMessage, header) == 0, "ConfigMessage.header golden offset");
|
|
static_assert(offsetof(ConfigMessage, depth_width) == 16, "ConfigMessage.depth_width golden offset");
|
|
static_assert(offsetof(ConfigMessage, depth_height) == 20, "ConfigMessage.depth_height golden offset");
|
|
static_assert(offsetof(ConfigMessage, color_width) == 24, "ConfigMessage.color_width golden offset");
|
|
static_assert(offsetof(ConfigMessage, color_height) == 28, "ConfigMessage.color_height golden offset");
|
|
|
|
// Calibration result (Z0/scale, mirror bit, floor plane); the full
|
|
// calibration flow is todo 11 - this pins only the wire shape.
|
|
struct CalibrationMessage {
|
|
ControlMessageHeader header;
|
|
float z0_meters;
|
|
uint32_t mirror; // 0 or 1; see nui-contracts.md §1.4
|
|
float floor_plane[4];
|
|
};
|
|
static_assert(sizeof(CalibrationMessage) == 40, "CalibrationMessage golden size");
|
|
static_assert(offsetof(CalibrationMessage, header) == 0, "CalibrationMessage.header golden offset");
|
|
static_assert(offsetof(CalibrationMessage, z0_meters) == 16, "CalibrationMessage.z0_meters golden offset");
|
|
static_assert(offsetof(CalibrationMessage, mirror) == 20, "CalibrationMessage.mirror golden offset");
|
|
static_assert(offsetof(CalibrationMessage, floor_plane) == 24, "CalibrationMessage.floor_plane golden offset");
|
|
|
|
// Liveness ping both directions can use to detect a stalled peer (drives
|
|
// IS-8's graceful-degradation recovery, not a fixed-delay reconnect).
|
|
struct HeartbeatMessage {
|
|
ControlMessageHeader header;
|
|
int64_t timestamp_micros;
|
|
uint64_t sequence; // last FrameContract.published_sequence the sender observed
|
|
};
|
|
static_assert(sizeof(HeartbeatMessage) == 32, "HeartbeatMessage golden size");
|
|
static_assert(offsetof(HeartbeatMessage, header) == 0, "HeartbeatMessage.header golden offset");
|
|
static_assert(offsetof(HeartbeatMessage, timestamp_micros) == 16, "HeartbeatMessage.timestamp_micros golden offset");
|
|
static_assert(offsetof(HeartbeatMessage, sequence) == 24, "HeartbeatMessage.sequence golden offset");
|
|
|
|
#pragma pack(pop)
|
|
|
|
// --- Triple-buffer publish/read protocol ------------------------------------
|
|
|
|
// Writes `data` into the next slot and publishes it as newest, overwriting the
|
|
// oldest-but-one slot (single-writer only; never call concurrently). The slot
|
|
// sequence advances by one to odd before the write and one to even after it.
|
|
void publish_frame(FrameContract& contract, const FrameData& data);
|
|
|
|
// Snapshots the newest published frame into `out`. Retries internally against
|
|
// a concurrent publish (seqlock-style) and always returns a consistent frame.
|
|
void read_latest_frame(const FrameContract& contract, FrameData& out);
|
|
|
|
} // namespace nui::contract
|
|
|
|
#endif // NUI_CORE_CONTRACT_H
|