FreeRDP
Loading...
Searching...
No Matches
TestSDLOverlayModel.cpp
1#include <array>
2#include <cstdio>
3#include <string>
4#include <vector>
5
6#include <freerdp/channels/rdpgfx.h>
7
8#include "../../SDL3/sdl_overlay_model.hpp"
9
13
14/* what AV1's counters are called before the caps version is known */
15#if defined(WITH_GFX_AV2)
16constexpr const char* av1Unknown = "AV1/AV2";
17#else
18constexpr const char* av1Unknown = "AV1";
19#endif
20
21/* the last name repeats the first: the first one counts */
22static std::vector<std::string> statNames()
23{
24 return {
25 "NETWORK_BYTES_RECEIVED", "NETWORK_BYTES_SENT", "NETWORK_BANDWIDTH_KBPS",
26 "NETWORK_RTT_BASE_MS", "NETWORK_RTT_AVERAGE_MS", "GFX_CAPS_VERSION",
27 "RDPGFX_CODECID_UNKNOWN", "RDPGFX_CODECID_AVC420", "RDPGFX_CODECID_AVC420_BYTES",
28 "RDPGFX_CMDID_CACHETOSURFACE", "RDPGFX_CODECID_AV1", "RDPGFX_CODECID_UNKNOWN_BYTES",
29 "FREERDP_CLIENT_STATS_UNUSED", "NETWORK_BYTES_RECEIVED",
30 };
31}
32
33enum
34{
35 RX,
36 TX,
37 KBPS,
38 RTT_BASE,
39 RTT,
40 CAPS,
41 UNKNOWN,
42 AVC420,
43 AVC420_BYTES,
44 CACHE,
45 AV1,
46 UNKNOWN_BYTES,
47 UNUSED,
48 RX_AGAIN,
49 COUNT
50};
51
52static Reading reading(uint64_t seconds)
53{
54 Reading r;
55 r.ns = seconds * 1000000000ull;
56 r.values.resize(COUNT);
57 r.desktopWidth = 1920;
58 r.desktopHeight = 1080;
59 return r;
60}
61
62static const Row* find(const std::vector<Row>& rows, const std::string& label)
63{
64 for (const auto& row : rows)
65 {
66 if (row.label == label)
67 return &row;
68 }
69 return nullptr;
70}
71
72/* the row after the one labelled label */
73static const Row* next(const std::vector<Row>& rows, const std::string& label)
74{
75 for (size_t x = 0; x + 1 < rows.size(); x++)
76 {
77 if (rows.at(x).label == label)
78 return &rows.at(x + 1);
79 }
80 return nullptr;
81}
82
83static bool expect(const Row* row, const std::string& value, int line)
84{
85 if (!row)
86 {
87 (void)fprintf(stderr, "line %d: no row, expected '%s'\n", line, value.c_str());
88 return false;
89 }
90 if (row->value != value)
91 {
92 (void)fprintf(stderr, "line %d: '%s' is '%s', expected '%s'\n", line, row->label.c_str(),
93 row->value.c_str(), value.c_str());
94 return false;
95 }
96 return true;
97}
98
99#define EXPECT(row, value) \
100 if (!expect((row), (value), __LINE__)) \
101 return __LINE__
102#define CHECK(cond) \
103 if (!(cond)) \
104 { \
105 (void)fprintf(stderr, "line %d: %s\n", __LINE__, #cond); \
106 return __LINE__; \
107 }
108
109static int checkNames()
110{
111 const Names n(statNames());
112 auto r = reading(1);
113 r.values.at(RX) = 7;
114 r.values.at(RX_AGAIN) = 99;
115 r.values.at(UNKNOWN) = 5;
116 CHECK(n.get(r, "NETWORK_BYTES_RECEIVED").value_or(0) == 7);
117 CHECK(!n.has("RDPGFX_CODECID_UNKNOWN"));
118 CHECK(!n.has("RDPGFX_CODECID_UNKNOWN_BYTES"));
119 CHECK(!n.has("FREERDP_CLIENT_STATS_UNUSED"));
120 CHECK(!n.get(r, "NO_SUCH_NAME"));
121
122 /* a reading shorter than the names, as from another client */
123 Reading shorter;
124 shorter.values.resize(2);
125 CHECK(!n.get(shorter, "NETWORK_RTT_AVERAGE_MS"));
126 return 0;
127}
128
129static int checkRates()
130{
131 const Names n(statNames());
132 auto prev = reading(10);
133 auto now = reading(12);
134 prev.values.at(RX) = 1000000;
135 now.values.at(RX) = 1500000; /* 250000 bytes a second: 2000 kbit/s */
136 now.values.at(TX) = 2500000;
137 prev.values.at(AVC420) = 100;
138 now.values.at(AVC420) = 160;
139 prev.values.at(AVC420_BYTES) = 0;
140 now.values.at(AVC420_BYTES) = 200000;
141 now.values.at(CACHE) = 8;
142 prev.values.at(CACHE) = 0;
143
144 const sdl::overlay::View view{ 1280, 720, 125 };
145 const auto rows = sdl::overlay::realtimeRows(n, now, &prev, view);
146 EXPECT(find(rows, "Resolution"), "1920 × 1080, window 1280 × 720");
147 EXPECT(find(rows, "Network in (RDP)"), "2000 kbit/s, 1.5 MB this connection");
148 /* without TX in prev: a counter that went up from 0 */
149 EXPECT(find(rows, "Network out (RDP)"), "10000 kbit/s, 2.5 MB this connection");
150 EXPECT(find(rows, "Video codecs"), "H.264 4:2:0 (AVC420) 30/s 800 kbit/s");
151 EXPECT(next(rows, "Video codecs"), "CacheToSurface 4/s");
152 EXPECT(find(rows, "Session"), "2:05");
153
154 /* no earlier reading: no rate */
155 const auto first = sdl::overlay::realtimeRows(n, now, nullptr, view);
156 EXPECT(find(first, "Network in (RDP)"), "– kbit/s, 1.5 MB this connection");
157 EXPECT(find(first, "Video codecs"), "H.264 4:2:0 (AVC420) –/s – kbit/s");
158
159 /* a counter that went down is a new channel, never a negative rate */
160 prev.values.at(AVC420) = 500;
161 const auto reset = sdl::overlay::realtimeRows(n, now, &prev, view);
162 EXPECT(find(reset, "Video codecs"), "H.264 4:2:0 (AVC420) –/s 800 kbit/s");
163 return 0;
164}
165
166static int checkLabels()
167{
168 const Names n(statNames());
169 auto now = reading(1);
170 now.values.at(AV1) = 1;
171
172 /* no caps version yet: AV1's counters can be AV2's */
173 auto rows = sdl::overlay::realtimeRows(n, now, nullptr, {});
174 EXPECT(find(rows, "Video codecs"), std::string(av1Unknown) + " –/s – kbit/s");
175
176#if defined(WITH_GFX_AV2)
177 now.values.at(CAPS) = RDPGFX_CAPVERSION_FRDP_2;
178 rows = sdl::overlay::realtimeRows(n, now, nullptr, {});
179 EXPECT(find(rows, "Video codecs"), "AV2 –/s – kbit/s");
180#endif
181
182 /* any other caps version: AV1 */
183 now.values.at(CAPS) = RDPGFX_CAPVERSION_107;
184 rows = sdl::overlay::realtimeRows(n, now, nullptr, {});
185 EXPECT(find(rows, "Video codecs"), "AV1 –/s – kbit/s");
186
187 /* nothing decoded: no codec rows */
188 const auto empty = sdl::overlay::realtimeRows(n, reading(1), nullptr, {});
189 EXPECT(find(empty, "Video codecs"), "no updates yet");
190 return 0;
191}
192
193static int checkNetwork()
194{
195 const Names n(statNames());
196 auto now = reading(1);
197 auto rows = sdl::overlay::realtimeRows(n, now, nullptr, {});
198 EXPECT(find(rows, "Round trip"), "not measured");
199 EXPECT(find(rows, "Bandwidth"), "not measured");
200
201 now.values.at(RTT) = 23;
202 now.values.at(RTT_BASE) = 11;
203 now.values.at(KBPS) = 54000;
204 rows = sdl::overlay::realtimeRows(n, now, nullptr, {});
205 EXPECT(find(rows, "Round trip"), "23 ms (lowest 11)");
206 EXPECT(find(rows, "Bandwidth"), "54000 kbit/s");
207
208 /* a client without the graphics channel's statistics: no codec row at all */
209 const Names few({ "NETWORK_BYTES_RECEIVED" });
210 rows = sdl::overlay::realtimeRows(few, reading(1), nullptr, {});
211 CHECK(!find(rows, "Video codecs"));
212 CHECK(!find(rows, "Round trip"));
213 return 0;
214}
215
216static int checkClient()
217{
218 const char* config = "Build configuration: WITH_A=ON WITH_B=OFF\n"
219 "Build type: Release\n"
220 "CFLAGS: -O2\n"
221 "Compiler: GNU, 16.2.0\n"
222 "Target architecture: x86_64\n";
223 CHECK(sdl::overlay::buildSummary(config) == "Release, GNU, 16.2.0, x86_64");
224 CHECK(sdl::overlay::buildSummary(nullptr).empty());
225 return 0;
226}
227static bool overlap(const sdl::overlay::Box& a, const sdl::overlay::Box& b)
228{
229 return (a.x < b.x + b.w) && (b.x < a.x + a.w) && (a.y < b.y + b.h) && (b.y < a.y + a.h);
230}
231
232static int checkLayout()
233{
234 /* both fit side by side: the right part at the top right */
235 auto l = sdl::overlay::layout(400, 100, 300, 200, 1280, 12);
236 CHECK(l.left.x == 12);
237 CHECK(l.left.y == 12);
238 CHECK(l.right.x == 1280 - 12 - 300);
239 CHECK(l.right.y == 12);
240
241 /* the 1000 px window of the rig: the right part goes under the left one */
242 l = sdl::overlay::layout(590, 110, 480, 200, 1000, 12);
243 CHECK(l.right.y == 12 + 110 + 12);
244 CHECK(l.right.x == 1000 - 12 - 480);
245
246 /* narrower than the right part: it starts at the left edge, still under the left one */
247 l = sdl::overlay::layout(590, 110, 480, 200, 400, 12);
248 CHECK(l.right.x == 0);
249 CHECK(l.right.y == 134);
250
251 /* no client part: the right part stays at the top */
252 l = sdl::overlay::layout(0, 0, 480, 200, 400, 12);
253 CHECK(l.right.y == 12);
254
255 /* at any width the two never cover each other */
256 for (int w = 50; w <= 3000; w += 7)
257 {
258 const auto width = static_cast<float>(w);
259 for (const auto& sizes : { std::array<float, 4>{ 590, 110, 480, 200 },
260 std::array<float, 4>{ 100, 300, 900, 50 },
261 std::array<float, 4>{ 700, 90, 700, 400 } })
262 {
263 l = sdl::overlay::layout(sizes.at(0), sizes.at(1), sizes.at(2), sizes.at(3), width, 12);
264 if (overlap(l.left, l.right))
265 {
266 (void)fprintf(stderr, "overlap at width %.0f\n", static_cast<double>(width));
267 return __LINE__;
268 }
269 }
270 }
271 return 0;
272}
273
274int TestSDLOverlayModel([[maybe_unused]] int argc, [[maybe_unused]] char* argv[])
275{
276 for (auto* check :
277 { checkNames, checkRates, checkLabels, checkNetwork, checkClient, checkLayout })
278 {
279 const int rc = check();
280 if (rc != 0)
281 return rc;
282 }
283 return 0;
284}
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