127 lines
4.6 KiB
Python
127 lines
4.6 KiB
Python
"""Deterministic fixture file format for recorded Kinect frames (plan todo 16).
|
|
|
|
A fixture is a simple framed binary file capturing a sequence of
|
|
``(sequence, FrameData)`` records, where ``FrameData`` (tracker/contract.py)
|
|
already carries the SkeletonFrame, the depth/color buffer descriptors, the
|
|
frame number and the QPC-style timestamp. The on-disk layout is fixed-width and
|
|
self-describing so a truncated or drifted fixture is detected and rejected
|
|
cleanly rather than replayed as garbage:
|
|
|
|
header (little-endian):
|
|
magic 4s b"VKRR"
|
|
format_version I FORMAT_VERSION
|
|
contract_version I tracker.contract.CONTRACT_VERSION at record time
|
|
frame_data_size I sizeof(FrameData); guards against contract drift
|
|
frame_count I number of records that follow
|
|
record (repeated frame_count times):
|
|
sequence Q publish sequence number
|
|
frame_data {frame_data_size} bytes of FrameData
|
|
|
|
The format is byte-exact: replaying a fixture reproduces the recorded frames
|
|
bit-for-bit, which is what the harness asserts.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import struct
|
|
from typing import BinaryIO, Iterable, List, NamedTuple, Tuple
|
|
|
|
from .buffer import copy_frame
|
|
from tracker.contract import CONTRACT_VERSION, FrameData
|
|
|
|
MAGIC = b"VKRR"
|
|
FORMAT_VERSION = 1
|
|
|
|
_HEADER = struct.Struct("<4sIIII")
|
|
_RECORD_PREFIX = struct.Struct("<Q")
|
|
|
|
|
|
class FixtureError(Exception):
|
|
"""Base class for fixture read failures."""
|
|
|
|
|
|
class FixtureFormatError(FixtureError):
|
|
"""The fixture is not a valid/compatible VKRR fixture (bad magic, version, drift)."""
|
|
|
|
|
|
class TruncatedFixtureError(FixtureError):
|
|
"""The fixture ended mid-header or mid-record."""
|
|
|
|
|
|
class FixtureHeader(NamedTuple):
|
|
format_version: int
|
|
contract_version: int
|
|
frame_data_size: int
|
|
frame_count: int
|
|
|
|
|
|
RecordedFrame = Tuple[int, FrameData]
|
|
|
|
|
|
def _frame_data_size() -> int:
|
|
import ctypes
|
|
|
|
return ctypes.sizeof(FrameData)
|
|
|
|
|
|
def write_fixture(path: str, frames: Iterable[RecordedFrame]) -> int:
|
|
"""Write ``frames`` to ``path`` as a VKRR fixture; returns the record count."""
|
|
frame_data_size = _frame_data_size()
|
|
materialized: List[RecordedFrame] = list(frames)
|
|
with open(path, "wb") as handle:
|
|
handle.write(
|
|
_HEADER.pack(MAGIC, FORMAT_VERSION, CONTRACT_VERSION, frame_data_size, len(materialized))
|
|
)
|
|
for sequence, frame in materialized:
|
|
payload = bytes(frame)
|
|
if len(payload) != frame_data_size:
|
|
raise FixtureFormatError(
|
|
f"frame payload is {len(payload)} bytes, expected {frame_data_size}"
|
|
)
|
|
handle.write(_RECORD_PREFIX.pack(sequence))
|
|
handle.write(payload)
|
|
return len(materialized)
|
|
|
|
|
|
def _read_exact(handle: BinaryIO, count: int, what: str) -> bytes:
|
|
data = handle.read(count)
|
|
if len(data) != count:
|
|
raise TruncatedFixtureError(
|
|
f"fixture truncated reading {what}: wanted {count} bytes, got {len(data)}"
|
|
)
|
|
return data
|
|
|
|
|
|
def read_fixture(path: str) -> Tuple[FixtureHeader, List[RecordedFrame]]:
|
|
"""Read and validate a VKRR fixture; returns ``(header, [(sequence, FrameData), ...])``."""
|
|
expected_size = _frame_data_size()
|
|
with open(path, "rb") as handle:
|
|
header_bytes = _read_exact(handle, _HEADER.size, "header")
|
|
magic, format_version, contract_version, frame_data_size, frame_count = _HEADER.unpack(
|
|
header_bytes
|
|
)
|
|
if magic != MAGIC:
|
|
raise FixtureFormatError(f"bad magic {magic!r}, expected {MAGIC!r}")
|
|
if format_version != FORMAT_VERSION:
|
|
raise FixtureFormatError(
|
|
f"unsupported fixture format version {format_version}, expected {FORMAT_VERSION}"
|
|
)
|
|
if frame_data_size != expected_size:
|
|
raise FixtureFormatError(
|
|
f"fixture frame_data_size {frame_data_size} != current FrameData size "
|
|
f"{expected_size} (contract drift)"
|
|
)
|
|
frames: List[RecordedFrame] = []
|
|
for index in range(frame_count):
|
|
(sequence,) = _RECORD_PREFIX.unpack(
|
|
_read_exact(handle, _RECORD_PREFIX.size, f"record {index} prefix")
|
|
)
|
|
payload = _read_exact(handle, frame_data_size, f"record {index} payload")
|
|
frames.append((sequence, FrameData.from_buffer_copy(payload)))
|
|
trailing = handle.read(1)
|
|
if trailing:
|
|
raise FixtureFormatError(
|
|
f"fixture has trailing bytes after {frame_count} records (corrupt)"
|
|
)
|
|
return FixtureHeader(format_version, contract_version, frame_data_size, frame_count), frames
|