1 /*
2  *  Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include <stdio.h>
12 
13 #include <memory>
14 #include <string>
15 #include <vector>
16 
17 #include "absl/flags/flag.h"
18 #include "absl/flags/parse.h"
19 #include "absl/types/optional.h"
20 #include "api/test/simulated_network.h"
21 #include "api/test/video_quality_test_fixture.h"
22 #include "api/transport/bitrate_settings.h"
23 #include "api/video_codecs/video_codec.h"
24 #include "rtc_base/checks.h"
25 #include "rtc_base/logging.h"
26 #include "rtc_base/string_encode.h"
27 #include "system_wrappers/include/field_trial.h"
28 #include "test/field_trial.h"
29 #include "test/gtest.h"
30 #include "test/run_test.h"
31 #include "video/video_quality_test.h"
32 
33 using ::webrtc::BitrateConstraints;
34 using ::webrtc::BuiltInNetworkBehaviorConfig;
35 using ::webrtc::InterLayerPredMode;
36 using ::webrtc::SdpVideoFormat;
37 using ::webrtc::VideoQualityTest;
38 
39 // Flags common with video loopback, with different default values.
40 ABSL_FLAG(int, width, 1850, "Video width (crops source).");
Width()41 size_t Width() {
42   return static_cast<size_t>(absl::GetFlag(FLAGS_width));
43 }
44 
45 ABSL_FLAG(int, height, 1110, "Video height (crops source).");
Height()46 size_t Height() {
47   return static_cast<size_t>(absl::GetFlag(FLAGS_height));
48 }
49 
50 ABSL_FLAG(int, fps, 5, "Frames per second.");
Fps()51 int Fps() {
52   return absl::GetFlag(FLAGS_fps);
53 }
54 
55 ABSL_FLAG(int, min_bitrate, 50, "Call and stream min bitrate in kbps.");
MinBitrateKbps()56 int MinBitrateKbps() {
57   return absl::GetFlag(FLAGS_min_bitrate);
58 }
59 
60 ABSL_FLAG(int, start_bitrate, 300, "Call start bitrate in kbps.");
StartBitrateKbps()61 int StartBitrateKbps() {
62   return absl::GetFlag(FLAGS_start_bitrate);
63 }
64 
65 ABSL_FLAG(int, target_bitrate, 200, "Stream target bitrate in kbps.");
TargetBitrateKbps()66 int TargetBitrateKbps() {
67   return absl::GetFlag(FLAGS_target_bitrate);
68 }
69 
70 ABSL_FLAG(int, max_bitrate, 1000, "Call and stream max bitrate in kbps.");
MaxBitrateKbps()71 int MaxBitrateKbps() {
72   return absl::GetFlag(FLAGS_max_bitrate);
73 }
74 
75 ABSL_FLAG(int, num_temporal_layers, 2, "Number of temporal layers to use.");
NumTemporalLayers()76 int NumTemporalLayers() {
77   return absl::GetFlag(FLAGS_num_temporal_layers);
78 }
79 
80 // Flags common with video loopback, with equal default values.
81 ABSL_FLAG(std::string, codec, "VP8", "Video codec to use.");
Codec()82 std::string Codec() {
83   return absl::GetFlag(FLAGS_codec);
84 }
85 
86 ABSL_FLAG(std::string,
87           rtc_event_log_name,
88           "",
89           "Filename for rtc event log. Two files "
90           "with \"_send\" and \"_recv\" suffixes will be created.");
RtcEventLogName()91 std::string RtcEventLogName() {
92   return absl::GetFlag(FLAGS_rtc_event_log_name);
93 }
94 
95 ABSL_FLAG(std::string,
96           rtp_dump_name,
97           "",
98           "Filename for dumped received RTP stream.");
RtpDumpName()99 std::string RtpDumpName() {
100   return absl::GetFlag(FLAGS_rtp_dump_name);
101 }
102 
103 ABSL_FLAG(int,
104           selected_tl,
105           -1,
106           "Temporal layer to show or analyze. -1 to disable filtering.");
SelectedTL()107 int SelectedTL() {
108   return absl::GetFlag(FLAGS_selected_tl);
109 }
110 
111 ABSL_FLAG(
112     int,
113     duration,
114     0,
115     "Duration of the test in seconds. If 0, rendered will be shown instead.");
DurationSecs()116 int DurationSecs() {
117   return absl::GetFlag(FLAGS_duration);
118 }
119 
120 ABSL_FLAG(std::string, output_filename, "", "Target graph data filename.");
OutputFilename()121 std::string OutputFilename() {
122   return absl::GetFlag(FLAGS_output_filename);
123 }
124 
125 ABSL_FLAG(std::string,
126           graph_title,
127           "",
128           "If empty, title will be generated automatically.");
GraphTitle()129 std::string GraphTitle() {
130   return absl::GetFlag(FLAGS_graph_title);
131 }
132 
133 ABSL_FLAG(int, loss_percent, 0, "Percentage of packets randomly lost.");
LossPercent()134 int LossPercent() {
135   return absl::GetFlag(FLAGS_loss_percent);
136 }
137 
138 ABSL_FLAG(int,
139           link_capacity,
140           0,
141           "Capacity (kbps) of the fake link. 0 means infinite.");
LinkCapacityKbps()142 int LinkCapacityKbps() {
143   return absl::GetFlag(FLAGS_link_capacity);
144 }
145 
146 ABSL_FLAG(int, queue_size, 0, "Size of the bottleneck link queue in packets.");
QueueSize()147 int QueueSize() {
148   return absl::GetFlag(FLAGS_queue_size);
149 }
150 
151 ABSL_FLAG(int,
152           avg_propagation_delay_ms,
153           0,
154           "Average link propagation delay in ms.");
AvgPropagationDelayMs()155 int AvgPropagationDelayMs() {
156   return absl::GetFlag(FLAGS_avg_propagation_delay_ms);
157 }
158 
159 ABSL_FLAG(int,
160           std_propagation_delay_ms,
161           0,
162           "Link propagation delay standard deviation in ms.");
StdPropagationDelayMs()163 int StdPropagationDelayMs() {
164   return absl::GetFlag(FLAGS_std_propagation_delay_ms);
165 }
166 
167 ABSL_FLAG(int, num_streams, 0, "Number of streams to show or analyze.");
NumStreams()168 int NumStreams() {
169   return absl::GetFlag(FLAGS_num_streams);
170 }
171 
172 ABSL_FLAG(int,
173           selected_stream,
174           0,
175           "ID of the stream to show or analyze. "
176           "Set to the number of streams to show them all.");
SelectedStream()177 int SelectedStream() {
178   return absl::GetFlag(FLAGS_selected_stream);
179 }
180 
181 ABSL_FLAG(int, num_spatial_layers, 1, "Number of spatial layers to use.");
NumSpatialLayers()182 int NumSpatialLayers() {
183   return absl::GetFlag(FLAGS_num_spatial_layers);
184 }
185 
186 ABSL_FLAG(int,
187           inter_layer_pred,
188           0,
189           "Inter-layer prediction mode. "
190           "0 - enabled, 1 - disabled, 2 - enabled only for key pictures.");
InterLayerPred()191 InterLayerPredMode InterLayerPred() {
192   if (absl::GetFlag(FLAGS_inter_layer_pred) == 0) {
193     return webrtc::InterLayerPredMode::kOn;
194   } else if (absl::GetFlag(FLAGS_inter_layer_pred) == 1) {
195     return webrtc::InterLayerPredMode::kOff;
196   } else {
197     RTC_DCHECK_EQ(absl::GetFlag(FLAGS_inter_layer_pred), 2);
198     return webrtc::InterLayerPredMode::kOnKeyPic;
199   }
200 }
201 
202 ABSL_FLAG(int,
203           selected_sl,
204           -1,
205           "Spatial layer to show or analyze. -1 to disable filtering.");
SelectedSL()206 int SelectedSL() {
207   return absl::GetFlag(FLAGS_selected_sl);
208 }
209 
210 ABSL_FLAG(std::string,
211           stream0,
212           "",
213           "Comma separated values describing VideoStream for stream #0.");
Stream0()214 std::string Stream0() {
215   return absl::GetFlag(FLAGS_stream0);
216 }
217 
218 ABSL_FLAG(std::string,
219           stream1,
220           "",
221           "Comma separated values describing VideoStream for stream #1.");
Stream1()222 std::string Stream1() {
223   return absl::GetFlag(FLAGS_stream1);
224 }
225 
226 ABSL_FLAG(std::string,
227           sl0,
228           "",
229           "Comma separated values describing SpatialLayer for layer #0.");
SL0()230 std::string SL0() {
231   return absl::GetFlag(FLAGS_sl0);
232 }
233 
234 ABSL_FLAG(std::string,
235           sl1,
236           "",
237           "Comma separated values describing SpatialLayer for layer #1.");
SL1()238 std::string SL1() {
239   return absl::GetFlag(FLAGS_sl1);
240 }
241 
242 ABSL_FLAG(std::string,
243           encoded_frame_path,
244           "",
245           "The base path for encoded frame logs. Created files will have "
246           "the form <encoded_frame_path>.<n>.(recv|send.<m>).ivf");
EncodedFramePath()247 std::string EncodedFramePath() {
248   return absl::GetFlag(FLAGS_encoded_frame_path);
249 }
250 
251 ABSL_FLAG(bool, logs, false, "print logs to stderr");
252 
253 ABSL_FLAG(bool, send_side_bwe, true, "Use send-side bandwidth estimation");
254 
255 ABSL_FLAG(bool, generic_descriptor, false, "Use the generic frame descriptor.");
256 
257 ABSL_FLAG(bool, allow_reordering, false, "Allow packet reordering to occur");
258 
259 ABSL_FLAG(
260     std::string,
261     force_fieldtrials,
262     "",
263     "Field trials control experimental feature code which can be forced. "
264     "E.g. running with --force_fieldtrials=WebRTC-FooFeature/Enable/"
265     " will assign the group Enable to field trial WebRTC-FooFeature. Multiple "
266     "trials are separated by \"/\"");
267 
268 // Screenshare-specific flags.
269 ABSL_FLAG(int,
270           min_transmit_bitrate,
271           400,
272           "Min transmit bitrate incl. padding.");
MinTransmitBitrateKbps()273 int MinTransmitBitrateKbps() {
274   return absl::GetFlag(FLAGS_min_transmit_bitrate);
275 }
276 
277 ABSL_FLAG(bool,
278           generate_slides,
279           false,
280           "Whether to use randomly generated slides or read them from files.");
GenerateSlides()281 bool GenerateSlides() {
282   return absl::GetFlag(FLAGS_generate_slides);
283 }
284 
285 ABSL_FLAG(int,
286           slide_change_interval,
287           10,
288           "Interval (in seconds) between simulated slide changes.");
SlideChangeInterval()289 int SlideChangeInterval() {
290   return absl::GetFlag(FLAGS_slide_change_interval);
291 }
292 
293 ABSL_FLAG(
294     int,
295     scroll_duration,
296     0,
297     "Duration (in seconds) during which a slide will be scrolled into place.");
ScrollDuration()298 int ScrollDuration() {
299   return absl::GetFlag(FLAGS_scroll_duration);
300 }
301 
302 ABSL_FLAG(std::string,
303           slides,
304           "",
305           "Comma-separated list of *.yuv files to display as slides.");
Slides()306 std::vector<std::string> Slides() {
307   std::vector<std::string> slides;
308   std::string slides_list = absl::GetFlag(FLAGS_slides);
309   rtc::tokenize(slides_list, ',', &slides);
310   return slides;
311 }
312 
Loopback()313 void Loopback() {
314   BuiltInNetworkBehaviorConfig pipe_config;
315   pipe_config.loss_percent = LossPercent();
316   pipe_config.link_capacity_kbps = LinkCapacityKbps();
317   pipe_config.queue_length_packets = QueueSize();
318   pipe_config.queue_delay_ms = AvgPropagationDelayMs();
319   pipe_config.delay_standard_deviation_ms = StdPropagationDelayMs();
320   pipe_config.allow_reordering = absl::GetFlag(FLAGS_allow_reordering);
321 
322   BitrateConstraints call_bitrate_config;
323   call_bitrate_config.min_bitrate_bps = MinBitrateKbps() * 1000;
324   call_bitrate_config.start_bitrate_bps = StartBitrateKbps() * 1000;
325   call_bitrate_config.max_bitrate_bps = -1;  // Don't cap bandwidth estimate.
326 
327   VideoQualityTest::Params params;
328   params.call.send_side_bwe = absl::GetFlag(FLAGS_send_side_bwe);
329   params.call.generic_descriptor = absl::GetFlag(FLAGS_generic_descriptor);
330   params.call.call_bitrate_config = call_bitrate_config;
331   params.video[0].enabled = true;
332   params.video[0].width = Width();
333   params.video[0].height = Height();
334   params.video[0].fps = Fps();
335   params.video[0].min_bitrate_bps = MinBitrateKbps() * 1000;
336   params.video[0].target_bitrate_bps = TargetBitrateKbps() * 1000;
337   params.video[0].max_bitrate_bps = MaxBitrateKbps() * 1000;
338   params.video[0].codec = Codec();
339   params.video[0].num_temporal_layers = NumTemporalLayers();
340   params.video[0].selected_tl = SelectedTL();
341   params.video[0].min_transmit_bps = MinTransmitBitrateKbps() * 1000;
342   params.screenshare[0].enabled = true;
343   params.screenshare[0].generate_slides = GenerateSlides();
344   params.screenshare[0].slide_change_interval = SlideChangeInterval();
345   params.screenshare[0].scroll_duration = ScrollDuration();
346   params.screenshare[0].slides = Slides();
347   params.config = pipe_config;
348   params.logging.rtc_event_log_name = RtcEventLogName();
349   params.logging.rtp_dump_name = RtpDumpName();
350   params.logging.encoded_frame_base_path = EncodedFramePath();
351 
352   if (NumStreams() > 1 && Stream0().empty() && Stream1().empty()) {
353     params.ss[0].infer_streams = true;
354   }
355 
356   std::vector<std::string> stream_descriptors;
357   stream_descriptors.push_back(Stream0());
358   stream_descriptors.push_back(Stream1());
359   std::vector<std::string> SL_descriptors;
360   SL_descriptors.push_back(SL0());
361   SL_descriptors.push_back(SL1());
362   VideoQualityTest::FillScalabilitySettings(
363       &params, 0, stream_descriptors, NumStreams(), SelectedStream(),
364       NumSpatialLayers(), SelectedSL(), InterLayerPred(), SL_descriptors);
365 
366   auto fixture = std::make_unique<VideoQualityTest>(nullptr);
367   if (DurationSecs()) {
368     fixture->RunWithAnalyzer(params);
369   } else {
370     fixture->RunWithRenderers(params);
371   }
372 }
373 
main(int argc,char * argv[])374 int main(int argc, char* argv[]) {
375   ::testing::InitGoogleTest(&argc, argv);
376   absl::ParseCommandLine(argc, argv);
377 
378   rtc::LogMessage::SetLogToStderr(absl::GetFlag(FLAGS_logs));
379 
380   // InitFieldTrialsFromString stores the char*, so the char array must outlive
381   // the application.
382   const std::string field_trials = absl::GetFlag(FLAGS_force_fieldtrials);
383   webrtc::field_trial::InitFieldTrialsFromString(field_trials.c_str());
384 
385   webrtc::test::RunTest(Loopback);
386   return 0;
387 }
388