Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
video_loss_monitor.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <algorithm>
8#include <atomic>
9#include <cstdint>
10#include <optional>
11#include <string_view>
12
13namespace stream {
14
23 constexpr std::uint16_t frame_fec_status_type = 0x5502;
24
36 public:
38 struct report_t {
39 std::uint32_t frame_index;
42 std::uint16_t total_data;
43 std::uint16_t total_parity;
44 std::uint16_t received_data;
45 std::uint16_t received_parity;
46
48 bool lost() const {
49 return received_data + received_parity < total_data;
50 }
51
64 std::uint32_t missing_packets() const {
65 const std::uint32_t total = total_data + total_parity;
66 const std::uint32_t received = received_data + received_parity;
67 const std::uint32_t unreceived = total > received ? total - received : 0;
68 if (lost()) {
69 return unreceived;
70 }
71 // Data shards precede parity on the wire, so a data shard the block was
72 // rebuilt without is below the highest received and counts as missing.
73 const std::uint32_t data_short = total_data > received_data ? total_data - received_data : 0;
74 return std::min(std::max<std::uint32_t>(missing_before_highest, data_short), unreceived);
75 }
76 };
77
79 static constexpr std::size_t report_size = 4 + 7 * 2 + 3;
80
85 static std::optional<report_t> parse(std::string_view payload) {
86 if (payload.size() < report_size) {
87 return std::nullopt;
88 }
89 const auto *bytes = reinterpret_cast<const unsigned char *>(payload.data());
90 const auto u16 = [bytes](std::size_t at) {
91 return static_cast<std::uint16_t>(bytes[at] << 8 | bytes[at + 1]);
92 };
93 report_t report {
94 .frame_index = static_cast<std::uint32_t>(bytes[0]) << 24 | static_cast<std::uint32_t>(bytes[1]) << 16 |
95 static_cast<std::uint32_t>(bytes[2]) << 8 | bytes[3],
96 // Bytes 4-7: highest received and next contiguous sequence numbers.
97 .missing_before_highest = u16(8),
98 .total_data = u16(10),
99 .total_parity = u16(12),
100 .received_data = u16(14),
101 .received_parity = u16(16),
102 };
103 if (report.total_data == 0) {
104 return std::nullopt;
105 }
106 return report;
107 }
108
110 void on_report(const report_t &report) {
111 reports.fetch_add(1, std::memory_order_relaxed);
112 missing.fetch_add(report.missing_packets(), std::memory_order_relaxed);
113 if (!report.lost()) {
114 repaired.fetch_add(1, std::memory_order_relaxed);
115 return;
116 }
117 // A frame with several lost blocks is one lost frame.
118 if (!last_lost_frame || *last_lost_frame != report.frame_index) {
119 last_lost_frame = report.frame_index;
120 lost_frames.fetch_add(1, std::memory_order_relaxed);
121 }
122 }
123
129 void on_sent(std::uint64_t packets, std::uint64_t data_packets) {
130 sent.fetch_add(packets, std::memory_order_relaxed);
131 sent_data.fetch_add(data_packets, std::memory_order_relaxed);
132 }
133
135 std::uint64_t sent_packets() const {
136 return sent.load(std::memory_order_relaxed);
137 }
138
149 std::uint64_t sent_data_packets() const {
150 return sent_data.load(std::memory_order_relaxed);
151 }
152
154 std::uint64_t repaired_blocks() const {
155 return repaired.load(std::memory_order_relaxed);
156 }
157
159 std::uint64_t dropped_frames() const {
160 return lost_frames.load(std::memory_order_relaxed);
161 }
162
164 std::uint64_t missing_packets() const {
165 return missing.load(std::memory_order_relaxed);
166 }
167
170 std::uint64_t reported_blocks() const {
171 return reports.load(std::memory_order_relaxed);
172 }
173
174 private:
175 std::optional<std::uint32_t> last_lost_frame;
176
177 std::atomic<std::uint64_t> repaired {0};
178 std::atomic<std::uint64_t> lost_frames {0};
179 std::atomic<std::uint64_t> missing {0};
180 std::atomic<std::uint64_t> sent {0};
181 std::atomic<std::uint64_t> sent_data {0};
182 std::atomic<std::uint64_t> reports {0};
183 };
184
185} // namespace stream
void on_report(const report_t &report)
Count one block's report.
std::uint64_t sent_packets() const
Video packets sent to the client, parity included.
static constexpr std::size_t report_size
The size of SS_FRAME_FEC_STATUS: a 32-bit frame index, seven 16-bit counts and three bytes.
static std::optional< report_t > parse(std::string_view payload)
std::uint64_t dropped_frames() const
Frames the client dropped because a block could not be rebuilt.
std::uint64_t repaired_blocks() const
Blocks the client rebuilt from parity: lost packets, no lost picture.
std::uint64_t sent_data_packets() const
std::uint64_t missing_packets() const
Packets missing from the blocks the client reported.
std::uint64_t reported_blocks() const
void on_sent(std::uint64_t packets, std::uint64_t data_packets)
One block's report. The wire format is big-endian.
std::uint16_t missing_before_highest
Packets missing below the highest sequence number received when the report was made.
bool lost() const
Too few packets arrived to rebuild the block, so its frame was dropped.
constexpr std::uint16_t frame_fec_status_type