Files
WiVRn/server/driver/history.h
T
Patrick Nicolas 7658f427e6 Request tracking extrapolation on client
Add the timestamp where data was requested, use this to compute how far
in the future the application is requesting samples.
With this information, client requests 2 tracking samples: one at the
current time and one in the future.
Server now keeps track of actual samples + one predicted sample, and is
most of the time in the interpolation use case.
2024-04-04 13:41:36 +02:00

117 lines
3.4 KiB
C++

/*
* WiVRn VR streaming
* Copyright (C) 2022-2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2022-2024 Patrick Nicolas <patricknicolas@laposte.net>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "clock_offset.h"
#include <algorithm>
#include <cstddef>
#include <list>
#include <mutex>
template <typename Derived, typename Data, bool extrapolate = false, size_t MaxSamples = 10>
class history
{
struct TimedData : public Data
{
XrTime produced_timestamp;
XrTime at_timestamp_ns;
};
std::mutex mutex;
std::list<TimedData> data;
protected:
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);
// Only keep one predicted value
// It should be the last one
if (not data.empty() and t != produced and data.back().at_timestamp_ns != data.back().produced_timestamp)
data.pop_back();
// Insert the new sample
auto it = std::lower_bound(data.begin(), data.end(), t, [](TimedData & sample, uint64_t t) { return sample.at_timestamp_ns < t; });
if (it == data.end())
data.emplace_back(sample, produced, t);
else if (it->at_timestamp_ns == t)
*it = TimedData(sample, produced, t);
else
data.emplace(it, sample, produced, t);
while (data.size() > MaxSamples)
data.pop_front();
}
public:
std::pair<std::chrono::nanoseconds, Data> get_at(XrTime at_timestamp_ns)
{
std::lock_guard lock(mutex);
std::chrono::nanoseconds ex(0);
if (data.empty())
{
return {};
}
if (data.size() == 1)
{
return {ex, data.front()};
}
if (data.front().at_timestamp_ns > at_timestamp_ns)
{
if (extrapolate)
{
auto second = data.begin();
auto first = second++;
return {ex, Derived::extrapolate(*first, *second, first->at_timestamp_ns, second->at_timestamp_ns, at_timestamp_ns)};
}
else
return {ex, data.front()};
}
for (auto after = data.begin(), before = after++; after != data.end(); before = after++)
{
if (after->at_timestamp_ns > at_timestamp_ns)
{
ex = std::chrono::nanoseconds(at_timestamp_ns - std::min(before->produced_timestamp, after->produced_timestamp));
float t = float(after->at_timestamp_ns - at_timestamp_ns) /
(after->at_timestamp_ns - before->at_timestamp_ns);
return {ex, Derived::interpolate(*before, *after, t)};
}
}
ex = std::chrono::nanoseconds(at_timestamp_ns - data.back().produced_timestamp);
if (extrapolate)
{
auto prev = data.rbegin();
auto last = prev++;
return {ex, Derived::extrapolate(*prev, *last, prev->at_timestamp_ns, prev->at_timestamp_ns, at_timestamp_ns)};
}
else
{
return {ex, data.back()};
}
}
};