FreeRDP
Loading...
Searching...
No Matches
TestShadowCodecOptions.c
1#include <stdio.h>
2#include <string.h>
3
4#include <winpr/wtypes.h>
5#include <winpr/cmdline.h>
6#include <freerdp/server/shadow.h>
7
8/* The option as freerdp-shadow-cli has it (server/shadow/cli/shadow.c): the table belongs to the
9 * program, so this tests the parsing of the option, not the program's table. */
10static COMMAND_LINE_ARGUMENT_A options[] = {
11 { "codec", COMMAND_LINE_VALUE_REQUIRED, "<codec>:<key>:<value>[,<codec>:<key>:<value>...]",
12 nullptr, nullptr, -1, nullptr, "Encoder settings" },
13 { nullptr, 0, nullptr, nullptr, nullptr, -1, nullptr, nullptr }
14};
15
16/* The encoder fields of rdpShadowServer, to compare */
17typedef struct
18{
19 H264_RATECONTROL_MODE h264Mode;
20 UINT32 h264BitRate;
21 UINT32 h264QP;
22#if defined(WITH_GFX_AV1)
23 FREERDP_AV1_RATECONTROL av1Mode;
24 UINT32 av1BitRate;
25#endif
26#if defined(WITH_GFX_AV2)
27 FREERDP_AV2_RATECONTROL av2Mode;
28 UINT32 av2BitRate;
29#endif
30#if defined(WITH_GFX_AZURE)
31 FREERDP_HEVC_RATECONTROL hevcMode;
32 UINT32 hevcBitRate;
33#endif
34} FIELDS;
35
36static void get_fields(const rdpShadowServer* server, FIELDS* fields)
37{
38 memset(fields, 0, sizeof(*fields));
39 fields->h264Mode = server->h264RateControlMode;
40 fields->h264BitRate = server->h264BitRate;
41 fields->h264QP = server->h264QP;
42#if defined(WITH_GFX_AV1)
43 fields->av1Mode = server->AV1RateControlMode;
44 fields->av1BitRate = server->AV1BitRate;
45#endif
46#if defined(WITH_GFX_AV2)
47 fields->av2Mode = server->AV2RateControlMode;
48 fields->av2BitRate = server->AV2BitRate;
49#endif
50#if defined(WITH_GFX_AZURE)
51 fields->hevcMode = server->HEVCRateControlMode;
52 fields->hevcBitRate = server->HEVCBitRate;
53#endif
54}
55
56/* Parses option on a new server. Accepted: the fields must be the defaults changed by change().
57 * Refused: the command line must fail and every field keep its default. */
58WINPR_ATTR_NODISCARD
59static BOOL expect(const char* what, const char* option, BOOL accepted, void (*change)(FIELDS*))
60{
61 char* argv[] = { "freerdp-shadow-cli", (char*)option, nullptr };
62 const int argc = option ? 2 : 1;
63
64 rdpShadowServer* server = shadow_server_new();
65 if (!server)
66 {
67 (void)fprintf(stderr, "%s: shadow_server_new failed\n", what);
68 return FALSE;
69 }
70
71 FIELDS expected = { 0 };
72 get_fields(server, &expected);
73 if (accepted && change)
74 change(&expected);
75
76 const int status = shadow_server_parse_command_line(server, argc, argv, options);
77 FIELDS got = { 0 };
78 get_fields(server, &got);
79 shadow_server_free(server);
80
81 if ((status >= 0) != accepted)
82 {
83 (void)fprintf(stderr, "%s: status %d, expected it %s\n", what, status,
84 accepted ? "accepted" : "refused");
85 return FALSE;
86 }
87 if (memcmp(&got, &expected, sizeof(got)) != 0)
88 {
89 (void)fprintf(stderr,
90 "%s: the fields are not as expected (h264 mode %d bit rate %" PRIu32
91 " qp %" PRIu32 ")\n",
92 what, (int)got.h264Mode, got.h264BitRate, got.h264QP);
93 return FALSE;
94 }
95 return TRUE;
96}
97
98static void h264_vbr_4000(FIELDS* f)
99{
100 f->h264Mode = H264_RATECONTROL_VBR;
101 f->h264BitRate = 4000000;
102}
103
104static void h264_cqp_26(FIELDS* f)
105{
106 f->h264Mode = H264_RATECONTROL_CQP;
107 f->h264QP = 26;
108}
109
110static void h264_limits(FIELDS* f)
111{
112 f->h264BitRate = 1000;
113 f->h264QP = 51;
114}
115
116#if defined(WITH_GFX_AV1)
117static void av1_cq_5000(FIELDS* f)
118{
119 f->av1Mode = FREERDP_AV1_CQ;
120 f->av1BitRate = 5000;
121}
122#endif
123
124#if defined(WITH_GFX_AV2)
125static void av2_q_800(FIELDS* f)
126{
127 f->av2Mode = FREERDP_AV2_Q;
128 f->av2BitRate = 800;
129}
130#endif
131
132#if defined(WITH_GFX_AZURE)
133static void hevc_vbr_2000(FIELDS* f)
134{
135 f->hevcMode = FREERDP_HEVC_VBR;
136 f->hevcBitRate = 2000000;
137}
138#endif
139
140static void mixed(FIELDS* f)
141{
142 h264_cqp_26(f);
143#if defined(WITH_GFX_AV1)
144 f->av1BitRate = 3500;
145#endif
146}
147
148int TestShadowCodecOptions(int argc, char* argv[])
149{
150 WINPR_UNUSED(argc);
151 WINPR_UNUSED(argv);
152
153 BOOL ok = TRUE;
154
155 /* none: every default stays */
156 ok &= expect("none", nullptr, TRUE, nullptr);
157
158 /* h264: bit rates in kbit/s, h264BitRate in bit/s */
159 ok &= expect("h264 vbr 4000", "/codec:h264:rate-control:vbr,h264:bitrate:4000", TRUE,
160 h264_vbr_4000);
161 ok &= expect("h264 cqp 26", "/codec:h264:rate-control:cqp,h264:qp:26", TRUE, h264_cqp_26);
162 ok &= expect("h264 limits", "/codec:h264:bitrate:1,h264:qp:51", TRUE, h264_limits);
163 ok &= expect("h264 qp 52", "/codec:h264:qp:52", FALSE, nullptr);
164 ok &= expect("h264 qp -1", "/codec:h264:qp:-1", FALSE, nullptr);
165 ok &= expect("h264 cbr", "/codec:h264:rate-control:cbr", FALSE, nullptr);
166 ok &= expect("h264 bit rate 4M", "/codec:h264:bitrate:4M", FALSE, nullptr);
167 ok &= expect("h264 bit rate 0", "/codec:h264:bitrate:0", FALSE, nullptr);
168 ok &= expect("h264 bit rate too high", "/codec:h264:bitrate:1000001", FALSE, nullptr);
169 ok &= expect("h264 unknown key", "/codec:h264:fps:60", FALSE, nullptr);
170
171 /* the whole value or nothing: a wrong part keeps the right ones from being set */
172 ok &= expect("one wrong part", "/codec:h264:rate-control:cqp,h264:qp:99", FALSE, nullptr);
173 ok &= expect("unknown codec", "/codec:vp9:bitrate:1000", FALSE, nullptr);
174 ok &= expect("no key", "/codec:h264", FALSE, nullptr);
175 ok &= expect("no value", "/codec", FALSE, nullptr);
176
177#if defined(WITH_GFX_AV1)
178 ok &= expect("av1 cq 5000", "/codec:av1:rate-control:cq,av1:bitrate:5000", TRUE, av1_cq_5000);
179 ok &= expect("av1 bad mode", "/codec:av1:rate-control:cqp", FALSE, nullptr);
180 ok &= expect("av1 qp", "/codec:av1:qp:20", FALSE, nullptr);
181 ok &= expect("mixed", "/codec:h264:rate-control:cqp,h264:qp:26,av1:bitrate:3500", TRUE, mixed);
182#else
183 ok &= expect("av1 not built in", "/codec:av1:bitrate:3000", FALSE, nullptr);
184 ok &= expect("mixed", "/codec:h264:rate-control:cqp,h264:qp:26", TRUE, mixed);
185#endif
186
187#if defined(WITH_GFX_AV2)
188 ok &= expect("av2 q 800", "/codec:av2:rate-control:q,av2:bitrate:800", TRUE, av2_q_800);
189 ok &= expect("av2 bad mode", "/codec:av2:rate-control:fast", FALSE, nullptr);
190#else
191 ok &= expect("av2 not built in", "/codec:av2:bitrate:500", FALSE, nullptr);
192#endif
193
194#if defined(WITH_GFX_AZURE)
195 ok &= expect("hevc vbr 2000", "/codec:hevc:rate-control:vbr,hevc:bitrate:2000", TRUE,
196 hevc_vbr_2000);
197 ok &= expect("hevc cbr", "/codec:hevc:rate-control:cbr", FALSE, nullptr);
198#else
199 ok &= expect("hevc not built in", "/codec:hevc:bitrate:500", FALSE, nullptr);
200#endif
201
202 return ok ? 0 : -1;
203}