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.
This commit is contained in:
1 parent
8f3ac6659d
commit
ddf4847a0c
4 files changed
+199
No files matched your search
@@ -4,9 +4,17 @@ HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
ORT="${HERE}/third_party/onnxruntime-linux-x64"
|
||||
mkdir -p "${HERE}/build"
|
||||
|
||||
CORE="${HERE}/../core"
|
||||
|
||||
g++ -std=c++17 -O2 -Wall -Wextra \
|
||||
-I "${ORT}/include" \
|
||||
"${HERE}/src/ort_smoke.cpp" \
|
||||
-o "${HERE}/build/ort_smoke" \
|
||||
-L "${ORT}/lib" -lonnxruntime -Wl,-rpath,"${ORT}/lib"
|
||||
echo "built ${HERE}/build/ort_smoke"
|
||||
|
||||
g++ -std=c++17 -O2 -Wall -Wextra \
|
||||
-I "${CORE}" -I "${HERE}/src" \
|
||||
"${HERE}/src/shm_publisher.cpp" "${HERE}/src/shm_roundtrip_test.cpp" \
|
||||
-o "${HERE}/build/shm_roundtrip_test"
|
||||
"${HERE}/build/shm_roundtrip_test"
|
||||
@@ -0,0 +1,82 @@
|
||||
#include "shm_publisher.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
namespace kinect_tracker {
|
||||
|
||||
using nui::contract::FrameData;
|
||||
using nui::contract::FrameSlot;
|
||||
using nui::contract::kContractVersion;
|
||||
using nui::contract::kFrameSlotCount;
|
||||
|
||||
ShmPublisher::ShmPublisher(uint8_t* base, uint32_t depth_w, uint32_t depth_h,
|
||||
uint32_t color_w, uint32_t color_h)
|
||||
: base_(base),
|
||||
depth_w_(depth_w),
|
||||
depth_h_(depth_h),
|
||||
color_w_(color_w),
|
||||
color_h_(color_h),
|
||||
depth_bytes_(static_cast<std::size_t>(depth_w) * depth_h * sizeof(uint16_t)),
|
||||
color_bytes_(static_cast<std::size_t>(color_w) * color_h * 4u),
|
||||
slot_pixel_bytes_(depth_bytes_ + color_bytes_) {}
|
||||
|
||||
std::size_t ShmPublisher::region_size(uint32_t depth_w, uint32_t depth_h,
|
||||
uint32_t color_w, uint32_t color_h) {
|
||||
const std::size_t depth_bytes =
|
||||
static_cast<std::size_t>(depth_w) * depth_h * sizeof(uint16_t);
|
||||
const std::size_t color_bytes = static_cast<std::size_t>(color_w) * color_h * 4u;
|
||||
return sizeof(FrameContract) + kFrameSlotCount * (depth_bytes + color_bytes);
|
||||
}
|
||||
|
||||
std::size_t ShmPublisher::depth_offset(std::size_t slot) const {
|
||||
return sizeof(FrameContract) + slot * slot_pixel_bytes_;
|
||||
}
|
||||
|
||||
std::size_t ShmPublisher::color_offset(std::size_t slot) const {
|
||||
return depth_offset(slot) + depth_bytes_;
|
||||
}
|
||||
|
||||
void ShmPublisher::init() {
|
||||
auto* fc = reinterpret_cast<FrameContract*>(base_);
|
||||
fc->contract_version = kContractVersion;
|
||||
fc->reserved = 0;
|
||||
fc->published_sequence = 0;
|
||||
for (std::size_t i = 0; i < kFrameSlotCount; ++i) {
|
||||
fc->slots[i].sequence = 0;
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t ShmPublisher::publish(uint32_t frame_number, int64_t timestamp_micros,
|
||||
const SkeletonFrame& skeleton,
|
||||
const uint16_t* depth, const uint8_t* color_bgra) {
|
||||
auto* fc = reinterpret_cast<FrameContract*>(base_);
|
||||
const uint64_t next_seq = fc->published_sequence + 1;
|
||||
const std::size_t slot_idx = next_seq % kFrameSlotCount;
|
||||
FrameSlot& slot = fc->slots[slot_idx];
|
||||
|
||||
slot.sequence += 1; // odd: write in progress
|
||||
FrameData& d = slot.data;
|
||||
d.frame_number = frame_number;
|
||||
d.timestamp_micros = timestamp_micros;
|
||||
d.skeleton = skeleton;
|
||||
|
||||
const std::size_t doff = depth_offset(slot_idx);
|
||||
const std::size_t coff = color_offset(slot_idx);
|
||||
d.depth.width = depth_w_;
|
||||
d.depth.height = depth_h_;
|
||||
d.depth.pitch = depth_w_ * sizeof(uint16_t);
|
||||
d.depth.byte_offset = doff;
|
||||
d.color.width = color_w_;
|
||||
d.color.height = color_h_;
|
||||
d.color.pitch = color_w_ * 4u;
|
||||
d.color.byte_offset = coff;
|
||||
|
||||
std::memcpy(base_ + doff, depth, depth_bytes_);
|
||||
std::memcpy(base_ + coff, color_bgra, color_bytes_);
|
||||
|
||||
slot.sequence += 1; // even: write complete
|
||||
fc->published_sequence = next_seq;
|
||||
return next_seq;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
#ifndef TRACKER_NATIVE_SHM_PUBLISHER_H
|
||||
#define TRACKER_NATIVE_SHM_PUBLISHER_H
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
#include "contract/contract.h"
|
||||
|
||||
namespace kinect_tracker {
|
||||
|
||||
using nui::contract::FrameContract;
|
||||
using nui::contract::SkeletonFrame;
|
||||
|
||||
class ShmPublisher {
|
||||
public:
|
||||
ShmPublisher(uint8_t* base, uint32_t depth_w, uint32_t depth_h,
|
||||
uint32_t color_w, uint32_t color_h);
|
||||
|
||||
static std::size_t region_size(uint32_t depth_w, uint32_t depth_h,
|
||||
uint32_t color_w, uint32_t color_h);
|
||||
|
||||
void init();
|
||||
|
||||
uint64_t publish(uint32_t frame_number, int64_t timestamp_micros,
|
||||
const SkeletonFrame& skeleton, const uint16_t* depth,
|
||||
const uint8_t* color_bgra);
|
||||
|
||||
private:
|
||||
std::size_t depth_offset(std::size_t slot) const;
|
||||
std::size_t color_offset(std::size_t slot) const;
|
||||
|
||||
uint8_t* base_;
|
||||
uint32_t depth_w_, depth_h_, color_w_, color_h_;
|
||||
std::size_t depth_bytes_, color_bytes_, slot_pixel_bytes_;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,70 @@
|
||||
#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;
|
||||
}
|
||||
Reference in new issue
Block a user