blob: 7a77a9ef01efce0c05f6e8819f54d36dfb0b7ab4 [file] [log] [blame]
Seth Hampsond1003d72018-06-22 15:40:16 -07001/*
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
11#include "api/audio_codecs/builtin_audio_decoder_factory.h"
12#include "api/audio_codecs/builtin_audio_encoder_factory.h"
13#include "api/stats/rtcstats_objects.h"
14#include "api/test/fakeconstraints.h"
15#include "api/video_codecs/builtin_video_decoder_factory.h"
16#include "api/video_codecs/builtin_video_encoder_factory.h"
17#include "p2p/base/testturnserver.h"
18#include "p2p/client/basicportallocator.h"
19#include "pc/peerconnection.h"
20#include "pc/peerconnectionwrapper.h"
21#include "pc/test/fakeaudiocapturemodule.h"
22#include "pc/test/fakeperiodicvideotracksource.h"
Seth Hampsonec207102018-06-29 11:12:19 -070023#include "pc/test/fakertccertificategenerator.h"
Seth Hampsond1003d72018-06-22 15:40:16 -070024#include "pc/test/fakevideotrackrenderer.h"
25#include "pc/test/framegeneratorcapturervideotracksource.h"
26#include "rtc_base/fakenetwork.h"
27#include "rtc_base/firewallsocketserver.h"
28#include "rtc_base/gunit.h"
29#include "rtc_base/platform_thread.h"
30#include "rtc_base/socketaddress.h"
Seth Hampsonec207102018-06-29 11:12:19 -070031#include "rtc_base/testcertificateverifier.h"
Seth Hampsond1003d72018-06-22 15:40:16 -070032#include "rtc_base/virtualsocketserver.h"
33#include "test/gtest.h"
34#include "test/testsupport/perf_test.h"
35
36namespace webrtc {
37
38namespace {
39static const int kDefaultTestTimeMs = 15000;
40static const int kRampUpTimeMs = 5000;
41static const int kPollIntervalTimeMs = 50;
42static const int kDefaultTimeoutMs = 10000;
43static const rtc::SocketAddress kDefaultLocalAddress("1.1.1.1", 0);
Seth Hampsonec207102018-06-29 11:12:19 -070044static const char kTurnInternalAddress[] = "88.88.88.0";
45static const char kTurnExternalAddress[] = "88.88.88.1";
46static const int kTurnInternalPort = 3478;
47static const int kTurnExternalPort = 0;
Seth Hampsond1003d72018-06-22 15:40:16 -070048// The video's configured max bitrate in webrtcvideoengine.cc is 1.7 Mbps.
49// Setting the network bandwidth to 1 Mbps allows the video's bitrate to push
50// the network's limitations.
51static const int kNetworkBandwidth = 1000000;
52} // namespace
53
54using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
55
56// This is an end to end test to verify that BWE is functioning when setting
57// up a one to one call at the PeerConnection level. The intention of the test
58// is to catch potential regressions for different ICE path configurations. The
59// test uses a VirtualSocketServer for it's underlying simulated network and
60// fake audio and video sources. The test is based upon rampup_tests.cc, but
61// instead is at the PeerConnection level and uses a different fake network
62// (rampup_tests.cc uses SimulatedNetwork). In the future, this test could
63// potentially test different network conditions and test video quality as well
64// (video_quality_test.cc does this, but at the call level).
65//
66// The perf test results are printed using the perf test support. If the
67// isolated_script_test_perf_output flag is specified in test_main.cc, then
68// the results are written to a JSON formatted file for the Chrome perf
69// dashboard. Since this test is a webrtc_perf_test, it will be run in the perf
70// console every webrtc commit.
71class PeerConnectionWrapperForRampUpTest : public PeerConnectionWrapper {
72 public:
73 using PeerConnectionWrapper::PeerConnectionWrapper;
74
75 PeerConnectionWrapperForRampUpTest(
76 rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory,
77 rtc::scoped_refptr<PeerConnectionInterface> pc,
Seth Hampsonec207102018-06-29 11:12:19 -070078 std::unique_ptr<MockPeerConnectionObserver> observer)
Seth Hampsond1003d72018-06-22 15:40:16 -070079 : PeerConnectionWrapper::PeerConnectionWrapper(pc_factory,
80 pc,
Seth Hampsonec207102018-06-29 11:12:19 -070081 std::move(observer)) {}
Seth Hampsond1003d72018-06-22 15:40:16 -070082
83 bool AddIceCandidates(std::vector<const IceCandidateInterface*> candidates) {
84 bool success = true;
85 for (const auto candidate : candidates) {
86 if (!pc()->AddIceCandidate(candidate)) {
87 success = false;
88 }
89 }
90 return success;
91 }
92
93 rtc::scoped_refptr<VideoTrackInterface> CreateLocalVideoTrack(
94 FrameGeneratorCapturerVideoTrackSource::Config config,
95 Clock* clock) {
96 video_track_sources_.emplace_back(
97 new rtc::RefCountedObject<FrameGeneratorCapturerVideoTrackSource>(
98 config, clock));
99 video_track_sources_.back()->Start();
100 return rtc::scoped_refptr<VideoTrackInterface>(
101 pc_factory()->CreateVideoTrack(rtc::CreateRandomUuid(),
102 video_track_sources_.back()));
103 }
104
105 rtc::scoped_refptr<AudioTrackInterface> CreateLocalAudioTrack(
106 const cricket::AudioOptions options) {
107 rtc::scoped_refptr<AudioSourceInterface> source =
108 pc_factory()->CreateAudioSource(options);
109 return pc_factory()->CreateAudioTrack(rtc::CreateRandomUuid(), source);
110 }
111
112 private:
Seth Hampsond1003d72018-06-22 15:40:16 -0700113 std::vector<rtc::scoped_refptr<FrameGeneratorCapturerVideoTrackSource>>
114 video_track_sources_;
115};
116
117// TODO(shampson): Paramaterize the test to run for both Plan B & Unified Plan.
118class PeerConnectionRampUpTest : public ::testing::Test {
119 public:
120 PeerConnectionRampUpTest()
121 : clock_(Clock::GetRealTimeClock()),
122 virtual_socket_server_(new rtc::VirtualSocketServer()),
123 firewall_socket_server_(
124 new rtc::FirewallSocketServer(virtual_socket_server_.get())),
125 network_thread_(new rtc::Thread(firewall_socket_server_.get())),
126 worker_thread_(rtc::Thread::Create()) {
127 network_thread_->SetName("PCNetworkThread", this);
128 worker_thread_->SetName("PCWorkerThread", this);
129 RTC_CHECK(network_thread_->Start());
130 RTC_CHECK(worker_thread_->Start());
131
132 virtual_socket_server_->set_bandwidth(kNetworkBandwidth / 8);
133 pc_factory_ = CreatePeerConnectionFactory(
134 network_thread_.get(), worker_thread_.get(), rtc::Thread::Current(),
135 rtc::scoped_refptr<AudioDeviceModule>(FakeAudioCaptureModule::Create()),
136 CreateBuiltinAudioEncoderFactory(), CreateBuiltinAudioDecoderFactory(),
137 CreateBuiltinVideoEncoderFactory(), CreateBuiltinVideoDecoderFactory(),
138 nullptr /* audio_mixer */, nullptr /* audio_processing */);
139 }
140
141 virtual ~PeerConnectionRampUpTest() {
142 network_thread()->Invoke<void>(RTC_FROM_HERE,
143 [this] { turn_servers_.clear(); });
144 }
145
146 bool CreatePeerConnectionWrappers(const RTCConfiguration& caller_config,
147 const RTCConfiguration& callee_config) {
148 caller_ = CreatePeerConnectionWrapper(caller_config);
149 callee_ = CreatePeerConnectionWrapper(callee_config);
150 return caller_ && callee_;
151 }
152
153 std::unique_ptr<PeerConnectionWrapperForRampUpTest>
154 CreatePeerConnectionWrapper(const RTCConfiguration& config) {
155 auto* fake_network_manager = new rtc::FakeNetworkManager();
156 fake_network_manager->AddInterface(kDefaultLocalAddress);
157 fake_network_managers_.emplace_back(fake_network_manager);
Seth Hampsond1003d72018-06-22 15:40:16 -0700158
Seth Hampsond1003d72018-06-22 15:40:16 -0700159 auto observer = rtc::MakeUnique<MockPeerConnectionObserver>();
Seth Hampsonec207102018-06-29 11:12:19 -0700160 webrtc::PeerConnectionDependencies dependencies(observer.get());
161 cricket::BasicPortAllocator* port_allocator =
162 new cricket::BasicPortAllocator(fake_network_manager);
163 port_allocator->set_step_delay(cricket::kDefaultStepDelay);
164 dependencies.allocator =
165 std::unique_ptr<cricket::BasicPortAllocator>(port_allocator);
166 dependencies.tls_cert_verifier =
167 rtc::MakeUnique<rtc::TestCertificateVerifier>();
168
169 auto pc =
170 pc_factory_->CreatePeerConnection(config, std::move(dependencies));
Seth Hampsond1003d72018-06-22 15:40:16 -0700171 if (!pc) {
172 return nullptr;
173 }
174
175 return rtc::MakeUnique<PeerConnectionWrapperForRampUpTest>(
Seth Hampsonec207102018-06-29 11:12:19 -0700176 pc_factory_, pc, std::move(observer));
Seth Hampsond1003d72018-06-22 15:40:16 -0700177 }
178
179 void SetupOneWayCall() {
180 ASSERT_TRUE(caller_);
181 ASSERT_TRUE(callee_);
182 FrameGeneratorCapturerVideoTrackSource::Config config;
183 caller_->AddTrack(caller_->CreateLocalVideoTrack(config, clock_));
184 // Disable highpass filter so that we can get all the test audio frames.
185 cricket::AudioOptions options;
186 options.highpass_filter = false;
187 caller_->AddTrack(caller_->CreateLocalAudioTrack(options));
188
189 // Do the SDP negotiation, and also exchange ice candidates.
190 ASSERT_TRUE(caller_->ExchangeOfferAnswerWith(callee_.get()));
191 ASSERT_TRUE_WAIT(
192 caller_->signaling_state() == PeerConnectionInterface::kStable,
193 kDefaultTimeoutMs);
194 ASSERT_TRUE_WAIT(caller_->IsIceGatheringDone(), kDefaultTimeoutMs);
195 ASSERT_TRUE_WAIT(callee_->IsIceGatheringDone(), kDefaultTimeoutMs);
196
197 // Connect an ICE candidate pairs.
198 ASSERT_TRUE(
199 callee_->AddIceCandidates(caller_->observer()->GetAllCandidates()));
200 ASSERT_TRUE(
201 caller_->AddIceCandidates(callee_->observer()->GetAllCandidates()));
202 // This means that ICE and DTLS are connected.
203 ASSERT_TRUE_WAIT(callee_->IsIceConnected(), kDefaultTimeoutMs);
204 ASSERT_TRUE_WAIT(caller_->IsIceConnected(), kDefaultTimeoutMs);
205 }
206
Seth Hampsonec207102018-06-29 11:12:19 -0700207 void CreateTurnServer(cricket::ProtocolType type,
208 const std::string& common_name = "test turn server") {
Seth Hampsond1003d72018-06-22 15:40:16 -0700209 rtc::Thread* thread = network_thread();
210 std::unique_ptr<cricket::TestTurnServer> turn_server =
211 network_thread_->Invoke<std::unique_ptr<cricket::TestTurnServer>>(
Seth Hampsonec207102018-06-29 11:12:19 -0700212 RTC_FROM_HERE, [thread, type, common_name] {
Seth Hampsond1003d72018-06-22 15:40:16 -0700213 static const rtc::SocketAddress turn_server_internal_address{
Seth Hampsonec207102018-06-29 11:12:19 -0700214 kTurnInternalAddress, kTurnInternalPort};
Seth Hampsond1003d72018-06-22 15:40:16 -0700215 static const rtc::SocketAddress turn_server_external_address{
Seth Hampsonec207102018-06-29 11:12:19 -0700216 kTurnExternalAddress, kTurnExternalPort};
Seth Hampsond1003d72018-06-22 15:40:16 -0700217 return rtc::MakeUnique<cricket::TestTurnServer>(
218 thread, turn_server_internal_address,
Seth Hampsonec207102018-06-29 11:12:19 -0700219 turn_server_external_address, type,
220 true /*ignore_bad_certs=*/, common_name);
Seth Hampsond1003d72018-06-22 15:40:16 -0700221 });
222 turn_servers_.push_back(std::move(turn_server));
223 }
224
225 // First runs the call for kRampUpTimeMs to ramp up the bandwidth estimate.
226 // Then runs the test for the remaining test time, grabbing the bandwidth
227 // estimation stat, every kPollIntervalTimeMs. When finished, averages the
228 // bandwidth estimations and prints the bandwidth estimation result as a perf
229 // metric.
230 void RunTest(const std::string& test_string) {
231 rtc::Thread::Current()->ProcessMessages(kRampUpTimeMs);
232 int number_of_polls =
233 (kDefaultTestTimeMs - kRampUpTimeMs) / kPollIntervalTimeMs;
234 int total_bwe = 0;
235 for (int i = 0; i < number_of_polls; ++i) {
236 rtc::Thread::Current()->ProcessMessages(kPollIntervalTimeMs);
237 total_bwe += static_cast<int>(GetCallerAvailableBitrateEstimate());
238 }
239 double average_bandwidth_estimate = total_bwe / number_of_polls;
240 std::string value_description =
241 "bwe_after_" + std::to_string(kDefaultTestTimeMs / 1000) + "_seconds";
242 test::PrintResult("peerconnection_ramp_up_", test_string, value_description,
243 average_bandwidth_estimate, "bwe", false);
244 }
245
246 rtc::Thread* network_thread() { return network_thread_.get(); }
247
Seth Hampsonec207102018-06-29 11:12:19 -0700248 rtc::FirewallSocketServer* firewall_socket_server() {
249 return firewall_socket_server_.get();
250 }
251
Seth Hampsond1003d72018-06-22 15:40:16 -0700252 PeerConnectionWrapperForRampUpTest* caller() { return caller_.get(); }
253
254 PeerConnectionWrapperForRampUpTest* callee() { return callee_.get(); }
255
256 private:
257 // Gets the caller's outgoing available bitrate from the stats. Returns 0 if
258 // something went wrong. It takes the outgoing bitrate from the current
259 // selected ICE candidate pair's stats.
260 double GetCallerAvailableBitrateEstimate() {
261 auto stats = caller_->GetStats();
262 auto transport_stats = stats->GetStatsOfType<RTCTransportStats>();
263 if (transport_stats.size() == 0u ||
264 !transport_stats[0]->selected_candidate_pair_id.is_defined()) {
265 return 0;
266 }
267 std::string selected_ice_id =
268 transport_stats[0]->selected_candidate_pair_id.ValueToString();
269 // Use the selected ICE candidate pair ID to get the appropriate ICE stats.
270 const RTCIceCandidatePairStats ice_candidate_pair_stats =
271 stats->Get(selected_ice_id)->cast_to<const RTCIceCandidatePairStats>();
272 if (ice_candidate_pair_stats.available_outgoing_bitrate.is_defined()) {
273 return *ice_candidate_pair_stats.available_outgoing_bitrate;
274 }
275 // We couldn't get the |available_outgoing_bitrate| for the active candidate
276 // pair.
277 return 0;
278 }
279
280 Clock* const clock_;
281 // The turn servers should be accessed & deleted on the network thread to
282 // avoid a race with the socket read/write which occurs on the network thread.
283 std::vector<std::unique_ptr<cricket::TestTurnServer>> turn_servers_;
284 // |virtual_socket_server_| is used by |network_thread_| so it must be
285 // destroyed later.
286 // TODO(bugs.webrtc.org/7668): We would like to update the virtual network we
287 // use for this test. VirtualSocketServer isn't ideal because:
288 // 1) It uses the same queue & network capacity for both directions.
289 // 2) VirtualSocketServer implements how the network bandwidth affects the
290 // send delay differently than the SimulatedNetwork, used by the
291 // FakeNetworkPipe. It would be ideal if all of levels of virtual
292 // networks used in testing were consistent.
293 // We would also like to update this test to record the time to ramp up,
294 // down, and back up (similar to in rampup_tests.cc). This is problematic with
295 // the VirtualSocketServer. The first ramp down time is very noisy and the
296 // second ramp up time can take up to 300 seconds, most likely due to a built
297 // up queue.
298 std::unique_ptr<rtc::VirtualSocketServer> virtual_socket_server_;
299 std::unique_ptr<rtc::FirewallSocketServer> firewall_socket_server_;
300 std::unique_ptr<rtc::Thread> network_thread_;
301 std::unique_ptr<rtc::Thread> worker_thread_;
302 // The |pc_factory| uses |network_thread_| & |worker_thread_|, so it must be
303 // destroyed first.
304 std::vector<std::unique_ptr<rtc::FakeNetworkManager>> fake_network_managers_;
305 rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_;
306 std::unique_ptr<PeerConnectionWrapperForRampUpTest> caller_;
307 std::unique_ptr<PeerConnectionWrapperForRampUpTest> callee_;
308};
309
310TEST_F(PeerConnectionRampUpTest, TurnOverTCP) {
311 CreateTurnServer(cricket::ProtocolType::PROTO_TCP);
312 PeerConnectionInterface::IceServer ice_server;
Seth Hampsonec207102018-06-29 11:12:19 -0700313 std::string ice_server_url = "turn:" + std::string(kTurnInternalAddress) +
314 ":" + std::to_string(kTurnInternalPort) +
315 "?transport=tcp";
316 ice_server.urls.push_back(ice_server_url);
Seth Hampsond1003d72018-06-22 15:40:16 -0700317 ice_server.username = "test";
318 ice_server.password = "test";
319 PeerConnectionInterface::RTCConfiguration client_1_config;
320 client_1_config.servers.push_back(ice_server);
321 client_1_config.type = PeerConnectionInterface::kRelay;
322 PeerConnectionInterface::RTCConfiguration client_2_config;
323 client_2_config.servers.push_back(ice_server);
324 client_2_config.type = PeerConnectionInterface::kRelay;
325 ASSERT_TRUE(CreatePeerConnectionWrappers(client_1_config, client_2_config));
326
327 SetupOneWayCall();
328 RunTest("turn_over_tcp");
329}
330
Seth Hampsonec207102018-06-29 11:12:19 -0700331TEST_F(PeerConnectionRampUpTest, TurnOverUDP) {
332 CreateTurnServer(cricket::ProtocolType::PROTO_UDP);
333 PeerConnectionInterface::IceServer ice_server;
334 std::string ice_server_url = "turn:" + std::string(kTurnInternalAddress) +
335 ":" + std::to_string(kTurnInternalPort);
336
337 ice_server.urls.push_back(ice_server_url);
338 ice_server.username = "test";
339 ice_server.password = "test";
340 PeerConnectionInterface::RTCConfiguration client_1_config;
341 client_1_config.servers.push_back(ice_server);
342 client_1_config.type = PeerConnectionInterface::kRelay;
343 PeerConnectionInterface::RTCConfiguration client_2_config;
344 client_2_config.servers.push_back(ice_server);
345 client_2_config.type = PeerConnectionInterface::kRelay;
346 ASSERT_TRUE(CreatePeerConnectionWrappers(client_1_config, client_2_config));
347
348 SetupOneWayCall();
349 RunTest("turn_over_udp");
350}
351
352TEST_F(PeerConnectionRampUpTest, TurnOverTLS) {
353 CreateTurnServer(cricket::ProtocolType::PROTO_TLS, kTurnInternalAddress);
354 PeerConnectionInterface::IceServer ice_server;
355 std::string ice_server_url = "turns:" + std::string(kTurnInternalAddress) +
356 ":" + std::to_string(kTurnInternalPort) +
357 "?transport=tcp";
358 ice_server.urls.push_back(ice_server_url);
359 ice_server.username = "test";
360 ice_server.password = "test";
361 PeerConnectionInterface::RTCConfiguration client_1_config;
362 client_1_config.servers.push_back(ice_server);
363 client_1_config.type = PeerConnectionInterface::kRelay;
364 PeerConnectionInterface::RTCConfiguration client_2_config;
365 client_2_config.servers.push_back(ice_server);
366 client_2_config.type = PeerConnectionInterface::kRelay;
367
368 ASSERT_TRUE(CreatePeerConnectionWrappers(client_1_config, client_2_config));
369
370 SetupOneWayCall();
371 RunTest("turn_over_tls");
372}
373
374TEST_F(PeerConnectionRampUpTest, UDPPeerToPeer) {
375 PeerConnectionInterface::RTCConfiguration client_1_config;
376 client_1_config.tcp_candidate_policy =
377 PeerConnection::kTcpCandidatePolicyDisabled;
378 PeerConnectionInterface::RTCConfiguration client_2_config;
379 client_2_config.tcp_candidate_policy =
380 PeerConnection::kTcpCandidatePolicyDisabled;
381 ASSERT_TRUE(CreatePeerConnectionWrappers(client_1_config, client_2_config));
382
383 SetupOneWayCall();
384 RunTest("udp_peer_to_peer");
385}
386
387TEST_F(PeerConnectionRampUpTest, TCPPeerToPeer) {
388 firewall_socket_server()->set_udp_sockets_enabled(false);
389 ASSERT_TRUE(CreatePeerConnectionWrappers(
390 PeerConnectionInterface::RTCConfiguration(),
391 PeerConnectionInterface::RTCConfiguration()));
392
393 SetupOneWayCall();
394 RunTest("tcp_peer_to_peer");
395}
Seth Hampsond1003d72018-06-22 15:40:16 -0700396
397} // namespace webrtc