blob: fe77e7aabccf6aff9b359378c048ca16bfa8a200 [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}
135NetworkControlUpdate NetworkControleUpdateCache::update_state() const {
136 return update_state_;
137}
138NetworkControlUpdate NetworkControleUpdateCache::Update(
139 NetworkControlUpdate update) {
140 if (update.target_rate)
141 update_state_.target_rate = update.target_rate;
142 if (update.pacer_config)
143 update_state_.pacer_config = update.pacer_config;
144 if (update.congestion_window)
145 update_state_.congestion_window = update.congestion_window;
146 if (!update.probe_cluster_configs.empty())
147 update_state_.probe_cluster_configs = update.probe_cluster_configs;
148 return update;
149}
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200150
151LoggingNetworkControllerFactory::LoggingNetworkControllerFactory(
Sebastian Jansson52de8b02019-01-16 17:25:44 +0100152 LogWriterFactoryInterface* log_writer_factory,
Sebastian Jansson7ccaf892019-04-24 15:13:26 +0200153 TransportControllerConfig config) {
154 if (config.cc_factory) {
155 cc_factory_ = config.cc_factory;
156 if (log_writer_factory)
157 RTC_LOG(LS_WARNING)
158 << "Can't log controller state for injected network controllers";
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200159 } else {
Sebastian Jansson7ccaf892019-04-24 15:13:26 +0200160 if (log_writer_factory) {
Sebastian Jansson871ac422019-05-17 17:53:44 +0200161 goog_cc_factory_.AttachWriter(
162 log_writer_factory->Create(".cc_state.txt"));
163 print_cc_state_ = true;
Sebastian Jansson7ccaf892019-04-24 15:13:26 +0200164 }
Sebastian Jansson871ac422019-05-17 17:53:44 +0200165 cc_factory_ = &goog_cc_factory_;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200166 }
167}
168
169LoggingNetworkControllerFactory::~LoggingNetworkControllerFactory() {
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200170}
171
172void LoggingNetworkControllerFactory::LogCongestionControllerStats(
173 Timestamp at_time) {
Sebastian Jansson871ac422019-05-17 17:53:44 +0200174 if (print_cc_state_)
175 goog_cc_factory_.PrintState(at_time);
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200176}
177
Sebastian Janssona7d70ab2019-06-11 10:21:32 +0200178NetworkControlUpdate LoggingNetworkControllerFactory::GetUpdate() const {
179 if (last_controller_)
180 return last_controller_->update_state();
181 return NetworkControlUpdate();
182}
183
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200184std::unique_ptr<NetworkControllerInterface>
185LoggingNetworkControllerFactory::Create(NetworkControllerConfig config) {
Sebastian Janssona7d70ab2019-06-11 10:21:32 +0200186 auto controller = absl::make_unique<NetworkControleUpdateCache>(
187 cc_factory_->Create(config));
188 last_controller_ = controller.get();
189 return controller;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200190}
191
192TimeDelta LoggingNetworkControllerFactory::GetProcessInterval() const {
193 return cc_factory_->GetProcessInterval();
194}
195
Sebastian Jansson52de8b02019-01-16 17:25:44 +0100196CallClient::CallClient(
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200197 TimeController* time_controller,
Sebastian Jansson52de8b02019-01-16 17:25:44 +0100198 std::unique_ptr<LogWriterFactoryInterface> log_writer_factory,
199 CallClientConfig config)
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200200 : time_controller_(time_controller),
201 clock_(time_controller->GetClock()),
Sebastian Jansson52de8b02019-01-16 17:25:44 +0100202 log_writer_factory_(std::move(log_writer_factory)),
Sebastian Jansson7ccaf892019-04-24 15:13:26 +0200203 network_controller_factory_(log_writer_factory_.get(), config.transport),
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200204 header_parser_(RtpHeaderParser::Create()),
205 task_queue_(time_controller->GetTaskQueueFactory()->CreateTaskQueue(
206 "CallClient",
207 TaskQueueFactory::Priority::NORMAL)) {
208 SendTask([this, config] {
Sebastian Jansson7ccaf892019-04-24 15:13:26 +0200209 event_log_ = CreateEventLog(time_controller_->GetTaskQueueFactory(),
210 log_writer_factory_.get());
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200211 fake_audio_setup_ = InitAudio(time_controller_);
Sebastian Jansson7ccaf892019-04-24 15:13:26 +0200212 call_.reset(CreateCall(time_controller_, event_log_.get(), config,
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200213 &network_controller_factory_,
214 fake_audio_setup_.audio_state));
215 transport_ = absl::make_unique<NetworkNodeTransport>(clock_, call_.get());
216 });
217}
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200218
Sebastian Jansson800e1212018-10-22 11:49:03 +0200219CallClient::~CallClient() {
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200220 SendTask([&] {
221 call_.reset();
222 fake_audio_setup_ = {};
Sebastian Jansson58c71db2019-05-22 16:20:56 +0200223 rtc::Event done;
224 event_log_->StopLogging([&done] { done.Set(); });
225 done.Wait(rtc::Event::kForever);
Sebastian Jansson7ccaf892019-04-24 15:13:26 +0200226 event_log_.reset();
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200227 });
Sebastian Jansson800e1212018-10-22 11:49:03 +0200228}
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200229
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200230ColumnPrinter CallClient::StatsPrinter() {
231 return ColumnPrinter::Lambda(
232 "pacer_delay call_send_bw",
233 [this](rtc::SimpleStringBuilder& sb) {
234 Call::Stats call_stats = call_->GetStats();
235 sb.AppendFormat("%.3lf %.0lf", call_stats.pacer_delay_ms / 1000.0,
236 call_stats.send_bandwidth_bps / 8.0);
237 },
238 64);
239}
240
241Call::Stats CallClient::GetStats() {
242 return call_->GetStats();
243}
244
Sebastian Janssona7d70ab2019-06-11 10:21:32 +0200245DataRate CallClient::target_rate() const {
246 return network_controller_factory_.GetUpdate().target_rate->target_rate;
247}
248
249DataRate CallClient::link_capacity() const {
250 return network_controller_factory_.GetUpdate()
251 .target_rate->network_estimate.bandwidth;
252}
253
254DataRate CallClient::padding_rate() const {
255 return network_controller_factory_.GetUpdate().pacer_config->pad_rate();
256}
257
Artem Titov40f51152019-01-04 15:45:01 +0100258void CallClient::OnPacketReceived(EmulatedIpPacket packet) {
Sebastian Jansson800e1212018-10-22 11:49:03 +0200259 // Removes added overhead before delivering packet to sender.
Artem Titov4cd433e2019-04-01 11:01:16 +0200260 size_t size =
261 packet.data.size() - route_overhead_.at(packet.to.ipaddr()).bytes();
262 RTC_DCHECK_GE(size, 0);
263 packet.data.SetSize(size);
Sebastian Jansson800e1212018-10-22 11:49:03 +0200264
265 MediaType media_type = MediaType::ANY;
Artem Titov40f51152019-01-04 15:45:01 +0100266 if (!RtpHeaderParser::IsRtcp(packet.cdata(), packet.data.size())) {
Sebastian Jansson1e427612019-03-05 14:25:03 +0100267 auto ssrc = RtpHeaderParser::GetSsrc(packet.cdata(), packet.data.size());
268 RTC_CHECK(ssrc.has_value());
269 media_type = ssrc_media_types_[*ssrc];
Sebastian Jansson800e1212018-10-22 11:49:03 +0200270 }
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200271 struct Closure {
272 void operator()() {
273 call->Receiver()->DeliverPacket(media_type, packet.data,
274 packet.arrival_time.us());
275 }
276 Call* call;
277 MediaType media_type;
278 EmulatedIpPacket packet;
279 };
280 task_queue_.PostTask(Closure{call_.get(), media_type, std::move(packet)});
Sebastian Jansson800e1212018-10-22 11:49:03 +0200281}
282
Sebastian Jansson52de8b02019-01-16 17:25:44 +0100283std::unique_ptr<RtcEventLogOutput> CallClient::GetLogWriter(std::string name) {
284 if (!log_writer_factory_ || name.empty())
285 return nullptr;
286 return log_writer_factory_->Create(name);
287}
288
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200289uint32_t CallClient::GetNextVideoSsrc() {
Sebastian Jansson5fbebd52019-02-20 11:16:19 +0100290 RTC_CHECK_LT(next_video_ssrc_index_, kNumSsrcs);
291 return kVideoSendSsrcs[next_video_ssrc_index_++];
292}
293
294uint32_t CallClient::GetNextVideoLocalSsrc() {
295 RTC_CHECK_LT(next_video_local_ssrc_index_, kNumSsrcs);
296 return kVideoRecvLocalSsrcs[next_video_local_ssrc_index_++];
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200297}
298
299uint32_t CallClient::GetNextAudioSsrc() {
300 RTC_CHECK_LT(next_audio_ssrc_index_, 1);
301 next_audio_ssrc_index_++;
Sebastian Jansson5fbebd52019-02-20 11:16:19 +0100302 return kAudioSendSsrc;
303}
304
305uint32_t CallClient::GetNextAudioLocalSsrc() {
306 RTC_CHECK_LT(next_audio_local_ssrc_index_, 1);
307 next_audio_local_ssrc_index_++;
308 return kReceiverLocalAudioSsrc;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200309}
310
311uint32_t CallClient::GetNextRtxSsrc() {
Sebastian Jansson5fbebd52019-02-20 11:16:19 +0100312 RTC_CHECK_LT(next_rtx_ssrc_index_, kNumSsrcs);
313 return kSendRtxSsrcs[next_rtx_ssrc_index_++];
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200314}
315
316std::string CallClient::GetNextPriorityId() {
317 RTC_CHECK_LT(next_priority_index_++, 1);
318 return kPriorityStreamId;
319}
320
Sebastian Janssonfd201712018-11-12 16:44:16 +0100321void CallClient::AddExtensions(std::vector<RtpExtension> extensions) {
322 for (const auto& extension : extensions)
323 header_parser_->RegisterRtpHeaderExtension(extension);
324}
325
Sebastian Jansson105a10a2019-04-01 09:18:14 +0200326void CallClient::SendTask(std::function<void()> task) {
327 time_controller_->InvokeWithControlledYield(
328 [&] { task_queue_.SendTask(std::move(task)); });
329}
330
Sebastian Jansson800e1212018-10-22 11:49:03 +0200331CallClientPair::~CallClientPair() = default;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200332
333} // namespace test
334} // namespace webrtc