1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
/*
* Copyright 2018 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "test/scenario/scenario.h"
#include <algorithm>
#include <memory>
#include "absl/flags/flag.h"
#include "absl/flags/parse.h"
#include "absl/strings/string_view.h"
#include "api/audio_codecs/builtin_audio_decoder_factory.h"
#include "api/audio_codecs/builtin_audio_encoder_factory.h"
#include "rtc_base/socket_address.h"
#include "test/logging/file_log_writer.h"
#include "test/network/network_emulation.h"
#include "test/scenario/video_stream.h"
#include "test/testsupport/file_utils.h"
ABSL_FLAG(bool, scenario_logs, false, "Save logs from scenario framework.");
ABSL_FLAG(std::string,
scenario_logs_root,
"",
"Output root path, based on project root if unset.");
namespace webrtc {
namespace test {
namespace {
std::unique_ptr<FileLogWriterFactory> GetScenarioLogManager(
absl::string_view file_name) {
if (absl::GetFlag(FLAGS_scenario_logs) && !file_name.empty()) {
std::string output_root = absl::GetFlag(FLAGS_scenario_logs_root);
if (output_root.empty())
output_root = OutputPath() + "output_data/";
auto base_filename = output_root + std::string(file_name) + ".";
RTC_LOG(LS_INFO) << "Saving scenario logs to: " << base_filename;
return std::make_unique<FileLogWriterFactory>(base_filename);
}
return nullptr;
}
} // namespace
Scenario::Scenario()
: Scenario(std::unique_ptr<LogWriterFactoryInterface>(),
/*real_time=*/false) {}
Scenario::Scenario(const testing::TestInfo* test_info)
: Scenario(std::string(test_info->test_suite_name()) + "/" +
test_info->name()) {}
Scenario::Scenario(absl::string_view file_name)
: Scenario(file_name, /*real_time=*/false) {}
Scenario::Scenario(absl::string_view file_name, bool real_time)
: Scenario(GetScenarioLogManager(file_name), real_time) {}
Scenario::Scenario(
std::unique_ptr<LogWriterFactoryInterface> log_writer_factory,
bool real_time)
: log_writer_factory_(std::move(log_writer_factory)),
network_manager_(real_time ? TimeMode::kRealTime : TimeMode::kSimulated,
EmulatedNetworkStatsGatheringMode::kDefault),
clock_(network_manager_.time_controller()->GetClock()),
audio_decoder_factory_(CreateBuiltinAudioDecoderFactory()),
audio_encoder_factory_(CreateBuiltinAudioEncoderFactory()),
task_queue_(network_manager_.time_controller()
->GetTaskQueueFactory()
->CreateTaskQueue("Scenario",
TaskQueueFactory::Priority::NORMAL)) {}
Scenario::~Scenario() {
if (start_time_.IsFinite())
Stop();
for (auto& call_client : clients_) {
call_client->SendTask([&] { call_client->transport_->Disconnect(); });
call_client->UnBind();
}
}
ColumnPrinter Scenario::TimePrinter() {
return ColumnPrinter::Lambda(
"time",
[this](rtc::SimpleStringBuilder& sb) {
sb.AppendFormat("%.3lf", Now().seconds<double>());
},
32);
}
StatesPrinter* Scenario::CreatePrinter(absl::string_view name,
TimeDelta interval,
std::vector<ColumnPrinter> printers) {
std::vector<ColumnPrinter> all_printers{TimePrinter()};
for (auto& printer : printers)
all_printers.push_back(printer);
StatesPrinter* printer = new StatesPrinter(GetLogWriter(name), all_printers);
printers_.emplace_back(printer);
printer->PrintHeaders();
if (interval.IsFinite())
Every(interval, [printer] { printer->PrintRow(); });
return printer;
}
CallClient* Scenario::CreateClient(absl::string_view name,
CallClientConfig config) {
CallClient* client = new CallClient(network_manager_.time_controller(),
GetLogWriterFactory(name), config);
if (config.transport.state_log_interval.IsFinite()) {
Every(config.transport.state_log_interval, [this, client]() {
client->network_controller_factory_.LogCongestionControllerStats(Now());
});
}
clients_.emplace_back(client);
return client;
}
CallClient* Scenario::CreateClient(
absl::string_view name,
std::function<void(CallClientConfig*)> config_modifier) {
CallClientConfig config;
config_modifier(&config);
return CreateClient(name, config);
}
CallClientPair* Scenario::CreateRoutes(
CallClient* first,
std::vector<EmulatedNetworkNode*> send_link,
CallClient* second,
std::vector<EmulatedNetworkNode*> return_link) {
return CreateRoutes(first, send_link,
DataSize::Bytes(PacketOverhead::kDefault), second,
return_link, DataSize::Bytes(PacketOverhead::kDefault));
}
CallClientPair* Scenario::CreateRoutes(
CallClient* first,
std::vector<EmulatedNetworkNode*> send_link,
DataSize first_overhead,
CallClient* second,
std::vector<EmulatedNetworkNode*> return_link,
DataSize second_overhead) {
CallClientPair* client_pair = new CallClientPair(first, second);
ChangeRoute(client_pair->forward(), send_link, first_overhead);
ChangeRoute(client_pair->reverse(), return_link, second_overhead);
client_pairs_.emplace_back(client_pair);
return client_pair;
}
void Scenario::ChangeRoute(std::pair<CallClient*, CallClient*> clients,
std::vector<EmulatedNetworkNode*> over_nodes) {
ChangeRoute(clients, over_nodes, DataSize::Bytes(PacketOverhead::kDefault));
}
void Scenario::ChangeRoute(std::pair<CallClient*, CallClient*> clients,
std::vector<EmulatedNetworkNode*> over_nodes,
DataSize overhead) {
EmulatedRoute* route = network_manager_.CreateRoute(over_nodes);
uint16_t port = clients.second->Bind(route->to);
auto addr = rtc::SocketAddress(route->to->GetPeerLocalAddress(), port);
clients.first->transport_->Connect(route->from, addr, overhead);
}
EmulatedNetworkNode* Scenario::CreateSimulationNode(
std::function<void(NetworkSimulationConfig*)> config_modifier) {
NetworkSimulationConfig config;
config_modifier(&config);
return CreateSimulationNode(config);
}
EmulatedNetworkNode* Scenario::CreateSimulationNode(
NetworkSimulationConfig config) {
return network_manager_.CreateEmulatedNode(
SimulationNode::CreateBehavior(config));
}
SimulationNode* Scenario::CreateMutableSimulationNode(
std::function<void(NetworkSimulationConfig*)> config_modifier) {
NetworkSimulationConfig config;
config_modifier(&config);
return CreateMutableSimulationNode(config);
}
SimulationNode* Scenario::CreateMutableSimulationNode(
NetworkSimulationConfig config) {
std::unique_ptr<SimulatedNetwork> behavior =
SimulationNode::CreateBehavior(config);
SimulatedNetwork* behavior_ptr = behavior.get();
auto* emulated_node =
network_manager_.CreateEmulatedNode(std::move(behavior));
simulation_nodes_.emplace_back(
new SimulationNode(config, behavior_ptr, emulated_node));
return simulation_nodes_.back().get();
}
void Scenario::TriggerPacketBurst(std::vector<EmulatedNetworkNode*> over_nodes,
size_t num_packets,
size_t packet_size) {
network_manager_.CreateCrossTrafficRoute(over_nodes)
->TriggerPacketBurst(num_packets, packet_size);
}
void Scenario::NetworkDelayedAction(
std::vector<EmulatedNetworkNode*> over_nodes,
size_t packet_size,
std::function<void()> action) {
network_manager_.CreateCrossTrafficRoute(over_nodes)
->NetworkDelayedAction(packet_size, action);
}
VideoStreamPair* Scenario::CreateVideoStream(
std::pair<CallClient*, CallClient*> clients,
std::function<void(VideoStreamConfig*)> config_modifier) {
VideoStreamConfig config;
config_modifier(&config);
return CreateVideoStream(clients, config);
}
VideoStreamPair* Scenario::CreateVideoStream(
std::pair<CallClient*, CallClient*> clients,
VideoStreamConfig config) {
std::vector<RtpExtension> extensions = GetVideoRtpExtensions(config);
clients.first->SetVideoReceiveRtpHeaderExtensions(extensions);
clients.second->SetVideoReceiveRtpHeaderExtensions(extensions);
video_streams_.emplace_back(
new VideoStreamPair(clients.first, clients.second, config));
return video_streams_.back().get();
}
AudioStreamPair* Scenario::CreateAudioStream(
std::pair<CallClient*, CallClient*> clients,
std::function<void(AudioStreamConfig*)> config_modifier) {
AudioStreamConfig config;
config_modifier(&config);
return CreateAudioStream(clients, config);
}
AudioStreamPair* Scenario::CreateAudioStream(
std::pair<CallClient*, CallClient*> clients,
AudioStreamConfig config) {
std::vector<RtpExtension> extensions = GetAudioRtpExtensions(config);
clients.first->SetAudioReceiveRtpHeaderExtensions(extensions);
clients.second->SetAudioReceiveRtpHeaderExtensions(extensions);
audio_streams_.emplace_back(
new AudioStreamPair(clients.first, audio_encoder_factory_, clients.second,
audio_decoder_factory_, config));
return audio_streams_.back().get();
}
void Scenario::Every(TimeDelta interval,
absl::AnyInvocable<void(TimeDelta)> function) {
RepeatingTaskHandle::DelayedStart(
task_queue_.get(), interval,
[interval, function = std::move(function)]() mutable {
function(interval);
return interval;
});
}
void Scenario::Every(TimeDelta interval, absl::AnyInvocable<void()> function) {
RepeatingTaskHandle::DelayedStart(
task_queue_.get(), interval,
[interval, function = std::move(function)]() mutable {
function();
return interval;
});
}
void Scenario::Post(absl::AnyInvocable<void() &&> function) {
task_queue_->PostTask(std::move(function));
}
void Scenario::At(TimeDelta offset, absl::AnyInvocable<void() &&> function) {
RTC_DCHECK_GT(offset, TimeSinceStart());
task_queue_->PostDelayedTask(std::move(function), TimeUntilTarget(offset));
}
void Scenario::RunFor(TimeDelta duration) {
if (start_time_.IsInfinite())
Start();
network_manager_.time_controller()->AdvanceTime(duration);
}
void Scenario::RunUntil(TimeDelta target_time_since_start) {
RunFor(TimeUntilTarget(target_time_since_start));
}
void Scenario::RunUntil(TimeDelta target_time_since_start,
TimeDelta check_interval,
std::function<bool()> exit_function) {
if (start_time_.IsInfinite())
Start();
while (check_interval >= TimeUntilTarget(target_time_since_start)) {
network_manager_.time_controller()->AdvanceTime(check_interval);
if (exit_function())
return;
}
network_manager_.time_controller()->AdvanceTime(
TimeUntilTarget(target_time_since_start));
}
void Scenario::Start() {
start_time_ = clock_->CurrentTime();
for (auto& stream_pair : video_streams_)
stream_pair->receive()->Start();
for (auto& stream_pair : audio_streams_)
stream_pair->receive()->Start();
for (auto& stream_pair : video_streams_) {
if (stream_pair->config_.autostart) {
stream_pair->send()->Start();
}
}
for (auto& stream_pair : audio_streams_) {
if (stream_pair->config_.autostart) {
stream_pair->send()->Start();
}
}
}
void Scenario::Stop() {
RTC_DCHECK(start_time_.IsFinite());
for (auto& stream_pair : video_streams_) {
stream_pair->send()->Stop();
}
for (auto& stream_pair : audio_streams_)
stream_pair->send()->Stop();
for (auto& stream_pair : video_streams_)
stream_pair->receive()->Stop();
for (auto& stream_pair : audio_streams_)
stream_pair->receive()->Stop();
start_time_ = Timestamp::PlusInfinity();
}
Timestamp Scenario::Now() {
return clock_->CurrentTime();
}
TimeDelta Scenario::TimeSinceStart() {
if (start_time_.IsInfinite())
return TimeDelta::Zero();
return Now() - start_time_;
}
TimeDelta Scenario::TimeUntilTarget(TimeDelta target_time_offset) {
return target_time_offset - TimeSinceStart();
}
} // namespace test
} // namespace webrtc
|