Use XrTime for all time data

This commit is contained in:
Patrick Nicolas
2024-03-24 14:01:44 +01:00
parent f951034de4
commit 8e3979cc8c
11 changed files with 47 additions and 63 deletions
+1 -1
View File
@@ -104,7 +104,7 @@ int32_t wivrn::android::audio::microphone_data_cb(AAudioStream * stream, void *
size_t frame_size = AAudioStream_getChannelCount(stream) * sizeof(uint16_t); size_t frame_size = AAudioStream_getChannelCount(stream) * sizeof(uint16_t);
xrt::drivers::wivrn::audio_data packet{ xrt::drivers::wivrn::audio_data packet{
.timestamp = uint64_t(self->instance.now()), .timestamp = self->instance.now(),
.payload = std::span(audio_data, frame_size * num_frames), .payload = std::span(audio_data, frame_size * num_frames),
}; };
+4 -4
View File
@@ -32,14 +32,14 @@ void scenes::stream::read_actions()
case XR_ACTION_TYPE_BOOLEAN_INPUT: { case XR_ACTION_TYPE_BOOLEAN_INPUT: {
auto value = application::read_action_bool(action); auto value = application::read_action_bool(action);
if (value) if (value)
inputs.values.push_back({id, (float)value->second, (uint64_t)value->first}); inputs.values.push_back({id, (float)value->second, value->first});
} }
break; break;
case XR_ACTION_TYPE_FLOAT_INPUT: { case XR_ACTION_TYPE_FLOAT_INPUT: {
auto value = application::read_action_float(action); auto value = application::read_action_float(action);
if (value) if (value)
inputs.values.push_back({id, value->second, (uint64_t)value->first}); inputs.values.push_back({id, value->second, value->first});
} }
break; break;
@@ -47,8 +47,8 @@ void scenes::stream::read_actions()
auto value = application::read_action_vec2(action); auto value = application::read_action_vec2(action);
if (value) if (value)
{ {
inputs.values.push_back({id, value->second.x, (uint64_t)value->first}); inputs.values.push_back({id, value->second.x, value->first});
inputs.values.push_back({(device_id)((int)id + 1), value->second.y, (uint64_t)value->first}); inputs.values.push_back({(device_id)((int)id + 1), value->second.y, value->first});
} }
} }
break; break;
+1 -1
View File
@@ -102,7 +102,7 @@ void scenes::stream::accumulate_metrics(XrTime predicted_display_time, const std
if (decoder_metrics.size() != blit_handles.size()) if (decoder_metrics.size() != blit_handles.size())
decoder_metrics.resize(blit_handles.size()); decoder_metrics.resize(blit_handles.size());
uint64_t min_encode_begin = std::numeric_limits<uint64_t>::max(); auto min_encode_begin = std::numeric_limits<decltype(blit_handles[0]->timing_info.encode_begin)>::max();
for(const auto& bh: blit_handles) for(const auto& bh: blit_handles)
min_encode_begin = std::min(min_encode_begin, bh->timing_info.encode_begin); min_encode_begin = std::min(min_encode_begin, bh->timing_info.encode_begin);
+17 -17
View File
@@ -84,7 +84,7 @@ enum video_codec
struct audio_data struct audio_data
{ {
uint64_t timestamp; XrTime timestamp;
std::span<uint8_t> payload; std::span<uint8_t> payload;
data_holder data; data_holder data;
}; };
@@ -141,7 +141,7 @@ struct tracking
XrFovf fov; XrFovf fov;
}; };
uint64_t timestamp; XrTime timestamp;
XrViewStateFlags flags; XrViewStateFlags flags;
std::array<view, 2> views; std::array<view, 2> views;
@@ -169,7 +169,7 @@ struct hand_tracking
uint8_t flags; uint8_t flags;
}; };
uint64_t timestamp; XrTime timestamp;
std::array<pose, XR_HAND_JOINT_COUNT_EXT> left; std::array<pose, XR_HAND_JOINT_COUNT_EXT> left;
std::array<pose, XR_HAND_JOINT_COUNT_EXT> right; std::array<pose, XR_HAND_JOINT_COUNT_EXT> right;
}; };
@@ -180,15 +180,15 @@ struct inputs
{ {
device_id id; device_id id;
float value; float value;
uint64_t last_change_time; XrTime last_change_time;
}; };
std::vector<input_value> values; std::vector<input_value> values;
}; };
struct timesync_response struct timesync_response
{ {
std::chrono::nanoseconds query; XrTime query;
uint64_t response; XrTime response;
}; };
struct feedback struct feedback
@@ -197,12 +197,12 @@ struct feedback
uint8_t stream_index; uint8_t stream_index;
// Timestamps // Timestamps
uint64_t received_first_packet; XrTime received_first_packet;
uint64_t received_last_packet; XrTime received_last_packet;
uint64_t sent_to_decoder; XrTime sent_to_decoder;
uint64_t received_from_decoder; XrTime received_from_decoder;
uint64_t blitted; XrTime blitted;
uint64_t displayed; XrTime displayed;
std::array<XrPosef, 2> received_pose; std::array<XrPosef, 2> received_pose;
std::array<XrPosef, 2> real_pose; std::array<XrPosef, 2> real_pose;
@@ -286,7 +286,7 @@ public:
struct view_info_t struct view_info_t
{ {
// ns in headset time referential // ns in headset time referential
uint64_t display_time; XrTime display_time;
std::array<XrPosef, 2> pose; std::array<XrPosef, 2> pose;
std::array<XrFovf, 2> fov; std::array<XrFovf, 2> fov;
@@ -296,9 +296,9 @@ public:
// Information about timing, on last video shard // Information about timing, on last video shard
struct timing_info_t struct timing_info_t
{ {
uint64_t encode_begin; XrTime encode_begin;
uint64_t send_begin; XrTime send_begin;
uint64_t send_end; XrTime send_end;
}; };
std::optional<timing_info_t> timing_info; std::optional<timing_info_t> timing_info;
// Actual video data, may contain multiple NAL units // Actual video data, may contain multiple NAL units
@@ -318,7 +318,7 @@ struct haptics
struct timesync_query struct timesync_query
{ {
std::chrono::nanoseconds query; XrTime query;
}; };
using packets = std::variant<handshake, audio_stream_description, video_stream_description, audio_data, video_stream_data_shard, haptics, timesync_query>; using packets = std::variant<handshake, audio_stream_description, video_stream_description, audio_data, video_stream_data_shard, haptics, timesync_query>;
+1 -1
View File
@@ -260,7 +260,7 @@ void pipewire_device::speaker_process(void * self_v)
return; return;
audio_data packet{ audio_data packet{
.timestamp = uint64_t(self->session.get_offset().to_headset(os_monotonic_get_ns()).count()), .timestamp = self->session.get_offset().to_headset(os_monotonic_get_ns()),
.payload = std::span( .payload = std::span(
(uint8_t *)data.data + data.chunk->offset, (uint8_t *)data.data + data.chunk->offset,
data.chunk->size), data.chunk->size),
+1 -1
View File
@@ -336,7 +336,7 @@ struct pulse_device : public audio_device
remainder = size % sample_size; // data to keep for next iteration remainder = size % sample_size; // data to keep for next iteration
size -= remainder; // size of data to send size -= remainder; // size of data to send
packet.payload = std::span<uint8_t>(buffer.begin(), size); packet.payload = std::span<uint8_t>(buffer.begin(), size);
packet.timestamp = session.get_offset().to_headset(os_monotonic_get_ns()).count(); packet.timestamp = session.get_offset().to_headset(os_monotonic_get_ns());
session.send_stream(packet); session.send_stream(packet);
// put the remaining data at the beginning of the buffer // put the remaining data at the beginning of the buffer
memmove(buffer.data(), buffer.data() + size, remainder); memmove(buffer.data(), buffer.data() + size, remainder);
+15 -24
View File
@@ -31,14 +31,15 @@ void clock_offset_estimator::request_sample(wivrn_connection & connection)
return; return;
next_sample = std::chrono::steady_clock::now() + sample_interval.load(); next_sample = std::chrono::steady_clock::now() + sample_interval.load();
xrt::drivers::wivrn::to_headset::timesync_query timesync{}; connection.send_stream(
timesync.query = std::chrono::nanoseconds(os_monotonic_get_ns()); xrt::drivers::wivrn::to_headset::timesync_query{
connection.send_stream(timesync); .query = XrTime(os_monotonic_get_ns()),
});
} }
void clock_offset_estimator::add_sample(const xrt::drivers::wivrn::from_headset::timesync_response & base_sample) void clock_offset_estimator::add_sample(const xrt::drivers::wivrn::from_headset::timesync_response & base_sample)
{ {
auto now = std::chrono::nanoseconds(os_monotonic_get_ns()); XrTime now = os_monotonic_get_ns();
clock_offset_estimator::sample sample{base_sample, now}; clock_offset_estimator::sample sample{base_sample, now};
std::lock_guard lock(mutex); std::lock_guard lock(mutex);
if (samples.size() < num_samples) if (samples.size() < num_samples)
@@ -50,12 +51,12 @@ void clock_offset_estimator::add_sample(const xrt::drivers::wivrn::from_headset:
sample_interval = std::chrono::seconds(1); sample_interval = std::chrono::seconds(1);
int64_t latency = 0; int64_t latency = 0;
for (const auto& s: samples) for (const auto& s: samples)
latency += (s.received - s.query).count(); latency += s.received - s.query;
latency /= samples.size(); latency /= samples.size();
// packets with too high latency are likely to be retransmitted // packets with too high latency are likely to be retransmitted
if ((sample.received - sample.query).count() > 3 * latency) if (sample.received - sample.query > 3 * latency)
{ {
U_LOG_D("drop packet for latency %ld > %ld", (sample.received - sample.query).count() /1000, latency/1000); U_LOG_D("drop packet for latency %ldµs > %ldµs", (sample.received - sample.query) /1000, latency/1000);
return; return;
} }
@@ -74,7 +75,7 @@ void clock_offset_estimator::add_sample(const xrt::drivers::wivrn::from_headset:
double y0 = 0; double y0 = 0;
for (const auto & s: samples) for (const auto & s: samples)
{ {
x0 += (s.query + s.received).count() * 0.5; x0 += (s.query + s.received) * 0.5;
y0 += s.response; y0 += s.response;
} }
x0 /= n; x0 /= n;
@@ -95,10 +96,10 @@ void clock_offset_estimator::add_sample(const xrt::drivers::wivrn::from_headset:
{ {
#if 1 #if 1
// assume symmetrical latency // assume symmetrical latency
double x = (s.query + s.received).count() * 0.5 - x0; double x = (s.query + s.received)* 0.5 - x0;
#else #else
// assume latency is only on server -> headset link // assume latency is only on server -> headset link
double x = (s.received).count() - x0; double x = s.received - x0;
#endif #endif
double y = s.response - y0; double y = s.response - y0;
sum_x += x; sum_x += x;
@@ -126,22 +127,12 @@ clock_offset clock_offset_estimator::get_offset()
return offset; return offset;
} }
int64_t clock_offset::from_headset(uint64_t ts) const XrTime clock_offset::from_headset(XrTime ts) const
{ {
int64_t res = (int64_t(ts) - b) / a; return (ts - b) / a;
#ifndef NDEBUG
if (res < 0)
U_LOG_W("negative from_headset: %ld", res);
#endif
return res;
} }
std::chrono::nanoseconds clock_offset::to_headset(uint64_t timestamp_ns) const XrTime clock_offset::to_headset(XrTime timestamp_ns) const
{ {
int64_t res = int64_t(a * timestamp_ns) + b; return a * timestamp_ns + b;
#ifndef NDEBUG
if (res < 0)
U_LOG_W("negative to_headset: %ld", res);
#endif
return std::chrono::nanoseconds(res);
} }
+3 -4
View File
@@ -38,17 +38,16 @@ struct clock_offset
operator bool() { return b != 0;} operator bool() { return b != 0;}
int64_t from_headset(uint64_t) const; XrTime from_headset(XrTime) const;
std::chrono::nanoseconds XrTime to_headset(XrTime timestamp_ns) const;
to_headset(uint64_t timestamp_ns) const;
}; };
class clock_offset_estimator class clock_offset_estimator
{ {
struct sample: public xrt::drivers::wivrn::from_headset::timesync_response struct sample: public xrt::drivers::wivrn::from_headset::timesync_response
{ {
std::chrono::nanoseconds received; XrTime received;
}; };
std::mutex mutex; std::mutex mutex;
+1 -1
View File
@@ -537,7 +537,7 @@ static VkResult comp_wivrn_present(struct comp_target * ct,
cn->psc.images[index].frame_index = cn->current_frame_id; cn->psc.images[index].frame_index = cn->current_frame_id;
auto & view_info = cn->psc.images[index].view_info; auto & view_info = cn->psc.images[index].view_info;
view_info.display_time = cn->cnx->get_offset().to_headset(desired_present_time_ns).count(); view_info.display_time = cn->cnx->get_offset().to_headset(desired_present_time_ns);
for (int eye = 0; eye < 2; ++eye) for (int eye = 0; eye < 2; ++eye)
{ {
const auto &slot = cn->c->base.slot; const auto &slot = cn->c->base.slot;
-6
View File
@@ -211,12 +211,6 @@ void wivrn_session::operator()(from_headset::inputs && inputs)
right_hand->set_inputs(inputs, offset); right_hand->set_inputs(inputs, offset);
} }
template <typename Rep, typename Period>
static auto lerp(std::chrono::duration<Rep, Period> a, std::chrono::duration<Rep, Period> b, double t)
{
return std::chrono::duration<Rep, Period>(Rep(std::lerp(a.count(), b.count(), t)));
}
void wivrn_session::operator()(from_headset::timesync_response && timesync) void wivrn_session::operator()(from_headset::timesync_response && timesync)
{ {
offset_est.add_sample(timesync); offset_est.add_sample(timesync);
+3 -3
View File
@@ -105,7 +105,7 @@ void VideoEncoder::Encode(wivrn_session & cnx,
bool idr = sync_needed.exchange(false); bool idr = sync_needed.exchange(false);
const char * extra = idr ? ",idr" : ",p"; const char * extra = idr ? ",idr" : ",p";
clock = cnx.get_offset(); clock = cnx.get_offset();
timing_info.encode_begin = clock.to_headset(os_monotonic_get_ns()).count(); timing_info.encode_begin = clock.to_headset(os_monotonic_get_ns());
cnx.dump_time("encode_begin", frame_index, os_monotonic_get_ns(), stream_idx, extra); cnx.dump_time("encode_begin", frame_index, os_monotonic_get_ns(), stream_idx, extra);
// Prepare the video shard template // Prepare the video shard template
@@ -122,13 +122,13 @@ void VideoEncoder::SendData(std::span<uint8_t> data, bool end_of_frame)
{ {
std::lock_guard lock(mutex); std::lock_guard lock(mutex);
if (end_of_frame) if (end_of_frame)
timing_info.send_end = clock.to_headset(os_monotonic_get_ns()).count(); timing_info.send_end = clock.to_headset(os_monotonic_get_ns());
if (video_dump) if (video_dump)
video_dump.write((char*)data.data(), data.size()); video_dump.write((char*)data.data(), data.size());
if (shard.shard_idx == 0) if (shard.shard_idx == 0)
{ {
cnx->dump_time("send_begin", shard.frame_idx, os_monotonic_get_ns(), stream_idx); cnx->dump_time("send_begin", shard.frame_idx, os_monotonic_get_ns(), stream_idx);
timing_info.send_begin = clock.to_headset(os_monotonic_get_ns()).count(); timing_info.send_begin = clock.to_headset(os_monotonic_get_ns());
} }
shard.flags = to_headset::video_stream_data_shard::start_of_slice; shard.flags = to_headset::video_stream_data_shard::start_of_slice;