blob: f36699759d38a5c84ac11ab775e855885680bccd [file] [log] [blame]
Sebastian Jansson98b07e92018-09-27 13:47:01 +02001/*
2 * Copyright 2018 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#include "test/scenario/call_client.h"
11
12#include <utility>
13
Steve Anton40d55332019-01-07 10:21:47 -080014#include "absl/memory/memory.h"
Danil Chapovalovb32f2c72019-05-22 13:39:25 +020015#include "api/rtc_event_log/rtc_event_log.h"
16#include "api/rtc_event_log/rtc_event_log_factory.h"
Sebastian Jansson98b07e92018-09-27 13:47:01 +020017#include "modules/audio_mixer/audio_mixer_impl.h"
Sebastian Jansson98b07e92018-09-27 13:47:01 +020018
19namespace webrtc {
20namespace test {
21namespace {
Sebastian Jansson5fbebd52019-02-20 11:16:19 +010022static constexpr size_t kNumSsrcs = 6;
23const uint32_t kSendRtxSsrcs[kNumSsrcs] = {0xBADCAFD, 0xBADCAFE, 0xBADCAFF,
24 0xBADCB00, 0xBADCB01, 0xBADCB02};
25const uint32_t kVideoSendSsrcs[kNumSsrcs] = {0xC0FFED, 0xC0FFEE, 0xC0FFEF,
26 0xC0FFF0, 0xC0FFF1, 0xC0FFF2};
27const uint32_t kVideoRecvLocalSsrcs[kNumSsrcs] = {0xDAB001, 0xDAB002, 0xDAB003,
28 0xDAB004, 0xDAB005, 0xDAB006};
29const uint32_t kAudioSendSsrc = 0xDEADBEEF;
30const uint32_t kReceiverLocalAudioSsrc = 0x1234567;
31
Sebastian Jansson98b07e92018-09-27 13:47:01 +020032const char* kPriorityStreamId = "priority-track";
Sebastian Jansson800e1212018-10-22 11:49:03 +020033
Sebastian Jansson7ccaf892019-04-24 15:13:26 +020034constexpr int kEventLogOutputIntervalMs = 5000;
35
Sebastian Jansson105a10a2019-04-01 09:18:14 +020036CallClientFakeAudio InitAudio(TimeController* time_controller) {
Sebastian Jansson800e1212018-10-22 11:49:03 +020037 CallClientFakeAudio setup;
38 auto capturer = TestAudioDeviceModule::CreatePulsedNoiseCapturer(256, 48000);
39 auto renderer = TestAudioDeviceModule::CreateDiscardRenderer(48000);
Sebastian Jansson105a10a2019-04-01 09:18:14 +020040 setup.fake_audio_device = TestAudioDeviceModule::Create(
41 time_controller->GetTaskQueueFactory(), std::move(capturer),
42 std::move(renderer), 1.f);
Sebastian Jansson800e1212018-10-22 11:49:03 +020043 setup.apm = AudioProcessingBuilder().Create();
44 setup.fake_audio_device->Init();
45 AudioState::Config audio_state_config;
46 audio_state_config.audio_mixer = AudioMixerImpl::Create();
47 audio_state_config.audio_processing = setup.apm;
48 audio_state_config.audio_device_module = setup.fake_audio_device;
49 setup.audio_state = AudioState::Create(audio_state_config);
50 setup.fake_audio_device->RegisterAudioCallback(
51 setup.audio_state->audio_transport());
52 return setup;
53}
54
Sebastian Jansson105a10a2019-04-01 09:18:14 +020055Call* CreateCall(TimeController* time_controller,
Sebastian Jansson7ccaf892019-04-24 15:13:26 +020056 RtcEventLog* event_log,
Sebastian Jansson105a10a2019-04-01 09:18:14 +020057 CallClientConfig config,
58 LoggingNetworkControllerFactory* network_controller_factory,
Sebastian Jansson800e1212018-10-22 11:49:03 +020059 rtc::scoped_refptr<AudioState> audio_state) {
Sebastian Jansson7ccaf892019-04-24 15:13:26 +020060 CallConfig call_config(event_log);
Sebastian Jansson800e1212018-10-22 11:49:03 +020061 call_config.bitrate_config.max_bitrate_bps =
62 config.transport.rates.max_rate.bps_or(-1);
63 call_config.bitrate_config.min_bitrate_bps =
64 config.transport.rates.min_rate.bps();
65 call_config.bitrate_config.start_bitrate_bps =
66 config.transport.rates.start_rate.bps();
Danil Chapovalov359fe332019-04-01 10:46:36 +020067 call_config.task_queue_factory = time_controller->GetTaskQueueFactory();
Sebastian Jansson105a10a2019-04-01 09:18:14 +020068 call_config.network_controller_factory = network_controller_factory;
Sebastian Jansson800e1212018-10-22 11:49:03 +020069 call_config.audio_state = audio_state;
Sebastian Jansson105a10a2019-04-01 09:18:14 +020070 return Call::Create(call_config, time_controller->GetClock(),
71 time_controller->CreateProcessThread("CallModules"),
Danil Chapovalov359fe332019-04-01 10:46:36 +020072 time_controller->CreateProcessThread("Pacer"));
Sebastian Jansson800e1212018-10-22 11:49:03 +020073}
Sebastian Jansson7ccaf892019-04-24 15:13:26 +020074
75std::unique_ptr<RtcEventLog> CreateEventLog(
76 TaskQueueFactory* task_queue_factory,
77 LogWriterFactoryInterface* log_writer_factory) {
78 if (!log_writer_factory) {
Danil Chapovalovb32f2c72019-05-22 13:39:25 +020079 return absl::make_unique<RtcEventLogNull>();
Sebastian Jansson7ccaf892019-04-24 15:13:26 +020080 }
Danil Chapovalovb32f2c72019-05-22 13:39:25 +020081 auto event_log = RtcEventLogFactory(task_queue_factory)
82 .CreateRtcEventLog(RtcEventLog::EncodingType::NewFormat);
Sebastian Jansson7ccaf892019-04-24 15:13:26 +020083 bool success = event_log->StartLogging(log_writer_factory->Create(".rtc.dat"),
84 kEventLogOutputIntervalMs);
85 RTC_CHECK(success);
86 return event_log;
87}
Sebastian Jansson98b07e92018-09-27 13:47:01 +020088}
Sebastian Janssona7d70ab2019-06-11 10:21:32 +020089NetworkControleUpdateCache::NetworkControleUpdateCache(
90 std::unique_ptr<NetworkControllerInterface> controller)
91 : controller_(std::move(controller)) {}
92NetworkControlUpdate NetworkControleUpdateCache::OnNetworkAvailability(
93 NetworkAvailability msg) {
94 return Update(controller_->OnNetworkAvailability(msg));
95}
96NetworkControlUpdate NetworkControleUpdateCache::OnNetworkRouteChange(
97 NetworkRouteChange msg) {
98 return Update(controller_->OnNetworkRouteChange(msg));
99}
100NetworkControlUpdate NetworkControleUpdateCache::OnProcessInterval(
101 ProcessInterval msg) {
102 return Update(controller_->OnProcessInterval(msg));
103}
104NetworkControlUpdate NetworkControleUpdateCache::OnRemoteBitrateReport(
105 RemoteBitrateReport msg) {
106 return Update(controller_->OnRemoteBitrateReport(msg));
107}
108NetworkControlUpdate NetworkControleUpdateCache::OnRoundTripTimeUpdate(
109 RoundTripTimeUpdate msg) {
110 return Update(controller_->OnRoundTripTimeUpdate(msg));
111}
112NetworkControlUpdate NetworkControleUpdateCache::OnSentPacket(SentPacket msg) {
113 return Update(controller_->OnSentPacket(msg));
114}
115NetworkControlUpdate NetworkControleUpdateCache::OnReceivedPacket(
116 ReceivedPacket msg) {
117 return Update(controller_->OnReceivedPacket(msg));
118}
119NetworkControlUpdate NetworkControleUpdateCache::OnStreamsConfig(
120 StreamsConfig msg) {
121 return Update(controller_->OnStreamsConfig(msg));
122}
123NetworkControlUpdate NetworkControleUpdateCache::OnTargetRateConstraints(
124 TargetRateConstraints msg) {
125 return Update(controller_->OnTargetRateConstraints(msg));
126}
127NetworkControlUpdate NetworkControleUpdateCache::OnTransportLossReport(
128 TransportLossReport msg) {
129 return Update(controller_->OnTransportLossReport(msg));
130}
131NetworkControlUpdate NetworkControleUpdateCache::OnTransportPacketsFeedback(
132 TransportPacketsFeedback msg) {
133 return Update(controller_->OnTransportPacketsFeedback(msg));
134}
Sebastian Jansson49167de2019-06-27 15:59:03 +0200135NetworkControlUpdate NetworkControleUpdateCache::OnNetworkStateEstimate(
136 NetworkStateEstimate msg) {
137 return Update(controller_->OnNetworkStateEstimate(msg));
138}
139
Sebastian Janssona7d70ab2019-06-11 10:21:32 +0200140NetworkControlUpdate NetworkControleUpdateCache::update_state() const {
141 return update_state_;
142}
143NetworkControlUpdate NetworkControleUpdateCache::Update(
144 NetworkControlUpdate update) {
145 if (update.target_rate)
146 update_state_.target_rate = update.target_rate;
147 if (update.pacer_config)
148 update_state_.pacer_config = update.pacer_config;
149 if (update.congestion_window)
150 update_state_.congestion_window = update.congestion_window;
151 if (!update.probe_cluster_configs.empty())
152 update_state_.probe_cluster_configs = update.probe_cluster_configs;
153 return update;
154}
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200155
156LoggingNetworkControllerFactory::LoggingNetworkControllerFactory(
Sebastian Jansson52de8b02019-01-16 17:25:44 +0100157 LogWriterFactoryInterface* log_writer_factory,
Sebastian Jansson7ccaf892019-04-24 15:13:26 +0200158 TransportControllerConfig config) {
159 if (config.cc_factory) {
160 cc_factory_ = config.cc_factory;
161 if (log_writer_factory)
162 RTC_LOG(LS_WARNING)
163 << "Can't log controller state for injected network controllers";
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200164 } else {
Sebastian Jansson7ccaf892019-04-24 15:13:26 +0200165 if (log_writer_factory) {
Sebastian Jansson871ac422019-05-17 17:53:44 +0200166 goog_cc_factory_.AttachWriter(
167 log_writer_factory->Create(".cc_state.txt"));
168 print_cc_state_ = true;
Sebastian Jansson7ccaf892019-04-24 15:13:26 +0200169 }
Sebastian Jansson871ac422019-05-17 17:53:44 +0200170 cc_factory_ = &goog_cc_factory_;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200171 }
172}
173
174LoggingNetworkControllerFactory::~LoggingNetworkControllerFactory() {
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200175}
176
177void LoggingNetworkControllerFactory::LogCongestionControllerStats(
178 Timestamp at_time) {
Sebastian Jansson871ac422019-05-17 17:53:44 +0200179 if (print_cc_state_)
180 goog_cc_factory_.PrintState(at_time);
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200181}
182
Sebastian Janssona7d70ab2019-06-11 10:21:32 +0200183NetworkControlUpdate LoggingNetworkControllerFactory::GetUpdate() const {
184 if (last_controller_)
185 return last_controller_->update_state();
186 return NetworkControlUpdate();
187}
188
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200189std::unique_ptr<NetworkControllerInterface>
190LoggingNetworkControllerFactory::Create(NetworkControllerConfig config) {
Sebastian Janssona7d70ab2019-06-11 10:21:32 +0200191 auto controller = absl::make_unique<NetworkControleUpdateCache>(
192 cc_factory_->Create(config));
193 last_controller_ = controller.get();
194 return controller;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200195}
196
197TimeDelta LoggingNetworkControllerFactory::GetProcessInterval() const {
198 return cc_factory_->GetProcessInterval();
199}
200
Sebastian Jansson52de8b02019-01-16 17:25:44 +0100201CallClient::CallClient(
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200202 TimeController* time_controller,
Sebastian Jansson52de8b02019-01-16 17:25:44 +0100203 std::unique_ptr<LogWriterFactoryInterface> log_writer_factory,
204 CallClientConfig config)
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200205 : time_controller_(time_controller),
206 clock_(time_controller->GetClock()),
Sebastian Jansson52de8b02019-01-16 17:25:44 +0100207 log_writer_factory_(std::move(log_writer_factory)),
Sebastian Jansson7ccaf892019-04-24 15:13:26 +0200208 network_controller_factory_(log_writer_factory_.get(), config.transport),
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200209 header_parser_(RtpHeaderParser::Create()),
210 task_queue_(time_controller->GetTaskQueueFactory()->CreateTaskQueue(
211 "CallClient",
212 TaskQueueFactory::Priority::NORMAL)) {
213 SendTask([this, config] {
Sebastian Jansson7ccaf892019-04-24 15:13:26 +0200214 event_log_ = CreateEventLog(time_controller_->GetTaskQueueFactory(),
215 log_writer_factory_.get());
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200216 fake_audio_setup_ = InitAudio(time_controller_);
Sebastian Jansson7ccaf892019-04-24 15:13:26 +0200217 call_.reset(CreateCall(time_controller_, event_log_.get(), config,
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200218 &network_controller_factory_,
219 fake_audio_setup_.audio_state));
220 transport_ = absl::make_unique<NetworkNodeTransport>(clock_, call_.get());
221 });
222}
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200223
Sebastian Jansson800e1212018-10-22 11:49:03 +0200224CallClient::~CallClient() {
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200225 SendTask([&] {
226 call_.reset();
227 fake_audio_setup_ = {};
Sebastian Jansson58c71db2019-05-22 16:20:56 +0200228 rtc::Event done;
229 event_log_->StopLogging([&done] { done.Set(); });
230 done.Wait(rtc::Event::kForever);
Sebastian Jansson7ccaf892019-04-24 15:13:26 +0200231 event_log_.reset();
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200232 });
Sebastian Jansson800e1212018-10-22 11:49:03 +0200233}
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200234
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200235ColumnPrinter CallClient::StatsPrinter() {
236 return ColumnPrinter::Lambda(
237 "pacer_delay call_send_bw",
238 [this](rtc::SimpleStringBuilder& sb) {
239 Call::Stats call_stats = call_->GetStats();
240 sb.AppendFormat("%.3lf %.0lf", call_stats.pacer_delay_ms / 1000.0,
241 call_stats.send_bandwidth_bps / 8.0);
242 },
243 64);
244}
245
246Call::Stats CallClient::GetStats() {
247 return call_->GetStats();
248}
249
Sebastian Janssona7d70ab2019-06-11 10:21:32 +0200250DataRate CallClient::target_rate() const {
251 return network_controller_factory_.GetUpdate().target_rate->target_rate;
252}
253
254DataRate CallClient::link_capacity() const {
255 return network_controller_factory_.GetUpdate()
256 .target_rate->network_estimate.bandwidth;
257}
258
259DataRate CallClient::padding_rate() const {
260 return network_controller_factory_.GetUpdate().pacer_config->pad_rate();
261}
262
Artem Titov40f51152019-01-04 15:45:01 +0100263void CallClient::OnPacketReceived(EmulatedIpPacket packet) {
Sebastian Jansson800e1212018-10-22 11:49:03 +0200264 // Removes added overhead before delivering packet to sender.
Artem Titov4cd433e2019-04-01 11:01:16 +0200265 size_t size =
266 packet.data.size() - route_overhead_.at(packet.to.ipaddr()).bytes();
267 RTC_DCHECK_GE(size, 0);
268 packet.data.SetSize(size);
Sebastian Jansson800e1212018-10-22 11:49:03 +0200269
270 MediaType media_type = MediaType::ANY;
Artem Titov40f51152019-01-04 15:45:01 +0100271 if (!RtpHeaderParser::IsRtcp(packet.cdata(), packet.data.size())) {
Sebastian Jansson1e427612019-03-05 14:25:03 +0100272 auto ssrc = RtpHeaderParser::GetSsrc(packet.cdata(), packet.data.size());
273 RTC_CHECK(ssrc.has_value());
274 media_type = ssrc_media_types_[*ssrc];
Sebastian Jansson800e1212018-10-22 11:49:03 +0200275 }
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200276 struct Closure {
277 void operator()() {
278 call->Receiver()->DeliverPacket(media_type, packet.data,
279 packet.arrival_time.us());
280 }
281 Call* call;
282 MediaType media_type;
283 EmulatedIpPacket packet;
284 };
285 task_queue_.PostTask(Closure{call_.get(), media_type, std::move(packet)});
Sebastian Jansson800e1212018-10-22 11:49:03 +0200286}
287
Sebastian Jansson52de8b02019-01-16 17:25:44 +0100288std::unique_ptr<RtcEventLogOutput> CallClient::GetLogWriter(std::string name) {
289 if (!log_writer_factory_ || name.empty())
290 return nullptr;
291 return log_writer_factory_->Create(name);
292}
293
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200294uint32_t CallClient::GetNextVideoSsrc() {
Sebastian Jansson5fbebd52019-02-20 11:16:19 +0100295 RTC_CHECK_LT(next_video_ssrc_index_, kNumSsrcs);
296 return kVideoSendSsrcs[next_video_ssrc_index_++];
297}
298
299uint32_t CallClient::GetNextVideoLocalSsrc() {
300 RTC_CHECK_LT(next_video_local_ssrc_index_, kNumSsrcs);
301 return kVideoRecvLocalSsrcs[next_video_local_ssrc_index_++];
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200302}
303
304uint32_t CallClient::GetNextAudioSsrc() {
305 RTC_CHECK_LT(next_audio_ssrc_index_, 1);
306 next_audio_ssrc_index_++;
Sebastian Jansson5fbebd52019-02-20 11:16:19 +0100307 return kAudioSendSsrc;
308}
309
310uint32_t CallClient::GetNextAudioLocalSsrc() {
311 RTC_CHECK_LT(next_audio_local_ssrc_index_, 1);
312 next_audio_local_ssrc_index_++;
313 return kReceiverLocalAudioSsrc;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200314}
315
316uint32_t CallClient::GetNextRtxSsrc() {
Sebastian Jansson5fbebd52019-02-20 11:16:19 +0100317 RTC_CHECK_LT(next_rtx_ssrc_index_, kNumSsrcs);
318 return kSendRtxSsrcs[next_rtx_ssrc_index_++];
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200319}
320
321std::string CallClient::GetNextPriorityId() {
322 RTC_CHECK_LT(next_priority_index_++, 1);
323 return kPriorityStreamId;
324}
325
Sebastian Janssonfd201712018-11-12 16:44:16 +0100326void CallClient::AddExtensions(std::vector<RtpExtension> extensions) {
327 for (const auto& extension : extensions)
328 header_parser_->RegisterRtpHeaderExtension(extension);
329}
330
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200331void CallClient::SendTask(std::function<void()> task) {
332 time_controller_->InvokeWithControlledYield(
333 [&] { task_queue_.SendTask(std::move(task)); });
334}
335
Sebastian Jansson800e1212018-10-22 11:49:03 +0200336CallClientPair::~CallClientPair() = default;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200337
338} // namespace test
339} // namespace webrtc