From a733e697968cd5a03c3810d0ca6e327e3f7b720f Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Tue, 13 Feb 2024 21:31:32 +0100 Subject: [PATCH] Send encode timings --- common/wivrn_packets.h | 11 ++++++++++- server/encoder/video_encoder.cpp | 12 +++++++++++- server/encoder/video_encoder.h | 4 ++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/common/wivrn_packets.h b/common/wivrn_packets.h index 117211ae..3a8fbeb8 100644 --- a/common/wivrn_packets.h +++ b/common/wivrn_packets.h @@ -257,7 +257,7 @@ public: uint16_t shard_idx; uint8_t flags; - // Position information, must be present on last video shard + // Position information, must be present on first video shard struct view_info_t { // ns in headset time referential @@ -267,6 +267,15 @@ public: std::array fov; }; std::optional view_info; + + // Information about timing, on last video shard + struct timing_info_t + { + uint64_t encode_begin; + uint64_t send_begin; + uint64_t send_end; + }; + std::optional timing_info; // Actual video data, may contain multiple NAL units std::span payload; diff --git a/server/encoder/video_encoder.cpp b/server/encoder/video_encoder.cpp index bfc4e269..49fe1e4c 100644 --- a/server/encoder/video_encoder.cpp +++ b/server/encoder/video_encoder.cpp @@ -104,7 +104,9 @@ void VideoEncoder::Encode(wivrn_session & cnx, this->cnx = &cnx; auto target_timestamp = std::chrono::steady_clock::time_point(std::chrono::nanoseconds(view_info.display_time)); bool idr = sync_needed.exchange(false); - const char* extra = idr ? ",idr" : ",p"; + const char * extra = idr ? ",idr" : ",p"; + clock = cnx.get_offset(); + timing_info.encode_begin = clock.to_headset(os_monotonic_get_ns()).count(); cnx.dump_time("encode_begin", frame_index, os_monotonic_get_ns(), stream_idx, extra); // Prepare the video shard template @@ -120,10 +122,15 @@ void VideoEncoder::Encode(wivrn_session & cnx, void VideoEncoder::SendData(std::span data, bool end_of_frame) { std::lock_guard lock(mutex); + if (end_of_frame) + timing_info.send_end = clock.to_headset(os_monotonic_get_ns()).count(); if (video_dump) video_dump.write((char*)data.data(), data.size()); if (shard.shard_idx == 0) + { 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(); + } shard.flags = to_headset::video_stream_data_shard::start_of_slice; auto begin = data.begin(); @@ -137,7 +144,10 @@ void VideoEncoder::SendData(std::span data, bool end_of_frame) { shard.flags |= to_headset::video_stream_data_shard::end_of_slice; if (end_of_frame) + { shard.flags |= to_headset::video_stream_data_shard::end_of_frame; + shard.timing_info = timing_info; + } } shard.payload = {begin, next}; cnx->send_stream(shard); diff --git a/server/encoder/video_encoder.h b/server/encoder/video_encoder.h index 3140691b..05b3349b 100644 --- a/server/encoder/video_encoder.h +++ b/server/encoder/video_encoder.h @@ -21,6 +21,7 @@ #include "vk/vk_cmd_pool.h" #include "vk/vk_helpers.h" +#include "driver/clock_offset.h" #include #include #include @@ -54,6 +55,9 @@ private: // shard to send to_headset::video_stream_data_shard shard; + to_headset::video_stream_data_shard::timing_info_t timing_info; + clock_offset clock; + std::atomic_bool sync_needed = true; std::ofstream video_dump;