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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
|
/*
* 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/call_client.h"
#include <iostream>
#include <memory>
#include <utility>
#include "api/media_types.h"
#include "api/rtc_event_log/rtc_event_log.h"
#include "api/rtc_event_log/rtc_event_log_factory.h"
#include "api/transport/network_types.h"
#include "call/call.h"
#include "call/rtp_transport_controller_send_factory.h"
#include "modules/audio_device/include/test_audio_device.h"
#include "modules/audio_mixer/audio_mixer_impl.h"
#include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
#include "modules/rtp_rtcp/source/rtp_packet_received.h"
#include "modules/rtp_rtcp/source/rtp_util.h"
namespace webrtc {
namespace test {
namespace {
static constexpr size_t kNumSsrcs = 6;
const uint32_t kSendRtxSsrcs[kNumSsrcs] = {0xBADCAFD, 0xBADCAFE, 0xBADCAFF,
0xBADCB00, 0xBADCB01, 0xBADCB02};
const uint32_t kVideoSendSsrcs[kNumSsrcs] = {0xC0FFED, 0xC0FFEE, 0xC0FFEF,
0xC0FFF0, 0xC0FFF1, 0xC0FFF2};
const uint32_t kVideoRecvLocalSsrcs[kNumSsrcs] = {0xDAB001, 0xDAB002, 0xDAB003,
0xDAB004, 0xDAB005, 0xDAB006};
const uint32_t kAudioSendSsrc = 0xDEADBEEF;
const uint32_t kReceiverLocalAudioSsrc = 0x1234567;
constexpr int kEventLogOutputIntervalMs = 5000;
CallClientFakeAudio InitAudio(TimeController* time_controller) {
CallClientFakeAudio setup;
auto capturer = TestAudioDeviceModule::CreatePulsedNoiseCapturer(256, 48000);
auto renderer = TestAudioDeviceModule::CreateDiscardRenderer(48000);
setup.fake_audio_device = TestAudioDeviceModule::Create(
time_controller->GetTaskQueueFactory(), std::move(capturer),
std::move(renderer), 1.f);
setup.apm = AudioProcessingBuilder().Create();
setup.fake_audio_device->Init();
AudioState::Config audio_state_config;
audio_state_config.audio_mixer = AudioMixerImpl::Create();
audio_state_config.audio_processing = setup.apm;
audio_state_config.audio_device_module = setup.fake_audio_device;
setup.audio_state = AudioState::Create(audio_state_config);
setup.fake_audio_device->RegisterAudioCallback(
setup.audio_state->audio_transport());
return setup;
}
std::unique_ptr<Call> CreateCall(
TimeController* time_controller,
RtcEventLog* event_log,
CallClientConfig config,
LoggingNetworkControllerFactory* network_controller_factory,
rtc::scoped_refptr<AudioState> audio_state) {
CallConfig call_config(event_log);
call_config.bitrate_config.max_bitrate_bps =
config.transport.rates.max_rate.bps_or(-1);
call_config.bitrate_config.min_bitrate_bps =
config.transport.rates.min_rate.bps();
call_config.bitrate_config.start_bitrate_bps =
config.transport.rates.start_rate.bps();
call_config.task_queue_factory = time_controller->GetTaskQueueFactory();
call_config.network_controller_factory = network_controller_factory;
call_config.audio_state = audio_state;
call_config.pacer_burst_interval = config.pacer_burst_interval;
call_config.trials = config.field_trials;
Clock* clock = time_controller->GetClock();
return Call::Create(call_config, clock,
RtpTransportControllerSendFactory().Create(
call_config.ExtractTransportConfig(), clock));
}
std::unique_ptr<RtcEventLog> CreateEventLog(
TaskQueueFactory* task_queue_factory,
LogWriterFactoryInterface* log_writer_factory) {
if (!log_writer_factory) {
return std::make_unique<RtcEventLogNull>();
}
auto event_log = RtcEventLogFactory(task_queue_factory)
.CreateRtcEventLog(RtcEventLog::EncodingType::NewFormat);
bool success = event_log->StartLogging(log_writer_factory->Create(".rtc.dat"),
kEventLogOutputIntervalMs);
RTC_CHECK(success);
return event_log;
}
} // namespace
NetworkControleUpdateCache::NetworkControleUpdateCache(
std::unique_ptr<NetworkControllerInterface> controller)
: controller_(std::move(controller)) {}
NetworkControlUpdate NetworkControleUpdateCache::OnNetworkAvailability(
NetworkAvailability msg) {
return Update(controller_->OnNetworkAvailability(msg));
}
NetworkControlUpdate NetworkControleUpdateCache::OnNetworkRouteChange(
NetworkRouteChange msg) {
return Update(controller_->OnNetworkRouteChange(msg));
}
NetworkControlUpdate NetworkControleUpdateCache::OnProcessInterval(
ProcessInterval msg) {
return Update(controller_->OnProcessInterval(msg));
}
NetworkControlUpdate NetworkControleUpdateCache::OnRemoteBitrateReport(
RemoteBitrateReport msg) {
return Update(controller_->OnRemoteBitrateReport(msg));
}
NetworkControlUpdate NetworkControleUpdateCache::OnRoundTripTimeUpdate(
RoundTripTimeUpdate msg) {
return Update(controller_->OnRoundTripTimeUpdate(msg));
}
NetworkControlUpdate NetworkControleUpdateCache::OnSentPacket(SentPacket msg) {
return Update(controller_->OnSentPacket(msg));
}
NetworkControlUpdate NetworkControleUpdateCache::OnReceivedPacket(
ReceivedPacket msg) {
return Update(controller_->OnReceivedPacket(msg));
}
NetworkControlUpdate NetworkControleUpdateCache::OnStreamsConfig(
StreamsConfig msg) {
return Update(controller_->OnStreamsConfig(msg));
}
NetworkControlUpdate NetworkControleUpdateCache::OnTargetRateConstraints(
TargetRateConstraints msg) {
return Update(controller_->OnTargetRateConstraints(msg));
}
NetworkControlUpdate NetworkControleUpdateCache::OnTransportLossReport(
TransportLossReport msg) {
return Update(controller_->OnTransportLossReport(msg));
}
NetworkControlUpdate NetworkControleUpdateCache::OnTransportPacketsFeedback(
TransportPacketsFeedback msg) {
return Update(controller_->OnTransportPacketsFeedback(msg));
}
NetworkControlUpdate NetworkControleUpdateCache::OnNetworkStateEstimate(
NetworkStateEstimate msg) {
return Update(controller_->OnNetworkStateEstimate(msg));
}
NetworkControlUpdate NetworkControleUpdateCache::update_state() const {
return update_state_;
}
NetworkControlUpdate NetworkControleUpdateCache::Update(
NetworkControlUpdate update) {
if (update.target_rate)
update_state_.target_rate = update.target_rate;
if (update.pacer_config)
update_state_.pacer_config = update.pacer_config;
if (update.congestion_window)
update_state_.congestion_window = update.congestion_window;
if (!update.probe_cluster_configs.empty())
update_state_.probe_cluster_configs = update.probe_cluster_configs;
return update;
}
LoggingNetworkControllerFactory::LoggingNetworkControllerFactory(
LogWriterFactoryInterface* log_writer_factory,
TransportControllerConfig config) {
if (config.cc_factory) {
cc_factory_ = config.cc_factory;
if (log_writer_factory)
RTC_LOG(LS_WARNING)
<< "Can't log controller state for injected network controllers";
} else {
if (log_writer_factory) {
goog_cc_factory_.AttachWriter(
log_writer_factory->Create(".cc_state.txt"));
print_cc_state_ = true;
}
cc_factory_ = &goog_cc_factory_;
}
}
LoggingNetworkControllerFactory::~LoggingNetworkControllerFactory() {}
void LoggingNetworkControllerFactory::LogCongestionControllerStats(
Timestamp at_time) {
if (print_cc_state_)
goog_cc_factory_.PrintState(at_time);
}
NetworkControlUpdate LoggingNetworkControllerFactory::GetUpdate() const {
if (last_controller_)
return last_controller_->update_state();
return NetworkControlUpdate();
}
std::unique_ptr<NetworkControllerInterface>
LoggingNetworkControllerFactory::Create(NetworkControllerConfig config) {
auto controller =
std::make_unique<NetworkControleUpdateCache>(cc_factory_->Create(config));
last_controller_ = controller.get();
return controller;
}
TimeDelta LoggingNetworkControllerFactory::GetProcessInterval() const {
return cc_factory_->GetProcessInterval();
}
void LoggingNetworkControllerFactory::SetRemoteBitrateEstimate(
RemoteBitrateReport msg) {
if (last_controller_)
last_controller_->OnRemoteBitrateReport(msg);
}
CallClient::CallClient(
TimeController* time_controller,
std::unique_ptr<LogWriterFactoryInterface> log_writer_factory,
CallClientConfig config)
: time_controller_(time_controller),
clock_(time_controller->GetClock()),
log_writer_factory_(std::move(log_writer_factory)),
network_controller_factory_(log_writer_factory_.get(), config.transport),
task_queue_(time_controller->GetTaskQueueFactory()->CreateTaskQueue(
"CallClient",
TaskQueueFactory::Priority::NORMAL)) {
config.field_trials = &field_trials_;
SendTask([this, config] {
event_log_ = CreateEventLog(time_controller_->GetTaskQueueFactory(),
log_writer_factory_.get());
fake_audio_setup_ = InitAudio(time_controller_);
call_ =
CreateCall(time_controller_, event_log_.get(), config,
&network_controller_factory_, fake_audio_setup_.audio_state);
transport_ = std::make_unique<NetworkNodeTransport>(clock_, call_.get());
});
}
CallClient::~CallClient() {
SendTask([&] {
call_.reset();
fake_audio_setup_ = {};
rtc::Event done;
event_log_->StopLogging([&done] { done.Set(); });
done.Wait(rtc::Event::kForever);
event_log_.reset();
});
}
ColumnPrinter CallClient::StatsPrinter() {
return ColumnPrinter::Lambda(
"pacer_delay call_send_bw",
[this](rtc::SimpleStringBuilder& sb) {
Call::Stats call_stats = call_->GetStats();
sb.AppendFormat("%.3lf %.0lf", call_stats.pacer_delay_ms / 1000.0,
call_stats.send_bandwidth_bps / 8.0);
},
64);
}
Call::Stats CallClient::GetStats() {
// This call needs to be made on the thread that `call_` was constructed on.
Call::Stats stats;
SendTask([this, &stats] { stats = call_->GetStats(); });
return stats;
}
DataRate CallClient::target_rate() const {
return network_controller_factory_.GetUpdate().target_rate->target_rate;
}
DataRate CallClient::stable_target_rate() const {
return network_controller_factory_.GetUpdate()
.target_rate->stable_target_rate;
}
DataRate CallClient::padding_rate() const {
return network_controller_factory_.GetUpdate().pacer_config->pad_rate();
}
void CallClient::SetRemoteBitrate(DataRate bitrate) {
RemoteBitrateReport msg;
msg.bandwidth = bitrate;
msg.receive_time = clock_->CurrentTime();
network_controller_factory_.SetRemoteBitrateEstimate(msg);
}
void CallClient::UpdateBitrateConstraints(
const BitrateConstraints& constraints) {
SendTask([this, &constraints]() {
call_->GetTransportControllerSend()->SetSdpBitrateParameters(constraints);
});
}
void CallClient::SetAudioReceiveRtpHeaderExtensions(
rtc::ArrayView<RtpExtension> extensions) {
SendTask([this, &extensions]() {
audio_extensions_ = RtpHeaderExtensionMap(extensions);
});
}
void CallClient::SetVideoReceiveRtpHeaderExtensions(
rtc::ArrayView<RtpExtension> extensions) {
SendTask([this, &extensions]() {
video_extensions_ = RtpHeaderExtensionMap(extensions);
});
}
void CallClient::OnPacketReceived(EmulatedIpPacket packet) {
MediaType media_type = MediaType::ANY;
if (IsRtpPacket(packet.data)) {
media_type = ssrc_media_types_[ParseRtpSsrc(packet.data)];
task_queue_.PostTask([this, media_type,
packet = std::move(packet)]() mutable {
RtpHeaderExtensionMap& extension_map = media_type == MediaType::AUDIO
? audio_extensions_
: video_extensions_;
RtpPacketReceived received_packet(&extension_map, packet.arrival_time);
RTC_CHECK(received_packet.Parse(packet.data));
call_->Receiver()->DeliverRtpPacket(media_type, received_packet,
/*undemuxable_packet_handler=*/
[](const RtpPacketReceived& packet) {
RTC_CHECK_NOTREACHED();
return false;
});
});
} else {
task_queue_.PostTask(
[call = call_.get(), packet = std::move(packet)]() mutable {
call->Receiver()->DeliverRtcpPacket(packet.data);
});
}
}
std::unique_ptr<RtcEventLogOutput> CallClient::GetLogWriter(std::string name) {
if (!log_writer_factory_ || name.empty())
return nullptr;
return log_writer_factory_->Create(name);
}
uint32_t CallClient::GetNextVideoSsrc() {
RTC_CHECK_LT(next_video_ssrc_index_, kNumSsrcs);
return kVideoSendSsrcs[next_video_ssrc_index_++];
}
uint32_t CallClient::GetNextVideoLocalSsrc() {
RTC_CHECK_LT(next_video_local_ssrc_index_, kNumSsrcs);
return kVideoRecvLocalSsrcs[next_video_local_ssrc_index_++];
}
uint32_t CallClient::GetNextAudioSsrc() {
RTC_CHECK_LT(next_audio_ssrc_index_, 1);
next_audio_ssrc_index_++;
return kAudioSendSsrc;
}
uint32_t CallClient::GetNextAudioLocalSsrc() {
RTC_CHECK_LT(next_audio_local_ssrc_index_, 1);
next_audio_local_ssrc_index_++;
return kReceiverLocalAudioSsrc;
}
uint32_t CallClient::GetNextRtxSsrc() {
RTC_CHECK_LT(next_rtx_ssrc_index_, kNumSsrcs);
return kSendRtxSsrcs[next_rtx_ssrc_index_++];
}
void CallClient::SendTask(std::function<void()> task) {
task_queue_.SendTask(std::move(task));
}
int16_t CallClient::Bind(EmulatedEndpoint* endpoint) {
uint16_t port = endpoint->BindReceiver(0, this).value();
endpoints_.push_back({endpoint, port});
return port;
}
void CallClient::UnBind() {
for (auto ep_port : endpoints_)
ep_port.first->UnbindReceiver(ep_port.second);
}
CallClientPair::~CallClientPair() = default;
} // namespace test
} // namespace webrtc
|