blob: b42f2a3d709b7463f8c3ff55c3cf7cbf6e87a059 [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}
Jonas Olssona4d87372019-07-05 19:08:33 +020088} // namespace
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
Jonas Olssona4d87372019-07-05 19:08:33 +0200174LoggingNetworkControllerFactory::~LoggingNetworkControllerFactory() {}
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200175
176void LoggingNetworkControllerFactory::LogCongestionControllerStats(
177 Timestamp at_time) {
Sebastian Jansson871ac422019-05-17 17:53:44 +0200178 if (print_cc_state_)
179 goog_cc_factory_.PrintState(at_time);
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200180}
181
Sebastian Janssona7d70ab2019-06-11 10:21:32 +0200182NetworkControlUpdate LoggingNetworkControllerFactory::GetUpdate() const {
183 if (last_controller_)
184 return last_controller_->update_state();
185 return NetworkControlUpdate();
186}
187
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200188std::unique_ptr<NetworkControllerInterface>
189LoggingNetworkControllerFactory::Create(NetworkControllerConfig config) {
Sebastian Janssona7d70ab2019-06-11 10:21:32 +0200190 auto controller = absl::make_unique<NetworkControleUpdateCache>(
191 cc_factory_->Create(config));
192 last_controller_ = controller.get();
193 return controller;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200194}
195
196TimeDelta LoggingNetworkControllerFactory::GetProcessInterval() const {
197 return cc_factory_->GetProcessInterval();
198}
199
Sebastian Jansson52de8b02019-01-16 17:25:44 +0100200CallClient::CallClient(
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200201 TimeController* time_controller,
Sebastian Jansson52de8b02019-01-16 17:25:44 +0100202 std::unique_ptr<LogWriterFactoryInterface> log_writer_factory,
203 CallClientConfig config)
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200204 : time_controller_(time_controller),
205 clock_(time_controller->GetClock()),
Sebastian Jansson52de8b02019-01-16 17:25:44 +0100206 log_writer_factory_(std::move(log_writer_factory)),
Sebastian Jansson7ccaf892019-04-24 15:13:26 +0200207 network_controller_factory_(log_writer_factory_.get(), config.transport),
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200208 header_parser_(RtpHeaderParser::Create()),
209 task_queue_(time_controller->GetTaskQueueFactory()->CreateTaskQueue(
210 "CallClient",
211 TaskQueueFactory::Priority::NORMAL)) {
212 SendTask([this, config] {
Sebastian Jansson7ccaf892019-04-24 15:13:26 +0200213 event_log_ = CreateEventLog(time_controller_->GetTaskQueueFactory(),
214 log_writer_factory_.get());
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200215 fake_audio_setup_ = InitAudio(time_controller_);
Sebastian Jansson7ccaf892019-04-24 15:13:26 +0200216 call_.reset(CreateCall(time_controller_, event_log_.get(), config,
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200217 &network_controller_factory_,
218 fake_audio_setup_.audio_state));
219 transport_ = absl::make_unique<NetworkNodeTransport>(clock_, call_.get());
220 });
221}
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200222
Sebastian Jansson800e1212018-10-22 11:49:03 +0200223CallClient::~CallClient() {
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200224 SendTask([&] {
225 call_.reset();
226 fake_audio_setup_ = {};
Sebastian Jansson58c71db2019-05-22 16:20:56 +0200227 rtc::Event done;
228 event_log_->StopLogging([&done] { done.Set(); });
229 done.Wait(rtc::Event::kForever);
Sebastian Jansson7ccaf892019-04-24 15:13:26 +0200230 event_log_.reset();
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200231 });
Sebastian Jansson800e1212018-10-22 11:49:03 +0200232}
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200233
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200234ColumnPrinter CallClient::StatsPrinter() {
235 return ColumnPrinter::Lambda(
236 "pacer_delay call_send_bw",
237 [this](rtc::SimpleStringBuilder& sb) {
238 Call::Stats call_stats = call_->GetStats();
239 sb.AppendFormat("%.3lf %.0lf", call_stats.pacer_delay_ms / 1000.0,
240 call_stats.send_bandwidth_bps / 8.0);
241 },
242 64);
243}
244
245Call::Stats CallClient::GetStats() {
246 return call_->GetStats();
247}
248
Sebastian Janssona7d70ab2019-06-11 10:21:32 +0200249DataRate CallClient::target_rate() const {
250 return network_controller_factory_.GetUpdate().target_rate->target_rate;
251}
252
253DataRate CallClient::link_capacity() const {
254 return network_controller_factory_.GetUpdate()
255 .target_rate->network_estimate.bandwidth;
256}
257
258DataRate CallClient::padding_rate() const {
259 return network_controller_factory_.GetUpdate().pacer_config->pad_rate();
260}
261
Artem Titov40f51152019-01-04 15:45:01 +0100262void CallClient::OnPacketReceived(EmulatedIpPacket packet) {
Sebastian Jansson800e1212018-10-22 11:49:03 +0200263 // Removes added overhead before delivering packet to sender.
Artem Titov4cd433e2019-04-01 11:01:16 +0200264 size_t size =
265 packet.data.size() - route_overhead_.at(packet.to.ipaddr()).bytes();
266 RTC_DCHECK_GE(size, 0);
267 packet.data.SetSize(size);
Sebastian Jansson800e1212018-10-22 11:49:03 +0200268
269 MediaType media_type = MediaType::ANY;
Artem Titov40f51152019-01-04 15:45:01 +0100270 if (!RtpHeaderParser::IsRtcp(packet.cdata(), packet.data.size())) {
Sebastian Jansson1e427612019-03-05 14:25:03 +0100271 auto ssrc = RtpHeaderParser::GetSsrc(packet.cdata(), packet.data.size());
272 RTC_CHECK(ssrc.has_value());
273 media_type = ssrc_media_types_[*ssrc];
Sebastian Jansson800e1212018-10-22 11:49:03 +0200274 }
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200275 struct Closure {
276 void operator()() {
277 call->Receiver()->DeliverPacket(media_type, packet.data,
278 packet.arrival_time.us());
279 }
280 Call* call;
281 MediaType media_type;
282 EmulatedIpPacket packet;
283 };
284 task_queue_.PostTask(Closure{call_.get(), media_type, std::move(packet)});
Sebastian Jansson800e1212018-10-22 11:49:03 +0200285}
286
Sebastian Jansson52de8b02019-01-16 17:25:44 +0100287std::unique_ptr<RtcEventLogOutput> CallClient::GetLogWriter(std::string name) {
288 if (!log_writer_factory_ || name.empty())
289 return nullptr;
290 return log_writer_factory_->Create(name);
291}
292
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200293uint32_t CallClient::GetNextVideoSsrc() {
Sebastian Jansson5fbebd52019-02-20 11:16:19 +0100294 RTC_CHECK_LT(next_video_ssrc_index_, kNumSsrcs);
295 return kVideoSendSsrcs[next_video_ssrc_index_++];
296}
297
298uint32_t CallClient::GetNextVideoLocalSsrc() {
299 RTC_CHECK_LT(next_video_local_ssrc_index_, kNumSsrcs);
300 return kVideoRecvLocalSsrcs[next_video_local_ssrc_index_++];
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200301}
302
303uint32_t CallClient::GetNextAudioSsrc() {
304 RTC_CHECK_LT(next_audio_ssrc_index_, 1);
305 next_audio_ssrc_index_++;
Sebastian Jansson5fbebd52019-02-20 11:16:19 +0100306 return kAudioSendSsrc;
307}
308
309uint32_t CallClient::GetNextAudioLocalSsrc() {
310 RTC_CHECK_LT(next_audio_local_ssrc_index_, 1);
311 next_audio_local_ssrc_index_++;
312 return kReceiverLocalAudioSsrc;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200313}
314
315uint32_t CallClient::GetNextRtxSsrc() {
Sebastian Jansson5fbebd52019-02-20 11:16:19 +0100316 RTC_CHECK_LT(next_rtx_ssrc_index_, kNumSsrcs);
317 return kSendRtxSsrcs[next_rtx_ssrc_index_++];
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200318}
319
320std::string CallClient::GetNextPriorityId() {
321 RTC_CHECK_LT(next_priority_index_++, 1);
322 return kPriorityStreamId;
323}
324
Sebastian Janssonfd201712018-11-12 16:44:16 +0100325void CallClient::AddExtensions(std::vector<RtpExtension> extensions) {
326 for (const auto& extension : extensions)
327 header_parser_->RegisterRtpHeaderExtension(extension);
328}
329
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200330void CallClient::SendTask(std::function<void()> task) {
331 time_controller_->InvokeWithControlledYield(
332 [&] { task_queue_.SendTask(std::move(task)); });
333}
334
Sebastian Jansson800e1212018-10-22 11:49:03 +0200335CallClientPair::~CallClientPair() = default;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200336
337} // namespace test
338} // namespace webrtc