#include "shm_frame_source.h" #include #include #include #include #include #include #include namespace nui::openni2_fallback { namespace { std::size_t region_size_for(synth::ImageSize depth_size, synth::ImageSize color_size) { const std::size_t depth_bytes = static_cast(depth_size.width) * depth_size.height * sizeof(uint16_t); const std::size_t color_bytes = static_cast(color_size.width) * color_size.height * 4; return sizeof(contract::FrameContract) + contract::kFrameSlotCount * (depth_bytes + color_bytes); } } // namespace ShmFrameSource::ShmFrameSource(std::string shm_name, synth::ImageSize depth_size, synth::ImageSize color_size) : shm_name_(std::move(shm_name)), depth_size_(depth_size), color_size_(color_size), region_size_(region_size_for(depth_size, color_size)) {} ShmFrameSource::~ShmFrameSource() { if (map_ != nullptr) { munmap(map_, region_size_); } if (fd_ >= 0) { close(fd_); } } bool ShmFrameSource::open(int timeout_ms) { const std::string posix_name = "/" + shm_name_; const auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeout_ms); while (fd_ < 0) { fd_ = shm_open(posix_name.c_str(), O_RDONLY, 0); if (fd_ >= 0) { break; } if (std::chrono::steady_clock::now() >= deadline) { return false; } std::this_thread::sleep_for(std::chrono::milliseconds(50)); } struct stat info {}; while (fstat(fd_, &info) == 0 && static_cast(info.st_size) < region_size_) { if (std::chrono::steady_clock::now() >= deadline) { close(fd_); fd_ = -1; return false; } std::this_thread::sleep_for(std::chrono::milliseconds(50)); } map_ = mmap(nullptr, region_size_, PROT_READ, MAP_SHARED, fd_, 0); if (map_ == MAP_FAILED) { map_ = nullptr; close(fd_); fd_ = -1; return false; } contract_ = static_cast(map_); return true; } ShmFrameSnapshot ShmFrameSource::try_read_frame(int max_attempts) const { ShmFrameSnapshot snapshot; if (contract_ == nullptr) { return snapshot; } const auto* base = static_cast(map_); for (int attempt = 0; attempt < max_attempts; ++attempt) { const uint64_t published = contract_->published_sequence; if (published == 0) { return snapshot; } const contract::FrameSlot& slot = contract_->slots[published % contract::kFrameSlotCount]; const uint64_t seq_before = slot.sequence; if (seq_before & 1) { continue; } const contract::FrameData data = slot.data; std::vector depth_words; std::vector color_bgra; if (data.depth.width > 0 && data.depth.height > 0) { const std::size_t count = static_cast(data.depth.width) * data.depth.height; depth_words.resize(count); std::memcpy(depth_words.data(), base + data.depth.byte_offset, count * sizeof(uint16_t)); } if (data.color.width > 0 && data.color.height > 0) { const std::size_t count = static_cast(data.color.width) * data.color.height * 4; color_bgra.resize(count); std::memcpy(color_bgra.data(), base + data.color.byte_offset, count); } const uint64_t seq_after = slot.sequence; if (seq_after != seq_before || contract_->published_sequence != published) { continue; } snapshot.skeleton = data.skeleton; snapshot.depth_size = {data.depth.width, data.depth.height}; snapshot.depth_words = std::move(depth_words); snapshot.color_size = {data.color.width, data.color.height}; snapshot.color_bgra = std::move(color_bgra); snapshot.has_data = true; return snapshot; } return snapshot; } synth::SegmentationMask ShmFrameSource::latest_mask(synth::ImageSize fallback_size) const { const auto snapshot = try_read_frame(); if (!snapshot.has_data || snapshot.depth_size.width == 0 || snapshot.depth_size.height == 0) { return synth::SegmentationMask{fallback_size, std::vector(synth::pixel_count(fallback_size), 0.0f)}; } synth::SegmentationMask mask{snapshot.depth_size, std::vector(snapshot.depth_words.size())}; for (std::size_t index = 0; index < snapshot.depth_words.size(); ++index) { mask.confidence[index] = snapshot.depth_words[index] != 0 ? 1.0f : 0.0f; } return mask; } contract::SkeletonFrame ShmFrameSource::latest_skeleton() const { return try_read_frame().skeleton; } synth::BgraFrame ShmFrameSource::latest_color(synth::ImageSize fallback_size) const { const auto snapshot = try_read_frame(); if (!snapshot.has_data || snapshot.color_size.width == 0 || snapshot.color_size.height == 0) { return synth::BgraFrame{fallback_size, fallback_size.width * 4, std::vector(synth::pixel_count(fallback_size) * 4, 0)}; } return synth::BgraFrame{snapshot.color_size, snapshot.color_size.width * 4, snapshot.color_bgra}; } } // namespace nui::openni2_fallback