Files
kinect/tools/mock-nui-host/mock_nui_host.cpp
T
kinect-bot 4a66699d82
ci / Linux C++ core (cmake + ctest) (push) Canceled after 0s
ci / Python tracker (pytest) (push) Canceled after 0s
feat(mapping,tracker,tools): NUI mapping+smoothing, MediaPipe tracker, record/replay harness, mock NUI host (wave 3)
2026-11-25 04:21:18 -05:00

193 lines
7.7 KiB
C++

// Mock NUI host for the clean-room Kinect10.dll shim (plan todo 16; Windows-only).
//
// Drives the full Kinect for Windows 1.8 NUI call sequence against whatever
// Kinect10.dll is loaded (the fallback/windows-nui shim from todo 14, or the
// real runtime), so the shim can be exercised with no sensor and no game. It is
// the Windows counterpart of the Python record/replay harness: a scripted NUI
// consumer that asserts the shim honours the event/timing/buffer contract in
// .omo/design/nui-contracts.md section 3.
//
// Clean-room note: every NUI name, flag, enum and struct used here is taken from
// the PUBLIC Kinect SDK 1.8 headers (NuiApi.h / NuiImageCamera.h / NuiSkeleton.h).
// No GPLv3 source (KinectFaker etc.) was consulted.
//
// BUILD: Windows + MSVC + Kinect for Windows SDK 1.8 ONLY. This file is NOT part
// of the Linux CMake build (it needs windows.h and the Kinect SDK import lib);
// see README.md for the [~] blocked status and the standalone build command.
#include <windows.h>
#include <NuiApi.h>
#include <cstdint>
#include <cstdio>
namespace {
constexpr int kTicksToObserve = 60; // ~2 s at 30 Hz
constexpr DWORD kFrameWaitMs = 100; // bounded wait per nui-contracts.md section 3.3
constexpr DWORD kDepthWidth = 640;
constexpr DWORD kDepthHeight = 480;
struct HostStats {
int skeleton_frames = 0;
int tracked_skeletons = 0;
int depth_frames = 0;
int color_frames = 0;
DWORD last_skeleton_frame_number = 0;
LARGE_INTEGER last_skeleton_timestamp = {};
bool frame_numbers_monotonic = true;
bool timestamps_monotonic = true;
bool errored = false;
};
bool Check(HRESULT hr, const char* what, HostStats& stats) {
if (FAILED(hr)) {
std::fprintf(stderr, "[mock-nui-host] %s failed: hr=0x%08lX\n", what,
static_cast<unsigned long>(hr));
stats.errored = true;
return false;
}
return true;
}
// Pulls the newest skeleton frame and validates the section-3.6 frame-number /
// timestamp invariants (one monotonic counter; monotonic QPC-style stamp).
void ConsumeSkeletonFrame(HostStats& stats) {
NUI_SKELETON_FRAME frame = {};
HRESULT hr = NuiSkeletonGetNextFrame(0, &frame);
if (hr == E_NUI_FRAME_NO_DATA) {
return;
}
if (!Check(hr, "NuiSkeletonGetNextFrame", stats)) {
return;
}
++stats.skeleton_frames;
if (stats.skeleton_frames > 1) {
if (frame.dwFrameNumber < stats.last_skeleton_frame_number) {
stats.frame_numbers_monotonic = false;
}
if (frame.liTimeStamp.QuadPart < stats.last_skeleton_timestamp.QuadPart) {
stats.timestamps_monotonic = false;
}
}
stats.last_skeleton_frame_number = frame.dwFrameNumber;
stats.last_skeleton_timestamp = frame.liTimeStamp;
// Smooth exactly as a real consumer would (NUI_TRANSFORM_SMOOTH_PARAMETERS),
// confirming the shim accepts the documented smoothing entry point.
NUI_TRANSFORM_SMOOTH_PARAMETERS smooth = {0.5f, 0.5f, 0.5f, 0.05f, 0.04f};
NuiTransformSmooth(&frame, &smooth);
for (int i = 0; i < NUI_SKELETON_COUNT; ++i) {
if (frame.SkeletonData[i].eTrackingState == NUI_SKELETON_TRACKED) {
++stats.tracked_skeletons;
}
}
}
// Pulls one image frame, locks the texture, validates the reported pitch/size
// against the requested resolution (section 3.5), then releases it.
void ConsumeImageFrame(HANDLE stream, const char* label, DWORD expected_pitch,
int& counter, HostStats& stats) {
NUI_IMAGE_FRAME frame = {};
HRESULT hr = NuiImageStreamGetNextFrame(stream, 0, &frame);
if (hr == E_NUI_FRAME_NO_DATA) {
return;
}
if (!Check(hr, "NuiImageStreamGetNextFrame", stats)) {
return;
}
INuiFrameTexture* texture = frame.pFrameTexture;
NUI_LOCKED_RECT locked = {};
if (texture != nullptr && Check(texture->LockRect(0, &locked, nullptr, 0),
"INuiFrameTexture::LockRect", stats)) {
if (locked.Pitch != static_cast<INT>(expected_pitch)) {
std::fprintf(stderr,
"[mock-nui-host] %s pitch %d != expected %lu (section 3.5)\n",
label, locked.Pitch, static_cast<unsigned long>(expected_pitch));
stats.errored = true;
}
texture->UnlockRect(0);
++counter;
}
Check(NuiImageStreamReleaseFrame(stream, &frame), "NuiImageStreamReleaseFrame", stats);
}
} // namespace
int main() {
HostStats stats;
HRESULT hr = NuiInitialize(NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX |
NUI_INITIALIZE_FLAG_USES_COLOR |
NUI_INITIALIZE_FLAG_USES_SKELETON);
if (!Check(hr, "NuiInitialize", stats)) {
return 2;
}
// Manual-reset events the producer signals per new frame (section 3.2/3.3).
HANDLE skeleton_event = CreateEvent(nullptr, TRUE, FALSE, nullptr);
HANDLE depth_event = CreateEvent(nullptr, TRUE, FALSE, nullptr);
HANDLE color_event = CreateEvent(nullptr, TRUE, FALSE, nullptr);
HANDLE depth_stream = nullptr;
HANDLE color_stream = nullptr;
bool ok = Check(NuiSkeletonTrackingEnable(skeleton_event, 0),
"NuiSkeletonTrackingEnable", stats);
ok = ok && Check(NuiImageStreamOpen(NUI_IMAGE_TYPE_DEPTH_AND_PLAYER_INDEX,
NUI_IMAGE_RESOLUTION_640x480, 0, 2, depth_event,
&depth_stream),
"NuiImageStreamOpen(depth)", stats);
ok = ok && Check(NuiImageStreamOpen(NUI_IMAGE_TYPE_COLOR, NUI_IMAGE_RESOLUTION_640x480,
0, 2, color_event, &color_stream),
"NuiImageStreamOpen(color)", stats);
if (ok) {
HANDLE events[3] = {skeleton_event, depth_event, color_event};
for (int tick = 0; tick < kTicksToObserve && !stats.errored; ++tick) {
DWORD signaled = WaitForMultipleObjects(3, events, FALSE, kFrameWaitMs);
if (signaled == WAIT_TIMEOUT) {
continue;
}
// Drain every signaled stream this tick; manual-reset events are reset
// by the DLL (skeleton) or the app (image) per section 3.3.
if (WaitForSingleObject(skeleton_event, 0) == WAIT_OBJECT_0) {
ConsumeSkeletonFrame(stats);
}
if (WaitForSingleObject(depth_event, 0) == WAIT_OBJECT_0) {
ResetEvent(depth_event);
ConsumeImageFrame(depth_stream, "depth", kDepthWidth * 2, stats.depth_frames,
stats);
}
if (WaitForSingleObject(color_event, 0) == WAIT_OBJECT_0) {
ResetEvent(color_event);
ConsumeImageFrame(color_stream, "color", kDepthWidth * 4, stats.color_frames,
stats);
}
}
}
NuiShutdown();
CloseHandle(skeleton_event);
CloseHandle(depth_event);
CloseHandle(color_event);
std::printf("[mock-nui-host] skeleton_frames=%d tracked=%d depth_frames=%d color_frames=%d\n",
stats.skeleton_frames, stats.tracked_skeletons, stats.depth_frames,
stats.color_frames);
std::printf("[mock-nui-host] frame_numbers_monotonic=%d timestamps_monotonic=%d\n",
stats.frame_numbers_monotonic ? 1 : 0, stats.timestamps_monotonic ? 1 : 0);
bool passed = ok && !stats.errored && stats.skeleton_frames > 0 &&
stats.depth_frames > 0 && stats.color_frames > 0 &&
stats.frame_numbers_monotonic && stats.timestamps_monotonic;
std::printf("[mock-nui-host] RESULT: %s\n", passed ? "PASS" : "FAIL");
return passed ? 0 : 1;
}