Files
kinect/tracker-native/src/shm_roundtrip_test.cpp
kinect-bot ddf4847a0c
ci / Linux C++ core (cmake + ctest) (push) Canceled after 0s
ci / Python root tests (pytest, no tracker deps) (push) Canceled after 0s
ci / Python tracker (pytest, tracker/.venv) (push) Canceled after 0s
feat(tracker-native): native shm writer + headless round-trip test
ShmPublisher mirrors the Python FrameContractPublisher seqlock (write slot
ahead, odd->even sequence, bump published_sequence) over a base pointer, reusing
core/contract structs directly (Windows MapViewOfFile on the app; a heap buffer
in the test). Round-trip test proves the layout+seqlock the shim reader accepts:
frame number, timestamp, skeleton, descriptors, depth+color pixels all verify.
2026-11-25 18:41:58 -05:00

71 lines
2.5 KiB
C++

#include <cassert>
#include <cstdint>
#include <cstdio>
#include <vector>
#include "shm_publisher.h"
using kinect_tracker::ShmPublisher;
using nui::contract::FrameContract;
using nui::contract::FrameData;
using nui::contract::FrameSlot;
using nui::contract::JointTrackingState;
using nui::contract::kContractVersion;
using nui::contract::kFrameSlotCount;
using nui::contract::SkeletonFrame;
int main() {
const uint32_t dw = 640, dh = 480, cw = 640, ch = 480;
const std::size_t rs = ShmPublisher::region_size(dw, dh, cw, ch);
std::vector<uint8_t> region(rs, 0);
ShmPublisher pub(region.data(), dw, dh, cw, ch);
pub.init();
std::vector<uint16_t> depth(static_cast<std::size_t>(dw) * dh, 0);
depth[100 * dw + 50] = 0x1234;
std::vector<uint8_t> color(static_cast<std::size_t>(cw) * ch * 4u);
for (std::size_t i = 0; i < color.size(); ++i) color[i] = static_cast<uint8_t>(i & 0xff);
SkeletonFrame skel{};
skel.skeleton_tracking_id = 7;
skel.quality_flags = 0;
skel.joints[0].position = {1.5f, -2.0f, 3.0f, 1.0f};
skel.joints[0].tracking_state = JointTrackingState::TRACKED;
uint64_t seq = 0;
for (uint32_t f = 1; f <= 4; ++f) {
depth[0] = static_cast<uint16_t>(f);
seq = pub.publish(f, static_cast<int64_t>(f) * 1000, skel, depth.data(), color.data());
}
assert(seq == 4);
auto* fc = reinterpret_cast<FrameContract*>(region.data());
assert(fc->contract_version == kContractVersion);
assert(fc->published_sequence == 4);
const FrameSlot& slot = fc->slots[fc->published_sequence % kFrameSlotCount];
const uint64_t s0 = slot.sequence;
assert((s0 & 1u) == 0u);
FrameData d = slot.data;
assert(slot.sequence == s0);
assert(d.frame_number == 4);
assert(d.timestamp_micros == 4000);
assert(d.skeleton.skeleton_tracking_id == 7);
assert(d.skeleton.joints[0].tracking_state == JointTrackingState::TRACKED);
assert(d.depth.width == dw && d.depth.height == dh && d.depth.pitch == dw * 2u);
assert(d.color.width == cw && d.color.pitch == cw * 4u);
const auto* dp = reinterpret_cast<const uint16_t*>(region.data() + d.depth.byte_offset);
assert(dp[0] == 4);
assert(dp[100 * dw + 50] == 0x1234);
const uint8_t* cp = region.data() + d.color.byte_offset;
assert(cp[5] == static_cast<uint8_t>(5));
assert(cp[777] == static_cast<uint8_t>(777 & 0xff));
std::printf("shm_roundtrip OK: region=%zu bytes, published_seq=%llu, frame=%u\n",
rs, static_cast<unsigned long long>(fc->published_sequence), d.frame_number);
return 0;
}