FreeRDP
Loading...
Searching...
No Matches
sdl_overlay_model.cpp
1
19
20#include <algorithm>
21#include <array>
22#include <cinttypes>
23#include <cstdio>
24#include <cstring>
25#include <sstream>
26
27#include <freerdp/channels/rdpgfx.h>
28
29#include "sdl_overlay_model.hpp"
30
31namespace sdl::overlay
32{
33 namespace
34 {
35 struct Label
36 {
37 const char* id;
38 const char* label;
39 };
40
41 /* by codec ID, as the web client's Info card names them (codec-stats.js) */
42 constexpr std::array<Label, 13> codecs = { {
43 { "UNCOMPRESSED", "Uncompressed" },
44 { "AV1", "AV1" },
45 { "CAVIDEO", "RemoteFX" },
46 { "CLEARCODEC", "ClearCodec" },
47 { "CAPROGRESSIVE", "Progressive" },
48 { "PLANAR", "Planar" },
49 { "AVC420", "H.264 4:2:0 (AVC420)" },
50 { "ALPHA", "Alpha" },
51 { "AVC444", "H.264 4:4:4 (AVC444)" },
52 { "AVC444v2", "H.264 4:4:4 (AVC444v2)" },
53 { "CAPROGRESSIVE_V2", "Progressive v2" },
54 { "HEVC420", "HEVC 4:2:0" },
55 { "HEVC444", "HEVC 4:4:4" },
56 } };
57
58 /* paints that carry no codec */
59 constexpr std::array<Label, 3> paints = { {
60 { "RDPGFX_CMDID_SOLIDFILL", "SolidFill" },
61 { "RDPGFX_CMDID_SURFACETOSURFACE", "SurfaceToSurface" },
62 { "RDPGFX_CMDID_CACHETOSURFACE", "CacheToSurface" },
63 } };
64
65 constexpr const char* codecPrefix = "RDPGFX_CODECID_";
66 } // namespace
67
68 static bool startsWith(const std::string& s, const char* prefix)
69 {
70 return s.compare(0, strlen(prefix), prefix) == 0;
71 }
72
73 static std::string fixed(double v, int precision)
74 {
75 std::array<char, 64> buffer{};
76 (void)snprintf(buffer.data(), buffer.size(), "%.*f", precision, v);
77 return buffer.data();
78 }
79
80 /* per second since prev; nothing without prev, or for a counter that went down (a new
81 * channel after a reconnect) */
82 static std::optional<double> rate(const Names& names, const Reading& now, const Reading* prev,
83 const std::string& name)
84 {
85 if (!prev || (now.ns <= prev->ns))
86 return std::nullopt;
87 const auto a = names.get(now, name);
88 const auto b = names.get(*prev, name);
89 if (!a || !b || (*a < *b))
90 return std::nullopt;
91 const double dt = static_cast<double>(now.ns - prev->ns) / 1e9;
92 return static_cast<double>(*a - *b) / dt;
93 }
94
95 static std::string per(const std::optional<double>& v, int precision = 1)
96 {
97 return v ? fixed(*v, precision) : "–";
98 }
99
100 /* AV1's counters count AV2 when the server confirmed RDPGFX_CAPVERSION_FRDP_2; without
101 * the caps version the label says both */
102 static const char* av1Label(const Names& names, const Reading& now)
103 {
104#if defined(WITH_GFX_AV2)
105 const auto caps = names.get(now, "GFX_CAPS_VERSION").value_or(0);
106 if (caps == 0)
107 return "AV1/AV2";
108 return (caps == RDPGFX_CAPVERSION_FRDP_2) ? "AV2" : "AV1";
109#else
110 /* a build without AV2 never confirms it */
111 WINPR_UNUSED(names);
112 WINPR_UNUSED(now);
113 return "AV1";
114#endif
115 }
116
117 static void videoRows(std::vector<Row>& rows, const Names& names, const Reading& now,
118 const Reading* prev)
119 {
120 /* without the graphics channel's statistics the row hides */
121 if (!names.has(std::string(codecPrefix) + "AVC420"))
122 return;
123
124 std::vector<std::string> parts;
125 for (const auto& c : codecs)
126 {
127 const std::string name = std::string(codecPrefix) + c.id;
128 if (names.get(now, name).value_or(0) == 0)
129 continue;
130 const std::string label = (strcmp(c.id, "AV1") == 0) ? av1Label(names, now) : c.label;
131 const auto bytes = rate(names, now, prev, name + "_BYTES");
132 const auto kbit = bytes ? std::optional<double>(*bytes * 8.0 / 1000.0) : std::nullopt;
133 parts.push_back(label + " " + per(rate(names, now, prev, name), 0) + "/s " +
134 per(kbit, 0) + " kbit/s");
135 }
136 for (const auto& p : paints)
137 {
138 if (names.get(now, p.id).value_or(0) == 0)
139 continue;
140 parts.push_back(std::string(p.label) + " " + per(rate(names, now, prev, p.id), 0) +
141 "/s");
142 }
143
144 if (parts.empty())
145 {
146 rows.push_back({ "Video codecs", "no updates yet" });
147 return;
148 }
149 for (size_t x = 0; x < parts.size(); x++)
150 rows.push_back({ (x == 0) ? "Video codecs" : "", parts.at(x) });
151 }
152
153 static void networkRows(std::vector<Row>& rows, const Names& names, const Reading& now,
154 const Reading* prev)
155 {
156 const auto bytesRow = [&](const char* label, const char* name)
157 {
158 const auto total = names.get(now, name);
159 if (!total)
160 return;
161 const auto bytes = rate(names, now, prev, name);
162 const auto kbit = bytes ? std::optional<double>(*bytes * 8.0 / 1000.0) : std::nullopt;
163 rows.push_back({ label, per(kbit, 0) + " kbit/s, " +
164 fixed(static_cast<double>(*total) / 1e6, 1) +
165 " MB this connection" });
166 };
167 bytesRow("Network in (RDP)", "NETWORK_BYTES_RECEIVED");
168 bytesRow("Network out (RDP)", "NETWORK_BYTES_SENT");
169
170 /* the server's own measurements, 0 until it makes them: not every server does */
171 if (const auto rtt = names.get(now, "NETWORK_RTT_AVERAGE_MS"))
172 {
173 const auto base = names.get(now, "NETWORK_RTT_BASE_MS").value_or(0);
174 rows.push_back({ "Round trip", (*rtt != 0) ? std::to_string(*rtt) + " ms (lowest " +
175 std::to_string(base) + ")"
176 : "not measured" });
177 }
178 if (const auto kbps = names.get(now, "NETWORK_BANDWIDTH_KBPS"))
179 rows.push_back(
180 { "Bandwidth", (*kbps != 0) ? std::to_string(*kbps) + " kbit/s" : "not measured" });
181 }
182 Names::Names(const std::vector<std::string>& names)
183 {
184 for (size_t x = 0; x < names.size(); x++)
185 {
186 const auto& name = names.at(x);
187 if (name.empty() || (name == "FREERDP_CLIENT_STATS_UNUSED") ||
188 startsWith(name, "RDPGFX_CODECID_UNKNOWN") ||
189 startsWith(name, "RDPGFX_STATS_UNUSED"))
190 continue;
191 _index.emplace(name, x);
192 }
193 }
194
195 std::optional<uint64_t> Names::get(const Reading& r, const std::string& name) const
196 {
197 const auto it = _index.find(name);
198 if ((it == _index.end()) || (it->second >= r.values.size()))
199 return std::nullopt;
200 return r.values.at(it->second);
201 }
202
203 bool Names::has(const std::string& name) const
204 {
205 return _index.find(name) != _index.end();
206 }
207
208 std::vector<Row> realtimeRows(const Names& names, const Reading& now, const Reading* prev,
209 const View& view)
210 {
211 std::vector<Row> rows;
212 rows.push_back({ "Resolution", std::to_string(now.desktopWidth) + " × " +
213 std::to_string(now.desktopHeight) + ", window " +
214 std::to_string(view.windowWidth) + " × " +
215 std::to_string(view.windowHeight) });
216 videoRows(rows, names, now, prev);
217 networkRows(rows, names, now, prev);
218
219 std::array<char, 32> session{};
220 (void)snprintf(session.data(), session.size(), "%" PRIu64 ":%02" PRIu64,
221 view.sessionSeconds / 60, view.sessionSeconds % 60);
222 rows.push_back({ "Session", session.data() });
223 return rows;
224 }
225
226 Layout layout(float leftWidth, float leftHeight, float rightWidth, float rightHeight,
227 float width, float margin)
228 {
229 Layout l;
230 l.left = { margin, margin, leftWidth, leftHeight };
231 l.right = { std::max(0.0F, width - margin - rightWidth), margin, rightWidth, rightHeight };
232
233 const bool hasLeft = (leftWidth > 0) && (leftHeight > 0);
234 if (hasLeft && (margin + leftWidth + margin + rightWidth + margin > width))
235 l.right.y = margin + leftHeight + margin;
236 return l;
237 }
238
239 std::string buildSummary(const char* config)
240 {
241 if (!config)
242 return "";
243
244 const std::array<const char*, 3> keys = { "Build type:", "Compiler:",
245 "Target architecture:" };
246 std::vector<std::string> parts;
247 std::istringstream lines(config);
248 std::string line;
249 while (std::getline(lines, line))
250 {
251 for (const auto* key : keys)
252 {
253 if (!startsWith(line, key))
254 continue;
255 auto value = line.substr(strlen(key));
256 const auto start = value.find_first_not_of(' ');
257 if (start != std::string::npos)
258 parts.push_back(value.substr(start));
259 }
260 }
261
262 std::string summary;
263 for (const auto& part : parts)
264 summary += summary.empty() ? part : ", " + part;
265 return summary;
266 }
267} // namespace sdl::overlay
std::optional< uint64_t > get(const Reading &r, const std::string &name) const
Layout layout(float leftWidth, float leftHeight, float rightWidth, float rightHeight, float width, float margin)
std::vector< Row > realtimeRows(const Names &names, const Reading &now, const Reading *prev, const View &view)
std::string buildSummary(const char *config)
std::vector< uint64_t > values