/* * WiVRn VR streaming * Copyright (C) 2022-2024 Guillaume Meunier * Copyright (C) 2022-2024 Patrick Nicolas * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #pragma once #include "clock_offset.h" #include "util/u_time.h" #include #include #include #include namespace wivrn { template class history { struct TimedData : public Data { XrTime produced_timestamp; XrTime at_timestamp_ns; }; std::array data{}; std::mutex mutex; protected: // return true if object is active (last request is not too old) void add_sample(XrTime produced_timestamp, XrTime timestamp, const Data & sample, const clock_offset & offset) { XrTime produced = offset.from_headset(produced_timestamp); XrTime t = offset.from_headset(timestamp); std::lock_guard lock(mutex); TimedData * target = data.data(); if (offset) { for (auto & item: data) { // Discard reordered packets if (item.produced_timestamp > produced) return; if (std::abs(item.at_timestamp_ns - t) < 2'000'000) { target = &item; break; } if (item.at_timestamp_ns < target->at_timestamp_ns) target = &item; } } *target = TimedData(sample, produced, t); } public: std::pair get_at(XrTime at_timestamp_ns) { std::lock_guard lock(mutex); TimedData * before = nullptr; TimedData * after = nullptr; for (auto & item: data) { if (not item.at_timestamp_ns) continue; if (item.at_timestamp_ns < at_timestamp_ns) { if (not before or before->at_timestamp_ns < item.at_timestamp_ns) before = &item; } else { if (not after or after->at_timestamp_ns > item.at_timestamp_ns) after = &item; } } XrTime produced = 0; if (after) produced = std::max(produced, after->produced_timestamp); if (before) produced = std::max(produced, before->produced_timestamp); if (before and after) { float t = float(at_timestamp_ns - before->at_timestamp_ns) / (after->at_timestamp_ns - before->at_timestamp_ns); return {produced, static_cast(this)->interpolate(*before, *after, t)}; } if (before) { if (at_timestamp_ns > before->at_timestamp_ns + U_TIME_1S_IN_NS) return {}; return {produced, *before}; } if (after) return {produced, *after}; return {}; } void reset() { std::lock_guard lock(mutex); data.fill({}); } }; } // namespace wivrn